LCOV - code coverage report
Current view: top level - gcc/cp - pt.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.2 % 15226 14501
Test Date: 2026-05-11 19:44:49 Functions: 98.7 % 479 473
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Handle parameterized types (templates) for GNU -*- C++ -*-.
       2              :    Copyright (C) 1992-2026 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              : #include "builtins.h"
      50              : #include "omp-general.h"
      51              : #include "pretty-print-markup.h"
      52              : #include "contracts.h"
      53              : 
      54              : /* The type of functions taking a tree, and some additional data, and
      55              :    returning an int.  */
      56              : typedef int (*tree_fn_t) (tree, void*);
      57              : 
      58              : /* The PENDING_TEMPLATES is a list of templates whose instantiations
      59              :    have been deferred, either because their definitions were not yet
      60              :    available, or because we were putting off doing the work.  */
      61              : struct GTY ((chain_next ("%h.next"))) pending_template
      62              : {
      63              :   struct pending_template *next;
      64              :   struct tinst_level *tinst;
      65              : };
      66              : 
      67              : static GTY(()) struct pending_template *pending_templates;
      68              : static GTY(()) struct pending_template *last_pending_template;
      69              : 
      70              : int processing_template_parmlist;
      71              : static int template_header_count;
      72              : 
      73              : static vec<int> inline_parm_levels;
      74              : 
      75              : static GTY(()) struct tinst_level *current_tinst_level;
      76              : 
      77              : static GTY(()) vec<tree, va_gc> *saved_access_scope;
      78              : 
      79              : /* Live only within one (recursive) call to tsubst_expr.  We use
      80              :    this to pass the statement expression node from the STMT_EXPR
      81              :    to the EXPR_STMT that is its result.  */
      82              : static tree cur_stmt_expr;
      83              : 
      84              : // -------------------------------------------------------------------------- //
      85              : // Local Specialization Stack
      86              : //
      87              : // Implementation of the RAII helper for creating new local
      88              : // specializations.
      89     96313868 : local_specialization_stack::local_specialization_stack (lss_policy policy)
      90     96313868 :   : saved (local_specializations)
      91              : {
      92     96313868 :   if (policy == lss_nop)
      93              :     ;
      94     34864949 :   else if (policy == lss_blank || !saved)
      95     32218259 :     local_specializations = new hash_map<tree, tree>;
      96              :   else
      97      2646690 :     local_specializations = new hash_map<tree, tree>(*saved);
      98     96313868 : }
      99              : 
     100     96308459 : local_specialization_stack::~local_specialization_stack ()
     101              : {
     102     96308459 :   if (local_specializations != saved)
     103              :     {
     104     69724480 :       delete local_specializations;
     105     34862240 :       local_specializations = saved;
     106              :     }
     107     96308459 : }
     108              : 
     109              : /* True if we've recursed into fn_type_unification too many times.  */
     110              : static bool excessive_deduction_depth;
     111              : 
     112              : struct spec_hasher : ggc_ptr_hash<spec_entry>
     113              : {
     114              :   static hashval_t hash (tree, tree);
     115              :   static hashval_t hash (spec_entry *);
     116              :   static bool equal (spec_entry *, spec_entry *);
     117              : };
     118              : 
     119              : /* The general template is not in these tables.  */
     120              : typedef hash_table<spec_hasher> spec_hash_table;
     121              : static GTY (()) spec_hash_table *decl_specializations;
     122              : static GTY (()) spec_hash_table *type_specializations;
     123              : 
     124              : /* Contains canonical template parameter types. The vector is indexed by
     125              :    the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
     126              :    TREE_LIST, whose TREE_VALUEs contain the canonical template
     127              :    parameters of various types and levels.  */
     128              : static GTY(()) vec<tree, va_gc> *canonical_template_parms;
     129              : 
     130              : #define UNIFY_ALLOW_NONE 0
     131              : #define UNIFY_ALLOW_MORE_CV_QUAL 1
     132              : #define UNIFY_ALLOW_LESS_CV_QUAL 2
     133              : #define UNIFY_ALLOW_DERIVED 4
     134              : #define UNIFY_ALLOW_INTEGER 8
     135              : #define UNIFY_ALLOW_OUTER_LEVEL 16
     136              : #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
     137              : #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
     138              : 
     139              : enum template_base_result {
     140              :   tbr_incomplete_type,
     141              :   tbr_ambiguous_baseclass,
     142              :   tbr_success
     143              : };
     144              : 
     145              : static bool resolve_overloaded_unification (tree, tree, tree, tree,
     146              :                                             unification_kind_t, int,
     147              :                                             bool);
     148              : static int try_one_overload (tree, tree, tree, tree, tree,
     149              :                              unification_kind_t, int, bool, bool);
     150              : static int unify (tree, tree, tree, tree, int, bool);
     151              : static void add_pending_template (tree);
     152              : static tree reopen_tinst_level (struct tinst_level *);
     153              : static tree tsubst_initializer_list (tree, tree);
     154              : static tree get_partial_spec_bindings (tree, tree, tree);
     155              : static void tsubst_enum (tree, tree, tree);
     156              : static bool check_instantiated_args (tree, tree, tsubst_flags_t);
     157              : static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
     158              :                                            struct conversion **, bool, bool);
     159              : static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
     160              :                                              tree*, tree*, tree);
     161              : static int type_unification_real (tree, tree, tree, const tree *,
     162              :                                   unsigned int, int, unification_kind_t,
     163              :                                   vec<deferred_access_check, va_gc> **,
     164              :                                   bool);
     165              : static void note_template_header (int);
     166              : static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
     167              : static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
     168              : static tree convert_template_argument (tree, tree, tree,
     169              :                                        tsubst_flags_t, int, tree);
     170              : static tree for_each_template_parm (tree, tree_fn_t, void*,
     171              :                                     hash_set<tree> *, bool, tree_fn_t = NULL);
     172              : static tree build_template_parm_index (int, int, int, tree, tree);
     173              : static bool inline_needs_template_parms (tree, bool);
     174              : static void push_inline_template_parms_recursive (tree, int);
     175              : static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
     176              : static int mark_template_parm (tree, void *);
     177              : static int template_parm_this_level_p (tree, void *);
     178              : static tree tsubst_friend_function (tree, tree);
     179              : static tree tsubst_friend_class (tree, tree);
     180              : static int can_complete_type_without_circularity (tree);
     181              : static tree get_bindings (tree, tree, tree, bool);
     182              : static int template_decl_level (tree);
     183              : static int check_cv_quals_for_unify (int, tree, tree);
     184              : static int unify_pack_expansion (tree, tree, tree,
     185              :                                  tree, unification_kind_t, bool, bool);
     186              : static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
     187              : static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
     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_type_p_r (tree);
     205              : static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree);
     206              : static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true);
     207              : static tree tsubst_scope (tree, tree, tsubst_flags_t, tree);
     208              : static tree tsubst_name (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 tree get_underlying_template (tree);
     215              : static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
     216              : static tree canonicalize_expr_argument (tree, tsubst_flags_t);
     217              : static tree make_argument_pack (tree);
     218              : static tree enclosing_instantiation_of (tree tctx);
     219              : static void instantiate_body (tree pattern, tree args, tree d, bool nested);
     220              : static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
     221              : static void mark_template_arguments_used (tree, tree);
     222              : static bool uses_outer_template_parms (tree);
     223              : static tree alias_ctad_tweaks (tree, tree);
     224              : static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t);
     225              : static tree deduction_guides_for (tree, bool&, tsubst_flags_t);
     226              : 
     227              : /* Make the current scope suitable for access checking when we are
     228              :    processing T.  T can be FUNCTION_DECL for instantiated function
     229              :    template, VAR_DECL for static member variable, or TYPE_DECL for
     230              :    for a class or alias template (needed by instantiate_decl).  */
     231              : 
     232              : void
     233    252427371 : push_access_scope (tree t)
     234              : {
     235    252427371 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
     236              :               || TREE_CODE (t) == TYPE_DECL);
     237              : 
     238    410567060 :   if (DECL_FRIEND_CONTEXT (t))
     239      7908824 :     push_nested_class (DECL_FRIEND_CONTEXT (t));
     240     65331474 :   else if (DECL_IMPLICIT_TYPEDEF_P (t)
     241    267423301 :            && CLASS_TYPE_P (TREE_TYPE (t)))
     242     18950342 :     push_nested_class (TREE_TYPE (t));
     243    229522617 :   else if (DECL_CLASS_SCOPE_P (t))
     244     46549721 :     push_nested_class (DECL_CONTEXT (t));
     245    182972896 :   else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
     246              :     /* An artificial deduction guide should have the same access as
     247              :        the constructor.  */
     248        48908 :     push_nested_class (TREE_TYPE (TREE_TYPE (t)));
     249              :   else
     250    182923988 :     push_to_top_level ();
     251              : 
     252    252427371 :   if (TREE_CODE (t) == FUNCTION_DECL)
     253              :     {
     254    158467574 :       vec_safe_push (saved_access_scope, current_function_decl);
     255    158467574 :       current_function_decl = t;
     256              :     }
     257    252427371 : }
     258              : 
     259              : /* Restore the scope set up by push_access_scope.  T is the node we
     260              :    are processing.  */
     261              : 
     262              : void
     263    252424671 : pop_access_scope (tree t)
     264              : {
     265    252424671 :   if (TREE_CODE (t) == FUNCTION_DECL)
     266    158464874 :     current_function_decl = saved_access_scope->pop();
     267              : 
     268    410561660 :   if (DECL_FRIEND_CONTEXT (t)
     269    248470259 :       || (DECL_IMPLICIT_TYPEDEF_P (t)
     270     18950342 :           && CLASS_TYPE_P (TREE_TYPE (t)))
     271    229519917 :       || DECL_CLASS_SCOPE_P (t)
     272    326509086 :       || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
     273     69503383 :     pop_nested_class ();
     274              :   else
     275    182921288 :     pop_from_top_level ();
     276    252424671 : }
     277              : 
     278              : /* Return current function, ignoring temporary overrides
     279              :    of current_function_decl by push_access_scope.  */
     280              : 
     281              : tree
     282          138 : current_function_decl_without_access_scope ()
     283              : {
     284          138 :   if (vec_safe_length (saved_access_scope))
     285            1 :     return (*saved_access_scope)[0];
     286              :   else
     287          137 :     return current_function_decl;
     288              : }
     289              : 
     290              : /* Do any processing required when DECL (a member template
     291              :    declaration) is finished.  Returns the TEMPLATE_DECL corresponding
     292              :    to DECL, unless it is a specialization, in which case the DECL
     293              :    itself is returned.  */
     294              : 
     295              : tree
     296     21213695 : finish_member_template_decl (tree decl)
     297              : {
     298     21213695 :   if (decl == error_mark_node)
     299              :     return error_mark_node;
     300              : 
     301     21213613 :   gcc_assert (DECL_P (decl));
     302              : 
     303     21213613 :   if (TREE_CODE (decl) == TYPE_DECL)
     304              :     {
     305      1292539 :       tree type;
     306              : 
     307      1292539 :       type = TREE_TYPE (decl);
     308      1292539 :       if (type == error_mark_node)
     309              :         return error_mark_node;
     310      1292539 :       if (MAYBE_CLASS_TYPE_P (type)
     311      1292539 :           && CLASSTYPE_TEMPLATE_INFO (type)
     312      1292539 :           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     313              :         {
     314       761951 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     315       761951 :           check_member_template (tmpl);
     316       761951 :           return tmpl;
     317              :         }
     318              :       return NULL_TREE;
     319              :     }
     320     19921074 :   else if (TREE_CODE (decl) == FIELD_DECL)
     321            3 :     error_at (DECL_SOURCE_LOCATION (decl),
     322              :               "data member %qD cannot be a member template", decl);
     323     19921071 :   else if (DECL_TEMPLATE_INFO (decl))
     324              :     {
     325     19921068 :       if (!DECL_TEMPLATE_SPECIALIZATION (decl))
     326              :         {
     327     19879570 :           check_member_template (DECL_TI_TEMPLATE (decl));
     328     19879570 :           return DECL_TI_TEMPLATE (decl);
     329              :         }
     330              :       else
     331              :         return NULL_TREE;
     332              :     }
     333              :   else
     334            3 :     error_at (DECL_SOURCE_LOCATION (decl),
     335              :               "invalid member template declaration %qD", decl);
     336              : 
     337            6 :   return error_mark_node;
     338              : }
     339              : 
     340              : /* Create a template info node.  */
     341              : 
     342              : tree
     343    681966218 : build_template_info (tree template_decl, tree template_args)
     344              : {
     345    681966218 :   tree result = make_node (TEMPLATE_INFO);
     346    681966218 :   TI_TEMPLATE (result) = template_decl;
     347    681966218 :   TI_ARGS (result) = template_args;
     348    681966218 :   return result;
     349              : }
     350              : 
     351              : /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE.  */
     352              : 
     353              : static tree
     354   2592282814 : decl_template_info (const_tree decl)
     355              : {
     356              :   /* This needs to match template_info_decl_check.  */
     357   2592282814 :   if (DECL_LANG_SPECIFIC (decl))
     358   1917908083 :     switch (TREE_CODE (decl))
     359              :       {
     360   1501808880 :       case FUNCTION_DECL:
     361   1501808880 :         if (DECL_THUNK_P (decl))
     362              :           break;
     363   1917904377 :         gcc_fallthrough ();
     364   1917904377 :       case VAR_DECL:
     365   1917904377 :       case FIELD_DECL:
     366   1917904377 :       case TYPE_DECL:
     367   1917904377 :       case CONCEPT_DECL:
     368   1917904377 :       case TEMPLATE_DECL:
     369   1917904377 :         return DECL_TEMPLATE_INFO (decl);
     370              : 
     371              :       default:
     372              :         break;
     373              :       }
     374              :   return NULL_TREE;
     375              : }
     376              : 
     377              : /* Return the template info node corresponding to T, whatever T is.  */
     378              : 
     379              : tree
     380   3439362228 : get_template_info (const_tree t)
     381              : {
     382   3439362228 :   tree tinfo = NULL_TREE;
     383              : 
     384   3439362228 :   if (!t || t == error_mark_node)
     385              :     return NULL;
     386              : 
     387   3439362226 :   if (TREE_CODE (t) == NAMESPACE_DECL
     388   3308477951 :       || TREE_CODE (t) == PARM_DECL)
     389              :     return NULL;
     390              : 
     391   3308477802 :   if (DECL_P (t))
     392   2592282814 :     tinfo = decl_template_info (t);
     393              : 
     394   3308477802 :   if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
     395    670258299 :     t = TREE_TYPE (t);
     396              : 
     397   3308477802 :   if (OVERLOAD_TYPE_P (t))
     398   1349865080 :     tinfo = TYPE_TEMPLATE_INFO (t);
     399   1958612722 :   else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
     400           11 :     tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
     401              : 
     402              :   return tinfo;
     403              : }
     404              : 
     405              : /* Returns the template nesting level of the indicated class TYPE.
     406              : 
     407              :    For example, in:
     408              :      template <class T>
     409              :      struct A
     410              :      {
     411              :        template <class U>
     412              :        struct B {};
     413              :      };
     414              : 
     415              :    A<T>::B<U> has depth two, while A<T> has depth one.
     416              :    Both A<T>::B<int> and A<int>::B<U> have depth one, if
     417              :    they are instantiations, not specializations.
     418              : 
     419              :    This function is guaranteed to return 0 if passed NULL_TREE so
     420              :    that, for example, `template_class_depth (current_class_type)' is
     421              :    always safe.  */
     422              : 
     423              : int
     424    785392976 : template_class_depth (tree type)
     425              : {
     426    785392976 :   int depth;
     427              : 
     428   1104571469 :   for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
     429              :     {
     430    319178493 :       tree tinfo = get_template_info (type);
     431              : 
     432    319178493 :       if (tinfo
     433    275801307 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
     434    275801289 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
     435    586947017 :           && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
     436    240529270 :         ++depth;
     437              : 
     438    319178493 :       if (DECL_P (type))
     439              :         {
     440      4542854 :           if (tree fctx = DECL_FRIEND_CONTEXT (type))
     441              :             type = fctx;
     442              :           else
     443      2363792 :             type = CP_DECL_CONTEXT (type);
     444              :         }
     445    634526112 :       else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
     446      3786384 :         type = LAMBDA_TYPE_EXTRA_SCOPE (type);
     447              :       else
     448    314906986 :         type = CP_TYPE_CONTEXT (type);
     449              :     }
     450              : 
     451    785392976 :   return depth;
     452              : }
     453              : 
     454              : /* Return TRUE if NODE instantiates a template that has arguments of
     455              :    its own, be it directly a primary template or indirectly through a
     456              :    partial specializations.  */
     457              : static bool
     458    153575946 : instantiates_primary_template_p (tree node)
     459              : {
     460    153575946 :   tree tinfo = get_template_info (node);
     461    153575946 :   if (!tinfo)
     462              :     return false;
     463              : 
     464    153562873 :   tree tmpl = TI_TEMPLATE (tinfo);
     465    153562873 :   if (PRIMARY_TEMPLATE_P (tmpl))
     466              :     return true;
     467              : 
     468    127927265 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
     469              :     return false;
     470              : 
     471              :   /* So now we know we have a specialization, but it could be a full
     472              :      or a partial specialization.  To tell which, compare the depth of
     473              :      its template arguments with those of its context.  */
     474              : 
     475            0 :   tree ctxt = DECL_CONTEXT (tmpl);
     476            0 :   tree ctinfo = get_template_info (ctxt);
     477            0 :   if (!ctinfo)
     478              :     return true;
     479              : 
     480            0 :   return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
     481            0 :           > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
     482              : }
     483              : 
     484              : /* Subroutine of maybe_begin_member_template_processing.
     485              :    Returns true if processing DECL needs us to push template parms.  */
     486              : 
     487              : static bool
     488    193587668 : inline_needs_template_parms (tree decl, bool nsdmi)
     489              : {
     490    193587668 :   if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
     491              :     return false;
     492              : 
     493    160492212 :   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
     494    160492212 :           > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
     495              : }
     496              : 
     497              : /* Subroutine of maybe_begin_member_template_processing.
     498              :    Push the template parms in PARMS, starting from LEVELS steps into the
     499              :    chain, and ending at the beginning, since template parms are listed
     500              :    innermost first.  */
     501              : 
     502              : static void
     503     45439477 : push_inline_template_parms_recursive (tree parmlist, int levels)
     504              : {
     505     45439477 :   tree parms = TREE_VALUE (parmlist);
     506     45439477 :   int i;
     507              : 
     508     45439477 :   if (levels > 1)
     509       544684 :     push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
     510              : 
     511     45439477 :   ++processing_template_decl;
     512     90878954 :   current_template_parms
     513     45439477 :     = tree_cons (size_int (current_template_depth + 1),
     514              :                  parms, current_template_parms);
     515     90878954 :   TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
     516     45439477 :     = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
     517     45439477 :   TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
     518              : 
     519     45439477 :   begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
     520              :                NULL);
     521    124320343 :   for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
     522              :     {
     523     78880866 :       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
     524              : 
     525     78880866 :       if (error_operand_p (parm))
     526           37 :         continue;
     527              : 
     528     78880829 :       gcc_assert (DECL_P (parm));
     529              : 
     530     78880829 :       switch (TREE_CODE (parm))
     531              :         {
     532     74890542 :         case TYPE_DECL:
     533     74890542 :         case TEMPLATE_DECL:
     534     74890542 :           pushdecl (parm);
     535     74890542 :           break;
     536              : 
     537      3990287 :         case PARM_DECL:
     538              :           /* Push the CONST_DECL.  */
     539      3990287 :           pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
     540      3990287 :           break;
     541              : 
     542            0 :         default:
     543            0 :           gcc_unreachable ();
     544              :         }
     545              :     }
     546     45439477 : }
     547              : 
     548              : /* Restore the template parameter context for a member template, a
     549              :    friend template defined in a class definition, or a non-template
     550              :    member of template class.  */
     551              : 
     552              : void
     553    193587668 : maybe_begin_member_template_processing (tree decl)
     554              : {
     555    193587668 :   tree parms;
     556    193587668 :   int levels = 0;
     557    193587668 :   bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
     558              : 
     559    193587668 :   if (nsdmi || decl_specialization_friend_p (decl))
     560              :     {
     561      2232249 :       tree ctx = nsdmi ? DECL_CONTEXT (decl) : DECL_CHAIN (decl);
     562      2232249 :       decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
     563              :               /* Disregard full specializations (c++/60999).  */
     564      1657619 :               && uses_template_parms (ctx)
     565      3879421 :               ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
     566              :     }
     567              : 
     568    193587668 :   if (inline_needs_template_parms (decl, nsdmi))
     569              :     {
     570     44894793 :       parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
     571     44894793 :       levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
     572              : 
     573     44894793 :       if (DECL_TEMPLATE_SPECIALIZATION (decl))
     574              :         {
     575            0 :           --levels;
     576            0 :           parms = TREE_CHAIN (parms);
     577              :         }
     578              : 
     579     44894793 :       push_inline_template_parms_recursive (parms, levels);
     580              :     }
     581              : 
     582              :   /* Remember how many levels of template parameters we pushed so that
     583              :      we can pop them later.  */
     584    193587668 :   inline_parm_levels.safe_push (levels);
     585    193587668 : }
     586              : 
     587              : /* Undo the effects of maybe_begin_member_template_processing.  */
     588              : 
     589              : void
     590    193588096 : maybe_end_member_template_processing (void)
     591              : {
     592    193588096 :   int i;
     593    193588096 :   int last;
     594              : 
     595    193588096 :   if (inline_parm_levels.length () == 0)
     596              :     return;
     597              : 
     598    193587668 :   last = inline_parm_levels.pop ();
     599    239027145 :   for (i = 0; i < last; ++i)
     600              :     {
     601     45439477 :       --processing_template_decl;
     602     45439477 :       current_template_parms = TREE_CHAIN (current_template_parms);
     603     45439477 :       poplevel (0, 0, 0);
     604              :     }
     605              : }
     606              : 
     607              : /* Return a new template argument vector which contains all of ARGS,
     608              :    but has as its innermost set of arguments the EXTRA_ARGS.  */
     609              : 
     610              : tree
     611     69082894 : add_to_template_args (tree args, tree extra_args)
     612              : {
     613     69082894 :   tree new_args;
     614     69082894 :   int extra_depth;
     615     69082894 :   int i;
     616     69082894 :   int j;
     617              : 
     618     69082894 :   if (args == NULL_TREE || extra_args == error_mark_node)
     619              :     return extra_args;
     620              : 
     621    104479164 :   extra_depth = TMPL_ARGS_DEPTH (extra_args);
     622     52239582 :   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
     623              : 
     624    178775816 :   for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
     625     52686116 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
     626              : 
     627    104479164 :   for (j = 1; j <= extra_depth; ++j, ++i)
     628    104479164 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
     629              : 
     630              :   return new_args;
     631              : }
     632              : 
     633              : /* Like add_to_template_args, but only the outermost ARGS are added to
     634              :    the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
     635              :    (EXTRA_ARGS) levels are added.  This function is used to combine
     636              :    the template arguments from a partial instantiation with the
     637              :    template arguments used to attain the full instantiation from the
     638              :    partial instantiation.
     639              : 
     640              :    If ARGS is a TEMPLATE_DECL, use its parameters as args.  */
     641              : 
     642              : tree
     643   1269574503 : add_outermost_template_args (tree args, tree extra_args)
     644              : {
     645   1269574503 :   tree new_args;
     646              : 
     647   1269574503 :   if (!args)
     648              :     return extra_args;
     649   1269574503 :   if (TREE_CODE (args) == TEMPLATE_DECL)
     650              :     {
     651    409305267 :       tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
     652    409305267 :       args = TI_ARGS (ti);
     653              :     }
     654              : 
     655              :   /* If there are more levels of EXTRA_ARGS than there are ARGS,
     656              :      something very fishy is going on.  */
     657   5425775561 :   gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
     658              : 
     659              :   /* If *all* the new arguments will be the EXTRA_ARGS, just return
     660              :      them.  */
     661   6286044797 :   if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
     662              :     return extra_args;
     663              : 
     664              :   /* For the moment, we make ARGS look like it contains fewer levels.  */
     665     82152388 :   TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
     666              : 
     667     41076194 :   new_args = add_to_template_args (args, extra_args);
     668              : 
     669              :   /* Now, we restore ARGS to its full dimensions.  */
     670     82152388 :   TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
     671              : 
     672     41076194 :   return new_args;
     673              : }
     674              : 
     675              : /* Return the N levels of innermost template arguments from the ARGS.  */
     676              : 
     677              : tree
     678   5806522673 : get_innermost_template_args (tree args, int n)
     679              : {
     680   5806522673 :   tree new_args;
     681   5806522673 :   int extra_levels;
     682   5806522673 :   int i;
     683              : 
     684   5806522673 :   gcc_assert (n >= 0);
     685              : 
     686              :   /* If N is 1, just return the innermost set of template arguments.  */
     687   5806522673 :   if (n == 1)
     688  11612949978 :     return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
     689              : 
     690              :   /* If we're not removing anything, just return the arguments we were
     691              :      given.  */
     692        95368 :   extra_levels = TMPL_ARGS_DEPTH (args) - n;
     693        47684 :   gcc_assert (extra_levels >= 0);
     694        47684 :   if (extra_levels == 0)
     695              :     return args;
     696              : 
     697              :   /* Make a new set of arguments, not containing the outer arguments.  */
     698          438 :   new_args = make_tree_vec (n);
     699         1752 :   for (i = 1; i <= n; ++i)
     700         1752 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     701              :                          TMPL_ARGS_LEVEL (args, i + extra_levels));
     702              : 
     703              :   return new_args;
     704              : }
     705              : 
     706              : /* The inverse of get_innermost_template_args: Return all but the innermost
     707              :    EXTRA_LEVELS levels of template arguments from the ARGS.  */
     708              : 
     709              : static tree
     710       464274 : strip_innermost_template_args (tree args, int extra_levels)
     711              : {
     712       464274 :   tree new_args;
     713       928548 :   int n = TMPL_ARGS_DEPTH (args) - extra_levels;
     714       464274 :   int i;
     715              : 
     716       464274 :   gcc_assert (n >= 0);
     717              : 
     718              :   /* If N is 1, just return the outermost set of template arguments.  */
     719       464274 :   if (n == 1)
     720       919980 :     return TMPL_ARGS_LEVEL (args, 1);
     721              : 
     722              :   /* If we're not removing anything, just return the arguments we were
     723              :      given.  */
     724         4284 :   gcc_assert (extra_levels >= 0);
     725         4284 :   if (extra_levels == 0)
     726              :     return args;
     727              : 
     728              :   /* Make a new set of arguments, not containing the inner arguments.  */
     729         4284 :   new_args = make_tree_vec (n);
     730        17136 :   for (i = 1; i <= n; ++i)
     731        17136 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     732              :                          TMPL_ARGS_LEVEL (args, i));
     733              : 
     734              :   return new_args;
     735              : }
     736              : 
     737              : /* We've got a template header coming up; push to a new level for storing
     738              :    the parms.  */
     739              : 
     740              : void
     741     88987991 : begin_template_parm_list (void)
     742              : {
     743              :   /* We use a non-tag-transparent scope here, which causes pushtag to
     744              :      put tags in this scope, rather than in the enclosing class or
     745              :      namespace scope.  This is the right thing, since we want
     746              :      TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
     747              :      global template class, push_template_decl handles putting the
     748              :      TEMPLATE_DECL into top-level scope.  For a nested template class,
     749              :      e.g.:
     750              : 
     751              :        template <class T> struct S1 {
     752              :          template <class T> struct S2 {};
     753              :        };
     754              : 
     755              :      pushtag contains special code to insert the TEMPLATE_DECL for S2
     756              :      at the right scope.  */
     757     88987991 :   begin_scope (sk_template_parms, NULL);
     758     88987991 :   ++processing_template_decl;
     759     88987991 :   ++processing_template_parmlist;
     760     88987991 :   note_template_header (0);
     761              : 
     762              :   /* Add a dummy parameter level while we process the parameter list.  */
     763    177975982 :   current_template_parms
     764     88987991 :     = tree_cons (size_int (current_template_depth + 1),
     765              :                  make_tree_vec (0),
     766              :                  current_template_parms);
     767     88987991 : }
     768              : 
     769              : /* This routine is called when a specialization is declared.  If it is
     770              :    invalid to declare a specialization here, an error is reported and
     771              :    false is returned, otherwise this routine will return true.  */
     772              : 
     773              : static bool
     774      5885534 : check_specialization_scope (void)
     775              : {
     776      5885534 :   tree scope = current_scope ();
     777              : 
     778              :   /* [temp.expl.spec]
     779              : 
     780              :      An explicit specialization shall be declared in the namespace of
     781              :      which the template is a member, or, for member templates, in the
     782              :      namespace of which the enclosing class or enclosing class
     783              :      template is a member.  An explicit specialization of a member
     784              :      function, member class or static data member of a class template
     785              :      shall be declared in the namespace of which the class template
     786              :      is a member.  */
     787      5885534 :   if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
     788              :     {
     789           24 :       error ("explicit specialization in non-namespace scope %qD", scope);
     790           24 :       return false;
     791              :     }
     792              : 
     793              :   /* [temp.expl.spec]
     794              : 
     795              :      In an explicit specialization declaration for a member of a class
     796              :      template or a member template that appears in namespace scope,
     797              :      the member template and some of its enclosing class templates may
     798              :      remain unspecialized, except that the declaration shall not
     799              :      explicitly specialize a class member template if its enclosing
     800              :      class templates are not explicitly specialized as well.  */
     801      5885510 :   if (current_template_parms)
     802              :     {
     803            6 :       error ("enclosing class templates are not explicitly specialized");
     804            6 :       return false;
     805              :     }
     806              : 
     807              :   return true;
     808              : }
     809              : 
     810              : /* We've just seen template <>.  */
     811              : 
     812              : bool
     813      5885534 : begin_specialization (void)
     814              : {
     815      5885534 :   begin_scope (sk_template_spec, NULL);
     816      5885534 :   note_template_header (1);
     817     11771068 :   return check_specialization_scope ();
     818              : }
     819              : 
     820              : /* Called at then end of processing a declaration preceded by
     821              :    template<>.  */
     822              : 
     823              : void
     824      5885534 : end_specialization (void)
     825              : {
     826      5885534 :   finish_scope ();
     827      5885534 :   reset_specialization ();
     828      5885534 : }
     829              : 
     830              : /* Any template <>'s that we have seen thus far are not referring to a
     831              :    function specialization.  */
     832              : 
     833              : void
     834    162849364 : reset_specialization (void)
     835              : {
     836    162849364 :   processing_specialization = 0;
     837    162849364 :   template_header_count = 0;
     838    162849364 : }
     839              : 
     840              : /* We've just seen a template header.  If SPECIALIZATION is nonzero,
     841              :    it was of the form template <>.  */
     842              : 
     843              : static void
     844     94873525 : note_template_header (int specialization)
     845              : {
     846     94873525 :   processing_specialization = specialization;
     847     94873525 :   template_header_count++;
     848            0 : }
     849              : 
     850              : /* We're beginning an explicit instantiation.  */
     851              : 
     852              : void
     853      2741649 : begin_explicit_instantiation (void)
     854              : {
     855      2741649 :   gcc_assert (!processing_explicit_instantiation);
     856      2741649 :   processing_explicit_instantiation = true;
     857      2741649 : }
     858              : 
     859              : 
     860              : void
     861      2741646 : end_explicit_instantiation (void)
     862              : {
     863      2741646 :   gcc_assert (processing_explicit_instantiation);
     864      2741646 :   processing_explicit_instantiation = false;
     865      2741646 : }
     866              : 
     867              : /* An explicit specialization or partial specialization of TMPL is being
     868              :    declared.  Check that the namespace in which the specialization is
     869              :    occurring is permissible.  Returns false iff it is invalid to
     870              :    specialize TMPL in the current namespace.  */
     871              : 
     872              : static bool
     873     13609771 : check_specialization_namespace (tree tmpl)
     874              : {
     875     13609771 :   tree tpl_ns = decl_namespace_context (tmpl);
     876              : 
     877              :   /* [tmpl.expl.spec]
     878              : 
     879              :      An explicit specialization shall be declared in a namespace enclosing the
     880              :      specialized template. An explicit specialization whose declarator-id is
     881              :      not qualified shall be declared in the nearest enclosing namespace of the
     882              :      template, or, if the namespace is inline (7.3.1), any namespace from its
     883              :      enclosing namespace set.  */
     884     13609771 :   if (current_scope() != DECL_CONTEXT (tmpl)
     885     13609771 :       && !at_namespace_scope_p ())
     886              :     {
     887           12 :       error ("specialization of %qD must appear at namespace scope", tmpl);
     888           12 :       return false;
     889              :     }
     890              : 
     891     13609759 :   if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
     892              :     /* Same or enclosing namespace.  */
     893              :     return true;
     894              :   else
     895              :     {
     896           14 :       auto_diagnostic_group d;
     897           14 :       if (permerror (input_location,
     898              :                      "specialization of %qD in different namespace", tmpl))
     899           11 :         inform (DECL_SOURCE_LOCATION (tmpl),
     900              :                 "  from definition of %q#D", tmpl);
     901           14 :       return false;
     902           14 :     }
     903              : }
     904              : 
     905              : /* SPEC is an explicit instantiation.  Check that it is valid to
     906              :    perform this explicit instantiation in the current namespace.  */
     907              : 
     908              : static void
     909      2784131 : check_explicit_instantiation_namespace (tree spec)
     910              : {
     911      2784131 :   tree ns;
     912              : 
     913              :   /* DR 275: An explicit instantiation shall appear in an enclosing
     914              :      namespace of its template.  */
     915      2784131 :   ns = decl_namespace_context (spec);
     916      2784131 :   if (!is_nested_namespace (current_namespace, ns))
     917            6 :     permerror (input_location, "explicit instantiation of %qD in namespace %qD "
     918              :                "(which does not enclose namespace %qD)",
     919              :                spec, current_namespace, ns);
     920      2784131 : }
     921              : 
     922              : /* Returns true if TYPE is a new partial specialization that needs to be
     923              :    set up.  This may also modify TYPE to point to the correct (new or
     924              :    existing) constrained partial specialization.  */
     925              : 
     926              : static bool
     927     33674341 : maybe_new_partial_specialization (tree& type)
     928              : {
     929              :   /* An implicit instantiation of an incomplete type implies
     930              :      the definition of a new class template.
     931              : 
     932              :         template<typename T>
     933              :           struct S;
     934              : 
     935              :         template<typename T>
     936              :           struct S<T*>;
     937              : 
     938              :      Here, S<T*> is an implicit instantiation of S whose type
     939              :      is incomplete.  */
     940     33674341 :   if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
     941              :     return true;
     942              : 
     943              :   /* It can also be the case that TYPE is a completed specialization.
     944              :      Continuing the previous example, suppose we also declare:
     945              : 
     946              :         template<typename T>
     947              :           requires Integral<T>
     948              :             struct S<T*>;
     949              : 
     950              :      Here, S<T*> refers to the specialization S<T*> defined
     951              :      above. However, we need to differentiate definitions because
     952              :      we intend to define a new partial specialization. In this case,
     953              :      we rely on the fact that the constraints are different for
     954              :      this declaration than that above.
     955              : 
     956              :      Note that we also get here for injected class names and
     957              :      late-parsed template definitions. We must ensure that we
     958              :      do not create new type declarations for those cases.  */
     959     22598031 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     960              :     {
     961     22598031 :       tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     962     22598031 :       tree args = CLASSTYPE_TI_ARGS (type);
     963              : 
     964              :       /* If there are no template parameters, this cannot be a new
     965              :          partial template specialization?  */
     966     22598031 :       if (!current_template_parms)
     967              :         return false;
     968              : 
     969              :       /* The injected-class-name is not a new partial specialization.  */
     970     14169719 :       if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
     971              :         return false;
     972              : 
     973              :       /* If the constraints are not the same as those of the primary
     974              :          then, we can probably create a new specialization.  */
     975     14169719 :       tree type_constr = current_template_constraints ();
     976              : 
     977     14169719 :       if (type == TREE_TYPE (tmpl))
     978              :         {
     979           52 :           tree main_constr = get_constraints (tmpl);
     980           52 :           if (equivalent_constraints (type_constr, main_constr))
     981              :             return false;
     982              :         }
     983              : 
     984              :       /* Also, if there's a pre-existing specialization with matching
     985              :          constraints, then this also isn't new.  */
     986     14169667 :       tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
     987     14806030 :       while (specs)
     988              :         {
     989     14595040 :           tree spec_tmpl = TREE_VALUE (specs);
     990     14595040 :           tree spec_args = TREE_PURPOSE (specs);
     991     14595040 :           tree spec_constr = get_constraints (spec_tmpl);
     992     14595040 :           tree spec_parms = DECL_TEMPLATE_PARMS (spec_tmpl);
     993              :           /* Per [temp.spec.partial.general]/2, two partial specializations
     994              :              declare the same entity if they have equivalent template-heads
     995              :              and template argument lists.  */
     996     14595040 :           if (comp_template_args (args, spec_args)
     997     14305582 :               && comp_template_parms (spec_parms, current_template_parms)
     998     28900619 :               && equivalent_constraints (type_constr, spec_constr))
     999              :             {
    1000     13958677 :               type = TREE_TYPE (spec_tmpl);
    1001     13958677 :               return false;
    1002              :             }
    1003       636363 :           specs = TREE_CHAIN (specs);
    1004              :         }
    1005              : 
    1006              :       /* Create a new type node (and corresponding type decl)
    1007              :          for the newly declared specialization.  */
    1008       210990 :       tree t = make_class_type (TREE_CODE (type));
    1009       210990 :       CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
    1010       210990 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
    1011       210990 :       TYPE_CONTEXT (t) = TYPE_CONTEXT (type);
    1012              : 
    1013              :       /* We only need a separate type node for storing the definition of this
    1014              :          partial specialization; uses of S<T*> are unconstrained, so all are
    1015              :          equivalent.  So keep TYPE_CANONICAL the same.  */
    1016       210990 :       TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
    1017              : 
    1018              :       /* Build the corresponding type decl.  */
    1019       210990 :       tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
    1020       210990 :       DECL_CONTEXT (d) = TYPE_CONTEXT (t);
    1021       210990 :       DECL_SOURCE_LOCATION (d) = input_location;
    1022       210990 :       TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl));
    1023              : 
    1024       210990 :       set_instantiating_module (d);
    1025       210990 :       DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
    1026              : 
    1027       210990 :       type = t;
    1028       210990 :       return true;
    1029              :     }
    1030              : 
    1031              :   return false;
    1032              : }
    1033              : 
    1034              : /* The TYPE is being declared.  If it is a template type, that means it
    1035              :    is a partial specialization.  Do appropriate error-checking.  */
    1036              : 
    1037              : tree
    1038     72845942 : maybe_process_partial_specialization (tree type)
    1039              : {
    1040     72845942 :   tree context;
    1041              : 
    1042     72845942 :   if (type == error_mark_node)
    1043              :     return error_mark_node;
    1044              : 
    1045              :   /* A lambda that appears in specialization context is not itself a
    1046              :      specialization.  */
    1047     72845178 :   if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
    1048              :     return type;
    1049              : 
    1050              :   /* An injected-class-name is not a specialization.  */
    1051     71395479 :   if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
    1052              :     return type;
    1053              : 
    1054     71395473 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
    1055              :     {
    1056            3 :       error ("name of class shadows template template parameter %qD",
    1057            3 :              TYPE_NAME (type));
    1058            3 :       return error_mark_node;
    1059              :     }
    1060              : 
    1061     71395470 :   context = TYPE_CONTEXT (type);
    1062              : 
    1063     71395470 :   if (TYPE_ALIAS_P (type))
    1064              :     {
    1065            6 :       tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
    1066              : 
    1067           12 :       if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    1068            6 :         error ("specialization of alias template %qD",
    1069            6 :                TI_TEMPLATE (tinfo));
    1070              :       else
    1071            0 :         error ("explicit specialization of non-template %qT", type);
    1072            6 :       return error_mark_node;
    1073              :     }
    1074     71395464 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
    1075              :     {
    1076              :       /* This is for ordinary explicit specialization and partial
    1077              :          specialization of a template class such as:
    1078              : 
    1079              :            template <> class C<int>;
    1080              : 
    1081              :          or:
    1082              : 
    1083              :            template <class T> class C<T*>;
    1084              : 
    1085              :          Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
    1086              : 
    1087     33674341 :       if (maybe_new_partial_specialization (type))
    1088              :         {
    1089     11287300 :           if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
    1090     11287300 :               && !at_namespace_scope_p ())
    1091           12 :             return error_mark_node;
    1092     11287288 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1093     11287288 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1094     11287288 :           if (processing_template_decl)
    1095              :             {
    1096      6979422 :               tree decl = push_template_decl (TYPE_MAIN_DECL (type));
    1097      6979422 :               if (decl == error_mark_node)
    1098              :                 return error_mark_node;
    1099      6979334 :               return TREE_TYPE (decl);
    1100              :             }
    1101              :         }
    1102     22387041 :       else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
    1103            0 :         error ("specialization of %qT after instantiation", type);
    1104         6970 :       else if (errorcount && !processing_specialization
    1105         5106 :                 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
    1106     22392147 :                && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
    1107              :         /* Trying to define a specialization either without a template<> header
    1108              :            or in an inappropriate place.  We've already given an error, so just
    1109              :            bail now so we don't actually define the specialization.  */
    1110         1820 :         return error_mark_node;
    1111              :     }
    1112     36835366 :   else if (CLASS_TYPE_P (type)
    1113     36835366 :            && !CLASSTYPE_USE_TEMPLATE (type)
    1114     36835366 :            && CLASSTYPE_TEMPLATE_INFO (type)
    1115     24472460 :            && context && CLASS_TYPE_P (context)
    1116     40209489 :            && CLASSTYPE_TEMPLATE_INFO (context))
    1117              :     {
    1118              :       /* This is for an explicit specialization of member class
    1119              :          template according to [temp.expl.spec/18]:
    1120              : 
    1121              :            template <> template <class U> class C<int>::D;
    1122              : 
    1123              :          The context `C<int>' must be an implicit instantiation.
    1124              :          Otherwise this is just a member class template declared
    1125              :          earlier like:
    1126              : 
    1127              :            template <> class C<int> { template <class U> class D; };
    1128              :            template <> template <class U> class C<int>::D;
    1129              : 
    1130              :          In the first case, `C<int>::D' is a specialization of `C<T>::D'
    1131              :          while in the second case, `C<int>::D' is a primary template
    1132              :          and `C<T>::D' may not exist.  */
    1133              : 
    1134      1985755 :       if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
    1135      1985755 :           && !COMPLETE_TYPE_P (type))
    1136              :         {
    1137           32 :           tree t;
    1138           32 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
    1139              : 
    1140           32 :           if (current_namespace
    1141           32 :               != decl_namespace_context (tmpl))
    1142              :             {
    1143            0 :               auto_diagnostic_group d;
    1144            0 :               if (permerror (input_location,
    1145              :                              "specialization of %qD in different namespace",
    1146              :                              type))
    1147            0 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    1148              :                         "from definition of %q#D", tmpl);
    1149            0 :             }
    1150              : 
    1151              :           /* Check for invalid specialization after instantiation:
    1152              : 
    1153              :                template <> template <> class C<int>::D<int>;
    1154              :                template <> template <class U> class C<int>::D;  */
    1155              : 
    1156           32 :           for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
    1157           43 :                t; t = TREE_CHAIN (t))
    1158              :             {
    1159           11 :               tree inst = TREE_VALUE (t);
    1160           11 :               if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
    1161           11 :                   || !COMPLETE_OR_OPEN_TYPE_P (inst))
    1162              :                 {
    1163              :                   /* We already have a full specialization of this partial
    1164              :                      instantiation, or a full specialization has been
    1165              :                      looked up but not instantiated.  Reassign it to the
    1166              :                      new member specialization template.  */
    1167            8 :                   spec_entry elt;
    1168            8 :                   spec_entry *entry;
    1169              : 
    1170            8 :                   elt.tmpl = most_general_template (tmpl);
    1171            8 :                   elt.args = CLASSTYPE_TI_ARGS (inst);
    1172            8 :                   elt.spec = inst;
    1173              : 
    1174            8 :                   type_specializations->remove_elt (&elt);
    1175              : 
    1176            8 :                   elt.tmpl = tmpl;
    1177            8 :                   CLASSTYPE_TI_ARGS (inst)
    1178            8 :                     = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
    1179            8 :                   elt.hash = 0; /* Recalculate after changing tmpl/args.  */
    1180              : 
    1181            8 :                   spec_entry **slot
    1182            8 :                     = type_specializations->find_slot (&elt, INSERT);
    1183            8 :                   entry = ggc_alloc<spec_entry> ();
    1184            8 :                   *entry = elt;
    1185            8 :                   *slot = entry;
    1186              :                 }
    1187              :               else
    1188              :                 /* But if we've had an implicit instantiation, that's a
    1189              :                    problem ([temp.expl.spec]/6).  */
    1190            3 :                 error ("specialization %qT after instantiation %qT",
    1191              :                        type, inst);
    1192              :             }
    1193              : 
    1194              :           /* Make sure that the specialization is valid.  */
    1195           32 :           if (!redeclare_class_template (type, current_template_parms,
    1196              :                                          current_template_constraints ()))
    1197            6 :             return error_mark_node;
    1198              : 
    1199              :           /* Mark TYPE as a specialization.  And as a result, we only
    1200              :              have one level of template argument for the innermost
    1201              :              class template.  */
    1202           26 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1203           26 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1204           26 :           CLASSTYPE_TI_ARGS (type)
    1205           52 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1206              :         }
    1207              :     }
    1208     35735368 :   else if (processing_specialization)
    1209              :     {
    1210              :        /* Someday C++0x may allow for enum template specialization.  */
    1211           21 :       if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
    1212           43 :           && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
    1213           12 :         pedwarn (input_location, OPT_Wpedantic, "template specialization "
    1214              :                  "of %qD not allowed by ISO C++", type);
    1215              :       else
    1216              :         {
    1217           12 :           error ("explicit specialization of non-template %qT", type);
    1218           12 :           return error_mark_node;
    1219              :         }
    1220              :     }
    1221              : 
    1222     64414192 :   return type;
    1223              : }
    1224              : 
    1225              : /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
    1226              :    gone through coerce_template_parms by now.  */
    1227              : 
    1228              : static void
    1229    660007674 : verify_unstripped_args_1 (tree inner)
    1230              : {
    1231   1998166912 :   for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
    1232              :     {
    1233   1338159238 :       tree arg = TREE_VEC_ELT (inner, i);
    1234   1338159238 :       if (TREE_CODE (arg) == TEMPLATE_DECL || REFLECT_EXPR_P (arg))
    1235              :         /* OK */;
    1236   1336872513 :       else if (TYPE_P (arg))
    1237   1218104047 :         gcc_assert (strip_typedefs (arg, NULL) == arg);
    1238    118768466 :       else if (ARGUMENT_PACK_P (arg))
    1239      3052751 :         verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
    1240    115715715 :       else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
    1241              :         /* Allow typedefs on the type of a non-type argument, since a
    1242              :            parameter can have them.  */;
    1243              :       else
    1244    115715433 :         gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
    1245              :     }
    1246    660007674 : }
    1247              : 
    1248              : static void
    1249    757279117 : verify_unstripped_args (tree args)
    1250              : {
    1251    757279117 :   ++processing_template_decl;
    1252    757279117 :   if (!any_dependent_template_arguments_p (args))
    1253    656954923 :     verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
    1254    757279117 :   --processing_template_decl;
    1255    757279117 : }
    1256              : 
    1257              : /* Retrieve the specialization (in the sense of [temp.spec] - a
    1258              :    specialization is either an instantiation or an explicit
    1259              :    specialization) of TMPL for the given template ARGS.  If there is
    1260              :    no such specialization, return NULL_TREE.  The ARGS are a vector of
    1261              :    arguments, or a vector of vectors of arguments, in the case of
    1262              :    templates with more than one level of parameters.
    1263              : 
    1264              :    If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
    1265              :    then we search for a partial specialization matching ARGS.  This
    1266              :    parameter is ignored if TMPL is not a class template.
    1267              : 
    1268              :    We can also look up a FIELD_DECL, if it is a lambda capture pack; the
    1269              :    result is a NONTYPE_ARGUMENT_PACK.  */
    1270              : 
    1271              : static tree
    1272    757279117 : retrieve_specialization (tree tmpl, tree args, hashval_t hash)
    1273              : {
    1274    757279117 :   if (tmpl == NULL_TREE)
    1275              :     return NULL_TREE;
    1276              : 
    1277    757279117 :   if (args == error_mark_node)
    1278              :     return NULL_TREE;
    1279              : 
    1280    757279117 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
    1281              :               || TREE_CODE (tmpl) == FIELD_DECL);
    1282              : 
    1283              :   /* There should be as many levels of arguments as there are
    1284              :      levels of parameters.  */
    1285   1514558234 :   gcc_assert (TMPL_ARGS_DEPTH (args)
    1286              :               == (TREE_CODE (tmpl) == TEMPLATE_DECL
    1287              :                   ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
    1288              :                   : template_class_depth (DECL_CONTEXT (tmpl))));
    1289              : 
    1290    757279117 :   if (flag_checking)
    1291    757279117 :     verify_unstripped_args (args);
    1292              : 
    1293              :   /* Lambda functions in templates aren't instantiated normally, but through
    1294              :      tsubst_lambda_expr.  */
    1295    757279117 :   if (lambda_fn_in_template_p (tmpl))
    1296              :     return NULL_TREE;
    1297              : 
    1298    757279117 :   spec_entry elt;
    1299    757279117 :   elt.tmpl = tmpl;
    1300    757279117 :   elt.args = args;
    1301    757279117 :   elt.hash = hash;
    1302              : 
    1303    757279117 :   spec_hash_table *specializations;
    1304    757279117 :   if (DECL_CLASS_TEMPLATE_P (tmpl))
    1305      1675636 :     specializations = type_specializations;
    1306              :   else
    1307    755603481 :     specializations = decl_specializations;
    1308              : 
    1309    757279117 :   if (spec_entry *found = specializations->find (&elt))
    1310    368758954 :     return found->spec;
    1311              : 
    1312              :   return NULL_TREE;
    1313              : }
    1314              : 
    1315              : /* Like retrieve_specialization, but for local declarations.  */
    1316              : 
    1317              : tree
    1318    183746385 : retrieve_local_specialization (tree tmpl)
    1319              : {
    1320    183746385 :   if (local_specializations == NULL)
    1321              :     return NULL_TREE;
    1322              : 
    1323    178446875 :   tree *slot = local_specializations->get (tmpl);
    1324    178446875 :   return slot ? *slot : NULL_TREE;
    1325              : }
    1326              : 
    1327              : /* Returns nonzero iff DECL is a specialization of TMPL.  */
    1328              : 
    1329              : int
    1330       811220 : is_specialization_of (tree decl, tree tmpl)
    1331              : {
    1332       811220 :   tree t;
    1333              : 
    1334       811220 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    1335              :     {
    1336       530158 :       for (t = decl;
    1337       455156 :            t != NULL_TREE;
    1338       302211 :            t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
    1339       455156 :         if (t == tmpl)
    1340              :           return 1;
    1341              :     }
    1342              :   else
    1343              :     {
    1344       584011 :       gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    1345              : 
    1346      1015147 :       for (t = TREE_TYPE (decl);
    1347      1015147 :            t != NULL_TREE;
    1348       802310 :            t = CLASSTYPE_USE_TEMPLATE (t)
    1349       802310 :              ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
    1350      1015147 :         if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
    1351              :           return 1;
    1352              :     }
    1353              : 
    1354              :   return 0;
    1355              : }
    1356              : 
    1357              : /* Returns nonzero iff DECL is a specialization of friend declaration
    1358              :    FRIEND_DECL according to [temp.friend].  */
    1359              : 
    1360              : bool
    1361      1201796 : is_specialization_of_friend (tree decl, tree friend_decl)
    1362              : {
    1363      1201796 :   bool need_template = true;
    1364      1201796 :   int template_depth;
    1365              : 
    1366      1201796 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
    1367              :               || TREE_CODE (decl) == TYPE_DECL);
    1368              : 
    1369              :   /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
    1370              :      of a template class, we want to check if DECL is a specialization
    1371              :      if this.  */
    1372      1201796 :   if (TREE_CODE (friend_decl) == FUNCTION_DECL
    1373       390771 :       && DECL_CLASS_SCOPE_P (friend_decl)
    1374           54 :       && DECL_TEMPLATE_INFO (friend_decl)
    1375      1201850 :       && !DECL_USE_TEMPLATE (friend_decl))
    1376              :     {
    1377              :       /* We want a TEMPLATE_DECL for `is_specialization_of'.  */
    1378           54 :       friend_decl = DECL_TI_TEMPLATE (friend_decl);
    1379           54 :       need_template = false;
    1380              :     }
    1381      1201742 :   else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
    1382      1201742 :            && !PRIMARY_TEMPLATE_P (friend_decl))
    1383              :     need_template = false;
    1384              : 
    1385              :   /* There is nothing to do if this is not a template friend.  */
    1386      1201796 :   if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
    1387              :     return false;
    1388              : 
    1389       811079 :   if (is_specialization_of (decl, friend_decl))
    1390              :     return true;
    1391              : 
    1392              :   /* [temp.friend/6]
    1393              :      A member of a class template may be declared to be a friend of a
    1394              :      non-template class.  In this case, the corresponding member of
    1395              :      every specialization of the class template is a friend of the
    1396              :      class granting friendship.
    1397              : 
    1398              :      For example, given a template friend declaration
    1399              : 
    1400              :        template <class T> friend void A<T>::f();
    1401              : 
    1402              :      the member function below is considered a friend
    1403              : 
    1404              :        template <> struct A<int> {
    1405              :          void f();
    1406              :        };
    1407              : 
    1408              :      For this type of template friend, TEMPLATE_DEPTH below will be
    1409              :      nonzero.  To determine if DECL is a friend of FRIEND, we first
    1410              :      check if the enclosing class is a specialization of another.  */
    1411              : 
    1412       445435 :   template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
    1413       445435 :   if (template_depth
    1414          147 :       && DECL_CLASS_SCOPE_P (decl)
    1415       445576 :       && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
    1416          141 :                                CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
    1417              :     {
    1418              :       /* Next, we check the members themselves.  In order to handle
    1419              :          a few tricky cases, such as when FRIEND_DECL's are
    1420              : 
    1421              :            template <class T> friend void A<T>::g(T t);
    1422              :            template <class T> template <T t> friend void A<T>::h();
    1423              : 
    1424              :          and DECL's are
    1425              : 
    1426              :            void A<int>::g(int);
    1427              :            template <int> void A<int>::h();
    1428              : 
    1429              :          we need to figure out ARGS, the template arguments from
    1430              :          the context of DECL.  This is required for template substitution
    1431              :          of `T' in the function parameter of `g' and template parameter
    1432              :          of `h' in the above examples.  Here ARGS corresponds to `int'.  */
    1433              : 
    1434          138 :       tree context = DECL_CONTEXT (decl);
    1435          138 :       tree args = NULL_TREE;
    1436          138 :       int current_depth = 0;
    1437              : 
    1438          276 :       while (current_depth < template_depth)
    1439              :         {
    1440          138 :           if (CLASSTYPE_TEMPLATE_INFO (context))
    1441              :             {
    1442          138 :               if (current_depth == 0)
    1443          276 :                 args = TYPE_TI_ARGS (context);
    1444              :               else
    1445            0 :                 args = add_to_template_args (TYPE_TI_ARGS (context), args);
    1446          138 :               current_depth++;
    1447              :             }
    1448          138 :           context = TYPE_CONTEXT (context);
    1449              :         }
    1450              : 
    1451          138 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    1452              :         {
    1453           72 :           bool is_template;
    1454           72 :           tree friend_type;
    1455           72 :           tree decl_type;
    1456           72 :           tree friend_args_type;
    1457           72 :           tree decl_args_type;
    1458              : 
    1459              :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1460              :              non-templates.  */
    1461           72 :           is_template = DECL_TEMPLATE_INFO (decl)
    1462           72 :                         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
    1463           72 :           if (need_template ^ is_template)
    1464              :             return false;
    1465           54 :           else if (is_template)
    1466              :             {
    1467              :               /* If both are templates, check template parameter list.  */
    1468           27 :               tree friend_parms
    1469           27 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1470              :                                          args, tf_none);
    1471           27 :               if (!comp_template_parms
    1472           27 :                      (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
    1473              :                       friend_parms))
    1474              :                 return false;
    1475              : 
    1476           18 :               decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
    1477              :             }
    1478              :           else
    1479           27 :             decl_type = TREE_TYPE (decl);
    1480              : 
    1481           45 :           friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
    1482              :                                               tf_none, NULL_TREE);
    1483           45 :           if (friend_type == error_mark_node)
    1484              :             return false;
    1485              : 
    1486              :           /* Check if return types match.  */
    1487           45 :           if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
    1488              :             return false;
    1489              : 
    1490              :           /* Check if function parameter types match, ignoring the
    1491              :              `this' parameter.  */
    1492           39 :           friend_args_type = TYPE_ARG_TYPES (friend_type);
    1493           39 :           decl_args_type = TYPE_ARG_TYPES (decl_type);
    1494           39 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl))
    1495           39 :             friend_args_type = TREE_CHAIN (friend_args_type);
    1496           39 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    1497           39 :             decl_args_type = TREE_CHAIN (decl_args_type);
    1498              : 
    1499           39 :           return compparms (decl_args_type, friend_args_type);
    1500              :         }
    1501              :       else
    1502              :         {
    1503              :           /* DECL is a TYPE_DECL */
    1504           66 :           bool is_template;
    1505           66 :           tree decl_type = TREE_TYPE (decl);
    1506              : 
    1507              :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1508              :              non-templates.  */
    1509           66 :           is_template
    1510           66 :             = CLASSTYPE_TEMPLATE_INFO (decl_type)
    1511           66 :               && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
    1512              : 
    1513           66 :           if (need_template ^ is_template)
    1514              :             return false;
    1515           66 :           else if (is_template)
    1516              :             {
    1517           51 :               tree friend_parms;
    1518              :               /* If both are templates, check the name of the two
    1519              :                  TEMPLATE_DECL's first because is_friend didn't.  */
    1520           51 :               if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
    1521           51 :                   != DECL_NAME (friend_decl))
    1522              :                 return false;
    1523              : 
    1524              :               /* Now check template parameter list.  */
    1525           48 :               friend_parms
    1526           48 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1527              :                                          args, tf_none);
    1528           48 :               return comp_template_parms
    1529           48 :                 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
    1530           48 :                  friend_parms);
    1531              :             }
    1532              :           else
    1533           15 :             return (DECL_NAME (decl)
    1534           15 :                     == DECL_NAME (friend_decl));
    1535              :         }
    1536              :     }
    1537              :   return false;
    1538              : }
    1539              : 
    1540              : /* Register the specialization SPEC as a specialization of TMPL with
    1541              :    the indicated ARGS.  IS_FRIEND indicates whether the specialization
    1542              :    is actually just a friend declaration.  ATTRLIST is the list of
    1543              :    attributes that the specialization is declared with or NULL when
    1544              :    it isn't.  Returns SPEC, or an equivalent prior declaration, if
    1545              :    available.
    1546              : 
    1547              :    We also store instantiations of field packs in the hash table, even
    1548              :    though they are not themselves templates, to make lookup easier.  */
    1549              : 
    1550              : static tree
    1551    271215480 : register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
    1552              :                          hashval_t hash)
    1553              : {
    1554    271215480 :   tree fn;
    1555              : 
    1556    271215480 :   gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
    1557              :               || (TREE_CODE (tmpl) == FIELD_DECL
    1558              :                   && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
    1559              : 
    1560    271215480 :   spec_entry elt;
    1561    271215480 :   elt.tmpl = tmpl;
    1562    271215480 :   elt.args = args;
    1563    271215480 :   elt.spec = spec;
    1564    271215480 :   elt.hash = hash;
    1565              : 
    1566    271215480 :   spec_entry **slot = decl_specializations->find_slot (&elt, INSERT);
    1567    271215480 :   if (*slot)
    1568       630609 :     fn = (*slot)->spec;
    1569              :   else
    1570              :     fn = NULL_TREE;
    1571              : 
    1572              :   /* We can sometimes try to re-register a specialization that we've
    1573              :      already got.  In particular, regenerate_decl_from_template calls
    1574              :      duplicate_decls which will update the specialization list.  But,
    1575              :      we'll still get called again here anyhow.  It's more convenient
    1576              :      to simply allow this than to try to prevent it.  */
    1577    271215480 :   if (fn == spec)
    1578              :     return spec;
    1579    271214380 :   else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
    1580              :     {
    1581       551941 :       if (DECL_TEMPLATE_INSTANTIATION (fn))
    1582              :         {
    1583       551695 :           if (DECL_ODR_USED (fn)
    1584       551695 :               || DECL_EXPLICIT_INSTANTIATION (fn))
    1585              :             {
    1586            6 :               error ("specialization of %qD after instantiation",
    1587              :                      fn);
    1588            6 :               return error_mark_node;
    1589              :             }
    1590              :           else
    1591              :             {
    1592       551689 :               tree clone;
    1593              :               /* This situation should occur only if the first
    1594              :                  specialization is an implicit instantiation, the
    1595              :                  second is an explicit specialization, and the
    1596              :                  implicit instantiation has not yet been used.  That
    1597              :                  situation can occur if we have implicitly
    1598              :                  instantiated a member function and then specialized
    1599              :                  it later.
    1600              : 
    1601              :                  We can also wind up here if a friend declaration that
    1602              :                  looked like an instantiation turns out to be a
    1603              :                  specialization:
    1604              : 
    1605              :                    template <class T> void foo(T);
    1606              :                    class S { friend void foo<>(int) };
    1607              :                    template <> void foo(int);
    1608              : 
    1609              :                  We transform the existing DECL in place so that any
    1610              :                  pointers to it become pointers to the updated
    1611              :                  declaration.
    1612              : 
    1613              :                  If there was a definition for the template, but not
    1614              :                  for the specialization, we want this to look as if
    1615              :                  there were no definition, and vice versa.  */
    1616       551689 :               DECL_INITIAL (fn) = NULL_TREE;
    1617       551689 :               duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1618              : 
    1619              :               /* The call to duplicate_decls will have applied
    1620              :                  [temp.expl.spec]:
    1621              : 
    1622              :                    An explicit specialization of a function template
    1623              :                    is inline only if it is explicitly declared to be,
    1624              :                    and independently of whether its function template
    1625              :                    is.
    1626              : 
    1627              :                 to the primary function; now copy the inline bits to
    1628              :                 the various clones.  */
    1629       737424 :               FOR_EACH_CLONE (clone, fn)
    1630              :                 {
    1631       185735 :                   DECL_DECLARED_INLINE_P (clone)
    1632       185735 :                     = DECL_DECLARED_INLINE_P (fn);
    1633       371470 :                   DECL_SOURCE_LOCATION (clone)
    1634       185735 :                     = DECL_SOURCE_LOCATION (fn);
    1635       185735 :                   DECL_DELETED_FN (clone)
    1636       371470 :                     = DECL_DELETED_FN (fn);
    1637              :                 }
    1638       551689 :               check_specialization_namespace (tmpl);
    1639              : 
    1640       551689 :               return fn;
    1641              :             }
    1642              :         }
    1643          246 :       else if (DECL_TEMPLATE_SPECIALIZATION (fn))
    1644              :         {
    1645          246 :           tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1646          246 :           if (dd == error_mark_node)
    1647              :             /* We've already complained in duplicate_decls.  */
    1648              :             return error_mark_node;
    1649              : 
    1650          225 :           if (dd == NULL_TREE && DECL_INITIAL (spec))
    1651              :             /* Dup decl failed, but this is a new definition. Set the
    1652              :                line number so any errors match this new
    1653              :                definition.  */
    1654            0 :             DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
    1655              : 
    1656          225 :           return fn;
    1657              :         }
    1658              :     }
    1659    270662439 :   else if (fn)
    1660        77568 :     return duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1661              : 
    1662              :   /* A specialization must be declared in the same namespace as the
    1663              :      template it is specializing.  */
    1664    270584871 :   if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
    1665    272355653 :       && !check_specialization_namespace (tmpl))
    1666            0 :     DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
    1667              : 
    1668    270584871 :   spec_entry *entry = ggc_alloc<spec_entry> ();
    1669    270584871 :   gcc_assert (tmpl && args && spec);
    1670    270584871 :   *entry = elt;
    1671    270584871 :   *slot = entry;
    1672    115352871 :   if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
    1673     24504970 :        && PRIMARY_TEMPLATE_P (tmpl)
    1674     23646918 :        && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
    1675    260612280 :       || module_maybe_has_cmi_p ()
    1676    530564928 :       || variable_template_p (tmpl))
    1677              :     /* If TMPL is a forward declaration of a template function, keep a list
    1678              :        of all specializations in case we need to reassign them to a friend
    1679              :        template later in tsubst_friend_function.
    1680              : 
    1681              :        If we're building a CMI, keep a list for all function templates.
    1682              : 
    1683              :        Also keep a list of all variable template instantiations so that
    1684              :        process_partial_specialization can check whether a later partial
    1685              :        specialization would have used it.  */
    1686     49833006 :     DECL_TEMPLATE_INSTANTIATIONS (tmpl)
    1687     49833006 :       = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
    1688              : 
    1689              :   return spec;
    1690              : }
    1691              : 
    1692              : /* Restricts tree and type comparisons.  */
    1693              : int comparing_specializations;
    1694              : int comparing_dependent_aliases;
    1695              : 
    1696              : /* Whether we are comparing template arguments during partial ordering
    1697              :    (and therefore want the comparison to look through dependent alias
    1698              :    template specializations).  */
    1699              : 
    1700              : static int comparing_for_partial_ordering;
    1701              : 
    1702              : /* Returns true iff two spec_entry nodes are equivalent.  */
    1703              : 
    1704              : bool
    1705  12784315900 : spec_hasher::equal (spec_entry *e1, spec_entry *e2)
    1706              : {
    1707  12784315900 :   int equal;
    1708              : 
    1709  12784315900 :   ++comparing_specializations;
    1710  12784315900 :   ++comparing_dependent_aliases;
    1711  12784315900 :   ++processing_template_decl;
    1712  13580979862 :   equal = (e1->tmpl == e2->tmpl
    1713  12784315900 :            && comp_template_args (e1->args, e2->args));
    1714    796663962 :   if (equal && flag_concepts
    1715              :       /* tmpl could be a FIELD_DECL for a capture pack.  */
    1716    790669511 :       && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
    1717    790669511 :       && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
    1718     30659934 :       && uses_template_parms (e1->args))
    1719              :     {
    1720              :       /* Partial specializations of a variable template can be distinguished by
    1721              :          constraints.  */
    1722         6574 :       tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
    1723         6574 :       tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
    1724         6574 :       equal = equivalent_constraints (c1, c2);
    1725              :     }
    1726  12784315900 :   --processing_template_decl;
    1727  12784315900 :   --comparing_dependent_aliases;
    1728  12784315900 :   --comparing_specializations;
    1729              : 
    1730  12784315900 :   return equal;
    1731              : }
    1732              : 
    1733              : /* Returns a hash for a template TMPL and template arguments ARGS.  */
    1734              : 
    1735              : static hashval_t
    1736   1256300450 : hash_tmpl_and_args (tree tmpl, tree args)
    1737              : {
    1738   1256300450 :   hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
    1739   1256300450 :   return iterative_hash_template_arg (args, val);
    1740              : }
    1741              : 
    1742              : hashval_t
    1743   1221257477 : spec_hasher::hash (tree tmpl, tree args)
    1744              : {
    1745   1221257477 :   ++comparing_specializations;
    1746   1221257477 :   hashval_t val = hash_tmpl_and_args (tmpl, args);
    1747   1221257477 :   --comparing_specializations;
    1748   1221257477 :   return val;
    1749              : }
    1750              : 
    1751              : /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
    1752              :    ignoring SPEC.  */
    1753              : 
    1754              : hashval_t
    1755  11609193650 : spec_hasher::hash (spec_entry *e)
    1756              : {
    1757  11609193650 :   if (e->hash == 0)
    1758    770282103 :     e->hash = hash (e->tmpl, e->args);
    1759  11609193650 :   return e->hash;
    1760              : }
    1761              : 
    1762              : /* Recursively calculate a hash value for a template argument ARG, for use
    1763              :    in the hash tables of template specializations.   We must be
    1764              :    careful to (at least) skip the same entities template_args_equal
    1765              :    does.  */
    1766              : 
    1767              : hashval_t
    1768  12113327804 : iterative_hash_template_arg (tree arg, hashval_t val)
    1769              : {
    1770  12113327804 :   if (arg == NULL_TREE)
    1771    653045945 :     return iterative_hash_hashval_t (0, val);
    1772              : 
    1773  11460281859 :   if (!TYPE_P (arg))
    1774              :     /* Strip nop-like things, but not the same as STRIP_NOPS.  */
    1775   3599387297 :     while (CONVERT_EXPR_P (arg)
    1776              :            || TREE_CODE (arg) == NON_LVALUE_EXPR
    1777   3599387297 :            || class_nttp_const_wrapper_p (arg))
    1778      4139882 :       arg = TREE_OPERAND (arg, 0);
    1779              : 
    1780  11460281859 :   enum tree_code code = TREE_CODE (arg);
    1781              : 
    1782  11460281859 :   val = iterative_hash_hashval_t (code, val);
    1783              : 
    1784  11460281859 :   switch (code)
    1785              :     {
    1786            0 :     case ARGUMENT_PACK_SELECT:
    1787              :       /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
    1788              :          preserving it in a hash table, which is bad because it will change
    1789              :          meaning when gen_elem_of_pack_expansion_instantiation changes the
    1790              :          ARGUMENT_PACK_SELECT_INDEX.  */
    1791            0 :       gcc_unreachable ();
    1792              : 
    1793              :     case ERROR_MARK:
    1794              :       return val;
    1795              : 
    1796    396050256 :     case IDENTIFIER_NODE:
    1797    396050256 :       return iterative_hash_hashval_t (IDENTIFIER_HASH_VALUE (arg), val);
    1798              : 
    1799   1992785139 :     case TREE_VEC:
    1800   5353053789 :       for (tree elt : tree_vec_range (arg))
    1801   3360268650 :         val = iterative_hash_template_arg (elt, val);
    1802   1992785139 :       return val;
    1803              : 
    1804     77918555 :     case TYPE_PACK_EXPANSION:
    1805     77918555 :     case EXPR_PACK_EXPANSION:
    1806     77918555 :       val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
    1807     77918555 :       return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
    1808              : 
    1809         2795 :     case PACK_INDEX_TYPE:
    1810         2795 :     case PACK_INDEX_EXPR:
    1811         2795 :       val = iterative_hash_template_arg (PACK_INDEX_PACK (arg), val);
    1812         2795 :       return iterative_hash_template_arg (PACK_INDEX_INDEX (arg), val);
    1813              : 
    1814    302422066 :     case TYPE_ARGUMENT_PACK:
    1815    302422066 :     case NONTYPE_ARGUMENT_PACK:
    1816    302422066 :       return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
    1817              : 
    1818              :     case TREE_LIST:
    1819      1540286 :       for (; arg; arg = TREE_CHAIN (arg))
    1820       770146 :         val = iterative_hash_template_arg (TREE_VALUE (arg), val);
    1821              :       return val;
    1822              : 
    1823          145 :     case OVERLOAD:
    1824          302 :       for (lkp_iterator iter (arg); iter; ++iter)
    1825          157 :         val = iterative_hash_template_arg (*iter, val);
    1826          145 :       return val;
    1827              : 
    1828      4173603 :     case CONSTRUCTOR:
    1829      4173603 :       {
    1830      4173603 :         iterative_hash_template_arg (TREE_TYPE (arg), val);
    1831      5142264 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
    1832              :           {
    1833       429121 :             val = iterative_hash_template_arg (e.index, val);
    1834       429121 :             val = iterative_hash_template_arg (e.value, val);
    1835              :           }
    1836              :         return val;
    1837              :       }
    1838              : 
    1839      4458074 :     case PARM_DECL:
    1840      4458074 :       if (!DECL_ARTIFICIAL (arg))
    1841              :         {
    1842      4416383 :           val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
    1843      4416383 :           val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
    1844              :         }
    1845      4458074 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1846              : 
    1847    753297717 :     case TEMPLATE_DECL:
    1848    753297717 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
    1849     81520421 :         return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1850              :       break;
    1851              : 
    1852            0 :     case TARGET_EXPR:
    1853            0 :       return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
    1854              : 
    1855        23981 :     case PTRMEM_CST:
    1856        23981 :       val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
    1857        23981 :       return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
    1858              : 
    1859     20434067 :     case TEMPLATE_PARM_INDEX:
    1860     20434067 :       val = iterative_hash_template_arg
    1861     20434067 :         (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
    1862     20434067 :       val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
    1863     20434067 :       return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
    1864              : 
    1865      2192704 :     case TRAIT_EXPR:
    1866      2192704 :       val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
    1867      2192704 :       val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
    1868      2192704 :       return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
    1869              : 
    1870      1567314 :     case BASELINK:
    1871      1567314 :       val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
    1872              :                                          val);
    1873      1567314 :       return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
    1874      1567314 :                                           val);
    1875              : 
    1876        31675 :     case MODOP_EXPR:
    1877        31675 :       val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
    1878        31675 :       code = TREE_CODE (TREE_OPERAND (arg, 1));
    1879        31675 :       val = iterative_hash_object (code, val);
    1880        31675 :       return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
    1881              : 
    1882          221 :     case LAMBDA_EXPR:
    1883              :       /* [temp.over.link] Two lambda-expressions are never considered
    1884              :          equivalent.
    1885              : 
    1886              :          So just hash the closure type.  */
    1887          221 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1888              : 
    1889      8066900 :     case CAST_EXPR:
    1890      8066900 :     case IMPLICIT_CONV_EXPR:
    1891      8066900 :     case STATIC_CAST_EXPR:
    1892      8066900 :     case REINTERPRET_CAST_EXPR:
    1893      8066900 :     case CONST_CAST_EXPR:
    1894      8066900 :     case DYNAMIC_CAST_EXPR:
    1895      8066900 :     case NEW_EXPR:
    1896      8066900 :       val = iterative_hash_template_arg (TREE_TYPE (arg), val);
    1897              :       /* Now hash operands as usual.  */
    1898      8066900 :       break;
    1899              : 
    1900    115138982 :     case CALL_EXPR:
    1901    115138982 :       {
    1902    115138982 :         tree fn = CALL_EXPR_FN (arg);
    1903    115138982 :         if (tree name = call_expr_dependent_name (arg))
    1904              :           {
    1905    109441261 :             if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
    1906     55868738 :               val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
    1907              :             fn = name;
    1908              :           }
    1909    115138982 :         val = iterative_hash_template_arg (fn, val);
    1910    115138982 :         call_expr_arg_iterator ai;
    1911    174863960 :         for (tree x = first_call_expr_arg (arg, &ai); x;
    1912     59724978 :              x = next_call_expr_arg (&ai))
    1913     59724978 :           val = iterative_hash_template_arg (x, val);
    1914    115138982 :         return val;
    1915              :       }
    1916              : 
    1917         2823 :     case REFLECT_EXPR:
    1918         2823 :       val = iterative_hash_hashval_t (REFLECT_EXPR_KIND (arg), val);
    1919         2823 :       if (REFLECT_EXPR_KIND (arg) == REFLECT_BASE)
    1920              :         {
    1921           53 :           tree binfo = REFLECT_EXPR_HANDLE (arg);
    1922           53 :           val = iterative_hash_template_arg (BINFO_TYPE (binfo), val);
    1923           53 :           val = iterative_hash_template_arg (direct_base_derived (binfo), val);
    1924           53 :           return val;
    1925              :         }
    1926              :       /* Now hash operands as usual.  */
    1927              :       break;
    1928              : 
    1929              :     default:
    1930              :       break;
    1931              :     }
    1932              : 
    1933   8460791638 :   char tclass = TREE_CODE_CLASS (code);
    1934   8460791638 :   switch (tclass)
    1935              :     {
    1936   7491873737 :     case tcc_type:
    1937   7491873737 :       if (tree ats = alias_template_specialization_p (arg, nt_transparent))
    1938              :         {
    1939              :           // We want an alias specialization that survived strip_typedefs
    1940              :           // to hash differently from its TYPE_CANONICAL, to avoid hash
    1941              :           // collisions that compare as different in template_args_equal.
    1942              :           // These could be dependent specializations that strip_typedefs
    1943              :           // left alone for example.
    1944     35042973 :           tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
    1945     35042973 :           return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
    1946              :         }
    1947              : 
    1948   7456830764 :       switch (code)
    1949              :         {
    1950     63068947 :         case DECLTYPE_TYPE:
    1951     63068947 :           val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
    1952     63068947 :           break;
    1953              : 
    1954    300458849 :         case TYPENAME_TYPE:
    1955    300458849 :           if (comparing_specializations)
    1956              :             {
    1957              :               /* Hash the components that are relevant to TYPENAME_TYPE
    1958              :                  equivalence as determined by structural_comptypes.  We
    1959              :                  can only coherently do this when comparing_specializations
    1960              :                  is set, because otherwise structural_comptypes tries
    1961              :                  resolving TYPENAME_TYPE via the current instantiation.  */
    1962    262468112 :               tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
    1963    262468112 :               tree fullname = TYPENAME_TYPE_FULLNAME (arg);
    1964    262468112 :               val = iterative_hash_template_arg (context, val);
    1965    262468112 :               val = iterative_hash_template_arg (fullname, val);
    1966              :             }
    1967              :           break;
    1968              : 
    1969   7093302968 :         default:
    1970   7093302968 :           if (tree canonical = TYPE_CANONICAL (arg))
    1971   6943826935 :             val = iterative_hash_hashval_t (TYPE_HASH (canonical), val);
    1972    149476033 :           else if (tree ti = TYPE_TEMPLATE_INFO (arg))
    1973              :             {
    1974     18331536 :               val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
    1975     18331536 :               val = iterative_hash_template_arg (TI_ARGS (ti), val);
    1976              :             }
    1977              :           break;
    1978              :         }
    1979              : 
    1980              :       return val;
    1981              : 
    1982    906260305 :     case tcc_declaration:
    1983    906260305 :     case tcc_constant:
    1984    906260305 :       return iterative_hash_expr (arg, val);
    1985              : 
    1986     62657596 :     default:
    1987     62657596 :       gcc_assert (IS_EXPR_CODE_CLASS (tclass));
    1988    171970124 :       for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
    1989    109312528 :         val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
    1990              :       return val;
    1991              :     }
    1992              : }
    1993              : 
    1994              : /* Unregister the specialization SPEC as a specialization of TMPL.
    1995              :    Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
    1996              :    if the SPEC was listed as a specialization of TMPL.
    1997              : 
    1998              :    Note that SPEC has been ggc_freed, so we can't look inside it.  */
    1999              : 
    2000              : bool
    2001       629494 : reregister_specialization (tree spec, tree tinfo, tree new_spec)
    2002              : {
    2003       629494 :   spec_entry *entry;
    2004       629494 :   spec_entry elt;
    2005              : 
    2006       629494 :   elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
    2007       629494 :   elt.args = TI_ARGS (tinfo);
    2008              : 
    2009       629494 :   entry = decl_specializations->find (&elt);
    2010       629494 :   if (entry != NULL)
    2011              :     {
    2012       629494 :       gcc_assert (entry->spec == spec || entry->spec == new_spec);
    2013       629494 :       gcc_assert (new_spec != NULL_TREE);
    2014       629494 :       entry->spec = new_spec;
    2015              : 
    2016              :       /* We need to also remove SPEC from DECL_TEMPLATE_INSTANTIATIONS
    2017              :          if it was placed there.  */
    2018       629494 :       for (tree *inst = &DECL_TEMPLATE_INSTANTIATIONS (elt.tmpl);
    2019       779257 :            *inst; inst = &TREE_CHAIN (*inst))
    2020       149778 :         if (TREE_VALUE (*inst) == spec)
    2021              :           {
    2022           15 :             *inst = TREE_CHAIN (*inst);
    2023           15 :             break;
    2024              :           }
    2025              : 
    2026       629494 :       return 1;
    2027              :     }
    2028              : 
    2029              :   return 0;
    2030              : }
    2031              : 
    2032              : /* Like register_specialization, but for local declarations.  We are
    2033              :    registering SPEC, an instantiation of TMPL.  */
    2034              : 
    2035              : void
    2036     71987435 : register_local_specialization (tree spec, tree tmpl)
    2037              : {
    2038     71987435 :   gcc_assert (tmpl != spec);
    2039     71987435 :   local_specializations->put (tmpl, spec);
    2040     71987435 : }
    2041              : 
    2042              : /* Registers T as a specialization of itself.  This is used to preserve
    2043              :    the references to already-parsed parameters when instantiating
    2044              :    postconditions.  */
    2045              : 
    2046              : void
    2047          174 : register_local_identity (tree t)
    2048              : {
    2049          174 :   local_specializations->put (t, t);
    2050          174 : }
    2051              : 
    2052              : /* TYPE is a class type.  Returns true if TYPE is an explicitly
    2053              :    specialized class.  */
    2054              : 
    2055              : bool
    2056     17829904 : explicit_class_specialization_p (tree type)
    2057              : {
    2058     17829904 :   if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
    2059              :     return false;
    2060       493083 :   return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
    2061              : }
    2062              : 
    2063              : /* Populate OUT with the overload set FNS, going through all the
    2064              :    overloads for each element of the list.  Alternatively, FNS can be a
    2065              :    TREE_LIST, in which case it will be added together with all the
    2066              :    overloads.  */
    2067              : 
    2068              : static void
    2069         1488 : flatten_candidates (tree fns, auto_vec<tree> &out)
    2070              : {
    2071         1488 :   if (TREE_CODE (fns) == TREE_LIST)
    2072         1092 :     for (; fns; fns = TREE_CHAIN (fns))
    2073          737 :       flatten_candidates (TREE_VALUE (fns), out);
    2074              :   else
    2075         3771 :     for (tree cand : lkp_range (fns))
    2076         1505 :       out.safe_push (cand);
    2077         1488 : }
    2078              : 
    2079              : /* Print a note announcing a list of candidates.  */
    2080              : 
    2081              : void
    2082         5202 : inform_num_candidates (location_t loc, int num_candidates)
    2083              : {
    2084         5202 :   inform_n (loc,
    2085              :             num_candidates, "there is %i candidate", "there are %i candidates",
    2086              :             num_candidates);
    2087         5202 : }
    2088              : 
    2089              : /* Print the list of candidate FNS in an error message.  FNS can also
    2090              :    be a TREE_LIST of non-functions in the case of an ambiguous lookup.
    2091              : 
    2092              :    If CAND_CTXT is non-null, use it for each candidate to allow for
    2093              :    additional per-candidate notes.  */
    2094              : 
    2095              : void
    2096          751 : print_candidates (location_t error_loc,
    2097              :                   tree fns,
    2098              :                   candidate_context *cand_ctxt)
    2099              : {
    2100          751 :   auto_vec<tree> candidates;
    2101          751 :   flatten_candidates (fns, candidates);
    2102              : 
    2103          751 :   auto_diagnostic_nesting_level sentinel;
    2104              : 
    2105         1502 :   inform_num_candidates (error_loc, candidates.length ());
    2106              : 
    2107          751 :   auto_diagnostic_nesting_level sentinel2;
    2108              : 
    2109          751 :   if (candidates.length () == 1)
    2110              :     {
    2111          255 :       tree cand = candidates[0];
    2112          255 :       inform (DECL_SOURCE_LOCATION (cand), "candidate is: %#qD", cand);
    2113          255 :       if (cand_ctxt)
    2114           66 :         cand_ctxt->emit_any_notes_for_candidate (cand);
    2115              :     }
    2116              :   else
    2117              :     {
    2118          496 :       int idx = 0;
    2119         2738 :       for (tree cand : candidates)
    2120              :         {
    2121         1250 :           inform (DECL_SOURCE_LOCATION (cand), "candidate %i: %#qD",
    2122              :                   ++idx, cand);
    2123         1250 :           if (cand_ctxt)
    2124           35 :             cand_ctxt->emit_any_notes_for_candidate (cand);
    2125              :         }
    2126              :     }
    2127          751 : }
    2128              : 
    2129              : /* Get a (possibly) constrained template declaration for the
    2130              :    purpose of ordering candidates.  */
    2131              : static tree
    2132          242 : get_template_for_ordering (tree list)
    2133              : {
    2134          242 :   gcc_assert (TREE_CODE (list) == TREE_LIST);
    2135          242 :   tree f = TREE_VALUE (list);
    2136          242 :   if (f == NULL_TREE)
    2137              :     /* Also handle a list from resolve_address_of_overloaded_function with the
    2138              :        function in TREE_PURPOSE.  */
    2139          194 :     f = TREE_PURPOSE (list);
    2140          242 :   if (tree ti = DECL_TEMPLATE_INFO (f))
    2141           66 :     return TI_TEMPLATE (ti);
    2142              :   return f;
    2143              : }
    2144              : 
    2145              : /* Among candidates having the same signature, return the
    2146              :    most constrained or NULL_TREE if there is no best candidate.
    2147              :    If the signatures of candidates vary (e.g., template
    2148              :    specialization vs. member function), then there can be no
    2149              :    most constrained.
    2150              : 
    2151              :    Note that we don't compare constraints on the functions
    2152              :    themselves, but rather those of their templates. */
    2153              : tree
    2154          109 : most_constrained_function (tree candidates)
    2155              : {
    2156              :   // Try to find the best candidate in a first pass.
    2157          109 :   tree champ = candidates;
    2158          130 :   for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
    2159              :     {
    2160          109 :       int winner = more_constrained (get_template_for_ordering (champ),
    2161              :                                      get_template_for_ordering (c));
    2162          109 :       if (winner == -1)
    2163              :         champ = c; // The candidate is more constrained
    2164           97 :       else if (winner == 0)
    2165              :         return NULL_TREE; // Neither is more constrained
    2166              :     }
    2167              : 
    2168              :   // Verify that the champ is better than previous candidates.
    2169           33 :   for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
    2170           12 :     if (!more_constrained (get_template_for_ordering (champ),
    2171              :                            get_template_for_ordering (c)))
    2172              :       return NULL_TREE;
    2173              :   }
    2174              : 
    2175              :   return champ;
    2176              : }
    2177              : 
    2178              : 
    2179              : /* Returns the template (one of the functions given by TEMPLATE_ID)
    2180              :    which can be specialized to match the indicated DECL with the
    2181              :    explicit template args given in TEMPLATE_ID.  The DECL may be
    2182              :    NULL_TREE if none is available.  In that case, the functions in
    2183              :    TEMPLATE_ID are non-members.
    2184              : 
    2185              :    If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
    2186              :    specialization of a member template.
    2187              : 
    2188              :    The TEMPLATE_COUNT is the number of references to qualifying
    2189              :    template classes that appeared in the name of the function. See
    2190              :    check_explicit_specialization for a more accurate description.
    2191              : 
    2192              :    TSK indicates what kind of template declaration (if any) is being
    2193              :    declared.  TSK_TEMPLATE indicates that the declaration given by
    2194              :    DECL, though a FUNCTION_DECL, has template parameters, and is
    2195              :    therefore a template function.
    2196              : 
    2197              :    The template args (those explicitly specified and those deduced)
    2198              :    are output in a newly created vector *TARGS_OUT.
    2199              : 
    2200              :    If it is impossible to determine the result, an error message is
    2201              :    issued.  The error_mark_node is returned to indicate failure.  */
    2202              : 
    2203              : static tree
    2204      4368628 : determine_specialization (tree template_id,
    2205              :                           tree decl,
    2206              :                           tree* targs_out,
    2207              :                           int need_member_template,
    2208              :                           int template_count,
    2209              :                           tmpl_spec_kind tsk)
    2210              : {
    2211      4368628 :   tree fns;
    2212      4368628 :   tree targs;
    2213      4368628 :   tree explicit_targs;
    2214      4368628 :   tree candidates = NULL_TREE;
    2215              : 
    2216              :   /* A TREE_LIST of templates of which DECL may be a specialization.
    2217              :      The TREE_VALUE of each node is a TEMPLATE_DECL.  The
    2218              :      corresponding TREE_PURPOSE is the set of template arguments that,
    2219              :      when used to instantiate the template, would produce a function
    2220              :      with the signature of DECL.  */
    2221      4368628 :   tree templates = NULL_TREE;
    2222      4368628 :   int header_count;
    2223      4368628 :   cp_binding_level *b;
    2224              : 
    2225      4368628 :   *targs_out = NULL_TREE;
    2226              : 
    2227      4368628 :   if (template_id == error_mark_node || decl == error_mark_node)
    2228              :     return error_mark_node;
    2229              : 
    2230              :   /* We shouldn't be specializing a member template of an
    2231              :      unspecialized class template; we already gave an error in
    2232              :      check_specialization_scope, now avoid crashing.  */
    2233      4368625 :   if (!VAR_P (decl)
    2234      2597689 :       && template_count && DECL_CLASS_SCOPE_P (decl)
    2235      5208922 :       && template_class_depth (DECL_CONTEXT (decl)) > 0)
    2236              :     {
    2237            3 :       gcc_assert (errorcount);
    2238            3 :       return error_mark_node;
    2239              :     }
    2240              : 
    2241      4368622 :   fns = TREE_OPERAND (template_id, 0);
    2242      4368622 :   explicit_targs = TREE_OPERAND (template_id, 1);
    2243              : 
    2244      4368622 :   if (fns == error_mark_node)
    2245              :     return error_mark_node;
    2246              : 
    2247              :   /* Check for baselinks.  */
    2248      4368622 :   if (BASELINK_P (fns))
    2249            0 :     fns = BASELINK_FUNCTIONS (fns);
    2250              : 
    2251      4368622 :   if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
    2252              :     {
    2253           15 :       error_at (DECL_SOURCE_LOCATION (decl),
    2254              :                 "%qD is not a function template", fns);
    2255           15 :       return error_mark_node;
    2256              :     }
    2257      4368607 :   else if (VAR_P (decl) && !variable_template_p (fns))
    2258              :     {
    2259            3 :       error ("%qD is not a variable template", fns);
    2260            3 :       return error_mark_node;
    2261              :     }
    2262              : 
    2263              :   /* Count the number of template headers specified for this
    2264              :      specialization.  */
    2265      4368604 :   header_count = 0;
    2266      4368604 :   for (b = current_binding_level;
    2267      6693737 :        b->kind == sk_template_parms;
    2268      2325133 :        b = b->level_chain)
    2269      2325133 :     ++header_count;
    2270              : 
    2271      4368604 :   tree orig_fns = fns;
    2272      4368604 :   bool header_mismatch = false;
    2273              : 
    2274      4368604 :   if (variable_template_p (fns))
    2275              :     {
    2276      1770933 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
    2277      1770933 :       targs = coerce_template_parms (parms, explicit_targs, fns,
    2278              :                                      tf_warning_or_error);
    2279      1770933 :       if (targs != error_mark_node
    2280      1770933 :           && constraints_satisfied_p (fns, targs))
    2281      1770924 :         templates = tree_cons (targs, fns, templates);
    2282              :     }
    2283     17639564 :   else for (lkp_iterator iter (fns); iter; ++iter)
    2284              :     {
    2285     15041893 :       tree fn = *iter;
    2286              : 
    2287     15041893 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2288              :         {
    2289     14473183 :           tree decl_arg_types;
    2290     14473183 :           tree fn_arg_types;
    2291              : 
    2292              :           /* In case of explicit specialization, we need to check if
    2293              :              the number of template headers appearing in the specialization
    2294              :              is correct. This is usually done in check_explicit_specialization,
    2295              :              but the check done there cannot be exhaustive when specializing
    2296              :              member functions. Consider the following code:
    2297              : 
    2298              :              template <> void A<int>::f(int);
    2299              :              template <> template <> void A<int>::f(int);
    2300              : 
    2301              :              Assuming that A<int> is not itself an explicit specialization
    2302              :              already, the first line specializes "f" which is a non-template
    2303              :              member function, whilst the second line specializes "f" which
    2304              :              is a template member function. So both lines are syntactically
    2305              :              correct, and check_explicit_specialization does not reject
    2306              :              them.
    2307              : 
    2308              :              Here, we can do better, as we are matching the specialization
    2309              :              against the declarations. We count the number of template
    2310              :              headers, and we check if they match TEMPLATE_COUNT + 1
    2311              :              (TEMPLATE_COUNT is the number of qualifying template classes,
    2312              :              plus there must be another header for the member template
    2313              :              itself).
    2314              : 
    2315              :              Notice that if header_count is zero, this is not a
    2316              :              specialization but rather a template instantiation, so there
    2317              :              is no check we can perform here.  */
    2318     14473183 :           if (header_count && header_count != template_count + 1)
    2319              :             {
    2320            6 :               header_mismatch = true;
    2321            6 :               continue;
    2322              :             }
    2323              : 
    2324              :           /* Check that the number of template arguments at the
    2325              :              innermost level for DECL is the same as for FN.  */
    2326     14473177 :           if (current_binding_level->kind == sk_template_parms
    2327       185190 :               && !current_binding_level->explicit_spec_p
    2328     14474312 :               && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
    2329         1135 :                   != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
    2330              :                                       (current_template_parms))))
    2331            3 :             continue;
    2332              : 
    2333              :           /* DECL might be a specialization of FN.  */
    2334     14473174 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2335     14473174 :           fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
    2336              : 
    2337              :           /* For a non-static member function, we need to make sure
    2338              :              that the const qualification is the same.  Since
    2339              :              get_bindings does not try to merge the "this" parameter,
    2340              :              we must do the comparison explicitly.  */
    2341     14473174 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
    2342              :             {
    2343       389538 :               if (!same_type_p (TREE_VALUE (fn_arg_types),
    2344              :                                 TREE_VALUE (decl_arg_types)))
    2345          102 :                 continue;
    2346              : 
    2347              :               /* And the ref-qualification.  */
    2348       778872 :               if (type_memfn_rqual (TREE_TYPE (decl))
    2349       389436 :                   != type_memfn_rqual (TREE_TYPE (fn)))
    2350           38 :                 continue;
    2351              :             }
    2352              : 
    2353              :           /* Skip the "this" parameter and, for constructors of
    2354              :              classes with virtual bases, the VTT parameter.  A
    2355              :              full specialization of a constructor will have a VTT
    2356              :              parameter, but a template never will.  */
    2357     14473034 :           decl_arg_types
    2358     14473034 :             = skip_artificial_parms_for (decl, decl_arg_types);
    2359     14473034 :           fn_arg_types
    2360     14473034 :             = skip_artificial_parms_for (fn, fn_arg_types);
    2361              : 
    2362              :           /* Function templates cannot be specializations; there are
    2363              :              no partial specializations of functions.  Therefore, if
    2364              :              the type of DECL does not match FN, there is no
    2365              :              match.
    2366              : 
    2367              :              Note that it should never be the case that we have both
    2368              :              candidates added here, and for regular member functions
    2369              :              below. */
    2370     14473034 :           if (tsk == tsk_template)
    2371              :             {
    2372         1132 :               if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
    2373         1132 :                                         current_template_parms))
    2374            0 :                 continue;
    2375         1132 :               if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2376              :                                 TREE_TYPE (TREE_TYPE (fn))))
    2377            0 :                 continue;
    2378         1132 :               if (!compparms (fn_arg_types, decl_arg_types))
    2379            3 :                 continue;
    2380              : 
    2381         1129 :               tree freq = get_constraints (fn);
    2382         1129 :               tree dreq = get_constraints (decl);
    2383         1129 :               if (!freq != !dreq)
    2384            0 :                 continue;
    2385         1129 :               if (freq)
    2386              :                 {
    2387              :                   /* C++20 CA104: Substitute directly into the
    2388              :                      constraint-expression.  */
    2389           46 :                   tree fargs = DECL_TI_ARGS (fn);
    2390           46 :                   tsubst_flags_t complain = tf_none;
    2391           46 :                   freq = tsubst_constraint_info (freq, fargs, complain, fn);
    2392           46 :                   if (!cp_tree_equal (freq, dreq))
    2393           29 :                     continue;
    2394              :                 }
    2395              : 
    2396         1100 :               candidates = tree_cons (NULL_TREE, fn, candidates);
    2397         1100 :               continue;
    2398         1100 :             }
    2399              : 
    2400              :           /* See whether this function might be a specialization of this
    2401              :              template.  Suppress access control because we might be trying
    2402              :              to make this specialization a friend, and we have already done
    2403              :              access control for the declaration of the specialization.  */
    2404     14471902 :           push_deferring_access_checks (dk_no_check);
    2405     14471902 :           targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
    2406     14471902 :           pop_deferring_access_checks ();
    2407              : 
    2408     14471902 :           if (!targs)
    2409              :             /* We cannot deduce template arguments that when used to
    2410              :                specialize TMPL will produce DECL.  */
    2411     12284983 :             continue;
    2412              : 
    2413      2186919 :           if (uses_template_parms (targs))
    2414              :             /* We deduced something involving 'auto', which isn't a valid
    2415              :                template argument.  */
    2416            3 :             continue;
    2417              : 
    2418              :           /* Save this template, and the arguments deduced.  */
    2419      2186916 :           templates = tree_cons (targs, fn, templates);
    2420              :         }
    2421       568710 :       else if (need_member_template)
    2422              :         /* FN is an ordinary member function, and we need a
    2423              :            specialization of a member template.  */
    2424              :         ;
    2425       568664 :       else if (TREE_CODE (fn) != FUNCTION_DECL)
    2426              :         /* We can get IDENTIFIER_NODEs here in certain erroneous
    2427              :            cases.  */
    2428              :         ;
    2429       568664 :       else if (!DECL_FUNCTION_MEMBER_P (fn))
    2430              :         /* This is just an ordinary non-member function.  Nothing can
    2431              :            be a specialization of that.  */
    2432              :         ;
    2433       558193 :       else if (DECL_ARTIFICIAL (fn))
    2434              :         /* Cannot specialize functions that are created implicitly.  */
    2435              :         ;
    2436              :       else
    2437              :         {
    2438       558036 :           tree decl_arg_types;
    2439              : 
    2440              :           /* This is an ordinary member function.  However, since
    2441              :              we're here, we can assume its enclosing class is a
    2442              :              template class.  For example,
    2443              : 
    2444              :                template <typename T> struct S { void f(); };
    2445              :                template <> void S<int>::f() {}
    2446              : 
    2447              :              Here, S<int>::f is a non-template, but S<int> is a
    2448              :              template class.  If FN has the same type as DECL, we
    2449              :              might be in business.  */
    2450              : 
    2451       558036 :           if (!DECL_TEMPLATE_INFO (fn))
    2452              :             /* Its enclosing class is an explicit specialization
    2453              :                of a template class.  This is not a candidate.  */
    2454            6 :             continue;
    2455              : 
    2456       558030 :           if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2457              :                             TREE_TYPE (TREE_TYPE (fn))))
    2458              :             /* The return types differ.  */
    2459         1246 :             continue;
    2460              : 
    2461              :           /* Adjust the type of DECL in case FN is a static member.  */
    2462       556784 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2463       556784 :           if (DECL_STATIC_FUNCTION_P (fn)
    2464       556784 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2465          190 :             decl_arg_types = TREE_CHAIN (decl_arg_types);
    2466              : 
    2467       556784 :           if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
    2468              :                          decl_arg_types))
    2469       106308 :             continue;
    2470              : 
    2471       450476 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
    2472       900773 :               && (type_memfn_rqual (TREE_TYPE (decl))
    2473       450297 :                   != type_memfn_rqual (TREE_TYPE (fn))))
    2474            6 :             continue;
    2475              : 
    2476              :           // If the deduced arguments do not satisfy the constraints,
    2477              :           // this is not a candidate.
    2478       450470 :           if (flag_concepts && !constraints_satisfied_p (fn))
    2479           21 :             continue;
    2480              : 
    2481              :           // Add the candidate.
    2482       450449 :           candidates = tree_cons (NULL_TREE, fn, candidates);
    2483              :         }
    2484              :     }
    2485              : 
    2486      4368595 :   if (templates && TREE_CHAIN (templates))
    2487              :     {
    2488              :       /* We have:
    2489              : 
    2490              :            [temp.expl.spec]
    2491              : 
    2492              :            It is possible for a specialization with a given function
    2493              :            signature to be instantiated from more than one function
    2494              :            template.  In such cases, explicit specification of the
    2495              :            template arguments must be used to uniquely identify the
    2496              :            function template specialization being specialized.
    2497              : 
    2498              :          Note that here, there's no suggestion that we're supposed to
    2499              :          determine which of the candidate templates is most
    2500              :          specialized.  However, we, also have:
    2501              : 
    2502              :            [temp.func.order]
    2503              : 
    2504              :            Partial ordering of overloaded function template
    2505              :            declarations is used in the following contexts to select
    2506              :            the function template to which a function template
    2507              :            specialization refers:
    2508              : 
    2509              :            -- when an explicit specialization refers to a function
    2510              :               template.
    2511              : 
    2512              :          So, we do use the partial ordering rules, at least for now.
    2513              :          This extension can only serve to make invalid programs valid,
    2514              :          so it's safe.  And, there is strong anecdotal evidence that
    2515              :          the committee intended the partial ordering rules to apply;
    2516              :          the EDG front end has that behavior, and John Spicer claims
    2517              :          that the committee simply forgot to delete the wording in
    2518              :          [temp.expl.spec].  */
    2519        20519 :       tree tmpl = most_specialized_instantiation (templates);
    2520        20519 :       if (tmpl != error_mark_node)
    2521              :         {
    2522        20516 :           templates = tmpl;
    2523        20516 :           TREE_CHAIN (templates) = NULL_TREE;
    2524              :         }
    2525              :     }
    2526              : 
    2527              :   // Concepts allows multiple declarations of member functions
    2528              :   // with the same signature. Like above, we need to rely on
    2529              :   // on the partial ordering of those candidates to determine which
    2530              :   // is the best.
    2531      4368604 :   if (flag_concepts && candidates && TREE_CHAIN (candidates))
    2532              :     {
    2533           12 :       if (tree cand = most_constrained_function (candidates))
    2534              :         {
    2535           12 :           candidates = cand;
    2536           12 :           TREE_CHAIN (cand) = NULL_TREE;
    2537              :         }
    2538              :     }
    2539              : 
    2540      4368604 :   if (templates == NULL_TREE && candidates == NULL_TREE)
    2541              :     {
    2542          219 :       auto_diagnostic_group d;
    2543          219 :       error ("template-id %qD for %q+D does not match any template "
    2544              :              "declaration", template_id, decl);
    2545          219 :       if (header_mismatch)
    2546            3 :         inform (DECL_SOURCE_LOCATION (decl),
    2547              :                 "saw %d %<template<>%>, need %d for "
    2548              :                 "specializing a member function template",
    2549              :                 header_count, template_count + 1);
    2550          219 :       print_candidates (DECL_SOURCE_LOCATION (decl), orig_fns);
    2551          219 :       return error_mark_node;
    2552          219 :     }
    2553      3916848 :   else if ((templates && TREE_CHAIN (templates))
    2554      4368382 :            || (candidates && TREE_CHAIN (candidates))
    2555      8736767 :            || (templates && candidates))
    2556              :     {
    2557            3 :       auto_diagnostic_group d;
    2558            3 :       error ("ambiguous template specialization %qD for %q+D",
    2559              :              template_id, decl);
    2560            3 :       candidates = chainon (candidates, templates);
    2561            3 :       print_candidates (input_location, candidates);
    2562            3 :       return error_mark_node;
    2563            3 :     }
    2564              : 
    2565              :   /* We have one, and exactly one, match.  */
    2566      4368382 :   if (candidates)
    2567              :     {
    2568       451537 :       tree fn = TREE_VALUE (candidates);
    2569       451537 :       *targs_out = copy_node (DECL_TI_ARGS (fn));
    2570              : 
    2571              :       /* Propagate the candidate's constraints to the declaration.  */
    2572       451537 :       if (tsk != tsk_template)
    2573       450437 :         set_constraints (decl, get_constraints (fn));
    2574              : 
    2575              :       /* DECL is a re-declaration or partial instantiation of a template
    2576              :          function.  */
    2577       451537 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2578              :         return fn;
    2579              :       /* It was a specialization of an ordinary member function in a
    2580              :          template class.  */
    2581       450437 :       return DECL_TI_TEMPLATE (fn);
    2582              :     }
    2583              : 
    2584              :   /* It was a specialization of a template.  */
    2585      3916845 :   tree tmpl = TREE_VALUE (templates);
    2586      3916845 :   *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
    2587              : 
    2588              :   /* Propagate the template's constraints to the declaration.  */
    2589      3916845 :   if (tsk != tsk_template)
    2590      2969907 :     set_constraints (decl, get_constraints (tmpl));
    2591              : 
    2592              :   return tmpl;
    2593              : }
    2594              : 
    2595              : /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
    2596              :    but with the default argument values filled in from those in the
    2597              :    TMPL_TYPES.  */
    2598              : 
    2599              : static tree
    2600       203437 : copy_default_args_to_explicit_spec_1 (tree spec_types,
    2601              :                                       tree tmpl_types)
    2602              : {
    2603       203437 :   tree new_spec_types;
    2604              : 
    2605       203437 :   if (!spec_types)
    2606              :     return NULL_TREE;
    2607              : 
    2608       203437 :   if (spec_types == void_list_node)
    2609              :     return void_list_node;
    2610              : 
    2611              :   /* Substitute into the rest of the list.  */
    2612       121680 :   new_spec_types =
    2613       121680 :     copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
    2614       121680 :                                           TREE_CHAIN (tmpl_types));
    2615              : 
    2616              :   /* Add the default argument for this parameter.  */
    2617       243360 :   return hash_tree_cons (TREE_PURPOSE (tmpl_types),
    2618       121680 :                          TREE_VALUE (spec_types),
    2619       121680 :                          new_spec_types);
    2620              : }
    2621              : 
    2622              : /* DECL is an explicit specialization.  Replicate default arguments
    2623              :    from the template it specializes.  (That way, code like:
    2624              : 
    2625              :      template <class T> void f(T = 3);
    2626              :      template <> void f(double);
    2627              :      void g () { f (); }
    2628              : 
    2629              :    works, as required.)  An alternative approach would be to look up
    2630              :    the correct default arguments at the call-site, but this approach
    2631              :    is consistent with how implicit instantiations are handled.  */
    2632              : 
    2633              : static void
    2634       629482 : copy_default_args_to_explicit_spec (tree decl)
    2635              : {
    2636       629482 :   tree tmpl;
    2637       629482 :   tree spec_types;
    2638       629482 :   tree tmpl_types;
    2639       629482 :   tree new_spec_types;
    2640       629482 :   tree old_type;
    2641       629482 :   tree new_type;
    2642       629482 :   tree t;
    2643       629482 :   tree object_type = NULL_TREE;
    2644       629482 :   tree in_charge = NULL_TREE;
    2645       629482 :   tree vtt = NULL_TREE;
    2646              : 
    2647              :   /* See if there's anything we need to do.  */
    2648       629482 :   tmpl = DECL_TI_TEMPLATE (decl);
    2649       629482 :   tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
    2650      2470371 :   for (t = tmpl_types; t; t = TREE_CHAIN (t))
    2651      1922646 :     if (TREE_PURPOSE (t))
    2652              :       break;
    2653       629482 :   if (!t)
    2654              :     return;
    2655              : 
    2656        81757 :   old_type = TREE_TYPE (decl);
    2657        81757 :   spec_types = TYPE_ARG_TYPES (old_type);
    2658              : 
    2659        81757 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2660              :     {
    2661              :       /* Remove the this pointer, but remember the object's type for
    2662              :          CV quals.  */
    2663        81757 :       object_type = TREE_TYPE (TREE_VALUE (spec_types));
    2664        81757 :       spec_types = TREE_CHAIN (spec_types);
    2665        81757 :       tmpl_types = TREE_CHAIN (tmpl_types);
    2666              : 
    2667        81757 :       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
    2668              :         {
    2669              :           /* DECL may contain more parameters than TMPL due to the extra
    2670              :              in-charge parameter in constructors and destructors.  */
    2671            0 :           in_charge = spec_types;
    2672            0 :           spec_types = TREE_CHAIN (spec_types);
    2673              :         }
    2674        81757 :       if (DECL_HAS_VTT_PARM_P (decl))
    2675              :         {
    2676            0 :           vtt = spec_types;
    2677            0 :           spec_types = TREE_CHAIN (spec_types);
    2678              :         }
    2679              :     }
    2680              : 
    2681              :   /* Compute the merged default arguments.  */
    2682        81757 :   new_spec_types =
    2683        81757 :     copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
    2684              : 
    2685              :   /* Compute the new FUNCTION_TYPE.  */
    2686        81757 :   if (object_type)
    2687              :     {
    2688        81757 :       if (vtt)
    2689            0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
    2690            0 :                                          TREE_VALUE (vtt),
    2691              :                                          new_spec_types);
    2692              : 
    2693        81757 :       if (in_charge)
    2694              :         /* Put the in-charge parameter back.  */
    2695            0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
    2696            0 :                                          TREE_VALUE (in_charge),
    2697              :                                          new_spec_types);
    2698              : 
    2699        81757 :       new_type = build_method_type_directly (object_type,
    2700        81757 :                                              TREE_TYPE (old_type),
    2701              :                                              new_spec_types);
    2702              :     }
    2703              :   else
    2704            0 :     new_type = cp_build_function_type (TREE_TYPE (old_type), new_spec_types);
    2705        81757 :   new_type = cp_build_type_attribute_variant (new_type,
    2706        81757 :                                               TYPE_ATTRIBUTES (old_type));
    2707        81757 :   new_type = cxx_copy_lang_qualifiers (new_type, old_type);
    2708              : 
    2709        81757 :   TREE_TYPE (decl) = new_type;
    2710              : }
    2711              : 
    2712              : /* Return the number of template headers we expect to see for a definition
    2713              :    or specialization of CTYPE or one of its non-template members.  */
    2714              : 
    2715              : int
    2716     34024910 : num_template_headers_for_class (tree ctype)
    2717              : {
    2718     34024910 :   int num_templates = 0;
    2719              : 
    2720     51621902 :   while (ctype && CLASS_TYPE_P (ctype))
    2721              :     {
    2722              :       /* You're supposed to have one `template <...>' for every
    2723              :          template class, but you don't need one for a full
    2724              :          specialization.  For example:
    2725              : 
    2726              :          template <class T> struct S{};
    2727              :          template <> struct S<int> { void f(); };
    2728              :          void S<int>::f () {}
    2729              : 
    2730              :          is correct; there shouldn't be a `template <>' for the
    2731              :          definition of `S<int>::f'.  */
    2732     24148881 :       if (!CLASSTYPE_TEMPLATE_INFO (ctype))
    2733              :         /* If CTYPE does not have template information of any
    2734              :            kind,  then it is not a template, nor is it nested
    2735              :            within a template.  */
    2736              :         break;
    2737     17829904 :       if (explicit_class_specialization_p (ctype))
    2738              :         break;
    2739     17596992 :       if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
    2740     17287818 :         ++num_templates;
    2741              : 
    2742     17596992 :       ctype = TYPE_CONTEXT (ctype);
    2743              :     }
    2744              : 
    2745     34024910 :   return num_templates;
    2746              : }
    2747              : 
    2748              : /* Do a simple sanity check on the template headers that precede the
    2749              :    variable declaration DECL.  */
    2750              : 
    2751              : void
    2752      4671257 : check_template_variable (tree decl)
    2753              : {
    2754      4671257 :   tree ctx = CP_DECL_CONTEXT (decl);
    2755      4671257 :   int wanted = num_template_headers_for_class (ctx);
    2756      9342511 :   if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
    2757      9342502 :       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
    2758              :     {
    2759      4342666 :       if (cxx_dialect < cxx14)
    2760         4037 :         pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
    2761              :                  "variable templates only available with "
    2762              :                  "%<-std=c++14%> or %<-std=gnu++14%>");
    2763              : 
    2764              :       // Namespace-scope variable templates should have a template header.
    2765      4342666 :       ++wanted;
    2766              :     }
    2767      4671257 :   if (template_header_count > wanted)
    2768              :     {
    2769           12 :       auto_diagnostic_group d;
    2770           12 :       bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    2771              :                              "too many template headers for %qD "
    2772              :                              "(should be %d)",
    2773              :                              decl, wanted);
    2774           12 :       if (warned && CLASS_TYPE_P (ctx)
    2775           21 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
    2776            3 :         inform (DECL_SOURCE_LOCATION (decl),
    2777              :                 "members of an explicitly specialized class are defined "
    2778              :                 "without a template header");
    2779           12 :     }
    2780      4671257 : }
    2781              : 
    2782              : /* An explicit specialization whose declarator-id or class-head-name is not
    2783              :    qualified shall be declared in the nearest enclosing namespace of the
    2784              :    template, or, if the namespace is inline (7.3.1), any namespace from its
    2785              :    enclosing namespace set.
    2786              : 
    2787              :    If the name declared in the explicit instantiation is an unqualified name,
    2788              :    the explicit instantiation shall appear in the namespace where its template
    2789              :    is declared or, if that namespace is inline (7.3.1), any namespace from its
    2790              :    enclosing namespace set.  */
    2791              : 
    2792              : void
    2793     15498963 : check_unqualified_spec_or_inst (tree t, location_t loc)
    2794              : {
    2795     15498963 :   tree tmpl = most_general_template (t);
    2796     30997926 :   if (DECL_NAMESPACE_SCOPE_P (tmpl)
    2797     30467273 :       && !is_nested_namespace (current_namespace,
    2798     14968310 :                                CP_DECL_CONTEXT (tmpl), true))
    2799              :     {
    2800           17 :       if (processing_specialization)
    2801            6 :         permerror (loc, "explicit specialization of %qD outside its "
    2802              :                    "namespace must use a nested-name-specifier", tmpl);
    2803           11 :       else if (processing_explicit_instantiation
    2804           11 :                && cxx_dialect >= cxx11)
    2805              :         /* This was allowed in C++98, so only pedwarn.  */
    2806            9 :         pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
    2807              :                  "outside its namespace must use a nested-name-"
    2808              :                  "specifier", tmpl);
    2809              :     }
    2810     15498963 : }
    2811              : 
    2812              : /* Warn for a template specialization SPEC that is missing some of a set
    2813              :    of function or type attributes that the template TEMPL is declared with.
    2814              :    ATTRLIST is a list of additional attributes that SPEC should be taken
    2815              :    to ultimately be declared with.  */
    2816              : 
    2817              : static void
    2818      1453344 : warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
    2819              : {
    2820      1453344 :   if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    2821              :     tmpl = DECL_TEMPLATE_RESULT (tmpl);
    2822              : 
    2823              :   /* Avoid warning if the difference between the primary and
    2824              :      the specialization is not in one of the attributes below.  */
    2825      1453344 :   const char* const blacklist[] = {
    2826              :     "alloc_align", "alloc_size", "assume_aligned", "format",
    2827              :     "format_arg", "malloc", "nonnull", NULL
    2828              :   };
    2829              : 
    2830              :   /* Put together a list of the black listed attributes that the primary
    2831              :      template is declared with that the specialization is not, in case
    2832              :      it's not apparent from the most recent declaration of the primary.  */
    2833      1453344 :   auto_vec<const char *> mismatches;
    2834      1453344 :   unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
    2835              :                                                  blacklist, mismatches);
    2836              : 
    2837      1453344 :   if (!nattrs)
    2838      1453317 :     return;
    2839              : 
    2840           27 :   auto_diagnostic_group d;
    2841           27 :   if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
    2842              :                   "explicit specialization %q#D may be missing attributes",
    2843              :                   spec))
    2844              :     {
    2845           27 :       pp_markup::comma_separated_quoted_strings e (mismatches);
    2846           51 :       inform (DECL_SOURCE_LOCATION (tmpl),
    2847              :               nattrs > 1
    2848              :               ? G_("missing primary template attributes %e")
    2849              :               : G_("missing primary template attribute %e"),
    2850              :               &e);
    2851           27 :     }
    2852      1453344 : }
    2853              : 
    2854              : /* Check to see if the function just declared, as indicated in
    2855              :    DECLARATOR, and in DECL, is a specialization of a function
    2856              :    template.  We may also discover that the declaration is an explicit
    2857              :    instantiation at this point.
    2858              : 
    2859              :    Returns DECL, or an equivalent declaration that should be used
    2860              :    instead if all goes well.  Issues an error message if something is
    2861              :    amiss.  Returns error_mark_node if the error is not easily
    2862              :    recoverable.
    2863              : 
    2864              :    FLAGS is a bitmask consisting of the following flags:
    2865              : 
    2866              :    2: The function has a definition.
    2867              :    4: The function is a friend.
    2868              : 
    2869              :    The TEMPLATE_COUNT is the number of references to qualifying
    2870              :    template classes that appeared in the name of the function.  For
    2871              :    example, in
    2872              : 
    2873              :      template <class T> struct S { void f(); };
    2874              :      void S<int>::f();
    2875              : 
    2876              :    the TEMPLATE_COUNT would be 1.  However, explicitly specialized
    2877              :    classes are not counted in the TEMPLATE_COUNT, so that in
    2878              : 
    2879              :      template <class T> struct S {};
    2880              :      template <> struct S<int> { void f(); }
    2881              :      template <> void S<int>::f();
    2882              : 
    2883              :    the TEMPLATE_COUNT would be 0.  (Note that this declaration is
    2884              :    invalid; there should be no template <>.)
    2885              : 
    2886              :    If the function is a specialization, it is marked as such via
    2887              :    DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
    2888              :    is set up correctly, and it is added to the list of specializations
    2889              :    for that template.  */
    2890              : 
    2891              : tree
    2892    245042796 : check_explicit_specialization (tree declarator,
    2893              :                                tree decl,
    2894              :                                int template_count,
    2895              :                                int flags,
    2896              :                                tree attrlist)
    2897              : {
    2898    245042796 :   int have_def = flags & 2;
    2899    245042796 :   int is_friend = flags & 4;
    2900    245042796 :   bool is_concept = flags & 8;
    2901    245042796 :   int specialization = 0;
    2902    245042796 :   int explicit_instantiation = 0;
    2903    245042796 :   int member_specialization = 0;
    2904    245042796 :   tree ctype = DECL_CLASS_CONTEXT (decl);
    2905    245042796 :   tree dname = DECL_NAME (decl);
    2906    245042796 :   tmpl_spec_kind tsk;
    2907              : 
    2908    245042796 :   if (is_friend)
    2909              :     {
    2910        77582 :       if (!processing_specialization)
    2911              :         tsk = tsk_none;
    2912              :       else
    2913              :         tsk = tsk_excessive_parms;
    2914              :     }
    2915              :   else
    2916    244965214 :     tsk = current_tmpl_spec_kind (template_count);
    2917              : 
    2918    244965214 :   switch (tsk)
    2919              :     {
    2920    190656275 :     case tsk_none:
    2921    190656275 :       if (processing_specialization && !VAR_P (decl))
    2922              :         {
    2923       448274 :           specialization = 1;
    2924       448274 :           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2925              :         }
    2926    190208001 :       else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
    2927    190208001 :                || (DECL_LANG_SPECIFIC (decl)
    2928    128514958 :                    && DECL_IMPLICIT_INSTANTIATION (decl)))
    2929              :         {
    2930        77594 :           if (is_friend)
    2931              :             /* This could be something like:
    2932              : 
    2933              :                template <class T> void f(T);
    2934              :                class S { friend void f<>(int); }  */
    2935              :             specialization = 1;
    2936              :           else
    2937              :             {
    2938              :               /* This case handles bogus declarations like template <>
    2939              :                  template <class T> void f<int>(); */
    2940              : 
    2941           12 :               error_at (cp_expr_loc_or_input_loc (declarator),
    2942              :                         "template-id %qE in declaration of primary template",
    2943              :                         declarator);
    2944           12 :               return decl;
    2945              :             }
    2946              :         }
    2947              :       break;
    2948              : 
    2949            0 :     case tsk_invalid_member_spec:
    2950              :       /* The error has already been reported in
    2951              :          check_specialization_scope.  */
    2952            0 :       return error_mark_node;
    2953              : 
    2954            0 :     case tsk_invalid_expl_inst:
    2955            0 :       error ("template parameter list used in explicit instantiation");
    2956              : 
    2957              :       /* Fall through.  */
    2958              : 
    2959      1940224 :     case tsk_expl_inst:
    2960      1940224 :       if (have_def)
    2961            0 :         error ("definition provided for explicit instantiation");
    2962              : 
    2963              :       explicit_instantiation = 1;
    2964              :       break;
    2965              : 
    2966            0 :     case tsk_excessive_parms:
    2967            0 :     case tsk_insufficient_parms:
    2968            0 :       if (tsk == tsk_excessive_parms)
    2969            0 :         error ("too many template parameter lists in declaration of %qD",
    2970              :                decl);
    2971            0 :       else if (template_header_count)
    2972            0 :         error("too few template parameter lists in declaration of %qD", decl);
    2973              :       else
    2974            0 :         error("explicit specialization of %qD must be introduced by "
    2975              :               "%<template <>%>", decl);
    2976              : 
    2977              :       /* Fall through.  */
    2978       927594 :     case tsk_expl_spec:
    2979       927594 :       if (is_concept)
    2980            0 :         error ("explicit specialization declared %<concept%>");
    2981              : 
    2982       927594 :       if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
    2983              :         /* In cases like template<> constexpr bool v = true;
    2984              :            We'll give an error in check_template_variable.  */
    2985              :         break;
    2986              : 
    2987       927582 :       SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2988       927582 :       if (ctype)
    2989              :         member_specialization = 1;
    2990              :       else
    2991              :         specialization = 1;
    2992              :       break;
    2993              : 
    2994     51518703 :     case tsk_template:
    2995     51518703 :       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
    2996              :         {
    2997              :           /* This case handles bogus declarations like template <>
    2998              :              template <class T> void f<int>(); */
    2999              : 
    3000       946965 :           if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
    3001           15 :             error_at (cp_expr_loc_or_input_loc (declarator),
    3002              :                       "template-id %qE in declaration of primary template",
    3003              :                       declarator);
    3004       946953 :           else if (variable_template_p (TREE_OPERAND (declarator, 0)))
    3005              :             {
    3006              :               /* Partial specialization of variable template.  */
    3007       946938 :               SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    3008       946938 :               specialization = 1;
    3009       946938 :               goto ok;
    3010              :             }
    3011           15 :           else if (cxx_dialect < cxx14)
    3012            5 :             error_at (cp_expr_loc_or_input_loc (declarator),
    3013              :                       "non-type partial specialization %qE "
    3014              :                       "is not allowed", declarator);
    3015              :           else
    3016           16 :             error_at (cp_expr_loc_or_input_loc (declarator),
    3017              :                       "non-class, non-variable partial specialization %qE "
    3018              :                       "is not allowed", declarator);
    3019           27 :           return decl;
    3020       946938 :         ok:;
    3021              :         }
    3022              : 
    3023     51518676 :       if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
    3024              :         /* This is a specialization of a member template, without
    3025              :            specialization the containing class.  Something like:
    3026              : 
    3027              :              template <class T> struct S {
    3028              :                template <class U> void f (U);
    3029              :              };
    3030              :              template <> template <class U> void S<int>::f(U) {}
    3031              : 
    3032              :            That's a specialization -- but of the entire template.  */
    3033              :         specialization = 1;
    3034              :       break;
    3035              : 
    3036            0 :     default:
    3037            0 :       gcc_unreachable ();
    3038              :     }
    3039              : 
    3040       448274 :   if ((specialization || member_specialization)
    3041              :       /* This doesn't apply to variable templates.  */
    3042     52972114 :       && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
    3043              :     {
    3044       630676 :       tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
    3045      2678653 :       for (; t; t = TREE_CHAIN (t))
    3046      2047980 :         if (TREE_PURPOSE (t))
    3047              :           {
    3048            3 :             permerror (input_location,
    3049              :                        "default argument specified in explicit specialization");
    3050            3 :             break;
    3051              :           }
    3052              :     }
    3053              : 
    3054    245042757 :   if (specialization || member_specialization || explicit_instantiation)
    3055              :     {
    3056      4341715 :       tree tmpl = NULL_TREE;
    3057      4341715 :       tree targs = NULL_TREE;
    3058      4341715 :       bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
    3059      4341715 :       bool found_hidden = false;
    3060              : 
    3061              :       /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
    3062      4341715 :       if (!was_template_id)
    3063              :         {
    3064      1647684 :           tree fns;
    3065              : 
    3066      1647684 :           gcc_assert (identifier_p (declarator));
    3067      1647684 :           if (ctype)
    3068              :             fns = dname;
    3069              :           else
    3070              :             {
    3071              :               /* If there is no class context, the explicit instantiation
    3072              :                  must be at namespace scope.  */
    3073       807368 :               gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
    3074              : 
    3075              :               /* Find the namespace binding, using the declaration
    3076              :                  context.  */
    3077       807368 :               fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
    3078              :                                            LOOK_want::NORMAL, true);
    3079       807368 :               if (fns == error_mark_node)
    3080              :                 {
    3081              :                   /* If lookup fails, look for a friend declaration so we can
    3082              :                      give a better diagnostic.  */
    3083           22 :                   fns = (lookup_qualified_name
    3084           22 :                          (CP_DECL_CONTEXT (decl), dname,
    3085              :                           LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
    3086              :                           /*complain*/true));
    3087           22 :                   found_hidden = true;
    3088              :                 }
    3089              : 
    3090       807368 :               if (fns == error_mark_node || !is_overloaded_fn (fns))
    3091              :                 {
    3092           22 :                   error ("%qD is not a template function", dname);
    3093           22 :                   fns = error_mark_node;
    3094              :                 }
    3095              :             }
    3096              : 
    3097      1647684 :           declarator = lookup_template_function (fns, NULL_TREE);
    3098              :         }
    3099              : 
    3100      4341715 :       if (declarator == error_mark_node)
    3101      1941433 :         return error_mark_node;
    3102              : 
    3103      4341693 :       if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
    3104              :         {
    3105            0 :           if (!explicit_instantiation)
    3106              :             /* A specialization in class scope.  This is invalid,
    3107              :                but the error will already have been flagged by
    3108              :                check_specialization_scope.  */
    3109              :             return error_mark_node;
    3110              :           else
    3111              :             {
    3112              :               /* It's not valid to write an explicit instantiation in
    3113              :                  class scope, e.g.:
    3114              : 
    3115              :                    class C { template void f(); }
    3116              : 
    3117              :                    This case is caught by the parser.  However, on
    3118              :                    something like:
    3119              : 
    3120              :                    template class C { void f(); };
    3121              : 
    3122              :                    (which is invalid) we can get here.  The error will be
    3123              :                    issued later.  */
    3124            0 :               ;
    3125              :             }
    3126              : 
    3127            0 :           return decl;
    3128              :         }
    3129      4341693 :       else if (ctype != NULL_TREE
    3130      4341693 :                && (identifier_p (TREE_OPERAND (declarator, 0))))
    3131              :         {
    3132              :           // We'll match variable templates in start_decl.
    3133       840316 :           if (VAR_P (decl))
    3134              :             return decl;
    3135              : 
    3136              :           /* Find the list of functions in ctype that have the same
    3137              :              name as the declared function.  */
    3138       840242 :           tree name = TREE_OPERAND (declarator, 0);
    3139              : 
    3140       840242 :           if (constructor_name_p (name, ctype))
    3141              :             {
    3142            0 :               if (DECL_CONSTRUCTOR_P (decl)
    3143            0 :                   ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
    3144            0 :                   : !CLASSTYPE_DESTRUCTOR (ctype))
    3145              :                 {
    3146              :                   /* From [temp.expl.spec]:
    3147              : 
    3148              :                      If such an explicit specialization for the member
    3149              :                      of a class template names an implicitly-declared
    3150              :                      special member function (clause _special_), the
    3151              :                      program is ill-formed.
    3152              : 
    3153              :                      Similar language is found in [temp.explicit].  */
    3154            0 :                   error ("specialization of implicitly-declared special member function");
    3155            0 :                   return error_mark_node;
    3156              :                 }
    3157              : 
    3158            0 :               name = DECL_NAME (decl);
    3159              :             }
    3160              : 
    3161              :           /* For a type-conversion operator, We might be looking for
    3162              :              `operator int' which will be a specialization of
    3163              :              `operator T'.  Grab all the conversion operators, and
    3164              :              then select from them.  */
    3165       840242 :           tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
    3166              :                                         ? conv_op_identifier : name);
    3167              : 
    3168       840242 :           if (fns == NULL_TREE)
    3169              :             {
    3170            0 :               error ("no member function %qD declared in %qT", name, ctype);
    3171            0 :               return error_mark_node;
    3172              :             }
    3173              :           else
    3174       840242 :             TREE_OPERAND (declarator, 0) = fns;
    3175              :         }
    3176              : 
    3177              :       /* Figure out what exactly is being specialized at this point.
    3178              :          Note that for an explicit instantiation, even one for a
    3179              :          member function, we cannot tell a priori whether the
    3180              :          instantiation is for a member template, or just a member
    3181              :          function of a template class.  Even if a member template is
    3182              :          being instantiated, the member template arguments may be
    3183              :          elided if they can be deduced from the rest of the
    3184              :          declaration.  */
    3185      4341619 :       tmpl = determine_specialization (declarator, decl,
    3186              :                                        &targs,
    3187              :                                        member_specialization,
    3188              :                                        template_count,
    3189              :                                        tsk);
    3190              : 
    3191      4341619 :       if (!tmpl || tmpl == error_mark_node)
    3192              :         /* We couldn't figure out what this declaration was
    3193              :            specializing.  */
    3194          237 :         return error_mark_node;
    3195              :       else
    3196              :         {
    3197      4341382 :           if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
    3198              :             {
    3199            9 :               auto_diagnostic_group d;
    3200            9 :               if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    3201              :                            "friend declaration %qD is not visible to "
    3202              :                            "explicit specialization", tmpl))
    3203            9 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    3204              :                         "friend declaration here");
    3205            9 :             }
    3206              : 
    3207      4341382 :           if (!ctype && !is_friend
    3208      4341382 :               && CP_DECL_CONTEXT (decl) == current_namespace)
    3209      3367620 :             check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
    3210              : 
    3211      4341382 :           tree gen_tmpl = most_general_template (tmpl);
    3212              : 
    3213      4341382 :           if (explicit_instantiation)
    3214              :             {
    3215              :               /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
    3216              :                  is done by do_decl_instantiation later.  */
    3217              : 
    3218      1940000 :               int arg_depth = TMPL_ARGS_DEPTH (targs);
    3219      1940000 :               int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
    3220              : 
    3221      1940000 :               if (arg_depth > parm_depth)
    3222              :                 {
    3223              :                   /* If TMPL is not the most general template (for
    3224              :                      example, if TMPL is a friend template that is
    3225              :                      injected into namespace scope), then there will
    3226              :                      be too many levels of TARGS.  Remove some of them
    3227              :                      here.  */
    3228       388646 :                   int i;
    3229       388646 :                   tree new_targs;
    3230              : 
    3231       388646 :                   new_targs = make_tree_vec (parm_depth);
    3232       777292 :                   for (i = arg_depth - parm_depth; i < arg_depth; ++i)
    3233       777292 :                     TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
    3234       388646 :                       = TREE_VEC_ELT (targs, i);
    3235       388646 :                   targs = new_targs;
    3236              :                 }
    3237              : 
    3238      1940000 :               return instantiate_template (tmpl, targs, tf_error);
    3239              :             }
    3240              : 
    3241              :           /* If we thought that the DECL was a member function, but it
    3242              :              turns out to be specializing a static member function,
    3243              :              make DECL a static member function as well.  */
    3244      2401382 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    3245       630582 :               && DECL_STATIC_FUNCTION_P (tmpl)
    3246      2402576 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    3247         1185 :             revert_static_member_fn (decl);
    3248              : 
    3249              :           /* If this is a specialization of a member template of a
    3250              :              template class, we want to return the TEMPLATE_DECL, not
    3251              :              the specialization of it.  */
    3252      2401382 :           if (tsk == tsk_template && !was_template_id)
    3253              :             {
    3254         1100 :               tree result = DECL_TEMPLATE_RESULT (tmpl);
    3255         1100 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    3256         1100 :               DECL_INITIAL (result) = NULL_TREE;
    3257         1100 :               if (have_def)
    3258              :                 {
    3259         1088 :                   tree parm;
    3260         1088 :                   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    3261         2176 :                   DECL_SOURCE_LOCATION (result)
    3262         1088 :                     = DECL_SOURCE_LOCATION (decl);
    3263              :                   /* We want to use the argument list specified in the
    3264              :                      definition, not in the original declaration.  */
    3265         1088 :                   DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
    3266         2244 :                   for (parm = DECL_ARGUMENTS (result); parm;
    3267         1156 :                        parm = DECL_CHAIN (parm))
    3268         1156 :                     DECL_CONTEXT (parm) = result;
    3269              :                 }
    3270         1100 :               decl = register_specialization (tmpl, gen_tmpl, targs,
    3271              :                                               is_friend, 0);
    3272         1100 :               if (flag_contracts)
    3273           31 :                 remove_fn_contract_specifiers (result);
    3274         1100 :               return decl;
    3275              :             }
    3276              : 
    3277              :           /* Set up the DECL_TEMPLATE_INFO for DECL.  */
    3278      2400282 :           DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
    3279              : 
    3280      2400282 :           if (was_template_id)
    3281      1849068 :             TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
    3282              : 
    3283              :           /* Inherit default function arguments from the template
    3284              :              DECL is specializing.  */
    3285      2400282 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    3286       629482 :             copy_default_args_to_explicit_spec (decl);
    3287              : 
    3288              :           /* This specialization has the same protection as the
    3289              :              template it specializes.  */
    3290      2400282 :           TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
    3291      2400282 :           TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
    3292              : 
    3293              :           /* 7.1.1-1 [dcl.stc]
    3294              : 
    3295              :              A storage-class-specifier shall not be specified in an
    3296              :              explicit specialization...
    3297              : 
    3298              :              The parser rejects these, so unless action is taken here,
    3299              :              explicit function specializations will always appear with
    3300              :              global linkage.
    3301              : 
    3302              :              The action recommended by the C++ CWG in response to C++
    3303              :              defect report 605 is to make the storage class and linkage
    3304              :              of the explicit specialization match the templated function:
    3305              : 
    3306              :              http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
    3307              :            */
    3308      2400282 :           if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
    3309              :             {
    3310       103649 :               tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
    3311       103649 :               gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
    3312              : 
    3313              :               /* This specialization has the same linkage and visibility as
    3314              :                  the function template it specializes.  */
    3315       103649 :               TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
    3316       103649 :               if (! TREE_PUBLIC (decl))
    3317              :                 {
    3318           37 :                   DECL_INTERFACE_KNOWN (decl) = 1;
    3319           37 :                   DECL_NOT_REALLY_EXTERN (decl) = 1;
    3320              :                 }
    3321       103649 :               DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
    3322       103649 :               if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
    3323              :                 {
    3324        11974 :                   DECL_VISIBILITY_SPECIFIED (decl) = 1;
    3325        11974 :                   DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
    3326              :                 }
    3327              :             }
    3328              : 
    3329              :           /* If DECL is a friend declaration, declared using an
    3330              :              unqualified name, the namespace associated with DECL may
    3331              :              have been set incorrectly.  For example, in:
    3332              : 
    3333              :                template <typename T> void f(T);
    3334              :                namespace N {
    3335              :                  struct S { friend void f<int>(int); }
    3336              :                }
    3337              : 
    3338              :              we will have set the DECL_CONTEXT for the friend
    3339              :              declaration to N, rather than to the global namespace.  */
    3340      2400282 :           if (DECL_NAMESPACE_SCOPE_P (decl))
    3341      1951705 :             DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
    3342              : 
    3343      2400282 :           if (is_friend && !have_def)
    3344              :             /* This is not really a declaration of a specialization.
    3345              :                It's just the name of an instantiation.  But, it's not
    3346              :                a request for an instantiation, either.  */
    3347        77568 :             SET_DECL_IMPLICIT_INSTANTIATION (decl);
    3348      2322714 :           else if (TREE_CODE (decl) == FUNCTION_DECL)
    3349              :             /* A specialization is not necessarily COMDAT.  */
    3350       551914 :             DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
    3351       551914 :                                   && DECL_DECLARED_INLINE_P (decl));
    3352      1770800 :           else if (VAR_P (decl))
    3353      1770800 :             DECL_COMDAT (decl) = false;
    3354              : 
    3355              :           /* If this is a full specialization, register it so that we can find
    3356              :              it again.  Partial specializations will be registered in
    3357              :              process_partial_specialization.  */
    3358      2400282 :           if (!processing_template_decl)
    3359              :             {
    3360      1453344 :               warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
    3361              : 
    3362      1453344 :               decl = register_specialization (decl, gen_tmpl, targs,
    3363              :                                               is_friend, 0);
    3364              :             }
    3365              : 
    3366      2400282 :           if (flag_contracts
    3367       293963 :               && decl != error_mark_node
    3368      2694237 :               && DECL_TEMPLATE_SPECIALIZATION (decl))
    3369       284959 :             remove_fn_contract_specifiers (decl);
    3370              : 
    3371              :           /* A 'structor should already have clones.  */
    3372      3029743 :           gcc_assert (decl == error_mark_node
    3373              :                       || variable_template_p (tmpl)
    3374              :                       || !(DECL_CONSTRUCTOR_P (decl)
    3375              :                            || DECL_DESTRUCTOR_P (decl))
    3376              :                       || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    3377              :         }
    3378              :     }
    3379              : 
    3380              :   return decl;
    3381              : }
    3382              : 
    3383              : /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
    3384              :    parameters.  These are represented in the same format used for
    3385              :    DECL_TEMPLATE_PARMS.  */
    3386              : 
    3387              : int
    3388    167963063 : comp_template_parms (const_tree parms1, const_tree parms2)
    3389              : {
    3390    167963063 :   if (parms1 == parms2)
    3391              :     return 1;
    3392              : 
    3393    152701938 :   tree t1 = TREE_VALUE (parms1);
    3394    152701938 :   tree t2 = TREE_VALUE (parms2);
    3395    152701938 :   int i;
    3396              : 
    3397    152701938 :   gcc_assert (TREE_CODE (t1) == TREE_VEC);
    3398    152701938 :   gcc_assert (TREE_CODE (t2) == TREE_VEC);
    3399              : 
    3400    152701938 :   if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
    3401              :     return 0;
    3402              : 
    3403    155845076 :   for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
    3404              :     {
    3405    106103658 :       tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
    3406    106103658 :       tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
    3407              : 
    3408              :       /* If either of the template parameters are invalid, assume
    3409              :          they match for the sake of error recovery. */
    3410    106103658 :       if (error_operand_p (parm1) || error_operand_p (parm2))
    3411              :         return 1;
    3412              : 
    3413    106103634 :       if (TREE_CODE (parm1) != TREE_CODE (parm2))
    3414              :         return 0;
    3415              : 
    3416    193055909 :       if (TREE_CODE (parm1) == TYPE_DECL
    3417    105402404 :           && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1))
    3418    104745766 :               == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2))))
    3419     87653505 :         continue;
    3420     17748899 :       else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
    3421              :         return 0;
    3422              :     }
    3423              : 
    3424              :   return 1;
    3425              : }
    3426              : 
    3427              : /* Returns true if two template parameters are declared with
    3428              :    equivalent constraints.  */
    3429              : 
    3430              : static bool
    3431    811589396 : template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
    3432              : {
    3433    811589396 :   tree req1 = TREE_TYPE (parm1);
    3434    811589396 :   tree req2 = TREE_TYPE (parm2);
    3435    811589396 :   if (!req1 != !req2)
    3436              :     return false;
    3437    809872037 :   if (req1)
    3438       935781 :     return cp_tree_equal (req1, req2);
    3439              :   return true;
    3440              : }
    3441              : 
    3442              : /* Returns true when two template parameters are equivalent.  */
    3443              : 
    3444              : static bool
    3445    916473076 : template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
    3446              : {
    3447    916473076 :   tree decl1 = TREE_VALUE (parm1);
    3448    916473076 :   tree decl2 = TREE_VALUE (parm2);
    3449              : 
    3450              :   /* If either of the template parameters are invalid, assume
    3451              :      they match for the sake of error recovery. */
    3452    916473076 :   if (error_operand_p (decl1) || error_operand_p (decl2))
    3453              :     return true;
    3454              : 
    3455              :   /* ... they declare parameters of the same kind.  */
    3456    916473073 :   if (TREE_CODE (decl1) != TREE_CODE (decl2))
    3457              :     return false;
    3458              : 
    3459              :   /* ... one parameter was introduced by a parameter declaration, then
    3460              :      both are. This case arises as a result of eagerly rewriting declarations
    3461              :      during parsing.  */
    3462    880829942 :   if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1)
    3463    880829942 :       != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2))
    3464              :     return false;
    3465              : 
    3466              :   /* ... if either declares a pack, they both do.  */
    3467    880813688 :   if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
    3468              :     return false;
    3469              : 
    3470    807339602 :   if (TREE_CODE (decl1) == PARM_DECL)
    3471              :     {
    3472              :       /* ... if they declare non-type parameters, the types are equivalent.  */
    3473      2875336 :       if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
    3474              :         return false;
    3475              :     }
    3476    804464266 :   else if (TREE_CODE (decl2) == TEMPLATE_DECL)
    3477              :     {
    3478              :       /* ... if they declare template template parameters, their template
    3479              :          parameter lists are equivalent.  */
    3480        30607 :       if (!template_heads_equivalent_p (decl1, decl2))
    3481              :         return false;
    3482              :     }
    3483              : 
    3484              :   /* ... if they are declared with a qualified-concept name, they both
    3485              :      are, and those names are equivalent.  */
    3486    807227394 :   return template_parameter_constraints_equivalent_p (parm1, parm2);
    3487              : }
    3488              : 
    3489              : /* Returns true if two template parameters lists are equivalent.
    3490              :    Two template parameter lists are equivalent if they have the
    3491              :    same length and their corresponding parameters are equivalent.
    3492              : 
    3493              :    PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
    3494              :    data structure returned by DECL_TEMPLATE_PARMS.
    3495              : 
    3496              :    This is generally the same implementation as comp_template_parms
    3497              :    except that it also the concept names and arguments used to
    3498              :    introduce parameters.  */
    3499              : 
    3500              : static bool
    3501   1556769033 : template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
    3502              : {
    3503   1556769033 :   if (parms1 == parms2)
    3504              :     return true;
    3505              : 
    3506   1556769033 :   tree list1 = TREE_VALUE (parms1);
    3507   1556769033 :   tree list2 = TREE_VALUE (parms2);
    3508              : 
    3509   1556769033 :   if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
    3510              :     return 0;
    3511              : 
    3512   1387606016 :   for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
    3513              :     {
    3514    916473076 :       tree parm1 = TREE_VEC_ELT (list1, i);
    3515    916473076 :       tree parm2 = TREE_VEC_ELT (list2, i);
    3516    916473076 :       if (!template_parameters_equivalent_p (parm1, parm2))
    3517              :         return false;
    3518              :     }
    3519              : 
    3520              :   return true;
    3521              : }
    3522              : 
    3523              : /* Return true if the requires-clause of the template parameter lists are
    3524              :    equivalent and false otherwise.  */
    3525              : static bool
    3526    535218576 : template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
    3527              : {
    3528    535218576 :   tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
    3529    535218576 :   tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
    3530    535218576 :   if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
    3531              :     return false;
    3532    455117237 :   if (!cp_tree_equal (req1, req2))
    3533              :     return false;
    3534              :   return true;
    3535              : }
    3536              : 
    3537              : /* Returns true if two template heads are equivalent. 17.6.6.1p6:
    3538              :    Two template heads are equivalent if their template parameter
    3539              :    lists are equivalent and their requires clauses are equivalent.
    3540              : 
    3541              :    In pre-C++20, this is equivalent to calling comp_template_parms
    3542              :    for the template parameters of TMPL1 and TMPL2.  */
    3543              : 
    3544              : bool
    3545   1556769033 : template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
    3546              : {
    3547   1556769033 :   tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
    3548   1556769033 :   tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
    3549              : 
    3550              :   /* ... have the same number of template parameters, and their
    3551              :      corresponding parameters are equivalent.  */
    3552   1556769033 :   if (!template_parameter_lists_equivalent_p (parms1, parms2))
    3553              :     return false;
    3554              : 
    3555              :   /* ... if either has a requires-clause, they both do and their
    3556              :      corresponding constraint-expressions are equivalent.  */
    3557    471132940 :   return template_requirements_equivalent_p (parms1, parms2);
    3558              : }
    3559              : 
    3560              : /* Determine whether PARM is a parameter pack.  */
    3561              : 
    3562              : bool
    3563   5905301283 : template_parameter_pack_p (const_tree parm)
    3564              : {
    3565              :   /* Determine if we have a non-type template parameter pack.  */
    3566   5905301283 :   if (TREE_CODE (parm) == PARM_DECL)
    3567    488313897 :     return (DECL_TEMPLATE_PARM_P (parm)
    3568    488313897 :             && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
    3569   5416987386 :   if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
    3570      1235951 :     return TEMPLATE_PARM_PARAMETER_PACK (parm);
    3571              : 
    3572              :   /* If this is a list of template parameters, we could get a
    3573              :      TYPE_DECL or a TEMPLATE_DECL.  */
    3574   5415751435 :   if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
    3575   5191400922 :     parm = TREE_TYPE (parm);
    3576              : 
    3577              :   /* Otherwise it must be a type template parameter.  */
    3578   5415751435 :   return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    3579   5415751435 :            || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    3580   5415751435 :           && TEMPLATE_TYPE_PARAMETER_PACK (parm));
    3581              : }
    3582              : 
    3583              : /* Determine if T is a function parameter pack.  */
    3584              : 
    3585              : bool
    3586      1816218 : function_parameter_pack_p (const_tree t)
    3587              : {
    3588      1816218 :   if (t && TREE_CODE (t) == PARM_DECL)
    3589      1816218 :     return DECL_PACK_P (t);
    3590              :   return false;
    3591              : }
    3592              : 
    3593              : /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
    3594              :    PRIMARY_FUNC_TMPL_INST is a primary function template instantiation.  */
    3595              : 
    3596              : tree
    3597      1723806 : get_function_template_decl (const_tree primary_func_tmpl_inst)
    3598              : {
    3599      1723806 :   if (! primary_func_tmpl_inst
    3600      1723806 :       || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
    3601      3447612 :       || ! primary_template_specialization_p (primary_func_tmpl_inst))
    3602      1255049 :     return NULL;
    3603              : 
    3604       468757 :   return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
    3605              : }
    3606              : 
    3607              : /* Return true iff the function parameter PARAM_DECL was expanded
    3608              :    from the function parameter pack PACK.  */
    3609              : 
    3610              : bool
    3611       649734 : function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
    3612              : {
    3613       649734 :   if (DECL_ARTIFICIAL (param_decl)
    3614       649734 :       || !function_parameter_pack_p (pack))
    3615            0 :     return false;
    3616              : 
    3617              :   /* The parameter pack and its pack arguments have the same
    3618              :      DECL_PARM_INDEX.  */
    3619       649734 :   return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
    3620              : }
    3621              : 
    3622              : /* Determine whether ARGS describes a variadic template args list,
    3623              :    i.e., one that is terminated by a template argument pack.  */
    3624              : 
    3625              : static bool
    3626           20 : template_args_variadic_p (tree args)
    3627              : {
    3628           20 :   int nargs;
    3629           20 :   tree last_parm;
    3630              : 
    3631           20 :   if (args == NULL_TREE)
    3632              :     return false;
    3633              : 
    3634           20 :   args = INNERMOST_TEMPLATE_ARGS (args);
    3635           20 :   nargs = TREE_VEC_LENGTH (args);
    3636              : 
    3637           20 :   if (nargs == 0)
    3638              :     return false;
    3639              : 
    3640           20 :   last_parm = TREE_VEC_ELT (args, nargs - 1);
    3641              : 
    3642           20 :   return ARGUMENT_PACK_P (last_parm);
    3643              : }
    3644              : 
    3645              : /* Generate a new name for the parameter pack name NAME (an
    3646              :    IDENTIFIER_NODE) that incorporates its */
    3647              : 
    3648              : static tree
    3649      1443498 : make_ith_pack_parameter_name (tree name, int i)
    3650              : {
    3651              :   /* Munge the name to include the parameter index.  */
    3652              : #define NUMBUF_LEN 128
    3653      1443498 :   char numbuf[NUMBUF_LEN];
    3654      1443498 :   char* newname;
    3655      1443498 :   int newname_len;
    3656              : 
    3657      1443498 :   if (name == NULL_TREE)
    3658              :     return name;
    3659      1437679 :   snprintf (numbuf, NUMBUF_LEN, "%i", i);
    3660      1437679 :   newname_len = IDENTIFIER_LENGTH (name)
    3661      1437679 :                 + strlen (numbuf) + 2;
    3662      1437679 :   newname = (char*)alloca (newname_len);
    3663      1437679 :   snprintf (newname, newname_len,
    3664      1437679 :             "%s#%i", IDENTIFIER_POINTER (name), i);
    3665      1437679 :   return get_identifier (newname);
    3666              : }
    3667              : 
    3668              : /* Return true if T is a primary function, class or alias template
    3669              :    specialization, not including the template pattern.  */
    3670              : 
    3671              : bool
    3672    161386095 : primary_template_specialization_p (const_tree t)
    3673              : {
    3674    161386095 :   if (!t)
    3675              :     return false;
    3676              : 
    3677    161386095 :   if (VAR_OR_FUNCTION_DECL_P (t))
    3678     81235644 :     return (DECL_LANG_SPECIFIC (t)
    3679     81235534 :             && DECL_USE_TEMPLATE (t)
    3680     75852752 :             && DECL_TEMPLATE_INFO (t)
    3681    157088396 :             && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
    3682     80150451 :   else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    3683     80150386 :     return (CLASSTYPE_TEMPLATE_INFO (t)
    3684     80150229 :             && CLASSTYPE_USE_TEMPLATE (t)
    3685    160300615 :             && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
    3686           65 :   else if (alias_template_specialization_p (t, nt_transparent))
    3687              :     return true;
    3688              :   return false;
    3689              : }
    3690              : 
    3691              : /* Return true if PARM is a template template parameter.  */
    3692              : 
    3693              : bool
    3694     56390558 : template_template_parameter_p (const_tree parm)
    3695              : {
    3696     56390558 :   return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
    3697              : }
    3698              : 
    3699              : /* Return true iff PARM is a DECL representing a type template
    3700              :    parameter.  */
    3701              : 
    3702              : bool
    3703   2227727771 : template_type_parameter_p (const_tree parm)
    3704              : {
    3705   2227727771 :   return (parm
    3706   2227727771 :           && (TREE_CODE (parm) == TYPE_DECL
    3707   1087794977 :               || TREE_CODE (parm) == TEMPLATE_DECL)
    3708   3367844916 :           && DECL_TEMPLATE_PARM_P (parm));
    3709              : }
    3710              : 
    3711              : /* Return the template parameters of T if T is a
    3712              :    primary template instantiation, NULL otherwise.  */
    3713              : 
    3714              : tree
    3715    178430797 : get_primary_template_innermost_parameters (const_tree t)
    3716              : {
    3717    178430797 :   tree parms = NULL, template_info = NULL;
    3718              : 
    3719    178430797 :   if ((template_info = get_template_info (t))
    3720    178430797 :       && primary_template_specialization_p (t))
    3721     80173660 :     parms = INNERMOST_TEMPLATE_PARMS
    3722              :         (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
    3723              : 
    3724    178430797 :   return parms;
    3725              : }
    3726              : 
    3727              : /* Returns the template arguments of T if T is a template instantiation,
    3728              :    NULL otherwise.  */
    3729              : 
    3730              : tree
    3731     38706525 : get_template_innermost_arguments (const_tree t)
    3732              : {
    3733     38706525 :   tree args = NULL, template_info = NULL;
    3734              : 
    3735     38706525 :   if ((template_info = get_template_info (t))
    3736     77413050 :       && TI_ARGS (template_info))
    3737     38706525 :     args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
    3738              : 
    3739     38706525 :   return args;
    3740              : }
    3741              : 
    3742              : /* Return the argument pack elements of T if T is a template argument pack,
    3743              :    NULL otherwise.  */
    3744              : 
    3745              : tree
    3746     60231517 : get_template_argument_pack_elems (const_tree t)
    3747              : {
    3748     60231517 :   if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
    3749     60231517 :       && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
    3750              :     return NULL;
    3751              : 
    3752      5046292 :   return ARGUMENT_PACK_ARGS (t);
    3753              : }
    3754              : 
    3755              : /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
    3756              :    ARGUMENT_PACK_SELECT represents. */
    3757              : 
    3758              : static tree
    3759     11201182 : argument_pack_select_arg (tree t)
    3760              : {
    3761     11201182 :   tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
    3762     11201182 :   tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
    3763              : 
    3764              :   /* If the selected argument is an expansion E, that most likely means we were
    3765              :      called from gen_elem_of_pack_expansion_instantiation during the
    3766              :      substituting of an argument pack (of which the Ith element is a pack
    3767              :      expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
    3768              :      In this case, the Ith element resulting from this substituting is going to
    3769              :      be a pack expansion, which pattern is the pattern of E.  Let's return the
    3770              :      pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
    3771              :      resulting pack expansion from it.  */
    3772     11201182 :   if (PACK_EXPANSION_P (arg))
    3773              :     {
    3774              :       /* Make sure we aren't throwing away arg info.  */
    3775       255535 :       gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
    3776       255535 :       arg = PACK_EXPANSION_PATTERN (arg);
    3777              :     }
    3778              : 
    3779     11201182 :   return arg;
    3780              : }
    3781              : 
    3782              : /* Return a modification of ARGS that's suitable for preserving inside a hash
    3783              :    table.  In particular, this replaces each ARGUMENT_PACK_SELECT with its
    3784              :    underlying argument.  ARGS is copied (upon modification) iff COW_P.  */
    3785              : 
    3786              : static tree
    3787       258616 : preserve_args (tree args, bool cow_p = true)
    3788              : {
    3789       258616 :   if (!args)
    3790              :     return NULL_TREE;
    3791              : 
    3792       657966 :   for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    3793              :     {
    3794       399352 :       tree t = TREE_VEC_ELT (args, i);
    3795       399352 :       tree r;
    3796       399352 :       if (!t)
    3797              :         r = NULL_TREE;
    3798       399160 :       else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
    3799           74 :         r = argument_pack_select_arg (t);
    3800       399086 :       else if (TREE_CODE (t) == TREE_VEC)
    3801        57460 :         r = preserve_args (t, cow_p);
    3802              :       else
    3803              :         r = t;
    3804       399160 :       if (r != t)
    3805              :         {
    3806           77 :           if (cow_p)
    3807              :             {
    3808           71 :               args = copy_template_args (args);
    3809           71 :               cow_p = false;
    3810              :             }
    3811           77 :           TREE_VEC_ELT (args, i) = r;
    3812              :         }
    3813              :     }
    3814              : 
    3815              :   return args;
    3816              : }
    3817              : 
    3818              : /* True iff FN is a function representing a built-in variadic parameter
    3819              :    pack.  */
    3820              : 
    3821              : bool
    3822    426049869 : builtin_pack_fn_p (tree fn)
    3823              : {
    3824    426049869 :   if (!fn
    3825    425954361 :       || TREE_CODE (fn) != FUNCTION_DECL
    3826    574606264 :       || !DECL_IS_UNDECLARED_BUILTIN (fn))
    3827              :     return false;
    3828              : 
    3829      8587502 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3830              :     return true;
    3831              : 
    3832              :   return false;
    3833              : }
    3834              : 
    3835              : /* True iff CALL is a call to a function representing a built-in variadic
    3836              :    parameter pack.  */
    3837              : 
    3838              : static bool
    3839    355192590 : builtin_pack_call_p (tree call)
    3840              : {
    3841    355192590 :   if (TREE_CODE (call) != CALL_EXPR)
    3842              :     return false;
    3843    265208820 :   return builtin_pack_fn_p (CALL_EXPR_FN (call));
    3844              : }
    3845              : 
    3846              : /* Return a TREE_VEC for the expansion of __integer_pack(HI).  */
    3847              : 
    3848              : static tree
    3849       101317 : expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
    3850              :                      tree in_decl)
    3851              : {
    3852       101317 :   tree ohi = CALL_EXPR_ARG (call, 0);
    3853       101317 :   tree hi = tsubst_expr (ohi, args, complain, in_decl);
    3854              : 
    3855       101317 :   if (instantiation_dependent_expression_p (hi))
    3856              :     {
    3857        99156 :       if (hi != ohi)
    3858              :         {
    3859        99156 :           call = copy_node (call);
    3860        99156 :           CALL_EXPR_ARG (call, 0) = hi;
    3861              :         }
    3862        99156 :       tree ex = make_pack_expansion (call, complain);
    3863        99156 :       tree vec = make_tree_vec (1);
    3864        99156 :       TREE_VEC_ELT (vec, 0) = ex;
    3865        99156 :       return vec;
    3866              :     }
    3867              :   else
    3868              :     {
    3869         2161 :       hi = instantiate_non_dependent_expr (hi, complain);
    3870         2161 :       hi = cxx_constant_value (hi, complain);
    3871         2161 :       int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
    3872              : 
    3873              :       /* Calculate the largest value of len that won't make the size of the vec
    3874              :          overflow an int.  The compiler will exceed resource limits long before
    3875              :          this, but it seems a decent place to diagnose.  */
    3876         2161 :       int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
    3877              : 
    3878         2161 :       if (len < 0 || len > max)
    3879              :         {
    3880           12 :           if ((complain & tf_error)
    3881            9 :               && hi != error_mark_node)
    3882            9 :             error ("argument to %<__integer_pack%> must be between 0 and %d",
    3883              :                    max);
    3884           12 :           return error_mark_node;
    3885              :         }
    3886              : 
    3887         2149 :       tree vec = make_tree_vec (len);
    3888              : 
    3889        10151 :       for (int i = 0; i < len; ++i)
    3890         8002 :         TREE_VEC_ELT (vec, i) = size_int (i);
    3891              : 
    3892              :       return vec;
    3893              :     }
    3894              : }
    3895              : 
    3896              : /* Return a TREE_VEC for the expansion of built-in template parameter pack
    3897              :    CALL.  */
    3898              : 
    3899              : static tree
    3900       101317 : expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
    3901              :                           tree in_decl)
    3902              : {
    3903       101317 :   if (!builtin_pack_call_p (call))
    3904              :     return NULL_TREE;
    3905              : 
    3906       101317 :   tree fn = CALL_EXPR_FN (call);
    3907              : 
    3908       101317 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3909       101317 :     return expand_integer_pack (call, args, complain, in_decl);
    3910              : 
    3911              :   return NULL_TREE;
    3912              : }
    3913              : 
    3914              : /* Return true if the tree T has the extra args mechanism for
    3915              :    avoiding partial instantiation.  */
    3916              : 
    3917              : static bool
    3918   7640070239 : has_extra_args_mechanism_p (const_tree t)
    3919              : {
    3920   7640070239 :   return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS  */
    3921   7606344058 :           || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS  */
    3922   7603095860 :           || (TREE_CODE (t) == IF_STMT
    3923       970046 :               && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS  */
    3924  15242866345 :           || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS  */
    3925              : }
    3926              : 
    3927              : /* Return *_EXTRA_ARGS of the given supported tree T.  */
    3928              : 
    3929              : static tree&
    3930        20392 : tree_extra_args (tree t)
    3931              : {
    3932        20392 :   gcc_checking_assert (has_extra_args_mechanism_p (t));
    3933              : 
    3934        20392 :   if (PACK_EXPANSION_P (t))
    3935         5756 :     return PACK_EXPANSION_EXTRA_ARGS (t);
    3936        14636 :   else if (TREE_CODE (t) == REQUIRES_EXPR)
    3937         1815 :     return REQUIRES_EXPR_EXTRA_ARGS (t);
    3938        12821 :   else if (TREE_CODE (t) == IF_STMT
    3939        12821 :            && IF_STMT_CONSTEXPR_P (t))
    3940        12232 :     return IF_STMT_EXTRA_ARGS (t);
    3941          589 :   else if (TREE_CODE (t) == LAMBDA_EXPR)
    3942          589 :     return LAMBDA_EXPR_EXTRA_ARGS (t);
    3943              : 
    3944            0 :   gcc_unreachable ();
    3945              : }
    3946              : 
    3947              : /* Structure used to track the progress of find_parameter_packs_r.  */
    3948   2096980704 : struct find_parameter_pack_data
    3949              : {
    3950              :   /* TREE_LIST that will contain all of the parameter packs found by
    3951              :      the traversal.  */
    3952              :   tree* parameter_packs;
    3953              : 
    3954              :   /* Set of AST nodes that have been visited by the traversal.  */
    3955              :   hash_set<tree> *visited;
    3956              : 
    3957              :   /* True iff we found a subtree that has the extra args mechanism.  */
    3958              :   bool found_extra_args_tree_p = false;
    3959              : };
    3960              : 
    3961              : /* Identifies all of the argument packs that occur in a template
    3962              :    argument and appends them to the TREE_LIST inside DATA, which is a
    3963              :    find_parameter_pack_data structure. This is a subroutine of
    3964              :    make_pack_expansion and uses_parameter_packs.  */
    3965              : static tree
    3966   7839032175 : find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
    3967              : {
    3968   7839032175 :   tree t = *tp;
    3969   7839032175 :   struct find_parameter_pack_data* ppd =
    3970              :     (struct find_parameter_pack_data*)data;
    3971   7839032175 :   bool parameter_pack_p = false;
    3972              : 
    3973              : #define WALK_SUBTREE(NODE)                              \
    3974              :   cp_walk_tree (&(NODE), &find_parameter_packs_r,       \
    3975              :                 ppd, ppd->visited)                   \
    3976              : 
    3977              :   /* Don't look through typedefs; we are interested in whether a
    3978              :      parameter pack is actually written in the expression/type we're
    3979              :      looking at, not the target type.  */
    3980   7839032175 :   if (TYPE_P (t) && typedef_variant_p (t))
    3981              :     {
    3982              :       /* But do look at arguments for an alias template.  */
    3983    199656403 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    3984    151111285 :         cp_walk_tree (&TI_ARGS (tinfo),
    3985              :                       &find_parameter_packs_r,
    3986              :                       ppd, ppd->visited);
    3987    199656403 :       *walk_subtrees = 0;
    3988    199656403 :       return NULL_TREE;
    3989              :     }
    3990              : 
    3991              :   /* Identify whether this is a parameter pack or not.  */
    3992   7639375772 :   switch (TREE_CODE (t))
    3993              :     {
    3994     73482644 :     case TEMPLATE_PARM_INDEX:
    3995     73482644 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    3996              :         parameter_pack_p = true;
    3997              :       break;
    3998              : 
    3999   1231367526 :     case TEMPLATE_TYPE_PARM:
    4000   1231367526 :       t = TYPE_MAIN_VARIANT (t);
    4001              :       /* FALLTHRU */
    4002   1232236545 :     case TEMPLATE_TEMPLATE_PARM:
    4003   1232236545 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    4004              :         parameter_pack_p = true;
    4005              :       break;
    4006              : 
    4007    447998082 :     case FIELD_DECL:
    4008    447998082 :     case PARM_DECL:
    4009    447998082 :       if (DECL_PACK_P (t))
    4010              :         {
    4011              :           /* We don't want to walk into the type of a PARM_DECL,
    4012              :              because we don't want to see the type parameter pack.  */
    4013      3517133 :           *walk_subtrees = 0;
    4014      3517133 :           parameter_pack_p = true;
    4015              :         }
    4016              :       break;
    4017              : 
    4018    202496525 :     case VAR_DECL:
    4019    202496525 :       if (DECL_PACK_P (t))
    4020              :         {
    4021              :           /* We don't want to walk into the type of a variadic capture proxy,
    4022              :              because we don't want to see the type parameter pack.  */
    4023         4426 :           *walk_subtrees = 0;
    4024         4426 :           parameter_pack_p = true;
    4025              :         }
    4026    202492099 :       else if (variable_template_specialization_p (t))
    4027              :         {
    4028       200521 :           cp_walk_tree (&DECL_TI_ARGS (t),
    4029              :                         find_parameter_packs_r,
    4030              :                         ppd, ppd->visited);
    4031       200521 :           *walk_subtrees = 0;
    4032              :         }
    4033              :       break;
    4034              : 
    4035    265006183 :     case CALL_EXPR:
    4036    265006183 :       if (builtin_pack_call_p (t))
    4037              :         parameter_pack_p = true;
    4038              :       break;
    4039              : 
    4040              :     case BASES:
    4041              :       parameter_pack_p = true;
    4042              :       break;
    4043              :     default:
    4044              :       /* Not a parameter pack.  */
    4045              :       break;
    4046              :     }
    4047              : 
    4048      3722080 :   if (parameter_pack_p)
    4049              :     {
    4050              :       /* Add this parameter pack to the list.  */
    4051     66424257 :       *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
    4052              :     }
    4053              : 
    4054   7639375772 :   if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
    4055      5162096 :     ppd->found_extra_args_tree_p = true;
    4056              : 
    4057   7639375772 :   if (TYPE_P (t))
    4058   2379108913 :     cp_walk_tree (&TYPE_CONTEXT (t),
    4059              :                   &find_parameter_packs_r, ppd, ppd->visited);
    4060              : 
    4061              :   /* This switch statement will return immediately if we don't find a
    4062              :      parameter pack.  ??? Should some of these be in cp_walk_subtrees?  */
    4063   7639375772 :   switch (TREE_CODE (t))
    4064              :     {
    4065       421152 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    4066              :       /* Check the template itself.  */
    4067       421152 :       cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
    4068              :                     &find_parameter_packs_r, ppd, ppd->visited);
    4069       421152 :       return NULL_TREE;
    4070              : 
    4071     73482644 :     case TEMPLATE_PARM_INDEX:
    4072     73482644 :       if (parameter_pack_p)
    4073      3165507 :         WALK_SUBTREE (TREE_TYPE (t));
    4074              :       return NULL_TREE;
    4075              : 
    4076      7419688 :     case DECL_EXPR:
    4077      7419688 :       {
    4078      7419688 :         tree decl = DECL_EXPR_DECL (t);
    4079              :         /* Ignore the declaration of a capture proxy for a parameter pack.  */
    4080      7419688 :         if (is_capture_proxy (decl))
    4081      2326332 :           *walk_subtrees = 0;
    4082      7419688 :         if (is_typedef_decl (decl))
    4083              :           /* Since we stop at typedefs above, we need to look through them at
    4084              :              the point of the DECL_EXPR.  */
    4085        84768 :           cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
    4086              :                         &find_parameter_packs_r, ppd, ppd->visited);
    4087              :         return NULL_TREE;
    4088              :       }
    4089              : 
    4090    224754044 :     case TEMPLATE_DECL:
    4091    224754044 :       if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
    4092              :         return NULL_TREE;
    4093         4207 :       cp_walk_tree (&TREE_TYPE (t),
    4094              :                     &find_parameter_packs_r, ppd, ppd->visited);
    4095         4207 :       return NULL_TREE;
    4096              : 
    4097     33714669 :     case TYPE_PACK_EXPANSION:
    4098     33714669 :     case EXPR_PACK_EXPANSION:
    4099     33714669 :       *walk_subtrees = 0;
    4100     33714669 :       return NULL_TREE;
    4101              : 
    4102        13583 :     case PACK_INDEX_TYPE:
    4103        13583 :     case PACK_INDEX_EXPR:
    4104              :       /* We can have an expansion of an expansion, such as "Ts...[Is]...",
    4105              :          so do look into the index (but not the pack).  */
    4106        13583 :       cp_walk_tree (&PACK_INDEX_INDEX (t), &find_parameter_packs_r, ppd,
    4107              :                     ppd->visited);
    4108        13583 :       *walk_subtrees = 0;
    4109        13583 :       return NULL_TREE;
    4110              : 
    4111     36648258 :     case INTEGER_TYPE:
    4112     36648258 :       cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
    4113              :                     ppd, ppd->visited);
    4114     36648258 :       *walk_subtrees = 0;
    4115     36648258 :       return NULL_TREE;
    4116              : 
    4117    235700823 :     case IDENTIFIER_NODE:
    4118    235700823 :       cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
    4119              :                     ppd->visited);
    4120    235700823 :       *walk_subtrees = 0;
    4121    235700823 :       return NULL_TREE;
    4122              : 
    4123      1642238 :     case LAMBDA_EXPR:
    4124      1642238 :       {
    4125              :         /* Since we defer implicit capture, look in the parms and body.  */
    4126      1642238 :         tree fn = lambda_function (t);
    4127      1642238 :         cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
    4128              :                       ppd->visited);
    4129      1642238 :         cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
    4130              :                       ppd->visited);
    4131      1642238 :         return NULL_TREE;
    4132              :       }
    4133              : 
    4134      9555984 :     case DECLTYPE_TYPE:
    4135      9555984 :       cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
    4136              :                     ppd, ppd->visited);
    4137      9555984 :       *walk_subtrees = 0;
    4138      9555984 :       return NULL_TREE;
    4139              : 
    4140       940876 :     case IF_STMT:
    4141       940876 :       cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
    4142              :                     ppd, ppd->visited);
    4143       940876 :       cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
    4144              :                     ppd, ppd->visited);
    4145       940876 :       cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
    4146              :                     ppd, ppd->visited);
    4147              :       /* Don't walk into IF_STMT_EXTRA_ARGS.  */
    4148       940876 :       *walk_subtrees = 0;
    4149       940876 :       return NULL_TREE;
    4150              : 
    4151           91 :     case TAG_DEFN:
    4152           91 :       t = TREE_TYPE (t);
    4153           91 :       if (CLASS_TYPE_P (t))
    4154              :         {
    4155              :           /* Local class, need to look through the whole definition.
    4156              :              TYPE_BINFO might be unset for a partial instantiation.  */
    4157           48 :           if (TYPE_BINFO (t))
    4158           54 :             for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
    4159            9 :               cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
    4160              :                             ppd, ppd->visited);
    4161              :         }
    4162              :       else
    4163              :         /* Enum, look at the values.  */
    4164          153 :         for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
    4165          110 :           cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
    4166              :                         &find_parameter_packs_r,
    4167              :                         ppd, ppd->visited);
    4168              :       return NULL_TREE;
    4169              : 
    4170      6365000 :     case FUNCTION_TYPE:
    4171      6365000 :     case METHOD_TYPE:
    4172      6365000 :       WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
    4173      6365000 :       break;
    4174              : 
    4175              :     default:
    4176              :       return NULL_TREE;
    4177              :     }
    4178              : 
    4179              : #undef WALK_SUBTREE
    4180              : 
    4181      6365000 :   return NULL_TREE;
    4182              : }
    4183              : 
    4184              : /* Determines if the expression or type T uses any parameter packs.  */
    4185              : tree
    4186     13167721 : uses_parameter_packs (tree t)
    4187              : {
    4188     13167721 :   tree parameter_packs = NULL_TREE;
    4189     13167721 :   struct find_parameter_pack_data ppd;
    4190     13167721 :   ppd.parameter_packs = &parameter_packs;
    4191     13167721 :   ppd.visited = new hash_set<tree>;
    4192     13167721 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4193     26335442 :   delete ppd.visited;
    4194     13167721 :   return parameter_packs;
    4195              : }
    4196              : 
    4197              : /* Turn ARG, which may be an expression, type, or a TREE_LIST
    4198              :    representation a base-class initializer into a parameter pack
    4199              :    expansion. If all goes well, the resulting node will be an
    4200              :    EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
    4201              :    respectively.  */
    4202              : tree
    4203     61207931 : make_pack_expansion (tree arg, tsubst_flags_t complain)
    4204              : {
    4205     61207931 :   tree result;
    4206     61207931 :   tree parameter_packs = NULL_TREE;
    4207     61207931 :   bool for_types = false;
    4208     61207931 :   struct find_parameter_pack_data ppd;
    4209              : 
    4210     61207931 :   if (!arg || arg == error_mark_node)
    4211              :     return arg;
    4212              : 
    4213     61207916 :   if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
    4214              :     {
    4215              :       /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
    4216              :          class initializer.  In this case, the TREE_PURPOSE will be a
    4217              :          _TYPE node (representing the base class expansion we're
    4218              :          initializing) and the TREE_VALUE will be a TREE_LIST
    4219              :          containing the initialization arguments.
    4220              : 
    4221              :          The resulting expansion looks somewhat different from most
    4222              :          expansions. Rather than returning just one _EXPANSION, we
    4223              :          return a TREE_LIST whose TREE_PURPOSE is a
    4224              :          TYPE_PACK_EXPANSION containing the bases that will be
    4225              :          initialized.  The TREE_VALUE will be identical to the
    4226              :          original TREE_VALUE, which is a list of arguments that will
    4227              :          be passed to each base.  We do not introduce any new pack
    4228              :          expansion nodes into the TREE_VALUE (although it is possible
    4229              :          that some already exist), because the TREE_PURPOSE and
    4230              :          TREE_VALUE all need to be expanded together with the same
    4231              :          _EXPANSION node.  Note that the TYPE_PACK_EXPANSION in the
    4232              :          resulting TREE_PURPOSE will mention the parameter packs in
    4233              :          both the bases and the arguments to the bases.  */
    4234           34 :       tree purpose;
    4235           34 :       tree value;
    4236           34 :       tree parameter_packs = NULL_TREE;
    4237              : 
    4238              :       /* Determine which parameter packs will be used by the base
    4239              :          class expansion.  */
    4240           34 :       ppd.visited = new hash_set<tree>;
    4241           34 :       ppd.parameter_packs = &parameter_packs;
    4242           34 :       gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
    4243           34 :       cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
    4244              :                     &ppd, ppd.visited);
    4245              : 
    4246           34 :       if (parameter_packs == NULL_TREE)
    4247              :         {
    4248            0 :           if (complain & tf_error)
    4249            0 :             error ("base initializer expansion %qT contains no parameter packs",
    4250              :                    arg);
    4251            0 :           delete ppd.visited;
    4252            0 :           return error_mark_node;
    4253              :         }
    4254              : 
    4255           34 :       if (TREE_VALUE (arg) != void_type_node)
    4256              :         {
    4257              :           /* Collect the sets of parameter packs used in each of the
    4258              :              initialization arguments.  */
    4259           65 :           for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
    4260              :             {
    4261              :               /* Determine which parameter packs will be expanded in this
    4262              :                  argument.  */
    4263           37 :               cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
    4264              :                             &ppd, ppd.visited);
    4265              :             }
    4266              :         }
    4267              : 
    4268           68 :       delete ppd.visited;
    4269              : 
    4270              :       /* Create the pack expansion type for the base type.  */
    4271           34 :       purpose = cxx_make_type (TYPE_PACK_EXPANSION);
    4272           34 :       PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
    4273           68 :       PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
    4274           34 :       PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
    4275              : 
    4276              :       /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4277              :          they will rarely be compared to anything.  */
    4278           34 :       SET_TYPE_STRUCTURAL_EQUALITY (purpose);
    4279              : 
    4280           34 :       return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
    4281              :     }
    4282              : 
    4283     61207882 :   if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
    4284     53758539 :     for_types = true;
    4285              : 
    4286              :   /* Build the PACK_EXPANSION_* node.  */
    4287     61207882 :   result = for_types
    4288     53758539 :      ? cxx_make_type (TYPE_PACK_EXPANSION)
    4289      7449343 :      : make_node (EXPR_PACK_EXPANSION);
    4290     61207882 :   PACK_EXPANSION_PATTERN (result) = arg;
    4291     61207882 :   if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
    4292              :     {
    4293              :       /* Propagate type and const-expression information.  */
    4294      7449343 :       TREE_TYPE (result) = TREE_TYPE (arg);
    4295      7449343 :       TREE_CONSTANT (result) = TREE_CONSTANT (arg);
    4296              :       /* Mark this read now, since the expansion might be length 0.  */
    4297      7449343 :       mark_exp_read (arg);
    4298              :     }
    4299              :   else
    4300              :     /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4301              :        they will rarely be compared to anything.  */
    4302     53758539 :     SET_TYPE_STRUCTURAL_EQUALITY (result);
    4303              : 
    4304              :   /* Determine which parameter packs will be expanded.  */
    4305     61207882 :   ppd.parameter_packs = &parameter_packs;
    4306     61207882 :   ppd.visited = new hash_set<tree>;
    4307     61207882 :   cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
    4308    122415764 :   delete ppd.visited;
    4309              : 
    4310              :   /* Make sure we found some parameter packs.  */
    4311     61207882 :   if (parameter_packs == NULL_TREE)
    4312              :     {
    4313           81 :       if (complain & tf_error)
    4314              :         {
    4315           81 :           if (TYPE_P (arg))
    4316           38 :             error ("expansion pattern %qT contains no parameter packs", arg);
    4317              :           else
    4318           43 :             error ("expansion pattern %qE contains no parameter packs", arg);
    4319              :         }
    4320           81 :       return error_mark_node;
    4321              :     }
    4322    114966302 :   PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
    4323              : 
    4324     61207801 :   PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
    4325     61207801 :   if (ppd.found_extra_args_tree_p)
    4326              :     /* If the pattern of this pack expansion contains a subtree that has
    4327              :        the extra args mechanism for avoiding partial instantiation, then
    4328              :        force this pack expansion to also use extra args.  Otherwise
    4329              :        partial instantiation of this pack expansion may not lower the
    4330              :        level of some parameter packs within the pattern, which would
    4331              :        confuse tsubst_pack_expansion later (PR101764).  */
    4332        78326 :     PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
    4333              : 
    4334              :   return result;
    4335              : }
    4336              : 
    4337              : /* Create a PACK_INDEX_* using the pack expansion PACK and index INDEX.  */
    4338              : 
    4339              : tree
    4340        14257 : make_pack_index (tree pack, tree index)
    4341              : {
    4342        14257 :   if (pack == error_mark_node)
    4343              :     return error_mark_node;
    4344              : 
    4345        14244 :   bool for_types;
    4346        14244 :   if (TREE_CODE (pack) == TYPE_PACK_EXPANSION)
    4347              :     for_types = true;
    4348        10191 :   else if (TREE_CODE (pack) == EXPR_PACK_EXPANSION)
    4349              :     for_types = false;
    4350              :   else
    4351              :     {
    4352              :       /* Maybe we've already partially substituted the pack.  */
    4353         1590 :       gcc_checking_assert (TREE_CODE (pack) == TREE_VEC);
    4354         1590 :       for_types = TYPE_P (TREE_VEC_ELT (pack, 0));
    4355              :     }
    4356              : 
    4357         1590 :   tree t = (for_types
    4358         5643 :             ? cxx_make_type (PACK_INDEX_TYPE)
    4359         9933 :             : make_node (PACK_INDEX_EXPR));
    4360        14244 :   PACK_INDEX_PACK (t) = pack;
    4361        14244 :   PACK_INDEX_INDEX (t) = index;
    4362        14244 :   if (TREE_CODE (t) == PACK_INDEX_TYPE)
    4363         4311 :     SET_TYPE_STRUCTURAL_EQUALITY (t);
    4364              :   return t;
    4365              : }
    4366              : 
    4367              : /* Checks T for any "bare" parameter packs, which have not yet been
    4368              :    expanded, and issues an error if any are found. This operation can
    4369              :    only be done on full expressions or types (e.g., an expression
    4370              :    statement, "if" condition, etc.), because we could have expressions like:
    4371              : 
    4372              :      foo(f(g(h(args)))...)
    4373              : 
    4374              :    where "args" is a parameter pack. check_for_bare_parameter_packs
    4375              :    should not be called for the subexpressions args, h(args),
    4376              :    g(h(args)), or f(g(h(args))), because we would produce erroneous
    4377              :    error messages.
    4378              : 
    4379              :    Returns TRUE and emits an error if there were bare parameter packs,
    4380              :    returns FALSE otherwise.  */
    4381              : bool
    4382   2022598743 : check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
    4383              : {
    4384   2022598743 :   tree parameter_packs = NULL_TREE;
    4385   2022598743 :   struct find_parameter_pack_data ppd;
    4386              : 
    4387   2022598743 :   if (!processing_template_decl || !t || t == error_mark_node)
    4388              :     return false;
    4389              : 
    4390   1304327851 :   if (TREE_CODE (t) == TYPE_DECL)
    4391            0 :     t = TREE_TYPE (t);
    4392              : 
    4393   1304327851 :   ppd.parameter_packs = &parameter_packs;
    4394   1304327851 :   ppd.visited = new hash_set<tree>;
    4395   1304327851 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4396   2608655702 :   delete ppd.visited;
    4397              : 
    4398   1304327851 :   if (!parameter_packs)
    4399              :     return false;
    4400              : 
    4401          369 :   if (loc == UNKNOWN_LOCATION)
    4402          363 :     loc = cp_expr_loc_or_input_loc (t);
    4403              : 
    4404              :   /* It's OK for a lambda to have an unexpanded parameter pack from the
    4405              :      containing context, but do complain about unexpanded capture packs.  */
    4406          369 :   tree lam = current_lambda_expr ();
    4407          369 :   if (lam)
    4408          100 :     lam = TREE_TYPE (lam);
    4409              : 
    4410          100 :   if (lam && lam != current_class_type)
    4411              :     {
    4412              :       /* We're in a lambda, but it isn't the innermost class.
    4413              :          This should work, but currently doesn't.  */
    4414            9 :       sorry_at (loc, "unexpanded parameter pack in local class in lambda");
    4415            9 :       return true;
    4416              :     }
    4417              : 
    4418          360 :   if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
    4419          168 :     for (; parameter_packs;
    4420           80 :          parameter_packs = TREE_CHAIN (parameter_packs))
    4421              :       {
    4422           88 :         tree pack = TREE_VALUE (parameter_packs);
    4423           88 :         if (is_capture_proxy (pack)
    4424           88 :             || (TREE_CODE (pack) == PARM_DECL
    4425           24 :                 && DECL_CONTEXT (pack)
    4426           21 :                 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
    4427              :           break;
    4428              :       }
    4429              : 
    4430          360 :   if (parameter_packs)
    4431              :     {
    4432          280 :       auto_diagnostic_group d;
    4433          280 :       error_at (loc, "parameter packs not expanded with %<...%>:");
    4434          840 :       while (parameter_packs)
    4435              :         {
    4436          280 :           tree pack = TREE_VALUE (parameter_packs);
    4437          280 :           tree name = NULL_TREE;
    4438              : 
    4439          280 :           if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
    4440          280 :               || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
    4441          203 :             name = TYPE_NAME (pack);
    4442           77 :           else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
    4443           39 :             name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
    4444           38 :           else if (TREE_CODE (pack) == CALL_EXPR)
    4445            3 :             name = DECL_NAME (CALL_EXPR_FN (pack));
    4446              :           else
    4447           35 :             name = DECL_NAME (pack);
    4448              : 
    4449          280 :           if (name)
    4450          280 :             inform (loc, "        %qD", name);
    4451              :           else
    4452            0 :             inform (loc, "        %s", "<anonymous>");
    4453              : 
    4454          280 :           parameter_packs = TREE_CHAIN (parameter_packs);
    4455              :         }
    4456              : 
    4457          280 :       return true;
    4458          280 :     }
    4459              : 
    4460              :   return false;
    4461              : }
    4462              : 
    4463              : /* Expand any parameter packs that occur in the template arguments in
    4464              :    ARGS.  */
    4465              : tree
    4466    855397475 : expand_template_argument_pack (tree args)
    4467              : {
    4468    855397475 :   if (args == error_mark_node)
    4469              :     return error_mark_node;
    4470              : 
    4471    855397453 :   tree result_args = NULL_TREE;
    4472   1710794906 :   int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
    4473    855397453 :   int num_result_args = -1;
    4474    855397453 :   int non_default_args_count = -1;
    4475              : 
    4476              :   /* First, determine if we need to expand anything, and the number of
    4477              :      slots we'll need.  */
    4478   2130106703 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4479              :     {
    4480   1274709291 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4481   1274709291 :       if (arg == NULL_TREE)
    4482              :         return args;
    4483   1274709250 :       if (ARGUMENT_PACK_P (arg))
    4484              :         {
    4485     76171923 :           int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
    4486     76171923 :           if (num_result_args < 0)
    4487     76171923 :             num_result_args = in_arg + num_packed;
    4488              :           else
    4489            0 :             num_result_args += num_packed;
    4490              :         }
    4491              :       else
    4492              :         {
    4493   1198537327 :           if (num_result_args >= 0)
    4494           26 :             num_result_args++;
    4495              :         }
    4496              :     }
    4497              : 
    4498              :   /* If no expansion is necessary, we're done.  */
    4499    855397412 :   if (num_result_args < 0)
    4500              :     return args;
    4501              : 
    4502              :   /* Expand arguments.  */
    4503     76171923 :   result_args = make_tree_vec (num_result_args);
    4504     76171923 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
    4505     74099680 :     non_default_args_count =
    4506     74099680 :       GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
    4507    170294830 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4508              :     {
    4509     94122907 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4510     94122907 :       if (ARGUMENT_PACK_P (arg))
    4511              :         {
    4512     76171923 :           tree packed = ARGUMENT_PACK_ARGS (arg);
    4513     76171923 :           int i, num_packed = TREE_VEC_LENGTH (packed);
    4514    208229585 :           for (i = 0; i < num_packed; ++i, ++out_arg)
    4515    132057662 :             TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
    4516     76171923 :           if (non_default_args_count > 0)
    4517     74099676 :             non_default_args_count += num_packed - 1;
    4518              :         }
    4519              :       else
    4520              :         {
    4521     17950984 :           TREE_VEC_ELT (result_args, out_arg) = arg;
    4522     17950984 :           ++out_arg;
    4523              :         }
    4524              :     }
    4525     76171923 :   if (non_default_args_count >= 0)
    4526     74099680 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
    4527              :   return result_args;
    4528              : }
    4529              : 
    4530              : /* Checks if DECL shadows a template parameter.
    4531              : 
    4532              :    [temp.local]: A template-parameter shall not be redeclared within its
    4533              :    scope (including nested scopes).
    4534              : 
    4535              :    Emits an error and returns TRUE if the DECL shadows a parameter,
    4536              :    returns FALSE otherwise.  */
    4537              : 
    4538              : bool
    4539   1747492993 : check_template_shadow (tree decl)
    4540              : {
    4541   1747492993 :   tree olddecl;
    4542              : 
    4543              :   /* If we're not in a template, we can't possibly shadow a template
    4544              :      parameter.  */
    4545   1747492993 :   if (!current_template_parms)
    4546              :     return true;
    4547              : 
    4548              :   /* Figure out what we're shadowing.  */
    4549    925432939 :   decl = OVL_FIRST (decl);
    4550    925432939 :   olddecl = innermost_non_namespace_value (DECL_NAME (decl));
    4551              : 
    4552              :   /* If there's no previous binding for this name, we're not shadowing
    4553              :      anything, let alone a template parameter.  */
    4554    925432939 :   if (!olddecl)
    4555              :     return true;
    4556              : 
    4557              :   /* If we're not shadowing a template parameter, we're done.  Note
    4558              :      that OLDDECL might be an OVERLOAD (or perhaps even an
    4559              :      ERROR_MARK), so we can't just blithely assume it to be a _DECL
    4560              :      node.  */
    4561     68975645 :   if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
    4562              :     return true;
    4563              : 
    4564              :   /* We check for decl != olddecl to avoid bogus errors for using a
    4565              :      name inside a class.  We check TPFI to avoid duplicate errors for
    4566              :      inline member templates.  */
    4567          161 :   if (decl == olddecl
    4568          161 :       || (DECL_TEMPLATE_PARM_P (decl)
    4569           69 :           && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
    4570              :     return true;
    4571              : 
    4572              :   /* Don't complain about the injected class name, as we've already
    4573              :      complained about the class itself.  */
    4574          128 :   if (DECL_SELF_REFERENCE_P (decl))
    4575              :     return false;
    4576              : 
    4577          119 :   auto_diagnostic_group d;
    4578          119 :   if (DECL_TEMPLATE_PARM_P (decl))
    4579           36 :     error ("declaration of template parameter %q+D shadows "
    4580              :            "template parameter", decl);
    4581              :   else
    4582           83 :     error ("declaration of %q+#D shadows template parameter", decl);
    4583          119 :   inform (DECL_SOURCE_LOCATION (olddecl),
    4584              :           "template parameter %qD declared here", olddecl);
    4585          119 :   return false;
    4586          119 : }
    4587              : 
    4588              : /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
    4589              :    ORIG_LEVEL, DECL, and TYPE.  */
    4590              : 
    4591              : static tree
    4592    210642510 : build_template_parm_index (int index,
    4593              :                            int level,
    4594              :                            int orig_level,
    4595              :                            tree decl,
    4596              :                            tree type)
    4597              : {
    4598    210642510 :   tree t = make_node (TEMPLATE_PARM_INDEX);
    4599    210642510 :   TEMPLATE_PARM_IDX (t) = index;
    4600    210642510 :   TEMPLATE_PARM_LEVEL (t) = level;
    4601    210642510 :   TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
    4602    210642510 :   TEMPLATE_PARM_DECL (t) = decl;
    4603    210642510 :   TREE_TYPE (t) = type;
    4604    210642510 :   TREE_CONSTANT (t) = TREE_CONSTANT (decl);
    4605    210642510 :   TREE_READONLY (t) = TREE_READONLY (decl);
    4606              : 
    4607    210642510 :   return t;
    4608              : }
    4609              : 
    4610              : struct ctp_hasher : ggc_ptr_hash<tree_node>
    4611              : {
    4612   1292146570 :   static hashval_t hash (tree t)
    4613              :   {
    4614   1292146570 :     ++comparing_specializations;
    4615   1292146570 :     tree_code code = TREE_CODE (t);
    4616   1292146570 :     hashval_t val = iterative_hash_object (code, 0);
    4617   1292146570 :     val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
    4618   1292146570 :     val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
    4619   1292146570 :     if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
    4620              :       {
    4621   1077062706 :         val
    4622   1077062706 :           = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val);
    4623   1077062706 :         if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
    4624     80905775 :           val = iterative_hash_placeholder_constraint (c, val);
    4625              :       }
    4626   1292146570 :     if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
    4627     57469573 :       val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
    4628   1292146570 :     --comparing_specializations;
    4629   1292146570 :     return val;
    4630              :   }
    4631              : 
    4632   1576391129 :   static bool equal (tree t, tree u)
    4633              :   {
    4634   1576391129 :     ++comparing_specializations;
    4635   1576391129 :     bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
    4636   1576391129 :     --comparing_specializations;
    4637   1576391129 :     return eq;
    4638              :   }
    4639              : };
    4640              : 
    4641              : static GTY (()) hash_table<ctp_hasher> *ctp_table;
    4642              : 
    4643              : /* Find the canonical type parameter for the given template type
    4644              :    parameter.  Returns the canonical type parameter, which may be TYPE
    4645              :    if no such parameter existed.  */
    4646              : 
    4647              : tree
    4648    199845294 : canonical_type_parameter (tree type)
    4649              : {
    4650    199845294 :   if (ctp_table == NULL)
    4651        98396 :     ctp_table = hash_table<ctp_hasher>::create_ggc (61);
    4652              : 
    4653    199845294 :   tree& slot = *ctp_table->find_slot (type, INSERT);
    4654    199845294 :   if (slot == NULL_TREE)
    4655      2614405 :     slot = type;
    4656    199845294 :   return slot;
    4657              : }
    4658              : 
    4659              : /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
    4660              :    TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
    4661              :    TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
    4662              :    new one is created.  */
    4663              : 
    4664              : static tree
    4665     19817830 : reduce_template_parm_level (tree index, tree type, int levels, tree args,
    4666              :                             tsubst_flags_t complain)
    4667              : {
    4668     19817830 :   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
    4669      5028937 :       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
    4670      5028937 :           != TEMPLATE_PARM_LEVEL (index) - levels)
    4671     24812372 :       || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
    4672              :     {
    4673     14827594 :       tree orig_decl = TEMPLATE_PARM_DECL (index);
    4674              : 
    4675     14827594 :       tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
    4676     14827594 :                               TREE_CODE (orig_decl), DECL_NAME (orig_decl),
    4677              :                               type);
    4678     14827594 :       TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
    4679     14827594 :       TREE_READONLY (decl) = TREE_READONLY (orig_decl);
    4680     14827594 :       DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
    4681     14827594 :       DECL_ARTIFICIAL (decl) = 1;
    4682     14827594 :       SET_DECL_TEMPLATE_PARM_P (decl);
    4683              : 
    4684     14827594 :       tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
    4685     14827594 :                                             TEMPLATE_PARM_LEVEL (index) - levels,
    4686     14827594 :                                             TEMPLATE_PARM_ORIG_LEVEL (index),
    4687              :                                             decl, type);
    4688     14827594 :       TEMPLATE_PARM_DESCENDANTS (index) = tpi;
    4689     29655188 :       TEMPLATE_PARM_PARAMETER_PACK (tpi)
    4690     14827594 :         = TEMPLATE_PARM_PARAMETER_PACK (index);
    4691              : 
    4692              :       /* Template template parameters need this.  */
    4693     14827594 :       tree inner = decl;
    4694     14827594 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
    4695              :         {
    4696         1798 :           inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl),
    4697         1798 :                                        TYPE_DECL, DECL_NAME (decl), type);
    4698         1798 :           DECL_TEMPLATE_RESULT (decl) = inner;
    4699         1798 :           DECL_ARTIFICIAL (inner) = true;
    4700         1798 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl),
    4701              :                                               args, complain);
    4702         1798 :           DECL_TEMPLATE_PARMS (decl) = parms;
    4703         1798 :           tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl);
    4704         1798 :           DECL_TEMPLATE_INFO (inner)
    4705         3596 :             = build_template_info (DECL_TI_TEMPLATE (orig_inner),
    4706              :                                    template_parms_to_args (parms));
    4707              :         }
    4708              : 
    4709              :       /* Attach the TPI to the decl.  */
    4710     14827594 :       if (TREE_CODE (inner) == TYPE_DECL)
    4711     14075046 :         TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
    4712              :       else
    4713       752548 :         DECL_INITIAL (decl) = tpi;
    4714              :     }
    4715              : 
    4716     19817830 :   return TEMPLATE_PARM_DESCENDANTS (index);
    4717              : }
    4718              : 
    4719              : /* Process information from new template parameter PARM and append it
    4720              :    to the LIST being built.  This new parameter is a non-type
    4721              :    parameter iff IS_NON_TYPE is true. This new parameter is a
    4722              :    parameter pack iff IS_PARAMETER_PACK is true.  The location of PARM
    4723              :    is in PARM_LOC.  */
    4724              : 
    4725              : tree
    4726    165758806 : process_template_parm (tree list, location_t parm_loc, tree parm,
    4727              :                        bool is_non_type, bool is_parameter_pack)
    4728              : {
    4729    165758806 :   gcc_assert (TREE_CODE (parm) == TREE_LIST);
    4730    165758806 :   tree prev = NULL_TREE;
    4731    165758806 :   int idx = 0;
    4732              : 
    4733    165758806 :   if (list)
    4734              :     {
    4735     76452790 :       prev = tree_last (list);
    4736              : 
    4737     76452790 :       tree p = TREE_VALUE (prev);
    4738     76452790 :       if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
    4739     71103250 :         idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
    4740      5349540 :       else if (TREE_CODE (p) == PARM_DECL)
    4741      5349520 :         idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
    4742              : 
    4743     76452790 :       ++idx;
    4744              :     }
    4745              : 
    4746    165758806 :   tree decl = NULL_TREE;
    4747    165758806 :   tree defval = TREE_PURPOSE (parm);
    4748    165758806 :   tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
    4749              : 
    4750    165758806 :   if (is_non_type)
    4751              :     {
    4752     10758053 :       parm = TREE_VALUE (parm);
    4753              : 
    4754     10758053 :       SET_DECL_TEMPLATE_PARM_P (parm);
    4755              : 
    4756     10758053 :       if (TREE_TYPE (parm) != error_mark_node)
    4757              :         {
    4758              :           /* [temp.param]
    4759              : 
    4760              :              The top-level cv-qualifiers on the template-parameter are
    4761              :              ignored when determining its type.  */
    4762     10757973 :           TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
    4763     10757973 :           if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
    4764          100 :             TREE_TYPE (parm) = error_mark_node;
    4765     10757873 :           else if (uses_parameter_packs (TREE_TYPE (parm))
    4766           63 :                    && !is_parameter_pack
    4767              :                    /* If we're in a nested template parameter list, the template
    4768              :                       template parameter could be a parameter pack.  */
    4769     10757891 :                    && processing_template_parmlist == 1)
    4770              :             {
    4771              :               /* This template parameter is not a parameter pack, but it
    4772              :                  should be. Complain about "bare" parameter packs.  */
    4773           12 :               check_for_bare_parameter_packs (TREE_TYPE (parm));
    4774              : 
    4775              :               /* Recover by calling this a parameter pack.  */
    4776           12 :               is_parameter_pack = true;
    4777              :             }
    4778              :         }
    4779              : 
    4780              :       /* A template parameter is not modifiable.  */
    4781     10758053 :       TREE_CONSTANT (parm) = 1;
    4782     10758053 :       TREE_READONLY (parm) = 1;
    4783     10758053 :       decl = build_decl (parm_loc,
    4784     10758053 :                          CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
    4785     10758053 :       TREE_CONSTANT (decl) = 1;
    4786     10758053 :       TREE_READONLY (decl) = 1;
    4787     10758053 :       DECL_INITIAL (parm) = DECL_INITIAL (decl)
    4788     21516106 :         = build_template_parm_index (idx, current_template_depth,
    4789     10758053 :                                      current_template_depth,
    4790     10758053 :                                      decl, TREE_TYPE (parm));
    4791              : 
    4792     10758053 :       TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
    4793     10758053 :         = is_parameter_pack;
    4794              :     }
    4795              :   else
    4796              :     {
    4797    155000753 :       tree t;
    4798    155000753 :       parm = TREE_VALUE (TREE_VALUE (parm));
    4799              : 
    4800    155000753 :       if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
    4801              :         {
    4802       386457 :           t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
    4803              :           /* This is for distinguishing between real templates and template
    4804              :              template parameters */
    4805       386457 :           TREE_TYPE (parm) = t;
    4806              : 
    4807              :           /* any_template_parm_r expects to be able to get the targs of a
    4808              :              DECL_TEMPLATE_RESULT.  */
    4809       386457 :           tree result = DECL_TEMPLATE_RESULT (parm);
    4810       386457 :           TREE_TYPE (result) = t;
    4811       386457 :           tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
    4812       386457 :           tree tinfo = build_template_info (parm, args);
    4813       386457 :           retrofit_lang_decl (result);
    4814       386457 :           DECL_TEMPLATE_INFO (result) = tinfo;
    4815              : 
    4816       386457 :           decl = parm;
    4817       386457 :         }
    4818              :       else
    4819              :         {
    4820    154614296 :           t = cxx_make_type (TEMPLATE_TYPE_PARM);
    4821              :           /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
    4822    154614296 :           decl = build_decl (parm_loc,
    4823              :                              TYPE_DECL, parm, t);
    4824              :         }
    4825              : 
    4826    155000753 :       TYPE_NAME (t) = decl;
    4827    155000753 :       TYPE_STUB_DECL (t) = decl;
    4828    155000753 :       parm = decl;
    4829    155000753 :       TEMPLATE_TYPE_PARM_INDEX (t)
    4830    155000753 :         = build_template_parm_index (idx, current_template_depth,
    4831    155000753 :                                      current_template_depth,
    4832    155000753 :                                      decl, TREE_TYPE (parm));
    4833    155000753 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
    4834    155000753 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
    4835              :     }
    4836    165758806 :   DECL_ARTIFICIAL (decl) = 1;
    4837    165758806 :   SET_DECL_TEMPLATE_PARM_P (decl);
    4838              : 
    4839    165758806 :   if (TREE_CODE (parm) == TEMPLATE_DECL
    4840    165758806 :       && !uses_outer_template_parms (parm))
    4841       386424 :     TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true;
    4842              : 
    4843              :   /* Build requirements for the type/template parameter.
    4844              :      This must be done after SET_DECL_TEMPLATE_PARM_P or
    4845              :      process_template_parm could fail. */
    4846    165758806 :   tree reqs = finish_shorthand_constraint (parm, constr, is_non_type);
    4847              : 
    4848    165758806 :   decl = pushdecl (decl);
    4849    165758806 :   if (!is_non_type)
    4850    155000753 :     parm = decl;
    4851              : 
    4852              :   /* Build the parameter node linking the parameter declaration,
    4853              :      its default argument (if any), and its constraints (if any). */
    4854    165758806 :   parm = build_tree_list (defval, parm);
    4855    165758806 :   TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
    4856              : 
    4857    165758806 :   if (prev)
    4858     76452790 :     TREE_CHAIN (prev) = parm;
    4859              :   else
    4860              :     list = parm;
    4861              : 
    4862    165758806 :   return list;
    4863              : }
    4864              : 
    4865              : /* The end of a template parameter list has been reached.  Process the
    4866              :    tree list into a parameter vector, converting each parameter into a more
    4867              :    useful form.  Type parameters are saved as IDENTIFIER_NODEs, and others
    4868              :    as PARM_DECLs.  */
    4869              : 
    4870              : tree
    4871     88987991 : end_template_parm_list (tree parms)
    4872              : {
    4873     88987991 :   tree saved_parmlist = make_tree_vec (list_length (parms));
    4874              : 
    4875              :   /* Pop the dummy parameter level and add the real one.  We do not
    4876              :      morph the dummy parameter in place, as it might have been
    4877              :      captured by a (nested) template-template-parm.  */
    4878     88987991 :   current_template_parms = TREE_CHAIN (current_template_parms);
    4879              : 
    4880    177975982 :   current_template_parms
    4881     88987991 :     = tree_cons (size_int (current_template_depth + 1),
    4882              :                  saved_parmlist, current_template_parms);
    4883              : 
    4884    254286385 :   for (unsigned ix = 0; parms; ix++)
    4885              :     {
    4886    165298394 :       tree parm = parms;
    4887    165298394 :       parms = TREE_CHAIN (parms);
    4888    165298394 :       TREE_CHAIN (parm) = NULL_TREE;
    4889              : 
    4890    165298394 :       TREE_VEC_ELT (saved_parmlist, ix) = parm;
    4891              :     }
    4892              : 
    4893     88987991 :   --processing_template_parmlist;
    4894              : 
    4895     88987991 :   return saved_parmlist;
    4896              : }
    4897              : 
    4898              : // Explicitly indicate the end of the template parameter list. We assume
    4899              : // that the current template parameters have been constructed and/or
    4900              : // managed explicitly, as when creating new template template parameters
    4901              : // from a shorthand constraint.
    4902              : void
    4903            0 : end_template_parm_list ()
    4904              : {
    4905            0 :   --processing_template_parmlist;
    4906            0 : }
    4907              : 
    4908              : /* end_template_decl is called after a template declaration is seen.  */
    4909              : 
    4910              : void
    4911     89306173 : end_template_decl (void)
    4912              : {
    4913     89306173 :   reset_specialization ();
    4914              : 
    4915     89306173 :   if (! processing_template_decl)
    4916              :     return;
    4917              : 
    4918              :   /* This matches the pushlevel in begin_template_parm_list.  */
    4919     89306170 :   finish_scope ();
    4920              : 
    4921     89306170 :   --processing_template_decl;
    4922     89306170 :   current_template_parms = TREE_CHAIN (current_template_parms);
    4923              : }
    4924              : 
    4925              : /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
    4926              :    thereof, and converts it into an argument suitable to be passed to
    4927              :    the type substitution functions.  Note that if the TREE_LIST contains
    4928              :    an error_mark node, the returned argument is error_mark_node.  */
    4929              : 
    4930              : tree
    4931    821326403 : template_parm_to_arg (tree t)
    4932              : {
    4933    821326403 :   if (!t)
    4934              :     return NULL_TREE;
    4935              : 
    4936    821326327 :   if (TREE_CODE (t) == TREE_LIST)
    4937    801727045 :     t = TREE_VALUE (t);
    4938              : 
    4939    821326327 :   if (error_operand_p (t))
    4940          619 :     return error_mark_node;
    4941              : 
    4942    821325708 :   if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
    4943              :     {
    4944    815605181 :       if (TREE_CODE (t) == TYPE_DECL
    4945     61009762 :           || TREE_CODE (t) == TEMPLATE_DECL)
    4946    755658692 :         t = TREE_TYPE (t);
    4947              :       else
    4948     59946489 :         t = DECL_INITIAL (t);
    4949              :     }
    4950              : 
    4951    821325708 :   gcc_assert (TEMPLATE_PARM_P (t));
    4952              : 
    4953    821325708 :   if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
    4954    821325708 :       || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
    4955              :     {
    4956    761236268 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    4957              :         {
    4958              :           /* Turn this argument into a TYPE_ARGUMENT_PACK
    4959              :              with a single element, which expands T.  */
    4960     24542974 :           tree vec = make_tree_vec (1);
    4961     24542974 :           if (CHECKING_P)
    4962     24542974 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4963              : 
    4964     24542974 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4965              : 
    4966     24542974 :           t = cxx_make_type (TYPE_ARGUMENT_PACK);
    4967     24542974 :           ARGUMENT_PACK_ARGS (t) = vec;
    4968              :         }
    4969              :     }
    4970              :   else
    4971              :     {
    4972     60089440 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    4973              :         {
    4974              :           /* Turn this argument into a NONTYPE_ARGUMENT_PACK
    4975              :              with a single element, which expands T.  */
    4976      1485733 :           tree vec = make_tree_vec (1);
    4977      1485733 :           if (CHECKING_P)
    4978      1485733 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4979              : 
    4980      1485733 :           t = convert_from_reference (t);
    4981      1485733 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4982              : 
    4983      1485733 :           t  = make_node (NONTYPE_ARGUMENT_PACK);
    4984      1485733 :           ARGUMENT_PACK_ARGS (t) = vec;
    4985              :         }
    4986              :       else
    4987     58603707 :         t = convert_from_reference (t);
    4988              :     }
    4989              :   return t;
    4990              : }
    4991              : 
    4992              : /* If T looks like a generic template argument produced by template_parm_to_arg,
    4993              :    return the corresponding template parameter, otherwise return NULL_TREE.  */
    4994              : 
    4995              : static tree
    4996   1781236705 : template_arg_to_parm (tree t)
    4997              : {
    4998   1781236705 :   if (t == NULL_TREE)
    4999              :     return NULL_TREE;
    5000              : 
    5001   1781236705 :   if (ARGUMENT_PACK_P (t))
    5002              :     {
    5003    203298330 :       tree args = ARGUMENT_PACK_ARGS (t);
    5004    203298330 :       if (TREE_VEC_LENGTH (args) == 1
    5005    203298330 :           && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
    5006     88644249 :         t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
    5007              :     }
    5008              : 
    5009   1781236705 :   if (REFERENCE_REF_P (t))
    5010         1775 :     t = TREE_OPERAND (t, 0);
    5011              : 
    5012   1781236705 :   if (TEMPLATE_PARM_P (t))
    5013              :     return t;
    5014              :   else
    5015    456247742 :     return NULL_TREE;
    5016              : }
    5017              : 
    5018              : /* Given a single level of template parameters (a TREE_VEC), return it
    5019              :    as a set of template arguments.  */
    5020              : 
    5021              : tree
    5022    392366550 : template_parms_level_to_args (tree parms)
    5023              : {
    5024    392366550 :   parms = copy_node (parms);
    5025    392366550 :   TREE_TYPE (parms) = NULL_TREE;
    5026   1188291843 :   for (tree& parm : tree_vec_range (parms))
    5027    795925293 :     parm = template_parm_to_arg (parm);
    5028              : 
    5029    392366550 :   if (CHECKING_P)
    5030    392366550 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
    5031              : 
    5032    392366550 :   return parms;
    5033              : }
    5034              : 
    5035              : /* Given a set of template parameters, return them as a set of template
    5036              :    arguments.  The template parameters are represented as a TREE_VEC, in
    5037              :    the form documented in cp-tree.h for template arguments.  */
    5038              : 
    5039              : tree
    5040    318408657 : template_parms_to_args (tree parms)
    5041              : {
    5042    318408657 :   tree header;
    5043    318408657 :   tree args = NULL_TREE;
    5044    318408657 :   int length = TMPL_PARMS_DEPTH (parms);
    5045    318408657 :   int l = length;
    5046              : 
    5047              :   /* If there is only one level of template parameters, we do not
    5048              :      create a TREE_VEC of TREE_VECs.  Instead, we return a single
    5049              :      TREE_VEC containing the arguments.  */
    5050    318408657 :   if (length > 1)
    5051     53377431 :     args = make_tree_vec (length);
    5052              : 
    5053    690932526 :   for (header = parms; header; header = TREE_CHAIN (header))
    5054              :     {
    5055    372523869 :       tree a = template_parms_level_to_args (TREE_VALUE (header));
    5056              : 
    5057    372523869 :       if (length > 1)
    5058    107492643 :         TREE_VEC_ELT (args, --l) = a;
    5059              :       else
    5060              :         args = a;
    5061              :     }
    5062              : 
    5063    318408657 :   return args;
    5064              : }
    5065              : 
    5066              : /* Within the declaration of a template, return the currently active
    5067              :    template parameters as an argument TREE_VEC.  */
    5068              : 
    5069              : static tree
    5070    317474433 : current_template_args (void)
    5071              : {
    5072            0 :   return template_parms_to_args (current_template_parms);
    5073              : }
    5074              : 
    5075              : /* Return the fully generic arguments for of TMPL, i.e. what
    5076              :    current_template_args would be while parsing it.  */
    5077              : 
    5078              : tree
    5079     47233185 : generic_targs_for (tree tmpl)
    5080              : {
    5081     47233185 :   if (tmpl == NULL_TREE)
    5082              :     return NULL_TREE;
    5083     47233185 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
    5084     94466370 :       || DECL_TEMPLATE_SPECIALIZATION (tmpl))
    5085              :     /* DECL_TEMPLATE_RESULT doesn't have the arguments we want.  For a template
    5086              :        template parameter, it has no TEMPLATE_INFO; for a partial
    5087              :        specialization, it has the arguments for the primary template, and we
    5088              :        want the arguments for the partial specialization.  */;
    5089     47116288 :   else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
    5090     47116288 :     if (tree ti = get_template_info (result))
    5091     47116288 :       return TI_ARGS (ti);
    5092       116897 :   return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
    5093              : }
    5094              : 
    5095              : /* Return the template arguments corresponding to the template parameters of
    5096              :    DECL's enclosing scope.  When DECL is a member of a partial specialization,
    5097              :    this returns the arguments for the partial specialization as opposed to those
    5098              :    for the primary template, which is the main difference between this function
    5099              :    and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT.  */
    5100              : 
    5101              : tree
    5102       182008 : outer_template_args (const_tree decl)
    5103              : {
    5104       182008 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    5105         5067 :     decl = DECL_TEMPLATE_RESULT (decl);
    5106       182008 :   tree ti = get_template_info (decl);
    5107       182008 :   if (!ti)
    5108              :     return NULL_TREE;
    5109       182008 :   tree args = TI_ARGS (ti);
    5110       182008 :   if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)))
    5111              :     return args;
    5112       364016 :   if (TMPL_ARGS_DEPTH (args) == 1)
    5113              :     return NULL_TREE;
    5114       177152 :   return strip_innermost_template_args (args, 1);
    5115              : }
    5116              : 
    5117              : /* Update the declared TYPE by doing any lookups which were thought to be
    5118              :    dependent, but are not now that we know the SCOPE of the declarator.  */
    5119              : 
    5120              : tree
    5121    175285218 : maybe_update_decl_type (tree orig_type, tree scope)
    5122              : {
    5123    175285218 :   tree type = orig_type;
    5124              : 
    5125    175285218 :   if (type == NULL_TREE)
    5126              :     return type;
    5127              : 
    5128    167106784 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5129     79837736 :     type = TREE_TYPE (type);
    5130              : 
    5131      8039363 :   if (scope && TYPE_P (scope) && dependent_type_p (scope)
    5132      6716717 :       && dependent_type_p (type)
    5133              :       /* Don't bother building up the args in this case.  */
    5134    170452387 :       && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
    5135              :     {
    5136              :       /* tsubst in the args corresponding to the template parameters,
    5137              :          including auto if present.  Most things will be unchanged, but
    5138              :          make_typename_type and tsubst_qualified_id will resolve
    5139              :          TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
    5140      2399839 :       tree args = current_template_args ();
    5141      2399839 :       tree auto_node = type_uses_auto (type);
    5142      2399839 :       tree pushed;
    5143      2399839 :       if (auto_node)
    5144              :         {
    5145            0 :           tree auto_vec = make_tree_vec (1);
    5146            0 :           TREE_VEC_ELT (auto_vec, 0) = auto_node;
    5147            0 :           args = add_to_template_args (args, auto_vec);
    5148              :         }
    5149      2399839 :       pushed = push_scope (scope);
    5150      2399839 :       type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
    5151      2399839 :       if (pushed)
    5152      2399836 :         pop_scope (scope);
    5153              :     }
    5154              : 
    5155    167106784 :   if (type == error_mark_node)
    5156              :     return orig_type;
    5157              : 
    5158    167106043 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5159              :     {
    5160     79837703 :       if (same_type_p (type, TREE_TYPE (orig_type)))
    5161              :         type = orig_type;
    5162              :       else
    5163        66019 :         type = TYPE_NAME (type);
    5164              :     }
    5165              :   return type;
    5166              : }
    5167              : 
    5168              : /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
    5169              :    template PARMS and constraints, CONSTR.  If MEMBER_TEMPLATE_P is true,
    5170              :    the new  template is a member template. */
    5171              : 
    5172              : static tree
    5173    180024759 : build_template_decl (tree decl, tree parms, bool member_template_p)
    5174              : {
    5175    180024759 :   gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
    5176              : 
    5177    180024759 :   tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
    5178    180024759 :   SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
    5179    180024759 :   DECL_TEMPLATE_PARMS (tmpl) = parms;
    5180    180024759 :   DECL_TEMPLATE_RESULT (tmpl) = decl;
    5181    180024759 :   DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
    5182    180024759 :   TREE_TYPE (tmpl) = TREE_TYPE (decl);
    5183    180024759 :   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    5184    180024759 :   DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
    5185              : 
    5186              :   /* Propagate module information from the decl.  */
    5187    180024759 :   DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
    5188              : 
    5189    180024759 :   return tmpl;
    5190              : }
    5191              : 
    5192              : struct template_parm_data
    5193              : {
    5194              :   /* The level of the template parameters we are currently
    5195              :      processing.  */
    5196              :   int level;
    5197              : 
    5198              :   /* The index of the specialization argument we are currently
    5199              :      processing.  */
    5200              :   int current_arg;
    5201              : 
    5202              :   /* An array whose size is the number of template parameters.  The
    5203              :      elements are nonzero if the parameter has been used in any one
    5204              :      of the arguments processed so far.  */
    5205              :   int* parms;
    5206              : 
    5207              :   /* An array whose size is the number of template arguments.  The
    5208              :      elements are nonzero if the argument makes use of template
    5209              :      parameters of this level.  */
    5210              :   int* arg_uses_template_parms;
    5211              : };
    5212              : 
    5213              : /* Subroutine of push_template_decl used to see if each template
    5214              :    parameter in a partial specialization is used in the explicit
    5215              :    argument list.  If T is of the LEVEL given in DATA (which is
    5216              :    treated as a template_parm_data*), then DATA->PARMS is marked
    5217              :    appropriately.  */
    5218              : 
    5219              : static int
    5220     15145327 : mark_template_parm (tree t, void* data)
    5221              : {
    5222     15145327 :   int level;
    5223     15145327 :   int idx;
    5224     15145327 :   struct template_parm_data* tpd = (struct template_parm_data*) data;
    5225              : 
    5226     15145327 :   template_parm_level_and_index (t, &level, &idx);
    5227              : 
    5228     15145327 :   if (level == tpd->level)
    5229              :     {
    5230     15121622 :       tpd->parms[idx] = 1;
    5231     15121622 :       tpd->arg_uses_template_parms[tpd->current_arg] = 1;
    5232              :     }
    5233              : 
    5234              :   /* In C++17 the type of a non-type argument is a deduced context.  */
    5235     15145327 :   if (cxx_dialect >= cxx17
    5236     15043635 :       && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5237      1771310 :     for_each_template_parm (TREE_TYPE (t),
    5238              :                             &mark_template_parm,
    5239              :                             data,
    5240              :                             NULL,
    5241              :                             /*include_nondeduced_p=*/false);
    5242              : 
    5243              :   /* Return zero so that for_each_template_parm will continue the
    5244              :      traversal of the tree; we want to mark *every* template parm.  */
    5245     15145327 :   return 0;
    5246              : }
    5247              : 
    5248              : /* Process the partial specialization DECL.  */
    5249              : 
    5250              : static tree
    5251      7926324 : process_partial_specialization (tree decl)
    5252              : {
    5253      7926324 :   tree type = TREE_TYPE (decl);
    5254      7926324 :   tree tinfo = get_template_info (decl);
    5255      7926324 :   tree maintmpl = TI_TEMPLATE (tinfo);
    5256      7926324 :   tree specargs = TI_ARGS (tinfo);
    5257      7926324 :   tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
    5258      7926324 :   tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
    5259      7926324 :   tree inner_parms;
    5260      7926324 :   tree inst;
    5261      7926324 :   int nargs = TREE_VEC_LENGTH (inner_args);
    5262      7926324 :   int ntparms;
    5263      7926324 :   int  i;
    5264      7926324 :   struct template_parm_data tpd;
    5265      7926324 :   struct template_parm_data tpd2;
    5266              : 
    5267      7926324 :   gcc_assert (current_template_parms);
    5268              : 
    5269      7926324 :   inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
    5270      7926324 :   ntparms = TREE_VEC_LENGTH (inner_parms);
    5271              : 
    5272              :   /* We check that each of the template parameters given in the
    5273              :      partial specialization is used in the argument list to the
    5274              :      specialization.  For example:
    5275              : 
    5276              :        template <class T> struct S;
    5277              :        template <class T> struct S<T*>;
    5278              : 
    5279              :      The second declaration is OK because `T*' uses the template
    5280              :      parameter T, whereas
    5281              : 
    5282              :        template <class T> struct S<int>;
    5283              : 
    5284              :      is no good.  Even trickier is:
    5285              : 
    5286              :        template <class T>
    5287              :        struct S1
    5288              :        {
    5289              :           template <class U>
    5290              :           struct S2;
    5291              :           template <class U>
    5292              :           struct S2<T>;
    5293              :        };
    5294              : 
    5295              :      The S2<T> declaration is actually invalid; it is a
    5296              :      full-specialization.  Of course,
    5297              : 
    5298              :           template <class U>
    5299              :           struct S2<T (*)(U)>;
    5300              : 
    5301              :      or some such would have been OK.  */
    5302      7926324 :   tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
    5303      7926324 :   tpd.parms = XALLOCAVEC (int, ntparms);
    5304      7926324 :   memset (tpd.parms, 0, sizeof (int) * ntparms);
    5305              : 
    5306      7926324 :   tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5307      7926324 :   memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
    5308     23410279 :   for (i = 0; i < nargs; ++i)
    5309              :     {
    5310     15483955 :       tpd.current_arg = i;
    5311     15483955 :       for_each_template_parm (TREE_VEC_ELT (inner_args, i),
    5312              :                               &mark_template_parm,
    5313              :                               &tpd,
    5314              :                               NULL,
    5315              :                               /*include_nondeduced_p=*/false);
    5316              :     }
    5317              : 
    5318      7926324 :   {
    5319      7926324 :     auto_diagnostic_group d;
    5320      7926324 :     bool did_error_intro = false;
    5321     30082573 :     for (i = 0; i < ntparms; ++i)
    5322     14229925 :       if (tpd.parms[i] == 0)
    5323              :         {
    5324              :           /* One of the template parms was not used in a deduced context in the
    5325              :              specialization.  */
    5326           55 :           if (!did_error_intro)
    5327              :             {
    5328           55 :               error ("template parameters not deducible in "
    5329              :                      "partial specialization:");
    5330           55 :               did_error_intro = true;
    5331              :             }
    5332              : 
    5333          110 :           inform (input_location, "        %qD",
    5334           55 :                   TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
    5335              :         }
    5336              : 
    5337      7926324 :     if (did_error_intro)
    5338           55 :       return error_mark_node;
    5339      7926324 :   }
    5340              : 
    5341              :   /* [temp.class.spec]
    5342              : 
    5343              :      The argument list of the specialization shall not be identical to
    5344              :      the implicit argument list of the primary template.  */
    5345      7926269 :   tree main_args
    5346      7926269 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
    5347      7926269 :   if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
    5348      7926269 :       && (!flag_concepts
    5349       645695 :           || !strictly_subsumes (current_template_constraints (), maintmpl)))
    5350              :     {
    5351           24 :       auto_diagnostic_group d;
    5352           24 :       if (!flag_concepts)
    5353            4 :         error ("partial specialization %q+D does not specialize "
    5354              :                "any template arguments; to define the primary template, "
    5355              :                "remove the template argument list", decl);
    5356              :       else
    5357           20 :         error ("partial specialization %q+D does not specialize any "
    5358              :                "template arguments and is not more constrained than "
    5359              :                "the primary template; to define the primary template, "
    5360              :                "remove the template argument list", decl);
    5361           24 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5362           24 :     }
    5363              : 
    5364              :   /* A partial specialization that replaces multiple parameters of the
    5365              :      primary template with a pack expansion is less specialized for those
    5366              :      parameters.  */
    5367      7926269 :   if (nargs < DECL_NTPARMS (maintmpl))
    5368              :     {
    5369            3 :       auto_diagnostic_group d;
    5370            3 :       error ("partial specialization is not more specialized than the "
    5371              :              "primary template because it replaces multiple parameters "
    5372              :              "with a pack expansion");
    5373            3 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5374              :       /* Avoid crash in process_partial_specialization.  */
    5375            3 :       return decl;
    5376            3 :     }
    5377              : 
    5378      7926266 :   else if (nargs > DECL_NTPARMS (maintmpl))
    5379              :     {
    5380            9 :       auto_diagnostic_group d;
    5381            9 :       error ("too many arguments for partial specialization %qT", type);
    5382            9 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5383              :       /* Avoid crash below.  */
    5384            9 :       return decl;
    5385            9 :     }
    5386              : 
    5387              :   /* If we aren't in a dependent class, we can actually try deduction.  */
    5388      7926257 :   else if (tpd.level == 1
    5389              :            /* FIXME we should be able to handle a partial specialization of a
    5390              :               partial instantiation, but currently we can't (c++/41727).  */
    5391     15023468 :            && TMPL_ARGS_DEPTH (specargs) == 1
    5392     15437985 :            && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
    5393              :     {
    5394           17 :       auto_diagnostic_group d;
    5395           17 :       if (pedwarn (input_location, 0,
    5396              :                    "partial specialization %qD is not more specialized than",
    5397              :                    decl))
    5398           17 :         inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
    5399              :                 maintmpl);
    5400           17 :     }
    5401              : 
    5402              :   /* [temp.spec.partial]
    5403              : 
    5404              :      The type of a template parameter corresponding to a specialized
    5405              :      non-type argument shall not be dependent on a parameter of the
    5406              :      specialization.
    5407              : 
    5408              :      Also, we verify that pack expansions only occur at the
    5409              :      end of the argument list.  */
    5410      7926257 :   tpd2.parms = 0;
    5411     23410111 :   for (i = 0; i < nargs; ++i)
    5412              :     {
    5413     15483854 :       tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
    5414     15483854 :       tree arg = TREE_VEC_ELT (inner_args, i);
    5415     15483854 :       tree packed_args = NULL_TREE;
    5416     15483854 :       int j, len = 1;
    5417              : 
    5418     15483854 :       if (ARGUMENT_PACK_P (arg))
    5419              :         {
    5420              :           /* Extract the arguments from the argument pack. We'll be
    5421              :              iterating over these in the following loop.  */
    5422       754001 :           packed_args = ARGUMENT_PACK_ARGS (arg);
    5423       754001 :           len = TREE_VEC_LENGTH (packed_args);
    5424              :         }
    5425              : 
    5426     31342282 :       for (j = 0; j < len; j++)
    5427              :         {
    5428     15858428 :           if (packed_args)
    5429              :             /* Get the Jth argument in the parameter pack.  */
    5430      1128575 :             arg = TREE_VEC_ELT (packed_args, j);
    5431              : 
    5432     15858428 :           if (PACK_EXPANSION_P (arg))
    5433              :             {
    5434              :               /* Pack expansions must come at the end of the
    5435              :                  argument list.  */
    5436       442349 :               if ((packed_args && j < len - 1)
    5437            6 :                   || (!packed_args && i < nargs - 1))
    5438              :                 {
    5439           12 :                   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5440            6 :                     error ("parameter pack argument %qE must be at the "
    5441              :                            "end of the template argument list", arg);
    5442              :                   else
    5443            6 :                     error ("parameter pack argument %qT must be at the "
    5444              :                            "end of the template argument list", arg);
    5445              :                 }
    5446              :             }
    5447              : 
    5448     15858428 :           if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5449              :             /* We only care about the pattern.  */
    5450        65725 :             arg = PACK_EXPANSION_PATTERN (arg);
    5451              : 
    5452     15858428 :           if (/* These first two lines are the `non-type' bit.  */
    5453     15858428 :               !TYPE_P (arg)
    5454      4201636 :               && TREE_CODE (arg) != TEMPLATE_DECL
    5455              :               /* This next two lines are the `argument expression is not just a
    5456              :                  simple identifier' condition and also the `specialized
    5457              :                  non-type argument' bit.  */
    5458      3949359 :               && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
    5459     18695413 :               && !((REFERENCE_REF_P (arg)
    5460      2836961 :                     || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
    5461           88 :                    && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
    5462              :             {
    5463              :               /* Look at the corresponding template parameter,
    5464              :                  marking which template parameters its type depends
    5465              :                  upon.  */
    5466      2836900 :               tree type = TREE_TYPE (parm);
    5467              : 
    5468      2836900 :               if (!tpd2.parms)
    5469              :                 {
    5470              :                   /* We haven't yet initialized TPD2.  Do so now.  */
    5471      1751127 :                   tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5472              :                   /* The number of parameters here is the number in the
    5473              :                      main template, which, as checked in the assertion
    5474              :                      above, is NARGS.  */
    5475      1751127 :                   tpd2.parms = XALLOCAVEC (int, nargs);
    5476      1751127 :                   tpd2.level =
    5477      1751127 :                     TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
    5478              :                 }
    5479              : 
    5480              :               /* Mark the template parameters.  But this time, we're
    5481              :                  looking for the template parameters of the main
    5482              :                  template, not in the specialization.  */
    5483      2836900 :               tpd2.current_arg = i;
    5484      2836900 :               tpd2.arg_uses_template_parms[i] = 0;
    5485      2836900 :               memset (tpd2.parms, 0, sizeof (int) * nargs);
    5486      2836900 :               for_each_template_parm (type,
    5487              :                                       &mark_template_parm,
    5488              :                                       &tpd2,
    5489              :                                       NULL,
    5490              :                                       /*include_nondeduced_p=*/false);
    5491              : 
    5492      2836900 :               if (tpd2.arg_uses_template_parms [i])
    5493              :                 {
    5494              :                   /* The type depended on some template parameters.
    5495              :                      If they are fully specialized in the
    5496              :                      specialization, that's OK.  */
    5497              :                   int j;
    5498              :                   int count = 0;
    5499           78 :                   for (j = 0; j < nargs; ++j)
    5500           54 :                     if (tpd2.parms[j] != 0
    5501           24 :                         && tpd.arg_uses_template_parms [j])
    5502           18 :                       ++count;
    5503           24 :                   if (count != 0)
    5504           18 :                     error_n (input_location, count,
    5505              :                              "type %qT of template argument %qE depends "
    5506              :                              "on a template parameter",
    5507              :                              "type %qT of template argument %qE depends "
    5508              :                              "on template parameters",
    5509              :                              type,
    5510              :                              arg);
    5511              :                 }
    5512              :             }
    5513              :         }
    5514              :     }
    5515              : 
    5516              :   /* We should only get here once.  */
    5517      7926257 :   if (TREE_CODE (decl) == TYPE_DECL)
    5518      6979322 :     gcc_assert (!COMPLETE_TYPE_P (type));
    5519              : 
    5520              :   // Build the template decl.
    5521      7926257 :   tree tmpl = build_template_decl (decl, current_template_parms,
    5522      7926257 :                                    DECL_MEMBER_TEMPLATE_P (maintmpl));
    5523      7926257 :   SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    5524      7926257 :   DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
    5525      7926257 :   DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
    5526              : 
    5527              :   /* Give template template parms a DECL_CONTEXT of the template
    5528              :      for which they are a parameter.  */
    5529     22156089 :   for (i = 0; i < ntparms; ++i)
    5530              :     {
    5531     14229832 :       tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
    5532     14229832 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    5533       171396 :         DECL_CONTEXT (parm) = tmpl;
    5534              :     }
    5535              : 
    5536      7926257 :   if (VAR_P (decl))
    5537              :     {
    5538              :       /* We didn't register this in check_explicit_specialization so we could
    5539              :          wait until the constraints were set.  */
    5540       946935 :       tree reg = register_specialization (decl, maintmpl, specargs, false, 0);
    5541       946935 :       if (reg != decl)
    5542              :         /* Redeclaration.  */
    5543              :         return reg;
    5544              :     }
    5545              :   else
    5546      6979322 :     associate_classtype_constraints (type);
    5547              : 
    5548     15852496 :   DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
    5549      7926248 :     = tree_cons (specargs, tmpl,
    5550      7926248 :                  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
    5551      7926248 :   TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
    5552              :   /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL.  */
    5553      7926248 :   gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
    5554      7926248 :   TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE);
    5555              : 
    5556      7926248 :   set_defining_module_for_partial_spec (decl);
    5557              : 
    5558     60248326 :   for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
    5559     52322078 :        inst = TREE_CHAIN (inst))
    5560              :     {
    5561     52322078 :       tree instance = TREE_VALUE (inst);
    5562     55335362 :       if (TYPE_P (instance)
    5563     52322078 :           ? (COMPLETE_TYPE_P (instance)
    5564     49308794 :              && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
    5565      3013284 :           : DECL_TEMPLATE_INSTANTIATION (instance))
    5566              :         {
    5567       128563 :           tree partial_ti = most_specialized_partial_spec (instance, tf_none,
    5568              :                                                            /*rechecking=*/true);
    5569       128563 :           tree inst_decl = (DECL_P (instance)
    5570       128563 :                             ? instance : TYPE_NAME (instance));
    5571       128563 :           if (!partial_ti)
    5572              :             /* OK */;
    5573        64505 :           else if (partial_ti == error_mark_node)
    5574            3 :             permerror (input_location,
    5575              :                        "declaration of %qD ambiguates earlier template "
    5576              :                        "instantiation for %qD", decl, inst_decl);
    5577        64502 :           else if (TI_TEMPLATE (partial_ti) == tmpl)
    5578            6 :             permerror (input_location,
    5579              :                        "partial specialization of %qD after instantiation "
    5580              :                        "of %qD", decl, inst_decl);
    5581              :         }
    5582              :     }
    5583              : 
    5584              :   return decl;
    5585              : }
    5586              : 
    5587              : /* PARM is a template parameter of some form; return the corresponding
    5588              :    TEMPLATE_PARM_INDEX.  */
    5589              : 
    5590              : static tree
    5591    363674136 : get_template_parm_index (tree parm)
    5592              : {
    5593    363674136 :   if (TREE_CODE (parm) == PARM_DECL
    5594    363674136 :       || TREE_CODE (parm) == CONST_DECL)
    5595    167518344 :     parm = DECL_INITIAL (parm);
    5596    196155792 :   else if (TREE_CODE (parm) == TYPE_DECL
    5597     10784573 :            || TREE_CODE (parm) == TEMPLATE_DECL)
    5598    185375626 :     parm = TREE_TYPE (parm);
    5599    363674136 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    5600    168776336 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
    5601    168776273 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    5602    194953917 :     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
    5603    363674136 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
    5604    363674136 :   return parm;
    5605              : }
    5606              : 
    5607              : /* Subroutine of fixed_parameter_pack_p below.  Look for any template
    5608              :    parameter packs used by the template parameter PARM.  */
    5609              : 
    5610              : static void
    5611         6342 : fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
    5612              : {
    5613              :   /* A type parm can't refer to another parm.  */
    5614         6342 :   if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
    5615              :     return;
    5616         6327 :   else if (TREE_CODE (parm) == PARM_DECL)
    5617              :     {
    5618         6285 :       cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
    5619              :                     ppd, ppd->visited);
    5620         6285 :       return;
    5621              :     }
    5622              : 
    5623           42 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
    5624              : 
    5625           42 :   tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
    5626           93 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    5627              :     {
    5628           51 :       tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
    5629           51 :       if (template_parameter_pack_p (p))
    5630              :         /* Any packs in the type are expanded by this parameter.  */;
    5631              :       else
    5632           33 :         fixed_parameter_pack_p_1 (p, ppd);
    5633              :     }
    5634              : }
    5635              : 
    5636              : /* PARM is a template parameter pack.  Return any parameter packs used in
    5637              :    its type or the type of any of its template parameters.  If there are
    5638              :    any such packs, it will be instantiated into a fixed template parameter
    5639              :    list by partial instantiation rather than be fully deduced.  */
    5640              : 
    5641              : tree
    5642    190698039 : fixed_parameter_pack_p (tree parm)
    5643              : {
    5644              :   /* This can only be true in a member template.  */
    5645    190698039 :   if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
    5646              :     return NULL_TREE;
    5647              :   /* This can only be true for a parameter pack.  */
    5648      3106892 :   if (!template_parameter_pack_p (parm))
    5649              :     return NULL_TREE;
    5650              :   /* A type parm can't refer to another parm.  */
    5651      3106892 :   if (TREE_CODE (parm) == TYPE_DECL)
    5652              :     return NULL_TREE;
    5653              : 
    5654         6309 :   tree parameter_packs = NULL_TREE;
    5655         6309 :   struct find_parameter_pack_data ppd;
    5656         6309 :   ppd.parameter_packs = &parameter_packs;
    5657         6309 :   ppd.visited = new hash_set<tree>;
    5658              : 
    5659         6309 :   fixed_parameter_pack_p_1 (parm, &ppd);
    5660              : 
    5661        12618 :   delete ppd.visited;
    5662         6309 :   return parameter_packs;
    5663              : }
    5664              : 
    5665              : /* Check that a template declaration's use of default arguments and
    5666              :    parameter packs is not invalid.  Here, PARMS are the template
    5667              :    parameters.  IS_PRIMARY is true if DECL is the thing declared by
    5668              :    a primary template.  IS_PARTIAL is true if DECL is a partial
    5669              :    specialization.
    5670              : 
    5671              :    IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
    5672              :    function template declaration or a friend class template
    5673              :    declaration.  In the function case, 1 indicates a declaration, 2
    5674              :    indicates a redeclaration.  When IS_FRIEND_DECL=2, no errors are
    5675              :    emitted for extraneous default arguments.
    5676              : 
    5677              :    Returns TRUE if there were no errors found, FALSE otherwise. */
    5678              : 
    5679              : bool
    5680    248132609 : check_default_tmpl_args (tree decl, tree parms, bool is_primary,
    5681              :                          bool is_partial, int is_friend_decl)
    5682              : {
    5683    248132609 :   const char *msg;
    5684    248132609 :   int last_level_to_check;
    5685    248132609 :   tree parm_level;
    5686    248132609 :   bool no_errors = true;
    5687              : 
    5688              :   /* [temp.param]
    5689              : 
    5690              :      A default template-argument shall not be specified in a
    5691              :      function template declaration or a function template definition, nor
    5692              :      in the template-parameter-list of the definition of a member of a
    5693              :      class template.  */
    5694              : 
    5695    248132609 :   if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
    5696    248132609 :       || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
    5697              :     /* You can't have a function template declaration in a local
    5698              :        scope, nor you can you define a member of a class template in a
    5699              :        local scope.  */
    5700              :     return true;
    5701              : 
    5702     62427111 :   if ((DECL_IMPLICIT_TYPEDEF_P (decl)
    5703     38373175 :        && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5704    309201198 :       || (TREE_CODE (decl) == FUNCTION_DECL
    5705    188548992 :           && LAMBDA_FUNCTION_P (decl)))
    5706              :     /* A lambda doesn't have an explicit declaration; don't complain
    5707              :        about the parms of the enclosing class.  */
    5708              :     return true;
    5709              : 
    5710    244133278 :   if (current_class_type
    5711    191690089 :       && !TYPE_BEING_DEFINED (current_class_type)
    5712     68946660 :       && DECL_LANG_SPECIFIC (decl)
    5713     68864531 :       && DECL_DECLARES_FUNCTION_P (decl)
    5714              :       /* If this is either a friend defined in the scope of the class
    5715              :          or a member function.  */
    5716    142101695 :       && (DECL_FUNCTION_MEMBER_P (decl)
    5717     68555648 :           ? same_type_p (DECL_CONTEXT (decl), current_class_type)
    5718            0 :           : DECL_FRIEND_CONTEXT (decl)
    5719            0 :           ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
    5720              :           : false)
    5721              :       /* And, if it was a member function, it really was defined in
    5722              :          the scope of the class.  */
    5723    381244574 :       && (!DECL_FUNCTION_MEMBER_P (decl)
    5724     68555648 :           || DECL_INITIALIZED_IN_CLASS_P (decl)))
    5725              :     /* We already checked these parameters when the template was
    5726              :        declared, so there's no need to do it again now.  This function
    5727              :        was defined in class scope, but we're processing its body now
    5728              :        that the class is complete.  */
    5729              :     return true;
    5730              : 
    5731              :   /* Core issue 226 (C++0x only): the following only applies to class
    5732              :      templates.  */
    5733    182870226 :   if (is_primary
    5734     76103183 :       && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
    5735              :     {
    5736              :       /* [temp.param]
    5737              : 
    5738              :          If a template-parameter has a default template-argument, all
    5739              :          subsequent template-parameters shall have a default
    5740              :          template-argument supplied.  */
    5741     61084894 :       for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
    5742              :         {
    5743     31984883 :           tree inner_parms = TREE_VALUE (parm_level);
    5744     31984883 :           int ntparms = TREE_VEC_LENGTH (inner_parms);
    5745     31984883 :           int seen_def_arg_p = 0;
    5746     31984883 :           int i;
    5747              : 
    5748     85493595 :           for (i = 0; i < ntparms; ++i)
    5749              :             {
    5750     53508712 :               tree parm = TREE_VEC_ELT (inner_parms, i);
    5751              : 
    5752     53508712 :               if (parm == error_mark_node)
    5753            0 :                 continue;
    5754              : 
    5755     53508712 :               if (TREE_PURPOSE (parm))
    5756              :                 seen_def_arg_p = 1;
    5757     49020569 :               else if (seen_def_arg_p
    5758     49020569 :                        && !template_parameter_pack_p (TREE_VALUE (parm)))
    5759              :                 {
    5760           21 :                   error ("no default argument for %qD", TREE_VALUE (parm));
    5761              :                   /* For better subsequent error-recovery, we indicate that
    5762              :                      there should have been a default argument.  */
    5763           21 :                   TREE_PURPOSE (parm) = error_mark_node;
    5764           21 :                   no_errors = false;
    5765              :                 }
    5766     49020548 :               else if (!is_partial
    5767     49020548 :                        && !is_friend_decl
    5768              :                        /* Don't complain about an enclosing partial
    5769              :                           specialization.  */
    5770     32259839 :                        && parm_level == parms
    5771     29264705 :                        && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
    5772     25066374 :                        && i < ntparms - 1
    5773      9913882 :                        && template_parameter_pack_p (TREE_VALUE (parm))
    5774              :                        /* A fixed parameter pack will be partially
    5775              :                           instantiated into a fixed length list.  */
    5776     49020587 :                        && !fixed_parameter_pack_p (TREE_VALUE (parm)))
    5777              :                 {
    5778              :                   /* A primary class template, primary variable template
    5779              :                      (DR 2032), or alias template can only have one
    5780              :                      parameter pack, at the end of the template
    5781              :                      parameter list.  */
    5782              : 
    5783           24 :                   error ("parameter pack %q+D must be at the end of the"
    5784           24 :                          " template parameter list", TREE_VALUE (parm));
    5785              : 
    5786           24 :                   TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
    5787           24 :                     = error_mark_node;
    5788           24 :                   no_errors = false;
    5789              :                 }
    5790              :             }
    5791              :         }
    5792              :     }
    5793              : 
    5794    182870226 :   if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
    5795              :       || is_partial
    5796    182422364 :       || !is_primary
    5797     68029175 :       || is_friend_decl)
    5798              :     /* For an ordinary class template, default template arguments are
    5799              :        allowed at the innermost level, e.g.:
    5800              :          template <class T = int>
    5801              :          struct S {};
    5802              :        but, in a partial specialization, they're not allowed even
    5803              :        there, as we have in [temp.class.spec]:
    5804              : 
    5805              :          The template parameter list of a specialization shall not
    5806              :          contain default template argument values.
    5807              : 
    5808              :        So, for a partial specialization, or for a function template
    5809              :        (in C++98/C++03), we look at all of them.  */
    5810              :     ;
    5811              :   else
    5812              :     /* But, for a primary class template that is not a partial
    5813              :        specialization we look at all template parameters except the
    5814              :        innermost ones.  */
    5815     66198717 :     parms = TREE_CHAIN (parms);
    5816              : 
    5817              :   /* Figure out what error message to issue.  */
    5818    182870226 :   if (is_friend_decl == 2)
    5819              :     msg = G_("default template arguments may not be used in function template "
    5820              :              "friend re-declaration");
    5821    182667390 :   else if (is_friend_decl)
    5822              :     msg = G_("default template arguments may not be used in template "
    5823              :              "friend declarations");
    5824    181039768 :   else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
    5825              :     msg = G_("default template arguments may not be used in function templates "
    5826              :              "without %<-std=c++11%> or %<-std=gnu++11%>");
    5827    180604766 :   else if (is_partial)
    5828              :     msg = G_("default template arguments may not be used in "
    5829              :              "partial specializations");
    5830    172678406 :   else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
    5831              :     msg = G_("default argument for template parameter for class enclosing %qD");
    5832              :   else
    5833              :     /* Per [temp.param]/9, "A default template-argument shall not be
    5834              :        specified in the template-parameter-lists of the definition of
    5835              :        a member of a class template that appears outside of the member's
    5836              :        class.", thus if we aren't handling a member of a class template
    5837              :        there is no need to examine the parameters.  */
    5838              :     return true;
    5839              : 
    5840    102107447 :   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
    5841              :     /* If we're inside a class definition, there's no need to
    5842              :        examine the parameters to the class itself.  On the one
    5843              :        hand, they will be checked when the class is defined, and,
    5844              :        on the other, default arguments are valid in things like:
    5845              :          template <class T = double>
    5846              :          struct S { template <class U> void f(U); };
    5847              :        Here the default argument for `S' has no bearing on the
    5848              :        declaration of `f'.  */
    5849     87217507 :     last_level_to_check = template_class_depth (current_class_type) + 1;
    5850              :   else
    5851              :     /* Check everything.  */
    5852              :     last_level_to_check = 0;
    5853              : 
    5854    102107447 :   for (parm_level = parms;
    5855    227161970 :        parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
    5856     19105876 :        parm_level = TREE_CHAIN (parm_level))
    5857              :     {
    5858     19105879 :       tree inner_parms = TREE_VALUE (parm_level);
    5859     19105879 :       int i;
    5860     19105879 :       int ntparms;
    5861              : 
    5862     19105879 :       ntparms = TREE_VEC_LENGTH (inner_parms);
    5863     58682811 :       for (i = 0; i < ntparms; ++i)
    5864              :         {
    5865     39576935 :           if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
    5866            0 :             continue;
    5867              : 
    5868     39576935 :           if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
    5869              :             {
    5870           30 :               if (msg)
    5871              :                 {
    5872           30 :                   no_errors = false;
    5873           30 :                   if (is_friend_decl == 2)
    5874              :                     return no_errors;
    5875              : 
    5876           27 :                   error (msg, decl);
    5877           27 :                   msg = 0;
    5878              :                 }
    5879              : 
    5880              :               /* Clear out the default argument so that we are not
    5881              :                  confused later.  */
    5882           27 :               TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
    5883              :             }
    5884              :         }
    5885              : 
    5886              :       /* At this point, if we're still interested in issuing messages,
    5887              :          they must apply to classes surrounding the object declared.  */
    5888     19105876 :       if (msg)
    5889     19105848 :         msg = G_("default argument for template parameter for class "
    5890              :                  "enclosing %qD");
    5891              :     }
    5892              : 
    5893              :   return no_errors;
    5894              : }
    5895              : 
    5896              : /* Worker for push_template_decl_real, called via
    5897              :    for_each_template_parm.  DATA is really an int, indicating the
    5898              :    level of the parameters we are interested in.  If T is a template
    5899              :    parameter of that level, return nonzero.  */
    5900              : 
    5901              : static int
    5902      2088031 : template_parm_this_level_p (tree t, void* data)
    5903              : {
    5904      2088031 :   int this_level = *(int *)data;
    5905      2088031 :   int level;
    5906              : 
    5907      2088031 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5908       417130 :     level = TEMPLATE_PARM_LEVEL (t);
    5909              :   else
    5910      1670901 :     level = TEMPLATE_TYPE_LEVEL (t);
    5911      2088031 :   return level == this_level;
    5912              : }
    5913              : 
    5914              : /* Worker for uses_outer_template_parms, called via for_each_template_parm.
    5915              :    DATA is really an int, indicating the innermost outer level of parameters.
    5916              :    If T is a template parameter of that level or further out, return
    5917              :    nonzero.  */
    5918              : 
    5919              : static int
    5920     11490025 : template_parm_outer_level (tree t, void *data)
    5921              : {
    5922     11490025 :   int this_level = *(int *)data;
    5923     11490025 :   int level;
    5924              : 
    5925     11490025 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5926       279460 :     level = TEMPLATE_PARM_LEVEL (t);
    5927              :   else
    5928     11210565 :     level = TEMPLATE_TYPE_LEVEL (t);
    5929     11490025 :   return level <= this_level;
    5930              : }
    5931              : 
    5932              : /* Creates a TEMPLATE_DECL for the indicated DECL using the template
    5933              :    parameters given by current_template_args, or reuses a
    5934              :    previously existing one, if appropriate.  Returns the DECL, or an
    5935              :    equivalent one, if it is replaced via a call to duplicate_decls.
    5936              : 
    5937              :    If IS_FRIEND is true, DECL is a friend declaration.  */
    5938              : 
    5939              : tree
    5940    322068500 : push_template_decl (tree decl, bool is_friend)
    5941              : {
    5942    322068500 :   if (decl == error_mark_node || !current_template_parms)
    5943              :     return error_mark_node;
    5944              : 
    5945              :   /* See if this is a partial specialization.  */
    5946     75203529 :   bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
    5947     20634019 :                       && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
    5948     20293107 :                       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
    5949    390292579 :                      || (VAR_P (decl)
    5950     61521750 :                          && DECL_LANG_SPECIFIC (decl)
    5951      7012045 :                          && DECL_TEMPLATE_SPECIALIZATION (decl)
    5952       946950 :                          && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
    5953              : 
    5954              :   /* No surprising friend functions.  */
    5955    322068472 :   gcc_checking_assert (is_friend
    5956              :                        || !(TREE_CODE (decl) == FUNCTION_DECL
    5957              :                             && DECL_UNIQUE_FRIEND_P (decl)));
    5958              : 
    5959    322068472 :   tree ctx;
    5960    322068472 :   if (is_friend)
    5961              :     /* For a friend, we want the context of the friend, not
    5962              :        the type of which it is a friend.  */
    5963      8935659 :     ctx = CP_DECL_CONTEXT (decl);
    5964    313132813 :   else if (CP_DECL_CONTEXT (decl)
    5965    313132813 :            && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
    5966              :     /* In the case of a virtual function, we want the class in which
    5967              :        it is defined.  */
    5968    261216166 :     ctx = CP_DECL_CONTEXT (decl);
    5969              :   else
    5970              :     /* Otherwise, if we're currently defining some class, the DECL
    5971              :        is assumed to be a member of the class.  */
    5972     51916647 :     ctx = current_scope ();
    5973              : 
    5974    322068472 :   if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
    5975     60785064 :     ctx = NULL_TREE;
    5976              : 
    5977    322068472 :   if (!DECL_CONTEXT (decl))
    5978          133 :     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
    5979              : 
    5980              :   /* See if this is a primary template.  */
    5981    322068472 :   bool is_primary = false;
    5982    322068472 :   if (is_friend && ctx
    5983    322068472 :       && uses_template_parms_level (ctx, current_template_depth))
    5984              :     /* A friend template that specifies a class context, i.e.
    5985              :          template <typename T> friend void A<T>::f();
    5986              :        is not primary.  */
    5987              :     ;
    5988    342194655 :   else if (DECL_IMPLICIT_TYPEDEF_P (decl) && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5989              :     /* Lambdas are not primary.  */
    5990              :     ;
    5991              :   else
    5992    320959127 :     is_primary = template_parm_scope_p ();
    5993              : 
    5994              :   /* True if the template is a member template, in the sense of
    5995              :      [temp.mem].  */
    5996    320959127 :   bool member_template_p = false;
    5997              : 
    5998    320959127 :   if (is_primary)
    5999              :     {
    6000     78283239 :       warning (OPT_Wtemplates, "template %qD declared", decl);
    6001              : 
    6002     78283239 :       if (DECL_CLASS_SCOPE_P (decl))
    6003              :         member_template_p = true;
    6004              : 
    6005     78283239 :       if (TREE_CODE (decl) == TYPE_DECL
    6006     78283239 :           && IDENTIFIER_ANON_P (DECL_NAME (decl)))
    6007              :         {
    6008            6 :           error ("template class without a name");
    6009            6 :           return error_mark_node;
    6010              :         }
    6011     78283233 :       else if (TREE_CODE (decl) == FUNCTION_DECL)
    6012              :         {
    6013     49717358 :           if (member_template_p)
    6014              :             {
    6015     20079947 :               if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
    6016            6 :                 error ("member template %qD may not have virt-specifiers", decl);
    6017              :             }
    6018     99434716 :           if (DECL_DESTRUCTOR_P (decl))
    6019              :             {
    6020              :               /* [temp.mem]
    6021              : 
    6022              :                  A destructor shall not be a member template.  */
    6023            6 :               error_at (DECL_SOURCE_LOCATION (decl),
    6024              :                         "destructor %qD declared as member template", decl);
    6025            6 :               return error_mark_node;
    6026              :             }
    6027     99434704 :           if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
    6028     49720044 :               && (!prototype_p (TREE_TYPE (decl))
    6029         2692 :                   || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
    6030         2692 :                   || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    6031         2692 :                   || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    6032              :                       == void_list_node)))
    6033              :             {
    6034              :               /* [basic.stc.dynamic.allocation]
    6035              : 
    6036              :                  An allocation function can be a function
    6037              :                  template. ... Template allocation functions shall
    6038              :                  have two or more parameters.  */
    6039            9 :               error ("invalid template declaration of %qD", decl);
    6040            9 :               return error_mark_node;
    6041              :             }
    6042              :         }
    6043     22439236 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6044     46527565 :                && CLASS_TYPE_P (TREE_TYPE (decl)))
    6045              :         /* Class template.  */;
    6046     10604191 :       else if (TREE_CODE (decl) == TYPE_DECL
    6047     10604191 :                && TYPE_DECL_ALIAS_P (decl))
    6048              :         /* alias-declaration */
    6049      4477543 :         gcc_assert (!DECL_ARTIFICIAL (decl));
    6050      6126648 :       else if (VAR_P (decl))
    6051              :         /* C++14 variable template. */;
    6052      2607937 :       else if (TREE_CODE (decl) == CONCEPT_DECL)
    6053              :         /* C++20 concept definitions.  */;
    6054              :       else
    6055              :         {
    6056            9 :           error ("template declaration of %q#D", decl);
    6057            9 :           return error_mark_node;
    6058              :         }
    6059              :     }
    6060              : 
    6061     75203514 :   bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
    6062    376637949 :                   && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
    6063    234217343 :                       || (VAR_OR_FUNCTION_DECL_P (decl)
    6064    189004592 :                           && DECL_LOCAL_DECL_P (decl))));
    6065              : 
    6066              :   /* Check to see that the rules regarding the use of default
    6067              :      arguments are not being violated.  We check args for a friend
    6068              :      functions when we know whether it's a definition, introducing
    6069              :      declaration or re-declaration.  */
    6070    254851350 :   if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
    6071    246396461 :     check_default_tmpl_args (decl, current_template_parms,
    6072              :                              is_primary, is_partial, is_friend);
    6073              : 
    6074              :   /* Ensure that there are no parameter packs in the type of this
    6075              :      declaration that have not been expanded.  */
    6076    322068442 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    6077              :     {
    6078              :       /* Check each of the arguments individually to see if there are
    6079              :          any bare parameter packs.  */
    6080    182735250 :       tree type = TREE_TYPE (decl);
    6081    182735250 :       tree arg = DECL_ARGUMENTS (decl);
    6082    182735250 :       tree argtype = TYPE_ARG_TYPES (type);
    6083              : 
    6084    580448037 :       while (arg && argtype)
    6085              :         {
    6086    397712787 :           if (!DECL_PACK_P (arg)
    6087    790756949 :               && check_for_bare_parameter_packs (TREE_TYPE (arg)))
    6088              :             {
    6089              :             /* This is a PARM_DECL that contains unexpanded parameter
    6090              :                packs. We have already complained about this in the
    6091              :                check_for_bare_parameter_packs call, so just replace
    6092              :                these types with ERROR_MARK_NODE.  */
    6093           42 :               TREE_TYPE (arg) = error_mark_node;
    6094           42 :               TREE_VALUE (argtype) = error_mark_node;
    6095              :             }
    6096              : 
    6097    397712787 :           arg = DECL_CHAIN (arg);
    6098    397712787 :           argtype = TREE_CHAIN (argtype);
    6099              :         }
    6100              : 
    6101              :       /* Check for bare parameter packs in the return type and the
    6102              :          exception specifiers.  */
    6103    182735250 :       if (check_for_bare_parameter_packs (TREE_TYPE (type)))
    6104              :         /* Errors were already issued, set return type to int
    6105              :            as the frontend doesn't expect error_mark_node as
    6106              :            the return type.  */
    6107            3 :         TREE_TYPE (type) = integer_type_node;
    6108    182735250 :       if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
    6109            0 :         TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
    6110              :     }
    6111              :   else
    6112              :     {
    6113    278666384 :       if (check_for_bare_parameter_packs (is_typedef_decl (decl)
    6114              :                                           ? DECL_ORIGINAL_TYPE (decl)
    6115     84763764 :                                           : TREE_TYPE (decl)))
    6116              :         {
    6117           64 :           TREE_TYPE (decl) = error_mark_node;
    6118           64 :           return error_mark_node;
    6119              :         }
    6120              : 
    6121      7926327 :       if (is_partial && VAR_P (decl)
    6122    140280066 :           && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
    6123            3 :         return error_mark_node;
    6124              :     }
    6125              : 
    6126    322068375 :   if (is_partial)
    6127      7926324 :     return process_partial_specialization (decl);
    6128              : 
    6129    314142051 :   tree args = current_template_args ();
    6130    314142051 :   tree tmpl = NULL_TREE;
    6131    314142051 :   bool new_template_p = false;
    6132    314142051 :   if (local_p)
    6133              :     {
    6134              :       /* Does not get a template head.  */
    6135     67217083 :       tmpl = NULL_TREE;
    6136     67217083 :       gcc_checking_assert (!is_primary);
    6137              :     }
    6138    246924968 :   else if (!ctx
    6139    193458093 :            || TREE_CODE (ctx) == FUNCTION_DECL
    6140    192259917 :            || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
    6141     68946847 :            || (DECL_IMPLICIT_TYPEDEF_P (decl)
    6142       165062 :                && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    6143    315871664 :            || (is_friend && !(DECL_LANG_SPECIFIC (decl)
    6144           84 :                               && DECL_TEMPLATE_INFO (decl))))
    6145              :     {
    6146    177978359 :       if (DECL_LANG_SPECIFIC (decl)
    6147    161181395 :           && DECL_TEMPLATE_INFO (decl)
    6148    184006703 :           && DECL_TI_TEMPLATE (decl))
    6149      6028344 :         tmpl = DECL_TI_TEMPLATE (decl);
    6150              :       /* If DECL is a TYPE_DECL for a class-template, then there won't
    6151              :          be DECL_LANG_SPECIFIC.  The information equivalent to
    6152              :          DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
    6153     56177013 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6154     13572208 :                && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
    6155    172012373 :                && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
    6156              :         {
    6157              :           /* Since a template declaration already existed for this
    6158              :              class-type, we must be redeclaring it here.  Make sure
    6159              :              that the redeclaration is valid.  */
    6160        31179 :           redeclare_class_template (TREE_TYPE (decl),
    6161              :                                     current_template_parms,
    6162              :                                     current_template_constraints ());
    6163              :           /* We don't need to create a new TEMPLATE_DECL; just use the
    6164              :              one we already had.  */
    6165        62358 :           tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
    6166              :         }
    6167              :       else
    6168              :         {
    6169    171918836 :           tmpl = build_template_decl (decl, current_template_parms,
    6170              :                                       member_template_p);
    6171    171918836 :           new_template_p = true;
    6172              : 
    6173    171918836 :           if (DECL_LANG_SPECIFIC (decl)
    6174    171918836 :               && DECL_TEMPLATE_SPECIALIZATION (decl))
    6175              :             {
    6176              :               /* A specialization of a member template of a template
    6177              :                  class.  */
    6178            0 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    6179            0 :               DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
    6180            0 :               DECL_TEMPLATE_INFO (decl) = NULL_TREE;
    6181              :             }
    6182              :         }
    6183              :     }
    6184              :   else
    6185              :     {
    6186     68946609 :       tree a, t, current, parms;
    6187     68946609 :       int i;
    6188     68946609 :       tree tinfo = get_template_info (decl);
    6189              : 
    6190     68946609 :       if (!tinfo)
    6191              :         {
    6192           15 :           error ("template definition of non-template %q#D", decl);
    6193           15 :           return error_mark_node;
    6194              :         }
    6195              : 
    6196     68946594 :       tmpl = TI_TEMPLATE (tinfo);
    6197              : 
    6198     68946594 :       if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    6199     68555648 :           && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
    6200     68555648 :           && DECL_TEMPLATE_SPECIALIZATION (decl)
    6201     68946612 :           && DECL_MEMBER_TEMPLATE_P (tmpl))
    6202              :         {
    6203              :           /* The declaration is a specialization of a member
    6204              :              template, declared outside the class.  Therefore, the
    6205              :              innermost template arguments will be NULL, so we
    6206              :              replace them with the arguments determined by the
    6207              :              earlier call to check_explicit_specialization.  */
    6208           12 :           args = DECL_TI_ARGS (decl);
    6209              : 
    6210           12 :           tree new_tmpl
    6211           12 :             = build_template_decl (decl, current_template_parms,
    6212              :                                    member_template_p);
    6213           12 :           DECL_TI_TEMPLATE (decl) = new_tmpl;
    6214           12 :           SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
    6215           12 :           DECL_TEMPLATE_INFO (new_tmpl)
    6216           12 :             = build_template_info (tmpl, args);
    6217              : 
    6218           12 :           register_specialization (new_tmpl,
    6219              :                                    most_general_template (tmpl),
    6220              :                                    args,
    6221              :                                    is_friend, 0);
    6222           12 :           return decl;
    6223              :         }
    6224              : 
    6225              :       /* Make sure the template headers we got make sense.  */
    6226              : 
    6227     68946582 :       parms = DECL_TEMPLATE_PARMS (tmpl);
    6228     68946582 :       i = TMPL_PARMS_DEPTH (parms);
    6229    192398009 :       if (TMPL_ARGS_DEPTH (args) != i)
    6230              :         {
    6231           12 :           error ("expected %d levels of template parms for %q#D, got %d",
    6232            6 :                  i, decl, TMPL_ARGS_DEPTH (args));
    6233            6 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6234            6 :           return error_mark_node;
    6235              :         }
    6236              :       else
    6237    152398132 :         for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
    6238              :           {
    6239    166903142 :             a = TMPL_ARGS_LEVEL (args, i);
    6240     83451571 :             t = INNERMOST_TEMPLATE_PARMS (parms);
    6241              : 
    6242     83451571 :             if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
    6243              :               {
    6244           15 :                 if (current == decl)
    6245           15 :                   error ("got %d template parameters for %q#D",
    6246           15 :                          TREE_VEC_LENGTH (a), decl);
    6247              :                 else
    6248            0 :                   error ("got %d template parameters for %q#T",
    6249            0 :                          TREE_VEC_LENGTH (a), current);
    6250           15 :                 error ("  but %d required", TREE_VEC_LENGTH (t));
    6251              :                 /* Avoid crash in import_export_decl.  */
    6252           15 :                 DECL_INTERFACE_KNOWN (decl) = 1;
    6253           15 :                 return error_mark_node;
    6254              :               }
    6255              : 
    6256     83451556 :             if (current == decl)
    6257              :               current = ctx;
    6258     14504995 :             else if (current == NULL_TREE)
    6259              :               /* Can happen in erroneous input.  */
    6260              :               break;
    6261              :             else
    6262     14504995 :               current = get_containing_scope (current);
    6263              :           }
    6264              : 
    6265              :       /* Check that the parms are used in the appropriate qualifying scopes
    6266              :          in the declarator.  */
    6267    137893122 :       if (!comp_template_args
    6268     68946561 :           (TI_ARGS (tinfo),
    6269     68946561 :            TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
    6270              :         {
    6271            3 :           auto_diagnostic_group d;
    6272            3 :           error ("template arguments to %qD do not match original "
    6273            3 :                  "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
    6274            3 :           if (!uses_template_parms (TI_ARGS (tinfo)))
    6275            3 :             inform (input_location, "use %<template<>%> for"
    6276              :                     " an explicit specialization");
    6277              :           /* Avoid crash in import_export_decl.  */
    6278            3 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6279            3 :           return error_mark_node;
    6280            3 :         }
    6281              : 
    6282              :       /* Check that the constraints for each enclosing template scope are
    6283              :          consistent with the original declarations.  */
    6284     68946558 :       if (flag_concepts)
    6285              :         {
    6286     68046403 :           tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
    6287     68046403 :           tree scope_parms = current_template_parms;
    6288     68046403 :           if (PRIMARY_TEMPLATE_P (tmpl))
    6289              :             {
    6290     18331024 :               decl_parms = TREE_CHAIN (decl_parms);
    6291     18331024 :               scope_parms = TREE_CHAIN (scope_parms);
    6292              :             }
    6293    132132021 :           while (decl_parms)
    6294              :             {
    6295     64085636 :               if (!template_requirements_equivalent_p (decl_parms, scope_parms))
    6296              :                 {
    6297           18 :                   error ("redeclaration of %qD with different constraints",
    6298           18 :                          TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
    6299           18 :                   break;
    6300              :                 }
    6301     64085618 :               decl_parms = TREE_CHAIN (decl_parms);
    6302     64085618 :               scope_parms = TREE_CHAIN (scope_parms);
    6303              :             }
    6304              :         }
    6305              :     }
    6306              : 
    6307    493849834 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6308              : 
    6309    314142000 :   if (new_template_p)
    6310              :     {
    6311              :       /* Push template declarations for global functions and types.
    6312              :          Note that we do not try to push a global template friend
    6313              :          declared in a template class; such a thing may well depend on
    6314              :          the template parameters of the class and we'll push it when
    6315              :          instantiating the befriending class.  */
    6316    171918836 :       if (!ctx
    6317    171918836 :           && !(is_friend && template_class_depth (current_class_type) > 0))
    6318              :         {
    6319     45268530 :           tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
    6320     45268530 :           if (pushed == error_mark_node)
    6321              :             return error_mark_node;
    6322              : 
    6323              :           /* pushdecl may have found an existing template.  */
    6324     45268473 :           if (pushed != tmpl)
    6325              :             {
    6326      3014577 :               decl = DECL_TEMPLATE_RESULT (pushed);
    6327      3014577 :               tmpl = NULL_TREE;
    6328              :             }
    6329              :         }
    6330    126650306 :       else if (is_friend)
    6331              :         {
    6332              :           /* Record this decl as belonging to the current class.  It's
    6333              :              not chained onto anything else.  */
    6334      3365820 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
    6335      3365820 :           gcc_checking_assert (!DECL_CHAIN (tmpl));
    6336      3365820 :           DECL_CHAIN (tmpl) = current_scope ();
    6337              :         }
    6338              :     }
    6339    142223164 :   else if (tmpl)
    6340              :     /* The type may have been completed, or (erroneously) changed.  */
    6341     75006081 :     TREE_TYPE (tmpl) = TREE_TYPE (decl);
    6342              : 
    6343    246924860 :   if (tmpl)
    6344              :     {
    6345    243910283 :       if (is_primary)
    6346              :         {
    6347     67342260 :           tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6348              : 
    6349     67342260 :           DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6350              : 
    6351              :           /* Give template template parms a DECL_CONTEXT of the template
    6352              :              for which they are a parameter.  */
    6353     67342260 :           parms = INNERMOST_TEMPLATE_PARMS (parms);
    6354    188640910 :           for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
    6355              :             {
    6356    121298650 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6357    121298650 :               if (TREE_CODE (parm) == TEMPLATE_DECL)
    6358       210539 :                 DECL_CONTEXT (parm) = tmpl;
    6359              :             }
    6360              : 
    6361     67342260 :           if (TREE_CODE (decl) == TYPE_DECL
    6362     67342260 :               && TYPE_DECL_ALIAS_P (decl))
    6363              :             {
    6364      4477513 :               if (tree constr
    6365      4477513 :                   = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
    6366              :                 {
    6367              :                   /* ??? Why don't we do this here for all templates?  */
    6368       263564 :                   constr = build_constraints (constr, NULL_TREE);
    6369       263564 :                   set_constraints (decl, constr);
    6370              :                 }
    6371              :             }
    6372              :         }
    6373              : 
    6374              :       /* The DECL_TI_ARGS of DECL contains full set of arguments
    6375              :          referring wback to its most general template.  If TMPL is a
    6376              :          specialization, ARGS may only have the innermost set of
    6377              :          arguments.  Add the missing argument levels if necessary.  */
    6378    243910283 :       if (DECL_TEMPLATE_INFO (tmpl))
    6379         1091 :         args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
    6380              : 
    6381              :       /* Bug c++/103901.  Let's sorry now rather than ICE later.  */
    6382    243910283 :       if (TREE_VEC_LENGTH (args) == 0
    6383          270 :           && ctx && LAMBDA_FUNCTION_P (ctx)
    6384            3 :           && DECL_IMPLICIT_TYPEDEF_P (decl)
    6385    243910286 :           && CLASS_TYPE_P (TREE_TYPE (decl)))
    6386            3 :         sorry ("local class in lambda in template parameter list");
    6387              : 
    6388    243910283 :       tree info = build_template_info (tmpl, args);
    6389              : 
    6390    243910283 :       if (DECL_IMPLICIT_TYPEDEF_P (decl))
    6391     13654561 :         SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
    6392              :       else
    6393              :         {
    6394    230255722 :           retrofit_lang_decl (decl);
    6395    230255722 :           DECL_TEMPLATE_INFO (decl) = info;
    6396              :         }
    6397              :     }
    6398              : 
    6399    314141943 :   if (is_typedef_decl (decl)
    6400     54569410 :       && (dependent_opaque_alias_p (TREE_TYPE (decl))
    6401     54569233 :           || dependent_alias_template_spec_p (TREE_TYPE (decl), nt_opaque)))
    6402              :     {
    6403              :       /* Manually mark such aliases as dependent so that dependent_type_p_r
    6404              :          doesn't have to handle them.  */
    6405       597722 :       TYPE_DEPENDENT_P_VALID (TREE_TYPE (decl)) = true;
    6406       597722 :       TYPE_DEPENDENT_P (TREE_TYPE (decl)) = true;
    6407              :       /* The identity of such aliases is hairy; see structural_comptypes.  */
    6408       597722 :       SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (decl));
    6409              :     }
    6410              : 
    6411    314141943 :   if (flag_implicit_templates
    6412    313442077 :       && !is_friend
    6413    304521671 :       && TREE_PUBLIC (decl)
    6414    178788335 :       && VAR_OR_FUNCTION_DECL_P (decl))
    6415              :     /* Set DECL_COMDAT on template instantiations; if we force
    6416              :        them to be emitted by explicit instantiation,
    6417              :        mark_needed will tell cgraph to do the right thing.  */
    6418    176068172 :     DECL_COMDAT (decl) = true;
    6419              : 
    6420    558052226 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6421              : 
    6422              :   return decl;
    6423              : }
    6424              : 
    6425              : /* FN is an inheriting constructor that inherits from the constructor
    6426              :    template INHERITED; turn FN into a constructor template with a matching
    6427              :    template header.  */
    6428              : 
    6429              : tree
    6430       115917 : add_inherited_template_parms (tree fn, tree inherited)
    6431              : {
    6432       115917 :   tree inner_parms
    6433       115917 :     = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
    6434       115917 :   inner_parms = copy_node (inner_parms);
    6435       115917 :   tree parms
    6436       115917 :     = tree_cons (size_int (current_template_depth + 1),
    6437              :                  inner_parms, current_template_parms);
    6438       115917 :   tree tmpl = build_template_decl (fn, parms, /*member*/true);
    6439       115917 :   tree args = template_parms_to_args (parms);
    6440       115917 :   DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
    6441       115917 :   DECL_ARTIFICIAL (tmpl) = true;
    6442       115917 :   DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6443       115917 :   return tmpl;
    6444              : }
    6445              : 
    6446              : /* Called when a class template TYPE is redeclared with the indicated
    6447              :    template PARMS, e.g.:
    6448              : 
    6449              :      template <class T> struct S;
    6450              :      template <class T> struct S {};  */
    6451              : 
    6452              : bool
    6453      2629980 : redeclare_class_template (tree type, tree parms, tree cons)
    6454              : {
    6455      2629980 :   tree tmpl;
    6456      2629980 :   tree tmpl_parms;
    6457      2629980 :   int i;
    6458              : 
    6459      2629980 :   if (!TYPE_TEMPLATE_INFO (type))
    6460              :     {
    6461            0 :       error ("%qT is not a template type", type);
    6462            0 :       return false;
    6463              :     }
    6464              : 
    6465      2629980 :   tmpl = TYPE_TI_TEMPLATE (type);
    6466      2629980 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6467              :     /* The type is nested in some template class.  Nothing to worry
    6468              :        about here; there are no new template parameters for the nested
    6469              :        type.  */
    6470              :     return true;
    6471              : 
    6472      2629980 :   if (!parms)
    6473              :     {
    6474            3 :       error ("template specifiers not specified in declaration of %qD",
    6475              :              tmpl);
    6476            3 :       return false;
    6477              :     }
    6478              : 
    6479      2629977 :   parms = INNERMOST_TEMPLATE_PARMS (parms);
    6480      2629977 :   tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
    6481              : 
    6482      2629977 :   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
    6483              :     {
    6484           21 :       auto_diagnostic_group d;
    6485           21 :       error_n (input_location, TREE_VEC_LENGTH (parms),
    6486              :                "redeclared with %d template parameter",
    6487              :                "redeclared with %d template parameters",
    6488           21 :                TREE_VEC_LENGTH (parms));
    6489           42 :       inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
    6490              :                 "previous declaration %qD used %d template parameter",
    6491              :                 "previous declaration %qD used %d template parameters",
    6492           21 :                 tmpl, TREE_VEC_LENGTH (tmpl_parms));
    6493           21 :       return false;
    6494           21 :     }
    6495              : 
    6496      6991952 :   for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
    6497              :     {
    6498      4362044 :       tree tmpl_parm;
    6499      4362044 :       tree parm;
    6500              : 
    6501      4362044 :       if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
    6502      4362044 :           || TREE_VEC_ELT (parms, i) == error_mark_node)
    6503            0 :         continue;
    6504              : 
    6505      4362044 :       tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
    6506      4362044 :       if (error_operand_p (tmpl_parm))
    6507              :         return false;
    6508              : 
    6509      4362029 :       parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6510              : 
    6511              :       /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
    6512              :          TEMPLATE_DECL.  */
    6513      4362029 :       if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
    6514      4362017 :           || (TREE_CODE (tmpl_parm) != TYPE_DECL
    6515       498598 :               && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
    6516      4362008 :           || (TREE_CODE (tmpl_parm) != PARM_DECL
    6517      3863988 :               && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
    6518      3863988 :                   != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
    6519      8724034 :           || (TREE_CODE (tmpl_parm) == PARM_DECL
    6520       498020 :               && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
    6521       498020 :                   != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
    6522              :         {
    6523           27 :           auto_diagnostic_group d;
    6524           27 :           error ("template parameter %q+#D", tmpl_parm);
    6525           27 :           if (DECL_P (parm))
    6526           24 :             inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
    6527              :           else
    6528            3 :             inform (input_location, "redeclared here");
    6529           27 :           return false;
    6530           27 :         }
    6531              : 
    6532              :       /* The parameters can be declared to introduce different
    6533              :          constraints.  */
    6534      4362002 :       tree p1 = TREE_VEC_ELT (tmpl_parms, i);
    6535      4362002 :       tree p2 = TREE_VEC_ELT (parms, i);
    6536      4362002 :       if (!template_parameter_constraints_equivalent_p (p1, p2))
    6537              :         {
    6538            6 :           auto_diagnostic_group d;
    6539            6 :           error ("declaration of template parameter %q+#D with different "
    6540              :                  "constraints", parm);
    6541            6 :           inform (DECL_SOURCE_LOCATION (tmpl_parm),
    6542              :                   "original declaration appeared here");
    6543            6 :           return false;
    6544            6 :         }
    6545              : 
    6546              :       /* Give each template template parm in this redeclaration a
    6547              :          DECL_CONTEXT of the template for which they are a parameter.  */
    6548      4361996 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    6549              :         {
    6550          569 :           gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
    6551              :                                || DECL_CONTEXT (parm) == tmpl);
    6552          569 :           DECL_CONTEXT (parm) = tmpl;
    6553              :         }
    6554              :     }
    6555              : 
    6556      2629908 :   if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
    6557              :     return false;
    6558              : 
    6559      2629905 :   tree ci = get_constraints (tmpl);
    6560      2684535 :   tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
    6561      2684538 :   tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
    6562              : 
    6563              :   /* Two classes with different constraints declare different entities.  */
    6564      2629905 :   if (!cp_tree_equal (req1, req2))
    6565              :     {
    6566           15 :       auto_diagnostic_group d;
    6567           15 :       error_at (input_location, "redeclaration of %q#D with different "
    6568              :                                 "constraints", tmpl);
    6569           15 :       inform (DECL_SOURCE_LOCATION (tmpl),
    6570              :               "original declaration appeared here");
    6571           15 :       return false;
    6572           15 :     }
    6573              : 
    6574              :     return true;
    6575              : }
    6576              : 
    6577              : /* The actual substitution part of instantiate_non_dependent_expr,
    6578              :    to be used when the caller has already checked
    6579              :     !instantiation_dependent_uneval_expression_p (expr)
    6580              :    and cleared processing_template_decl.  */
    6581              : 
    6582              : tree
    6583     69604114 : instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
    6584              : {
    6585     69604114 :   return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE);
    6586              : }
    6587              : 
    6588              : /* Instantiate the non-dependent expression EXPR.  */
    6589              : 
    6590              : tree
    6591    133068695 : instantiate_non_dependent_expr (tree expr,
    6592              :                                 tsubst_flags_t complain /* = tf_error */)
    6593              : {
    6594    133068695 :   if (expr == NULL_TREE)
    6595              :     return NULL_TREE;
    6596              : 
    6597    133068695 :   if (processing_template_decl)
    6598              :     {
    6599              :       /* The caller should have checked this already.  */
    6600     35684931 :       gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
    6601     35684931 :       processing_template_decl_sentinel s;
    6602     35684931 :       expr = instantiate_non_dependent_expr_internal (expr, complain);
    6603     35684931 :     }
    6604              :   return expr;
    6605              : }
    6606              : 
    6607              : /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
    6608              :    expression is dependent or non-constant.  */
    6609              : 
    6610              : tree
    6611    144430116 : instantiate_non_dependent_or_null (tree expr)
    6612              : {
    6613    144430116 :   if (expr == NULL_TREE)
    6614              :     return NULL_TREE;
    6615    133414774 :   if (processing_template_decl)
    6616              :     {
    6617       347056 :       if (!is_nondependent_constant_expression (expr))
    6618              :         expr = NULL_TREE;
    6619              :       else
    6620              :         {
    6621       340906 :           processing_template_decl_sentinel s;
    6622       340906 :           expr = instantiate_non_dependent_expr_internal (expr, tf_error);
    6623       340906 :         }
    6624              :     }
    6625              :   return expr;
    6626              : }
    6627              : 
    6628              : /* True iff T is a specialization of a variable template.  */
    6629              : 
    6630              : bool
    6631    264505408 : variable_template_specialization_p (tree t)
    6632              : {
    6633    264505408 :   if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
    6634              :     return false;
    6635     25399313 :   tree tmpl = DECL_TI_TEMPLATE (t);
    6636     25399313 :   return variable_template_p (tmpl);
    6637              : }
    6638              : 
    6639              : /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
    6640              :    template declaration, or a TYPE_DECL for an alias declaration.  */
    6641              : 
    6642              : bool
    6643    150455881 : alias_type_or_template_p (tree t)
    6644              : {
    6645    150455881 :   if (t == NULL_TREE)
    6646              :     return false;
    6647            0 :   return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
    6648    150455881 :           || (TYPE_P (t)
    6649    150455881 :               && TYPE_NAME (t)
    6650    150455881 :               && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    6651    293650946 :           || DECL_ALIAS_TEMPLATE_P (t));
    6652              : }
    6653              : 
    6654              : /* If T is a specialization of an alias template, return it; otherwise return
    6655              :    NULL_TREE.  If TRANSPARENT_TYPEDEFS is true, look through other aliases.  */
    6656              : 
    6657              : tree
    6658   8131539229 : alias_template_specialization_p (const_tree t,
    6659              :                                  bool transparent_typedefs)
    6660              : {
    6661   8131539229 :   if (!TYPE_P (t))
    6662              :     return NULL_TREE;
    6663              : 
    6664              :   /* It's an alias template specialization if it's an alias and its
    6665              :      TYPE_NAME is a specialization of a primary template.  */
    6666   8131539171 :   if (typedef_variant_p (t))
    6667              :     {
    6668    674382304 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6669    608199462 :         if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    6670              :           return const_cast<tree> (t);
    6671    437825507 :       if (transparent_typedefs && !dependent_opaque_alias_p (t))
    6672       123607 :         return alias_template_specialization_p (DECL_ORIGINAL_TYPE
    6673              :                                                 (TYPE_NAME (t)),
    6674       123607 :                                                 transparent_typedefs);
    6675              :     }
    6676              : 
    6677              :   return NULL_TREE;
    6678              : }
    6679              : 
    6680              : /* A cache of the result of complex_alias_template_p.  */
    6681              : 
    6682              : static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
    6683              : 
    6684              : /* Data structure for complex_alias_template_*.  */
    6685              : 
    6686              : struct uses_all_template_parms_data
    6687              : {
    6688              :   int level;
    6689              :   tree *seen;
    6690              : };
    6691              : 
    6692              : /* walk_tree callback for complex_alias_template_p.  */
    6693              : 
    6694              : static tree
    6695     57551377 : complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
    6696              : {
    6697     57551377 :   tree t = *tp;
    6698     57551377 :   auto &data = *(struct uses_all_template_parms_data*)data_;
    6699              : 
    6700     57551377 :   switch (TREE_CODE (t))
    6701              :     {
    6702     10780166 :     case TEMPLATE_TYPE_PARM:
    6703     10780166 :     case TEMPLATE_PARM_INDEX:
    6704     10780166 :     case TEMPLATE_TEMPLATE_PARM:
    6705     10780166 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    6706     10780166 :       {
    6707     10780166 :         tree idx = get_template_parm_index (t);
    6708     10780166 :         if (TEMPLATE_PARM_LEVEL (idx) == data.level)
    6709      9148893 :           data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
    6710              :       }
    6711              : 
    6712     57551377 :     default:;
    6713              :     }
    6714              : 
    6715     57551377 :   if (!PACK_EXPANSION_P (t))
    6716              :     return 0;
    6717              : 
    6718              :   /* An alias template with a pack expansion that expands a pack from the
    6719              :      enclosing class needs to be considered complex, to avoid confusion with
    6720              :      the same pack being used as an argument to the alias's own template
    6721              :      parameter (91966).  */
    6722      2971209 :   for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
    6723       974589 :        pack = TREE_CHAIN (pack))
    6724              :     {
    6725      1064350 :       tree parm_pack = TREE_VALUE (pack);
    6726      1064350 :       if (!TEMPLATE_PARM_P (parm_pack))
    6727        37838 :         continue;
    6728      1026512 :       int idx, level;
    6729      1026512 :       template_parm_level_and_index (parm_pack, &level, &idx);
    6730      1026512 :       if (level < data.level)
    6731        89761 :         return t;
    6732              : 
    6733              :       /* Consider the expanded packs to be used outside the expansion...  */
    6734       936751 :       data.seen[idx] = boolean_true_node;
    6735              :     }
    6736              : 
    6737              :   /* ...but don't walk into the pattern.  Consider PR104008:
    6738              : 
    6739              :      template <typename T, typename... Ts>
    6740              :      using IsOneOf = disjunction<is_same<T, Ts>...>;
    6741              : 
    6742              :      where IsOneOf seemingly uses all of its template parameters in its
    6743              :      expansion (and does not expand a pack from the enclosing class), so the
    6744              :      alias was not marked as complex.  However, if it is used like
    6745              :      "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
    6746              :      expansion.  So only Ts is considered used by the pack expansion.  */
    6747       962553 :   *walk_subtrees = false;
    6748              : 
    6749       962553 :   return 0;
    6750              : }
    6751              : 
    6752              : /* An alias template is complex from a SFINAE perspective if a template-id
    6753              :    using that alias can be ill-formed when the expansion is not, as with
    6754              :    the void_t template.
    6755              : 
    6756              :    If this predicate returns true in the ordinary case, the out parameter
    6757              :    SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
    6758              :    the I'th template parameter of the alias template is used in the alias.  */
    6759              : 
    6760              : static bool
    6761    143371253 : complex_alias_template_p (const_tree tmpl, tree *seen_out)
    6762              : {
    6763    143371253 :   tmpl = most_general_template (tmpl);
    6764    143371253 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6765              :     return false;
    6766              : 
    6767              :   /* A renaming alias isn't complex.  */
    6768    103317260 :   if (get_underlying_template (const_cast<tree> (tmpl)) != tmpl)
    6769              :     return false;
    6770              : 
    6771              :   /* Any other constrained alias is complex.  */
    6772     95589715 :   if (get_constraints (tmpl))
    6773              :     return true;
    6774              : 
    6775              :   /* An alias with dependent type attributes is complex.  */
    6776     74357192 :   if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
    6777              :     return true;
    6778              : 
    6779     74357117 :   if (!complex_alias_tmpl_info)
    6780        93824 :     complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
    6781              : 
    6782     74357117 :   if (tree *slot = complex_alias_tmpl_info->get (tmpl))
    6783              :     {
    6784     67806921 :       tree result = *slot;
    6785     67806921 :       if (result == boolean_false_node)
    6786              :         return false;
    6787      8494150 :       if (result == boolean_true_node)
    6788              :         return true;
    6789      7722111 :       gcc_assert (TREE_CODE (result) == TREE_VEC);
    6790      7722111 :       if (seen_out)
    6791      7722111 :         *seen_out = result;
    6792      7722111 :       return true;
    6793              :     }
    6794              : 
    6795      6550196 :   struct uses_all_template_parms_data data;
    6796      6550196 :   tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6797      6550196 :   tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6798      6550196 :   data.level = TMPL_PARMS_DEPTH (parms);
    6799      6550196 :   int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
    6800      6550196 :   tree seen = make_tree_vec (len);
    6801      6550196 :   data.seen = TREE_VEC_BEGIN (seen);
    6802     16242697 :   for (int i = 0; i < len; ++i)
    6803      9692501 :     data.seen[i] = boolean_false_node;
    6804              : 
    6805      6550196 :   if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
    6806              :     {
    6807        89761 :       complex_alias_tmpl_info->put (tmpl, boolean_true_node);
    6808        89761 :       return true;
    6809              :     }
    6810              : 
    6811     15218558 :   for (int i = 0; i < len; ++i)
    6812      9232341 :     if (data.seen[i] != boolean_true_node)
    6813              :       {
    6814       474218 :         complex_alias_tmpl_info->put (tmpl, seen);
    6815       474218 :         if (seen_out)
    6816       474218 :           *seen_out = seen;
    6817       474218 :         return true;
    6818              :       }
    6819              : 
    6820      5986217 :   complex_alias_tmpl_info->put (tmpl, boolean_false_node);
    6821      5986217 :   return false;
    6822              : }
    6823              : 
    6824              : /* If T is a specialization of a complex alias template with a dependent
    6825              :    argument for an unused template parameter, return it; otherwise return
    6826              :    NULL_TREE.  If T is a typedef to such a specialization, return the
    6827              :    specialization.  This predicate is usually checked alongside
    6828              :    dependent_opaque_alias_p.  Whereas dependent_opaque_alias_p checks
    6829              :    type equivalence of an alias vs its expansion, this predicate more
    6830              :    broadly checks SFINAE equivalence.  */
    6831              : 
    6832              : tree
    6833    326006896 : dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
    6834              : {
    6835    326006896 :   if (t == error_mark_node)
    6836              :     return NULL_TREE;
    6837    290893669 :   gcc_assert (TYPE_P (t));
    6838              : 
    6839    290893669 :   if (!processing_template_decl || !typedef_variant_p (t))
    6840              :     return NULL_TREE;
    6841              : 
    6842    156291233 :   if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6843              :     {
    6844    143371253 :       tree seen = NULL_TREE;
    6845    143371253 :       if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
    6846              :         {
    6847     30290727 :           tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
    6848     30290727 :           if (!seen)
    6849              :             {
    6850     22094398 :               if (any_dependent_template_arguments_p (args))
    6851     30290727 :                 return const_cast<tree> (t);
    6852              :             }
    6853              :           else
    6854              :             {
    6855      8196329 :               gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
    6856     14016001 :               for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    6857     11978347 :                 if (TREE_VEC_ELT (seen, i) != boolean_true_node
    6858     11978347 :                     && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
    6859              :                   return const_cast<tree> (t);
    6860              :             }
    6861              : 
    6862      2122432 :           return NULL_TREE;
    6863              :         }
    6864              :     }
    6865              : 
    6866    126000506 :   if (transparent_typedefs && !dependent_opaque_alias_p (t))
    6867              :     {
    6868      2912376 :       tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
    6869      2912376 :       return dependent_alias_template_spec_p (utype, transparent_typedefs);
    6870              :     }
    6871              : 
    6872              :   return NULL_TREE;
    6873              : }
    6874              : 
    6875              : /* Return true if substituting into T would yield a different type than
    6876              :    substituting into its expansion.  This predicate is usually checked
    6877              :    alongside dependent_alias_template_spec_p.  */
    6878              : 
    6879              : bool
    6880    494417795 : dependent_opaque_alias_p (const_tree t)
    6881              : {
    6882    588903697 :   auto any_lambda_targ_p = [] (tree args)
    6883              :     {
    6884    295190206 :       for (tree arg : tree_vec_range (args))
    6885    200704337 :         if (TREE_CODE (arg) == LAMBDA_EXPR)
    6886           33 :           return true;
    6887     94485869 :       return false;
    6888              :     };
    6889              : 
    6890    494417795 :   return (TYPE_P (t)
    6891    494417795 :           && typedef_variant_p (t)
    6892    965990056 :           && (any_dependent_type_attributes_p (DECL_ATTRIBUTES
    6893              :                                                (TYPE_NAME (t)))
    6894              :               /* Treat a dependent decltype(lambda) alias as opaque so that we
    6895              :                  don't prematurely strip it when used as a template argument.
    6896              :                  Otherwise substitution into each occurrence of the (stripped)
    6897              :                  alias would incorrectly yield a distinct lambda type.  */
    6898    471571597 :               || (TREE_CODE (t) == DECLTYPE_TYPE
    6899     23050462 :                   && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == LAMBDA_EXPR
    6900          110 :                   && !typedef_variant_p (DECL_ORIGINAL_TYPE (TYPE_NAME (t))))
    6901              :               /* Also treat an alias to A<lambda> as opaque so that it doesn't
    6902              :                  "leak" into a deeper template context which would cause us to
    6903              :                  over substitute into the lambda.  */
    6904              :               /* FIXME These lambda checks don't recognize deeply nested lambda
    6905              :                  subexpressions, and we can't use walk_tree here because it's
    6906              :                  slow.  Maybe a tree flag indicating typedef opaqueness?  */
    6907    471571499 :               || (TYPE_TEMPLATE_INFO (t)
    6908     98416625 :                   && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
    6909     94485902 :                   && any_lambda_targ_p (INNERMOST_TEMPLATE_ARGS
    6910    494417795 :                                         (TYPE_TI_ARGS (t))))));
    6911              : }
    6912              : 
    6913              : /* Return the number of innermost template parameters in TMPL.  */
    6914              : 
    6915              : static int
    6916     86210152 : num_innermost_template_parms (const_tree tmpl)
    6917              : {
    6918     86210152 :   tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6919     86210152 :   return TREE_VEC_LENGTH (parms);
    6920              : }
    6921              : 
    6922              : /* Return either TMPL or another template that it is equivalent to under DR
    6923              :    1286: An alias that just changes the name of a template is equivalent to
    6924              :    the other template.  */
    6925              : 
    6926              : static tree
    6927    119680997 : get_underlying_template (tree tmpl)
    6928              : {
    6929    119680997 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
    6930    127410194 :   while (DECL_ALIAS_TEMPLATE_P (tmpl))
    6931              :     {
    6932              :       /* Determine if the alias is equivalent to an underlying template.  */
    6933    110909374 :       tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6934              :       /* The underlying type may have been ill-formed. Don't proceed.  */
    6935    110909374 :       if (!orig_type)
    6936              :         break;
    6937    110909371 :       tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
    6938     43106839 :       if (!tinfo)
    6939              :         break;
    6940              : 
    6941     43105160 :       tree underlying = TI_TEMPLATE (tinfo);
    6942     43105160 :       if (!PRIMARY_TEMPLATE_P (underlying)
    6943     86210236 :           || (num_innermost_template_parms (tmpl)
    6944     43105076 :               != num_innermost_template_parms (underlying)))
    6945              :         break;
    6946              : 
    6947              :       /* Does the alias add cv-quals?  */
    6948     23356077 :       if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
    6949              :         break;
    6950              : 
    6951     23356054 :       tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
    6952     23356054 :       if (!comp_template_args (TI_ARGS (tinfo), alias_args))
    6953              :         break;
    6954              : 
    6955              :       /* Are any default template arguments equivalent?  */
    6956      7729409 :       tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6957      7729409 :       tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
    6958      7729409 :       const int nparms = TREE_VEC_LENGTH (aparms);
    6959     16592338 :       for (int i = 0; i < nparms; ++i)
    6960              :         {
    6961      8863070 :           tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
    6962      8863070 :           tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
    6963      8863070 :           if (!template_args_equal (adefarg, udefarg))
    6964          141 :             goto top_break;
    6965              :         }
    6966              : 
    6967              :       /* If TMPL adds or changes any constraints, it isn't equivalent.  I think
    6968              :          it's appropriate to treat a less-constrained alias as equivalent.  */
    6969      7729268 :       if (!at_least_as_constrained (underlying, tmpl))
    6970              :         break;
    6971              : 
    6972              :       /* If TMPL adds dependent type attributes, it isn't equivalent.  */
    6973      7729205 :       if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
    6974              :         break;
    6975              : 
    6976              :       /* Alias is equivalent.  Strip it and repeat.  */
    6977              :       tmpl = underlying;
    6978              :     }
    6979     15628421 :   top_break:;
    6980              : 
    6981    119680997 :   return tmpl;
    6982              : }
    6983              : 
    6984              : /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
    6985              :    must be a reference-to-function or a pointer-to-function type, as specified
    6986              :    in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
    6987              :    and check that the resulting function has external linkage.  */
    6988              : 
    6989              : static tree
    6990          889 : convert_nontype_argument_function (tree type, tree expr,
    6991              :                                    tsubst_flags_t complain)
    6992              : {
    6993          889 :   tree fns = expr;
    6994          889 :   tree fn, fn_no_ptr;
    6995          889 :   linkage_kind linkage;
    6996              : 
    6997          889 :   fn = instantiate_type (type, fns, tf_none);
    6998          889 :   if (fn == error_mark_node)
    6999              :     return error_mark_node;
    7000              : 
    7001          884 :   if (value_dependent_expression_p (fn))
    7002           21 :     goto accept;
    7003              : 
    7004          863 :   fn_no_ptr = fn;
    7005          863 :   if (REFERENCE_REF_P (fn_no_ptr))
    7006           12 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    7007          863 :   fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
    7008          863 :   if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
    7009          735 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    7010          863 :   if (BASELINK_P (fn_no_ptr))
    7011            0 :     fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
    7012              : 
    7013              :   /* [temp.arg.nontype]/1
    7014              : 
    7015              :      A template-argument for a non-type, non-template template-parameter
    7016              :      shall be one of:
    7017              :      [...]
    7018              :      -- the address of an object or function with external [C++11: or
    7019              :         internal] linkage.  */
    7020              : 
    7021          863 :   STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
    7022          863 :   if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
    7023              :     {
    7024           35 :       if (complain & tf_error)
    7025              :         {
    7026           20 :           auto_diagnostic_group d;
    7027           20 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    7028           20 :           tree c;
    7029           20 :           if (cxx_dialect >= cxx17
    7030           20 :               && (c = cxx_constant_value (fn),
    7031           15 :                   c == error_mark_node))
    7032              :             ;
    7033              :           else
    7034              :             {
    7035            5 :               error_at (loc, "%qE is not a valid template argument for "
    7036              :                         "type %qT", expr, type);
    7037            5 :               if (TYPE_PTR_P (type))
    7038            5 :                 inform (loc, "it must be the address of a function "
    7039              :                         "with external linkage");
    7040              :               else
    7041            0 :                 inform (loc, "it must be the name of a function with "
    7042              :                         "external linkage");
    7043              :             }
    7044           20 :         }
    7045           35 :       return NULL_TREE;
    7046              :     }
    7047              : 
    7048          828 :   linkage = decl_linkage (fn_no_ptr);
    7049          828 :   if ((cxx_dialect < cxx11 && linkage != lk_external)
    7050          827 :       || (cxx_dialect < cxx17 && linkage == lk_none))
    7051              :     {
    7052            5 :       if (complain & tf_error)
    7053              :         {
    7054            2 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    7055            2 :           if (cxx_dialect >= cxx11)
    7056            2 :             error_at (loc, "%qE is not a valid template argument for type "
    7057              :                       "%qT because %qD has no linkage",
    7058              :                       expr, type, fn_no_ptr);
    7059              :           else
    7060            0 :             error_at (loc, "%qE is not a valid template argument for type "
    7061              :                       "%qT because %qD does not have external linkage",
    7062              :                       expr, type, fn_no_ptr);
    7063              :         }
    7064            5 :       return NULL_TREE;
    7065              :     }
    7066              : 
    7067          823 :  accept:
    7068          844 :   if (TYPE_REF_P (type))
    7069              :     {
    7070          104 :       if (REFERENCE_REF_P (fn))
    7071           11 :         fn = TREE_OPERAND (fn, 0);
    7072              :       else
    7073           93 :         fn = build_address (fn);
    7074              :     }
    7075          844 :   if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
    7076           93 :     fn = build_nop (type, fn);
    7077              : 
    7078              :   return fn;
    7079              : }
    7080              : 
    7081              : /* Subroutine of convert_nontype_argument.
    7082              :    Check if EXPR of type TYPE is a valid pointer-to-member constant.
    7083              :    Emit an error otherwise.  */
    7084              : 
    7085              : static bool
    7086         1000 : check_valid_ptrmem_cst_expr (tree type, tree expr,
    7087              :                              tsubst_flags_t complain)
    7088              : {
    7089         1000 :   tree orig_expr = expr;
    7090         1000 :   STRIP_NOPS (expr);
    7091         1000 :   if (null_ptr_cst_p (expr))
    7092              :     return true;
    7093         1000 :   if (TREE_CODE (expr) == PTRMEM_CST
    7094         1000 :       && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
    7095              :                       PTRMEM_CST_CLASS (expr)))
    7096              :     return true;
    7097          104 :   if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
    7098              :     return true;
    7099           37 :   if (processing_template_decl
    7100            0 :       && TREE_CODE (expr) == ADDR_EXPR
    7101           37 :       && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
    7102              :     return true;
    7103           37 :   if (complain & tf_error)
    7104              :     {
    7105           19 :       auto_diagnostic_group d;
    7106           19 :       location_t loc = cp_expr_loc_or_input_loc (orig_expr);
    7107           19 :       error_at (loc, "%qE is not a valid template argument for type %qT",
    7108              :                 orig_expr, type);
    7109           19 :       if (TREE_CODE (expr) != PTRMEM_CST)
    7110           10 :         inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
    7111              :       else
    7112            9 :         inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
    7113           19 :     }
    7114              :   return false;
    7115              : }
    7116              : 
    7117              : /* Returns TRUE iff the address of OP is value-dependent.
    7118              : 
    7119              :    14.6.2.4 [temp.dep.temp]:
    7120              :    A non-integral non-type template-argument is dependent if its type is
    7121              :    dependent or it has either of the following forms
    7122              :      qualified-id
    7123              :      & qualified-id
    7124              :    and contains a nested-name-specifier which specifies a class-name that
    7125              :    names a dependent type.
    7126              : 
    7127              :    We generalize this to just say that the address of a member of a
    7128              :    dependent class is value-dependent; the above doesn't cover the
    7129              :    address of a static data member named with an unqualified-id.  */
    7130              : 
    7131              : static bool
    7132      5373120 : has_value_dependent_address (tree op)
    7133              : {
    7134      5373120 :   STRIP_ANY_LOCATION_WRAPPER (op);
    7135              : 
    7136              :   /* We could use get_inner_reference here, but there's no need;
    7137              :      this is only relevant for template non-type arguments, which
    7138              :      can only be expressed as &id-expression.  */
    7139      5373120 :   if (DECL_P (op))
    7140              :     {
    7141       227036 :       tree ctx = CP_DECL_CONTEXT (op);
    7142              : 
    7143       227036 :       if (TYPE_P (ctx) && dependent_type_p (ctx))
    7144              :         return true;
    7145              : 
    7146       208473 :       if (VAR_P (op)
    7147        38823 :           && TREE_STATIC (op)
    7148        10280 :           && TREE_CODE (ctx) == FUNCTION_DECL
    7149          470 :           && DECL_TEMPLATE_INFO (ctx)
    7150       208833 :           && any_dependent_template_arguments_p (DECL_TI_ARGS (ctx)))
    7151              :         return true;
    7152              :     }
    7153              : 
    7154              :   return false;
    7155              : }
    7156              : 
    7157              : /* The next set of functions are used for providing helpful explanatory
    7158              :    diagnostics for failed overload resolution.  Their messages should be
    7159              :    indented by two spaces for consistency with the messages in
    7160              :    call.cc  */
    7161              : 
    7162              : static int
    7163            0 : unify_success (bool /*explain_p*/)
    7164              : {
    7165            0 :   return 0;
    7166              : }
    7167              : 
    7168              : /* Other failure functions should call this one, to provide a single function
    7169              :    for setting a breakpoint on.  */
    7170              : 
    7171              : static int
    7172            0 : unify_invalid (bool /*explain_p*/)
    7173              : {
    7174            0 :   return 1;
    7175              : }
    7176              : 
    7177              : static int
    7178       162867 : unify_parameter_deduction_failure (bool explain_p, tree parm)
    7179              : {
    7180            0 :   if (explain_p)
    7181          268 :     inform (input_location,
    7182              :             "  couldn%'t deduce template parameter %qD", parm);
    7183            0 :   return unify_invalid (explain_p);
    7184              : }
    7185              : 
    7186              : static int
    7187      3671308 : unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
    7188              : {
    7189            0 :   if (explain_p)
    7190           56 :     inform (input_location,
    7191              :             "  types %qT and %qT have incompatible cv-qualifiers",
    7192              :             parm, arg);
    7193            0 :   return unify_invalid (explain_p);
    7194              : }
    7195              : 
    7196              : static int
    7197    214056145 : unify_type_mismatch (bool explain_p, tree parm, tree arg)
    7198              : {
    7199         6181 :   if (explain_p)
    7200          512 :     inform (input_location, "  mismatched types %qT and %qT", parm, arg);
    7201         6181 :   return unify_invalid (explain_p);
    7202              : }
    7203              : 
    7204              : static int
    7205       431285 : unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
    7206              : {
    7207            0 :   if (explain_p)
    7208            0 :     inform (input_location,
    7209              :             "  template parameter %qD is not a parameter pack, but "
    7210              :             "argument %qD is",
    7211              :             parm, arg);
    7212            0 :   return unify_invalid (explain_p);
    7213              : }
    7214              : 
    7215              : static int
    7216            3 : unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
    7217              : {
    7218            0 :   if (explain_p)
    7219            0 :     inform (input_location,
    7220              :             "  template argument %qE does not match "
    7221              :             "pointer-to-member constant %qE",
    7222              :             arg, parm);
    7223            0 :   return unify_invalid (explain_p);
    7224              : }
    7225              : 
    7226              : static int
    7227           14 : unify_expression_unequal (bool explain_p, tree parm, tree arg)
    7228              : {
    7229            3 :   if (explain_p)
    7230            0 :     inform (input_location, "  %qE is not equivalent to %qE", parm, arg);
    7231            3 :   return unify_invalid (explain_p);
    7232              : }
    7233              : 
    7234              : static int
    7235           18 : unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
    7236              : {
    7237            0 :   if (explain_p)
    7238            3 :     inform (input_location,
    7239              :             "  inconsistent parameter pack deduction with %qT and %qT",
    7240              :             old_arg, new_arg);
    7241           18 :   return unify_invalid (explain_p);
    7242              : }
    7243              : 
    7244              : static int
    7245       958811 : unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
    7246              : {
    7247       958811 :   if (explain_p)
    7248              :     {
    7249           60 :       if (TYPE_P (parm))
    7250           54 :         inform (input_location,
    7251              :                 "  deduced conflicting types for parameter %qT (%qT and %qT)",
    7252              :                 parm, first, second);
    7253              :       else
    7254            6 :         inform (input_location,
    7255              :                 "  deduced conflicting values for non-type parameter "
    7256              :                 "%qE (%qE and %qE)", parm, first, second);
    7257              :     }
    7258       958811 :   return unify_invalid (explain_p);
    7259              : }
    7260              : 
    7261              : static int
    7262           20 : unify_vla_arg (bool explain_p, tree arg)
    7263              : {
    7264            0 :   if (explain_p)
    7265            9 :     inform (input_location,
    7266              :             "  variable-sized array type %qT is not "
    7267              :             "a valid template argument",
    7268              :             arg);
    7269            0 :   return unify_invalid (explain_p);
    7270              : }
    7271              : 
    7272              : static int
    7273            6 : unify_method_type_error (bool explain_p, tree arg)
    7274              : {
    7275            0 :   if (explain_p)
    7276            0 :     inform (input_location,
    7277              :             "  member function type %qT is not a valid template argument",
    7278              :             arg);
    7279            0 :   return unify_invalid (explain_p);
    7280              : }
    7281              : 
    7282              : static int
    7283      3839622 : unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
    7284              : {
    7285      3839622 :   if (explain_p)
    7286              :     {
    7287            3 :       if (least_p)
    7288            0 :         inform_n (input_location, wanted,
    7289              :                   "  candidate expects at least %d argument, %d provided",
    7290              :                   "  candidate expects at least %d arguments, %d provided",
    7291              :                   wanted, have);
    7292              :       else
    7293            3 :         inform_n (input_location, wanted,
    7294              :                   "  candidate expects %d argument, %d provided",
    7295              :                   "  candidate expects %d arguments, %d provided",
    7296              :                   wanted, have);
    7297              :     }
    7298      3839622 :   return unify_invalid (explain_p);
    7299              : }
    7300              : 
    7301              : static int
    7302        48214 : unify_too_many_arguments (bool explain_p, int have, int wanted)
    7303              : {
    7304            0 :   return unify_arity (explain_p, have, wanted);
    7305              : }
    7306              : 
    7307              : static int
    7308        24239 : unify_too_few_arguments (bool explain_p, int have, int wanted,
    7309              :                          bool least_p = false)
    7310              : {
    7311            0 :   return unify_arity (explain_p, have, wanted, least_p);
    7312              : }
    7313              : 
    7314              : static int
    7315      2580481 : unify_arg_conversion (bool explain_p, tree to_type,
    7316              :                       tree from_type, tree arg)
    7317              : {
    7318      2580481 :   if (explain_p)
    7319          239 :     inform (cp_expr_loc_or_input_loc (arg),
    7320              :             "  cannot convert %qE (type %qT) to type %qT",
    7321              :             arg, from_type, to_type);
    7322      2580481 :   return unify_invalid (explain_p);
    7323              : }
    7324              : 
    7325              : static int
    7326    179782137 : unify_no_common_base (bool explain_p, enum template_base_result r,
    7327              :                       tree parm, tree arg)
    7328              : {
    7329    179782137 :   if (explain_p)
    7330          852 :     switch (r)
    7331              :       {
    7332            3 :       case tbr_ambiguous_baseclass:
    7333            3 :         inform (input_location, "  %qT is an ambiguous base class of %qT",
    7334              :                 parm, arg);
    7335            3 :         break;
    7336          849 :       default:
    7337          849 :         inform (input_location, "  %qT is not derived from %qT", arg, parm);
    7338          849 :         break;
    7339              :       }
    7340    179782137 :   return unify_invalid (explain_p);
    7341              : }
    7342              : 
    7343              : static int
    7344           24 : unify_inconsistent_template_template_parameters (bool explain_p)
    7345              : {
    7346           24 :   if (explain_p)
    7347           10 :     inform (input_location,
    7348              :             "  template parameters of a template template argument are "
    7349              :             "inconsistent with other deduced template arguments");
    7350           24 :   return unify_invalid (explain_p);
    7351              : }
    7352              : 
    7353              : static int
    7354          958 : unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
    7355              : {
    7356            0 :   if (explain_p)
    7357            0 :     inform (input_location,
    7358              :             "  cannot deduce a template for %qT from non-template type %qT",
    7359              :             parm, arg);
    7360            0 :   return unify_invalid (explain_p);
    7361              : }
    7362              : 
    7363              : static int
    7364     11290580 : unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
    7365              : {
    7366            0 :   if (explain_p)
    7367            9 :     inform (input_location,
    7368              :             "  template argument %qE does not match %qE", arg, parm);
    7369            0 :   return unify_invalid (explain_p);
    7370              : }
    7371              : 
    7372              : /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
    7373              :    argument for TYPE, points to an unsuitable object.
    7374              : 
    7375              :    Also adjust the type of the index in C++20 array subobject references.  */
    7376              : 
    7377              : static bool
    7378        85814 : invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
    7379              : {
    7380        85847 :   switch (TREE_CODE (expr))
    7381              :     {
    7382           33 :     CASE_CONVERT:
    7383           33 :       return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
    7384           33 :                                        complain);
    7385              : 
    7386            0 :     case TARGET_EXPR:
    7387            0 :       return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
    7388            0 :                                        complain);
    7389              : 
    7390        42088 :     case CONSTRUCTOR:
    7391        42088 :       {
    7392       173960 :         for (auto &e: CONSTRUCTOR_ELTS (expr))
    7393        55177 :           if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
    7394              :             return true;
    7395              :       }
    7396              :       break;
    7397              : 
    7398         1832 :     case ADDR_EXPR:
    7399         1832 :       {
    7400         1832 :         tree decl = TREE_OPERAND (expr, 0);
    7401              : 
    7402         1832 :         if (cxx_dialect >= cxx20)
    7403         1740 :           while (TREE_CODE (decl) == COMPONENT_REF
    7404         1740 :                  || TREE_CODE (decl) == ARRAY_REF)
    7405              :             {
    7406          178 :               tree &op = TREE_OPERAND (decl, 1);
    7407          178 :               if (TREE_CODE (decl) == ARRAY_REF
    7408           85 :                   && TREE_CODE (op) == INTEGER_CST)
    7409              :                 /* Canonicalize array offsets to ptrdiff_t; how they were
    7410              :                    written doesn't matter for subobject identity.  */
    7411           85 :                 op = fold_convert (ptrdiff_type_node, op);
    7412          178 :               decl = TREE_OPERAND (decl, 0);
    7413              :             }
    7414              : 
    7415         1832 :         if (!VAR_OR_FUNCTION_DECL_P (decl))
    7416              :           {
    7417           47 :             if (complain & tf_error)
    7418           33 :               error_at (cp_expr_loc_or_input_loc (expr),
    7419              :                         "%qE is not a valid template argument of type %qT "
    7420              :                         "because %qE is not a variable or function",
    7421              :                         expr, type, decl);
    7422           47 :             return true;
    7423              :           }
    7424         1785 :         else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
    7425              :           {
    7426            2 :             if (complain & tf_error)
    7427            3 :               error_at (cp_expr_loc_or_input_loc (expr),
    7428              :                         "%qE is not a valid template argument of type %qT "
    7429              :                         "in C++98 because %qD does not have external linkage",
    7430              :                         expr, type, decl);
    7431            2 :             return true;
    7432              :           }
    7433         1783 :         else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
    7434         1783 :                  && decl_linkage (decl) == lk_none)
    7435              :           {
    7436            7 :             if (complain & tf_error)
    7437           14 :               error_at (cp_expr_loc_or_input_loc (expr),
    7438              :                         "%qE is not a valid template argument of type %qT "
    7439              :                         "because %qD has no linkage", expr, type, decl);
    7440            7 :             return true;
    7441              :           }
    7442              :         /* For a constant template parameter of reference or pointer type,
    7443              :            or for each non-static data member of reference or pointer type
    7444              :            in a constant template parameter of class type or subobject thereof,
    7445              :            the reference or pointer value shall not refer or point to
    7446              :            (respectively):
    7447              :            -- a temporary object,
    7448              :            -- a string literal object,
    7449              :            -- the result of a typeid expression,
    7450              :            -- a predefined __func__ variable, or
    7451              :            -- a subobject of one of the above.  */
    7452         1621 :         else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)
    7453         1852 :                  && !DECL_NTTP_OBJECT_P (decl))
    7454              :           {
    7455           12 :             gcc_checking_assert (DECL_TINFO_P (decl)
    7456              :                                  || DECL_FNAME_P (decl)
    7457              :                                  || DECL_IGNORED_P (decl));
    7458           12 :             if (complain & tf_error)
    7459            9 :               error ("the address of %qD is not a valid template argument",
    7460              :                      decl);
    7461           12 :             return true;
    7462              :           }
    7463         1764 :         else if (cxx_dialect < cxx20
    7464         2018 :                  && !(same_type_ignoring_top_level_qualifiers_p
    7465          254 :                       (strip_array_types (TREE_TYPE (type)),
    7466          254 :                        strip_array_types (TREE_TYPE (decl)))))
    7467              :           {
    7468            6 :             if (complain & tf_error)
    7469            4 :               error ("the address of the %qT subobject of %qD is not a "
    7470            4 :                      "valid template argument", TREE_TYPE (type), decl);
    7471            6 :             return true;
    7472              :           }
    7473         1758 :         else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
    7474              :           {
    7475            1 :             if (complain & tf_error)
    7476            1 :               error ("the address of %qD is not a valid template argument "
    7477              :                      "because it does not have static storage duration",
    7478              :                      decl);
    7479            1 :             return true;
    7480              :           }
    7481              :       }
    7482              :       break;
    7483              : 
    7484        41894 :     default:
    7485        41894 :       if (!INDIRECT_TYPE_P (type))
    7486              :         /* We're only concerned about pointers and references here.  */;
    7487          215 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7488              :         /* Null pointer values are OK in C++11.  */;
    7489              :       else
    7490              :         {
    7491           17 :           tree c;
    7492           17 :           if (!(complain & tf_error))
    7493              :             ;
    7494           11 :           else if (cxx_dialect >= cxx17
    7495           11 :                    && (c = cxx_constant_value (expr),
    7496            7 :                        c == error_mark_node))
    7497              :             ;
    7498            4 :           else if (VAR_P (expr))
    7499            2 :             error ("%qD is not a valid template argument "
    7500              :                    "because %qD is a variable, not the address of "
    7501              :                    "a variable", expr, expr);
    7502              :           else
    7503            2 :             error ("%qE is not a valid template argument for %qT "
    7504              :                    "because it is not the address of a variable",
    7505              :                    expr, type);
    7506           17 :           return true;
    7507              :         }
    7508              :     }
    7509              :   return false;
    7510              : 
    7511              : }
    7512              : 
    7513              : /* Return a VAR_DECL for the C++20 template parameter object corresponding to
    7514              :    template argument EXPR.  */
    7515              : 
    7516              : static tree
    7517        29025 : create_template_parm_object (tree expr, tsubst_flags_t complain)
    7518              : {
    7519        29025 :   tree orig = expr;
    7520        29025 :   if (TREE_CODE (expr) == TARGET_EXPR)
    7521        29025 :     expr = TARGET_EXPR_INITIAL (expr);
    7522              : 
    7523        29025 :   if (!TREE_CONSTANT (expr))
    7524              :     {
    7525           50 :       if ((complain & tf_error)
    7526           50 :           && require_rvalue_constant_expression (orig))
    7527           14 :         cxx_constant_value (orig);
    7528           50 :       return error_mark_node;
    7529              :     }
    7530        28975 :   if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
    7531            3 :     return error_mark_node;
    7532              : 
    7533              :   /* This is no longer a compound literal.  */
    7534        28972 :   gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
    7535              : 
    7536        28972 :   return get_template_parm_object (expr, mangle_template_parm_object (expr));
    7537              : }
    7538              : 
    7539              : /* The template arguments corresponding to template parameter objects of types
    7540              :    that contain pointers to members.  */
    7541              : 
    7542              : static GTY(()) hash_map<tree, tree> *tparm_obj_values;
    7543              : 
    7544              : /* Find or build an nttp object for (already-validated) EXPR with name
    7545              :    NAME.  When CHECK_INIT is false we don't need to process the initialiser,
    7546              :    it's already been done.  */
    7547              : 
    7548              : tree
    7549        29449 : get_template_parm_object (tree expr, tree name, bool check_init/*=true*/)
    7550              : {
    7551        29449 :   tree decl = get_global_binding (name);
    7552        29449 :   if (decl)
    7553              :     return decl;
    7554              : 
    7555         2659 :   tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
    7556         2659 :   decl = create_temporary_var (type);
    7557         2659 :   DECL_NTTP_OBJECT_P (decl) = true;
    7558         2659 :   DECL_CONTEXT (decl) = NULL_TREE;
    7559         2659 :   TREE_STATIC (decl) = true;
    7560         2659 :   DECL_DECLARED_CONSTEXPR_P (decl) = true;
    7561         2659 :   TREE_READONLY (decl) = true;
    7562         2659 :   DECL_NAME (decl) = name;
    7563         2659 :   SET_DECL_ASSEMBLER_NAME (decl, name);
    7564         2659 :   comdat_linkage (decl);
    7565         2659 :   if (check_init)
    7566         2650 :     expr = unshare_expr_without_location (expr);
    7567              : 
    7568         2659 :   if (!zero_init_p (type))
    7569              :     {
    7570              :       /* If EXPR contains any PTRMEM_CST, they will get clobbered by
    7571              :          lower_var_init before we're done mangling.  So store the original
    7572              :          value elsewhere.  We only need to unshare EXPR if it's not yet
    7573              :          been processed.  */
    7574           42 :       tree copy = check_init ? unshare_constructor (expr) : expr;
    7575           42 :       hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
    7576              :     }
    7577              : 
    7578         2659 :   if (!check_init)
    7579              :     {
    7580              :       /* The EXPR is the already processed initializer, set it on the NTTP
    7581              :          object now so that cp_finish_decl doesn't do it again later.  */
    7582            9 :       gcc_checking_assert (reduced_constant_expression_p (expr));
    7583            9 :       DECL_INITIAL (decl) = expr;
    7584            9 :       DECL_INITIALIZED_P (decl) = true;
    7585            9 :       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
    7586              :       /* FIXME setting TREE_CONSTANT on refs breaks the back end.  */
    7587            9 :       if (!TYPE_REF_P (type))
    7588            9 :         TREE_CONSTANT (decl) = true;
    7589            9 :       pushdecl_top_level (decl);
    7590              :       /* The caller must call cp_finish_decl to complete its processing.  */
    7591            9 :       return decl;
    7592              :     }
    7593              : 
    7594         2650 :   pushdecl_top_level_and_finish (decl, expr);
    7595              : 
    7596         2650 :   return decl;
    7597              : }
    7598              : 
    7599              : /* Return the actual template argument corresponding to template parameter
    7600              :    object VAR.  */
    7601              : 
    7602              : tree
    7603        59579 : tparm_object_argument (tree var)
    7604              : {
    7605        59579 :   if (zero_init_p (TREE_TYPE (var)))
    7606        59455 :     return DECL_INITIAL (var);
    7607          124 :   return *(tparm_obj_values->get (var));
    7608              : }
    7609              : 
    7610              : /* Attempt to convert the non-type template parameter EXPR to the
    7611              :    indicated TYPE.  If the conversion is successful, return the
    7612              :    converted value.  If the conversion is unsuccessful, return
    7613              :    NULL_TREE if we issued an error message, or error_mark_node if we
    7614              :    did not.  If tf_error is not set in COMPLAIN, whether NULL_TREE
    7615              :    or error_mark_node is returned doesn't matter.  We issue error
    7616              :    messages for out-and-out bad template parameters, but not simply
    7617              :    because the conversion failed, since we might be just trying to
    7618              :    do argument deduction.  Both TYPE and EXPR must be non-dependent.
    7619              : 
    7620              :    The conversion follows the special rules described in
    7621              :    [temp.arg.nontype], and it is much more strict than an implicit
    7622              :    conversion.
    7623              : 
    7624              :    This function is called twice for each template argument (see
    7625              :    lookup_template_class for a more accurate description of this
    7626              :    problem). This means that we need to handle expressions which
    7627              :    are not valid in a C++ source, but can be created from the
    7628              :    first call (for instance, casts to perform conversions). These
    7629              :    hacks can go away after we fix the double coercion problem.  */
    7630              : 
    7631              : static tree
    7632    169077406 : convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
    7633              : {
    7634    169077406 :   tree expr_type;
    7635    169077406 :   location_t loc = cp_expr_loc_or_input_loc (expr);
    7636              : 
    7637              :   /* Detect immediately string literals as invalid non-type argument.
    7638              :      This special-case is not needed for correctness (we would easily
    7639              :      catch this later), but only to provide better diagnostic for this
    7640              :      common user mistake. As suggested by DR 100, we do not mention
    7641              :      linkage issues in the diagnostic as this is not the point.  */
    7642    169077406 :   if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
    7643              :     {
    7644            6 :       if (complain & tf_error)
    7645            6 :         error ("%qE is not a valid template argument for type %qT "
    7646              :                "because string literals can never be used in this context",
    7647              :                expr, type);
    7648            6 :       return NULL_TREE;
    7649              :     }
    7650              : 
    7651              :   /* Add the ADDR_EXPR now for the benefit of
    7652              :      value_dependent_expression_p.  */
    7653         2228 :   if (TYPE_PTROBV_P (type)
    7654    169078801 :       && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
    7655              :     {
    7656          107 :       expr = decay_conversion (expr, complain);
    7657          107 :       if (expr == error_mark_node)
    7658              :         return error_mark_node;
    7659              :     }
    7660              : 
    7661              :   /* If we are in a template, EXPR may be non-dependent, but still
    7662              :      have a syntactic, rather than semantic, form.  For example, EXPR
    7663              :      might be a SCOPE_REF, rather than the VAR_DECL to which the
    7664              :      SCOPE_REF refers.  Preserving the qualifying scope is necessary
    7665              :      so that access checking can be performed when the template is
    7666              :      instantiated -- but here we need the resolved form so that we can
    7667              :      convert the argument.  */
    7668    169077400 :   bool non_dep = false;
    7669          652 :   if (TYPE_REF_OBJ_P (type)
    7670    169077930 :       && has_value_dependent_address (expr))
    7671              :     /* If we want the address and it's value-dependent, don't fold.  */;
    7672    169077373 :   else if (processing_template_decl
    7673    169077373 :            && !instantiation_dependent_expression_p (expr))
    7674              :     non_dep = true;
    7675    169077400 :   if (error_operand_p (expr))
    7676          706 :     return error_mark_node;
    7677    169076694 :   expr_type = TREE_TYPE (expr);
    7678              : 
    7679              :   /* If the argument is non-dependent, perform any conversions in
    7680              :      non-dependent context as well.  */
    7681    169076694 :   processing_template_decl_sentinel s (non_dep);
    7682     17398392 :   if (non_dep)
    7683     17398392 :     expr = instantiate_non_dependent_expr_internal (expr, complain);
    7684              : 
    7685    169076694 :   bool val_dep_p = value_dependent_expression_p (expr);
    7686    169076694 :   if (val_dep_p)
    7687     22934358 :     expr = canonicalize_expr_argument (expr, complain);
    7688              :   else
    7689    146142336 :     STRIP_ANY_LOCATION_WRAPPER (expr);
    7690              : 
    7691              :   /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
    7692              :      to a non-type argument of "nullptr".  */
    7693    169076694 :   if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
    7694           99 :     expr = fold_simple (convert (type, expr));
    7695              : 
    7696              :   /* In C++11, integral or enumeration non-type template arguments can be
    7697              :      arbitrary constant expressions.  Pointer and pointer to
    7698              :      member arguments can be general constant expressions that evaluate
    7699              :      to a null value, but otherwise still need to be of a specific form.  */
    7700    169076694 :   if (cxx_dialect >= cxx11)
    7701              :     {
    7702    169041689 :       if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
    7703              :         /* A PTRMEM_CST is already constant, and a valid template
    7704              :            argument for a parameter of pointer to member type, we just want
    7705              :            to leave it in that form rather than lower it to a
    7706              :            CONSTRUCTOR.  */;
    7707    169041059 :       else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7708        33579 :                || cxx_dialect >= cxx17)
    7709              :         {
    7710              :           /* C++17: A template-argument for a non-type template-parameter shall
    7711              :              be a converted constant expression (8.20) of the type of the
    7712              :              template-parameter.  */
    7713    169040794 :           expr = build_converted_constant_expr (type, expr, complain);
    7714    169040794 :           if (expr == error_mark_node)
    7715              :             /* Make sure we return NULL_TREE only if we have really issued
    7716              :                an error, as described above.  */
    7717         1069 :             return (complain & tf_error) ? NULL_TREE : error_mark_node;
    7718    169040195 :           else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
    7719              :             {
    7720      3531775 :               IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
    7721      3531775 :               return expr;
    7722              :             }
    7723    165508420 :           expr = maybe_constant_value (expr, NULL_TREE, mce_true);
    7724    165508420 :           expr = convert_from_reference (expr);
    7725              :           /* EXPR may have become value-dependent.  */
    7726    165508420 :           val_dep_p = value_dependent_expression_p (expr);
    7727              :         }
    7728           74 :       else if (TYPE_PTR_OR_PTRMEM_P (type)
    7729          314 :                || NULLPTR_TYPE_P (type))
    7730              :         {
    7731          222 :           tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
    7732          253 :           if ((TYPE_PTR_P (type) || NULLPTR_TYPE_P (type))
    7733          228 :               ? integer_zerop (folded)
    7734           25 :               : null_member_pointer_value_p (folded))
    7735    165544320 :             expr = folded;
    7736              :         }
    7737              :     }
    7738              : 
    7739    165544320 :   if (TYPE_REF_P (type))
    7740          632 :     expr = mark_lvalue_use (expr);
    7741              :   else
    7742    165543688 :     expr = mark_rvalue_use (expr);
    7743              : 
    7744              :   /* HACK: Due to double coercion, we can get a
    7745              :      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
    7746              :      which is the tree that we built on the first call (see
    7747              :      below when coercing to reference to object or to reference to
    7748              :      function). We just strip everything and get to the arg.
    7749              :      See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
    7750              :      for examples.  */
    7751    165544320 :   if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
    7752              :     {
    7753              :       /* Check this before we strip *& to avoid redundancy.  */
    7754          632 :       if (!mark_single_function (expr, complain))
    7755            0 :         return error_mark_node;
    7756              : 
    7757          632 :       tree probe_type, probe = expr;
    7758          632 :       if (REFERENCE_REF_P (probe))
    7759          557 :         probe = TREE_OPERAND (probe, 0);
    7760          632 :       probe_type = TREE_TYPE (probe);
    7761          632 :       if (TREE_CODE (probe) == NOP_EXPR)
    7762              :         {
    7763              :           /* ??? Maybe we could use convert_from_reference here, but we
    7764              :              would need to relax its constraints because the NOP_EXPR
    7765              :              could actually change the type to something more cv-qualified,
    7766              :              and this is not folded by convert_from_reference.  */
    7767          517 :           tree addr = TREE_OPERAND (probe, 0);
    7768          517 :           if (TYPE_REF_P (probe_type)
    7769          517 :               && TREE_CODE (addr) == ADDR_EXPR
    7770          515 :               && TYPE_PTR_P (TREE_TYPE (addr))
    7771         1032 :               && (same_type_ignoring_top_level_qualifiers_p
    7772          515 :                   (TREE_TYPE (probe_type),
    7773          515 :                    TREE_TYPE (TREE_TYPE (addr)))))
    7774              :             {
    7775          505 :               expr = TREE_OPERAND (addr, 0);
    7776          505 :               expr_type = TREE_TYPE (probe_type);
    7777              :             }
    7778              :         }
    7779              :     }
    7780              : 
    7781              :   /* [temp.arg.nontype]/5, bullet 1
    7782              : 
    7783              :      For a non-type template-parameter of integral or enumeration type,
    7784              :      integral promotions (_conv.prom_) and integral conversions
    7785              :      (_conv.integral_) are applied.  */
    7786    165544320 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7787        34054 :       || SCALAR_FLOAT_TYPE_P (type))
    7788              :     {
    7789    165510576 :       if (cxx_dialect < cxx11)
    7790              :         {
    7791        34567 :           tree t = build_converted_constant_expr (type, expr, complain);
    7792        34567 :           t = maybe_constant_value (t);
    7793        34567 :           if (t != error_mark_node)
    7794    165510576 :             expr = t;
    7795              :         }
    7796              : 
    7797    165510576 :       if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
    7798           11 :         return error_mark_node;
    7799              : 
    7800              :       /* Notice that there are constant expressions like '4 % 0' which
    7801              :          do not fold into integer constants.  */
    7802    165510565 :       if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
    7803              :         {
    7804          173 :           if (complain & tf_error)
    7805              :             {
    7806           78 :               int errs = errorcount, warns = warningcount + werrorcount;
    7807           78 :               if (!require_potential_constant_expression (expr))
    7808           19 :                 expr = error_mark_node;
    7809              :               else
    7810           59 :                 expr = cxx_constant_value (expr);
    7811           78 :               if (errorcount > errs || warningcount + werrorcount > warns)
    7812           57 :                 inform (loc, "in template argument for type %qT", type);
    7813           78 :               if (expr == error_mark_node)
    7814              :                 return NULL_TREE;
    7815              :               /* else cxx_constant_value complained but gave us
    7816              :                  a real constant, so go ahead.  */
    7817            3 :               if (!CONSTANT_CLASS_P (expr))
    7818              :                 {
    7819              :                   /* Some assemble time constant expressions like
    7820              :                      (intptr_t)&&lab1 - (intptr_t)&&lab2 or
    7821              :                      4 + (intptr_t)&&var satisfy reduced_constant_expression_p
    7822              :                      as we can emit them into .rodata initializers of
    7823              :                      variables, yet they can't fold into an INTEGER_CST at
    7824              :                      compile time.  Refuse them here.  */
    7825            0 :                   gcc_checking_assert (reduced_constant_expression_p (expr));
    7826            0 :                   error_at (loc, "template argument %qE for type %qT not "
    7827              :                                  "a compile-time constant", expr, type);
    7828            0 :                   return NULL_TREE;
    7829              :                 }
    7830              :             }
    7831              :           else
    7832              :             return NULL_TREE;
    7833              :         }
    7834              : 
    7835              :       /* Avoid typedef problems.  */
    7836    165510395 :       if (TREE_TYPE (expr) != type)
    7837      1164756 :         expr = fold_convert (type, expr);
    7838              :     }
    7839              :   /* [temp.arg.nontype]/5, bullet 2
    7840              : 
    7841              :      For a non-type template-parameter of type pointer to object,
    7842              :      qualification conversions (_conv.qual_) and the array-to-pointer
    7843              :      conversion (_conv.array_) are applied.  */
    7844        33744 :   else if (TYPE_PTROBV_P (type))
    7845              :     {
    7846         1355 :       tree decayed = expr;
    7847              : 
    7848              :       /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
    7849              :          decay_conversion or an explicit cast.  If it's a problematic cast,
    7850              :          we'll complain about it below.  */
    7851         1355 :       if (TREE_CODE (expr) == NOP_EXPR)
    7852              :         {
    7853          200 :           tree probe = expr;
    7854          200 :           STRIP_NOPS (probe);
    7855          200 :           if (TREE_CODE (probe) == ADDR_EXPR
    7856          200 :               && TYPE_PTR_P (TREE_TYPE (probe)))
    7857              :             {
    7858          197 :               expr = probe;
    7859          197 :               expr_type = TREE_TYPE (expr);
    7860              :             }
    7861              :         }
    7862              : 
    7863              :       /* [temp.arg.nontype]/1  (TC1 version, DR 49):
    7864              : 
    7865              :          A template-argument for a non-type, non-template template-parameter
    7866              :          shall be one of: [...]
    7867              : 
    7868              :          -- the name of a non-type template-parameter;
    7869              :          -- the address of an object or function with external linkage, [...]
    7870              :             expressed as "& id-expression" where the & is optional if the name
    7871              :             refers to a function or array, or if the corresponding
    7872              :             template-parameter is a reference.
    7873              : 
    7874              :         Here, we do not care about functions, as they are invalid anyway
    7875              :         for a parameter of type pointer-to-object.  */
    7876              : 
    7877         1355 :       if (val_dep_p)
    7878              :         /* Non-type template parameters are OK.  */
    7879              :         ;
    7880         1294 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7881              :         /* Null pointer values are OK in C++11.  */;
    7882         1208 :       else if (TREE_CODE (expr) != ADDR_EXPR
    7883           42 :                && !INDIRECT_TYPE_P (expr_type))
    7884              :         /* Other values, like integer constants, might be valid
    7885              :            non-type arguments of some other type.  */
    7886           25 :         return error_mark_node;
    7887         1183 :       else if (invalid_tparm_referent_p (type, expr, complain))
    7888              :         return NULL_TREE;
    7889              : 
    7890         1251 :       expr = decayed;
    7891              : 
    7892         1251 :       expr = perform_qualification_conversions (type, expr);
    7893         1251 :       if (expr == error_mark_node)
    7894              :         return error_mark_node;
    7895              :     }
    7896              :   /* [temp.arg.nontype]/5, bullet 3
    7897              : 
    7898              :      For a non-type template-parameter of type reference to object, no
    7899              :      conversions apply. The type referred to by the reference may be more
    7900              :      cv-qualified than the (otherwise identical) type of the
    7901              :      template-argument. The template-parameter is bound directly to the
    7902              :      template-argument, which must be an lvalue.  */
    7903        32389 :   else if (TYPE_REF_OBJ_P (type))
    7904              :     {
    7905          518 :       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
    7906              :                                                       expr_type))
    7907            0 :         return error_mark_node;
    7908              : 
    7909          518 :       if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
    7910              :         {
    7911            2 :           if (complain & tf_error)
    7912            1 :             error ("%qE is not a valid template argument for type %qT "
    7913              :                    "because of conflicts in cv-qualification", expr, type);
    7914            2 :           return NULL_TREE;
    7915              :         }
    7916              : 
    7917          516 :       if (!lvalue_p (expr))
    7918              :         {
    7919            6 :           if (complain & tf_error)
    7920            6 :             error ("%qE is not a valid template argument for type %qT "
    7921              :                    "because it is not an lvalue", expr, type);
    7922            6 :           return NULL_TREE;
    7923              :         }
    7924              : 
    7925              :       /* [temp.arg.nontype]/1
    7926              : 
    7927              :          A template-argument for a non-type, non-template template-parameter
    7928              :          shall be one of: [...]
    7929              : 
    7930              :          -- the address of an object or function with external linkage.  */
    7931          510 :       if (INDIRECT_REF_P (expr)
    7932          510 :           && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
    7933              :         {
    7934           31 :           expr = TREE_OPERAND (expr, 0);
    7935           31 :           if (DECL_P (expr))
    7936              :             {
    7937            5 :               if (complain & tf_error)
    7938            3 :                 error ("%q#D is not a valid template argument for type %qT "
    7939              :                        "because a reference variable does not have a constant "
    7940              :                        "address", expr, type);
    7941            5 :               return NULL_TREE;
    7942              :             }
    7943              :         }
    7944              : 
    7945          505 :       if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
    7946              :         /* OK, dependent reference.  We don't want to ask whether a DECL is
    7947              :            itself value-dependent, since what we want here is its address.  */;
    7948              :       else
    7949              :         {
    7950          479 :           expr = build_address (expr);
    7951              : 
    7952          479 :           if (invalid_tparm_referent_p (type, expr, complain))
    7953              :             return NULL_TREE;
    7954              :         }
    7955              : 
    7956          495 :       if (!same_type_p (type, TREE_TYPE (expr)))
    7957          469 :         expr = build_nop (type, expr);
    7958              :     }
    7959              :   /* [temp.arg.nontype]/5, bullet 4
    7960              : 
    7961              :      For a non-type template-parameter of type pointer to function, only
    7962              :      the function-to-pointer conversion (_conv.func_) is applied. If the
    7963              :      template-argument represents a set of overloaded functions (or a
    7964              :      pointer to such), the matching function is selected from the set
    7965              :      (_over.over_).  */
    7966        31871 :   else if (TYPE_PTRFN_P (type))
    7967              :     {
    7968              :       /* If the argument is a template-id, we might not have enough
    7969              :          context information to decay the pointer.  */
    7970          809 :       if (!type_unknown_p (expr_type))
    7971              :         {
    7972          550 :           expr = decay_conversion (expr, complain);
    7973          550 :           if (expr == error_mark_node)
    7974              :             return error_mark_node;
    7975              :         }
    7976              : 
    7977          805 :       if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7978              :         /* Null pointer values are OK in C++11.  */
    7979           30 :         return perform_qualification_conversions (type, expr);
    7980              : 
    7981          775 :       expr = convert_nontype_argument_function (type, expr, complain);
    7982          775 :       if (!expr || expr == error_mark_node)
    7983              :         return expr;
    7984              :     }
    7985              :   /* [temp.arg.nontype]/5, bullet 5
    7986              : 
    7987              :      For a non-type template-parameter of type reference to function, no
    7988              :      conversions apply. If the template-argument represents a set of
    7989              :      overloaded functions, the matching function is selected from the set
    7990              :      (_over.over_).  */
    7991        31062 :   else if (TYPE_REFFN_P (type))
    7992              :     {
    7993          114 :       if (TREE_CODE (expr) == ADDR_EXPR)
    7994              :         {
    7995            0 :           if (complain & tf_error)
    7996              :             {
    7997            0 :               auto_diagnostic_group d;
    7998            0 :               error ("%qE is not a valid template argument for type %qT "
    7999              :                      "because it is a pointer", expr, type);
    8000            0 :               inform (input_location, "try using %qE instead",
    8001            0 :                       TREE_OPERAND (expr, 0));
    8002            0 :             }
    8003            0 :           return NULL_TREE;
    8004              :         }
    8005              : 
    8006          114 :       expr = convert_nontype_argument_function (type, expr, complain);
    8007          114 :       if (!expr || expr == error_mark_node)
    8008              :         return expr;
    8009              :     }
    8010              :   /* [temp.arg.nontype]/5, bullet 6
    8011              : 
    8012              :      For a non-type template-parameter of type pointer to member function,
    8013              :      no conversions apply. If the template-argument represents a set of
    8014              :      overloaded member functions, the matching member function is selected
    8015              :      from the set (_over.over_).  */
    8016        30948 :   else if (TYPE_PTRMEMFUNC_P (type))
    8017              :     {
    8018          627 :       expr = instantiate_type (type, expr, tf_none);
    8019          627 :       if (expr == error_mark_node)
    8020              :         return error_mark_node;
    8021              : 
    8022              :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    8023              :          expression must be a pointer-to-member constant.  */
    8024          598 :       if (!val_dep_p
    8025          598 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    8026              :         return NULL_TREE;
    8027              : 
    8028              :       /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
    8029              :          into a CONSTRUCTOR, so build up a new PTRMEM_CST instead.  */
    8030          581 :       if (fnptr_conv_p (type, TREE_TYPE (expr)))
    8031            3 :         expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
    8032              :     }
    8033              :   /* [temp.arg.nontype]/5, bullet 7
    8034              : 
    8035              :      For a non-type template-parameter of type pointer to data member,
    8036              :      qualification conversions (_conv.qual_) are applied.  */
    8037        30321 :   else if (TYPE_PTRDATAMEM_P (type))
    8038              :     {
    8039              :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    8040              :          expression must be a pointer-to-member constant.  */
    8041          437 :       if (!val_dep_p
    8042          437 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    8043              :         return NULL_TREE;
    8044              : 
    8045          417 :       expr = perform_qualification_conversions (type, expr);
    8046          417 :       if (expr == error_mark_node)
    8047              :         return expr;
    8048              :     }
    8049        29884 :   else if (NULLPTR_TYPE_P (type))
    8050              :     {
    8051           27 :       if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
    8052              :         {
    8053            0 :           if (complain & tf_error)
    8054            0 :             error ("%qE is not a valid template argument for type %qT "
    8055            0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    8056            0 :           return NULL_TREE;
    8057              :         }
    8058           27 :       if (!integer_zerop (expr) && !val_dep_p)
    8059              :         {
    8060            2 :           if (complain & tf_error)
    8061              :             {
    8062            1 :               expr = cxx_constant_value (expr);
    8063            1 :               if (expr == error_mark_node)
    8064              :                 return NULL_TREE;
    8065            0 :               gcc_assert (integer_zerop (expr));
    8066              :             }
    8067              :           else
    8068              :             return NULL_TREE;
    8069              :         }
    8070           25 :       return expr;
    8071              :     }
    8072        29857 :   else if (CLASS_TYPE_P (type))
    8073              :     {
    8074              :       /* Replace the argument with a reference to the corresponding template
    8075              :          parameter object.  */
    8076        29177 :       if (!val_dep_p)
    8077        29025 :         expr = create_template_parm_object (expr, complain);
    8078        29177 :       if (expr == error_mark_node)
    8079              :         return NULL_TREE;
    8080              :     }
    8081          680 :   else if (REFLECTION_TYPE_P (type))
    8082              :     {
    8083          680 :       if (!REFLECTION_TYPE_P (TREE_TYPE (expr)))
    8084              :         {
    8085            0 :           if (complain & tf_error)
    8086            0 :             error ("%qE is not a valid template argument for type %qT "
    8087            0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    8088            0 :           return NULL_TREE;
    8089              :         }
    8090          680 :       if (!REFLECT_EXPR_P (expr) && !val_dep_p)
    8091              :         {
    8092            4 :           if (complain & tf_error)
    8093              :             {
    8094            2 :               expr = cxx_constant_value (expr);
    8095            2 :               if (expr == error_mark_node)
    8096              :                 return NULL_TREE;
    8097            0 :               gcc_assert (REFLECT_EXPR_P (expr));
    8098              :             }
    8099              :           else
    8100              :             return NULL_TREE;
    8101              :         }
    8102          676 :       return expr;
    8103              :     }
    8104              :   /* A template non-type parameter must be one of the above.  */
    8105              :   else
    8106            0 :     gcc_unreachable ();
    8107              : 
    8108              :   /* Sanity check: did we actually convert the argument to the
    8109              :      right type?  */
    8110    165543100 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p
    8111              :               (type, TREE_TYPE (expr)));
    8112    165543100 :   return convert_from_reference (expr);
    8113    169076694 : }
    8114              : 
    8115              : /* Subroutine of coerce_template_template_parms, which returns true if
    8116              :    PARM and ARG match using the rule for the template parameters of
    8117              :    template template parameters.  Both PARM and ARG are template parameters;
    8118              :    the rest of the arguments are the same as for
    8119              :    coerce_template_template_parms.  */
    8120              : 
    8121              : static bool
    8122      1400143 : coerce_template_template_parm (tree parm, tree arg, tsubst_flags_t complain,
    8123              :                                tree in_decl, tree outer_args)
    8124              : {
    8125      1400143 :   if (arg == NULL_TREE || error_operand_p (arg)
    8126      2800280 :       || parm == NULL_TREE || error_operand_p (parm))
    8127              :     return false;
    8128              : 
    8129      1400137 :   if (TREE_CODE (arg) != TREE_CODE (parm))
    8130              :     return false;
    8131              : 
    8132      1399790 :   switch (TREE_CODE (parm))
    8133              :     {
    8134           13 :     case TEMPLATE_DECL:
    8135              :       /* We encounter instantiations of templates like
    8136              :          template <template <template <class> class> class TT>
    8137              :          class C;  */
    8138           13 :       {
    8139           13 :         if (!coerce_template_template_parms
    8140           13 :             (parm, arg, complain, in_decl, outer_args))
    8141              :           return false;
    8142              :       }
    8143              :       /* Fall through.  */
    8144              : 
    8145      1395276 :     case TYPE_DECL:
    8146      1395276 :       if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
    8147      1395276 :           && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8148              :         /* Argument is a parameter pack but parameter is not.  */
    8149              :         return false;
    8150              :       break;
    8151              : 
    8152         4514 :     case PARM_DECL:
    8153              :       /* The tsubst call is used to handle cases such as
    8154              : 
    8155              :            template <int> class C {};
    8156              :            template <class T, template <T> class TT> class D {};
    8157              :            D<int, C> d;
    8158              : 
    8159              :          i.e. the parameter list of TT depends on earlier parameters.  */
    8160         4514 :       if (!uses_template_parms (TREE_TYPE (arg)))
    8161              :         {
    8162         4485 :           ++processing_template_decl;
    8163         4485 :           tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
    8164         4485 :           --processing_template_decl;
    8165         4485 :           if (!uses_template_parms (t)
    8166         4485 :               && !same_type_p (t, TREE_TYPE (arg)))
    8167              :             return false;
    8168              :         }
    8169              : 
    8170         4490 :       if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
    8171         4490 :           && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8172              :         /* Argument is a parameter pack but parameter is not.  */
    8173              :         return false;
    8174              : 
    8175              :       break;
    8176              : 
    8177            0 :     default:
    8178            0 :       gcc_unreachable ();
    8179              :     }
    8180              : 
    8181              :   return true;
    8182              : }
    8183              : 
    8184              : /* Coerce template argument list ARGLIST for use with template
    8185              :    template-parameter TEMPL.  */
    8186              : 
    8187              : static tree
    8188       339773 : coerce_template_args_for_ttp (tree templ, tree arglist,
    8189              :                               tsubst_flags_t complain)
    8190              : {
    8191              :   /* Consider an example where a template template parameter declared as
    8192              : 
    8193              :      template <class T, class U = std::allocator<T> > class TT
    8194              : 
    8195              :      The template parameter level of T and U are one level larger than
    8196              :      of TT.  To proper process the default argument of U, say when an
    8197              :      instantiation `TT<int>' is seen, we need to build the full
    8198              :      arguments containing {int} as the innermost level.  Outer levels,
    8199              :      available when not appearing as default template argument, can be
    8200              :      obtained from the arguments of the enclosing template.
    8201              : 
    8202              :      Suppose that TT is later substituted with std::vector.  The above
    8203              :      instantiation is `TT<int, std::allocator<T> >' with TT at
    8204              :      level 1, and T at level 2, while the template arguments at level 1
    8205              :      becomes {std::vector} and the inner level 2 is {int}.  */
    8206              : 
    8207       339773 :   tree outer = DECL_CONTEXT (templ);
    8208       339773 :   if (outer)
    8209       167265 :     outer = generic_targs_for (outer);
    8210       172508 :   else if (current_template_parms)
    8211              :     {
    8212              :       /* This is an argument of the current template, so we haven't set
    8213              :          DECL_CONTEXT yet.  We can also get here when level-lowering a
    8214              :          bound ttp. */
    8215              :       tree relevant_template_parms;
    8216              : 
    8217              :       /* Parameter levels that are greater than the level of the given
    8218              :          template template parm are irrelevant.  */
    8219              :       relevant_template_parms = current_template_parms;
    8220       167153 :       while (TMPL_PARMS_DEPTH (relevant_template_parms)
    8221       334306 :              != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
    8222            3 :         relevant_template_parms = TREE_CHAIN (relevant_template_parms);
    8223              : 
    8224       167150 :       outer = template_parms_to_args (relevant_template_parms);
    8225              :     }
    8226              : 
    8227       339773 :   if (outer)
    8228       334415 :     arglist = add_to_template_args (outer, arglist);
    8229              : 
    8230       339773 :   tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
    8231       339773 :   return coerce_template_parms (parmlist, arglist, templ, complain);
    8232              : }
    8233              : 
    8234              : /* A cache of template template parameters with match-all default
    8235              :    arguments.  */
    8236              : static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
    8237              : 
    8238              : /* T is a bound template template-parameter.  Copy its arguments into default
    8239              :    arguments of the template template-parameter's template parameters.  */
    8240              : 
    8241              : static tree
    8242           48 : add_defaults_to_ttp (tree otmpl)
    8243              : {
    8244           82 :   if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
    8245           18 :     return *c;
    8246              : 
    8247           30 :   tree ntmpl = copy_node (otmpl);
    8248              : 
    8249           30 :   tree ntype = copy_node (TREE_TYPE (otmpl));
    8250           30 :   TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
    8251           30 :   TYPE_MAIN_VARIANT (ntype) = ntype;
    8252           30 :   TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
    8253           30 :   TYPE_NAME (ntype) = ntmpl;
    8254           30 :   SET_TYPE_STRUCTURAL_EQUALITY (ntype);
    8255              : 
    8256           30 :   tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
    8257           30 :     = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
    8258           30 :   TEMPLATE_PARM_DECL (idx) = ntmpl;
    8259           30 :   TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
    8260              : 
    8261           30 :   tree oparms = DECL_TEMPLATE_PARMS (otmpl);
    8262           30 :   tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
    8263           30 :   TREE_CHAIN (parms) = TREE_CHAIN (oparms);
    8264           30 :   tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
    8265           68 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    8266              :     {
    8267           38 :       tree o = TREE_VEC_ELT (vec, i);
    8268           38 :       if (!template_parameter_pack_p (TREE_VALUE (o)))
    8269              :         {
    8270           34 :           tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
    8271           34 :           TREE_PURPOSE (n) = any_targ_node;
    8272              :         }
    8273              :     }
    8274              : 
    8275           30 :   tree oresult = DECL_TEMPLATE_RESULT (otmpl);
    8276           30 :   tree gen_otmpl = DECL_TI_TEMPLATE (oresult);
    8277           30 :   tree gen_ntmpl;
    8278           30 :   if (gen_otmpl == otmpl)
    8279              :     gen_ntmpl = ntmpl;
    8280              :   else
    8281            0 :     gen_ntmpl = add_defaults_to_ttp (gen_otmpl);
    8282              : 
    8283           30 :   tree nresult = copy_decl (oresult);
    8284           30 :   DECL_TEMPLATE_INFO (nresult)
    8285           30 :     = build_template_info (gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult)));
    8286           30 :   DECL_TEMPLATE_RESULT (ntmpl) = nresult;
    8287              : 
    8288           30 :   hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
    8289           30 :   return ntmpl;
    8290              : }
    8291              : 
    8292              : /* ARG is a bound potential template template-argument, and PARGS is a list
    8293              :    of arguments for the corresponding template template-parameter.  Adjust
    8294              :    PARGS as appropriate for application to ARG's template, and if ARG is a
    8295              :    BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
    8296              :    arguments to the template template parameter.  */
    8297              : 
    8298              : static tree
    8299       168662 : coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
    8300              : {
    8301       168662 :   ++processing_template_decl;
    8302       168662 :   tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
    8303       168662 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8304              :     {
    8305              :       /* When comparing two template template-parameters in partial ordering,
    8306              :          rewrite the one currently being used as an argument to have default
    8307              :          arguments for all parameters.  */
    8308           48 :       arg_tmpl = add_defaults_to_ttp (arg_tmpl);
    8309           48 :       pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
    8310           48 :       if (pargs != error_mark_node)
    8311           44 :         arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
    8312           88 :                                            TYPE_TI_ARGS (arg));
    8313              :     }
    8314              :   else
    8315              :     {
    8316       168614 :       tree aparms
    8317       168614 :         = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
    8318       168614 :       pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
    8319              :     }
    8320       168662 :   --processing_template_decl;
    8321       168662 :   return pargs;
    8322              : }
    8323              : 
    8324              : /* Subroutine of unify for the case when PARM is a
    8325              :    BOUND_TEMPLATE_TEMPLATE_PARM.  */
    8326              : 
    8327              : static int
    8328       168860 : unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
    8329              :                       bool explain_p)
    8330              : {
    8331       168860 :   tree parmvec = TYPE_TI_ARGS (parm);
    8332       337720 :   tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
    8333              : 
    8334              :   /* The template template parm might be variadic and the argument
    8335              :      not, so flatten both argument lists.  */
    8336       168860 :   parmvec = expand_template_argument_pack (parmvec);
    8337       168860 :   argvec = expand_template_argument_pack (argvec);
    8338              : 
    8339       168860 :   if (flag_new_ttp)
    8340              :     {
    8341              :       /* In keeping with P0522R0, adjust P's template arguments
    8342              :          to apply to A's template; then flatten it again.  */
    8343       168662 :       tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
    8344       168662 :       nparmvec = expand_template_argument_pack (nparmvec);
    8345              : 
    8346       168662 :       if (unify (tparms, targs, nparmvec, argvec,
    8347              :                  UNIFY_ALLOW_NONE, explain_p))
    8348              :         return 1;
    8349              : 
    8350              :       /* If the P0522 adjustment eliminated a pack expansion, deduce
    8351              :          empty packs.  */
    8352       168387 :       if (flag_new_ttp
    8353       168387 :           && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
    8354       168387 :           && unify_pack_expansion (tparms, targs, parmvec, argvec,
    8355              :                                    DEDUCE_EXACT, /*sub*/true, explain_p))
    8356              :         return 1;
    8357              :     }
    8358              :   else
    8359              :     {
    8360              :       /* Deduce arguments T, i from TT<T> or TT<i>.
    8361              :          We check each element of PARMVEC and ARGVEC individually
    8362              :          rather than the whole TREE_VEC since they can have
    8363              :          different number of elements, which is allowed under N2555.  */
    8364              : 
    8365          198 :       int len = TREE_VEC_LENGTH (parmvec);
    8366              : 
    8367              :       /* Check if the parameters end in a pack, making them
    8368              :          variadic.  */
    8369          198 :       int parm_variadic_p = 0;
    8370          198 :       if (len > 0
    8371          198 :           && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
    8372              :         parm_variadic_p = 1;
    8373              : 
    8374          400 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8375              :         /* If the template argument list of P contains a pack
    8376              :            expansion that is not the last template argument, the
    8377              :            entire template argument list is a non-deduced
    8378              :            context.  */
    8379          204 :         if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
    8380       168566 :           return unify_success (explain_p);
    8381              : 
    8382          196 :       if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
    8383            2 :         return unify_too_few_arguments (explain_p,
    8384            2 :                                         TREE_VEC_LENGTH (argvec), len);
    8385              : 
    8386          377 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8387          198 :         if (unify (tparms, targs,
    8388          198 :                    TREE_VEC_ELT (parmvec, i),
    8389          198 :                    TREE_VEC_ELT (argvec, i),
    8390              :                    UNIFY_ALLOW_NONE, explain_p))
    8391              :           return 1;
    8392              : 
    8393          179 :       if (parm_variadic_p
    8394          179 :           && unify_pack_expansion (tparms, targs,
    8395              :                                    parmvec, argvec,
    8396              :                                    DEDUCE_EXACT,
    8397              :                                    /*subr=*/true, explain_p))
    8398              :         return 1;
    8399              :     }
    8400              : 
    8401              :   return 0;
    8402              : }
    8403              : 
    8404              : /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
    8405              :    template template parameters.
    8406              : 
    8407              :    Consider the example:
    8408              :      template <class T> class A;
    8409              :      template<template <class U> class TT> class B;
    8410              : 
    8411              :    For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
    8412              :    and OUTER_ARGS contains A.  */
    8413              : 
    8414              : static int
    8415      9946865 : coerce_template_template_parms (tree parm_tmpl,
    8416              :                                 tree arg_tmpl,
    8417              :                                 tsubst_flags_t complain,
    8418              :                                 tree in_decl,
    8419              :                                 tree outer_args)
    8420              : {
    8421      9946865 :   int nparms, nargs, i;
    8422      9946865 :   tree parm, arg;
    8423      9946865 :   int variadic_p = 0;
    8424              : 
    8425      9946865 :   tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl);
    8426      9946865 :   tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl);
    8427      9946865 :   tree gen_arg_tmpl = most_general_template (arg_tmpl);
    8428      9946865 :   tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl);
    8429              : 
    8430      9946865 :   nparms = TREE_VEC_LENGTH (parm_parms);
    8431      9946865 :   nargs = TREE_VEC_LENGTH (arg_parms);
    8432              : 
    8433      9946865 :   if (flag_new_ttp)
    8434              :     {
    8435              :       /* P0522R0: A template template-parameter P is at least as specialized as
    8436              :          a template template-argument A if, given the following rewrite to two
    8437              :          function templates, the function template corresponding to P is at
    8438              :          least as specialized as the function template corresponding to A
    8439              :          according to the partial ordering rules for function templates
    8440              :          ([temp.func.order]). Given an invented class template X with the
    8441              :          template parameter list of A (including default arguments):
    8442              : 
    8443              :          * Each of the two function templates has the same template parameters,
    8444              :          respectively, as P or A.
    8445              : 
    8446              :          * Each function template has a single function parameter whose type is
    8447              :          a specialization of X with template arguments corresponding to the
    8448              :          template parameters from the respective function template where, for
    8449              :          each template parameter PP in the template parameter list of the
    8450              :          function template, a corresponding template argument AA is formed. If
    8451              :          PP declares a parameter pack, then AA is the pack expansion
    8452              :          PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
    8453              : 
    8454              :          If the rewrite produces an invalid type, then P is not at least as
    8455              :          specialized as A.  */
    8456              : 
    8457              :       /* So coerce P's args to apply to A's parms, and then deduce between A's
    8458              :          args and the converted args.  If that succeeds, A is at least as
    8459              :          specialized as P, so they match.*/
    8460      9921793 :       processing_template_decl_sentinel ptds (/*reset*/false);
    8461      9921793 :       ++processing_template_decl;
    8462              : 
    8463      9921793 :       tree pargs = template_parms_level_to_args (parm_parms);
    8464              : 
    8465              :       /* PARM and ARG might be at different template depths, and we want to
    8466              :          pass the right additional levels of args when coercing PARGS to
    8467              :          ARG_PARMS in case we need to do any substitution into non-type
    8468              :          template parameter types.
    8469              : 
    8470              :          OUTER_ARGS are not the right outer levels in this case, as they are
    8471              :          the args we're building up for PARM, and for the coercion we want the
    8472              :          args for ARG.  If DECL_CONTEXT isn't set for a template template
    8473              :          parameter, we can assume that it's in the current scope.  */
    8474      9921793 :       tree ctx = DECL_CONTEXT (arg_tmpl);
    8475      9921793 :       if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8476       433290 :         ctx = current_scope ();
    8477      9921793 :       tree scope_args = NULL_TREE;
    8478      9921793 :       if (tree tinfo = get_template_info (ctx))
    8479       691463 :         scope_args = TI_ARGS (tinfo);
    8480      9921793 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8481              :         {
    8482       503449 :           int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl));
    8483       582494 :           int scope_depth = TMPL_ARGS_DEPTH (scope_args);
    8484       503449 :           tree full_pargs = make_tree_vec (level + 1);
    8485              : 
    8486              :         /* Only use as many levels from the scope as needed
    8487              :            (excluding the level of ARG).  */
    8488       523071 :           for (int i = 0; i < level - 1; ++i)
    8489        19622 :             if (i < scope_depth)
    8490        38282 :               TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1);
    8491              :             else
    8492          481 :               TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0);
    8493              : 
    8494              :           /* Add the arguments that appear at the levels of ARG.  */
    8495       503449 :           tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl));
    8496      1006898 :           adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1);
    8497       503449 :           TREE_VEC_ELT (full_pargs, level - 1) = adjacent;
    8498              : 
    8499       503449 :           TREE_VEC_ELT (full_pargs, level) = pargs;
    8500       503449 :           pargs = full_pargs;
    8501              :         }
    8502              :       else
    8503      9418344 :         pargs = add_to_template_args (scope_args, pargs);
    8504              : 
    8505      9921793 :       pargs = coerce_template_parms (gen_arg_parms, pargs,
    8506              :                                      NULL_TREE, tf_none);
    8507      9921793 :       if (pargs != error_mark_node)
    8508              :         {
    8509      9920766 :           tree targs = make_tree_vec (nargs);
    8510      9920766 :           tree aargs = template_parms_level_to_args (arg_parms);
    8511      9920766 :           if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
    8512              :                       /*explain*/false))
    8513      9051996 :             return 1;
    8514              :         }
    8515      9921793 :     }
    8516              : 
    8517              :   /* Determine whether we have a parameter pack at the end of the
    8518              :      template template parameter's template parameter list.  */
    8519       894869 :   if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
    8520              :     {
    8521       894869 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
    8522              : 
    8523       894869 :       if (error_operand_p (parm))
    8524              :         return 0;
    8525              : 
    8526       894863 :       switch (TREE_CODE (parm))
    8527              :         {
    8528       889375 :         case TEMPLATE_DECL:
    8529       889375 :         case TYPE_DECL:
    8530       889375 :           if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8531       876218 :             variadic_p = 1;
    8532              :           break;
    8533              : 
    8534         5488 :         case PARM_DECL:
    8535         5488 :           if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8536       876218 :             variadic_p = 1;
    8537              :           break;
    8538              : 
    8539            0 :         default:
    8540            0 :           gcc_unreachable ();
    8541              :         }
    8542              :     }
    8543              : 
    8544       894863 :   if (nargs != nparms
    8545       439149 :       && !(variadic_p && nargs >= nparms - 1))
    8546              :     return 0;
    8547              : 
    8548              :   /* Check all of the template parameters except the parameter pack at
    8549              :      the end (if any).  */
    8550      1052980 :   for (i = 0; i < nparms - variadic_p; ++i)
    8551              :     {
    8552       159205 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
    8553       159205 :           || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8554            0 :         continue;
    8555              : 
    8556       159205 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8557       159205 :       arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8558              : 
    8559       159205 :       if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8560              :                                           outer_args))
    8561              :         return 0;
    8562              : 
    8563              :     }
    8564              : 
    8565       893775 :   if (variadic_p)
    8566              :     {
    8567              :       /* Check each of the template parameters in the template
    8568              :          argument against the template parameter pack at the end of
    8569              :          the template template parameter.  */
    8570       876214 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
    8571              :         return 0;
    8572              : 
    8573       876214 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8574              : 
    8575      2117128 :       for (; i < nargs; ++i)
    8576              :         {
    8577      1240938 :           if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8578            0 :             continue;
    8579              : 
    8580      1240938 :           arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8581              : 
    8582      1240938 :           if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8583              :                                               outer_args))
    8584              :             return 0;
    8585              :         }
    8586              :     }
    8587              : 
    8588              :   return 1;
    8589              : }
    8590              : 
    8591              : /* Verifies that the deduced template arguments (in TARGS) for the
    8592              :    template template parameters (in TPARMS) represent valid bindings,
    8593              :    by comparing the template parameter list of each template argument
    8594              :    to the template parameter list of its corresponding template
    8595              :    template parameter, in accordance with DR150. This
    8596              :    routine can only be called after all template arguments have been
    8597              :    deduced. It will return TRUE if all of the template template
    8598              :    parameter bindings are okay, FALSE otherwise.  */
    8599              : bool
    8600     77330087 : template_template_parm_bindings_ok_p (tree tparms, tree targs)
    8601              : {
    8602     77330087 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
    8603     77330087 :   bool ret = true;
    8604              : 
    8605              :   /* We're dealing with template parms in this process.  */
    8606     77330087 :   ++processing_template_decl;
    8607              : 
    8608     77330087 :   targs = INNERMOST_TEMPLATE_ARGS (targs);
    8609              : 
    8610    203041542 :   for (i = 0; i < ntparms; ++i)
    8611              :     {
    8612    125711491 :       tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
    8613    125711491 :       tree targ = TREE_VEC_ELT (targs, i);
    8614              : 
    8615    125711491 :       if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
    8616              :         {
    8617       744520 :           tree packed_args = NULL_TREE;
    8618       744520 :           int idx, len = 1;
    8619              : 
    8620       744520 :           if (ARGUMENT_PACK_P (targ))
    8621              :             {
    8622              :               /* Look inside the argument pack.  */
    8623          145 :               packed_args = ARGUMENT_PACK_ARGS (targ);
    8624          145 :               len = TREE_VEC_LENGTH (packed_args);
    8625              :             }
    8626              : 
    8627      1489026 :           for (idx = 0; idx < len; ++idx)
    8628              :             {
    8629       744542 :               if (packed_args)
    8630              :                 /* Extract the next argument from the argument
    8631              :                    pack.  */
    8632          167 :                 targ = TREE_VEC_ELT (packed_args, idx);
    8633              : 
    8634       744542 :               if (PACK_EXPANSION_P (targ))
    8635              :                 /* Look at the pattern of the pack expansion.  */
    8636           40 :                 targ = PACK_EXPANSION_PATTERN (targ);
    8637              : 
    8638              :               /* Extract the template parameters from the template
    8639              :                  argument.  */
    8640       744542 :               if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
    8641       121745 :                 targ = TYPE_NAME (targ);
    8642              : 
    8643              :               /* Verify that we can coerce the template template
    8644              :                  parameters from the template argument to the template
    8645              :                  parameter.  This requires an exact match.  */
    8646       744542 :               if (TREE_CODE (targ) == TEMPLATE_DECL
    8647      1489075 :                   && !coerce_template_template_parms
    8648       744533 :                        (tparm,
    8649              :                         targ,
    8650              :                         tf_none,
    8651              :                         tparm,
    8652              :                         targs))
    8653              :                 {
    8654           36 :                   ret = false;
    8655           36 :                   goto out;
    8656              :                 }
    8657              :             }
    8658              :         }
    8659              :     }
    8660              : 
    8661     77330051 :  out:
    8662              : 
    8663     77330087 :   --processing_template_decl;
    8664     77330087 :   return ret;
    8665              : }
    8666              : 
    8667              : /* Since type attributes aren't mangled, we need to strip them from
    8668              :    template type arguments.  */
    8669              : 
    8670              : tree
    8671   1498199217 : canonicalize_type_argument (tree arg, tsubst_flags_t complain)
    8672              : {
    8673   1498199217 :   if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
    8674              :     return arg;
    8675    659902229 :   bool removed_attributes = false;
    8676    659902229 :   tree canon = strip_typedefs (arg, &removed_attributes);
    8677    659902229 :   if (removed_attributes
    8678          634 :       && (complain & tf_warning))
    8679           15 :     warning (OPT_Wignored_attributes,
    8680              :              "ignoring attributes on template argument %qT", arg);
    8681              :   return canon;
    8682              : }
    8683              : 
    8684              : /* And from inside dependent non-type arguments like sizeof(Type).  */
    8685              : 
    8686              : static tree
    8687     39232742 : canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
    8688              : {
    8689     39232742 :   if (!arg || arg == error_mark_node)
    8690              :     return arg;
    8691     39232742 :   bool removed_attributes = false;
    8692     39232742 :   tree canon = strip_typedefs_expr (arg, &removed_attributes);
    8693     39232742 :   if (removed_attributes
    8694            0 :       && (complain & tf_warning))
    8695            0 :     warning (OPT_Wignored_attributes,
    8696              :              "ignoring attributes in template argument %qE", arg);
    8697              :   return canon;
    8698              : }
    8699              : 
    8700              : /* A template declaration can be substituted for a constrained
    8701              :    template template parameter only when the argument is no more
    8702              :    constrained than the parameter.  */
    8703              : 
    8704              : static bool
    8705      9201237 : is_compatible_template_arg (tree parm, tree arg, tree args)
    8706              : {
    8707      9201237 :   tree parm_cons = get_constraints (parm);
    8708              : 
    8709              :   /* For now, allow constrained template template arguments
    8710              :      and unconstrained template template parameters.  */
    8711      9201237 :   if (parm_cons == NULL_TREE)
    8712              :     return true;
    8713              : 
    8714              :   /* If the template parameter is constrained, we need to rewrite its
    8715              :      constraints in terms of the ARG's template parameters. This ensures
    8716              :      that all of the template parameter types will have the same depth.
    8717              : 
    8718              :      Note that this is only valid when coerce_template_template_parm is
    8719              :      true for the innermost template parameters of PARM and ARG. In other
    8720              :      words, because coercion is successful, this conversion will be valid.  */
    8721           87 :   tree new_args = NULL_TREE;
    8722           87 :   if (parm_cons)
    8723              :     {
    8724           87 :       tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
    8725           87 :       new_args = template_parms_level_to_args (aparms);
    8726           87 :       new_args = add_to_template_args (args, new_args);
    8727           87 :       ++processing_template_decl;
    8728           87 :       parm_cons = tsubst_constraint_info (parm_cons, new_args,
    8729              :                                           tf_none, NULL_TREE);
    8730           87 :       --processing_template_decl;
    8731           87 :       if (parm_cons == error_mark_node)
    8732              :         return false;
    8733              :     }
    8734              : 
    8735           87 :   return ttp_subsumes (parm_cons, arg);
    8736              : }
    8737              : 
    8738              : /* We can't fully resolve ARG given as a non-type template argument to TYPE,
    8739              :    because one of them is dependent.  But we need to represent the
    8740              :    conversion for the benefit of cp_tree_equal.  */
    8741              : 
    8742              : static tree
    8743     16298384 : maybe_convert_nontype_argument (tree type, tree arg, bool force)
    8744              : {
    8745              :   /* Auto parms get no conversion.  */
    8746     16298384 :   if (type_uses_auto (type))
    8747              :     return arg;
    8748              :   /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
    8749              :      That would complicate other things, and it doesn't seem necessary.  */
    8750     16200019 :   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    8751              :     return arg;
    8752              :   /* We don't need or want to add this conversion now if we're going to use the
    8753              :      argument for deduction.  */
    8754     14984764 :   if (!value_dependent_expression_p (arg))
    8755              :     force = false;
    8756     14984587 :   else if (!force)
    8757              :     return arg;
    8758              : 
    8759      1675910 :   type = cv_unqualified (type);
    8760      1675910 :   tree argtype = TREE_TYPE (arg);
    8761      1675910 :   if (argtype && same_type_p (type, argtype))
    8762              :     return arg;
    8763              : 
    8764      1675874 :   arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
    8765      1675874 :   IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
    8766      1675874 :   IMPLICIT_CONV_EXPR_FORCED (arg) = force;
    8767      1675874 :   return arg;
    8768              : }
    8769              : 
    8770              : /* True if we need an IMPLICIT_CONV_EXPR for converting EXPR to TYPE, possibly
    8771              :    in a FORCED context (i.e. alias or concept).  */
    8772              : 
    8773              : static bool
    8774    191219426 : dependent_implicit_conv_p (tree type, tree expr, bool forced)
    8775              : {
    8776    381120681 :   return (dependent_type_p (type) || type_dependent_expression_p (expr)
    8777    361231660 :           || (forced
    8778     61601099 :               && !(same_type_ignoring_top_level_qualifiers_p
    8779     61601099 :                    (TREE_TYPE (expr), type))
    8780       300715 :               && value_dependent_expression_p (expr)));
    8781              : }
    8782              : 
    8783              : /* Convert the indicated template ARG as necessary to match the
    8784              :    indicated template PARM.  Returns the converted ARG, or
    8785              :    error_mark_node if the conversion was unsuccessful.  Error and
    8786              :    warning messages are issued under control of COMPLAIN.  This
    8787              :    conversion is for the Ith parameter in the parameter list.  ARGS is
    8788              :    the full set of template arguments deduced so far.  */
    8789              : 
    8790              : static tree
    8791   1356439208 : convert_template_argument (tree parm,
    8792              :                            tree arg,
    8793              :                            tree args,
    8794              :                            tsubst_flags_t complain,
    8795              :                            int i,
    8796              :                            tree in_decl)
    8797              : {
    8798   1356439208 :   tree orig_arg;
    8799   1356439208 :   tree val;
    8800   1356439208 :   int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
    8801              : 
    8802   1356439208 :   if (parm == error_mark_node || error_operand_p (arg))
    8803              :     return error_mark_node;
    8804              : 
    8805   1339041403 :   if (arg == any_targ_node)
    8806              :     return arg;
    8807              : 
    8808   1339041391 :   if (TREE_CODE (arg) == TREE_LIST
    8809   1339041391 :       && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
    8810              :     {
    8811              :       /* The template argument was the name of some
    8812              :          member function.  That's usually
    8813              :          invalid, but static members are OK.  In any
    8814              :          case, grab the underlying fields/functions
    8815              :          and issue an error later if required.  */
    8816            0 :       TREE_TYPE (arg) = unknown_type_node;
    8817              :     }
    8818              : 
    8819   1339041391 :   orig_arg = arg;
    8820              : 
    8821   1339041391 :   requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
    8822   2678082782 :   requires_type = (TREE_CODE (parm) == TYPE_DECL
    8823   1339041391 :                    || requires_tmpl_type);
    8824              : 
    8825              :   /* When determining whether an argument pack expansion is a template,
    8826              :      look at the pattern.  */
    8827   1339041391 :   if (PACK_EXPANSION_P (arg))
    8828     18790032 :     arg = PACK_EXPANSION_PATTERN (arg);
    8829              : 
    8830              :   /* Deal with an injected-class-name used as a template template arg.  */
    8831   1339041391 :   if (requires_tmpl_type && CLASS_TYPE_P (arg))
    8832              :     {
    8833          815 :       tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
    8834          815 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    8835              :         {
    8836           39 :           if (cxx_dialect >= cxx11)
    8837              :             /* OK under DR 1004.  */;
    8838            2 :           else if (complain & tf_warning_or_error)
    8839            2 :             pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
    8840            2 :                      " used as template template argument", TYPE_NAME (arg));
    8841            0 :           else if (flag_pedantic_errors)
    8842   1339041352 :             t = arg;
    8843              : 
    8844              :           arg = t;
    8845              :         }
    8846              :     }
    8847              : 
    8848   2687338229 :   is_tmpl_type =
    8849   1339041391 :     ((TREE_CODE (arg) == TEMPLATE_DECL
    8850      9014660 :       && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
    8851   1330026731 :      || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
    8852   1330026722 :      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8853   2668880195 :      || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
    8854              : 
    8855      9255447 :   if (is_tmpl_type
    8856      9255447 :       && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8857      9067529 :           || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
    8858       240778 :     arg = TYPE_STUB_DECL (arg);
    8859              : 
    8860   1339041391 :   is_type = TYPE_P (arg) || is_tmpl_type;
    8861              : 
    8862      3099839 :   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
    8863   1339041409 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
    8864              :     {
    8865           12 :       if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
    8866              :         {
    8867            3 :           if (complain & tf_error)
    8868            3 :             error ("invalid use of destructor %qE as a type", orig_arg);
    8869            3 :           return error_mark_node;
    8870              :         }
    8871              : 
    8872            9 :       permerror (input_location,
    8873              :                  "to refer to a type member of a template parameter, "
    8874              :                  "use %<typename %E%>", orig_arg);
    8875              : 
    8876            9 :       orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
    8877            9 :                                      TREE_OPERAND (arg, 1),
    8878              :                                      typename_type,
    8879              :                                      complain);
    8880            9 :       arg = orig_arg;
    8881            9 :       is_type = 1;
    8882              :     }
    8883   1339041388 :   if (is_type != requires_type)
    8884              :     {
    8885      3105586 :       if (in_decl)
    8886              :         {
    8887      3105170 :           if (complain & tf_error)
    8888              :             {
    8889          130 :               auto_diagnostic_group d;
    8890          130 :               error ("type/value mismatch at argument %d in template "
    8891              :                      "parameter list for %qD",
    8892              :                      i + 1, in_decl);
    8893          130 :               if (is_type)
    8894              :                 {
    8895              :                   /* The template argument is a type, but we're expecting
    8896              :                      an expression.  */
    8897           55 :                   inform (input_location,
    8898              :                           "  expected a constant of type %qT, got %qT",
    8899           40 :                           TREE_TYPE (parm),
    8900           40 :                           (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
    8901              :                   /* [temp.arg]/2: "In a template-argument, an ambiguity
    8902              :                      between a type-id and an expression is resolved to a
    8903              :                      type-id, regardless of the form of the corresponding
    8904              :                      template-parameter."  So give the user a clue.  */
    8905           40 :                   if (TREE_CODE (arg) == FUNCTION_TYPE)
    8906            6 :                     inform (input_location, "  ambiguous template argument "
    8907              :                             "for non-type template parameter is treated as "
    8908              :                             "function type");
    8909              :                 }
    8910           90 :               else if (requires_tmpl_type)
    8911            9 :                 inform (input_location,
    8912              :                         "  expected a class template, got %qE", orig_arg);
    8913              :               else
    8914           81 :                 inform (input_location,
    8915              :                         "  expected a type, got %qE", orig_arg);
    8916          130 :             }
    8917              :         }
    8918      3105586 :       return error_mark_node;
    8919              :     }
    8920   1335935802 :   if (is_tmpl_type ^ requires_tmpl_type)
    8921              :     {
    8922         1035 :       if (in_decl && (complain & tf_error))
    8923              :         {
    8924           15 :           auto_diagnostic_group d;
    8925           15 :           error ("type/value mismatch at argument %d in template "
    8926              :                  "parameter list for %qD",
    8927              :                  i + 1, in_decl);
    8928           15 :           if (is_tmpl_type)
    8929            6 :             inform (input_location,
    8930            6 :                     "  expected a type, got %qT", DECL_NAME (arg));
    8931              :           else
    8932            9 :             inform (input_location,
    8933              :                     "  expected a class template, got %qT", orig_arg);
    8934           15 :         }
    8935         1035 :       return error_mark_node;
    8936              :     }
    8937              : 
    8938   1335934767 :   if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
    8939              :     /* We already did the appropriate conversion when packing args.  */
    8940              :     val = orig_arg;
    8941   1335934728 :   else if (is_type)
    8942              :     {
    8943   1173820101 :       if (requires_tmpl_type)
    8944              :         {
    8945      9255173 :           if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
    8946              :             /* The number of argument required is not known yet.
    8947              :                Just accept it for now.  */
    8948              :             val = orig_arg;
    8949              :           else
    8950              :             {
    8951              :               /* Strip alias templates that are equivalent to another
    8952              :                  template.  */
    8953      9202319 :               arg = get_underlying_template (arg);
    8954              : 
    8955      9202319 :               if (coerce_template_template_parms (parm, arg,
    8956              :                                                   complain, in_decl,
    8957              :                                                   args))
    8958              :                 {
    8959      9201237 :                   val = arg;
    8960              : 
    8961              :                   /* TEMPLATE_TEMPLATE_PARM node is preferred over
    8962              :                      TEMPLATE_DECL.  */
    8963      9201237 :                   if (val != error_mark_node)
    8964              :                     {
    8965      9201237 :                       if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
    8966       387823 :                         val = TREE_TYPE (val);
    8967      9201237 :                       if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
    8968          107 :                         val = make_pack_expansion (val, complain);
    8969              :                     }
    8970              :                 }
    8971              :               else
    8972              :                 {
    8973         1082 :                   if (in_decl && (complain & tf_error))
    8974              :                     {
    8975           39 :                       auto_diagnostic_group d;
    8976           39 :                       error ("type/value mismatch at argument %d in "
    8977              :                              "template parameter list for %qD",
    8978              :                              i + 1, in_decl);
    8979           39 :                       inform (input_location,
    8980              :                               "  expected a template of type %qD, got %qT",
    8981              :                               parm, orig_arg);
    8982           39 :                     }
    8983              : 
    8984         1082 :                   val = error_mark_node;
    8985              :                 }
    8986              : 
    8987              :               // Check that the constraints are compatible before allowing the
    8988              :               // substitution.
    8989      9202319 :               if (val != error_mark_node)
    8990      9201237 :                 if (!is_compatible_template_arg (parm, arg, args))
    8991              :                   {
    8992           18 :                     if (in_decl && (complain & tf_error))
    8993              :                       {
    8994           15 :                         auto_diagnostic_group d;
    8995           15 :                         error ("constraint mismatch at argument %d in "
    8996              :                                "template parameter list for %qD",
    8997              :                                i + 1, in_decl);
    8998           15 :                         inform (input_location, "  expected %qD but got %qD",
    8999              :                                 parm, arg);
    9000           15 :                       }
    9001           18 :                     val = error_mark_node;
    9002              :                   }
    9003              :             }
    9004              :         }
    9005              :       else
    9006              :         val = orig_arg;
    9007              :       /* We only form one instance of each template specialization.
    9008              :          Therefore, if we use a non-canonical variant (i.e., a
    9009              :          typedef), any future messages referring to the type will use
    9010              :          the typedef, which is confusing if those future uses do not
    9011              :          themselves also use the typedef.  */
    9012   1173820101 :       if (TYPE_P (val))
    9013   1165005605 :         val = canonicalize_type_argument (val, complain);
    9014              :     }
    9015              :   else
    9016              :     {
    9017    162114627 :       tree t = TREE_TYPE (parm);
    9018              : 
    9019    324229254 :       if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
    9020    324229254 :           > TMPL_ARGS_DEPTH (args))
    9021              :         /* We don't have enough levels of args to do any substitution.  This
    9022              :            can happen in the context of -fnew-ttp-matching.  */;
    9023    162114139 :       else if (tree a = type_uses_auto (t))
    9024              :         {
    9025       118990 :           t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
    9026              :                                  LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
    9027       118990 :           if (t == error_mark_node)
    9028              :             return error_mark_node;
    9029              :         }
    9030              :       else
    9031    161995149 :         t = tsubst (t, args, complain, in_decl);
    9032              : 
    9033              :       /* Perform array-to-pointer and function-to-pointer conversion
    9034              :          as per [temp.param]/10.  */
    9035    162114609 :       t = type_decays_to (t);
    9036              : 
    9037    162114609 :       if (invalid_nontype_parm_type_p (t, complain))
    9038           80 :         return error_mark_node;
    9039              : 
    9040              :       /* Drop top-level cv-qualifiers on the substituted/deduced type of
    9041              :          this non-type template parameter, as per [temp.param]/6.  */
    9042    162114529 :       t = cv_unqualified (t);
    9043              : 
    9044    162114529 :       if (t != TREE_TYPE (parm))
    9045      5498050 :         t = canonicalize_type_argument (t, complain);
    9046              : 
    9047              :       /* We need to handle arguments for alias or concept templates
    9048              :          differently: we need to force building an IMPLICIT_CONV_EXPR, because
    9049              :          these arguments are going to be substituted directly into the
    9050              :          dependent type; they might not get another chance at
    9051              :          convert_nontype_argument.  But if the argument ends up here again for
    9052              :          a template that isn't one of those, remove the conversion for
    9053              :          consistency between naming the same dependent type directly or through
    9054              :          an alias.  */
    9055    162114529 :       bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9056    120026381 :                                     || concept_definition_p (in_decl));
    9057    121842324 :       if (!force_conv
    9058    121842324 :           && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR
    9059      3897308 :           && IMPLICIT_CONV_EXPR_FORCED (orig_arg)
    9060      3897158 :           && same_type_p (TREE_TYPE (orig_arg), t))
    9061      3897155 :         orig_arg = TREE_OPERAND (orig_arg, 0);
    9062              : 
    9063    162114529 :       if (!dependent_implicit_conv_p (t, orig_arg, force_conv))
    9064              :         /* We used to call digest_init here.  However, digest_init
    9065              :            will report errors, which we don't want when complain
    9066              :            is zero.  More importantly, digest_init will try too
    9067              :            hard to convert things: for example, `0' should not be
    9068              :            converted to pointer type at this point according to
    9069              :            the standard.  Accepting this is not merely an
    9070              :            extension, since deciding whether or not these
    9071              :            conversions can occur is part of determining which
    9072              :            function template to call, or whether a given explicit
    9073              :            argument specification is valid.  */
    9074    145816145 :         val = convert_nontype_argument (t, orig_arg, complain);
    9075              :       else
    9076              :         {
    9077     16298384 :           val = canonicalize_expr_argument (orig_arg, complain);
    9078     16298384 :           val = maybe_convert_nontype_argument (t, val, force_conv);
    9079              :         }
    9080              : 
    9081    162114529 :       if (val == NULL_TREE)
    9082          494 :         val = error_mark_node;
    9083    162114035 :       else if (val == error_mark_node && (complain & tf_error))
    9084           57 :         error_at (cp_expr_loc_or_input_loc (orig_arg),
    9085              :                   "could not convert template argument %qE from %qT to %qT",
    9086           41 :                   orig_arg, TREE_TYPE (orig_arg), t);
    9087              : 
    9088    162114529 :       if (INDIRECT_REF_P (val))
    9089              :         {
    9090              :           /* Reject template arguments that are references to built-in
    9091              :              functions with no library fallbacks.  */
    9092         3160 :           const_tree inner = TREE_OPERAND (val, 0);
    9093         3160 :           const_tree innertype = TREE_TYPE (inner);
    9094         3160 :           if (innertype
    9095         1799 :               && TYPE_REF_P (innertype)
    9096         1790 :               && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
    9097           96 :               && TREE_OPERAND_LENGTH (inner) > 0
    9098         3226 :               && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
    9099            4 :               return error_mark_node;
    9100              :         }
    9101              : 
    9102    162114525 :       if (TREE_CODE (val) == SCOPE_REF)
    9103              :         {
    9104              :           /* Strip typedefs from the SCOPE_REF.  */
    9105      7879455 :           tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
    9106      7879455 :           tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
    9107              :                                                    complain);
    9108     15758910 :           val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
    9109      7879455 :                                       QUALIFIED_NAME_IS_TEMPLATE (val));
    9110              :         }
    9111              :     }
    9112              : 
    9113              :   return val;
    9114              : }
    9115              : 
    9116              : /* Coerces the remaining template arguments in INNER_ARGS (from
    9117              :    ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
    9118              :    Returns the coerced argument pack. PARM_IDX is the position of this
    9119              :    parameter in the template parameter list. ARGS is the original
    9120              :    template argument list.  */
    9121              : static tree
    9122     95349024 : coerce_template_parameter_pack (tree parms,
    9123              :                                 int parm_idx,
    9124              :                                 tree args,
    9125              :                                 tree inner_args,
    9126              :                                 int arg_idx,
    9127              :                                 tree new_args,
    9128              :                                 int* lost,
    9129              :                                 tree in_decl,
    9130              :                                 tsubst_flags_t complain)
    9131              : {
    9132     95349024 :   tree parm = TREE_VEC_ELT (parms, parm_idx);
    9133    190698048 :   int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    9134     95349024 :   tree packed_args;
    9135     95349024 :   tree argument_pack;
    9136     95349024 :   tree packed_parms = NULL_TREE;
    9137              : 
    9138     95349024 :   if (arg_idx > nargs)
    9139              :     arg_idx = nargs;
    9140              : 
    9141     95349024 :   if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
    9142              :     {
    9143              :       /* When the template parameter is a non-type template parameter pack
    9144              :          or template template parameter pack whose type or template
    9145              :          parameters use parameter packs, we know exactly how many arguments
    9146              :          we are looking for.  Build a vector of the instantiated decls for
    9147              :          these template parameters in PACKED_PARMS.  */
    9148              :       /* We can't use make_pack_expansion here because it would interpret a
    9149              :          _DECL as a use rather than a declaration.  */
    9150           87 :       tree decl = TREE_VALUE (parm);
    9151           87 :       tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
    9152           87 :       PACK_EXPANSION_PATTERN (exp) = decl;
    9153          174 :       PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
    9154           87 :       SET_TYPE_STRUCTURAL_EQUALITY (exp);
    9155              : 
    9156           87 :       TREE_VEC_LENGTH (args)--;
    9157           87 :       packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
    9158           84 :       TREE_VEC_LENGTH (args)++;
    9159              : 
    9160           84 :       if (packed_parms == error_mark_node)
    9161              :         return error_mark_node;
    9162              : 
    9163              :       /* If we're doing a partial instantiation of a member template,
    9164              :          verify that all of the types used for the non-type
    9165              :          template parameter pack are, in fact, valid for non-type
    9166              :          template parameters.  */
    9167           84 :       if (arg_idx < nargs
    9168           84 :           && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
    9169              :         {
    9170           42 :           int j, len = TREE_VEC_LENGTH (packed_parms);
    9171           92 :           for (j = 0; j < len; ++j)
    9172              :             {
    9173           52 :               tree t = TREE_VEC_ELT (packed_parms, j);
    9174           52 :               if (TREE_CODE (t) == PARM_DECL
    9175           52 :                   && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
    9176            2 :                 return error_mark_node;
    9177              :             }
    9178              :           /* We don't know how many args we have yet, just
    9179              :              use the unconverted ones for now.  */
    9180              :           return NULL_TREE;
    9181              :         }
    9182              : 
    9183           42 :       packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
    9184              :     }
    9185              :   else
    9186     95348937 :     packed_args = make_tree_vec (nargs - arg_idx);
    9187              : 
    9188              :   /* Convert the remaining arguments, which will be a part of the
    9189              :      parameter pack "parm".  */
    9190     95348979 :   int first_pack_arg = arg_idx;
    9191    259478928 :   for (; arg_idx < nargs; ++arg_idx)
    9192              :     {
    9193    164129961 :       tree arg = TREE_VEC_ELT (inner_args, arg_idx);
    9194    164129961 :       tree actual_parm = TREE_VALUE (parm);
    9195    164129961 :       int pack_idx = arg_idx - first_pack_arg;
    9196              : 
    9197    164129961 :       if (packed_parms)
    9198              :         {
    9199              :           /* Once we've packed as many args as we have types, stop.  */
    9200           72 :           if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
    9201              :             break;
    9202           60 :           else if (PACK_EXPANSION_P (arg))
    9203              :             /* We don't know how many args we have yet, just
    9204              :                use the unconverted ones for now.  */
    9205              :             return NULL_TREE;
    9206              :           else
    9207           60 :             actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
    9208              :         }
    9209              : 
    9210    164129949 :       if (arg == error_mark_node)
    9211              :         {
    9212          749 :           if (complain & tf_error)
    9213          746 :             error ("template argument %d is invalid", arg_idx + 1);
    9214              :         }
    9215              :       else
    9216    164129200 :         arg = convert_template_argument (actual_parm,
    9217              :                                          arg, new_args, complain, parm_idx,
    9218              :                                          in_decl);
    9219    164129949 :       if (arg == error_mark_node)
    9220          918 :         (*lost)++;
    9221    164129949 :       TREE_VEC_ELT (packed_args, pack_idx) = arg;
    9222              :     }
    9223              : 
    9224     95348979 :   if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
    9225     95348979 :       && TREE_VEC_LENGTH (packed_args) > 0)
    9226              :     {
    9227            3 :       if (complain & tf_error)
    9228            3 :         error ("wrong number of template arguments (%d, should be %d)",
    9229            3 :                arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
    9230            3 :       return error_mark_node;
    9231              :     }
    9232              : 
    9233     95348976 :   if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
    9234     95348976 :       || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
    9235     92649335 :     argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
    9236              :   else
    9237              :     {
    9238      2699641 :       argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
    9239      2699641 :       TREE_CONSTANT (argument_pack) = 1;
    9240              :     }
    9241              : 
    9242     95348976 :   ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
    9243     95348976 :   if (CHECKING_P)
    9244     95348976 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
    9245              :                                          TREE_VEC_LENGTH (packed_args));
    9246     95348976 :   return argument_pack;
    9247              : }
    9248              : 
    9249              : /* Returns the number of pack expansions in the template argument vector
    9250              :    ARGS.  */
    9251              : 
    9252              : static int
    9253   1960499855 : pack_expansion_args_count (tree args)
    9254              : {
    9255   1960499855 :   int i;
    9256   1960499855 :   int count = 0;
    9257   1960499855 :   if (args)
    9258   5390863502 :     for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
    9259              :       {
    9260   3430363647 :         tree elt = TREE_VEC_ELT (args, i);
    9261   3430363647 :         if (elt && PACK_EXPANSION_P (elt))
    9262     29036580 :           ++count;
    9263              :       }
    9264   1960499855 :   return count;
    9265              : }
    9266              : 
    9267              : /* Convert all template arguments to their appropriate types, and
    9268              :    return a vector containing the innermost resulting template
    9269              :    arguments.  If any error occurs, return error_mark_node. Error and
    9270              :    warning messages are issued under control of COMPLAIN.
    9271              : 
    9272              :    If PARMS represents all template parameters levels, this function
    9273              :    returns a vector of vectors representing all the resulting argument
    9274              :    levels.  Note that in this case, only the innermost arguments are
    9275              :    coerced because the outermost ones are supposed to have been coerced
    9276              :    already.  Otherwise, if PARMS represents only (the innermost) vector
    9277              :    of parameters, this function returns a vector containing just the
    9278              :    innermost resulting arguments.
    9279              : 
    9280              :    If REQUIRE_ALL_ARGS is false, argument deduction will be performed
    9281              :    for arguments not specified in ARGS.  If REQUIRE_ALL_ARGS is true,
    9282              :    arguments not specified in ARGS must have default arguments which
    9283              :    we'll use to fill in ARGS.  */
    9284              : 
    9285              : tree
    9286    846862107 : coerce_template_parms (tree parms,
    9287              :                        tree args,
    9288              :                        tree in_decl,
    9289              :                        tsubst_flags_t complain,
    9290              :                        bool require_all_args /* = true */)
    9291              : {
    9292    846862107 :   int nparms, nargs, parm_idx, arg_idx, lost = 0;
    9293    846862107 :   tree orig_inner_args;
    9294    846862107 :   tree inner_args;
    9295              : 
    9296              :   /* When used as a boolean value, indicates whether this is a
    9297              :      variadic template parameter list. Since it's an int, we can also
    9298              :      subtract it from nparms to get the number of non-variadic
    9299              :      parameters.  */
    9300    846862107 :   int variadic_p = 0;
    9301    846862107 :   int variadic_args_p = 0;
    9302    846862107 :   int post_variadic_parms = 0;
    9303              : 
    9304              :   /* Adjustment to nparms for fixed parameter packs.  */
    9305    846862107 :   int fixed_pack_adjust = 0;
    9306    846862107 :   int fixed_packs = 0;
    9307    846862107 :   int missing = 0;
    9308              : 
    9309              :   /* Likewise for parameters with default arguments.  */
    9310    846862107 :   int default_p = 0;
    9311              : 
    9312    846862107 :   if (args == error_mark_node)
    9313              :     return error_mark_node;
    9314              : 
    9315    846862107 :   bool return_full_args = false;
    9316    846862107 :   if (TREE_CODE (parms) == TREE_LIST)
    9317              :     {
    9318    672764641 :       if (TMPL_PARMS_DEPTH (parms) > 1)
    9319              :         {
    9320     24396430 :           gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
    9321              :           return_full_args = true;
    9322              :         }
    9323    672764641 :       parms = INNERMOST_TEMPLATE_PARMS (parms);
    9324              :     }
    9325              : 
    9326    846862107 :   nparms = TREE_VEC_LENGTH (parms);
    9327              : 
    9328              :   /* Determine if there are any parameter packs or default arguments.  */
    9329   2123015284 :   for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
    9330              :     {
    9331   1276153177 :       tree parm = TREE_VEC_ELT (parms, parm_idx);
    9332   1276153177 :       if (variadic_p)
    9333         9884 :         ++post_variadic_parms;
    9334   1276153177 :       if (template_parameter_pack_p (TREE_VALUE (parm)))
    9335     98987529 :         ++variadic_p;
    9336   1276153177 :       if (TREE_PURPOSE (parm))
    9337    179723045 :         ++default_p;
    9338              :     }
    9339              : 
    9340    846862107 :   inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
    9341              :   /* If there are no parameters that follow a parameter pack, we need to
    9342              :      expand any argument packs so that we can deduce a parameter pack from
    9343              :      some non-packed args followed by an argument pack, as in variadic85.C.
    9344              :      If there are such parameters, we need to leave argument packs intact
    9345              :      so the arguments are assigned properly.  This can happen when dealing
    9346              :      with a nested class inside a partial specialization of a class
    9347              :      template, as in variadic92.C, or when deducing a template parameter pack
    9348              :      from a sub-declarator, as in variadic114.C.  */
    9349    846862107 :   if (!post_variadic_parms)
    9350    846853889 :     inner_args = expand_template_argument_pack (inner_args);
    9351              : 
    9352              :   /* Count any pack expansion args.  */
    9353    846862107 :   variadic_args_p = pack_expansion_args_count (inner_args);
    9354              : 
    9355   1693724214 :   nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    9356     62481398 :   if ((nargs - variadic_args_p > nparms && !variadic_p)
    9357    906941299 :       || (nargs < nparms - variadic_p
    9358              :           && require_all_args
    9359     22022295 :           && !variadic_args_p
    9360     13642227 :           && (TREE_VEC_ELT (parms, nargs) != error_mark_node
    9361     13642227 :               && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
    9362              :     {
    9363      2402354 :     bad_nargs:
    9364      2402354 :       if (complain & tf_error)
    9365              :         {
    9366           86 :           auto_diagnostic_group d;
    9367           86 :           if (variadic_p || default_p)
    9368              :             {
    9369           12 :               nparms -= variadic_p + default_p;
    9370           12 :               error ("wrong number of template arguments "
    9371              :                      "(%d, should be at least %d)", nargs, nparms);
    9372              :             }
    9373              :           else
    9374           74 :              error ("wrong number of template arguments "
    9375              :                     "(%d, should be %d)", nargs, nparms);
    9376              : 
    9377           86 :           if (in_decl)
    9378           86 :             inform (DECL_SOURCE_LOCATION (in_decl),
    9379              :                     "provided for %qD", in_decl);
    9380           86 :         }
    9381              : 
    9382      2402354 :       return error_mark_node;
    9383              :     }
    9384              :   /* We can't pass a pack expansion to a non-pack parameter of an alias
    9385              :      template (DR 1430).  */
    9386    844459762 :   else if (in_decl
    9387    834536723 :            && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9388    688928713 :                || concept_definition_p (in_decl))
    9389    199788727 :            && variadic_args_p
    9390    848545346 :            && nargs - variadic_args_p < nparms - variadic_p)
    9391              :     {
    9392           24 :       if (complain & tf_error)
    9393              :         {
    9394           27 :           for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
    9395              :             {
    9396           27 :               tree arg = TREE_VEC_ELT (inner_args, i);
    9397           27 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    9398              : 
    9399           27 :               if (PACK_EXPANSION_P (arg)
    9400           27 :                   && !template_parameter_pack_p (parm))
    9401              :                 {
    9402           24 :                   auto_diagnostic_group d;
    9403           24 :                   if (DECL_ALIAS_TEMPLATE_P (in_decl))
    9404           12 :                     error_at (location_of (arg),
    9405              :                               "pack expansion argument for non-pack parameter "
    9406              :                               "%qD of alias template %qD", parm, in_decl);
    9407              :                   else
    9408           12 :                     error_at (location_of (arg),
    9409              :                               "pack expansion argument for non-pack parameter "
    9410              :                               "%qD of concept %qD", parm, in_decl);
    9411           24 :                   inform (DECL_SOURCE_LOCATION (parm), "declared here");
    9412           24 :                   goto found;
    9413           24 :                 }
    9414              :             }
    9415            0 :           gcc_unreachable ();
    9416           24 :         found:;
    9417              :         }
    9418           24 :       return error_mark_node;
    9419              :     }
    9420              : 
    9421              :   /* We need to evaluate the template arguments, even though this
    9422              :      template-id may be nested within a "sizeof".  */
    9423    844459738 :   cp_evaluated ev;
    9424              : 
    9425    844459738 :   tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
    9426    844459738 :   tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
    9427    844459738 :   int pack_adjust = 0;
    9428   2100644032 :   for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
    9429              :     {
    9430   1268153234 :       tree arg;
    9431   1268153234 :       tree parm;
    9432              : 
    9433              :       /* Get the Ith template parameter.  */
    9434   1268153234 :       parm = TREE_VEC_ELT (parms, parm_idx);
    9435              : 
    9436   1268153234 :       if (parm == error_mark_node)
    9437              :         {
    9438            0 :           TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
    9439            0 :           continue;
    9440              :         }
    9441              : 
    9442              :       /* Calculate the next argument.  */
    9443   1268153234 :       if (arg_idx < nargs)
    9444   1238779833 :         arg = TREE_VEC_ELT (inner_args, arg_idx);
    9445              :       else
    9446              :         arg = NULL_TREE;
    9447              : 
    9448   1268153234 :       if (template_parameter_pack_p (TREE_VALUE (parm))
    9449     98350440 :           && (arg || require_all_args || !(complain & tf_partial))
    9450   1363502297 :           && !(arg && ARGUMENT_PACK_P (arg)))
    9451              :         {
    9452              :           /* Some arguments will be placed in the
    9453              :              template parameter pack PARM.  */
    9454     95349024 :           arg = coerce_template_parameter_pack (parms, parm_idx, args,
    9455              :                                                 inner_args, arg_idx,
    9456              :                                                 new_args, &lost,
    9457              :                                                 in_decl, complain);
    9458              : 
    9459     95349021 :           if (arg == NULL_TREE)
    9460              :             {
    9461              :               /* We don't know how many args we have yet, just use the
    9462              :                  unconverted (and still packed) ones for now.  */
    9463           40 :               ggc_free (new_inner_args);
    9464           40 :               new_inner_args = strip_typedefs (orig_inner_args,
    9465              :                                                /*remove_attrs=*/nullptr,
    9466              :                                                STF_KEEP_INJ_CLASS_NAME);
    9467           40 :               arg_idx = nargs;
    9468           40 :               break;
    9469              :             }
    9470              : 
    9471     95348981 :           TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
    9472              : 
    9473              :           /* Store this argument.  */
    9474     95348981 :           if (arg == error_mark_node)
    9475              :             {
    9476            5 :               lost++;
    9477              :               /* We are done with all of the arguments.  */
    9478            5 :               arg_idx = nargs;
    9479            5 :               break;
    9480              :             }
    9481              :           else
    9482              :             {
    9483     95348976 :               pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
    9484     95348976 :               arg_idx += pack_adjust;
    9485     95348976 :               if (fixed_parameter_pack_p (TREE_VALUE (parm)))
    9486              :                 {
    9487           39 :                   ++fixed_packs;
    9488           39 :                   fixed_pack_adjust += pack_adjust;
    9489              :                 }
    9490              :             }
    9491              : 
    9492     95348976 :           continue;
    9493              :         }
    9494   1172804210 :       else if (arg)
    9495              :         {
    9496   1145454108 :           if (PACK_EXPANSION_P (arg))
    9497              :             {
    9498              :               /* "If every valid specialization of a variadic template
    9499              :                  requires an empty template parameter pack, the template is
    9500              :                  ill-formed, no diagnostic required."  So check that the
    9501              :                  pattern works with this parameter.  */
    9502      1014849 :               tree pattern = PACK_EXPANSION_PATTERN (arg);
    9503      1014849 :               tree conv = convert_template_argument (TREE_VALUE (parm),
    9504              :                                                      pattern, new_args,
    9505              :                                                      complain, parm_idx,
    9506              :                                                      in_decl);
    9507      1014849 :               if (conv == error_mark_node)
    9508              :                 {
    9509            9 :                   if (complain & tf_error)
    9510            6 :                     inform (input_location, "so any instantiation with a "
    9511              :                             "non-empty parameter pack would be ill-formed");
    9512            9 :                   ++lost;
    9513              :                 }
    9514      1014840 :               else if (TYPE_P (conv) && !TYPE_P (pattern))
    9515              :                 /* Recover from missing typename.  */
    9516            9 :                 TREE_VEC_ELT (inner_args, arg_idx)
    9517           18 :                   = make_pack_expansion (conv, complain);
    9518              : 
    9519              :               /* We don't know how many args we have yet, just
    9520              :                  use the unconverted (but unpacked) ones for now.  */
    9521      1014849 :               ggc_free (new_inner_args);
    9522      1014849 :               new_inner_args = strip_typedefs (inner_args,
    9523              :                                                /*remove_attrs=*/nullptr,
    9524              :                                                STF_KEEP_INJ_CLASS_NAME);
    9525      1014849 :               arg_idx = nargs;
    9526      1014849 :               break;
    9527              :             }
    9528              :         }
    9529     27350102 :       else if (require_all_args)
    9530              :         {
    9531              :           /* There must be a default arg in this case.  */
    9532     16396059 :           arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
    9533     16396059 :                                      complain | (processing_template_decl
    9534     16396059 :                                                  ? tf_partial : tf_none),
    9535              :                                      in_decl);
    9536              :           /* The position of the first default template argument,
    9537              :              is also the number of non-defaulted arguments in NEW_INNER_ARGS.
    9538              :              Record that.  */
    9539     16396059 :           if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9540     13642135 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9541              :                                                  arg_idx - pack_adjust);
    9542              :         }
    9543              :       else
    9544              :         break;
    9545              : 
    9546   1160835318 :       if (arg == error_mark_node)
    9547              :         {
    9548         1011 :           if (complain & tf_error)
    9549          983 :             error ("template argument %d is invalid", arg_idx + 1);
    9550              :         }
    9551   1160834307 :       else if (!arg)
    9552              :         {
    9553              :           /* This can occur if there was an error in the template
    9554              :              parameter list itself (which we would already have
    9555              :              reported) that we are trying to recover from, e.g., a class
    9556              :              template with a parameter list such as
    9557              :              template<typename..., typename> (cpp0x/variadic150.C).  */
    9558           35 :           ++lost;
    9559              : 
    9560              :           /* This can also happen with a fixed parameter pack (71834).  */
    9561           35 :           if (arg_idx >= nargs)
    9562            6 :             ++missing;
    9563              :         }
    9564              :       else
    9565   1160834272 :         arg = convert_template_argument (TREE_VALUE (parm),
    9566              :                                          arg, new_args, complain,
    9567              :                                          parm_idx, in_decl);
    9568              : 
    9569   1160835318 :       if (arg == error_mark_node)
    9570      3109717 :         lost++;
    9571              : 
    9572   1160835318 :       TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
    9573              :     }
    9574              : 
    9575    844459735 :   if (missing || arg_idx < nargs - variadic_args_p)
    9576              :     {
    9577              :       /* If we had fixed parameter packs, we didn't know how many arguments we
    9578              :          actually needed earlier; now we do.  */
    9579            9 :       nparms += fixed_pack_adjust;
    9580            9 :       variadic_p -= fixed_packs;
    9581            9 :       goto bad_nargs;
    9582              :     }
    9583              : 
    9584    844459726 :   if (arg_idx < nargs)
    9585              :     {
    9586              :       /* We had some pack expansion arguments that will only work if the packs
    9587              :          are empty, but wait until instantiation time to complain.
    9588              :          See variadic-ttp3.C.  */
    9589              : 
    9590              :       /* Except that we can't provide empty packs to alias templates or
    9591              :          concepts when there are no corresponding parameters. Basically,
    9592              :          we can get here with this:
    9593              : 
    9594              :              template<typename T> concept C = true;
    9595              : 
    9596              :              template<typename... Args>
    9597              :                requires C<Args...>
    9598              :              void f();
    9599              : 
    9600              :          When parsing C<Args...>, we try to form a concept check of
    9601              :          C<?, Args...>. Without the extra check for substituting an empty
    9602              :          pack past the last parameter, we can accept the check as valid.
    9603              : 
    9604              :          FIXME: This may be valid for alias templates (but I doubt it).
    9605              : 
    9606              :          FIXME: The error could be better also.   */
    9607       324168 :       if (in_decl && concept_definition_p (in_decl))
    9608              :         {
    9609            0 :           if (complain & tf_error)
    9610            0 :             error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
    9611              :                       "too many arguments");
    9612    844459726 :           return error_mark_node;
    9613              :         }
    9614              : 
    9615       215859 :       int len = nparms + (nargs - arg_idx);
    9616       215859 :       tree args = make_tree_vec (len);
    9617       215859 :       int i = 0;
    9618       647580 :       for (; i < nparms; ++i)
    9619       215862 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
    9620       431718 :       for (; i < len; ++i, ++arg_idx)
    9621       215859 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
    9622              :                                                arg_idx - pack_adjust);
    9623       215859 :       new_inner_args = args;
    9624              :     }
    9625              : 
    9626    844459726 :   if (lost)
    9627              :     {
    9628      3109922 :       gcc_assert (!(complain & tf_error) || seen_error ());
    9629      3109922 :       return error_mark_node;
    9630              :     }
    9631              : 
    9632    841349804 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9633    826692878 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9634              :                                          TREE_VEC_LENGTH (new_inner_args));
    9635              : 
    9636              :   /* If we expanded packs in inner_args and aren't returning it now, the
    9637              :      expanded vec is garbage.  */
    9638    841349804 :   if (inner_args != new_inner_args
    9639    840354524 :       && inner_args != orig_inner_args)
    9640     75242960 :     ggc_free (inner_args);
    9641              : 
    9642    841349804 :   return return_full_args ? new_args : new_inner_args;
    9643              : }
    9644              : 
    9645              : /* Returns true if T is a wrapper to make a C++20 template parameter
    9646              :    object const.  */
    9647              : 
    9648              : static bool
    9649   8840183373 : class_nttp_const_wrapper_p (tree t)
    9650              : {
    9651   8840183373 :   if (cxx_dialect < cxx20)
    9652              :     return false;
    9653   8782010113 :   return (TREE_CODE (t) == VIEW_CONVERT_EXPR
    9654          576 :           && CP_TYPE_CONST_P (TREE_TYPE (t))
    9655   8782010689 :           && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
    9656              : }
    9657              : 
    9658              : /* Returns 1 if template args OT and NT are equivalent.  */
    9659              : 
    9660              : int
    9661   4049915831 : template_args_equal (tree ot, tree nt)
    9662              : {
    9663   4049915831 :   if (nt == ot)
    9664              :     return 1;
    9665   2622714845 :   if (nt == NULL_TREE || ot == NULL_TREE)
    9666              :     return false;
    9667   2622467922 :   if (nt == any_targ_node || ot == any_targ_node)
    9668              :     return true;
    9669              : 
    9670   2622467918 :   if (class_nttp_const_wrapper_p (nt))
    9671          210 :     nt = TREE_OPERAND (nt, 0);
    9672   2622467918 :   if (class_nttp_const_wrapper_p (ot))
    9673          173 :     ot = TREE_OPERAND (ot, 0);
    9674              : 
    9675              :   /* DR 1558: Don't treat an alias template specialization with dependent
    9676              :      arguments as equivalent to its underlying type when used as a template
    9677              :      argument; we need them to be distinct so that we substitute into the
    9678              :      specialization arguments at instantiation time.  And aliases can't be
    9679              :      equivalent without being ==, so we don't need to look any deeper.
    9680              : 
    9681              :      During partial ordering, however, we need to treat them normally so we can
    9682              :      order uses of the same alias with different cv-qualification (79960).  */
    9683   2622467918 :   auto cso = make_temp_override (comparing_dependent_aliases);
    9684   2622467918 :   if (!comparing_for_partial_ordering)
    9685   2617361294 :     ++comparing_dependent_aliases;
    9686              : 
    9687   2622467918 :   if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
    9688              :     /* For member templates */
    9689    147045713 :     return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
    9690   2539104305 :   else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
    9691     21466063 :     return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
    9692     19945189 :             && template_args_equal (PACK_EXPANSION_PATTERN (ot),
    9693     19945189 :                                     PACK_EXPANSION_PATTERN (nt))
    9694     35282727 :             && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
    9695     35282727 :                                     PACK_EXPANSION_EXTRA_ARGS (nt)));
    9696   2517088178 :   else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
    9697    570119329 :     return cp_tree_equal (ot, nt);
    9698   1946968849 :   else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
    9699            0 :     gcc_unreachable ();
    9700   1946968849 :   else if (TYPE_P (nt) || TYPE_P (ot))
    9701              :     {
    9702   1702011753 :       if (!(TYPE_P (nt) && TYPE_P (ot)))
    9703              :         return false;
    9704   1682619252 :       return same_type_p (ot, nt);
    9705              :     }
    9706              :   else
    9707              :     {
    9708              :       /* Try to treat a template non-type argument that has been converted
    9709              :          to the parameter type as equivalent to one that hasn't yet.  */
    9710    244957096 :       for (enum tree_code code1 = TREE_CODE (ot);
    9711    245892668 :            CONVERT_EXPR_CODE_P (code1)
    9712    245892668 :              || code1 == NON_LVALUE_EXPR;
    9713       935572 :            code1 = TREE_CODE (ot))
    9714       935572 :         ot = TREE_OPERAND (ot, 0);
    9715              : 
    9716    244957096 :       for (enum tree_code code2 = TREE_CODE (nt);
    9717    245938367 :            CONVERT_EXPR_CODE_P (code2)
    9718    245938367 :              || code2 == NON_LVALUE_EXPR;
    9719       981271 :            code2 = TREE_CODE (nt))
    9720       981271 :         nt = TREE_OPERAND (nt, 0);
    9721              : 
    9722    244957096 :       return cp_tree_equal (ot, nt);
    9723              :     }
    9724   2622467918 : }
    9725              : 
    9726              : /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
    9727              :    template arguments.  Returns false otherwise, and updates OLDARG_PTR and
    9728              :    NEWARG_PTR with the offending arguments if they are non-NULL.  */
    9729              : 
    9730              : bool
    9731   3058812404 : comp_template_args (tree oldargs, tree newargs,
    9732              :                     tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
    9733              : {
    9734   3058812404 :   if (oldargs == newargs)
    9735              :     return true;
    9736              : 
    9737   2270773294 :   if (!oldargs || !newargs)
    9738              :     return false;
    9739              : 
    9740   2270773293 :   if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    9741              :     return false;
    9742              : 
    9743   3512648193 :   for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
    9744              :     {
    9745   2742981240 :       tree nt = TREE_VEC_ELT (newargs, i);
    9746   2742981240 :       tree ot = TREE_VEC_ELT (oldargs, i);
    9747              : 
    9748   2742981240 :       if (! template_args_equal (ot, nt))
    9749              :         {
    9750   1499880476 :           if (oldarg_ptr != NULL)
    9751           12 :             *oldarg_ptr = ot;
    9752   1499880476 :           if (newarg_ptr != NULL)
    9753           12 :             *newarg_ptr = nt;
    9754   1499880476 :           return false;
    9755              :         }
    9756              :     }
    9757              :   return true;
    9758              : }
    9759              : 
    9760              : static bool
    9761     22323393 : comp_template_args_porder (tree oargs, tree nargs)
    9762              : {
    9763     22323393 :   ++comparing_for_partial_ordering;
    9764     22323393 :   bool equal = comp_template_args (oargs, nargs);
    9765     22323393 :   --comparing_for_partial_ordering;
    9766     22323393 :   return equal;
    9767              : }
    9768              : 
    9769              : /* Implement a freelist interface for objects of type T.
    9770              : 
    9771              :    Head is a separate object, rather than a regular member, so that we
    9772              :    can define it as a GTY deletable pointer, which is highly
    9773              :    desirable.  A data member could be declared that way, but then the
    9774              :    containing object would implicitly get GTY((user)), which would
    9775              :    prevent us from instantiating freelists as global objects.
    9776              :    Although this way we can create freelist global objects, they're
    9777              :    such thin wrappers that instantiating temporaries at every use
    9778              :    loses nothing and saves permanent storage for the freelist object.
    9779              : 
    9780              :    Member functions next, anew, poison and reinit have default
    9781              :    implementations that work for most of the types we're interested
    9782              :    in, but if they don't work for some type, they should be explicitly
    9783              :    specialized.  See the comments before them for requirements, and
    9784              :    the example specializations for the tree_list_freelist.  */
    9785              : template <typename T>
    9786              : class freelist
    9787              : {
    9788              :   /* Return the next object in a chain.  We could just do type
    9789              :      punning, but if we access the object with its underlying type, we
    9790              :      avoid strict-aliasing trouble.  This needs only work between
    9791              :      poison and reinit.  */
    9792    867129737 :   static T *&next (T *obj) { return obj->next; }
    9793              : 
    9794              :   /* Return a newly allocated, uninitialized or minimally-initialized
    9795              :      object of type T.  Any initialization performed by anew should
    9796              :      either remain across the life of the object and the execution of
    9797              :      poison, or be redone by reinit.  */
    9798     19216030 :   static T *anew () { return ggc_alloc<T> (); }
    9799              : 
    9800              :   /* Optionally scribble all over the bits holding the object, so that
    9801              :      they become (mostly?) uninitialized memory.  This is called while
    9802              :      preparing to make the object part of the free list.  */
    9803    884519214 :   static void poison (T *obj) {
    9804    884519214 :     T *p ATTRIBUTE_UNUSED = obj;
    9805    884519214 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9806              : 
    9807              : #ifdef ENABLE_GC_CHECKING
    9808              :     /* Poison the data, to indicate the data is garbage.  */
    9809              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
    9810    884519214 :     memset (p, 0xa5, sizeof (*p));
    9811              : #endif
    9812              :     /* Let valgrind know the object is free.  */
    9813              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
    9814              : 
    9815              :     /* Let valgrind know the next portion of the object is available,
    9816              :        but uninitialized.  */
    9817              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9818              :   }
    9819              : 
    9820              :   /* Bring an object that underwent at least one lifecycle after anew
    9821              :      and before the most recent free and poison, back to a usable
    9822              :      state, reinitializing whatever is needed for it to be
    9823              :      functionally equivalent to an object just allocated and returned
    9824              :      by anew.  This may poison or clear the next field, used by
    9825              :      freelist housekeeping after poison was called.  */
    9826    867129737 :   static void reinit (T *obj) {
    9827    867129737 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9828              : 
    9829              : #ifdef ENABLE_GC_CHECKING
    9830    867129737 :     memset (q, 0xa5, sizeof (*q));
    9831              : #endif
    9832              :     /* Let valgrind know the entire object is available, but
    9833              :        uninitialized.  */
    9834              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
    9835              :   }
    9836              : 
    9837              :   /* Reference a GTY-deletable pointer that points to the first object
    9838              :      in the free list proper.  */
    9839              :   T *&head;
    9840              : public:
    9841              :   /* Construct a freelist object chaining objects off of HEAD.  */
    9842   1770867191 :   freelist (T *&head) : head(head) {}
    9843              : 
    9844              :   /* Add OBJ to the free object list.  The former head becomes OBJ's
    9845              :      successor.  */
    9846    884519963 :   void free (T *obj)
    9847              :   {
    9848    884519963 :     poison (obj);
    9849    884519963 :     next (obj) = head;
    9850    884519963 :     head = obj;
    9851    884519963 :   }
    9852              : 
    9853              :   /* Take an object from the free list, if one is available, or
    9854              :      allocate a new one.  Objects taken from the free list should be
    9855              :      regarded as filled with garbage, except for bits that are
    9856              :      configured to be preserved across free and alloc.  */
    9857    886347228 :   T *alloc ()
    9858              :   {
    9859    886347228 :     if (head)
    9860              :       {
    9861    867130405 :         T *obj = head;
    9862    867130405 :         head = next (head);
    9863    867130405 :         reinit (obj);
    9864    867130405 :         return obj;
    9865              :       }
    9866              :     else
    9867     19216823 :       return anew ();
    9868              :   }
    9869              : };
    9870              : 
    9871              : /* Explicitly specialize the interfaces for freelist<tree_node>: we
    9872              :    want to allocate a TREE_LIST using the usual interface, and ensure
    9873              :    TREE_CHAIN remains functional.  Alas, we have to duplicate a bit of
    9874              :    build_tree_list logic in reinit, so this could go out of sync.  */
    9875              : template <>
    9876              : inline tree &
    9877         2166 : freelist<tree_node>::next (tree obj)
    9878              : {
    9879         2166 :   return TREE_CHAIN (obj);
    9880              : }
    9881              : template <>
    9882              : inline tree
    9883          793 : freelist<tree_node>::anew ()
    9884              : {
    9885          793 :   return build_tree_list (NULL, NULL);
    9886              : }
    9887              : template <>
    9888              : inline void
    9889          749 : freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
    9890              : {
    9891          749 :   int size ATTRIBUTE_UNUSED = sizeof (tree_list);
    9892          749 :   tree p ATTRIBUTE_UNUSED = obj;
    9893          749 :   tree_base *b ATTRIBUTE_UNUSED = &obj->base;
    9894          749 :   tree *q ATTRIBUTE_UNUSED = &next (obj);
    9895              : 
    9896              : #ifdef ENABLE_GC_CHECKING
    9897          749 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9898              : 
    9899              :   /* Poison the data, to indicate the data is garbage.  */
    9900          749 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
    9901          749 :   memset (p, 0xa5, size);
    9902              : #endif
    9903              :   /* Let valgrind know the object is free.  */
    9904          749 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
    9905              :   /* But we still want to use the TREE_CODE and TREE_CHAIN parts.  */
    9906          749 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9907          749 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9908              : 
    9909              : #ifdef ENABLE_GC_CHECKING
    9910          749 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
    9911              :   /* Keep TREE_CHAIN functional.  */
    9912          749 :   TREE_SET_CODE (obj, TREE_LIST);
    9913              : #else
    9914              :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9915              : #endif
    9916          749 : }
    9917              : template <>
    9918              : inline void
    9919          668 : freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
    9920              : {
    9921          668 :   tree_common *c ATTRIBUTE_UNUSED = &obj->common;
    9922              : 
    9923              : #ifdef ENABLE_GC_CHECKING
    9924          668 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9925          668 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9926          668 :   memset (obj, 0, sizeof (tree_list));
    9927              : #endif
    9928              : 
    9929              :   /* Let valgrind know the entire object is available, but
    9930              :      uninitialized.  */
    9931          668 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9932              : 
    9933              : #ifdef ENABLE_GC_CHECKING
    9934          668 :   TREE_SET_CODE (obj, TREE_LIST);
    9935              : #else
    9936              :   TREE_CHAIN (obj) = NULL_TREE;
    9937              :   TREE_TYPE (obj) = NULL_TREE;
    9938              : #endif
    9939          668 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c)));
    9940          668 : }
    9941              : 
    9942              : /* Point to the first object in the TREE_LIST freelist.  */
    9943              : static GTY((deletable)) tree tree_list_freelist_head;
    9944              : /* Return the/an actual TREE_LIST freelist.  */
    9945              : static inline freelist<tree_node>
    9946         2210 : tree_list_freelist ()
    9947              : {
    9948         2210 :   return tree_list_freelist_head;
    9949              : }
    9950              : 
    9951              : /* Point to the first object in the tinst_level freelist.  */
    9952              : static GTY((deletable)) tinst_level *tinst_level_freelist_head;
    9953              : /* Return the/an actual tinst_level freelist.  */
    9954              : static inline freelist<tinst_level>
    9955   1731207054 : tinst_level_freelist ()
    9956              : {
    9957   1731207054 :   return tinst_level_freelist_head;
    9958              : }
    9959              : 
    9960              : /* Point to the first object in the pending_template freelist.  */
    9961              : static GTY((deletable)) pending_template *pending_template_freelist_head;
    9962              : /* Return the/an actual pending_template freelist.  */
    9963              : static inline freelist<pending_template>
    9964     39657927 : pending_template_freelist ()
    9965              : {
    9966     39657927 :   return pending_template_freelist_head;
    9967              : }
    9968              : 
    9969              : /* Build the TREE_LIST object out of a split list, store it
    9970              :    permanently, and return it.  */
    9971              : tree
    9972         1461 : tinst_level::to_list ()
    9973              : {
    9974         1461 :   gcc_assert (split_list_p ());
    9975         1461 :   tree ret = tree_list_freelist ().alloc ();
    9976         1461 :   TREE_PURPOSE (ret) = tldcl;
    9977         1461 :   TREE_VALUE (ret) = targs;
    9978         1461 :   tldcl = ret;
    9979         1461 :   targs = NULL;
    9980         1461 :   gcc_assert (tree_list_p ());
    9981         1461 :   return ret;
    9982              : }
    9983              : 
    9984              : const unsigned short tinst_level::refcount_infinity;
    9985              : 
    9986              : /* Increment OBJ's refcount unless it is already infinite.  */
    9987              : static tinst_level *
    9988   2655184643 : inc_refcount_use (tinst_level *obj)
    9989              : {
    9990   2025104913 :   if (obj && obj->refcount != tinst_level::refcount_infinity)
    9991   2025104913 :     ++obj->refcount;
    9992   2655184643 :   return obj;
    9993              : }
    9994              : 
    9995              : /* Release storage for OBJ and node, if it's a TREE_LIST.  */
    9996              : void
    9997    864985005 : tinst_level::free (tinst_level *obj)
    9998              : {
    9999    864985005 :   if (obj->tree_list_p ())
   10000          749 :     tree_list_freelist ().free (obj->get_node ());
   10001    864985005 :   tinst_level_freelist ().free (obj);
   10002    864985005 : }
   10003              : 
   10004              : /* Decrement OBJ's refcount if not infinite.  If it reaches zero, release
   10005              :    OBJ's DECL and OBJ, and start over with the tinst_level object that
   10006              :    used to be referenced by OBJ's NEXT.  */
   10007              : static void
   10008   2692928446 : dec_refcount_use (tinst_level *obj)
   10009              : {
   10010   2692928446 :   while (obj
   10011   2023432351 :          && obj->refcount != tinst_level::refcount_infinity
   10012   5581345802 :          && !--obj->refcount)
   10013              :     {
   10014    864985005 :       tinst_level *next = obj->next;
   10015    864985005 :       tinst_level::free (obj);
   10016    864985005 :       obj = next;
   10017              :     }
   10018   2692928446 : }
   10019              : 
   10020              : /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
   10021              :    and of the former PTR.  Omitting the second argument is equivalent
   10022              :    to passing (T*)NULL; this is allowed because passing the
   10023              :    zero-valued integral constant NULL confuses type deduction and/or
   10024              :    overload resolution.  */
   10025              : template <typename T>
   10026              : static void
   10027   2692928446 : set_refcount_ptr (T *& ptr, T *obj = NULL)
   10028              : {
   10029   2655184643 :   T *save = ptr;
   10030   2692928446 :   ptr = inc_refcount_use (obj);
   10031   2655184643 :   dec_refcount_use (save);
   10032   2655184643 : }
   10033              : 
   10034              : static void
   10035     25606525 : add_pending_template (tree d)
   10036              : {
   10037     25606525 :   tree ti = (TYPE_P (d)
   10038     25606525 :              ? CLASSTYPE_TEMPLATE_INFO (d)
   10039     25606525 :              : DECL_TEMPLATE_INFO (d));
   10040     25606525 :   struct pending_template *pt;
   10041     25606525 :   int level;
   10042              : 
   10043     25606525 :   if (TI_PENDING_TEMPLATE_FLAG (ti))
   10044              :     return;
   10045              : 
   10046              :   /* We are called both from instantiate_decl, where we've already had a
   10047              :      tinst_level pushed, and instantiate_template, where we haven't.
   10048              :      Compensate.  */
   10049     20123718 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   10050     40247436 :   level = !current_tinst_level
   10051     40247436 :     || current_tinst_level->maybe_get_node () != d;
   10052              : 
   10053            0 :   if (level)
   10054            0 :     push_tinst_level (d);
   10055              : 
   10056     20123718 :   pt = pending_template_freelist ().alloc ();
   10057     20123718 :   pt->next = NULL;
   10058     20123718 :   pt->tinst = NULL;
   10059     20123718 :   set_refcount_ptr (pt->tinst, current_tinst_level);
   10060     20123718 :   if (last_pending_template)
   10061     20092292 :     last_pending_template->next = pt;
   10062              :   else
   10063        31426 :     pending_templates = pt;
   10064              : 
   10065     20123718 :   last_pending_template = pt;
   10066              : 
   10067     20123718 :   TI_PENDING_TEMPLATE_FLAG (ti) = 1;
   10068              : 
   10069     20123718 :   if (level)
   10070            0 :     pop_tinst_level ();
   10071              : }
   10072              : 
   10073              : /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
   10074              :    ARGLIST.  Valid choices for FNS are given in the cp-tree.def
   10075              :    documentation for TEMPLATE_ID_EXPR.  */
   10076              : 
   10077              : tree
   10078     61940465 : lookup_template_function (tree fns, tree arglist)
   10079              : {
   10080     61940465 :   if (fns == error_mark_node || arglist == error_mark_node)
   10081              :     return error_mark_node;
   10082              : 
   10083     61940431 :   gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
   10084              : 
   10085     61940431 :   if (!is_overloaded_fn (fns) && !identifier_p (fns))
   10086              :     {
   10087            3 :       error ("%q#D is not a function template", fns);
   10088            3 :       return error_mark_node;
   10089              :     }
   10090              : 
   10091     61940428 :   if (BASELINK_P (fns))
   10092              :     {
   10093       784065 :       fns = copy_node (fns);
   10094      1568130 :       BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
   10095              :                                          unknown_type_node,
   10096       784065 :                                          BASELINK_FUNCTIONS (fns),
   10097              :                                          arglist);
   10098       784065 :       return fns;
   10099              :     }
   10100              : 
   10101     61156363 :   return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
   10102              : }
   10103              : 
   10104              : /* Within the scope of a template class S<T>, the name S gets bound
   10105              :    (in build_self_reference) to a TYPE_DECL for the class, not a
   10106              :    TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
   10107              :    or one of its enclosing classes, and that type is a template,
   10108              :    return the associated TEMPLATE_DECL.  Otherwise, the original
   10109              :    DECL is returned.
   10110              : 
   10111              :    Also handle the case when DECL is a TREE_LIST of ambiguous
   10112              :    injected-class-names from different bases.  */
   10113              : 
   10114              : tree
   10115    706695634 : maybe_get_template_decl_from_type_decl (tree decl)
   10116              : {
   10117    706695634 :   if (decl == NULL_TREE)
   10118              :     return decl;
   10119              : 
   10120              :   /* DR 176: A lookup that finds an injected-class-name (10.2
   10121              :      [class.member.lookup]) can result in an ambiguity in certain cases
   10122              :      (for example, if it is found in more than one base class). If all of
   10123              :      the injected-class-names that are found refer to specializations of
   10124              :      the same class template, and if the name is followed by a
   10125              :      template-argument-list, the reference refers to the class template
   10126              :      itself and not a specialization thereof, and is not ambiguous.  */
   10127    706695634 :   if (TREE_CODE (decl) == TREE_LIST)
   10128              :     {
   10129              :       tree t, tmpl = NULL_TREE;
   10130        36476 :       for (t = decl; t; t = TREE_CHAIN (t))
   10131              :         {
   10132        36374 :           tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
   10133        36374 :           if (!tmpl)
   10134              :             tmpl = elt;
   10135        18187 :           else if (tmpl != elt)
   10136              :             break;
   10137              :         }
   10138        18187 :       if (tmpl && t == NULL_TREE)
   10139              :         return tmpl;
   10140              :       else
   10141              :         return decl;
   10142              :     }
   10143              : 
   10144    706677447 :   return (decl != NULL_TREE
   10145    706677447 :           && DECL_SELF_REFERENCE_P (decl)
   10146      6551956 :           && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
   10147      6551956 :     ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
   10148              : }
   10149              : 
   10150              : /* If TYPE is the generic implicit instantiation A<T>, return the primary
   10151              :    template type A<T> (which is suitable for entering into, e.g. for defining
   10152              :    a member of A<T>), otherwise return TYPE.  */
   10153              : 
   10154              : tree
   10155    484644517 : adjust_type_for_entering_scope (tree type)
   10156              : {
   10157    482466889 :   if (CLASS_TYPE_P (type)
   10158    482466886 :       && dependent_type_p (type)
   10159     47196199 :       && TYPE_TEMPLATE_INFO (type)
   10160              :       /* We detect the generic implicit instantiation A<T> by inspecting
   10161              :          TYPE_CANONICAL, which lookup_template_class sets to the primary
   10162              :          template type A<T>.  */
   10163    531840700 :       && TYPE_CANONICAL (type) == TREE_TYPE (TYPE_TI_TEMPLATE (type)))
   10164     18351686 :     type = TYPE_CANONICAL (type);
   10165              : 
   10166    484644517 :   return type;
   10167              : }
   10168              : 
   10169              : /* Convenience wrapper over tsubst that does adjust_type_for_entering_scope
   10170              :    on the result.  */
   10171              : 
   10172              : static tree
   10173    465992806 : tsubst_entering_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   10174              : {
   10175    465992806 :   t = tsubst (t, args, complain, in_decl);
   10176    465992806 :   return adjust_type_for_entering_scope (t);
   10177              : }
   10178              : 
   10179              : /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
   10180              :    parameters, find the desired type.
   10181              : 
   10182              :    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
   10183              : 
   10184              :    If D1 is an identifier and CONTEXT is non-NULL, then the lookup is
   10185              :    carried out in CONTEXT. Currently, only namespaces are supported for
   10186              :    CONTEXT.
   10187              : 
   10188              :    If D1 is an identifier and CONTEXT is NULL, the lookup is performed
   10189              :    in the innermost non-namespace binding.
   10190              : 
   10191              :    Otherwise CONTEXT is ignored and no lookup is carried out.
   10192              : 
   10193              :    IN_DECL, if non-NULL, is the template declaration we are trying to
   10194              :    instantiate.
   10195              : 
   10196              :    Issue error and warning messages under control of COMPLAIN.
   10197              : 
   10198              :    ??? Note that this function is currently called *twice* for each
   10199              :    template-id: the first time from the parser, while creating the
   10200              :    incomplete type (finish_template_type), and the second type during the
   10201              :    real instantiation (instantiate_template_class). This is surely something
   10202              :    that we want to avoid. It also causes some problems with argument
   10203              :    coercion (see convert_nontype_argument for more information on this).  */
   10204              : 
   10205              : tree
   10206   1086572940 : lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
   10207              :                        tsubst_flags_t complain)
   10208              : {
   10209   1086572940 :   auto_timevar tv (TV_TEMPLATE_INST);
   10210              : 
   10211   1086572940 :   tree templ = NULL_TREE, parmlist;
   10212   1086572940 :   tree t;
   10213   1086572940 :   spec_entry **slot;
   10214   1086572940 :   spec_entry *entry;
   10215              : 
   10216   1086572940 :   if (identifier_p (d1) && context)
   10217              :     {
   10218       187168 :       gcc_checking_assert (TREE_CODE (context) == NAMESPACE_DECL);
   10219       187168 :       push_decl_namespace (context);
   10220       187168 :       templ = lookup_name (d1, LOOK_where::NAMESPACE, LOOK_want::NORMAL);
   10221       187168 :       pop_decl_namespace ();
   10222              :     }
   10223   1086385772 :   else if (identifier_p (d1))
   10224              :     {
   10225            0 :       tree value = innermost_non_namespace_value (d1);
   10226            0 :       if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
   10227              :         templ = value;
   10228              :       else
   10229              :         {
   10230            0 :           templ = lookup_name (d1);
   10231            0 :           templ = maybe_get_template_decl_from_type_decl (templ);
   10232              :         }
   10233              :     }
   10234   1086385772 :   else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
   10235              :     {
   10236            0 :       tree type = TREE_TYPE (d1);
   10237              : 
   10238              :       /* If we are declaring a constructor, say A<T>::A<T>, we will get
   10239              :          an implicit typename for the second A.  Deal with it.  */
   10240            0 :       if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
   10241            0 :         type = TREE_TYPE (type);
   10242              : 
   10243            0 :       if (CLASSTYPE_TEMPLATE_INFO (type))
   10244              :         {
   10245            0 :           templ = CLASSTYPE_TI_TEMPLATE (type);
   10246            0 :           d1 = DECL_NAME (templ);
   10247              :         }
   10248              :     }
   10249   1086385772 :   else if (TREE_CODE (d1) == ENUMERAL_TYPE
   10250   1086385772 :            || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
   10251              :     {
   10252    901691176 :       templ = TYPE_TI_TEMPLATE (d1);
   10253    901691176 :       d1 = DECL_NAME (templ);
   10254              :     }
   10255    184694596 :   else if (DECL_TYPE_TEMPLATE_P (d1))
   10256              :     {
   10257    184694596 :       templ = d1;
   10258    184694596 :       d1 = DECL_NAME (templ);
   10259              :     }
   10260            0 :   else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
   10261              :     {
   10262            0 :       templ = d1;
   10263            0 :       d1 = DECL_NAME (templ);
   10264              :     }
   10265              : 
   10266              :   /* Issue an error message if we didn't find a template.  */
   10267   1086572940 :   if (! templ)
   10268              :     {
   10269         1471 :       if (complain & tf_error)
   10270            0 :         error ("%qT is not a template", d1);
   10271         1471 :       return error_mark_node;
   10272              :     }
   10273              : 
   10274   1086571469 :   if (TREE_CODE (templ) != TEMPLATE_DECL
   10275              :          /* Make sure it's a user visible template, if it was named by
   10276              :             the user.  */
   10277   1086571469 :       || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
   10278    183693504 :           && !PRIMARY_TEMPLATE_P (templ)))
   10279              :     {
   10280            0 :       if (complain & tf_error)
   10281              :         {
   10282            0 :           error ("non-template type %qT used as a template", d1);
   10283            0 :           if (in_decl)
   10284            0 :             error ("for template declaration %q+D", in_decl);
   10285              :         }
   10286            0 :       return error_mark_node;
   10287              :     }
   10288              : 
   10289   1086571469 :   complain &= ~tf_user;
   10290              : 
   10291              :   /* An alias that just changes the name of a template is equivalent to the
   10292              :      other template, so if any of the arguments are pack expansions, strip
   10293              :      the alias to avoid problems with a pack expansion passed to a non-pack
   10294              :      alias template parameter (DR 1430).  */
   10295   1086571469 :   if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
   10296      7132453 :     templ = get_underlying_template (templ);
   10297              : 
   10298   1086571469 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
   10299              :     {
   10300       339725 :       tree parm;
   10301       339725 :       tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
   10302       339725 :       if (arglist2 == error_mark_node
   10303       339725 :           || (!uses_template_parms (arglist2)
   10304        11781 :               && check_instantiated_args (templ, arglist2, complain)))
   10305           18 :         return error_mark_node;
   10306              : 
   10307       339707 :       parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
   10308       339707 :       return parm;
   10309              :     }
   10310              :   else
   10311              :     {
   10312   1086231744 :       tree template_type = TREE_TYPE (templ);
   10313   1086231744 :       tree gen_tmpl;
   10314   1086231744 :       tree type_decl;
   10315   1086231744 :       tree found = NULL_TREE;
   10316   1086231744 :       int arg_depth;
   10317   1086231744 :       int parm_depth;
   10318   1086231744 :       int is_dependent_type;
   10319   1086231744 :       int use_partial_inst_tmpl = false;
   10320              : 
   10321   1086231744 :       if (template_type == error_mark_node)
   10322              :         /* An error occurred while building the template TEMPL, and a
   10323              :            diagnostic has most certainly been emitted for that
   10324              :            already.  Let's propagate that error.  */
   10325              :         return error_mark_node;
   10326              : 
   10327   1086231741 :       gen_tmpl = most_general_template (templ);
   10328   1086231741 :       if (modules_p ())
   10329      3907682 :         lazy_load_pendings (gen_tmpl);
   10330              : 
   10331   1086231741 :       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
   10332   1086231741 :       parm_depth = TMPL_PARMS_DEPTH (parmlist);
   10333   2172463482 :       arg_depth = TMPL_ARGS_DEPTH (arglist);
   10334              : 
   10335   1086231741 :       if (arg_depth == 1 && parm_depth > 1)
   10336              :         {
   10337              :           /* We've been given an incomplete set of template arguments.
   10338              :              For example, given:
   10339              : 
   10340              :                template <class T> struct S1 {
   10341              :                  template <class U> struct S2 {};
   10342              :                  template <class U> struct S2<U*> {};
   10343              :                 };
   10344              : 
   10345              :              we will be called with an ARGLIST of `U*', but the
   10346              :              TEMPLATE will be `template <class T> template
   10347              :              <class U> struct S1<T>::S2'.  We must fill in the missing
   10348              :              arguments.  */
   10349      6520801 :           tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
   10350      6520801 :           arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
   10351     13041602 :           arg_depth = TMPL_ARGS_DEPTH (arglist);
   10352              :         }
   10353              : 
   10354              :       /* Now we should have enough arguments.  */
   10355   1086231741 :       gcc_assert (parm_depth == arg_depth);
   10356              : 
   10357   1086231741 :       if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
   10358              :         {
   10359              :           /* The user referred to a specialization of an alias
   10360              :             template represented by GEN_TMPL.
   10361              : 
   10362              :             [temp.alias]/2 says:
   10363              : 
   10364              :                 When a template-id refers to the specialization of an
   10365              :                 alias template, it is equivalent to the associated
   10366              :                 type obtained by substitution of its
   10367              :                 template-arguments for the template-parameters in the
   10368              :                 type-id of the alias template.  */
   10369              : 
   10370     39074033 :           t = instantiate_alias_template (gen_tmpl, arglist, complain);
   10371              :           /* Note that the call above (by indirectly calling
   10372              :              register_specialization in tsubst_decl) registers the
   10373              :              TYPE_DECL representing the specialization of the alias
   10374              :              template.  So next time someone substitutes ARGLIST for
   10375              :              the template parms into the alias template (GEN_TMPL),
   10376              :              she'll get that TYPE_DECL back.  */
   10377              : 
   10378     39074033 :           if (t == error_mark_node)
   10379              :             return error_mark_node;
   10380     39064032 :           return TREE_TYPE (t);
   10381              :         }
   10382              : 
   10383              :       /* From here on, we're only interested in the most general
   10384              :          template.  */
   10385              : 
   10386              :       /* Shortcut looking up the current class scope again.  */
   10387   1047157708 :       for (tree cur = current_nonlambda_class_type ();
   10388   1823795789 :            cur != NULL_TREE;
   10389    776638081 :            cur = get_containing_scope (cur))
   10390              :         {
   10391   1349109817 :           if (!CLASS_TYPE_P (cur))
   10392    531838377 :             continue;
   10393              : 
   10394    817271440 :           tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
   10395   2333596102 :           if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
   10396              :             break;
   10397              : 
   10398    758605526 :           if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
   10399   1290775555 :               && comp_template_args (arglist, TI_ARGS (ti)))
   10400              :             return cur;
   10401              :         }
   10402              : 
   10403              :       /* Calculate the BOUND_ARGS.  These will be the args that are
   10404              :          actually tsubst'd into the definition to create the
   10405              :          instantiation.  */
   10406    533351886 :       if (PRIMARY_TEMPLATE_P (gen_tmpl))
   10407    527156822 :         arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
   10408              : 
   10409    533351883 :       if (arglist == error_mark_node)
   10410              :         /* We were unable to bind the arguments.  */
   10411              :         return error_mark_node;
   10412              : 
   10413              :       /* In the scope of a template class, explicit references to the
   10414              :          template class refer to the type of the template, not any
   10415              :          instantiation of it.  For example, in:
   10416              : 
   10417              :            template <class T> class C { void f(C<T>); }
   10418              : 
   10419              :          the `C<T>' is just the same as `C'.  Outside of the
   10420              :          class, however, such a reference is an instantiation.
   10421              :          One can use adjust_type_for_entering_scope to make
   10422              :          this adjustment as needed.  */
   10423    533350179 :       if (!PRIMARY_TEMPLATE_P (gen_tmpl)
   10424    533350179 :           || currently_open_class (template_type))
   10425              :         {
   10426      9839520 :           tree tinfo = TYPE_TEMPLATE_INFO (template_type);
   10427              : 
   10428     19679040 :           if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
   10429              :             return template_type;
   10430              :         }
   10431              : 
   10432              :       /* If we already have this specialization, return it.  */
   10433    533310899 :       spec_entry elt;
   10434    533310899 :       elt.tmpl = gen_tmpl;
   10435    533310899 :       elt.args = arglist;
   10436    533310899 :       entry = type_specializations->find (&elt);
   10437              : 
   10438    533310899 :       if (entry)
   10439    425015848 :         return entry->spec;
   10440              : 
   10441              :       /* If the template's constraints are not satisfied,
   10442              :          then we cannot form a valid type.
   10443              : 
   10444              :          Note that the check is deferred until after the hash
   10445              :          lookup. This prevents redundant checks on previously
   10446              :          instantiated specializations. */
   10447    108295051 :       if (flag_concepts
   10448    108295051 :           && !constraints_satisfied_p (gen_tmpl, arglist))
   10449              :         {
   10450          745 :           if (complain & tf_error)
   10451              :             {
   10452           86 :               auto_diagnostic_group d;
   10453           86 :               error ("template constraint failure for %qD", gen_tmpl);
   10454           86 :               diagnose_constraints (input_location, gen_tmpl, arglist);
   10455           86 :             }
   10456          745 :           return error_mark_node;
   10457              :         }
   10458              : 
   10459    108294306 :       is_dependent_type = uses_template_parms (arglist);
   10460              : 
   10461              :       /* If the deduced arguments are invalid, then the binding
   10462              :          failed.  */
   10463    108294306 :       if (!is_dependent_type
   10464    108294306 :           && check_instantiated_args (gen_tmpl,
   10465              :                                       INNERMOST_TEMPLATE_ARGS (arglist),
   10466              :                                       complain))
   10467            8 :         return error_mark_node;
   10468              : 
   10469    108294298 :       if (!is_dependent_type
   10470     49431218 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   10471      3172592 :           && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
   10472    110103913 :           && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
   10473              :         /* This occurs when the user has tried to define a tagged type
   10474              :            in a scope that forbids it.  We emitted an error during the
   10475              :            parse.  We didn't complete the bail out then, so here we
   10476              :            are.  */
   10477           12 :         return error_mark_node;
   10478              : 
   10479    108294286 :       context = DECL_CONTEXT (gen_tmpl);
   10480    108294286 :       if (context && TYPE_P (context))
   10481              :         {
   10482      5280109 :           if (!uses_template_parms (DECL_CONTEXT (templ)))
   10483              :             /* If the context of the partially instantiated template is
   10484              :                already non-dependent, then we might as well use it.  */
   10485      1786611 :             context = DECL_CONTEXT (templ);
   10486              :           else
   10487              :             {
   10488      3493498 :               context = tsubst_entering_scope (context, arglist,
   10489              :                                                complain, in_decl);
   10490              :               /* Try completing the enclosing context if it's not already so.  */
   10491      3493498 :               if (context != error_mark_node
   10492      3493498 :                   && !COMPLETE_TYPE_P (context))
   10493              :                 {
   10494      3190995 :                   context = complete_type (context);
   10495      3190995 :                   if (COMPLETE_TYPE_P (context))
   10496              :                     {
   10497              :                       /* Completion could have caused us to register the desired
   10498              :                          specialization already, so check the table again.  */
   10499            3 :                       entry = type_specializations->find (&elt);
   10500            3 :                       if (entry)
   10501            3 :                         return entry->spec;
   10502              :                     }
   10503              :                 }
   10504              :             }
   10505              :         }
   10506              :       else
   10507    103014177 :         context = tsubst (context, arglist, complain, in_decl);
   10508              : 
   10509    108294283 :       if (context == error_mark_node)
   10510              :         return error_mark_node;
   10511              : 
   10512    108294280 :       if (!context)
   10513            0 :         context = global_namespace;
   10514              : 
   10515              :       /* Create the type.  */
   10516    108294280 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE)
   10517              :         {
   10518       361949 :           if (!is_dependent_type)
   10519              :             {
   10520       361946 :               set_current_access_from_decl (TYPE_NAME (template_type));
   10521       723892 :               t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
   10522       361946 :                               tsubst (ENUM_UNDERLYING_TYPE (template_type),
   10523              :                                       arglist, complain, in_decl),
   10524       361946 :                               tsubst_attributes (TYPE_ATTRIBUTES (template_type),
   10525              :                                                  arglist, complain, in_decl),
   10526       361946 :                               SCOPED_ENUM_P (template_type), NULL);
   10527              : 
   10528       361946 :               if (t == error_mark_node)
   10529              :                 return t;
   10530              :             }
   10531              :           else
   10532              :             {
   10533              :               /* We don't want to call start_enum for this type, since
   10534              :                  the values for the enumeration constants may involve
   10535              :                  template parameters.  And, no one should be interested
   10536              :                  in the enumeration constants for such a type.  */
   10537            3 :               t = cxx_make_type (ENUMERAL_TYPE);
   10538            6 :               SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
   10539              :             }
   10540       723853 :           SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
   10541       723886 :           ENUM_FIXED_UNDERLYING_TYPE_P (t)
   10542       361943 :             = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
   10543              :         }
   10544    107932331 :       else if (CLASS_TYPE_P (template_type))
   10545              :         {
   10546              :           /* Lambda closures are regenerated in tsubst_lambda_expr, not
   10547              :              instantiated here.  */
   10548    215711583 :           gcc_assert (!LAMBDA_TYPE_P (template_type));
   10549              : 
   10550    107932331 :           t = make_class_type (TREE_CODE (template_type));
   10551    323796993 :           CLASSTYPE_DECLARED_CLASS (t)
   10552    107932331 :             = CLASSTYPE_DECLARED_CLASS (template_type);
   10553    107932331 :           SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
   10554              : 
   10555              :           /* A local class.  Make sure the decl gets registered properly.  */
   10556    107932331 :           if (context == current_function_decl)
   10557       568224 :             if (pushtag (DECL_NAME (gen_tmpl), t)
   10558       568224 :                 == error_mark_node)
   10559              :               return error_mark_node;
   10560              : 
   10561    107932319 :           if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
   10562              :             /* This instantiation is another name for the primary
   10563              :                template type. Set the TYPE_CANONICAL field
   10564              :                appropriately. */
   10565      5981469 :             TYPE_CANONICAL (t) = template_type;
   10566    101950850 :           else if (any_template_arguments_need_structural_equality_p (arglist))
   10567      1667292 :             SET_TYPE_STRUCTURAL_EQUALITY (t);
   10568              :         }
   10569              :       else
   10570            0 :         gcc_unreachable ();
   10571              : 
   10572              :       /* If we called start_enum or pushtag above, this information
   10573              :          will already be set up.  */
   10574    108294262 :       type_decl = TYPE_NAME (t);
   10575    108294262 :       if (!type_decl)
   10576              :         {
   10577    107364110 :           TYPE_CONTEXT (t) = FROB_CONTEXT (context);
   10578              : 
   10579    107364110 :           type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
   10580    107364110 :           DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
   10581    214728220 :           DECL_SOURCE_LOCATION (type_decl)
   10582    107364110 :             = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
   10583              :         }
   10584              : 
   10585    108294262 :       set_instantiating_module (type_decl);
   10586              :       /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
   10587              :          of export flag.  We want to propagate this because it might
   10588              :          be a friend declaration that pushes a new hidden binding.  */
   10589    108294262 :       DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
   10590              : 
   10591    108294262 :       if (CLASS_TYPE_P (template_type))
   10592              :         {
   10593    107932319 :           TREE_PRIVATE (type_decl)
   10594    107932319 :             = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
   10595    107932319 :           TREE_PROTECTED (type_decl)
   10596    107932319 :             = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
   10597    107932319 :           if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
   10598              :             {
   10599    106931155 :               DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
   10600    106931155 :               DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
   10601              :             }
   10602              :         }
   10603              : 
   10604    108294262 :       if (OVERLOAD_TYPE_P (t))
   10605              :         {
   10606              :           static const char *tags[] = {"abi_tag", "may_alias"};
   10607              : 
   10608    324882786 :           for (unsigned ix = 0; ix != 2; ix++)
   10609              :             {
   10610    216588524 :               tree attributes
   10611    216588524 :                 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
   10612              : 
   10613    216588524 :               if (attributes)
   10614           48 :                 TYPE_ATTRIBUTES (t)
   10615           96 :                   = tree_cons (TREE_PURPOSE (attributes),
   10616           48 :                                TREE_VALUE (attributes),
   10617           48 :                                TYPE_ATTRIBUTES (t));
   10618              :             }
   10619              :         }
   10620              : 
   10621              :       /* Let's consider the explicit specialization of a member
   10622              :          of a class template specialization that is implicitly instantiated,
   10623              :          e.g.:
   10624              :              template<class T>
   10625              :              struct S
   10626              :              {
   10627              :                template<class U> struct M {}; //#0
   10628              :              };
   10629              : 
   10630              :              template<>
   10631              :              template<>
   10632              :              struct S<int>::M<char> //#1
   10633              :              {
   10634              :                int i;
   10635              :              };
   10636              :         [temp.expl.spec]/4 says this is valid.
   10637              : 
   10638              :         In this case, when we write:
   10639              :         S<int>::M<char> m;
   10640              : 
   10641              :         M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
   10642              :         the one of #0.
   10643              : 
   10644              :         When we encounter #1, we want to store the partial instantiation
   10645              :         of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
   10646              : 
   10647              :         For all cases other than this "explicit specialization of member of a
   10648              :         class template", we just want to store the most general template into
   10649              :         the CLASSTYPE_TI_TEMPLATE of M.
   10650              : 
   10651              :         This case of "explicit specialization of member of a class template"
   10652              :         only happens when:
   10653              :         1/ the enclosing class is an instantiation of, and therefore not
   10654              :         the same as, the context of the most general template, and
   10655              :         2/ we aren't looking at the partial instantiation itself, i.e.
   10656              :         the innermost arguments are not the same as the innermost parms of
   10657              :         the most general template.
   10658              : 
   10659              :         So it's only when 1/ and 2/ happens that we want to use the partial
   10660              :         instantiation of the member template in lieu of its most general
   10661              :         template.  */
   10662              : 
   10663    108294262 :       if (PRIMARY_TEMPLATE_P (gen_tmpl)
   10664    212969312 :           && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
   10665              :           /* the enclosing class must be an instantiation...  */
   10666      2408278 :           && CLASS_TYPE_P (context)
   10667    110702273 :           && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
   10668              :         {
   10669      1341479 :           TREE_VEC_LENGTH (arglist)--;
   10670      1341479 :           ++processing_template_decl;
   10671      1341479 :           tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl));
   10672      1341479 :           tree partial_inst_args =
   10673      1341479 :             tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
   10674              :                     arglist, complain, NULL_TREE);
   10675      1341479 :           --processing_template_decl;
   10676      1341479 :           TREE_VEC_LENGTH (arglist)++;
   10677      1341479 :           if (partial_inst_args == error_mark_node)
   10678              :             return error_mark_node;
   10679      2682952 :           use_partial_inst_tmpl =
   10680              :             /*...and we must not be looking at the partial instantiation
   10681              :              itself. */
   10682      1341476 :             !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
   10683              :                                  partial_inst_args);
   10684              :         }
   10685              : 
   10686      1341476 :       if (!use_partial_inst_tmpl)
   10687              :         /* This case is easy; there are no member templates involved.  */
   10688              :         found = gen_tmpl;
   10689              :       else
   10690              :         {
   10691              :           /* This is a full instantiation of a member template.  Find
   10692              :              the partial instantiation of which this is an instance.  */
   10693              : 
   10694              :           /* Temporarily reduce by one the number of levels in the ARGLIST
   10695              :              so as to avoid comparing the last set of arguments.  */
   10696       760551 :           TREE_VEC_LENGTH (arglist)--;
   10697              :           /* We don't use COMPLAIN in the following call because this isn't
   10698              :              the immediate context of deduction.  For instance, tf_partial
   10699              :              could be set here as we might be at the beginning of template
   10700              :              argument deduction when any explicitly specified template
   10701              :              arguments are substituted into the function type.  tf_partial
   10702              :              could lead into trouble because we wouldn't find the partial
   10703              :              instantiation that might have been created outside tf_partial
   10704              :              context, because the levels of template parameters wouldn't
   10705              :              match, because in a tf_partial context, tsubst doesn't reduce
   10706              :              TEMPLATE_PARM_LEVEL.  */
   10707       760551 :           found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
   10708       760551 :           TREE_VEC_LENGTH (arglist)++;
   10709       760551 :           found = (TREE_CODE (found) == TEMPLATE_DECL
   10710       760551 :                    ? found
   10711            0 :                    : CLASSTYPE_TI_TEMPLATE (found));
   10712              : 
   10713       760551 :           if (DECL_CLASS_TEMPLATE_P (found)
   10714      1521102 :               && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
   10715              :             {
   10716              :               /* If this partial instantiation is specialized, we want to
   10717              :                  use it for hash table lookup.  */
   10718            9 :               elt.tmpl = found;
   10719            9 :               elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
   10720            9 :               elt.hash = 0; /* Recalculate after changing tmpl/args.  */
   10721              :             }
   10722              :         }
   10723              : 
   10724              :       /* Build template info for the new specialization.  */
   10725    108294259 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
   10726              : 
   10727    108294259 :       elt.spec = t;
   10728    108294259 :       slot = type_specializations->find_slot (&elt, INSERT);
   10729    108294259 :       gcc_checking_assert (*slot == NULL);
   10730    108294259 :       entry = ggc_alloc<spec_entry> ();
   10731    108294259 :       *entry = elt;
   10732    108294259 :       *slot = entry;
   10733              : 
   10734              :       /* Note this use of the partial instantiation so we can check it
   10735              :          later in maybe_process_partial_specialization.  */
   10736    216588518 :       DECL_TEMPLATE_INSTANTIATIONS (found)
   10737    108294259 :         = tree_cons (arglist, t,
   10738    108294259 :                      DECL_TEMPLATE_INSTANTIATIONS (found));
   10739              : 
   10740    108294259 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE
   10741    108294259 :           && !uses_template_parms (current_nonlambda_scope ()))
   10742              :         /* Now that the type has been registered on the instantiations
   10743              :            list, we set up the enumerators.  Because the enumeration
   10744              :            constants may involve the enumeration type itself, we make
   10745              :            sure to register the type first, and then create the
   10746              :            constants.  That way, doing tsubst_expr for the enumeration
   10747              :            constants won't result in recursive calls here; we'll find
   10748              :            the instantiation and exit above.  */
   10749       361943 :         tsubst_enum (template_type, t, arglist);
   10750              : 
   10751    108294214 :       if (CLASS_TYPE_P (template_type) && is_dependent_type)
   10752              :         /* If the type makes use of template parameters, the
   10753              :            code that generates debugging information will crash.  */
   10754     58863068 :         DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
   10755              : 
   10756              :       /* Possibly limit visibility based on template args.  */
   10757    108294214 :       TREE_PUBLIC (type_decl) = 1;
   10758    108294214 :       determine_visibility (type_decl);
   10759              : 
   10760    108294214 :       inherit_targ_abi_tags (t);
   10761              : 
   10762    108294214 :       return t;
   10763              :     }
   10764   1086572892 : }
   10765              : 
   10766              : /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.  */
   10767              : 
   10768              : tree
   10769     34346117 : lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
   10770              : {
   10771     34346117 :   tree gen_templ = most_general_template (templ);
   10772     34346117 :   tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
   10773     34346117 :   arglist = add_outermost_template_args (templ, arglist);
   10774     34346117 :   arglist = coerce_template_parms (parms, arglist, templ, complain);
   10775     34346117 :   if (arglist == error_mark_node)
   10776              :     return error_mark_node;
   10777              : 
   10778              :   /* The type of the expression is NULL_TREE since the template-id could refer
   10779              :      to an explicit or partial specialization. */
   10780     34346057 :   return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
   10781              : }
   10782              : 
   10783              : /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
   10784              :    not dependent.  */
   10785              : 
   10786              : tree
   10787     32575709 : finish_template_variable (tree var, tsubst_flags_t complain)
   10788              : {
   10789     32575709 :   tree templ = TREE_OPERAND (var, 0);
   10790     32575709 :   tree arglist = TREE_OPERAND (var, 1);
   10791              : 
   10792              :   /* If the template or arguments are dependent, then we
   10793              :      can't resolve the TEMPLATE_ID_EXPR yet.  */
   10794     32575709 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
   10795     32575709 :       || any_dependent_template_arguments_p (arglist))
   10796     12149310 :     return var;
   10797              : 
   10798     20426399 :   if (flag_concepts && !constraints_satisfied_p (templ, arglist))
   10799              :     {
   10800           21 :       if (complain & tf_error)
   10801              :         {
   10802           18 :           auto_diagnostic_group d;
   10803           18 :           error ("use of invalid variable template %qE", var);
   10804           18 :           diagnose_constraints (location_of (var), templ, arglist);
   10805           18 :         }
   10806           21 :       return error_mark_node;
   10807              :     }
   10808              : 
   10809     20426378 :   return instantiate_template (templ, arglist, complain);
   10810              : }
   10811              : 
   10812              : /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
   10813              :    TARGS template args, and instantiate it if it's not dependent.  */
   10814              : 
   10815              : tree
   10816     21040669 : lookup_and_finish_template_variable (tree templ, tree targs,
   10817              :                                      tsubst_flags_t complain)
   10818              : {
   10819     21040669 :   tree var = lookup_template_variable (templ, targs, complain);
   10820     21040669 :   if (var == error_mark_node)
   10821              :     return error_mark_node;
   10822     21040661 :   var = finish_template_variable (var, complain);
   10823     21040661 :   mark_used (var, complain);
   10824     21040661 :   return var;
   10825              : }
   10826              : 
   10827              : /* If the set of template parameters PARMS contains a template parameter
   10828              :    at the given LEVEL and INDEX, then return this parameter.  Otherwise
   10829              :    return NULL_TREE.  */
   10830              : 
   10831              : static tree
   10832     72995947 : corresponding_template_parameter_list (tree parms, int level, int index)
   10833              : {
   10834     78165422 :   while (TMPL_PARMS_DEPTH (parms) > level)
   10835      5169475 :     parms = TREE_CHAIN (parms);
   10836              : 
   10837     72995947 :   if (TMPL_PARMS_DEPTH (parms) != level
   10838     72995947 :       || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
   10839              :     return NULL_TREE;
   10840              : 
   10841     71156333 :   return TREE_VEC_ELT (TREE_VALUE (parms), index);
   10842              : }
   10843              : 
   10844              : /* Return the TREE_LIST for the template parameter from PARMS that positionally
   10845              :    corresponds to the template parameter PARM, or else return NULL_TREE.  */
   10846              : 
   10847              : static tree
   10848     72995947 : corresponding_template_parameter_list (tree parms, tree parm)
   10849              : {
   10850     72995947 :   int level, index;
   10851     72995947 :   template_parm_level_and_index (parm, &level, &index);
   10852     72995947 :   return corresponding_template_parameter_list (parms, level, index);
   10853              : }
   10854              : 
   10855              : /* As above, but pull out the actual parameter.  */
   10856              : 
   10857              : static tree
   10858     72994946 : corresponding_template_parameter (tree parms, tree parm)
   10859              : {
   10860     72994946 :   tree list = corresponding_template_parameter_list (parms, parm);
   10861     72994946 :   if (!list)
   10862              :     return NULL_TREE;
   10863              : 
   10864     71155332 :   tree t = TREE_VALUE (list);
   10865              :   /* As in template_parm_to_arg.  */
   10866     71155332 :   if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
   10867     69425052 :     t = TREE_TYPE (t);
   10868              :   else
   10869      1730280 :     t = DECL_INITIAL (t);
   10870              : 
   10871     71155332 :   gcc_assert (TEMPLATE_PARM_P (t));
   10872              :   return t;
   10873              : }
   10874              : 
   10875              : struct pair_fn_data
   10876              : {
   10877              :   tree_fn_t fn;
   10878              :   tree_fn_t any_fn;
   10879              :   void *data;
   10880              :   /* True when we should also visit template parameters that occur in
   10881              :      non-deduced contexts.  */
   10882              :   bool include_nondeduced_p;
   10883              :   hash_set<tree> *visited;
   10884              : };
   10885              : 
   10886              : /* Called from for_each_template_parm via walk_tree.  */
   10887              : 
   10888              : static tree
   10889    896565205 : for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
   10890              : {
   10891    896565205 :   tree t = *tp;
   10892    896565205 :   struct pair_fn_data *pfd = (struct pair_fn_data *) d;
   10893    896565205 :   tree_fn_t fn = pfd->fn;
   10894    896565205 :   void *data = pfd->data;
   10895    896565205 :   tree result = NULL_TREE;
   10896              : 
   10897              : #define WALK_SUBTREE(NODE)                                              \
   10898              :   do                                                                    \
   10899              :     {                                                                   \
   10900              :       result = for_each_template_parm (NODE, fn, data, pfd->visited, \
   10901              :                                        pfd->include_nondeduced_p,    \
   10902              :                                        pfd->any_fn);                 \
   10903              :       if (result) goto out;                                             \
   10904              :     }                                                                   \
   10905              :   while (0)
   10906              : 
   10907    896565205 :   if (pfd->any_fn && (*pfd->any_fn)(t, data))
   10908              :     return t;
   10909              : 
   10910    896565205 :   if (TYPE_P (t)
   10911    378593645 :       && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
   10912    378214151 :     WALK_SUBTREE (TYPE_CONTEXT (t));
   10913              : 
   10914    896416903 :   switch (TREE_CODE (t))
   10915              :     {
   10916     67265865 :     case RECORD_TYPE:
   10917     67265865 :       if (TYPE_PTRMEMFUNC_P (t))
   10918              :         break;
   10919              :       /* Fall through.  */
   10920              : 
   10921     67784742 :     case UNION_TYPE:
   10922     67784742 :     case ENUMERAL_TYPE:
   10923     67784742 :       if (!TYPE_TEMPLATE_INFO (t))
   10924      7450673 :         *walk_subtrees = 0;
   10925              :       else
   10926     60334069 :         WALK_SUBTREE (TYPE_TI_ARGS (t));
   10927              :       break;
   10928              : 
   10929     10195459 :     case METHOD_TYPE:
   10930              :       /* Since we're not going to walk subtrees, we have to do this
   10931              :          explicitly here.  */
   10932     10195459 :       WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
   10933              :       /* Fall through.  */
   10934              : 
   10935      2223843 :     case FUNCTION_TYPE:
   10936              :       /* Check the return type.  */
   10937      2223843 :       WALK_SUBTREE (TREE_TYPE (t));
   10938              : 
   10939              :       /* Check the parameter types.  Since default arguments are not
   10940              :          instantiated until they are needed, the TYPE_ARG_TYPES may
   10941              :          contain expressions that involve template parameters.  But,
   10942              :          no-one should be looking at them yet.  And, once they're
   10943              :          instantiated, they don't contain template parameters, so
   10944              :          there's no point in looking at them then, either.  */
   10945      2018021 :       {
   10946      2018021 :         tree parm;
   10947              : 
   10948      6709360 :         for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
   10949      4730909 :           WALK_SUBTREE (TREE_VALUE (parm));
   10950              : 
   10951              :         /* Since we've already handled the TYPE_ARG_TYPES, we don't
   10952              :            want walk_tree walking into them itself.  */
   10953      1978451 :         *walk_subtrees = 0;
   10954              :       }
   10955              : 
   10956      1978451 :       if (flag_noexcept_type)
   10957              :         {
   10958      1972186 :           tree spec = TYPE_RAISES_EXCEPTIONS (t);
   10959      1972186 :           if (spec)
   10960       523364 :             WALK_SUBTREE (TREE_PURPOSE (spec));
   10961              :         }
   10962              :       break;
   10963              : 
   10964     10964752 :     case TYPEOF_TYPE:
   10965     10964752 :     case DECLTYPE_TYPE:
   10966     10964752 :       if (pfd->include_nondeduced_p
   10967     10964752 :           && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
   10968              :                                      pfd->visited,
   10969              :                                      pfd->include_nondeduced_p,
   10970              :                                      pfd->any_fn))
   10971         1760 :         return error_mark_node;
   10972     10962992 :       *walk_subtrees = false;
   10973     10962992 :       break;
   10974              : 
   10975            6 :     case TRAIT_TYPE:
   10976            6 :       if (pfd->include_nondeduced_p)
   10977              :         {
   10978            0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
   10979            0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
   10980              :         }
   10981            6 :       *walk_subtrees = false;
   10982            6 :       break;
   10983              : 
   10984      1255260 :     case FUNCTION_DECL:
   10985      1255260 :     case VAR_DECL:
   10986      1255260 :       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   10987       205411 :         WALK_SUBTREE (DECL_TI_ARGS (t));
   10988              :       break;
   10989              : 
   10990      6433882 :     case PARM_DECL:
   10991      6433882 :       WALK_SUBTREE (TREE_TYPE (t));
   10992              :       break;
   10993              : 
   10994       442818 :     case CONST_DECL:
   10995       442818 :       if (DECL_TEMPLATE_PARM_P (t))
   10996            0 :         WALK_SUBTREE (DECL_INITIAL (t));
   10997       442818 :       if (DECL_CONTEXT (t)
   10998       442818 :           && pfd->include_nondeduced_p)
   10999        41995 :         WALK_SUBTREE (DECL_CONTEXT (t));
   11000              :       break;
   11001              : 
   11002       276082 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   11003              :       /* Record template parameters such as `T' inside `TT<T>'.  */
   11004       276082 :       WALK_SUBTREE (TYPE_TI_ARGS (t));
   11005              :       /* Fall through.  */
   11006              : 
   11007    135812193 :     case TEMPLATE_TEMPLATE_PARM:
   11008    135812193 :     case TEMPLATE_TYPE_PARM:
   11009    135812193 :     case TEMPLATE_PARM_INDEX:
   11010    135812193 :       if (fn && (*fn)(t, data))
   11011              :         return t;
   11012    125261855 :       else if (!fn)
   11013              :         return t;
   11014              :       break;
   11015              : 
   11016     33962791 :     case TEMPLATE_DECL:
   11017              :       /* A template template parameter is encountered.  */
   11018     33962791 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   11019           20 :         WALK_SUBTREE (TREE_TYPE (t));
   11020              : 
   11021              :       /* Already substituted template template parameter */
   11022     33962791 :       *walk_subtrees = 0;
   11023     33962791 :       break;
   11024              : 
   11025     40608743 :     case TYPENAME_TYPE:
   11026              :       /* A template-id in a TYPENAME_TYPE might be a deduced context after
   11027              :          partial instantiation.  */
   11028     40608743 :       WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
   11029     40608743 :       *walk_subtrees = 0;
   11030     40608743 :       break;
   11031              : 
   11032     18094254 :     case INDIRECT_REF:
   11033     18094254 :     case COMPONENT_REF:
   11034              :       /* If there's no type, then this thing must be some expression
   11035              :          involving template parameters.  */
   11036     18094254 :       if (!fn && !TREE_TYPE (t))
   11037            0 :         return error_mark_node;
   11038              :       break;
   11039              : 
   11040      1545458 :     case CONSTRUCTOR:
   11041      1545458 :     case TRAIT_EXPR:
   11042      1545458 :     case PLUS_EXPR:
   11043      1545458 :     case MULT_EXPR:
   11044      1545458 :     case SCOPE_REF:
   11045              :       /* These are non-deduced contexts.  */
   11046      1545458 :       if (!pfd->include_nondeduced_p)
   11047        84998 :         *walk_subtrees = 0;
   11048              :       break;
   11049              : 
   11050      8193486 :     case MODOP_EXPR:
   11051      8193486 :     case CAST_EXPR:
   11052      8193486 :     case IMPLICIT_CONV_EXPR:
   11053      8193486 :     case REINTERPRET_CAST_EXPR:
   11054      8193486 :     case CONST_CAST_EXPR:
   11055      8193486 :     case STATIC_CAST_EXPR:
   11056      8193486 :     case DYNAMIC_CAST_EXPR:
   11057      8193486 :     case ARROW_EXPR:
   11058      8193486 :     case DOTSTAR_EXPR:
   11059      8193486 :     case TYPEID_EXPR:
   11060      8193486 :     case PSEUDO_DTOR_EXPR:
   11061      8193486 :       if (!fn)
   11062            0 :         return error_mark_node;
   11063              :       break;
   11064              : 
   11065           10 :     case SPLICE_SCOPE:
   11066           10 :       WALK_SUBTREE (SPLICE_SCOPE_EXPR (t));
   11067           10 :       *walk_subtrees = 0;
   11068           10 :       break;
   11069              : 
   11070              :     default:
   11071              :       break;
   11072              :     }
   11073              : 
   11074              :   #undef WALK_SUBTREE
   11075              : 
   11076              :   /* We didn't find any template parameters we liked.  */
   11077              :  out:
   11078              :   return result;
   11079              : }
   11080              : 
   11081              : /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
   11082              :    BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
   11083              :    call FN with the parameter and the DATA.
   11084              :    If FN returns nonzero, the iteration is terminated, and
   11085              :    for_each_template_parm returns 1.  Otherwise, the iteration
   11086              :    continues.  If FN never returns a nonzero value, the value
   11087              :    returned by for_each_template_parm is 0.  If FN is NULL, it is
   11088              :    considered to be the function which always returns 1.
   11089              : 
   11090              :    If INCLUDE_NONDEDUCED_P, then this routine will also visit template
   11091              :    parameters that occur in non-deduced contexts.  When false, only
   11092              :    visits those template parameters that can be deduced.  */
   11093              : 
   11094              : static tree
   11095    747528143 : for_each_template_parm (tree t, tree_fn_t fn, void* data,
   11096              :                         hash_set<tree> *visited,
   11097              :                         bool include_nondeduced_p,
   11098              :                         tree_fn_t any_fn)
   11099              : {
   11100    747528143 :   struct pair_fn_data pfd;
   11101    747528143 :   tree result;
   11102              : 
   11103              :   /* Set up.  */
   11104    747528143 :   pfd.fn = fn;
   11105    747528143 :   pfd.any_fn = any_fn;
   11106    747528143 :   pfd.data = data;
   11107    747528143 :   pfd.include_nondeduced_p = include_nondeduced_p;
   11108              : 
   11109              :   /* Walk the tree.  (Conceptually, we would like to walk without
   11110              :      duplicates, but for_each_template_parm_r recursively calls
   11111              :      for_each_template_parm, so we would need to reorganize a fair
   11112              :      bit to use walk_tree_without_duplicates, so we keep our own
   11113              :      visited list.)  */
   11114    747528143 :   if (visited)
   11115    713611245 :     pfd.visited = visited;
   11116              :   else
   11117     33916898 :     pfd.visited = new hash_set<tree>;
   11118    747528143 :   result = cp_walk_tree (&t,
   11119              :                          for_each_template_parm_r,
   11120              :                          &pfd,
   11121              :                          pfd.visited);
   11122              : 
   11123              :   /* Clean up.  */
   11124    747528143 :   if (!visited)
   11125              :     {
   11126     67833796 :       delete pfd.visited;
   11127     33916898 :       pfd.visited = 0;
   11128              :     }
   11129              : 
   11130    747528143 :   return result;
   11131              : }
   11132              : 
   11133     43230571 : struct find_template_parameter_info
   11134              : {
   11135     43230571 :   explicit find_template_parameter_info (tree ctx_parms)
   11136     86461142 :     : ctx_parms (ctx_parms),
   11137     43230571 :       max_depth (TMPL_PARMS_DEPTH (ctx_parms))
   11138     43230571 :   {}
   11139              : 
   11140              :   hash_set<tree> visited;
   11141              :   hash_set<tree> parms;
   11142              :   tree parm_list = NULL_TREE;
   11143              :   tree *parm_list_tail = &parm_list;
   11144              :   tree ctx_parms;
   11145              :   int max_depth;
   11146              : 
   11147              :   tree find_in (tree);
   11148              :   tree find_in_recursive (tree);
   11149              :   bool found (tree);
   11150          534 :   unsigned num_found () { return parms.elements (); }
   11151              : };
   11152              : 
   11153              : /* Appends the declaration of T to the list in DATA.  */
   11154              : 
   11155              : static int
   11156     80312618 : keep_template_parm (tree t, void* data)
   11157              : {
   11158     80312618 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   11159              : 
   11160              :   /* Template parameters declared within the expression are not part of
   11161              :      the parameter mapping. For example, in this concept:
   11162              : 
   11163              :        template<typename T>
   11164              :        concept C = requires { <expr> } -> same_as<int>;
   11165              : 
   11166              :      the return specifier same_as<int> declares a new decltype parameter
   11167              :      that must not be part of the parameter mapping. The same is true
   11168              :      for generic lambda parameters, lambda template parameters, etc.  */
   11169     80312618 :   int level;
   11170     80312618 :   int index;
   11171     80312618 :   template_parm_level_and_index (t, &level, &index);
   11172     80312618 :   if (level == 0 || level > ftpi->max_depth)
   11173              :     return 0;
   11174              : 
   11175     72994946 :   if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
   11176              :     /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
   11177              :        BOUND_TEMPLATE_TEMPLATE_PARM itself.  */
   11178       215757 :     t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
   11179              : 
   11180              :   /* This template parameter might be an argument to a cached dependent
   11181              :      specalization that was formed earlier inside some other template, in
   11182              :      which case the parameter is not among the ones that are in-scope.
   11183              :      Look in CTX_PARMS to find the corresponding in-scope template
   11184              :      parameter, and use it instead.  */
   11185     72994946 :   if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
   11186     71155332 :     t = in_scope;
   11187              : 
   11188              :   /* Arguments like const T yield parameters like const T. This means that
   11189              :      a template-id like X<T, const T> would yield two distinct parameters:
   11190              :      T and const T. Adjust types to their unqualified versions.  */
   11191     72994946 :   if (TYPE_P (t))
   11192     71264666 :     t = TYPE_MAIN_VARIANT (t);
   11193     72994946 :   if (!ftpi->parms.add (t))
   11194              :     {
   11195              :       /* Append T to PARM_LIST.  */
   11196     64767827 :       tree node = build_tree_list (NULL_TREE, t);
   11197     64767827 :       *ftpi->parm_list_tail = node;
   11198     64767827 :       ftpi->parm_list_tail = &TREE_CHAIN (node);
   11199              :     }
   11200              : 
   11201              :   /* Verify the parameter we found has a valid index.  */
   11202     72994946 :   if (flag_checking)
   11203              :     {
   11204     72994946 :       tree parms = ftpi->ctx_parms;
   11205     78164421 :       while (TMPL_PARMS_DEPTH (parms) > level)
   11206      5169475 :         parms = TREE_CHAIN (parms);
   11207     72994946 :       if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
   11208     71155332 :         gcc_assert (index < len);
   11209              :     }
   11210              : 
   11211              :   return 0;
   11212              : }
   11213              : 
   11214              : /* Ensure that we recursively examine certain terms that are not normally
   11215              :    visited in for_each_template_parm_r.  */
   11216              : 
   11217              : static int
   11218    613524764 : any_template_parm_r (tree t, void *data)
   11219              : {
   11220    613524764 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   11221              : 
   11222              : #define WALK_SUBTREE(NODE)                                              \
   11223              :   do                                                                    \
   11224              :     {                                                                   \
   11225              :       for_each_template_parm (NODE, keep_template_parm, data,           \
   11226              :                               &ftpi->visited, true,                      \
   11227              :                               any_template_parm_r);                     \
   11228              :     }                                                                   \
   11229              :   while (0)
   11230              : 
   11231              :   /* A mention of a member alias/typedef is a use of all of its template
   11232              :      arguments, including those from the enclosing class, so we don't use
   11233              :      alias_template_specialization_p here.  */
   11234    613524764 :   if (TYPE_P (t) && typedef_variant_p (t))
   11235      9509957 :     if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
   11236      9509864 :       WALK_SUBTREE (TI_ARGS (tinfo));
   11237              : 
   11238    613524764 :   switch (TREE_CODE (t))
   11239              :     {
   11240     78279350 :     case TEMPLATE_TYPE_PARM:
   11241              :       /* Type constraints of a placeholder type may contain parameters.  */
   11242     78279350 :       if (is_auto (t))
   11243      9157220 :         if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
   11244      9134238 :           WALK_SUBTREE (constr);
   11245              :       break;
   11246              : 
   11247     37512452 :     case TEMPLATE_ID_EXPR:
   11248              :       /* Search through references to variable templates.  */
   11249     37512452 :       WALK_SUBTREE (TREE_OPERAND (t, 0));
   11250     37512452 :       WALK_SUBTREE (TREE_OPERAND (t, 1));
   11251     37512452 :       break;
   11252              : 
   11253              :     case TEMPLATE_PARM_INDEX:
   11254              :       /* No need to consider template parameters within the type of an NTTP:
   11255              :          substitution into an NTTP is done directly with the corresponding
   11256              :          template argument, and its type only comes into play earlier during
   11257              :          coercion.  */
   11258              :       break;
   11259              : 
   11260     33655247 :     case TEMPLATE_DECL:
   11261              :       /* If T is a member template that shares template parameters with
   11262              :          ctx_parms, we need to mark all those parameters for mapping.
   11263              :          To that end, it should suffice to just walk the DECL_CONTEXT of
   11264              :          the template (assuming the template is not overly general).  */
   11265     33655247 :       WALK_SUBTREE (DECL_CONTEXT (t));
   11266     33655247 :       break;
   11267              : 
   11268          139 :     case LAMBDA_EXPR:
   11269          139 :       {
   11270              :         /* TREE_STATIC on LAMBDA_EXPR_EXTRA_ARGS means a full set of
   11271              :            arguments, so we can just look there; they will replace
   11272              :            any template parms in the rest of the LAMBDA_EXPR.  */
   11273          139 :         if (tree args = LAMBDA_EXPR_EXTRA_ARGS (t))
   11274              :           {
   11275           55 :             WALK_SUBTREE (args);
   11276              :             /* Without TREE_STATIC the args are just outer levels, so we'd
   11277              :                still need to look through the lambda for just inner
   11278              :                parameters.  Hopefully that's not necessary.  */
   11279           55 :             gcc_checking_assert (TREE_STATIC (args));
   11280              :             return 0;
   11281              :           }
   11282              :         /* Look in the parms and body.  */
   11283           84 :         tree fn = lambda_function (t);
   11284           84 :         WALK_SUBTREE (TREE_TYPE (fn));
   11285           84 :         WALK_SUBTREE (DECL_SAVED_TREE (fn));
   11286              :       }
   11287           84 :       break;
   11288              : 
   11289     17417377 :     case IDENTIFIER_NODE:
   11290     17417377 :       if (IDENTIFIER_CONV_OP_P (t))
   11291              :         /* The conversion-type-id of a conversion operator may be dependent.  */
   11292         3977 :         WALK_SUBTREE (TREE_TYPE (t));
   11293              :       break;
   11294              : 
   11295           18 :     case CONVERT_EXPR:
   11296           18 :       if (is_dummy_object (t))
   11297           15 :         WALK_SUBTREE (TREE_TYPE (t));
   11298              :       break;
   11299              : 
   11300              :     default:
   11301              :       break;
   11302              :     }
   11303              : 
   11304              :   /* Keep walking.  */
   11305              :   return 0;
   11306              : }
   11307              : 
   11308              : /* Look through T for template parameters.  */
   11309              : 
   11310              : tree
   11311     43231572 : find_template_parameter_info::find_in (tree t)
   11312              : {
   11313     43231572 :   return for_each_template_parm (t, keep_template_parm, this, &visited,
   11314              :                                  /*include_nondeduced*/true,
   11315     43231572 :                                  any_template_parm_r);
   11316              : }
   11317              : 
   11318              : /* As above, but also recursively look into the default arguments of template
   11319              :    parameters we found.  Used for alias CTAD.  */
   11320              : 
   11321              : tree
   11322          534 : find_template_parameter_info::find_in_recursive (tree t)
   11323              : {
   11324          534 :   if (tree r = find_in (t))
   11325              :     return r;
   11326              :   /* Since newly found parms are added to the end of the list, we
   11327              :      can just walk it until we reach the end.  */
   11328         1535 :   for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
   11329              :     {
   11330         1001 :       tree parm = TREE_VALUE (pl);
   11331         1001 :       tree list = corresponding_template_parameter_list (ctx_parms, parm);
   11332         1001 :       if (tree r = find_in (TREE_PURPOSE (list)))
   11333              :         return r;
   11334              :     }
   11335              :   return NULL_TREE;
   11336              : }
   11337              : 
   11338              : /* True if PARM was found by a previous call to find_in.  PARM can be a
   11339              :    TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P.  */
   11340              : 
   11341              : bool
   11342         1074 : find_template_parameter_info::found (tree parm)
   11343              : {
   11344         1074 :   if (TREE_CODE (parm) == TREE_LIST)
   11345         1074 :     parm = TREE_VALUE (parm);
   11346         1074 :   if (TREE_CODE (parm) == TYPE_DECL
   11347         1074 :       || TREE_CODE (parm) == TEMPLATE_DECL)
   11348         1031 :     parm = TREE_TYPE (parm);
   11349              :   else
   11350           43 :     parm = DECL_INITIAL (parm);
   11351         1074 :   gcc_checking_assert (TEMPLATE_PARM_P (parm));
   11352         1074 :   return parms.contains (parm);
   11353              : }
   11354              : 
   11355              : /* Returns a list of unique template parameters found within T, where CTX_PARMS
   11356              :    are the template parameters in scope.  */
   11357              : 
   11358              : tree
   11359     43230127 : find_template_parameters (tree t, tree ctx_parms)
   11360              : {
   11361     43230127 :   if (!ctx_parms)
   11362              :     return NULL_TREE;
   11363              : 
   11364     43230037 :   find_template_parameter_info ftpi (ctx_parms);
   11365     43230037 :   ftpi.find_in (t);
   11366     43230037 :   return ftpi.parm_list;
   11367     43230037 : }
   11368              : 
   11369              : /* Returns true if T depends on any template parameter.  */
   11370              : 
   11371              : bool
   11372   4034522755 : uses_template_parms (tree t)
   11373              : {
   11374   4034522755 :   if (t == NULL_TREE || t == error_mark_node)
   11375              :     return false;
   11376              : 
   11377              :   /* Namespaces can't depend on any template parameters.  */
   11378   3701884383 :   if (TREE_CODE (t) == NAMESPACE_DECL)
   11379              :     return false;
   11380              : 
   11381   3701883843 :   processing_template_decl_sentinel ptds (/*reset*/false);
   11382   3701883843 :   ++processing_template_decl;
   11383              : 
   11384   3701883843 :   if (TYPE_P (t))
   11385   2007995157 :     return dependent_type_p (t);
   11386   1693888686 :   else if (TREE_CODE (t) == TREE_VEC)
   11387   1368648302 :     return any_dependent_template_arguments_p (t);
   11388    325240384 :   else if (TREE_CODE (t) == TREE_LIST)
   11389    145851687 :     return (uses_template_parms (TREE_VALUE (t))
   11390    145851687 :             || uses_template_parms (TREE_CHAIN (t)));
   11391    179388697 :   else if (TREE_CODE (t) == TYPE_DECL)
   11392           93 :     return dependent_type_p (TREE_TYPE (t));
   11393              :   else
   11394    179388604 :     return instantiation_dependent_expression_p (t);
   11395   3701883843 : }
   11396              : 
   11397              : /* Returns true if T depends on any template parameter with level LEVEL.  */
   11398              : 
   11399              : bool
   11400      2085934 : uses_template_parms_level (tree t, int level)
   11401              : {
   11402      2085934 :   return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
   11403      2085934 :                                  /*include_nondeduced_p=*/true);
   11404              : }
   11405              : 
   11406              : /* Returns true if the signature of DECL depends on any template parameter from
   11407              :    its enclosing class.  */
   11408              : 
   11409              : static bool
   11410    299043436 : uses_outer_template_parms (tree decl)
   11411              : {
   11412    299043436 :   int depth;
   11413    299043436 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11414       386460 :     depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
   11415              :   else
   11416    298656976 :     depth = template_class_depth (CP_DECL_CONTEXT (decl));
   11417    299043436 :   if (depth == 0)
   11418              :     return false;
   11419     11008419 :   if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
   11420              :                               &depth, NULL, /*include_nondeduced_p=*/true))
   11421              :     return true;
   11422       990156 :   if (PRIMARY_TEMPLATE_P (decl)
   11423       990156 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11424              :     {
   11425       990156 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
   11426      2418829 :       for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
   11427              :         {
   11428      1428718 :           tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
   11429      1428718 :           tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
   11430      1428718 :           if (TREE_CODE (parm) == PARM_DECL
   11431      1428718 :               && for_each_template_parm (TREE_TYPE (parm),
   11432              :                                          template_parm_outer_level,
   11433              :                                          &depth, NULL, /*nondeduced*/true))
   11434              :             return true;
   11435      1428679 :           if (TREE_CODE (parm) == TEMPLATE_DECL
   11436      1428679 :               && uses_outer_template_parms (parm))
   11437              :             return true;
   11438      1428679 :           if (defarg
   11439      1428679 :               && for_each_template_parm (defarg, template_parm_outer_level,
   11440              :                                          &depth, NULL, /*nondeduced*/true))
   11441              :             return true;
   11442              :         }
   11443              :     }
   11444       990111 :   if (uses_outer_template_parms_in_constraints (decl))
   11445              :     return true;
   11446              :   return false;
   11447              : }
   11448              : 
   11449              : /* Returns true if the constraints of DECL depend on any template parameters
   11450              :    from its enclosing scope.  */
   11451              : 
   11452              : bool
   11453      4713234 : uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/)
   11454              : {
   11455      4713234 :   tree ci = get_constraints (decl);
   11456      4713234 :   if (ci)
   11457        42701 :     ci = CI_ASSOCIATED_CONSTRAINTS (ci);
   11458        42701 :   if (!ci)
   11459              :     return false;
   11460        42701 :   if (!ctx)
   11461              :     {
   11462        13025 :       if (tree fc = DECL_FRIEND_CONTEXT (decl))
   11463              :         ctx = fc;
   11464              :       else
   11465          108 :         ctx = CP_DECL_CONTEXT (decl);
   11466              :     }
   11467        42701 :   int depth = template_class_depth (ctx);
   11468        42701 :   if (depth == 0)
   11469              :     return false;
   11470        42677 :   return for_each_template_parm (ci, template_parm_outer_level,
   11471        42677 :                                  &depth, NULL, /*nondeduced*/true);
   11472              : }
   11473              : 
   11474              : /* Returns TRUE iff INST is an instantiation we don't need to do in an
   11475              :    ill-formed translation unit, i.e. a variable or function that isn't
   11476              :    usable in a constant expression.  */
   11477              : 
   11478              : static inline bool
   11479      5707922 : neglectable_inst_p (tree d)
   11480              : {
   11481      5547385 :   return (d && DECL_P (d)
   11482      4825656 :           && !undeduced_auto_decl (d)
   11483     10444042 :           && !(TREE_CODE (d) == FUNCTION_DECL
   11484      4736120 :                ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
   11485       685047 :                : decl_maybe_constant_var_p (d)));
   11486              : }
   11487              : 
   11488              : /* Returns TRUE iff we should refuse to instantiate DECL because it's
   11489              :    neglectable and instantiated from within an erroneous instantiation.  */
   11490              : 
   11491              : static bool
   11492    126830471 : limit_bad_template_recursion (tree decl)
   11493              : {
   11494    126830471 :   struct tinst_level *lev = current_tinst_level;
   11495    126830471 :   int errs = errorcount + sorrycount;
   11496    126830471 :   if (errs == 0 || !neglectable_inst_p (decl))
   11497    124613414 :     return false;
   11498              : 
   11499              :   /* Avoid instantiating members of an ill-formed class.  */
   11500      2217057 :   bool refuse
   11501      4434087 :     = (DECL_CLASS_SCOPE_P (decl)
   11502      3734621 :        && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
   11503              : 
   11504              :   if (!refuse)
   11505              :     {
   11506      2521510 :       for (; lev; lev = lev->next)
   11507      4083441 :         if (neglectable_inst_p (lev->maybe_get_node ()))
   11508              :           break;
   11509      2215930 :       refuse = (lev && errs > lev->errors);
   11510              :     }
   11511              : 
   11512              :   if (refuse)
   11513              :     {
   11514              :       /* Don't warn about it not being defined.  */
   11515         3001 :       suppress_warning (decl, OPT_Wunused);
   11516         3001 :       tree clone;
   11517         5213 :       FOR_EACH_CLONE (clone, decl)
   11518         2212 :         suppress_warning (clone, OPT_Wunused);
   11519              :     }
   11520              :   return refuse;
   11521              : }
   11522              : 
   11523              : static int tinst_depth;
   11524              : extern int max_tinst_depth;
   11525              : int depth_reached;
   11526              : int tinst_dump_id;
   11527              : 
   11528              : static GTY(()) struct tinst_level *last_error_tinst_level;
   11529              : 
   11530              : /* We're starting to instantiate D; record the template instantiation context
   11531              :    at LOC for diagnostics and to restore it later.  */
   11532              : 
   11533              : bool
   11534    866224123 : push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
   11535              : {
   11536    866224123 :   struct tinst_level *new_level;
   11537              : 
   11538    866224123 :   if (tinst_depth >= max_tinst_depth)
   11539              :     {
   11540              :       /* Tell error.cc not to try to instantiate any templates.  */
   11541           48 :       at_eof = 3;
   11542           48 :       fatal_error (input_location,
   11543              :                    "template instantiation depth exceeds maximum of %d"
   11544              :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   11545              :                    max_tinst_depth);
   11546              :       return false;
   11547              :     }
   11548              : 
   11549              :   /* If the current instantiation caused problems, don't let it instantiate
   11550              :      anything else.  Do allow deduction substitution and decls usable in
   11551              :      constant expressions.  */
   11552    866224075 :   if (!targs && limit_bad_template_recursion (tldcl))
   11553              :     {
   11554              :       /* Avoid no_linkage_errors and unused function (and all other)
   11555              :          warnings for this decl.  */
   11556         2026 :       suppress_warning (tldcl);
   11557         2026 :       return false;
   11558              :     }
   11559              : 
   11560              :   /* When not -quiet, dump template instantiations other than functions, since
   11561              :      announce_function will take care of those.  */
   11562    866222049 :   if (!quiet_flag && !targs
   11563            0 :       && TREE_CODE (tldcl) != TREE_LIST
   11564            0 :       && TREE_CODE (tldcl) != FUNCTION_DECL)
   11565            0 :     fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
   11566              : 
   11567    866222049 :   new_level = tinst_level_freelist ().alloc ();
   11568    866222049 :   new_level->tldcl = tldcl;
   11569    866222049 :   new_level->targs = targs;
   11570    866222049 :   new_level->locus = loc;
   11571    866222049 :   new_level->errors = errorcount + sorrycount;
   11572    866222049 :   new_level->had_errors = false;
   11573    866222049 :   new_level->next = NULL;
   11574    866222049 :   new_level->refcount = 0;
   11575    866222049 :   new_level->path = new_level->visible = nullptr;
   11576    866222049 :   set_refcount_ptr (new_level->next, current_tinst_level);
   11577    866222049 :   set_refcount_ptr (current_tinst_level, new_level);
   11578              : 
   11579    866222049 :   if (cxx_dump_pretty_printer pp {tinst_dump_id})
   11580              :     {
   11581              : #if __GNUC__ >= 10
   11582            0 : #pragma GCC diagnostic push
   11583            0 : #pragma GCC diagnostic ignored "-Wformat-diag"
   11584              : #endif
   11585            0 :       bool list_p = new_level->list_p ();
   11586            0 :       if ((list_p || TREE_CODE (tldcl) == TEMPLATE_FOR_STMT)
   11587            0 :           && !pp.has_flag (TDF_DETAILS))
   11588              :         /* Skip non-instantiations unless -details.  */;
   11589              :       else
   11590              :         {
   11591            0 :           if (tinst_depth == 0)
   11592            0 :             pp_newline (&pp);
   11593            0 :           if (loc && pp.has_flag (TDF_LINENO))
   11594              :             {
   11595            0 :               for (int i = 0; i < tinst_depth; ++i)
   11596            0 :                 pp_space (&pp);
   11597            0 :               const expanded_location el = expand_location (loc);
   11598            0 :               pp_printf (&pp, "%s:%d:%d", el.file, el.line, el.column);
   11599            0 :               pp_newline (&pp);
   11600              :             }
   11601            0 :           for (int i = 0; i < tinst_depth; ++i)
   11602            0 :             pp_space (&pp);
   11603            0 :           if (TREE_CODE (tldcl) == TEMPLATE_FOR_STMT)
   11604            0 :             pp_printf (&pp, "I template for");
   11605            0 :           else if (list_p)
   11606            0 :             pp_printf (&pp, "S %S", new_level->get_node ());
   11607              :           else
   11608            0 :             pp_printf (&pp, "I %D", tldcl);
   11609            0 :           pp_newline (&pp);
   11610              :         }
   11611              : #if __GNUC__ >= 10
   11612              : #pragma GCC diagnostic pop
   11613              : #endif
   11614    866222049 :     }
   11615              : 
   11616    866222049 :   ++tinst_depth;
   11617    866222049 :   if (GATHER_STATISTICS && (tinst_depth > depth_reached))
   11618              :     depth_reached = tinst_depth;
   11619              : 
   11620    866222049 :   return true;
   11621              : }
   11622              : 
   11623              : /* We're starting substitution of TMPL<ARGS>; record the template
   11624              :    substitution context for diagnostics and to restore it later.  */
   11625              : 
   11626              : bool
   11627    757602247 : push_tinst_level (tree tmpl, tree args)
   11628              : {
   11629    757602247 :   return push_tinst_level_loc (tmpl, args, input_location);
   11630              : }
   11631              : 
   11632              : /* We're starting to instantiate D; record INPUT_LOCATION and the
   11633              :    template instantiation context for diagnostics and to restore it
   11634              :    later.  */
   11635              : 
   11636              : bool
   11637    108464971 : push_tinst_level (tree d)
   11638              : {
   11639    108464971 :   return push_tinst_level_loc (d, input_location);
   11640              : }
   11641              : 
   11642              : /* Likewise, but record LOC as the program location.  */
   11643              : 
   11644              : bool
   11645    108620895 : push_tinst_level_loc (tree d, location_t loc)
   11646              : {
   11647    108620895 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   11648    108620895 :   return push_tinst_level_loc (d, NULL, loc);
   11649              : }
   11650              : 
   11651              : /* We're done instantiating this template; return to the instantiation
   11652              :    context.  */
   11653              : 
   11654              : void
   11655    884401865 : pop_tinst_level (void)
   11656              : {
   11657              :   /* Restore the filename and line number stashed away when we started
   11658              :      this instantiation.  */
   11659    884401865 :   input_location = current_tinst_level->locus;
   11660    884401865 :   if (unsigned errs = errorcount + sorrycount)
   11661     18654669 :     if (errs > current_tinst_level->errors)
   11662       257523 :       current_tinst_level->had_errors = true;
   11663    884401865 :   set_refcount_ptr (current_tinst_level, current_tinst_level->next);
   11664    884401865 :   --tinst_depth;
   11665    884401865 : }
   11666              : 
   11667              : /* True if the instantiation represented by LEVEL is complete.  */
   11668              : 
   11669              : static bool
   11670     41140256 : tinst_complete_p (struct tinst_level *level)
   11671              : {
   11672     41140256 :   gcc_assert (!level->list_p ());
   11673     41140256 :   tree node = level->get_node ();
   11674     41140256 :   if (TYPE_P (node))
   11675            0 :     return COMPLETE_TYPE_P (node);
   11676              :   else
   11677     41140256 :     return (DECL_TEMPLATE_INSTANTIATED (node)
   11678     41140256 :             || DECL_TEMPLATE_SPECIALIZATION (node));
   11679              : }
   11680              : 
   11681              : /* We're instantiating a deferred template; restore the template
   11682              :    instantiation context in which the instantiation was requested, which
   11683              :    is one step out from LEVEL.  Return the corresponding DECL or TYPE.  */
   11684              : 
   11685              : static tree
   11686     18209603 : reopen_tinst_level (struct tinst_level *level)
   11687              : {
   11688     18209603 :   struct tinst_level *t;
   11689              : 
   11690     18209603 :   tinst_depth = 0;
   11691     89197074 :   for (t = level; t; t = t->next)
   11692     70987471 :     ++tinst_depth;
   11693              : 
   11694     18209603 :   set_refcount_ptr (current_tinst_level, level);
   11695     18209603 :   pop_tinst_level ();
   11696     18209603 :   if (current_tinst_level && !current_tinst_level->had_errors)
   11697     13637129 :     current_tinst_level->errors = errorcount+sorrycount;
   11698              : 
   11699     18209603 :   if (cxx_dump_pretty_printer pp {tinst_dump_id})
   11700              :     {
   11701              : #if __GNUC__ >= 10
   11702            0 : #pragma GCC diagnostic push
   11703            0 : #pragma GCC diagnostic ignored "-Wformat-diag"
   11704              : #endif
   11705              :       /* Dump the reopened instantiation context.  */
   11706            0 :       t = current_tinst_level;
   11707            0 :       if (!pp.has_flag (TDF_DETAILS))
   11708              :         /* Skip non-instantiations unless -details.  */
   11709            0 :         while (t && t->list_p ())
   11710            0 :           t = t->next;
   11711            0 :       if (t)
   11712              :         {
   11713            0 :           static tree last_ctx = NULL_TREE;
   11714            0 :           tree ctx = t->get_node ();
   11715            0 :           if (ctx != last_ctx)
   11716              :             {
   11717            0 :               last_ctx = ctx;
   11718            0 :               pp_newline (&pp);
   11719            0 :               if (TREE_CODE (t->tldcl) == TEMPLATE_FOR_STMT)
   11720            0 :                 pp_printf (&pp, "RI template for");
   11721            0 :               else if (t->list_p ())
   11722            0 :                 pp_printf (&pp, "RS %S", ctx);
   11723              :               else
   11724            0 :                 pp_printf (&pp, "RI %D", ctx);
   11725            0 :               pp_newline (&pp);
   11726              :             }
   11727              :         }
   11728              : #if __GNUC__ >= 10
   11729              : #pragma GCC diagnostic pop
   11730              : #endif
   11731     18209603 :     }
   11732              : 
   11733     18209603 :   tree decl = level->maybe_get_node ();
   11734     18209603 :   if (decl && modules_p ())
   11735              :     {
   11736              :       /* An instantiation is in module purview only if it had an explicit
   11737              :          instantiation definition in module purview; mark_decl_instantiated uses
   11738              :          set_instantiating_module to set the flag in that case.  */
   11739        56000 :       if (DECL_MODULE_PURVIEW_P (decl))
   11740        21224 :         module_kind |= MK_PURVIEW;
   11741              :       else
   11742        34776 :         module_kind &= ~MK_PURVIEW;
   11743              :     }
   11744     18209603 :   return decl;
   11745              : }
   11746              : 
   11747              : /* Returns the TINST_LEVEL which gives the original instantiation
   11748              :    context.  */
   11749              : 
   11750              : struct tinst_level *
   11751          142 : outermost_tinst_level (void)
   11752              : {
   11753          142 :   struct tinst_level *level = current_tinst_level;
   11754          142 :   if (level)
   11755           18 :     while (level->next)
   11756              :       level = level->next;
   11757          142 :   return level;
   11758              : }
   11759              : 
   11760              : /* True iff T is a friend function declaration that is not itself a template
   11761              :    and is not defined in a class template.  */
   11762              : 
   11763              : bool
   11764    148302071 : non_templated_friend_p (tree t)
   11765              : {
   11766    148302071 :   if (t && TREE_CODE (t) == FUNCTION_DECL
   11767    296275488 :       && DECL_UNIQUE_FRIEND_P (t))
   11768              :     {
   11769      2772935 :       tree ti = DECL_TEMPLATE_INFO (t);
   11770      2772935 :       if (!ti)
   11771              :         return true;
   11772              :       /* DECL_FRIEND_CONTEXT is set for a friend defined in class.  */
   11773      5545870 :       if (DECL_FRIEND_CONTEXT (t))
   11774              :         return false;
   11775              :       /* Non-templated friends in a class template are still represented with a
   11776              :          TEMPLATE_DECL; check that its primary template is the befriending
   11777              :          class.  Note that DECL_PRIMARY_TEMPLATE is null for
   11778              :          template <class T> friend A<T>::f(); */
   11779      1043351 :       tree tmpl = TI_TEMPLATE (ti);
   11780      1043351 :       tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
   11781      1043351 :       return ((primary || DECL_NAMESPACE_SCOPE_P (t)) && primary != tmpl);
   11782              :     }
   11783              :   else
   11784              :     return false;
   11785              : }
   11786              : 
   11787              : /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
   11788              :    vector of template arguments, as for tsubst.
   11789              : 
   11790              :    Returns an appropriate tsubst'd friend declaration.  */
   11791              : 
   11792              : static tree
   11793      2539471 : tsubst_friend_function (tree decl, tree args)
   11794              : {
   11795      2539471 :   tree new_friend;
   11796              : 
   11797      2539471 :   if (decl_specialization_friend_p (decl))
   11798              :     /* This was a friend declared with an explicit template
   11799              :        argument list, e.g.:
   11800              : 
   11801              :        friend void f<>(T);
   11802              : 
   11803              :        to indicate that f was a template instantiation, not a new
   11804              :        function declaration.  Now, we have to figure out what
   11805              :        instantiation of what template.  */
   11806              :     {
   11807        27009 :       tree template_id, arglist, fns;
   11808        27009 :       tree new_args;
   11809        27009 :       tree tmpl;
   11810        27009 :       tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
   11811              : 
   11812              :       /* Friend functions are looked up in the containing namespace scope.
   11813              :          We must enter that scope, to avoid finding member functions of the
   11814              :          current class with same name.  */
   11815        27009 :       push_nested_namespace (ns);
   11816        27009 :       fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
   11817              :                          tf_warning_or_error, NULL_TREE);
   11818        27009 :       pop_nested_namespace (ns);
   11819        27009 :       arglist = tsubst (DECL_TI_ARGS (decl), args,
   11820              :                         tf_warning_or_error, NULL_TREE);
   11821        27009 :       template_id = lookup_template_function (fns, arglist);
   11822              : 
   11823        27009 :       new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11824        27009 :       tmpl = determine_specialization (template_id, new_friend,
   11825              :                                        &new_args,
   11826              :                                        /*need_member_template=*/0,
   11827        27009 :                                        TREE_VEC_LENGTH (args),
   11828              :                                        tsk_none);
   11829        27009 :       return instantiate_template (tmpl, new_args, tf_error);
   11830              :     }
   11831              : 
   11832      2512462 :   new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11833      2512462 :   if (new_friend == error_mark_node)
   11834              :     return error_mark_node;
   11835              : 
   11836              :   /* The NEW_FRIEND will look like an instantiation, to the
   11837              :      compiler, but is not an instantiation from the point of view of
   11838              :      the language.  For example, we might have had:
   11839              : 
   11840              :      template <class T> struct S {
   11841              :        template <class U> friend void f(T, U);
   11842              :      };
   11843              : 
   11844              :      Then, in S<int>, template <class U> void f(int, U) is not an
   11845              :      instantiation of anything.  */
   11846              : 
   11847      2512462 :   DECL_USE_TEMPLATE (new_friend) = 0;
   11848      2512462 :   if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11849              :     {
   11850      1632086 :       DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
   11851      1632086 :       DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
   11852      3264172 :       DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
   11853      1632086 :         = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
   11854              : 
   11855              :       /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
   11856              :          match in decls_match.  */
   11857      1632086 :       tree parms = DECL_TEMPLATE_PARMS (new_friend);
   11858      1632086 :       tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
   11859      1632086 :       treqs = maybe_substitute_reqs_for (treqs, new_friend);
   11860      1632086 :       if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
   11861              :         {
   11862       176927 :           TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
   11863              :           /* As well as each TEMPLATE_PARM_CONSTRAINTS.  */
   11864       176927 :           tsubst_each_template_parm_constraints (parms, args,
   11865              :                                                  tf_warning_or_error);
   11866              :         }
   11867              :     }
   11868              : 
   11869              :   /* The mangled name for the NEW_FRIEND is incorrect.  The function
   11870              :      is not a template instantiation and should not be mangled like
   11871              :      one.  Therefore, we forget the mangling here; we'll recompute it
   11872              :      later if we need it.  */
   11873      2512462 :   if (TREE_CODE (new_friend) != TEMPLATE_DECL)
   11874              :     {
   11875       880376 :       SET_DECL_RTL (new_friend, NULL);
   11876       880376 :       SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
   11877              :     }
   11878              : 
   11879      2512462 :   if (DECL_NAMESPACE_SCOPE_P (new_friend))
   11880              :     {
   11881      2512387 :       tree old_decl;
   11882      2512387 :       tree ns;
   11883              : 
   11884              :       /* We must save some information from NEW_FRIEND before calling
   11885              :          duplicate decls since that function will free NEW_FRIEND if
   11886              :          possible.  */
   11887      2512387 :       tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
   11888      2512387 :       tree new_friend_result_template_info = NULL_TREE;
   11889      2512387 :       bool new_friend_is_defn =
   11890              :         (new_friend_template_info
   11891      2512387 :          && (DECL_INITIAL (DECL_TEMPLATE_RESULT
   11892              :                            (template_for_substitution (new_friend)))
   11893      2512387 :              != NULL_TREE));
   11894      2512387 :       tree not_tmpl = new_friend;
   11895              : 
   11896      2512387 :       if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11897              :         {
   11898              :           /* This declaration is a `primary' template.  */
   11899      1632038 :           DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
   11900              : 
   11901      1632038 :           not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
   11902      1632038 :           new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
   11903              :         }
   11904              : 
   11905              :       /* We need to propagate module attachment for the new friend from the
   11906              :          owner of this template.  */
   11907      2512387 :       propagate_defining_module (new_friend, decl);
   11908              : 
   11909              :       /* Inside pushdecl_namespace_level, we will push into the
   11910              :          current namespace. However, the friend function should go
   11911              :          into the namespace of the template.  */
   11912      2512387 :       ns = decl_namespace_context (new_friend);
   11913      2512387 :       push_nested_namespace (ns);
   11914      2512387 :       old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
   11915      2512387 :       pop_nested_namespace (ns);
   11916              : 
   11917      2512387 :       if (old_decl == error_mark_node)
   11918              :         return error_mark_node;
   11919              : 
   11920      2512366 :       if (old_decl != new_friend)
   11921              :         {
   11922              :           /* This new friend declaration matched an existing
   11923              :              declaration.  For example, given:
   11924              : 
   11925              :                template <class T> void f(T);
   11926              :                template <class U> class C {
   11927              :                  template <class T> friend void f(T) {}
   11928              :                };
   11929              : 
   11930              :              the friend declaration actually provides the definition
   11931              :              of `f', once C has been instantiated for some type.  So,
   11932              :              old_decl will be the out-of-class template declaration,
   11933              :              while new_friend is the in-class definition.
   11934              : 
   11935              :              But, if `f' was called before this point, the
   11936              :              instantiation of `f' will have DECL_TI_ARGS corresponding
   11937              :              to `T' but not to `U', references to which might appear
   11938              :              in the definition of `f'.  Previously, the most general
   11939              :              template for an instantiation of `f' was the out-of-class
   11940              :              version; now it is the in-class version.  Therefore, we
   11941              :              run through all specialization of `f', adding to their
   11942              :              DECL_TI_ARGS appropriately.  In particular, they need a
   11943              :              new set of outer arguments, corresponding to the
   11944              :              arguments for this class instantiation.
   11945              : 
   11946              :              The same situation can arise with something like this:
   11947              : 
   11948              :                friend void f(int);
   11949              :                template <class T> class C {
   11950              :                  friend void f(T) {}
   11951              :                };
   11952              : 
   11953              :              when `C<int>' is instantiated.  Now, `f(int)' is defined
   11954              :              in the class.  */
   11955              : 
   11956       900070 :           if (!new_friend_is_defn)
   11957              :             /* On the other hand, if the in-class declaration does
   11958              :                *not* provide a definition, then we don't want to alter
   11959              :                existing definitions.  We can just leave everything
   11960              :                alone.  */
   11961              :             ;
   11962              :           else
   11963              :             {
   11964           66 :               tree old_template = most_general_template (old_decl);
   11965           66 :               tree new_template = TI_TEMPLATE (new_friend_template_info);
   11966           66 :               tree new_args = TI_ARGS (new_friend_template_info);
   11967              : 
   11968              :               /* Overwrite whatever template info was there before, if
   11969              :                  any, with the new template information pertaining to
   11970              :                  the declaration.  */
   11971           66 :               DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
   11972              : 
   11973           66 :               if (TREE_CODE (old_decl) != TEMPLATE_DECL)
   11974              :                 {
   11975              :                   /* We should have called reregister_specialization in
   11976              :                      duplicate_decls.  */
   11977           33 :                   gcc_assert (retrieve_specialization (new_template,
   11978              :                                                        new_args, 0)
   11979              :                               == old_decl);
   11980              : 
   11981              :                   /* Instantiate it if the global has already been used.  */
   11982           33 :                   if (DECL_ODR_USED (old_decl))
   11983            3 :                     instantiate_decl (old_decl, /*defer_ok=*/true,
   11984              :                                       /*expl_inst_class_mem_p=*/false);
   11985              :                 }
   11986              :               else
   11987              :                 {
   11988           33 :                   tree t;
   11989              : 
   11990              :                   /* Indicate that the old function template is a partial
   11991              :                      instantiation.  */
   11992           33 :                   DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
   11993           33 :                     = new_friend_result_template_info;
   11994              : 
   11995           33 :                   gcc_assert (new_template
   11996              :                               == most_general_template (new_template));
   11997           33 :                   gcc_assert (new_template != old_decl);
   11998              : 
   11999              :                   /* Reassign any specializations already in the hash table
   12000              :                      to the new more general template, and add the
   12001              :                      additional template args.  */
   12002           33 :                   for (t = DECL_TEMPLATE_INSTANTIATIONS (old_template);
   12003          252 :                        t != NULL_TREE;
   12004          219 :                        t = TREE_CHAIN (t))
   12005              :                     {
   12006          219 :                       tree spec = TREE_VALUE (t);
   12007          219 :                       spec_entry elt;
   12008              : 
   12009          219 :                       elt.tmpl = old_decl;
   12010          219 :                       elt.args = DECL_TI_ARGS (spec);
   12011          219 :                       elt.spec = NULL_TREE;
   12012              : 
   12013          438 :                       if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (DECL_TI_ARGS (spec))
   12014          429 :                           && !is_specialization_of_friend (spec, new_template))
   12015          192 :                         continue;
   12016              : 
   12017           27 :                       decl_specializations->remove_elt (&elt);
   12018              : 
   12019           27 :                       tree& spec_args = DECL_TI_ARGS (spec);
   12020           54 :                       spec_args = add_outermost_template_args
   12021           27 :                         (new_args, INNERMOST_TEMPLATE_ARGS (spec_args));
   12022              : 
   12023           27 :                       register_specialization
   12024           27 :                         (spec, new_template, spec_args, true, 0);
   12025              : 
   12026              :                     }
   12027           33 :                   DECL_TEMPLATE_INSTANTIATIONS (old_template) = NULL_TREE;
   12028              :                 }
   12029              :             }
   12030              : 
   12031              :           /* The information from NEW_FRIEND has been merged into OLD_DECL
   12032              :              by duplicate_decls.  */
   12033              :           new_friend = old_decl;
   12034              :         }
   12035              : 
   12036              :       /* We've just introduced a namespace-scope function without
   12037              :          necessarily having opened the enclosing namespace, so
   12038              :          make sure the namespace is declared in this TU as well.  */
   12039      2512366 :       if (modules_p ())
   12040         6849 :         for (tree ctx = DECL_CONTEXT (new_friend);
   12041        15051 :              TREE_CODE (ctx) == NAMESPACE_DECL; ctx = DECL_CONTEXT (ctx))
   12042         8202 :           expose_existing_namespace (ctx);
   12043              :     }
   12044              :   else
   12045              :     {
   12046           75 :       tree context = DECL_CONTEXT (new_friend);
   12047           75 :       bool dependent_p;
   12048              : 
   12049              :       /* In the code
   12050              :            template <class T> class C {
   12051              :              template <class U> friend void C1<U>::f (); // case 1
   12052              :              friend void C2<T>::f ();                      // case 2
   12053              :            };
   12054              :          we only need to make sure CONTEXT is a complete type for
   12055              :          case 2.  To distinguish between the two cases, we note that
   12056              :          CONTEXT of case 1 remains dependent type after tsubst while
   12057              :          this isn't true for case 2.  */
   12058           75 :       ++processing_template_decl;
   12059           75 :       dependent_p = dependent_type_p (context);
   12060           75 :       --processing_template_decl;
   12061              : 
   12062           75 :       if (!dependent_p
   12063           75 :           && !complete_type_or_else (context, NULL_TREE))
   12064            0 :         return error_mark_node;
   12065              : 
   12066           75 :       if (COMPLETE_TYPE_P (context))
   12067              :         {
   12068           69 :           tree fn = new_friend;
   12069              :           /* do_friend adds the TEMPLATE_DECL for any member friend
   12070              :              template even if it isn't a member template, i.e.
   12071              :                template <class T> friend A<T>::f();
   12072              :              Look through it in that case.  */
   12073           69 :           if (TREE_CODE (fn) == TEMPLATE_DECL
   12074           69 :               && !PRIMARY_TEMPLATE_P (fn))
   12075           24 :             fn = DECL_TEMPLATE_RESULT (fn);
   12076              :           /* Check to see that the declaration is really present, and,
   12077              :              possibly obtain an improved declaration.  */
   12078           69 :           fn = check_classfn (context, fn, NULL_TREE);
   12079              : 
   12080           69 :           if (fn)
   12081      2539471 :             new_friend = fn;
   12082              :         }
   12083              :     }
   12084              : 
   12085              :   return new_friend;
   12086              : }
   12087              : 
   12088              : /* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
   12089              :    template arguments, as for tsubst.
   12090              : 
   12091              :    Returns an appropriate tsubst'd friend type or error_mark_node on
   12092              :    failure.  */
   12093              : 
   12094              : static tree
   12095       525600 : tsubst_friend_class (tree friend_tmpl, tree args)
   12096              : {
   12097       525600 :   tree tmpl;
   12098              : 
   12099       525600 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
   12100              :     {
   12101            3 :       tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
   12102            3 :       return TREE_TYPE (tmpl);
   12103              :     }
   12104              : 
   12105       525597 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) == 1)
   12106              :     /* The template has already been fully substituted, e.g. for
   12107              : 
   12108              :          template <typename> friend class ::C;
   12109              : 
   12110              :        so we can just return it directly.  */
   12111       110533 :     return TREE_TYPE (friend_tmpl);
   12112              : 
   12113       415064 :   tree context = CP_DECL_CONTEXT (friend_tmpl);
   12114       415064 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   12115       414665 :     push_nested_namespace (context);
   12116              :   else
   12117              :     {
   12118          399 :       context = tsubst (context, args, tf_error, NULL_TREE);
   12119          399 :       push_nested_class (context);
   12120              :     }
   12121              : 
   12122       415064 :   tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
   12123              :                       LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
   12124              : 
   12125       415064 :   if (!tmpl)
   12126              :     /* If we didn't find by name lookup, the type may still exist but as a
   12127              :        'hidden' import; we should check for this too to avoid accidentally
   12128              :        instantiating a duplicate.  */
   12129          284 :     tmpl = lookup_imported_hidden_friend (friend_tmpl);
   12130              : 
   12131       415064 :   if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
   12132              :     {
   12133              :       /* The friend template has already been declared.  Just
   12134              :          check to see that the declarations match, and install any new
   12135              :          default parameters.  We must tsubst the default parameters,
   12136              :          of course.  We only need the innermost template parameters
   12137              :          because that is all that redeclare_class_template will look
   12138              :          at.  */
   12139              : 
   12140       414792 :       if (modules_p ())
   12141              :         /* Check that the existing declaration's module attachment is
   12142              :            compatible with the attachment of the friend template.  */
   12143         1514 :         module_may_redeclare (tmpl, friend_tmpl);
   12144              : 
   12145       414792 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (friend_tmpl))
   12146              :         {
   12147       414789 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
   12148              :                                               args, tf_warning_or_error);
   12149       414789 :           tsubst_each_template_parm_constraints (parms, args,
   12150              :                                                  tf_warning_or_error);
   12151       414789 :           location_t saved_input_location = input_location;
   12152       414789 :           input_location = DECL_SOURCE_LOCATION (friend_tmpl);
   12153       414789 :           tree cons = get_constraints (friend_tmpl);
   12154       414789 :           ++processing_template_decl;
   12155       414789 :           cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
   12156       414789 :                                          DECL_FRIEND_CONTEXT (friend_tmpl));
   12157       414789 :           --processing_template_decl;
   12158       414789 :           redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
   12159       414789 :           input_location = saved_input_location;
   12160              :         }
   12161              :     }
   12162              :   else
   12163              :     {
   12164              :       /* The friend template has not already been declared.  In this
   12165              :          case, the instantiation of the template class will cause the
   12166              :          injection of this template into the namespace scope.  */
   12167          272 :       tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
   12168              : 
   12169          272 :       if (tmpl != error_mark_node)
   12170              :         {
   12171              :           /* The new TMPL is not an instantiation of anything, so we
   12172              :              forget its origins.  It is also not a specialization of
   12173              :              anything.  We don't reset CLASSTYPE_TI_TEMPLATE
   12174              :              for the new type because that is supposed to be the
   12175              :              corresponding template decl, i.e., TMPL.  */
   12176          267 :           spec_entry elt;
   12177          267 :           elt.tmpl = friend_tmpl;
   12178          267 :           elt.args = CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl));
   12179          267 :           elt.spec = TREE_TYPE (tmpl);
   12180          267 :           type_specializations->remove_elt (&elt);
   12181              : 
   12182          267 :           DECL_USE_TEMPLATE (tmpl) = 0;
   12183          267 :           DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
   12184          267 :           CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
   12185          267 :           CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
   12186          267 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
   12187          267 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = false;
   12188              : 
   12189              :           /* Substitute into and set the constraints on the new declaration.  */
   12190          267 :           if (tree ci = get_constraints (friend_tmpl))
   12191              :             {
   12192            6 :               ++processing_template_decl;
   12193            6 :               ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
   12194            6 :                                            DECL_FRIEND_CONTEXT (friend_tmpl));
   12195            6 :               --processing_template_decl;
   12196            6 :               set_constraints (tmpl, ci);
   12197            6 :               tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
   12198              :                                                      args, tf_warning_or_error);
   12199              :             }
   12200              : 
   12201              :           /* We need to propagate the attachment of the original template to the
   12202              :              newly instantiated template type.  */
   12203          267 :           propagate_defining_module (tmpl, friend_tmpl);
   12204              : 
   12205              :           /* Inject this template into the enclosing namspace scope.  */
   12206          267 :           tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
   12207              :         }
   12208              :     }
   12209              : 
   12210       415064 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   12211       414665 :     pop_nested_namespace (context);
   12212              :   else
   12213          399 :     pop_nested_class ();
   12214              : 
   12215       415064 :   return TREE_TYPE (tmpl);
   12216              : }
   12217              : 
   12218              : /* Returns zero if TYPE cannot be completed later due to circularity.
   12219              :    Otherwise returns one.  */
   12220              : 
   12221              : static int
   12222      7265189 : can_complete_type_without_circularity (tree type)
   12223              : {
   12224      7265589 :   if (type == NULL_TREE || type == error_mark_node)
   12225              :     return 0;
   12226      7265589 :   else if (COMPLETE_TYPE_P (type))
   12227              :     return 1;
   12228       648572 :   else if (TREE_CODE (type) == ARRAY_TYPE)
   12229          400 :     return can_complete_type_without_circularity (TREE_TYPE (type));
   12230       648167 :   else if (CLASS_TYPE_P (type)
   12231      1296339 :            && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
   12232              :     return 0;
   12233              :   else
   12234       648163 :     return 1;
   12235              : }
   12236              : 
   12237              : static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
   12238              :                                 tsubst_flags_t, tree);
   12239              : 
   12240              : /* Instantiate the contract statement.  */
   12241              : 
   12242              : static tree
   12243          263 : tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
   12244              :                  tree in_decl)
   12245              : {
   12246          263 :   tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
   12247          263 :   bool auto_p  = type_uses_auto (type);
   12248              : 
   12249          263 :   tree r = copy_node (t);
   12250              : 
   12251              :   /* Rebuild the result variable, if present.  */
   12252          263 :   tree oldvar = NULL_TREE;
   12253          263 :   tree newvar = NULL_TREE;
   12254          263 :   if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
   12255              :     {
   12256           42 :       oldvar = POSTCONDITION_IDENTIFIER (t);
   12257           42 :       if (oldvar == error_mark_node)
   12258            2 :         return invalidate_contract (r);
   12259              : 
   12260           40 :       newvar = copy_node (oldvar);
   12261           40 :       TREE_TYPE (newvar) = type;
   12262           40 :       DECL_CONTEXT (newvar) = decl;
   12263           40 :       POSTCONDITION_IDENTIFIER (r) = newvar;
   12264              : 
   12265              :       /* Make sure the postcondition is valid.  */
   12266           40 :       location_t loc = DECL_SOURCE_LOCATION (oldvar);
   12267           40 :       if (!auto_p)
   12268           28 :         if (!check_postcondition_result (decl, type, loc))
   12269            0 :           return invalidate_contract (r);
   12270              :     }
   12271              : 
   12272              :   /* Instantiate the condition.  If the return type is undeduced, process
   12273              :      the expression as if inside a template to avoid spurious type errors.  */
   12274          261 :   begin_scope (sk_contract, decl);
   12275          261 :   bool old_pc = processing_postcondition;
   12276          261 :   processing_postcondition = POSTCONDITION_P (t);
   12277          261 :   if (auto_p)
   12278           14 :     ++processing_template_decl;
   12279          261 :   if (newvar)
   12280              :     /* Make the variable available for lookup.  */
   12281           40 :     register_local_specialization (newvar, oldvar);
   12282              : 
   12283              :   /* Contract conditions have a wider application of location wrappers than
   12284              :      other trees (which will not work with the generic handling in tsubst_expr),
   12285              :      remove the wrapper here...  */
   12286          261 :   location_t cond_l = EXPR_LOCATION (CONTRACT_CONDITION (t));
   12287          261 :   tree cond_t = tree_strip_any_location_wrapper (CONTRACT_CONDITION (t));
   12288              : 
   12289              :   /* ... and substitute the contained expression.  */
   12290          261 :   cond_t = tsubst_expr (cond_t, args, complain, in_decl);
   12291              : 
   12292              :   /* Convert to bool, if possible, and then re-apply a location wrapper
   12293              :      when required.  */
   12294          261 :   cp_expr new_condition (cond_t, cond_l);
   12295          261 :   CONTRACT_CONDITION (r) = finish_contract_condition (new_condition);
   12296              : 
   12297              :   /* At present, the semantic, kind and comment cannot be dependent.  */
   12298          261 :   gcc_checking_assert
   12299              :     (!type_dependent_expression_p (CONTRACT_EVALUATION_SEMANTIC (r))
   12300              :      && !type_dependent_expression_p (CONTRACT_ASSERTION_KIND (r))
   12301              :      && !type_dependent_expression_p (CONTRACT_COMMENT (r)));
   12302              : 
   12303          261 :   if (auto_p)
   12304           14 :     --processing_template_decl;
   12305          261 :   processing_postcondition = old_pc;
   12306          261 :   gcc_checking_assert (scope_chain && scope_chain->bindings
   12307              :                        && scope_chain->bindings->kind == sk_contract);
   12308          261 :   pop_bindings_and_leave_scope ();
   12309              : 
   12310          261 :   return r;
   12311              : }
   12312              : 
   12313              : /* Update T instantiating a contract specifier.  */
   12314              : 
   12315              : static void
   12316          262 : tsubst_contract_specifier (tree decl, tree t, tree args,
   12317              :                            tsubst_flags_t complain, tree in_decl)
   12318              : {
   12319              :   /* For non-specializations, adjust the current declaration to the most general
   12320              :      version of in_decl. Because we defer the instantiation of contracts as long
   12321              :      as possible, they are still written in terms of the parameters (and return
   12322              :      type) of the most general template.  */
   12323          262 :   tree tmpl = DECL_TI_TEMPLATE (in_decl);
   12324          262 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
   12325          250 :     in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
   12326          262 :   local_specialization_stack specs (lss_copy);
   12327          262 :   register_parameter_specializations (in_decl, decl);
   12328              : 
   12329              :   /* Get the contract to be instantiated.  */
   12330          262 :   tree contract = CONTRACT_STATEMENT (t);
   12331              : 
   12332              :   /* Use the complete set of template arguments for instantiation. The
   12333              :      contract may not have been instantiated and still refer to outer levels
   12334              :      of template parameters.  */
   12335          262 :   args = DECL_TI_ARGS (decl);
   12336              : 
   12337              :   /* For member functions, make this available for semantic analysis.  */
   12338          262 :   tree save_ccp = current_class_ptr;
   12339          262 :   tree save_ccr = current_class_ref;
   12340          262 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
   12341              :     {
   12342          184 :       tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
   12343          184 :       tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
   12344          184 :       inject_this_parameter (this_type, cp_type_quals (this_type));
   12345              :     }
   12346              : 
   12347          262 :   contract = tsubst_contract (decl, contract, args, complain, in_decl);
   12348              : 
   12349          262 :   current_class_ptr = save_ccp;
   12350          262 :   current_class_ref = save_ccr;
   12351              : 
   12352              :   /* Rebuild the attribute.  */
   12353          262 :   TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
   12354          262 : }
   12355              : 
   12356              : /* For unsubstituted list of contracts in SPECIFIERS, instantiate contracts
   12357              :  for DECL and set the list as contracts for decl. Substitution creates a deep
   12358              :  copy of the contract.  */
   12359              : 
   12360              : void
   12361          142 : tsubst_contract_specifiers (tree specfiers, tree decl, tree args,
   12362              :                             tsubst_flags_t complain, tree in_decl)
   12363              : {
   12364          142 :   tree subst_contract_list = NULL_TREE;
   12365          404 :   for (tree spec = specfiers; spec; spec = TREE_CHAIN (spec))
   12366              :     {
   12367          262 :       tree nc = copy_node (spec);
   12368          262 :       tsubst_contract_specifier (decl, nc, args, complain, in_decl);
   12369          262 :       TREE_CHAIN (nc) = subst_contract_list;
   12370          262 :       subst_contract_list = nc;
   12371              :     }
   12372          142 :   if (flag_contracts)
   12373          142 :     set_fn_contract_specifiers (decl, nreverse (subst_contract_list));
   12374          142 : }
   12375              : 
   12376              : /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
   12377              :    T or a new TREE_LIST, possibly a chain in the case of a pack expansion.  */
   12378              : 
   12379              : static tree
   12380      1063955 : tsubst_attribute (tree t, tree *decl_p, tree args,
   12381              :                   tsubst_flags_t complain, tree in_decl)
   12382              : {
   12383      1063955 :   gcc_assert (ATTR_IS_DEPENDENT (t));
   12384              : 
   12385      1063955 :   tree val = TREE_VALUE (t);
   12386      1063955 :   if (val == NULL_TREE)
   12387              :     /* Nothing to do.  */;
   12388       228762 :   else if ((flag_openmp || flag_openmp_simd)
   12389       229386 :            && is_attribute_p ("omp declare simd",
   12390          345 :                               get_attribute_name (t)))
   12391              :     {
   12392          105 :       tree clauses = TREE_VALUE (val);
   12393          105 :       clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
   12394              :                                     complain, in_decl);
   12395          105 :       c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   12396          105 :       clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   12397          105 :       tree parms = DECL_ARGUMENTS (*decl_p);
   12398          105 :       clauses
   12399          105 :         = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
   12400          105 :       if (clauses)
   12401          105 :         val = build_tree_list (NULL_TREE, clauses);
   12402              :       else
   12403              :         val = NULL_TREE;
   12404              :     }
   12405       228936 :   else if (flag_openmp
   12406       229176 :            && is_attribute_p ("omp declare variant base",
   12407          240 :                               get_attribute_name (t)))
   12408              :     {
   12409          213 :       ++cp_unevaluated_operand;
   12410          213 :       tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
   12411          213 :       --cp_unevaluated_operand;
   12412          213 :       tree chain = TREE_CHAIN (val);
   12413          213 :       location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
   12414          213 :       tree ctx = copy_list (TREE_VALUE (val));
   12415          213 :       tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
   12416          213 :       if (append_args_list
   12417           63 :           && TREE_VALUE (append_args_list)
   12418          276 :           && TREE_CHAIN (TREE_VALUE (append_args_list)))
   12419              :         {
   12420           54 :           append_args_list = TREE_VALUE (append_args_list);
   12421           54 :           append_args_list = TREE_VALUE (TREE_CHAIN (append_args_list));
   12422          153 :           for (; append_args_list;
   12423           99 :                append_args_list = TREE_CHAIN (append_args_list))
   12424              :              {
   12425           99 :               tree pref_list = TREE_VALUE (append_args_list);
   12426           99 :               if (pref_list == NULL_TREE || TREE_CODE (pref_list) != TREE_LIST)
   12427           63 :                 continue;
   12428           36 :               tree fr_list = TREE_VALUE (pref_list);
   12429           36 :               int len = TREE_VEC_LENGTH (fr_list);
   12430          105 :               for (int i = 0; i < len; i++)
   12431              :                 {
   12432           69 :                   tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
   12433              :                   /* Preserve NOP_EXPR to have a location.  */
   12434           69 :                   if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
   12435            0 :                     TREE_OPERAND (*fr_expr, 0)
   12436            0 :                       = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
   12437              :                                      in_decl);
   12438              :                   else
   12439           69 :                     *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
   12440              :                 }
   12441              :             }
   12442              :         }
   12443          423 :       for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
   12444              :         {
   12445          219 :           enum omp_tss_code set = OMP_TSS_CODE (tss);
   12446          219 :           tree selectors = NULL_TREE;
   12447          429 :           for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
   12448          210 :                ts = TREE_CHAIN (ts))
   12449              :             {
   12450          219 :               tree properties = NULL_TREE;
   12451          219 :               tree scoreval = NULL_TREE;
   12452              :               /* FIXME: The body of this loop should really be dispatching
   12453              :                  according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
   12454              :                  of having hard-wired knowledge of specific selectors.  */
   12455          219 :               if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
   12456          219 :                   && set == OMP_TRAIT_SET_CONSTRUCT)
   12457              :                 {
   12458            0 :                   tree clauses = OMP_TS_PROPERTIES (ts);
   12459            0 :                   clauses = tsubst_omp_clauses (clauses,
   12460              :                                                 C_ORT_OMP_DECLARE_SIMD, args,
   12461              :                                                 complain, in_decl);
   12462            0 :                   c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   12463            0 :                   clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   12464            0 :                   properties = clauses;
   12465              :                 }
   12466              :               else
   12467              :                 {
   12468          219 :                   tree v = OMP_TS_SCORE (ts);
   12469           45 :                   if (v)
   12470              :                     {
   12471           45 :                       v = tsubst_expr (v, args, complain, in_decl);
   12472           45 :                       v = fold_non_dependent_expr (v);
   12473           90 :                       if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   12474           87 :                           || TREE_CODE (v) != INTEGER_CST)
   12475              :                         {
   12476            3 :                           location_t loc
   12477            3 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   12478              :                                                   match_loc);
   12479            3 :                           error_at (loc, "score argument must be "
   12480              :                                     "constant integer expression");
   12481            3 :                           return NULL_TREE;
   12482              :                         }
   12483           42 :                       else if (tree_int_cst_sgn (v) < 0)
   12484              :                         {
   12485            3 :                           location_t loc
   12486            3 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   12487              :                                                   match_loc);
   12488            3 :                           error_at (loc, "score argument must be "
   12489              :                                     "non-negative");
   12490            3 :                           return NULL_TREE;
   12491              :                         }
   12492              :                       scoreval = v;
   12493              :                     }
   12494          213 :                   properties = copy_list (OMP_TS_PROPERTIES (ts));
   12495          350 :                   for (tree p = properties; p; p = TREE_CHAIN (p))
   12496          140 :                     if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
   12497           80 :                       continue;
   12498           60 :                     else if (OMP_TP_VALUE (p))
   12499              :                       {
   12500           60 :                         bool allow_string
   12501           60 :                           = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
   12502           60 :                              || set != OMP_TRAIT_SET_USER);
   12503           60 :                         tree v = OMP_TP_VALUE (p);
   12504           60 :                         if (TREE_CODE (v) == STRING_CST && allow_string)
   12505            0 :                           continue;
   12506           60 :                         v = tsubst_expr (v, args, complain, in_decl);
   12507           60 :                         v = fold_non_dependent_expr (v);
   12508          120 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   12509          117 :                             || !tree_fits_shwi_p (v))
   12510              :                           {
   12511            3 :                             location_t loc
   12512            3 :                               = cp_expr_loc_or_loc (OMP_TP_VALUE (p),
   12513              :                                                     match_loc);
   12514            3 :                             if (allow_string)
   12515            0 :                               error_at (loc, "property must be constant "
   12516              :                                              "integer expression or string "
   12517              :                                              "literal");
   12518              :                             else
   12519            3 :                               error_at (loc, "property must be constant "
   12520              :                                              "integer expression");
   12521            3 :                             return NULL_TREE;
   12522              :                           }
   12523           57 :                         OMP_TP_VALUE (p) = v;
   12524              :                       }
   12525              :                 }
   12526          210 :               selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
   12527              :                                                properties, selectors);
   12528              :             }
   12529          210 :           OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
   12530              :         }
   12531          204 :       if (varid == error_mark_node)
   12532              :         val = error_mark_node;
   12533              :       else
   12534          198 :         val = tree_cons (varid, ctx, chain);
   12535              :     }
   12536              :   /* If the first attribute argument is an identifier, don't
   12537              :      pass it through tsubst.  Attributes like mode, format,
   12538              :      cleanup and several target specific attributes expect it
   12539              :      unmodified.  */
   12540       228723 :   else if (get_attribute_namespace (t) == gnu_identifier
   12541       228723 :            && attribute_takes_identifier_p (get_attribute_name (t)))
   12542              :     {
   12543          109 :       tree chain
   12544          109 :         = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
   12545          109 :       if (chain != TREE_CHAIN (val))
   12546           12 :         val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
   12547              :     }
   12548       228614 :   else if (PACK_EXPANSION_P (val))
   12549              :     {
   12550              :       /* An attribute pack expansion.  */
   12551           20 :       tree purp = TREE_PURPOSE (t);
   12552           20 :       tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
   12553           20 :       if (pack == error_mark_node)
   12554              :         return error_mark_node;
   12555           17 :       int len = TREE_VEC_LENGTH (pack);
   12556           17 :       tree list = NULL_TREE;
   12557           17 :       tree *q = &list;
   12558           39 :       for (int i = 0; i < len; ++i)
   12559              :         {
   12560           22 :           tree elt = TREE_VEC_ELT (pack, i);
   12561           22 :           *q = build_tree_list (purp, elt);
   12562           22 :           q = &TREE_CHAIN (*q);
   12563              :         }
   12564           17 :       return list;
   12565              :     }
   12566              :   else
   12567       228594 :     val = tsubst_expr (val, args, complain, in_decl);
   12568              : 
   12569      1063926 :   if (val == error_mark_node)
   12570              :     return error_mark_node;
   12571      1063905 :   if (val != TREE_VALUE (t))
   12572       228256 :     return build_tree_list (TREE_PURPOSE (t), val);
   12573              :   return t;
   12574              : }
   12575              : 
   12576              : /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
   12577              :    unchanged or a new TREE_LIST chain.  */
   12578              : 
   12579              : static tree
   12580       361950 : tsubst_attributes (tree attributes, tree args,
   12581              :                    tsubst_flags_t complain, tree in_decl)
   12582              : {
   12583       361950 :   tree last_dep = NULL_TREE;
   12584              : 
   12585       361958 :   for (tree t = attributes; t; t = TREE_CHAIN (t))
   12586           24 :     if (ATTR_IS_DEPENDENT (t))
   12587              :       {
   12588           16 :         last_dep = t;
   12589           16 :         attributes = copy_list (attributes);
   12590           16 :         break;
   12591              :       }
   12592              : 
   12593       361950 :   if (last_dep)
   12594           32 :     for (tree *p = &attributes; *p; )
   12595              :       {
   12596           16 :         tree t = *p;
   12597           16 :         if (ATTR_IS_DEPENDENT (t))
   12598              :           {
   12599           16 :             tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
   12600           16 :             if (subst != t)
   12601              :               {
   12602            6 :                 *p = subst;
   12603            9 :                 while (*p)
   12604            3 :                   p = &TREE_CHAIN (*p);
   12605            6 :                 *p = TREE_CHAIN (t);
   12606            6 :                 continue;
   12607              :               }
   12608              :           }
   12609           10 :         p = &TREE_CHAIN (*p);
   12610              :       }
   12611              : 
   12612       361950 :   return attributes;
   12613              : }
   12614              : 
   12615              : /* Apply any attributes which had to be deferred until instantiation
   12616              :    time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
   12617              :    ARGS, COMPLAIN, IN_DECL are as tsubst.  Returns true normally,
   12618              :    false on error.  */
   12619              : 
   12620              : static bool
   12621   1372844697 : apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
   12622              :                                 tree args, tsubst_flags_t complain, tree in_decl)
   12623              : {
   12624   1372844697 :   tree t;
   12625   1372844697 :   tree *p;
   12626              : 
   12627   1372844697 :   if (attributes == NULL_TREE)
   12628              :     return true;
   12629              : 
   12630     32546552 :   if (DECL_P (*decl_p))
   12631              :     {
   12632     32524094 :       if (TREE_TYPE (*decl_p) == error_mark_node)
   12633              :         return false;
   12634     32524091 :       p = &DECL_ATTRIBUTES (*decl_p);
   12635              :       /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
   12636              :          to our attributes parameter.  */
   12637     32524091 :       gcc_assert (*p == attributes);
   12638              :     }
   12639        22458 :   else if (FUNC_OR_METHOD_TYPE_P (*decl_p)
   12640         2540 :            || (attr_flags & ATTR_FLAG_TYPE_IN_PLACE) == 0)
   12641              :     p = NULL;
   12642              :   else
   12643              :     {
   12644         2513 :       p = &TYPE_ATTRIBUTES (*decl_p);
   12645              :       /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
   12646              :          lookup_template_class_1, and should be preserved.  */
   12647         2513 :       gcc_assert (*p != attributes);
   12648         3012 :       while (*p)
   12649          499 :         p = &TREE_CHAIN (*p);
   12650              :     }
   12651              : 
   12652              :   /* save_template_attributes puts the dependent attributes at the beginning of
   12653              :      the list; find the non-dependent ones.  */
   12654     33610488 :   for (t = attributes; t; t = TREE_CHAIN (t))
   12655     33264597 :     if (!ATTR_IS_DEPENDENT (t))
   12656              :       break;
   12657     32546549 :   tree nondep = t;
   12658              : 
   12659              :   /* Apply any non-dependent attributes.  */
   12660     32546549 :   if (p)
   12661     32526604 :     *p = nondep;
   12662        19945 :   else if (nondep)
   12663        19883 :     *decl_p = cp_build_type_attribute_variant (*decl_p, nondep);
   12664              : 
   12665     32546549 :   if (nondep == attributes)
   12666              :     return true;
   12667              : 
   12668              :   /* And then any dependent ones.  */
   12669      1063862 :   tree late_attrs = NULL_TREE;
   12670      1063862 :   tree *q = &late_attrs;
   12671      2127777 :   for (t = attributes; t != nondep; t = TREE_CHAIN (t))
   12672              :     {
   12673      1063939 :       *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
   12674      1063939 :       if (*q == error_mark_node)
   12675              :         return false;
   12676      1063915 :       if (*q == t)
   12677              :         {
   12678       835639 :           *q = copy_node (t);
   12679       835639 :           TREE_CHAIN (*q) = NULL_TREE;
   12680              :         }
   12681      2127829 :       while (*q)
   12682      1063914 :         q = &TREE_CHAIN (*q);
   12683              :     }
   12684              : 
   12685              :   /* cplus_decl_attributes can add some attributes implicitly.  For templates,
   12686              :      those attributes should have been added already when those templates were
   12687              :      parsed, and shouldn't be added based on from which context they are
   12688              :      first time instantiated.  */
   12689      1063838 :   auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
   12690      1063838 :   auto o2 = make_temp_override (optimization_current_node,
   12691      1063838 :                                 optimization_default_node);
   12692      1063838 :   auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
   12693      1063838 :   auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
   12694      1063838 :                                 NULL);
   12695      1063838 :   auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
   12696      1063838 :   auto o6 = make_temp_override (target_option_current_node,
   12697      1063838 :                                 target_option_default_node);
   12698              : 
   12699      1063838 :   cplus_decl_attributes (decl_p, late_attrs, attr_flags);
   12700              : 
   12701      1063838 :   return true;
   12702      1063838 : }
   12703              : 
   12704              : /* The template TMPL is being instantiated with the template arguments TARGS.
   12705              :    Perform the access checks that we deferred when parsing the template.  */
   12706              : 
   12707              : static void
   12708     61538282 : perform_instantiation_time_access_checks (tree tmpl, tree targs)
   12709              : {
   12710     61538282 :   unsigned i;
   12711     61538282 :   deferred_access_check *chk;
   12712              : 
   12713     61538282 :   if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
   12714     61538282 :     return;
   12715              : 
   12716    123076564 :   if (vec<deferred_access_check, va_gc> *access_checks
   12717     61538282 :       = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
   12718          105 :     FOR_EACH_VEC_ELT (*access_checks, i, chk)
   12719              :       {
   12720           57 :         tree decl = chk->decl;
   12721           57 :         tree diag_decl = chk->diag_decl;
   12722           57 :         tree type_scope = TREE_TYPE (chk->binfo);
   12723              : 
   12724           57 :         if (uses_template_parms (type_scope))
   12725            6 :           type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
   12726              : 
   12727              :         /* Make access check error messages point to the location
   12728              :            of the use of the typedef.  */
   12729           57 :         iloc_sentinel ils (chk->loc);
   12730           57 :         perform_or_defer_access_check (TYPE_BINFO (type_scope),
   12731              :                                        decl, diag_decl, tf_warning_or_error);
   12732           57 :       }
   12733              : }
   12734              : 
   12735              : /* If the template T that we're about to instantiate contained errors at
   12736              :    parse time that we downgraded into warnings or suppressed, diagnose the
   12737              :    error now to render the TU ill-formed (if the TU has not already been
   12738              :    deemed ill-formed by an earlier error).  */
   12739              : 
   12740              : static void
   12741    101623464 : maybe_diagnose_erroneous_template (tree t)
   12742              : {
   12743    101623464 :   if (erroneous_templates && !(seen_error) ())
   12744         1050 :     if (location_t *error_loc = erroneous_templates->get (t))
   12745              :       {
   12746           15 :         auto_diagnostic_group d;
   12747           15 :         location_t decl_loc = location_of (t);
   12748           15 :         error_at (decl_loc, "instantiating erroneous template");
   12749           15 :         inform (*error_loc, "first error appeared here");
   12750           15 :       }
   12751    101623464 : }
   12752              : 
   12753              : tree
   12754    930309224 : instantiate_class_template (tree type)
   12755              : {
   12756    930309224 :   auto_timevar tv (TV_TEMPLATE_INST);
   12757              : 
   12758    930309224 :   tree templ, args, pattern, t, member;
   12759    930309224 :   tree typedecl;
   12760    930309224 :   tree pbinfo;
   12761    930309224 :   tree base_list;
   12762    930309224 :   unsigned int saved_maximum_field_alignment;
   12763    930309224 :   tree fn_context;
   12764              : 
   12765    930309224 :   if (type == error_mark_node)
   12766              :     return error_mark_node;
   12767              : 
   12768   1849277686 :   if (COMPLETE_OR_OPEN_TYPE_P (type)
   12769    979847464 :       || uses_template_parms (type))
   12770    889832518 :     return type;
   12771              : 
   12772              :   /* Figure out which template is being instantiated.  */
   12773     40476706 :   templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
   12774     40476706 :   gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
   12775              : 
   12776              :   /* Mark the type as in the process of being defined.  */
   12777     40476706 :   TYPE_BEING_DEFINED (type) = 1;
   12778              : 
   12779              :   /* We may be in the middle of deferred access check.  Disable
   12780              :      it now.  */
   12781     40476706 :   deferring_access_check_sentinel acs (dk_no_deferred);
   12782              : 
   12783              :   /* Determine what specialization of the original template to
   12784              :      instantiate.  */
   12785     40476706 :   t = most_specialized_partial_spec (type, tf_warning_or_error);
   12786     40476706 :   if (t == error_mark_node)
   12787              :     return error_mark_node;
   12788     40476691 :   else if (t)
   12789              :     {
   12790              :       /* This TYPE is actually an instantiation of a partial
   12791              :          specialization.  We replace the innermost set of ARGS with
   12792              :          the arguments appropriate for substitution.  For example,
   12793              :          given:
   12794              : 
   12795              :            template <class T> struct S {};
   12796              :            template <class T> struct S<T*> {};
   12797              : 
   12798              :          and supposing that we are instantiating S<int*>, ARGS will
   12799              :          presently be {int*} -- but we need {int}.  */
   12800      8680253 :       pattern = TREE_TYPE (TI_TEMPLATE (t));
   12801      8680253 :       args = TI_ARGS (t);
   12802              :     }
   12803              :   else
   12804              :     {
   12805     31796438 :       pattern = TREE_TYPE (templ);
   12806     31796438 :       args = CLASSTYPE_TI_ARGS (type);
   12807              :     }
   12808              : 
   12809              :   /* If the template we're instantiating is incomplete, then clearly
   12810              :      there's nothing we can do.  */
   12811     40476691 :   if (!COMPLETE_TYPE_P (pattern))
   12812              :     {
   12813              :       /* We can try again later.  */
   12814       865911 :       TYPE_BEING_DEFINED (type) = 0;
   12815       865911 :       return type;
   12816              :     }
   12817              : 
   12818              :   /* If we've recursively instantiated too many templates, stop.  */
   12819     39610780 :   if (! push_tinst_level (type))
   12820            0 :     return type;
   12821              : 
   12822     48290954 :   maybe_diagnose_erroneous_template (t ? TI_TEMPLATE (t) : templ);
   12823              : 
   12824     39610759 :   int saved_unevaluated_operand = cp_unevaluated_operand;
   12825     39610759 :   int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
   12826              : 
   12827     39610759 :   fn_context = decl_function_context (TYPE_MAIN_DECL (type));
   12828              :   /* Also avoid push_to_top_level for a lambda in an NSDMI.  */
   12829     78565910 :   if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
   12830            0 :     fn_context = error_mark_node;
   12831     39610759 :   if (!fn_context)
   12832     39042489 :     push_to_top_level ();
   12833              :   else
   12834              :     {
   12835       568270 :       cp_unevaluated_operand = 0;
   12836       568270 :       c_inhibit_evaluation_warnings = 0;
   12837              :     }
   12838              : 
   12839     39610759 :   mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
   12840              : 
   12841              :   /* Use #pragma pack from the template context.  */
   12842     39610759 :   saved_maximum_field_alignment = maximum_field_alignment;
   12843     39610759 :   maximum_field_alignment = TYPE_PRECISION (pattern);
   12844              : 
   12845     39610759 :   SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
   12846              : 
   12847              :   /* Set the input location to the most specialized template definition.
   12848              :      This is needed if tsubsting causes an error.  */
   12849     39610759 :   typedecl = TYPE_MAIN_DECL (pattern);
   12850     39610759 :   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
   12851     39610759 :     DECL_SOURCE_LOCATION (typedecl);
   12852              : 
   12853     39610759 :   set_instantiating_module (TYPE_NAME (type));
   12854              : 
   12855     39610759 :   TYPE_PACKED (type) = TYPE_PACKED (pattern);
   12856     39610759 :   SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
   12857     39610759 :   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
   12858     39610759 :   CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
   12859     39610759 :   if (ANON_AGGR_TYPE_P (pattern))
   12860        83308 :     SET_ANON_AGGR_TYPE_P (type);
   12861     39610759 :   if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
   12862              :     {
   12863     39028819 :       CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
   12864     39028819 :       CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
   12865              :       /* Adjust visibility for template arguments.  */
   12866     39028819 :       determine_visibility (TYPE_MAIN_DECL (type));
   12867              :     }
   12868     39610759 :   if (CLASS_TYPE_P (type))
   12869     39610759 :     CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
   12870              : 
   12871     39610759 :   pbinfo = TYPE_BINFO (pattern);
   12872              : 
   12873              :   /* We should never instantiate a nested class before its enclosing
   12874              :      class; we need to look up the nested class by name before we can
   12875              :      instantiate it, and that lookup should instantiate the enclosing
   12876              :      class.  */
   12877     39610759 :   gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
   12878              :               || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
   12879              : 
   12880              :   /* When instantiating nested lambdas, ensure that they get the mangling
   12881              :      scope of the new class type.  */
   12882     39610759 :   start_lambda_scope (TYPE_NAME (type));
   12883              : 
   12884     39610759 :   base_list = NULL_TREE;
   12885              :   /* Defer access checking while we substitute into the types named in
   12886              :      the base-clause.  */
   12887     39610759 :   push_deferring_access_checks (dk_deferred);
   12888     39610759 :   if (BINFO_N_BASE_BINFOS (pbinfo))
   12889              :     {
   12890              :       tree pbase_binfo;
   12891              :       int i;
   12892              : 
   12893              :       /* Substitute into each of the bases to determine the actual
   12894              :          basetypes.  */
   12895     42390623 :       for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
   12896              :         {
   12897     21452257 :           tree base;
   12898     21452257 :           tree access = BINFO_BASE_ACCESS (pbinfo, i);
   12899     21452257 :           tree annotations = NULL_TREE;
   12900     21452257 :           tree expanded_bases = NULL_TREE;
   12901     21452257 :           int idx, len = 1;
   12902              : 
   12903     21452257 :           if (i + BINFO_BASE_BINFOS (pbinfo)->length ()
   12904     21452257 :               < vec_safe_length (BINFO_BASE_ACCESSES (pbinfo)))
   12905            6 :             annotations
   12906            6 :               = BINFO_BASE_ACCESS (pbinfo,
   12907              :                                    i + BINFO_BASE_BINFOS (pbinfo)->length ());
   12908              : 
   12909     21452257 :           if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
   12910              :             {
   12911          483 :               expanded_bases
   12912          483 :                 = tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
   12913              :                                          args, tf_error, NULL_TREE);
   12914          483 :               if (expanded_bases == error_mark_node)
   12915            0 :                 continue;
   12916              : 
   12917          483 :               len = TREE_VEC_LENGTH (expanded_bases);
   12918              :             }
   12919              : 
   12920     21452257 :           if (annotations)
   12921            4 :             annotations = tsubst_attributes (annotations, args,
   12922              :                                              tf_warning_or_error, NULL_TREE);
   12923              : 
   12924     42905124 :           for (idx = 0; idx < len; idx++)
   12925              :             {
   12926     21452867 :               if (expanded_bases)
   12927              :                 /* Extract the already-expanded base class.  */
   12928         1093 :                 base = TREE_VEC_ELT (expanded_bases, idx);
   12929              :               else
   12930              :                 /* Substitute to figure out the base class.  */
   12931     21451774 :                 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
   12932              :                                NULL_TREE);
   12933              : 
   12934     21452867 :               if (base == error_mark_node)
   12935           32 :                 continue;
   12936              : 
   12937     21452835 :               tree acc = access;
   12938     21452835 :               if (annotations)
   12939              :                 /* Make sure each direct base from the pack has its unique
   12940              :                    set of annotations.  */
   12941            7 :                 acc = build_tree_list (access, idx == 0 ? annotations
   12942            2 :                                                : copy_list (annotations));
   12943     21452835 :               base_list = tree_cons (acc, base, base_list);
   12944     21452835 :               if (BINFO_VIRTUAL_P (pbase_binfo))
   12945        41948 :                 TREE_TYPE (base_list) = integer_type_node;
   12946              :             }
   12947              :         }
   12948              : 
   12949              :       /* The list is now in reverse order; correct that.  */
   12950     20938366 :       base_list = nreverse (base_list);
   12951              :     }
   12952              :   /* Now call xref_basetypes to set up all the base-class
   12953              :      information.  */
   12954     39610759 :   xref_basetypes (type, base_list);
   12955              : 
   12956     39610759 :   apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
   12957              :                                   (int) ATTR_FLAG_TYPE_IN_PLACE,
   12958              :                                   args, tf_error, NULL_TREE);
   12959     39610759 :   fixup_attribute_variants (type);
   12960              : 
   12961              :   /* Now that our base classes are set up, enter the scope of the
   12962              :      class, so that name lookups into base classes, etc. will work
   12963              :      correctly.  This is precisely analogous to what we do in
   12964              :      begin_class_definition when defining an ordinary non-template
   12965              :      class, except we also need to push the enclosing classes.  */
   12966     39610759 :   push_nested_class (type);
   12967              : 
   12968              :   /* Now check accessibility of the types named in its base-clause,
   12969              :      relative to the scope of the class.  */
   12970     39610759 :   pop_to_parent_deferring_access_checks ();
   12971              : 
   12972              :   /* A vector to hold members marked with attribute used. */
   12973     39610759 :   auto_vec<tree> used;
   12974              : 
   12975              :   /* Now members are processed in the order of declaration.  */
   12976     39610759 :   for (member = CLASSTYPE_DECL_LIST (pattern);
   12977    254143069 :        member; member = TREE_CHAIN (member))
   12978              :     {
   12979    214535061 :       tree t = TREE_VALUE (member);
   12980              : 
   12981    214535061 :       if (TREE_PURPOSE (member))
   12982              :         {
   12983    210975341 :           if (TYPE_P (t))
   12984              :             {
   12985      3205086 :               if (LAMBDA_TYPE_P (t))
   12986              :                 /* A closure type for a lambda in an NSDMI or default argument.
   12987              :                    Ignore it; it will be regenerated when needed.  */
   12988         4269 :                 continue;
   12989              : 
   12990              :               /* If the member is a class template, we've
   12991              :                  already substituted its type.  */
   12992      1460395 :               if (CLASS_TYPE_P (t)
   12993      3282442 :                   && CLASSTYPE_IS_TEMPLATE (t))
   12994       580944 :                 continue;
   12995              : 
   12996      1241103 :               tree newtag = tsubst (t, args, tf_error, NULL_TREE);
   12997      1241103 :               if (newtag == error_mark_node)
   12998            0 :                 continue;
   12999              : 
   13000      1241103 :               if (TREE_CODE (newtag) != ENUMERAL_TYPE)
   13001              :                 {
   13002       879451 :                   tree name = TYPE_IDENTIFIER (t);
   13003              : 
   13004              :                   /* Now, install the tag.  We don't use pushtag
   13005              :                      because that does too much work -- creating an
   13006              :                      implicit typedef, which we've already done.  */
   13007       879451 :                   set_identifier_type_value (name, TYPE_NAME (newtag));
   13008       879451 :                   maybe_add_class_template_decl_list (type, newtag, false);
   13009       879451 :                   TREE_PUBLIC (TYPE_NAME (newtag)) = true;
   13010       879451 :                   determine_visibility (TYPE_NAME (newtag));
   13011              :                 }
   13012              :             }
   13013    209149025 :           else if (DECL_DECLARES_FUNCTION_P (t))
   13014              :             {
   13015    105924805 :               tree r;
   13016              : 
   13017    105924805 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13018     20726608 :                 ++processing_template_decl;
   13019    105924805 :               r = tsubst (t, args, tf_error, NULL_TREE);
   13020    105924805 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13021     20726608 :                 --processing_template_decl;
   13022              : 
   13023    105924805 :               set_current_access_from_decl (r);
   13024    105924805 :               finish_member_declaration (r);
   13025              :               /* Instantiate members marked with attribute used.  */
   13026    105924805 :               if (r != error_mark_node && DECL_PRESERVE_P (r))
   13027           24 :                 used.safe_push (r);
   13028    105924805 :               if (TREE_CODE (r) == FUNCTION_DECL
   13029    105924805 :                   && DECL_OMP_DECLARE_REDUCTION_P (r))
   13030          134 :                 cp_check_omp_declare_reduction (r);
   13031              :             }
   13032    103224220 :           else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
   13033    106429353 :                    && LAMBDA_TYPE_P (TREE_TYPE (t)))
   13034              :             /* A closure type for a lambda in an NSDMI or default argument.
   13035              :                Ignore it; it will be regenerated when needed.  */;
   13036              :           else
   13037              :             {
   13038              :               /* Build new TYPE_FIELDS.  */
   13039    103219947 :               if (TREE_CODE (t) == STATIC_ASSERT)
   13040      5168298 :                 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE);
   13041     98051649 :               else if (TREE_CODE (t) != CONST_DECL)
   13042              :                 {
   13043     98051649 :                   tree r;
   13044     98051649 :                   tree vec = NULL_TREE;
   13045     98051649 :                   int len = 1;
   13046              : 
   13047     98051649 :                   gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
   13048              :                   /* The file and line for this declaration, to
   13049              :                      assist in error message reporting.  Since we
   13050              :                      called push_tinst_level above, we don't need to
   13051              :                      restore these.  */
   13052     98051649 :                   input_location = DECL_SOURCE_LOCATION (t);
   13053              : 
   13054     98051649 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   13055      2917218 :                     ++processing_template_decl;
   13056     98051649 :                   r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
   13057     98051598 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   13058      2917218 :                     --processing_template_decl;
   13059              : 
   13060     98051598 :                   if (TREE_CODE (r) == TREE_VEC)
   13061              :                     {
   13062              :                       /* A capture pack became multiple fields.  */
   13063          268 :                       vec = r;
   13064          268 :                       len = TREE_VEC_LENGTH (vec);
   13065              :                     }
   13066              : 
   13067    196100900 :                   for (int i = 0; i < len; ++i)
   13068              :                     {
   13069     98052002 :                       if (vec)
   13070          672 :                         r = TREE_VEC_ELT (vec, i);
   13071     98052002 :                       if (VAR_P (r))
   13072              :                         {
   13073              :                           /* In [temp.inst]:
   13074              : 
   13075              :                              [t]he initialization (and any associated
   13076              :                              side-effects) of a static data member does
   13077              :                              not occur unless the static data member is
   13078              :                              itself used in a way that requires the
   13079              :                              definition of the static data member to
   13080              :                              exist.
   13081              : 
   13082              :                              Therefore, we do not substitute into the
   13083              :                              initialized for the static data member here.  */
   13084      3716131 :                           finish_static_data_member_decl
   13085      3716131 :                             (r,
   13086              :                              /*init=*/NULL_TREE,
   13087              :                              /*init_const_expr_p=*/false,
   13088              :                              /*asmspec_tree=*/NULL_TREE,
   13089              :                              /*flags=*/0);
   13090              :                           /* Instantiate members marked with attribute used. */
   13091      3716131 :                           if (r != error_mark_node && DECL_PRESERVE_P (r))
   13092            6 :                             used.safe_push (r);
   13093              :                         }
   13094     94335871 :                       else if (TREE_CODE (r) == FIELD_DECL)
   13095              :                         {
   13096              :                           /* Determine whether R has a valid type and can be
   13097              :                              completed later.  If R is invalid, then its type
   13098              :                              is replaced by error_mark_node.  */
   13099      7265189 :                           tree rtype = TREE_TYPE (r);
   13100      7265189 :                           if (can_complete_type_without_circularity (rtype))
   13101      7265180 :                             complete_type (rtype);
   13102              : 
   13103      7262489 :                           if (!complete_or_array_type_p (rtype))
   13104              :                             {
   13105              :                               /* If R's type couldn't be completed and
   13106              :                                  it isn't a flexible array member (whose
   13107              :                                  type is incomplete by definition) give
   13108              :                                  an error.  */
   13109           29 :                               cxx_incomplete_type_error (r, rtype);
   13110           29 :                               TREE_TYPE (r) = error_mark_node;
   13111              :                             }
   13112      7262460 :                           else if (TREE_CODE (rtype) == ARRAY_TYPE
   13113       213902 :                                    && TYPE_DOMAIN (rtype) == NULL_TREE
   13114      7262460 :                                    && (TREE_CODE (type) == UNION_TYPE
   13115           48 :                                        || TREE_CODE (type) == QUAL_UNION_TYPE))
   13116              :                             {
   13117            4 :                               error ("flexible array member %qD in union", r);
   13118            4 :                               TREE_TYPE (r) = error_mark_node;
   13119              :                             }
   13120      7262456 :                           else if (!verify_type_context (input_location,
   13121              :                                                          TCTX_FIELD, rtype))
   13122            0 :                             TREE_TYPE (r) = error_mark_node;
   13123              :                         }
   13124              : 
   13125              :                       /* If it is a TYPE_DECL for a class-scoped
   13126              :                          ENUMERAL_TYPE, such a thing will already have
   13127              :                          been added to the field list by tsubst_enum
   13128              :                          in finish_member_declaration case above.  */
   13129     98049302 :                       if (!(TREE_CODE (r) == TYPE_DECL
   13130     82498298 :                             && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
   13131       580491 :                             && DECL_ARTIFICIAL (r)))
   13132              :                         {
   13133     97687650 :                           set_current_access_from_decl (r);
   13134     97687650 :                           finish_member_declaration (r);
   13135              :                         }
   13136              :                     }
   13137              :                 }
   13138              :             }
   13139              :         }
   13140              :       else
   13141              :         {
   13142      3559720 :           if (TREE_CODE (t) == TU_LOCAL_ENTITY)
   13143              :             /* Ignore.  */;
   13144      3065071 :           else if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
   13145      6099152 :                    || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   13146              :             {
   13147              :               /* Build new CLASSTYPE_FRIEND_CLASSES.  */
   13148              : 
   13149      1020207 :               tree friend_type = t;
   13150      1020207 :               if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   13151              :                 {
   13152              :                   /* template <class T> friend class C;  */
   13153       525600 :                   friend_type = tsubst_friend_class (friend_type, args);
   13154              :                 }
   13155       494607 :               else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
   13156              :                 {
   13157              :                   /* template <class T> friend class C::D;  */
   13158           15 :                   friend_type = tsubst (friend_type, args,
   13159              :                                         tf_warning_or_error, NULL_TREE);
   13160           15 :                   if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   13161           12 :                     friend_type = TREE_TYPE (friend_type);
   13162              :                 }
   13163       494592 :               else if (TREE_CODE (friend_type) == TYPENAME_TYPE
   13164       494592 :                        || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
   13165              :                 {
   13166              :                   /* This could be either
   13167              : 
   13168              :                        friend class T::C;
   13169              : 
   13170              :                      when dependent_type_p is false or
   13171              : 
   13172              :                        template <class U> friend class T::C;
   13173              : 
   13174              :                      otherwise.  */
   13175              :                   /* Bump processing_template_decl in case this is something like
   13176              :                      template <class T> friend struct A<T>::B.  */
   13177         2172 :                   ++processing_template_decl;
   13178         2172 :                   friend_type = tsubst (friend_type, args,
   13179              :                                         tf_warning_or_error, NULL_TREE);
   13180         2172 :                   --processing_template_decl;
   13181              :                 }
   13182       492420 :               else if (PACK_EXPANSION_P (friend_type))
   13183              :                 {
   13184           24 :                   friend_type = tsubst_pack_expansion (friend_type, args,
   13185              :                                                        tf_warning_or_error,
   13186              :                                                        NULL_TREE);
   13187           24 :                   if (friend_type != error_mark_node)
   13188              :                     {
   13189           24 :                       unsigned int len = TREE_VEC_LENGTH (friend_type);
   13190           63 :                       for (unsigned int idx = 0; idx < len; ++idx)
   13191           39 :                         if (TREE_VEC_ELT (friend_type, idx) != error_mark_node)
   13192           39 :                           make_friend_class (type,
   13193           39 :                                              TREE_VEC_ELT (friend_type, idx),
   13194              :                                              /*complain=*/false);
   13195              :                     }
   13196           24 :                   friend_type = error_mark_node;
   13197              :                 }
   13198       492396 :               else if (uses_template_parms (friend_type))
   13199              :                 /* friend class C<T>;  */
   13200       462024 :                 friend_type = tsubst (friend_type, args,
   13201              :                                       tf_warning_or_error, NULL_TREE);
   13202              : 
   13203              :               /* Otherwise it's
   13204              : 
   13205              :                    friend class C;
   13206              : 
   13207              :                  where C is already declared or
   13208              : 
   13209              :                    friend class C<int>;
   13210              : 
   13211              :                  We don't have to do anything in these cases.  */
   13212              : 
   13213      1020207 :               if (friend_type != error_mark_node)
   13214      1020163 :                 make_friend_class (type, friend_type, /*complain=*/false);
   13215              :             }
   13216              :           else
   13217              :             {
   13218              :               /* Build new DECL_FRIENDLIST.  */
   13219      2539471 :               tree r;
   13220              : 
   13221              :               /* The file and line for this declaration, to
   13222              :                  assist in error message reporting.  Since we
   13223              :                  called push_tinst_level above, we don't need to
   13224              :                  restore these.  */
   13225      2539471 :               input_location = DECL_SOURCE_LOCATION (t);
   13226              : 
   13227      2539471 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13228              :                 {
   13229      1632086 :                   ++processing_template_decl;
   13230      1632086 :                   push_deferring_access_checks (dk_no_check);
   13231              :                 }
   13232              : 
   13233      2539471 :               r = tsubst_friend_function (t, args);
   13234      2539471 :               add_friend (type, r, /*complain=*/false);
   13235      2539471 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13236              :                 {
   13237      1632086 :                   pop_deferring_access_checks ();
   13238      1632086 :                   --processing_template_decl;
   13239              :                 }
   13240              :             }
   13241              :         }
   13242              :     }
   13243              : 
   13244     39608008 :   if (fn_context)
   13245              :     {
   13246              :       /* Restore these before substituting into the lambda capture
   13247              :          initializers.  */
   13248       568270 :       cp_unevaluated_operand = saved_unevaluated_operand;
   13249       568270 :       c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
   13250              :     }
   13251              : 
   13252              :   /* Set the file and line number information to whatever is given for
   13253              :      the class itself.  This puts error messages involving generated
   13254              :      implicit functions at a predictable point, and the same point
   13255              :      that would be used for non-template classes.  */
   13256     39608008 :   input_location = DECL_SOURCE_LOCATION (typedecl);
   13257              : 
   13258     39608008 :   unreverse_member_declarations (type);
   13259     39608008 :   finish_struct_1 (type);
   13260     39608008 :   TYPE_BEING_DEFINED (type) = 0;
   13261              : 
   13262     39608008 :   finish_lambda_scope ();
   13263              : 
   13264              :   /* Remember if instantiating this class ran into errors, so we can avoid
   13265              :      instantiating member functions in limit_bad_template_recursion.  We set
   13266              :      this flag even if the problem was in another instantiation triggered by
   13267              :      this one, as that will likely also cause trouble for member functions.  */
   13268     39608008 :   if (errorcount + sorrycount > current_tinst_level->errors)
   13269         1306 :     CLASSTYPE_ERRONEOUS (type) = true;
   13270              : 
   13271              :   /* We don't instantiate default arguments for member functions.  14.7.1:
   13272              : 
   13273              :      The implicit instantiation of a class template specialization causes
   13274              :      the implicit instantiation of the declarations, but not of the
   13275              :      definitions or default arguments, of the class member functions,
   13276              :      member classes, static data members and member templates....  */
   13277              : 
   13278     39608008 :   perform_instantiation_time_access_checks (pattern, args);
   13279     39608008 :   perform_deferred_access_checks (tf_warning_or_error);
   13280              : 
   13281              :   /* Now that we've gone through all the members, instantiate those
   13282              :      marked with attribute used.  We must do this in the context of
   13283              :      the class -- not the context we pushed from, as that might be
   13284              :      inside a template and change the behaviour of mark_used.  */
   13285     39608092 :   for (tree x : used)
   13286           30 :     mark_used (x);
   13287              : 
   13288     39608008 :   pop_nested_class ();
   13289     39608008 :   maximum_field_alignment = saved_maximum_field_alignment;
   13290     39608008 :   if (!fn_context)
   13291     39039738 :     pop_from_top_level ();
   13292     39608008 :   pop_tinst_level ();
   13293              : 
   13294              :   /* The vtable for a template class can be emitted in any translation
   13295              :      unit in which the class is instantiated.  When there is no key
   13296              :      method, however, finish_struct_1 will already have added TYPE to
   13297              :      the keyed_classes.  */
   13298     39608008 :   if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
   13299            0 :     vec_safe_push (keyed_classes, type);
   13300              : 
   13301     39608008 :   return type;
   13302    969914460 : }
   13303              : 
   13304              : tree
   13305   3199875690 : tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13306              : {
   13307   3199875690 :   tree r;
   13308              : 
   13309   3199875690 :   if (!t)
   13310              :     r = t;
   13311   3165780685 :   else if (TYPE_P (t))
   13312   2882062135 :     r = tsubst (t, args, complain, in_decl);
   13313              :   else
   13314              :     {
   13315    283718550 :       if (!(complain & tf_warning))
   13316    178194788 :         ++c_inhibit_evaluation_warnings;
   13317    283718550 :       r = tsubst_expr (t, args, complain, in_decl);
   13318    283718514 :       if (!(complain & tf_warning))
   13319    178194758 :         --c_inhibit_evaluation_warnings;
   13320              :     }
   13321              : 
   13322   3199875654 :   return r;
   13323              : }
   13324              : 
   13325              : /* Given a function parameter pack TMPL_PARM and some function parameters
   13326              :    instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
   13327              :    and set *SPEC_P to point at the next point in the list.  */
   13328              : 
   13329              : tree
   13330       714170 : extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
   13331              : {
   13332              :   /* Collect all of the extra "packed" parameters into an
   13333              :      argument pack.  */
   13334       714170 :   tree argpack;
   13335       714170 :   tree spec_parm = *spec_p;
   13336       714170 :   int len;
   13337              : 
   13338      1243174 :   for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
   13339       529373 :     if (tmpl_parm
   13340       529373 :         && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
   13341              :       break;
   13342              : 
   13343       714170 :   spec_parm = *spec_p;
   13344       714170 :   if (len == 1 && DECL_PACK_P (spec_parm))
   13345              :     {
   13346              :       /* The instantiation is still a parameter pack; don't wrap it in a
   13347              :          NONTYPE_ARGUMENT_PACK.  */
   13348         2930 :       argpack = spec_parm;
   13349         2930 :       spec_parm = DECL_CHAIN (spec_parm);
   13350              :     }
   13351              :   else
   13352              :     {
   13353              :       /* Fill in PARMVEC with all of the parameters.  */
   13354       711240 :       tree parmvec = make_tree_vec (len);
   13355       711240 :       argpack = make_node (NONTYPE_ARGUMENT_PACK);
   13356      1237314 :       for (int i = 0; i < len; i++)
   13357              :         {
   13358       526074 :           tree elt = spec_parm;
   13359       526074 :           if (DECL_PACK_P (elt))
   13360            0 :             elt = make_pack_expansion (elt);
   13361       526074 :           TREE_VEC_ELT (parmvec, i) = elt;
   13362       526074 :           spec_parm = DECL_CHAIN (spec_parm);
   13363              :         }
   13364              : 
   13365              :       /* Build the argument packs.  */
   13366       711240 :       ARGUMENT_PACK_ARGS (argpack) = parmvec;
   13367              :     }
   13368       714170 :   *spec_p = spec_parm;
   13369              : 
   13370       714170 :   return argpack;
   13371              : }
   13372              : 
   13373              : /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
   13374              :    NONTYPE_ARGUMENT_PACK.  */
   13375              : 
   13376              : static tree
   13377         1025 : make_fnparm_pack (tree spec_parm)
   13378              : {
   13379            0 :   return extract_fnparm_pack (NULL_TREE, &spec_parm);
   13380              : }
   13381              : 
   13382              : /* Return 1 if the Ith element of the argument pack ARG_PACK is a
   13383              :    pack expansion with no extra args, 2 if it has extra args, or 0
   13384              :    if it is not a pack expansion.  */
   13385              : 
   13386              : static int
   13387     22749148 : argument_pack_element_is_expansion_p (tree arg_pack, int i)
   13388              : {
   13389     22749148 :   if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   13390              :     /* We're being called before this happens in tsubst_pack_expansion.  */
   13391            0 :     arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   13392     22749148 :   tree vec = ARGUMENT_PACK_ARGS (arg_pack);
   13393     22749148 :   if (i >= TREE_VEC_LENGTH (vec))
   13394              :     return 0;
   13395     22749148 :   tree elt = TREE_VEC_ELT (vec, i);
   13396     22749148 :   if (DECL_P (elt))
   13397              :     /* A decl pack is itself an expansion.  */
   13398      1143049 :     elt = TREE_TYPE (elt);
   13399     22749148 :   if (!PACK_EXPANSION_P (elt))
   13400              :     return 0;
   13401       416958 :   if (PACK_EXPANSION_EXTRA_ARGS (elt))
   13402            3 :     return 2;
   13403              :   return 1;
   13404              : }
   13405              : 
   13406              : 
   13407              : /* Creates and return an ARGUMENT_PACK_SELECT tree node.  */
   13408              : 
   13409              : static tree
   13410      6385690 : make_argument_pack_select (tree arg_pack, unsigned index)
   13411              : {
   13412      6385690 :   tree aps = make_node (ARGUMENT_PACK_SELECT);
   13413              : 
   13414      6385690 :   ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
   13415      6385690 :   ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   13416              : 
   13417      6385690 :   return aps;
   13418              : }
   13419              : 
   13420              : /*  This is a subroutine of tsubst_pack_expansion.
   13421              : 
   13422              :     It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
   13423              :     mechanism to store the (non complete list of) arguments of the
   13424              :     substitution and return a non substituted pack expansion, in order
   13425              :     to wait for when we have enough arguments to really perform the
   13426              :     substitution.  */
   13427              : 
   13428              : static bool
   13429     22522619 : use_pack_expansion_extra_args_p (tree t,
   13430              :                                  tree parm_packs,
   13431              :                                  int arg_pack_len,
   13432              :                                  bool has_empty_arg)
   13433              : {
   13434     22522619 :   if (has_empty_arg
   13435     22522619 :       && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
   13436              :     return true;
   13437              : 
   13438              :   /* If one pack has an expansion and another pack has a normal
   13439              :      argument or if one pack has an empty argument and an another
   13440              :      one hasn't then tsubst_pack_expansion cannot perform the
   13441              :      substitution and need to fall back on the
   13442              :      PACK_EXPANSION_EXTRA mechanism.  */
   13443     22522608 :   if (parm_packs == NULL_TREE)
   13444              :     return false;
   13445      7016542 :   else if (has_empty_arg)
   13446              :     {
   13447              :       /* If all the actual packs are pack expansions, we can still
   13448              :          subsitute directly.  */
   13449          802 :       for (tree p = parm_packs; p; p = TREE_CHAIN (p))
   13450              :         {
   13451          802 :           tree a = TREE_VALUE (p);
   13452          802 :           if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
   13453            0 :             a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
   13454          802 :           a = ARGUMENT_PACK_ARGS (a);
   13455          802 :           if (TREE_VEC_LENGTH (a) == 1)
   13456          169 :             a = TREE_VEC_ELT (a, 0);
   13457          802 :           if (PACK_EXPANSION_P (a))
   13458            0 :             continue;
   13459              :           return true;
   13460              :         }
   13461              :       return false;
   13462              :     }
   13463              : 
   13464     17848605 :   for (int i = 0 ; i < arg_pack_len; ++i)
   13465              :     {
   13466              :       bool has_expansion_arg = false;
   13467              :       bool has_non_expansion_arg = false;
   13468     11729736 :       for (tree parm_pack = parm_packs;
   13469     22562613 :            parm_pack;
   13470     11729736 :            parm_pack = TREE_CHAIN (parm_pack))
   13471              :         {
   13472     11729739 :           tree arg = TREE_VALUE (parm_pack);
   13473              : 
   13474     11729739 :           int exp = argument_pack_element_is_expansion_p (arg, i);
   13475     11729739 :           if (exp == 2)
   13476              :             /* We can't substitute a pack expansion with extra args into
   13477              :                our pattern.  */
   13478              :             return true;
   13479     11729736 :           else if (exp)
   13480              :             has_expansion_arg = true;
   13481              :           else
   13482     11521254 :             has_non_expansion_arg = true;
   13483              :         }
   13484              : 
   13485     10832874 :       if (has_expansion_arg && has_non_expansion_arg)
   13486              :         {
   13487              :           /* We can get here with:
   13488              : 
   13489              :               template <class... Ts> struct X {
   13490              :                 template <class... Us> using Y = Z<void(Ts, Us)...>;
   13491              :               };
   13492              :               template <class A, class... P>
   13493              :               using foo = X<int, int>::Y<A, P...>;
   13494              : 
   13495              :              where we compare int and A and then the second int and P...,
   13496              :              whose expansion-ness doesn't match, but that's OK.  */
   13497              :           return true;
   13498              :         }
   13499              :     }
   13500              :   return false;
   13501              : }
   13502              : 
   13503              : /* [temp.variadic]/6 says that:
   13504              : 
   13505              :        The instantiation of a pack expansion [...]
   13506              :        produces a list E1,E2, ..., En, where N is the number of elements
   13507              :        in the pack expansion parameters.
   13508              : 
   13509              :    This subroutine of tsubst_pack_expansion produces one of these Ei.
   13510              : 
   13511              :    PATTERN is the pattern of the pack expansion.  PARM_PACKS is a
   13512              :    TREE_LIST in which each TREE_PURPOSE is a parameter pack of
   13513              :    PATTERN, and each TREE_VALUE is its corresponding argument pack.
   13514              :    INDEX is the index 'i' of the element Ei to produce.  ARGS,
   13515              :    COMPLAIN, and IN_DECL are the same parameters as for the
   13516              :    tsubst_pack_expansion function.
   13517              : 
   13518              :    The function returns the resulting Ei upon successful completion,
   13519              :    or error_mark_node.
   13520              : 
   13521              :    Note that this function possibly modifies the ARGS parameter, so
   13522              :    it's the responsibility of the caller to restore it.  */
   13523              : 
   13524              : static tree
   13525     10122562 : gen_elem_of_pack_expansion_instantiation (tree pattern,
   13526              :                                           tree parm_packs,
   13527              :                                           unsigned index,
   13528              :                                           tree args /* This parm gets
   13529              :                                                        modified.  */,
   13530              :                                           tsubst_flags_t complain,
   13531              :                                           tree in_decl)
   13532              : {
   13533     10122562 :   tree t;
   13534     10122562 :   bool ith_elem_is_expansion = false;
   13535              : 
   13536              :   /* For each parameter pack, change the substitution of the parameter
   13537              :      pack to the ith argument in its argument pack, then expand the
   13538              :      pattern.  */
   13539     21141971 :   for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
   13540              :     {
   13541     11019409 :       tree parm = TREE_PURPOSE (pack);
   13542     11019409 :       tree arg_pack = TREE_VALUE (pack);
   13543     11019409 :       tree aps;                 /* instance of ARGUMENT_PACK_SELECT.  */
   13544              : 
   13545     22038818 :       ith_elem_is_expansion |=
   13546     11019409 :         argument_pack_element_is_expansion_p (arg_pack, index);
   13547              : 
   13548              :       /* Select the Ith argument from the pack.  */
   13549     11019409 :       if (TREE_CODE (parm) == PARM_DECL
   13550     10467428 :           || VAR_P (parm)
   13551     10448074 :           || TREE_CODE (parm) == FIELD_DECL)
   13552              :         {
   13553       571351 :           if (index == 0)
   13554              :             {
   13555       423715 :               aps = make_argument_pack_select (arg_pack, index);
   13556       423715 :               if (!mark_used (parm, complain) && !(complain & tf_error))
   13557            0 :                 return error_mark_node;
   13558       423715 :               register_local_specialization (aps, parm);
   13559              :             }
   13560              :           else
   13561       147636 :             aps = retrieve_local_specialization (parm);
   13562              :         }
   13563              :       else
   13564              :         {
   13565     10448058 :           int idx, level;
   13566     10448058 :           template_parm_level_and_index (parm, &level, &idx);
   13567              : 
   13568     10448058 :           if (index == 0)
   13569              :             {
   13570      5961975 :               aps = make_argument_pack_select (arg_pack, index);
   13571              :               /* Update the corresponding argument.  */
   13572     11923950 :               TMPL_ARG (args, level, idx) = aps;
   13573              :             }
   13574              :           else
   13575              :             /* Re-use the ARGUMENT_PACK_SELECT.  */
   13576      8972166 :             aps = TMPL_ARG (args, level, idx);
   13577              :         }
   13578     11019409 :       ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   13579              :     }
   13580              : 
   13581              :   /* Substitute into the PATTERN with the (possibly altered)
   13582              :      arguments.  */
   13583     10122562 :   if (pattern == in_decl)
   13584              :     /* Expanding a fixed parameter pack from
   13585              :        coerce_template_parameter_pack.  */
   13586          115 :     t = tsubst_decl (pattern, args, complain);
   13587     10122447 :   else if (pattern == error_mark_node)
   13588              :     t = error_mark_node;
   13589     10122444 :   else if (!TYPE_P (pattern))
   13590      1979166 :     t = tsubst_expr (pattern, args, complain, in_decl);
   13591              :   else
   13592              :     {
   13593      8143278 :       t = tsubst (pattern, args, complain, in_decl);
   13594      8143278 :       if (is_auto (t) && !ith_elem_is_expansion)
   13595              :         /* When expanding the fake auto... pack expansion from add_capture, we
   13596              :            need to mark that the expansion is no longer a pack.  */
   13597          171 :         TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
   13598              :     }
   13599              : 
   13600              :   /*  If the Ith argument pack element is a pack expansion, then
   13601              :       the Ith element resulting from the substituting is going to
   13602              :       be a pack expansion as well.  */
   13603     10122562 :   if (ith_elem_is_expansion)
   13604       184426 :     t = make_pack_expansion (t, complain);
   13605              : 
   13606              :   return t;
   13607              : }
   13608              : 
   13609              : /* When the unexpanded parameter pack in a fold expression expands to an empty
   13610              :    sequence, the value of the expression is as follows; the program is
   13611              :    ill-formed if the operator is not listed in this table.
   13612              : 
   13613              :    &&   true
   13614              :    ||   false
   13615              :    ,    void()  */
   13616              : 
   13617              : tree
   13618        41605 : expand_empty_fold (tree t, tsubst_flags_t complain)
   13619              : {
   13620        41605 :   tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
   13621        41605 :   if (!FOLD_EXPR_MODIFY_P (t))
   13622        41572 :     switch (code)
   13623              :       {
   13624        39606 :       case TRUTH_ANDIF_EXPR:
   13625        39606 :         return boolean_true_node;
   13626           58 :       case TRUTH_ORIF_EXPR:
   13627           58 :         return boolean_false_node;
   13628         1854 :       case COMPOUND_EXPR:
   13629         1854 :         return void_node;
   13630              :       default:
   13631              :         break;
   13632              :       }
   13633              : 
   13634           87 :   if (complain & tf_error)
   13635           87 :     error_at (location_of (t),
   13636              :               "fold of empty expansion over %O", code);
   13637           87 :   return error_mark_node;
   13638              : }
   13639              : 
   13640              : /* Given a fold-expression T and a current LEFT and RIGHT operand,
   13641              :    form an expression that combines the two terms using the
   13642              :    operator of T. */
   13643              : 
   13644              : static tree
   13645       565560 : fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
   13646              : {
   13647       565560 :   tree_code code = FOLD_EXPR_OP (t);
   13648              : 
   13649       565560 :   tree lookups = templated_operator_saved_lookups (t);
   13650              : 
   13651              :   // Handle compound assignment operators.
   13652       565560 :   if (FOLD_EXPR_MODIFY_P (t))
   13653          913 :     return build_x_modify_expr (input_location, left, code, right,
   13654              :                                 lookups, complain);
   13655              : 
   13656       564647 :   warning_sentinel s(warn_parentheses);
   13657       564647 :   switch (code)
   13658              :     {
   13659         3408 :     case COMPOUND_EXPR:
   13660         3408 :       return build_x_compound_expr (input_location, left, right,
   13661         3408 :                                     lookups, complain);
   13662       561239 :     default:
   13663       561239 :       return build_x_binary_op (input_location, code,
   13664       561239 :                                 left, TREE_CODE (left),
   13665       561239 :                                 right, TREE_CODE (right),
   13666              :                                 lookups, /*overload=*/NULL,
   13667              :                                 complain);
   13668              :     }
   13669       564647 : }
   13670              : 
   13671              : /* Substitute ARGS into the pack of a fold expression T. */
   13672              : 
   13673              : static inline tree
   13674       469595 : tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13675              : {
   13676       469595 :   return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
   13677              : }
   13678              : 
   13679              : /* Substitute ARGS into the pack of a fold expression T. */
   13680              : 
   13681              : static inline tree
   13682         7496 : tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13683              : {
   13684         7496 :   return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
   13685              : }
   13686              : 
   13687              : /* Expand a PACK of arguments into a grouped as left fold.
   13688              :    Given a pack containing elements A0, A1, ..., An and an
   13689              :    operator @, this builds the expression:
   13690              : 
   13691              :       ((A0 @ A1) @ A2) ... @ An
   13692              : 
   13693              :    Note that PACK must not be empty.
   13694              : 
   13695              :    The operator is defined by the original fold expression T. */
   13696              : 
   13697              : static tree
   13698        10111 : expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
   13699              : {
   13700        10111 :   tree left = TREE_VEC_ELT (pack, 0);
   13701        25124 :   for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
   13702              :     {
   13703        15013 :       tree right = TREE_VEC_ELT (pack, i);
   13704        15013 :       left = fold_expression (t, left, right, complain);
   13705              :     }
   13706        10111 :   return left;
   13707              : }
   13708              : 
   13709              : /* Substitute into a unary left fold expression. */
   13710              : 
   13711              : static tree
   13712         4888 : tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13713              :                         tree in_decl)
   13714              : {
   13715         4888 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13716         4888 :   if (pack == error_mark_node)
   13717              :     return error_mark_node;
   13718         4888 :   if (PACK_EXPANSION_P (pack))
   13719              :     {
   13720         1015 :       tree r = copy_node (t);
   13721         1015 :       FOLD_EXPR_PACK (r) = pack;
   13722         1015 :       return r;
   13723              :     }
   13724         3873 :   if (TREE_VEC_LENGTH (pack) == 0)
   13725          178 :     return expand_empty_fold (t, complain);
   13726              :   else
   13727         3695 :     return expand_left_fold (t, pack, complain);
   13728              : }
   13729              : 
   13730              : /* Substitute into a binary left fold expression.
   13731              : 
   13732              :    Do ths by building a single (non-empty) vector of argumnts and
   13733              :    building the expression from those elements. */
   13734              : 
   13735              : static tree
   13736         6563 : tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13737              :                          tree in_decl)
   13738              : {
   13739         6563 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13740         6563 :   if (pack == error_mark_node)
   13741              :     return error_mark_node;
   13742         6563 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13743         6563 :   if (init == error_mark_node)
   13744              :     return error_mark_node;
   13745              : 
   13746         6563 :   if (PACK_EXPANSION_P (pack))
   13747              :     {
   13748          147 :       tree r = copy_node (t);
   13749          147 :       FOLD_EXPR_PACK (r) = pack;
   13750          147 :       FOLD_EXPR_INIT (r) = init;
   13751          147 :       return r;
   13752              :     }
   13753              : 
   13754         6416 :   tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
   13755         6416 :   TREE_VEC_ELT (vec, 0) = init;
   13756        12055 :   for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
   13757         5639 :     TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
   13758              : 
   13759         6416 :   return expand_left_fold (t, vec, complain);
   13760              : }
   13761              : 
   13762              : /* Expand a PACK of arguments into a grouped as right fold.
   13763              :    Given a pack containing elementns A0, A1, ..., and an
   13764              :    operator @, this builds the expression:
   13765              : 
   13766              :       A0@ ... (An-2 @ (An-1 @ An))
   13767              : 
   13768              :    Note that PACK must not be empty.
   13769              : 
   13770              :    The operator is defined by the original fold expression T. */
   13771              : 
   13772              : tree
   13773       409095 : expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
   13774              : {
   13775              :   // Build the expression.
   13776       409095 :   int n = TREE_VEC_LENGTH (pack);
   13777       409095 :   tree right = TREE_VEC_ELT (pack, n - 1);
   13778       959642 :   for (--n; n != 0; --n)
   13779              :     {
   13780       550547 :       tree left = TREE_VEC_ELT (pack, n - 1);
   13781       550547 :       right = fold_expression (t, left, right, complain);
   13782              :     }
   13783       409095 :   return right;
   13784              : }
   13785              : 
   13786              : /* Substitute into a unary right fold expression. */
   13787              : 
   13788              : static tree
   13789       457211 : tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13790              :                          tree in_decl)
   13791              : {
   13792       457211 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13793       457211 :   if (pack == error_mark_node)
   13794              :     return error_mark_node;
   13795       455330 :   if (PACK_EXPANSION_P (pack))
   13796              :     {
   13797         5651 :       tree r = copy_node (t);
   13798         5651 :       FOLD_EXPR_PACK (r) = pack;
   13799         5651 :       return r;
   13800              :     }
   13801       449679 :   if (TREE_VEC_LENGTH (pack) == 0)
   13802        41427 :     return expand_empty_fold (t, complain);
   13803              :   else
   13804       408252 :     return expand_right_fold (t, pack, complain);
   13805              : }
   13806              : 
   13807              : /* Substitute into a binary right fold expression.
   13808              : 
   13809              :    Do ths by building a single (non-empty) vector of arguments and
   13810              :    building the expression from those elements. */
   13811              : 
   13812              : static tree
   13813          933 : tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13814              :                          tree in_decl)
   13815              : {
   13816          933 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13817          933 :   if (pack == error_mark_node)
   13818              :     return error_mark_node;
   13819          933 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13820          933 :   if (init == error_mark_node)
   13821              :     return error_mark_node;
   13822              : 
   13823          933 :   if (PACK_EXPANSION_P (pack))
   13824              :     {
   13825           90 :       tree r = copy_node (t);
   13826           90 :       FOLD_EXPR_PACK (r) = pack;
   13827           90 :       FOLD_EXPR_INIT (r) = init;
   13828           90 :       return r;
   13829              :     }
   13830              : 
   13831          843 :   int n = TREE_VEC_LENGTH (pack);
   13832          843 :   tree vec = make_tree_vec (n + 1);
   13833         2211 :   for (int i = 0; i < n; ++i)
   13834         1368 :     TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
   13835          843 :   TREE_VEC_ELT (vec, n) = init;
   13836              : 
   13837          843 :   return expand_right_fold (t, vec, complain);
   13838              : }
   13839              : 
   13840              : /* Walk through the pattern of a pack expansion, adding everything in
   13841              :    local_specializations to a list.  */
   13842              : 
   13843              : class el_data
   13844              : {
   13845              : public:
   13846              :   /* Set of variables declared within the pattern.  */
   13847              :   hash_set<tree> internal;
   13848              :   /* Set of AST nodes that have been visited by the traversal.  */
   13849              :   hash_set<tree> visited;
   13850              :   /* List of local_specializations used within the pattern.  */
   13851              :   tree extra;
   13852              :   tsubst_flags_t complain;
   13853              :   /* True iff we don't want to walk into unevaluated contexts.  */
   13854              :   bool skip_unevaluated_operands = false;
   13855              :   /* The unevaluated contexts that we avoided walking.  */
   13856              :   auto_vec<tree> skipped_trees;
   13857              : 
   13858         7012 :   el_data (tsubst_flags_t c)
   13859         7012 :     : extra (NULL_TREE), complain (c) {}
   13860              : };
   13861              : static tree
   13862       681313 : extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
   13863              : {
   13864       681313 :   el_data &data = *reinterpret_cast<el_data*>(data_);
   13865       681313 :   tree *extra = &data.extra;
   13866       681313 :   tsubst_flags_t complain = data.complain;
   13867              : 
   13868       681313 :   if (data.skip_unevaluated_operands
   13869       681313 :       && unevaluated_p (TREE_CODE (*tp)))
   13870              :     {
   13871         7238 :       data.skipped_trees.safe_push (*tp);
   13872         7238 :       *walk_subtrees = 0;
   13873         7238 :       return NULL_TREE;
   13874              :     }
   13875              : 
   13876       674075 :   if (TYPE_P (*tp) && typedef_variant_p (*tp))
   13877              :     /* Remember local typedefs (85214).  */
   13878         6831 :     tp = &TYPE_NAME (*tp);
   13879              : 
   13880       674075 :   if (has_extra_args_mechanism_p (*tp))
   13881              :     /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
   13882              :        potentially see a previously captured local in an evaluated context
   13883              :        that's really only used in an unevaluated context (PR114303).  This
   13884              :        means callers of build_extra_args need to clear *_EXTRA_ARGS of the
   13885              :        outermost tree.  Nested *_EXTRA_ARGS should naturally be empty since
   13886              :        the outermost (extra-args) tree will intercept any substitution before
   13887              :        a nested tree can.  */
   13888        20392 :     gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE
   13889              :                         /* Except a lambda nested inside an extra-args tree
   13890              :                            can have extra args if we deferred partial
   13891              :                            substitution into it at template parse time.  But
   13892              :                            we don't walk LAMBDA_EXPR_EXTRA_ARGS anyway.  */
   13893              :                          || TREE_CODE (*tp) == LAMBDA_EXPR);
   13894              : 
   13895       674075 :   if (TREE_CODE (*tp) == DECL_EXPR)
   13896              :     {
   13897        14372 :       tree decl = DECL_EXPR_DECL (*tp);
   13898        14372 :       data.internal.add (decl);
   13899        13137 :       if (DECL_DECOMPOSITION_P (decl)
   13900        14378 :           && TREE_TYPE (decl) != error_mark_node)
   13901              :         {
   13902            6 :           gcc_assert (DECL_NAME (decl) == NULL_TREE);
   13903            6 :           for (tree decl2 = DECL_CHAIN (decl);
   13904           18 :                decl2
   13905           12 :                && DECL_DECOMPOSITION_P (decl2)
   13906           12 :                && DECL_NAME (decl2)
   13907           30 :                && TREE_TYPE (decl2) != error_mark_node;
   13908           12 :                decl2 = DECL_CHAIN (decl2))
   13909              :             {
   13910           12 :               gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
   13911           12 :               data.internal.add (decl2);
   13912              :             }
   13913              :         }
   13914              :     }
   13915       659703 :   else if (TREE_CODE (*tp) == LAMBDA_EXPR)
   13916              :     {
   13917              :       /* Since we defer implicit capture, look in the parms and body.  */
   13918          589 :       tree fn = lambda_function (*tp);
   13919          589 :       cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
   13920              :                     &data.visited);
   13921          589 :       cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
   13922              :                     &data.visited);
   13923              :     }
   13924       659114 :   else if (tree spec = retrieve_local_specialization (*tp))
   13925              :     {
   13926        14148 :       if (data.internal.contains (*tp))
   13927              :         /* Don't mess with variables declared within the pattern.  */
   13928              :         return NULL_TREE;
   13929        14133 :       if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13930              :         {
   13931              :           /* Maybe pull out the PARM_DECL for a partial instantiation.  */
   13932          339 :           tree args = ARGUMENT_PACK_ARGS (spec);
   13933          339 :           if (TREE_VEC_LENGTH (args) == 1)
   13934              :             {
   13935           34 :               tree elt = TREE_VEC_ELT (args, 0);
   13936           34 :               if (PACK_EXPANSION_P (elt))
   13937            0 :                 elt = PACK_EXPANSION_PATTERN (elt);
   13938           34 :               if (DECL_PACK_P (elt))
   13939              :                 spec = elt;
   13940              :             }
   13941          339 :           if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13942              :             {
   13943              :               /* Handle lambda capture here, since we aren't doing any
   13944              :                  substitution now, and so tsubst_copy won't call
   13945              :                  process_outer_var_ref.  */
   13946          339 :               tree args = ARGUMENT_PACK_ARGS (spec);
   13947          339 :               int len = TREE_VEC_LENGTH (args);
   13948         1283 :               for (int i = 0; i < len; ++i)
   13949              :                 {
   13950          944 :                   tree arg = TREE_VEC_ELT (args, i);
   13951          944 :                   tree carg = arg;
   13952          944 :                   if (outer_automatic_var_p (arg))
   13953          944 :                     carg = process_outer_var_ref (arg, complain);
   13954          944 :                   if (carg != arg)
   13955              :                     {
   13956              :                       /* Make a new NONTYPE_ARGUMENT_PACK of the capture
   13957              :                          proxies.  */
   13958          944 :                       if (i == 0)
   13959              :                         {
   13960          333 :                           spec = copy_node (spec);
   13961          333 :                           args = copy_node (args);
   13962          333 :                           ARGUMENT_PACK_ARGS (spec) = args;
   13963          333 :                           register_local_specialization (spec, *tp);
   13964              :                         }
   13965          944 :                       TREE_VEC_ELT (args, i) = carg;
   13966              :                     }
   13967              :                 }
   13968              :             }
   13969              :         }
   13970        14133 :       if (outer_automatic_var_p (spec))
   13971           21 :         spec = process_outer_var_ref (spec, complain);
   13972        14133 :       *extra = tree_cons (*tp, spec, *extra);
   13973              :     }
   13974              :   return NULL_TREE;
   13975              : }
   13976              : static tree
   13977         7012 : extract_local_specs (tree pattern, tsubst_flags_t complain)
   13978              : {
   13979         7012 :   el_data data (complain);
   13980              :   /* Walk the pattern twice, ignoring unevaluated operands the first time
   13981              :      around, so that if a local specialization appears in both an evaluated
   13982              :      and unevaluated context we prefer to process it in the evaluated context
   13983              :      (since e.g. process_outer_var_ref is a no-op inside an unevaluated
   13984              :      context).  */
   13985         7012 :   data.skip_unevaluated_operands = true;
   13986         7012 :   cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
   13987              :   /* Now walk the unevaluated contexts we skipped the first time around.  */
   13988         7012 :   data.skip_unevaluated_operands = false;
   13989        26758 :   for (tree t : data.skipped_trees)
   13990              :     {
   13991         7238 :       data.visited.remove (t);
   13992         7238 :       cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
   13993              :     }
   13994        14024 :   return data.extra;
   13995         7012 : }
   13996              : 
   13997              : /* Extract any uses of local_specializations from PATTERN and add them to ARGS
   13998              :    for use in PACK_EXPANSION_EXTRA_ARGS.  */
   13999              : 
   14000              : tree
   14001         8196 : build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
   14002              : {
   14003              :   /* Make a copy of the extra arguments so that they won't get changed
   14004              :      out from under us.  */
   14005         8196 :   tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
   14006         8196 :   if ((complain & tf_partial) || TREE_STATIC (args))
   14007              :     /* Remember whether this is a partial substitution.  */
   14008         1370 :     TREE_STATIC (extra) = true;
   14009         8196 :   if (local_specializations)
   14010         7012 :     if (tree locals = extract_local_specs (pattern, complain))
   14011         5071 :       extra = tree_cons (NULL_TREE, extra, locals);
   14012         8196 :   return extra;
   14013              : }
   14014              : 
   14015              : /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
   14016              :    normal template args to ARGS.  */
   14017              : 
   14018              : tree
   14019    106192362 : add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
   14020              : {
   14021    106192362 :   if (!extra)
   14022              :     return args;
   14023              : 
   14024        62269 :   if (TREE_CODE (extra) == TREE_LIST)
   14025              :     {
   14026       243524 :       for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
   14027              :         {
   14028              :           /* The partial instantiation involved local declarations collected in
   14029              :              extract_local_specs; map from the general template to our local
   14030              :              context.  */
   14031       181816 :           tree gen = TREE_PURPOSE (elt);
   14032       181816 :           tree inst = TREE_VALUE (elt);
   14033       181816 :           if (DECL_P (inst))
   14034       181477 :             if (tree local = retrieve_local_specialization (inst))
   14035       181816 :               inst = local;
   14036              :           /* else inst is already a full instantiation of the pack.  */
   14037       181816 :           register_local_specialization (inst, gen);
   14038       181816 :           if (is_normal_capture_proxy (gen))
   14039        78159 :             register_local_specialization (inst, DECL_CAPTURED_VARIABLE (gen));
   14040              :         }
   14041        61708 :       gcc_assert (!TREE_PURPOSE (extra));
   14042        61708 :       extra = TREE_VALUE (extra);
   14043              :     }
   14044        62269 :   if (TREE_STATIC (extra))
   14045              :     {
   14046              :       /* This is a partial substitution into e.g. a requires-expr or lambda-expr
   14047              :          inside a default template argument; we expect 'extra' to be a full set
   14048              :          of template arguments for the template context, so it suffices to just
   14049              :          substitute into them.  */
   14050          104 :       args = tsubst_template_args (extra, args, complain, in_decl);
   14051          104 :       if (processing_template_decl)
   14052              :         /* A templated substitution into a partial substitution is still a
   14053              :            partial substitution.  */
   14054           12 :         TREE_STATIC (args) = true;
   14055              :     }
   14056              :   else
   14057        62165 :     args = add_to_template_args (extra, args);
   14058              :   return args;
   14059              : }
   14060              : 
   14061              : /* We've seen a lambda capture proxy pack PAT in a context outside the body,
   14062              :    such as in a trailing requires-clause.  Here we cannot use
   14063              :    lookup_init_capture_pack, both because checking satisfaction comes after
   14064              :    tsubst_lambda_expr (so the field pack isn't in local_specializations) and
   14065              :    before start_lambda_function (so the proxies aren't there to lookup_name).
   14066              : 
   14067              :    However, because satisfy_declaration_constraints pushed into the context of
   14068              :    the substituted closure type, we can find it in current_class_type,
   14069              :    enumerate the expanded field pack, and then use that to expand the capture
   14070              :    proxy pack.
   14071              : 
   14072              :    This seems like cheating, but there's really no alternative since lambdas
   14073              :    are not instantiated; we have to use the same closure type.  To address this
   14074              :    in the language, probably lambda constraints should be substituted
   14075              :    immediately like lambda noexcept.  */
   14076              : 
   14077              : tree
   14078           11 : reconstruct_lambda_capture_pack (tree pat, tree args, tsubst_flags_t complain,
   14079              :                                  tree in_decl)
   14080              : {
   14081           11 :   gcc_assert (cp_unevaluated_operand && !at_function_scope_p ());
   14082           11 :   tree newc = current_class_type;
   14083              : 
   14084              :   /* Look up the pattern FIELD_DECL.  */
   14085           11 :   tree oldfield = TREE_OPERAND (DECL_VALUE_EXPR (pat), 1);
   14086           11 :   gcc_assert (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (oldfield)))
   14087              :               == DECL_SOURCE_LOCATION (TYPE_NAME (newc)));
   14088              : 
   14089           11 :   if (!retrieve_local_specialization (oldfield))
   14090              :     {
   14091              :       /* Look through TYPE_FIELDS to find captures with matching
   14092              :          capture#1 names.  */
   14093           11 :       tree pn = DECL_NAME (oldfield);
   14094           11 :       const char *ps = IDENTIFIER_POINTER (pn);
   14095           11 :       int pl = IDENTIFIER_LENGTH (pn);
   14096           11 :       tree firstcap = NULL_TREE;
   14097           11 :       int ncap = 0;
   14098           11 :       for (tree f = next_subobject_field (TYPE_FIELDS (newc));
   14099           31 :            f; f = DECL_CHAIN (f))
   14100              :         {
   14101           28 :           const char *s = IDENTIFIER_POINTER (DECL_NAME (f));
   14102           28 :           if (strncmp (ps, s, pl) == 0 && s[pl] == '#')
   14103              :             {
   14104           16 :               if (!firstcap)
   14105            8 :                 firstcap = f;
   14106           16 :               ++ncap;
   14107              :             }
   14108           12 :           else if (firstcap)
   14109              :             break;
   14110              :         }
   14111           11 :       tree fpack = make_tree_vec (ncap);
   14112           27 :       for (int i = 0; i < ncap; ++i)
   14113              :         {
   14114           16 :           TREE_VEC_ELT (fpack, i) = firstcap;
   14115           16 :           firstcap = DECL_CHAIN (firstcap);
   14116              :         }
   14117           11 :       tree spec = make_node (NONTYPE_ARGUMENT_PACK);
   14118           11 :       ARGUMENT_PACK_ARGS (spec) = fpack;
   14119              :       /* satisfy_declaration_constraints set up local_specializations.  */
   14120           11 :       register_local_specialization (spec, oldfield);
   14121              :     }
   14122              : 
   14123              :   /* Now that the field pack is in local_specializations, we can substitute
   14124              :      into the DECL_VALUE_EXPR of the proxy.  */
   14125           11 :   tree ve = DECL_VALUE_EXPR (pat);
   14126           11 :   ve = make_pack_expansion (ve);
   14127           11 :   ve = tsubst_pack_expansion (ve, args, complain, in_decl);
   14128           11 :   const int len = TREE_VEC_LENGTH (ve);
   14129           11 :   tree ppack = make_tree_vec (len);
   14130           27 :   for (int i = 0; i < len; ++i)
   14131              :     {
   14132           16 :       tree p = copy_decl (pat);
   14133           16 :       tree vei = TREE_VEC_ELT (ve, i);
   14134           16 :       SET_DECL_VALUE_EXPR (p, vei);
   14135           16 :       TREE_TYPE (p) = lambda_proxy_type (vei);
   14136           16 :       TREE_VEC_ELT (ppack, i) = p;
   14137              :     }
   14138           11 :   tree spec = make_node (NONTYPE_ARGUMENT_PACK);
   14139           11 :   ARGUMENT_PACK_ARGS (spec) = ppack;
   14140           11 :   register_local_specialization (spec, pat);
   14141           11 :   return spec;
   14142              : }
   14143              : 
   14144              : /* Substitute ARGS into T, which is a pack expansion
   14145              :    (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION).  Returns a
   14146              :    TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
   14147              :    (if only a partial substitution could be performed) or
   14148              :    ERROR_MARK_NODE if there was an error.  */
   14149              : 
   14150              : tree
   14151     89282110 : tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   14152              :                        tree in_decl)
   14153              : {
   14154     89282110 :   tree pattern;
   14155     89282110 :   tree pack, packs = NULL_TREE;
   14156     89282110 :   bool unsubstituted_packs = false;
   14157     89282110 :   int i, len = -1;
   14158     89282110 :   tree result;
   14159     89282110 :   bool need_local_specializations = false;
   14160     89282110 :   int levels;
   14161              : 
   14162     89282110 :   gcc_assert (PACK_EXPANSION_P (t));
   14163     89282110 :   pattern = PACK_EXPANSION_PATTERN (t);
   14164              : 
   14165              :   /* Add in any args remembered from an earlier partial instantiation.  */
   14166     89282110 :   args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
   14167              : 
   14168    178564217 :   levels = TMPL_ARGS_DEPTH (args);
   14169              : 
   14170              :   /* Determine the argument packs that will instantiate the parameter
   14171              :      packs used in the expansion expression. While we're at it,
   14172              :      compute the number of arguments to be expanded and make sure it
   14173              :      is consistent.  */
   14174    259756520 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
   14175     87349022 :        pack = TREE_CHAIN (pack))
   14176              :     {
   14177     90085117 :       tree parm_pack = TREE_VALUE (pack);
   14178     90085117 :       tree arg_pack = NULL_TREE;
   14179     90085117 :       tree orig_arg = NULL_TREE;
   14180     90085117 :       int level = 0;
   14181              : 
   14182     90085117 :       if (TREE_CODE (parm_pack) == BASES)
   14183              :         {
   14184           27 :           gcc_assert (parm_pack == pattern);
   14185           27 :           tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl);
   14186           27 :           if (BASES_DIRECT (parm_pack))
   14187      2736092 :             return calculate_direct_bases (type, complain);
   14188              :           else
   14189           12 :             return calculate_bases (type, complain);
   14190              :         }
   14191     90085090 :       else if (builtin_pack_call_p (parm_pack))
   14192              :         {
   14193       101320 :           if (parm_pack != pattern)
   14194              :             {
   14195            3 :               if (complain & tf_error)
   14196            3 :                 sorry ("%qE is not the entire pattern of the pack expansion",
   14197              :                        parm_pack);
   14198            3 :               return error_mark_node;
   14199              :             }
   14200       101317 :           return expand_builtin_pack_call (parm_pack, args,
   14201       101317 :                                            complain, in_decl);
   14202              :         }
   14203     89983770 :       else if (TREE_CODE (parm_pack) == PARM_DECL)
   14204              :         {
   14205              :           /* We know we have correct local_specializations if this
   14206              :              expansion is at function scope, or if we're dealing with a
   14207              :              local parameter in a requires expression; for the latter,
   14208              :              tsubst_requires_expr set it up appropriately.  */
   14209       750603 :           if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
   14210       749426 :             arg_pack = retrieve_local_specialization (parm_pack);
   14211              :           else
   14212              :             /* We can't rely on local_specializations for a parameter
   14213              :                name used later in a function declaration (such as in a
   14214              :                late-specified return type).  Even if it exists, it might
   14215              :                have the wrong value for a recursive call.  */
   14216              :             need_local_specializations = true;
   14217              : 
   14218       749426 :           if (!arg_pack)
   14219              :             {
   14220              :               /* This parameter pack was used in an unevaluated context.  Just
   14221              :                  make a dummy decl, since it's only used for its type.  */
   14222         1178 :               ++cp_unevaluated_operand;
   14223         1178 :               arg_pack = tsubst_decl (parm_pack, args, complain);
   14224         1178 :               --cp_unevaluated_operand;
   14225         1178 :               if (arg_pack && DECL_PACK_P (arg_pack))
   14226              :                 /* Partial instantiation of the parm_pack, we can't build
   14227              :                    up an argument pack yet.  */
   14228              :                 arg_pack = NULL_TREE;
   14229              :               else
   14230         1025 :                 arg_pack = make_fnparm_pack (arg_pack);
   14231              :             }
   14232       749425 :           else if (DECL_PACK_P (arg_pack))
   14233              :             /* This argument pack isn't fully instantiated yet.  */
   14234              :             arg_pack = NULL_TREE;
   14235              :         }
   14236     89233167 :       else if (is_capture_proxy (parm_pack))
   14237              :         {
   14238          402 :           arg_pack = retrieve_local_specialization (parm_pack);
   14239          402 :           if (!arg_pack)
   14240           11 :             arg_pack = reconstruct_lambda_capture_pack (parm_pack, args,
   14241              :                                                         complain, in_decl);
   14242          402 :           if (DECL_DECOMPOSITION_P (arg_pack))
   14243              :             {
   14244          128 :               orig_arg = arg_pack;
   14245          128 :               goto expand_sb_pack;
   14246              :             }
   14247          274 :           if (DECL_PACK_P (arg_pack))
   14248              :             arg_pack = NULL_TREE;
   14249              :         }
   14250     89232765 :       else if (TREE_CODE (parm_pack) == FIELD_DECL)
   14251              :         /* For reconstruct_lambda_capture_pack.  */
   14252           11 :         arg_pack = retrieve_local_specialization (parm_pack);
   14253     89232754 :       else if (DECL_DECOMPOSITION_P (parm_pack))
   14254              :         {
   14255         4949 :           orig_arg = retrieve_local_specialization (parm_pack);
   14256         5077 :         expand_sb_pack:
   14257         5077 :           gcc_assert (DECL_DECOMPOSITION_P (orig_arg));
   14258         5077 :           if (TREE_TYPE (orig_arg) == error_mark_node)
   14259              :             return error_mark_node;
   14260         5077 :           gcc_assert (DECL_HAS_VALUE_EXPR_P (orig_arg));
   14261         5077 :           arg_pack = DECL_VALUE_EXPR (orig_arg);
   14262         5077 :           if (TREE_CODE (arg_pack) != ARRAY_REF)
   14263              :             {
   14264              :               /* Structured binding packs when initializer is non-dependent
   14265              :                  should have their DECL_VALUE_EXPR set to a TREE_VEC.  See
   14266              :                  cp_finish_decomp comment above the packv variable for
   14267              :                  details.  */
   14268         5074 :               tree vec = make_tree_vec (TREE_VEC_LENGTH (arg_pack) - 2);
   14269         5074 :               if (TREE_VEC_LENGTH (vec))
   14270         4652 :                 memcpy (TREE_VEC_BEGIN (vec), &TREE_VEC_ELT (arg_pack, 2),
   14271         4652 :                         TREE_VEC_LENGTH (vec) * sizeof (tree));
   14272         5074 :               arg_pack = make_node (NONTYPE_ARGUMENT_PACK);
   14273         5074 :               ARGUMENT_PACK_ARGS (arg_pack) = vec;
   14274              :             }
   14275              :           else
   14276              :             {
   14277              :               /* If the structured binding pack has type dependent
   14278              :                  base, we can't expand it yet.  */
   14279            3 :               tree base = TREE_OPERAND (arg_pack, 0);
   14280            3 :               gcc_assert (VAR_P (base)
   14281              :                           && type_dependent_expression_p (base));
   14282              :               arg_pack = NULL_TREE;
   14283              :             }
   14284              :         }
   14285              :       else
   14286              :         {
   14287     89227805 :           int idx;
   14288     89227805 :           template_parm_level_and_index (parm_pack, &level, &idx);
   14289     89227805 :           if (level <= levels)
   14290    155151596 :             arg_pack = TMPL_ARG (args, level, idx);
   14291              : 
   14292     73724540 :           if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
   14293     77575819 :               && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
   14294              :             arg_pack = NULL_TREE;
   14295              :         }
   14296              : 
   14297     89228841 :       if (orig_arg == NULL_TREE)
   14298              :         orig_arg = arg_pack;
   14299     89983770 :       if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   14300         4146 :         arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   14301              : 
   14302     74475480 :       if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
   14303              :         /* This can only happen if we forget to expand an argument
   14304              :            pack somewhere else. Just return an error, silently.  */
   14305              :         {
   14306            9 :           result = make_tree_vec (1);
   14307            9 :           TREE_VEC_ELT (result, 0) = error_mark_node;
   14308            9 :           return result;
   14309              :         }
   14310              : 
   14311     89983761 :       if (arg_pack)
   14312              :         {
   14313     74475471 :           int my_len
   14314     74475471 :             = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
   14315              : 
   14316              :           /* Don't bother trying to do a partial substitution with
   14317              :              incomplete packs; we'll try again after deduction.  */
   14318     74475471 :           if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
   14319              :             return t;
   14320              : 
   14321     71840770 :           if (len < 0)
   14322              :             len = my_len;
   14323       800791 :           else if (len != my_len)
   14324              :             {
   14325           35 :               if (!(complain & tf_error))
   14326              :                 /* Fail quietly.  */;
   14327           13 :               else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
   14328            7 :                 error ("mismatched argument pack lengths while expanding %qT",
   14329              :                        pattern);
   14330              :               else
   14331            6 :                 error ("mismatched argument pack lengths while expanding %qE",
   14332              :                        pattern);
   14333           35 :               return error_mark_node;
   14334              :             }
   14335              : 
   14336              :           /* Keep track of the parameter packs and their corresponding
   14337              :              argument packs.  */
   14338     71840735 :           packs = tree_cons (parm_pack, arg_pack, packs);
   14339     71840735 :           TREE_TYPE (packs) = orig_arg;
   14340              :         }
   14341              :       else
   14342              :         {
   14343              :           /* We can't substitute for this parameter pack.  We use a flag as
   14344              :              well as the missing_level counter because function parameter
   14345              :              packs don't have a level.  */
   14346     15508290 :           gcc_assert (processing_template_decl || is_auto (parm_pack)
   14347              :                       || args == NULL_TREE);
   14348              :           unsubstituted_packs = true;
   14349              :         }
   14350              :     }
   14351              : 
   14352              :   /* If the expansion is just T..., return the matching argument pack, unless
   14353              :      we need to call convert_from_reference on all the elements.  This is an
   14354              :      important optimization; see c++/68422.  */
   14355     86546015 :   if (!unsubstituted_packs
   14356    157585151 :       && TREE_PURPOSE (packs) == pattern)
   14357              :     {
   14358     64040580 :       tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
   14359              : 
   14360              :       /* If the argument pack is a single pack expansion, pull it out.  */
   14361     64040580 :       if (TREE_VEC_LENGTH (args) == 1
   14362     64040580 :           && pack_expansion_args_count (args))
   14363              :         {
   14364      1741137 :           tree arg = TREE_VEC_ELT (args, 0);
   14365      1741137 :           if (PACK_EXPANSION_SIZEOF_P (t)
   14366      1741137 :               && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
   14367              :             /* Except if this isn't a simple sizeof...(T) which gets sZ
   14368              :                mangling, keep the TREE_VEC to get sP mangling.  */;
   14369              :           else
   14370      1741128 :             return TREE_VEC_ELT (args, 0);
   14371              :         }
   14372              : 
   14373              :       /* Types need no adjustment, nor does sizeof..., and if we still have
   14374              :          some pack expansion args we won't do anything yet.  */
   14375     62299452 :       if (TREE_CODE (t) == TYPE_PACK_EXPANSION
   14376      3747062 :           || PACK_EXPANSION_SIZEOF_P (t)
   14377     66041710 :           || pack_expansion_args_count (args))
   14378              :         return args;
   14379              :       /* Also optimize expression pack expansions if we can tell that the
   14380              :          elements won't have reference type.  */
   14381      3658360 :       tree type = TREE_TYPE (pattern);
   14382      3658360 :       if (type && !TYPE_REF_P (type)
   14383              :           && !PACK_EXPANSION_P (type)
   14384              :           && !WILDCARD_TYPE_P (type))
   14385              :         return args;
   14386              :       /* Otherwise use the normal path so we get convert_from_reference.  */
   14387              :     }
   14388              : 
   14389              :   /* We cannot expand this expansion expression, because we don't have
   14390              :      all of the argument packs we need.  */
   14391     22522619 :   if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
   14392              :     {
   14393              :       /* We got some full packs, but we can't substitute them in until we
   14394              :          have values for all the packs.  So remember these until then.  */
   14395              : 
   14396          825 :       t = make_pack_expansion (pattern, complain);
   14397          825 :       PACK_EXPANSION_EXTRA_ARGS (t)
   14398          825 :         = build_extra_args (pattern, args, complain);
   14399          825 :       return t;
   14400              :     }
   14401              : 
   14402              :   /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
   14403              :      type, so create our own local specializations map; the current map is
   14404              :      either NULL or (in the case of recursive unification) might have
   14405              :      bindings that we don't want to use or alter.  */
   14406     22521794 :   local_specialization_stack lss (need_local_specializations
   14407     67564339 :                                   ? lss_blank : lss_nop);
   14408              : 
   14409     22521794 :   if (unsubstituted_packs)
   14410              :     {
   14411              :       /* There were no real arguments, we're just replacing a parameter
   14412              :          pack with another version of itself. Substitute into the
   14413              :          pattern and return a PACK_EXPANSION_*. The caller will need to
   14414              :          deal with that.  */
   14415     15506066 :       if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
   14416       547836 :         result = tsubst_expr (pattern, args, complain, in_decl);
   14417              :       else
   14418     14958230 :         result = tsubst (pattern, args, complain, in_decl);
   14419     15506066 :       result = make_pack_expansion (result, complain);
   14420     15506066 :       if (result == error_mark_node)
   14421              :         return error_mark_node;
   14422     15506063 :       PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
   14423     15506063 :       PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
   14424     15506063 :       if (PACK_EXPANSION_AUTO_P (t))
   14425              :         {
   14426              :           /* This is a fake auto... pack expansion created in add_capture with
   14427              :              _PACKS that don't appear in the pattern.  Copy one over.  */
   14428            6 :           packs = PACK_EXPANSION_PARAMETER_PACKS (t);
   14429            3 :           pack = retrieve_local_specialization (TREE_VALUE (packs));
   14430            3 :           gcc_checking_assert (DECL_PACK_P (pack));
   14431            6 :           PACK_EXPANSION_PARAMETER_PACKS (result)
   14432            3 :             = build_tree_list (NULL_TREE, pack);
   14433            3 :           PACK_EXPANSION_AUTO_P (result) = true;
   14434              :         }
   14435     15506063 :       return result;
   14436              :     }
   14437              : 
   14438      7015728 :   gcc_assert (len >= 0);
   14439              : 
   14440              :   /* For each argument in each argument pack, substitute into the
   14441              :      pattern.  */
   14442      7015728 :   result = make_tree_vec (len);
   14443      7015728 :   tree elem_args = copy_template_args (args);
   14444     23461541 :   for (i = 0; i < len; ++i)
   14445              :     {
   14446     10122562 :       t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
   14447              :                                                     i,
   14448              :                                                     elem_args, complain,
   14449              :                                                     in_decl);
   14450     10122562 :       TREE_VEC_ELT (result, i) = t;
   14451     10122562 :       if (t == error_mark_node)
   14452              :         {
   14453              :           result = error_mark_node;
   14454              :           break;
   14455              :         }
   14456              :     }
   14457              : 
   14458              :   /* Update ARGS to restore the substitution from parameter packs to
   14459              :      their argument packs.  */
   14460     14832154 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   14461              :     {
   14462      7816426 :       tree parm = TREE_PURPOSE (pack);
   14463              : 
   14464      7816426 :       if (TREE_CODE (parm) == PARM_DECL
   14465      7073039 :           || VAR_P (parm)
   14466      7067887 :           || TREE_CODE (parm) == FIELD_DECL)
   14467       748550 :         register_local_specialization (TREE_TYPE (pack), parm);
   14468              :       else
   14469              :         {
   14470      7067876 :           int idx, level;
   14471              : 
   14472      7067876 :           if (TREE_VALUE (pack) == NULL_TREE)
   14473            0 :             continue;
   14474              : 
   14475      7067876 :           template_parm_level_and_index (parm, &level, &idx);
   14476              : 
   14477              :           /* Update the corresponding argument.  */
   14478     14135752 :           if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   14479      1221306 :             TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx)
   14480      2442612 :               = TREE_TYPE (pack);
   14481              :           else
   14482      5846570 :             TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
   14483              :         }
   14484              :     }
   14485              : 
   14486              :   /* If the dependent pack arguments were such that we end up with only a
   14487              :      single pack expansion again, there's no need to keep it in a TREE_VEC.  */
   14488      1947616 :   if (len == 1 && TREE_CODE (result) == TREE_VEC
   14489      8961060 :       && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
   14490       175382 :     return TREE_VEC_ELT (result, 0);
   14491              : 
   14492              :   return result;
   14493              : }
   14494              : 
   14495              : /* Substitute ARGS into T, which is a TREE_VEC.  This function creates a new
   14496              :    TREE_VEC rather than substituting the elements in-place.  */
   14497              : 
   14498              : static tree
   14499         4027 : tsubst_tree_vec (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14500              : {
   14501         4027 :   const int len = TREE_VEC_LENGTH (t);
   14502         4027 :   tree r = make_tree_vec (len);
   14503        18144 :   for (int i = 0; i < len; ++i)
   14504              :     {
   14505        14117 :       tree arg = TREE_VEC_ELT (t, i);
   14506        14117 :       if (TYPE_P (arg))
   14507         2194 :         TREE_VEC_ELT (r, i) = tsubst (arg, args, complain, in_decl);
   14508              :       else
   14509        11923 :         TREE_VEC_ELT (r, i) = tsubst_expr (arg, args, complain, in_decl);
   14510              :     }
   14511         4027 :   return r;
   14512              : }
   14513              : 
   14514              : /* Substitute ARGS into T, which is a pack index (i.e., PACK_INDEX_TYPE or
   14515              :    PACK_INDEX_EXPR).  Returns a single type or expression, a PACK_INDEX_*
   14516              :    node if only a partial substitution could be performed, or ERROR_MARK_NODE
   14517              :    if there was an error.  */
   14518              : 
   14519              : static tree
   14520        13856 : tsubst_pack_index (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14521              : {
   14522        13856 :   tree pack = PACK_INDEX_PACK (t);
   14523        13856 :   if (PACK_EXPANSION_P (pack))
   14524         9829 :     pack = tsubst_pack_expansion (pack, args, complain, in_decl);
   14525              :   else
   14526              :     {
   14527              :       /* PACK can be {*args#0} whose args#0's value-expr refers to
   14528              :          a partially instantiated closure.  Let tsubst find the
   14529              :          fully-instantiated one.  */
   14530         4027 :       gcc_assert (TREE_CODE (pack) == TREE_VEC);
   14531         4027 :       pack = tsubst_tree_vec (pack, args, complain, in_decl);
   14532              :     }
   14533        13856 :   if (TREE_CODE (pack) == TREE_VEC && TREE_VEC_LENGTH (pack) == 0)
   14534              :     {
   14535            6 :       if (complain & tf_error)
   14536            6 :         error ("cannot index an empty pack");
   14537            6 :       return error_mark_node;
   14538              :     }
   14539        13850 :   tree index = tsubst_expr (PACK_INDEX_INDEX (t), args, complain, in_decl);
   14540        13850 :   const bool parenthesized_p = (TREE_CODE (t) == PACK_INDEX_EXPR
   14541        13850 :                                 && PACK_INDEX_PARENTHESIZED_P (t));
   14542        13850 :   tree r;
   14543        13850 :   if (!type_dependent_expression_p (index))
   14544        13708 :     index = build_converted_constant_expr (size_type_node, index, complain);
   14545        13850 :   if (error_operand_p (index))
   14546            1 :     return error_mark_node;
   14547        13849 :   if (!value_dependent_expression_p (index) && TREE_CODE (pack) == TREE_VEC)
   14548        10832 :     r = pack_index_element (index, pack, parenthesized_p, complain);
   14549              :   else
   14550         3017 :     r = make_pack_index (pack, index);
   14551        13849 :   if (TREE_CODE (t) == PACK_INDEX_TYPE)
   14552         2736 :     r = cp_build_qualified_type (r, cp_type_quals (t) | cp_type_quals (r),
   14553              :                                  complain | tf_ignore_bad_quals);
   14554              :   return r;
   14555              : }
   14556              : 
   14557              : /* Make an argument pack out of the TREE_VEC VEC.  */
   14558              : 
   14559              : static tree
   14560           27 : make_argument_pack (tree vec)
   14561              : {
   14562           27 :   tree pack;
   14563              : 
   14564           27 :   if (TYPE_P (TREE_VEC_ELT (vec, 0)))
   14565           15 :     pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   14566              :   else
   14567              :     {
   14568           12 :       pack = make_node (NONTYPE_ARGUMENT_PACK);
   14569           12 :       TREE_CONSTANT (pack) = 1;
   14570              :     }
   14571           27 :   ARGUMENT_PACK_ARGS (pack) = vec;
   14572           27 :   return pack;
   14573              : }
   14574              : 
   14575              : /* Return an exact copy of template args T that can be modified
   14576              :    independently.  */
   14577              : 
   14578              : tree
   14579     24687695 : copy_template_args (tree t)
   14580              : {
   14581     24687695 :   if (t == error_mark_node)
   14582              :     return t;
   14583              : 
   14584     24687695 :   int len = TREE_VEC_LENGTH (t);
   14585     24687695 :   tree new_vec = make_tree_vec (len);
   14586              : 
   14587     83411865 :   for (int i = 0; i < len; ++i)
   14588              :     {
   14589     58724170 :       tree elt = TREE_VEC_ELT (t, i);
   14590     58724170 :       if (elt && TREE_CODE (elt) == TREE_VEC)
   14591      1912114 :         elt = copy_template_args (elt);
   14592     58724170 :       TREE_VEC_ELT (new_vec, i) = elt;
   14593              :     }
   14594              : 
   14595     49375390 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
   14596     24687695 :     = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   14597              : 
   14598     24687695 :   return new_vec;
   14599              : }
   14600              : 
   14601              : /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg.  */
   14602              : 
   14603              : tree
   14604    126573452 : tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
   14605              :                       tree in_decl)
   14606              : {
   14607              :   /* This flag is used only during deduction, and we don't expect to
   14608              :      substitute such ARGUMENT_PACKs.  */
   14609    126573452 :   gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
   14610              : 
   14611              :   /* Substitute into each of the arguments.  */
   14612    126573452 :   tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
   14613              :                                          args, complain, in_decl);
   14614    126573452 :   if (pack_args == error_mark_node)
   14615              :     return error_mark_node;
   14616              : 
   14617    125753903 :   if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
   14618              :     return orig_arg;
   14619              : 
   14620              :   /* If we're substituting into a generic ARGUMENT_PACK for a variadic
   14621              :      template parameter, we might be able to avoid allocating a new
   14622              :      ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
   14623              :      if the substituted result is identical to it.  */
   14624    121749363 :   if (tree parm = template_arg_to_parm (orig_arg))
   14625              :     {
   14626     53496468 :       int level, index;
   14627     53496468 :       template_parm_level_and_index (parm, &level, &index);
   14628    158077627 :       if (TMPL_ARGS_DEPTH (args) >= level)
   14629     93845414 :         if (tree arg = TMPL_ARG (args, level, index))
   14630     43956286 :           if (TREE_CODE (arg) == TREE_CODE (orig_arg)
   14631     43956286 :               && ARGUMENT_PACK_ARGS (arg) == pack_args)
   14632              :             {
   14633     42637028 :               gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
   14634     42637028 :               return arg;
   14635              :             }
   14636              :     }
   14637              : 
   14638     79112335 :   tree new_arg;
   14639     79112335 :   if (TYPE_P (orig_arg))
   14640              :     {
   14641     78396925 :       new_arg = cxx_make_type (TREE_CODE (orig_arg));
   14642     78396925 :       SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
   14643              :     }
   14644              :   else
   14645              :     {
   14646       715410 :       new_arg = make_node (TREE_CODE (orig_arg));
   14647       715410 :       TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
   14648              :     }
   14649     79112335 :   ARGUMENT_PACK_ARGS (new_arg) = pack_args;
   14650              :   return new_arg;
   14651              : }
   14652              : 
   14653              : /* Substitute ARGS into the vector or list of template arguments T.  */
   14654              : 
   14655              : tree
   14656   1685343567 : tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14657              : {
   14658   1685343567 :   if (t == error_mark_node)
   14659              :     return error_mark_node;
   14660              : 
   14661              :   /* In "sizeof(X<I>)" we need to evaluate "I".  */
   14662   1685343567 :   cp_evaluated ev;
   14663              : 
   14664   1685343567 :   const int len = TREE_VEC_LENGTH (t);
   14665   1685343567 :   tree *elts = XALLOCAVEC (tree, len);
   14666   1685343567 :   int expanded_len_adjust = 0;
   14667              : 
   14668              :   /* True iff the substituted result is identical to T.  */
   14669   1685343567 :   bool const_subst_p = true;
   14670              : 
   14671   4988487826 :   for (int i = 0; i < len; i++)
   14672              :     {
   14673   3305097100 :       tree orig_arg = TREE_VEC_ELT (t, i);
   14674   3305097100 :       tree new_arg;
   14675              : 
   14676   3305097100 :       if (!orig_arg)
   14677              :         new_arg = NULL_TREE;
   14678   3305097034 :       else if (TREE_CODE (orig_arg) == TREE_VEC)
   14679     77474332 :         new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
   14680   3227622702 :       else if (PACK_EXPANSION_P (orig_arg))
   14681              :         {
   14682              :           /* Substitute into an expansion expression.  */
   14683     75990023 :           new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
   14684              : 
   14685     75990023 :           if (TREE_CODE (new_arg) == TREE_VEC)
   14686              :             /* Add to the expanded length adjustment the number of
   14687              :                expanded arguments. We subtract one from this
   14688              :                measurement, because the argument pack expression
   14689              :                itself is already counted as 1 in
   14690              :                LEN. EXPANDED_LEN_ADJUST can actually be negative, if
   14691              :                the argument pack is empty.  */
   14692     60495795 :             expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
   14693              :         }
   14694   3151632679 :       else if (ARGUMENT_PACK_P (orig_arg))
   14695    123660962 :         new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
   14696              :       else
   14697   3027971717 :         new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
   14698              : 
   14699   3305097064 :       if (new_arg == error_mark_node)
   14700              :         return error_mark_node;
   14701              : 
   14702   3303144259 :       elts[i] = new_arg;
   14703   3303144259 :       if (new_arg != orig_arg)
   14704   3187725657 :         const_subst_p = false;
   14705              :     }
   14706              : 
   14707   1683390726 :   if (const_subst_p)
   14708              :     return t;
   14709              : 
   14710   1666229933 :   tree maybe_reuse = NULL_TREE;
   14711              : 
   14712              :   /* If ARGS and T are both multi-level, the substituted result may be
   14713              :      identical to ARGS.  */
   14714   1666229933 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
   14715     77282534 :       && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
   14716      7563178 :       && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
   14717              :     maybe_reuse = args;
   14718              :   /* If T appears to be a vector of generic template arguments, the
   14719              :      substituted result may be identical to the corresponding level
   14720              :      from ARGS.  */
   14721   1659487342 :   else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
   14722              :     {
   14723   1271492495 :       int level, index;
   14724   1271492495 :       template_parm_level_and_index (parm, &level, &index);
   14725   3617491151 :       if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
   14726   2306138938 :         maybe_reuse = TMPL_ARGS_LEVEL (args, level);
   14727              :     }
   14728              : 
   14729              :   /* If the substituted result is identical to MAYBE_REUSE, return
   14730              :      it and avoid allocating a new TREE_VEC, as an optimization.  */
   14731    118423026 :   if (maybe_reuse != NULL_TREE
   14732   1159812060 :       && TREE_VEC_LENGTH (maybe_reuse) == len
   14733   2197142806 :       && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
   14734              :     return maybe_reuse;
   14735              : 
   14736              :   /* If T consists of only a pack expansion for which substitution yielded
   14737              :      a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
   14738              :      of effectively making a copy.  */
   14739    656701679 :   if (len == 1
   14740    385756241 :       && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
   14741    718862977 :       && TREE_CODE (elts[0]) == TREE_VEC)
   14742              :     return elts[0];
   14743              : 
   14744              :   /* Make space for the expanded arguments coming from template
   14745              :      argument packs.  */
   14746    605905845 :   tree r = make_tree_vec (len + expanded_len_adjust);
   14747              :   /* T can contain TREE_VECs. That happens if T contains the
   14748              :      arguments for a member template.
   14749              :      In that case each TREE_VEC in T represents a level of template
   14750              :      arguments, and T won't carry any non defaulted argument count.
   14751              :      It will rather be the nested TREE_VECs that will carry one.
   14752              :      In other words, T carries a non defaulted argument count only
   14753              :      if it doesn't contain any nested TREE_VEC.  */
   14754    605905845 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
   14755              :     {
   14756    572897554 :       int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   14757    572897554 :       count += expanded_len_adjust;
   14758    572897554 :       SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
   14759              :     }
   14760              : 
   14761              :   int out = 0;
   14762   1554412755 :   for (int i = 0; i < len; i++)
   14763              :     {
   14764    948506910 :       tree orig_arg = TREE_VEC_ELT (t, i);
   14765    948506910 :       if (orig_arg
   14766    948506883 :           && PACK_EXPANSION_P (orig_arg)
   14767     21895206 :           && TREE_CODE (elts[i]) == TREE_VEC)
   14768              :         {
   14769              :           /* Now expand the template argument pack "in place".  */
   14770     23970648 :           for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
   14771     14270687 :             TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
   14772              :         }
   14773              :       else
   14774              :         {
   14775    938806949 :           TREE_VEC_ELT (r, out) = elts[i];
   14776    938806949 :           out++;
   14777              :         }
   14778              :     }
   14779    605905845 :   gcc_assert (out == TREE_VEC_LENGTH (r));
   14780              : 
   14781              :   return r;
   14782   1685343531 : }
   14783              : 
   14784              : /* Substitute ARGS into one level PARMS of template parameters.  */
   14785              : 
   14786              : static tree
   14787     26069708 : tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
   14788              : {
   14789     26069708 :   if (parms == error_mark_node)
   14790              :     return error_mark_node;
   14791              : 
   14792     26069708 :   tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
   14793              : 
   14794     65426419 :   for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
   14795              :     {
   14796     39356711 :       tree tuple = TREE_VEC_ELT (parms, i);
   14797              : 
   14798     39356711 :       if (tuple == error_mark_node)
   14799            0 :         continue;
   14800              : 
   14801     39356711 :       TREE_VEC_ELT (new_vec, i) =
   14802     39356711 :         tsubst_template_parm (tuple, args, complain);
   14803              :     }
   14804              : 
   14805              :   return new_vec;
   14806              : }
   14807              : 
   14808              : /* Return the result of substituting ARGS into the template parameters
   14809              :    given by PARMS.  If there are m levels of ARGS and m + n levels of
   14810              :    PARMS, then the result will contain n levels of PARMS.  For
   14811              :    example, if PARMS is `template <class T> template <class U>
   14812              :    template <T*, U, class V>' and ARGS is {{int}, {double}} then the
   14813              :    result will be `template <int*, double, class V>'.  */
   14814              : 
   14815              : static tree
   14816     26178744 : tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
   14817              : {
   14818     26178744 :   tree r = NULL_TREE;
   14819     26178744 :   tree* new_parms;
   14820              : 
   14821              :   /* When substituting into a template, we must set
   14822              :      PROCESSING_TEMPLATE_DECL as the template parameters may be
   14823              :      dependent if they are based on one-another, and the dependency
   14824              :      predicates are short-circuit outside of templates.  */
   14825     26178744 :   ++processing_template_decl;
   14826              : 
   14827     26178744 :   for (new_parms = &r;
   14828    156521384 :        parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
   14829     52139276 :        new_parms = &(TREE_CHAIN (*new_parms)),
   14830     26069638 :          parms = TREE_CHAIN (parms))
   14831              :     {
   14832     26069638 :       tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
   14833              :                                                   args, complain);
   14834     52139276 :       *new_parms =
   14835     52139272 :         tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
   14836              :                              - TMPL_ARGS_DEPTH (args)),
   14837              :                    new_vec, NULL_TREE);
   14838     52139276 :       TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
   14839     52139276 :         = TEMPLATE_PARMS_CONSTRAINTS (parms);
   14840              :     }
   14841              : 
   14842     26178744 :   --processing_template_decl;
   14843              : 
   14844     26178744 :   return r;
   14845              : }
   14846              : 
   14847              : /* Return the result of substituting ARGS into one template parameter
   14848              :    given by T. T Must be a TREE_LIST which TREE_VALUE is the template
   14849              :    parameter and which TREE_PURPOSE is the default argument of the
   14850              :    template parameter.  */
   14851              : 
   14852              : static tree
   14853     39356711 : tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
   14854              : {
   14855     39356711 :   tree default_value, parm_decl;
   14856              : 
   14857     39356711 :   if (args == NULL_TREE
   14858     39356711 :       || t == NULL_TREE
   14859     39356707 :       || t == error_mark_node)
   14860              :     return t;
   14861              : 
   14862     39356707 :   gcc_assert (TREE_CODE (t) == TREE_LIST);
   14863              : 
   14864     39356707 :   default_value = TREE_PURPOSE (t);
   14865     39356707 :   parm_decl = TREE_VALUE (t);
   14866              : 
   14867     39356707 :   parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
   14868     39356707 :   if (TREE_CODE (parm_decl) == PARM_DECL
   14869     39356707 :       && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
   14870           28 :     parm_decl = error_mark_node;
   14871     39356707 :   default_value = tsubst_template_arg (default_value, args,
   14872              :                                        complain, NULL_TREE);
   14873              : 
   14874     39356707 :   tree r = build_tree_list (default_value, parm_decl);
   14875     39356707 :   TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
   14876     39356707 :   return r;
   14877              : }
   14878              : 
   14879              : /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
   14880              :    parameter in PARMS for sake of declaration matching.  */
   14881              : 
   14882              : static void
   14883       591722 : tsubst_each_template_parm_constraints (tree parms, tree args,
   14884              :                                        tsubst_flags_t complain)
   14885              : {
   14886       591722 :   ++processing_template_decl;
   14887      1183444 :   for (; parms; parms = TREE_CHAIN (parms))
   14888              :     {
   14889       591722 :       tree level = TREE_VALUE (parms);
   14890      1810792 :       for (tree parm : tree_vec_range (level))
   14891      2438140 :         TEMPLATE_PARM_CONSTRAINTS (parm)
   14892      2438140 :           = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
   14893              :                                complain, NULL_TREE);
   14894              :     }
   14895       591722 :   --processing_template_decl;
   14896       591722 : }
   14897              : 
   14898              : /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
   14899              :    indexed in reverse order of the parameters.  */
   14900              : 
   14901              : static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
   14902              : 
   14903              : /* Return a reference to the vec* of defarg insts for FN.  */
   14904              : 
   14905              : static vec<tree,va_gc> *&
   14906       721772 : defarg_insts_for (tree fn)
   14907              : {
   14908       721772 :   if (!defarg_inst)
   14909        13085 :     defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
   14910       721772 :   tree_vec_map in = { { fn }, nullptr };
   14911       721772 :   tree_vec_map **slot
   14912       721772 :     = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
   14913       721772 :   if (!*slot)
   14914              :     {
   14915       500084 :       *slot = ggc_alloc<tree_vec_map> ();
   14916       500084 :       **slot = in;
   14917              :     }
   14918       721772 :   return (*slot)->to;
   14919              : }
   14920              : 
   14921              : /* Substitute into the default argument ARG (a default argument for
   14922              :    FN), which has the indicated TYPE.  */
   14923              : 
   14924              : tree
   14925       744822 : tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
   14926              :                          tsubst_flags_t complain)
   14927              : {
   14928       744822 :   int errs = errorcount + sorrycount;
   14929              : 
   14930              :   /* This can happen in invalid code.  */
   14931       744822 :   if (TREE_CODE (arg) == DEFERRED_PARSE)
   14932              :     return arg;
   14933              : 
   14934              :   /* Shortcut {}.  */
   14935        23147 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg)
   14936       767872 :       && CONSTRUCTOR_NELTS (arg) == 0)
   14937              :     return arg;
   14938              : 
   14939       721772 :   tree parm = FUNCTION_FIRST_USER_PARM (fn);
   14940       721772 :   parm = chain_index (parmnum, parm);
   14941       721772 :   tree parmtype = TREE_TYPE (parm);
   14942       721772 :   if (DECL_BY_REFERENCE (parm))
   14943            3 :     parmtype = TREE_TYPE (parmtype);
   14944       721772 :   if (parmtype == error_mark_node)
   14945              :     return error_mark_node;
   14946              : 
   14947       721772 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
   14948              : 
   14949              :   /* Remember the location of the pointer to the vec rather than the location
   14950              :      of the particular element, in case the vec grows in tsubst_expr.  */
   14951       721772 :   vec<tree,va_gc> *&defs = defarg_insts_for (fn);
   14952              :   /* Index in reverse order to avoid allocating space for initial parameters
   14953              :      that don't have default arguments.  */
   14954       721772 :   unsigned ridx = list_length (parm);
   14955       943460 :   if (vec_safe_length (defs) < ridx)
   14956       500227 :     vec_safe_grow_cleared (defs, ridx);
   14957       221545 :   else if (tree inst = (*defs)[ridx - 1])
   14958              :     return inst;
   14959              : 
   14960              :   /* This default argument came from a template.  Instantiate the
   14961              :      default argument here, not in tsubst.  In the case of
   14962              :      something like:
   14963              : 
   14964              :        template <class T>
   14965              :        struct S {
   14966              :          static T t();
   14967              :          void f(T = t());
   14968              :        };
   14969              : 
   14970              :      we must be careful to do name lookup in the scope of S<T>,
   14971              :      rather than in the current class.  */
   14972       538815 :   push_to_top_level ();
   14973       538815 :   push_access_scope (fn);
   14974       538815 :   push_deferring_access_checks (dk_no_deferred);
   14975              :   /* So in_immediate_context knows this is a default argument.  */
   14976       538815 :   begin_scope (sk_function_parms, fn);
   14977       538815 :   start_lambda_scope (parm);
   14978              : 
   14979              :   /* The default argument expression may cause implicitly defined
   14980              :      member functions to be synthesized, which will result in garbage
   14981              :      collection.  We must treat this situation as if we were within
   14982              :      the body of function so as to avoid collecting live data on the
   14983              :      stack.  */
   14984       538815 :   ++function_depth;
   14985       538815 :   arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
   14986       538815 :   --function_depth;
   14987              : 
   14988       538815 :   finish_lambda_scope ();
   14989              : 
   14990              :   /* Make sure the default argument is reasonable.  */
   14991       538815 :   arg = check_default_argument (type, arg, complain);
   14992              : 
   14993       538815 :   if (errorcount+sorrycount > errs
   14994       538815 :       && (complain & tf_warning_or_error))
   14995           15 :     inform (input_location,
   14996              :             "  when instantiating default argument for call to %qD", fn);
   14997              : 
   14998       538815 :   leave_scope ();
   14999       538815 :   pop_deferring_access_checks ();
   15000       538815 :   pop_access_scope (fn);
   15001       538815 :   pop_from_top_level ();
   15002              : 
   15003       538815 :   if (arg != error_mark_node && !cp_unevaluated_operand)
   15004       536395 :     (*defs)[ridx - 1] = arg;
   15005              : 
   15006              :   return arg;
   15007              : }
   15008              : 
   15009              : /* Substitute into all the default arguments for FN.  */
   15010              : 
   15011              : static void
   15012       858052 : tsubst_default_arguments (tree fn, tsubst_flags_t complain)
   15013              : {
   15014       858052 :   tree arg;
   15015       858052 :   tree tmpl_args;
   15016              : 
   15017       858052 :   tmpl_args = DECL_TI_ARGS (fn);
   15018              : 
   15019              :   /* If this function is not yet instantiated, we certainly don't need
   15020              :      its default arguments.  */
   15021       858052 :   if (uses_template_parms (tmpl_args))
   15022              :     return;
   15023              :   /* Don't do this again for clones.  */
   15024       858052 :   if (DECL_CLONED_FUNCTION_P (fn))
   15025              :     return;
   15026              : 
   15027       858052 :   int i = 0;
   15028       858052 :   for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
   15029      3402764 :        arg;
   15030      2544712 :        arg = TREE_CHAIN (arg), ++i)
   15031      2544712 :     if (TREE_PURPOSE (arg))
   15032           12 :       TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
   15033           12 :                                                     TREE_VALUE (arg),
   15034           12 :                                                     TREE_PURPOSE (arg),
   15035              :                                                     complain);
   15036              : }
   15037              : 
   15038              : /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier.  */
   15039              : static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
   15040              : 
   15041              : /* Store a pair to EXPLICIT_SPECIFIER_MAP.  */
   15042              : 
   15043              : void
   15044      1526935 : store_explicit_specifier (tree v, tree t)
   15045              : {
   15046      1526935 :   if (!explicit_specifier_map)
   15047        12513 :     explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
   15048      1526935 :   DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
   15049      1526935 :   explicit_specifier_map->put (v, t);
   15050      1526935 : }
   15051              : 
   15052              : /* Lookup an element in EXPLICIT_SPECIFIER_MAP.  */
   15053              : 
   15054              : tree
   15055      1763128 : lookup_explicit_specifier (tree v)
   15056              : {
   15057      1763128 :   return *explicit_specifier_map->get (v);
   15058              : }
   15059              : 
   15060              : /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
   15061              :    FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
   15062              :    are ARG_TYPES, and exception specification is RAISES, and otherwise is
   15063              :    identical to T.  */
   15064              : 
   15065              : static tree
   15066    160972849 : rebuild_function_or_method_type (tree t, tree args, tree return_type,
   15067              :                                  tree arg_types, tree raises,
   15068              :                                  tsubst_flags_t complain)
   15069              : {
   15070    160972849 :   gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
   15071              : 
   15072    160972849 :   tree new_type;
   15073    160972849 :   if (TREE_CODE (t) == FUNCTION_TYPE)
   15074              :     {
   15075     61120430 :       new_type = cp_build_function_type (return_type, arg_types);
   15076     61120430 :       new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
   15077              :     }
   15078              :   else
   15079              :     {
   15080     99852419 :       tree r = TREE_TYPE (TREE_VALUE (arg_types));
   15081              :       /* Don't pick up extra function qualifiers from the basetype.  */
   15082     99852419 :       r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
   15083     99852419 :       if (! MAYBE_CLASS_TYPE_P (r))
   15084              :         {
   15085              :           /* [temp.deduct]
   15086              : 
   15087              :              Type deduction may fail for any of the following
   15088              :              reasons:
   15089              : 
   15090              :              -- Attempting to create "pointer to member of T" when T
   15091              :              is not a class type.  */
   15092            0 :           if (complain & tf_error)
   15093            0 :             error ("creating pointer to member function of non-class type %qT",
   15094              :                    r);
   15095            0 :           return error_mark_node;
   15096              :         }
   15097              : 
   15098     99852419 :       new_type = build_method_type_directly (r, return_type,
   15099     99852419 :                                              TREE_CHAIN (arg_types));
   15100              :     }
   15101    160972849 :   if (!apply_late_template_attributes (&new_type, TYPE_ATTRIBUTES (t), 0,
   15102              :                                        args, complain, NULL_TREE))
   15103            0 :     return error_mark_node;
   15104              : 
   15105    160972849 :   cp_ref_qualifier rqual = type_memfn_rqual (t);
   15106    160972849 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   15107    160972849 :   return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
   15108              : }
   15109              : 
   15110              : /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
   15111              :    each of its formal parameters.  If there is a disagreement then rebuild
   15112              :    DECL's function type according to its formal parameter types, as part of a
   15113              :    resolution for Core issues 1001/1322.  */
   15114              : 
   15115              : static void
   15116    137955375 : maybe_rebuild_function_decl_type (tree decl, tree args)
   15117              : {
   15118    137955375 :   bool function_type_needs_rebuilding = false;
   15119    137955375 :   if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
   15120              :     {
   15121    100620445 :       tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
   15122    375015992 :       while (parm_type_list && parm_type_list != void_list_node)
   15123              :         {
   15124    173775208 :           tree parm_type = TREE_VALUE (parm_type_list);
   15125    173775208 :           tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   15126    173775208 :           if (!same_type_p (parm_type, formal_parm_type_unqual))
   15127              :             {
   15128              :               function_type_needs_rebuilding = true;
   15129              :               break;
   15130              :             }
   15131              : 
   15132    173775102 :           parm_list = DECL_CHAIN (parm_list);
   15133    173775102 :           parm_type_list = TREE_CHAIN (parm_type_list);
   15134              :         }
   15135              :     }
   15136              : 
   15137    100620445 :   if (!function_type_needs_rebuilding)
   15138    137955269 :     return;
   15139              : 
   15140          106 :   const tree fntype = TREE_TYPE (decl);
   15141          106 :   tree parm_list = DECL_ARGUMENTS (decl);
   15142          106 :   tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
   15143          106 :   tree new_parm_type_list = NULL_TREE;
   15144          106 :   tree *q = &new_parm_type_list;
   15145          136 :   for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
   15146              :     {
   15147           30 :       *q = copy_node (old_parm_type_list);
   15148           30 :       parm_list = DECL_CHAIN (parm_list);
   15149           30 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   15150           30 :       q = &TREE_CHAIN (*q);
   15151              :     }
   15152          221 :   while (old_parm_type_list && old_parm_type_list != void_list_node)
   15153              :     {
   15154          115 :       *q = copy_node (old_parm_type_list);
   15155          115 :       tree *new_parm_type = &TREE_VALUE (*q);
   15156          115 :       tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   15157          115 :       if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
   15158          106 :         *new_parm_type = formal_parm_type_unqual;
   15159              : 
   15160          115 :       parm_list = DECL_CHAIN (parm_list);
   15161          115 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   15162          115 :       q = &TREE_CHAIN (*q);
   15163              :     }
   15164          106 :   if (old_parm_type_list == void_list_node)
   15165          106 :     *q = void_list_node;
   15166              : 
   15167          106 :   TREE_TYPE (decl)
   15168          212 :     = rebuild_function_or_method_type (fntype, args,
   15169          106 :                                        TREE_TYPE (fntype), new_parm_type_list,
   15170          106 :                                        TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
   15171              : }
   15172              : 
   15173              : /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL.  */
   15174              : 
   15175              : static tree
   15176    140549468 : tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
   15177              :                       tree lambda_fntype, bool use_spec_table = true)
   15178              : {
   15179    140549468 :   tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
   15180    140549468 :   hashval_t hash = 0;
   15181    140549468 :   tree in_decl = t;
   15182              : 
   15183              :   /* Nobody should be tsubst'ing into non-template functions.  */
   15184    140549468 :   gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
   15185              :               || DECL_LOCAL_DECL_P (t));
   15186              : 
   15187    140549468 :   if (DECL_LOCAL_DECL_P (t))
   15188              :     {
   15189          382 :       if (tree spec = retrieve_local_specialization (t))
   15190              :         return spec;
   15191              :     }
   15192    140549086 :   else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
   15193              :     {
   15194              :       /* If T is not dependent, just return it.  */
   15195    140522077 :       if (!uses_template_parms (DECL_TI_ARGS (t))
   15196    140522149 :           && !LAMBDA_FUNCTION_P (t))
   15197              :         return t;
   15198              : 
   15199              :       /* A non-templated friend doesn't get DECL_TEMPLATE_INFO.  */
   15200    140051549 :       if (non_templated_friend_p (t))
   15201        22321 :         goto friend_case;
   15202              : 
   15203              :       /* Calculate the most general template of which R is a
   15204              :          specialization.  */
   15205    140029228 :       gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
   15206              : 
   15207              :       /* We're substituting a lambda function under tsubst_lambda_expr but not
   15208              :          directly from it; find the matching function we're already inside.
   15209              :          But don't do this if T is a generic lambda with a single level of
   15210              :          template parms, as in that case we're doing a normal instantiation. */
   15211    141747335 :       if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
   15212    140131992 :           && (!generic_lambda_fn_p (t)
   15213       102740 :               || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
   15214           57 :         return enclosing_instantiation_of (t);
   15215              : 
   15216              :       /* Calculate the complete set of arguments used to
   15217              :          specialize R.  */
   15218    140029171 :       if (use_spec_table && !lambda_fntype)
   15219              :         {
   15220     86624810 :           argvec = tsubst_template_args (DECL_TI_ARGS
   15221              :                                          (DECL_TEMPLATE_RESULT
   15222              :                                           (DECL_TI_TEMPLATE (t))),
   15223              :                                          args, complain, in_decl);
   15224     86624810 :           if (argvec == error_mark_node)
   15225              :             return error_mark_node;
   15226              : 
   15227              :           /* Check to see if we already have this specialization.  */
   15228     86624810 :           hash = spec_hasher::hash (gen_tmpl, argvec);
   15229     86624810 :           if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
   15230              :             /* The spec for these args might be a partial instantiation of the
   15231              :                template, but here what we want is the FUNCTION_DECL.  */
   15232       568442 :             return STRIP_TEMPLATE (spec);
   15233              :         }
   15234              :       else
   15235              :         argvec = args;
   15236              :     }
   15237              :   else
   15238              :     {
   15239              :       /* This special case arises when we have something like this:
   15240              : 
   15241              :          template <class T> struct S {
   15242              :            friend void f<int>(int, double);
   15243              :          };
   15244              : 
   15245              :          Here, the DECL_TI_TEMPLATE for the friend declaration
   15246              :          will be an IDENTIFIER_NODE.  We are being called from
   15247              :          tsubst_friend_function, and we want only to create a
   15248              :          new decl (R) with appropriate types so that we can call
   15249              :          determine_specialization.  */
   15250        27009 :     friend_case:
   15251              :       gen_tmpl = NULL_TREE;
   15252              :       argvec = NULL_TREE;
   15253              :     }
   15254              : 
   15255              :   /* Make sure tsubst_decl substitutes all the parameters.  */
   15256    279012746 :   cp_evaluated ev;
   15257              : 
   15258    139510441 :   tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
   15259       192942 :                   : NULL_TREE);
   15260    139510441 :   tree ctx = closure ? closure : DECL_CONTEXT (t);
   15261    139510441 :   bool member = ctx && TYPE_P (ctx);
   15262              : 
   15263              :   /* If this is a static or xobj lambda, remove the 'this' pointer added in
   15264              :      tsubst_lambda_expr now that we know the closure type.  */
   15265    139510441 :   if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15266          166 :     lambda_fntype = static_fn_type (lambda_fntype);
   15267              : 
   15268    139510441 :   if (member && !closure)
   15269    111761442 :     ctx = tsubst_entering_scope (ctx, args, complain, t);
   15270              : 
   15271    139510441 :   tree type = (lambda_fntype ? lambda_fntype
   15272    139317499 :                : tsubst (TREE_TYPE (t), args,
   15273              :                          complain | tf_fndecl_type, in_decl));
   15274    139502305 :   if (type == error_mark_node)
   15275              :     return error_mark_node;
   15276              : 
   15277              :   /* If we hit excessive deduction depth, the type is bogus even if
   15278              :      it isn't error_mark_node, so don't build a decl.  */
   15279    137955402 :   if (excessive_deduction_depth)
   15280              :     return error_mark_node;
   15281              : 
   15282              :   /* We do NOT check for matching decls pushed separately at this
   15283              :      point, as they may not represent instantiations of this
   15284              :      template, and in any case are considered separate under the
   15285              :      discrete model.  */
   15286    137955402 :   tree r = copy_decl (t);
   15287    137955402 :   DECL_USE_TEMPLATE (r) = 0;
   15288    137955402 :   TREE_TYPE (r) = type;
   15289              :   /* Clear out the mangled name and RTL for the instantiation.  */
   15290    137955402 :   SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   15291    137955402 :   SET_DECL_RTL (r, NULL);
   15292              :   /* Leave DECL_INITIAL set on deleted instantiations.  */
   15293    137955402 :   if (!DECL_DELETED_FN (r))
   15294    134925019 :     DECL_INITIAL (r) = NULL_TREE;
   15295    137955402 :   DECL_CONTEXT (r) = ctx;
   15296    137955402 :   set_instantiating_module (r);
   15297              : 
   15298              :   /* Handle explicit(dependent-expr).  */
   15299    137955402 :   if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
   15300              :     {
   15301      1755018 :       tree spec = lookup_explicit_specifier (t);
   15302      1755018 :       spec = tsubst_expr (spec, args, complain, in_decl);
   15303      1755018 :       spec = build_explicit_specifier (spec, complain);
   15304      1755018 :       if (spec == error_mark_node)
   15305              :         return error_mark_node;
   15306      1754997 :       if (instantiation_dependent_expression_p (spec))
   15307      1131558 :         store_explicit_specifier (r, spec);
   15308              :       else
   15309              :         {
   15310       623439 :           DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
   15311       623439 :           DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
   15312              :         }
   15313              :     }
   15314              : 
   15315              :   /* OpenMP UDRs have the only argument a reference to the declared
   15316              :      type.  We want to diagnose if the declared type is a reference,
   15317              :      which is invalid, but as references to references are usually
   15318              :      quietly merged, diagnose it here.  */
   15319    137955381 :   if (DECL_OMP_DECLARE_REDUCTION_P (t))
   15320              :     {
   15321          267 :       tree argtype
   15322          267 :         = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
   15323          267 :       argtype = tsubst (argtype, args, complain, in_decl);
   15324          267 :       if (TYPE_REF_P (argtype))
   15325            6 :         error_at (DECL_SOURCE_LOCATION (t),
   15326              :                   "reference type %qT in "
   15327              :                   "%<#pragma omp declare reduction%>", argtype);
   15328          267 :       if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   15329          210 :         DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
   15330              :                                           argtype);
   15331              :     }
   15332              : 
   15333    137955381 :   if (member && DECL_CONV_FN_P (r))
   15334              :     /* Type-conversion operator.  Reconstruct the name, in
   15335              :        case it's the name of one of the template's parameters.  */
   15336      1298128 :     DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
   15337              : 
   15338    137955381 :   tree parms = DECL_ARGUMENTS (t);
   15339    137955381 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15340       192776 :     parms = DECL_CHAIN (parms);
   15341    137955381 :   parms = tsubst (parms, args, complain, t);
   15342    137955381 :   if (parms == error_mark_node)
   15343              :     return error_mark_node;
   15344    411452357 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   15345    273496982 :     DECL_CONTEXT (parm) = r;
   15346    137955375 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15347              :     {
   15348       192776 :       tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
   15349       192776 :       DECL_NAME (tparm) = closure_identifier;
   15350       192776 :       DECL_CHAIN (tparm) = parms;
   15351       192776 :       parms = tparm;
   15352              :     }
   15353    137955375 :   DECL_ARGUMENTS (r) = parms;
   15354    137955375 :   DECL_RESULT (r) = NULL_TREE;
   15355              : 
   15356    137955375 :   maybe_rebuild_function_decl_type (r, args);
   15357              : 
   15358    137955375 :   TREE_STATIC (r) = 0;
   15359    137955375 :   TREE_PUBLIC (r) = TREE_PUBLIC (t);
   15360    137955375 :   DECL_EXTERNAL (r) = 1;
   15361              :   /* If this is an instantiation of a function with internal
   15362              :      linkage, we already know what object file linkage will be
   15363              :      assigned to the instantiation.  */
   15364    137955375 :   DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
   15365    137955375 :   DECL_DEFER_OUTPUT (r) = 0;
   15366    137955375 :   DECL_CHAIN (r) = NULL_TREE;
   15367    137955375 :   DECL_PENDING_INLINE_INFO (r) = 0;
   15368    137955375 :   DECL_PENDING_INLINE_P (r) = 0;
   15369    137955375 :   DECL_SAVED_TREE (r) = NULL_TREE;
   15370    137955375 :   DECL_STRUCT_FUNCTION (r) = NULL;
   15371    137955375 :   TREE_USED (r) = 0;
   15372              :   /* We'll re-clone as appropriate in instantiate_template.  */
   15373    137955375 :   DECL_CLONED_FUNCTION (r) = NULL_TREE;
   15374              : 
   15375              :   /* If we aren't complaining now, return on error before we register
   15376              :      the specialization so that we'll complain eventually.  */
   15377    137955375 :   if ((complain & tf_error) == 0
   15378     29297653 :       && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   15379    140062805 :       && !grok_op_properties (r, /*complain=*/false))
   15380            3 :     return error_mark_node;
   15381              : 
   15382              :   /* If we are looking at an xobj lambda, we might need to check the type of
   15383              :      its xobj parameter.  */
   15384    139672535 :   if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r))
   15385              :     {
   15386          616 :       tree closure_obj = DECL_CONTEXT (r);
   15387          616 :       tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj);
   15388          616 :       tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r));
   15389              : 
   15390          616 :       if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
   15391          441 :             || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)))
   15392              :         /* If a lambda has an empty capture clause, an xobj parameter of
   15393              :            unrelated type is not an error.  */;
   15394          475 :       else if (dependent_type_p (obj_param))
   15395              :         /* If we are coming from tsubst_lambda_expr we might not have
   15396              :            substituted into our xobj parameter yet.  We can't error out until
   15397              :            we know what the type really is so do nothing...
   15398              :            ...but if we are instantiating the call op for real and we don't
   15399              :            have a real type then something has gone incredibly wrong.  */
   15400          107 :         gcc_assert (lambda_fntype);
   15401              :       else
   15402              :         {
   15403              :           /* We have a lambda with captures, and know the type of the xobj
   15404              :              parameter, time to check it.  */
   15405          368 :           tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param));
   15406          368 :           if (!same_or_base_type_p (closure_obj, obj_param_type))
   15407              :             {
   15408              :               /* This error does not emit when the lambda's call operator
   15409              :                  template is instantiated by taking its address, such as in
   15410              :                  the following case:
   15411              : 
   15412              :                  auto f = [x = 0](this auto&&){};
   15413              :                  int (*fp)(int&) = &decltype(f)::operator();
   15414              : 
   15415              :                  It only emits when explicitly calling the call operator with
   15416              :                  an explicit template parameter:
   15417              : 
   15418              :                  template<typename T>
   15419              :                  struct S : T {
   15420              :                    using T::operator();
   15421              :                    operator int() const {return {};}
   15422              :                  };
   15423              : 
   15424              :                  auto s = S{[x = 0](this auto&&) {}};
   15425              :                  s.operator()<int>();
   15426              : 
   15427              :                  This is due to resolve_address_of_overloaded_function being
   15428              :                  deficient at reporting candidates when overload resolution
   15429              :                  fails.
   15430              : 
   15431              :                  This diagnostic will be active in the first case if/when
   15432              :                  resolve_address_of_overloaded_function is fixed to properly
   15433              :                  emit candidates upon failure to resolve to an overload.  */
   15434           36 :               if (complain & tf_error)
   15435            2 :                 error ("a lambda with captures may not have an explicit "
   15436              :                        "object parameter of an unrelated type");
   15437           36 :               return error_mark_node;
   15438              :             }
   15439              :         }
   15440              :     }
   15441              : 
   15442              :   /* Associate the constraints directly with the instantiation. We
   15443              :      don't substitute through the constraints; that's only done when
   15444              :      they are checked.  */
   15445    137955336 :   if (tree ci = get_constraints (t))
   15446      7128939 :     set_constraints (r, ci);
   15447              : 
   15448              :   /* copy_decl () does not know about contract specifiers.  NOTE these are not
   15449              :      substituted at this point.  */
   15450    137955336 :   if (tree ctrct = get_fn_contract_specifiers (t))
   15451          207 :     set_fn_contract_specifiers (r, ctrct);
   15452              : 
   15453              :   /* The parms have now been substituted, check for incorrect const cases.  */
   15454    137955336 :   check_postconditions_in_redecl (t, r);
   15455              : 
   15456    272563752 :   if (DECL_FRIEND_CONTEXT (t))
   15457      3453622 :     SET_DECL_FRIEND_CONTEXT (r,
   15458              :                              tsubst (DECL_FRIEND_CONTEXT (t),
   15459              :                                      args, complain, in_decl));
   15460              : 
   15461    137955336 :   if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15462              :                                        args, complain, in_decl))
   15463           15 :     return error_mark_node;
   15464              : 
   15465              :   /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
   15466              :      this in the special friend case mentioned above where
   15467              :      GEN_TMPL is NULL.  */
   15468    137955321 :   if (gen_tmpl && !closure)
   15469              :     {
   15470    137712670 :       DECL_TEMPLATE_INFO (r)
   15471    137712670 :         = build_template_info (gen_tmpl, argvec);
   15472    137712670 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   15473              : 
   15474    137712670 :       if (use_spec_table)
   15475              :         {
   15476     86056223 :           tree new_r
   15477     86056223 :             = register_specialization (r, gen_tmpl, argvec, false, hash);
   15478     86056223 :           if (new_r != r)
   15479              :             /* We instantiated this while substituting into
   15480              :                the type earlier (template/friend54.C).  */
   15481              :             return new_r;
   15482              :         }
   15483              : 
   15484              :       /* We're not supposed to instantiate default arguments
   15485              :          until they are called, for a template.  But, for a
   15486              :          declaration like:
   15487              : 
   15488              :          template <class T> void f ()
   15489              :          { extern void g(int i = T()); }
   15490              : 
   15491              :          we should do the substitution when the template is
   15492              :          instantiated.  We handle the member function case in
   15493              :          instantiate_class_template since the default arguments
   15494              :          might refer to other members of the class.  */
   15495    137712670 :       if (!member
   15496     26137506 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   15497    138570722 :           && !uses_template_parms (argvec))
   15498       858052 :         tsubst_default_arguments (r, complain);
   15499              :     }
   15500       242651 :   else if (DECL_LOCAL_DECL_P (r))
   15501              :     {
   15502          382 :       if (!cp_unevaluated_operand)
   15503          382 :         register_local_specialization (r, t);
   15504              :     }
   15505              :   else
   15506       242269 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15507              : 
   15508              :   /* Copy the list of befriending classes.  */
   15509    137955321 :   for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
   15510    138960686 :        *friends;
   15511      1005365 :        friends = &TREE_CHAIN (*friends))
   15512              :     {
   15513      1005365 :       *friends = copy_node (*friends);
   15514      1005365 :       TREE_VALUE (*friends)
   15515      2010730 :         = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
   15516              :     }
   15517              : 
   15518    275910642 :   if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
   15519              :     {
   15520     26241331 :       maybe_retrofit_in_chrg (r);
   15521     52482662 :       if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
   15522            6 :         return error_mark_node;
   15523              :       /* If this is an instantiation of a member template, clone it.
   15524              :          If it isn't, that'll be handled by
   15525              :          clone_constructors_and_destructors.  */
   15526     52482644 :       if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
   15527      9720785 :         clone_cdtor (r, /*update_methods=*/false);
   15528              :     }
   15529    111713990 :   else if ((complain & tf_error) != 0
   15530     83754602 :            && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   15531    131631180 :            && !grok_op_properties (r, /*complain=*/true))
   15532            3 :     return error_mark_node;
   15533              : 
   15534              :   /* Possibly limit visibility based on template args.  */
   15535    137955312 :   DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   15536    137955312 :   if (DECL_VISIBILITY_SPECIFIED (t))
   15537              :     {
   15538    111788246 :       DECL_VISIBILITY_SPECIFIED (r) = 0;
   15539    111788246 :       DECL_ATTRIBUTES (r)
   15540    223576492 :         = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   15541              :     }
   15542    137955312 :   determine_visibility (r);
   15543    137955312 :   if (DECL_SECTION_NAME (t))
   15544            6 :     set_decl_section_name (r, t);
   15545    142173839 :   if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
   15546        43179 :       && COMPLETE_TYPE_P (DECL_CONTEXT (r))
   15547    137955312 :       && !processing_template_decl)
   15548            0 :     defaulted_late_check (r);
   15549              : 
   15550    137955312 :   if (flag_openmp)
   15551       195040 :     if (tree attr = lookup_attribute ("omp declare variant base",
   15552       195040 :                                       DECL_ATTRIBUTES (r)))
   15553          191 :       omp_declare_variant_finalize (r, attr);
   15554              : 
   15555    137955312 :   return r;
   15556              : }
   15557              : 
   15558              : /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL.  */
   15559              : 
   15560              : static tree
   15561     26429621 : tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
   15562              :                       tree lambda_fntype, tree lambda_tparms)
   15563              : {
   15564              :   /* We can get here when processing a member function template,
   15565              :      member class template, or template template parameter.  */
   15566     26429621 :   tree decl = DECL_TEMPLATE_RESULT (t);
   15567     26429621 :   tree in_decl = t;
   15568     26429621 :   tree spec;
   15569     26429621 :   tree tmpl_args;
   15570     26429621 :   tree full_args = NULL_TREE;
   15571     26429621 :   tree r;
   15572     26429621 :   hashval_t hash = 0;
   15573              : 
   15574     26429621 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   15575              :     {
   15576              :       /* Template template parameter is treated here.  */
   15577        18326 :       tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15578        18326 :       if (new_type == error_mark_node)
   15579              :         r = error_mark_node;
   15580              :       /* If we get a real template back, return it.  This can happen in
   15581              :          the context of most_specialized_partial_spec.  */
   15582        18326 :       else if (TREE_CODE (new_type) == TEMPLATE_DECL)
   15583              :         r = new_type;
   15584              :       else
   15585              :         /* The new TEMPLATE_DECL was built in
   15586              :            reduce_template_parm_level.  */
   15587        36652 :         r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
   15588        18326 :       return r;
   15589              :     }
   15590              : 
   15591     26411295 :   if (!lambda_fntype)
   15592              :     {
   15593              :       /* We might already have an instance of this template.
   15594              :          The ARGS are for the surrounding class type, so the
   15595              :          full args contain the tsubst'd args for the context,
   15596              :          plus the innermost args from the template decl.  */
   15597     26373606 :       tmpl_args = DECL_CLASS_TEMPLATE_P (t)
   15598      1675651 :         ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
   15599     24697955 :         : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
   15600              :       /* Because this is a template, the arguments will still be
   15601              :          dependent, even after substitution.  If
   15602              :          PROCESSING_TEMPLATE_DECL is not set, the dependency
   15603              :          predicates will short-circuit.  */
   15604     26373606 :       ++processing_template_decl;
   15605     26373606 :       full_args = tsubst_template_args (tmpl_args, args,
   15606              :                                         complain, in_decl);
   15607     26373606 :       --processing_template_decl;
   15608     26373606 :       if (full_args == error_mark_node)
   15609              :         return error_mark_node;
   15610              : 
   15611              :       /* If this is a default template template argument,
   15612              :          tsubst might not have changed anything.  */
   15613     26373588 :       if (full_args == tmpl_args)
   15614              :         return t;
   15615              : 
   15616     26373588 :       hash = spec_hasher::hash (t, full_args);
   15617     26373588 :       spec = retrieve_specialization (t, full_args, hash);
   15618     26373588 :       if (spec != NULL_TREE)
   15619              :         {
   15620       761087 :           if (TYPE_P (spec))
   15621              :             /* Type partial instantiations are stored as the type by
   15622              :                lookup_template_class_1, not here as the template.  */
   15623       760523 :             spec = CLASSTYPE_TI_TEMPLATE (spec);
   15624          564 :           else if (TREE_CODE (spec) != TEMPLATE_DECL)
   15625          468 :             spec = DECL_TI_TEMPLATE (spec);
   15626       761087 :           return spec;
   15627              :         }
   15628              :     }
   15629              : 
   15630              :   /* Make a new template decl.  It will be similar to the
   15631              :      original, but will record the current template arguments.
   15632              :      We also create a new function declaration, which is just
   15633              :      like the old one, but points to this new template, rather
   15634              :      than the old one.  */
   15635     25650190 :   r = copy_decl (t);
   15636     25650190 :   gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
   15637     25650190 :   DECL_CHAIN (r) = NULL_TREE;
   15638              : 
   15639              :   // Build new template info linking to the original template decl.
   15640     25650190 :   if (!lambda_fntype)
   15641              :     {
   15642     25612501 :       DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   15643     25612501 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   15644              :     }
   15645              :   else
   15646        37689 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15647              : 
   15648              :   /* The template parameters for this new template are all the
   15649              :      template parameters for the old template, except the
   15650              :      outermost level of parameters.  */
   15651     25650190 :   auto tparm_guard = make_temp_override (current_template_parms);
   15652     51300380 :   DECL_TEMPLATE_PARMS (r)
   15653     51300380 :     = current_template_parms
   15654     25650190 :     = (lambda_tparms
   15655     25650190 :        ? lambda_tparms
   15656     25612501 :        : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
   15657              :                                 complain));
   15658              : 
   15659     25650190 :   bool class_p = false;
   15660     25650190 :   tree inner = decl;
   15661     25650190 :   ++processing_template_decl;
   15662     25650190 :   if (TREE_CODE (inner) == FUNCTION_DECL)
   15663     22397021 :     inner = tsubst_function_decl (inner, args, complain, lambda_fntype,
   15664              :                                   /*use_spec_table=*/false);
   15665              :   else
   15666              :     {
   15667      3253169 :       if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
   15668              :         {
   15669       915113 :           class_p = true;
   15670       915113 :           inner = TREE_TYPE (inner);
   15671              :         }
   15672      3253169 :       if (class_p)
   15673       915113 :         inner = tsubst_entering_scope (inner, args, complain, in_decl);
   15674              :       else
   15675      2338056 :         inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false);
   15676              :     }
   15677     25650190 :   --processing_template_decl;
   15678     25650190 :   if (inner == error_mark_node)
   15679              :     return error_mark_node;
   15680              : 
   15681     25650132 :   if (class_p)
   15682              :     {
   15683              :       /* For a partial specialization, we need to keep pointing to
   15684              :          the primary template.  */
   15685       915095 :       if (!DECL_TEMPLATE_SPECIALIZATION (t))
   15686              :         {
   15687       581220 :           CLASSTYPE_TI_TEMPLATE (inner) = r;
   15688       581220 :           CLASSTYPE_USE_TEMPLATE (inner) = 0;
   15689              :         }
   15690              : 
   15691       915095 :       DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
   15692       915095 :       inner = TYPE_MAIN_DECL (inner);
   15693              :     }
   15694     24735037 :   else if (lambda_fntype)
   15695              :     {
   15696        37689 :       tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
   15697        37689 :       DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
   15698              :     }
   15699              :   else
   15700              :     {
   15701     24697348 :       DECL_TI_TEMPLATE (inner) = r;
   15702              :       /* Set DECL_TI_ARGS to the full set of template arguments,
   15703              :          which tsubst_function_decl / tsubst_decl didn't do due to
   15704              :          use_spec_table=false.  */
   15705     24697348 :       DECL_TI_ARGS (inner) = full_args;
   15706     24697348 :       DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
   15707              :     }
   15708              : 
   15709     25650132 :   DECL_TEMPLATE_RESULT (r) = inner;
   15710     25650132 :   TREE_TYPE (r) = TREE_TYPE (inner);
   15711     25650132 :   DECL_CONTEXT (r) = DECL_CONTEXT (inner);
   15712              : 
   15713     25650132 :   if (modules_p ())
   15714              :     {
   15715              :       /* Propagate module information from the decl.  */
   15716        75604 :       DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
   15717        75604 :       if (DECL_LANG_SPECIFIC (inner))
   15718              :         /* If this is a constrained template, the above tsubst of
   15719              :            inner can find the unconstrained template, which may have
   15720              :            come from an import.  This is ok, because we don't
   15721              :            register this instantiation (see below).  */
   15722        72613 :         gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
   15723              :                              || (TEMPLATE_PARMS_CONSTRAINTS
   15724              :                                  (DECL_TEMPLATE_PARMS (t))));
   15725              :     }
   15726              : 
   15727     25650132 :   DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
   15728     25650132 :   DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
   15729              : 
   15730     25650132 :   if (PRIMARY_TEMPLATE_P (t))
   15731     25314185 :     DECL_PRIMARY_TEMPLATE (r) = r;
   15732              : 
   15733     25650132 :   if (!lambda_fntype && !class_p)
   15734              :     {
   15735              :       /* Record this non-type partial instantiation.  */
   15736              :       /* FIXME we'd like to always register the TEMPLATE_DECL, or always
   15737              :          the DECL_TEMPLATE_RESULT, but it seems the modules code relies
   15738              :          on this current behavior.  */
   15739     24697348 :       if (TREE_CODE (inner) == FUNCTION_DECL)
   15740     22359292 :         register_specialization (r, t, full_args, false, hash);
   15741              :       else
   15742      2338056 :         register_specialization (inner, t, full_args, false, hash);
   15743              :     }
   15744              : 
   15745              :   return r;
   15746     25650190 : }
   15747              : 
   15748              : /* True if FN is the op() for a lambda in an uninstantiated template.  */
   15749              : 
   15750              : bool
   15751   1058834914 : lambda_fn_in_template_p (tree fn)
   15752              : {
   15753   1064771506 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15754              :     return false;
   15755       598101 :   tree closure = DECL_CONTEXT (fn);
   15756       598101 :   return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
   15757              : }
   15758              : 
   15759              : /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
   15760              :    which the above is true.  */
   15761              : 
   15762              : bool
   15763    881067606 : regenerated_lambda_fn_p (tree fn)
   15764              : {
   15765    885880186 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15766              :     return false;
   15767      1025856 :   tree closure = DECL_CONTEXT (fn);
   15768      1025856 :   tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
   15769      1025856 :   return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
   15770              : }
   15771              : 
   15772              : /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
   15773              :    If T is not a regenerated LAMBDA_EXPR, return T.  */
   15774              : 
   15775              : tree
   15776       357899 : most_general_lambda (tree t)
   15777              : {
   15778       719452 :   while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15779       361553 :     t = TI_TEMPLATE (ti);
   15780       357899 :   return t;
   15781              : }
   15782              : 
   15783              : /* Return the set of template arguments used to regenerate the lambda T
   15784              :    from its most general lambda.  */
   15785              : 
   15786              : tree
   15787       248870 : lambda_regenerating_args (tree t)
   15788              : {
   15789       497740 :   if (LAMBDA_FUNCTION_P (t))
   15790       248870 :     t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
   15791       248870 :   gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
   15792       248870 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15793       248870 :     return TI_ARGS (ti);
   15794              :   else
   15795              :     return NULL_TREE;
   15796              : }
   15797              : 
   15798              : /* We're instantiating a variable from template function TCTX.  Return the
   15799              :    corresponding current enclosing scope.  We can match them up using
   15800              :    DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
   15801              :    the DECL_SOURCE_LOCATION for a function instantiation is updated to match
   15802              :    the template definition in regenerate_decl_from_template.  */
   15803              : 
   15804              : static tree
   15805        99826 : enclosing_instantiation_of (tree tctx)
   15806              : {
   15807        99826 :   tree fn = current_function_decl;
   15808              : 
   15809              :   /* We shouldn't ever need to do this for other artificial functions.  */
   15810       100508 :   gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
   15811              : 
   15812        99829 :   for (; fn; fn = decl_function_context (fn))
   15813        99829 :     if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
   15814        99826 :       return fn;
   15815            0 :   gcc_unreachable ();
   15816              : }
   15817              : 
   15818              : /* Substitute the ARGS into the T, which is a _DECL.  Return the
   15819              :    result of the substitution.  Issue error and warning messages under
   15820              :    control of COMPLAIN.  The flag USE_SPEC_TABLE controls if we look up
   15821              :    and insert into the specializations table or if we can assume it's
   15822              :    the caller's responsibility; this is used by instantiate_template
   15823              :    to avoid doing some redundant work.  */
   15824              : 
   15825              : static tree
   15826    803762606 : tsubst_decl (tree t, tree args, tsubst_flags_t complain,
   15827              :              bool use_spec_table /* = true */)
   15828              : {
   15829              : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   15830    803762606 :   location_t saved_loc;
   15831    803762606 :   tree r = NULL_TREE;
   15832    803762606 :   tree in_decl = t;
   15833    803762606 :   hashval_t hash = 0;
   15834              : 
   15835    803762606 :   if (t == error_mark_node)
   15836              :     return error_mark_node;
   15837              : 
   15838              :   /* Set the filename and linenumber to improve error-reporting.  */
   15839    803762603 :   saved_loc = input_location;
   15840    803762603 :   input_location = DECL_SOURCE_LOCATION (t);
   15841              : 
   15842    803762603 :   switch (TREE_CODE (t))
   15843              :     {
   15844     26391932 :     case TEMPLATE_DECL:
   15845     26391932 :       r = tsubst_template_decl (t, args, complain,
   15846              :                                 /*lambda_fntype=*/NULL_TREE,
   15847              :                                 /*lambda_tparms=*/NULL_TREE);
   15848     26391932 :       break;
   15849              : 
   15850    117997194 :     case FUNCTION_DECL:
   15851    117997194 :       r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE,
   15852              :                                 use_spec_table);
   15853    117989058 :       break;
   15854              : 
   15855    312632430 :     case PARM_DECL:
   15856    312632430 :       {
   15857    312632430 :         tree type = NULL_TREE;
   15858    312632430 :         int i, len = 1;
   15859    312632430 :         tree expanded_types = NULL_TREE;
   15860    312632430 :         tree prev_r = NULL_TREE;
   15861    312632430 :         tree first_r = NULL_TREE;
   15862              : 
   15863    312632430 :         if (DECL_PACK_P (t))
   15864              :           {
   15865              :             /* If there is a local specialization that isn't a
   15866              :                parameter pack, it means that we're doing a "simple"
   15867              :                substitution from inside tsubst_pack_expansion. Just
   15868              :                return the local specialization (which will be a single
   15869              :                parm).  */
   15870      3733238 :             tree spec = retrieve_local_specialization (t);
   15871      3733238 :             if (spec
   15872           39 :                 && TREE_CODE (spec) == PARM_DECL
   15873      3733247 :                 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
   15874            0 :               RETURN (spec);
   15875              : 
   15876              :             /* Expand the TYPE_PACK_EXPANSION that provides the types for
   15877              :                the parameters in this function parameter pack.  */
   15878      3733238 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   15879              :                                                     complain, in_decl);
   15880      3733238 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   15881              :               {
   15882      1680943 :                 len = TREE_VEC_LENGTH (expanded_types);
   15883              : 
   15884              :                 /* Zero-length parameter packs are boring. Just substitute
   15885              :                    into the chain.  */
   15886      1680943 :                 if (len == 0 && !cp_unevaluated_operand)
   15887       614942 :                   RETURN (tsubst (TREE_CHAIN (t), args, complain,
   15888              :                                   TREE_CHAIN (t)));
   15889              :               }
   15890              :             else
   15891              :               {
   15892              :                 /* All we did was update the type. Make a note of that.  */
   15893              :                 type = expanded_types;
   15894              :                 expanded_types = NULL_TREE;
   15895              :               }
   15896              :           }
   15897              : 
   15898              :         /* Loop through all of the parameters we'll build. When T is
   15899              :            a function parameter pack, LEN is the number of expanded
   15900              :            types in EXPANDED_TYPES; otherwise, LEN is 1.  */
   15901    312017488 :         r = NULL_TREE;
   15902    624411489 :         for (i = 0; i < len; ++i)
   15903              :           {
   15904    312394007 :             prev_r = r;
   15905    312394007 :             r = copy_node (t);
   15906    312394007 :             if (DECL_TEMPLATE_PARM_P (t))
   15907      1824548 :               SET_DECL_TEMPLATE_PARM_P (r);
   15908              : 
   15909    312394007 :             if (expanded_types)
   15910              :               /* We're on the Ith parameter of the function parameter
   15911              :                  pack.  */
   15912              :               {
   15913              :                 /* Get the Ith type.  */
   15914      1442520 :                 type = TREE_VEC_ELT (expanded_types, i);
   15915              : 
   15916              :                 /* Rename the parameter to include the index.  */
   15917      1442520 :                 DECL_NAME (r)
   15918      2885040 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   15919              :               }
   15920    310951487 :             else if (!type)
   15921              :               /* We're dealing with a normal parameter.  */
   15922    308899192 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15923              : 
   15924    312394007 :             if (type == error_mark_node && !(complain & tf_error))
   15925            6 :               RETURN (error_mark_node);
   15926              : 
   15927    312394001 :             type = type_decays_to (type);
   15928    312394001 :             TREE_TYPE (r) = type;
   15929    312394001 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15930              : 
   15931    312394001 :             if (DECL_INITIAL (r))
   15932              :               {
   15933     44494315 :                 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
   15934     42669767 :                   DECL_INITIAL (r) = TREE_TYPE (r);
   15935              :                 else
   15936      1824548 :                   DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
   15937              :                                              complain, in_decl);
   15938              :               }
   15939              : 
   15940    312394001 :             DECL_CONTEXT (r) = NULL_TREE;
   15941              : 
   15942    312394001 :             if (!DECL_TEMPLATE_PARM_P (r))
   15943    310569453 :               DECL_ARG_TYPE (r) = type_passed_as (type);
   15944              : 
   15945    312394001 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15946              :                                                  args, complain, in_decl))
   15947            0 :               return error_mark_node;
   15948              : 
   15949              :             /* Keep track of the first new parameter we
   15950              :                generate. That's what will be returned to the
   15951              :                caller.  */
   15952    312394001 :             if (!first_r)
   15953    312017363 :               first_r = r;
   15954              : 
   15955              :             /* Build a proper chain of parameters when substituting
   15956              :                into a function parameter pack.  */
   15957    312394001 :             if (prev_r)
   15958       376638 :               DECL_CHAIN (prev_r) = r;
   15959              :           }
   15960              : 
   15961              :         /* If cp_unevaluated_operand is set, we're just looking for a
   15962              :            single dummy parameter, so don't keep going.  */
   15963    312017482 :         if (DECL_CHAIN (t) && !cp_unevaluated_operand)
   15964              :           {
   15965    161262926 :             tree chain = tsubst (DECL_CHAIN (t), args,
   15966    161262926 :                                  complain, DECL_CHAIN (t));
   15967    161262926 :             if (chain == error_mark_node)
   15968            3 :               RETURN (error_mark_node);
   15969    161262923 :             DECL_CHAIN (r) = chain;
   15970              :           }
   15971              : 
   15972              :         /* FIRST_R contains the start of the chain we've built.  */
   15973    312017479 :         r = first_r;
   15974              :       }
   15975    312017479 :       break;
   15976              : 
   15977      7496894 :     case FIELD_DECL:
   15978      7496894 :       {
   15979      7496894 :         tree type = NULL_TREE;
   15980      7496894 :         tree vec = NULL_TREE;
   15981      7496894 :         tree expanded_types = NULL_TREE;
   15982      7496894 :         int len = 1;
   15983              : 
   15984      7496894 :         if (PACK_EXPANSION_P (TREE_TYPE (t)))
   15985              :           {
   15986              :             /* This field is a lambda capture pack.  Return a TREE_VEC of
   15987              :                the expanded fields to instantiate_class_template_1.  */
   15988          368 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   15989              :                                                     complain, in_decl);
   15990          368 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   15991              :               {
   15992          359 :                 len = TREE_VEC_LENGTH (expanded_types);
   15993          359 :                 vec = make_tree_vec (len);
   15994              :               }
   15995              :             else
   15996              :               {
   15997              :                 /* All we did was update the type. Make a note of that.  */
   15998              :                 type = expanded_types;
   15999              :                 expanded_types = NULL_TREE;
   16000              :               }
   16001              :           }
   16002              : 
   16003     14993938 :         for (int i = 0; i < len; ++i)
   16004              :           {
   16005      7497205 :             r = copy_decl (t);
   16006      7497205 :             if (expanded_types)
   16007              :               {
   16008          670 :                 type = TREE_VEC_ELT (expanded_types, i);
   16009          670 :                 DECL_NAME (r)
   16010         1340 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   16011              :               }
   16012      7496535 :             else if (!type)
   16013      7496526 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16014              : 
   16015      7497199 :             if (type == error_mark_node)
   16016          155 :               RETURN (error_mark_node);
   16017      7497044 :             TREE_TYPE (r) = type;
   16018      7497044 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   16019              : 
   16020      7497044 :             if (DECL_C_BIT_FIELD (r))
   16021              :               /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
   16022              :                  number of bits.  */
   16023       487008 :               DECL_BIT_FIELD_REPRESENTATIVE (r)
   16024       487008 :                 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
   16025              :                                complain, in_decl);
   16026      7497044 :             if (DECL_INITIAL (t))
   16027              :               {
   16028              :                 /* Set up DECL_TEMPLATE_INFO so that we can get at the
   16029              :                    NSDMI in perform_member_init.  Still set DECL_INITIAL
   16030              :                    so that we know there is one.  */
   16031       793951 :                 DECL_INITIAL (r) = void_node;
   16032       793951 :                 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
   16033       793951 :                 retrofit_lang_decl (r);
   16034       793951 :                 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   16035              :               }
   16036              :             /* We don't have to set DECL_CONTEXT here; it is set by
   16037              :                finish_member_declaration.  */
   16038      7497044 :             DECL_CHAIN (r) = NULL_TREE;
   16039              : 
   16040      7497044 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   16041              :                                                  args, complain, in_decl))
   16042            0 :               return error_mark_node;
   16043              : 
   16044      7497044 :             if (vec)
   16045          670 :               TREE_VEC_ELT (vec, i) = r;
   16046              :           }
   16047              : 
   16048      7496733 :         if (vec)
   16049          359 :           r = vec;
   16050              :       }
   16051              :       break;
   16052              : 
   16053      1654601 :     case USING_DECL:
   16054              :       /* We reach here only for member using decls.  We also need to check
   16055              :          uses_template_parms because DECL_DEPENDENT_P is not set for a
   16056              :          using-declaration that designates a member of the current
   16057              :          instantiation (c++/53549).  */
   16058      1654601 :       if (DECL_DEPENDENT_P (t)
   16059      1654601 :           || uses_template_parms (USING_DECL_SCOPE (t)))
   16060              :         {
   16061              :           /* True iff this using-decl was written as a pack expansion
   16062              :              (and a pack appeared in its scope or name).  If a pack
   16063              :              appeared in both, we expand the packs separately and
   16064              :              manually merge them.  */
   16065      1654368 :           bool variadic_p = false;
   16066              : 
   16067      1654368 :           tree scope = USING_DECL_SCOPE (t);
   16068      1654368 :           if (PACK_EXPANSION_P (scope))
   16069              :             {
   16070          262 :               scope = tsubst_pack_expansion (scope, args,
   16071              :                                              complain | tf_qualifying_scope,
   16072              :                                              in_decl);
   16073          262 :               variadic_p = true;
   16074              :             }
   16075              :           else
   16076      1654106 :             scope = tsubst_scope (scope, args, complain, in_decl);
   16077              : 
   16078      1654368 :           tree name = DECL_NAME (t);
   16079      1654368 :           if (IDENTIFIER_CONV_OP_P (name)
   16080      1654368 :               && PACK_EXPANSION_P (TREE_TYPE (name)))
   16081              :             {
   16082           33 :               name = tsubst_pack_expansion (TREE_TYPE (name), args,
   16083              :                                             complain, in_decl);
   16084           33 :               if (name == error_mark_node)
   16085              :                 {
   16086            0 :                   r = error_mark_node;
   16087            0 :                   break;
   16088              :                 }
   16089           84 :               for (tree& elt : tree_vec_range (name))
   16090           51 :                 elt = make_conv_op_name (elt);
   16091           33 :               variadic_p = true;
   16092              :             }
   16093              :           else
   16094      3308670 :             name = tsubst_name (name, args, complain, in_decl);
   16095              : 
   16096      1654368 :           int len;
   16097      1654368 :           if (!variadic_p)
   16098              :             len = 1;
   16099          271 :           else if (TREE_CODE (scope) == TREE_VEC
   16100          262 :                    && TREE_CODE (name) == TREE_VEC)
   16101              :             {
   16102           24 :               if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
   16103              :                 {
   16104            3 :                   error ("mismatched argument pack lengths (%d vs %d)",
   16105            3 :                          TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
   16106            3 :                   r = error_mark_node;
   16107            3 :                   break;
   16108              :                 }
   16109              :               len = TREE_VEC_LENGTH (scope);
   16110              :             }
   16111          247 :           else if (TREE_CODE (scope) == TREE_VEC)
   16112          238 :             len = TREE_VEC_LENGTH (scope);
   16113              :           else /* TREE_CODE (name) == TREE_VEC  */
   16114            9 :             len = TREE_VEC_LENGTH (name);
   16115              : 
   16116      1654365 :           r = make_tree_vec (len);
   16117      3309104 :           for (int i = 0; i < len; ++i)
   16118              :             {
   16119      1654769 :               tree escope = (TREE_CODE (scope) == TREE_VEC
   16120      1654769 :                              ? TREE_VEC_ELT (scope, i)
   16121          660 :                              : scope);
   16122      1654769 :               tree ename = (TREE_CODE (name) == TREE_VEC
   16123      1654769 :                             ? TREE_VEC_ELT (name, i)
   16124           45 :                             : name);
   16125      1654769 :               tree elt = do_class_using_decl (escope, ename);
   16126      1654769 :               if (!elt)
   16127              :                 {
   16128           30 :                   r = error_mark_node;
   16129           30 :                   break;
   16130              :                 }
   16131      1654739 :               TREE_PROTECTED (elt) = TREE_PROTECTED (t);
   16132      1654739 :               TREE_PRIVATE (elt) = TREE_PRIVATE (t);
   16133      1654739 :               TREE_VEC_ELT (r, i) = elt;
   16134              :             }
   16135              : 
   16136      1654365 :           if (!variadic_p && r != error_mark_node)
   16137      1654067 :             r = TREE_VEC_ELT (r, 0);
   16138              :         }
   16139              :       else
   16140              :         {
   16141          233 :           r = copy_node (t);
   16142          233 :           DECL_CHAIN (r) = NULL_TREE;
   16143              :         }
   16144              :       break;
   16145              : 
   16146    235239861 :     case TYPE_DECL:
   16147    235239861 :     case VAR_DECL:
   16148    235239861 :       {
   16149    235239861 :         tree argvec = NULL_TREE;
   16150    235239861 :         tree gen_tmpl = NULL_TREE;
   16151    235239861 :         tree tmpl = NULL_TREE;
   16152    235239861 :         tree type = NULL_TREE;
   16153              : 
   16154    235239861 :         if (TREE_TYPE (t) == error_mark_node)
   16155           11 :           RETURN (error_mark_node);
   16156              : 
   16157    235239850 :         if (TREE_CODE (t) == TYPE_DECL
   16158    235239850 :             && (TREE_CODE (TREE_TYPE (t)) == TU_LOCAL_ENTITY
   16159    191268745 :                 || t == TYPE_MAIN_DECL (TREE_TYPE (t))))
   16160              :           {
   16161              :             /* If this is the canonical decl, we don't have to
   16162              :                mess with instantiations, and often we can't (for
   16163              :                typename, template type parms and such).  Note that
   16164              :                TYPE_NAME is not correct for the above test if
   16165              :                we've copied the type for a typedef.  */
   16166     39334462 :             type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16167     39334417 :             if (type == error_mark_node)
   16168            9 :               RETURN (error_mark_node);
   16169     39334408 :             r = TYPE_NAME (type);
   16170     39334408 :             break;
   16171              :           }
   16172              : 
   16173              :         /* Check to see if we already have the specialization we
   16174              :            need.  */
   16175    195905388 :         tree spec = NULL_TREE;
   16176    195905388 :         bool local_p = false;
   16177    195905388 :         tree ctx = DECL_CONTEXT (t);
   16178     43971102 :         if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
   16179    239876404 :             && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
   16180              :           {
   16181    174121341 :             local_p = false;
   16182    174121341 :             if (DECL_CLASS_SCOPE_P (t))
   16183              :               {
   16184    115591636 :                 ctx = tsubst_entering_scope (ctx, args, complain, in_decl);
   16185    115591636 :                 if (DECL_SELF_REFERENCE_P (t))
   16186              :                   /* The context and type of an injected-class-name are
   16187              :                      the same, so we don't need to substitute both.  */
   16188              :                   type = ctx;
   16189              :                 /* If CTX is unchanged, then T is in fact the
   16190              :                    specialization we want.  That situation occurs when
   16191              :                    referencing a static data member within in its own
   16192              :                    class.  We can use pointer equality, rather than
   16193              :                    same_type_p, because DECL_CONTEXT is always
   16194              :                    canonical...  */
   16195    115591636 :                 if (ctx == DECL_CONTEXT (t)
   16196              :                     /* ... unless T is a member template; in which
   16197              :                        case our caller can be willing to create a
   16198              :                        specialization of that template represented
   16199              :                        by T.  */
   16200    115591636 :                     && !(DECL_TI_TEMPLATE (t)
   16201     23242771 :                          && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
   16202              :                   spec = t;
   16203              :               }
   16204              : 
   16205              :             if (!spec)
   16206              :               {
   16207    169385332 :                 tmpl = DECL_TI_TEMPLATE (t);
   16208    169385332 :                 if (use_spec_table)
   16209              :                   {
   16210     88146474 :                     argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
   16211     88146474 :                     if (argvec == error_mark_node)
   16212            0 :                       RETURN (error_mark_node);
   16213     88146474 :                     gen_tmpl = most_general_template (tmpl);
   16214     88146474 :                     hash = spec_hasher::hash (gen_tmpl, argvec);
   16215     88146474 :                     spec = retrieve_specialization (gen_tmpl, argvec, hash);
   16216              :                   }
   16217              :                 else
   16218              :                   argvec = args;
   16219              :               }
   16220              :           }
   16221              :         else
   16222              :           {
   16223     21784047 :             if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
   16224              :               /* Subsequent calls to pushdecl will fill this in.  */
   16225              :               ctx = NULL_TREE;
   16226              :             /* A local variable.  */
   16227     21784047 :             local_p = true;
   16228              :             /* Unless this is a reference to a static variable from an
   16229              :                enclosing function, in which case we need to fill it in now.  */
   16230     21784047 :             if (TREE_STATIC (t))
   16231              :               {
   16232        99141 :                 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
   16233        99141 :                 if (fn != current_function_decl)
   16234     21784047 :                   ctx = fn;
   16235              :               }
   16236     21784047 :             spec = retrieve_local_specialization (t);
   16237              :           }
   16238              :         /* If we already have the specialization we need, there is
   16239              :            nothing more to do.  */
   16240    191169379 :         if (spec)
   16241              :           {
   16242      7909473 :             r = spec;
   16243      7909473 :             break;
   16244              :           }
   16245              : 
   16246              :         /* Create a new node for the specialization we need.  */
   16247    187995915 :         if (type == NULL_TREE)
   16248              :           {
   16249    148385159 :             if (is_typedef_decl (t))
   16250              :               type = DECL_ORIGINAL_TYPE (t);
   16251              :             else
   16252     36061779 :               type = TREE_TYPE (t);
   16253    148385159 :             if (VAR_P (t)
   16254     36061632 :                 && VAR_HAD_UNKNOWN_BOUND (t)
   16255    148385546 :                 && type != error_mark_node)
   16256          387 :               type = strip_array_domain (type);
   16257    148385159 :             tsubst_flags_t tcomplain = complain;
   16258    148385159 :             if (VAR_P (t))
   16259     36061632 :               tcomplain |= tf_tst_ok;
   16260    148385159 :             if (DECL_DECOMPOSITION_P (t) && DECL_PACK_P (t))
   16261              :               type = NULL_TREE;
   16262    148381799 :             else if (is_capture_proxy (t) && WILDCARD_TYPE_P (type))
   16263              :               /* We'll set type from DECL_VALUE_EXPR.  */
   16264              :               type = NULL_TREE;
   16265              :             else
   16266    148381622 :               type = tsubst (type, args, tcomplain, in_decl);
   16267              :             /* Substituting the type might have recursively instantiated this
   16268              :                same alias (c++/86171).  */
   16269     45362577 :             if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   16270    190031604 :                 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
   16271              :               {
   16272            0 :                 r = spec;
   16273            0 :                 break;
   16274              :               }
   16275              :           }
   16276    187995915 :         if (type == error_mark_node && !(complain & tf_error))
   16277     35110253 :           RETURN (error_mark_node);
   16278    152885662 :         r = copy_decl (t);
   16279    152885662 :         if (VAR_P (r))
   16280              :           {
   16281     36061632 :             DECL_INITIALIZED_P (r) = 0;
   16282     36061632 :             DECL_TEMPLATE_INSTANTIATED (r) = 0;
   16283     36061632 :             if (DECL_DECOMPOSITION_P (t) && DECL_PACK_P (t))
   16284              :               {
   16285         3360 :                 tree dtype = cxx_make_type (DECLTYPE_TYPE);
   16286         3360 :                 DECLTYPE_TYPE_EXPR (dtype) = r;
   16287         3360 :                 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (dtype) = 1;
   16288         3360 :                 SET_TYPE_STRUCTURAL_EQUALITY (dtype);
   16289         3360 :                 type = cxx_make_type (TYPE_PACK_EXPANSION);
   16290         3360 :                 PACK_EXPANSION_PATTERN (type) = dtype;
   16291         3360 :                 SET_TYPE_STRUCTURAL_EQUALITY (type);
   16292         6720 :                 PACK_EXPANSION_PARAMETER_PACKS (type) = r;
   16293              :               }
   16294     36061632 :             if (DECL_HAS_VALUE_EXPR_P (t))
   16295              :               {
   16296       112525 :                 tree ve = DECL_VALUE_EXPR (t);
   16297              :                 /* If the DECL_VALUE_EXPR is converted to the declared type,
   16298              :                    preserve the identity so that gimplify_type_sizes works.  */
   16299       112525 :                 bool nop = (type && TREE_CODE (ve) == NOP_EXPR);
   16300            3 :                 if (nop)
   16301            3 :                   ve = TREE_OPERAND (ve, 0);
   16302       112525 :                 ve = tsubst_expr (ve, args, complain, in_decl);
   16303       112525 :                 gcc_assert (ve != error_mark_node);
   16304       112525 :                 if (REFERENCE_REF_P (ve))
   16305              :                   {
   16306        71377 :                     gcc_assert (!type || TYPE_REF_P (type));
   16307        71377 :                     ve = TREE_OPERAND (ve, 0);
   16308              :                   }
   16309       112525 :                 if (nop)
   16310            3 :                   ve = build_nop (type, ve);
   16311       112522 :                 else if (!type && is_capture_proxy (t))
   16312          177 :                   type = lambda_proxy_type (ve);
   16313       112345 :                 else if (DECL_LANG_SPECIFIC (t)
   16314       112170 :                          && DECL_OMP_PRIVATIZED_MEMBER (t)
   16315          183 :                          && TREE_CODE (ve) == COMPONENT_REF
   16316          183 :                          && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
   16317       112528 :                          && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
   16318            6 :                   type = TREE_TYPE (ve);
   16319              :                 else
   16320       112339 :                   gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
   16321              :                                        == TYPE_MAIN_VARIANT (type));
   16322       112525 :                 SET_DECL_VALUE_EXPR (r, ve);
   16323              :               }
   16324     36061632 :             if (TREE_CODE (type) == FUNCTION_TYPE)
   16325              :               {
   16326              :                 /* It may seem that this case cannot occur, since:
   16327              : 
   16328              :                    typedef void f();
   16329              :                    void g() { f x; }
   16330              : 
   16331              :                    declares a function, not a variable.  However:
   16332              : 
   16333              :                    typedef void f();
   16334              :                    template <typename T> void g() { T t; }
   16335              :                    template void g<f>();
   16336              : 
   16337              :                    is an attempt to declare a variable with function
   16338              :                    type.  */
   16339            3 :                 error ("variable %qD has function type",
   16340              :                        /* R is not yet sufficiently initialized, so we
   16341              :                           just use its name.  */
   16342            3 :                        DECL_NAME (r));
   16343            3 :                 RETURN (error_mark_node);
   16344              :               }
   16345     36061629 :             type = complete_type (type);
   16346              :             /* Wait until cp_finish_decl to set this again, to handle
   16347              :                circular dependency (template/instantiate6.C). */
   16348     36061626 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
   16349     36061626 :             type = check_var_type (DECL_NAME (r), type,
   16350     36061626 :                                    DECL_SOURCE_LOCATION (r));
   16351              :           }
   16352    116824030 :         else if (DECL_SELF_REFERENCE_P (t))
   16353     39610756 :           SET_DECL_SELF_REFERENCE_P (r);
   16354    152885656 :         TREE_TYPE (r) = type;
   16355    152885656 :         cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   16356    152885656 :         DECL_CONTEXT (r) = ctx;
   16357              :         /* Clear out the mangled name and RTL for the instantiation.  */
   16358    152885656 :         SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   16359    152885656 :         if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
   16360    152885656 :           SET_DECL_RTL (r, NULL);
   16361    152885656 :         set_instantiating_module (r);
   16362              : 
   16363              :         /* The initializer must not be expanded until it is required;
   16364              :            see [temp.inst].  */
   16365    152885656 :         DECL_INITIAL (r) = NULL_TREE;
   16366    152885656 :         DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
   16367    152885656 :         if (VAR_P (r))
   16368              :           {
   16369     36061626 :             if (DECL_LANG_SPECIFIC (r))
   16370     17134268 :               SET_DECL_DEPENDENT_INIT_P (r, false);
   16371              : 
   16372     36061626 :             SET_DECL_MODE (r, VOIDmode);
   16373              : 
   16374              :             /* Possibly limit visibility based on template args.  */
   16375     36061626 :             DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   16376     36061626 :             if (DECL_VISIBILITY_SPECIFIED (t))
   16377              :               {
   16378            0 :                 DECL_VISIBILITY_SPECIFIED (r) = 0;
   16379            0 :                 DECL_ATTRIBUTES (r)
   16380            0 :                   = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   16381              :               }
   16382     36061626 :             determine_visibility (r);
   16383     19756205 :             if ((!local_p || TREE_STATIC (t))
   16384     16404562 :                 && !(flag_openmp && DECL_LANG_SPECIFIC (t)
   16385        29955 :                      && DECL_OMP_DECLARE_MAPPER_P (t))
   16386     52466185 :                 && DECL_SECTION_NAME (t))
   16387            9 :               set_decl_section_name (r, t);
   16388              :           }
   16389              : 
   16390    152885656 :         if (!local_p)
   16391              :           {
   16392              :             /* A static data member declaration is always marked
   16393              :                external when it is declared in-class, even if an
   16394              :                initializer is present.  We mimic the non-template
   16395              :                processing here.  */
   16396    131101938 :             DECL_EXTERNAL (r) = 1;
   16397    131101938 :             if (DECL_NAMESPACE_SCOPE_P (t))
   16398     39823059 :               DECL_NOT_REALLY_EXTERN (r) = 1;
   16399              : 
   16400    131101938 :             DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
   16401    131101938 :             SET_DECL_IMPLICIT_INSTANTIATION (r);
   16402    131101938 :             if (use_spec_table)
   16403     84973333 :               register_specialization (r, gen_tmpl, argvec, false, hash);
   16404              :           }
   16405              :         else
   16406              :           {
   16407     21783718 :             if (DECL_LANG_SPECIFIC (r))
   16408       832045 :               DECL_TEMPLATE_INFO (r) = NULL_TREE;
   16409     21783718 :             if (!cp_unevaluated_operand)
   16410     21783703 :               register_local_specialization (r, t);
   16411              :           }
   16412              : 
   16413    152885656 :         if (VAR_P (r)
   16414     36061626 :             && CP_DECL_THREAD_LOCAL_P (r)
   16415    152885785 :             && !processing_template_decl)
   16416          117 :           set_decl_tls_model (r, decl_default_tls_model (r));
   16417              : 
   16418    152885656 :         DECL_CHAIN (r) = NULL_TREE;
   16419              : 
   16420    152885656 :         if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
   16421              :                                              /*flags=*/0,
   16422              :                                              args, complain, in_decl))
   16423            3 :           return error_mark_node;
   16424              : 
   16425              :         /* Preserve a typedef that names a type.  */
   16426    269709542 :         if (is_typedef_decl (r) && type != error_mark_node)
   16427              :           {
   16428    116823527 :             DECL_ORIGINAL_TYPE (r) = NULL_TREE;
   16429    116823527 :             set_underlying_type (r);
   16430              : 
   16431              :             /* common_handle_aligned_attribute doesn't apply the alignment
   16432              :                to DECL_ORIGINAL_TYPE.  */
   16433    116823527 :             if (TYPE_USER_ALIGN (TREE_TYPE (t)))
   16434          259 :               TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
   16435          259 :                                                   TYPE_ALIGN (TREE_TYPE (t)));
   16436              : 
   16437              :             /* Preserve structural-ness of a partially instantiated typedef.  */
   16438    116823527 :             if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t))
   16439    116823527 :                 && dependent_type_p (TREE_TYPE (r)))
   16440     15105686 :               SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (r));
   16441              :           }
   16442              : 
   16443    152885653 :         if (flag_openmp
   16444       295220 :             && VAR_P (t)
   16445        73001 :             && DECL_LANG_SPECIFIC (t)
   16446        32430 :             && DECL_OMP_DECLARE_MAPPER_P (t)
   16447    152885657 :             && strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   16448            4 :           DECL_NAME (r) = omp_mapper_id (DECL_NAME (t), TREE_TYPE (r));
   16449              : 
   16450    152885653 :         layout_decl (r, 0);
   16451              :       }
   16452    152885653 :       break;
   16453              : 
   16454    102349691 :     case NAMESPACE_DECL:
   16455    102349691 :       if (dependent_namespace_p (t))
   16456            1 :         r = tsubst_expr (ORIGINAL_NAMESPACE (t), args, complain, in_decl);
   16457              :       else
   16458    102349690 :         r = t;
   16459              :       break;
   16460              : 
   16461            0 :     default:
   16462            0 :       gcc_unreachable ();
   16463              :     }
   16464              : #undef RETURN
   16465              : 
   16466    803754410 :  out:
   16467              :   /* Restore the file and line information.  */
   16468    803754410 :   input_location = saved_loc;
   16469              : 
   16470    803754410 :   return r;
   16471              : }
   16472              : 
   16473              : /* Substitute into the complete parameter type list PARMS.  */
   16474              : 
   16475              : tree
   16476      5258224 : tsubst_function_parms (tree parms,
   16477              :                        tree args,
   16478              :                        tsubst_flags_t complain,
   16479              :                        tree in_decl)
   16480              : {
   16481      5258224 :   return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
   16482              : }
   16483              : 
   16484              : /* Substitute into the ARG_TYPES of a function type.
   16485              :    If END is a TREE_CHAIN, leave it and any following types
   16486              :    un-substituted.  */
   16487              : 
   16488              : static tree
   16489    470397627 : tsubst_arg_types (tree arg_types,
   16490              :                   tree args,
   16491              :                   tree end,
   16492              :                   tsubst_flags_t complain,
   16493              :                   tree in_decl)
   16494              : {
   16495    470397627 :   tree type = NULL_TREE;
   16496    470397627 :   int len = 1;
   16497    470397627 :   tree expanded_args = NULL_TREE;
   16498              : 
   16499    470397627 :   if (!arg_types || arg_types == void_list_node || arg_types == end)
   16500              :     return arg_types;
   16501              : 
   16502    301561969 :   if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
   16503              :     {
   16504              :       /* For a pack expansion, perform substitution on the
   16505              :          entire expression. Later on, we'll handle the arguments
   16506              :          one-by-one.  */
   16507      6122015 :       expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
   16508              :                                             args, complain, in_decl);
   16509              : 
   16510      6122015 :       if (TREE_CODE (expanded_args) == TREE_VEC)
   16511              :         /* So that we'll spin through the parameters, one by one.  */
   16512      3611339 :         len = TREE_VEC_LENGTH (expanded_args);
   16513              :       else
   16514              :         {
   16515              :           /* We only partially substituted into the parameter
   16516              :              pack. Our type is TYPE_PACK_EXPANSION.  */
   16517              :           type = expanded_args;
   16518              :           expanded_args = NULL_TREE;
   16519              :         }
   16520              :     }
   16521              :   else
   16522    295439954 :     type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
   16523              : 
   16524              :   /* Check if a substituted type is erroneous before substituting into
   16525              :      the rest of the chain.  */
   16526    601672309 :   for (int i = 0; i < len; i++)
   16527              :     {
   16528    300116512 :       if (expanded_args)
   16529      2165882 :         type = TREE_VEC_ELT (expanded_args, i);
   16530              : 
   16531    300116512 :       if (type == error_mark_node)
   16532              :         return error_mark_node;
   16533    300110372 :       if (VOID_TYPE_P (type))
   16534              :         {
   16535           32 :           if (complain & tf_error)
   16536              :             {
   16537            6 :               error ("invalid parameter type %qT", type);
   16538            6 :               if (in_decl)
   16539            3 :                 error ("in declaration %q+D", in_decl);
   16540              :             }
   16541           32 :           return error_mark_node;
   16542              :         }
   16543              :     }
   16544              : 
   16545              :   /* We do not substitute into default arguments here.  The standard
   16546              :      mandates that they be instantiated only when needed, which is
   16547              :      done in build_over_call.  */
   16548    301555797 :   tree default_arg = TREE_PURPOSE (arg_types);
   16549              : 
   16550              :   /* Except that we do substitute default arguments under tsubst_lambda_expr,
   16551              :      since the new op() won't have any associated template arguments for us
   16552              :      to refer to later.  */
   16553    301555797 :   if (lambda_fn_in_template_p (in_decl)
   16554    301555797 :       || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
   16555    276119460 :           && DECL_LOCAL_DECL_P (in_decl)))
   16556       143250 :     default_arg = tsubst_expr (default_arg, args, complain, in_decl);
   16557              : 
   16558    301555797 :   tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
   16559    301555797 :                                                args, end, complain, in_decl);
   16560    301555797 :   if (remaining_arg_types == error_mark_node)
   16561              :     return error_mark_node;
   16562              : 
   16563    601649856 :   for (int i = len-1; i >= 0; i--)
   16564              :     {
   16565    300102201 :       if (expanded_args)
   16566      2165879 :         type = TREE_VEC_ELT (expanded_args, i);
   16567              : 
   16568              :       /* Do array-to-pointer, function-to-pointer conversion, and ignore
   16569              :          top-level qualifiers as required.  */
   16570    300102201 :       type = cv_unqualified (type_decays_to (type));
   16571              : 
   16572    300102201 :       if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
   16573              :         {
   16574              :           /* We've instantiated a template before its default arguments
   16575              :              have been parsed.  This can happen for a nested template
   16576              :              class, and is not an error unless we require the default
   16577              :              argument in a call of this function.  */
   16578            3 :           remaining_arg_types
   16579            3 :             = tree_cons (default_arg, type, remaining_arg_types);
   16580            3 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
   16581              :                          remaining_arg_types);
   16582              :         }
   16583              :       else
   16584    300102198 :         remaining_arg_types
   16585    300102198 :           = hash_tree_cons (default_arg, type, remaining_arg_types);
   16586              :     }
   16587              : 
   16588    301547655 :   return remaining_arg_types;
   16589              : }
   16590              : 
   16591              : /* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
   16592              :    *not* handle the exception-specification for FNTYPE, because the
   16593              :    initial substitution of explicitly provided template parameters
   16594              :    during argument deduction forbids substitution into the
   16595              :    exception-specification:
   16596              : 
   16597              :      [temp.deduct]
   16598              : 
   16599              :      All references in the function type of the function template to  the
   16600              :      corresponding template parameters are replaced by the specified tem-
   16601              :      plate argument values.  If a substitution in a template parameter or
   16602              :      in  the function type of the function template results in an invalid
   16603              :      type, type deduction fails.  [Note: The equivalent  substitution  in
   16604              :      exception specifications is done only when the function is instanti-
   16605              :      ated, at which point a program is  ill-formed  if  the  substitution
   16606              :      results in an invalid type.]  */
   16607              : 
   16608              : static tree
   16609    162549187 : tsubst_function_type (tree t,
   16610              :                       tree args,
   16611              :                       tsubst_flags_t complain,
   16612              :                       tree in_decl)
   16613              : {
   16614    162549187 :   tree return_type;
   16615    162549187 :   tree arg_types = NULL_TREE;
   16616              : 
   16617              :   /* The TYPE_CONTEXT is not used for function/method types.  */
   16618    162549187 :   gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
   16619              : 
   16620              :   /* DR 1227: Mixing immediate and non-immediate contexts in deduction
   16621              :      failure.  */
   16622    162549187 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   16623              : 
   16624    162549187 :   if (late_return_type_p)
   16625              :     {
   16626              :       /* Substitute the argument types.  */
   16627     14804650 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   16628              :                                     complain, in_decl);
   16629     14804650 :       if (arg_types == error_mark_node)
   16630              :         return error_mark_node;
   16631              : 
   16632     14804626 :       tree save_ccp = current_class_ptr;
   16633     14804626 :       tree save_ccr = current_class_ref;
   16634     14804626 :       tree this_type = (TREE_CODE (t) == METHOD_TYPE
   16635     16465207 :                         ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
   16636      1660581 :       bool do_inject = this_type && CLASS_TYPE_P (this_type);
   16637      1660581 :       if (do_inject)
   16638              :         {
   16639              :           /* DR 1207: 'this' is in scope in the trailing return type.  */
   16640      1660581 :           inject_this_parameter (this_type, cp_type_quals (this_type));
   16641              :         }
   16642              : 
   16643              :       /* Substitute the return type.  */
   16644     14804626 :       return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16645              : 
   16646     14801926 :       if (do_inject)
   16647              :         {
   16648      1660581 :           current_class_ptr = save_ccp;
   16649      1660581 :           current_class_ref = save_ccr;
   16650              :         }
   16651              :     }
   16652              :   else
   16653              :     /* Substitute the return type.  */
   16654    147744537 :     return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16655              : 
   16656    162538330 :   if (return_type == error_mark_node)
   16657              :     return error_mark_node;
   16658              :   /* DR 486 clarifies that creation of a function type with an
   16659              :      invalid return type is a deduction failure.  */
   16660    160979040 :   if (TREE_CODE (return_type) == ARRAY_TYPE
   16661    160978935 :       || TREE_CODE (return_type) == FUNCTION_TYPE)
   16662              :     {
   16663          221 :       if (complain & tf_error)
   16664              :         {
   16665           26 :           if (TREE_CODE (return_type) == ARRAY_TYPE)
   16666           12 :             error ("function returning an array");
   16667              :           else
   16668           14 :             error ("function returning a function");
   16669              :         }
   16670          221 :       return error_mark_node;
   16671              :     }
   16672              : 
   16673    160978819 :   if (!late_return_type_p)
   16674              :     {
   16675              :       /* Substitute the argument types.  */
   16676    147048005 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   16677              :                                     complain, in_decl);
   16678    147048005 :       if (arg_types == error_mark_node)
   16679              :         return error_mark_node;
   16680              :     }
   16681              : 
   16682              :   /* Construct a new type node and return it.  */
   16683    160972743 :   return rebuild_function_or_method_type (t, args, return_type, arg_types,
   16684    160972743 :                                           /*raises=*/NULL_TREE, complain);
   16685              : }
   16686              : 
   16687              : /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
   16688              :    ARGS into that specification, and return the substituted
   16689              :    specification.  If there is no specification, return NULL_TREE.  */
   16690              : 
   16691              : static tree
   16692    160972720 : tsubst_exception_specification (tree fntype,
   16693              :                                 tree args,
   16694              :                                 tsubst_flags_t complain,
   16695              :                                 tree in_decl,
   16696              :                                 bool defer_ok)
   16697              : {
   16698    160972720 :   tree specs;
   16699    160972720 :   tree new_specs;
   16700              : 
   16701    160972720 :   specs = TYPE_RAISES_EXCEPTIONS (fntype);
   16702    160972720 :   new_specs = NULL_TREE;
   16703    219193823 :   if (specs && TREE_PURPOSE (specs))
   16704              :     {
   16705              :       /* A noexcept-specifier.  */
   16706     58185776 :       tree expr = TREE_PURPOSE (specs);
   16707     58185776 :       if (TREE_CODE (expr) == INTEGER_CST)
   16708     51318345 :         new_specs = expr;
   16709      6867431 :       else if (defer_ok)
   16710              :         {
   16711              :           /* Defer instantiation of noexcept-specifiers to avoid
   16712              :              excessive instantiations (c++/49107).  */
   16713      6825741 :           new_specs = make_node (DEFERRED_NOEXCEPT);
   16714      6825741 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   16715              :             {
   16716              :               /* We already partially instantiated this member template,
   16717              :                  so combine the new args with the old.  */
   16718           27 :               DEFERRED_NOEXCEPT_PATTERN (new_specs)
   16719           27 :                 = DEFERRED_NOEXCEPT_PATTERN (expr);
   16720           54 :               DEFERRED_NOEXCEPT_ARGS (new_specs)
   16721           54 :                 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
   16722              :             }
   16723              :           else
   16724              :             {
   16725      6825714 :               DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
   16726      6825714 :               DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
   16727              :             }
   16728              :         }
   16729              :       else
   16730              :         {
   16731        41690 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   16732              :             {
   16733            0 :               args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
   16734              :                                            args);
   16735            0 :               expr = DEFERRED_NOEXCEPT_PATTERN (expr);
   16736              :             }
   16737        41690 :           cp_unevaluated u;
   16738        41690 :           new_specs = tsubst_expr (expr, args, complain, in_decl);
   16739        41690 :         }
   16740     58185776 :       new_specs = build_noexcept_spec (new_specs, complain);
   16741              :       /* We've instantiated a template before a noexcept-specifier
   16742              :          contained therein has been parsed.  This can happen for
   16743              :          a nested template class:
   16744              : 
   16745              :           struct S {
   16746              :             template<typename> struct B { B() noexcept(...); };
   16747              :             struct A : B<int> { ... use B() ... };
   16748              :           };
   16749              : 
   16750              :          where completing B<int> will trigger instantiating the
   16751              :          noexcept, even though we only parse it at the end of S.  */
   16752     58185776 :       if (UNPARSED_NOEXCEPT_SPEC_P (specs))
   16753              :         {
   16754           15 :           gcc_checking_assert (defer_ok);
   16755           15 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
   16756              :         }
   16757              :     }
   16758    102786944 :   else if (specs)
   16759              :     {
   16760        35327 :       if (! TREE_VALUE (specs))
   16761        35266 :         new_specs = specs;
   16762              :       else
   16763          118 :         while (specs)
   16764              :           {
   16765           61 :             tree spec;
   16766           61 :             int i, len = 1;
   16767           61 :             tree expanded_specs = NULL_TREE;
   16768              : 
   16769           61 :             if (PACK_EXPANSION_P (TREE_VALUE (specs)))
   16770              :               {
   16771              :                 /* Expand the pack expansion type.  */
   16772           13 :                 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
   16773              :                                                        args, complain,
   16774              :                                                        in_decl);
   16775              : 
   16776           13 :                 if (expanded_specs == error_mark_node)
   16777              :                   return error_mark_node;
   16778           11 :                 else if (TREE_CODE (expanded_specs) == TREE_VEC)
   16779            4 :                   len = TREE_VEC_LENGTH (expanded_specs);
   16780              :                 else
   16781              :                   {
   16782              :                     /* We're substituting into a member template, so
   16783              :                        we got a TYPE_PACK_EXPANSION back.  Add that
   16784              :                        expansion and move on.  */
   16785            7 :                     gcc_assert (TREE_CODE (expanded_specs)
   16786              :                                 == TYPE_PACK_EXPANSION);
   16787            7 :                     new_specs = add_exception_specifier (new_specs,
   16788              :                                                          expanded_specs,
   16789              :                                                          complain);
   16790            7 :                     specs = TREE_CHAIN (specs);
   16791            7 :                     continue;
   16792              :                   }
   16793              :               }
   16794              : 
   16795          110 :             for (i = 0; i < len; ++i)
   16796              :               {
   16797           60 :                 if (expanded_specs)
   16798           12 :                   spec = TREE_VEC_ELT (expanded_specs, i);
   16799              :                 else
   16800           48 :                   spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
   16801           60 :                 if (spec == error_mark_node)
   16802              :                   return spec;
   16803           58 :                 new_specs = add_exception_specifier (new_specs, spec,
   16804              :                                                      complain);
   16805              :               }
   16806              : 
   16807           50 :             specs = TREE_CHAIN (specs);
   16808              :           }
   16809              :     }
   16810    160972716 :   return new_specs;
   16811              : }
   16812              : 
   16813              : /* Substitute through a TREE_LIST of types or expressions, handling pack
   16814              :    expansions.  */
   16815              : 
   16816              : tree
   16817     32863490 : tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16818              : {
   16819     32863490 :   if (t == void_list_node)
   16820              :     return t;
   16821              : 
   16822     32863472 :   tree purpose = TREE_PURPOSE (t);
   16823     32863472 :   tree purposevec = NULL_TREE;
   16824     32863472 :   if (!purpose)
   16825              :     ;
   16826            2 :   else if (PACK_EXPANSION_P (purpose))
   16827              :     {
   16828            0 :       purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
   16829            0 :       if (TREE_CODE (purpose) == TREE_VEC)
   16830            0 :         purposevec = purpose;
   16831              :     }
   16832            2 :   else if (TYPE_P (purpose))
   16833            0 :     purpose = tsubst (purpose, args, complain, in_decl);
   16834              :   else
   16835            2 :     purpose = tsubst_expr (purpose, args, complain, in_decl);
   16836     32863472 :   if (purpose == error_mark_node || purposevec == error_mark_node)
   16837              :     return error_mark_node;
   16838              : 
   16839     32863472 :   tree value = TREE_VALUE (t);
   16840     32863472 :   tree valuevec = NULL_TREE;
   16841     32863472 :   if (!value)
   16842              :     ;
   16843     32863472 :   else if (PACK_EXPANSION_P (value))
   16844              :     {
   16845       661595 :       value = tsubst_pack_expansion (value, args, complain, in_decl);
   16846       661595 :       if (TREE_CODE (value) == TREE_VEC)
   16847       657916 :         valuevec = value;
   16848              :     }
   16849     32201877 :   else if (TYPE_P (value))
   16850           24 :     value = tsubst (value, args, complain, in_decl);
   16851              :   else
   16852     32201853 :     value = tsubst_expr (value, args, complain, in_decl);
   16853     32863472 :   if (value == error_mark_node || valuevec == error_mark_node)
   16854              :     return error_mark_node;
   16855              : 
   16856     32835549 :   tree chain = TREE_CHAIN (t);
   16857     32835549 :   if (!chain)
   16858              :     ;
   16859       688732 :   else if (TREE_CODE (chain) == TREE_LIST)
   16860       688732 :     chain = tsubst_tree_list (chain, args, complain, in_decl);
   16861            0 :   else if (TYPE_P (chain))
   16862            0 :     chain = tsubst (chain, args, complain, in_decl);
   16863              :   else
   16864            0 :     chain = tsubst_expr (chain, args, complain, in_decl);
   16865     32835549 :   if (chain == error_mark_node)
   16866              :     return error_mark_node;
   16867              : 
   16868     32835549 :   if (purpose == TREE_PURPOSE (t)
   16869     32835547 :       && value == TREE_VALUE (t)
   16870     33452345 :       && chain == TREE_CHAIN (t))
   16871              :     return t;
   16872              : 
   16873     32218969 :   int len;
   16874              :   /* Determine the number of arguments.  */
   16875     32218969 :   if (purposevec)
   16876              :     {
   16877            0 :       len = TREE_VEC_LENGTH (purposevec);
   16878            0 :       gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
   16879              :     }
   16880     32218969 :   else if (valuevec)
   16881       657916 :     len = TREE_VEC_LENGTH (valuevec);
   16882              :   else
   16883              :     len = 1;
   16884              : 
   16885     64274742 :   for (int i = len; i-- > 0; )
   16886              :     {
   16887     32055773 :       if (purposevec)
   16888            0 :         purpose = TREE_VEC_ELT (purposevec, i);
   16889     32055773 :       if (valuevec)
   16890       494720 :         value = TREE_VEC_ELT (valuevec, i);
   16891              : 
   16892     32055773 :       if (value && TYPE_P (value))
   16893           26 :         chain = hash_tree_cons (purpose, value, chain);
   16894              :       else
   16895     32055747 :         chain = tree_cons (purpose, value, chain);
   16896              :     }
   16897              : 
   16898              :   return chain;
   16899              : }
   16900              : 
   16901              : /* Substitute ARGS into T, which is a splice scope.  */
   16902              : 
   16903              : static tree
   16904          225 : tsubst_splice_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16905              : {
   16906          225 :   tree r = tsubst (SPLICE_SCOPE_EXPR (t), args, complain, in_decl);
   16907          225 :   if (r == error_mark_node)
   16908              :     return r;
   16909          210 :   const bool type_p = SPLICE_SCOPE_TYPE_P (t);
   16910          210 :   if (dependent_splice_p (r))
   16911            5 :     r = make_splice_scope (r, type_p);
   16912          205 :   else if (type_p && ctad_template_p (r))
   16913            5 :     r = make_template_placeholder (r);
   16914          210 :   if (type_p
   16915          210 :       ? !valid_splice_type_p (r)
   16916           84 :       : !valid_splice_scope_p (r))
   16917              :     {
   16918           23 :       if (complain & tf_error)
   16919              :         {
   16920           23 :           const location_t loc = EXPR_LOCATION (SPLICE_SCOPE_EXPR (t));
   16921           23 :           auto_diagnostic_group d;
   16922           23 :           if (type_p)
   16923           12 :             error_at (loc, "expected a reflection of a type");
   16924              :           else
   16925           11 :             error_at (loc, "expected a reflection of a class, namespace, or "
   16926              :                       "enumeration");
   16927           23 :           inform_tree_category (r);
   16928           23 :         }
   16929           23 :       return error_mark_node;
   16930              :     }
   16931              : 
   16932          187 :   if (type_p)
   16933          114 :     r = cp_build_qualified_type (r, cp_type_quals (t) | cp_type_quals (r),
   16934              :                                  complain | tf_ignore_bad_quals);
   16935              : 
   16936              :   return r;
   16937              : }
   16938              : 
   16939              : /* Substitute ARGS into T, which is a splice expression.  */
   16940              : 
   16941              : static tree
   16942          787 : tsubst_splice_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16943              : {
   16944          787 :   tree op = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl);
   16945          787 :   if (op == error_mark_node)
   16946              :     return error_mark_node;
   16947          773 :   op = splice (op);
   16948          773 :   if (op == error_mark_node)
   16949              :     return error_mark_node;
   16950          771 :   if (dependent_splice_p (op))
   16951              :     {
   16952            9 :       if (SPLICE_EXPR_EXPRESSION_P (t))
   16953            8 :         SET_SPLICE_EXPR_EXPRESSION_P (op);
   16954            9 :       if (SPLICE_EXPR_MEMBER_ACCESS_P (t))
   16955            6 :         SET_SPLICE_EXPR_MEMBER_ACCESS_P (op, true);
   16956            9 :       if (SPLICE_EXPR_ADDRESS_P (t))
   16957            0 :         SET_SPLICE_EXPR_ADDRESS_P (op, true);
   16958            9 :       if (SPLICE_EXPR_TEMPLATE_P (t))
   16959            0 :         SET_SPLICE_EXPR_TEMPLATE_P (op, true);
   16960            9 :       if (SPLICE_EXPR_TARGS_P (t))
   16961            0 :         SET_SPLICE_EXPR_TARGS_P (op, true);
   16962            9 :       return op;
   16963              :     }
   16964          762 :   if (SPLICE_EXPR_EXPRESSION_P (t)
   16965          762 :       && !check_splice_expr (input_location, UNKNOWN_LOCATION, op,
   16966          548 :                              SPLICE_EXPR_ADDRESS_P (t),
   16967          548 :                              SPLICE_EXPR_MEMBER_ACCESS_P (t),
   16968          548 :                              SPLICE_EXPR_TEMPLATE_P (t),
   16969          548 :                              SPLICE_EXPR_TARGS_P (t),
   16970              :                              (complain & tf_error)))
   16971              :     return error_mark_node;
   16972              : 
   16973          737 :   if (SPLICE_EXPR_ADDRESS_P (t))
   16974              :     {
   16975           20 :       push_deferring_access_checks (dk_no_check);
   16976           20 :       if (BASELINK_P (op))
   16977            4 :         op = build_offset_ref (BINFO_TYPE (BASELINK_ACCESS_BINFO (op)), op,
   16978              :                                /*address_p=*/true, complain);
   16979           16 :       else if (DECL_NONSTATIC_MEMBER_P (op))
   16980            8 :         op = build_offset_ref (DECL_CONTEXT (op), op,
   16981              :                                /*address_p=*/true, complain);
   16982           20 :       pop_deferring_access_checks ();
   16983              :     }
   16984              : 
   16985          737 :   if (outer_automatic_var_p (op))
   16986            1 :     op = process_outer_var_ref (op, complain);
   16987              :   /* Like in cp_parser_splice_expression, for foo.[: bar :]
   16988              :      cp_parser_postfix_dot_deref_expression wants to see only
   16989              :      certain kind of entities.  */
   16990          737 :   if (SPLICE_EXPR_MEMBER_ACCESS_P (t))
   16991          426 :     gcc_assert (valid_splice_for_member_access_p (op, /*decls_only_p=*/false));
   16992              : 
   16993              :   return op;
   16994              : }
   16995              : 
   16996              : /* Return true iff we're in an expansion statement.  */
   16997              : 
   16998              : static bool
   16999     39429075 : in_expansion_stmt_p ()
   17000              : {
   17001     39429075 :   if (in_expansion_stmt)
   17002              :     return true;
   17003              : 
   17004              :   /* In instantiations in_expansion_stmt is false.  */
   17005     39429075 :   for (cp_binding_level *b = current_binding_level;
   17006    111118359 :        b && b->kind != sk_function_parms;
   17007     71689284 :        b = b->level_chain)
   17008     71689474 :     if (b->kind == sk_template_for)
   17009              :       return true;
   17010              :   return false;
   17011              : }
   17012              : 
   17013              : /* Take the tree structure T and replace template parameters used
   17014              :    therein with the argument vector ARGS.  IN_DECL is an associated
   17015              :    decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
   17016              :    Issue error and warning messages under control of COMPLAIN.  Note
   17017              :    that we must be relatively non-tolerant of extensions here, in
   17018              :    order to preserve conformance; if we allow substitutions that
   17019              :    should not be allowed, we may allow argument deductions that should
   17020              :    not succeed, and therefore report ambiguous overload situations
   17021              :    where there are none.  In theory, we could allow the substitution,
   17022              :    but indicate that it should have failed, and allow our caller to
   17023              :    make sure that the right thing happens, but we don't try to do this
   17024              :    yet.
   17025              : 
   17026              :    This function is used for dealing with types, decls and the like;
   17027              :    for expressions, use tsubst_expr or tsubst_copy.  */
   17028              : 
   17029              : tree
   17030   7052971967 : tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17031              : {
   17032   7052971967 :   enum tree_code code;
   17033   7052971967 :   tree type, r = NULL_TREE;
   17034              : 
   17035   7052971967 :   if (t == NULL_TREE || t == error_mark_node
   17036   7006116565 :       || t == integer_type_node
   17037   6923214130 :       || t == void_type_node
   17038   6840570790 :       || t == char_type_node
   17039   6827574716 :       || t == unknown_type_node
   17040   6827574711 :       || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
   17041              :     return t;
   17042              : 
   17043              :   /* Any instantiation of a template containing a TU-local entity is an
   17044              :      exposure, so always issue a diagnostic irrespective of complain.  */
   17045   6827388061 :   if (instantiating_tu_local_entity (t))
   17046           28 :     return error_mark_node;
   17047              : 
   17048   6827388033 :   tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
   17049   6827388033 :   complain &= ~tf_tst_ok;
   17050              : 
   17051   6827388033 :   tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
   17052   6827388033 :   complain &= ~tf_qualifying_scope;
   17053              : 
   17054   6827388033 :   if (DECL_P (t))
   17055    666330316 :     return tsubst_decl (t, args, complain);
   17056              : 
   17057   6161057717 :   if (args == NULL_TREE)
   17058              :     return t;
   17059              : 
   17060   6098981457 :   code = TREE_CODE (t);
   17061              : 
   17062   6098981457 :   gcc_assert (code != IDENTIFIER_NODE);
   17063   6098981457 :   type = TREE_TYPE (t);
   17064              : 
   17065   6098981457 :   gcc_assert (type != unknown_type_node);
   17066              : 
   17067   6098981457 :   if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   17068              :     return d;
   17069              : 
   17070              :   /* Reuse typedefs.  We need to do this to handle dependent attributes,
   17071              :      such as attribute aligned.  */
   17072   6098965894 :   if (TYPE_P (t)
   17073   6098965894 :       && typedef_variant_p (t))
   17074              :     {
   17075    311022474 :       tree decl = TYPE_NAME (t);
   17076              : 
   17077    311022474 :       if (alias_template_specialization_p (t, nt_opaque))
   17078              :         {
   17079              :           /* DECL represents an alias template and we want to
   17080              :              instantiate it.  */
   17081    107342879 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   17082    107342879 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   17083    107342879 :           r = instantiate_alias_template (tmpl, gen_args, complain);
   17084              :         }
   17085    407359190 :       else if (DECL_CLASS_SCOPE_P (decl)
   17086    167800383 :                && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
   17087    366075287 :                && uses_template_parms (DECL_CONTEXT (decl)))
   17088              :         {
   17089    162165201 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   17090    162165201 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   17091    162165201 :           r = retrieve_specialization (tmpl, gen_args, 0);
   17092              :         }
   17093     83028788 :       else if ((DECL_FUNCTION_SCOPE_P (decl)
   17094      2088881 :                 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
   17095      2088878 :                 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
   17096              :                /* The { } of an expansion-statement is considered a template
   17097              :                   definition.  */
   17098     80943469 :                || in_expansion_stmt_p ())
   17099      2085509 :         r = retrieve_local_specialization (decl);
   17100              :       else
   17101              :         /* The typedef is from a non-template context.  */
   17102              :         return t;
   17103              : 
   17104    271593586 :       if (r)
   17105              :         {
   17106    271592505 :           r = TREE_TYPE (r);
   17107    271592505 :           r = cp_build_qualified_type
   17108    271592505 :             (r, cp_type_quals (t) | cp_type_quals (r),
   17109              :              complain | tf_ignore_bad_quals);
   17110    271592505 :           return r;
   17111              :         }
   17112              :       else
   17113              :         {
   17114              :           /* We don't have an instantiation yet, so drop the typedef.  */
   17115         1081 :           int quals = cp_type_quals (t);
   17116         1081 :           t = DECL_ORIGINAL_TYPE (decl);
   17117         1081 :           t = cp_build_qualified_type (t, quals,
   17118              :                                        complain | tf_ignore_bad_quals);
   17119              :         }
   17120              :     }
   17121              : 
   17122   5787944501 :   bool fndecl_type = (complain & tf_fndecl_type);
   17123   5787944501 :   complain &= ~tf_fndecl_type;
   17124              : 
   17125   5787944501 :   if (type
   17126   5787944501 :       && code != TYPENAME_TYPE
   17127    992673741 :       && code != TEMPLATE_TYPE_PARM
   17128    992673741 :       && code != TEMPLATE_PARM_INDEX
   17129              :       && code != IDENTIFIER_NODE
   17130    796475263 :       && code != FUNCTION_TYPE
   17131    733944716 :       && code != METHOD_TYPE
   17132    733944716 :       && code != PACK_INDEX_TYPE)
   17133    633923382 :     type = tsubst (type, args, complain, in_decl);
   17134   5787944501 :   if (type == error_mark_node)
   17135              :     return error_mark_node;
   17136              : 
   17137   5787939119 :   switch (code)
   17138              :     {
   17139    987455225 :     case RECORD_TYPE:
   17140    987455225 :       if (TYPE_PTRMEMFUNC_P (t))
   17141        31620 :         return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
   17142              :       /* Fall through.  */
   17143   1013343333 :     case UNION_TYPE:
   17144   1013343333 :     case ENUMERAL_TYPE:
   17145   1013343333 :       if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
   17146              :         {
   17147   1801473524 :           if (LAMBDA_TYPE_P (t))
   17148              :             {
   17149              :               /* In reconstruct_lambda_capture_pack we need to be able to
   17150              :                  rebuild the lambda closure parm, which means looking up the
   17151              :                  closure type.  See the comment for that function about using
   17152              :                  current_class_type.  */
   17153           24 :               tree c = current_class_type;
   17154           48 :               if (LAMBDA_TYPE_P (c)
   17155           48 :                   && (DECL_SOURCE_LOCATION (TYPE_NAME (t))
   17156           24 :                       == DECL_SOURCE_LOCATION (TYPE_NAME (c))))
   17157              :                 return c;
   17158            0 :               gcc_unreachable ();
   17159              :             }
   17160              :           /* Figure out what arguments are appropriate for the
   17161              :              type we are trying to find.  For example, given:
   17162              : 
   17163              :                template <class T> struct S;
   17164              :                template <class T, class U> void f(T, U) { S<U> su; }
   17165              : 
   17166              :              and supposing that we are instantiating f<int, double>,
   17167              :              then our ARGS will be {int, double}, but, when looking up
   17168              :              S we only want {double}.  */
   17169   1803606480 :           tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
   17170              :                                               complain, in_decl);
   17171    901803204 :           if (argvec == error_mark_node)
   17172              :             return error_mark_node;
   17173              : 
   17174    901691176 :           tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
   17175              :                                           complain);
   17176    901691131 :           return cp_build_qualified_type (r, cp_type_quals (t), complain);
   17177              :         }
   17178              :       else
   17179              :         /* This is not a template type, so there's nothing to do.  */
   17180    111540069 :         return t;
   17181              : 
   17182              :     case ERROR_MARK:
   17183              :     case IDENTIFIER_NODE:
   17184              :     case VOID_TYPE:
   17185              :     case OPAQUE_TYPE:
   17186              :     case REAL_TYPE:
   17187              :     case COMPLEX_TYPE:
   17188              :     case VECTOR_TYPE:
   17189              :     case BOOLEAN_TYPE:
   17190              :     case NULLPTR_TYPE:
   17191              :     case META_TYPE:
   17192              :     case LANG_TYPE:
   17193              :       return t;
   17194              : 
   17195    108619439 :     case INTEGER_TYPE:
   17196    108619439 :       if (t == integer_type_node)
   17197              :         return t;
   17198              : 
   17199    108619427 :       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
   17200    108619427 :           && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
   17201              :         return t;
   17202              : 
   17203       570533 :       {
   17204       570533 :         tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
   17205              : 
   17206       570533 :         max = tsubst_expr (omax, args, complain, in_decl);
   17207              : 
   17208              :         /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
   17209              :            needed.  */
   17210       570533 :         if (TREE_CODE (max) == NOP_EXPR
   17211        33558 :             && TREE_SIDE_EFFECTS (omax)
   17212       570596 :             && !TREE_TYPE (max))
   17213            0 :           TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
   17214              : 
   17215              :         /* If we're in a partial instantiation, preserve the magic NOP_EXPR
   17216              :            with TREE_SIDE_EFFECTS that indicates this is not an integral
   17217              :            constant expression.  */
   17218       570533 :         if (processing_template_decl
   17219       570533 :             && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
   17220              :           {
   17221            0 :             gcc_assert (TREE_CODE (max) == NOP_EXPR);
   17222            0 :             TREE_SIDE_EFFECTS (max) = 1;
   17223              :           }
   17224              : 
   17225       570533 :         return compute_array_index_type (NULL_TREE, max, complain);
   17226              :       }
   17227              : 
   17228   2855766524 :     case TEMPLATE_TYPE_PARM:
   17229   2855766524 :       if (TEMPLATE_TYPE_LEVEL (t) == 0)
   17230              :         {
   17231              :           /* This is either an ordinary level-less auto or a CTAD placeholder
   17232              :              auto.  These get replaced only via do_auto_deduction which, in the
   17233              :              ordinary case, temporarily overrides its level to 1 before calling
   17234              :              tsubst.  CTAD placeholders are replaced via do_class_deduction.  */
   17235       213384 :           gcc_checking_assert (is_auto (t));
   17236       213384 :           tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
   17237       213384 :           if (!tmpl)
   17238              :             /* Ordinary level-less auto has nothing to substitute.  */
   17239              :             return t;
   17240              : 
   17241              :           /* Substitute the template of this CTAD placeholder.  */
   17242        91002 :           tmpl = tsubst_expr (tmpl, args, complain, in_decl);
   17243        91002 :           if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   17244         2283 :             tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
   17245              : 
   17246        91002 :           if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
   17247         2726 :             return make_template_placeholder (tmpl);
   17248              :           else
   17249              :             return t;
   17250              :         }
   17251              :       /* Fall through.  */
   17252   3054802523 :     case TEMPLATE_TEMPLATE_PARM:
   17253   3054802523 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   17254   3054802523 :     case TEMPLATE_PARM_INDEX:
   17255   3054802523 :       {
   17256   3054802523 :         int idx;
   17257   3054802523 :         int level;
   17258   3054802523 :         int levels;
   17259   3054802523 :         tree arg = NULL_TREE;
   17260              : 
   17261   3054802523 :         r = NULL_TREE;
   17262              : 
   17263   3054802523 :         gcc_assert (TREE_VEC_LENGTH (args) > 0);
   17264   3054802523 :         template_parm_level_and_index (t, &level, &idx);
   17265              : 
   17266   3054802523 :         levels = TMPL_ARGS_DEPTH (args);
   17267   3054802523 :         if (level <= levels
   17268   8811356027 :             && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
   17269              :           {
   17270   2878276709 :             arg = TMPL_ARG (args, level, idx);
   17271              : 
   17272              :             /* See through ARGUMENT_PACK_SELECT arguments. */
   17273   2878276709 :             if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   17274     10629311 :               arg = argument_pack_select_arg (arg);
   17275              :           }
   17276              : 
   17277   3054802523 :         if (arg == error_mark_node)
   17278              :           return error_mark_node;
   17279   3054802466 :         else if (arg != NULL_TREE)
   17280              :           {
   17281   2861323489 :             if (ARGUMENT_PACK_P (arg))
   17282              :               /* If ARG is an argument pack, we don't actually want to
   17283              :                  perform a substitution here, because substitutions
   17284              :                  for argument packs are only done
   17285              :                  element-by-element. We can get to this point when
   17286              :                  substituting the type of a non-type template
   17287              :                  parameter pack, when that type actually contains
   17288              :                  template parameter packs from an outer template, e.g.,
   17289              : 
   17290              :                  template<typename... Types> struct A {
   17291              :                    template<Types... Values> struct B { };
   17292              :                  };  */
   17293              :               return t;
   17294              : 
   17295   2861323181 :             if (code == TEMPLATE_TYPE_PARM)
   17296              :               {
   17297   2669374017 :                 int quals;
   17298              : 
   17299   2669374017 :                 gcc_assert (TYPE_P (arg));
   17300              : 
   17301   2669374017 :                 quals = cp_type_quals (arg) | cp_type_quals (t);
   17302              : 
   17303   2669374017 :                 return cp_build_qualified_type
   17304   2669374017 :                   (arg, quals, complain | tf_ignore_bad_quals);
   17305              :               }
   17306    191949164 :             else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
   17307              :               {
   17308              :                 /* We are processing a type constructed from a
   17309              :                    template template parameter.  */
   17310      1318020 :                 tree argvec = tsubst (TYPE_TI_ARGS (t),
   17311              :                                       args, complain, in_decl);
   17312       659010 :                 if (argvec == error_mark_node)
   17313              :                   return error_mark_node;
   17314              : 
   17315       658195 :                 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
   17316              :                             || TREE_CODE (arg) == TEMPLATE_DECL
   17317              :                             || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
   17318              : 
   17319       658195 :                 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
   17320              :                   /* Consider this code:
   17321              : 
   17322              :                         template <template <class> class Template>
   17323              :                         struct Internal {
   17324              :                         template <class Arg> using Bind = Template<Arg>;
   17325              :                         };
   17326              : 
   17327              :                         template <template <class> class Template, class Arg>
   17328              :                         using Instantiate = Template<Arg>; //#0
   17329              : 
   17330              :                         template <template <class> class Template,
   17331              :                                   class Argument>
   17332              :                         using Bind =
   17333              :                           Instantiate<Internal<Template>::template Bind,
   17334              :                                       Argument>; //#1
   17335              : 
   17336              :                      When #1 is parsed, the
   17337              :                      BOUND_TEMPLATE_TEMPLATE_PARM representing the
   17338              :                      parameter `Template' in #0 matches the
   17339              :                      UNBOUND_CLASS_TEMPLATE representing the argument
   17340              :                      `Internal<Template>::template Bind'; We then want
   17341              :                      to assemble the type `Bind<Argument>' that can't
   17342              :                      be fully created right now, because
   17343              :                      `Internal<Template>' not being complete, the Bind
   17344              :                      template cannot be looked up in that context.  So
   17345              :                      we need to "store" `Bind<Argument>' for later
   17346              :                      when the context of Bind becomes complete.  Let's
   17347              :                      store that in a TYPENAME_TYPE.  */
   17348            6 :                   return make_typename_type (TYPE_CONTEXT (arg),
   17349              :                                              build_nt (TEMPLATE_ID_EXPR,
   17350            6 :                                                        TYPE_IDENTIFIER (arg),
   17351              :                                                        argvec),
   17352              :                                              typename_type,
   17353            6 :                                              complain);
   17354              : 
   17355              :                 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
   17356              :                    are resolving nested-types in the signature of a
   17357              :                    member function templates.  Otherwise ARG is a
   17358              :                    TEMPLATE_DECL and is the real template to be
   17359              :                    instantiated.  */
   17360       658189 :                 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   17361          127 :                   arg = TYPE_NAME (arg);
   17362              : 
   17363       658189 :                 r = lookup_template_class (arg,
   17364              :                                            argvec, in_decl,
   17365       658189 :                                            DECL_CONTEXT (arg),
   17366              :                                            complain);
   17367       658189 :                 return cp_build_qualified_type
   17368       658189 :                   (r, cp_type_quals (t) | cp_type_quals (r), complain);
   17369              :               }
   17370    191290154 :             else if (code == TEMPLATE_TEMPLATE_PARM)
   17371              :               return arg;
   17372              :             else
   17373              :               /* TEMPLATE_PARM_INDEX.  */
   17374    188958562 :               return convert_from_reference (unshare_expr (arg));
   17375              :           }
   17376              : 
   17377    193478977 :         if (level == 1)
   17378              :           /* This can happen during the attempted tsubst'ing in
   17379              :              unify.  This means that we don't yet have any information
   17380              :              about the template parameter in question.  */
   17381              :           return t;
   17382              : 
   17383              :         /* Early in template argument deduction substitution, we don't
   17384              :            want to reduce the level of 'auto', or it will be confused
   17385              :            with a normal template parm in subsequent deduction.
   17386              :            Similarly, don't reduce the level of template parameters to
   17387              :            avoid mismatches when deducing their types.  */
   17388    176525774 :         if (complain & tf_partial)
   17389              :           return t;
   17390              : 
   17391              :         /* If we get here, we must have been looking at a parm for a
   17392              :            more deeply nested template.  Make a new version of this
   17393              :            template parameter, but with a lower level.  */
   17394    176000721 :         int quals;
   17395    176000721 :         switch (code)
   17396              :           {
   17397    170247094 :           case TEMPLATE_TYPE_PARM:
   17398    170247094 :           case TEMPLATE_TEMPLATE_PARM:
   17399    170247094 :             quals = cp_type_quals (t);
   17400    170247094 :             if (quals)
   17401              :               {
   17402     11802605 :                 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
   17403     11802605 :                 t = TYPE_MAIN_VARIANT (t);
   17404              :               }
   17405              : 
   17406    170247094 :             if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
   17407    156268613 :               if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
   17408    156268613 :                   && (code == TEMPLATE_TYPE_PARM
   17409        47814 :                       || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
   17410              :                 /* Cache lowering a type parameter or a simple template
   17411              :                    template parameter.  */
   17412    156234071 :                 r = TREE_TYPE (d);
   17413              : 
   17414    170247094 :             if (!r)
   17415              :               {
   17416     14013023 :                 r = copy_type (t);
   17417     14013023 :                 TEMPLATE_TYPE_PARM_INDEX (r)
   17418     14013023 :                   = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
   17419              :                                                 r, levels, args, complain);
   17420     14013023 :                 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
   17421     14013023 :                 TYPE_MAIN_VARIANT (r) = r;
   17422     14013023 :                 TYPE_POINTER_TO (r) = NULL_TREE;
   17423     14013023 :                 TYPE_REFERENCE_TO (r) = NULL_TREE;
   17424              : 
   17425     14013023 :                 if (code == TEMPLATE_TYPE_PARM)
   17426     14011225 :                   if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
   17427              :                     /* Propagate constraints on placeholders since they are
   17428              :                        only instantiated during satisfaction.  */
   17429       486918 :                     PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
   17430              : 
   17431     14013023 :                 if (TYPE_STRUCTURAL_EQUALITY_P (t))
   17432            0 :                   SET_TYPE_STRUCTURAL_EQUALITY (r);
   17433              :                 else
   17434     14013023 :                   TYPE_CANONICAL (r) = canonical_type_parameter (r);
   17435              :               }
   17436              : 
   17437    170247094 :             if (quals)
   17438     11802605 :               r = cp_build_qualified_type (r, quals,
   17439              :                                            complain | tf_ignore_bad_quals);
   17440              :             break;
   17441              : 
   17442        10837 :           case BOUND_TEMPLATE_TEMPLATE_PARM:
   17443        10837 :             {
   17444        10837 :               tree tinfo = TYPE_TEMPLATE_INFO (t);
   17445              :               /* We might need to substitute into the types of non-type
   17446              :                  template parameters.  This also lowers the level of
   17447              :                  the ttp appropriately.  */
   17448        10837 :               tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
   17449              :                                   complain, in_decl);
   17450        10837 :               if (tmpl == error_mark_node)
   17451              :                 return error_mark_node;
   17452        10837 :               tree argvec = tsubst (TI_ARGS (tinfo), args,
   17453              :                                     complain, in_decl);
   17454        10837 :               if (argvec == error_mark_node)
   17455              :                 return error_mark_node;
   17456        10837 :               r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE,
   17457              :                                          complain);
   17458        10837 :               r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   17459        10837 :               break;
   17460              :             }
   17461              : 
   17462      5742790 :           case TEMPLATE_PARM_INDEX:
   17463              :             /* OK, now substitute the type of the non-type parameter.  We
   17464              :                couldn't do it earlier because it might be an auto parameter,
   17465              :                and we wouldn't need to if we had an argument.  */
   17466      5742790 :             type = tsubst (type, args, complain, in_decl);
   17467      5742790 :             if (type == error_mark_node)
   17468              :               return error_mark_node;
   17469      5742784 :             r = reduce_template_parm_level (t, type, levels, args, complain);
   17470      5742784 :             break;
   17471              : 
   17472            0 :           default:
   17473            0 :             gcc_unreachable ();
   17474              :           }
   17475              : 
   17476    176000715 :         return r;
   17477              :       }
   17478              : 
   17479           26 :     case TREE_LIST:
   17480           26 :       return tsubst_tree_list (t, args, complain, in_decl);
   17481              : 
   17482            0 :     case TREE_BINFO:
   17483              :       /* We should never be tsubsting a binfo.  */
   17484            0 :       gcc_unreachable ();
   17485              : 
   17486    385747526 :     case TREE_VEC:
   17487              :       /* A vector of template arguments.  */
   17488    385747526 :       gcc_assert (!type);
   17489    385747526 :       return tsubst_template_args (t, args, complain, in_decl);
   17490              : 
   17491    592684608 :     case POINTER_TYPE:
   17492    592684608 :     case REFERENCE_TYPE:
   17493    592684608 :       {
   17494    592684608 :         if (type == TREE_TYPE (t)
   17495     33573421 :             && TREE_CODE (type) != METHOD_TYPE
   17496    626257453 :             && (TYPE_ATTRIBUTES (t) == NULL_TREE
   17497            6 :                 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
   17498              :           return t;
   17499              : 
   17500              :         /* [temp.deduct]
   17501              : 
   17502              :            Type deduction may fail for any of the following
   17503              :            reasons:
   17504              : 
   17505              :            -- Attempting to create a pointer to reference type.
   17506              :            -- Attempting to create a reference to a reference type or
   17507              :               a reference to void.
   17508              : 
   17509              :           Core issue 106 says that creating a reference to a reference
   17510              :           during instantiation is no longer a cause for failure. We
   17511              :           only enforce this check in strict C++98 mode.  */
   17512    559111769 :         if ((TYPE_REF_P (type)
   17513     28978061 :              && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
   17514    559111583 :             || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
   17515              :           {
   17516         1316 :             static location_t last_loc;
   17517              : 
   17518              :             /* We keep track of the last time we issued this error
   17519              :                message to avoid spewing a ton of messages during a
   17520              :                single bad template instantiation.  */
   17521         1316 :             if (complain & tf_error
   17522           84 :                 && last_loc != input_location)
   17523              :               {
   17524           84 :                 if (VOID_TYPE_P (type))
   17525            8 :                   error ("forming reference to void");
   17526           76 :                else if (code == POINTER_TYPE)
   17527           76 :                  error ("forming pointer to reference type %qT", type);
   17528              :                else
   17529            0 :                   error ("forming reference to reference type %qT", type);
   17530           84 :                 last_loc = input_location;
   17531              :               }
   17532              : 
   17533         1316 :             return error_mark_node;
   17534              :           }
   17535    559110453 :         else if (TREE_CODE (type) == FUNCTION_TYPE
   17536    559110453 :                  && (type_memfn_quals (type) != TYPE_UNQUALIFIED
   17537      6692553 :                      || type_memfn_rqual (type) != REF_QUAL_NONE))
   17538              :           {
   17539           76 :             if (complain & tf_error)
   17540              :               {
   17541           11 :                 if (code == POINTER_TYPE)
   17542            6 :                   error ("forming pointer to qualified function type %qT",
   17543              :                          type);
   17544              :                 else
   17545            5 :                   error ("forming reference to qualified function type %qT",
   17546              :                          type);
   17547              :               }
   17548           76 :             return error_mark_node;
   17549              :           }
   17550    559110377 :         else if (code == POINTER_TYPE)
   17551              :           {
   17552    261793347 :             r = build_pointer_type (type);
   17553    261793347 :             if (TREE_CODE (type) == METHOD_TYPE)
   17554        31614 :               r = build_ptrmemfunc_type (r);
   17555              :           }
   17556    297317030 :         else if (TYPE_REF_P (type))
   17557              :           /* In C++0x, during template argument substitution, when there is an
   17558              :              attempt to create a reference to a reference type, reference
   17559              :              collapsing is applied as described in [14.3.1/4 temp.arg.type]:
   17560              : 
   17561              :              "If a template-argument for a template-parameter T names a type
   17562              :              that is a reference to a type A, an attempt to create the type
   17563              :              'lvalue reference to cv T' creates the type 'lvalue reference to
   17564              :              A,' while an attempt to create the type type rvalue reference to
   17565              :              cv T' creates the type T"
   17566              :           */
   17567     57955750 :           r = cp_build_reference_type (TREE_TYPE (type),
   17568     28977875 :                                        TYPE_REF_IS_RVALUE (t)
   17569     28977875 :                                        && TYPE_REF_IS_RVALUE (type));
   17570              :         else
   17571    268339155 :           r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
   17572    559110377 :         r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   17573              : 
   17574    559110377 :         if (r != error_mark_node)
   17575              :           /* Will this ever be needed for TYPE_..._TO values?  */
   17576    559110377 :           layout_type (r);
   17577              : 
   17578    559110377 :         if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
   17579              :                                              /*flags=*/0,
   17580              :                                              args, complain, in_decl))
   17581            0 :           return error_mark_node;
   17582              : 
   17583    559110377 :         return r;
   17584              :       }
   17585       197037 :     case OFFSET_TYPE:
   17586       197037 :       {
   17587       197037 :         r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
   17588       197037 :         if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
   17589              :           {
   17590              :             /* [temp.deduct]
   17591              : 
   17592              :                Type deduction may fail for any of the following
   17593              :                reasons:
   17594              : 
   17595              :                -- Attempting to create "pointer to member of T" when T
   17596              :                   is not a class type.  */
   17597            0 :             if (complain & tf_error)
   17598            0 :               error ("creating pointer to member of non-class type %qT", r);
   17599            0 :             return error_mark_node;
   17600              :           }
   17601       197037 :         if (TYPE_REF_P (type))
   17602              :           {
   17603            6 :             if (complain & tf_error)
   17604            3 :               error ("creating pointer to member reference type %qT", type);
   17605            6 :             return error_mark_node;
   17606              :           }
   17607       197031 :         if (VOID_TYPE_P (type))
   17608              :           {
   17609            3 :             if (complain & tf_error)
   17610            3 :               error ("creating pointer to member of type void");
   17611            3 :             return error_mark_node;
   17612              :           }
   17613       197028 :         gcc_assert (TREE_CODE (type) != METHOD_TYPE);
   17614       197028 :         if (TREE_CODE (type) == FUNCTION_TYPE)
   17615              :           {
   17616              :             /* The type of the implicit object parameter gets its
   17617              :                cv-qualifiers from the FUNCTION_TYPE. */
   17618       148142 :             tree memptr;
   17619       148142 :             tree method_type
   17620       148142 :               = build_memfn_type (type, r, type_memfn_quals (type),
   17621              :                                   type_memfn_rqual (type));
   17622       148142 :             memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
   17623       148142 :             return cp_build_qualified_type (memptr, cp_type_quals (t),
   17624       148142 :                                             complain);
   17625              :           }
   17626              :         else
   17627        48886 :           return cp_build_qualified_type (build_ptrmem_type (r, type),
   17628              :                                           cp_type_quals (t),
   17629        48886 :                                           complain);
   17630              :       }
   17631    162549142 :     case FUNCTION_TYPE:
   17632    162549142 :     case METHOD_TYPE:
   17633    162549142 :       {
   17634    162549142 :         tree fntype;
   17635    162549142 :         tree specs;
   17636    162549142 :         fntype = tsubst_function_type (t, args, complain, in_decl);
   17637    162538309 :         if (fntype == error_mark_node)
   17638              :           return error_mark_node;
   17639              : 
   17640              :         /* Substitute the exception specification.  */
   17641    160972698 :         specs = tsubst_exception_specification (t, args, complain, in_decl,
   17642              :                                                 /*defer_ok*/fndecl_type);
   17643    160972698 :         if (specs == error_mark_node)
   17644              :           return error_mark_node;
   17645    160972685 :         if (specs)
   17646     58221066 :           fntype = build_exception_variant (fntype, specs);
   17647              :         return fntype;
   17648              :       }
   17649     10445480 :     case ARRAY_TYPE:
   17650     10445480 :       {
   17651     10445480 :         tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
   17652     10445480 :         if (domain == error_mark_node)
   17653              :           return error_mark_node;
   17654              : 
   17655              :         /* As an optimization, we avoid regenerating the array type if
   17656              :            it will obviously be the same as T.  */
   17657     10445289 :         if (type == TREE_TYPE (t)
   17658      8096230 :             && domain == TYPE_DOMAIN (t)
   17659     18473866 :             && (TYPE_ATTRIBUTES (t) == NULL_TREE
   17660            3 :                 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
   17661              :           return t;
   17662              : 
   17663              :         /* These checks should match the ones in create_array_type_for_decl.
   17664              : 
   17665              :            [temp.deduct]
   17666              : 
   17667              :            The deduction may fail for any of the following reasons:
   17668              : 
   17669              :            -- Attempting to create an array with an element type that
   17670              :               is void, a function type, or a reference type, or [DR337]
   17671              :               an abstract class type.  */
   17672      2416715 :         if (VOID_TYPE_P (type)
   17673      2416715 :             || TREE_CODE (type) == FUNCTION_TYPE
   17674      2416705 :             || (TREE_CODE (type) == ARRAY_TYPE
   17675         1549 :                 && TYPE_DOMAIN (type) == NULL_TREE)
   17676      4833412 :             || TYPE_REF_P (type))
   17677              :           {
   17678           18 :             if (complain & tf_error)
   17679            3 :               error ("creating array of %qT", type);
   17680           18 :             return error_mark_node;
   17681              :           }
   17682              : 
   17683      2416697 :         if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
   17684              :                                   !(complain & tf_error)))
   17685            0 :           return error_mark_node;
   17686              : 
   17687      2416697 :         r = build_cplus_array_type (type, domain);
   17688              : 
   17689      2416697 :         if (!valid_array_size_p (input_location, r, in_decl,
   17690              :                                  (complain & tf_error)))
   17691           15 :           return error_mark_node;
   17692              : 
   17693      2416682 :         if (TYPE_USER_ALIGN (t))
   17694              :           {
   17695            6 :             SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
   17696            6 :             TYPE_USER_ALIGN (r) = 1;
   17697              :           }
   17698              : 
   17699      2416682 :         if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
   17700              :                                              /*flags=*/0,
   17701              :                                              args, complain, in_decl))
   17702            0 :           return error_mark_node;
   17703              : 
   17704      2416682 :         return r;
   17705              :       }
   17706              : 
   17707    221613647 :     case TYPENAME_TYPE:
   17708    221613647 :       {
   17709    221613647 :         tree ctx = TYPE_CONTEXT (t);
   17710    221613647 :         if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
   17711              :           {
   17712            6 :             ctx = tsubst_pack_expansion (ctx, args,
   17713              :                                          complain | tf_qualifying_scope,
   17714              :                                          in_decl);
   17715            6 :             if (ctx == error_mark_node)
   17716              :               return error_mark_node;
   17717            6 :             if (TREE_VEC_LENGTH (ctx) > 1)
   17718              :                 {
   17719            0 :                   if (complain & tf_error)
   17720            0 :                     error ("%qD expanded to more than one element",
   17721            0 :                            TYPENAME_TYPE_FULLNAME (t));
   17722            0 :                   return error_mark_node;
   17723              :                 }
   17724            6 :             if (TREE_VEC_LENGTH (ctx) == 0)
   17725              :               {
   17726            3 :                 if (complain & tf_error)
   17727            6 :                   error ("%qD is instantiated for an empty pack",
   17728            3 :                          TYPENAME_TYPE_FULLNAME (t));
   17729            3 :                 return error_mark_node;
   17730              :               }
   17731            3 :             ctx = TREE_VEC_ELT (ctx, 0);
   17732              :           }
   17733              :         else
   17734    221613641 :           ctx = tsubst_entering_scope (ctx, args,
   17735              :                                        complain | tf_qualifying_scope,
   17736              :                                        in_decl);
   17737    221613644 :         if (ctx == error_mark_node)
   17738              :           return error_mark_node;
   17739              : 
   17740    221531138 :         tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args,
   17741              :                               complain, in_decl);
   17742    221531138 :         if (f == error_mark_node)
   17743              :           return error_mark_node;
   17744              : 
   17745              :         /* We had [:X:]:: which was substituted into a NAMESPACE_DECL and not
   17746              :            a type.  */
   17747    221530894 :         if (TREE_CODE (ctx) == NAMESPACE_DECL)
   17748              :           {
   17749           44 :             if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
   17750              :               {
   17751            6 :                 tree d = TREE_OPERAND (f, 0);
   17752            6 :                 tree n = TREE_OPERAND (f, 1);
   17753            6 :                 f = lookup_template_class (d, n, in_decl, ctx, complain);
   17754            6 :                 if (f == error_mark_node)
   17755              :                   return error_mark_node;
   17756              :               }
   17757              :             else
   17758              :               {
   17759           38 :                 gcc_assert (TREE_CODE (f) == IDENTIFIER_NODE);
   17760           38 :                 tree decl = lookup_qualified_name (ctx, f);
   17761           38 :                 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
   17762              :                   {
   17763            2 :                     qualified_name_lookup_error (ctx, f, decl, input_location);
   17764            2 :                     return error_mark_node;
   17765              :                   }
   17766           36 :                 if (TREE_CODE (decl) == NAMESPACE_DECL)
   17767              :                   return decl;
   17768              :                 else
   17769              :                   {
   17770           25 :                     gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL);
   17771           25 :                     f = TREE_TYPE (decl);
   17772              :                   }
   17773              :               }
   17774           31 :             return cp_build_qualified_type
   17775           31 :                     (f, cp_type_quals (f) | cp_type_quals (t), complain);
   17776              :           }
   17777              : 
   17778    219595425 :         if (!MAYBE_CLASS_TYPE_P (ctx))
   17779              :           {
   17780        11698 :             if (complain & tf_error)
   17781           67 :               error ("%qT is not a class, struct, or union type", ctx);
   17782        11698 :             return error_mark_node;
   17783              :           }
   17784    221519152 :         else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
   17785              :           {
   17786              :             /* Normally, make_typename_type does not require that the CTX
   17787              :                have complete type in order to allow things like:
   17788              : 
   17789              :                  template <class T> struct S { typename S<T>::X Y; };
   17790              : 
   17791              :                But, such constructs have already been resolved by this
   17792              :                point, so here CTX really should have complete type, unless
   17793              :                it's a partial instantiation.  */
   17794    194649787 :             if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
   17795         3201 :               return error_mark_node;
   17796              :           }
   17797              : 
   17798    221515951 :         enum tag_types tag_type = get_typename_tag (t);
   17799    221515951 :         tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
   17800    221515951 :         tcomplain |= tst_ok_flag | qualifying_scope_flag;
   17801    221515951 :         f = make_typename_type (ctx, f, tag_type, tcomplain);
   17802    221515951 :         if (f == error_mark_node)
   17803              :           return f;
   17804    202486876 :         if (TREE_CODE (f) == TYPE_DECL)
   17805              :           {
   17806    175415748 :             complain |= tf_ignore_bad_quals;
   17807    175415748 :             f = TREE_TYPE (f);
   17808              :           }
   17809              : 
   17810    202486876 :         if (!WILDCARD_TYPE_P (f))
   17811              :           {
   17812    176015183 :             if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
   17813              :               {
   17814            3 :                 if (complain & tf_error)
   17815            3 :                   error ("%qT resolves to %qT, which is not an enumeration type",
   17816              :                          t, f);
   17817              :                 else
   17818              :                   return error_mark_node;
   17819              :               }
   17820    173235730 :             else if (TYPENAME_IS_CLASS_OR_STRUCT_P (t)
   17821    176015236 :                      && !NON_UNION_CLASS_TYPE_P (f))
   17822              :               {
   17823           41 :                 if (complain & tf_error)
   17824            6 :                   error ("%qT resolves to %qT, which is not a non-union "
   17825              :                          "class type", t, f);
   17826              :                 else
   17827              :                   return error_mark_node;
   17828              :               }
   17829    176015139 :             else if (TYPENAME_IS_UNION_P (t) && !UNION_TYPE_P (f))
   17830              :               {
   17831            3 :                 if (complain & tf_error)
   17832            3 :                   error ("%qT resolves to %qT, which is not a union type",
   17833              :                          t, f);
   17834              :                 else
   17835              :                   return error_mark_node;
   17836              :               }
   17837              :           }
   17838              : 
   17839    202486841 :         return cp_build_qualified_type
   17840    202486841 :           (f, cp_type_quals (f) | cp_type_quals (t), complain);
   17841              :       }
   17842              : 
   17843       111892 :     case UNBOUND_CLASS_TEMPLATE:
   17844       111892 :       {
   17845       111892 :         tree name = TYPE_IDENTIFIER (t);
   17846       111892 :         if (name == error_mark_node)
   17847              :           return error_mark_node;
   17848              : 
   17849       111892 :         tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
   17850       111892 :         parm_list = tsubst_template_parms (parm_list, args, complain);
   17851       111892 :         if (parm_list == error_mark_node)
   17852              :           return error_mark_node;
   17853              : 
   17854       111907 :         if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
   17855           15 :           ++processing_template_decl;
   17856       111892 :         tree ctx = tsubst_entering_scope (TYPE_CONTEXT (t), args,
   17857              :                                           complain, in_decl);
   17858       111907 :         if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
   17859           15 :           --processing_template_decl;
   17860       111892 :         if (ctx == error_mark_node)
   17861              :           return error_mark_node;
   17862              : 
   17863       111892 :         return make_unbound_class_template (ctx, name, parm_list, complain);
   17864              :       }
   17865              : 
   17866          222 :     case TYPEOF_TYPE:
   17867          222 :       {
   17868          222 :         tree type;
   17869              : 
   17870          222 :         ++cp_unevaluated_operand;
   17871          222 :         ++c_inhibit_evaluation_warnings;
   17872              : 
   17873          222 :         type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
   17874              : 
   17875          222 :         --cp_unevaluated_operand;
   17876          222 :         --c_inhibit_evaluation_warnings;
   17877              : 
   17878          222 :         type = finish_typeof (type);
   17879          222 :         return cp_build_qualified_type (type,
   17880          222 :                                         cp_type_quals (t)
   17881          222 :                                         | cp_type_quals (type),
   17882          222 :                                         complain);
   17883              :       }
   17884              : 
   17885     24740350 :     case DECLTYPE_TYPE:
   17886     24740350 :       {
   17887     24740350 :         tree type;
   17888              : 
   17889     24740350 :         ++cp_unevaluated_operand;
   17890     24740350 :         ++c_inhibit_evaluation_warnings;
   17891              : 
   17892     24740350 :         type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
   17893              :                             complain|tf_decltype, in_decl);
   17894              : 
   17895     24729550 :         --cp_unevaluated_operand;
   17896     24729550 :         --c_inhibit_evaluation_warnings;
   17897              : 
   17898     24729550 :         if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
   17899       227312 :           type = lambda_capture_field_type (type,
   17900              :                                             false /*explicit_init*/,
   17901       113656 :                                             DECLTYPE_FOR_REF_CAPTURE (t));
   17902     24615894 :         else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
   17903           26 :           type = lambda_proxy_type (type);
   17904              :         else
   17905              :           {
   17906     24615868 :             bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
   17907       417109 :             if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
   17908     24615868 :                 && EXPR_P (type))
   17909              :               /* In a template ~id could be either a complement expression
   17910              :                  or an unqualified-id naming a destructor; if instantiating
   17911              :                  it produces an expression, it's not an id-expression or
   17912              :                  member access.  */
   17913              :               id = false;
   17914     24615868 :             type = finish_decltype_type (type, id, complain);
   17915              :           }
   17916     24729550 :         return cp_build_qualified_type (type,
   17917     24729550 :                                         cp_type_quals (t)
   17918     24729550 :                                         | cp_type_quals (type),
   17919     24729550 :                                         complain | tf_ignore_bad_quals);
   17920              :       }
   17921              : 
   17922      7445606 :     case TRAIT_TYPE:
   17923      7445606 :       {
   17924      7445606 :         tree type1 = TRAIT_TYPE_TYPE1 (t);
   17925      7445606 :         if (TYPE_P (type1))
   17926      7338597 :           type1 = tsubst (type1, args, complain, in_decl);
   17927              :         else
   17928       107009 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   17929      7445606 :         tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
   17930      7445606 :         type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
   17931      7445606 :         return cp_build_qualified_type (type,
   17932      7445606 :                                         cp_type_quals (t) | cp_type_quals (type),
   17933      7445606 :                                         complain | tf_ignore_bad_quals);
   17934              :       }
   17935              : 
   17936            0 :     case TYPE_ARGUMENT_PACK:
   17937            0 :     case NONTYPE_ARGUMENT_PACK:
   17938            0 :       return tsubst_argument_pack (t, args, complain, in_decl);
   17939              : 
   17940         2739 :     case PACK_INDEX_TYPE:
   17941         2739 :       return tsubst_pack_index (t, args, complain, in_decl);
   17942              : 
   17943          225 :     case SPLICE_SCOPE:
   17944          225 :       return tsubst_splice_scope (t, args, complain, in_decl);
   17945              : 
   17946          197 :     case SPLICE_EXPR:
   17947              :       /* We are coming from tsubst_splice_scope for [:R:]
   17948              :          where R needs to expand to a type or scope.  */
   17949          197 :       gcc_checking_assert (!SPLICE_EXPR_EXPRESSION_P (t));
   17950          197 :       return tsubst_splice_expr (t, args, complain, in_decl);
   17951              : 
   17952           28 :     case TEMPLATE_ID_EXPR:
   17953           28 :       {
   17954              :         /* We end up here coming from tsubst_splice_scope for
   17955              :            [:R:]<int>.  R needs to expand to a type or scope.  */
   17956           28 :         tree templ = TREE_OPERAND (t, 0);
   17957           28 :         gcc_assert (TREE_CODE (templ) == SPLICE_EXPR);
   17958           28 :         templ = tsubst_splice_expr (templ, args, complain, in_decl);
   17959           28 :         if (templ == error_mark_node)
   17960              :           return error_mark_node;
   17961           22 :         if (!DECL_TYPE_TEMPLATE_P (templ)
   17962           28 :             && !DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
   17963              :           {
   17964            6 :             if (complain & tf_error)
   17965              :               {
   17966            3 :                 auto_diagnostic_group d;
   17967            6 :                 error_at (cp_expr_loc_or_input_loc (TREE_OPERAND (t, 0)),
   17968              :                           "expected a reflection of a type template");
   17969            3 :                 inform_tree_category (templ);
   17970            3 :               }
   17971            6 :             return error_mark_node;
   17972              :           }
   17973           20 :         tree targs = TREE_OPERAND (t, 1);
   17974           20 :         if (targs)
   17975           20 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   17976           20 :         if (targs == error_mark_node)
   17977              :           return error_mark_node;
   17978           20 :         tree r = finish_template_type (templ, targs, /*entering_scope=*/false);
   17979           20 :         if (TREE_CODE (r) == TYPE_DECL)
   17980           18 :           r = TREE_TYPE (r);
   17981              :         return r;
   17982              :       }
   17983              : 
   17984            0 :     case VOID_CST:
   17985            0 :     case INTEGER_CST:
   17986            0 :     case REAL_CST:
   17987            0 :     case STRING_CST:
   17988            0 :     case PLUS_EXPR:
   17989            0 :     case MINUS_EXPR:
   17990            0 :     case NEGATE_EXPR:
   17991            0 :     case NOP_EXPR:
   17992            0 :     case INDIRECT_REF:
   17993            0 :     case ADDR_EXPR:
   17994            0 :     case CALL_EXPR:
   17995            0 :     case ARRAY_REF:
   17996            0 :     case SCOPE_REF:
   17997            0 :     case OMP_ARRAY_SECTION:
   17998              :       /* We should use one of the expression tsubsts for these codes.  */
   17999            0 :       gcc_unreachable ();
   18000              : 
   18001            0 :     default:
   18002            0 :       sorry ("use of %qs in template", get_tree_code_name (code));
   18003            0 :       return error_mark_node;
   18004              :     }
   18005              : }
   18006              : 
   18007              : /* Convenience wrapper over tsubst for substituting into the LHS
   18008              :    of the :: scope resolution operator.  */
   18009              : 
   18010              : static tree
   18011     80601294 : tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   18012              : {
   18013     80601294 :   gcc_checking_assert (TYPE_P (t) || TREE_CODE (t) == NAMESPACE_DECL);
   18014     80601294 :   return tsubst (t, args, complain | tf_qualifying_scope, in_decl);
   18015              : }
   18016              : 
   18017              : /* Convenience wrapper over tsubst for substituting into an id-expression
   18018              :    without resolving its terminal name.  */
   18019              : 
   18020              : static tree
   18021    323291149 : tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   18022              : {
   18023    244344729 :   return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl);
   18024              : }
   18025              : 
   18026              : /* OLDFNS is a lookup set of member functions from some class template, and
   18027              :    NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
   18028              :    of that class template.  Return the subset of NEWFNS which are
   18029              :    specializations of a function from OLDFNS.  */
   18030              : 
   18031              : static tree
   18032      4590887 : filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
   18033              : {
   18034              :   /* Record all member functions from the old lookup set OLDFNS into
   18035              :      VISIBLE_SET.  */
   18036      4590887 :   hash_set<tree> visible_set;
   18037      4590887 :   bool seen_dep_using = false;
   18038     13816627 :   for (tree fn : lkp_range (oldfns))
   18039              :     {
   18040      4634853 :       if (TREE_CODE (fn) == USING_DECL)
   18041              :         {
   18042              :           /* Imprecisely handle dependent using-decl by keeping all members
   18043              :              in the new lookup set that are defined in a base class, i.e.
   18044              :              members that could plausibly have been introduced by this
   18045              :              dependent using-decl.
   18046              :              FIXME: Track which members are introduced by a dependent
   18047              :              using-decl precisely, perhaps by performing another lookup
   18048              :              from the substituted USING_DECL_SCOPE.  */
   18049            0 :           gcc_checking_assert (DECL_DEPENDENT_P (fn));
   18050              :           seen_dep_using = true;
   18051              :         }
   18052              :       else
   18053      4634853 :         visible_set.add (fn);
   18054              :     }
   18055              : 
   18056              :   /* Returns true iff (a less specialized version of) FN appeared in
   18057              :      the old lookup set OLDFNS.  */
   18058      9228695 :   auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
   18059      4637808 :     if (DECL_CONTEXT (fn) != newtype)
   18060              :       /* FN is a member function from a base class, introduced via a
   18061              :          using-decl; if it might have been introduced by a dependent
   18062              :          using-decl then just conservatively keep it, otherwise look
   18063              :          in the old lookup set for FN exactly.  */
   18064           33 :       return seen_dep_using || visible_set.contains (fn);
   18065      4637775 :     else if (TREE_CODE (fn) == TEMPLATE_DECL)
   18066              :       /* FN is a member function template from the current class;
   18067              :          look in the old lookup set for the TEMPLATE_DECL from which
   18068              :          it was specialized.  */
   18069      2383187 :       return visible_set.contains (DECL_TI_TEMPLATE (fn));
   18070              :     else
   18071              :       /* FN is a non-template member function from the current class;
   18072              :          look in the old lookup set for the FUNCTION_DECL from which
   18073              :          it was specialized.  */
   18074      2254588 :       return visible_set.contains (DECL_TEMPLATE_RESULT
   18075      2254588 :                                    (DECL_TI_TEMPLATE (fn)));
   18076      4590887 :   };
   18077              : 
   18078      4590887 :   bool lookup_changed_p = false;
   18079     13816468 :   for (tree fn : lkp_range (newfns))
   18080      4634856 :     if (!visible_p (fn))
   18081              :       {
   18082              :         lookup_changed_p = true;
   18083              :         break;
   18084              :       }
   18085      4590887 :   if (!lookup_changed_p)
   18086              :     return newfns;
   18087              : 
   18088              :   /* Filter out from NEWFNS the member functions that weren't
   18089              :      previously visible according to OLDFNS.  */
   18090          162 :   tree filtered_fns = NULL_TREE;
   18091          162 :   unsigned filtered_size = 0;
   18092         3276 :   for (tree fn : lkp_range (newfns))
   18093         2952 :     if (visible_p (fn))
   18094              :       {
   18095          162 :         filtered_fns = lookup_add (fn, filtered_fns);
   18096          162 :         filtered_size++;
   18097              :       }
   18098          162 :   gcc_checking_assert (seen_dep_using
   18099              :                        ? filtered_size >= visible_set.elements ()
   18100              :                        : filtered_size == visible_set.elements ());
   18101              : 
   18102              :   return filtered_fns;
   18103      4590887 : }
   18104              : 
   18105              : /* tsubst a BASELINK.  OBJECT_TYPE, if non-NULL, is the type of the
   18106              :    expression on the left-hand side of the "." or "->" operator.  We
   18107              :    only do the lookup if we had a dependent BASELINK.  Otherwise we
   18108              :    adjust it onto the instantiated heirarchy.  */
   18109              : 
   18110              : static tree
   18111     19636072 : tsubst_baselink (tree baselink, tree object_type,
   18112              :                  tree args, tsubst_flags_t complain, tree in_decl)
   18113              : {
   18114     19636072 :   bool qualified_p = BASELINK_QUALIFIED_P (baselink);
   18115     19636072 :   tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
   18116     19636072 :   qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
   18117              : 
   18118     19636072 :   tree optype = BASELINK_OPTYPE (baselink);
   18119     19636072 :   optype = tsubst (optype, args, complain, in_decl);
   18120              : 
   18121     19636072 :   tree template_args = NULL_TREE;
   18122     19636072 :   bool template_id_p = false;
   18123     19636072 :   tree fns = BASELINK_FUNCTIONS (baselink);
   18124     19636072 :   if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
   18125              :     {
   18126      3563830 :       template_id_p = true;
   18127      3563830 :       template_args = TREE_OPERAND (fns, 1);
   18128      3563830 :       fns = TREE_OPERAND (fns, 0);
   18129      3563830 :       if (template_args)
   18130      3563830 :         template_args = tsubst_template_args (template_args, args,
   18131              :                                               complain, in_decl);
   18132      3563830 :       if (template_args == error_mark_node)
   18133              :         return error_mark_node;
   18134              :     }
   18135              : 
   18136     19636046 :   tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
   18137     19636046 :   binfo_type = tsubst (binfo_type, args, complain, in_decl);
   18138     19636046 :   bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
   18139     40111884 :                       || optype != BASELINK_OPTYPE (baselink));
   18140              : 
   18141      5149847 :   if (dependent_p)
   18142              :     {
   18143     14486199 :       tree name = OVL_NAME (fns);
   18144     14486199 :       if (IDENTIFIER_CONV_OP_P (name))
   18145           12 :         name = make_conv_op_name (optype);
   18146              : 
   18147              :       /* See maybe_dependent_member_ref.  */
   18148     14486199 :       if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
   18149              :         {
   18150        87025 :           if (template_id_p)
   18151        86581 :             name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
   18152              :                            template_args);
   18153        87025 :           return build_qualified_name (NULL_TREE, qualifying_scope, name,
   18154        87025 :                                        /* ::template */false);
   18155              :         }
   18156              : 
   18157     14399174 :       if (name == complete_dtor_identifier)
   18158              :         /* Treat as-if non-dependent below.  */
   18159            0 :         dependent_p = false;
   18160              : 
   18161     14399174 :       bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
   18162     14399174 :       baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
   18163              :                                   complain);
   18164     14399174 :       if (!baselink)
   18165              :         {
   18166            6 :           if (complain & tf_error)
   18167              :             {
   18168            6 :               if (constructor_name_p (name, qualifying_scope))
   18169            0 :                 error ("cannot call constructor %<%T::%D%> directly",
   18170              :                        qualifying_scope, name);
   18171              :               else
   18172              :                 /* Lookup succeeded at parse time, but failed during
   18173              :                    instantiation; must be because we're trying to refer to it
   18174              :                    while forming its declaration (c++/120204).  */
   18175            6 :                 error ("declaration of %<%T::%D%> depends on itself",
   18176              :                        qualifying_scope, name);
   18177              :             }
   18178            6 :           return error_mark_node;
   18179              :         }
   18180              : 
   18181     14399168 :       if (maybe_incomplete)
   18182              :         {
   18183              :           /* Filter out from the new lookup set those functions which didn't
   18184              :              appear in the original lookup set (in a less specialized form).
   18185              :              This is needed to preserve the consistency of member lookup
   18186              :              performed in an incomplete-class context, within which
   18187              :              later-declared members ought to remain invisible.  */
   18188      4590887 :           BASELINK_FUNCTIONS (baselink)
   18189      4590887 :             = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
   18190              :                                    binfo_type);
   18191      4590887 :           BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
   18192              :         }
   18193              : 
   18194     14399168 :       fns = BASELINK_FUNCTIONS (baselink);
   18195              :     }
   18196              :   else
   18197              :     {
   18198              :       /* We're going to overwrite pieces below, make a duplicate.  */
   18199      5149847 :       baselink = copy_node (baselink);
   18200              : 
   18201      5149847 :       if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
   18202              :         {
   18203              :           /* The decl we found was from non-dependent scope, but we still need
   18204              :              to update the binfos for the instantiated qualifying_scope.  */
   18205       161033 :           BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
   18206       161033 :           BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
   18207              :                                                    ba_unique, nullptr, complain);
   18208              :         }
   18209              :     }
   18210              : 
   18211              :   /* If lookup found a single function, mark it as used at this point.
   18212              :      (If lookup found multiple functions the one selected later by
   18213              :      overload resolution will be marked as used at that point.)  */
   18214     19549015 :   if (!template_id_p && !really_overloaded_fn (fns))
   18215              :     {
   18216     12335102 :       tree fn = OVL_FIRST (fns);
   18217     12335102 :       bool ok = mark_used (fn, complain);
   18218     12335102 :       if (!ok && !(complain & tf_error))
   18219            6 :         return error_mark_node;
   18220     12335093 :       if (ok && BASELINK_P (baselink))
   18221              :         /* We might have instantiated an auto function.  */
   18222     12335093 :         TREE_TYPE (baselink) = TREE_TYPE (fn);
   18223              :     }
   18224              : 
   18225     19549009 :   if (BASELINK_P (baselink))
   18226              :     {
   18227              :       /* Add back the template arguments, if present.  */
   18228     19549009 :       if (template_id_p)
   18229      6954446 :         BASELINK_FUNCTIONS (baselink)
   18230      3477223 :           = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
   18231              : 
   18232              :       /* Update the conversion operator type.  */
   18233     19549009 :       BASELINK_OPTYPE (baselink) = optype;
   18234              :     }
   18235              : 
   18236     19549009 :   if (!object_type)
   18237       592843 :     object_type = current_class_type;
   18238              : 
   18239     19549009 :   if (qualified_p || !dependent_p)
   18240              :     {
   18241      5190496 :       baselink = adjust_result_of_qualified_name_lookup (baselink,
   18242              :                                                          qualifying_scope,
   18243              :                                                          object_type);
   18244      5190496 :       if (!qualified_p)
   18245              :         /* We need to call adjust_result_of_qualified_name_lookup in case the
   18246              :            destructor names a base class, but we unset BASELINK_QUALIFIED_P
   18247              :            so that we still get virtual function binding.  */
   18248      5058565 :         BASELINK_QUALIFIED_P (baselink) = false;
   18249              :     }
   18250              : 
   18251              :   return baselink;
   18252              : }
   18253              : 
   18254              : /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
   18255              :    true if the qualified-id will be a postfix-expression in-and-of
   18256              :    itself; false if more of the postfix-expression follows the
   18257              :    QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
   18258              :    of "&".  REFLECTING_P is true if this SCOPE_REF is an operand of ^^.  */
   18259              : 
   18260              : static tree
   18261     80109987 : tsubst_qualified_id (tree qualified_id, tree args,
   18262              :                      tsubst_flags_t complain, tree in_decl,
   18263              :                      bool done, bool address_p, bool reflecting_p = false)
   18264              : {
   18265     80109987 :   tree expr;
   18266     80109987 :   tree scope;
   18267     80109987 :   tree name;
   18268     80109987 :   bool is_template;
   18269     80109987 :   tree template_args;
   18270     80109987 :   location_t loc = EXPR_LOCATION (qualified_id);
   18271              : 
   18272     80109987 :   gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
   18273              : 
   18274              :   /* Figure out what name to look up.  */
   18275     80109987 :   name = TREE_OPERAND (qualified_id, 1);
   18276     80109987 :   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
   18277              :     {
   18278        65425 :       is_template = true;
   18279        65425 :       template_args = TREE_OPERAND (name, 1);
   18280        65425 :       if (template_args)
   18281        65425 :         template_args = tsubst_template_args (template_args, args,
   18282              :                                               complain, in_decl);
   18283        65425 :       if (template_args == error_mark_node)
   18284              :         return error_mark_node;
   18285        65417 :       name = TREE_OPERAND (name, 0);
   18286              :     }
   18287              :   else
   18288              :     {
   18289              :       is_template = false;
   18290              :       template_args = NULL_TREE;
   18291              :     }
   18292              : 
   18293              :   /* Substitute into the qualifying scope.  When there are no ARGS, we
   18294              :      are just trying to simplify a non-dependent expression.  In that
   18295              :      case the qualifying scope may be dependent, and, in any case,
   18296              :      substituting will not help.  */
   18297     80109979 :   scope = TREE_OPERAND (qualified_id, 0);
   18298     80109979 :   if (args)
   18299              :     {
   18300     78946420 :       scope = tsubst_scope (scope, args, complain, in_decl);
   18301     78946420 :       expr = tsubst_name (name, args, complain, in_decl);
   18302              :     }
   18303              :   else
   18304              :     expr = name;
   18305              : 
   18306     80109979 :   if (dependent_scope_p (scope) || dependent_namespace_p (scope))
   18307              :     {
   18308     10860690 :       if (TREE_CODE (expr) == SCOPE_REF)
   18309              :         /* We built one in tsubst_baselink.  */
   18310            6 :         gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
   18311              :       else
   18312              :         {
   18313     10860684 :           if (is_template)
   18314        11538 :             expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
   18315              :                                      template_args);
   18316     10860684 :           expr = build_qualified_name (NULL_TREE, scope, expr,
   18317     10860684 :                                        QUALIFIED_NAME_IS_TEMPLATE
   18318              :                                        (qualified_id));
   18319              :         }
   18320     10860690 :       REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
   18321     10860690 :       return expr;
   18322              :     }
   18323              : 
   18324     69249289 :   if (!BASELINK_P (name) && !DECL_P (expr))
   18325              :     {
   18326     63265570 :       if (TREE_CODE (expr) == BIT_NOT_EXPR)
   18327              :         {
   18328              :           /* A BIT_NOT_EXPR is used to represent a destructor.  */
   18329            3 :           if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
   18330              :             {
   18331            0 :               error ("qualifying type %qT does not match destructor name ~%qT",
   18332            0 :                      scope, TREE_OPERAND (expr, 0));
   18333            0 :               expr = error_mark_node;
   18334              :             }
   18335              :           else
   18336            3 :             expr = lookup_qualified_name (scope, complete_dtor_identifier,
   18337              :                                           LOOK_want::NORMAL, false);
   18338              :         }
   18339              :       else
   18340     63265567 :         expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
   18341     63265516 :       if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
   18342              :                      ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL
   18343              :           /* For ^^T::X, we'll take both types and non-types.  */
   18344     63265516 :           && !reflecting_p)
   18345              :         {
   18346           30 :           if (complain & tf_error)
   18347              :             {
   18348           24 :               auto_diagnostic_group d;
   18349           24 :               error ("dependent-name %qE is parsed as a non-type, but "
   18350              :                      "instantiation yields a type", qualified_id);
   18351           24 :               inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
   18352           24 :             }
   18353           30 :           return error_mark_node;
   18354              :         }
   18355              :     }
   18356              : 
   18357     69249205 :   if (DECL_P (expr))
   18358              :     {
   18359     64465127 :       if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
   18360              :                                                 scope, complain))
   18361           17 :         return error_mark_node;
   18362              :       /* Remember that there was a reference to this entity.  */
   18363     64465110 :       if (!mark_used (expr, complain) && !(complain & tf_error))
   18364            0 :         return error_mark_node;
   18365              :     }
   18366              : 
   18367     69241097 :   if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
   18368              :     {
   18369        50089 :       if (complain & tf_error)
   18370          151 :         qualified_name_lookup_error (scope,
   18371          151 :                                      TREE_OPERAND (qualified_id, 1),
   18372              :                                      expr, input_location);
   18373        50089 :       return error_mark_node;
   18374              :     }
   18375              : 
   18376     69191008 :   if (is_template)
   18377              :     {
   18378        50679 :       if (variable_template_p (expr))
   18379         9855 :         expr = lookup_and_finish_template_variable (expr, template_args,
   18380              :                                                     complain);
   18381              :       else
   18382        40824 :         expr = lookup_template_function (expr, template_args);
   18383              :     }
   18384              : 
   18385     69191008 :   if (expr == error_mark_node && complain & tf_error)
   18386            3 :     qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
   18387              :                                  expr, input_location);
   18388              :   /* For ^^S::mem, we do not want to create the dummy object that
   18389              :      finish_non_static_data_member would give us.  */
   18390     69191005 :   else if (TYPE_P (scope) && !reflecting_p)
   18391              :     {
   18392     69190965 :       expr = (adjust_result_of_qualified_name_lookup
   18393     69190965 :               (expr, scope, current_nonlambda_class_type ()));
   18394     69190965 :       expr = (finish_qualified_id_expr
   18395     69190971 :               (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
   18396     69190965 :                QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
   18397              :                /*template_arg_p=*/false, complain));
   18398              :     }
   18399              : 
   18400              :   /* Expressions do not generally have reference type.  */
   18401     69191008 :   if (TREE_CODE (expr) != SCOPE_REF
   18402              :       /* However, if we're about to form a pointer-to-member, we just
   18403              :          want the referenced member referenced.  */
   18404     66108239 :       && TREE_CODE (expr) != OFFSET_REF)
   18405     66078716 :     expr = convert_from_reference (expr);
   18406              : 
   18407     69191008 :   if (REF_PARENTHESIZED_P (qualified_id))
   18408          125 :     expr = force_paren_expr (expr);
   18409              : 
   18410     69191008 :   expr = maybe_wrap_with_location (expr, loc);
   18411              : 
   18412     69191008 :   return expr;
   18413              : }
   18414              : 
   18415              : /* tsubst the initializer for a VAR_DECL.  INIT is the unsubstituted
   18416              :    initializer, DECL is the substituted VAR_DECL.  Other arguments are as
   18417              :    for tsubst.  */
   18418              : 
   18419              : static tree
   18420     36473271 : tsubst_init (tree init, tree decl, tree args,
   18421              :              tsubst_flags_t complain, tree in_decl)
   18422              : {
   18423     36473271 :   if (!init)
   18424              :     return NULL_TREE;
   18425              : 
   18426     31041191 :   init = tsubst_expr (init, args, complain, in_decl);
   18427              : 
   18428     31041191 :   tree type = TREE_TYPE (decl);
   18429              : 
   18430     31041191 :   if (!init && type != error_mark_node)
   18431              :     {
   18432           74 :       if (tree auto_node = type_uses_auto (type))
   18433              :         {
   18434            8 :           if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   18435              :             {
   18436            6 :               if (complain & tf_error)
   18437            6 :                 error ("initializer for %q#D expands to an empty list "
   18438              :                        "of expressions", decl);
   18439            6 :               return error_mark_node;
   18440              :             }
   18441              :         }
   18442           66 :       else if (!dependent_type_p (type))
   18443              :         {
   18444              :           /* If we had an initializer but it
   18445              :              instantiated to nothing,
   18446              :              value-initialize the object.  This will
   18447              :              only occur when the initializer was a
   18448              :              pack expansion where the parameter packs
   18449              :              used in that expansion were of length
   18450              :              zero.  */
   18451           66 :           init = build_value_init (type, complain);
   18452           66 :           if (TREE_CODE (init) == AGGR_INIT_EXPR)
   18453           45 :             init = get_target_expr (init, complain);
   18454           66 :           if (TREE_CODE (init) == TARGET_EXPR)
   18455           45 :             TARGET_EXPR_DIRECT_INIT_P (init) = true;
   18456              :         }
   18457              :     }
   18458              : 
   18459              :   return init;
   18460              : }
   18461              : 
   18462              : /* If T is a reference to a dependent member of the current instantiation C and
   18463              :    we are trying to refer to that member in a partial instantiation of C,
   18464              :    return a SCOPE_REF; otherwise, return NULL_TREE.
   18465              : 
   18466              :    This can happen when forming a C++17 deduction guide, as in PR96199.  */
   18467              : 
   18468              : static tree
   18469   7529676906 : maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
   18470              :                             tree in_decl)
   18471              : {
   18472   7529676906 :   if (!(complain & tf_dguide))
   18473              :     return NULL_TREE;
   18474              : 
   18475      1724208 :   tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
   18476      1724208 :   if (!decl || !DECL_P (decl))
   18477              :     return NULL_TREE;
   18478              : 
   18479       969427 :   tree ctx = context_for_name_lookup (decl);
   18480       969427 :   if (!CLASS_TYPE_P (ctx))
   18481              :     return NULL_TREE;
   18482              : 
   18483        48662 :   ctx = tsubst (ctx, args, complain, in_decl);
   18484        48662 :   if (!dependent_scope_p (ctx))
   18485              :     return NULL_TREE;
   18486              : 
   18487        44675 :   if (TYPE_P (t))
   18488              :     {
   18489        26971 :       bool stripped = false;
   18490        26971 :       if (typedef_variant_p (t))
   18491              :         {
   18492              :           /* Since this transformation may undesirably turn a deduced context
   18493              :              into a non-deduced one, we'd rather strip typedefs than perform
   18494              :              the transformation.  */
   18495        14755 :           tree u = strip_typedefs (t);
   18496        14755 :           if (u != t)
   18497              :             {
   18498        26971 :               stripped = true;
   18499        26971 :               t = u;
   18500              :             }
   18501              :         }
   18502        26971 :       decl = TYPE_NAME (t);
   18503        26971 :       if (decl)
   18504        26351 :         decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
   18505        26351 :       if (!decl)
   18506              :         {
   18507        23951 :           if (stripped)
   18508              :             /* The original type was an alias from the current instantiation
   18509              :                which we stripped to something outside it.  At this point we
   18510              :                need to commit to using the stripped type rather than deferring
   18511              :                to the caller (which would use the original type), to ensure
   18512              :                eligible bits of the stripped type get transformed.  */
   18513        12543 :             return tsubst (t, args, complain, in_decl);
   18514              :           else
   18515              :             /* The original type wasn't a typedef, and we decided it doesn't
   18516              :                need rewriting, so just let the caller (tsubst) substitute it
   18517              :                normally.  */
   18518              :             return NULL_TREE;
   18519              :         }
   18520         3020 :       return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
   18521         3020 :                                       complain);
   18522              :     }
   18523              : 
   18524        17704 :   tree name = DECL_NAME (t);
   18525        17704 :   tree fullname = name;
   18526        17704 :   if (instantiates_primary_template_p (t))
   18527              :     {
   18528         2220 :       tree tinfo = get_template_info (t);
   18529         2220 :       name = DECL_NAME (TI_TEMPLATE (tinfo));
   18530         2220 :       tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
   18531         2220 :       targs = tsubst_template_args (targs, args, complain, in_decl);
   18532         2220 :       fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
   18533              :     }
   18534              : 
   18535        17704 :   if (TREE_CODE (t) == TYPE_DECL)
   18536              :     {
   18537        16015 :       if (!is_typedef_decl (t)
   18538        13803 :           && TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
   18539        12995 :           && TYPE_NAME (TREE_TYPE (t)) == t)
   18540              :         /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
   18541              :            scope, but it doesn't need to be rewritten again.  */
   18542              :         return NULL_TREE;
   18543         3020 :       tree type = build_typename_type (ctx, name, fullname, typename_type);
   18544         3020 :       return TYPE_NAME (type);
   18545              :     }
   18546         1689 :   else if (DECL_TYPE_TEMPLATE_P (t))
   18547           24 :     return make_unbound_class_template (ctx, name,
   18548           24 :                                         NULL_TREE, complain);
   18549              :   else
   18550         1665 :     return build_qualified_name (NULL_TREE, ctx, fullname,
   18551         1665 :                                  TREE_CODE (t) == TEMPLATE_DECL);
   18552              : }
   18553              : 
   18554              : /* Helper function for tsubst_omp_clauses, used for instantiation of
   18555              :    OMP_CLAUSE_DECL of clauses.  */
   18556              : 
   18557              : static tree
   18558         6237 : tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
   18559              :                         tree in_decl, tree *iterator_cache)
   18560              : {
   18561         6237 :   if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
   18562              :     return decl;
   18563              : 
   18564              :   /* Handle OpenMP iterators.  */
   18565         6189 :   if (OMP_ITERATOR_DECL_P (decl))
   18566              :     {
   18567          135 :       tree ret;
   18568          135 :       if (iterator_cache[0] == TREE_PURPOSE (decl))
   18569           20 :         ret = iterator_cache[1];
   18570              :       else
   18571              :         {
   18572          115 :           tree *tp = &ret;
   18573          115 :           begin_scope (sk_omp, NULL);
   18574          254 :           for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
   18575              :             {
   18576          139 :               *tp = copy_node (it);
   18577          139 :               TREE_VEC_ELT (*tp, 0)
   18578          139 :                 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
   18579          139 :               DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
   18580          139 :               pushdecl (TREE_VEC_ELT (*tp, 0));
   18581          139 :               TREE_VEC_ELT (*tp, 1)
   18582          139 :                 = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl);
   18583          139 :               TREE_VEC_ELT (*tp, 2)
   18584          139 :                 = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl);
   18585          139 :               TREE_VEC_ELT (*tp, 3)
   18586          139 :                 = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl);
   18587          139 :               TREE_CHAIN (*tp) = NULL_TREE;
   18588          139 :               tp = &TREE_CHAIN (*tp);
   18589              :             }
   18590          115 :           TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
   18591          115 :           iterator_cache[0] = TREE_PURPOSE (decl);
   18592          115 :           iterator_cache[1] = ret;
   18593              :         }
   18594          135 :       return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
   18595              :                                                            args, complain,
   18596              :                                                            in_decl, NULL));
   18597              :     }
   18598              : 
   18599              :   /* Handle an OpenMP array section represented as a TREE_LIST (or
   18600              :      OMP_CLAUSE_DOACROSS_KIND).  An OMP_CLAUSE_DOACROSS (with a depend
   18601              :      kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
   18602              :      TREE_LIST.  We can handle it exactly the same as an array section
   18603              :      (purpose, value, and a chain), even though the nomenclature
   18604              :      (low_bound, length, etc) is different.  */
   18605         6054 :   if (TREE_CODE (decl) == TREE_LIST)
   18606              :     {
   18607           27 :       tree low_bound
   18608           27 :         = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl);
   18609           27 :       tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl);
   18610           27 :       tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
   18611              :                                            in_decl, NULL);
   18612           27 :       if (TREE_PURPOSE (decl) == low_bound
   18613           27 :           && TREE_VALUE (decl) == length
   18614           36 :           && TREE_CHAIN (decl) == chain)
   18615              :         return decl;
   18616           18 :       tree ret = tree_cons (low_bound, length, chain);
   18617           18 :       OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
   18618           18 :         = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
   18619           18 :       return ret;
   18620              :     }
   18621         6027 :   else if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
   18622              :     {
   18623         1855 :       tree low_bound
   18624         1855 :         = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl);
   18625         1855 :       tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain,
   18626              :                                  in_decl);
   18627         1855 :       tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args,
   18628              :                                            complain, in_decl, NULL);
   18629         1855 :       if (TREE_OPERAND (decl, 0) == base
   18630          594 :           && TREE_OPERAND (decl, 1) == low_bound
   18631         2337 :           && TREE_OPERAND (decl, 2) == length)
   18632              :         return decl;
   18633         1382 :       return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound,
   18634         1382 :                      length);
   18635              :     }
   18636         4172 :   tree ret = tsubst_stmt (decl, args, complain, in_decl);
   18637              :   /* Undo convert_from_reference tsubst_expr could have called.  */
   18638         4172 :   if (decl
   18639         4172 :       && REFERENCE_REF_P (ret)
   18640          561 :       && !REFERENCE_REF_P (decl))
   18641          551 :     ret = TREE_OPERAND (ret, 0);
   18642              :   return ret;
   18643              : }
   18644              : 
   18645              : /* Like tsubst_copy, but specifically for OpenMP clauses.  */
   18646              : 
   18647              : static tree
   18648         4793 : tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
   18649              :                     tree args, tsubst_flags_t complain, tree in_decl)
   18650              : {
   18651         4793 :   tree new_clauses = NULL_TREE, nc, oc;
   18652         4793 :   tree linear_no_step = NULL_TREE;
   18653         4793 :   tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
   18654              : 
   18655        10752 :   for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
   18656              :     {
   18657         5959 :       nc = copy_node (oc);
   18658         5959 :       OMP_CLAUSE_CHAIN (nc) = new_clauses;
   18659         5959 :       new_clauses = nc;
   18660              : 
   18661         5959 :       switch (OMP_CLAUSE_CODE (nc))
   18662              :         {
   18663          190 :         case OMP_CLAUSE_LASTPRIVATE:
   18664          190 :           if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
   18665              :             {
   18666           38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
   18667           38 :               tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
   18668              :                            complain, in_decl);
   18669           38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc)
   18670           76 :                 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
   18671              :             }
   18672              :           /* FALLTHRU */
   18673         2932 :         case OMP_CLAUSE_PRIVATE:
   18674         2932 :         case OMP_CLAUSE_SHARED:
   18675         2932 :         case OMP_CLAUSE_FIRSTPRIVATE:
   18676         2932 :         case OMP_CLAUSE_COPYIN:
   18677         2932 :         case OMP_CLAUSE_COPYPRIVATE:
   18678         2932 :         case OMP_CLAUSE_UNIFORM:
   18679         2932 :         case OMP_CLAUSE_DEPEND:
   18680         2932 :         case OMP_CLAUSE_DOACROSS:
   18681         2932 :         case OMP_CLAUSE_AFFINITY:
   18682         2932 :         case OMP_CLAUSE_FROM:
   18683         2932 :         case OMP_CLAUSE_TO:
   18684         2932 :         case OMP_CLAUSE_MAP:
   18685         2932 :         case OMP_CLAUSE__CACHE_:
   18686         2932 :         case OMP_CLAUSE_NONTEMPORAL:
   18687         2932 :         case OMP_CLAUSE_USE_DEVICE_PTR:
   18688         2932 :         case OMP_CLAUSE_USE_DEVICE_ADDR:
   18689         2932 :         case OMP_CLAUSE_IS_DEVICE_PTR:
   18690         2932 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
   18691         2932 :         case OMP_CLAUSE_INCLUSIVE:
   18692         2932 :         case OMP_CLAUSE_EXCLUSIVE:
   18693         5864 :           OMP_CLAUSE_DECL (nc)
   18694         2932 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18695              :                                       in_decl, iterator_cache);
   18696         2932 :           break;
   18697          150 :         case OMP_CLAUSE_NUM_TEAMS:
   18698          150 :           if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
   18699           66 :             OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
   18700          132 :               = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
   18701              :                              complain, in_decl);
   18702              :           /* FALLTHRU */
   18703          762 :         case OMP_CLAUSE_TILE:
   18704          762 :         case OMP_CLAUSE_IF:
   18705          762 :         case OMP_CLAUSE_SELF:
   18706          762 :         case OMP_CLAUSE_NUM_THREADS:
   18707          762 :         case OMP_CLAUSE_SCHEDULE:
   18708          762 :         case OMP_CLAUSE_COLLAPSE:
   18709          762 :         case OMP_CLAUSE_FINAL:
   18710          762 :         case OMP_CLAUSE_DEVICE:
   18711          762 :         case OMP_CLAUSE_DIST_SCHEDULE:
   18712          762 :         case OMP_CLAUSE_THREAD_LIMIT:
   18713          762 :         case OMP_CLAUSE_SAFELEN:
   18714          762 :         case OMP_CLAUSE_SIMDLEN:
   18715          762 :         case OMP_CLAUSE_NUM_TASKS:
   18716          762 :         case OMP_CLAUSE_GRAINSIZE:
   18717          762 :         case OMP_CLAUSE_PRIORITY:
   18718          762 :         case OMP_CLAUSE_ORDERED:
   18719          762 :         case OMP_CLAUSE_HINT:
   18720          762 :         case OMP_CLAUSE_FILTER:
   18721          762 :         case OMP_CLAUSE_NUM_GANGS:
   18722          762 :         case OMP_CLAUSE_NUM_WORKERS:
   18723          762 :         case OMP_CLAUSE_VECTOR_LENGTH:
   18724          762 :         case OMP_CLAUSE_WORKER:
   18725          762 :         case OMP_CLAUSE_VECTOR:
   18726          762 :         case OMP_CLAUSE_ASYNC:
   18727          762 :         case OMP_CLAUSE_WAIT:
   18728          762 :         case OMP_CLAUSE_DETACH:
   18729          762 :         case OMP_CLAUSE_DYN_GROUPPRIVATE:
   18730          762 :           OMP_CLAUSE_OPERAND (nc, 0)
   18731          762 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18732          762 :           break;
   18733           18 :         case OMP_CLAUSE_PARTIAL:
   18734           18 :           OMP_CLAUSE_PARTIAL_EXPR (nc)
   18735           18 :             = tsubst_expr (OMP_CLAUSE_PARTIAL_EXPR (oc), args, complain,
   18736              :                            in_decl);
   18737           18 :           break;
   18738           36 :         case OMP_CLAUSE_SIZES:
   18739           36 :           OMP_CLAUSE_SIZES_LIST (nc)
   18740           36 :             = tsubst_expr (OMP_CLAUSE_SIZES_LIST (oc), args, complain,
   18741              :                            in_decl);
   18742           36 :           break;
   18743            6 :         case OMP_CLAUSE_NOCONTEXT:
   18744            6 :           OMP_CLAUSE_NOCONTEXT_EXPR (nc)
   18745            6 :             = tsubst_expr (OMP_CLAUSE_NOCONTEXT_EXPR (oc), args, complain,
   18746              :                            in_decl);
   18747            6 :           break;
   18748            6 :         case OMP_CLAUSE_NOVARIANTS:
   18749            6 :           OMP_CLAUSE_NOVARIANTS_EXPR (nc)
   18750            6 :             = tsubst_expr (OMP_CLAUSE_NOVARIANTS_EXPR (oc), args, complain,
   18751              :                            in_decl);
   18752            6 :           break;
   18753          890 :         case OMP_CLAUSE_REDUCTION:
   18754          890 :         case OMP_CLAUSE_IN_REDUCTION:
   18755          890 :         case OMP_CLAUSE_TASK_REDUCTION:
   18756          890 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
   18757              :             {
   18758           45 :               tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
   18759           45 :               if (TREE_CODE (placeholder) == SCOPE_REF)
   18760              :                 {
   18761            5 :                   tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
   18762              :                                        complain, in_decl);
   18763            5 :                   OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
   18764           10 :                     = build_qualified_name (NULL_TREE, scope,
   18765            5 :                                             TREE_OPERAND (placeholder, 1),
   18766              :                                             false);
   18767              :                 }
   18768              :               else
   18769           40 :                 gcc_assert (identifier_p (placeholder));
   18770              :             }
   18771         1780 :           OMP_CLAUSE_DECL (nc)
   18772          890 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18773              :                                       in_decl, NULL);
   18774          890 :           break;
   18775           66 :         case OMP_CLAUSE_GANG:
   18776           66 :         case OMP_CLAUSE_ALIGNED:
   18777          132 :           OMP_CLAUSE_DECL (nc)
   18778           66 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18779              :                                       in_decl, NULL);
   18780           66 :           OMP_CLAUSE_OPERAND (nc, 1)
   18781           66 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18782           66 :           break;
   18783          205 :         case OMP_CLAUSE_ALLOCATE:
   18784          410 :           OMP_CLAUSE_DECL (nc)
   18785          205 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18786              :                                       in_decl, NULL);
   18787          205 :           OMP_CLAUSE_OPERAND (nc, 1)
   18788          205 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18789          205 :           OMP_CLAUSE_OPERAND (nc, 2)
   18790          205 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   18791          205 :           break;
   18792          127 :         case OMP_CLAUSE_LINEAR:
   18793          254 :           OMP_CLAUSE_DECL (nc)
   18794          127 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18795              :                                       in_decl, NULL);
   18796          127 :           if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
   18797              :             {
   18798            2 :               gcc_assert (!linear_no_step);
   18799              :               linear_no_step = nc;
   18800              :             }
   18801          125 :           else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
   18802            0 :             OMP_CLAUSE_LINEAR_STEP (nc)
   18803            0 :               = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
   18804              :                                         complain, in_decl, NULL);
   18805              :           else
   18806          125 :             OMP_CLAUSE_LINEAR_STEP (nc)
   18807          250 :               = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args,
   18808              :                              complain, in_decl);
   18809              :           break;
   18810           18 :         case OMP_CLAUSE_USES_ALLOCATORS:
   18811           18 :           OMP_CLAUSE_OPERAND (nc, 0)
   18812           18 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18813           18 :           OMP_CLAUSE_OPERAND (nc, 1)
   18814           18 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18815           18 :           OMP_CLAUSE_OPERAND (nc, 2)
   18816           18 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   18817           18 :           break;
   18818          138 :         case OMP_CLAUSE_INIT:
   18819          138 :           if (ort == C_ORT_OMP_INTEROP
   18820          138 :               && OMP_CLAUSE_INIT_PREFER_TYPE (nc)
   18821           66 :               && TREE_CODE (OMP_CLAUSE_INIT_PREFER_TYPE (nc)) == TREE_LIST
   18822          192 :               && (OMP_CLAUSE_CHAIN (nc)  == NULL_TREE
   18823           42 :                   || OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (nc) ) != OMP_CLAUSE_INIT
   18824           36 :                   || (OMP_CLAUSE_INIT_PREFER_TYPE (nc)
   18825           36 :                       != OMP_CLAUSE_INIT_PREFER_TYPE (OMP_CLAUSE_CHAIN (nc) ))))
   18826              :             {
   18827           30 :               tree pref_list = OMP_CLAUSE_INIT_PREFER_TYPE (nc);
   18828           30 :               tree fr_list = TREE_VALUE (pref_list);
   18829           30 :               int len = TREE_VEC_LENGTH (fr_list);
   18830          144 :               for (int i = 0; i < len; i++)
   18831              :                 {
   18832          114 :                   tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
   18833              :                   /* Preserve NOP_EXPR to have a location.  */
   18834          114 :                   if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
   18835           42 :                     TREE_OPERAND (*fr_expr, 0)
   18836           84 :                       = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
   18837              :                                      in_decl);
   18838              :                   else
   18839           72 :                     *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
   18840              :                 }
   18841              :             }
   18842              :           /* FALLTHRU */
   18843          192 :         case OMP_CLAUSE_DESTROY:
   18844          192 :         case OMP_CLAUSE_USE:
   18845          192 :         case OMP_CLAUSE_INTEROP:
   18846          192 :           OMP_CLAUSE_OPERAND (nc, 0)
   18847          192 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18848          192 :           break;
   18849              :         case OMP_CLAUSE_NOWAIT:
   18850              :         case OMP_CLAUSE_DEFAULT:
   18851              :         case OMP_CLAUSE_UNTIED:
   18852              :         case OMP_CLAUSE_MERGEABLE:
   18853              :         case OMP_CLAUSE_INBRANCH:
   18854              :         case OMP_CLAUSE_NOTINBRANCH:
   18855              :         case OMP_CLAUSE_PROC_BIND:
   18856              :         case OMP_CLAUSE_FOR:
   18857              :         case OMP_CLAUSE_PARALLEL:
   18858              :         case OMP_CLAUSE_SECTIONS:
   18859              :         case OMP_CLAUSE_TASKGROUP:
   18860              :         case OMP_CLAUSE_NOGROUP:
   18861              :         case OMP_CLAUSE_THREADS:
   18862              :         case OMP_CLAUSE_SIMD:
   18863              :         case OMP_CLAUSE_DEFAULTMAP:
   18864              :         case OMP_CLAUSE_ORDER:
   18865              :         case OMP_CLAUSE_BIND:
   18866              :         case OMP_CLAUSE_INDEPENDENT:
   18867              :         case OMP_CLAUSE_AUTO:
   18868              :         case OMP_CLAUSE_SEQ:
   18869              :         case OMP_CLAUSE_IF_PRESENT:
   18870              :         case OMP_CLAUSE_FINALIZE:
   18871              :         case OMP_CLAUSE_NOHOST:
   18872              :         case OMP_CLAUSE_FULL:
   18873              :           break;
   18874            0 :         default:
   18875            0 :           gcc_unreachable ();
   18876              :         }
   18877         5959 :       if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
   18878         5066 :         switch (OMP_CLAUSE_CODE (nc))
   18879              :           {
   18880         1850 :           case OMP_CLAUSE_SHARED:
   18881         1850 :           case OMP_CLAUSE_PRIVATE:
   18882         1850 :           case OMP_CLAUSE_FIRSTPRIVATE:
   18883         1850 :           case OMP_CLAUSE_LASTPRIVATE:
   18884         1850 :           case OMP_CLAUSE_COPYPRIVATE:
   18885         1850 :           case OMP_CLAUSE_LINEAR:
   18886         1850 :           case OMP_CLAUSE_REDUCTION:
   18887         1850 :           case OMP_CLAUSE_IN_REDUCTION:
   18888         1850 :           case OMP_CLAUSE_TASK_REDUCTION:
   18889         1850 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   18890         1850 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   18891         1850 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   18892         1850 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   18893         1850 :           case OMP_CLAUSE_INCLUSIVE:
   18894         1850 :           case OMP_CLAUSE_EXCLUSIVE:
   18895         1850 :           case OMP_CLAUSE_ALLOCATE:
   18896              :             /* tsubst_expr on SCOPE_REF results in returning
   18897              :                finish_non_static_data_member result.  Undo that here.  */
   18898         1850 :             if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
   18899         1850 :                 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
   18900              :                     == IDENTIFIER_NODE))
   18901              :               {
   18902           79 :                 tree t = OMP_CLAUSE_DECL (nc);
   18903           79 :                 tree v = t;
   18904          464 :                 while (v)
   18905          464 :                   switch (TREE_CODE (v))
   18906              :                     {
   18907          385 :                     case COMPONENT_REF:
   18908          385 :                     case MEM_REF:
   18909          385 :                     case INDIRECT_REF:
   18910          385 :                     CASE_CONVERT:
   18911          385 :                     case POINTER_PLUS_EXPR:
   18912          385 :                       v = TREE_OPERAND (v, 0);
   18913          385 :                       continue;
   18914           77 :                     case PARM_DECL:
   18915           77 :                       if (DECL_CONTEXT (v) == current_function_decl
   18916           77 :                           && DECL_ARTIFICIAL (v)
   18917          154 :                           && DECL_NAME (v) == this_identifier)
   18918           77 :                         OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
   18919              :                       /* FALLTHRU */
   18920              :                     default:
   18921              :                       v = NULL_TREE;
   18922              :                       break;
   18923              :                     }
   18924              :               }
   18925         1771 :             else if (VAR_P (OMP_CLAUSE_DECL (oc))
   18926          913 :                      && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
   18927          197 :                      && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
   18928          197 :                      && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
   18929         1968 :                      && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
   18930              :               {
   18931          197 :                 tree decl = OMP_CLAUSE_DECL (nc);
   18932          197 :                 if (VAR_P (decl))
   18933              :                   {
   18934          197 :                     retrofit_lang_decl (decl);
   18935          197 :                     DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
   18936              :                   }
   18937              :               }
   18938              :             break;
   18939              :           default:
   18940              :             break;
   18941              :           }
   18942              :     }
   18943              : 
   18944         4793 :   new_clauses = nreverse (new_clauses);
   18945         4793 :   if (ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_OMP_DECLARE_MAPPER)
   18946              :     {
   18947         4686 :       if (ort & C_ORT_OMP)
   18948         3979 :         new_clauses = c_omp_instantiate_mappers (new_clauses, ort);
   18949         4686 :       new_clauses = finish_omp_clauses (new_clauses, ort);
   18950         4686 :       if (linear_no_step)
   18951            2 :         for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
   18952            2 :           if (nc == linear_no_step)
   18953              :             {
   18954            2 :               OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
   18955            2 :               break;
   18956              :             }
   18957              :     }
   18958         4793 :   return new_clauses;
   18959              : }
   18960              : 
   18961              : /* Like tsubst_copy, but specifically for OpenMP context selectors.  */
   18962              : static tree
   18963           58 : tsubst_omp_context_selector (tree ctx, tree args, tsubst_flags_t complain,
   18964              :                              tree in_decl)
   18965              : {
   18966           58 :   tree new_ctx = NULL_TREE;
   18967          128 :   for (tree set = ctx; set; set = TREE_CHAIN (set))
   18968              :     {
   18969           70 :       tree selectors = NULL_TREE;
   18970          140 :       for (tree sel = OMP_TSS_TRAIT_SELECTORS (set); sel;
   18971           70 :            sel = TREE_CHAIN (sel))
   18972              :         {
   18973           70 :           enum omp_ts_code code = OMP_TS_CODE (sel);
   18974           70 :           tree properties = NULL_TREE;
   18975           70 :           tree score = OMP_TS_SCORE (sel);
   18976            6 :           tree t;
   18977              : 
   18978            6 :           if (score)
   18979              :             {
   18980            6 :               score = tsubst_expr (score, args, complain, in_decl);
   18981            6 :               score = fold_non_dependent_expr (score);
   18982            6 :               if (!value_dependent_expression_p (score)
   18983            6 :                   && !type_dependent_expression_p (score))
   18984              :                 {
   18985           12 :                   if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
   18986           12 :                       || TREE_CODE (score) != INTEGER_CST)
   18987              :                     {
   18988            0 :                       error_at (cp_expr_loc_or_input_loc (score),
   18989              :                                 "%<score%> argument must "
   18990              :                                 "be constant integer expression");
   18991            0 :                       score = NULL_TREE;
   18992              :                     }
   18993            6 :                   else if (tree_int_cst_sgn (score) < 0)
   18994              :                     {
   18995            0 :                       error_at (cp_expr_loc_or_input_loc (score),
   18996              :                                 "%<score%> argument must be non-negative");
   18997            0 :                       score = NULL_TREE;
   18998              :                     }
   18999              :                 }
   19000              :             }
   19001              : 
   19002           70 :           enum omp_tp_type property_kind
   19003           70 :             = omp_ts_map[OMP_TS_CODE (sel)].tp_type;
   19004           70 :           switch (property_kind)
   19005              :               {
   19006           70 :               case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
   19007           70 :               case OMP_TRAIT_PROPERTY_BOOL_EXPR:
   19008          140 :                 t = tsubst_expr (OMP_TP_VALUE (OMP_TS_PROPERTIES (sel)),
   19009              :                                  args, complain, in_decl);
   19010           70 :                 t = fold_non_dependent_expr (t);
   19011           70 :                 if (!value_dependent_expression_p (t)
   19012           70 :                     && !type_dependent_expression_p (t))
   19013              :                   {
   19014           70 :                     if (property_kind == OMP_TRAIT_PROPERTY_BOOL_EXPR)
   19015           58 :                       t = maybe_convert_cond (t);
   19016              :                     else
   19017              :                       {
   19018           12 :                         t = convert_from_reference (t);
   19019           12 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
   19020              :                           {
   19021            0 :                             error_at (cp_expr_loc_or_input_loc (t),
   19022              :                                       "property must be integer expression");
   19023            0 :                             t = error_mark_node;
   19024              :                           }
   19025              :                       }
   19026              :                   }
   19027           70 :                 if (t != error_mark_node
   19028           70 :                     && !processing_template_decl
   19029           70 :                     && TREE_CODE (t) != CLEANUP_POINT_EXPR)
   19030           55 :                   t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   19031           70 :                 properties = make_trait_property (NULL_TREE, t, NULL_TREE);
   19032           70 :                 break;
   19033            0 :               case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
   19034            0 :                 if (OMP_TS_CODE (sel) == OMP_TRAIT_CONSTRUCT_SIMD)
   19035            0 :                   properties = tsubst_omp_clauses (OMP_TS_PROPERTIES (sel),
   19036              :                                                    C_ORT_OMP_DECLARE_SIMD,
   19037              :                                                    args, complain, in_decl);
   19038              :                 break;
   19039            0 :               default:
   19040              :                 /* Nothing to do here, just copy.  */
   19041            0 :                 for (tree prop = OMP_TS_PROPERTIES (sel);
   19042            0 :                      prop; prop = TREE_CHAIN (prop))
   19043            0 :                   properties = make_trait_property (OMP_TP_NAME (prop),
   19044            0 :                                                     OMP_TP_VALUE (prop),
   19045              :                                                     properties);
   19046              :               }
   19047           70 :           selectors = make_trait_selector (code, score, properties, selectors);
   19048              :         }
   19049           70 :       new_ctx = make_trait_set_selector (OMP_TSS_CODE (set),
   19050              :                                          nreverse (selectors),
   19051              :                                          new_ctx);
   19052              :     }
   19053           58 :   return nreverse (new_ctx);
   19054              : }
   19055              : 
   19056              : 
   19057              : /* Like tsubst_expr, but unshare TREE_LIST nodes.  */
   19058              : 
   19059              : static tree
   19060         9558 : tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
   19061              :                           tree in_decl)
   19062              : {
   19063              : #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
   19064              : 
   19065         9558 :   tree purpose, value, chain;
   19066              : 
   19067         9558 :   if (t == NULL)
   19068              :     return t;
   19069              : 
   19070         7379 :   if (TREE_CODE (t) != TREE_LIST)
   19071         3805 :     return tsubst_expr (t, args, complain, in_decl);
   19072              : 
   19073         3574 :   if (t == void_list_node)
   19074              :     return t;
   19075              : 
   19076         3574 :   purpose = TREE_PURPOSE (t);
   19077         3574 :   if (purpose)
   19078         1872 :     purpose = RECUR (purpose);
   19079         3574 :   value = TREE_VALUE (t);
   19080         3574 :   if (value)
   19081              :     {
   19082         3574 :       if (TREE_CODE (value) != LABEL_DECL)
   19083         3571 :         value = RECUR (value);
   19084              :       else
   19085              :         {
   19086            3 :           value = lookup_label (DECL_NAME (value));
   19087            3 :           gcc_assert (TREE_CODE (value) == LABEL_DECL);
   19088            3 :           TREE_USED (value) = 1;
   19089              :         }
   19090              :     }
   19091         3574 :   chain = TREE_CHAIN (t);
   19092         3574 :   if (chain && chain != void_type_node)
   19093         1039 :     chain = RECUR (chain);
   19094         3574 :   return tree_cons (purpose, value, chain);
   19095              : #undef RECUR
   19096              : }
   19097              : 
   19098              : /* Used to temporarily communicate the list of #pragma omp parallel
   19099              :    clauses to #pragma omp for instantiation if they are combined
   19100              :    together.  */
   19101              : 
   19102              : static tree *omp_parallel_combined_clauses;
   19103              : 
   19104              : static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
   19105              :                                  cp_decomp *);
   19106              : 
   19107              : /* Substitute one OMP_FOR iterator.  */
   19108              : 
   19109              : static bool
   19110         1253 : tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
   19111              :                          tree initv, tree condv, tree incrv, tree *clauses,
   19112              :                          tree args, tsubst_flags_t complain, tree in_decl)
   19113              : {
   19114              : #define RECUR(NODE)                             \
   19115              :   tsubst_stmt ((NODE), args, complain, in_decl)
   19116         1253 :   tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
   19117         1253 :   bool ret = false;
   19118              : 
   19119         1253 :   init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
   19120         1253 :   gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
   19121              : 
   19122         1253 :   decl = TREE_OPERAND (init, 0);
   19123         1253 :   init = TREE_OPERAND (init, 1);
   19124         1253 :   tree decl_expr = NULL_TREE;
   19125         1253 :   bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
   19126         1253 :   if (range_for)
   19127              :     {
   19128          164 :       bool decomp = false;
   19129          164 :       if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
   19130              :         {
   19131           41 :           tree v = DECL_VALUE_EXPR (decl);
   19132           41 :           if ((TREE_CODE (v) == ARRAY_REF
   19133           41 :                && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
   19134           41 :               || (TREE_CODE (v) == TREE_VEC
   19135            0 :                   && DECL_DECOMPOSITION_P (TREE_VEC_ELT (v, 0))))
   19136              :             {
   19137           41 :               v = (TREE_CODE (v) == ARRAY_REF
   19138           41 :                    ? TREE_OPERAND (v, 0) : TREE_VEC_ELT (v, 0));
   19139           41 :               cp_decomp decomp_d = { NULL_TREE, 0 };
   19140           41 :               tree d = tsubst_decl (v, args, complain);
   19141           41 :               maybe_push_decl (d);
   19142           41 :               d = tsubst_decomp_names (d, v, args, complain,
   19143              :                                        in_decl, &decomp_d);
   19144           41 :               decomp = true;
   19145           41 :               if (d == error_mark_node)
   19146            0 :                 decl = error_mark_node;
   19147              :               else
   19148          155 :                 for (unsigned int i = 0; i < decomp_d.count; i++)
   19149              :                   {
   19150          114 :                     if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl))
   19151              :                       {
   19152          114 :                         tree v = build_nt (ARRAY_REF, d,
   19153          114 :                                            size_int (decomp_d.count - i - 1),
   19154              :                                            NULL_TREE, NULL_TREE);
   19155          114 :                         SET_DECL_VALUE_EXPR (decomp_d.decl, v);
   19156          114 :                         DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1;
   19157              :                       }
   19158          114 :                     fit_decomposition_lang_decl (decomp_d.decl, d);
   19159          114 :                     decomp_d.decl = DECL_CHAIN (decomp_d.decl);
   19160              :                   }
   19161              :             }
   19162              :         }
   19163          164 :       decl = tsubst_decl (decl, args, complain);
   19164          164 :       if (!decomp)
   19165          123 :         maybe_push_decl (decl);
   19166              :     }
   19167         1089 :   else if (init && TREE_CODE (init) == DECL_EXPR)
   19168              :     {
   19169              :       /* We need to jump through some hoops to handle declarations in the
   19170              :          init-statement, since we might need to handle auto deduction,
   19171              :          but we need to keep control of initialization.  */
   19172          223 :       decl_expr = init;
   19173          223 :       init = DECL_INITIAL (DECL_EXPR_DECL (init));
   19174          223 :       decl = tsubst_decl (decl, args, complain);
   19175              :     }
   19176              :   else
   19177              :     {
   19178          866 :       if (TREE_CODE (decl) == SCOPE_REF)
   19179              :         {
   19180            8 :           decl = RECUR (decl);
   19181            8 :           if (TREE_CODE (decl) == COMPONENT_REF)
   19182              :             {
   19183              :               tree v = decl;
   19184           24 :               while (v)
   19185           24 :                 switch (TREE_CODE (v))
   19186              :                   {
   19187           20 :                   case COMPONENT_REF:
   19188           20 :                   case MEM_REF:
   19189           20 :                   case INDIRECT_REF:
   19190           20 :                   CASE_CONVERT:
   19191           20 :                   case POINTER_PLUS_EXPR:
   19192           20 :                     v = TREE_OPERAND (v, 0);
   19193           20 :                     continue;
   19194            4 :                   case PARM_DECL:
   19195            4 :                     if (DECL_CONTEXT (v) == current_function_decl
   19196            4 :                         && DECL_ARTIFICIAL (v)
   19197            8 :                         && DECL_NAME (v) == this_identifier)
   19198              :                       {
   19199            4 :                         decl = TREE_OPERAND (decl, 1);
   19200            4 :                         decl = omp_privatize_field (decl, false);
   19201              :                       }
   19202              :                     /* FALLTHRU */
   19203              :                   default:
   19204              :                     v = NULL_TREE;
   19205              :                     break;
   19206              :                   }
   19207              :             }
   19208              :         }
   19209              :       else
   19210          858 :         decl = RECUR (decl);
   19211              :     }
   19212         1253 :   if (init && TREE_CODE (init) == TREE_VEC)
   19213              :     {
   19214           12 :       init = copy_node (init);
   19215           12 :       TREE_VEC_ELT (init, 0)
   19216           12 :         = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
   19217           12 :       TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
   19218           12 :       TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
   19219              :     }
   19220              :   else
   19221         1241 :     init = RECUR (init);
   19222              : 
   19223         1253 :   if (orig_declv && OMP_FOR_ORIG_DECLS (t))
   19224              :     {
   19225          618 :       tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   19226          618 :       if (TREE_CODE (o) == TREE_LIST)
   19227           92 :         TREE_VEC_ELT (orig_declv, i)
   19228          184 :           = tree_cons (RECUR (TREE_PURPOSE (o)),
   19229           92 :                        RECUR (TREE_VALUE (o)),
   19230              :                        NULL_TREE);
   19231              :       else
   19232          526 :         TREE_VEC_ELT (orig_declv, i) = RECUR (o);
   19233              :     }
   19234              : 
   19235         1253 :   if (range_for)
   19236              :     {
   19237          164 :       tree this_pre_body = NULL_TREE;
   19238          164 :       tree orig_init = NULL_TREE;
   19239          164 :       tree orig_decl = NULL_TREE;
   19240          164 :       tree init_sl = NULL_TREE;
   19241          164 :       cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
   19242              :                                 orig_init, cond, incr, true);
   19243          164 :       if (orig_decl)
   19244              :         {
   19245          159 :           if (orig_declv == NULL_TREE)
   19246          143 :             orig_declv = copy_node (declv);
   19247          159 :           TREE_VEC_ELT (orig_declv, i) = orig_decl;
   19248          159 :           ret = true;
   19249              :         }
   19250            5 :       else if (orig_declv)
   19251            0 :         TREE_VEC_ELT (orig_declv, i) = decl;
   19252              :     }
   19253              : 
   19254         1253 :   tree auto_node = type_uses_auto (TREE_TYPE (decl));
   19255         1253 :   if (!range_for && auto_node && init)
   19256            2 :     TREE_TYPE (decl)
   19257            4 :       = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
   19258              : 
   19259         1253 :   gcc_assert (!type_dependent_expression_p (decl));
   19260              : 
   19261         1253 :   if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
   19262              :     {
   19263         1058 :       if (decl_expr)
   19264              :         {
   19265              :           /* Declare the variable, but don't let that initialize it.  */
   19266          172 :           tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
   19267          172 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
   19268          172 :           RECUR (decl_expr);
   19269          172 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
   19270              :         }
   19271              : 
   19272         1058 :       if (!range_for)
   19273              :         {
   19274          894 :           cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   19275          894 :           if (COMPARISON_CLASS_P (cond)
   19276          894 :               && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
   19277              :             {
   19278           12 :               tree lhs = RECUR (TREE_OPERAND (cond, 0));
   19279           12 :               tree rhs = copy_node (TREE_OPERAND (cond, 1));
   19280           12 :               TREE_VEC_ELT (rhs, 0)
   19281           12 :                 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
   19282           12 :               TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
   19283           12 :               TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
   19284           12 :               cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
   19285              :                              lhs, rhs);
   19286              :             }
   19287              :           else
   19288          882 :             cond = RECUR (cond);
   19289          894 :           incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   19290          894 :           if (TREE_CODE (incr) == MODIFY_EXPR)
   19291              :             {
   19292          211 :               tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19293          211 :               tree rhs = RECUR (TREE_OPERAND (incr, 1));
   19294          211 :               incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
   19295              :                                           NOP_EXPR, rhs, NULL_TREE, complain);
   19296              :             }
   19297              :           else
   19298          683 :             incr = RECUR (incr);
   19299          894 :           if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   19300           18 :             TREE_VEC_ELT (orig_declv, i) = decl;
   19301              :         }
   19302         1058 :       TREE_VEC_ELT (declv, i) = decl;
   19303         1058 :       TREE_VEC_ELT (initv, i) = init;
   19304         1058 :       TREE_VEC_ELT (condv, i) = cond;
   19305         1058 :       TREE_VEC_ELT (incrv, i) = incr;
   19306         1058 :       return ret;
   19307              :     }
   19308              : 
   19309          195 :   if (decl_expr)
   19310              :     {
   19311              :       /* Declare and initialize the variable.  */
   19312           51 :       RECUR (decl_expr);
   19313           51 :       init = NULL_TREE;
   19314              :     }
   19315          144 :   else if (init)
   19316              :     {
   19317           61 :       tree *pc;
   19318           61 :       int j;
   19319          109 :       for (j = ((omp_parallel_combined_clauses == NULL
   19320          170 :                 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
   19321              :         {
   19322          210 :           for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
   19323              :             {
   19324           80 :               if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
   19325           80 :                   && OMP_CLAUSE_DECL (*pc) == decl)
   19326              :                 break;
   19327           76 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
   19328           76 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19329              :                 {
   19330           31 :                   if (j)
   19331              :                     break;
   19332              :                   /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
   19333            1 :                   tree c = *pc;
   19334            1 :                   *pc = OMP_CLAUSE_CHAIN (c);
   19335            1 :                   OMP_CLAUSE_CHAIN (c) = *clauses;
   19336            1 :                   *clauses = c;
   19337              :                 }
   19338           45 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
   19339           45 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19340              :                 {
   19341            0 :                   error ("iteration variable %qD should not be firstprivate",
   19342              :                          decl);
   19343            0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   19344              :                 }
   19345           45 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
   19346           45 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19347              :                 {
   19348            0 :                   error ("iteration variable %qD should not be reduction",
   19349              :                          decl);
   19350            0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   19351              :                 }
   19352              :               else
   19353           45 :                 pc = &OMP_CLAUSE_CHAIN (*pc);
   19354              :             }
   19355           82 :           if (*pc)
   19356              :             break;
   19357              :         }
   19358           61 :       if (*pc == NULL_TREE)
   19359              :         {
   19360           27 :           tree c = build_omp_clause (input_location,
   19361           27 :                                      TREE_CODE (t) == OMP_LOOP
   19362              :                                      ? OMP_CLAUSE_LASTPRIVATE
   19363              :                                      : OMP_CLAUSE_PRIVATE);
   19364           27 :           OMP_CLAUSE_DECL (c) = decl;
   19365           27 :           c = finish_omp_clauses (c, C_ORT_OMP);
   19366           27 :           if (c)
   19367              :             {
   19368           27 :               OMP_CLAUSE_CHAIN (c) = *clauses;
   19369           27 :               *clauses = c;
   19370              :             }
   19371              :         }
   19372              :     }
   19373          195 :   cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   19374          195 :   if (COMPARISON_CLASS_P (cond))
   19375              :     {
   19376          195 :       tree op0 = RECUR (TREE_OPERAND (cond, 0));
   19377          195 :       tree op1 = RECUR (TREE_OPERAND (cond, 1));
   19378          195 :       cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
   19379              :     }
   19380              :   else
   19381            0 :     cond = RECUR (cond);
   19382          195 :   incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   19383          195 :   switch (TREE_CODE (incr))
   19384              :     {
   19385           85 :     case PREINCREMENT_EXPR:
   19386           85 :     case PREDECREMENT_EXPR:
   19387           85 :     case POSTINCREMENT_EXPR:
   19388           85 :     case POSTDECREMENT_EXPR:
   19389           85 :       incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
   19390           85 :                      RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
   19391           85 :       break;
   19392          110 :     case MODIFY_EXPR:
   19393          110 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   19394          110 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   19395              :         {
   19396          110 :           tree rhs = TREE_OPERAND (incr, 1);
   19397          110 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19398          110 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   19399          110 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   19400          110 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19401          110 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   19402              :                                  rhs0, rhs1));
   19403              :         }
   19404              :       else
   19405            0 :         incr = RECUR (incr);
   19406              :       break;
   19407            0 :     case MODOP_EXPR:
   19408            0 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   19409            0 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   19410              :         {
   19411            0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19412            0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19413            0 :                          build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
   19414            0 :                                  TREE_TYPE (decl), lhs,
   19415            0 :                                  RECUR (TREE_OPERAND (incr, 2))));
   19416              :         }
   19417            0 :       else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
   19418            0 :                && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
   19419            0 :                    || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
   19420              :         {
   19421            0 :           tree rhs = TREE_OPERAND (incr, 2);
   19422            0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19423            0 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   19424            0 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   19425            0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19426            0 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   19427              :                                  rhs0, rhs1));
   19428              :         }
   19429              :       else
   19430            0 :         incr = RECUR (incr);
   19431              :       break;
   19432            0 :     default:
   19433            0 :       incr = RECUR (incr);
   19434            0 :       break;
   19435              :     }
   19436              : 
   19437          195 :   if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   19438            5 :     TREE_VEC_ELT (orig_declv, i) = decl;
   19439          195 :   TREE_VEC_ELT (declv, i) = decl;
   19440          195 :   TREE_VEC_ELT (initv, i) = init;
   19441          195 :   TREE_VEC_ELT (condv, i) = cond;
   19442          195 :   TREE_VEC_ELT (incrv, i) = incr;
   19443          195 :   return false;
   19444              : #undef RECUR
   19445              : }
   19446              : 
   19447              : /* Helper function of tsubst_expr, find OMP_TEAMS inside
   19448              :    of OMP_TARGET's body.  */
   19449              : 
   19450              : static tree
   19451          164 : tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
   19452              : {
   19453          164 :   *walk_subtrees = 0;
   19454          164 :   switch (TREE_CODE (*tp))
   19455              :     {
   19456              :     case OMP_TEAMS:
   19457              :       return *tp;
   19458           82 :     case BIND_EXPR:
   19459           82 :     case STATEMENT_LIST:
   19460           82 :       *walk_subtrees = 1;
   19461           82 :       break;
   19462              :     default:
   19463              :       break;
   19464              :     }
   19465              :   return NULL_TREE;
   19466              : }
   19467              : 
   19468              : /* Helper function for tsubst_expr.  For decomposition declaration
   19469              :    artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
   19470              :    also the corresponding decls representing the identifiers
   19471              :    of the decomposition declaration.  Return DECL if successful
   19472              :    or error_mark_node otherwise, set *FIRST to the first decl
   19473              :    in the list chained through DECL_CHAIN and *CNT to the number
   19474              :    of such decls.  */
   19475              : 
   19476              : static tree
   19477        36612 : tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
   19478              :                      tsubst_flags_t complain, tree in_decl, cp_decomp *decomp)
   19479              : {
   19480        36612 :   tree decl2, decl3, prev = decl;
   19481        36612 :   decomp->count = 0;
   19482        36612 :   gcc_assert (DECL_NAME (decl) == NULL_TREE);
   19483        36612 :   for (decl2 = DECL_CHAIN (pattern_decl);
   19484              :        decl2
   19485        71950 :        && DECL_DECOMPOSITION_P (decl2)
   19486       178308 :        && DECL_NAME (decl2);
   19487        70561 :        decl2 = DECL_CHAIN (decl2))
   19488              :     {
   19489        70570 :       if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0)
   19490              :         {
   19491            9 :           gcc_assert (errorcount);
   19492              :           return error_mark_node;
   19493              :         }
   19494        70561 :       decomp->count++;
   19495        70561 :       gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
   19496        70561 :       gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
   19497        70561 :       tree v = DECL_VALUE_EXPR (decl2);
   19498        70561 :       DECL_HAS_VALUE_EXPR_P (decl2) = 0;
   19499        70561 :       SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
   19500        70561 :       decl3 = tsubst (decl2, args, complain, in_decl);
   19501        70561 :       SET_DECL_VALUE_EXPR (decl2, v);
   19502        70561 :       DECL_HAS_VALUE_EXPR_P (decl2) = 1;
   19503        70561 :       if (VAR_P (decl3))
   19504        70561 :         DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
   19505              :       else
   19506              :         {
   19507            0 :           gcc_assert (errorcount);
   19508            0 :           decl = error_mark_node;
   19509            0 :           continue;
   19510              :         }
   19511        70561 :       maybe_push_decl (decl3);
   19512        70561 :       if (error_operand_p (decl3))
   19513            0 :         decl = error_mark_node;
   19514        70561 :       else if (decl != error_mark_node
   19515        70561 :                && DECL_CHAIN (decl3) != prev
   19516        70561 :                && decl != prev)
   19517              :         {
   19518            0 :           gcc_assert (errorcount);
   19519              :           decl = error_mark_node;
   19520              :         }
   19521              :       else
   19522              :         prev = decl3;
   19523              :     }
   19524        36603 :   decomp->decl = prev;
   19525        36603 :   return decl;
   19526              : }
   19527              : 
   19528              : /* Return the proper local_specialization for init-capture pack DECL.  */
   19529              : 
   19530              : static tree
   19531          169 : lookup_init_capture_pack (tree decl)
   19532              : {
   19533              :   /* We handle normal pack captures by forwarding to the specialization of the
   19534              :      captured parameter.  We can't do that for pack init-captures; we need them
   19535              :      to have their own local_specialization.  We created the individual
   19536              :      VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
   19537              :      when we process the DECL_EXPR for the pack init-capture in the template.
   19538              :      So, how do we find them?  We don't know the capture proxy pack when
   19539              :      building the individual resulting proxies, and we don't know the
   19540              :      individual proxies when instantiating the pack.  What we have in common is
   19541              :      the FIELD_DECL.
   19542              : 
   19543              :      So...when we instantiate the FIELD_DECL, we stick the result in
   19544              :      local_specializations.  Then at the DECL_EXPR we look up that result, see
   19545              :      how many elements it has, synthesize the names, and look them up.  */
   19546              : 
   19547          169 :   tree cname = DECL_NAME (decl);
   19548          169 :   tree val = DECL_VALUE_EXPR (decl);
   19549          169 :   tree field = TREE_OPERAND (val, 1);
   19550          169 :   gcc_assert (TREE_CODE (field) == FIELD_DECL);
   19551          169 :   tree fpack = retrieve_local_specialization (field);
   19552          169 :   if (fpack == error_mark_node)
   19553              :     return error_mark_node;
   19554              : 
   19555          169 :   int len = 1;
   19556          169 :   tree vec = NULL_TREE;
   19557          169 :   tree r = NULL_TREE;
   19558          169 :   if (TREE_CODE (fpack) == TREE_VEC)
   19559              :     {
   19560          166 :       len = TREE_VEC_LENGTH (fpack);
   19561          166 :       vec = make_tree_vec (len);
   19562          166 :       r = make_node (NONTYPE_ARGUMENT_PACK);
   19563          166 :       ARGUMENT_PACK_ARGS (r) = vec;
   19564              :     }
   19565          480 :   for (int i = 0; i < len; ++i)
   19566              :     {
   19567          311 :       tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
   19568          311 :       tree elt = lookup_name (ename);
   19569          311 :       if (vec)
   19570          308 :         TREE_VEC_ELT (vec, i) = elt;
   19571              :       else
   19572              :         r = elt;
   19573              :     }
   19574              :   return r;
   19575              : }
   19576              : 
   19577              : /* T is an operand of a template tree being substituted.  Return whether
   19578              :    T is dependent such that we should suppress some warnings that would
   19579              :    make sense if the substituted expression were written directly, like
   19580              :      template <int I> bool f() { return I == 2; }
   19581              :    We don't want to warn when instantiating f that comparing two constants
   19582              :    always has the same value.
   19583              : 
   19584              :    This is a more limited concept of dependence than instantiation-dependent;
   19585              :    here we don't care whether substitution could fail.  */
   19586              : 
   19587              : static bool
   19588    109469101 : dependent_operand_p (tree t)
   19589              : {
   19590    110790570 :   while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
   19591      1321469 :     t = TREE_OPERAND (t, 0);
   19592              : 
   19593    109469101 :   ++processing_template_decl;
   19594    109469101 :   bool r = (potential_constant_expression (t)
   19595    109469101 :             ? value_dependent_expression_p (t)
   19596    109469101 :             : type_dependent_expression_p (t));
   19597    109469101 :   --processing_template_decl;
   19598    109469101 :   return r;
   19599              : }
   19600              : 
   19601              : /* A superset of tsubst_expr that also handles statement trees.  */
   19602              : 
   19603              : static tree
   19604    342944749 : tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   19605              : {
   19606              : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   19607              : #define RECUR(NODE)                             \
   19608              :   tsubst_stmt ((NODE), args, complain, in_decl)
   19609              : 
   19610    342944749 :   tree stmt, tmp;
   19611    342944749 :   tree r;
   19612    342944749 :   location_t loc;
   19613              : 
   19614    342944749 :   if (t == NULL_TREE || t == error_mark_node)
   19615              :     return t;
   19616              : 
   19617    340871142 :   loc = input_location;
   19618    340871142 :   if (location_t eloc = cp_expr_location (t))
   19619    293657867 :     input_location = eloc;
   19620    340871142 :   if (STATEMENT_CODE_P (TREE_CODE (t)))
   19621     49005305 :     current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
   19622              : 
   19623    340871142 :   switch (TREE_CODE (t))
   19624              :     {
   19625     43518722 :     case STATEMENT_LIST:
   19626     43518722 :       {
   19627    218894243 :         for (tree stmt : tsi_range (t))
   19628    175375524 :           RECUR (stmt);
   19629              :         break;
   19630              :       }
   19631              : 
   19632      3069304 :     case CTOR_INITIALIZER:
   19633      3069304 :       finish_mem_initializers (tsubst_initializer_list
   19634      3069304 :                                (TREE_OPERAND (t, 0), args));
   19635      3069304 :       break;
   19636              : 
   19637     20542894 :     case RETURN_EXPR:
   19638     20542894 :       finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
   19639     20542891 :       break;
   19640              : 
   19641          176 :     case CO_RETURN_EXPR:
   19642          176 :       finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
   19643          176 :       break;
   19644              : 
   19645     24431676 :     case EXPR_STMT:
   19646     24431676 :       tmp = RECUR (EXPR_STMT_EXPR (t));
   19647     24431673 :       if (EXPR_STMT_STMT_EXPR_RESULT (t))
   19648          160 :         finish_stmt_expr_expr (tmp, cur_stmt_expr);
   19649              :       else
   19650     24431513 :         finish_expr_stmt (tmp);
   19651              :       break;
   19652              : 
   19653        23002 :     case USING_STMT:
   19654        23002 :       finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
   19655        23002 :       break;
   19656              : 
   19657            0 :     case PRECONDITION_STMT:
   19658            0 :     case POSTCONDITION_STMT:
   19659            0 :       gcc_unreachable ();
   19660              : 
   19661            1 :     case ASSERTION_STMT:
   19662            1 :       {
   19663            1 :         r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
   19664            1 :         if (r != error_mark_node)
   19665            1 :           add_stmt (r);
   19666            1 :         RETURN (r);
   19667              :       }
   19668              :       break;
   19669              : 
   19670     22912686 :     case DECL_EXPR:
   19671     22912686 :       {
   19672     22912686 :         tree decl, pattern_decl;
   19673     22912686 :         tree init;
   19674              : 
   19675     22912686 :         pattern_decl = decl = DECL_EXPR_DECL (t);
   19676     22912686 :         if (TREE_CODE (decl) == LABEL_DECL)
   19677           39 :           finish_label_decl (DECL_NAME (decl));
   19678     22912647 :         else if (TREE_CODE (decl) == USING_DECL)
   19679              :           {
   19680       259985 :             tree scope = USING_DECL_SCOPE (decl);
   19681       259985 :             if (DECL_DEPENDENT_P (decl))
   19682              :               {
   19683            9 :                 scope = tsubst (scope, args, complain, in_decl);
   19684            9 :                 if (!MAYBE_CLASS_TYPE_P (scope)
   19685            6 :                     && TREE_CODE (scope) != ENUMERAL_TYPE)
   19686              :                   {
   19687            3 :                     if (complain & tf_error)
   19688            3 :                       error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
   19689              :                                 "class, namespace, or enumeration", scope);
   19690            3 :                     return error_mark_node;
   19691              :                   }
   19692            6 :                 finish_nonmember_using_decl (scope, DECL_NAME (decl));
   19693              :               }
   19694              :             else
   19695              :               {
   19696              :                 /* This is a non-dependent using-decl, and we'll have
   19697              :                    used the names it found during template parsing.  We do
   19698              :                    not want to do the lookup again, because we might not
   19699              :                    find the things we found then.  */
   19700       259976 :                 gcc_checking_assert (scope == tsubst (scope, args,
   19701              :                                                       complain, in_decl));
   19702              :                 /* We still need to push the bindings so that we can look up
   19703              :                    this name later.  */
   19704       519952 :                 push_using_decl_bindings (DECL_NAME (decl),
   19705       259976 :                                           USING_DECL_DECLS (decl));
   19706              :               }
   19707              :           }
   19708     22652662 :         else if (is_capture_proxy (decl)
   19709     22652662 :                  && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
   19710              :           {
   19711              :             /* We're in tsubst_lambda_expr, we've already inserted a new
   19712              :                capture proxy, so look it up and register it.  */
   19713       231553 :             tree inst;
   19714       231553 :             if (!DECL_PACK_P (decl))
   19715              :               {
   19716       231185 :                 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
   19717              :                                     LOOK_want::HIDDEN_LAMBDA);
   19718       231185 :                 gcc_assert (inst != decl && is_capture_proxy (inst));
   19719              :               }
   19720          368 :             else if (is_normal_capture_proxy (decl))
   19721              :               {
   19722          199 :                 inst = (retrieve_local_specialization
   19723          199 :                         (DECL_CAPTURED_VARIABLE (decl)));
   19724          199 :                 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
   19725              :                             || DECL_PACK_P (inst));
   19726              :               }
   19727              :             else
   19728          169 :               inst = lookup_init_capture_pack (decl);
   19729              : 
   19730       231553 :             register_local_specialization (inst, decl);
   19731       231553 :             break;
   19732              :           }
   19733     22421109 :         else if (DECL_PRETTY_FUNCTION_P (decl))
   19734        28868 :           decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
   19735        28868 :                                   DECL_NAME (decl),
   19736              :                                   true/*DECL_PRETTY_FUNCTION_P (decl)*/);
   19737      2782450 :         else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   19738     23901776 :                  && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   19739              :           /* Don't copy the old closure; we'll create a new one in
   19740              :              tsubst_lambda_expr.  */
   19741              :           break;
   19742              :         else
   19743              :           {
   19744     22205789 :             init = DECL_INITIAL (decl);
   19745     22205789 :             decl = tsubst (decl, args, complain, in_decl);
   19746     22205786 :             if (decl != error_mark_node)
   19747              :               {
   19748              :                 /* By marking the declaration as instantiated, we avoid
   19749              :                    trying to instantiate it.  Since instantiate_decl can't
   19750              :                    handle local variables, and since we've already done
   19751              :                    all that needs to be done, that's the right thing to
   19752              :                    do.  */
   19753     22205774 :                 if (VAR_P (decl))
   19754     19609403 :                   DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   19755     19609403 :                 if (VAR_P (decl) && !DECL_NAME (decl)
   19756     22242417 :                     && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   19757              :                   /* Anonymous aggregates are a special case.  */
   19758           33 :                   finish_anon_union (decl);
   19759     22205741 :                 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
   19760              :                   {
   19761       112230 :                     DECL_CONTEXT (decl) = current_function_decl;
   19762       112230 :                     insert_capture_proxy (decl);
   19763              :                   }
   19764     22093511 :                 else if (DECL_IMPLICIT_TYPEDEF_P (t))
   19765              :                   /* We already did a pushtag.  */;
   19766      2596371 :                 else if (VAR_OR_FUNCTION_DECL_P (decl)
   19767     22093893 :                          && DECL_LOCAL_DECL_P (decl))
   19768              :                   {
   19769          468 :                     if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   19770          133 :                       DECL_CONTEXT (decl) = NULL_TREE;
   19771          468 :                     decl = pushdecl (decl);
   19772          468 :                     if (TREE_CODE (decl) == FUNCTION_DECL
   19773          373 :                         && DECL_OMP_DECLARE_REDUCTION_P (decl)
   19774          592 :                         && cp_check_omp_declare_reduction (decl))
   19775           88 :                       instantiate_body (pattern_decl, args, decl, true);
   19776              :                   }
   19777              :                 else
   19778              :                   {
   19779     22093043 :                     bool const_init = false;
   19780     22093043 :                     cp_decomp decomp_d, *decomp = NULL;
   19781     22093043 :                     tree asmspec_tree = NULL_TREE;
   19782     22093043 :                     maybe_push_decl (decl);
   19783              : 
   19784     22093043 :                     if (VAR_P (decl)
   19785     19497054 :                         && DECL_LANG_SPECIFIC (decl)
   19786     22738308 :                         && DECL_OMP_PRIVATIZED_MEMBER (decl))
   19787              :                       break;
   19788              : 
   19789     19496871 :                     if (DECL_DECOMPOSITION_P (decl)
   19790     22129065 :                         && TREE_TYPE (pattern_decl) != error_mark_node)
   19791              :                       {
   19792        36205 :                         decomp = &decomp_d;
   19793        36205 :                         if (tsubst_decomp_names (decl, pattern_decl, args,
   19794              :                                                  complain, in_decl, decomp)
   19795        36205 :                             == error_mark_node)
   19796     22056664 :                           decomp = NULL;
   19797              :                       }
   19798              : 
   19799     22092860 :                     init = tsubst_init (init, decl, args, complain, in_decl);
   19800              : 
   19801     22092860 :                     if (VAR_P (decl))
   19802     19496871 :                       const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
   19803              :                                     (pattern_decl));
   19804              : 
   19805              :                     /* In a non-template function, VLA type declarations are
   19806              :                        handled in grokdeclarator; for templates, handle them
   19807              :                        now.  */
   19808     22092860 :                     predeclare_vla (decl);
   19809              : 
   19810     41589731 :                     if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
   19811              :                       {
   19812            6 :                         tree id = DECL_ASSEMBLER_NAME (pattern_decl);
   19813            6 :                         const char *asmspec = IDENTIFIER_POINTER (id);
   19814            6 :                         gcc_assert (asmspec[0] == '*');
   19815            6 :                         asmspec_tree
   19816            6 :                           = build_string (IDENTIFIER_LENGTH (id) - 1,
   19817              :                                           asmspec + 1);
   19818            6 :                         TREE_TYPE (asmspec_tree) = char_array_type_node;
   19819              :                       }
   19820              : 
   19821     22092860 :                     cp_finish_decl (decl, init, const_init, asmspec_tree, 0,
   19822              :                                     decomp);
   19823              :                   }
   19824              :               }
   19825              :           }
   19826              : 
   19827              :         break;
   19828              :       }
   19829              : 
   19830      1203605 :     case FOR_STMT:
   19831      1203605 :       stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
   19832      1203605 :       RECUR (FOR_INIT_STMT (t));
   19833      1203605 :       finish_init_stmt (stmt);
   19834      1203605 :       tmp = RECUR (FOR_COND (t));
   19835      1203605 :       finish_for_cond (tmp, stmt, false, 0, false);
   19836      1203605 :       tmp = RECUR (FOR_EXPR (t));
   19837      1203605 :       finish_for_expr (tmp, stmt);
   19838      1203605 :       {
   19839      1203605 :         bool prev = note_iteration_stmt_body_start ();
   19840      1203605 :         RECUR (FOR_BODY (t));
   19841      1203605 :         note_iteration_stmt_body_end (prev);
   19842              :       }
   19843      1203605 :       finish_for_stmt (stmt);
   19844      1203605 :       break;
   19845              : 
   19846        73014 :     case RANGE_FOR_STMT:
   19847        73014 :       {
   19848              :         /* Construct another range_for, if this is not a final
   19849              :            substitution (for inside a generic lambda of a
   19850              :            template).  Otherwise convert to a regular for.  */
   19851        73014 :         tree decl, expr;
   19852       146028 :         stmt = (processing_template_decl
   19853        73014 :                 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
   19854        72760 :                 : begin_for_stmt (NULL_TREE, NULL_TREE));
   19855        73014 :         RECUR (RANGE_FOR_INIT_STMT (t));
   19856        73014 :         decl = RANGE_FOR_DECL (t);
   19857        73014 :         decl = tsubst (decl, args, complain, in_decl);
   19858        73014 :         maybe_push_decl (decl);
   19859        73014 :         expr = RECUR (RANGE_FOR_EXPR (t));
   19860              : 
   19861        73014 :         cp_decomp decomp_d, *decomp = NULL;
   19862        73014 :         if (DECL_DECOMPOSITION_P (decl))
   19863              :           {
   19864          366 :             decomp = &decomp_d;
   19865          366 :             decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
   19866              :                                         complain, in_decl, decomp);
   19867              :           }
   19868              : 
   19869        73014 :         tree unroll = RECUR (RANGE_FOR_UNROLL (t));
   19870        73014 :         if (unroll)
   19871           33 :           unroll
   19872           33 :             = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)),
   19873              :                                       unroll);
   19874        73014 :         if (processing_template_decl)
   19875              :           {
   19876          254 :             RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
   19877          254 :             RANGE_FOR_UNROLL (stmt) = unroll;
   19878          254 :             RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t);
   19879          254 :             finish_range_for_decl (stmt, decl, expr);
   19880          254 :             if (decomp && decl != error_mark_node)
   19881            3 :               cp_finish_decomp (decl, decomp);
   19882              :           }
   19883              :         else
   19884       145520 :           stmt = cp_convert_range_for (stmt, decl, expr, decomp,
   19885        72760 :                                        RANGE_FOR_IVDEP (t), unroll,
   19886        72760 :                                        RANGE_FOR_NOVECTOR (t));
   19887              : 
   19888        73014 :         bool prev = note_iteration_stmt_body_start ();
   19889        73014 :         RECUR (RANGE_FOR_BODY (t));
   19890        73014 :         note_iteration_stmt_body_end (prev);
   19891        73014 :         finish_for_stmt (stmt);
   19892              :       }
   19893        73014 :       break;
   19894              : 
   19895       409068 :     case WHILE_STMT:
   19896       409068 :       stmt = begin_while_stmt ();
   19897       409068 :       tmp = RECUR (WHILE_COND (t));
   19898       409068 :       finish_while_stmt_cond (tmp, stmt, false, 0, false);
   19899       409068 :       {
   19900       409068 :         bool prev = note_iteration_stmt_body_start ();
   19901       409068 :         RECUR (WHILE_BODY (t));
   19902       409068 :         note_iteration_stmt_body_end (prev);
   19903              :       }
   19904       409068 :       finish_while_stmt (stmt);
   19905       409068 :       break;
   19906              : 
   19907       864274 :     case DO_STMT:
   19908       864274 :       stmt = begin_do_stmt ();
   19909       864274 :       {
   19910       864274 :         bool prev = note_iteration_stmt_body_start ();
   19911       864274 :         RECUR (DO_BODY (t));
   19912       864274 :         note_iteration_stmt_body_end (prev);
   19913              :       }
   19914       864274 :       finish_do_body (stmt);
   19915       864274 :       tmp = RECUR (DO_COND (t));
   19916       864274 :       finish_do_stmt (tmp, stmt, false, 0, false);
   19917       864274 :       break;
   19918              : 
   19919          559 :     case TEMPLATE_FOR_STMT:
   19920          559 :       {
   19921          559 :         tree init;
   19922          559 :         stmt = build_stmt (EXPR_LOCATION (t), TEMPLATE_FOR_STMT, NULL_TREE,
   19923              :                            NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
   19924          559 :         TEMPLATE_FOR_SCOPE (stmt) = begin_template_for_scope (&init);
   19925          559 :         TEMPLATE_FOR_INIT_STMT (stmt) = init;
   19926          559 :         RECUR (TEMPLATE_FOR_INIT_STMT (t));
   19927          559 :         TEMPLATE_FOR_EXPR (stmt) = RECUR (TEMPLATE_FOR_EXPR (t));
   19928          559 :         if (processing_template_decl)
   19929              :           {
   19930           10 :             tree orig_decl = TEMPLATE_FOR_DECL (t);
   19931           10 :             if (TREE_CODE (orig_decl) == TREE_VEC)
   19932            0 :               orig_decl = TREE_VEC_ELT (orig_decl, 0);
   19933           10 :             tree decl = tsubst (orig_decl, args, complain, in_decl);
   19934           10 :             maybe_push_decl (decl);
   19935           10 :             if (VAR_P (decl))
   19936              :               {
   19937           10 :                 retrofit_lang_decl (decl);
   19938           10 :                 SET_DECL_DEPENDENT_INIT_P (decl, 1);
   19939              :               }
   19940              : 
   19941           10 :             cp_decomp decomp_d, *decomp = NULL;
   19942           10 :             if (DECL_DECOMPOSITION_P (decl))
   19943              :               {
   19944            0 :                 decomp = &decomp_d;
   19945            0 :                 decl = tsubst_decomp_names (decl, orig_decl, args,
   19946              :                                             complain, in_decl, decomp);
   19947            0 :                 if (decl != error_mark_node)
   19948              :                   {
   19949            0 :                     tree v = make_tree_vec (decomp->count + 1);
   19950            0 :                     TREE_VEC_ELT (v, 0) = decl;
   19951            0 :                     decl = decomp->decl;
   19952            0 :                     for (unsigned i = 0; i < decomp->count; ++i)
   19953              :                       {
   19954            0 :                         TREE_VEC_ELT (v, decomp->count - i) = decl;
   19955            0 :                         decl = DECL_CHAIN (decl);
   19956              :                       }
   19957              :                     decl = v;
   19958              :                   }
   19959              :               }
   19960           10 :             TEMPLATE_FOR_DECL (stmt) = decl;
   19961           10 :             TEMPLATE_FOR_INIT_STMT (stmt) = pop_stmt_list (init);
   19962           10 :             add_stmt (stmt);
   19963           10 :             TEMPLATE_FOR_BODY (stmt) = do_pushlevel (sk_block);
   19964           10 :             bool prev = note_iteration_stmt_body_start ();
   19965           10 :             RECUR (TEMPLATE_FOR_BODY (t));
   19966           10 :             note_iteration_stmt_body_end (prev);
   19967           10 :             TEMPLATE_FOR_BODY (stmt)
   19968           20 :               = do_poplevel (TEMPLATE_FOR_BODY (stmt));
   19969              :           }
   19970              :         else
   19971              :           {
   19972          549 :             TEMPLATE_FOR_DECL (stmt) = TEMPLATE_FOR_DECL (t);
   19973          549 :             TEMPLATE_FOR_BODY (stmt) = TEMPLATE_FOR_BODY (t);
   19974          549 :             finish_expansion_stmt (stmt, args, complain, in_decl);
   19975              :           }
   19976          559 :         add_stmt (do_poplevel (TEMPLATE_FOR_SCOPE (stmt)));
   19977              :       }
   19978          559 :       break;
   19979              : 
   19980     17007501 :     case IF_STMT:
   19981     17007501 :       stmt = begin_if_stmt ();
   19982     17007501 :       IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
   19983     17007501 :       IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
   19984     17007501 :       if (IF_STMT_CONSTEXPR_P (t))
   19985      5235638 :         args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
   19986     17007501 :       {
   19987     17007501 :         tree cond = IF_COND (t);
   19988     17007501 :         bool was_dep = dependent_operand_p (cond);
   19989     17007501 :         cond = RECUR (cond);
   19990     17007501 :         warning_sentinel s1(warn_address, was_dep);
   19991     17007501 :         tmp = finish_if_stmt_cond (cond, stmt);
   19992     17007501 :       }
   19993     17007501 :       if (IF_STMT_CONSTEXPR_P (t)
   19994     17007501 :           && instantiation_dependent_expression_p (tmp))
   19995              :         {
   19996              :           /* We're partially instantiating a generic lambda, but the condition
   19997              :              of the constexpr if is still dependent.  Don't substitute into the
   19998              :              branches now, just remember the template arguments.  */
   19999         4883 :           do_poplevel (IF_SCOPE (stmt));
   20000         4883 :           IF_SCOPE (stmt) = NULL_TREE;
   20001         4883 :           IF_COND (stmt) = IF_COND (t);
   20002         4883 :           THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
   20003         4883 :           ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
   20004         4883 :           IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
   20005         4883 :           add_stmt (stmt);
   20006         4883 :           break;
   20007              :         }
   20008     17002618 :       if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
   20009              :         /* Don't instantiate the THEN_CLAUSE. */;
   20010     14501675 :       else if (IF_STMT_CONSTEVAL_P (t))
   20011              :         {
   20012         4051 :           bool save_in_consteval_if_p = in_consteval_if_p;
   20013         4051 :           in_consteval_if_p = true;
   20014         4051 :           RECUR (THEN_CLAUSE (t));
   20015         4051 :           in_consteval_if_p = save_in_consteval_if_p;
   20016              :         }
   20017              :       else
   20018              :         {
   20019     14497624 :           tree folded = fold_non_dependent_expr (tmp, complain);
   20020     14497624 :           bool inhibit = integer_zerop (folded);
   20021     14497624 :           if (inhibit)
   20022        52224 :             ++c_inhibit_evaluation_warnings;
   20023     14497624 :           RECUR (THEN_CLAUSE (t));
   20024     14497624 :           if (inhibit)
   20025        52224 :             --c_inhibit_evaluation_warnings;
   20026              :         }
   20027     17002618 :       finish_then_clause (stmt);
   20028              : 
   20029     17002618 :       if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
   20030              :         /* Don't instantiate the ELSE_CLAUSE. */;
   20031     14272822 :       else if (ELSE_CLAUSE (t))
   20032              :         {
   20033      6399470 :           tree folded = fold_non_dependent_expr (tmp, complain);
   20034      6399470 :           bool inhibit = integer_nonzerop (folded);
   20035      6399470 :           begin_else_clause (stmt);
   20036      6399470 :           if (inhibit)
   20037        21382 :             ++c_inhibit_evaluation_warnings;
   20038      6399470 :           RECUR (ELSE_CLAUSE (t));
   20039      6399470 :           if (inhibit)
   20040        21382 :             --c_inhibit_evaluation_warnings;
   20041      6399470 :           finish_else_clause (stmt);
   20042              :         }
   20043              : 
   20044     17002618 :       finish_if_stmt (stmt);
   20045     17002618 :       break;
   20046              : 
   20047     45893579 :     case BIND_EXPR:
   20048     45893579 :       if (BIND_EXPR_BODY_BLOCK (t))
   20049      4008042 :         stmt = begin_function_body ();
   20050              :       else
   20051     83667489 :         stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
   20052              :                                     ? BCS_TRY_BLOCK : 0);
   20053              : 
   20054     45893579 :       RECUR (BIND_EXPR_BODY (t));
   20055              : 
   20056     45893570 :       if (BIND_EXPR_BODY_BLOCK (t))
   20057      4008042 :         finish_function_body (stmt);
   20058              :       else
   20059     41885528 :         finish_compound_stmt (stmt);
   20060              :       break;
   20061              : 
   20062       906616 :     case BREAK_STMT:
   20063       906616 :       finish_break_stmt ();
   20064       906616 :       break;
   20065              : 
   20066         1428 :     case CONTINUE_STMT:
   20067         1428 :       finish_continue_stmt ();
   20068         1428 :       break;
   20069              : 
   20070       239079 :     case SWITCH_STMT:
   20071       239079 :       stmt = begin_switch_stmt ();
   20072       239079 :       tmp = RECUR (SWITCH_STMT_COND (t));
   20073       239079 :       finish_switch_cond (tmp, stmt);
   20074       239079 :       RECUR (SWITCH_STMT_BODY (t));
   20075       239079 :       finish_switch_stmt (stmt);
   20076       239079 :       break;
   20077              : 
   20078      1314386 :     case CASE_LABEL_EXPR:
   20079      1314386 :       {
   20080      1314386 :         tree decl = CASE_LABEL (t);
   20081      1314386 :         tree low = RECUR (CASE_LOW (t));
   20082      1314386 :         tree high = RECUR (CASE_HIGH (t));
   20083      1314386 :         tree l = finish_case_label (EXPR_LOCATION (t), low, high);
   20084      1314386 :         if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
   20085              :           {
   20086      1314377 :             tree label = CASE_LABEL (l);
   20087      1314377 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   20088      1314377 :             if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   20089            6 :               cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   20090              :           }
   20091              :       }
   20092              :       break;
   20093              : 
   20094          275 :     case LABEL_EXPR:
   20095          275 :       {
   20096          275 :         tree decl = LABEL_EXPR_LABEL (t);
   20097          275 :         tree label = finish_label_stmt (DECL_NAME (decl));
   20098          275 :         if (TREE_CODE (label) == LABEL_DECL)
   20099              :           {
   20100          275 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   20101          275 :             copy_warning (label, decl);
   20102              :           }
   20103          275 :         if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   20104           18 :           cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   20105              :       }
   20106          275 :       break;
   20107              : 
   20108          475 :     case GOTO_EXPR:
   20109          475 :       tmp = GOTO_DESTINATION (t);
   20110          475 :       if (TREE_CODE (tmp) != LABEL_DECL)
   20111              :         /* Computed goto's must be tsubst'd into.  On the other hand,
   20112              :            non-computed gotos must not be; the identifier in question
   20113              :            will have no binding.  */
   20114           23 :         tmp = RECUR (tmp);
   20115              :       else
   20116          452 :         tmp = DECL_NAME (tmp);
   20117          475 :       finish_goto_stmt (tmp);
   20118          475 :       break;
   20119              : 
   20120          769 :     case ASM_EXPR:
   20121          769 :       {
   20122          769 :         tree string = RECUR (ASM_STRING (t));
   20123          769 :         tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
   20124              :                                                  complain, in_decl);
   20125          769 :         tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
   20126              :                                                 complain, in_decl);
   20127          769 :         tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
   20128              :                                                   complain, in_decl);
   20129          769 :         tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
   20130              :                                                 complain, in_decl);
   20131         1538 :         tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
   20132              :                                outputs, inputs, clobbers, labels,
   20133          769 :                                ASM_INLINE_P (t), false);
   20134          769 :         tree asm_expr = tmp;
   20135          769 :         if (asm_expr != error_mark_node)
   20136              :           {
   20137          660 :             if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
   20138          660 :               asm_expr = TREE_OPERAND (asm_expr, 0);
   20139          660 :             ASM_BASIC_P (asm_expr) = ASM_BASIC_P (t);
   20140              :           }
   20141              :       }
   20142              :       break;
   20143              : 
   20144       103585 :     case TRY_BLOCK:
   20145       103585 :       if (CLEANUP_P (t))
   20146              :         {
   20147            0 :           stmt = begin_try_block ();
   20148            0 :           RECUR (TRY_STMTS (t));
   20149            0 :           finish_cleanup_try_block (stmt);
   20150            0 :           finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
   20151              :         }
   20152              :       else
   20153              :         {
   20154       103585 :           tree compound_stmt = NULL_TREE;
   20155              : 
   20156       103585 :           if (FN_TRY_BLOCK_P (t))
   20157           42 :             stmt = begin_function_try_block (&compound_stmt);
   20158              :           else
   20159       103543 :             stmt = begin_try_block ();
   20160              : 
   20161       103585 :           RECUR (TRY_STMTS (t));
   20162              : 
   20163       103585 :           if (FN_TRY_BLOCK_P (t))
   20164           42 :             finish_function_try_block (stmt);
   20165              :           else
   20166       103543 :             finish_try_block (stmt);
   20167              : 
   20168       103585 :           RECUR (TRY_HANDLERS (t));
   20169       103585 :           if (FN_TRY_BLOCK_P (t))
   20170           42 :             finish_function_handler_sequence (stmt, compound_stmt);
   20171              :           else
   20172       103543 :             finish_handler_sequence (stmt);
   20173              :         }
   20174              :       break;
   20175              : 
   20176       104588 :     case HANDLER:
   20177       104588 :       {
   20178       104588 :         tree decl = HANDLER_PARMS (t);
   20179              : 
   20180       104588 :         if (decl)
   20181              :           {
   20182         2619 :             decl = tsubst (decl, args, complain, in_decl);
   20183              :             /* Prevent instantiate_decl from trying to instantiate
   20184              :                this variable.  We've already done all that needs to be
   20185              :                done.  */
   20186         2619 :             if (decl != error_mark_node)
   20187         2616 :               DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   20188              :           }
   20189       104588 :         stmt = begin_handler ();
   20190       104588 :         finish_handler_parms (decl, stmt);
   20191       104588 :         RECUR (HANDLER_BODY (t));
   20192       104588 :         finish_handler (stmt);
   20193              :       }
   20194       104588 :       break;
   20195              : 
   20196       568451 :     case TAG_DEFN:
   20197       568451 :       tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
   20198       568451 :       if (dependent_type_p (tmp))
   20199              :         /* This is a partial instantiation, try again when full.  */
   20200           21 :         add_stmt (build_min (TAG_DEFN, tmp));
   20201       568430 :       else if (CLASS_TYPE_P (tmp))
   20202              :         {
   20203              :           /* Local classes are not independent templates; they are
   20204              :              instantiated along with their containing function.  And this
   20205              :              way we don't have to deal with pushing out of one local class
   20206              :              to instantiate a member of another local class.  */
   20207              :           /* Closures are handled by the LAMBDA_EXPR.  */
   20208      1136284 :           gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
   20209       568181 :           complete_type (tmp);
   20210       568181 :           tree save_ccp = current_class_ptr;
   20211       568181 :           tree save_ccr = current_class_ref;
   20212      3973019 :           for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
   20213      3404838 :             if ((VAR_P (fld)
   20214      3404835 :                  || (TREE_CODE (fld) == FUNCTION_DECL
   20215      2353316 :                      && !DECL_ARTIFICIAL (fld)))
   20216      5758138 :                 && DECL_TEMPLATE_INSTANTIATION (fld))
   20217      2353303 :               instantiate_decl (fld, /*defer_ok=*/false,
   20218              :                                 /*expl_inst_class=*/false);
   20219      1051535 :             else if (TREE_CODE (fld) == FIELD_DECL)
   20220       483125 :               maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
   20221       568181 :           current_class_ptr = save_ccp;
   20222       568181 :           current_class_ref = save_ccr;
   20223              :         }
   20224              :       break;
   20225              : 
   20226      6084563 :     case STATIC_ASSERT:
   20227      6084563 :       {
   20228      6084563 :         tree condition, message;
   20229              : 
   20230      6084563 :         ++c_inhibit_evaluation_warnings;
   20231      6084563 :         condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
   20232              :                                  complain, in_decl);
   20233      6084563 :         message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args,
   20234              :                                complain, in_decl);
   20235      6084563 :         if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST
   20236      6084563 :             && TREE_CODE (message) == STRING_CST)
   20237            0 :           message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t),
   20238            0 :                                 PAREN_EXPR, TREE_TYPE (message), message);
   20239      6084563 :         --c_inhibit_evaluation_warnings;
   20240              : 
   20241      6084563 :         finish_static_assert (condition, message,
   20242      6084563 :                               STATIC_ASSERT_SOURCE_LOCATION (t),
   20243              :                               /*member_p=*/false, /*show_expr_p=*/true,
   20244      6084563 :                               CONSTEVAL_BLOCK_P (t));
   20245              :       }
   20246      6084563 :       break;
   20247              : 
   20248          245 :     case OACC_KERNELS:
   20249          245 :     case OACC_PARALLEL:
   20250          245 :     case OACC_SERIAL:
   20251          245 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC_TARGET, args,
   20252              :                                 complain, in_decl);
   20253          245 :       stmt = begin_omp_parallel ();
   20254          245 :       RECUR (OMP_BODY (t));
   20255          245 :       finish_omp_construct (TREE_CODE (t), stmt, tmp);
   20256          245 :       break;
   20257              : 
   20258          811 :     case OMP_PARALLEL:
   20259          811 :       r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
   20260          811 :       tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
   20261              :                                 complain, in_decl);
   20262          811 :       if (OMP_PARALLEL_COMBINED (t))
   20263          423 :         omp_parallel_combined_clauses = &tmp;
   20264          811 :       stmt = begin_omp_parallel ();
   20265          811 :       RECUR (OMP_PARALLEL_BODY (t));
   20266          811 :       gcc_assert (omp_parallel_combined_clauses == NULL);
   20267         1622 :       OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
   20268          811 :         = OMP_PARALLEL_COMBINED (t);
   20269          811 :       pop_omp_privatization_clauses (r);
   20270          811 :       break;
   20271              : 
   20272          355 :     case OMP_TASK:
   20273          355 :       if (OMP_TASK_BODY (t) == NULL_TREE)
   20274              :         {
   20275            0 :           tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   20276              :                                     complain, in_decl);
   20277            0 :           t = copy_node (t);
   20278            0 :           OMP_TASK_CLAUSES (t) = tmp;
   20279            0 :           add_stmt (t);
   20280            0 :           break;
   20281              :         }
   20282          355 :       r = push_omp_privatization_clauses (false);
   20283          355 :       tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   20284              :                                 complain, in_decl);
   20285          355 :       stmt = begin_omp_task ();
   20286          355 :       RECUR (OMP_TASK_BODY (t));
   20287          355 :       finish_omp_task (tmp, stmt);
   20288          355 :       pop_omp_privatization_clauses (r);
   20289          355 :       break;
   20290              : 
   20291         1219 :     case OMP_FOR:
   20292         1219 :     case OMP_LOOP:
   20293         1219 :     case OMP_SIMD:
   20294         1219 :     case OMP_DISTRIBUTE:
   20295         1219 :     case OMP_TASKLOOP:
   20296         1219 :     case OMP_TILE:
   20297         1219 :     case OMP_UNROLL:
   20298         1219 :     case OACC_LOOP:
   20299         1219 :       {
   20300         1219 :         tree clauses, body, pre_body;
   20301         1219 :         tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
   20302         1219 :         tree orig_declv = NULL_TREE;
   20303         1219 :         tree incrv = NULL_TREE;
   20304         1219 :         enum c_omp_region_type ort = C_ORT_OMP;
   20305         1219 :         bool any_range_for = false;
   20306         1219 :         int i;
   20307              : 
   20308         1219 :         if (TREE_CODE (t) == OACC_LOOP)
   20309           51 :           ort = C_ORT_ACC;
   20310              : 
   20311         1219 :         r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
   20312         1219 :         clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
   20313              :                                       in_decl);
   20314         1219 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20315              :           {
   20316         1059 :             declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20317         1059 :             if (OMP_FOR_ORIG_DECLS (t))
   20318          541 :               orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20319         1059 :             initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20320         1059 :             condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20321         1059 :             incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20322              :           }
   20323              : 
   20324         1219 :         keep_next_level (true);
   20325         1219 :         stmt = begin_omp_structured_block ();
   20326              : 
   20327         1219 :         pre_body = push_stmt_list ();
   20328         1219 :         RECUR (OMP_FOR_PRE_BODY (t));
   20329         1219 :         pre_body = pop_stmt_list (pre_body);
   20330              : 
   20331         1219 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20332         2351 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20333              :             {
   20334         1292 :               if (TREE_VEC_ELT (OMP_FOR_INIT (t), i))
   20335         1253 :                 any_range_for
   20336         1253 :                   |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
   20337              :                                               condv, incrv, &clauses, args,
   20338              :                                               complain, in_decl);
   20339              :               else
   20340              :                 {
   20341           39 :                   TREE_VEC_ELT (declv, i) = global_namespace;
   20342           39 :                   TREE_VEC_ELT (initv, i) = NULL_TREE;
   20343           39 :                   TREE_VEC_ELT (condv, i) = NULL_TREE;
   20344           39 :                   TREE_VEC_ELT (incrv, i) = NULL_TREE;
   20345           39 :                   if (orig_declv)
   20346           30 :                     TREE_VEC_ELT (orig_declv, i) = NULL_TREE;
   20347              :                 }
   20348              :             }
   20349         1219 :         omp_parallel_combined_clauses = NULL;
   20350              : 
   20351         1219 :         if (any_range_for)
   20352              :           {
   20353          143 :             gcc_assert (orig_declv);
   20354          143 :             body = begin_omp_structured_block ();
   20355          468 :             for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20356          182 :               if (TREE_VEC_ELT (declv, i) != global_namespace
   20357          182 :                   && TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
   20358          159 :                   && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
   20359          341 :                   && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
   20360          159 :                 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
   20361          159 :                                          TREE_VEC_ELT (declv, i));
   20362              :           }
   20363              :         else
   20364         1076 :           body = push_stmt_list ();
   20365         1219 :         RECUR (OMP_FOR_BODY (t));
   20366         1219 :         if (any_range_for)
   20367          143 :           body = finish_omp_structured_block (body);
   20368              :         else
   20369         1076 :           body = pop_stmt_list (body);
   20370              : 
   20371         1219 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20372         1059 :           t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
   20373              :                               orig_declv, initv, condv, incrv, body, pre_body,
   20374              :                               NULL, clauses);
   20375              :         else
   20376              :           {
   20377          160 :             t = make_node (TREE_CODE (t));
   20378          160 :             TREE_TYPE (t) = void_type_node;
   20379          160 :             OMP_FOR_BODY (t) = body;
   20380          160 :             OMP_FOR_PRE_BODY (t) = pre_body;
   20381          160 :             OMP_FOR_CLAUSES (t) = clauses;
   20382          160 :             SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
   20383          160 :             add_stmt (t);
   20384              :           }
   20385              : 
   20386         1219 :         add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
   20387              :                                         t));
   20388         1219 :         pop_omp_privatization_clauses (r);
   20389              : 
   20390         1219 :         if (any_range_for && !processing_template_decl && t)
   20391          267 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20392          144 :             if (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i)
   20393          144 :                 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t),
   20394              :                                             i)) == TREE_LIST)
   20395              :               {
   20396          144 :                 tree v = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   20397          144 :                 if (TREE_CHAIN (v) == NULL_TREE
   20398          144 :                     || TREE_CODE (TREE_CHAIN (v)) != TREE_VEC)
   20399            5 :                   continue;
   20400          139 :                 v = TREE_VEC_ELT (TREE_CHAIN (v), 0);
   20401          139 :                 gcc_assert (VAR_P (v) && DECL_NAME (v) == NULL_TREE);
   20402          139 :                 DECL_NAME (v) = for_range_identifier;
   20403              :               }
   20404              :       }
   20405         1219 :       break;
   20406              : 
   20407           19 :     case OMP_SECTIONS:
   20408           19 :     case OMP_MASKED:
   20409           19 :       omp_parallel_combined_clauses = NULL;
   20410              :       /* FALLTHRU */
   20411          472 :     case OMP_SINGLE:
   20412          472 :     case OMP_SCOPE:
   20413          472 :     case OMP_TEAMS:
   20414          472 :     case OMP_CRITICAL:
   20415          472 :     case OMP_TASKGROUP:
   20416          472 :     case OMP_SCAN:
   20417          472 :       r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
   20418          472 :                                           && OMP_TEAMS_COMBINED (t));
   20419          472 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
   20420              :                                 in_decl);
   20421          472 :       if (TREE_CODE (t) == OMP_TEAMS)
   20422              :         {
   20423          242 :           keep_next_level (true);
   20424          242 :           stmt = begin_omp_structured_block ();
   20425          242 :           RECUR (OMP_BODY (t));
   20426          242 :           stmt = finish_omp_structured_block (stmt);
   20427              :         }
   20428              :       else
   20429              :         {
   20430          230 :           stmt = push_stmt_list ();
   20431          230 :           RECUR (OMP_BODY (t));
   20432          230 :           stmt = pop_stmt_list (stmt);
   20433              :         }
   20434              : 
   20435          472 :       if (TREE_CODE (t) == OMP_CRITICAL
   20436           18 :           && tmp != NULL_TREE
   20437          484 :           && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
   20438              :         {
   20439            6 :           error_at (OMP_CLAUSE_LOCATION (tmp),
   20440              :                     "%<#pragma omp critical%> with %<hint%> clause requires "
   20441              :                     "a name, except when %<omp_sync_hint_none%> is used");
   20442            6 :           RETURN (error_mark_node);
   20443              :         }
   20444          466 :       t = copy_node (t);
   20445          466 :       OMP_BODY (t) = stmt;
   20446          466 :       OMP_CLAUSES (t) = tmp;
   20447          466 :       add_stmt (t);
   20448          466 :       pop_omp_privatization_clauses (r);
   20449          466 :       break;
   20450              : 
   20451          114 :     case OMP_DEPOBJ:
   20452          114 :       r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
   20453          114 :       if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
   20454              :         {
   20455          105 :           enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
   20456          105 :           if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
   20457              :             {
   20458           58 :               tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
   20459              :                                         args, complain, in_decl);
   20460           58 :               if (tmp == NULL_TREE)
   20461            6 :                 tmp = error_mark_node;
   20462              :             }
   20463              :           else
   20464              :             {
   20465           94 :               kind = (enum omp_clause_depend_kind)
   20466           47 :                      tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
   20467           47 :               tmp = NULL_TREE;
   20468              :             }
   20469          105 :           finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
   20470              :         }
   20471              :       else
   20472            9 :         finish_omp_depobj (EXPR_LOCATION (t), r,
   20473              :                            OMP_CLAUSE_DEPEND_INVALID,
   20474            9 :                            OMP_DEPOBJ_CLAUSES (t));
   20475              :       break;
   20476              : 
   20477          851 :     case OACC_DATA:
   20478          851 :     case OMP_TARGET_DATA:
   20479          851 :     case OMP_TARGET:
   20480         1687 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
   20481              :                                 TREE_CODE (t) == OACC_DATA
   20482              :                                 ? C_ORT_ACC
   20483              :                                 : TREE_CODE (t) == OMP_TARGET
   20484          836 :                                 ? C_ORT_OMP_TARGET : C_ORT_OMP,
   20485              :                                 args, complain, in_decl);
   20486          851 :       keep_next_level (true);
   20487          851 :       stmt = begin_omp_structured_block ();
   20488              : 
   20489          851 :       RECUR (OMP_BODY (t));
   20490          851 :       stmt = finish_omp_structured_block (stmt);
   20491              : 
   20492          851 :       t = copy_node (t);
   20493          851 :       OMP_BODY (t) = stmt;
   20494          851 :       OMP_CLAUSES (t) = tmp;
   20495              : 
   20496          851 :       if (TREE_CODE (t) == OMP_TARGET)
   20497          825 :         finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
   20498              :                                    &OMP_CLAUSES (t));
   20499              : 
   20500          851 :       if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
   20501              :         {
   20502           82 :           tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
   20503           82 :           if (teams)
   20504              :             /* For combined target teams, ensure the num_teams and
   20505              :                thread_limit clause expressions are evaluated on the host,
   20506              :                before entering the target construct.  */
   20507          160 :             for (tree c = OMP_TEAMS_CLAUSES (teams);
   20508          160 :                  c; c = OMP_CLAUSE_CHAIN (c))
   20509           78 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
   20510           78 :                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
   20511          141 :                 for (int i = 0;
   20512          219 :                      i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
   20513          141 :                   if (OMP_CLAUSE_OPERAND (c, i)
   20514          141 :                       && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
   20515              :                     {
   20516           42 :                       tree expr = OMP_CLAUSE_OPERAND (c, i);
   20517           42 :                       expr = force_target_expr (TREE_TYPE (expr), expr,
   20518              :                                                 tf_none);
   20519           42 :                       if (expr == error_mark_node)
   20520            0 :                         continue;
   20521           42 :                       tmp = TARGET_EXPR_SLOT (expr);
   20522           42 :                       add_stmt (expr);
   20523           42 :                       OMP_CLAUSE_OPERAND (c, i) = expr;
   20524           42 :                       tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   20525              :                                                   OMP_CLAUSE_FIRSTPRIVATE);
   20526           42 :                       OMP_CLAUSE_DECL (tc) = tmp;
   20527           42 :                       OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
   20528           42 :                       OMP_TARGET_CLAUSES (t) = tc;
   20529              :                     }
   20530              :         }
   20531          851 :       add_stmt (t);
   20532          851 :       break;
   20533              : 
   20534            1 :     case OACC_DECLARE:
   20535            1 :       t = copy_node (t);
   20536            1 :       tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
   20537              :                                 complain, in_decl);
   20538            1 :       OACC_DECLARE_CLAUSES (t) = tmp;
   20539            1 :       add_stmt (t);
   20540            1 :       break;
   20541              : 
   20542          168 :     case OMP_TARGET_UPDATE:
   20543          168 :     case OMP_TARGET_ENTER_DATA:
   20544          168 :     case OMP_TARGET_EXIT_DATA:
   20545          272 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t),
   20546              :                                 (TREE_CODE (t) == OMP_TARGET_EXIT_DATA
   20547              :                                  ? C_ORT_OMP_EXIT_DATA : C_ORT_OMP), args,
   20548              :                                 complain, in_decl);
   20549          168 :       t = copy_node (t);
   20550          168 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   20551          168 :       add_stmt (t);
   20552          168 :       break;
   20553              : 
   20554          395 :     case OACC_CACHE:
   20555          395 :     case OACC_ENTER_DATA:
   20556          395 :     case OACC_EXIT_DATA:
   20557          395 :     case OACC_UPDATE:
   20558          395 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
   20559              :                                 complain, in_decl);
   20560          395 :       t = copy_node (t);
   20561          395 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   20562          395 :       add_stmt (t);
   20563          395 :       break;
   20564              : 
   20565           33 :     case OMP_ORDERED:
   20566           33 :       tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
   20567              :                                 complain, in_decl);
   20568           33 :       if (OMP_BODY (t))
   20569              :         {
   20570            3 :           stmt = push_stmt_list ();
   20571            3 :           RECUR (OMP_BODY (t));
   20572            3 :           stmt = pop_stmt_list (stmt);
   20573              :         }
   20574              :       else
   20575           30 :         stmt = NULL_TREE;
   20576              : 
   20577           33 :       t = copy_node (t);
   20578           33 :       OMP_BODY (t) = stmt;
   20579           33 :       OMP_ORDERED_CLAUSES (t) = tmp;
   20580           33 :       add_stmt (t);
   20581           33 :       break;
   20582              : 
   20583           73 :     case OMP_MASTER:
   20584           73 :     case OMP_STRUCTURED_BLOCK:
   20585           73 :       omp_parallel_combined_clauses = NULL;
   20586              :       /* FALLTHRU */
   20587           92 :     case OMP_SECTION:
   20588           92 :       stmt = push_stmt_list ();
   20589           92 :       RECUR (OMP_BODY (t));
   20590           92 :       stmt = pop_stmt_list (stmt);
   20591              : 
   20592           92 :       t = copy_node (t);
   20593           92 :       OMP_BODY (t) = stmt;
   20594           92 :       add_stmt (t);
   20595           92 :       break;
   20596              : 
   20597           12 :     case OMP_DISPATCH:
   20598           12 :       tmp = tsubst_omp_clauses (OMP_DISPATCH_CLAUSES (t), C_ORT_OMP, args,
   20599              :                                 complain, in_decl);
   20600           12 :       stmt = RECUR (OMP_DISPATCH_BODY (t));
   20601           12 :       t = copy_node (t);
   20602           12 :       OMP_DISPATCH_BODY (t) = stmt;
   20603           12 :       OMP_DISPATCH_CLAUSES (t) = tmp;
   20604           12 :       add_stmt (t);
   20605           12 :       break;
   20606              : 
   20607          609 :     case OMP_ATOMIC:
   20608          609 :       gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
   20609          609 :       tmp = NULL_TREE;
   20610          609 :       if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
   20611           30 :         tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
   20612              :                                   complain, in_decl);
   20613          609 :       if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
   20614              :         {
   20615          184 :           tree op1 = TREE_OPERAND (t, 1);
   20616          184 :           tree rhs1 = NULL_TREE;
   20617          184 :           tree r = NULL_TREE;
   20618          184 :           tree lhs, rhs;
   20619          184 :           if (TREE_CODE (op1) == COMPOUND_EXPR)
   20620              :             {
   20621           99 :               rhs1 = RECUR (TREE_OPERAND (op1, 0));
   20622           99 :               op1 = TREE_OPERAND (op1, 1);
   20623              :             }
   20624          184 :           if (TREE_CODE (op1) == COND_EXPR)
   20625              :             {
   20626           46 :               gcc_assert (rhs1 == NULL_TREE);
   20627           46 :               tree c = TREE_OPERAND (op1, 0);
   20628           46 :               if (TREE_CODE (c) == MODIFY_EXPR)
   20629              :                 {
   20630           14 :                   r = RECUR (TREE_OPERAND (c, 0));
   20631           14 :                   c = TREE_OPERAND (c, 1);
   20632              :                 }
   20633           46 :               gcc_assert (TREE_CODE (c) == EQ_EXPR);
   20634           46 :               rhs = RECUR (TREE_OPERAND (c, 1));
   20635           46 :               lhs = RECUR (TREE_OPERAND (op1, 2));
   20636           46 :               rhs1 = RECUR (TREE_OPERAND (op1, 1));
   20637              :             }
   20638              :           else
   20639              :             {
   20640          138 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   20641          138 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   20642              :             }
   20643          184 :           finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
   20644              :                              lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
   20645          184 :                              tmp, OMP_ATOMIC_MEMORY_ORDER (t),
   20646          184 :                              OMP_ATOMIC_WEAK (t));
   20647              :         }
   20648              :       else
   20649              :         {
   20650          425 :           tree op1 = TREE_OPERAND (t, 1);
   20651          425 :           tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
   20652          425 :           tree rhs1 = NULL_TREE, r = NULL_TREE;
   20653          425 :           enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
   20654          425 :           enum tree_code opcode = NOP_EXPR;
   20655          425 :           if (code == OMP_ATOMIC_READ)
   20656              :             {
   20657          166 :               v = RECUR (TREE_OPERAND (op1, 0));
   20658          166 :               lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   20659              :             }
   20660          259 :           else if (code == OMP_ATOMIC_CAPTURE_OLD
   20661          259 :                    || code == OMP_ATOMIC_CAPTURE_NEW)
   20662              :             {
   20663          233 :               tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
   20664          233 :               v = RECUR (TREE_OPERAND (op1, 0));
   20665          233 :               lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   20666          233 :               if (TREE_CODE (op11) == COMPOUND_EXPR)
   20667              :                 {
   20668           91 :                   rhs1 = RECUR (TREE_OPERAND (op11, 0));
   20669           91 :                   op11 = TREE_OPERAND (op11, 1);
   20670              :                 }
   20671          233 :               if (TREE_CODE (op11) == COND_EXPR)
   20672              :                 {
   20673           78 :                   gcc_assert (rhs1 == NULL_TREE);
   20674           78 :                   tree c = TREE_OPERAND (op11, 0);
   20675           78 :                   if (TREE_CODE (c) == MODIFY_EXPR)
   20676              :                     {
   20677           36 :                       r = RECUR (TREE_OPERAND (c, 0));
   20678           36 :                       c = TREE_OPERAND (c, 1);
   20679              :                     }
   20680           78 :                   gcc_assert (TREE_CODE (c) == EQ_EXPR);
   20681           78 :                   rhs = RECUR (TREE_OPERAND (c, 1));
   20682           78 :                   lhs = RECUR (TREE_OPERAND (op11, 2));
   20683           78 :                   rhs1 = RECUR (TREE_OPERAND (op11, 1));
   20684              :                 }
   20685              :               else
   20686              :                 {
   20687          155 :                   lhs = RECUR (TREE_OPERAND (op11, 0));
   20688          155 :                   rhs = RECUR (TREE_OPERAND (op11, 1));
   20689              :                 }
   20690          233 :               opcode = TREE_CODE (op11);
   20691          233 :               if (opcode == MODIFY_EXPR)
   20692           11 :                 opcode = NOP_EXPR;
   20693              :             }
   20694              :           else
   20695              :             {
   20696           26 :               code = OMP_ATOMIC;
   20697           26 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   20698           26 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   20699              :             }
   20700          425 :           finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
   20701              :                              lhs1, rhs1, r, tmp,
   20702          425 :                              OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
   20703              :         }
   20704              :       break;
   20705              : 
   20706           54 :     case OMP_METADIRECTIVE:
   20707           54 :       {
   20708           54 :         tree variants = NULL_TREE;
   20709          166 :         for (tree v = OMP_METADIRECTIVE_VARIANTS (t); v; v = TREE_CHAIN (v))
   20710              :           {
   20711          112 :             tree ctx = OMP_METADIRECTIVE_VARIANT_SELECTOR (v);
   20712          112 :             tree directive = OMP_METADIRECTIVE_VARIANT_DIRECTIVE (v);
   20713          112 :             tree body = OMP_METADIRECTIVE_VARIANT_BODY (v);
   20714          112 :             tree s;
   20715              : 
   20716              :             /* CTX is null if this is the default variant.  */
   20717          112 :             if (ctx)
   20718              :               {
   20719           58 :                 ctx = tsubst_omp_context_selector (ctx, args, complain,
   20720              :                                                    in_decl);
   20721              :                 /* Remove the selector from further consideration if it can be
   20722              :                    evaluated as a non-match at this point.  */
   20723           58 :                 if (omp_context_selector_matches (ctx, NULL_TREE, false) == 0)
   20724           16 :                   continue;
   20725              :               }
   20726           96 :             s = push_stmt_list ();
   20727           96 :             RECUR (directive);
   20728           96 :             directive = pop_stmt_list (s);
   20729           96 :             if (body)
   20730              :               {
   20731           36 :                 s = push_stmt_list ();
   20732           36 :                 RECUR (body);
   20733           36 :                 body = pop_stmt_list (s);
   20734              :               }
   20735           96 :             variants
   20736           96 :               = chainon (variants,
   20737              :                          make_omp_metadirective_variant (ctx, directive,
   20738              :                                                          body));
   20739              :           }
   20740           54 :         t = copy_node (t);
   20741           54 :         OMP_METADIRECTIVE_VARIANTS (t) = variants;
   20742              : 
   20743              :         /* Try to resolve the metadirective early.  */
   20744           54 :         vec<struct omp_variant> candidates
   20745           54 :           = omp_early_resolve_metadirective (t);
   20746           54 :         if (!candidates.is_empty ())
   20747           54 :           t = c_omp_expand_variant_construct (candidates);
   20748           54 :         add_stmt (t);
   20749           54 :         break;
   20750              :       }
   20751              : 
   20752            0 :     case OMP_DECLARE_MAPPER:
   20753            0 :       {
   20754            0 :         t = copy_node (t);
   20755              : 
   20756            0 :         tree decl = OMP_DECLARE_MAPPER_DECL (t);
   20757            0 :         decl = tsubst (decl, args, complain, in_decl);
   20758            0 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20759            0 :         tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t);
   20760            0 :         clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_MAPPER, args,
   20761              :                                       complain, in_decl);
   20762            0 :         TREE_TYPE (t) = type;
   20763            0 :         OMP_DECLARE_MAPPER_DECL (t) = decl;
   20764            0 :         OMP_DECLARE_MAPPER_CLAUSES (t) = clauses;
   20765            0 :         RETURN (t);
   20766              :       }
   20767              : 
   20768           80 :     case TRANSACTION_EXPR:
   20769           80 :       {
   20770           80 :         int flags = 0;
   20771           80 :         flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
   20772           80 :         flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
   20773              : 
   20774           80 :         if (TRANSACTION_EXPR_IS_STMT (t))
   20775              :           {
   20776           47 :             tree body = TRANSACTION_EXPR_BODY (t);
   20777           47 :             tree noex = NULL_TREE;
   20778           47 :             if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
   20779              :               {
   20780           12 :                 noex = MUST_NOT_THROW_COND (body);
   20781           12 :                 if (noex == NULL_TREE)
   20782            3 :                   noex = boolean_true_node;
   20783           12 :                 body = TREE_OPERAND (body, 0);
   20784              :               }
   20785           47 :             stmt = begin_transaction_stmt (input_location, NULL, flags);
   20786           47 :             RECUR (body);
   20787           47 :             finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
   20788              :           }
   20789              :         else
   20790              :           {
   20791           33 :             stmt = build_transaction_expr (EXPR_LOCATION (t),
   20792           33 :                                            RECUR (TRANSACTION_EXPR_BODY (t)),
   20793              :                                            flags, NULL_TREE);
   20794           33 :             RETURN (stmt);
   20795              :           }
   20796              :       }
   20797           47 :       break;
   20798              : 
   20799           36 :     case OMP_INTEROP:
   20800           36 :       tmp = tsubst_omp_clauses (OMP_INTEROP_CLAUSES (t), C_ORT_OMP_INTEROP,
   20801              :                                 args, complain, in_decl);
   20802           36 :       t = copy_node (t);
   20803           36 :       OMP_INTEROP_CLAUSES (t) = tmp;
   20804           36 :       add_stmt (t);
   20805           36 :       break;
   20806              : 
   20807           21 :     case MUST_NOT_THROW_EXPR:
   20808           21 :       {
   20809           21 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20810           21 :         tree cond = RECUR (MUST_NOT_THROW_COND (t));
   20811           21 :         stmt = build_must_not_throw_expr (op0, cond);
   20812           21 :         if (stmt && TREE_CODE (stmt) == MUST_NOT_THROW_EXPR)
   20813              :           {
   20814           18 :             MUST_NOT_THROW_NOEXCEPT_P (stmt) = MUST_NOT_THROW_NOEXCEPT_P (t);
   20815           18 :             MUST_NOT_THROW_THROW_P (stmt) = MUST_NOT_THROW_THROW_P (t);
   20816           18 :             MUST_NOT_THROW_CATCH_P (stmt) = MUST_NOT_THROW_CATCH_P (t);
   20817              :           }
   20818           21 :         RETURN (stmt);
   20819              :       }
   20820              : 
   20821            0 :     case EXPR_PACK_EXPANSION:
   20822            0 :       error ("invalid use of pack expansion expression");
   20823            0 :       RETURN (error_mark_node);
   20824              : 
   20825            0 :     case NONTYPE_ARGUMENT_PACK:
   20826            0 :       error ("use %<...%> to expand argument pack");
   20827            0 :       RETURN (error_mark_node);
   20828              : 
   20829       195460 :     case COMPOUND_EXPR:
   20830       195460 :       tmp = RECUR (TREE_OPERAND (t, 0));
   20831       195460 :       if (tmp == NULL_TREE)
   20832              :         /* If the first operand was a statement, we're done with it.  */
   20833       123978 :         RETURN (RECUR (TREE_OPERAND (t, 1)));
   20834        71482 :       RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
   20835              :                                     RECUR (TREE_OPERAND (t, 1)),
   20836              :                                     templated_operator_saved_lookups (t),
   20837              :                                     complain));
   20838              : 
   20839       286409 :     case PREDICT_EXPR:
   20840       286409 :       RETURN (add_stmt (copy_node (t)));
   20841              : 
   20842         9889 :     case ANNOTATE_EXPR:
   20843         9889 :       {
   20844              :         /* Although ANNOTATE_EXPR is an expression, it can only appear in
   20845              :            WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
   20846              :            using tsubst_stmt rather than tsubst_expr and can contain
   20847              :            DECL_EXPRs.  */
   20848         9889 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   20849         9889 :         tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   20850         9889 :         tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
   20851         9889 :         if (TREE_CODE (op2) == INTEGER_CST
   20852         9889 :             && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
   20853         9874 :           op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
   20854              :                                         op3);
   20855         9889 :         RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
   20856              :                             TREE_TYPE (op1), op1, op2, op3));
   20857              :       }
   20858              : 
   20859    151099540 :     default:
   20860    151099540 :       gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
   20861              : 
   20862    151099540 :       RETURN (tsubst_expr (t, args, complain, in_decl));
   20863              :     }
   20864              : 
   20865    189279759 :   RETURN (NULL_TREE);
   20866    340871112 :  out:
   20867    340871112 :   input_location = loc;
   20868    340871112 :   return r;
   20869              : #undef RECUR
   20870              : #undef RETURN
   20871              : }
   20872              : 
   20873              : /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
   20874              :    function.  For description of the body see comment above
   20875              :    cp_parser_omp_declare_reduction_exprs.  */
   20876              : 
   20877              : static void
   20878          104 : tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   20879              : {
   20880          104 :   if (t == NULL_TREE || t == error_mark_node)
   20881            0 :     return;
   20882              : 
   20883          104 :   gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
   20884              : 
   20885          104 :   tree_stmt_iterator tsi;
   20886          104 :   int i;
   20887          104 :   tree stmts[7];
   20888          104 :   memset (stmts, 0, sizeof stmts);
   20889          104 :   for (i = 0, tsi = tsi_start (t);
   20890          619 :        i < 7 && !tsi_end_p (tsi);
   20891          515 :        i++, tsi_next (&tsi))
   20892          515 :     stmts[i] = tsi_stmt (tsi);
   20893          104 :   gcc_assert (tsi_end_p (tsi));
   20894              : 
   20895          104 :   if (i >= 3)
   20896              :     {
   20897          104 :       gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
   20898              :                   && TREE_CODE (stmts[1]) == DECL_EXPR);
   20899          104 :       tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
   20900              :                              args, complain, in_decl);
   20901          104 :       tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
   20902              :                             args, complain, in_decl);
   20903              :       /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
   20904              :          expect to be pushing it.  */
   20905          104 :       DECL_CONTEXT (omp_out) = current_function_decl;
   20906          104 :       DECL_CONTEXT (omp_in) = current_function_decl;
   20907          104 :       keep_next_level (true);
   20908          104 :       tree block = begin_omp_structured_block ();
   20909          104 :       tsubst_stmt (stmts[2], args, complain, in_decl);
   20910          104 :       block = finish_omp_structured_block (block);
   20911          104 :       block = maybe_cleanup_point_expr_void (block);
   20912          104 :       add_decl_expr (omp_out);
   20913          104 :       copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
   20914          104 :       add_decl_expr (omp_in);
   20915          104 :       finish_expr_stmt (block);
   20916              :     }
   20917          104 :   if (i >= 6)
   20918              :     {
   20919           57 :       gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
   20920              :                   && TREE_CODE (stmts[4]) == DECL_EXPR);
   20921           57 :       tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
   20922              :                               args, complain, in_decl);
   20923           57 :       tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
   20924              :                               args, complain, in_decl);
   20925           57 :       DECL_CONTEXT (omp_priv) = current_function_decl;
   20926           57 :       DECL_CONTEXT (omp_orig) = current_function_decl;
   20927           57 :       keep_next_level (true);
   20928           57 :       tree block = begin_omp_structured_block ();
   20929           57 :       tsubst_stmt (stmts[5], args, complain, in_decl);
   20930           57 :       block = finish_omp_structured_block (block);
   20931           57 :       block = maybe_cleanup_point_expr_void (block);
   20932           57 :       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
   20933           57 :       add_decl_expr (omp_priv);
   20934           57 :       add_decl_expr (omp_orig);
   20935           57 :       finish_expr_stmt (block);
   20936           57 :       if (i == 7)
   20937           32 :         add_decl_expr (omp_orig);
   20938              :     }
   20939              : }
   20940              : 
   20941              : /* T is a postfix-expression that is not being used in a function
   20942              :    call.  Return the substituted version of T.  */
   20943              : 
   20944              : static tree
   20945     34888842 : tsubst_non_call_postfix_expression (tree t, tree args,
   20946              :                                     tsubst_flags_t complain,
   20947              :                                     tree in_decl)
   20948              : {
   20949     34888842 :   if (TREE_CODE (t) == SCOPE_REF)
   20950        17881 :     t = tsubst_qualified_id (t, args, complain, in_decl,
   20951              :                              /*done=*/false, /*address_p=*/false);
   20952              :   else
   20953     34870961 :     t = tsubst_expr (t, args, complain, in_decl);
   20954              : 
   20955     34888842 :   return t;
   20956              : }
   20957              : 
   20958              : /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
   20959              :    LAMBDA_EXPR_CAPTURE_LIST passed in LIST.  Do deduction for a previously
   20960              :    dependent init-capture.  EXPLICIT_P is true if the original list had
   20961              :    explicit captures.  */
   20962              : 
   20963              : static void
   20964       231855 : prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
   20965              :                      tsubst_flags_t complain)
   20966              : {
   20967       231855 :   if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
   20968              :     {
   20969          510 :       tree type = NULL_TREE;
   20970          510 :       if (!init)
   20971              :         {
   20972            3 :           if (complain & tf_error)
   20973            3 :             error ("empty initializer in lambda init-capture");
   20974            3 :           init = error_mark_node;
   20975              :         }
   20976          507 :       else if (TREE_CODE (init) == TREE_LIST)
   20977            0 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   20978          510 :       if (!type)
   20979          510 :         type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
   20980          510 :       TREE_TYPE (field) = type;
   20981          510 :       cp_apply_type_quals_to_decl (cp_type_quals (type), field);
   20982              :     }
   20983       231855 :   list = tree_cons (field, init, list);
   20984       231855 :   LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
   20985       231855 : }
   20986              : 
   20987              : /* T is a LAMBDA_EXPR.  Generate a new LAMBDA_EXPR for the current
   20988              :    instantiation context.  Instantiating a pack expansion containing a lambda
   20989              :    might result in multiple lambdas all based on the same lambda in the
   20990              :    template.  */
   20991              : 
   20992              : tree
   20993       193638 : tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   20994              : {
   20995       193638 :   tree oldfn = lambda_function (t);
   20996       193638 :   in_decl = oldfn;
   20997              : 
   20998       193638 :   args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
   20999       193638 :   if (processing_template_decl
   21000       193638 :       && (!in_template_context || (complain & tf_partial)
   21001         1404 :           || LAMBDA_EXPR_EXTRA_ARGS (t)))
   21002              :     {
   21003              :       /* Defer templated substitution into a lambda-expr if we lost the
   21004              :          necessary template context.  This may happen for a lambda-expr
   21005              :          used as a default template argument.
   21006              : 
   21007              :          Defer dependent substitution as well so that we don't prematurely
   21008              :          lower the level of a deduced return type or any other auto or
   21009              :          template parameter belonging to the lambda.
   21010              : 
   21011              :          Finally, if a substitution into this lambda was previously
   21012              :          deferred, keep deferring until the final (non-templated)
   21013              :          substitution.  */
   21014          678 :       t = copy_node (t);
   21015          678 :       LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
   21016          678 :       LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain);
   21017          678 :       return t;
   21018              :     }
   21019              : 
   21020       192960 :   tree r = build_lambda_expr ();
   21021              : 
   21022       192960 :   LAMBDA_EXPR_LOCATION (r) = LAMBDA_EXPR_LOCATION (t);
   21023       192960 :   LAMBDA_EXPR_CONSTEVAL_BLOCK_P (r) = LAMBDA_EXPR_CONSTEVAL_BLOCK_P (t);
   21024       192960 :   LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
   21025       192960 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   21026         2726 :     LAMBDA_EXPR_REGEN_INFO (r)
   21027         1363 :       = build_template_info (t, add_to_template_args (TI_ARGS (ti),
   21028              :                                                       preserve_args (args)));
   21029              :   else
   21030       383194 :     LAMBDA_EXPR_REGEN_INFO (r)
   21031       191597 :       = build_template_info (t, preserve_args (args));
   21032              : 
   21033       192960 :   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
   21034              : 
   21035       192960 :   vec<tree,va_gc>* field_packs = NULL;
   21036       192960 :   unsigned name_independent_cnt = 0;
   21037       424513 :   for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
   21038       231553 :        cap = TREE_CHAIN (cap))
   21039              :     {
   21040       231559 :       tree ofield = TREE_PURPOSE (cap);
   21041       231559 :       tree init = TREE_VALUE (cap);
   21042       231559 :       if (PACK_EXPANSION_P (init))
   21043          368 :         init = tsubst_pack_expansion (init, args, complain, in_decl);
   21044              :       else
   21045       231191 :         init = tsubst_expr (init, args, complain, in_decl);
   21046              : 
   21047       231559 :       if (init == error_mark_node)
   21048            6 :         return error_mark_node;
   21049              : 
   21050       231553 :       if (init && TREE_CODE (init) == TREE_LIST)
   21051           12 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   21052              : 
   21053       231553 :       if (!processing_template_decl
   21054       230428 :           && init && TREE_CODE (init) != TREE_VEC
   21055       461619 :           && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
   21056              :         {
   21057              :           /* For a VLA, simply tsubsting the field type won't work, we need to
   21058              :              go through add_capture again.  XXX do we want to do this for all
   21059              :              captures?  */
   21060            9 :           tree name = (get_identifier
   21061              :                        (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
   21062            9 :           tree ftype = TREE_TYPE (ofield);
   21063            9 :           bool by_ref = (TYPE_REF_P (ftype)
   21064            9 :                          || (TREE_CODE (ftype) == DECLTYPE_TYPE
   21065            0 :                              && DECLTYPE_FOR_REF_CAPTURE (ftype)));
   21066            9 :           add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield),
   21067              :                        &name_independent_cnt);
   21068            9 :           continue;
   21069            9 :         }
   21070              : 
   21071       231544 :       if (PACK_EXPANSION_P (ofield))
   21072          356 :         ofield = PACK_EXPANSION_PATTERN (ofield);
   21073       231544 :       tree field = tsubst_decl (ofield, args, complain);
   21074              : 
   21075       231544 :       if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
   21076              :         {
   21077              :           /* Remember these for when we've pushed local_specializations.  */
   21078          169 :           vec_safe_push (field_packs, ofield);
   21079          169 :           vec_safe_push (field_packs, field);
   21080              :         }
   21081              : 
   21082       231544 :       if (field == error_mark_node)
   21083              :         return error_mark_node;
   21084              : 
   21085       231544 :       if (TREE_CODE (field) == TREE_VEC)
   21086              :         {
   21087          359 :           int len = TREE_VEC_LENGTH (field);
   21088          359 :           gcc_assert (TREE_CODE (init) == TREE_VEC
   21089              :                       && TREE_VEC_LENGTH (init) == len);
   21090         1029 :           for (int i = 0; i < len; ++i)
   21091         1340 :             prepend_one_capture (TREE_VEC_ELT (field, i),
   21092          670 :                                  TREE_VEC_ELT (init, i),
   21093          670 :                                  LAMBDA_EXPR_CAPTURE_LIST (r),
   21094          670 :                                  LAMBDA_CAPTURE_EXPLICIT_P (cap),
   21095              :                                  complain);
   21096              :         }
   21097              :       else
   21098              :         {
   21099       462370 :           prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
   21100       231185 :                                LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
   21101              : 
   21102       231185 :           if (id_equal (DECL_NAME (field), "__this"))
   21103        29888 :             LAMBDA_EXPR_THIS_CAPTURE (r) = field;
   21104              :         }
   21105              :     }
   21106              : 
   21107       192954 :   tree type = begin_lambda_type (r);
   21108       192954 :   if (type == error_mark_node)
   21109              :     {
   21110            0 :       gcc_checking_assert (!(complain & tf_error) || seen_error ());
   21111            0 :       return error_mark_node;
   21112              :     }
   21113              : 
   21114       192954 :   if (LAMBDA_EXPR_EXTRA_SCOPE (t)
   21115              :       /* When evaluating a concept we instantiate any lambda bodies
   21116              :          in the context of the evaluation.  For ABI reasons don't
   21117              :          record a scope for this instantiated lambda so we don't
   21118              :          throw off the scope counter.  */
   21119       192954 :       && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (t)) != CONCEPT_DECL)
   21120       191532 :     record_lambda_scope (r);
   21121       192954 :   if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
   21122              :     /* If we're pushed into another scope (PR105652), fix it.  */
   21123         2396 :     TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
   21124         2396 :       = TYPE_CONTEXT (TREE_TYPE (t));
   21125       192954 :   DECL_SOURCE_LOCATION (TYPE_NAME (type))
   21126       192954 :     = DECL_SOURCE_LOCATION (TYPE_NAME (TREE_TYPE (t)));
   21127       192954 :   record_lambda_scope_discriminator (r);
   21128              : 
   21129              :   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
   21130       192954 :   determine_visibility (TYPE_NAME (type));
   21131              : 
   21132       192954 :   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
   21133              : 
   21134       192954 :   tree oldtmpl = (generic_lambda_fn_p (oldfn)
   21135       192954 :                   ? DECL_TI_TEMPLATE (oldfn)
   21136        37689 :                   : NULL_TREE);
   21137              : 
   21138        37689 :   tree tparms = NULL_TREE;
   21139        37689 :   if (oldtmpl)
   21140        37689 :     tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
   21141              : 
   21142       192954 :   tree fntype = static_fn_type (oldfn);
   21143              : 
   21144       192954 :   begin_scope (sk_lambda, NULL_TREE);
   21145              : 
   21146              :   /* Like in cp_parser_lambda_expression, we need to bring the captures
   21147              :      into the lambda scope.  */
   21148       192954 :   tree ns = decl_namespace_context (type);
   21149       192954 :   push_nested_namespace (ns);
   21150       192954 :   push_nested_class (type);
   21151       192954 :   tree dummy_fco = maybe_add_dummy_lambda_op (r);
   21152       192954 :   pop_nested_class ();
   21153       192954 :   pop_nested_namespace (ns);
   21154       192954 :   push_capture_proxies (r, /*early_p=*/true);
   21155              : 
   21156       192954 :   tree saved_ctp = current_template_parms;
   21157       192954 :   if (oldtmpl)
   21158              :     {
   21159        37689 :       ++processing_template_decl;
   21160        37689 :       current_template_parms = tparms;
   21161              :     }
   21162       192954 :   fntype = tsubst (fntype, args, complain, in_decl);
   21163       192954 :   if (oldtmpl)
   21164              :     {
   21165        37689 :       current_template_parms = saved_ctp;
   21166        37689 :       --processing_template_decl;
   21167              :     }
   21168              : 
   21169              :   /* We are about to create the real operator(), so get rid of the old one.  */
   21170       192954 :   if (dummy_fco)
   21171       116282 :     remove_dummy_lambda_op (dummy_fco, r);
   21172              : 
   21173       192954 :   if (fntype == error_mark_node)
   21174              :     r = error_mark_node;
   21175              :   else
   21176              :     {
   21177              :       /* Fix the type of 'this'.
   21178              :          For static and xobj member functions we use this to transport the
   21179              :          lambda's closure type.  It appears that in the regular case the
   21180              :          object parameter is still pulled off, and then re-added again anyway.
   21181              :          So perhaps we could do something better here?  */
   21182       192942 :       fntype = build_memfn_type (fntype, type,
   21183              :                                  type_memfn_quals (fntype),
   21184              :                                  type_memfn_rqual (fntype));
   21185       192942 :       tree inst = (oldtmpl
   21186       192942 :                    ? tsubst_template_decl (oldtmpl, args, complain,
   21187              :                                            fntype, tparms)
   21188       155253 :                    : tsubst_function_decl (oldfn, args, complain, fntype));
   21189       192942 :       if (inst == error_mark_node)
   21190              :         {
   21191            0 :           r = error_mark_node;
   21192            0 :           goto out;
   21193              :         }
   21194       192942 :       finish_member_declaration (inst);
   21195       192942 :       record_lambda_scope_sig_discriminator (r, inst);
   21196              : 
   21197       230631 :       tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
   21198              : 
   21199              :       /* Let finish_function set this.  */
   21200       192942 :       DECL_DECLARED_CONSTEXPR_P (fn) = false;
   21201              : 
   21202              :       /* The body of a lambda-expression is not a subexpression of the
   21203              :          enclosing expression.  */
   21204       192942 :       cp_evaluated ev;
   21205              : 
   21206              :       /* Now we're done with the parameter-declaration-clause, and should
   21207              :          assume "const" unless "mutable" was present.  */
   21208       192942 :       LAMBDA_EXPR_CONST_QUAL_P (r) = LAMBDA_EXPR_CONST_QUAL_P (t);
   21209              : 
   21210       192942 :       bool nested = cfun;
   21211       192942 :       if (nested)
   21212       186864 :         push_function_context ();
   21213              :       else
   21214              :         /* Still increment function_depth so that we don't GC in the
   21215              :            middle of an expression.  */
   21216         6078 :         ++function_depth;
   21217              : 
   21218       192942 :       local_specialization_stack s (lss_copy);
   21219              : 
   21220       192942 :       bool save_in_consteval_if_p = in_consteval_if_p;
   21221       192942 :       in_consteval_if_p = false;
   21222              : 
   21223       192942 :       tree body = start_lambda_function (fn, r);
   21224              : 
   21225              :       /* Now record them for lookup_init_capture_pack.  */
   21226       192942 :       int fplen = vec_safe_length (field_packs);
   21227       193111 :       for (int i = 0; i < fplen; )
   21228              :         {
   21229          169 :           tree pack = (*field_packs)[i++];
   21230          169 :           tree inst = (*field_packs)[i++];
   21231          169 :           register_local_specialization (inst, pack);
   21232              :         }
   21233       192942 :       release_tree_vector (field_packs);
   21234              : 
   21235       192942 :       register_parameter_specializations (oldfn, fn);
   21236              : 
   21237       192942 :       if (oldtmpl)
   21238              :         {
   21239              :           /* We might not partially instantiate some parts of the function, so
   21240              :              copy these flags from the original template.  */
   21241        37689 :           language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
   21242        37689 :           current_function_returns_value = ol->returns_value;
   21243        37689 :           current_function_returns_null = ol->returns_null;
   21244        37689 :           current_function_returns_abnormally = ol->returns_abnormally;
   21245        37689 :           current_function_infinite_loop = ol->infinite_loop;
   21246              :         }
   21247              : 
   21248              :       /* [temp.deduct] A lambda-expression appearing in a function type or a
   21249              :          template parameter is not considered part of the immediate context for
   21250              :          the purposes of template argument deduction. */
   21251       192942 :       if (!emitting_diagnostic_p ())
   21252       192921 :         complain = tf_warning_or_error;
   21253              : 
   21254       192942 :       tree saved = DECL_SAVED_TREE (oldfn);
   21255       192942 :       if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
   21256              :         /* We already have a body block from start_lambda_function, we don't
   21257              :            need another to confuse NRV (91217).  */
   21258       192942 :         saved = BIND_EXPR_BODY (saved);
   21259              : 
   21260       192942 :       tsubst_stmt (saved, args, complain, r);
   21261              : 
   21262       192942 :       finish_lambda_function (body);
   21263              : 
   21264       192942 :       in_consteval_if_p = save_in_consteval_if_p;
   21265              : 
   21266       192942 :       if (nested)
   21267       186864 :         pop_function_context ();
   21268              :       else
   21269         6078 :         --function_depth;
   21270              : 
   21271              :       /* The capture list was built up in reverse order; fix that now.  */
   21272       192942 :       LAMBDA_EXPR_CAPTURE_LIST (r)
   21273       192942 :         = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
   21274              : 
   21275       192942 :       maybe_add_lambda_conv_op (type);
   21276       192942 :     }
   21277              : 
   21278       192954 : out:
   21279       192954 :   pop_bindings_and_leave_scope ();
   21280       192954 :   finish_struct (type, /*attr*/NULL_TREE);
   21281              : 
   21282       192954 :   insert_pending_capture_proxies ();
   21283              : 
   21284       192954 :   return r;
   21285              : }
   21286              : 
   21287              : /* Subroutine of maybe_fold_fn_template_args.  */
   21288              : 
   21289              : static bool
   21290     31326097 : fold_targs_r (tree targs, tsubst_flags_t complain)
   21291              : {
   21292     31326097 :   int len = TREE_VEC_LENGTH (targs);
   21293     67789208 :   for (int i = 0; i < len; ++i)
   21294              :     {
   21295     36463114 :       tree &elt = TREE_VEC_ELT (targs, i);
   21296     36463114 :       if (!elt || TYPE_P (elt)
   21297       653367 :           || TREE_CODE (elt) == TEMPLATE_DECL)
   21298     35810565 :         continue;
   21299       652549 :       if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
   21300              :         {
   21301            0 :           if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
   21302              :             return false;
   21303              :         }
   21304       652549 :       else if (/* We can only safely preevaluate scalar prvalues.  */
   21305      1305092 :                SCALAR_TYPE_P (TREE_TYPE (elt))
   21306       649360 :                && !glvalue_p (elt)
   21307      1293500 :                && !TREE_CONSTANT (elt))
   21308              :         {
   21309         3290 :           elt = cxx_constant_value (elt, complain);
   21310         3290 :           if (elt == error_mark_node)
   21311              :             return false;
   21312              :         }
   21313              :     }
   21314              : 
   21315              :   return true;
   21316              : }
   21317              : 
   21318              : /* Try to do constant evaluation of any explicit template arguments in FN
   21319              :    before overload resolution, to get any errors only once.  Return true iff
   21320              :    we didn't have any problems folding.  */
   21321              : 
   21322              : static bool
   21323    103809473 : maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
   21324              : {
   21325    103809473 :   if (processing_template_decl || fn == NULL_TREE)
   21326              :     return true;
   21327     95753879 :   if (fn == error_mark_node)
   21328              :     return false;
   21329     95453024 :   if (TREE_CODE (fn) == OFFSET_REF
   21330     95388470 :       || TREE_CODE (fn) == COMPONENT_REF)
   21331     15515106 :     fn = TREE_OPERAND (fn, 1);
   21332     95453024 :   if (BASELINK_P (fn))
   21333     25624419 :     fn = BASELINK_FUNCTIONS (fn);
   21334     95453024 :   if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
   21335              :     return true;
   21336     31326097 :   tree targs = TREE_OPERAND (fn, 1);
   21337     31326097 :   if (targs == NULL_TREE)
   21338              :     return true;
   21339     31326097 :   if (targs == error_mark_node)
   21340              :     return false;
   21341     31326097 :   return fold_targs_r (targs, complain);
   21342              : }
   21343              : 
   21344              : /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling.  */
   21345              : 
   21346              : static void
   21347    103866318 : tsubst_call_args (tree t, tree args, tsubst_flags_t complain,
   21348              :                   tree in_decl, releasing_vec &call_args)
   21349              : {
   21350    103866318 :   unsigned int nargs = call_expr_nargs (t);
   21351    179986115 :   for (unsigned int i = 0; i < nargs; ++i)
   21352              :     {
   21353     76119800 :       tree arg = CALL_EXPR_ARG (t, i);
   21354              : 
   21355     76119800 :       if (!PACK_EXPANSION_P (arg))
   21356     75533665 :         vec_safe_push (call_args, tsubst_expr (arg, args, complain, in_decl));
   21357              :       else
   21358              :         {
   21359              :           /* Expand the pack expansion and push each entry onto CALL_ARGS.  */
   21360       586135 :           arg = tsubst_pack_expansion (arg, args, complain, in_decl);
   21361       586135 :           if (TREE_CODE (arg) == TREE_VEC)
   21362              :             {
   21363       530972 :               unsigned int len, j;
   21364              : 
   21365       530972 :               len = TREE_VEC_LENGTH (arg);
   21366       931825 :               for (j = 0; j < len; ++j)
   21367              :                 {
   21368       400853 :                   tree value = TREE_VEC_ELT (arg, j);
   21369       400853 :                   if (value != NULL_TREE)
   21370       400853 :                     value = convert_from_reference (value);
   21371       400853 :                   vec_safe_push (call_args, value);
   21372              :                 }
   21373              :             }
   21374              :           else
   21375              :             /* A partial substitution.  Add one entry.  */
   21376        55163 :             vec_safe_push (call_args, arg);
   21377              :         }
   21378              :     }
   21379    103866315 : }
   21380              : 
   21381              : /* Like tsubst but deals with expressions and performs semantic
   21382              :    analysis.  */
   21383              : 
   21384              : tree
   21385   1757906710 : tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   21386              : {
   21387              : #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
   21388              : #define RECUR(NODE)                                             \
   21389              :   tsubst_expr (NODE, args, complain, in_decl)
   21390              : 
   21391   1757906710 :   tree retval, op1;
   21392   1757906710 :   location_t save_loc;
   21393              : 
   21394   1757906710 :   if (t == NULL_TREE || t == error_mark_node)
   21395              :     return t;
   21396              : 
   21397   1753960299 :   save_loc = input_location;
   21398   1753960299 :   if (location_t eloc = cp_expr_location (t))
   21399    696105298 :     input_location = eloc;
   21400              : 
   21401              :   /* N3276 decltype magic only applies to calls at the top level or on the
   21402              :      right side of a comma.  */
   21403   1753960299 :   tsubst_flags_t decltype_flag = (complain & tf_decltype);
   21404   1753960299 :   complain &= ~tf_decltype;
   21405              : 
   21406              :   /* This flag only applies to id-expressions at the top level, and
   21407              :      controls resolution thereof.  */
   21408   1753960299 :   tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup);
   21409   1753960299 :   complain &= ~tf_no_name_lookup;
   21410              : 
   21411   1753960299 :   if (instantiating_tu_local_entity (t))
   21412           52 :     RETURN (error_mark_node);
   21413              : 
   21414   1753960247 :   if (!no_name_lookup_flag)
   21415   1430669098 :     if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   21416              :       return d;
   21417              : 
   21418   1753958558 :   switch (TREE_CODE (t))
   21419              :     {
   21420       161606 :     case USING_DECL:
   21421       161606 :       t = DECL_NAME (t);
   21422              :       /* Fall through.  */
   21423    307430444 :     case IDENTIFIER_NODE:
   21424    307430444 :       {
   21425    307430444 :         tree decl;
   21426    307430444 :         cp_id_kind idk;
   21427    307430444 :         const char *error_msg;
   21428              : 
   21429    307430444 :         if (IDENTIFIER_CONV_OP_P (t))
   21430              :           {
   21431         1588 :             tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21432         1588 :             t = make_conv_op_name (new_type);
   21433              :           }
   21434              : 
   21435    307430444 :         if (no_name_lookup_flag)
   21436    307197585 :           RETURN (t);
   21437              : 
   21438              :         /* Look up the name.  */
   21439       232859 :         decl = lookup_name (t);
   21440              : 
   21441              :         /* By convention, expressions use ERROR_MARK_NODE to indicate
   21442              :            failure, not NULL_TREE.  */
   21443       232859 :         if (decl == NULL_TREE)
   21444           57 :           decl = error_mark_node;
   21445              : 
   21446       232859 :         decl = finish_id_expression (t, decl, NULL_TREE,
   21447              :                                      &idk,
   21448              :                                      /*i_c_e_p=*/false,
   21449              :                                      /*allow_i_c_e_p=*/true,
   21450              :                                      /*non_i_c_e_p=*/nullptr,
   21451              :                                      /*template_p=*/false,
   21452              :                                      /*done=*/true,
   21453              :                                      /*address_p=*/false,
   21454              :                                      /*template_arg_p=*/false,
   21455              :                                      &error_msg,
   21456              :                                      input_location);
   21457       232859 :         if (error_msg)
   21458            0 :           error (error_msg);
   21459       232859 :         if (identifier_p (decl))
   21460              :           {
   21461           57 :             if (complain & tf_error)
   21462           54 :               unqualified_name_lookup_error (decl);
   21463           57 :             decl = error_mark_node;
   21464              :           }
   21465    307430444 :         RETURN (decl);
   21466              :       }
   21467              : 
   21468     59967965 :     case TEMPLATE_ID_EXPR:
   21469     59967965 :       {
   21470     59967965 :         tree object;
   21471     59967965 :         tree templ = TREE_OPERAND (t, 0);
   21472     59967965 :         tree targs = TREE_OPERAND (t, 1);
   21473              : 
   21474     59967965 :         if (no_name_lookup_flag)
   21475      4498110 :           templ = tsubst_name (templ, args, complain, in_decl);
   21476              :         else
   21477     55469855 :           templ = tsubst_expr (templ, args, complain, in_decl);
   21478              : 
   21479     59967965 :         if (targs)
   21480     59967965 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   21481     59967965 :         if (targs == error_mark_node)
   21482         2657 :           RETURN (error_mark_node);
   21483              : 
   21484     59965308 :         if (TREE_CODE (templ) == SCOPE_REF)
   21485              :           {
   21486           44 :             tree name = TREE_OPERAND (templ, 1);
   21487           44 :             tree tid = lookup_template_function (name, targs);
   21488           44 :             TREE_OPERAND (templ, 1) = tid;
   21489           44 :             RETURN (templ);
   21490              :           }
   21491              : 
   21492     83941437 :         if (concept_definition_p (templ))
   21493              :           {
   21494      2945452 :             tree check = build_concept_check (templ, targs, complain);
   21495      2945452 :             if (check == error_mark_node)
   21496              :               RETURN (error_mark_node);
   21497     57019812 :             RETURN (check);
   21498              :           }
   21499              : 
   21500     57019812 :         if (variable_template_p (templ))
   21501              :           {
   21502     21030721 :             if (no_name_lookup_flag)
   21503           59 :               RETURN (lookup_template_variable (templ, targs, complain));
   21504              : 
   21505     21030662 :             tree r = lookup_and_finish_template_variable (templ, targs,
   21506              :                                                           complain);
   21507     21030662 :             r = convert_from_reference (r);
   21508     21030662 :             r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
   21509     21030662 :             RETURN (r);
   21510              :           }
   21511              : 
   21512     35989091 :         if (TREE_CODE (templ) == COMPONENT_REF)
   21513              :           {
   21514            0 :             object = TREE_OPERAND (templ, 0);
   21515            0 :             templ = TREE_OPERAND (templ, 1);
   21516              :           }
   21517              :         else
   21518              :           object = NULL_TREE;
   21519              : 
   21520     35989091 :         tree tid = lookup_template_function (templ, targs);
   21521     35989091 :         protected_set_expr_location (tid, EXPR_LOCATION (t));
   21522              : 
   21523     35989091 :         if (object)
   21524            0 :           RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
   21525              :                          object, tid, NULL_TREE));
   21526     35989091 :         else if (no_name_lookup_flag)
   21527      4497804 :           RETURN (tid);
   21528     31491287 :         else if (identifier_p (templ))
   21529              :           {
   21530              :             /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
   21531              :                name lookup found nothing when parsing the template name.  */
   21532            0 :             gcc_assert (cxx_dialect >= cxx20 || seen_error ());
   21533            0 :             RETURN (tid);
   21534              :           }
   21535              :         else
   21536     31491287 :           RETURN (baselink_for_fns (tid));
   21537              :       }
   21538              : 
   21539     50761603 :     case INDIRECT_REF:
   21540     50761603 :       {
   21541     50761603 :         tree r = RECUR (TREE_OPERAND (t, 0));
   21542              : 
   21543     50761603 :         if (REFERENCE_REF_P (t))
   21544              :           {
   21545              :             /* A type conversion to reference type will be enclosed in
   21546              :                such an indirect ref, but the substitution of the cast
   21547              :                will have also added such an indirect ref.  */
   21548     32618522 :             r = convert_from_reference (r);
   21549              :           }
   21550              :         else
   21551     18143081 :           r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
   21552              :                                     templated_operator_saved_lookups (t),
   21553              :                                     complain|decltype_flag);
   21554              : 
   21555     50761603 :         if (REF_PARENTHESIZED_P (t))
   21556         9508 :           r = force_paren_expr (r);
   21557              : 
   21558     50761603 :         RETURN (r);
   21559              :       }
   21560              : 
   21561            9 :     case MEM_REF:
   21562            9 :       {
   21563            9 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21564            9 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   21565            9 :         tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21566            9 :         RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1));
   21567              :       }
   21568              : 
   21569     16864490 :     case NOP_EXPR:
   21570     16864490 :       {
   21571     16864490 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21572     16864490 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21573     16864487 :         RETURN (build_nop (type, op0));
   21574              :       }
   21575              : 
   21576     29104900 :     case IMPLICIT_CONV_EXPR:
   21577     29104900 :       {
   21578     29104900 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21579     29104900 :         if (type == error_mark_node)
   21580            3 :           RETURN (error_mark_node);
   21581     29104897 :         tree expr = RECUR (TREE_OPERAND (t, 0));
   21582     58209794 :         if (dependent_implicit_conv_p (type, expr,
   21583     29104897 :                                        IMPLICIT_CONV_EXPR_FORCED (t)))
   21584              :           {
   21585      4909977 :             retval = copy_node (t);
   21586      4909977 :             TREE_TYPE (retval) = type;
   21587      4909977 :             TREE_OPERAND (retval, 0) = expr;
   21588      4909977 :             RETURN (retval);
   21589              :           }
   21590     24194920 :         if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
   21591              :           {
   21592     23259163 :             tree r = convert_nontype_argument (type, expr, complain);
   21593     23259163 :             if (r == NULL_TREE)
   21594           28 :               r = error_mark_node;
   21595     23259163 :             RETURN (r);
   21596              :           }
   21597       935757 :         int flags = LOOKUP_IMPLICIT;
   21598       935757 :         if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
   21599       214562 :           flags = LOOKUP_NORMAL;
   21600       935757 :         if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
   21601        21867 :           flags |= LOOKUP_NO_NARROWING;
   21602       935757 :         RETURN (perform_implicit_conversion_flags (type, expr, complain,
   21603              :                                                   flags));
   21604              :       }
   21605              : 
   21606        13802 :     case CONVERT_EXPR:
   21607        13802 :       {
   21608        13802 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21609        13802 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21610        13802 :         if (op0 == error_mark_node)
   21611           21 :           RETURN (error_mark_node);
   21612        13781 :         RETURN (build1 (CONVERT_EXPR, type, op0));
   21613              :       }
   21614              : 
   21615     34926948 :     case CAST_EXPR:
   21616     34926948 :     case REINTERPRET_CAST_EXPR:
   21617     34926948 :     case CONST_CAST_EXPR:
   21618     34926948 :     case DYNAMIC_CAST_EXPR:
   21619     34926948 :     case STATIC_CAST_EXPR:
   21620     34926948 :       {
   21621     34926948 :         tree type;
   21622     34926948 :         tree op, r = NULL_TREE;
   21623              : 
   21624     34926948 :         tsubst_flags_t tcomplain = complain;
   21625     34926948 :         if (TREE_CODE (t) == CAST_EXPR)
   21626     28512334 :           tcomplain |= tf_tst_ok;
   21627     34926948 :         type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   21628              : 
   21629     34926948 :         op = RECUR (TREE_OPERAND (t, 0));
   21630              : 
   21631     34926948 :         warning_sentinel s(warn_useless_cast);
   21632     34926948 :         warning_sentinel s2(warn_ignored_qualifiers);
   21633     34926948 :         warning_sentinel s3(warn_int_in_bool_context);
   21634     34926948 :         switch (TREE_CODE (t))
   21635              :           {
   21636     28512334 :           case CAST_EXPR:
   21637     28512334 :             r = build_functional_cast (input_location, type, op, complain);
   21638     28512334 :             break;
   21639        84874 :           case REINTERPRET_CAST_EXPR:
   21640        84874 :             r = build_reinterpret_cast (input_location, type, op, complain);
   21641        84874 :             break;
   21642       185749 :           case CONST_CAST_EXPR:
   21643       185749 :             r = build_const_cast (input_location, type, op, complain);
   21644       185749 :             break;
   21645         4328 :           case DYNAMIC_CAST_EXPR:
   21646         4328 :             r = build_dynamic_cast (input_location, type, op, complain);
   21647         4328 :             break;
   21648      6139663 :           case STATIC_CAST_EXPR:
   21649      6139663 :             r = build_static_cast (input_location, type, op, complain);
   21650      6139663 :             if (IMPLICIT_RVALUE_P (t))
   21651            0 :               set_implicit_rvalue_p (r);
   21652              :             break;
   21653            0 :           default:
   21654            0 :             gcc_unreachable ();
   21655              :           }
   21656              : 
   21657     34926945 :         RETURN (r);
   21658     34926945 :       }
   21659              : 
   21660        72920 :     case BIT_CAST_EXPR:
   21661        72920 :       {
   21662        72920 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21663        72920 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21664        72920 :         RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
   21665              :       }
   21666              : 
   21667       927385 :     case POSTDECREMENT_EXPR:
   21668       927385 :     case POSTINCREMENT_EXPR:
   21669       927385 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21670              :                                                 args, complain, in_decl);
   21671       927385 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
   21672              :                                 templated_operator_saved_lookups (t),
   21673              :                                 complain|decltype_flag));
   21674              : 
   21675       526759 :     case BIT_NOT_EXPR:
   21676       526759 :       if (identifier_p (TREE_OPERAND (t, 0)))
   21677              :         {
   21678           23 :           gcc_checking_assert (no_name_lookup_flag);
   21679           23 :           RETURN (t);
   21680              :         }
   21681       526736 :       else if (TYPE_P (TREE_OPERAND (t, 0)))
   21682              :         {
   21683       340367 :           gcc_checking_assert (no_name_lookup_flag);
   21684       340367 :           tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
   21685       340367 :           RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0));
   21686              :         }
   21687              :       /* Fall through.  */
   21688     14470399 :     case PREDECREMENT_EXPR:
   21689     14470399 :     case PREINCREMENT_EXPR:
   21690     14470399 :     case NEGATE_EXPR:
   21691     14470399 :     case ABS_EXPR:
   21692     14470399 :     case TRUTH_NOT_EXPR:
   21693     14470399 :     case UNARY_PLUS_EXPR:  /* Unary + */
   21694     14470399 :     case REALPART_EXPR:
   21695     14470399 :     case IMAGPART_EXPR:
   21696     14470399 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t),
   21697              :                                 RECUR (TREE_OPERAND (t, 0)),
   21698              :                                 templated_operator_saved_lookups (t),
   21699              :                                 complain|decltype_flag));
   21700              : 
   21701         2043 :     case EXCESS_PRECISION_EXPR:
   21702         2043 :       {
   21703         2043 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21704         2043 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21705         2043 :         if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
   21706            0 :           RETURN (op0);
   21707         2043 :         RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
   21708              :                             type, op0));
   21709              :       }
   21710              : 
   21711            0 :     case FIX_TRUNC_EXPR:
   21712              :       /* convert_like should have created an IMPLICIT_CONV_EXPR.  */
   21713            0 :       gcc_unreachable ();
   21714              : 
   21715      1088433 :     case ADDR_EXPR:
   21716      1088433 :       op1 = TREE_OPERAND (t, 0);
   21717      1088433 :       if (TREE_CODE (op1) == LABEL_DECL)
   21718           31 :         RETURN (finish_label_address_expr (DECL_NAME (op1),
   21719              :                                           EXPR_LOCATION (op1)));
   21720      1088402 :       if (TREE_CODE (op1) == SCOPE_REF)
   21721        72186 :         op1 = tsubst_qualified_id (op1, args, complain, in_decl,
   21722              :                                    /*done=*/true, /*address_p=*/true);
   21723              :       else
   21724      1016216 :         op1 = tsubst_non_call_postfix_expression (op1, args, complain,
   21725              :                                                   in_decl);
   21726      1088402 :       RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
   21727              :                                 templated_operator_saved_lookups (t),
   21728              :                                 complain|decltype_flag));
   21729              : 
   21730     78706555 :     case PLUS_EXPR:
   21731     78706555 :     case MINUS_EXPR:
   21732     78706555 :     case MULT_EXPR:
   21733     78706555 :     case TRUNC_DIV_EXPR:
   21734     78706555 :     case CEIL_DIV_EXPR:
   21735     78706555 :     case FLOOR_DIV_EXPR:
   21736     78706555 :     case ROUND_DIV_EXPR:
   21737     78706555 :     case EXACT_DIV_EXPR:
   21738     78706555 :     case BIT_AND_EXPR:
   21739     78706555 :     case BIT_IOR_EXPR:
   21740     78706555 :     case BIT_XOR_EXPR:
   21741     78706555 :     case TRUNC_MOD_EXPR:
   21742     78706555 :     case FLOOR_MOD_EXPR:
   21743     78706555 :     case TRUTH_ANDIF_EXPR:
   21744     78706555 :     case TRUTH_ORIF_EXPR:
   21745     78706555 :     case TRUTH_AND_EXPR:
   21746     78706555 :     case TRUTH_OR_EXPR:
   21747     78706555 :     case RSHIFT_EXPR:
   21748     78706555 :     case LSHIFT_EXPR:
   21749     78706555 :     case EQ_EXPR:
   21750     78706555 :     case NE_EXPR:
   21751     78706555 :     case MAX_EXPR:
   21752     78706555 :     case MIN_EXPR:
   21753     78706555 :     case LE_EXPR:
   21754     78706555 :     case GE_EXPR:
   21755     78706555 :     case LT_EXPR:
   21756     78706555 :     case GT_EXPR:
   21757     78706555 :     case SPACESHIP_EXPR:
   21758     78706555 :     case MEMBER_REF:
   21759     78706555 :     case DOTSTAR_EXPR:
   21760     78706555 :       {
   21761              :         /* If either OP0 or OP1 was value- or type-dependent, suppress
   21762              :            warnings that depend on the range of the types involved.  */
   21763     78706555 :         tree op0 = TREE_OPERAND (t, 0);
   21764     78706555 :         tree op1 = TREE_OPERAND (t, 1);
   21765     78706555 :         const bool was_dep = (dependent_operand_p (op0)
   21766     78706555 :                               || dependent_operand_p (op1));
   21767     78706555 :         op0 = RECUR (op0);
   21768     78706555 :         op1 = RECUR (op1);
   21769              : 
   21770     78706510 :         warning_sentinel s1(warn_type_limits, was_dep);
   21771     78706510 :         warning_sentinel s2(warn_div_by_zero, was_dep);
   21772     78706510 :         warning_sentinel s3(warn_logical_op, was_dep);
   21773     78706510 :         warning_sentinel s4(warn_tautological_compare, was_dep);
   21774     78706510 :         warning_sentinel s5(warn_address, was_dep);
   21775              : 
   21776     78706510 :         tree r = build_x_binary_op
   21777    305352803 :           (input_location, TREE_CODE (t),
   21778              :            op0,
   21779     78706510 :            (warning_suppressed_p (TREE_OPERAND (t, 0))
   21780              :             ? ERROR_MARK
   21781     69233273 :             : TREE_CODE (TREE_OPERAND (t, 0))),
   21782              :            op1,
   21783     78706510 :            (warning_suppressed_p (TREE_OPERAND (t, 1))
   21784              :             ? ERROR_MARK
   21785     67487333 :             : TREE_CODE (TREE_OPERAND (t, 1))),
   21786              :            templated_operator_saved_lookups (t),
   21787              :            /*overload=*/NULL,
   21788              :            complain|decltype_flag);
   21789     78706480 :         if (EXPR_P (r))
   21790     78661994 :           copy_warning (r, t);
   21791              : 
   21792     78706480 :         RETURN (r);
   21793     78706480 :       }
   21794              : 
   21795            6 :     case POINTER_PLUS_EXPR:
   21796            6 :       {
   21797            6 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21798            6 :         if (op0 == error_mark_node)
   21799            0 :           RETURN (error_mark_node);
   21800            6 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21801            6 :         if (op1 == error_mark_node)
   21802            0 :           RETURN (error_mark_node);
   21803            6 :         RETURN (fold_build_pointer_plus (op0, op1));
   21804              :       }
   21805              : 
   21806     75300120 :     case SCOPE_REF:
   21807     75300120 :       if (no_name_lookup_flag)
   21808              :         {
   21809          768 :           tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
   21810          768 :           tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl);
   21811          768 :           RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1,
   21812              :                                         QUALIFIED_NAME_IS_TEMPLATE (t)));
   21813              :         }
   21814              :       else
   21815     75299352 :         RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
   21816              :                                     /*address_p=*/false));
   21817              : 
   21818      8993307 :     case BASELINK:
   21819      8993307 :       RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
   21820              :                                args, complain, in_decl));
   21821              : 
   21822      2418959 :     case ARRAY_REF:
   21823      2418959 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21824              :                                                 args, complain, in_decl);
   21825      2418959 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
   21826      2418959 :           && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
   21827         3136 :               == ovl_op_identifier (ARRAY_REF)))
   21828              :         {
   21829          391 :           tree c = TREE_OPERAND (t, 1);
   21830          391 :           releasing_vec index_exp_list;
   21831          391 :           tsubst_call_args (c, args, complain, in_decl, index_exp_list);
   21832              : 
   21833          391 :           tree r;
   21834          391 :           if (vec_safe_length (index_exp_list) == 1
   21835          452 :               && !PACK_EXPANSION_P (index_exp_list[0]))
   21836           61 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   21837           61 :                                  index_exp_list[0], NULL,
   21838              :                                  complain | decltype_flag);
   21839              :           else
   21840          330 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   21841              :                                  NULL_TREE, &index_exp_list,
   21842              :                                  complain | decltype_flag);
   21843          391 :           RETURN (r);
   21844          391 :         }
   21845      2418568 :       RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
   21846              :                                  RECUR (TREE_OPERAND (t, 1)),
   21847              :                                  complain|decltype_flag));
   21848              : 
   21849           30 :     case OMP_ARRAY_SECTION:
   21850           30 :       {
   21851           30 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21852           30 :         tree op1 = NULL_TREE, op2 = NULL_TREE;
   21853           30 :         if (op0 == error_mark_node)
   21854            0 :           RETURN (error_mark_node);
   21855           30 :         if (TREE_OPERAND (t, 1))
   21856              :           {
   21857           27 :             op1 = RECUR (TREE_OPERAND (t, 1));
   21858           27 :             if (op1 == error_mark_node)
   21859            0 :               RETURN (error_mark_node);
   21860              :           }
   21861           30 :         if (TREE_OPERAND (t, 2))
   21862              :           {
   21863           30 :             op2 = RECUR (TREE_OPERAND (t, 2));
   21864           30 :             if (op2 == error_mark_node)
   21865            0 :               RETURN (error_mark_node);
   21866              :           }
   21867           30 :         RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2));
   21868              :       }
   21869              : 
   21870            2 :     case OMP_DECLARE_MAPPER:
   21871            2 :       {
   21872            2 :         t = copy_node (t);
   21873              : 
   21874            2 :         tree decl = OMP_DECLARE_MAPPER_DECL (t);
   21875            2 :         DECL_OMP_DECLARE_MAPPER_P (decl) = 1;
   21876            2 :         decl = tsubst (decl, args, complain, in_decl);
   21877            2 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21878            2 :         tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t);
   21879            2 :         clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_MAPPER, args,
   21880              :                                       complain, in_decl);
   21881            2 :         TREE_TYPE (t) = type;
   21882            2 :         OMP_DECLARE_MAPPER_DECL (t) = decl;
   21883            2 :         OMP_DECLARE_MAPPER_CLAUSES (t) = clauses;
   21884            2 :         RETURN (t);
   21885              :       }
   21886              : 
   21887      7143091 :     case SIZEOF_EXPR:
   21888     12596065 :       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
   21889     12590836 :           || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
   21890              :         {
   21891      1695358 :           tree expanded, op = TREE_OPERAND (t, 0);
   21892      1695358 :           int len = 0;
   21893              : 
   21894      1695358 :           if (SIZEOF_EXPR_TYPE_P (t))
   21895            0 :             op = TREE_TYPE (op);
   21896              : 
   21897      1695358 :           ++cp_unevaluated_operand;
   21898      1695358 :           ++c_inhibit_evaluation_warnings;
   21899              :           /* We only want to compute the number of arguments.  */
   21900      1695358 :           if (PACK_EXPANSION_P (op))
   21901              :             {
   21902      1695346 :               expanded = NULL_TREE;
   21903      1695346 :               if (DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (op)))
   21904              :                 {
   21905          389 :                   tree d = PACK_EXPANSION_PATTERN (op);
   21906          389 :                   if (DECL_HAS_VALUE_EXPR_P (d))
   21907              :                     {
   21908          389 :                       d = DECL_VALUE_EXPR (d);
   21909          389 :                       if (TREE_CODE (d) == TREE_VEC)
   21910              :                         {
   21911          202 :                           tree b = TREE_VEC_ELT (d, 0);
   21912          202 :                           if (!type_dependent_expression_p_push (b))
   21913              :                             {
   21914          202 :                               expanded = void_node;
   21915          202 :                               len = TREE_VEC_LENGTH (d) - 2;
   21916              :                             }
   21917              :                         }
   21918              :                     }
   21919              :                 }
   21920          202 :               if (!expanded)
   21921      1695144 :                 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
   21922              :             }
   21923              :           else
   21924           12 :             expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
   21925              :                                              args, complain, in_decl);
   21926      1695358 :           --cp_unevaluated_operand;
   21927      1695358 :           --c_inhibit_evaluation_warnings;
   21928              : 
   21929      1695358 :           if (TREE_CODE (expanded) == TREE_VEC)
   21930              :             {
   21931      1071564 :               len = TREE_VEC_LENGTH (expanded);
   21932              :               /* Set TREE_USED for the benefit of -Wunused.  */
   21933      3113880 :               for (int i = 0; i < len; i++)
   21934      2042316 :                 if (DECL_P (TREE_VEC_ELT (expanded, i)))
   21935         5751 :                   TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
   21936              :             }
   21937              : 
   21938      1695358 :           if (expanded == error_mark_node)
   21939            0 :             RETURN (error_mark_node);
   21940      1695358 :           else if (PACK_EXPANSION_P (expanded)
   21941      1695358 :                    || (TREE_CODE (expanded) == TREE_VEC
   21942      1071564 :                        && pack_expansion_args_count (expanded)))
   21943              : 
   21944              :             {
   21945       623619 :               if (PACK_EXPANSION_P (expanded))
   21946              :                 /* OK.  */;
   21947              :               else
   21948           27 :                 expanded = make_argument_pack (expanded);
   21949              : 
   21950       623619 :               if (TYPE_P (expanded))
   21951       623384 :                 RETURN (cxx_sizeof_or_alignof_type (input_location,
   21952              :                                                     expanded, SIZEOF_EXPR,
   21953              :                                                     false,
   21954              :                                                     complain & tf_error));
   21955              :               else
   21956          235 :                 RETURN (cxx_sizeof_or_alignof_expr (input_location,
   21957              :                                                     expanded, SIZEOF_EXPR,
   21958              :                                                     false,
   21959              :                                                     complain & tf_error));
   21960              :             }
   21961              :           else
   21962      1071739 :             RETURN (build_int_cst (size_type_node, len));
   21963              :         }
   21964              :       /* Fall through */
   21965              : 
   21966      6203898 :     case ALIGNOF_EXPR:
   21967      6203898 :       {
   21968      6203898 :         tree r;
   21969              : 
   21970      6203898 :         op1 = TREE_OPERAND (t, 0);
   21971      6203898 :         if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
   21972            0 :           op1 = TREE_TYPE (op1);
   21973      6203898 :         bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
   21974      6203898 :                             && ALIGNOF_EXPR_STD_P (t));
   21975      6203898 :         if (!args)
   21976              :           {
   21977              :             /* When there are no ARGS, we are trying to evaluate a
   21978              :                non-dependent expression from the parser.  Trying to do
   21979              :                the substitutions may not work.  */
   21980       152098 :             if (!TYPE_P (op1))
   21981          324 :               op1 = TREE_TYPE (op1);
   21982              :           }
   21983              :         else
   21984              :           {
   21985      6051800 :             ++cp_unevaluated_operand;
   21986      6051800 :             ++c_inhibit_evaluation_warnings;
   21987      6051800 :             if (TYPE_P (op1))
   21988      5911208 :               op1 = tsubst (op1, args, complain, in_decl);
   21989              :             else
   21990       140592 :               op1 = tsubst_expr (op1, args, complain, in_decl);
   21991      6051800 :             --cp_unevaluated_operand;
   21992      6051800 :             --c_inhibit_evaluation_warnings;
   21993              :           }
   21994      6203898 :         if (TYPE_P (op1))
   21995      6063287 :           r = cxx_sizeof_or_alignof_type (input_location,
   21996      6063287 :                                           op1, TREE_CODE (t), std_alignof,
   21997              :                                           complain & tf_error);
   21998              :         else
   21999       140611 :           r = cxx_sizeof_or_alignof_expr (input_location,
   22000       140611 :                                           op1, TREE_CODE (t), std_alignof,
   22001              :                                           complain & tf_error);
   22002      6203898 :         if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
   22003              :           {
   22004      5446998 :             if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
   22005              :               {
   22006      5446890 :                 if (!processing_template_decl && TYPE_P (op1))
   22007              :                   {
   22008      4984164 :                     r = build_min (SIZEOF_EXPR, size_type_node,
   22009              :                                    build1 (NOP_EXPR, op1, error_mark_node));
   22010      4984164 :                     SIZEOF_EXPR_TYPE_P (r) = 1;
   22011              :                   }
   22012              :                 else
   22013       462726 :                   r = build_min (SIZEOF_EXPR, size_type_node, op1);
   22014      5446890 :                 TREE_SIDE_EFFECTS (r) = 0;
   22015      5446890 :                 TREE_READONLY (r) = 1;
   22016              :               }
   22017      5446998 :             SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
   22018      5446998 :             copy_warning (r, t);
   22019              :           }
   22020      6203898 :         RETURN (r);
   22021              :       }
   22022              : 
   22023            0 :     case AT_ENCODE_EXPR:
   22024            0 :       {
   22025            0 :         op1 = TREE_OPERAND (t, 0);
   22026            0 :         ++cp_unevaluated_operand;
   22027            0 :         ++c_inhibit_evaluation_warnings;
   22028            0 :         op1 = tsubst (op1, args, complain, in_decl);
   22029            0 :         --cp_unevaluated_operand;
   22030            0 :         --c_inhibit_evaluation_warnings;
   22031            0 :         RETURN (objc_build_encode_expr (op1));
   22032              :       }
   22033              : 
   22034       832538 :     case NOEXCEPT_EXPR:
   22035       832538 :       op1 = TREE_OPERAND (t, 0);
   22036       832538 :       ++cp_unevaluated_operand;
   22037       832538 :       ++c_inhibit_evaluation_warnings;
   22038       832538 :       ++cp_noexcept_operand;
   22039       832538 :       op1 = tsubst_expr (op1, args, complain, in_decl);
   22040       832538 :       --cp_unevaluated_operand;
   22041       832538 :       --c_inhibit_evaluation_warnings;
   22042       832538 :       --cp_noexcept_operand;
   22043       832538 :       RETURN (finish_noexcept_expr (op1, complain));
   22044              : 
   22045     15248480 :     case MODOP_EXPR:
   22046     15248480 :       {
   22047     15248480 :         warning_sentinel s(warn_div_by_zero);
   22048     15248480 :         tree lhs = RECUR (TREE_OPERAND (t, 0));
   22049     15248480 :         tree rhs = RECUR (TREE_OPERAND (t, 2));
   22050              : 
   22051     15248480 :         tree r = build_x_modify_expr
   22052     15248480 :           (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
   22053              :            templated_operator_saved_lookups (t),
   22054              :            complain|decltype_flag);
   22055              :         /* TREE_NO_WARNING must be set if either the expression was
   22056              :            parenthesized or it uses an operator such as >>= rather
   22057              :            than plain assignment.  In the former case, it was already
   22058              :            set and must be copied.  In the latter case,
   22059              :            build_x_modify_expr sets it and it must not be reset
   22060              :            here.  */
   22061     15248480 :         if (warning_suppressed_p (t, OPT_Wparentheses))
   22062        13450 :           suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
   22063              : 
   22064     15248480 :         RETURN (r);
   22065     15248480 :       }
   22066              : 
   22067      3227795 :     case ARROW_EXPR:
   22068      3227795 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   22069              :                                                 args, complain, in_decl);
   22070              :       /* Remember that there was a reference to this entity.  */
   22071      3227795 :       if (DECL_P (op1)
   22072      3227795 :           && !mark_used (op1, complain) && !(complain & tf_error))
   22073            0 :         RETURN (error_mark_node);
   22074      3227795 :       RETURN (build_x_arrow (input_location, op1, complain));
   22075              : 
   22076       846172 :     case NEW_EXPR:
   22077       846172 :       {
   22078       846172 :         tree placement = RECUR (TREE_OPERAND (t, 0));
   22079       846172 :         tree init = RECUR (TREE_OPERAND (t, 3));
   22080       846172 :         vec<tree, va_gc> *placement_vec;
   22081       846172 :         vec<tree, va_gc> *init_vec;
   22082       846172 :         tree ret;
   22083       846172 :         location_t loc = EXPR_LOCATION (t);
   22084              : 
   22085       846172 :         if (placement == NULL_TREE)
   22086       226360 :           placement_vec = NULL;
   22087       619812 :         else if (placement == error_mark_node)
   22088            3 :           RETURN (error_mark_node);
   22089              :         else
   22090              :           {
   22091       619809 :             placement_vec = make_tree_vector ();
   22092      1239618 :             for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
   22093       619809 :               vec_safe_push (placement_vec, TREE_VALUE (placement));
   22094              :           }
   22095              : 
   22096              :         /* If there was an initializer in the original tree, but it
   22097              :            instantiated to an empty list, then we should pass a
   22098              :            non-NULL empty vector to tell build_new that it was an
   22099              :            empty initializer() rather than no initializer.  This can
   22100              :            only happen when the initializer is a pack expansion whose
   22101              :            parameter packs are of length zero.  */
   22102       846169 :         if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
   22103       216914 :           init_vec = NULL;
   22104       629255 :         else if (init == error_mark_node)
   22105            6 :           RETURN (error_mark_node);
   22106              :         else
   22107              :           {
   22108       629249 :             init_vec = make_tree_vector ();
   22109       629249 :             if (init == void_node)
   22110         9550 :               gcc_assert (init_vec != NULL);
   22111              :             else
   22112              :               {
   22113      1131239 :                 for (; init != NULL_TREE; init = TREE_CHAIN (init))
   22114       511540 :                   vec_safe_push (init_vec, TREE_VALUE (init));
   22115              :               }
   22116              :           }
   22117              : 
   22118              :         /* Avoid passing an enclosing decl to valid_array_size_p.  */
   22119       846163 :         in_decl = NULL_TREE;
   22120              : 
   22121       846163 :         tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
   22122       846163 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   22123       846163 :         ret = build_new (loc, &placement_vec, op1, op2,
   22124       846163 :                          &init_vec, NEW_EXPR_USE_GLOBAL (t),
   22125              :                          complain);
   22126              : 
   22127       846163 :         if (placement_vec != NULL)
   22128       845947 :           release_tree_vector (placement_vec);
   22129       846163 :         if (init_vec != NULL)
   22130       499141 :           release_tree_vector (init_vec);
   22131              : 
   22132       846172 :         RETURN (ret);
   22133              :       }
   22134              : 
   22135        50130 :     case DELETE_EXPR:
   22136        50130 :       {
   22137        50130 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22138        50130 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   22139        50130 :         RETURN (delete_sanity (input_location, op0, op1,
   22140              :                                DELETE_EXPR_USE_VEC (t),
   22141              :                                DELETE_EXPR_USE_GLOBAL (t),
   22142              :                                complain));
   22143              :       }
   22144              : 
   22145       173620 :     case COMPOUND_EXPR:
   22146       173620 :       {
   22147       173620 :         tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args,
   22148              :                                 complain & ~tf_decltype, in_decl);
   22149       173620 :         RETURN (build_x_compound_expr (EXPR_LOCATION (t),
   22150              :                                        op0,
   22151              :                                        RECUR (TREE_OPERAND (t, 1)),
   22152              :                                        templated_operator_saved_lookups (t),
   22153              :                                        complain|decltype_flag));
   22154              :       }
   22155              : 
   22156    103908446 :     case CALL_EXPR:
   22157    103908446 :       {
   22158    103908446 :         tree function;
   22159    103908446 :         unsigned int nargs;
   22160    103908446 :         bool qualified_p;
   22161    103908446 :         bool koenig_p;
   22162    103908446 :         tree ret;
   22163              : 
   22164    103908446 :         function = CALL_EXPR_FN (t);
   22165              :         /* Internal function with no arguments.  */
   22166    103908446 :         if (function == NULL_TREE && call_expr_nargs (t) == 0)
   22167    103594082 :           RETURN (t);
   22168              : 
   22169              :         /* When we parsed the expression, we determined whether or
   22170              :            not Koenig lookup should be performed.  */
   22171    103866026 :         koenig_p = KOENIG_LOOKUP_P (t);
   22172    103866026 :         if (function == NULL_TREE)
   22173              :           {
   22174              :             koenig_p = false;
   22175              :             qualified_p = false;
   22176              :           }
   22177    103865417 :         else if (TREE_CODE (function) == SCOPE_REF)
   22178              :           {
   22179      4720541 :             qualified_p = true;
   22180      4720541 :             function = tsubst_qualified_id (function, args, complain, in_decl,
   22181              :                                             /*done=*/false,
   22182              :                                             /*address_p=*/false);
   22183              :           }
   22184     99144876 :         else if (CALL_EXPR_STATIC_CHAIN (t)
   22185           99 :                  && TREE_CODE (function) == FUNCTION_DECL
   22186     99144975 :                  && fndecl_built_in_p (function, BUILT_IN_CLASSIFY_TYPE))
   22187              :           {
   22188           99 :             tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain,
   22189              :                                 in_decl);
   22190           99 :             if (dependent_type_p (type))
   22191              :               {
   22192            0 :                 ret = build_vl_exp (CALL_EXPR, 4);
   22193            0 :                 CALL_EXPR_FN (ret) = function;
   22194            0 :                 CALL_EXPR_STATIC_CHAIN (ret) = type;
   22195            0 :                 CALL_EXPR_ARG (ret, 0)
   22196            0 :                   = build_min (SIZEOF_EXPR, size_type_node, type);
   22197            0 :                 TREE_TYPE (ret) = integer_type_node;
   22198              :               }
   22199              :             else
   22200           99 :               ret = build_int_cst (integer_type_node, type_to_class (type));
   22201           99 :             RETURN (ret);
   22202              :           }
   22203     99144777 :         else if (koenig_p
   22204     99144777 :                  && (identifier_p (function)
   22205      7676551 :                      || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22206      2567635 :                          && identifier_p (TREE_OPERAND (function, 0)))))
   22207              :           {
   22208              :             /* Do nothing; calling tsubst_expr on an identifier
   22209              :                would incorrectly perform unqualified lookup again.
   22210              : 
   22211              :                Note that we can also have an IDENTIFIER_NODE if the earlier
   22212              :                unqualified lookup found a dependent local extern declaration
   22213              :                (as per finish_call_expr); in that case koenig_p will be false
   22214              :                and we do want to do the lookup again to find the substituted
   22215              :                declaration.  */
   22216       180958 :             qualified_p = false;
   22217              : 
   22218       180958 :             if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
   22219         4672 :               function = tsubst_name (function, args, complain, in_decl);
   22220              :           }
   22221              :         else
   22222              :           {
   22223     98963819 :             if (TREE_CODE (function) == COMPONENT_REF)
   22224              :               {
   22225     16665160 :                 tree op = TREE_OPERAND (function, 1);
   22226              : 
   22227     16665160 :                 qualified_p = (TREE_CODE (op) == SCOPE_REF
   22228     16665160 :                                || (BASELINK_P (op)
   22229     10642712 :                                    && BASELINK_QUALIFIED_P (op)));
   22230              :               }
   22231              :             else
   22232              :               qualified_p = false;
   22233              : 
   22234     98963819 :             if (TREE_CODE (function) == ADDR_EXPR
   22235     98963819 :                 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
   22236              :               /* Avoid error about taking the address of a constructor.  */
   22237       144363 :               function = TREE_OPERAND (function, 0);
   22238              : 
   22239     98963819 :             function = tsubst_expr (function, args, complain, in_decl);
   22240              : 
   22241     98963819 :             if (BASELINK_P (function))
   22242      8906971 :               qualified_p = true;
   22243              :           }
   22244              : 
   22245    103865927 :         nargs = call_expr_nargs (t);
   22246    103865927 :         releasing_vec call_args;
   22247    103865927 :         tsubst_call_args (t, args, complain, in_decl, call_args);
   22248              : 
   22249              :         /* Stripped-down processing for a call in a thunk.  Specifically, in
   22250              :            the thunk template for a generic lambda.  */
   22251    103865924 :         if (call_from_lambda_thunk_p (t))
   22252              :           {
   22253              :             /* Now that we've expanded any packs, the number of call args
   22254              :                might be different.  */
   22255          105 :             unsigned int cargs = call_args->length ();
   22256          105 :             tree thisarg = NULL_TREE;
   22257          105 :             if (TREE_CODE (function) == COMPONENT_REF)
   22258              :               {
   22259          105 :                 thisarg = TREE_OPERAND (function, 0);
   22260          105 :                 if (TREE_CODE (thisarg) == INDIRECT_REF)
   22261          105 :                   thisarg = TREE_OPERAND (thisarg, 0);
   22262          105 :                 function = TREE_OPERAND (function, 1);
   22263          105 :                 if (TREE_CODE (function) == BASELINK)
   22264          105 :                   function = BASELINK_FUNCTIONS (function);
   22265              :               }
   22266              :             /* We aren't going to do normal overload resolution, so force the
   22267              :                template-id to resolve.  */
   22268          105 :             function = resolve_nondeduced_context (function, complain);
   22269          327 :             for (unsigned i = 0; i < cargs; ++i)
   22270              :               {
   22271              :                 /* In a thunk, pass through args directly, without any
   22272              :                    conversions.  */
   22273          222 :                 tree arg = (*call_args)[i];
   22274          222 :                 while (TREE_CODE (arg) != PARM_DECL)
   22275            0 :                   arg = TREE_OPERAND (arg, 0);
   22276          222 :                 (*call_args)[i] = arg;
   22277              :               }
   22278          105 :             if (thisarg)
   22279              :               {
   22280              :                 /* If there are no other args, just push 'this'.  */
   22281          105 :                 if (cargs == 0)
   22282            9 :                   vec_safe_push (call_args, thisarg);
   22283              :                 else
   22284              :                   {
   22285              :                     /* Otherwise, shift the other args over to make room.  */
   22286           96 :                     tree last = (*call_args)[cargs - 1];
   22287           96 :                     vec_safe_push (call_args, last);
   22288          222 :                     for (int i = cargs - 1; i > 0; --i)
   22289          126 :                       (*call_args)[i] = (*call_args)[i - 1];
   22290           96 :                     (*call_args)[0] = thisarg;
   22291              :                   }
   22292              :               }
   22293          105 :             ret = build_call_a (function, call_args->length (),
   22294              :                                 call_args->address ());
   22295              :             /* The thunk location is not interesting.  */
   22296          105 :             SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
   22297          105 :             CALL_FROM_THUNK_P (ret) = true;
   22298          105 :             if (CLASS_TYPE_P (TREE_TYPE (ret)))
   22299            9 :               CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
   22300              : 
   22301          105 :             RETURN (ret);
   22302              :           }
   22303              : 
   22304              :         /* We do not perform argument-dependent lookup if normal
   22305              :            lookup finds a non-function, in accordance with the
   22306              :            resolution of DR 218.  */
   22307    103865819 :         if (koenig_p
   22308      7852834 :             && ((is_overloaded_fn (function)
   22309              :                  /* If lookup found a member function, the Koenig lookup is
   22310              :                     not appropriate, even if an unqualified-name was used
   22311              :                     to denote the function.  */
   22312      7671857 :                  && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
   22313      7857506 :                 || identifier_p (function)
   22314              :                 /* C++20 P0846: Lookup found nothing.  */
   22315         4691 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22316         4672 :                     && identifier_p (TREE_OPERAND (function, 0))))
   22317              :             /* Only do this when substitution turns a dependent call
   22318              :                into a non-dependent call.  */
   22319      7852815 :             && type_dependent_expression_p_push (t)
   22320    110809877 :             && !any_type_dependent_arguments_p (call_args))
   22321      6829681 :           function = perform_koenig_lookup (function, call_args, tf_none);
   22322              : 
   22323    103865819 :         if (function != NULL_TREE
   22324       158437 :             && (identifier_p (function)
   22325    103706781 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22326     31486758 :                     && identifier_p (TREE_OPERAND (function, 0))
   22327          368 :                     && !any_dependent_template_arguments_p (TREE_OPERAND
   22328              :                                                             (function, 1))))
   22329    104024256 :             && !any_type_dependent_arguments_p (call_args))
   22330              :           {
   22331        56352 :             bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
   22332        56352 :             if (template_id_p)
   22333            6 :               function = TREE_OPERAND (function, 0);
   22334        56352 :             if (koenig_p && (complain & tf_warning_or_error))
   22335              :               {
   22336              :                 /* For backwards compatibility and good diagnostics, try
   22337              :                    the unqualified lookup again if we aren't in SFINAE
   22338              :                    context.  */
   22339           57 :                 tree unq = tsubst_expr (function, args, complain, in_decl);
   22340           57 :                 if (unq == error_mark_node)
   22341           33 :                   RETURN (error_mark_node);
   22342              : 
   22343           24 :                 if (unq != function)
   22344              :                   {
   22345           24 :                     auto_diagnostic_group d;
   22346           24 :                     char const *const msg
   22347              :                       = G_("%qD was not declared in this scope, "
   22348              :                            "and no declarations were found by "
   22349              :                            "argument-dependent lookup at the point "
   22350              :                            "of instantiation");
   22351              : 
   22352           24 :                     bool in_lambda = (current_class_type
   22353           36 :                                       && LAMBDA_TYPE_P (current_class_type));
   22354              :                     /* In a lambda fn, we have to be careful to not
   22355              :                        introduce new this captures.  Legacy code can't
   22356              :                        be using lambdas anyway, so it's ok to be
   22357              :                        stricter.  Be strict with C++20 template-id ADL too.
   22358              :                        And be strict if we're already failing anyway.  */
   22359           24 :                     bool strict = in_lambda || template_id_p || seen_error ();
   22360           24 :                     bool diag = true;
   22361           24 :                     if (strict)
   22362           15 :                       error_at (cp_expr_loc_or_input_loc (t),
   22363              :                                 msg, function);
   22364              :                     else
   22365            9 :                       diag = permerror (cp_expr_loc_or_input_loc (t),
   22366              :                                         msg, function);
   22367           24 :                     if (diag)
   22368              :                       {
   22369           24 :                         tree fn = unq;
   22370              : 
   22371           24 :                         if (INDIRECT_REF_P (fn))
   22372            3 :                           fn = TREE_OPERAND (fn, 0);
   22373           24 :                         if (is_overloaded_fn (fn))
   22374           21 :                           fn = get_first_fn (fn);
   22375              : 
   22376           24 :                         if (!DECL_P (fn))
   22377              :                           /* Can't say anything more.  */;
   22378           21 :                         else if (DECL_CLASS_SCOPE_P (fn))
   22379              :                           {
   22380            9 :                             location_t loc = cp_expr_loc_or_input_loc (t);
   22381            9 :                             inform (loc,
   22382              :                                     "declarations in dependent base %qT are "
   22383              :                                     "not found by unqualified lookup",
   22384            9 :                                     DECL_CLASS_CONTEXT (fn));
   22385            9 :                             if (current_class_ptr)
   22386            6 :                               inform (loc,
   22387              :                                       "use %<this->%D%> instead", function);
   22388              :                             else
   22389            3 :                               inform (loc,
   22390              :                                       "use %<%T::%D%> instead",
   22391              :                                       current_class_name, function);
   22392              :                           }
   22393              :                         else
   22394           12 :                           inform (DECL_SOURCE_LOCATION (fn),
   22395              :                                   "%qD declared here, later in the "
   22396              :                                   "translation unit", fn);
   22397           24 :                         if (strict)
   22398           15 :                           RETURN (error_mark_node);
   22399              :                       }
   22400              : 
   22401            9 :                     function = unq;
   22402           24 :                   }
   22403              :               }
   22404        56304 :             if (identifier_p (function))
   22405              :               {
   22406        56295 :                 if (complain & tf_error)
   22407            0 :                   unqualified_name_lookup_error (function);
   22408        56295 :                 RETURN (error_mark_node);
   22409              :               }
   22410              :           }
   22411              : 
   22412              :         /* Remember that there was a reference to this entity.  */
   22413    103809476 :         if (function != NULL_TREE
   22414    103808867 :             && DECL_P (function)
   22415    113705639 :             && !mark_used (function, complain) && !(complain & tf_error))
   22416            3 :           RETURN (error_mark_node);
   22417              : 
   22418    103809473 :         if (!maybe_fold_fn_template_args (function, complain))
   22419       300858 :           return error_mark_node;
   22420              : 
   22421              :         /* Put back tf_decltype for the actual call.  */
   22422    103508615 :         complain |= decltype_flag;
   22423              : 
   22424    103508615 :         if (function == NULL_TREE)
   22425          609 :           switch (CALL_EXPR_IFN (t))
   22426              :             {
   22427           45 :             case IFN_LAUNDER:
   22428           45 :               gcc_assert (nargs == 1);
   22429           45 :               if (vec_safe_length (call_args) != 1)
   22430              :                 {
   22431            6 :                   error_at (cp_expr_loc_or_input_loc (t),
   22432              :                             "wrong number of arguments to "
   22433              :                             "%<__builtin_launder%>");
   22434            6 :                   ret = error_mark_node;
   22435              :                 }
   22436              :               else
   22437           39 :                 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
   22438           39 :                                               (*call_args)[0], complain);
   22439              :               break;
   22440              : 
   22441          125 :             case IFN_VEC_CONVERT:
   22442          125 :               gcc_assert (nargs == 1);
   22443          125 :               if (vec_safe_length (call_args) != 1)
   22444              :                 {
   22445            0 :                   error_at (cp_expr_loc_or_input_loc (t),
   22446              :                             "wrong number of arguments to "
   22447              :                             "%<__builtin_convertvector%>");
   22448            0 :                   ret = error_mark_node;
   22449            0 :                   break;
   22450              :                 }
   22451          125 :               ret = cp_build_vec_convert ((*call_args)[0], input_location,
   22452          125 :                                           tsubst (TREE_TYPE (t), args,
   22453              :                                                   complain, in_decl),
   22454              :                                           complain);
   22455          125 :               if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
   22456           15 :                 RETURN (ret);
   22457              :               break;
   22458              : 
   22459          344 :             case IFN_SHUFFLEVECTOR:
   22460          344 :               {
   22461          344 :                 ret = build_x_shufflevector (input_location, call_args,
   22462              :                                              complain);
   22463          344 :                 if (ret != error_mark_node)
   22464          341 :                   RETURN (ret);
   22465              :                 break;
   22466              :               }
   22467              : 
   22468           83 :             case IFN_ASSUME:
   22469           83 :               gcc_assert (nargs == 1);
   22470           83 :               if (vec_safe_length (call_args) != 1)
   22471              :                 {
   22472            0 :                   error_at (cp_expr_loc_or_input_loc (t),
   22473              :                             "wrong number of arguments to "
   22474              :                             "%<assume%> attribute");
   22475            0 :                   ret = error_mark_node;
   22476              :                 }
   22477              :               else
   22478              :                 {
   22479           83 :                   tree &arg = (*call_args)[0];
   22480           83 :                   if (!type_dependent_expression_p (arg))
   22481           83 :                     arg = contextual_conv_bool (arg, tf_warning_or_error);
   22482           83 :                   if (error_operand_p (arg))
   22483              :                     {
   22484            6 :                       ret = error_mark_node;
   22485            6 :                       break;
   22486              :                     }
   22487           77 :                   ret = build_assume_call (EXPR_LOCATION (t), arg);
   22488           77 :                   RETURN (ret);
   22489              :                 }
   22490            0 :               break;
   22491              : 
   22492           12 :             case IFN_GOMP_DISPATCH:
   22493           24 :               ret = build_call_expr_internal_loc (EXPR_LOCATION (t),
   22494              :                                                   IFN_GOMP_DISPATCH,
   22495           12 :                                                   TREE_TYPE (call_args[0]), 1,
   22496           12 :                                                   call_args[0]);
   22497           12 :               RETURN (ret);
   22498              :               break;
   22499              : 
   22500            0 :             default:
   22501              :               /* Unsupported internal function with arguments.  */
   22502            0 :               gcc_unreachable ();
   22503              :             }
   22504    103508006 :         else if (TREE_CODE (function) == OFFSET_REF
   22505              :                  || TREE_CODE (function) == DOTSTAR_EXPR
   22506              :                  || TREE_CODE (function) == MEMBER_REF)
   22507        82989 :           ret = build_offset_ref_call_from_tree (function, &call_args,
   22508              :                                                  complain);
   22509    103425017 :         else if (concept_check_p (function))
   22510              :           /* Calls to concepts should have been previously diagnosed.  */
   22511            0 :           gcc_unreachable ();
   22512              :         else
   22513    103425017 :           ret = finish_call_expr (function, &call_args,
   22514              :                                   /*disallow_virtual=*/qualified_p,
   22515              :                                   koenig_p,
   22516              :                                   complain);
   22517              : 
   22518    103494667 :         if (ret != error_mark_node)
   22519              :           {
   22520    103210761 :             bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
   22521    103210761 :             bool ord = CALL_EXPR_ORDERED_ARGS (t);
   22522    103210761 :             bool rev = CALL_EXPR_REVERSE_ARGS (t);
   22523    103210761 :             bool mtc = false;
   22524    103210761 :             if (TREE_CODE (t) == CALL_EXPR)
   22525    103210761 :               mtc = CALL_EXPR_MUST_TAIL_CALL (t);
   22526    103210761 :             if (op || ord || rev || mtc)
   22527       394356 :               if (tree call = extract_call_expr (ret))
   22528              :                 {
   22529       394356 :                   CALL_EXPR_OPERATOR_SYNTAX (call) = op;
   22530       394356 :                   CALL_EXPR_ORDERED_ARGS (call) = ord;
   22531       394356 :                   CALL_EXPR_REVERSE_ARGS (call) = rev;
   22532       394356 :                   if (TREE_CODE (call) == CALL_EXPR)
   22533       394056 :                     CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
   22534          300 :                   else if (TREE_CODE (call) == AGGR_INIT_EXPR)
   22535          300 :                     AGGR_INIT_EXPR_MUST_TAIL (call) = mtc;
   22536              :                 }
   22537    103210761 :             if (CALL_FROM_NEW_OR_DELETE_P (t))
   22538              :               {
   22539       195216 :                 tree call = extract_call_expr (ret);
   22540       195216 :                 tree fn = cp_get_callee_fndecl_nofold (call);
   22541       390432 :                 if (fn ? !DECL_IS_REPLACEABLE_OPERATOR (fn)
   22542            0 :                        : !processing_template_decl)
   22543              :                   {
   22544            9 :                     auto_diagnostic_group d;
   22545            9 :                     location_t loc = cp_expr_loc_or_input_loc (t);
   22546            9 :                     if (!fn || IDENTIFIER_NEW_OP_P (DECL_NAME (fn)))
   22547            6 :                       error_at (loc, "call to %<__builtin_operator_new%> "
   22548              :                                      "does not select replaceable global "
   22549              :                                      "allocation function");
   22550              :                     else
   22551            3 :                       error_at (loc, "call to %<__builtin_operator_delete%> "
   22552              :                                      "does not select replaceable global "
   22553              :                                      "deallocation function");
   22554            9 :                     if (fn)
   22555            9 :                       inform (DECL_SOURCE_LOCATION (fn),
   22556              :                               "selected function declared here");
   22557            9 :                   }
   22558       195207 :                 else if (call && TREE_CODE (call) == CALL_EXPR)
   22559       195207 :                   CALL_FROM_NEW_OR_DELETE_P (call) = 1;
   22560              :               }
   22561    103210761 :             if (warning_suppressed_p (t, OPT_Wpessimizing_move))
   22562              :               /* This also suppresses -Wredundant-move.  */
   22563     93811709 :               suppress_warning (ret, OPT_Wpessimizing_move);
   22564              :           }
   22565              : 
   22566    103551563 :         RETURN (ret);
   22567    103852421 :       }
   22568              : 
   22569      4716582 :     case COND_EXPR:
   22570      4716582 :       {
   22571      4716582 :         tree cond = RECUR (TREE_OPERAND (t, 0));
   22572      4716582 :         cond = mark_rvalue_use (cond);
   22573      4716582 :         tree folded_cond = fold_non_dependent_expr (cond, complain);
   22574      4716582 :         tree exp1, exp2;
   22575              : 
   22576      4716582 :         if (TREE_CODE (folded_cond) == INTEGER_CST)
   22577              :           {
   22578      3681780 :             if (integer_zerop (folded_cond))
   22579              :               {
   22580      3050758 :                 ++c_inhibit_evaluation_warnings;
   22581      3050758 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   22582      3050758 :                 --c_inhibit_evaluation_warnings;
   22583      3050758 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   22584              :               }
   22585              :             else
   22586              :               {
   22587       631022 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   22588       631022 :                 ++c_inhibit_evaluation_warnings;
   22589       631022 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   22590       631022 :                 --c_inhibit_evaluation_warnings;
   22591              :               }
   22592              :             cond = folded_cond;
   22593              :           }
   22594              :         else
   22595              :           {
   22596      1034802 :             exp1 = RECUR (TREE_OPERAND (t, 1));
   22597      1034802 :             exp2 = RECUR (TREE_OPERAND (t, 2));
   22598              :           }
   22599              : 
   22600      4716582 :         warning_sentinel s(warn_duplicated_branches);
   22601      4716582 :         RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
   22602              :                                          cond, exp1, exp2, complain));
   22603      4716582 :       }
   22604              : 
   22605           18 :     case PSEUDO_DTOR_EXPR:
   22606           18 :       {
   22607           18 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22608           18 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   22609           18 :         tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
   22610           18 :         RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
   22611              :                                                input_location, complain));
   22612              :       }
   22613              : 
   22614     32174732 :     case TREE_LIST:
   22615     32174732 :       RETURN (tsubst_tree_list (t, args, complain, in_decl));
   22616              : 
   22617     27298487 :     case COMPONENT_REF:
   22618     27298487 :       {
   22619     27298487 :         tree object;
   22620     27298487 :         tree object_type;
   22621     27298487 :         tree member;
   22622     27298487 :         tree r;
   22623              : 
   22624     27298487 :         object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   22625              :                                                      args, complain, in_decl);
   22626              :         /* Remember that there was a reference to this entity.  */
   22627     27298487 :         if (DECL_P (object)
   22628     27298487 :             && !mark_used (object, complain) && !(complain & tf_error))
   22629            0 :           RETURN (error_mark_node);
   22630     27298487 :         object_type = TREE_TYPE (object);
   22631              : 
   22632     27298487 :         member = TREE_OPERAND (t, 1);
   22633     27298487 :         const bool splice_p = dependent_splice_p (member);
   22634     27298487 :         if (BASELINK_P (member))
   22635     10642765 :           member = tsubst_baselink (member,
   22636     10642765 :                                     non_reference (TREE_TYPE (object)),
   22637              :                                     args, complain, in_decl);
   22638              :         /* In reconstruct_lambda_capture_pack, handle replacing the FIELD_DECL
   22639              :            pack with an element.  */
   22640     31903105 :         else if (object_type && LAMBDA_TYPE_P (object_type)
   22641       115412 :                  && TREE_CODE (member) == FIELD_DECL
   22642     16768007 :                  && (r = retrieve_local_specialization (member)))
   22643              :           {
   22644           16 :             if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   22645           16 :               r = argument_pack_select_arg (r);
   22646              :             member = r;
   22647              :           }
   22648              :         else
   22649     16655706 :           member = tsubst_name (member, args, complain, in_decl);
   22650     27298487 :         if (member == error_mark_node)
   22651           29 :           RETURN (error_mark_node);
   22652              : 
   22653     26809389 :         if (object_type && TYPE_PTRMEMFUNC_P (object_type)
   22654     27298482 :             && TREE_CODE (member) == FIELD_DECL)
   22655              :           {
   22656            3 :             r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
   22657            3 :             RETURN (r);
   22658              :           }
   22659     27298455 :         else if (TREE_CODE (member) == FIELD_DECL)
   22660              :           {
   22661              :             /* Assume access of this FIELD_DECL has already been checked; we
   22662              :                don't recheck it to avoid bogus access errors when substituting
   22663              :                a reduced constant initializer (97740).  */
   22664      6408831 :             gcc_checking_assert (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
   22665              :                                  || dependent_splice_p (TREE_OPERAND (t, 1)));
   22666      6408831 :             push_deferring_access_checks (dk_deferred);
   22667      6408831 :             r = finish_non_static_data_member (member, object, NULL_TREE,
   22668              :                                                complain);
   22669      6408831 :             pop_deferring_access_checks ();
   22670      6408831 :             if (REF_PARENTHESIZED_P (t))
   22671         2785 :               r = force_paren_expr (r);
   22672      6408831 :             RETURN (r);
   22673              :           }
   22674     20889624 :         else if (type_dependent_expression_p (object))
   22675              :           /* We can't do much here.  */;
   22676     20286467 :         else if (!CLASS_TYPE_P (object_type))
   22677              :           {
   22678       410122 :             if (scalarish_type_p (object_type))
   22679              :               {
   22680       246710 :                 tree s = NULL_TREE;
   22681       246710 :                 tree dtor = member;
   22682              : 
   22683       246710 :                 if (TREE_CODE (dtor) == SCOPE_REF)
   22684              :                   {
   22685           15 :                     s = TREE_OPERAND (dtor, 0);
   22686           15 :                     dtor = TREE_OPERAND (dtor, 1);
   22687              :                   }
   22688       246710 :                 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
   22689              :                   {
   22690       150253 :                     dtor = TREE_OPERAND (dtor, 0);
   22691       150253 :                     if (TYPE_P (dtor))
   22692       150253 :                       RETURN (finish_pseudo_destructor_expr
   22693              :                               (object, s, dtor, input_location, complain));
   22694              :                   }
   22695              :               }
   22696              :           }
   22697     19876345 :         else if (TREE_CODE (member) == SCOPE_REF
   22698          753 :                  && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR
   22699     19876363 :                  && identifier_p (TREE_OPERAND (TREE_OPERAND (member, 1), 0)))
   22700              :           {
   22701              :             /* Lookup the template functions now that we know what the
   22702              :                scope is.  */
   22703           15 :             tree scope = TREE_OPERAND (member, 0);
   22704           15 :             tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
   22705           15 :             tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
   22706           15 :             member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
   22707              :                                             /*complain=*/false);
   22708           15 :             if (BASELINK_P (member))
   22709              :               {
   22710           30 :                 BASELINK_FUNCTIONS (member)
   22711           15 :                   = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
   22712              :                               args);
   22713           15 :                 member = (adjust_result_of_qualified_name_lookup
   22714           15 :                           (member, BINFO_TYPE (BASELINK_BINFO (member)),
   22715              :                            object_type));
   22716              :               }
   22717              :             else
   22718              :               {
   22719            0 :                 qualified_name_lookup_error (scope, tmpl, member,
   22720              :                                              input_location);
   22721           15 :                 RETURN (error_mark_node);
   22722              :               }
   22723              :           }
   22724     19876330 :         else if (TREE_CODE (member) == SCOPE_REF
   22725          738 :                  && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
   22726     19876333 :                  && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
   22727              :           {
   22728            3 :             if (complain & tf_error)
   22729              :               {
   22730            3 :                 if (TYPE_P (TREE_OPERAND (member, 0)))
   22731            3 :                   error ("%qT is not a class or namespace",
   22732            3 :                          TREE_OPERAND (member, 0));
   22733              :                 else
   22734            0 :                   error ("%qD is not a class or namespace",
   22735            0 :                          TREE_OPERAND (member, 0));
   22736              :               }
   22737            3 :             RETURN (error_mark_node);
   22738              :           }
   22739     19876327 :         else if (splice_p && BASELINK_P (member))
   22740              :           /* We need to call adjust_result_of_qualified_name_lookup when
   22741              :              we have obj->[:^^T::fn:], but we don't set BASELINK_QUALIFIED_P
   22742              :              so that we still get virtual function binding.  */
   22743            6 :           member = (adjust_result_of_qualified_name_lookup
   22744            6 :                     (member, NULL_TREE, object_type));
   22745              : 
   22746     20739368 :         r = finish_class_member_access_expr (object, member,
   22747              :                                              /*template_p=*/false,
   22748              :                                              complain,
   22749     20739368 :                                              COMPONENT_REF_SPLICE_P (t));
   22750     20739368 :         if (REF_PARENTHESIZED_P (t))
   22751         1065 :           r = force_paren_expr (r);
   22752     20739368 :         RETURN (r);
   22753              :       }
   22754              : 
   22755        23072 :     case THROW_EXPR:
   22756        23072 :       RETURN (build_throw
   22757              :        (input_location, RECUR (TREE_OPERAND (t, 0)), complain));
   22758              : 
   22759      5257061 :     case CONSTRUCTOR:
   22760      5257061 :       {
   22761      5257061 :         vec<constructor_elt, va_gc> *n;
   22762      5257061 :         constructor_elt *ce;
   22763      5257061 :         unsigned HOST_WIDE_INT idx;
   22764      5257061 :         bool process_index_p;
   22765      5257061 :         int newlen;
   22766      5257061 :         bool need_copy_p = false;
   22767      5257061 :         tree r;
   22768              : 
   22769      5257061 :         tsubst_flags_t tcomplain = complain;
   22770      5257061 :         if (COMPOUND_LITERAL_P (t))
   22771      3014134 :           tcomplain |= tf_tst_ok;
   22772      5257061 :         tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   22773      5257061 :         if (type == error_mark_node)
   22774           22 :           RETURN (error_mark_node);
   22775              : 
   22776              :         /* We do not want to process the index of aggregate
   22777              :            initializers as they are identifier nodes which will be
   22778              :            looked up by digest_init.  */
   22779      5257039 :         process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
   22780              : 
   22781      5257039 :         if (null_member_pointer_value_p (t))
   22782              :           {
   22783           20 :             gcc_assert (same_type_p (type, TREE_TYPE (t)));
   22784           20 :             RETURN (t);
   22785              :           }
   22786              : 
   22787      5257019 :         n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
   22788      5257019 :         newlen = vec_safe_length (n);
   22789      8190955 :         FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
   22790              :           {
   22791      2933936 :             if (ce->index && process_index_p
   22792              :                 /* An identifier index is looked up in the type
   22793              :                    being initialized, not the current scope.  */
   22794          492 :                 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
   22795          459 :               ce->index = RECUR (ce->index);
   22796              : 
   22797      2933936 :             if (PACK_EXPANSION_P (ce->value))
   22798              :               {
   22799              :                 /* Substitute into the pack expansion.  */
   22800        12796 :                 ce->value = tsubst_pack_expansion (ce->value, args, complain,
   22801              :                                                   in_decl);
   22802              : 
   22803        12796 :                 if (ce->value == error_mark_node
   22804        12793 :                     || PACK_EXPANSION_P (ce->value))
   22805              :                   ;
   22806        12044 :                 else if (TREE_VEC_LENGTH (ce->value) == 1)
   22807              :                   /* Just move the argument into place.  */
   22808         3409 :                   ce->value = TREE_VEC_ELT (ce->value, 0);
   22809              :                 else
   22810              :                   {
   22811              :                     /* Update the length of the final CONSTRUCTOR
   22812              :                        arguments vector, and note that we will need to
   22813              :                        copy.*/
   22814         8635 :                     newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
   22815         8635 :                     need_copy_p = true;
   22816              :                   }
   22817              :               }
   22818              :             else
   22819      2921140 :               ce->value = RECUR (ce->value);
   22820              :           }
   22821              : 
   22822      5257019 :         if (need_copy_p)
   22823              :           {
   22824         8613 :             vec<constructor_elt, va_gc> *old_n = n;
   22825              : 
   22826         8613 :             vec_alloc (n, newlen);
   22827        17531 :             FOR_EACH_VEC_ELT (*old_n, idx, ce)
   22828              :               {
   22829         8918 :                 if (TREE_CODE (ce->value) == TREE_VEC)
   22830              :                   {
   22831         8635 :                     int i, len = TREE_VEC_LENGTH (ce->value);
   22832        41005 :                     for (i = 0; i < len; ++i)
   22833        32370 :                       CONSTRUCTOR_APPEND_ELT (n, 0,
   22834              :                                               TREE_VEC_ELT (ce->value, i));
   22835              :                   }
   22836              :                 else
   22837         9201 :                   CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
   22838              :               }
   22839              :           }
   22840              : 
   22841      5257019 :         r = build_constructor (init_list_type_node, n);
   22842      5257019 :         CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
   22843     10514038 :         CONSTRUCTOR_IS_DESIGNATED_INIT (r)
   22844      5257019 :           = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
   22845              : 
   22846      5257019 :         if (TREE_HAS_CONSTRUCTOR (t))
   22847              :           {
   22848      3014112 :             fcl_t cl = fcl_functional;
   22849      3014112 :             if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
   22850          372 :               cl = fcl_c99;
   22851      3014112 :             RETURN (finish_compound_literal (type, r, complain, cl));
   22852              :           }
   22853              : 
   22854      2242907 :         TREE_TYPE (r) = type;
   22855      5257061 :         RETURN (r);
   22856              :       }
   22857              : 
   22858        72298 :     case TYPEID_EXPR:
   22859        72298 :       {
   22860        72298 :         tree operand_0 = TREE_OPERAND (t, 0);
   22861        72298 :         if (TYPE_P (operand_0))
   22862              :           {
   22863        71883 :             operand_0 = tsubst (operand_0, args, complain, in_decl);
   22864        71883 :             RETURN (get_typeid (operand_0, complain));
   22865              :           }
   22866              :         else
   22867              :           {
   22868          415 :             operand_0 = RECUR (operand_0);
   22869          415 :             RETURN (build_typeid (operand_0, complain));
   22870              :           }
   22871              :       }
   22872              : 
   22873    170015033 :     case FUNCTION_DECL:
   22874    170015033 :     case PARM_DECL:
   22875    170015033 :     case VAR_DECL:
   22876    170015033 :       if (!args)
   22877      2384384 :         RETURN (t);
   22878    167630649 :       tree r;
   22879     98944189 :       if (VAR_OR_FUNCTION_DECL_P (t)
   22880    175489817 :           && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   22881      8379817 :         r = tsubst_decl (t, args, complain);
   22882    159250832 :       else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
   22883              :         {
   22884              :           /* Local specialization will usually have been created when
   22885              :              we instantiated the DECL_EXPR_DECL. */
   22886          358 :           r = retrieve_local_specialization (t);
   22887          358 :           if (!r)
   22888              :             {
   22889              :               /* We're in a generic lambda referencing a local extern
   22890              :                  from an outer block-scope of a non-template.  */
   22891           18 :               gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
   22892              :               r = t;
   22893              :             }
   22894              :         }
   22895    159250474 :       else if (local_variable_p (t)
   22896    159250474 :                && ((r = retrieve_local_specialization (t))
   22897      2055371 :                    || TREE_CODE (t) == PARM_DECL
   22898         5400 :                    || uses_template_parms (DECL_CONTEXT (t))))
   22899              :         {
   22900    149411146 :           if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
   22901              :             {
   22902              :               /* We get here for a use of 'this' in an NSDMI.  */
   22903      2393134 :               if (DECL_NAME (t) == this_identifier && current_class_ptr
   22904      2735982 :                   && !LAMBDA_TYPE_P (TREE_TYPE (TREE_TYPE (current_class_ptr))))
   22905       343007 :                 RETURN (current_class_ptr);
   22906              : 
   22907              :               /* Parameters of non-templates map to themselves (e.g. in
   22908              :                  expansion statement body).  */
   22909      1706964 :               if (DECL_CONTEXT (t) && !uses_template_parms (DECL_CONTEXT (t)))
   22910           41 :                 RETURN (t);
   22911              : 
   22912              :               /* This can happen for a parameter name used later in a function
   22913              :                  declaration (such as in a late-specified return type).  Just
   22914              :                  make a dummy decl, since it's only used for its type.  */
   22915      1706923 :               gcc_assert (cp_unevaluated_operand);
   22916      1706923 :               r = tsubst_decl (t, args, complain);
   22917              :               /* Give it the template pattern as its context; its true context
   22918              :                  hasn't been instantiated yet and this is good enough for
   22919              :                  mangling.  */
   22920      1706923 :               DECL_CONTEXT (r) = DECL_CONTEXT (t);
   22921              :             }
   22922          675 :           else if (r == NULL_TREE)
   22923              :             {
   22924              :               /* First try name lookup to find the instantiation.  */
   22925          675 :               if (DECL_NAME (t))
   22926          669 :                 r = lookup_name (DECL_NAME (t));
   22927          675 :               if (r)
   22928              :                 {
   22929          657 :                   if (!VAR_P (r))
   22930              :                     {
   22931              :                       /* During error-recovery we may find a non-variable,
   22932              :                          even an OVERLOAD: just bail out and avoid ICEs and
   22933              :                          duplicate diagnostics (c++/62207).  */
   22934            3 :                       gcc_assert (seen_error ());
   22935            3 :                       RETURN (error_mark_node);
   22936              :                     }
   22937          654 :                   if (!is_capture_proxy (r))
   22938              :                     {
   22939              :                       /* Make sure the one we found is the one we want.  */
   22940          628 :                       tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
   22941          628 :                       if (ctx != DECL_CONTEXT (r))
   22942              :                         r = NULL_TREE;
   22943              :                     }
   22944              :                 }
   22945              : 
   22946              :               if (r)
   22947              :                 /* OK */;
   22948              :               else
   22949              :                 {
   22950              :                   /* This can happen for a variable used in a
   22951              :                      late-specified return type of a local lambda, or for a
   22952              :                      local static or constant.  Building a new VAR_DECL
   22953              :                      should be OK in all those cases.  */
   22954           18 :                   r = tsubst_decl (t, args, complain);
   22955           18 :                   if (local_specializations)
   22956              :                     /* Avoid infinite recursion (79640).  */
   22957           10 :                     register_local_specialization (r, t);
   22958           18 :                   if (decl_maybe_constant_var_p (r))
   22959              :                     {
   22960              :                       /* We can't call cp_finish_decl, so handle the
   22961              :                          initializer by hand.  */
   22962            0 :                       tree init = tsubst_init (DECL_INITIAL (t), r, args,
   22963              :                                                complain, in_decl);
   22964            0 :                       if (!processing_template_decl)
   22965            0 :                         init = maybe_constant_init (init);
   22966            0 :                       if (processing_template_decl
   22967            0 :                           ? potential_constant_expression (init)
   22968            0 :                           : reduced_constant_expression_p (init))
   22969            0 :                         DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
   22970            0 :                           = TREE_CONSTANT (r) = true;
   22971            0 :                       DECL_INITIAL (r) = init;
   22972            0 :                       if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
   22973            0 :                         TREE_TYPE (r)
   22974            0 :                           = do_auto_deduction (TREE_TYPE (r), init, auto_node,
   22975              :                                                complain, adc_variable_type);
   22976              :                     }
   22977           18 :                   gcc_assert (cp_unevaluated_operand
   22978              :                               || TREE_STATIC (r)
   22979              :                               || decl_constant_var_p (r)
   22980              :                               || seen_error ());
   22981           18 :                   if (!processing_template_decl
   22982           18 :                       && !TREE_STATIC (r))
   22983           18 :                     r = process_outer_var_ref (r, complain);
   22984              :                 }
   22985              :               /* Remember this for subsequent uses.  */
   22986          672 :               if (local_specializations)
   22987          664 :                 register_local_specialization (r, t);
   22988              :             }
   22989    149068095 :           if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   22990       571781 :             r = argument_pack_select_arg (r);
   22991              :         }
   22992              :       else
   22993              :         r = t;
   22994    167287598 :       if (!mark_used (r, complain))
   22995           24 :         RETURN (error_mark_node);
   22996              : 
   22997    167287571 :       if (!no_name_lookup_flag
   22998    162527663 :           && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL))
   22999              :         {
   23000              :           /* ??? We're doing a subset of finish_id_expression here.  */
   23001    154668558 :           if (tree wrap = maybe_get_tls_wrapper_call (r))
   23002              :             /* Replace an evaluated use of the thread_local variable with
   23003              :                a call to its wrapper.  */
   23004              :             r = wrap;
   23005    154668432 :           else if (outer_automatic_var_p (r))
   23006      1597363 :             r = process_outer_var_ref (r, complain);
   23007              : 
   23008    154668558 :           if (!TYPE_REF_P (TREE_TYPE (t)))
   23009              :             /* If the original type was a reference, we'll be wrapped in
   23010              :                the appropriate INDIRECT_REF.  */
   23011    125559724 :             r = convert_from_reference (r);
   23012              :         }
   23013    167287571 :       RETURN (r);
   23014              : 
   23015      2887861 :     case CONST_DECL:
   23016      2887861 :       {
   23017      2887861 :         tree enum_type;
   23018      2887861 :         tree v;
   23019              : 
   23020      2887861 :         if (DECL_TEMPLATE_PARM_P (t))
   23021           15 :           RETURN (RECUR (DECL_INITIAL (t)));
   23022      2887846 :         if (!args || !uses_template_parms (DECL_CONTEXT (t)))
   23023      2474631 :           RETURN (t);
   23024              : 
   23025              :         /* Unfortunately, we cannot just call lookup_name here.
   23026              :            Consider:
   23027              : 
   23028              :              template <int I> int f() {
   23029              :              enum E { a = I };
   23030              :              struct S { void g() { E e = a; } };
   23031              :              };
   23032              : 
   23033              :            When we instantiate f<7>::S::g(), say, lookup_name is not
   23034              :            clever enough to find f<7>::a.  */
   23035       413215 :         enum_type = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
   23036              : 
   23037       413215 :         for (v = TYPE_VALUES (enum_type);
   23038       476649 :              v != NULL_TREE;
   23039        63434 :              v = TREE_CHAIN (v))
   23040       476649 :           if (TREE_PURPOSE (v) == DECL_NAME (t))
   23041       476649 :             RETURN (TREE_VALUE (v));
   23042              : 
   23043              :           /* We didn't find the name.  That should never happen; if
   23044              :              name-lookup found it during preliminary parsing, we
   23045              :              should find it again here during instantiation.  */
   23046            0 :         gcc_unreachable ();
   23047      9324192 :         RETURN (t);
   23048              :       }
   23049              : 
   23050      9324192 :     case FIELD_DECL:
   23051      9324192 :       if (DECL_CONTEXT (t))
   23052              :         {
   23053      9324192 :           tree ctx;
   23054              : 
   23055      9324192 :           ctx = tsubst_entering_scope (DECL_CONTEXT (t), args,
   23056              :                                        complain, in_decl);
   23057      9324192 :           if (ctx != DECL_CONTEXT (t))
   23058              :             {
   23059      8685692 :               tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
   23060      8685692 :               if (!r)
   23061              :                 {
   23062            0 :                   if (complain & tf_error)
   23063            0 :                     error ("using invalid field %qD", t);
   23064            0 :                   RETURN (error_mark_node);
   23065              :                 }
   23066      8685692 :               RETURN (r);
   23067              :             }
   23068              :         }
   23069       638500 :       RETURN (t);
   23070              : 
   23071     56998926 :     case OVERLOAD:
   23072     56998926 :       if (modules_p ())
   23073      1068024 :         for (tree ovl : lkp_range (t))
   23074       491784 :           if (instantiating_tu_local_entity (ovl))
   23075       491784 :             RETURN (error_mark_node);
   23076     56998914 :       RETURN (t);
   23077              : 
   23078     24587113 :     case TEMPLATE_DECL:
   23079     24587113 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   23080         3400 :         RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
   23081              :                         args, complain, in_decl));
   23082     24583713 :       else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
   23083            0 :         RETURN (tsubst (t, args, complain, in_decl));
   23084     49167426 :       else if (DECL_CLASS_SCOPE_P (t)
   23085     24834572 :                && uses_template_parms (DECL_CONTEXT (t)))
   23086              :         {
   23087              :           /* Template template argument like the following example need
   23088              :              special treatment:
   23089              : 
   23090              :                template <template <class> class TT> struct C {};
   23091              :                template <class T> struct D {
   23092              :                  template <class U> struct E {};
   23093              :                  C<E> c;                          // #1
   23094              :                };
   23095              :                D<int> d;                          // #2
   23096              : 
   23097              :              We are processing the template argument `E' in #1 for
   23098              :              the template instantiation #2.  Originally, `E' is a
   23099              :              TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
   23100              :              have to substitute this with one having context `D<int>'.  */
   23101              : 
   23102       176402 :           tree context = tsubst_entering_scope (DECL_CONTEXT (t), args,
   23103              :                                                 complain, in_decl);
   23104       176402 :           RETURN (lookup_field (context, DECL_NAME(t), 0, false));
   23105              :         }
   23106              :       else
   23107              :         /* Ordinary template template argument.  */
   23108     24407311 :         RETURN (t);
   23109              : 
   23110    194373987 :     case TEMPLATE_PARM_INDEX:
   23111    194373987 :     case TYPE_DECL:
   23112    194373987 :       RETURN (tsubst (t, args, complain, in_decl));
   23113              : 
   23114            0 :     case CLEANUP_POINT_EXPR:
   23115              :       /* We shouldn't have built any of these during initial template
   23116              :          generation.  Instead, they should be built during instantiation
   23117              :          in response to the saved STMT_IS_FULL_EXPR_P setting.  */
   23118            0 :       gcc_unreachable ();
   23119              : 
   23120            0 :     case TARGET_EXPR:
   23121              :       /* TARGET_EXPR represents temporary objects and should not appear in
   23122              :          templated trees.  */
   23123            0 :       gcc_unreachable ();
   23124              : 
   23125          138 :     case OFFSET_REF:
   23126          138 :       {
   23127              :         /* We should only get here for an OFFSET_REF like A::m; a .* in a
   23128              :            template is represented as a DOTSTAR_EXPR.  */
   23129          138 :         gcc_checking_assert
   23130              :           (same_type_p (TREE_TYPE (t), TREE_TYPE (TREE_OPERAND (t, 1))));
   23131          138 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23132          138 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   23133          138 :         tree type = TREE_TYPE (op1);
   23134          138 :         r = build2 (OFFSET_REF, type, op0, op1);
   23135          138 :         PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
   23136          138 :         if (!mark_used (op1, complain)
   23137          138 :             && !(complain & tf_error))
   23138            0 :           RETURN (error_mark_node);
   23139          138 :         RETURN (r);
   23140              :       }
   23141              : 
   23142        11117 :     case PACK_INDEX_EXPR:
   23143        11117 :       RETURN (tsubst_pack_index (t, args, complain, in_decl));
   23144              : 
   23145            0 :     case EXPR_PACK_EXPANSION:
   23146            0 :       error ("invalid use of pack expansion expression");
   23147            0 :       RETURN (error_mark_node);
   23148              : 
   23149            0 :     case NONTYPE_ARGUMENT_PACK:
   23150            0 :       error ("use %<...%> to expand argument pack");
   23151            0 :       RETURN (error_mark_node);
   23152              : 
   23153        16110 :     case VOID_CST:
   23154        16110 :       gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
   23155        16110 :       RETURN (t);
   23156              : 
   23157    139645401 :     case INTEGER_CST:
   23158    139645401 :     case REAL_CST:
   23159    139645401 :     case COMPLEX_CST:
   23160    139645401 :     case VECTOR_CST:
   23161    139645401 :       {
   23162              :         /* Instantiate any typedefs in the type.  */
   23163    139645401 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23164    139645401 :         r = fold_convert (type, t);
   23165    139645401 :         gcc_assert (TREE_CODE (r) == TREE_CODE (t));
   23166    139645401 :         RETURN (r);
   23167              :       }
   23168              : 
   23169      7790156 :     case STRING_CST:
   23170      7790156 :       {
   23171      7790156 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23172      7790156 :         r = t;
   23173      7790156 :         if (type != TREE_TYPE (t))
   23174              :           {
   23175            3 :             r = copy_node (t);
   23176            3 :             TREE_TYPE (r) = type;
   23177              :           }
   23178      7790156 :         RETURN (r);
   23179              :       }
   23180              : 
   23181           54 :     case RAW_DATA_CST:
   23182           54 :       {
   23183           54 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23184           54 :         r = copy_node (t);
   23185           54 :         TREE_TYPE (r) = type;
   23186           54 :         RETURN (r);
   23187              :       }
   23188              : 
   23189           26 :     case PTRMEM_CST:
   23190              :       /* These can sometimes show up in a partial instantiation, but never
   23191              :          involve template parms.  */
   23192           26 :       gcc_assert (!uses_template_parms (t));
   23193           26 :       RETURN (t);
   23194              : 
   23195         4888 :     case UNARY_LEFT_FOLD_EXPR:
   23196         4888 :       RETURN (tsubst_unary_left_fold (t, args, complain, in_decl));
   23197       457211 :     case UNARY_RIGHT_FOLD_EXPR:
   23198       457211 :       RETURN (tsubst_unary_right_fold (t, args, complain, in_decl));
   23199         6563 :     case BINARY_LEFT_FOLD_EXPR:
   23200         6563 :       RETURN (tsubst_binary_left_fold (t, args, complain, in_decl));
   23201          933 :     case BINARY_RIGHT_FOLD_EXPR:
   23202          933 :       RETURN (tsubst_binary_right_fold (t, args, complain, in_decl));
   23203            0 :     case PREDICT_EXPR:
   23204            0 :       RETURN (t);
   23205              : 
   23206     84404558 :     case DEBUG_BEGIN_STMT:
   23207              :       /* ??? There's no point in copying it for now, but maybe some
   23208              :          day it will contain more information, such as a pointer back
   23209              :          to the containing function, inlined copy or so.  */
   23210     84404558 :       RETURN (t);
   23211              : 
   23212          113 :     case CO_YIELD_EXPR:
   23213          113 :       RETURN (finish_co_yield_expr (input_location,
   23214              :                                     RECUR (TREE_OPERAND (t, 0))));
   23215              : 
   23216          121 :     case CO_AWAIT_EXPR:
   23217          121 :       RETURN (finish_co_await_expr (input_location,
   23218              :                                     RECUR (TREE_OPERAND (t, 0))));
   23219              : 
   23220           42 :     case VA_ARG_EXPR:
   23221           42 :       {
   23222           42 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23223           42 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23224           42 :         RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
   23225              :       }
   23226              : 
   23227           48 :     case OFFSETOF_EXPR:
   23228           48 :       {
   23229           48 :         tree object_ptr
   23230           48 :           = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   23231           48 :         RETURN (finish_offsetof (object_ptr,
   23232              :                                  RECUR (TREE_OPERAND (t, 0)),
   23233              :                                  EXPR_LOCATION (t)));
   23234              :       }
   23235              : 
   23236       425320 :     case ADDRESSOF_EXPR:
   23237       425320 :       RETURN (cp_build_addressof (EXPR_LOCATION (t),
   23238              :                                   RECUR (TREE_OPERAND (t, 0)), complain));
   23239              : 
   23240     17727754 :     case TRAIT_EXPR:
   23241     17727754 :       {
   23242     17727754 :         tree type1 = TRAIT_EXPR_TYPE1 (t);
   23243     17727754 :         if (TYPE_P (type1))
   23244     17727645 :           type1 = tsubst (type1, args, complain, in_decl);
   23245              :         else
   23246          109 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   23247     17727754 :         tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
   23248              :                              complain, in_decl);
   23249     17727754 :         if (TRAIT_EXPR_KIND (t) == CPTK_STRUCTURED_BINDING_SIZE
   23250          129 :             && type1 != error_mark_node
   23251     17727883 :             && !processing_template_decl)
   23252              :           /* __builtin_structured_binding_size handled separately
   23253              :              to make it SFINAE friendly.  */
   23254          129 :           RETURN (finish_structured_binding_size (TRAIT_EXPR_LOCATION (t),
   23255              :                                                   type1, complain));
   23256     17727625 :         RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
   23257              :                                    TRAIT_EXPR_KIND (t), type1, type2));
   23258              :       }
   23259              : 
   23260          172 :     case STMT_EXPR:
   23261          172 :       {
   23262          172 :         tree old_stmt_expr = cur_stmt_expr;
   23263          172 :         tree stmt_expr = begin_stmt_expr ();
   23264              : 
   23265          172 :         cur_stmt_expr = stmt_expr;
   23266          172 :         tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl);
   23267          172 :         stmt_expr = finish_stmt_expr (stmt_expr, false);
   23268          172 :         cur_stmt_expr = old_stmt_expr;
   23269              : 
   23270              :         /* If the resulting list of expression statement is empty,
   23271              :            fold it further into void_node.  */
   23272          172 :         if (empty_expr_stmt_p (stmt_expr))
   23273            9 :           stmt_expr = void_node;
   23274              : 
   23275          172 :         RETURN (stmt_expr);
   23276              :       }
   23277              : 
   23278       193638 :     case LAMBDA_EXPR:
   23279       193638 :       {
   23280       193638 :         tree r = tsubst_lambda_expr (t, args, complain, in_decl);
   23281              : 
   23282       193638 :         RETURN (build_lambda_object (r));
   23283              :       }
   23284              : 
   23285           12 :     case TRANSACTION_EXPR:
   23286           12 :       gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t));
   23287           12 :       RETURN (tsubst_stmt (t, args, complain, in_decl));
   23288              : 
   23289       250831 :     case PAREN_EXPR:
   23290       250831 :       if (REF_PARENTHESIZED_P (t))
   23291       250830 :         RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
   23292              :       else
   23293              :         /* Recreate the PAREN_EXPR from __builtin_assoc_barrier.  */
   23294              :         {
   23295            1 :           tree op0 = RECUR (TREE_OPERAND (t, 0));
   23296            1 :           RETURN (build1_loc (input_location, PAREN_EXPR,
   23297              :                               TREE_TYPE (op0), op0));
   23298              :         }
   23299              : 
   23300           23 :     case VEC_PERM_EXPR:
   23301           23 :       {
   23302           23 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23303           23 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   23304           23 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   23305           23 :         RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
   23306              :                                        complain));
   23307              :       }
   23308              : 
   23309     11480378 :     case REQUIRES_EXPR:
   23310     11480378 :       {
   23311     11480378 :         complain &= ~tf_warning_or_error;
   23312     11480378 :         tree r = tsubst_requires_expr (t, args, complain, in_decl);
   23313     11477678 :         RETURN (r);
   23314              :       }
   23315              : 
   23316           12 :     case RANGE_EXPR:
   23317              :       /* No need to substitute further, a RANGE_EXPR will always be built
   23318              :          with constant operands.  */
   23319           12 :       RETURN (t);
   23320              : 
   23321    150233756 :     case NON_LVALUE_EXPR:
   23322    150233756 :     case VIEW_CONVERT_EXPR:
   23323    150233756 :       {
   23324    150233756 :         tree op = RECUR (TREE_OPERAND (t, 0));
   23325              : 
   23326    150233756 :         if (location_wrapper_p (t))
   23327              :           /* We need to do this here as well as in tsubst_copy so we get the
   23328              :              other tsubst_copy_and_build semantics for a PARM_DECL operand.  */
   23329    150227633 :           RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
   23330              : 
   23331         6123 :         gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
   23332         6123 :         if (REF_PARENTHESIZED_P (t))
   23333              :           /* force_paren_expr can also create a VIEW_CONVERT_EXPR.  */
   23334            0 :           RETURN (finish_parenthesized_expr (op));
   23335              : 
   23336         6123 :         check_param_in_postcondition (op, EXPR_LOCATION (t));
   23337              : 
   23338         6123 :         if (flag_contracts && processing_contract_condition)
   23339          302 :             op = constify_contract_access (op);
   23340              : 
   23341              :         /* Otherwise, we're dealing with a wrapper to make a C++20 template
   23342              :            parameter object const.  */
   23343         6123 :         if (TREE_TYPE (op) == NULL_TREE
   23344         6123 :             || !CP_TYPE_CONST_P (TREE_TYPE (op)))
   23345              :           {
   23346              :             /* The template argument is not const, presumably because
   23347              :                it is still dependent, and so not the const template parm
   23348              :                object.  */
   23349           82 :             tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23350           82 :             if (TREE_CODE (op) == CONSTRUCTOR
   23351           79 :                 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
   23352              :               {
   23353              :                 /* Don't add a wrapper to these.  */
   23354           12 :                 op = copy_node (op);
   23355           12 :                 TREE_TYPE (op) = type;
   23356              :               }
   23357              :             else
   23358              :               /* Do add a wrapper otherwise (in particular, if op is
   23359              :                  another TEMPLATE_PARM_INDEX).  */
   23360           70 :               op = build1 (VIEW_CONVERT_EXPR, type, op);
   23361              :           }
   23362         6123 :         RETURN (op);
   23363              :       }
   23364              : 
   23365         2112 :     case REFLECT_EXPR:
   23366         2112 :       {
   23367         2112 :         tree h = REFLECT_EXPR_HANDLE (t);
   23368         2112 :         reflect_kind kind = REFLECT_EXPR_KIND (t);
   23369         2112 :         if (TYPE_P (h) || TREE_CODE (h) == NAMESPACE_DECL)
   23370         1871 :           h = tsubst (h, args, complain, in_decl);
   23371          241 :         else if (kind == REFLECT_ANNOTATION)
   23372              :           /* annotations_of should be called on reflections of already
   23373              :              instantiated entities and so no need to tsubst the annotation
   23374              :              attribute and we rely on pointer equality of that.  */
   23375              :           ;
   23376          240 :         else if (TREE_CODE (h) == SCOPE_REF)
   23377           27 :           h = tsubst_qualified_id (h, args, complain, in_decl,
   23378              :                                    /*done=*/true, /*address_p=*/false,
   23379              :                                    /*reflecting_p=*/true);
   23380              :         else
   23381              :           {
   23382              :             /* [expr.reflect] The id-expression of a reflect-expression is
   23383              :                an unevaluated operand.  */
   23384          213 :             cp_unevaluated u;
   23385          213 :             h = RECUR (h);
   23386          213 :           }
   23387         2112 :         RETURN (get_reflection (EXPR_LOCATION (t), h, kind));
   23388              :       }
   23389              : 
   23390          562 :     case SPLICE_EXPR:
   23391          562 :       RETURN (tsubst_splice_expr (t, args, complain, in_decl));
   23392              : 
   23393            0 :     default:
   23394              :       /* Handle Objective-C++ constructs, if appropriate.  */
   23395            0 :       if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
   23396            0 :         RETURN (subst);
   23397              : 
   23398              :       /* We shouldn't get here, but keep going if !flag_checking.  */
   23399            0 :       if (flag_checking)
   23400            0 :         gcc_unreachable ();
   23401            0 :       RETURN (t);
   23402              :     }
   23403              : 
   23404              : #undef RECUR
   23405              : #undef RETURN
   23406   1753633317 :  out:
   23407   1753633317 :   input_location = save_loc;
   23408   1753633317 :   return retval;
   23409              : }
   23410              : 
   23411              : /* Verify that the instantiated ARGS are valid. For type arguments,
   23412              :    make sure that the type's linkage is ok. For non-type arguments,
   23413              :    make sure they are constants if they are integral or enumerations.
   23414              :    Emit an error under control of COMPLAIN, and return TRUE on error.  */
   23415              : 
   23416              : static bool
   23417    275297018 : check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
   23418              : {
   23419    275297018 :   if (dependent_template_arg_p (t))
   23420              :     return false;
   23421    249312568 :   if (ARGUMENT_PACK_P (t))
   23422              :     {
   23423     17516886 :       tree vec = ARGUMENT_PACK_ARGS (t);
   23424     17516886 :       int len = TREE_VEC_LENGTH (vec);
   23425     17516886 :       bool result = false;
   23426     17516886 :       int i;
   23427              : 
   23428     48498582 :       for (i = 0; i < len; ++i)
   23429     30981696 :         if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
   23430            0 :           result = true;
   23431              :       return result;
   23432              :     }
   23433    231795682 :   else if (TYPE_P (t))
   23434              :     {
   23435              :       /* [basic.link]: A name with no linkage (notably, the name
   23436              :          of a class or enumeration declared in a local scope)
   23437              :          shall not be used to declare an entity with linkage.
   23438              :          This implies that names with no linkage cannot be used as
   23439              :          template arguments
   23440              : 
   23441              :          DR 757 relaxes this restriction for C++0x.  */
   23442    195577867 :       tree nt = (cxx_dialect > cxx98 ? NULL_TREE
   23443       176976 :                  : no_linkage_check (t, /*relaxed_p=*/false));
   23444              : 
   23445       176976 :       if (nt)
   23446              :         {
   23447              :           /* DR 488 makes use of a type with no linkage cause
   23448              :              type deduction to fail.  */
   23449            8 :           if (complain & tf_error)
   23450              :             {
   23451           10 :               if (TYPE_UNNAMED_P (nt))
   23452            0 :                 error ("%qT is/uses unnamed type", t);
   23453              :               else
   23454            5 :                 error ("template argument for %qD uses local type %qT",
   23455              :                        tmpl, t);
   23456              :             }
   23457            8 :           return true;
   23458              :         }
   23459              :       /* In order to avoid all sorts of complications, we do not
   23460              :          allow variably-modified types as template arguments.  */
   23461    195577859 :       else if (variably_modified_type_p (t, NULL_TREE))
   23462              :         {
   23463            6 :           if (complain & tf_error)
   23464            3 :             error ("%qT is a variably modified type", t);
   23465            6 :           return true;
   23466              :         }
   23467              :     }
   23468              :   /* Class template and alias template arguments should be OK.  */
   23469     36217815 :   else if (DECL_TYPE_TEMPLATE_P (t))
   23470              :     ;
   23471              :   /* A non-type argument of integral or enumerated type must be a
   23472              :      constant.  */
   23473     35740278 :   else if (TREE_TYPE (t)
   23474     35740278 :            && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
   23475     35728267 :            && !REFERENCE_REF_P (t)
   23476     71468414 :            && !TREE_CONSTANT (t))
   23477              :     {
   23478            0 :       if (complain & tf_error)
   23479            0 :         error ("integral expression %qE is not constant", t);
   23480            0 :       return true;
   23481              :     }
   23482              :   return false;
   23483              : }
   23484              : 
   23485              : static bool
   23486    159195371 : check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
   23487              : {
   23488    159195371 :   int ix, len = DECL_NTPARMS (tmpl);
   23489    159195371 :   bool result = false;
   23490              : 
   23491    403510693 :   for (ix = 0; ix != len; ix++)
   23492              :     {
   23493    244315322 :       if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
   23494           14 :         result = true;
   23495              :     }
   23496    159195371 :   if (result && (complain & tf_error))
   23497            8 :     error ("  trying to instantiate %qD", tmpl);
   23498    159195371 :   return result;
   23499              : }
   23500              : 
   23501              : /* Call mark_used on each entity within the non-type template arguments in
   23502              :    ARGS for an instantiation of TMPL, to ensure that each such entity is
   23503              :    considered odr-used (and therefore marked for instantiation) regardless of
   23504              :    whether the specialization was first formed in a template context (which
   23505              :    inhibits mark_used).
   23506              : 
   23507              :    This function assumes push_to_top_level has been called beforehand.  */
   23508              : 
   23509              : static void
   23510     75921599 : mark_template_arguments_used (tree tmpl, tree args)
   23511              : {
   23512              :   /* It suffices to do this only when instantiating a primary template.  */
   23513     75921599 :   if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
   23514              :     return;
   23515              : 
   23516              :   /* We already marked outer arguments when specializing the context.  */
   23517     61181966 :   args = INNERMOST_TEMPLATE_ARGS (args);
   23518              : 
   23519    158828158 :   for (tree arg : tree_vec_range (args))
   23520              :     {
   23521              :       /* A (pointer/reference to) function or variable NTTP argument.  */
   23522     97646192 :       if (TREE_CODE (arg) == ADDR_EXPR
   23523     97644988 :           || TREE_CODE (arg) == INDIRECT_REF)
   23524              :         {
   23525         3284 :           while (TREE_CODE (arg) == ADDR_EXPR
   23526         1861 :                  || REFERENCE_REF_P (arg)
   23527         4926 :                  || CONVERT_EXPR_P (arg))
   23528         1861 :             arg = TREE_OPERAND (arg, 0);
   23529         1423 :           if (VAR_OR_FUNCTION_DECL_P (arg))
   23530              :             {
   23531              :               /* Pass tf_none to avoid duplicate diagnostics: if this call
   23532              :                  fails then an earlier call to mark_used for this argument
   23533              :                  must have also failed and emitted a diagnostic.  */
   23534         1409 :               bool ok = mark_used (arg, tf_none);
   23535         1409 :               gcc_checking_assert (ok || seen_error ());
   23536              :             }
   23537              :         }
   23538              :       /* A member function pointer.  */
   23539     97644769 :       else if (TREE_CODE (arg) == PTRMEM_CST)
   23540              :         {
   23541          478 :           bool ok = mark_used (PTRMEM_CST_MEMBER (arg), tf_none);
   23542          478 :           gcc_checking_assert (ok || seen_error ());
   23543              :         }
   23544              :       /* A class NTTP argument.  */
   23545     97644291 :       else if (VAR_P (arg)
   23546     97644291 :                && DECL_NTTP_OBJECT_P (arg))
   23547              :         {
   23548        29787 :           auto mark_used_r = [](tree *tp, int *, void *) {
   23549        21329 :             if (VAR_OR_FUNCTION_DECL_P (*tp))
   23550              :               {
   23551           49 :                 bool ok = mark_used (*tp, tf_none);
   23552           49 :                 gcc_checking_assert (ok || seen_error ());
   23553              :               }
   23554        21329 :             return NULL_TREE;
   23555              :           };
   23556         8458 :           cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
   23557              :                                            mark_used_r, nullptr);
   23558              :         }
   23559              :     }
   23560              : }
   23561              : 
   23562              : /* We're out of SFINAE context now, so generate diagnostics for the access
   23563              :    errors we saw earlier when instantiating D from TMPL and ARGS.  */
   23564              : 
   23565              : static void
   23566            9 : recheck_decl_substitution (tree d, tree tmpl, tree args)
   23567              : {
   23568            9 :   tree pattern = DECL_TEMPLATE_RESULT (tmpl);
   23569            9 :   tree type = TREE_TYPE (pattern);
   23570            9 :   location_t loc = input_location;
   23571              : 
   23572            9 :   push_access_scope (d);
   23573            9 :   push_deferring_access_checks (dk_no_deferred);
   23574            9 :   input_location = DECL_SOURCE_LOCATION (pattern);
   23575            9 :   tsubst (type, args, tf_warning_or_error, d);
   23576            9 :   input_location = loc;
   23577            9 :   pop_deferring_access_checks ();
   23578            9 :   pop_access_scope (d);
   23579            9 : }
   23580              : 
   23581              : /* Instantiate the indicated variable, function, or alias template TMPL with
   23582              :    the template arguments in TARG_PTR.  */
   23583              : 
   23584              : tree
   23585    255105094 : instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
   23586              : {
   23587    255105094 :   auto_timevar tv (TV_TEMPLATE_INST);
   23588              : 
   23589    255105094 :   tree targ_ptr = orig_args;
   23590    255105094 :   tree fndecl;
   23591    255105094 :   tree gen_tmpl;
   23592    255105094 :   bool access_ok = true;
   23593              : 
   23594    255105094 :   if (tmpl == error_mark_node)
   23595              :     return error_mark_node;
   23596              : 
   23597              :   /* The other flags are not relevant anymore here, especially tf_partial
   23598              :      shouldn't be set.  For instance, we may be called while doing a partial
   23599              :      substitution of a template variable, but the type of the variable
   23600              :      template may be auto, in which case we will call do_auto_deduction
   23601              :      in mark_used (which clears tf_partial) and the auto must be properly
   23602              :      reduced at that time for the deduction to work.
   23603              : 
   23604              :      Note that later below we set tf_partial iff there are dependent arguments;
   23605              :      the above is concerned specifically about the non-dependent case.  */
   23606    255105085 :   complain &= tf_warning_or_error;
   23607              : 
   23608    255105085 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
   23609              : 
   23610    255105085 :   if (modules_p ())
   23611      1078805 :     lazy_load_pendings (tmpl);
   23612              : 
   23613              :   /* If this function is a clone, handle it specially.  */
   23614    255105085 :   if (DECL_CLONED_FUNCTION_P (tmpl))
   23615              :     {
   23616      5274583 :       tree spec;
   23617      5274583 :       tree clone;
   23618              : 
   23619              :       /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
   23620              :          DECL_CLONED_FUNCTION.  */
   23621      5274583 :       spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
   23622              :                                    targ_ptr, complain);
   23623      5274583 :       if (spec == error_mark_node)
   23624              :         return error_mark_node;
   23625              : 
   23626              :       /* Look for the clone.  */
   23627     10326846 :       FOR_EACH_CLONE (clone, spec)
   23628     10326846 :         if (DECL_NAME (clone) == DECL_NAME (tmpl))
   23629              :           return clone;
   23630              :       /* We should always have found the clone by now.  */
   23631            0 :       gcc_unreachable ();
   23632              :       return NULL_TREE;
   23633              :     }
   23634              : 
   23635    249830502 :   if (targ_ptr == error_mark_node)
   23636              :     return error_mark_node;
   23637              : 
   23638              :   /* Check to see if we already have this specialization.  */
   23639    249830502 :   gen_tmpl = most_general_template (tmpl);
   23640    499661004 :   if (TMPL_ARGS_DEPTH (targ_ptr)
   23641    249830502 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
   23642              :     /* targ_ptr only has the innermost template args, so add the outer ones
   23643              :        from tmpl, which could be either a partial instantiation or gen_tmpl (in
   23644              :        the case of a non-dependent call within a template definition).  */
   23645      9211245 :     targ_ptr = (add_outermost_template_args
   23646      9211245 :                 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
   23647              :                  targ_ptr));
   23648              : 
   23649    249830502 :   hashval_t hash = spec_hasher::hash (gen_tmpl, targ_ptr);
   23650    249830502 :   tree spec = retrieve_specialization (gen_tmpl, targ_ptr, hash);
   23651              : 
   23652    249830502 :   gcc_checking_assert (tmpl == gen_tmpl
   23653              :                        || ((fndecl
   23654              :                             = retrieve_specialization (tmpl, orig_args, 0))
   23655              :                            == spec)
   23656              :                        || fndecl == NULL_TREE);
   23657              : 
   23658    249830502 :   if (spec != NULL_TREE)
   23659              :     {
   23660    140078138 :       if (FNDECL_HAS_ACCESS_ERRORS (spec))
   23661              :         {
   23662           15 :           if (complain & tf_error)
   23663            9 :             recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
   23664           15 :           return error_mark_node;
   23665              :         }
   23666              :       return spec;
   23667              :     }
   23668              : 
   23669    109752364 :   if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
   23670              :                                complain))
   23671            6 :     return error_mark_node;
   23672              : 
   23673              :   /* We are building a FUNCTION_DECL, during which the access of its
   23674              :      parameters and return types have to be checked.  However this
   23675              :      FUNCTION_DECL which is the desired context for access checking
   23676              :      is not built yet.  We solve this chicken-and-egg problem by
   23677              :      deferring all checks until we have the FUNCTION_DECL.  */
   23678    109752358 :   deferring_access_check_sentinel dacs (dk_deferred);
   23679              : 
   23680              :   /* Instantiation of the function happens in the context of the function
   23681              :      template, not the context of the overload resolution we're doing.  */
   23682    109752358 :   push_to_top_level ();
   23683              :   /* If there are dependent arguments, e.g. because we're doing partial
   23684              :      ordering, make sure processing_template_decl stays set.  And set
   23685              :      tf_partial mainly for benefit of instantiation of alias templates
   23686              :      that contain extra-args trees.  */
   23687    109752358 :   if (uses_template_parms (targ_ptr))
   23688              :     {
   23689     18900009 :       ++processing_template_decl;
   23690     18900009 :       complain |= tf_partial;
   23691              :     }
   23692    109752358 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   23693              :     {
   23694     26220225 :       tree ctx;
   23695     26220225 :       if (!uses_template_parms (DECL_CONTEXT (tmpl)))
   23696              :         /* If the context of the partially instantiated template is
   23697              :            already non-dependent, then we might as well use it.  */
   23698     23215235 :         ctx = DECL_CONTEXT (tmpl);
   23699              :       else
   23700      3004990 :         ctx = tsubst_entering_scope (DECL_CONTEXT (gen_tmpl), targ_ptr,
   23701              :                                      complain, gen_tmpl);
   23702     26220225 :       push_nested_class (ctx);
   23703              :     }
   23704              : 
   23705    109752358 :   tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
   23706              : 
   23707    109752358 :   tree partial_ti = NULL_TREE;
   23708    109752358 :   fndecl = NULL_TREE;
   23709    109752358 :   if (VAR_P (pattern))
   23710              :     {
   23711              :       /* We need to determine if we're using a partial or explicit
   23712              :          specialization now, because the type of the variable could be
   23713              :          different.  */
   23714     12477695 :       tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
   23715     12477695 :       partial_ti = most_specialized_partial_spec (tid, complain);
   23716     12477695 :       if (partial_ti == error_mark_node)
   23717              :         pattern = error_mark_node;
   23718     12477692 :       else if (partial_ti)
   23719              :         {
   23720       837852 :           tree partial_tmpl = TI_TEMPLATE (partial_ti);
   23721       837852 :           tree partial_args = TI_ARGS (partial_ti);
   23722       837852 :           tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
   23723       837852 :           fndecl = tsubst_decl (partial_pat, partial_args, complain,
   23724              :                                 /*use_spec_table=*/false);
   23725              :         }
   23726              :     }
   23727              : 
   23728              :   /* Substitute template parameters to obtain the specialization.  */
   23729       837852 :   if (fndecl == NULL_TREE)
   23730    108914506 :     fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false);
   23731    109744222 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   23732     26220225 :     pop_nested_class ();
   23733    109744222 :   pop_from_top_level ();
   23734              : 
   23735    109744222 :   if (fndecl == error_mark_node)
   23736              :     return error_mark_node;
   23737              : 
   23738              :   /* Substituting the type might have recursively instantiated this
   23739              :      same alias (c++/117530).  */
   23740     73087170 :   if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   23741    104400027 :       && (spec = retrieve_specialization (gen_tmpl, targ_ptr, hash)))
   23742              :     return spec;
   23743              : 
   23744              :   /* The DECL_TI_TEMPLATE should always be the immediate parent
   23745              :      template, not the most general template.  */
   23746     73087158 :   DECL_TI_TEMPLATE (fndecl) = tmpl;
   23747     73087158 :   DECL_TI_ARGS (fndecl) = targ_ptr;
   23748     73087158 :   if (VAR_P (pattern))
   23749              :     {
   23750              :       /* Now that we we've formed this variable template specialization,
   23751              :          remember the result of most_specialized_partial_spec for it.  */
   23752     12477692 :       TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
   23753              : 
   23754              :       /* And remember if the variable was declared with [].  */
   23755     12477692 :       if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
   23756     12477692 :           && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
   23757           12 :         SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
   23758              :     }
   23759              : 
   23760     73087158 :   fndecl = register_specialization (fndecl, gen_tmpl, targ_ptr, false, hash);
   23761     73087158 :   if (fndecl == error_mark_node)
   23762              :     return error_mark_node;
   23763              : 
   23764     73087158 :   set_instantiating_module (fndecl);
   23765              : 
   23766              :   /* Now we know the specialization, compute access previously
   23767              :      deferred.  Do no access control for inheriting constructors,
   23768              :      as we already checked access for the inherited constructor.  */
   23769     73087158 :   if (!(flag_new_inheriting_ctors
   23770    102382675 :         && DECL_INHERITED_CTOR (fndecl)))
   23771              :     {
   23772     73075630 :       push_access_scope (fndecl);
   23773     73075630 :       if (!perform_deferred_access_checks (complain))
   23774              :         access_ok = false;
   23775     73075630 :       pop_access_scope (fndecl);
   23776              :     }
   23777              : 
   23778              :   /* If we've just instantiated the main entry point for a function,
   23779              :      instantiate all the alternate entry points as well.  We do this
   23780              :      by cloning the instantiation of the main entry point, not by
   23781              :      instantiating the template clones.  */
   23782     73087158 :   if (tree chain = DECL_CHAIN (gen_tmpl))
   23783     72637320 :     if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
   23784       174505 :       clone_cdtor (fndecl, /*update_methods=*/false);
   23785              : 
   23786     73087158 :   if (!access_ok)
   23787              :     {
   23788           83 :       if (!(complain & tf_error))
   23789              :         {
   23790              :           /* Remember to reinstantiate when we're out of SFINAE so the user
   23791              :              can see the errors.  */
   23792           83 :           FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
   23793              :         }
   23794           83 :       return error_mark_node;
   23795              :     }
   23796              : 
   23797              :   return fndecl;
   23798    255096958 : }
   23799              : 
   23800              : /* Instantiate the alias template TMPL with ARGS.  Also push a template
   23801              :    instantiation level, which instantiate_template doesn't do because
   23802              :    functions and variables have sufficient context established by the
   23803              :    callers.  */
   23804              : 
   23805              : static tree
   23806    146416912 : instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
   23807              : {
   23808    146416912 :   if (tmpl == error_mark_node || args == error_mark_node)
   23809              :     return error_mark_node;
   23810              : 
   23811    145607819 :   args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
   23812              :                                 args, tmpl, complain);
   23813    145607819 :   if (args == error_mark_node)
   23814              :     return error_mark_node;
   23815              : 
   23816              :   /* FIXME check for satisfaction in check_instantiated_args.  */
   23817    145607087 :   if (!constraints_satisfied_p (tmpl, args))
   23818              :     {
   23819       212468 :       if (complain & tf_error)
   23820              :         {
   23821           27 :           auto_diagnostic_group d;
   23822           27 :           error ("template constraint failure for %qD", tmpl);
   23823           27 :           diagnose_constraints (input_location, tmpl, args);
   23824           27 :         }
   23825       212468 :       return error_mark_node;
   23826              :     }
   23827              : 
   23828    145394619 :   if (!push_tinst_level (tmpl, args))
   23829            0 :     return error_mark_node;
   23830    145394616 :   tree r = instantiate_template (tmpl, args, complain);
   23831    145394616 :   pop_tinst_level ();
   23832              : 
   23833    145394616 :   if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
   23834              :     {
   23835              :       /* Note this is also done at parse time from push_template_decl.  */
   23836              :       /* An alias template specialization can be dependent
   23837              :          even if its underlying type is not.  */
   23838     11247957 :       TYPE_DEPENDENT_P (d) = true;
   23839     11247957 :       TYPE_DEPENDENT_P_VALID (d) = true;
   23840              :       /* Sometimes a dependent alias spec is equivalent to its expansion,
   23841              :          sometimes not.  So always use structural_comptypes.  */
   23842     11247957 :       SET_TYPE_STRUCTURAL_EQUALITY (d);
   23843              :     }
   23844              : 
   23845              :   return r;
   23846              : }
   23847              : 
   23848              : /* PARM is a template parameter pack for FN.  Returns true iff
   23849              :    PARM is used in a deducible way in the argument list of FN.  */
   23850              : 
   23851              : static bool
   23852      6050567 : pack_deducible_p (tree parm, tree fn)
   23853              : {
   23854      6050567 :   tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
   23855     12083973 :   for (; t; t = TREE_CHAIN (t))
   23856              :     {
   23857      6033732 :       tree type = TREE_VALUE (t);
   23858      6033732 :       tree packs;
   23859      6033732 :       if (!PACK_EXPANSION_P (type))
   23860      6032099 :         continue;
   23861         3266 :       for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
   23862         2943 :            packs; packs = TREE_CHAIN (packs))
   23863         1636 :         if (template_args_equal (TREE_VALUE (packs), parm))
   23864              :           {
   23865              :             /* The template parameter pack is used in a function parameter
   23866              :                pack.  If this is the end of the parameter list, the
   23867              :                template parameter pack is deducible.  */
   23868          326 :             if (TREE_CHAIN (t) == void_list_node)
   23869              :               return true;
   23870              :             else
   23871              :               /* Otherwise, not.  Well, it could be deduced from
   23872              :                  a non-pack parameter, but doing so would end up with
   23873              :                  a deduction mismatch, so don't bother.  */
   23874              :               return false;
   23875              :           }
   23876              :     }
   23877              :   /* The template parameter pack isn't used in any function parameter
   23878              :      packs, but it might be used deeper, e.g. tuple<Args...>.  */
   23879              :   return true;
   23880              : }
   23881              : 
   23882              : /* Subroutine of fn_type_unification: check non-dependent parms for
   23883              :    convertibility.  */
   23884              : 
   23885              : static int
   23886    104160929 : check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
   23887              :                                  tree fn, unification_kind_t strict, int flags,
   23888              :                                  struct conversion **convs, bool explain_p,
   23889              :                                  bool noninst_only_p)
   23890              : {
   23891              :   /* Non-constructor methods need to leave a conversion for 'this', which
   23892              :      isn't included in nargs here.  */
   23893    104160929 :   unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   23894    122353479 :                      && !DECL_CONSTRUCTOR_P (fn));
   23895              : 
   23896    104160929 :   for (unsigned ia = 0;
   23897    239154330 :        parms && parms != void_list_node && ia < nargs; )
   23898              :     {
   23899    138869208 :       tree parm = TREE_VALUE (parms);
   23900              : 
   23901    138869208 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   23902    138869208 :           && (!TREE_CHAIN (parms)
   23903      1295401 :               || TREE_CHAIN (parms) == void_list_node))
   23904              :         /* For a function parameter pack that occurs at the end of the
   23905              :            parameter-declaration-list, the type A of each remaining
   23906              :            argument of the call is compared with the type P of the
   23907              :            declarator-id of the function parameter pack.  */
   23908              :         break;
   23909              : 
   23910    137573882 :       parms = TREE_CHAIN (parms);
   23911              : 
   23912    137573882 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   23913              :         /* For a function parameter pack that does not occur at the
   23914              :            end of the parameter-declaration-list, the type of the
   23915              :            parameter pack is a non-deduced context.  */
   23916           81 :         continue;
   23917              : 
   23918    137573801 :       if (!uses_template_parms (parm))
   23919              :         {
   23920     30458706 :           tree arg = args[ia];
   23921     30458706 :           conversion **conv_p = convs ? &convs[ia+offset] : NULL;
   23922     30458706 :           int lflags = conv_flags (ia, nargs, fn, arg, flags);
   23923              : 
   23924     30458706 :           if (check_non_deducible_conversion (parm, arg, strict, lflags,
   23925              :                                               conv_p, explain_p, noninst_only_p))
   23926              :             return 1;
   23927              :         }
   23928              : 
   23929    134993320 :       ++ia;
   23930              :     }
   23931              : 
   23932              :   return 0;
   23933              : }
   23934              : 
   23935              : /* The FN is a TEMPLATE_DECL for a function.  ARGS is an array with
   23936              :    NARGS elements of the arguments that are being used when calling
   23937              :    it.  TARGS is a vector into which the deduced template arguments
   23938              :    are placed.
   23939              : 
   23940              :    Returns either a FUNCTION_DECL for the matching specialization of FN or
   23941              :    NULL_TREE if no suitable specialization can be found.  If EXPLAIN_P is
   23942              :    true, diagnostics will be printed to explain why it failed.
   23943              : 
   23944              :    If FN is a conversion operator, or we are trying to produce a specific
   23945              :    specialization, RETURN_TYPE is the return type desired.
   23946              : 
   23947              :    The EXPLICIT_TARGS are explicit template arguments provided via a
   23948              :    template-id.
   23949              : 
   23950              :    The parameter STRICT is one of:
   23951              : 
   23952              :    DEDUCE_CALL:
   23953              :      We are deducing arguments for a function call, as in
   23954              :      [temp.deduct.call].  If RETURN_TYPE is non-null, we are
   23955              :      deducing arguments for a call to the result of a conversion
   23956              :      function template, as in [over.call.object].
   23957              : 
   23958              :    DEDUCE_CONV:
   23959              :      We are deducing arguments for a conversion function, as in
   23960              :      [temp.deduct.conv].
   23961              : 
   23962              :    DEDUCE_EXACT:
   23963              :      We are deducing arguments when doing an explicit instantiation
   23964              :      as in [temp.explicit], when determining an explicit specialization
   23965              :      as in [temp.expl.spec], or when taking the address of a function
   23966              :      template, as in [temp.deduct.funcaddr].  */
   23967              : 
   23968              : tree
   23969    489674968 : fn_type_unification (tree fn,
   23970              :                      tree explicit_targs,
   23971              :                      tree targs,
   23972              :                      const tree *args,
   23973              :                      unsigned int nargs,
   23974              :                      tree return_type,
   23975              :                      unification_kind_t strict,
   23976              :                      int flags,
   23977              :                      struct conversion **convs,
   23978              :                      bool explain_p,
   23979              :                      bool decltype_p)
   23980              : {
   23981    489674968 :   tree parms;
   23982    489674968 :   tree fntype;
   23983    489674968 :   tree decl = NULL_TREE;
   23984    489674968 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   23985    489674968 :   bool ok;
   23986    489674968 :   static int deduction_depth;
   23987              :   /* type_unification_real will pass back any access checks from default
   23988              :      template argument substitution.  */
   23989    489674968 :   vec<deferred_access_check, va_gc> *checks = NULL;
   23990              :   /* We don't have all the template args yet.  */
   23991    489674968 :   bool incomplete = true;
   23992              : 
   23993    489674968 :   tree orig_fn = fn;
   23994    489674968 :   if (flag_new_inheriting_ctors)
   23995    489673170 :     fn = strip_inheriting_ctors (fn);
   23996              : 
   23997    489674968 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
   23998    489674968 :   tree r = error_mark_node;
   23999              : 
   24000    489674968 :   tree full_targs = targs;
   24001    979349936 :   if (TMPL_ARGS_DEPTH (targs)
   24002    489674968 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
   24003        45957 :     full_targs = (add_outermost_template_args
   24004        45957 :                   (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
   24005              :                    targs));
   24006              : 
   24007    489674968 :   if (decltype_p)
   24008     25015028 :     complain |= tf_decltype;
   24009              : 
   24010              :   /* In C++0x, it's possible to have a function template whose type depends
   24011              :      on itself recursively.  This is most obvious with decltype, but can also
   24012              :      occur with enumeration scope (c++/48969).  So we need to catch infinite
   24013              :      recursion and reject the substitution at deduction time; this function
   24014              :      will return error_mark_node for any repeated substitution.
   24015              : 
   24016              :      This also catches excessive recursion such as when f<N> depends on
   24017              :      f<N-1> across all integers, and returns error_mark_node for all the
   24018              :      substitutions back up to the initial one.
   24019              : 
   24020              :      This is, of course, not reentrant.  */
   24021    489674968 :   if (excessive_deduction_depth)
   24022            0 :     return error_mark_node;
   24023    489674968 :   ++deduction_depth;
   24024              : 
   24025    489674968 :   gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
   24026              : 
   24027    489674968 :   fntype = TREE_TYPE (fn);
   24028    489674968 :   if (explicit_targs)
   24029              :     {
   24030              :       /* [temp.deduct]
   24031              : 
   24032              :          The specified template arguments must match the template
   24033              :          parameters in kind (i.e., type, nontype, template), and there
   24034              :          must not be more arguments than there are parameters;
   24035              :          otherwise type deduction fails.
   24036              : 
   24037              :          Nontype arguments must match the types of the corresponding
   24038              :          nontype template parameters, or must be convertible to the
   24039              :          types of the corresponding nontype parameters as specified in
   24040              :          _temp.arg.nontype_, otherwise type deduction fails.
   24041              : 
   24042              :          All references in the function type of the function template
   24043              :          to the corresponding template parameters are replaced by the
   24044              :          specified template argument values.  If a substitution in a
   24045              :          template parameter or in the function type of the function
   24046              :          template results in an invalid type, type deduction fails.  */
   24047     48642802 :       int i, len = TREE_VEC_LENGTH (tparms);
   24048     48642802 :       location_t loc = input_location;
   24049     48642802 :       incomplete = false;
   24050              : 
   24051     48642802 :       if (explicit_targs == error_mark_node)
   24052            0 :         goto fail;
   24053              : 
   24054     97285604 :       if (TMPL_ARGS_DEPTH (explicit_targs)
   24055     97285604 :           < TMPL_ARGS_DEPTH (full_targs))
   24056        17719 :         explicit_targs = add_outermost_template_args (full_targs,
   24057              :                                                       explicit_targs);
   24058              : 
   24059              :       /* Adjust any explicit template arguments before entering the
   24060              :          substitution context.  */
   24061     48642802 :       explicit_targs
   24062     48642802 :         = (coerce_template_parms (tparms, explicit_targs, fn,
   24063              :                                   complain|tf_partial,
   24064              :                                   /*require_all_args=*/false));
   24065     48642802 :       if (explicit_targs == error_mark_node)
   24066      3107506 :         goto fail;
   24067              : 
   24068              :       /* Substitute the explicit args into the function type.  This is
   24069              :          necessary so that, for instance, explicitly declared function
   24070              :          arguments can match null pointed constants.  If we were given
   24071              :          an incomplete set of explicit args, we must not do semantic
   24072              :          processing during substitution as we could create partial
   24073              :          instantiations.  */
   24074    104519378 :       for (i = 0; i < len; i++)
   24075              :         {
   24076     58984082 :           tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
   24077     58984082 :           bool parameter_pack = false;
   24078     58984082 :           tree targ = TREE_VEC_ELT (explicit_targs, i);
   24079              : 
   24080              :           /* Dig out the actual parm.  */
   24081     58984082 :           if (TREE_CODE (parm) == TYPE_DECL
   24082      6490234 :               || TREE_CODE (parm) == TEMPLATE_DECL)
   24083              :             {
   24084     52498268 :               parm = TREE_TYPE (parm);
   24085     52498268 :               parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
   24086              :             }
   24087      6485814 :           else if (TREE_CODE (parm) == PARM_DECL)
   24088              :             {
   24089      6485814 :               parm = DECL_INITIAL (parm);
   24090      6485814 :               parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
   24091              :             }
   24092              : 
   24093     58984082 :           if (targ == NULL_TREE)
   24094              :             /* No explicit argument for this template parameter.  */
   24095              :             incomplete = true;
   24096     47307785 :           else if (parameter_pack && pack_deducible_p (parm, fn))
   24097              :             {
   24098              :               /* Mark the argument pack as "incomplete". We could
   24099              :                  still deduce more arguments during unification.
   24100              :                  We remove this mark in type_unification_real.  */
   24101      6050546 :               ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
   24102      6050546 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ)
   24103      6050546 :                 = ARGUMENT_PACK_ARGS (targ);
   24104              : 
   24105              :               /* We have some incomplete argument packs.  */
   24106      6050546 :               incomplete = true;
   24107              :             }
   24108              :         }
   24109              : 
   24110     45535296 :       if (incomplete)
   24111              :         {
   24112     13898481 :           if (!push_tinst_level (fn, explicit_targs))
   24113              :             {
   24114            0 :               excessive_deduction_depth = true;
   24115            0 :               goto fail;
   24116              :             }
   24117     13898475 :           ++processing_template_decl;
   24118     13898475 :           input_location = DECL_SOURCE_LOCATION (fn);
   24119              :           /* Ignore any access checks; we'll see them again in
   24120              :              instantiate_template and they might have the wrong
   24121              :              access path at this point.  */
   24122     13898475 :           push_deferring_access_checks (dk_deferred);
   24123     13898475 :           tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
   24124     13898475 :           fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
   24125     13895778 :           pop_deferring_access_checks ();
   24126     13895778 :           input_location = loc;
   24127     13895778 :           --processing_template_decl;
   24128     13895778 :           pop_tinst_level ();
   24129              : 
   24130     13895778 :           if (fntype == error_mark_node)
   24131        18671 :             goto fail;
   24132              :         }
   24133              : 
   24134              :       /* Place the explicitly specified arguments in TARGS.  */
   24135     45513922 :       explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
   24136    104455138 :       for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
   24137     58941216 :         TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
   24138     45513922 :       if (!incomplete && CHECKING_P
   24139     77150737 :           && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   24140     31636815 :         SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
   24141              :           (targs, NUM_TMPL_ARGS (explicit_targs));
   24142              :     }
   24143              : 
   24144    486546088 :   if (return_type && strict != DEDUCE_CALL)
   24145              :     {
   24146     14927198 :       tree *new_args = XALLOCAVEC (tree, nargs + 1);
   24147     14927198 :       new_args[0] = return_type;
   24148     14927198 :       memcpy (new_args + 1, args, nargs * sizeof (tree));
   24149     14927198 :       args = new_args;
   24150     14927198 :       ++nargs;
   24151              :     }
   24152              : 
   24153    486546088 :   if (!incomplete)
   24154     31636815 :     goto deduced;
   24155              : 
   24156              :   /* Never do unification on the 'this' parameter.  */
   24157    454909273 :   parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
   24158              : 
   24159    454909273 :   if (return_type && strict == DEDUCE_CALL)
   24160              :     {
   24161              :       /* We're deducing for a call to the result of a template conversion
   24162              :          function.  The parms we really want are in return_type.  */
   24163           15 :       if (INDIRECT_TYPE_P (return_type))
   24164           15 :         return_type = TREE_TYPE (return_type);
   24165           15 :       parms = TYPE_ARG_TYPES (return_type);
   24166              :     }
   24167    454909258 :   else if (return_type)
   24168              :     {
   24169     13969307 :       parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
   24170              :     }
   24171              : 
   24172              :   /* We allow incomplete unification without an error message here
   24173              :      because the standard doesn't seem to explicitly prohibit it.  Our
   24174              :      callers must be ready to deal with unification failures in any
   24175              :      event.  */
   24176              : 
   24177              :   /* If we aren't explaining yet, push tinst context so we can see where
   24178              :      any errors (e.g. from class instantiations triggered by instantiation
   24179              :      of default template arguments) come from.  If we are explaining, this
   24180              :      context is redundant.  */
   24181    454909273 :   if (!explain_p && !push_tinst_level (fn, targs))
   24182              :     {
   24183            0 :       excessive_deduction_depth = true;
   24184            0 :       goto fail;
   24185              :     }
   24186              : 
   24187    454909267 :   ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   24188              :                                full_targs, parms, args, nargs, /*subr=*/0,
   24189              :                                strict, &checks, explain_p);
   24190    454909267 :   if (!explain_p)
   24191    454906547 :     pop_tinst_level ();
   24192    454909267 :   if (!ok)
   24193    399902427 :     goto fail;
   24194              : 
   24195              :   /* Now that we have bindings for all of the template arguments,
   24196              :      ensure that the arguments deduced for the template template
   24197              :      parameters have compatible template parameter lists.  We cannot
   24198              :      check this property before we have deduced all template
   24199              :      arguments, because the template parameter types of a template
   24200              :      template parameter might depend on prior template parameters
   24201              :      deduced after the template template parameter.  The following
   24202              :      ill-formed example illustrates this issue:
   24203              : 
   24204              :        template<typename T, template<T> class C> void f(C<5>, T);
   24205              : 
   24206              :        template<int N> struct X {};
   24207              : 
   24208              :        void g() {
   24209              :          f(X<5>(), 5l); // error: template argument deduction fails
   24210              :        }
   24211              : 
   24212              :      The template parameter list of 'C' depends on the template type
   24213              :      parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
   24214              :      'long'.  Thus, we can't check that 'C' cannot bind to 'X' at the
   24215              :      time that we deduce 'C'.  */
   24216    110013680 :   if (!template_template_parm_bindings_ok_p
   24217     55006840 :            (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
   24218              :     {
   24219           24 :       unify_inconsistent_template_template_parameters (explain_p);
   24220           24 :       goto fail;
   24221              :     }
   24222              : 
   24223     55006816 :  deduced:
   24224              : 
   24225              :   /* As a refinement of CWG2369, check first and foremost non-dependent
   24226              :      conversions that we know are not going to induce template instantiation
   24227              :      (PR99599).  */
   24228     86643631 :   if (strict == DEDUCE_CALL
   24229     53266611 :       && incomplete && flag_concepts
   24230    139675235 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   24231              :                                           convs, explain_p,
   24232              :                                           /*noninst_only_p=*/true))
   24233      1836209 :     goto fail;
   24234              : 
   24235              :   /* CWG2369: Check satisfaction before non-deducible conversions.  */
   24236     84807422 :   if (!constraints_satisfied_p (fn, targs))
   24237              :     {
   24238      2039101 :       if (explain_p)
   24239          663 :         diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
   24240      2039101 :       goto fail;
   24241              :     }
   24242              : 
   24243              :   /* DR 1391: All parameters have args, now check non-dependent parms for
   24244              :      convertibility.  We don't do this if all args were explicitly specified,
   24245              :      as the standard says that we substitute explicit args immediately.  */
   24246     82765621 :   if (incomplete
   24247     82765621 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   24248              :                                           convs, explain_p,
   24249              :                                           /*noninst_only_p=*/false))
   24250       744272 :     goto fail;
   24251              : 
   24252              :   /* All is well so far.  Now, check:
   24253              : 
   24254              :      [temp.deduct]
   24255              : 
   24256              :      When all template arguments have been deduced, all uses of
   24257              :      template parameters in nondeduced contexts are replaced with
   24258              :      the corresponding deduced argument values.  If the
   24259              :      substitution results in an invalid type, as described above,
   24260              :      type deduction fails.  */
   24261     82021349 :   if (!push_tinst_level (fn, targs))
   24262              :     {
   24263            0 :       excessive_deduction_depth = true;
   24264            0 :       goto fail;
   24265              :     }
   24266              : 
   24267              :   /* Also collect access checks from the instantiation.  */
   24268     82021343 :   reopen_deferring_access_checks (checks);
   24269              : 
   24270     82021343 :   decl = instantiate_template (fn, targs, complain);
   24271              : 
   24272     82013207 :   checks = get_deferred_access_checks ();
   24273     82013207 :   pop_deferring_access_checks ();
   24274              : 
   24275     82013207 :   pop_tinst_level ();
   24276              : 
   24277     82013207 :   if (decl == error_mark_node)
   24278      1546851 :     goto fail;
   24279              : 
   24280              :   /* Now perform any access checks encountered during substitution.  */
   24281     80466356 :   push_access_scope (decl);
   24282     80466356 :   ok = perform_access_checks (checks, complain);
   24283     80466356 :   pop_access_scope (decl);
   24284     80466356 :   if (!ok)
   24285           12 :     goto fail;
   24286              : 
   24287              :   /* If we're looking for an exact match, check that what we got
   24288              :      is indeed an exact match.  It might not be if some template
   24289              :      parameters are used in non-deduced contexts.  But don't check
   24290              :      for an exact match if we have dependent template arguments;
   24291              :      in that case we're doing partial ordering, and we already know
   24292              :      that we have two candidates that will provide the actual type.  */
   24293     80466344 :   if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
   24294              :     {
   24295      2230995 :       tree substed = TREE_TYPE (decl);
   24296      2230995 :       unsigned int i;
   24297              : 
   24298      2230995 :       tree sarg
   24299      2230995 :         = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
   24300      2230995 :       if (return_type)
   24301      2230803 :         sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
   24302      9914267 :       for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
   24303      7689453 :         if (!same_type_p (args[i], TREE_VALUE (sarg)))
   24304              :           {
   24305         6181 :             unify_type_mismatch (explain_p, args[i],
   24306         6181 :                                  TREE_VALUE (sarg));
   24307         6181 :             goto fail;
   24308              :           }
   24309      2224814 :       if ((i < nargs || sarg)
   24310              :           /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
   24311              :              doesn't contain the trailing void, and conv fns are always ().  */
   24312      2224814 :           && !DECL_CONV_FN_P (decl))
   24313              :         {
   24314            6 :           unsigned nsargs = i + list_length (sarg);
   24315            6 :           unify_arity (explain_p, nargs, nsargs);
   24316            6 :           goto fail;
   24317              :         }
   24318              :     }
   24319              : 
   24320              :   /* After doing deduction with the inherited constructor, actually return an
   24321              :      instantiation of the inheriting constructor.  */
   24322     80460157 :   if (orig_fn != fn)
   24323        19322 :     decl = instantiate_template (orig_fn, targs, complain);
   24324              : 
   24325              :   r = decl;
   24326              : 
   24327    489661417 :  fail:
   24328    489661417 :   --deduction_depth;
   24329    489661417 :   if (excessive_deduction_depth)
   24330              :     {
   24331            0 :       if (deduction_depth == 0)
   24332              :         /* Reset once we're all the way out.  */
   24333            0 :         excessive_deduction_depth = false;
   24334              :     }
   24335              : 
   24336              :   return r;
   24337              : }
   24338              : 
   24339              : /* Returns true iff PARM is a forwarding reference in the context of
   24340              :    template argument deduction for TMPL.  */
   24341              : 
   24342              : static bool
   24343    491302664 : forwarding_reference_p (tree parm, tree tmpl)
   24344              : {
   24345              :   /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
   24346              :      cv-unqualified template parameter ..."  */
   24347    491302664 :   if (TYPE_REF_P (parm)
   24348    429370676 :       && TYPE_REF_IS_RVALUE (parm)
   24349     15835799 :       && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
   24350    498846471 :       && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
   24351              :     {
   24352      7348252 :       parm = TREE_TYPE (parm);
   24353              :       /* [temp.deduct.call], "... that does not represent a template parameter
   24354              :          of a class template (during class template argument deduction)."  */
   24355      7348252 :       if (tmpl
   24356      7220336 :           && deduction_guide_p (tmpl)
   24357      7364707 :           && DECL_ARTIFICIAL (tmpl))
   24358              :         {
   24359              :           /* Since the template parameters of a synthesized guide consist of
   24360              :              the template parameters of the class template followed by those of
   24361              :              the constructor (if any), we can tell if PARM represents a template
   24362              :              parameter of the class template by comparing its index with the
   24363              :              arity of the class template.  */
   24364        11404 :           tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
   24365        11404 :           if (TEMPLATE_TYPE_IDX (parm)
   24366        11404 :               < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
   24367              :             return false;
   24368              :         }
   24369      7347797 :       return true;
   24370              :     }
   24371              :   return false;
   24372              : }
   24373              : 
   24374              : /* Adjust types before performing type deduction, as described in
   24375              :    [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
   24376              :    sections are symmetric.  PARM is the type of a function parameter
   24377              :    or the return type of the conversion function.  ARG is the type of
   24378              :    the argument passed to the call, or the type of the value
   24379              :    initialized with the result of the conversion function.
   24380              :    ARG_EXPR is the original argument expression, which may be null.  */
   24381              : 
   24382              : static int
   24383    491302664 : maybe_adjust_types_for_deduction (tree tparms,
   24384              :                                   unification_kind_t strict,
   24385              :                                   tree* parm,
   24386              :                                   tree* arg,
   24387              :                                   tree arg_expr)
   24388              : {
   24389    491302664 :   int result = 0;
   24390              : 
   24391    491302664 :   switch (strict)
   24392              :     {
   24393              :     case DEDUCE_CALL:
   24394              :       break;
   24395              : 
   24396       299757 :     case DEDUCE_CONV:
   24397              :       /* [temp.deduct.conv] First remove a reference type on parm.
   24398              :          DRs 322 & 976 affected this.  */
   24399       299757 :       if (TYPE_REF_P (*parm))
   24400           89 :         *parm = TREE_TYPE (*parm);
   24401              : 
   24402              :       /* Swap PARM and ARG throughout the remainder of this
   24403              :          function; the handling is precisely symmetric since PARM
   24404              :          will initialize ARG rather than vice versa.  */
   24405              :       std::swap (parm, arg);
   24406              : 
   24407              :       break;
   24408              : 
   24409     34115490 :     case DEDUCE_EXACT:
   24410              :       /* Core issue #873: Do the DR606 thing (see below) for these cases,
   24411              :          too, but here handle it by stripping the reference from PARM
   24412              :          rather than by adding it to ARG.  */
   24413     34115490 :       if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   24414      1135687 :           && TYPE_REF_P (*arg)
   24415     35142202 :           && !TYPE_REF_IS_RVALUE (*arg))
   24416      1026649 :         *parm = TREE_TYPE (*parm);
   24417              :       /* Nothing else to do in this case.  */
   24418              :       return 0;
   24419              : 
   24420            0 :     default:
   24421            0 :       gcc_unreachable ();
   24422              :     }
   24423              : 
   24424    457187174 :   if (!TYPE_REF_P (*parm))
   24425              :     {
   24426              :       /* [temp.deduct.call]
   24427              : 
   24428              :          If P is not a reference type:
   24429              : 
   24430              :          --If A is an array type, the pointer type produced by the
   24431              :          array-to-pointer standard conversion (_conv.array_) is
   24432              :          used in place of A for type deduction; otherwise,
   24433              : 
   24434              :          --If A is a function type, the pointer type produced by
   24435              :          the function-to-pointer standard conversion
   24436              :          (_conv.func_) is used in place of A for type deduction;
   24437              :          otherwise,
   24438              : 
   24439              :          --If A is a cv-qualified type, the top level
   24440              :          cv-qualifiers of A's type are ignored for type
   24441              :          deduction.  */
   24442     54438210 :       if (TREE_CODE (*arg) == ARRAY_TYPE)
   24443       572199 :         *arg = build_pointer_type (TREE_TYPE (*arg));
   24444     53866011 :       else if (TREE_CODE (*arg) == FUNCTION_TYPE)
   24445         3044 :         *arg = build_pointer_type (*arg);
   24446              :       else
   24447     53862967 :         *arg = TYPE_MAIN_VARIANT (*arg);
   24448              :     }
   24449              : 
   24450              :   /* [temp.deduct.call], "If P is a forwarding reference and the argument is
   24451              :      an lvalue, the type 'lvalue reference to A' is used in place of A for
   24452              :      type deduction."  */
   24453    457187174 :   if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   24454    457187174 :       && (arg_expr ? lvalue_p (arg_expr)
   24455              :           /* try_one_overload doesn't provide an arg_expr, but
   24456              :              functions are always lvalues.  */
   24457           47 :           : TREE_CODE (*arg) == FUNCTION_TYPE))
   24458      3646434 :     *arg = build_reference_type (*arg);
   24459              : 
   24460              :   /* [temp.deduct.call]
   24461              : 
   24462              :      If P is a cv-qualified type, the top level cv-qualifiers
   24463              :      of P's type are ignored for type deduction.  If P is a
   24464              :      reference type, the type referred to by P is used for
   24465              :      type deduction.  */
   24466    457187174 :   *parm = TYPE_MAIN_VARIANT (*parm);
   24467    457187174 :   if (TYPE_REF_P (*parm))
   24468              :     {
   24469    402748964 :       *parm = TREE_TYPE (*parm);
   24470    402748964 :       result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   24471              :     }
   24472              : 
   24473              :   return result;
   24474              : }
   24475              : 
   24476              : /* Return true if computing a conversion from FROM to TO might consider
   24477              :    user-defined conversions, which could lead to arbitrary template
   24478              :    instantiations (e.g. g++.dg/cpp2a/concepts-nondep1.C).  If this predicate
   24479              :    returns false then computing the conversion definitely won't try UDCs.
   24480              : 
   24481              :    Note that this restriction parallels LOOKUP_DEFAULTED for CWG1092, but in
   24482              :    this case we want the early filter to pass instead of fail.  */
   24483              : 
   24484              : static bool
   24485      7085735 : conversion_may_instantiate_p (tree to, tree from)
   24486              : {
   24487      7085735 :   to = non_reference (to);
   24488      7085735 :   from = non_reference (from);
   24489              : 
   24490              :   /* Converting between reference-related types is a standard conversion.  */
   24491      7085735 :   if (reference_related_p (to, from))
   24492              :     return false;
   24493              : 
   24494              :   /* Converting to a non-aggregate class type will consider its
   24495              :      user-declared constructors, which might induce instantiation.  */
   24496      7946990 :   if (CLASS_TYPE_P (complete_type (to))
   24497      5516938 :       && type_has_converting_constructor (to))
   24498              :     return true;
   24499              : 
   24500              :   /* Similarly, converting from a class type will consider its conversion
   24501              :      functions.  */
   24502      6168308 :   if (CLASS_TYPE_P (complete_type (from))
   24503      4350233 :       && TYPE_HAS_CONVERSION (from))
   24504              :     return true;
   24505              : 
   24506              :   /* Otherwise, computing this conversion won't risk arbitrary
   24507              :      template instantiation.  */
   24508              :   return false;
   24509              : }
   24510              : 
   24511              : /* Subroutine of fn_type_unification.  PARM is a function parameter of a
   24512              :    template which doesn't contain any deducible template parameters; check if
   24513              :    ARG is a suitable match for it.  STRICT, FLAGS and EXPLAIN_P are as in
   24514              :    unify_one_argument.  */
   24515              : 
   24516              : static int
   24517     30458706 : check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
   24518              :                                 int flags, struct conversion **conv_p,
   24519              :                                 bool explain_p, bool noninst_only_p)
   24520              : {
   24521     30458706 :   tree type;
   24522              : 
   24523     30458706 :   if (!TYPE_P (arg))
   24524     29445356 :     type = TREE_TYPE (arg);
   24525              :   else
   24526              :     type = arg;
   24527              : 
   24528     30458706 :   if (same_type_p (parm, type))
   24529     27878225 :     return unify_success (explain_p);
   24530              : 
   24531     11562785 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   24532     11562785 :   if (strict == DEDUCE_CONV)
   24533              :     {
   24534           27 :       if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
   24535     27878225 :         return unify_success (explain_p);
   24536              :     }
   24537     11562758 :   else if (strict == DEDUCE_CALL)
   24538              :     {
   24539     11562758 :       if (conv_p && *conv_p)
   24540              :         {
   24541              :           /* This conversion was already computed earlier (when
   24542              :              computing only non-instantiating conversions).  */
   24543      3477784 :           gcc_checking_assert (!noninst_only_p);
   24544     27878225 :           return unify_success (explain_p);
   24545              :         }
   24546              : 
   24547      8084974 :       if (noninst_only_p
   24548      8084974 :           && conversion_may_instantiate_p (parm, type))
   24549     27878225 :         return unify_success (explain_p);
   24550              : 
   24551      6704372 :       bool ok = false;
   24552      6704372 :       tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
   24553      6704372 :       if (conv_p)
   24554              :         /* Avoid recalculating this in add_function_candidate.  */
   24555     13408264 :         ok = (*conv_p
   24556      6704132 :               = good_conversion (parm, type, conv_arg, flags, complain));
   24557              :       else
   24558          240 :         ok = can_convert_arg (parm, type, conv_arg, flags, complain);
   24559      6704372 :       if (ok)
   24560     27878225 :         return unify_success (explain_p);
   24561              :     }
   24562              : 
   24563      2580481 :   if (strict == DEDUCE_EXACT)
   24564            0 :     return unify_type_mismatch (explain_p, parm, arg);
   24565              :   else
   24566      2580481 :     return unify_arg_conversion (explain_p, parm, type, arg);
   24567              : }
   24568              : 
   24569              : static bool uses_deducible_template_parms (tree type);
   24570              : 
   24571              : /* Returns true iff the expression EXPR is one from which a template
   24572              :    argument can be deduced.  In other words, if it's an undecorated
   24573              :    use of a template non-type parameter.  */
   24574              : 
   24575              : static bool
   24576    910553738 : deducible_expression (tree expr)
   24577              : {
   24578              :   /* Strip implicit conversions and implicit INDIRECT_REFs.  */
   24579   1821109678 :   while (CONVERT_EXPR_P (expr)
   24580    910555901 :          || TREE_CODE (expr) == VIEW_CONVERT_EXPR
   24581    910554016 :          || TREE_CODE (expr) == IMPLICIT_CONV_EXPR
   24582   1821112179 :          || REFERENCE_REF_P (expr))
   24583         4639 :     expr = TREE_OPERAND (expr, 0);
   24584    910553738 :   return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
   24585              : }
   24586              : 
   24587              : /* Returns true iff the array domain DOMAIN uses a template parameter in a
   24588              :    deducible way; that is, if it has a max value of <PARM> - 1.  */
   24589              : 
   24590              : static bool
   24591       136355 : deducible_array_bound (tree domain)
   24592              : {
   24593       136355 :   if (domain == NULL_TREE)
   24594              :     return false;
   24595              : 
   24596       136286 :   tree max = TYPE_MAX_VALUE (domain);
   24597       136286 :   if (TREE_CODE (max) != MINUS_EXPR)
   24598              :     return false;
   24599              : 
   24600       134887 :   return deducible_expression (TREE_OPERAND (max, 0));
   24601              : }
   24602              : 
   24603              : /* Returns true iff the template arguments ARGS use a template parameter
   24604              :    in a deducible way.  */
   24605              : 
   24606              : static bool
   24607    413918231 : deducible_template_args (tree args)
   24608              : {
   24609    433698094 :   for (tree elt : tree_vec_range (args))
   24610              :     {
   24611    421622581 :       bool deducible;
   24612    421622581 :       if (ARGUMENT_PACK_P (elt))
   24613     20220406 :         deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
   24614              :       else
   24615              :         {
   24616    401402175 :           if (PACK_EXPANSION_P (elt))
   24617     19838156 :             elt = PACK_EXPANSION_PATTERN (elt);
   24618    401402175 :           if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
   24619              :             deducible = true;
   24620    401402121 :           else if (TYPE_P (elt))
   24621    399499884 :             deducible = uses_deducible_template_parms (elt);
   24622              :           else
   24623      1902237 :             deducible = deducible_expression (elt);
   24624              :         }
   24625    421622527 :       if (deducible)
   24626    401842718 :         return true;
   24627              :     }
   24628     12075513 :   return false;
   24629              : }
   24630              : 
   24631              : /* Returns true iff TYPE contains any deducible references to template
   24632              :    parameters, as per 14.8.2.5.  */
   24633              : 
   24634              : static bool
   24635    903612754 : uses_deducible_template_parms (tree type)
   24636              : {
   24637   1336741745 :   if (PACK_EXPANSION_P (type))
   24638           90 :     type = PACK_EXPANSION_PATTERN (type);
   24639              : 
   24640              :   /* T
   24641              :      cv-list T
   24642              :      TT<T>
   24643              :      TT<i>
   24644              :      TT<> */
   24645   1336741745 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   24646    879714587 :       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24647              :     return true;
   24648              : 
   24649              :   /* T*
   24650              :      T&
   24651              :      T&&  */
   24652    879713773 :   if (INDIRECT_TYPE_P (type))
   24653    433128991 :     return uses_deducible_template_parms (TREE_TYPE (type));
   24654              : 
   24655              :   /* T[integer-constant ]
   24656              :      type [i]  */
   24657    446584782 :   if (TREE_CODE (type) == ARRAY_TYPE)
   24658      1531652 :     return (uses_deducible_template_parms (TREE_TYPE (type))
   24659      1667509 :             || deducible_array_bound (TYPE_DOMAIN (type)));
   24660              : 
   24661              :   /* T type ::*
   24662              :      type T::*
   24663              :      T T::*
   24664              :      T (type ::*)()
   24665              :      type (T::*)()
   24666              :      type (type ::*)(T)
   24667              :      type (T::*)(T)
   24668              :      T (type ::*)(T)
   24669              :      T (T::*)()
   24670              :      T (T::*)(T) */
   24671    445053130 :   if (TYPE_PTRMEM_P (type))
   24672        22777 :     return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
   24673        22975 :             || (uses_deducible_template_parms
   24674          198 :                 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
   24675              : 
   24676              :   /* template-name <T> (where template-name refers to a class template)
   24677              :      template-name <i> (where template-name refers to a class template) */
   24678    399774253 :   if (CLASS_TYPE_P (type)
   24679    399774241 :       && CLASSTYPE_TEMPLATE_INFO (type)
   24680    838985328 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
   24681    393697825 :     return deducible_template_args (INNERMOST_TEMPLATE_ARGS
   24682    393697825 :                                     (CLASSTYPE_TI_ARGS (type)));
   24683              : 
   24684              :   /* type (T)
   24685              :      T()
   24686              :      T(T)  */
   24687     51332528 :   if (FUNC_OR_METHOD_TYPE_P (type))
   24688              :     {
   24689      7504129 :       if (uses_deducible_template_parms (TREE_TYPE (type)))
   24690              :         return true;
   24691        60247 :       tree parm = TYPE_ARG_TYPES (type);
   24692        60247 :       if (TREE_CODE (type) == METHOD_TYPE)
   24693          139 :         parm = TREE_CHAIN (parm);
   24694        63782 :       for (; parm; parm = TREE_CHAIN (parm))
   24695        61563 :         if (uses_deducible_template_parms (TREE_VALUE (parm)))
   24696              :           return true;
   24697         2219 :       if (flag_noexcept_type
   24698         2202 :           && TYPE_RAISES_EXCEPTIONS (type)
   24699           18 :           && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
   24700         2237 :           && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
   24701              :         return true;
   24702              :     }
   24703              : 
   24704              :   return false;
   24705              : }
   24706              : 
   24707              : /* Subroutine of type_unification_real and unify_pack_expansion to
   24708              :    handle unification of a single P/A pair.  Parameters are as
   24709              :    for those functions.  */
   24710              : 
   24711              : static int
   24712    537279076 : unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
   24713              :                     int subr, unification_kind_t strict,
   24714              :                     bool explain_p)
   24715              : {
   24716    537279076 :   tree arg_expr = NULL_TREE;
   24717    537279076 :   int arg_strict;
   24718              : 
   24719    537279076 :   if (arg == error_mark_node || parm == error_mark_node)
   24720           81 :     return unify_invalid (explain_p);
   24721    537279064 :   if (arg == unknown_type_node)
   24722              :     /* We can't deduce anything from this, but we might get all the
   24723              :        template args from other function args.  */
   24724     37810041 :     return unify_success (explain_p);
   24725              : 
   24726              :   /* Implicit conversions (Clause 4) will be performed on a function
   24727              :      argument to convert it to the type of the corresponding function
   24728              :      parameter if the parameter type contains no template-parameters that
   24729              :      participate in template argument deduction.  */
   24730    537279064 :   if (strict != DEDUCE_EXACT
   24731    537279064 :       && TYPE_P (parm) && !uses_deducible_template_parms (parm))
   24732              :     /* For function parameters with no deducible template parameters,
   24733              :        just return.  We'll check non-dependent conversions later.  */
   24734     37810041 :     return unify_success (explain_p);
   24735              : 
   24736    499562135 :   switch (strict)
   24737              :     {
   24738              :     case DEDUCE_CALL:
   24739              :       arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
   24740              :                     | UNIFY_ALLOW_MORE_CV_QUAL
   24741              :                     | UNIFY_ALLOW_DERIVED);
   24742              :       break;
   24743              : 
   24744              :     case DEDUCE_CONV:
   24745              :       arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
   24746              :       break;
   24747              : 
   24748              :     case DEDUCE_EXACT:
   24749              :       arg_strict = UNIFY_ALLOW_NONE;
   24750              :       break;
   24751              : 
   24752            0 :     default:
   24753            0 :       gcc_unreachable ();
   24754              :     }
   24755              : 
   24756              :   /* We only do these transformations if this is the top-level
   24757              :      parameter_type_list in a call or declaration matching; in other
   24758              :      situations (nested function declarators, template argument lists) we
   24759              :      won't be comparing a type to an expression, and we don't do any type
   24760              :      adjustments.  */
   24761    499562135 :   if (!subr)
   24762              :     {
   24763    491388454 :       if (!TYPE_P (arg))
   24764              :         {
   24765    456973207 :           gcc_assert (TREE_TYPE (arg) != NULL_TREE);
   24766    456973207 :           if (type_unknown_p (arg))
   24767              :             {
   24768              :               /* [temp.deduct.type] A template-argument can be
   24769              :                  deduced from a pointer to function or pointer
   24770              :                  to member function argument if the set of
   24771              :                  overloaded functions does not contain function
   24772              :                  templates and at most one of a set of
   24773              :                  overloaded functions provides a unique
   24774              :                  match.  */
   24775        93112 :               resolve_overloaded_unification (tparms, targs, parm,
   24776              :                                               arg, strict,
   24777              :                                               arg_strict, explain_p);
   24778              :               /* If a unique match was not found, this is a
   24779              :                  non-deduced context, so we still succeed. */
   24780        93112 :               return unify_success (explain_p);
   24781              :             }
   24782              : 
   24783    456880095 :           arg_expr = arg;
   24784    456880095 :           arg = unlowered_expr_type (arg);
   24785    456880095 :           if (arg == error_mark_node)
   24786           81 :             return unify_invalid (explain_p);
   24787              :         }
   24788              : 
   24789    491295342 :       arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   24790              :                                                       &parm, &arg, arg_expr);
   24791              :     }
   24792              :   else
   24793     16347362 :     if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
   24794      8173681 :         != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
   24795           69 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   24796              : 
   24797              :   /* For deduction from an init-list we need the actual list.  */
   24798    491295342 :   if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
   24799         8323 :     arg = arg_expr;
   24800    499468954 :   return unify (tparms, targs, parm, arg, arg_strict, explain_p);
   24801              : }
   24802              : 
   24803              : /* for_each_template_parm callback that always returns 0.  */
   24804              : 
   24805              : static int
   24806     26776192 : zero_r (tree, void *)
   24807              : {
   24808     26776192 :   return 0;
   24809              : }
   24810              : 
   24811              : /* for_each_template_parm any_fn callback to handle deduction of a template
   24812              :    type argument from the type of an array bound.  */
   24813              : 
   24814              : static int
   24815    165950504 : array_deduction_r (tree t, void *data)
   24816              : {
   24817    165950504 :   tree_pair_p d = (tree_pair_p)data;
   24818    165950504 :   tree &tparms = d->purpose;
   24819    165950504 :   tree &targs = d->value;
   24820              : 
   24821    165950504 :   if (TREE_CODE (t) == ARRAY_TYPE)
   24822          256 :     if (tree dom = TYPE_DOMAIN (t))
   24823          144 :       if (tree max = TYPE_MAX_VALUE (dom))
   24824              :         {
   24825          144 :           if (TREE_CODE (max) == MINUS_EXPR)
   24826           14 :             max = TREE_OPERAND (max, 0);
   24827          144 :           if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
   24828           14 :             unify (tparms, targs, TREE_TYPE (max), size_type_node,
   24829              :                    UNIFY_ALLOW_NONE, /*explain*/false);
   24830              :         }
   24831              : 
   24832              :   /* Keep walking.  */
   24833    165950504 :   return 0;
   24834              : }
   24835              : 
   24836              : /* Try to deduce any not-yet-deduced template type arguments from the type of
   24837              :    an array bound.  This is handled separately from unify because 14.8.2.5 says
   24838              :    "The type of a type parameter is only deduced from an array bound if it is
   24839              :    not otherwise deduced."  */
   24840              : 
   24841              : static void
   24842     28397293 : try_array_deduction (tree tparms, tree targs, tree parm)
   24843              : {
   24844     28397293 :   tree_pair_s data = { tparms, targs };
   24845     28397293 :   hash_set<tree> visited;
   24846     28397293 :   for_each_template_parm (parm, zero_r, &data, &visited,
   24847              :                           /*nondeduced*/false, array_deduction_r);
   24848     28397293 : }
   24849              : 
   24850              : /* Most parms like fn_type_unification.
   24851              : 
   24852              :    If SUBR is 1, we're being called recursively (to unify the
   24853              :    arguments of a function or method parameter of a function
   24854              :    template).
   24855              : 
   24856              :    CHECKS is a pointer to a vector of access checks encountered while
   24857              :    substituting default template arguments.  */
   24858              : 
   24859              : static int
   24860    460274193 : type_unification_real (tree tparms,
   24861              :                        tree full_targs,
   24862              :                        tree xparms,
   24863              :                        const tree *xargs,
   24864              :                        unsigned int xnargs,
   24865              :                        int subr,
   24866              :                        unification_kind_t strict,
   24867              :                        vec<deferred_access_check, va_gc> **checks,
   24868              :                        bool explain_p)
   24869              : {
   24870    460274193 :   tree parm, arg;
   24871    460274193 :   int i;
   24872    460274193 :   int ntparms = TREE_VEC_LENGTH (tparms);
   24873    460274193 :   int saw_undeduced = 0;
   24874    460274193 :   tree parms;
   24875    460274193 :   const tree *args;
   24876    460274193 :   unsigned int nargs;
   24877    460274193 :   unsigned int ia;
   24878              : 
   24879    460274193 :   gcc_assert (TREE_CODE (tparms) == TREE_VEC);
   24880    460274193 :   gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
   24881    460274193 :   gcc_assert (ntparms > 0);
   24882              : 
   24883    460274193 :   tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
   24884              : 
   24885              :   /* Reset the number of non-defaulted template arguments contained
   24886              :      in TARGS.  */
   24887    460274193 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
   24888              : 
   24889    460288040 :  again:
   24890    460288040 :   parms = xparms;
   24891    460288040 :   args = xargs;
   24892    460288040 :   nargs = xnargs;
   24893              : 
   24894              :   /* Only fn_type_unification cares about terminal void.  */
   24895    460288040 :   if (nargs && args[nargs-1] == void_type_node)
   24896     13669661 :     --nargs;
   24897              : 
   24898    460288040 :   ia = 0;
   24899   1059173462 :   while (parms && parms != void_list_node
   24900   1139295377 :          && ia < nargs)
   24901              :     {
   24902    529525990 :       parm = TREE_VALUE (parms);
   24903              : 
   24904    529525990 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   24905    529525990 :           && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
   24906              :         /* For a function parameter pack that occurs at the end of the
   24907              :            parameter-declaration-list, the type A of each remaining
   24908              :            argument of the call is compared with the type P of the
   24909              :            declarator-id of the function parameter pack.  */
   24910              :         break;
   24911              : 
   24912    528666204 :       parms = TREE_CHAIN (parms);
   24913              : 
   24914    528666204 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   24915              :         /* For a function parameter pack that does not occur at the
   24916              :            end of the parameter-declaration-list, the type of the
   24917              :            parameter pack is a non-deduced context.  */
   24918           54 :         continue;
   24919              : 
   24920              :       /* [temp.deduct.conv] only applies to the deduction of the return
   24921              :          type, which is always the first argument here.  Other arguments
   24922              :          (notably, explicit object parameters) should undergo normal
   24923              :          call-like unification.  */
   24924    528666150 :       unification_kind_t kind = strict;
   24925    528666150 :       if (strict == DEDUCE_CONV && ia > 0)
   24926           24 :         kind = DEDUCE_CALL;
   24927              : 
   24928    528666150 :       arg = args[ia];
   24929    528666150 :       ++ia;
   24930              : 
   24931    528666150 :       if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind,
   24932              :                               explain_p))
   24933              :         return 1;
   24934              :     }
   24935              : 
   24936     78033045 :   if (parms
   24937     70219218 :       && parms != void_list_node
   24938     81962969 :       && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
   24939              :     {
   24940      1435763 :       gcc_assert (strict != DEDUCE_CONV);
   24941              : 
   24942              :       /* Unify the remaining arguments with the pack expansion type.  */
   24943      1435763 :       tree argvec;
   24944      1435763 :       tree parmvec = make_tree_vec (1);
   24945              : 
   24946              :       /* Allocate a TREE_VEC and copy in all of the arguments */
   24947      1435763 :       argvec = make_tree_vec (nargs - ia);
   24948      3967096 :       for (i = 0; ia < nargs; ++ia, ++i)
   24949      1095570 :         TREE_VEC_ELT (argvec, i) = args[ia];
   24950              : 
   24951              :       /* Copy the parameter into parmvec.  */
   24952      1435763 :       TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
   24953      1435763 :       if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
   24954              :                                 /*subr=*/subr, explain_p))
   24955              :         return 1;
   24956              : 
   24957              :       /* Advance to the end of the list of parameters.  */
   24958      1435614 :       parms = TREE_CHAIN (parms);
   24959              :     }
   24960              : 
   24961              :   /* Fail if we've reached the end of the parm list, and more args
   24962              :      are present, and the parm list isn't variadic.  */
   24963     78032896 :   if (ia < nargs && parms == void_list_node)
   24964        48214 :     return unify_too_many_arguments (explain_p, nargs, ia);
   24965              :   /* Fail if parms are left and they don't have default values and
   24966              :      they aren't all deduced as empty packs (c++/57397).  This is
   24967              :      consistent with sufficient_parms_p.  */
   24968     70170737 :   if (parms && parms != void_list_node
   24969     80478852 :       && TREE_PURPOSE (parms) == NULL_TREE)
   24970              :     {
   24971              :       unsigned int count = nargs;
   24972              :       tree p = parms;
   24973        24326 :       bool type_pack_p;
   24974        24326 :       do
   24975              :         {
   24976        24326 :           type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
   24977        24326 :           if (!type_pack_p)
   24978        24318 :             count++;
   24979        24326 :           p = TREE_CHAIN (p);
   24980              :         }
   24981        24326 :       while (p && p != void_list_node);
   24982        24243 :       if (count != nargs)
   24983        24237 :         return unify_too_few_arguments (explain_p, ia, count,
   24984        24237 :                                         type_pack_p);
   24985              :     }
   24986              : 
   24987     77960445 :   if (!subr)
   24988              :     {
   24989     77689587 :       tsubst_flags_t complain = (explain_p
   24990     77690866 :                                  ? tf_warning_or_error
   24991              :                                  : tf_none);
   24992     77690866 :       bool tried_array_deduction = (cxx_dialect < cxx17);
   24993              : 
   24994    201708838 :       for (i = 0; i < ntparms; i++)
   24995              :         {
   24996    124180839 :           tree targ = TREE_VEC_ELT (targs, i);
   24997    124180839 :           tree tparm = TREE_VEC_ELT (tparms, i);
   24998              : 
   24999              :           /* Clear the "incomplete" flags on all argument packs now so that
   25000              :              substituting them into later default arguments works.  */
   25001    124180839 :           if (targ && ARGUMENT_PACK_P (targ))
   25002              :             {
   25003      8384548 :               ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
   25004      8384548 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
   25005              :             }
   25006              : 
   25007     39433052 :           if (targ || tparm == error_mark_node)
   25008     93132335 :             continue;
   25009     31048504 :           tparm = TREE_VALUE (tparm);
   25010              : 
   25011     31048504 :           if (TREE_CODE (tparm) == TYPE_DECL
   25012     28836604 :               && !tried_array_deduction)
   25013              :             {
   25014     28397240 :               try_array_deduction (tparms, targs, xparms);
   25015     28397240 :               tried_array_deduction = true;
   25016     28397240 :               if (TREE_VEC_ELT (targs, i))
   25017            3 :                 continue;
   25018              :             }
   25019              : 
   25020              :           /* If this is an undeduced nontype parameter that depends on
   25021              :              a type parameter, try another pass; its type may have been
   25022              :              deduced from a later argument than the one from which
   25023              :              this parameter can be deduced.  */
   25024     31086818 :           if (TREE_CODE (tparm) == PARM_DECL
   25025      2211865 :               && !is_auto (TREE_TYPE (tparm))
   25026      2211638 :               && uses_template_parms (TREE_TYPE (tparm))
   25027     31110531 :               && saw_undeduced < 2)
   25028              :             {
   25029        38317 :               saw_undeduced = 1;
   25030        38317 :               continue;
   25031              :             }
   25032              : 
   25033              :           /* Core issue #226 (C++0x) [temp.deduct]:
   25034              : 
   25035              :              If a template argument has not been deduced, its
   25036              :              default template argument, if any, is used.
   25037              : 
   25038              :              When we are in C++98 mode, TREE_PURPOSE will either
   25039              :              be NULL_TREE or ERROR_MARK_NODE, so we do not need
   25040              :              to explicitly check cxx_dialect here.  */
   25041     31010184 :           if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
   25042              :             /* OK, there is a default argument.  Wait until after the
   25043              :                conversion check to do substitution.  */
   25044     30754898 :             continue;
   25045              : 
   25046              :           /* If the type parameter is a parameter pack, then it will
   25047              :              be deduced to an empty parameter pack.  */
   25048       255286 :           if (template_parameter_pack_p (tparm))
   25049              :             {
   25050        92419 :               tree arg;
   25051              : 
   25052        92419 :               if (TREE_CODE (tparm) == PARM_DECL)
   25053              :                 {
   25054         1119 :                   arg = make_node (NONTYPE_ARGUMENT_PACK);
   25055         1119 :                   TREE_CONSTANT (arg) = 1;
   25056              :                 }
   25057              :               else
   25058        91300 :                 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   25059              : 
   25060        92419 :               ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   25061              : 
   25062        92419 :               TREE_VEC_ELT (targs, i) = arg;
   25063        92419 :               continue;
   25064        92419 :             }
   25065              : 
   25066       162867 :           return unify_parameter_deduction_failure (explain_p, tparm);
   25067              :         }
   25068              : 
   25069              :       /* During partial ordering, we deduce dependent template args.  */
   25070              :       bool any_dependent_targs = false;
   25071              : 
   25072              :       /* Now substitute into the default template arguments.  */
   25073    183733100 :       for (i = 0; i < ntparms; i++)
   25074              :         {
   25075    123617391 :           tree targ = TREE_VEC_ELT (targs, i);
   25076    123617391 :           tree tparm = TREE_VEC_ELT (tparms, i);
   25077              : 
   25078    123617391 :           if (targ)
   25079              :             {
   25080     93118246 :               if (!any_dependent_targs && dependent_template_arg_p (targ))
   25081              :                 any_dependent_targs = true;
   25082     93118246 :               continue;
   25083              :             }
   25084     30499145 :           if (tparm == error_mark_node)
   25085            0 :             continue;
   25086              : 
   25087     30499145 :           tree parm = TREE_VALUE (tparm);
   25088     30499145 :           tree arg = TREE_PURPOSE (tparm);
   25089     30499145 :           reopen_deferring_access_checks (*checks);
   25090     30499145 :           location_t save_loc = input_location;
   25091     30499145 :           if (DECL_P (parm))
   25092     30499141 :             input_location = DECL_SOURCE_LOCATION (parm);
   25093              : 
   25094     30499145 :           if (saw_undeduced == 1
   25095        43328 :               && TREE_CODE (parm) == PARM_DECL
   25096        38867 :               && !is_auto (TREE_TYPE (parm))
   25097     30538012 :               && uses_template_parms (TREE_TYPE (parm)))
   25098              :             {
   25099              :               /* The type of this non-type parameter depends on undeduced
   25100              :                  parameters.  Don't try to use its default argument yet,
   25101              :                  since we might deduce an argument for it on the next pass,
   25102              :                  but do check whether the arguments we already have cause
   25103              :                  substitution failure, so that that happens before we try
   25104              :                  later default arguments (78489).  */
   25105        38252 :               ++processing_template_decl;
   25106        38252 :               tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
   25107              :                                   NULL_TREE);
   25108        38252 :               --processing_template_decl;
   25109        38252 :               if (type == error_mark_node)
   25110        14513 :                 arg = error_mark_node;
   25111              :               else
   25112              :                 arg = NULL_TREE;
   25113              :             }
   25114              :           else
   25115              :             {
   25116              :               /* Even if the call is happening in template context, getting
   25117              :                  here means it's non-dependent, and a default argument is
   25118              :                  considered a separate definition under [temp.decls], so we can
   25119              :                  do this substitution without processing_template_decl.  This
   25120              :                  is important if the default argument contains something that
   25121              :                  might be instantiation-dependent like access (87480).  */
   25122     30460893 :               processing_template_decl_sentinel s (!any_dependent_targs);
   25123              : 
   25124     30460893 :               tree used_tparms = NULL_TREE;
   25125     30460893 :               if (saw_undeduced == 1)
   25126              :                 {
   25127         5076 :                   tree tparms_list = build_tree_list (size_int (1), tparms);
   25128         5076 :                   used_tparms = find_template_parameters (arg, tparms_list);
   25129        12930 :                   for (; used_tparms; used_tparms = TREE_CHAIN (used_tparms))
   25130              :                     {
   25131         2784 :                       int level, index;
   25132         2784 :                       template_parm_level_and_index (TREE_VALUE (used_tparms),
   25133              :                                                      &level, &index);
   25134         2784 :                       if (TREE_VEC_ELT (targs, index) == NULL_TREE)
   25135              :                         break;
   25136              :                     }
   25137              :                 }
   25138              : 
   25139         5076 :               if (!used_tparms)
   25140              :                 {
   25141              :                   /* All template parameters within this default argument are
   25142              :                      deduced, so we can use it.  */
   25143     30460887 :                   arg = tsubst_template_arg (arg, full_targs, complain,
   25144              :                                              NULL_TREE);
   25145     30460887 :                   arg = convert_template_argument (parm, arg, full_targs,
   25146              :                                                    complain, i, NULL_TREE);
   25147              :                 }
   25148              :               else if (saw_undeduced == 1)
   25149              :                 arg = NULL_TREE;
   25150              :               else if (!any_dependent_targs)
   25151              :                 arg = error_mark_node;
   25152     30460893 :             }
   25153              : 
   25154     30499145 :           input_location = save_loc;
   25155     30499145 :           *checks = get_deferred_access_checks ();
   25156     30499145 :           pop_deferring_access_checks ();
   25157              : 
   25158     30499145 :           if (arg == error_mark_node)
   25159              :             return 1;
   25160     13086855 :           else if (arg)
   25161              :             {
   25162     13063110 :               TREE_VEC_ELT (targs, i) = arg;
   25163              :               /* The position of the first default template argument,
   25164              :                  is also the number of non-defaulted arguments in TARGS.
   25165              :                  Record that.  */
   25166     13063110 :               if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   25167     10762310 :                 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
   25168              :             }
   25169              :         }
   25170              : 
   25171     60115709 :       if (saw_undeduced++ == 1)
   25172        13847 :         goto again;
   25173              :     }
   25174              : 
   25175     60371441 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   25176     49387099 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
   25177              : 
   25178     60371441 :   return unify_success (explain_p);
   25179              : }
   25180              : 
   25181              : /* Subroutine of type_unification_real.  Args are like the variables
   25182              :    at the call site.  ARG is an overloaded function (or template-id);
   25183              :    we try deducing template args from each of the overloads, and if
   25184              :    only one succeeds, we go with that.  Modifies TARGS and returns
   25185              :    true on success.  */
   25186              : 
   25187              : static bool
   25188        93112 : resolve_overloaded_unification (tree tparms,
   25189              :                                 tree targs,
   25190              :                                 tree parm,
   25191              :                                 tree arg,
   25192              :                                 unification_kind_t strict,
   25193              :                                 int sub_strict,
   25194              :                                 bool explain_p)
   25195              : {
   25196        93112 :   tree tempargs = copy_node (targs);
   25197        93112 :   int good = 0;
   25198        93112 :   tree goodfn = NULL_TREE;
   25199        93112 :   bool addr_p;
   25200              : 
   25201        93112 :   if (TREE_CODE (arg) == ADDR_EXPR)
   25202              :     {
   25203          680 :       arg = TREE_OPERAND (arg, 0);
   25204          680 :       addr_p = true;
   25205              :     }
   25206              :   else
   25207              :     addr_p = false;
   25208              : 
   25209        93112 :   if (TREE_CODE (arg) == COMPONENT_REF)
   25210              :     /* Handle `&x' where `x' is some static or non-static member
   25211              :        function name.  */
   25212          159 :     arg = TREE_OPERAND (arg, 1);
   25213              : 
   25214        93112 :   if (TREE_CODE (arg) == OFFSET_REF)
   25215          448 :     arg = TREE_OPERAND (arg, 1);
   25216              : 
   25217              :   /* Strip baselink information.  */
   25218        93112 :   if (BASELINK_P (arg))
   25219          613 :     arg = BASELINK_FUNCTIONS (arg);
   25220              : 
   25221        93112 :   if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
   25222              :     {
   25223              :       /* If we got some explicit template args, we need to plug them into
   25224              :          the affected templates before we try to unify, in case the
   25225              :          explicit args will completely resolve the templates in question.  */
   25226              : 
   25227          453 :       int ok = 0;
   25228          453 :       tree expl_subargs = TREE_OPERAND (arg, 1);
   25229          453 :       arg = TREE_OPERAND (arg, 0);
   25230              : 
   25231          963 :       for (lkp_iterator iter (arg); iter; ++iter)
   25232              :         {
   25233          510 :           tree fn = *iter;
   25234          510 :           tree subargs, elem;
   25235              : 
   25236          510 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   25237            0 :             continue;
   25238              : 
   25239          510 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   25240              :                                            expl_subargs, NULL_TREE, tf_none);
   25241          510 :           if (subargs != error_mark_node
   25242          510 :               && !any_dependent_template_arguments_p (subargs))
   25243              :             {
   25244          486 :               fn = instantiate_template (fn, subargs, tf_none);
   25245          486 :               if (!constraints_satisfied_p (fn))
   25246           41 :                 continue;
   25247          445 :               if (undeduced_auto_decl (fn))
   25248              :                 {
   25249              :                   /* Instantiate the function to deduce its return type.  */
   25250           12 :                   ++function_depth;
   25251           12 :                   instantiate_decl (fn, /*defer*/false, /*class*/false);
   25252           12 :                   --function_depth;
   25253              :                 }
   25254              : 
   25255          445 :               if (flag_noexcept_type)
   25256          401 :                 maybe_instantiate_noexcept (fn, tf_none);
   25257              : 
   25258          445 :               elem = TREE_TYPE (fn);
   25259          445 :               if (try_one_overload (tparms, targs, tempargs, parm,
   25260              :                                     elem, strict, sub_strict, addr_p, explain_p)
   25261          445 :                   && (!goodfn || !same_type_p (goodfn, elem)))
   25262              :                 {
   25263          418 :                   goodfn = elem;
   25264          418 :                   ++good;
   25265              :                 }
   25266              :             }
   25267           24 :           else if (subargs)
   25268           24 :             ++ok;
   25269              :         }
   25270              :       /* If no templates (or more than one) are fully resolved by the
   25271              :          explicit arguments, this template-id is a non-deduced context; it
   25272              :          could still be OK if we deduce all template arguments for the
   25273              :          enclosing call through other arguments.  */
   25274          453 :       if (good != 1)
   25275              :         good = ok;
   25276              :     }
   25277        92659 :   else if (!OVL_P (arg))
   25278              :     /* If ARG is, for example, "(0, &f)" then its type will be unknown
   25279              :        -- but the deduction does not succeed because the expression is
   25280              :        not just the function on its own.  */
   25281              :     return false;
   25282              :   else
   25283       185740 :     for (lkp_iterator iter (arg); iter; ++iter)
   25284              :       {
   25285        93081 :         tree fn = *iter;
   25286        93081 :         if (flag_noexcept_type)
   25287        92250 :           maybe_instantiate_noexcept (fn, tf_none);
   25288        93081 :         if (TREE_CODE (fn) == FUNCTION_DECL && !constraints_satisfied_p (fn))
   25289            6 :           continue;
   25290        93075 :         tree elem = TREE_TYPE (fn);
   25291        93075 :         if (try_one_overload (tparms, targs, tempargs, parm, elem,
   25292              :                               strict, sub_strict, addr_p, explain_p)
   25293        93075 :             && (!goodfn || !same_type_p (goodfn, elem)))
   25294              :           {
   25295        92831 :             goodfn = elem;
   25296        92831 :             ++good;
   25297              :           }
   25298              :       }
   25299              : 
   25300              :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   25301              :      to function or pointer to member function argument if the set of
   25302              :      overloaded functions does not contain function templates and at most
   25303              :      one of a set of overloaded functions provides a unique match.
   25304              : 
   25305              :      CWG2918 allows multiple functions to match if they all have the same type,
   25306              :      so that we can choose the most constrained later.
   25307              : 
   25308              :      So if we found multiple possibilities, we return success but don't
   25309              :      deduce anything.  */
   25310              : 
   25311        92730 :   if (good == 1)
   25312              :     {
   25313        92845 :       int i = TREE_VEC_LENGTH (targs);
   25314       353593 :       for (; i--; )
   25315       260748 :         if (TREE_VEC_ELT (tempargs, i))
   25316              :           {
   25317       147552 :             tree old = TREE_VEC_ELT (targs, i);
   25318       147552 :             tree new_ = TREE_VEC_ELT (tempargs, i);
   25319       146876 :             if (new_ && old && ARGUMENT_PACK_P (old)
   25320       147561 :                 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
   25321              :               /* Don't forget explicit template arguments in a pack.  */
   25322            6 :               ARGUMENT_PACK_EXPLICIT_ARGS (new_)
   25323           12 :                 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
   25324       147552 :             TREE_VEC_ELT (targs, i) = new_;
   25325              :           }
   25326              :     }
   25327        93112 :   if (good)
   25328              :     return true;
   25329              : 
   25330              :   return false;
   25331              : }
   25332              : 
   25333              : /* Core DR 115: In contexts where deduction is done and fails, or in
   25334              :    contexts where deduction is not done, if a template argument list is
   25335              :    specified and it, along with any default template arguments, identifies
   25336              :    a single function template specialization, then the template-id is an
   25337              :    lvalue for the function template specialization.  */
   25338              : 
   25339              : tree
   25340   1586458579 : resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
   25341              : {
   25342   1586458579 :   tree expr, offset, baselink;
   25343   1586458579 :   bool addr;
   25344              : 
   25345   1586458579 :   if (!type_unknown_p (orig_expr))
   25346              :     return orig_expr;
   25347              : 
   25348        10264 :   expr = orig_expr;
   25349        10264 :   addr = false;
   25350        10264 :   offset = NULL_TREE;
   25351        10264 :   baselink = NULL_TREE;
   25352              : 
   25353        10264 :   if (TREE_CODE (expr) == ADDR_EXPR)
   25354              :     {
   25355          445 :       expr = TREE_OPERAND (expr, 0);
   25356          445 :       addr = true;
   25357              :     }
   25358        10264 :   if (TREE_CODE (expr) == OFFSET_REF)
   25359              :     {
   25360          108 :       offset = expr;
   25361          108 :       expr = TREE_OPERAND (expr, 1);
   25362              :     }
   25363        10264 :   if (BASELINK_P (expr))
   25364              :     {
   25365          289 :       baselink = expr;
   25366          289 :       expr = BASELINK_FUNCTIONS (expr);
   25367              :     }
   25368              : 
   25369        10264 :   if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
   25370              :     {
   25371         1356 :       int good = 0;
   25372         1356 :       tree goodfn = NULL_TREE;
   25373              : 
   25374              :       /* If we got some explicit template args, we need to plug them into
   25375              :          the affected templates before we try to unify, in case the
   25376              :          explicit args will completely resolve the templates in question.  */
   25377              : 
   25378         1356 :       tree expl_subargs = TREE_OPERAND (expr, 1);
   25379         1356 :       tree arg = TREE_OPERAND (expr, 0);
   25380         1356 :       tree badfn = NULL_TREE;
   25381         1356 :       tree badargs = NULL_TREE;
   25382              : 
   25383         2832 :       for (lkp_iterator iter (arg); iter; ++iter)
   25384              :         {
   25385         1476 :           tree fn = *iter;
   25386         1476 :           tree subargs, elem;
   25387              : 
   25388         1476 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   25389            4 :             continue;
   25390              : 
   25391         1472 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   25392              :                                            expl_subargs, NULL_TREE, tf_none);
   25393         1472 :           if (subargs != error_mark_node
   25394         1472 :               && !any_dependent_template_arguments_p (subargs))
   25395              :             {
   25396         1354 :               elem = instantiate_template (fn, subargs, tf_none);
   25397         1354 :               if (elem == error_mark_node)
   25398              :                 {
   25399              :                   badfn = fn;
   25400              :                   badargs = subargs;
   25401              :                 }
   25402         1315 :               else if (elem && (!goodfn || !decls_match (goodfn, elem))
   25403         2630 :                        && constraints_satisfied_p (elem))
   25404              :                 {
   25405         1270 :                   goodfn = elem;
   25406         1270 :                   ++good;
   25407              :                 }
   25408              :             }
   25409              :         }
   25410         1356 :       if (good == 1)
   25411              :         {
   25412         1186 :           expr = goodfn;
   25413         1186 :           if (baselink)
   25414          209 :             expr = build_baselink (BASELINK_BINFO (baselink),
   25415          209 :                                    BASELINK_ACCESS_BINFO (baselink),
   25416          209 :                                    expr, BASELINK_OPTYPE (baselink));
   25417         1186 :           if (offset)
   25418              :             {
   25419           57 :               tree base
   25420           57 :                 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
   25421           57 :               expr = build_offset_ref (base, expr, addr, complain);
   25422              :             }
   25423         1186 :           if (addr)
   25424          313 :             expr = cp_build_addr_expr (expr, complain);
   25425         1186 :           return expr;
   25426              :         }
   25427          170 :       else if (good == 0 && badargs && (complain & tf_error))
   25428              :         /* There were no good options and at least one bad one, so let the
   25429              :            user know what the problem is.  */
   25430            3 :         instantiate_template (badfn, badargs, complain);
   25431              :     }
   25432              :   return orig_expr;
   25433              : }
   25434              : 
   25435              : /* As above, but error out if the expression remains overloaded.  */
   25436              : 
   25437              : tree
   25438   1145896526 : resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
   25439              : {
   25440   1145896526 :   exp = resolve_nondeduced_context (exp, complain);
   25441   1145896526 :   if (type_unknown_p (exp))
   25442              :     {
   25443          108 :       if (complain & tf_error)
   25444           93 :         cxx_incomplete_type_error (exp, TREE_TYPE (exp));
   25445          108 :       return error_mark_node;
   25446              :     }
   25447              :   return exp;
   25448              : }
   25449              : 
   25450              : /* Subroutine of resolve_overloaded_unification; does deduction for a single
   25451              :    overload.  Fills TARGS with any deduced arguments, or error_mark_node if
   25452              :    different overloads deduce different arguments for a given parm.
   25453              :    ADDR_P is true if the expression for which deduction is being
   25454              :    performed was of the form "& fn" rather than simply "fn".
   25455              : 
   25456              :    Returns 1 on success.  */
   25457              : 
   25458              : static int
   25459        93520 : try_one_overload (tree tparms,
   25460              :                   tree orig_targs,
   25461              :                   tree targs,
   25462              :                   tree parm,
   25463              :                   tree arg,
   25464              :                   unification_kind_t strict,
   25465              :                   int sub_strict,
   25466              :                   bool addr_p,
   25467              :                   bool explain_p)
   25468              : {
   25469        93520 :   int nargs;
   25470        93520 :   tree tempargs;
   25471        93520 :   int i;
   25472              : 
   25473        93520 :   if (arg == error_mark_node)
   25474              :     return 0;
   25475              : 
   25476              :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   25477              :      to function or pointer to member function argument if the set of
   25478              :      overloaded functions does not contain function templates and at most
   25479              :      one of a set of overloaded functions provides a unique match.
   25480              : 
   25481              :      So if this is a template, just return success.  */
   25482              : 
   25483        93519 :   if (uses_template_parms (arg))
   25484              :     return 1;
   25485              : 
   25486         1245 :   if (TREE_CODE (arg) == METHOD_TYPE)
   25487          378 :     arg = build_ptrmemfunc_type (build_pointer_type (arg));
   25488          867 :   else if (addr_p)
   25489          639 :     arg = build_pointer_type (arg);
   25490              : 
   25491         1245 :   sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   25492              :                                                   &parm, &arg, NULL_TREE);
   25493              : 
   25494              :   /* We don't copy orig_targs for this because if we have already deduced
   25495              :      some template args from previous args, unify would complain when we
   25496              :      try to deduce a template parameter for the same argument, even though
   25497              :      there isn't really a conflict.  */
   25498         1245 :   nargs = TREE_VEC_LENGTH (targs);
   25499         1245 :   tempargs = make_tree_vec (nargs);
   25500              : 
   25501         1245 :   if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
   25502              :     return 0;
   25503              : 
   25504              :   /* First make sure we didn't deduce anything that conflicts with
   25505              :      explicitly specified args.  */
   25506         2772 :   for (i = nargs; i--; )
   25507              :     {
   25508         1779 :       tree elt = TREE_VEC_ELT (tempargs, i);
   25509         1779 :       tree oldelt = TREE_VEC_ELT (orig_targs, i);
   25510              : 
   25511         1779 :       if (!elt)
   25512              :         /*NOP*/;
   25513         1157 :       else if (uses_template_parms (elt))
   25514              :         /* Since we're unifying against ourselves, we will fill in
   25515              :            template args used in the function parm list with our own
   25516              :            template parms.  Discard them.  */
   25517            0 :         TREE_VEC_ELT (tempargs, i) = NULL_TREE;
   25518         1157 :       else if (oldelt && ARGUMENT_PACK_P (oldelt))
   25519              :         {
   25520              :           /* Check that the argument at each index of the deduced argument pack
   25521              :              is equivalent to the corresponding explicitly specified argument.
   25522              :              We may have deduced more arguments than were explicitly specified,
   25523              :              and that's OK.  */
   25524              : 
   25525              :           /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
   25526              :              that's wrong if we deduce the same argument pack from multiple
   25527              :              function arguments: it's only incomplete the first time.  */
   25528              : 
   25529           24 :           tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
   25530           24 :           tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
   25531              : 
   25532           24 :           if (TREE_VEC_LENGTH (deduced_pack)
   25533           24 :               < TREE_VEC_LENGTH (explicit_pack))
   25534              :             return 0;
   25535              : 
   25536           30 :           for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
   25537           21 :             if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
   25538           21 :                                       TREE_VEC_ELT (deduced_pack, j)))
   25539              :               return 0;
   25540              :         }
   25541           23 :       else if (oldelt && !template_args_equal (oldelt, elt))
   25542              :         return 0;
   25543              :     }
   25544              : 
   25545         2719 :   for (i = nargs; i--; )
   25546              :     {
   25547         1726 :       tree elt = TREE_VEC_ELT (tempargs, i);
   25548              : 
   25549         1726 :       if (elt)
   25550         1122 :         TREE_VEC_ELT (targs, i) = elt;
   25551              :     }
   25552              : 
   25553              :   return 1;
   25554              : }
   25555              : 
   25556              : /* PARM is a template class (perhaps with unbound template
   25557              :    parameters).  ARG is a fully instantiated type.  If ARG can be
   25558              :    bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
   25559              :    TARGS are as for unify.  */
   25560              : 
   25561              : static tree
   25562    226899658 : try_class_unification (tree tparms, tree targs, tree parm, tree arg,
   25563              :                        bool explain_p)
   25564              : {
   25565    226899658 :   if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   25566              :     return NULL_TREE;
   25567    165758833 :   else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25568              :     /* Matches anything.  */;
   25569    165758065 :   else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
   25570              :     return NULL_TREE;
   25571              : 
   25572              :   /* We need to make a new template argument vector for the call to
   25573              :      unify.  If we used TARGS, we'd clutter it up with the result of
   25574              :      the attempted unification, even if this class didn't work out.
   25575              :      We also don't want to commit ourselves to all the unifications
   25576              :      we've already done, since unification is supposed to be done on
   25577              :      an argument-by-argument basis.  In other words, consider the
   25578              :      following pathological case:
   25579              : 
   25580              :        template <int I, int J, int K>
   25581              :        struct S {};
   25582              : 
   25583              :        template <int I, int J>
   25584              :        struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
   25585              : 
   25586              :        template <int I, int J, int K>
   25587              :        void f(S<I, J, K>, S<I, I, I>);
   25588              : 
   25589              :        void g() {
   25590              :          S<0, 0, 0> s0;
   25591              :          S<0, 1, 2> s2;
   25592              : 
   25593              :          f(s0, s2);
   25594              :        }
   25595              : 
   25596              :      Now, by the time we consider the unification involving `s2', we
   25597              :      already know that we must have `f<0, 0, 0>'.  But, even though
   25598              :      `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
   25599              :      because there are two ways to unify base classes of S<0, 1, 2>
   25600              :      with S<I, I, I>.  If we kept the already deduced knowledge, we
   25601              :      would reject the possibility I=1.  */
   25602     15751482 :   targs = copy_template_args (targs);
   25603     59693560 :   for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
   25604     43942078 :     targ = NULL_TREE;
   25605              : 
   25606     15751482 :   int err;
   25607     15751482 :   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25608          768 :     err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
   25609              :   else
   25610     15750714 :     err = unify (tparms, targs,
   25611     15750714 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   25612     15750714 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)),
   25613              :                  UNIFY_ALLOW_NONE, explain_p);
   25614              : 
   25615     15751482 :   return err ? NULL_TREE : arg;
   25616              : }
   25617              : 
   25618              : /* Given a template type PARM and a class type ARG, find the unique
   25619              :    base type in ARG that is an instance of PARM.  We do not examine
   25620              :    ARG itself; only its base-classes.  If there is not exactly one
   25621              :    appropriate base class, return NULL_TREE.  PARM may be the type of
   25622              :    a partial specialization, as well as a plain template type.  Used
   25623              :    by unify.  */
   25624              : 
   25625              : static enum template_base_result
   25626    180473364 : get_template_base (tree tparms, tree targs, tree parm, tree arg,
   25627              :                    bool explain_p, tree *result)
   25628              : {
   25629    180473364 :   tree rval = NULL_TREE;
   25630    180473364 :   tree binfo;
   25631              : 
   25632    180473364 :   gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
   25633              : 
   25634    180473364 :   binfo = TYPE_BINFO (complete_type (arg));
   25635    180473364 :   if (!binfo)
   25636              :     {
   25637              :       /* The type could not be completed.  */
   25638          147 :       *result = NULL_TREE;
   25639          147 :       return tbr_incomplete_type;
   25640              :     }
   25641              : 
   25642              :   /* Walk in inheritance graph order.  The search order is not
   25643              :      important, and this avoids multiple walks of virtual bases.  */
   25644    212347570 :   for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
   25645              :     {
   25646     63748732 :       tree r = try_class_unification (tparms, targs, parm,
   25647     31874366 :                                       BINFO_TYPE (binfo), explain_p);
   25648              : 
   25649     31874366 :       if (r)
   25650              :         {
   25651              :           /* If there is more than one satisfactory baseclass, then:
   25652              : 
   25653              :                [temp.deduct.call]
   25654              : 
   25655              :               If they yield more than one possible deduced A, the type
   25656              :               deduction fails.
   25657              : 
   25658              :              applies.  */
   25659       691283 :           if (rval && !same_type_p (r, rval))
   25660              :             {
   25661              :               /* [temp.deduct.call]/4.3: If there is a class C that is a
   25662              :                  (direct or indirect) base class of D and derived (directly or
   25663              :                  indirectly) from a class B and that would be a valid deduced
   25664              :                  A, the deduced A cannot be B or pointer to B, respectively. */
   25665           43 :               if (DERIVED_FROM_P (r, rval))
   25666              :                 /* Ignore r.  */
   25667           27 :                 continue;
   25668           16 :               else if (DERIVED_FROM_P (rval, r))
   25669              :                 /* Ignore rval.  */;
   25670              :               else
   25671              :                 {
   25672           13 :                   *result = NULL_TREE;
   25673           13 :                   return tbr_ambiguous_baseclass;
   25674              :                 }
   25675              :             }
   25676              : 
   25677              :           rval = r;
   25678              :         }
   25679              :     }
   25680              : 
   25681    180473204 :   *result = rval;
   25682    180473204 :   return tbr_success;
   25683              : }
   25684              : 
   25685              : /* Returns the level of DECL, which declares a template parameter.  */
   25686              : 
   25687              : static int
   25688    253164794 : template_decl_level (tree decl)
   25689              : {
   25690    253164794 :   switch (TREE_CODE (decl))
   25691              :     {
   25692    237110424 :     case TYPE_DECL:
   25693    237110424 :     case TEMPLATE_DECL:
   25694    237110424 :       return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
   25695              : 
   25696     16054370 :     case PARM_DECL:
   25697     16054370 :       return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
   25698              : 
   25699            0 :     default:
   25700            0 :       gcc_unreachable ();
   25701              :     }
   25702              :   return 0;
   25703              : }
   25704              : 
   25705              : /* Decide whether ARG can be unified with PARM, considering only the
   25706              :    cv-qualifiers of each type, given STRICT as documented for unify.
   25707              :    Returns nonzero iff the unification is OK on that basis.  */
   25708              : 
   25709              : static int
   25710    526878865 : check_cv_quals_for_unify (int strict, tree arg, tree parm)
   25711              : {
   25712    526878865 :   int arg_quals = cp_type_quals (arg);
   25713    526878865 :   int parm_quals = cp_type_quals (parm);
   25714              : 
   25715    526878865 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   25716    242613264 :       && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   25717              :     {
   25718              :       /*  Although a CVR qualifier is ignored when being applied to a
   25719              :           substituted template parameter ([8.3.2]/1 for example), that
   25720              :           does not allow us to unify "const T" with "int&" because both
   25721              :           types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
   25722              :           It is ok when we're allowing additional CV qualifiers
   25723              :           at the outer level [14.8.2.1]/3,1st bullet.  */
   25724    208901607 :       if ((TYPE_REF_P (arg)
   25725    191830953 :            || FUNC_OR_METHOD_TYPE_P (arg))
   25726     17203204 :           && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
   25727              :         return 0;
   25728              : 
   25729    208901300 :       if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
   25730    139158152 :           && (parm_quals & TYPE_QUAL_RESTRICT))
   25731              :         return 0;
   25732              :     }
   25733              : 
   25734    493166901 :   if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   25735    255812104 :       && (arg_quals & parm_quals) != parm_quals)
   25736              :     return 0;
   25737              : 
   25738    525121121 :   if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
   25739    284077255 :       && (parm_quals & arg_quals) != arg_quals)
   25740      1912769 :     return 0;
   25741              : 
   25742              :   return 1;
   25743              : }
   25744              : 
   25745              : /* Determines the LEVEL and INDEX for the template parameter PARM.  */
   25746              : void
   25747   7675512565 : template_parm_level_and_index (tree parm, int* level, int* index)
   25748              : {
   25749   7675512565 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   25750   7675512565 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   25751    302139073 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25752              :     {
   25753   7374319424 :       *index = TEMPLATE_TYPE_IDX (parm);
   25754   7374319424 :       *level = TEMPLATE_TYPE_LEVEL (parm);
   25755              :     }
   25756              :   else
   25757              :     {
   25758    301193141 :       *index = TEMPLATE_PARM_IDX (parm);
   25759    301193141 :       *level = TEMPLATE_PARM_LEVEL (parm);
   25760              :     }
   25761   7675512565 : }
   25762              : 
   25763              : #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP)                    \
   25764              :   do {                                                                  \
   25765              :     if (unify (TP, TA, P, A, S, EP))                                    \
   25766              :       return 1;                                                         \
   25767              :   } while (0)
   25768              : 
   25769              : /* Unifies the remaining arguments in PACKED_ARGS with the pack
   25770              :    expansion at the end of PACKED_PARMS. Returns 0 if the type
   25771              :    deduction succeeds, 1 otherwise. STRICT is the same as in
   25772              :    fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
   25773              :    function call argument list. We'll need to adjust the arguments to make them
   25774              :    types. SUBR tells us if this is from a recursive call to
   25775              :    type_unification_real, or for comparing two template argument
   25776              :    lists. */
   25777              : 
   25778              : static int
   25779      8037182 : unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
   25780              :                       tree packed_args, unification_kind_t strict,
   25781              :                       bool subr, bool explain_p)
   25782              : {
   25783      8037182 :   tree parm
   25784      8037182 :     = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
   25785      8037182 :   tree pattern = PACK_EXPANSION_PATTERN (parm);
   25786      8037182 :   tree pack, packs = NULL_TREE;
   25787      8037182 :   int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
   25788              : 
   25789              :   /* Add in any args remembered from an earlier partial instantiation.  */
   25790      8037182 :   targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
   25791     16074364 :   int levels = TMPL_ARGS_DEPTH (targs);
   25792              : 
   25793      8037182 :   packed_args = expand_template_argument_pack (packed_args);
   25794              : 
   25795      8037182 :   int len = TREE_VEC_LENGTH (packed_args);
   25796              : 
   25797              :   /* Determine the parameter packs we will be deducing from the
   25798              :      pattern, and record their current deductions.  */
   25799     15889705 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
   25800     16074402 :        pack; pack = TREE_CHAIN (pack))
   25801              :     {
   25802      8037220 :       tree parm_pack = TREE_VALUE (pack);
   25803      8037220 :       int idx, level;
   25804              : 
   25805              :       /* Only template parameter packs can be deduced, not e.g. function
   25806              :          parameter packs or __bases or __integer_pack.  */
   25807      8037220 :       if (!TEMPLATE_PARM_P (parm_pack))
   25808       293397 :         continue;
   25809              : 
   25810              :       /* Determine the index and level of this parameter pack.  */
   25811      8037214 :       template_parm_level_and_index (parm_pack, &level, &idx);
   25812      8037214 :       if (level > levels)
   25813       293391 :         continue;
   25814              : 
   25815              :       /* Keep track of the parameter packs and their corresponding
   25816              :          argument packs.  */
   25817     15487646 :       packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
   25818      7743823 :       TREE_TYPE (packs) = make_tree_vec (len - start);
   25819              :     }
   25820              : 
   25821              :   /* Loop through all of the arguments that have not yet been
   25822              :      unified and unify each with the pattern.  */
   25823     16646821 :   for (i = start; i < len; i++)
   25824              :     {
   25825      8612926 :       tree parm;
   25826      8612926 :       bool any_explicit = false;
   25827      8612926 :       tree arg = TREE_VEC_ELT (packed_args, i);
   25828              : 
   25829              :       /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
   25830              :          or the element of its argument pack at the current index if
   25831              :          this argument was explicitly specified.  */
   25832     16932523 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   25833              :         {
   25834      8319597 :           int idx, level;
   25835      8319597 :           tree arg, pargs;
   25836      8319597 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   25837              : 
   25838      8319597 :           arg = NULL_TREE;
   25839      8319597 :           if (TREE_VALUE (pack)
   25840       123289 :               && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
   25841      8320354 :               && (i - start < TREE_VEC_LENGTH (pargs)))
   25842              :             {
   25843          694 :               any_explicit = true;
   25844          694 :               arg = TREE_VEC_ELT (pargs, i - start);
   25845              :             }
   25846     16639194 :           TMPL_ARG (targs, level, idx) = arg;
   25847              :         }
   25848              : 
   25849              :       /* If we had explicit template arguments, substitute them into the
   25850              :          pattern before deduction.  */
   25851      8612926 :       if (any_explicit)
   25852              :         {
   25853              :           /* Some arguments might still be unspecified or dependent.  */
   25854          694 :           bool dependent;
   25855          694 :           ++processing_template_decl;
   25856          694 :           dependent = any_dependent_template_arguments_p (targs);
   25857          694 :           if (!dependent)
   25858          679 :             --processing_template_decl;
   25859         1388 :           parm = tsubst (pattern, targs,
   25860              :                          explain_p ? tf_warning_or_error : tf_none,
   25861              :                          NULL_TREE);
   25862          694 :           if (dependent)
   25863           15 :             --processing_template_decl;
   25864          694 :           if (parm == error_mark_node)
   25865              :             return 1;
   25866              :         }
   25867              :       else
   25868              :         parm = pattern;
   25869              : 
   25870              :       /* Unify the pattern with the current argument.  */
   25871      8612926 :       if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
   25872              :                               explain_p))
   25873              :         return 1;
   25874              : 
   25875              :       /* For each parameter pack, collect the deduced value.  */
   25876     16925943 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   25877              :         {
   25878      8316304 :           int idx, level;
   25879      8316304 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   25880              : 
   25881      8316304 :           TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
   25882     16632608 :             TMPL_ARG (targs, level, idx);
   25883              :         }
   25884              :     }
   25885              : 
   25886              :   /* Verify that the results of unification with the parameter packs
   25887              :      produce results consistent with what we've seen before, and make
   25888              :      the deduced argument packs available.  */
   25889     15774407 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   25890              :     {
   25891      7740530 :       tree old_pack = TREE_VALUE (pack);
   25892      7740530 :       tree new_args = TREE_TYPE (pack);
   25893      7740530 :       int i, len = TREE_VEC_LENGTH (new_args);
   25894      7740530 :       int idx, level;
   25895      7740530 :       bool nondeduced_p = false;
   25896              : 
   25897              :       /* By default keep the original deduced argument pack.
   25898              :          If necessary, more specific code is going to update the
   25899              :          resulting deduced argument later down in this function.  */
   25900      7740530 :       template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   25901     15481060 :       TMPL_ARG (targs, level, idx) = old_pack;
   25902              : 
   25903              :       /* If NEW_ARGS contains any NULL_TREE entries, we didn't
   25904              :          actually deduce anything.  */
   25905     16054473 :       for (i = 0; i < len && !nondeduced_p; ++i)
   25906      8313943 :         if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
   25907         1394 :           nondeduced_p = true;
   25908      7740530 :       if (nondeduced_p)
   25909         1394 :         continue;
   25910              : 
   25911      7739136 :       if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
   25912              :         {
   25913              :           /* If we had fewer function args than explicit template args,
   25914              :              just use the explicits.  */
   25915          299 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   25916          299 :           int explicit_len = TREE_VEC_LENGTH (explicit_args);
   25917          299 :           if (len < explicit_len)
   25918      7739136 :             new_args = explicit_args;
   25919              :         }
   25920              : 
   25921      7739136 :       if (!old_pack)
   25922              :         {
   25923      7542728 :           tree result;
   25924              :           /* Build the deduced *_ARGUMENT_PACK.  */
   25925      7542728 :           if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
   25926              :             {
   25927       184638 :               result = make_node (NONTYPE_ARGUMENT_PACK);
   25928       184638 :               TREE_CONSTANT (result) = 1;
   25929              :             }
   25930              :           else
   25931      7358090 :             result = cxx_make_type (TYPE_ARGUMENT_PACK);
   25932              : 
   25933      7542728 :           ARGUMENT_PACK_ARGS (result) = new_args;
   25934              : 
   25935              :           /* Note the deduced argument packs for this parameter
   25936              :              pack.  */
   25937     15085456 :           TMPL_ARG (targs, level, idx) = result;
   25938              :         }
   25939       196408 :       else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
   25940       196408 :                && (ARGUMENT_PACK_ARGS (old_pack)
   25941          299 :                    == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
   25942              :         {
   25943              :           /* We only had the explicitly-provided arguments before, but
   25944              :              now we have a complete set of arguments.  */
   25945          299 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   25946              : 
   25947          299 :           ARGUMENT_PACK_ARGS (old_pack) = new_args;
   25948          299 :           ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
   25949          299 :           ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
   25950              :         }
   25951              :       else
   25952              :         {
   25953       196109 :           tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
   25954       196109 :           tree old_args = ARGUMENT_PACK_ARGS (old_pack);
   25955       196109 :           temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
   25956              :           /* During template argument deduction for the aggregate deduction
   25957              :              candidate, the number of elements in a trailing parameter pack
   25958              :              is only deduced from the number of remaining function
   25959              :              arguments if it is not otherwise deduced.  */
   25960       196109 :           if (cxx_dialect >= cxx20
   25961       191659 :               && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
   25962              :               /* FIXME This isn't set properly for partial instantiations.  */
   25963           14 :               && TPARMS_PRIMARY_TEMPLATE (tparms)
   25964       196121 :               && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
   25965           12 :             TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
   25966       196109 :           if (!comp_template_args (old_args, new_args,
   25967              :                                    &bad_old_arg, &bad_new_arg))
   25968              :             /* Inconsistent unification of this parameter pack.  */
   25969           21 :             return unify_parameter_pack_inconsistent (explain_p,
   25970              :                                                       bad_old_arg,
   25971              :                                                       bad_new_arg);
   25972       196109 :         }
   25973              :     }
   25974              : 
   25975      8037182 :   return unify_success (explain_p);
   25976              : }
   25977              : 
   25978              : /* Handle unification of the domain of an array.  PARM_DOM and ARG_DOM are
   25979              :    INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs.  The other
   25980              :    parameters and return value are as for unify.  */
   25981              : 
   25982              : static int
   25983       164396 : unify_array_domain (tree tparms, tree targs,
   25984              :                     tree parm_dom, tree arg_dom,
   25985              :                     bool explain_p)
   25986              : {
   25987       164396 :   tree parm_max;
   25988       164396 :   tree arg_max;
   25989       164396 :   bool parm_cst;
   25990       164396 :   bool arg_cst;
   25991              : 
   25992              :   /* Our representation of array types uses "N - 1" as the
   25993              :      TYPE_MAX_VALUE for an array with "N" elements, if "N" is
   25994              :      not an integer constant.  We cannot unify arbitrarily
   25995              :      complex expressions, so we eliminate the MINUS_EXPRs
   25996              :      here.  */
   25997       164396 :   parm_max = TYPE_MAX_VALUE (parm_dom);
   25998       164396 :   parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
   25999       164396 :   if (!parm_cst)
   26000              :     {
   26001       164303 :       gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
   26002       164303 :       parm_max = TREE_OPERAND (parm_max, 0);
   26003              :     }
   26004       164396 :   arg_max = TYPE_MAX_VALUE (arg_dom);
   26005       164396 :   arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
   26006       164396 :   if (!arg_cst)
   26007              :     {
   26008              :       /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
   26009              :          trying to unify the type of a variable with the type
   26010              :          of a template parameter.  For example:
   26011              : 
   26012              :            template <unsigned int N>
   26013              :            void f (char (&) [N]);
   26014              :            int g();
   26015              :            void h(int i) {
   26016              :              char a[g(i)];
   26017              :              f(a);
   26018              :            }
   26019              : 
   26020              :          Here, the type of the ARG will be "int [g(i)]", and
   26021              :          may be a SAVE_EXPR, etc.  */
   26022         2776 :       if (TREE_CODE (arg_max) != MINUS_EXPR)
   26023            6 :         return unify_vla_arg (explain_p, arg_dom);
   26024         2770 :       arg_max = TREE_OPERAND (arg_max, 0);
   26025              :     }
   26026              : 
   26027              :   /* If only one of the bounds used a MINUS_EXPR, compensate
   26028              :      by adding one to the other bound.  */
   26029       164390 :   if (parm_cst && !arg_cst)
   26030            0 :     parm_max = fold_build2_loc (input_location, PLUS_EXPR,
   26031              :                                 integer_type_node,
   26032              :                                 parm_max,
   26033              :                                 integer_one_node);
   26034       164390 :   else if (arg_cst && !parm_cst)
   26035       161527 :     arg_max = fold_build2_loc (input_location, PLUS_EXPR,
   26036              :                                integer_type_node,
   26037              :                                arg_max,
   26038              :                                integer_one_node);
   26039              : 
   26040       164390 :   return unify (tparms, targs, parm_max, arg_max,
   26041       164390 :                 UNIFY_ALLOW_INTEGER, explain_p);
   26042              : }
   26043              : 
   26044              : /* Returns whether T, a P or A in unify, is a type, template or expression.  */
   26045              : 
   26046              : enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
   26047              : 
   26048              : static pa_kind_t
   26049   1773719622 : pa_kind (tree t)
   26050              : {
   26051   1773719622 :   if (PACK_EXPANSION_P (t))
   26052      1180273 :     t = PACK_EXPANSION_PATTERN (t);
   26053   1773719622 :   if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
   26054   1772952540 :       || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
   26055   3546672141 :       || DECL_TYPE_TEMPLATE_P (t))
   26056              :     return pa_tmpl;
   26057   1772424479 :   else if (TYPE_P (t))
   26058              :     return pa_type;
   26059              :   else
   26060    313085887 :     return pa_expr;
   26061              : }
   26062              : 
   26063              : /* Deduce the value of template parameters.  TPARMS is the (innermost)
   26064              :    set of template parameters to a template.  TARGS is the bindings
   26065              :    for those template parameters, as determined thus far; TARGS may
   26066              :    include template arguments for outer levels of template parameters
   26067              :    as well.  PARM is a parameter to a template function, or a
   26068              :    subcomponent of that parameter; ARG is the corresponding argument.
   26069              :    This function attempts to match PARM with ARG in a manner
   26070              :    consistent with the existing assignments in TARGS.  If more values
   26071              :    are deduced, then TARGS is updated.
   26072              : 
   26073              :    Returns 0 if the type deduction succeeds, 1 otherwise.  The
   26074              :    parameter STRICT is a bitwise or of the following flags:
   26075              : 
   26076              :      UNIFY_ALLOW_NONE:
   26077              :        Require an exact match between PARM and ARG.
   26078              :      UNIFY_ALLOW_MORE_CV_QUAL:
   26079              :        Allow the deduced ARG to be more cv-qualified (by qualification
   26080              :        conversion) than ARG.
   26081              :      UNIFY_ALLOW_LESS_CV_QUAL:
   26082              :        Allow the deduced ARG to be less cv-qualified than ARG.
   26083              :      UNIFY_ALLOW_DERIVED:
   26084              :        Allow the deduced ARG to be a template base class of ARG,
   26085              :        or a pointer to a template base class of the type pointed to by
   26086              :        ARG.
   26087              :      UNIFY_ALLOW_INTEGER:
   26088              :        Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
   26089              :        case for more information.
   26090              :      UNIFY_ALLOW_OUTER_LEVEL:
   26091              :        This is the outermost level of a deduction. Used to determine validity
   26092              :        of qualification conversions. A valid qualification conversion must
   26093              :        have const qualified pointers leading up to the inner type which
   26094              :        requires additional CV quals, except at the outer level, where const
   26095              :        is not required [conv.qual]. It would be normal to set this flag in
   26096              :        addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
   26097              :      UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
   26098              :        This is the outermost level of a deduction, and PARM can be more CV
   26099              :        qualified at this point.
   26100              :      UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
   26101              :        This is the outermost level of a deduction, and PARM can be less CV
   26102              :        qualified at this point.  */
   26103              : 
   26104              : static int
   26105    908516604 : unify (tree tparms, tree targs, tree parm, tree arg, int strict,
   26106              :        bool explain_p)
   26107              : {
   26108    908516604 :   int idx;
   26109    908516604 :   tree targ;
   26110    908516604 :   tree tparm;
   26111    908516604 :   int strict_in = strict;
   26112    908509846 :   tsubst_flags_t complain = (explain_p
   26113    908516604 :                              ? tf_warning_or_error
   26114              :                              : tf_none);
   26115              : 
   26116    908516604 :   if (arg == error_mark_node)
   26117    264531790 :     return unify_invalid (explain_p);
   26118    908516604 :   if (arg == unknown_type_node
   26119    908516604 :       || arg == init_list_type_node)
   26120              :     /* We can't deduce anything from this, but we might get all the
   26121              :        template args from other function args.  */
   26122    364662892 :     return unify_success (explain_p);
   26123              : 
   26124    908516604 :   if (parm == any_targ_node || arg == any_targ_node)
   26125    364662892 :     return unify_success (explain_p);
   26126              : 
   26127              :   /* Strip conversions that will interfere with NTTP deduction.
   26128              :      I don't think this will do the right thing with respect to types.
   26129              :      But the only case I've seen it in so far has been array bounds, where
   26130              :      signedness is the only information lost, and I think that will be
   26131              :      okay.  VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
   26132              :      finish_id_expression_1, and are also OK.  */
   26133    908516596 :   if (deducible_expression (parm))
   26134      9740815 :     while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR
   26135     19481886 :            || TREE_CODE (parm) == IMPLICIT_CONV_EXPR)
   26136         4032 :       parm = TREE_OPERAND (parm, 0);
   26137              : 
   26138              :   /* If PARM uses template parameters, then we can't bail out here,
   26139              :      even if ARG == PARM, since we won't record unifications for the
   26140              :      template parameters.  We might need them if we're trying to
   26141              :      figure out which of two things is more specialized.  */
   26142    908516596 :   if (arg == parm
   26143    908516596 :       && (DECL_P (parm) || !uses_template_parms (parm)))
   26144     21647467 :     return unify_success (explain_p);
   26145              : 
   26146              :   /* Handle init lists early, so the rest of the function can assume
   26147              :      we're dealing with a type. */
   26148    886869129 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg))
   26149              :     {
   26150         9318 :       tree elttype;
   26151         9318 :       tree orig_parm = parm;
   26152              : 
   26153         9318 :       if (!is_std_init_list (parm)
   26154         9318 :           && TREE_CODE (parm) != ARRAY_TYPE)
   26155              :         /* We can only deduce from an initializer list argument if the
   26156              :            parameter is std::initializer_list or an array; otherwise this
   26157              :            is a non-deduced context. */
   26158         8788 :         return unify_success (explain_p);
   26159              : 
   26160         2661 :       if (TREE_CODE (parm) == ARRAY_TYPE)
   26161          519 :         elttype = TREE_TYPE (parm);
   26162              :       else
   26163              :         {
   26164         2142 :           elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
   26165              :           /* Deduction is defined in terms of a single type, so just punt
   26166              :              on the (bizarre) std::initializer_list<T...>.  */
   26167         2142 :           if (PACK_EXPANSION_P (elttype))
   26168         8788 :             return unify_success (explain_p);
   26169              :         }
   26170              : 
   26171         2658 :       if (strict != DEDUCE_EXACT
   26172         2658 :           && TYPE_P (elttype)
   26173         5316 :           && !uses_deducible_template_parms (elttype))
   26174              :         /* If ELTTYPE has no deducible template parms, skip deduction from
   26175              :            the list elements.  */;
   26176              :       else
   26177        14417 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
   26178              :           {
   26179         7078 :             tree elt = e.value;
   26180         7078 :             int elt_strict = strict;
   26181              : 
   26182         7078 :             if (elt == error_mark_node)
   26183          467 :               return unify_invalid (explain_p);
   26184              : 
   26185         7078 :             if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
   26186              :               {
   26187         6083 :                 tree type = TREE_TYPE (elt);
   26188         6083 :                 if (type == error_mark_node)
   26189            6 :                   return unify_invalid (explain_p);
   26190              :                 /* It should only be possible to get here for a call.  */
   26191         6077 :                 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
   26192        12154 :                 elt_strict |= maybe_adjust_types_for_deduction
   26193         6077 :                   (tparms, DEDUCE_CALL, &elttype, &type, elt);
   26194         6077 :                 elt = type;
   26195              :               }
   26196              : 
   26197         7072 :           RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
   26198              :                                    explain_p);
   26199              :         }
   26200              : 
   26201         2191 :       if (TREE_CODE (parm) == ARRAY_TYPE
   26202         2191 :           && deducible_array_bound (TYPE_DOMAIN (parm)))
   26203              :         {
   26204              :           /* Also deduce from the length of the initializer list.  */
   26205           63 :           tree max = size_int (count_ctor_elements (arg));
   26206           63 :           tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
   26207           63 :           if (idx == error_mark_node)
   26208          467 :             return unify_invalid (explain_p);
   26209           63 :           return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   26210           63 :                                      idx, explain_p);
   26211              :         }
   26212              : 
   26213              :       /* If the std::initializer_list<T> deduction worked, replace the
   26214              :          deduced A with std::initializer_list<A>.  */
   26215              :       if (orig_parm != parm)
   26216              :         {
   26217              :           idx = TEMPLATE_TYPE_IDX (orig_parm);
   26218              :           targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26219              :           targ = listify (targ);
   26220              :           TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
   26221              :         }
   26222         8788 :       return unify_success (explain_p);
   26223              :     }
   26224              : 
   26225              :   /* If parm and arg aren't the same kind of thing (template, type, or
   26226              :      expression), fail early.  */
   26227    886859811 :   if (pa_kind (parm) != pa_kind (arg))
   26228    264531790 :     return unify_invalid (explain_p);
   26229              : 
   26230              :   /* Immediately reject some pairs that won't unify because of
   26231              :      cv-qualification mismatches.  */
   26232    886859651 :   if (TREE_CODE (arg) == TREE_CODE (parm)
   26233    471863546 :       && TYPE_P (arg)
   26234              :       /* It is the elements of the array which hold the cv quals of an array
   26235              :          type, and the elements might be template type parms. We'll check
   26236              :          when we recurse.  */
   26237    323824150 :       && TREE_CODE (arg) != ARRAY_TYPE
   26238              :       /* We check the cv-qualifiers when unifying with template type
   26239              :          parameters below.  We want to allow ARG `const T' to unify with
   26240              :          PARM `T' for example, when computing which of two templates
   26241              :          is more specialized, for example.  */
   26242    323619753 :       && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
   26243   1171106288 :       && !check_cv_quals_for_unify (strict_in, arg, parm))
   26244      2076338 :     return unify_cv_qual_mismatch (explain_p, parm, arg);
   26245              : 
   26246    884783313 :   if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
   26247    429806988 :       && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
   26248   1154687308 :       && !FUNC_OR_METHOD_TYPE_P (parm))
   26249    269629398 :     strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
   26250              :   /* PMFs recurse at the same level, so don't strip this yet.  */
   26251    884783313 :   if (!TYPE_PTRMEMFUNC_P (parm))
   26252    884780655 :     strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
   26253    884783313 :   strict &= ~UNIFY_ALLOW_DERIVED;
   26254    884783313 :   strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   26255    884783313 :   strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
   26256              : 
   26257    884783313 :   switch (TREE_CODE (parm))
   26258              :     {
   26259              :     case TYPENAME_TYPE:
   26260              :     case SCOPE_REF:
   26261              :     case UNBOUND_CLASS_TEMPLATE:
   26262              :       /* In a type which contains a nested-name-specifier, template
   26263              :          argument values cannot be deduced for template parameters used
   26264              :          within the nested-name-specifier.  */
   26265    364662892 :       return unify_success (explain_p);
   26266              : 
   26267    243425932 :     case TEMPLATE_TYPE_PARM:
   26268    243425932 :     case TEMPLATE_TEMPLATE_PARM:
   26269    243425932 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   26270    243425932 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   26271    243425932 :       if (error_operand_p (tparm))
   26272    264531790 :         return unify_invalid (explain_p);
   26273              : 
   26274    486851856 :       if (TEMPLATE_TYPE_LEVEL (parm)
   26275    243425928 :           != template_decl_level (tparm))
   26276              :         /* The PARM is not one we're trying to unify.  Just check
   26277              :            to see if it matches ARG.  */
   26278              :         {
   26279          261 :           if (TREE_CODE (arg) == TREE_CODE (parm)
   26280          270 :               && (is_auto (parm) ? is_auto (arg)
   26281            9 :                   : same_type_p (parm, arg)))
   26282           39 :             return unify_success (explain_p);
   26283              :           else
   26284          222 :             return unify_type_mismatch (explain_p, parm, arg);
   26285              :         }
   26286    243425667 :       idx = TEMPLATE_TYPE_IDX (parm);
   26287    243425667 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26288    243425667 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
   26289    243425667 :       if (error_operand_p (tparm))
   26290    264531790 :         return unify_invalid (explain_p);
   26291              : 
   26292              :       /* Check for mixed types and values.  */
   26293    243425667 :       if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   26294    242611390 :            && TREE_CODE (tparm) != TYPE_DECL)
   26295    243425667 :           || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   26296       645193 :               && TREE_CODE (tparm) != TEMPLATE_DECL))
   26297            0 :         gcc_unreachable ();
   26298              : 
   26299    243425667 :       if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   26300              :         {
   26301       169084 :           if ((strict_in & UNIFY_ALLOW_DERIVED)
   26302       169084 :               && CLASS_TYPE_P (arg))
   26303              :             {
   26304              :               /* First try to match ARG directly.  */
   26305          768 :               tree t = try_class_unification (tparms, targs, parm, arg,
   26306          768 :                                               explain_p);
   26307          768 :               if (!t)
   26308              :                 {
   26309              :                   /* Otherwise, look for a suitable base of ARG, as below.  */
   26310           40 :                   enum template_base_result r;
   26311           40 :                   r = get_template_base (tparms, targs, parm, arg,
   26312              :                                          explain_p, &t);
   26313           40 :                   if (!t)
   26314           34 :                     return unify_no_common_base (explain_p, r, parm, arg);
   26315            6 :                   arg = t;
   26316              :                 }
   26317              :             }
   26318              :           /* ARG must be constructed from a template class or a template
   26319              :              template parameter.  */
   26320       168316 :           else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
   26321       168316 :                    && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   26322          958 :             return unify_template_deduction_failure (explain_p, parm, arg);
   26323              : 
   26324              :           /* Deduce arguments T, i from TT<T> or TT<i>.  */
   26325       168092 :           if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
   26326              :             return 1;
   26327              : 
   26328       167832 :           arg = TYPE_TI_TEMPLATE (arg);
   26329       167832 :           if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
   26330              :             /* If the template is a template template parameter, use the
   26331              :                TEMPLATE_TEMPLATE_PARM for matching.  */
   26332           41 :             arg = TREE_TYPE (arg);
   26333              : 
   26334              :           /* Fall through to deduce template name.  */
   26335              :         }
   26336              : 
   26337    243424415 :       if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   26338    242779222 :           || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   26339              :         {
   26340              :           /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
   26341              : 
   26342              :           /* Simple cases: Value already set, does match or doesn't.  */
   26343       813025 :           if (targ != NULL_TREE && template_args_equal (targ, arg))
   26344    364662892 :             return unify_success (explain_p);
   26345       794258 :           else if (targ)
   26346        19249 :             return unify_inconsistency (explain_p, parm, targ, arg);
   26347              :         }
   26348              :       else
   26349              :         {
   26350              :           /* If PARM is `const T' and ARG is only `int', we don't have
   26351              :              a match unless we are allowing additional qualification.
   26352              :              If ARG is `const int' and PARM is just `T' that's OK;
   26353              :              that binds `const int' to `T'.  */
   26354    242611390 :           if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
   26355              :                                          arg, parm))
   26356      1592947 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   26357              : 
   26358              :           /* Consider the case where ARG is `const volatile int' and
   26359              :              PARM is `const T'.  Then, T should be `volatile int'.  */
   26360    482036886 :           arg = cp_build_qualified_type
   26361    241018443 :             (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
   26362    241018443 :           if (arg == error_mark_node)
   26363    264531790 :             return unify_invalid (explain_p);
   26364              : 
   26365              :           /* Simple cases: Value already set, does match or doesn't.  */
   26366    241018443 :           if (targ != NULL_TREE && same_type_p (targ, arg))
   26367    364662892 :             return unify_success (explain_p);
   26368    215560321 :           else if (targ)
   26369       938985 :             return unify_inconsistency (explain_p, parm, targ, arg);
   26370              : 
   26371              :           /* Make sure that ARG is not a variable-sized array.  (Note
   26372              :              that were talking about variable-sized arrays (like
   26373              :              `int[n]'), rather than arrays of unknown size (like
   26374              :              `int[]').)  We'll get very confused by such a type since
   26375              :              the bound of the array is not constant, and therefore
   26376              :              not mangleable.  Besides, such types are not allowed in
   26377              :              ISO C++, so we can do as we please here.  We do allow
   26378              :              them for 'auto' deduction, since that isn't ABI-exposed.  */
   26379    214621336 :           if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
   26380           14 :             return unify_vla_arg (explain_p, arg);
   26381              : 
   26382              :           /* Strip typedefs as in convert_template_argument.  */
   26383    214621322 :           arg = canonicalize_type_argument (arg, tf_none);
   26384              :         }
   26385              : 
   26386              :       /* If ARG is a parameter pack or an expansion, we cannot unify
   26387              :          against it unless PARM is also a parameter pack.  */
   26388    430792010 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   26389    216507629 :           && !template_parameter_pack_p (parm))
   26390       431278 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   26391              : 
   26392              :       /* If the argument deduction results is a METHOD_TYPE,
   26393              :          then there is a problem.
   26394              :          METHOD_TYPE doesn't map to any real C++ type the result of
   26395              :          the deduction cannot be of that type.  */
   26396    214965053 :       if (TREE_CODE (arg) == METHOD_TYPE)
   26397            6 :         return unify_method_type_error (explain_p, arg);
   26398              : 
   26399    214965047 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   26400    214965047 :       return unify_success (explain_p);
   26401              : 
   26402      9738866 :     case TEMPLATE_PARM_INDEX:
   26403      9738866 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   26404      9738866 :       if (error_operand_p (tparm))
   26405    264531790 :         return unify_invalid (explain_p);
   26406              : 
   26407      9738866 :       if (TEMPLATE_PARM_LEVEL (parm)
   26408      9738866 :           != template_decl_level (tparm))
   26409              :         {
   26410              :           /* The PARM is not one we're trying to unify.  Just check
   26411              :              to see if it matches ARG.  */
   26412            3 :           int result = !(TREE_CODE (arg) == TREE_CODE (parm)
   26413            0 :                          && cp_tree_equal (parm, arg));
   26414            3 :           if (result)
   26415            3 :             unify_expression_unequal (explain_p, parm, arg);
   26416            3 :           return result;
   26417              :         }
   26418              : 
   26419      9738863 :       idx = TEMPLATE_PARM_IDX (parm);
   26420      9738863 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26421              : 
   26422      9738863 :       if (targ)
   26423              :         {
   26424         7430 :           if ((strict & UNIFY_ALLOW_INTEGER)
   26425         1231 :               && TREE_TYPE (targ) && TREE_TYPE (arg)
   26426         8661 :               && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
   26427              :             /* We're deducing from an array bound, the type doesn't matter.
   26428              :                This conversion should match the one below.  */
   26429         1231 :             arg = fold (build_nop (TREE_TYPE (targ), arg));
   26430         7430 :           int x = !cp_tree_equal (targ, arg);
   26431         7430 :           if (x)
   26432          577 :             unify_inconsistency (explain_p, parm, targ, arg);
   26433         7430 :           return x;
   26434              :         }
   26435              : 
   26436              :       /* [temp.deduct.type] If, in the declaration of a function template
   26437              :          with a non-type template-parameter, the non-type
   26438              :          template-parameter is used in an expression in the function
   26439              :          parameter-list and, if the corresponding template-argument is
   26440              :          deduced, the template-argument type shall match the type of the
   26441              :          template-parameter exactly, except that a template-argument
   26442              :          deduced from an array bound may be of any integral type.
   26443              :          The non-type parameter might use already deduced type parameters.  */
   26444      9731433 :       tparm = TREE_TYPE (parm);
   26445     29194245 :       if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
   26446              :         /* We don't have enough levels of args to do any substitution.  This
   26447              :            can happen in the context of -fnew-ttp-matching.  */;
   26448              :       else
   26449              :         {
   26450      9725786 :           ++processing_template_decl;
   26451      9725786 :           tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
   26452      9725786 :           --processing_template_decl;
   26453              : 
   26454      9725786 :           if (tree a = type_uses_auto (tparm))
   26455              :             {
   26456         6660 :               tparm = do_auto_deduction (tparm, arg, a,
   26457              :                                          complain, adc_unify, targs,
   26458              :                                          LOOKUP_NORMAL,
   26459         3330 :                                          TPARMS_PRIMARY_TEMPLATE (tparms));
   26460         3330 :               if (tparm == error_mark_node)
   26461              :                 return 1;
   26462              :             }
   26463              :         }
   26464              : 
   26465      9731430 :       if (!TREE_TYPE (arg)
   26466      9731430 :           || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
   26467              :         /* Template-parameter dependent expression.  Just accept it for now.
   26468              :            It will later be processed in convert_template_argument.  */
   26469              :         ;
   26470      9731415 :       else if (same_type_ignoring_top_level_qualifiers_p
   26471      9731415 :                (non_reference (TREE_TYPE (arg)),
   26472              :                 non_reference (tparm)))
   26473              :         /* OK.  Ignore top-level quals here because a class-type template
   26474              :            parameter object is const.  */;
   26475       160776 :       else if ((strict & UNIFY_ALLOW_INTEGER)
   26476       160492 :                && CP_INTEGRAL_TYPE_P (tparm))
   26477              :         /* Convert the ARG to the type of PARM; the deduced non-type
   26478              :            template argument must exactly match the types of the
   26479              :            corresponding parameter.  This conversion should match the
   26480              :            one above.  */
   26481       160477 :         arg = fold (build_nop (tparm, arg));
   26482          299 :       else if (uses_template_parms (tparm))
   26483              :         {
   26484              :           /* We haven't deduced the type of this parameter yet.  */
   26485          200 :           if (cxx_dialect >= cxx17
   26486              :               /* We deduce from array bounds in try_array_deduction.  */
   26487          172 :               && !(strict & UNIFY_ALLOW_INTEGER)
   26488          671 :               && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
   26489              :             {
   26490              :               /* Deduce it from the non-type argument.  As above, ignore
   26491              :                  top-level quals here too.  */
   26492          120 :               tree atype = cv_unqualified (TREE_TYPE (arg));
   26493          120 :               RECUR_AND_CHECK_FAILURE (tparms, targs,
   26494              :                                        tparm, atype,
   26495              :                                        UNIFY_ALLOW_NONE, explain_p);
   26496              :               /* Now check whether the type of this parameter is still
   26497              :                  dependent, and give up if so.  */
   26498          111 :               ++processing_template_decl;
   26499          111 :               tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
   26500          111 :               --processing_template_decl;
   26501          111 :               if (uses_template_parms (tparm))
   26502              :                 return unify_success (explain_p);
   26503              :             }
   26504              :           else
   26505              :             /* Try again later.  */
   26506    364662892 :             return unify_success (explain_p);
   26507              :         }
   26508              :       else
   26509           99 :         return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
   26510              : 
   26511              :       /* If ARG is a parameter pack or an expansion, we cannot unify
   26512              :          against it unless PARM is also a parameter pack.  */
   26513     19462360 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   26514      9799658 :           && !TEMPLATE_PARM_PARAMETER_PACK (parm))
   26515            7 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   26516              : 
   26517      9731173 :       {
   26518      9731173 :         bool removed_attr = false;
   26519      9731173 :         arg = strip_typedefs_expr (arg, &removed_attr);
   26520              :       }
   26521      9731173 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   26522      9731173 :       return unify_success (explain_p);
   26523              : 
   26524           21 :     case PTRMEM_CST:
   26525           21 :      {
   26526              :         /* A pointer-to-member constant can be unified only with
   26527              :          another constant.  */
   26528           21 :       if (TREE_CODE (arg) != PTRMEM_CST)
   26529            3 :         return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
   26530              : 
   26531              :       /* Just unify the class member. It would be useless (and possibly
   26532              :          wrong, depending on the strict flags) to unify also
   26533              :          PTRMEM_CST_CLASS, because we want to be sure that both parm and
   26534              :          arg refer to the same variable, even if through different
   26535              :          classes. For instance:
   26536              : 
   26537              :          struct A { int x; };
   26538              :          struct B : A { };
   26539              : 
   26540              :          Unification of &A::x and &B::x must succeed.  */
   26541           18 :       return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
   26542           18 :                     PTRMEM_CST_MEMBER (arg), strict, explain_p);
   26543              :      }
   26544              : 
   26545      8431549 :     case POINTER_TYPE:
   26546      8431549 :       {
   26547      8431549 :         if (!TYPE_PTR_P (arg))
   26548      4413214 :           return unify_type_mismatch (explain_p, parm, arg);
   26549              : 
   26550              :         /* [temp.deduct.call]
   26551              : 
   26552              :            A can be another pointer or pointer to member type that can
   26553              :            be converted to the deduced A via a qualification
   26554              :            conversion (_conv.qual_).
   26555              : 
   26556              :            We pass down STRICT here rather than UNIFY_ALLOW_NONE.
   26557              :            This will allow for additional cv-qualification of the
   26558              :            pointed-to types if appropriate.  */
   26559              : 
   26560      4018335 :         if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
   26561              :           /* The derived-to-base conversion only persists through one
   26562              :              level of pointers.  */
   26563       631455 :           strict |= (strict_in & UNIFY_ALLOW_DERIVED);
   26564              : 
   26565      4018335 :         return unify (tparms, targs, TREE_TYPE (parm),
   26566      8036670 :                       TREE_TYPE (arg), strict, explain_p);
   26567              :       }
   26568              : 
   26569     32877172 :     case REFERENCE_TYPE:
   26570     32877172 :       if (!TYPE_REF_P (arg)
   26571     32877172 :           || TYPE_REF_IS_RVALUE (parm) != TYPE_REF_IS_RVALUE (arg))
   26572      6139293 :         return unify_type_mismatch (explain_p, parm, arg);
   26573     26737879 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26574     26737879 :                     strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   26575              : 
   26576      2823677 :     case ARRAY_TYPE:
   26577      2823677 :       if (TREE_CODE (arg) != ARRAY_TYPE)
   26578      2619280 :         return unify_type_mismatch (explain_p, parm, arg);
   26579       204397 :       if ((TYPE_DOMAIN (parm) == NULL_TREE)
   26580       204397 :           != (TYPE_DOMAIN (arg) == NULL_TREE))
   26581        11969 :         return unify_type_mismatch (explain_p, parm, arg);
   26582       192428 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26583              :                                strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   26584       192242 :       if (TYPE_DOMAIN (parm) != NULL_TREE)
   26585       328666 :         return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   26586       164333 :                                    TYPE_DOMAIN (arg), explain_p);
   26587              :       return unify_success (explain_p);
   26588              : 
   26589      7038867 :     case REAL_TYPE:
   26590      7038867 :     case COMPLEX_TYPE:
   26591      7038867 :     case VECTOR_TYPE:
   26592      7038867 :     case INTEGER_TYPE:
   26593      7038867 :     case BOOLEAN_TYPE:
   26594      7038867 :     case ENUMERAL_TYPE:
   26595      7038867 :     case VOID_TYPE:
   26596      7038867 :     case OPAQUE_TYPE:
   26597      7038867 :     case NULLPTR_TYPE:
   26598      7038867 :     case META_TYPE:
   26599      7038867 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26600      2225670 :         return unify_type_mismatch (explain_p, parm, arg);
   26601              : 
   26602              :       /* We have already checked cv-qualification at the top of the
   26603              :          function.  */
   26604      4813197 :       if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
   26605      3776510 :         return unify_type_mismatch (explain_p, parm, arg);
   26606              : 
   26607              :       /* As far as unification is concerned, this wins.  Later checks
   26608              :          will invalidate it if necessary.  */
   26609    364662892 :       return unify_success (explain_p);
   26610              : 
   26611              :       /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
   26612              :       /* Type INTEGER_CST can come from ordinary constant template args.  */
   26613     11288212 :     case INTEGER_CST:
   26614     11288212 :     case REAL_CST:
   26615     11288212 :       if (TREE_TYPE (arg) == NULL_TREE
   26616     11288212 :           || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
   26617           12 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   26618     11288200 :       while (CONVERT_EXPR_P (arg))
   26619            0 :         arg = TREE_OPERAND (arg, 0);
   26620              : 
   26621     11288200 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26622         2675 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   26623     11285525 :       return (simple_cst_equal (parm, arg)
   26624     11285525 :               ? unify_success (explain_p)
   26625     11285519 :               : unify_template_argument_mismatch (explain_p, parm, arg));
   26626              : 
   26627    134779550 :     case TREE_VEC:
   26628    134779550 :       {
   26629    134779550 :         int i, len, argslen;
   26630    134779550 :         int parm_variadic_p = 0;
   26631              : 
   26632    134779550 :         if (TREE_CODE (arg) != TREE_VEC)
   26633            0 :           return unify_template_argument_mismatch (explain_p, parm, arg);
   26634              : 
   26635    134779550 :         len = TREE_VEC_LENGTH (parm);
   26636    134779550 :         argslen = TREE_VEC_LENGTH (arg);
   26637              : 
   26638              :         /* Check for pack expansions in the parameters.  */
   26639    393363075 :         for (i = 0; i < len; ++i)
   26640              :           {
   26641    258583538 :             if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
   26642              :               {
   26643      6915344 :                 if (i == len - 1)
   26644              :                   /* We can unify against something with a trailing
   26645              :                      parameter pack.  */
   26646              :                   parm_variadic_p = 1;
   26647              :                 else
   26648              :                   /* [temp.deduct.type]/9: If the template argument list of
   26649              :                      P contains a pack expansion that is not the last
   26650              :                      template argument, the entire template argument list
   26651              :                      is a non-deduced context.  */
   26652    364662892 :                   return unify_success (explain_p);
   26653              :               }
   26654              :           }
   26655              : 
   26656              :         /* If we don't have enough arguments to satisfy the parameters
   26657              :            (not counting the pack expression at the end), or we have
   26658              :            too many arguments for a parameter list that doesn't end in
   26659              :            a pack expression, we can't unify.  */
   26660    134779537 :         if (parm_variadic_p
   26661    134779537 :             ? argslen < len - parm_variadic_p
   26662              :             : argslen != len)
   26663      3767163 :           return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
   26664              : 
   26665              :         /* Unify all of the parameters that precede the (optional)
   26666              :            pack expression.  */
   26667    325535620 :         for (i = 0; i < len - parm_variadic_p; ++i)
   26668              :           {
   26669    228243405 :             RECUR_AND_CHECK_FAILURE (tparms, targs,
   26670              :                                      TREE_VEC_ELT (parm, i),
   26671              :                                      TREE_VEC_ELT (arg, i),
   26672              :                                      UNIFY_ALLOW_NONE, explain_p);
   26673              :           }
   26674     97292215 :         if (parm_variadic_p)
   26675      6598298 :           return unify_pack_expansion (tparms, targs, parm, arg,
   26676              :                                        DEDUCE_EXACT,
   26677      6598298 :                                        /*subr=*/true, explain_p);
   26678    364662892 :         return unify_success (explain_p);
   26679              :       }
   26680              : 
   26681    420668493 :     case RECORD_TYPE:
   26682    420668493 :     case UNION_TYPE:
   26683    420668493 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26684    189694178 :         return unify_type_mismatch (explain_p, parm, arg);
   26685              : 
   26686    230974315 :       if (TYPE_PTRMEMFUNC_P (parm))
   26687              :         {
   26688         2254 :           if (!TYPE_PTRMEMFUNC_P (arg))
   26689          264 :             return unify_type_mismatch (explain_p, parm, arg);
   26690              : 
   26691         5970 :           return unify (tparms, targs,
   26692         1990 :                         TYPE_PTRMEMFUNC_FN_TYPE (parm),
   26693         1990 :                         TYPE_PTRMEMFUNC_FN_TYPE (arg),
   26694         1990 :                         strict, explain_p);
   26695              :         }
   26696    230972061 :       else if (TYPE_PTRMEMFUNC_P (arg))
   26697          359 :         return unify_type_mismatch (explain_p, parm, arg);
   26698              : 
   26699    230971702 :       if (CLASSTYPE_TEMPLATE_INFO (parm))
   26700              :         {
   26701    229184641 :           tree t = NULL_TREE;
   26702              : 
   26703    229184641 :           if (strict_in & UNIFY_ALLOW_DERIVED)
   26704              :             {
   26705              :               /* First, we try to unify the PARM and ARG directly.  */
   26706    195024524 :               t = try_class_unification (tparms, targs,
   26707              :                                          parm, arg, explain_p);
   26708              : 
   26709    195024524 :               if (!t)
   26710              :                 {
   26711              :                   /* Fallback to the special case allowed in
   26712              :                      [temp.deduct.call]:
   26713              : 
   26714              :                        If P is a class, and P has the form
   26715              :                        template-id, then A can be a derived class of
   26716              :                        the deduced A.  Likewise, if P is a pointer to
   26717              :                        a class of the form template-id, A can be a
   26718              :                        pointer to a derived class pointed to by the
   26719              :                        deduced A.  */
   26720    180473324 :                   enum template_base_result r;
   26721    180473324 :                   r = get_template_base (tparms, targs, parm, arg,
   26722              :                                          explain_p, &t);
   26723              : 
   26724    180473324 :                   if (!t)
   26725              :                     {
   26726              :                       /* Don't give the derived diagnostic if we're
   26727              :                          already dealing with the same template.  */
   26728    179782103 :                       bool same_template
   26729    179782103 :                         = (CLASSTYPE_TEMPLATE_INFO (arg)
   26730    179782103 :                            && (CLASSTYPE_TI_TEMPLATE (parm)
   26731    126681049 :                                == CLASSTYPE_TI_TEMPLATE (arg)));
   26732    179782103 :                       return unify_no_common_base (explain_p && !same_template,
   26733    179782103 :                                                    r, parm, arg);
   26734              :                     }
   26735              :                 }
   26736              :             }
   26737     34160117 :           else if (CLASSTYPE_TEMPLATE_INFO (arg)
   26738     34160117 :                    && (CLASSTYPE_TI_TEMPLATE (parm)
   26739     32958886 :                        == CLASSTYPE_TI_TEMPLATE (arg)))
   26740              :             /* Perhaps PARM is something like S<U> and ARG is S<int>.
   26741              :                Then, we should unify `int' and `U'.  */
   26742     30800700 :             t = arg;
   26743              :           else
   26744              :             /* There's no chance of unification succeeding.  */
   26745    232544055 :             return unify_type_mismatch (explain_p, parm, arg);
   26746              : 
   26747     46043121 :           if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
   26748     46043118 :             return unify (tparms, targs,
   26749     46043118 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   26750     46043118 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)),
   26751     46043118 :                           UNIFY_ALLOW_NONE, explain_p);
   26752            3 :           gcc_checking_assert (t == arg);
   26753              :         }
   26754              : 
   26755      1787064 :       if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
   26756      1785885 :         return unify_type_mismatch (explain_p, parm, arg);
   26757    364662892 :       return unify_success (explain_p);
   26758              : 
   26759       274600 :     case METHOD_TYPE:
   26760       274600 :     case FUNCTION_TYPE:
   26761       274600 :       {
   26762       274600 :         unsigned int nargs;
   26763       274600 :         tree *args;
   26764       274600 :         tree a;
   26765       274600 :         unsigned int i;
   26766              : 
   26767       274600 :         if (TREE_CODE (arg) != TREE_CODE (parm))
   26768         1539 :           return unify_type_mismatch (explain_p, parm, arg);
   26769              : 
   26770              :         /* CV qualifications for methods can never be deduced, they must
   26771              :            match exactly.  We need to check them explicitly here,
   26772              :            because type_unification_real treats them as any other
   26773              :            cv-qualified parameter.  */
   26774       273061 :         if (TREE_CODE (parm) == METHOD_TYPE
   26775       275051 :             && (!check_cv_quals_for_unify
   26776         1990 :                 (UNIFY_ALLOW_NONE,
   26777              :                  class_of_this_parm (arg),
   26778              :                  class_of_this_parm (parm))))
   26779         1204 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   26780       271857 :         if (TREE_CODE (arg) == FUNCTION_TYPE
   26781       271857 :             && type_memfn_quals (parm) != type_memfn_quals (arg))
   26782          795 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   26783       271062 :         if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
   26784          570 :           return unify_type_mismatch (explain_p, parm, arg);
   26785              : 
   26786       270492 :         RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
   26787              :                                  TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
   26788              : 
   26789       269770 :         nargs = list_length (TYPE_ARG_TYPES (arg));
   26790       269770 :         args = XALLOCAVEC (tree, nargs);
   26791       269770 :         for (a = TYPE_ARG_TYPES (arg), i = 0;
   26792       926063 :              a != NULL_TREE && a != void_list_node;
   26793       656293 :              a = TREE_CHAIN (a), ++i)
   26794       656293 :           args[i] = TREE_VALUE (a);
   26795       269770 :         nargs = i;
   26796              : 
   26797       269770 :         if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
   26798              :                                    args, nargs, 1, DEDUCE_EXACT,
   26799              :                                    NULL, explain_p))
   26800              :           return 1;
   26801              : 
   26802       269579 :         if (flag_noexcept_type)
   26803              :           {
   26804       266051 :             tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
   26805       266051 :             tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
   26806       266051 :             if (pspec == NULL_TREE) pspec = noexcept_false_spec;
   26807       266051 :             if (aspec == NULL_TREE) aspec = noexcept_false_spec;
   26808       532102 :             if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
   26809       532102 :                 && uses_template_parms (TREE_PURPOSE (pspec)))
   26810          384 :               RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
   26811              :                                        TREE_PURPOSE (aspec),
   26812              :                                        UNIFY_ALLOW_NONE, explain_p);
   26813              :             else
   26814              :               {
   26815       265667 :                 bool pn = nothrow_spec_p (pspec);
   26816       265667 :                 bool an = nothrow_spec_p (aspec);
   26817              :                 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
   26818              :                    /more/ noexcept, since function pointer conversions are the
   26819              :                    reverse of qualification conversions.  */
   26820       265667 :                 if (an == pn
   26821       220240 :                     || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   26822       220234 :                     || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   26823              :                   /* OK.  */;
   26824              :                 else
   26825           50 :                   return unify_type_mismatch (explain_p, parm, arg);
   26826              :               }
   26827              :           }
   26828       269529 :         if (flag_tm)
   26829              :           {
   26830              :             /* As for noexcept.  */
   26831           62 :             bool pn = tx_safe_fn_type_p (parm);
   26832           62 :             bool an = tx_safe_fn_type_p (arg);
   26833           62 :             if (an == pn
   26834            4 :                 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   26835            4 :                 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   26836              :               /* OK.  */;
   26837              :             else
   26838            1 :               return unify_type_mismatch (explain_p, parm, arg);
   26839              :           }
   26840              : 
   26841              :         return 0;
   26842              :       }
   26843              : 
   26844        41843 :     case OFFSET_TYPE:
   26845              :       /* Unify a pointer to member with a pointer to member function, which
   26846              :          deduces the type of the member as a function type. */
   26847        41843 :       if (TYPE_PTRMEMFUNC_P (arg))
   26848              :         {
   26849              :           /* Check top-level cv qualifiers */
   26850        18848 :           if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
   26851           24 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   26852              : 
   26853        18824 :           RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   26854              :                                    TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
   26855              :                                    UNIFY_ALLOW_NONE, explain_p);
   26856              : 
   26857              :           /* Determine the type of the function we are unifying against. */
   26858        18824 :           tree fntype = static_fn_type (arg);
   26859              : 
   26860        18824 :           return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
   26861              :         }
   26862              : 
   26863        22995 :       if (TREE_CODE (arg) != OFFSET_TYPE)
   26864        21444 :         return unify_type_mismatch (explain_p, parm, arg);
   26865         1551 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   26866              :                                TYPE_OFFSET_BASETYPE (arg),
   26867              :                                UNIFY_ALLOW_NONE, explain_p);
   26868         1551 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26869         1551 :                     strict, explain_p);
   26870              : 
   26871            0 :     case CONST_DECL:
   26872              :       /* CONST_DECL should already have been folded to its DECL_INITIAL.  */
   26873            0 :       gcc_unreachable ();
   26874              : 
   26875         2305 :     case FIELD_DECL:
   26876         2305 :     case FUNCTION_DECL:
   26877         2305 :     case TEMPLATE_DECL:
   26878              :       /* Matched cases are handled by the ARG == PARM test above.  */
   26879         2305 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   26880              : 
   26881            0 :     case VAR_DECL:
   26882              :       /* We might get a variable as a non-type template argument in parm if the
   26883              :          corresponding parameter is type-dependent.  Make any necessary
   26884              :          adjustments based on whether arg is a reference.  */
   26885            0 :       if (CONSTANT_CLASS_P (arg))
   26886            0 :         parm = fold_non_dependent_expr (parm, complain);
   26887            0 :       else if (REFERENCE_REF_P (arg))
   26888              :         {
   26889            0 :           tree sub = TREE_OPERAND (arg, 0);
   26890            0 :           STRIP_NOPS (sub);
   26891            0 :           if (TREE_CODE (sub) == ADDR_EXPR)
   26892            0 :             arg = TREE_OPERAND (sub, 0);
   26893              :         }
   26894              :       /* Now use the normal expression code to check whether they match.  */
   26895            0 :       goto expr;
   26896              : 
   26897     12579247 :     case TYPE_ARGUMENT_PACK:
   26898     12579247 :     case NONTYPE_ARGUMENT_PACK:
   26899     12579247 :       return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
   26900     25158494 :                     ARGUMENT_PACK_ARGS (arg), strict, explain_p);
   26901              : 
   26902              :     case TYPEOF_TYPE:
   26903              :     case DECLTYPE_TYPE:
   26904              :     case TRAIT_TYPE:
   26905              :     case PACK_INDEX_TYPE:
   26906              :       /* These are non-deduced contexts.  */
   26907    364662892 :       return unify_success (explain_p);
   26908              : 
   26909              :     case ERROR_MARK:
   26910              :       /* Unification fails if we hit an error node.  */
   26911    264531790 :       return unify_invalid (explain_p);
   26912              : 
   26913           58 :     case INDIRECT_REF:
   26914           58 :       if (REFERENCE_REF_P (parm))
   26915              :         {
   26916           58 :           bool pexp = PACK_EXPANSION_P (arg);
   26917           58 :           if (pexp)
   26918            3 :             arg = PACK_EXPANSION_PATTERN (arg);
   26919           58 :           if (REFERENCE_REF_P (arg))
   26920           58 :             arg = TREE_OPERAND (arg, 0);
   26921           58 :           if (pexp)
   26922            3 :             arg = make_pack_expansion (arg, complain);
   26923           58 :           return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
   26924           58 :                         strict, explain_p);
   26925              :         }
   26926              :       /* FALLTHRU */
   26927              : 
   26928         1055 :     default:
   26929              :       /* An unresolved overload is a nondeduced context.  */
   26930         1055 :       if (is_overloaded_fn (parm) || type_unknown_p (parm))
   26931    364662892 :         return unify_success (explain_p);
   26932         1055 :       gcc_assert (EXPR_P (parm)
   26933              :                   || TREE_CODE (parm) == CONSTRUCTOR
   26934              :                   || TREE_CODE (parm) == LAMBDA_EXPR
   26935              :                   || TREE_CODE (parm) == TRAIT_EXPR);
   26936         1055 :     expr:
   26937              :       /* We must be looking at an expression.  This can happen with
   26938              :          something like:
   26939              : 
   26940              :            template <int I>
   26941              :            void foo(S<I>, S<I + 2>);
   26942              : 
   26943              :          or
   26944              : 
   26945              :            template<typename T>
   26946              :            void foo(A<T, T{}>);
   26947              : 
   26948              :          This is a "non-deduced context":
   26949              : 
   26950              :            [deduct.type]
   26951              : 
   26952              :            The non-deduced contexts are:
   26953              : 
   26954              :            --A non-type template argument or an array bound in which
   26955              :              a subexpression references a template parameter.
   26956              : 
   26957              :          In these cases, we assume deduction succeeded, but don't
   26958              :          actually infer any unifications.  */
   26959              : 
   26960         1055 :       if (!uses_template_parms (parm)
   26961         1055 :           && !template_args_equal (parm, arg))
   26962           11 :         return unify_expression_unequal (explain_p, parm, arg);
   26963              :       else
   26964         1044 :         return unify_success (explain_p);
   26965              :     }
   26966              : }
   26967              : #undef RECUR_AND_CHECK_FAILURE
   26968              : 
   26969              : /* Note that DECL can be defined in this translation unit, if
   26970              :    required.  */
   26971              : 
   26972              : static void
   26973     21756474 : mark_definable (tree decl)
   26974              : {
   26975     21756474 :   tree clone;
   26976     21756474 :   DECL_NOT_REALLY_EXTERN (decl) = 1;
   26977     28017940 :   FOR_EACH_CLONE (clone, decl)
   26978      6261466 :     DECL_NOT_REALLY_EXTERN (clone) = 1;
   26979     21756474 : }
   26980              : 
   26981              : /* DECL is an explicit instantiation definition, ensure that it will
   26982              :    be written out here and that it won't clash with other instantiations
   26983              :    in other translation units.  */
   26984              : 
   26985              : void
   26986        31073 : setup_explicit_instantiation_definition_linkage (tree decl)
   26987              : {
   26988        31073 :   mark_definable (decl);
   26989        31073 :   mark_needed (decl);
   26990              :   /* Always make artificials weak.  */
   26991        31073 :   if (DECL_ARTIFICIAL (decl) && flag_weak)
   26992            0 :     comdat_linkage (decl);
   26993              :   /* We also want to put explicit instantiations in linkonce sections.  */
   26994        31073 :   else if (TREE_PUBLIC (decl))
   26995        31054 :     maybe_make_one_only (decl);
   26996        31073 : }
   26997              : 
   26998              : /* Called if RESULT is explicitly instantiated, or is a member of an
   26999              :    explicitly instantiated class.  */
   27000              : 
   27001              : void
   27002     23269592 : mark_decl_instantiated (tree result, int extern_p)
   27003              : {
   27004     23269592 :   SET_DECL_EXPLICIT_INSTANTIATION (result);
   27005              : 
   27006              :   /* consteval functions are never emitted.  */
   27007     23269592 :   if (TREE_CODE (result) == FUNCTION_DECL
   27008     46191195 :       && DECL_IMMEDIATE_FUNCTION_P (result))
   27009              :     return;
   27010              : 
   27011              :   /* For anonymous namespace we don't need to do anything.  */
   27012     23269586 :   if (decl_internal_context_p (result))
   27013              :     {
   27014          993 :       gcc_assert (!TREE_PUBLIC (result));
   27015              :       return;
   27016              :     }
   27017              : 
   27018     23268593 :   if (TREE_CODE (result) != FUNCTION_DECL)
   27019              :     /* The TREE_PUBLIC flag for function declarations will have been
   27020              :        set correctly by tsubst.  */
   27021       347981 :     TREE_PUBLIC (result) = 1;
   27022              : 
   27023     23268593 :   if (extern_p)
   27024              :     {
   27025     23237556 :       DECL_EXTERNAL (result) = 1;
   27026     23237556 :       DECL_NOT_REALLY_EXTERN (result) = 0;
   27027              :     }
   27028              :   else
   27029              :     {
   27030        31037 :       set_instantiating_module (result);
   27031        31037 :       setup_explicit_instantiation_definition_linkage (result);
   27032        31037 :       if (TREE_CODE (result) == FUNCTION_DECL
   27033        31037 :           && DECL_TEMPLATE_INSTANTIATED (result))
   27034              :         /* If the function has already been instantiated, clear DECL_EXTERNAL,
   27035              :            since start_preparsed_function wouldn't have if we had an earlier
   27036              :            extern explicit instantiation.  */
   27037           98 :         DECL_EXTERNAL (result) = 0;
   27038              :     }
   27039              : 
   27040              :   /* If EXTERN_P, then this function will not be emitted -- unless
   27041              :      followed by an explicit instantiation, at which point its linkage
   27042              :      will be adjusted.  If !EXTERN_P, then this function will be
   27043              :      emitted here.  In neither circumstance do we want
   27044              :      import_export_decl to adjust the linkage.  */
   27045     23268593 :   DECL_INTERFACE_KNOWN (result) = 1;
   27046              : }
   27047              : 
   27048              : /* Subroutine of more_specialized_fn: check whether TARGS is missing any
   27049              :    important template arguments.  If any are missing, we check whether
   27050              :    they're important by using error_mark_node for substituting into any
   27051              :    args that were used for partial ordering (the ones between ARGS and END)
   27052              :    and seeing if it bubbles up.  */
   27053              : 
   27054              : static bool
   27055      6899534 : check_undeduced_parms (tree targs, tree args, tree end)
   27056              : {
   27057      6899534 :   bool found = false;
   27058     23037784 :   for (tree& targ : tree_vec_range (targs))
   27059     16138250 :     if (targ == NULL_TREE)
   27060              :       {
   27061      1676014 :         found = true;
   27062      1676014 :         targ = error_mark_node;
   27063              :       }
   27064      6899534 :   if (found)
   27065              :     {
   27066      1674752 :       tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
   27067      1674752 :       if (substed == error_mark_node)
   27068              :         return true;
   27069              :     }
   27070              :   return false;
   27071              : }
   27072              : 
   27073              : /* Given two function templates PAT1 and PAT2, return:
   27074              : 
   27075              :    1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   27076              :    -1 if PAT2 is more specialized than PAT1.
   27077              :    0 if neither is more specialized.
   27078              : 
   27079              :    LEN indicates the number of parameters we should consider
   27080              :    (defaulted parameters should not be considered).
   27081              : 
   27082              :    The 1998 std underspecified function template partial ordering, and
   27083              :    DR214 addresses the issue.  We take pairs of arguments, one from
   27084              :    each of the templates, and deduce them against each other.  One of
   27085              :    the templates will be more specialized if all the *other*
   27086              :    template's arguments deduce against its arguments and at least one
   27087              :    of its arguments *does* *not* deduce against the other template's
   27088              :    corresponding argument.  Deduction is done as for class templates.
   27089              :    The arguments used in deduction have reference and top level cv
   27090              :    qualifiers removed.  Iff both arguments were originally reference
   27091              :    types *and* deduction succeeds in both directions, an lvalue reference
   27092              :    wins against an rvalue reference and otherwise the template
   27093              :    with the more cv-qualified argument wins for that pairing (if
   27094              :    neither is more cv-qualified, they both are equal).  Unlike regular
   27095              :    deduction, after all the arguments have been deduced in this way,
   27096              :    we do *not* verify the deduced template argument values can be
   27097              :    substituted into non-deduced contexts.
   27098              : 
   27099              :    The logic can be a bit confusing here, because we look at deduce1 and
   27100              :    targs1 to see if pat2 is at least as specialized, and vice versa; if we
   27101              :    can find template arguments for pat1 to make arg1 look like arg2, that
   27102              :    means that arg2 is at least as specialized as arg1.  */
   27103              : 
   27104              : int
   27105      4471118 : more_specialized_fn (tree pat1, tree pat2, int len)
   27106              : {
   27107      4471118 :   tree decl1 = DECL_TEMPLATE_RESULT (pat1);
   27108      4471118 :   tree decl2 = DECL_TEMPLATE_RESULT (pat2);
   27109      4471118 :   tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
   27110      4471118 :   tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
   27111      4471118 :   tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
   27112      4471118 :   tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
   27113      4471118 :   tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
   27114      4471118 :   tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
   27115      4471118 :   tree origs1, origs2;
   27116      4471118 :   bool lose1 = false;
   27117      4471118 :   bool lose2 = false;
   27118              : 
   27119              :   /* C++17 [temp.func.order]/3 (CWG532)
   27120              : 
   27121              :      If only one of the function templates M is a non-static member of some
   27122              :      class A, M is considered to have a new first parameter inserted in its
   27123              :      function parameter list. Given cv as the cv-qualifiers of M (if any), the
   27124              :      new parameter is of type "rvalue reference to cv A" if the optional
   27125              :      ref-qualifier of M is && or if M has no ref-qualifier and the first
   27126              :      parameter of the other template has rvalue reference type. Otherwise, the
   27127              :      new parameter is of type "lvalue reference to cv A".  */
   27128              : 
   27129      4471118 :   if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2))
   27130              :     {
   27131              :       /* Note C++20 DR2445 extended the above to static member functions, but
   27132              :          I think the old G++ behavior of just skipping the object
   27133              :          parameter when comparing to a static member function was better, so
   27134              :          let's stick with that for now.  This is CWG2834.  --jason 2023-12 */
   27135          530 :       if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1))
   27136              :         {
   27137           32 :           len--; /* LEN is the number of significant arguments for DECL1 */
   27138           32 :           args1 = TREE_CHAIN (args1);
   27139              :         }
   27140          498 :       else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2))
   27141           20 :         args2 = TREE_CHAIN (args2);
   27142              :     }
   27143      4470588 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   27144      4470588 :            && DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   27145              :     {
   27146              :       /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
   27147              :          would break e.g.  cpp1y/lambda-generic-variadic5.C.  */
   27148        14667 :       len--;
   27149        14667 :       args1 = TREE_CHAIN (args1);
   27150        14667 :       args2 = TREE_CHAIN (args2);
   27151              :     }
   27152      4455921 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   27153      4455921 :            || DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   27154              :     {
   27155              :       /* The other is a non-member or explicit object member function;
   27156              :          rewrite the implicit object parameter to a reference.  */
   27157          125 :       tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1;
   27158          125 :       tree &nsargs = ns == decl2 ? args2 : args1;
   27159          125 :       tree obtype = TREE_TYPE (TREE_VALUE (nsargs));
   27160              : 
   27161          125 :       nsargs = TREE_CHAIN (nsargs);
   27162              : 
   27163          125 :       cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns));
   27164          125 :       if (rqual == REF_QUAL_NONE)
   27165              :         {
   27166           93 :           tree otherfirst = ns == decl1 ? args2 : args1;
   27167           93 :           otherfirst = TREE_VALUE (otherfirst);
   27168           93 :           if (TREE_CODE (otherfirst) == REFERENCE_TYPE
   27169           93 :               && TYPE_REF_IS_RVALUE (otherfirst))
   27170              :             rqual = REF_QUAL_RVALUE;
   27171              :         }
   27172          125 :       obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE);
   27173          125 :       nsargs = tree_cons (NULL_TREE, obtype, nsargs);
   27174              :     }
   27175              : 
   27176              :   /* If only one is a conversion operator, they are unordered.  */
   27177      4471118 :   if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
   27178              :     return 0;
   27179              : 
   27180              :   /* Consider the return type for a conversion function */
   27181      4471105 :   if (DECL_CONV_FN_P (decl1))
   27182              :     {
   27183            9 :       args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
   27184            9 :       args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
   27185            9 :       len++;
   27186              :     }
   27187              : 
   27188      4471105 :   processing_template_decl++;
   27189              : 
   27190      4471105 :   origs1 = args1;
   27191      4471105 :   origs2 = args2;
   27192              : 
   27193      4471105 :   while (len--
   27194              :          /* Stop when an ellipsis is seen.  */
   27195     11507591 :          && args1 != NULL_TREE && args2 != NULL_TREE)
   27196              :     {
   27197      7101049 :       tree arg1 = TREE_VALUE (args1);
   27198      7101049 :       tree arg2 = TREE_VALUE (args2);
   27199      7101049 :       int deduce1, deduce2;
   27200      7101049 :       int quals1 = -1;
   27201      7101049 :       int quals2 = -1;
   27202      7101049 :       int ref1 = 0;
   27203      7101049 :       int ref2 = 0;
   27204              : 
   27205      7101049 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   27206         2608 :           && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27207              :         {
   27208              :           /* When both arguments are pack expansions, we need only
   27209              :              unify the patterns themselves.  */
   27210          329 :           arg1 = PACK_EXPANSION_PATTERN (arg1);
   27211          329 :           arg2 = PACK_EXPANSION_PATTERN (arg2);
   27212              : 
   27213              :           /* This is the last comparison we need to do.  */
   27214              :           len = 0;
   27215              :         }
   27216              : 
   27217      7101049 :       if (TYPE_REF_P (arg1))
   27218              :         {
   27219      5111523 :           ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
   27220      5111523 :           arg1 = TREE_TYPE (arg1);
   27221      5111523 :           quals1 = cp_type_quals (arg1);
   27222              :         }
   27223              : 
   27224      7101049 :       if (TYPE_REF_P (arg2))
   27225              :         {
   27226      5238782 :           ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
   27227      5238782 :           arg2 = TREE_TYPE (arg2);
   27228      5238782 :           quals2 = cp_type_quals (arg2);
   27229              :         }
   27230              : 
   27231      7101049 :       arg1 = TYPE_MAIN_VARIANT (arg1);
   27232      7101049 :       arg2 = TYPE_MAIN_VARIANT (arg2);
   27233              : 
   27234      7101049 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
   27235              :         {
   27236         2279 :           int i, len2 = remaining_arguments (args2);
   27237         2279 :           tree parmvec = make_tree_vec (1);
   27238         2279 :           tree argvec = make_tree_vec (len2);
   27239         2279 :           tree ta = args2;
   27240              : 
   27241              :           /* Setup the parameter vector, which contains only ARG1.  */
   27242         2279 :           TREE_VEC_ELT (parmvec, 0) = arg1;
   27243              : 
   27244              :           /* Setup the argument vector, which contains the remaining
   27245              :              arguments.  */
   27246         5326 :           for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
   27247         3047 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   27248              : 
   27249         2279 :           deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
   27250              :                                            argvec, DEDUCE_EXACT,
   27251              :                                            /*subr=*/true, /*explain_p=*/false)
   27252              :                      == 0);
   27253              : 
   27254              :           /* We cannot deduce in the other direction, because ARG1 is
   27255              :              a pack expansion but ARG2 is not.  */
   27256         2279 :           deduce2 = 0;
   27257              :         }
   27258      7098770 :       else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27259              :         {
   27260          806 :           int i, len1 = remaining_arguments (args1);
   27261          806 :           tree parmvec = make_tree_vec (1);
   27262          806 :           tree argvec = make_tree_vec (len1);
   27263          806 :           tree ta = args1;
   27264              : 
   27265              :           /* Setup the parameter vector, which contains only ARG1.  */
   27266          806 :           TREE_VEC_ELT (parmvec, 0) = arg2;
   27267              : 
   27268              :           /* Setup the argument vector, which contains the remaining
   27269              :              arguments.  */
   27270         2240 :           for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
   27271         1434 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   27272              : 
   27273          806 :           deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
   27274              :                                            argvec, DEDUCE_EXACT,
   27275              :                                            /*subr=*/true, /*explain_p=*/false)
   27276              :                      == 0);
   27277              : 
   27278              :           /* We cannot deduce in the other direction, because ARG2 is
   27279              :              a pack expansion but ARG1 is not.*/
   27280          806 :           deduce1 = 0;
   27281              :         }
   27282              : 
   27283              :       else
   27284              :         {
   27285              :           /* The normal case, where neither argument is a pack
   27286              :              expansion.  */
   27287      7097964 :           deduce1 = (unify (tparms1, targs1, arg1, arg2,
   27288              :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   27289      7097964 :                      == 0);
   27290      7097964 :           deduce2 = (unify (tparms2, targs2, arg2, arg1,
   27291              :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   27292      7097964 :                      == 0);
   27293              :         }
   27294              : 
   27295              :       /* If we couldn't deduce arguments for tparms1 to make arg1 match
   27296              :          arg2, then arg2 is not as specialized as arg1.  */
   27297      7101049 :       if (!deduce1)
   27298              :         lose2 = true;
   27299      7101049 :       if (!deduce2)
   27300       139195 :         lose1 = true;
   27301              : 
   27302              :       /* "If, for a given type, deduction succeeds in both directions
   27303              :          (i.e., the types are identical after the transformations above)
   27304              :          and both P and A were reference types (before being replaced with
   27305              :          the type referred to above):
   27306              :          - if the type from the argument template was an lvalue reference and
   27307              :          the type from the parameter template was not, the argument type is
   27308              :          considered to be more specialized than the other; otherwise,
   27309              :          - if the type from the argument template is more cv-qualified
   27310              :          than the type from the parameter template (as described above),
   27311              :          the argument type is considered to be more specialized than the other;
   27312              :          otherwise,
   27313              :          - neither type is more specialized than the other."  */
   27314              : 
   27315      7101049 :       if (deduce1 && deduce2)
   27316              :         {
   27317      5098402 :           if (ref1 && ref2 && ref1 != ref2)
   27318              :             {
   27319            3 :               if (ref1 > ref2)
   27320              :                 lose1 = true;
   27321              :               else
   27322            3 :                 lose2 = true;
   27323              :             }
   27324      5098399 :           else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
   27325              :             {
   27326         1015 :               if ((quals1 & quals2) == quals2)
   27327           21 :                 lose2 = true;
   27328         1015 :               if ((quals1 & quals2) == quals1)
   27329          994 :                 lose1 = true;
   27330              :             }
   27331              :         }
   27332              : 
   27333      7101049 :       if (lose1 && lose2)
   27334              :         /* We've failed to deduce something in either direction.
   27335              :            These must be unordered.  */
   27336              :         break;
   27337              : 
   27338      7036486 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   27339      7036447 :           || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27340              :         /* We have already processed all of the arguments in our
   27341              :            handing of the pack expansion type.  */
   27342          187 :         len = 0;
   27343              : 
   27344      7036486 :       args1 = TREE_CHAIN (args1);
   27345      7036486 :       args2 = TREE_CHAIN (args2);
   27346              :     }
   27347              : 
   27348              :   /* "In most cases, all template parameters must have values in order for
   27349              :      deduction to succeed, but for partial ordering purposes a template
   27350              :      parameter may remain without a value provided it is not used in the
   27351              :      types being used for partial ordering."
   27352              : 
   27353              :      Thus, if we are missing any of the targs1 we need to substitute into
   27354              :      origs1, then pat2 is not as specialized as pat1.  This can happen when
   27355              :      there is a nondeduced context.  */
   27356      4471105 :   if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
   27357              :     lose2 = true;
   27358      4471105 :   if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
   27359              :     lose1 = true;
   27360              : 
   27361      4471105 :   processing_template_decl--;
   27362              : 
   27363              :   /* If both deductions succeed, the partial ordering selects the more
   27364              :      constrained template.  */
   27365              :   /* P2113: If the corresponding template-parameters of the
   27366              :      template-parameter-lists are not equivalent ([temp.over.link]) or if
   27367              :      the function parameters that positionally correspond between the two
   27368              :      templates are not of the same type, neither template is more
   27369              :      specialized than the other.  */
   27370      4471105 :   if (!lose1 && !lose2
   27371      2492977 :       && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
   27372      2492977 :                               DECL_TEMPLATE_PARMS (pat2))
   27373      6963960 :       && compparms (origs1, origs2))
   27374              :     {
   27375      2489134 :       int winner = more_constrained (decl1, decl2);
   27376      2489134 :       if (winner > 0)
   27377              :         lose2 = true;
   27378      1682890 :       else if (winner < 0)
   27379       292529 :         lose1 = true;
   27380              :     }
   27381              : 
   27382              :   /* All things being equal, if the next argument is a pack expansion
   27383              :      for one function but not for the other, prefer the
   27384              :      non-variadic function.  FIXME this is bogus; see c++/41958.  */
   27385      4471105 :   if (lose1 == lose2
   27386      1458779 :       && args1 && TREE_VALUE (args1)
   27387      5929878 :       && args2 && TREE_VALUE (args2))
   27388              :     {
   27389      1458764 :       lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
   27390      1458764 :       lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
   27391              :     }
   27392              : 
   27393      4471105 :   if (lose1 == lose2)
   27394              :     return 0;
   27395      3016054 :   else if (!lose1)
   27396              :     return 1;
   27397              :   else
   27398       362780 :     return -1;
   27399              : }
   27400              : 
   27401              : /* Determine which of two partial specializations of TMPL is more
   27402              :    specialized.
   27403              : 
   27404              :    PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
   27405              :    to the first partial specialization.  The TREE_PURPOSE is the
   27406              :    innermost set of template parameters for the partial
   27407              :    specialization.  PAT2 is similar, but for the second template.
   27408              : 
   27409              :    Return 1 if the first partial specialization is more specialized;
   27410              :    -1 if the second is more specialized; 0 if neither is more
   27411              :    specialized.
   27412              : 
   27413              :    See [temp.class.order] for information about determining which of
   27414              :    two templates is more specialized.  */
   27415              : 
   27416              : static int
   27417      1628788 : more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
   27418              : {
   27419      1628788 :   tree targs;
   27420      1628788 :   int winner = 0;
   27421      1628788 :   bool any_deductions = false;
   27422              : 
   27423      1628788 :   tree tmpl1 = TREE_VALUE (pat1);
   27424      1628788 :   tree tmpl2 = TREE_VALUE (pat2);
   27425      1628788 :   tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
   27426      1628788 :   tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
   27427              : 
   27428              :   /* Just like what happens for functions, if we are ordering between
   27429              :      different template specializations, we may encounter dependent
   27430              :      types in the arguments, and we need our dependency check functions
   27431              :      to behave correctly.  */
   27432      1628788 :   ++processing_template_decl;
   27433      1628788 :   targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
   27434      1628788 :   if (targs)
   27435              :     {
   27436       228666 :       --winner;
   27437       228666 :       any_deductions = true;
   27438              :     }
   27439              : 
   27440      1628788 :   targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
   27441      1628788 :   if (targs)
   27442              :     {
   27443      1405397 :       ++winner;
   27444      1405397 :       any_deductions = true;
   27445              :     }
   27446      1628788 :   --processing_template_decl;
   27447              : 
   27448              :   /* If both deductions succeed, the partial ordering selects the more
   27449              :      constrained template.  */
   27450      1628788 :   if (!winner && any_deductions)
   27451         5356 :     winner = more_constrained (tmpl1, tmpl2);
   27452              : 
   27453              :   /* In the case of a tie where at least one of the templates
   27454              :      has a parameter pack at the end, the template with the most
   27455              :      non-packed parameters wins.  */
   27456      1628788 :   if (winner == 0
   27457      1628788 :       && any_deductions
   27458      1628798 :       && (template_args_variadic_p (TREE_PURPOSE (pat1))
   27459           10 :           || template_args_variadic_p (TREE_PURPOSE (pat2))))
   27460              :     {
   27461            0 :       tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
   27462            0 :       tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
   27463            0 :       int len1 = TREE_VEC_LENGTH (args1);
   27464            0 :       int len2 = TREE_VEC_LENGTH (args2);
   27465              : 
   27466              :       /* We don't count the pack expansion at the end.  */
   27467            0 :       if (template_args_variadic_p (TREE_PURPOSE (pat1)))
   27468            0 :         --len1;
   27469            0 :       if (template_args_variadic_p (TREE_PURPOSE (pat2)))
   27470            0 :         --len2;
   27471              : 
   27472            0 :       if (len1 > len2)
   27473              :         return 1;
   27474            0 :       else if (len1 < len2)
   27475              :         return -1;
   27476              :     }
   27477              : 
   27478              :   return winner;
   27479              : }
   27480              : 
   27481              : /* Return the template arguments that will produce the function signature
   27482              :    DECL from the function template FN, with the explicit template
   27483              :    arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
   27484              :    also match.  Return NULL_TREE if no satisfactory arguments could be
   27485              :    found.  */
   27486              : 
   27487              : static tree
   27488     14595284 : get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
   27489              : {
   27490     14595284 :   int ntparms = DECL_NTPARMS (fn);
   27491     14595284 :   tree targs = make_tree_vec (ntparms);
   27492     14595284 :   tree decl_type = TREE_TYPE (decl);
   27493     14595284 :   tree decl_arg_types;
   27494     14595284 :   tree *args;
   27495     14595284 :   unsigned int nargs, ix;
   27496     14595284 :   tree arg;
   27497              : 
   27498     14595284 :   gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
   27499              : 
   27500              :   /* Never do unification on the 'this' parameter.  */
   27501     14595284 :   decl_arg_types = skip_artificial_parms_for (decl,
   27502     14595284 :                                               TYPE_ARG_TYPES (decl_type));
   27503              : 
   27504     14595284 :   nargs = list_length (decl_arg_types);
   27505     14595284 :   args = XALLOCAVEC (tree, nargs);
   27506     14595284 :   for (arg = decl_arg_types, ix = 0;
   27507     57343514 :        arg != NULL_TREE;
   27508     42748230 :        arg = TREE_CHAIN (arg), ++ix)
   27509     42748230 :     args[ix] = TREE_VALUE (arg);
   27510              : 
   27511     29190568 :   if (fn_type_unification (fn, explicit_args, targs,
   27512              :                            args, ix,
   27513            0 :                            (check_rettype || DECL_CONV_FN_P (fn)
   27514     14595284 :                             ? TREE_TYPE (decl_type) : NULL_TREE),
   27515              :                            DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
   27516              :                            /*explain_p=*/false,
   27517              :                            /*decltype*/false)
   27518     14595284 :       == error_mark_node)
   27519     12367036 :     return NULL_TREE;
   27520              : 
   27521              :   return targs;
   27522              : }
   27523              : 
   27524              : /* Return the innermost template arguments that, when applied to a partial
   27525              :    specialization SPEC_TMPL of TMPL, yield the ARGS.
   27526              : 
   27527              :    For example, suppose we have:
   27528              : 
   27529              :      template <class T, class U> struct S {};
   27530              :      template <class T> struct S<T*, int> {};
   27531              : 
   27532              :    Then, suppose we want to get `S<double*, int>'.  SPEC_TMPL will be the
   27533              :    partial specialization and the ARGS will be {double*, int}.  The resulting
   27534              :    vector will be {double}, indicating that `T' is bound to `double'.  */
   27535              : 
   27536              : static tree
   27537     50707938 : get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
   27538              : {
   27539     50707938 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
   27540     50707938 :   tree spec_args
   27541     50707938 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
   27542     50707938 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
   27543     50707938 :   tree deduced_args;
   27544     50707938 :   tree innermost_deduced_args;
   27545              : 
   27546     50707938 :   innermost_deduced_args = make_tree_vec (ntparms);
   27547    101415876 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   27548              :     {
   27549          114 :       deduced_args = copy_node (args);
   27550          228 :       SET_TMPL_ARGS_LEVEL (deduced_args,
   27551              :                            TMPL_ARGS_DEPTH (deduced_args),
   27552              :                            innermost_deduced_args);
   27553              :     }
   27554              :   else
   27555              :     deduced_args = innermost_deduced_args;
   27556              : 
   27557     50707938 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   27558     50707941 :  again:
   27559     50707941 :   if (unify (tparms, deduced_args,
   27560              :              INNERMOST_TEMPLATE_ARGS (spec_args),
   27561              :              INNERMOST_TEMPLATE_ARGS (args),
   27562              :              UNIFY_ALLOW_NONE, /*explain_p=*/false))
   27563              :     return NULL_TREE;
   27564              : 
   27565     64975520 :   for (i =  0; i < ntparms; ++i)
   27566     42522847 :     if (! TREE_VEC_ELT (innermost_deduced_args, i))
   27567              :       {
   27568           26 :         if (!tried_array_deduction)
   27569              :           {
   27570           19 :             try_array_deduction (tparms, innermost_deduced_args,
   27571              :                                  INNERMOST_TEMPLATE_ARGS (spec_args));
   27572           19 :             tried_array_deduction = true;
   27573           19 :             if (TREE_VEC_ELT (innermost_deduced_args, i))
   27574            3 :               goto again;
   27575              :           }
   27576           23 :         return NULL_TREE;
   27577              :       }
   27578              : 
   27579     22452673 :   if (!push_tinst_level (spec_tmpl, deduced_args))
   27580              :     {
   27581            0 :       excessive_deduction_depth = true;
   27582            0 :       return NULL_TREE;
   27583              :     }
   27584              : 
   27585              :   /* Verify that nondeduced template arguments agree with the type
   27586              :      obtained from argument deduction.
   27587              : 
   27588              :      For example:
   27589              : 
   27590              :        struct A { typedef int X; };
   27591              :        template <class T, class U> struct C {};
   27592              :        template <class T> struct C<T, typename T::X> {};
   27593              : 
   27594              :      Then with the instantiation `C<A, int>', we can deduce that
   27595              :      `T' is `A' but unify () does not check whether `typename T::X'
   27596              :      is `int'.  */
   27597     22452673 :   spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
   27598              : 
   27599     22452673 :   if (spec_args != error_mark_node)
   27600     22323393 :     spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
   27601              :                                        INNERMOST_TEMPLATE_ARGS (spec_args),
   27602              :                                        tmpl, tf_none, false);
   27603              : 
   27604     22452673 :   pop_tinst_level ();
   27605              : 
   27606     22452673 :   if (spec_args == error_mark_node
   27607              :       /* We only need to check the innermost arguments; the other
   27608              :          arguments will always agree.  */
   27609     22452673 :       || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
   27610              :                                      INNERMOST_TEMPLATE_ARGS (args)))
   27611       129426 :     return NULL_TREE;
   27612              : 
   27613              :   /* Now that we have bindings for all of the template arguments,
   27614              :      ensure that the arguments deduced for the template template
   27615              :      parameters have compatible template parameter lists.  See the use
   27616              :      of template_template_parm_bindings_ok_p in fn_type_unification
   27617              :      for more information.  */
   27618     22323247 :   if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
   27619              :     return NULL_TREE;
   27620              : 
   27621              :   return deduced_args;
   27622              : }
   27623              : 
   27624              : // Compare two function templates T1 and T2 by deducing bindings
   27625              : // from one against the other. If both deductions succeed, compare
   27626              : // constraints to see which is more constrained.
   27627              : static int
   27628        61691 : more_specialized_inst (tree t1, tree t2)
   27629              : {
   27630        61691 :   int fate = 0;
   27631        61691 :   int count = 0;
   27632              : 
   27633        61691 :   if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
   27634              :     {
   27635          193 :       --fate;
   27636          193 :       ++count;
   27637              :     }
   27638              : 
   27639        61691 :   if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
   27640              :     {
   27641        41136 :       ++fate;
   27642        41136 :       ++count;
   27643              :     }
   27644              : 
   27645              :   // If both deductions succeed, then one may be more constrained.
   27646        61691 :   if (count == 2 && fate == 0)
   27647          126 :     fate = more_constrained (t1, t2);
   27648              : 
   27649        61691 :   return fate;
   27650              : }
   27651              : 
   27652              : /* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
   27653              :    Return the TREE_LIST node with the most specialized template, if
   27654              :    any.  If there is no most specialized template, the error_mark_node
   27655              :    is returned.
   27656              : 
   27657              :    Note that this function does not look at, or modify, the
   27658              :    TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
   27659              :    returned is one of the elements of INSTANTIATIONS, callers may
   27660              :    store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
   27661              :    and retrieve it from the value returned.  */
   27662              : 
   27663              : tree
   27664        58210 : most_specialized_instantiation (tree templates)
   27665              : {
   27666        58210 :   tree fn, champ;
   27667              : 
   27668        58210 :   ++processing_template_decl;
   27669              : 
   27670        58210 :   champ = templates;
   27671        78825 :   for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
   27672              :     {
   27673        20645 :       gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
   27674        20645 :       int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
   27675        20645 :       if (fate == -1)
   27676              :         champ = fn;
   27677        20515 :       else if (!fate)
   27678              :         {
   27679              :           /* Equally specialized, move to next function.  If there
   27680              :              is no next function, nothing's most specialized.  */
   27681        20488 :           fn = TREE_CHAIN (fn);
   27682        20488 :           champ = fn;
   27683        20488 :           if (!fn)
   27684              :             break;
   27685              :         }
   27686              :     }
   27687              : 
   27688        58210 :   if (champ)
   27689              :     /* Now verify that champ is better than everything earlier in the
   27690              :        instantiation list.  */
   27691        99226 :     for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
   27692        41046 :       if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
   27693              :       {
   27694              :         champ = NULL_TREE;
   27695              :         break;
   27696              :       }
   27697              :     }
   27698              : 
   27699        58210 :   processing_template_decl--;
   27700              : 
   27701        58210 :   if (!champ)
   27702           30 :     return error_mark_node;
   27703              : 
   27704              :   return champ;
   27705              : }
   27706              : 
   27707              : /* If DECL is a specialization of some template, return the most
   27708              :    general such template.  Otherwise, returns NULL_TREE.
   27709              : 
   27710              :    For example, given:
   27711              : 
   27712              :      template <class T> struct S { template <class U> void f(U); };
   27713              : 
   27714              :    if TMPL is `template <class U> void S<int>::f(U)' this will return
   27715              :    the full template.  This function will not trace past partial
   27716              :    specializations, however.  For example, given in addition:
   27717              : 
   27718              :      template <class T> struct S<T*> { template <class U> void f(U); };
   27719              : 
   27720              :    if TMPL is `template <class U> void S<int*>::f(U)' this will return
   27721              :    `template <class T> template <class U> S<T*>::f(U)'.  */
   27722              : 
   27723              : tree
   27724   4289933270 : most_general_template (const_tree decl)
   27725              : {
   27726   4289933270 :   if (TREE_CODE (decl) != TEMPLATE_DECL)
   27727              :     {
   27728    878755509 :       if (tree tinfo = get_template_info (decl))
   27729    553549180 :         decl = TI_TEMPLATE (tinfo);
   27730              :       /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
   27731              :          template friend, or a FIELD_DECL for a capture pack.  */
   27732    878755509 :       if (TREE_CODE (decl) != TEMPLATE_DECL)
   27733              :         return NULL_TREE;
   27734              :     }
   27735              : 
   27736   3964724985 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   27737       509626 :     return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl));
   27738              : 
   27739              :   /* Look for more and more general templates.  */
   27740   4007259204 :   while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
   27741              :     {
   27742              :       /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
   27743              :          (See cp-tree.h for details.)  */
   27744     43044157 :       if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
   27745              :         break;
   27746              : 
   27747     86088314 :       if (CLASS_TYPE_P (TREE_TYPE (decl))
   27748      7954032 :           && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
   27749     50104742 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   27750              :         break;
   27751              : 
   27752              :       /* Stop if we run into an explicitly specialized class template.  */
   27753     86087690 :       if (!DECL_NAMESPACE_SCOPE_P (decl)
   27754     39940296 :           && DECL_CONTEXT (decl)
   27755     82984141 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
   27756              :         break;
   27757              : 
   27758     43043845 :       decl = DECL_TI_TEMPLATE (decl);
   27759              :     }
   27760              : 
   27761              :   return const_cast<tree> (decl);
   27762              : }
   27763              : 
   27764              : /* Return the most specialized of the template partial specializations
   27765              :    which can produce TARGET, a specialization of some class or variable
   27766              :    template.  The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
   27767              :    TEMPLATE_DECL node corresponding to the partial specialization, while
   27768              :    the TI_ARGS is the set of template arguments that must be substituted
   27769              :    into the template pattern in order to generate TARGET.  The result is
   27770              :    cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
   27771              :    RECHECKING is true.
   27772              : 
   27773              :    If the choice of partial specialization is ambiguous, a diagnostic
   27774              :    is issued, and the error_mark_node is returned.  If there are no
   27775              :    partial specializations matching TARGET, then NULL_TREE is
   27776              :    returned, indicating that the primary template should be used.  */
   27777              : 
   27778              : tree
   27779     85295303 : most_specialized_partial_spec (tree target, tsubst_flags_t complain,
   27780              :                                bool rechecking /* = false */)
   27781              : {
   27782     85295303 :   tree tinfo = NULL_TREE;
   27783     85295303 :   tree tmpl, args, decl;
   27784     85295303 :   if (TYPE_P (target))
   27785              :     {
   27786     40603904 :       tinfo = CLASSTYPE_TEMPLATE_INFO (target);
   27787     40603904 :       tmpl = TI_TEMPLATE (tinfo);
   27788     40603904 :       args = TI_ARGS (tinfo);
   27789     40603904 :       decl = TYPE_NAME (target);
   27790              :     }
   27791     44691399 :   else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
   27792              :     {
   27793     12477695 :       tmpl = TREE_OPERAND (target, 0);
   27794     12477695 :       args = TREE_OPERAND (target, 1);
   27795     12477695 :       decl = DECL_TEMPLATE_RESULT (tmpl);
   27796              :     }
   27797     32213704 :   else if (VAR_P (target))
   27798              :     {
   27799     32213704 :       tinfo = DECL_TEMPLATE_INFO (target);
   27800     32213704 :       tmpl = TI_TEMPLATE (tinfo);
   27801     32213704 :       args = TI_ARGS (tinfo);
   27802     32213704 :       decl = target;
   27803              :     }
   27804              :   else
   27805            0 :     gcc_unreachable ();
   27806              : 
   27807     85295303 :   if (!PRIMARY_TEMPLATE_P (tmpl))
   27808              :     return NULL_TREE;
   27809              : 
   27810     76172134 :   if (!rechecking
   27811     76172134 :       && tinfo
   27812     76172134 :       && (VAR_P (target) || COMPLETE_TYPE_P (target)))
   27813     24945032 :     return TI_PARTIAL_INFO (tinfo);
   27814              : 
   27815     51227102 :   tree main_tmpl = most_general_template (tmpl);
   27816     51227102 :   tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
   27817     51227102 :   if (!specs)
   27818              :     /* There are no partial specializations of this template.  */
   27819              :     return NULL_TREE;
   27820              : 
   27821     14140849 :   push_access_scope_guard pas (decl);
   27822     14140849 :   deferring_access_check_sentinel acs (dk_no_deferred);
   27823              : 
   27824              :   /* For determining which partial specialization to use, only the
   27825              :      innermost args are interesting.  */
   27826     14140849 :   tree outer_args = NULL_TREE;
   27827     28281698 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   27828              :     {
   27829       287119 :       outer_args = strip_innermost_template_args (args, 1);
   27830       287119 :       args = INNERMOST_TEMPLATE_ARGS (args);
   27831              :     }
   27832              : 
   27833              :   /* The caller hasn't called push_to_top_level yet, but we need
   27834              :      get_partial_spec_bindings to be done in non-template context so that we'll
   27835              :      fully resolve everything.  */
   27836     14140849 :   processing_template_decl_sentinel ptds;
   27837              : 
   27838     14140849 :   tree list = NULL_TREE;
   27839     54079483 :   for (tree t = specs; t; t = TREE_CHAIN (t))
   27840              :     {
   27841     39938640 :       const tree ospec_tmpl = TREE_VALUE (t);
   27842              : 
   27843     39938640 :       tree spec_tmpl;
   27844     39938640 :       if (outer_args)
   27845              :         {
   27846              :           /* Substitute in the template args from the enclosing class.  */
   27847       336131 :           ++processing_template_decl;
   27848       336131 :           spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
   27849       336131 :           --processing_template_decl;
   27850       336131 :           if (spec_tmpl == error_mark_node)
   27851              :             return error_mark_node;
   27852              :         }
   27853              :       else
   27854              :         spec_tmpl = ospec_tmpl;
   27855              : 
   27856     39938634 :       tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
   27857     39938634 :       if (spec_args)
   27858              :         {
   27859     13177461 :           if (outer_args)
   27860       234742 :             spec_args = add_to_template_args (outer_args, spec_args);
   27861              : 
   27862              :           /* Keep the candidate only if its constraints are satisfied.  */
   27863     13177461 :           if (constraints_satisfied_p (ospec_tmpl, spec_args))
   27864     10877938 :             list = tree_cons (spec_args, ospec_tmpl, list);
   27865              :         }
   27866              :     }
   27867              : 
   27868     14140843 :   if (! list)
   27869              :     return NULL_TREE;
   27870              : 
   27871      9582670 :   tree champ = list;
   27872      9582670 :   bool ambiguous_p = false;
   27873     10877847 :   for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
   27874              :     {
   27875      1295189 :       int fate = more_specialized_partial_spec (tmpl, champ, t);
   27876      1295189 :       if (fate == 1)
   27877              :         ;
   27878              :       else
   27879              :         {
   27880       223535 :           if (fate == 0)
   27881              :             {
   27882           91 :               t = TREE_CHAIN (t);
   27883           91 :               if (! t)
   27884              :                 {
   27885              :                   ambiguous_p = true;
   27886              :                   break;
   27887              :                 }
   27888              :             }
   27889              :           champ = t;
   27890              :         }
   27891              :     }
   27892              : 
   27893      9582670 :   if (!ambiguous_p)
   27894      9916254 :     for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
   27895              :       {
   27896       333599 :         int fate = more_specialized_partial_spec (tmpl, champ, t);
   27897       333599 :         if (fate != 1)
   27898              :           {
   27899              :             ambiguous_p = true;
   27900              :             break;
   27901              :           }
   27902              :       }
   27903              : 
   27904      9582658 :   if (ambiguous_p)
   27905              :     {
   27906           15 :       const char *str;
   27907           15 :       char *spaces = NULL;
   27908           15 :       if (!(complain & tf_error))
   27909            3 :         return error_mark_node;
   27910           12 :       auto_diagnostic_group d;
   27911           12 :       if (TYPE_P (target))
   27912            9 :         error ("ambiguous template instantiation for %q#T", target);
   27913              :       else
   27914            3 :         error ("ambiguous template instantiation for %q#D", target);
   27915           12 :       str = ngettext ("candidate is:", "candidates are:", list_length (list));
   27916           45 :       for (tree t = list; t; t = TREE_CHAIN (t))
   27917              :         {
   27918           33 :           tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
   27919           66 :           inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
   27920              :                   "%s %#qS", spaces ? spaces : str, subst);
   27921           33 :           spaces = spaces ? spaces : get_spaces (str);
   27922              :         }
   27923           12 :       free (spaces);
   27924           12 :       return error_mark_node;
   27925           12 :     }
   27926              : 
   27927      9582655 :   tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
   27928      9582655 :   if (!rechecking && tinfo)
   27929      8680301 :     TI_PARTIAL_INFO (tinfo) = result;
   27930              :   return result;
   27931     28281698 : }
   27932              : 
   27933              : /* Explicitly instantiate DECL.  */
   27934              : 
   27935              : void
   27936      1940230 : do_decl_instantiation (tree decl, tree storage)
   27937              : {
   27938      1940230 :   tree result = NULL_TREE;
   27939      1940230 :   int extern_p = 0;
   27940              : 
   27941      1940230 :   if (!decl || decl == error_mark_node)
   27942              :     /* An error occurred, for which grokdeclarator has already issued
   27943              :        an appropriate message.  */
   27944              :     return;
   27945      1940068 :   else if (! DECL_LANG_SPECIFIC (decl))
   27946              :     {
   27947            0 :       error ("explicit instantiation of non-template %q#D", decl);
   27948            0 :       return;
   27949              :     }
   27950              : 
   27951      1940068 :   bool var_templ = (DECL_TEMPLATE_INFO (decl)
   27952      1940068 :                     && variable_template_p (DECL_TI_TEMPLATE (decl)));
   27953              : 
   27954      1940068 :   if (VAR_P (decl) && !var_templ)
   27955              :     {
   27956              :       /* There is an asymmetry here in the way VAR_DECLs and
   27957              :          FUNCTION_DECLs are handled by grokdeclarator.  In the case of
   27958              :          the latter, the DECL we get back will be marked as a
   27959              :          template instantiation, and the appropriate
   27960              :          DECL_TEMPLATE_INFO will be set up.  This does not happen for
   27961              :          VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
   27962              :          should handle VAR_DECLs as it currently handles
   27963              :          FUNCTION_DECLs.  */
   27964           68 :       if (!DECL_CLASS_SCOPE_P (decl))
   27965              :         {
   27966            0 :           error ("%qD is not a static data member of a class template", decl);
   27967            0 :           return;
   27968              :         }
   27969           68 :       result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
   27970           68 :       if (!result || !VAR_P (result))
   27971              :         {
   27972            0 :           error ("no matching template for %qD found", decl);
   27973            0 :           return;
   27974              :         }
   27975           68 :       if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
   27976              :         {
   27977            9 :           error ("type %qT for explicit instantiation %qD does not match "
   27978            3 :                  "declared type %qT", TREE_TYPE (result), decl,
   27979            3 :                  TREE_TYPE (decl));
   27980            3 :           return;
   27981              :         }
   27982              :     }
   27983      1940000 :   else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
   27984              :     {
   27985            0 :       error ("explicit instantiation of %q#D", decl);
   27986            0 :       return;
   27987              :     }
   27988              :   else
   27989              :     result = decl;
   27990              : 
   27991              :   /* Check for various error cases.  Note that if the explicit
   27992              :      instantiation is valid the RESULT will currently be marked as an
   27993              :      *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
   27994              :      until we get here.  */
   27995              : 
   27996      1940065 :   if (DECL_TEMPLATE_SPECIALIZATION (result))
   27997              :     {
   27998              :       /* DR 259 [temp.spec].
   27999              : 
   28000              :          Both an explicit instantiation and a declaration of an explicit
   28001              :          specialization shall not appear in a program unless the explicit
   28002              :          instantiation follows a declaration of the explicit specialization.
   28003              : 
   28004              :          For a given set of template parameters, if an explicit
   28005              :          instantiation of a template appears after a declaration of an
   28006              :          explicit specialization for that template, the explicit
   28007              :          instantiation has no effect.  */
   28008              :       return;
   28009              :     }
   28010      1904056 :   else if (DECL_EXPLICIT_INSTANTIATION (result))
   28011              :     {
   28012              :       /* [temp.spec]
   28013              : 
   28014              :          No program shall explicitly instantiate any template more
   28015              :          than once.
   28016              : 
   28017              :          We check DECL_NOT_REALLY_EXTERN so as not to complain when
   28018              :          the first instantiation was `extern' and the second is not,
   28019              :          and EXTERN_P for the opposite case.  */
   28020          860 :       if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
   28021            6 :         permerror (input_location, "duplicate explicit instantiation of %q#D", result);
   28022              :       /* If an "extern" explicit instantiation follows an ordinary
   28023              :          explicit instantiation, the template is instantiated.  */
   28024              :       if (extern_p)
   28025              :         return;
   28026              :     }
   28027      1903196 :   else if (!DECL_IMPLICIT_INSTANTIATION (result))
   28028              :     {
   28029            0 :       error ("no matching template for %qD found", result);
   28030            0 :       return;
   28031              :     }
   28032      1903196 :   else if (!DECL_TEMPLATE_INFO (result))
   28033              :     {
   28034            0 :       permerror (input_location, "explicit instantiation of non-template %q#D", result);
   28035            0 :       return;
   28036              :     }
   28037              : 
   28038      1904056 :   if (storage == NULL_TREE)
   28039              :     ;
   28040      1900625 :   else if (storage == ridpointers[(int) RID_EXTERN])
   28041              :     {
   28042      1900625 :       if (cxx_dialect == cxx98 && pedantic)
   28043         7392 :         pedwarn (input_location, OPT_Wc__11_extensions,
   28044              :                  "ISO C++ 1998 forbids the use of %<extern%> on explicit "
   28045              :                  "instantiations");
   28046              :       extern_p = 1;
   28047              :     }
   28048              :   else
   28049            0 :     error ("storage class %qD applied to template instantiation", storage);
   28050              : 
   28051      1904056 :   check_explicit_instantiation_namespace (result);
   28052      1904056 :   mark_decl_instantiated (result, extern_p);
   28053      1904056 :   if (! extern_p)
   28054         3431 :     instantiate_decl (result, /*defer_ok=*/true,
   28055              :                       /*expl_inst_class_mem_p=*/false);
   28056              : }
   28057              : 
   28058              : static void
   28059       880075 : mark_class_instantiated (tree t, int extern_p)
   28060              : {
   28061       880075 :   SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
   28062       880075 :   SET_CLASSTYPE_INTERFACE_KNOWN (t);
   28063       880075 :   CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
   28064       880075 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
   28065       880075 :   if (! extern_p)
   28066              :     {
   28067         2718 :       CLASSTYPE_DEBUG_REQUESTED (t) = 1;
   28068         2718 :       rest_of_type_compilation (t, 1);
   28069              :     }
   28070       880075 : }
   28071              : 
   28072              : /* Perform an explicit instantiation of template class T.  STORAGE, if
   28073              :    non-null, is the RID for extern, inline or static.  COMPLAIN is
   28074              :    nonzero if this is called from the parser, zero if called recursively,
   28075              :    since the standard is unclear (as detailed below).  */
   28076              : 
   28077              : void
   28078       941882 : do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
   28079              : {
   28080       941882 :   if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
   28081              :     {
   28082            3 :       if (tree ti = TYPE_TEMPLATE_INFO (t))
   28083            0 :         error ("explicit instantiation of non-class template %qD",
   28084            0 :                TI_TEMPLATE (ti));
   28085              :       else
   28086            3 :         error ("explicit instantiation of non-template type %qT", t);
   28087            3 :       return;
   28088              :     }
   28089              : 
   28090       941879 :   complete_type (t);
   28091              : 
   28092       941879 :   if (!COMPLETE_TYPE_P (t))
   28093              :     {
   28094          272 :       if (complain & tf_error)
   28095            0 :         error ("explicit instantiation of %q#T before definition of template",
   28096              :                t);
   28097          272 :       return;
   28098              :     }
   28099              : 
   28100              :   /* At most one of these will be true.  */
   28101       941607 :   bool extern_p = false;
   28102       941607 :   bool nomem_p = false;
   28103       941607 :   bool static_p = false;
   28104              : 
   28105       941607 :   if (storage != NULL_TREE)
   28106              :     {
   28107       938850 :       if (storage == ridpointers[(int) RID_EXTERN])
   28108              :         {
   28109       938825 :           if (cxx_dialect == cxx98 && pedantic)
   28110         2553 :             pedwarn (input_location, OPT_Wc__11_extensions,
   28111              :                      "ISO C++ 1998 forbids the use of %<extern%> on "
   28112              :                      "explicit instantiations");
   28113              :         }
   28114              :       else
   28115           25 :         pedwarn (input_location, OPT_Wpedantic,
   28116              :                  "ISO C++ forbids the use of %qE"
   28117              :                  " on explicit instantiations", storage);
   28118              : 
   28119       938850 :       if (storage == ridpointers[(int) RID_INLINE])
   28120              :         nomem_p = true;
   28121       938828 :       else if (storage == ridpointers[(int) RID_EXTERN])
   28122              :         extern_p = true;
   28123            3 :       else if (storage == ridpointers[(int) RID_STATIC])
   28124              :         static_p = true;
   28125              :       else
   28126            0 :         error ("storage class %qD applied to template instantiation",
   28127              :                storage);
   28128              :     }
   28129              : 
   28130       941607 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
   28131              :     /* DR 259 [temp.spec].
   28132              : 
   28133              :        Both an explicit instantiation and a declaration of an explicit
   28134              :        specialization shall not appear in a program unless the
   28135              :        explicit instantiation follows a declaration of the explicit
   28136              :        specialization.
   28137              : 
   28138              :        For a given set of template parameters, if an explicit
   28139              :        instantiation of a template appears after a declaration of an
   28140              :        explicit specialization for that template, the explicit
   28141              :        instantiation has no effect.  */
   28142              :     return;
   28143              : 
   28144       880078 :   if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
   28145              :     {
   28146              :       /* We've already instantiated the template.  */
   28147              : 
   28148              :       /* [temp.spec]
   28149              : 
   28150              :          No program shall explicitly instantiate any template more
   28151              :          than once.
   28152              : 
   28153              :          If EXTERN_P then this is ok.  */
   28154            3 :       if (!extern_p && (complain & tf_error))
   28155            3 :         permerror (input_location,
   28156              :                    "duplicate explicit instantiation of %q#T", t);
   28157              : 
   28158            3 :       return;
   28159              :     }
   28160              : 
   28161       880075 :   check_explicit_instantiation_namespace (TYPE_NAME (t));
   28162       880075 :   mark_class_instantiated (t, extern_p);
   28163              : 
   28164       880075 :   if (nomem_p)
   28165              :     return;
   28166              : 
   28167              :   /* In contrast to implicit instantiation, where only the
   28168              :      declarations, and not the definitions, of members are
   28169              :      instantiated, we have here:
   28170              : 
   28171              :          [temp.explicit]
   28172              : 
   28173              :          An explicit instantiation that names a class template
   28174              :          specialization is also an explicit instantiation of the same
   28175              :          kind (declaration or definition) of each of its members (not
   28176              :          including members inherited from base classes and members
   28177              :          that are templates) that has not been previously explicitly
   28178              :          specialized in the translation unit containing the explicit
   28179              :          instantiation, provided that the associated constraints, if
   28180              :          any, of that member are satisfied by the template arguments
   28181              :          of the explicit instantiation.  */
   28182     35261737 :   for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
   28183     34381687 :     if ((VAR_P (fld)
   28184     34033883 :          || (TREE_CODE (fld) == FUNCTION_DECL
   28185     22934126 :              && !static_p
   28186     22934126 :              && user_provided_p (fld)))
   28187     21695122 :         && DECL_TEMPLATE_INSTANTIATION (fld)
   28188     55747876 :         && constraints_satisfied_p (fld))
   28189              :       {
   28190     21365536 :         mark_decl_instantiated (fld, extern_p);
   28191     21365536 :         if (! extern_p)
   28192        28605 :           instantiate_decl (fld, /*defer_ok=*/true,
   28193              :                             /*expl_inst_class_mem_p=*/true);
   28194              :       }
   28195     13016151 :     else if (DECL_IMPLICIT_TYPEDEF_P (fld))
   28196              :       {
   28197       160556 :         tree type = TREE_TYPE (fld);
   28198              : 
   28199       140531 :         if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   28200       301071 :             && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
   28201       140515 :           do_type_instantiation (type, storage, 0);
   28202              :       }
   28203              : }
   28204              : 
   28205              : /* Given a function DECL, which is a specialization of TMPL, modify
   28206              :    DECL to be a re-instantiation of TMPL with the same template
   28207              :    arguments.  TMPL should be the template into which tsubst'ing
   28208              :    should occur for DECL, not the most general template.
   28209              : 
   28210              :    One reason for doing this is a scenario like this:
   28211              : 
   28212              :      template <class T>
   28213              :      void f(const T&, int i);
   28214              : 
   28215              :      void g() { f(3, 7); }
   28216              : 
   28217              :      template <class T>
   28218              :      void f(const T& t, const int i) { }
   28219              : 
   28220              :    Note that when the template is first instantiated, with
   28221              :    instantiate_template, the resulting DECL will have no name for the
   28222              :    first parameter, and the wrong type for the second.  So, when we go
   28223              :    to instantiate the DECL, we regenerate it.  */
   28224              : 
   28225              : static void
   28226     36310752 : regenerate_decl_from_template (tree decl, tree tmpl, tree args)
   28227              : {
   28228              :   /* The arguments used to instantiate DECL, from the most general
   28229              :      template.  */
   28230     36310752 :   tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
   28231              : 
   28232              :   /* Make sure that we can see identifiers, and compute access correctly.  */
   28233     36310752 :   push_access_scope (decl);
   28234              : 
   28235     36310752 :   if (TREE_CODE (decl) == FUNCTION_DECL)
   28236              :     {
   28237     21930341 :       tree specs;
   28238     21930341 :       int args_depth;
   28239     21930341 :       int parms_depth;
   28240              : 
   28241              :       /* Don't bother with this for unique friends that can't be redeclared and
   28242              :          might change type if regenerated (PR69836).  */
   28243     21930341 :       if (DECL_UNIQUE_FRIEND_P (decl))
   28244        94824 :         goto done;
   28245              : 
   28246              :       /* A template with a lambda in the signature also changes type if
   28247              :          regenerated (PR119401).  */
   28248     21835517 :       walk_tree_fn find_lambda
   28249              :         = [](tree *tp, int *, void *)
   28250              :         {
   28251              :           if (TREE_CODE (*tp) == LAMBDA_EXPR)
   28252              :             return *tp;
   28253              :           return NULL_TREE;
   28254              :         };
   28255     21835517 :       if (cp_walk_tree_without_duplicates
   28256              :           (&TREE_TYPE (tmpl), find_lambda, nullptr))
   28257           43 :         goto done;
   28258              : 
   28259              :       /* Use the source location of the definition.  */
   28260     21835474 :       DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
   28261              : 
   28262     43670948 :       args_depth = TMPL_ARGS_DEPTH (args);
   28263     21835474 :       parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   28264     21835474 :       if (args_depth > parms_depth)
   28265            0 :         args = get_innermost_template_args (args, parms_depth);
   28266              : 
   28267              :       /* Instantiate a dynamic exception-specification.  noexcept will be
   28268              :          handled below.  */
   28269     21835474 :       if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
   28270      9290336 :         if (TREE_VALUE (raises))
   28271              :           {
   28272           22 :             specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
   28273              :                                                     args, tf_error, NULL_TREE,
   28274              :                                                     /*defer_ok*/false);
   28275           22 :             if (specs && specs != error_mark_node)
   28276           21 :               TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
   28277              :                                                           specs);
   28278              :           }
   28279              : 
   28280              :       /* Merge parameter declarations.  */
   28281     21835474 :       if (tree pattern_parm
   28282     21835474 :           = skip_artificial_parms_for (code_pattern,
   28283     21835474 :                                        DECL_ARGUMENTS (code_pattern)))
   28284              :         {
   28285     15021156 :           tree *p = &DECL_ARGUMENTS (decl);
   28286     20796912 :           for (int skip = num_artificial_parms_for (decl); skip; --skip)
   28287      5775756 :             p = &DECL_CHAIN (*p);
   28288     15021156 :           tree oldarg = *p;
   28289     15021156 :           *p = tsubst_decl (pattern_parm, args, tf_error);
   28290     41901264 :           for (tree t = *p; t; t = DECL_CHAIN (t))
   28291     26880108 :             DECL_CONTEXT (t) = decl;
   28292              :           /* Mark the old PARM_DECLs in case std::meta::parameters_of has
   28293              :              been called on the old declaration and reflections of those
   28294              :              arguments are held across this point and used later.
   28295              :              Such PARM_DECLs are no longer present in
   28296              :              DECL_ARGUMENTS (DECL_CONTEXT (oldarg)) chain.  */
   28297     15021156 :           if (*p != oldarg)
   28298     41872950 :             for (tree t = oldarg; t; t = DECL_CHAIN (t))
   28299     26880108 :               OLD_PARM_DECL_P (t) = 1;
   28300              :         }
   28301              : 
   28302     21835474 :       if (tree attr = get_fn_contract_specifiers (decl))
   28303              :         {
   28304              :           /* If we're regenerating a specialization, the contracts will have
   28305              :              been copied from the most general template. Replace those with
   28306              :              the ones from the actual specialization.  */
   28307          142 :           tree tmpl = DECL_TI_TEMPLATE (decl);
   28308          142 :           if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
   28309            4 :             attr = get_fn_contract_specifiers (code_pattern);
   28310              : 
   28311          142 :           tsubst_contract_specifiers (attr, decl, args,
   28312              :                                       tf_warning_or_error, code_pattern);
   28313              :         }
   28314              : 
   28315              :       /* Merge additional specifiers from the CODE_PATTERN.  */
   28316     21835474 :       if (DECL_DECLARED_INLINE_P (code_pattern)
   28317     42051137 :           && !DECL_DECLARED_INLINE_P (decl))
   28318        63052 :         DECL_DECLARED_INLINE_P (decl) = 1;
   28319              : 
   28320     21835474 :       maybe_instantiate_noexcept (decl, tf_error);
   28321              :     }
   28322     14380411 :   else if (VAR_P (decl))
   28323              :     {
   28324     14380411 :       start_lambda_scope (decl);
   28325     14380411 :       DECL_INITIAL (decl) =
   28326     14380411 :         tsubst_init (DECL_INITIAL (code_pattern), decl, args,
   28327     14380411 :                      tf_error, DECL_TI_TEMPLATE (decl));
   28328     14380411 :       finish_lambda_scope ();
   28329     14380411 :       if (VAR_HAD_UNKNOWN_BOUND (decl))
   28330          214 :         TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
   28331          214 :                                    tf_error, DECL_TI_TEMPLATE (decl));
   28332              :     }
   28333              :   else
   28334            0 :     gcc_unreachable ();
   28335              : 
   28336     36310752 :  done:
   28337     36310752 :   pop_access_scope (decl);
   28338     36310752 : }
   28339              : 
   28340              : /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
   28341              :    substituted to get DECL.  */
   28342              : 
   28343              : tree
   28344    100026232 : template_for_substitution (tree decl)
   28345              : {
   28346    100026232 :   tree tmpl = DECL_TI_TEMPLATE (decl);
   28347    100026232 :   if (VAR_P (decl))
   28348     19742548 :     if (tree partial = most_specialized_partial_spec (decl, tf_none))
   28349       839597 :       if (partial != error_mark_node)
   28350       839597 :         tmpl = TI_TEMPLATE (partial);
   28351              : 
   28352              :   /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
   28353              :      for the instantiation.  This is not always the most general
   28354              :      template.  Consider, for example:
   28355              : 
   28356              :         template <class T>
   28357              :         struct S { template <class U> void f();
   28358              :                    template <> void f<int>(); };
   28359              : 
   28360              :      and an instantiation of S<double>::f<int>.  We want TD to be the
   28361              :      specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
   28362              :   while (/* An instantiation cannot have a definition, so we need a
   28363              :             more general template.  */
   28364    218618942 :          DECL_TEMPLATE_INSTANTIATION (tmpl)
   28365              :            /* We must also deal with friend templates.  Given:
   28366              : 
   28367              :                 template <class T> struct S {
   28368              :                   template <class U> friend void f() {};
   28369              :                 };
   28370              : 
   28371              :               S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
   28372              :               so far as the language is concerned, but that's still
   28373              :               where we get the pattern for the instantiation from.  On
   28374              :               other hand, if the definition comes outside the class, say:
   28375              : 
   28376              :                 template <class T> struct S {
   28377              :                   template <class U> friend void f();
   28378              :                 };
   28379              :                 template <class U> friend void f() {}
   28380              : 
   28381              :               we don't need to look any further.  That's what the check for
   28382              :               DECL_INITIAL is for.  */
   28383    109309471 :           || (TREE_CODE (decl) == FUNCTION_DECL
   28384     79695170 :               && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
   28385      1046124 :               && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
   28386              :     {
   28387              :       /* The present template, TD, should not be a definition.  If it
   28388              :          were a definition, we should be using it!  Note that we
   28389              :          cannot restructure the loop to just keep going until we find
   28390              :          a template with a definition, since that might go too far if
   28391              :          a specialization was declared, but not defined.  */
   28392              : 
   28393              :       /* Fetch the more general template.  */
   28394      9283239 :       tmpl = DECL_TI_TEMPLATE (tmpl);
   28395              :     }
   28396              : 
   28397    100026232 :   return tmpl;
   28398              : }
   28399              : 
   28400              : /* Returns true if we need to instantiate this template instance even if we
   28401              :    know we aren't going to emit it.  */
   28402              : 
   28403              : bool
   28404      3448253 : always_instantiate_p (tree decl)
   28405              : {
   28406              :   /* We always instantiate inline functions so that we can inline them.  An
   28407              :      explicit instantiation declaration prohibits implicit instantiation of
   28408              :      non-inline functions.  With high levels of optimization, we would
   28409              :      normally inline non-inline functions -- but we're not allowed to do
   28410              :      that for "extern template" functions.  Therefore, we check
   28411              :      DECL_DECLARED_INLINE_P, rather than possibly_inlined_p.  */
   28412      3448253 :   return ((TREE_CODE (decl) == FUNCTION_DECL
   28413      3352035 :            && (DECL_DECLARED_INLINE_P (decl)
   28414        61441 :                || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
   28415              :           /* And we need to instantiate static data members so that
   28416              :              their initializers are available in integral constant
   28417              :              expressions.  */
   28418      3509694 :           || (VAR_P (decl)
   28419        96218 :               && decl_maybe_constant_var_p (decl)));
   28420              : }
   28421              : 
   28422              : /* If FN has a noexcept-specifier that hasn't been instantiated yet,
   28423              :    instantiate it now, modifying TREE_TYPE (fn).  Returns false on
   28424              :    error, true otherwise.  */
   28425              : 
   28426              : bool
   28427    423534767 : maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
   28428              : {
   28429    423534767 :   if (fn == error_mark_node)
   28430              :     return false;
   28431              : 
   28432              :   /* Don't instantiate a noexcept-specification from template context.  */
   28433    423534767 :   if (processing_template_decl
   28434    423534767 :       && (!flag_noexcept_type || type_dependent_expression_p (fn)))
   28435       183622 :     return true;
   28436              : 
   28437    423351145 :   tree fntype = TREE_TYPE (fn);
   28438    423351145 :   tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
   28439              : 
   28440    234596298 :   if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
   28441    431249225 :       && DECL_MAYBE_DELETED (fn))
   28442              :     {
   28443        37937 :       if (fn == current_function_decl)
   28444              :         /* We're in start_preparsed_function, keep going.  */
   28445              :         return true;
   28446              : 
   28447           23 :       ++function_depth;
   28448           23 :       maybe_synthesize_method (fn);
   28449           23 :       --function_depth;
   28450           23 :       return !DECL_DELETED_FN (fn);
   28451              :     }
   28452              : 
   28453    657909506 :   if (!spec || !TREE_PURPOSE (spec))
   28454              :     return true;
   28455              : 
   28456    233509200 :   tree noex = TREE_PURPOSE (spec);
   28457    233509200 :   if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   28458    233509200 :       && TREE_CODE (noex) != DEFERRED_PARSE)
   28459              :     return true;
   28460              : 
   28461     10078255 :   tree orig_fn = NULL_TREE;
   28462              :   /* For a member friend template we can get a TEMPLATE_DECL.  Let's use
   28463              :      its FUNCTION_DECL for the rest of this function -- push_access_scope
   28464              :      doesn't accept TEMPLATE_DECLs.  */
   28465     10078255 :   if (DECL_FUNCTION_TEMPLATE_P (fn))
   28466              :     {
   28467           46 :       orig_fn = fn;
   28468           46 :       fn = DECL_TEMPLATE_RESULT (fn);
   28469              :     }
   28470              : 
   28471     10078255 :   if (DECL_CLONED_FUNCTION_P (fn))
   28472              :     {
   28473      4567511 :       tree prime = DECL_CLONED_FUNCTION (fn);
   28474      4567511 :       if (!maybe_instantiate_noexcept (prime, complain))
   28475              :         return false;
   28476      4567410 :       spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
   28477              :     }
   28478      5510744 :   else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
   28479              :     {
   28480      5510744 :       static hash_set<tree>* fns = new hash_set<tree>;
   28481      5510744 :       bool added = false;
   28482      5510744 :       tree pattern = DEFERRED_NOEXCEPT_PATTERN (noex);
   28483      5510744 :       if (pattern == NULL_TREE)
   28484              :         {
   28485      3925970 :           spec = get_defaulted_eh_spec (fn, complain);
   28486      3925970 :           if (spec == error_mark_node)
   28487              :             /* This might have failed because of an unparsed DMI, so
   28488              :                let's try again later.  */
   28489              :             return false;
   28490              :         }
   28491      1584774 :       else if (!(added = !fns->add (fn)))
   28492              :         {
   28493              :           /* If hash_set::add returns true, the element was already there.  */
   28494            3 :           location_t loc = cp_expr_loc_or_loc (pattern,
   28495            3 :                                                DECL_SOURCE_LOCATION (fn));
   28496            3 :           error_at (loc,
   28497              :                     "exception specification of %qD depends on itself",
   28498              :                     fn);
   28499            3 :           spec = noexcept_false_spec;
   28500              :         }
   28501      1584771 :       else if (TREE_CODE (pattern) == DEFERRED_PARSE)
   28502              :         {
   28503            6 :           error ("exception specification of %qD is not available "
   28504              :                  "until end of class definition", fn);
   28505            6 :           spec = noexcept_false_spec;
   28506              :         }
   28507      1584765 :       else if (push_tinst_level (fn))
   28508              :         {
   28509      1584689 :           const bool push_to_top = maybe_push_to_top_level (fn);
   28510      1584689 :           push_access_scope (fn);
   28511      1584689 :           push_deferring_access_checks (dk_no_deferred);
   28512      1584689 :           input_location = DECL_SOURCE_LOCATION (fn);
   28513              : 
   28514      1584689 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   28515      1584689 :               && !DECL_LOCAL_DECL_P (fn))
   28516              :             {
   28517              :               /* If needed, set current_class_ptr for the benefit of
   28518              :                  tsubst_copy/PARM_DECL.  */
   28519       813505 :               tree this_parm = DECL_ARGUMENTS (fn);
   28520       813505 :               current_class_ptr = NULL_TREE;
   28521       813505 :               current_class_ref = cp_build_fold_indirect_ref (this_parm);
   28522       813505 :               current_class_ptr = this_parm;
   28523              :             }
   28524              : 
   28525              :           /* If this function is represented by a TEMPLATE_DECL, then
   28526              :              the deferred noexcept-specification might still contain
   28527              :              dependent types, even after substitution.  And we need the
   28528              :              dependency check functions to work in build_noexcept_spec.  */
   28529      1584689 :           if (orig_fn)
   28530           46 :             ++processing_template_decl;
   28531              : 
   28532              :           /* Do deferred instantiation of the noexcept-specifier.  */
   28533      1584689 :           noex = tsubst_expr (pattern, DEFERRED_NOEXCEPT_ARGS (noex),
   28534              :                               tf_warning_or_error, fn);
   28535              :           /* Build up the noexcept-specification.  */
   28536      1584689 :           spec = build_noexcept_spec (noex, tf_warning_or_error);
   28537              : 
   28538      1584689 :           if (orig_fn)
   28539           46 :             --processing_template_decl;
   28540              : 
   28541      1584689 :           pop_deferring_access_checks ();
   28542      1584689 :           pop_access_scope (fn);
   28543      1584689 :           pop_tinst_level ();
   28544      1584689 :           maybe_pop_from_top_level (push_to_top);
   28545              :         }
   28546              :       else
   28547           76 :         spec = noexcept_false_spec;
   28548              : 
   28549      1584774 :       if (added)
   28550      1584771 :         fns->remove (fn);
   28551              :     }
   28552              : 
   28553     10078053 :   if (spec == error_mark_node)
   28554              :     {
   28555              :       /* This failed with a hard error, so let's go with false.  */
   28556           25 :       gcc_assert (seen_error ());
   28557           25 :       spec = noexcept_false_spec;
   28558              :     }
   28559              : 
   28560     10078053 :   TREE_TYPE (fn) = build_exception_variant (fntype, spec);
   28561     10078053 :   if (orig_fn)
   28562           46 :     TREE_TYPE (orig_fn) = TREE_TYPE (fn);
   28563              : 
   28564              :   return true;
   28565              : }
   28566              : 
   28567              : /* We're starting to process the function INST, an instantiation of PATTERN;
   28568              :    add their parameters to local_specializations.  */
   28569              : 
   28570              : void
   28571     22123566 : register_parameter_specializations (tree pattern, tree inst)
   28572              : {
   28573     22123566 :   tree tmpl_parm = DECL_ARGUMENTS (pattern);
   28574     22123566 :   tree spec_parm = DECL_ARGUMENTS (inst);
   28575     22123566 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (inst))
   28576              :     {
   28577     10758316 :       register_local_specialization (spec_parm, tmpl_parm);
   28578     10758316 :       spec_parm = skip_artificial_parms_for (inst, spec_parm);
   28579     10758316 :       tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
   28580              :     }
   28581     49524297 :   for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
   28582              :     {
   28583     27400731 :       if (!DECL_PACK_P (tmpl_parm))
   28584              :         {
   28585     26690679 :           register_local_specialization (spec_parm, tmpl_parm);
   28586     26690679 :           spec_parm = DECL_CHAIN (spec_parm);
   28587              :         }
   28588              :       else
   28589              :         {
   28590              :           /* Register the (value) argument pack as a specialization of
   28591              :              TMPL_PARM, then move on.  */
   28592       710052 :           tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
   28593       710052 :           register_local_specialization (argpack, tmpl_parm);
   28594              :         }
   28595              :     }
   28596     22123566 :   gcc_assert (!spec_parm);
   28597     22123566 : }
   28598              : 
   28599              : /* Instantiate the body of D using PATTERN with ARGS.  We have
   28600              :    already determined PATTERN is the correct template to use.
   28601              :    NESTED_P is true if this is a nested function, in which case
   28602              :    PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL.  */
   28603              : 
   28604              : static void
   28605     36310840 : instantiate_body (tree pattern, tree args, tree d, bool nested_p)
   28606              : {
   28607     36310840 :   tree td = NULL_TREE;
   28608     36310840 :   tree code_pattern = pattern;
   28609              : 
   28610     36310840 :   if (!nested_p)
   28611              :     {
   28612     36310752 :       td = pattern;
   28613     36310752 :       code_pattern = DECL_TEMPLATE_RESULT (td);
   28614              :     }
   28615              :   else
   28616              :     /* Only OMP reductions are nested.  */
   28617           88 :     gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
   28618              : 
   28619     36310840 :   vec<tree> omp_privatization_save;
   28620     36310840 :   if (current_function_decl)
   28621     14025270 :     save_omp_privatization_clauses (omp_privatization_save);
   28622              : 
   28623     36310840 :   bool push_to_top = maybe_push_to_top_level (d);
   28624              : 
   28625     36310840 :   mark_template_arguments_used (pattern, args);
   28626              : 
   28627     36310840 :   if (VAR_P (d))
   28628              :     {
   28629              :       /* The variable might be a lambda's extra scope, and that
   28630              :          lambda's visibility depends on D's.  */
   28631     14380411 :       maybe_commonize_var (d);
   28632     14380411 :       determine_visibility (d);
   28633              :     }
   28634              : 
   28635              :   /* Mark D as instantiated so that recursive calls to
   28636              :      instantiate_decl do not try to instantiate it again.  */
   28637     36310840 :   DECL_TEMPLATE_INSTANTIATED (d) = 1;
   28638              : 
   28639     36310840 :   if (td)
   28640              :     /* Regenerate the declaration in case the template has been modified
   28641              :        by a subsequent redeclaration.  */
   28642     36310752 :     regenerate_decl_from_template (d, td, args);
   28643              : 
   28644              :   /* We already set the file and line above.  Reset them now in case
   28645              :      they changed as a result of calling regenerate_decl_from_template.  */
   28646     36310840 :   input_location = DECL_SOURCE_LOCATION (d);
   28647              : 
   28648     36310840 :   if (VAR_P (d))
   28649              :     {
   28650              :       /* Clear out DECL_RTL; whatever was there before may not be right
   28651              :          since we've reset the type of the declaration.  */
   28652     14380411 :       SET_DECL_RTL (d, NULL);
   28653     14380411 :       DECL_IN_AGGR_P (d) = 0;
   28654              : 
   28655              :       /* The initializer is placed in DECL_INITIAL by
   28656              :          regenerate_decl_from_template so we don't need to
   28657              :          push/pop_access_scope again here.  Pull it out so that
   28658              :          cp_finish_decl can process it.  */
   28659     14380411 :       bool const_init = false;
   28660     14380411 :       tree init = DECL_INITIAL (d);
   28661     14380411 :       DECL_INITIAL (d) = NULL_TREE;
   28662     14380411 :       DECL_INITIALIZED_P (d) = 0;
   28663              : 
   28664              :       /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
   28665              :          initializer.  That function will defer actual emission until
   28666              :          we have a chance to determine linkage.  */
   28667     14380411 :       DECL_EXTERNAL (d) = 0;
   28668              : 
   28669              :       /* Enter the scope of D so that access-checking works correctly.  */
   28670     14380411 :       bool enter_context = DECL_CLASS_SCOPE_P (d);
   28671      1994659 :       if (enter_context)
   28672      1994659 :         push_nested_class (DECL_CONTEXT (d));
   28673              : 
   28674     14380411 :       const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   28675     14380411 :       cp_finish_decl (d, init, const_init, NULL_TREE, 0);
   28676              : 
   28677     14380411 :       if (enter_context)
   28678      1994659 :         pop_nested_class ();
   28679              :     }
   28680     21930429 :   else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
   28681           67 :     synthesize_method (d);
   28682     21930362 :   else if (TREE_CODE (d) == FUNCTION_DECL)
   28683              :     {
   28684              :       /* Set up the list of local specializations.  */
   28685     22964619 :       local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
   28686     21930362 :       tree block = NULL_TREE;
   28687              : 
   28688              :       /* Set up context.  */
   28689     21930362 :       if (nested_p)
   28690           88 :         block = push_stmt_list ();
   28691              :       else
   28692              :         {
   28693     21930274 :           start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
   28694              : 
   28695     21930274 :           perform_instantiation_time_access_checks (code_pattern, args);
   28696              :         }
   28697              : 
   28698              :       /* Create substitution entries for the parameters.  */
   28699     21930362 :       register_parameter_specializations (code_pattern, d);
   28700              : 
   28701              :       /* Substitute into the body of the function.  */
   28702     21930362 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   28703          104 :         tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
   28704              :                         tf_warning_or_error, d);
   28705              :       else
   28706              :         {
   28707     21930258 :           tsubst_stmt (DECL_SAVED_TREE (code_pattern), args,
   28708     21930258 :                        tf_warning_or_error, DECL_TI_TEMPLATE (d));
   28709              : 
   28710              :           /* Set the current input_location to the end of the function
   28711              :              so that finish_function knows where we are.  */
   28712     21930249 :           input_location
   28713     21930249 :             = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
   28714              : 
   28715              :           /* Remember if we saw an infinite loop in the template.  */
   28716     21930249 :           current_function_infinite_loop
   28717     21930249 :             = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
   28718              :         }
   28719              : 
   28720              :       /* Finish the function.  */
   28721     21930353 :       if (nested_p)
   28722           88 :         DECL_SAVED_TREE (d) = pop_stmt_list (block);
   28723              :       else
   28724              :         {
   28725     21930265 :           d = finish_function (/*inline_p=*/false);
   28726     21930265 :           expand_or_defer_fn (d);
   28727              :         }
   28728              : 
   28729     21930353 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   28730          104 :         cp_check_omp_declare_reduction (d);
   28731              : 
   28732     21930353 :       check_consteval_only_fn (d);
   28733              : 
   28734     21930353 :       if (int errs = errorcount + sorrycount)
   28735       831651 :         if (errs > current_tinst_level->errors)
   28736         2331 :           if (function *f = DECL_STRUCT_FUNCTION (d))
   28737         2313 :             f->language->erroneous = true;
   28738     21930353 :     }
   28739              : 
   28740              :   /* We're not deferring instantiation any more.  */
   28741     36310831 :   if (!nested_p)
   28742     36310743 :     TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
   28743              : 
   28744     36310831 :   maybe_pop_from_top_level (push_to_top);
   28745              : 
   28746     36310831 :   if (current_function_decl)
   28747     14025270 :     restore_omp_privatization_clauses (omp_privatization_save);
   28748     36310831 : }
   28749              : 
   28750              : /* Produce the definition of D, a _DECL generated from a template.  If
   28751              :    DEFER_OK is true, then we don't have to actually do the
   28752              :    instantiation now; we just have to do it sometime.  Normally it is
   28753              :    an error if this is an explicit instantiation but D is undefined.
   28754              :    EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
   28755              :    instantiated class template.  */
   28756              : 
   28757              : tree
   28758    151277880 : instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
   28759              : {
   28760    151277880 :   tree tmpl = DECL_TI_TEMPLATE (d);
   28761    151277880 :   tree gen_args;
   28762    151277880 :   tree args;
   28763    151277880 :   tree td;
   28764    151277880 :   tree code_pattern;
   28765    151277880 :   tree spec;
   28766    151277880 :   tree gen_tmpl;
   28767    151277880 :   bool pattern_defined;
   28768    151277880 :   location_t saved_loc = input_location;
   28769    151277880 :   bool external_p;
   28770    151277880 :   bool deleted_p;
   28771              : 
   28772              :   /* This function should only be used to instantiate templates for
   28773              :      functions and static member variables.  */
   28774    151277880 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
   28775              : 
   28776    151277880 :   gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
   28777              : 
   28778    151277880 :   if (modules_p ())
   28779              :     /* We may have a pending instantiation of D itself.  */
   28780       529801 :     lazy_load_pendings (d);
   28781              : 
   28782              :   /* Variables are never deferred; if instantiation is required, they
   28783              :      are instantiated right away.  That allows for better code in the
   28784              :      case that an expression refers to the value of the variable --
   28785              :      if the variable has a constant value the referring expression can
   28786              :      take advantage of that fact.  */
   28787    151277880 :   if (VAR_P (d))
   28788    106124512 :     defer_ok = false;
   28789              : 
   28790              :   /* Don't instantiate cloned functions.  Instead, instantiate the
   28791              :      functions they cloned.  */
   28792    151277880 :   if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
   28793      5478760 :     d = DECL_CLONED_FUNCTION (d);
   28794              : 
   28795    151277880 :   if (DECL_TEMPLATE_INSTANTIATED (d)
   28796     62230498 :       || TREE_TYPE (d) == error_mark_node
   28797     62230498 :       || (TREE_CODE (d) == FUNCTION_DECL
   28798     42368747 :           && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
   28799    213488919 :       || DECL_TEMPLATE_SPECIALIZATION (d))
   28800              :     /* D has already been instantiated or explicitly specialized, so
   28801              :        there's nothing for us to do here.
   28802              : 
   28803              :        It might seem reasonable to check whether or not D is an explicit
   28804              :        instantiation, and, if so, stop here.  But when an explicit
   28805              :        instantiation is deferred until the end of the compilation,
   28806              :        DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
   28807              :        the instantiation.  */
   28808              :     return d;
   28809              : 
   28810              :   /* Check to see whether we know that this template will be
   28811              :      instantiated in some other file, as with "extern template"
   28812              :      extension.  */
   28813     62025577 :   external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
   28814              : 
   28815              :   /* In general, we do not instantiate such templates.  */
   28816      2314483 :   if (external_p && !always_instantiate_p (d))
   28817              :     return d;
   28818              : 
   28819     62013907 :   gen_tmpl = most_general_template (tmpl);
   28820     62013907 :   gen_args = DECL_TI_ARGS (d);
   28821              : 
   28822              :   /* We should already have the extra args.  */
   28823     70608683 :   gcc_checking_assert (tmpl == gen_tmpl
   28824              :                        || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
   28825              :                            == TMPL_ARGS_DEPTH (gen_args)));
   28826              :   /* And what's in the hash table should match D.  */
   28827     62013907 :   gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
   28828              :                        == d
   28829              :                        || spec == NULL_TREE);
   28830              : 
   28831              :   /* This needs to happen before any tsubsting.  */
   28832     62013907 :   if (! push_tinst_level (d))
   28833              :     return d;
   28834              : 
   28835     62012705 :   auto_timevar tv (TV_TEMPLATE_INST);
   28836              : 
   28837              :   /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
   28838              :      for the instantiation.  */
   28839     62012705 :   td = template_for_substitution (d);
   28840     62012705 :   args = gen_args;
   28841              : 
   28842     62012705 :   if (variable_template_specialization_p (d))
   28843              :     {
   28844              :       /* Look up an explicit specialization, if any.  */
   28845     12469692 :       tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error);
   28846     12469692 :       if (partial_ti && partial_ti != error_mark_node)
   28847              :         {
   28848       839291 :           td = TI_TEMPLATE (partial_ti);
   28849       839291 :           args = TI_ARGS (partial_ti);
   28850              :         }
   28851              :     }
   28852              : 
   28853     62012705 :   maybe_diagnose_erroneous_template (td);
   28854              : 
   28855     62012705 :   code_pattern = DECL_TEMPLATE_RESULT (td);
   28856              : 
   28857              :   /* We should never be trying to instantiate a member of a class
   28858              :      template or partial specialization.  */
   28859     62012705 :   gcc_assert (d != code_pattern);
   28860              : 
   28861    124025410 :   if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
   28862     97659412 :       || DECL_TEMPLATE_SPECIALIZATION (td))
   28863              :     /* In the case of a friend template whose definition is provided
   28864              :        outside the class, we may have too many arguments.  Drop the
   28865              :        ones we don't need.  The same is true for specializations.  */
   28866     26367824 :     args = get_innermost_template_args
   28867     26367824 :       (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
   28868              : 
   28869     62012705 :   if (TREE_CODE (d) == FUNCTION_DECL)
   28870              :     {
   28871     42305241 :       deleted_p = DECL_DELETED_FN (code_pattern);
   28872     42305241 :       pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
   28873     41397523 :                           && DECL_INITIAL (code_pattern) != error_mark_node)
   28874       907762 :                          || DECL_DEFAULTED_FN (code_pattern)
   28875     43212852 :                          || deleted_p);
   28876              :     }
   28877              :   else
   28878              :     {
   28879     19707464 :       deleted_p = false;
   28880     19707464 :       if (DECL_CLASS_SCOPE_P (code_pattern))
   28881      7320091 :         pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
   28882              :       else
   28883     12387373 :         pattern_defined = ! DECL_EXTERNAL (code_pattern);
   28884              :     }
   28885              : 
   28886              :   /* We may be in the middle of deferred access check.  Disable it now.  */
   28887     62012705 :   push_deferring_access_checks (dk_no_deferred);
   28888              : 
   28889              :   /* Unless an explicit instantiation directive has already determined
   28890              :      the linkage of D, remember that a definition is available for
   28891              :      this entity.  */
   28892     62012705 :   if (pattern_defined
   28893     55862467 :       && !DECL_INTERFACE_KNOWN (d)
   28894    114307868 :       && !DECL_NOT_REALLY_EXTERN (d))
   28895     21725401 :     mark_definable (d);
   28896              : 
   28897     62012705 :   DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
   28898     62012705 :   DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
   28899     62012705 :   input_location = DECL_SOURCE_LOCATION (d);
   28900              : 
   28901              :   /* If D is a member of an explicitly instantiated class template,
   28902              :      and no definition is available, treat it like an implicit
   28903              :      instantiation.  */
   28904     62012705 :   if (!pattern_defined && expl_inst_class_mem_p
   28905     62012705 :       && DECL_EXPLICIT_INSTANTIATION (d))
   28906              :     {
   28907              :       /* Leave linkage flags alone on instantiations with anonymous
   28908              :          visibility.  */
   28909          357 :       if (TREE_PUBLIC (d))
   28910              :         {
   28911          348 :           DECL_NOT_REALLY_EXTERN (d) = 0;
   28912          348 :           DECL_INTERFACE_KNOWN (d) = 0;
   28913              :         }
   28914          357 :       SET_DECL_IMPLICIT_INSTANTIATION (d);
   28915              :     }
   28916              : 
   28917              :   /* Defer all other templates, unless we have been explicitly
   28918              :      forbidden from doing so.  */
   28919     62012705 :   if (/* If there is no definition, we cannot instantiate the
   28920              :          template.  */
   28921              :       ! pattern_defined
   28922              :       /* If it's OK to postpone instantiation, do so.  */
   28923     62012705 :       || defer_ok
   28924              :       /* If this is a static data member that will be defined
   28925              :          elsewhere, we don't want to instantiate the entire data
   28926              :          member, but we do want to instantiate the initializer so that
   28927              :          we can substitute that elsewhere.  */
   28928     36395169 :       || (external_p && VAR_P (d))
   28929              :       /* Handle here a deleted function too, avoid generating
   28930              :          its body (c++/61080).  */
   28931     36311030 :       || deleted_p
   28932              :       /* We need the initializer for an OpenMP declare mapper.  */
   28933     98323465 :       || (VAR_P (d) && DECL_LANG_SPECIFIC (d) && DECL_OMP_DECLARE_MAPPER_P (d)))
   28934              :     {
   28935              :       /* The definition of the static data member is now required so
   28936              :          we must substitute the initializer.  */
   28937     25701953 :       if (VAR_P (d)
   28938      5327053 :           && !DECL_INITIAL (d)
   28939     26183102 :           && DECL_INITIAL (code_pattern))
   28940              :         {
   28941       480611 :           tree ns;
   28942       480611 :           tree init;
   28943       480611 :           bool const_init = false;
   28944       480611 :           bool enter_context = DECL_CLASS_SCOPE_P (d);
   28945              : 
   28946       480611 :           ns = decl_namespace_context (d);
   28947       480611 :           push_nested_namespace (ns);
   28948       480611 :           if (enter_context)
   28949       480369 :             push_nested_class (DECL_CONTEXT (d));
   28950       480611 :           init = tsubst_expr (DECL_INITIAL (code_pattern),
   28951              :                               args,
   28952              :                               tf_warning_or_error, NULL_TREE);
   28953              :           /* If instantiating the initializer involved instantiating this
   28954              :              again, don't call cp_finish_decl twice.  */
   28955       472514 :           if (!DECL_INITIAL (d))
   28956              :             {
   28957              :               /* Make sure the initializer is still constant, in case of
   28958              :                  circular dependency (template/instantiate6.C). */
   28959       472511 :               const_init
   28960       472511 :                 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   28961       472511 :               cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
   28962              :                               /*asmspec_tree=*/NULL_TREE, 0);
   28963              :             }
   28964       469817 :           if (enter_context)
   28965       469575 :             pop_nested_class ();
   28966       469817 :           pop_nested_namespace (ns);
   28967              :         }
   28968              : 
   28969              :       /* We restore the source position here because it's used by
   28970              :          add_pending_template.  */
   28971     25691159 :       input_location = saved_loc;
   28972              : 
   28973     17531691 :       if (at_eof && !pattern_defined
   28974      5167693 :           && DECL_EXPLICIT_INSTANTIATION (d)
   28975     25691174 :           && DECL_NOT_REALLY_EXTERN (d))
   28976              :         /* [temp.explicit]
   28977              : 
   28978              :            The definition of a non-exported function template, a
   28979              :            non-exported member function template, or a non-exported
   28980              :            member function or static data member of a class template
   28981              :            shall be present in every translation unit in which it is
   28982              :            explicitly instantiated.  */
   28983           15 :         permerror (input_location,  "explicit instantiation of %qD "
   28984              :                    "but no definition available", d);
   28985              : 
   28986              :       /* If we're in unevaluated context, we just wanted to get the
   28987              :          constant value; this isn't an odr use, so don't queue
   28988              :          a full instantiation.  */
   28989     25691159 :       if (!cp_unevaluated_operand
   28990              :           /* ??? Historically, we have instantiated inline functions, even
   28991              :              when marked as "extern template".  */
   28992     25690664 :           && !(external_p && VAR_P (d)))
   28993     25606525 :         add_pending_template (d);
   28994              :     }
   28995              :   else
   28996              :     {
   28997     36310752 :       set_instantiating_module (d);
   28998     36310752 :       if (variable_template_p (gen_tmpl))
   28999     12467712 :         note_vague_linkage_variable (d);
   29000     36310752 :       instantiate_body (td, args, d, false);
   29001              :     }
   29002              : 
   29003     62001902 :   pop_deferring_access_checks ();
   29004     62001902 :   pop_tinst_level ();
   29005     62001902 :   input_location = saved_loc;
   29006              : 
   29007     62001902 :   return d;
   29008     62001902 : }
   29009              : 
   29010              : /* Run through the list of templates that we wish we could
   29011              :    instantiate, and instantiate any we can.  RETRIES is the
   29012              :    number of times we retry pending template instantiation.  */
   29013              : 
   29014              : void
   29015       142896 : instantiate_pending_templates (int retries)
   29016              : {
   29017       142896 :   int reconsider;
   29018       142896 :   location_t saved_loc = input_location;
   29019       142896 :   unsigned saved_module_kind = module_kind;
   29020              : 
   29021              :   /* Instantiating templates may trigger vtable generation.  This in turn
   29022              :      may require further template instantiations.  We place a limit here
   29023              :      to avoid infinite loop.  */
   29024       142896 :   if (pending_templates && retries >= max_tinst_depth)
   29025              :     {
   29026            3 :       tree decl = pending_templates->tinst->maybe_get_node ();
   29027              : 
   29028            3 :       fatal_error (input_location,
   29029              :                    "template instantiation depth exceeds maximum of %d"
   29030              :                    " instantiating %q+D, possibly from virtual table generation"
   29031              :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   29032              :                    max_tinst_depth, decl);
   29033              :       if (TREE_CODE (decl) == FUNCTION_DECL)
   29034              :         /* Pretend that we defined it.  */
   29035              :         DECL_INITIAL (decl) = error_mark_node;
   29036              :       return;
   29037              :     }
   29038              : 
   29039       180624 :   do
   29040              :     {
   29041       180624 :       struct pending_template **t = &pending_templates;
   29042       180624 :       struct pending_template *last = NULL;
   29043       180624 :       reconsider = 0;
   29044     23111277 :       while (*t)
   29045              :         {
   29046     22930662 :           struct tinst_level *tinst = (*t)->tinst;
   29047     22930662 :           bool complete = tinst_complete_p (tinst);
   29048              : 
   29049     22930662 :           if (!complete)
   29050              :             {
   29051     18209603 :               tree instantiation = reopen_tinst_level (tinst);
   29052              : 
   29053     18209603 :               if (limit_bad_template_recursion (instantiation))
   29054              :                 /* Do nothing.  */;
   29055     18208628 :               else if (TYPE_P (instantiation))
   29056              :                 {
   29057            0 :                   instantiate_class_template (instantiation);
   29058            0 :                   if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
   29059            0 :                     for (tree fld = TYPE_FIELDS (instantiation);
   29060            0 :                          fld; fld = TREE_CHAIN (fld))
   29061            0 :                       if ((VAR_P (fld)
   29062            0 :                            || (TREE_CODE (fld) == FUNCTION_DECL
   29063            0 :                                && !DECL_ARTIFICIAL (fld)))
   29064            0 :                           && DECL_TEMPLATE_INSTANTIATION (fld))
   29065            0 :                         instantiate_decl (fld,
   29066              :                                           /*defer_ok=*/false,
   29067              :                                           /*expl_inst_class_mem_p=*/false);
   29068              : 
   29069            0 :                   if (COMPLETE_TYPE_P (instantiation))
   29070     14813150 :                     reconsider = 1;
   29071              :                 }
   29072              :               else
   29073              :                 {
   29074     18208628 :                   instantiation
   29075     18208628 :                     = instantiate_decl (instantiation,
   29076              :                                         /*defer_ok=*/false,
   29077              :                                         /*expl_inst_class_mem_p=*/false);
   29078     18208619 :                   if (DECL_TEMPLATE_INSTANTIATED (instantiation))
   29079     14813150 :                     reconsider = 1;
   29080              :                 }
   29081              : 
   29082     18209594 :               complete = tinst_complete_p (tinst);
   29083              : 
   29084     18209594 :               tinst_depth = 0;
   29085     18209594 :               set_refcount_ptr (current_tinst_level);
   29086              :             }
   29087              : 
   29088     18209594 :           if (complete)
   29089              :             {
   29090              :               /* If INSTANTIATION has been instantiated, then we don't
   29091              :                  need to consider it again in the future.  */
   29092     19534209 :               struct pending_template *drop = *t;
   29093     19534209 :               *t = (*t)->next;
   29094     19534209 :               set_refcount_ptr (drop->tinst);
   29095     19534209 :               pending_template_freelist ().free (drop);
   29096              :             }
   29097              :           else
   29098              :             {
   29099      3396444 :               last = *t;
   29100      3396444 :               t = &(*t)->next;
   29101              :             }
   29102              :         }
   29103       180615 :       last_pending_template = last;
   29104              :     }
   29105       180615 :   while (reconsider);
   29106              : 
   29107       142884 :   input_location = saved_loc;
   29108       142884 :   module_kind = saved_module_kind;
   29109              : }
   29110              : 
   29111              : /* Substitute ARGVEC into T, which is a list of initializers for
   29112              :    either base class or a non-static data member.  The TREE_PURPOSEs
   29113              :    are DECLs, and the TREE_VALUEs are the initializer values.  Used by
   29114              :    instantiate_decl.  */
   29115              : 
   29116              : static tree
   29117      3069304 : tsubst_initializer_list (tree t, tree argvec)
   29118              : {
   29119      3069304 :   tree inits = NULL_TREE;
   29120      3069304 :   tree target_ctor = error_mark_node;
   29121              : 
   29122      6600416 :   for (; t; t = TREE_CHAIN (t))
   29123              :     {
   29124      3531115 :       tree decl;
   29125      3531115 :       tree init;
   29126      3531115 :       tree expanded_bases = NULL_TREE;
   29127      3531115 :       tree expanded_arguments = NULL_TREE;
   29128      3531115 :       int i, len = 1;
   29129              : 
   29130      3531115 :       if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
   29131              :         {
   29132           31 :           tree expr;
   29133           31 :           tree arg;
   29134              : 
   29135              :           /* Expand the base class expansion type into separate base
   29136              :              classes.  */
   29137           31 :           expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
   29138              :                                                  tf_warning_or_error,
   29139              :                                                  NULL_TREE);
   29140           31 :           if (expanded_bases == error_mark_node)
   29141            0 :             continue;
   29142              : 
   29143              :           /* We'll be building separate TREE_LISTs of arguments for
   29144              :              each base.  */
   29145           31 :           len = TREE_VEC_LENGTH (expanded_bases);
   29146           31 :           expanded_arguments = make_tree_vec (len);
   29147          108 :           for (i = 0; i < len; i++)
   29148           46 :             TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
   29149              : 
   29150              :           /* Build a dummy EXPR_PACK_EXPANSION that will be used to
   29151              :              expand each argument in the TREE_VALUE of t.  */
   29152           31 :           expr = make_node (EXPR_PACK_EXPANSION);
   29153           31 :           PACK_EXPANSION_LOCAL_P (expr) = true;
   29154           62 :           PACK_EXPANSION_PARAMETER_PACKS (expr) =
   29155           31 :             PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
   29156              : 
   29157           31 :           if (TREE_VALUE (t) == void_type_node)
   29158              :             /* VOID_TYPE_NODE is used to indicate
   29159              :                value-initialization.  */
   29160              :             {
   29161            9 :               for (i = 0; i < len; i++)
   29162            3 :                 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
   29163              :             }
   29164              :           else
   29165              :             {
   29166              :               /* Substitute parameter packs into each argument in the
   29167              :                  TREE_LIST.  */
   29168           25 :               in_base_initializer = 1;
   29169           59 :               for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
   29170              :                 {
   29171           34 :                   tree expanded_exprs;
   29172              : 
   29173              :                   /* Expand the argument.  */
   29174           34 :                   tree value;
   29175           34 :                   if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   29176              :                     value = TREE_VALUE (arg);
   29177              :                   else
   29178              :                     {
   29179           31 :                       value = expr;
   29180           31 :                       PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
   29181              :                     }
   29182           34 :                   expanded_exprs
   29183           34 :                     = tsubst_pack_expansion (value, argvec,
   29184              :                                              tf_warning_or_error,
   29185              :                                              NULL_TREE);
   29186           34 :                   if (expanded_exprs == error_mark_node)
   29187            3 :                     continue;
   29188              : 
   29189              :                   /* Prepend each of the expanded expressions to the
   29190              :                      corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
   29191           95 :                   for (i = 0; i < len; i++)
   29192           64 :                     if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   29193           36 :                       for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
   29194           27 :                         TREE_VEC_ELT (expanded_arguments, i)
   29195           54 :                           = tree_cons (NULL_TREE,
   29196           27 :                                        TREE_VEC_ELT (expanded_exprs, j),
   29197           27 :                                        TREE_VEC_ELT (expanded_arguments, i));
   29198              :                     else
   29199           55 :                       TREE_VEC_ELT (expanded_arguments, i)
   29200          110 :                         = tree_cons (NULL_TREE,
   29201           55 :                                      TREE_VEC_ELT (expanded_exprs, i),
   29202           55 :                                      TREE_VEC_ELT (expanded_arguments, i));
   29203              :                 }
   29204           25 :               in_base_initializer = 0;
   29205              : 
   29206              :               /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
   29207              :                  since we built them backwards.  */
   29208           68 :               for (i = 0; i < len; i++)
   29209              :                 {
   29210           43 :                   TREE_VEC_ELT (expanded_arguments, i) =
   29211           43 :                     nreverse (TREE_VEC_ELT (expanded_arguments, i));
   29212              :                 }
   29213              :             }
   29214              :         }
   29215              : 
   29216      7062242 :       for (i = 0; i < len; ++i)
   29217              :         {
   29218      3531130 :           if (expanded_bases)
   29219              :             {
   29220           46 :               decl = TREE_VEC_ELT (expanded_bases, i);
   29221           46 :               decl = expand_member_init (decl);
   29222           46 :               init = TREE_VEC_ELT (expanded_arguments, i);
   29223              :             }
   29224              :           else
   29225              :             {
   29226      3531084 :               tree tmp;
   29227      3531084 :               if (TYPE_P (TREE_PURPOSE (t)))
   29228       615978 :                 decl = tsubst (TREE_PURPOSE (t), argvec,
   29229              :                                tf_warning_or_error, NULL_TREE);
   29230              :               else
   29231      2915106 :                 decl = tsubst_expr (TREE_PURPOSE (t), argvec,
   29232              :                                     tf_warning_or_error, NULL_TREE);
   29233              : 
   29234      3531084 :               decl = expand_member_init (decl);
   29235      3531084 :               if (decl && !DECL_P (decl))
   29236       615972 :                 in_base_initializer = 1;
   29237              : 
   29238      3531084 :               init = TREE_VALUE (t);
   29239      3531084 :               tmp = init;
   29240      3531084 :               if (init != void_type_node)
   29241      3039479 :                 init = tsubst_expr (init, argvec,
   29242              :                                     tf_warning_or_error, NULL_TREE);
   29243      3531084 :               if (init == NULL_TREE && tmp != NULL_TREE)
   29244              :                 /* If we had an initializer but it instantiated to nothing,
   29245              :                    value-initialize the object.  This will only occur when
   29246              :                    the initializer was a pack expansion where the parameter
   29247              :                    packs used in that expansion were of length zero.  */
   29248          640 :                 init = void_type_node;
   29249      3531084 :               in_base_initializer = 0;
   29250              :             }
   29251              : 
   29252      3531130 :           if (target_ctor != error_mark_node
   29253            3 :               && init != error_mark_node)
   29254              :             {
   29255            3 :               error ("mem-initializer for %qD follows constructor delegation",
   29256              :                      decl);
   29257            3 :               return inits;
   29258              :             }
   29259              :           /* Look for a target constructor. */
   29260      3531127 :           if (init != error_mark_node
   29261      3531115 :               && decl && CLASS_TYPE_P (decl)
   29262      3538753 :               && same_type_p (decl, current_class_type))
   29263              :             {
   29264         7626 :               maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
   29265         7626 :               if (inits)
   29266              :                 {
   29267            0 :                   error ("constructor delegation follows mem-initializer for %qD",
   29268            0 :                          TREE_PURPOSE (inits));
   29269            0 :                   continue;
   29270              :                 }
   29271              :               target_ctor = init;
   29272              :             }
   29273              : 
   29274      3531127 :           if (decl)
   29275              :             {
   29276      3531121 :               init = build_tree_list (decl, init);
   29277              :               /* Carry over the dummy TREE_TYPE node containing the source
   29278              :                  location.  */
   29279      3531121 :               TREE_TYPE (init) = TREE_TYPE (t);
   29280      3531121 :               TREE_CHAIN (init) = inits;
   29281      3531121 :               inits = init;
   29282              :             }
   29283              :         }
   29284              :     }
   29285              :   return inits;
   29286              : }
   29287              : 
   29288              : /* Instantiate an enumerated type.  TAG is the template type, NEWTAG
   29289              :    is the instantiation (which should have been created with
   29290              :    start_enum) and ARGS are the template arguments to use.  */
   29291              : 
   29292              : static void
   29293       361943 : tsubst_enum (tree tag, tree newtag, tree args)
   29294              : {
   29295       361943 :   tree e;
   29296              : 
   29297       361943 :   if (SCOPED_ENUM_P (newtag))
   29298          376 :     begin_scope (sk_scoped_enum, newtag);
   29299              : 
   29300       361943 :   ENUM_BEING_DEFINED_P (newtag) = 1;
   29301              : 
   29302       772221 :   for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
   29303              :     {
   29304       410323 :       tree value;
   29305       410323 :       tree decl = TREE_VALUE (e);
   29306              : 
   29307              :       /* Note that in a template enum, the TREE_VALUE is the
   29308              :          CONST_DECL, not the corresponding INTEGER_CST.  */
   29309       410323 :       value = tsubst_expr (DECL_INITIAL (decl),
   29310              :                            args, tf_warning_or_error, NULL_TREE);
   29311              : 
   29312              :       /* Give this enumeration constant the correct access.  */
   29313       410278 :       set_current_access_from_decl (decl);
   29314              : 
   29315              :       /* Actually build the enumerator itself.  Here we're assuming that
   29316              :          enumerators can't have dependent attributes.  */
   29317       410278 :       tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
   29318       410278 :                                        DECL_ATTRIBUTES (decl),
   29319       410278 :                                        DECL_SOURCE_LOCATION (decl));
   29320              :       /* Attribute deprecated without an argument isn't sticky: it'll
   29321              :          melt into a tree flag, so we need to propagate the flag here,
   29322              :          since we just created a new enumerator.  */
   29323       410278 :       TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
   29324       410278 :       TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
   29325              :     }
   29326              : 
   29327       361898 :   if (SCOPED_ENUM_P (newtag))
   29328          376 :     finish_scope ();
   29329              : 
   29330       361898 :   finish_enum_value_list (newtag);
   29331       361898 :   finish_enum (newtag);
   29332              : 
   29333       361898 :   DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
   29334       361898 :     = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
   29335       361898 :   TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
   29336       361898 :   TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
   29337       361898 : }
   29338              : 
   29339              : /* DECL is a FUNCTION_DECL that is a template specialization.  Return
   29340              :    its type -- but without substituting the innermost set of template
   29341              :    arguments.  So, innermost set of template parameters will appear in
   29342              :    the type.  */
   29343              : 
   29344              : tree
   29345     15857722 : get_mostly_instantiated_function_type (tree decl)
   29346              : {
   29347              :   /* For a function, DECL_TI_TEMPLATE is partially instantiated.  */
   29348     15857722 :   return TREE_TYPE (DECL_TI_TEMPLATE (decl));
   29349              : }
   29350              : 
   29351              : /* Return truthvalue if we're processing a template different from
   29352              :    the last one involved in diagnostics.  */
   29353              : bool
   29354       243836 : problematic_instantiation_changed (void)
   29355              : {
   29356       243836 :   return current_tinst_level != last_error_tinst_level;
   29357              : }
   29358              : 
   29359              : /* Remember current template involved in diagnostics.  */
   29360              : void
   29361         5359 : record_last_problematic_instantiation (void)
   29362              : {
   29363         5359 :   set_refcount_ptr (last_error_tinst_level, current_tinst_level);
   29364         5359 : }
   29365              : 
   29366              : struct tinst_level *
   29367    243252227 : current_instantiation (void)
   29368              : {
   29369    243252227 :   return current_tinst_level;
   29370              : }
   29371              : 
   29372              : /* Return TRUE if current_function_decl is being instantiated, false
   29373              :    otherwise.  */
   29374              : 
   29375              : bool
   29376    239213977 : instantiating_current_function_p (void)
   29377              : {
   29378    239213977 :   return (current_instantiation ()
   29379    239213977 :           && (current_instantiation ()->maybe_get_node ()
   29380      2733855 :               == current_function_decl));
   29381              : }
   29382              : 
   29383              : /* [temp.param] Check that template non-type parm TYPE is of an allowable
   29384              :    type.  Return false for ok, true for disallowed.  Issue error and
   29385              :    inform messages under control of COMPLAIN.  */
   29386              : 
   29387              : static bool
   29388    174699177 : invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
   29389              : {
   29390    174699177 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
   29391              :     return false;
   29392      2501785 :   else if (TYPE_PTR_P (type))
   29393              :     return false;
   29394      2498660 :   else if (TYPE_REF_P (type)
   29395      2498660 :            && !TYPE_REF_IS_RVALUE (type))
   29396              :     return false;
   29397      2497567 :   else if (TYPE_PTRMEM_P (type))
   29398              :     return false;
   29399      2494090 :   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
   29400              :     {
   29401      2334721 :       if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
   29402              :         {
   29403            1 :           if (complain & tf_error)
   29404            1 :             error ("non-type template parameters of deduced class type only "
   29405              :                    "available with %<-std=c++20%> or %<-std=gnu++20%>");
   29406            1 :           return true;
   29407              :         }
   29408              :       return false;
   29409              :     }
   29410       159369 :   else if (TREE_CODE (type) == NULLPTR_TYPE)
   29411              :     return false;
   29412       159320 :   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
   29413           18 :            && cxx_dialect < cxx11)
   29414              :     /* Fall through; before C++11 alias templates, a bound ttp
   29415              :        always instantiates into a class type.  */;
   29416       159320 :   else if (WILDCARD_TYPE_P (type))
   29417              :     /* Any other wildcard type not already handled above is allowed.  */
   29418              :     return false;
   29419              :   else if (TREE_CODE (type) == COMPLEX_TYPE)
   29420              :     /* Fall through.  */;
   29421              :   else if (VOID_TYPE_P (type))
   29422              :     /* Fall through.  */;
   29423        36716 :   else if (cxx_dialect >= cxx20)
   29424              :     {
   29425        36663 :       if (dependent_type_p (type))
   29426              :         return false;
   29427        36580 :       if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
   29428              :         return true;
   29429        36547 :       if (structural_type_p (type))
   29430              :         return false;
   29431           34 :       if (complain & tf_error)
   29432              :         {
   29433           11 :           auto_diagnostic_group d;
   29434           11 :           error ("%qT is not a valid type for a template non-type "
   29435              :                  "parameter because it is not structural", type);
   29436           11 :           structural_type_p (type, true);
   29437           11 :         }
   29438           34 :       return true;
   29439              :     }
   29440           53 :   else if (CLASS_TYPE_P (type))
   29441              :     {
   29442            9 :       if (complain & tf_error)
   29443            7 :         error ("non-type template parameters of class type only available "
   29444              :                "with %<-std=c++20%> or %<-std=gnu++20%>");
   29445            9 :       return true;
   29446              :     }
   29447              : 
   29448          133 :   if (complain & tf_error)
   29449              :     {
   29450          128 :       if (type == error_mark_node)
   29451           11 :         inform (input_location, "invalid template non-type parameter");
   29452              :       else
   29453          117 :         error ("%q#T is not a valid type for a template non-type parameter",
   29454              :                type);
   29455              :     }
   29456              :   return true;
   29457              : }
   29458              : 
   29459              : /* Returns true iff the noexcept-specifier for TYPE is value-dependent.  */
   29460              : 
   29461              : static bool
   29462     16921411 : value_dependent_noexcept_spec_p (tree type)
   29463              : {
   29464     16921411 :   if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
   29465      7938301 :     if (tree noex = TREE_PURPOSE (spec))
   29466              :       /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
   29467              :          affect overload resolution and treating it as dependent breaks
   29468              :          things.  Same for an unparsed noexcept expression.  */
   29469      7937992 :       if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   29470      7937992 :           && TREE_CODE (noex) != DEFERRED_PARSE
   29471      7937992 :           && value_dependent_expression_p (noex))
   29472              :         return true;
   29473              : 
   29474              :   return false;
   29475              : }
   29476              : 
   29477              : /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
   29478              :    Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
   29479              : 
   29480              : static bool
   29481    579849337 : dependent_type_p_r (tree type)
   29482              : {
   29483    579849337 :   tree scope;
   29484              : 
   29485              :   /* [temp.dep.type]
   29486              : 
   29487              :      A type is dependent if it is:
   29488              : 
   29489              :      -- a template parameter. Template template parameters are types
   29490              :         for us (since TYPE_P holds true for them) so we handle
   29491              :         them here.  */
   29492    579849337 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   29493    579849337 :       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
   29494              :     return true;
   29495              :   /* -- a qualified-id with a nested-name-specifier which contains a
   29496              :         class-name that names a dependent type or whose unqualified-id
   29497              :         names a dependent type.  */
   29498    458202202 :   if (TREE_CODE (type) == TYPENAME_TYPE)
   29499              :     return true;
   29500              : 
   29501              :   /* -- a cv-qualified type where the cv-unqualified type is
   29502              :         dependent.
   29503              :      No code is necessary for this bullet; the code below handles
   29504              :      cv-qualified types, and we don't want to strip aliases with
   29505              :      TYPE_MAIN_VARIANT because of DR 1558.  */
   29506              :   /* -- a compound type constructed from any dependent type.  */
   29507    429103660 :   if (TYPE_PTRMEM_P (type))
   29508       929077 :     return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
   29509       929077 :             || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
   29510              :                                            (type)));
   29511    428174583 :   else if (INDIRECT_TYPE_P (type))
   29512     85406327 :     return dependent_type_p (TREE_TYPE (type));
   29513    342768256 :   else if (FUNC_OR_METHOD_TYPE_P (type))
   29514              :     {
   29515     89731030 :       tree arg_type;
   29516              : 
   29517     89731030 :       if (dependent_type_p (TREE_TYPE (type)))
   29518              :         return true;
   29519     52605795 :       for (arg_type = TYPE_ARG_TYPES (type);
   29520    102197183 :            arg_type;
   29521     49591388 :            arg_type = TREE_CHAIN (arg_type))
   29522     85212374 :         if (dependent_type_p (TREE_VALUE (arg_type)))
   29523              :           return true;
   29524     16984809 :       if (cxx_dialect >= cxx17
   29525     16984809 :           && value_dependent_noexcept_spec_p (type))
   29526              :         /* A value-dependent noexcept-specifier makes the type dependent.  */
   29527              :         return true;
   29528     16984770 :       return false;
   29529              :     }
   29530              :   /* -- an array type constructed from any dependent type or whose
   29531              :         size is specified by a constant expression that is
   29532              :         value-dependent.
   29533              : 
   29534              :         We checked for type- and value-dependence of the bounds in
   29535              :         compute_array_index_type, so TYPE_DEPENDENT_P is already set.  */
   29536    253037226 :   if (TREE_CODE (type) == ARRAY_TYPE)
   29537              :     {
   29538        12013 :       if (TYPE_DOMAIN (type)
   29539        12013 :           && dependent_type_p (TYPE_DOMAIN (type)))
   29540              :         return true;
   29541        12013 :       return dependent_type_p (TREE_TYPE (type));
   29542              :     }
   29543              : 
   29544              :   /* -- a template-id in which either the template name is a template
   29545              :      parameter ...  */
   29546    253025213 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   29547              :     return true;
   29548              :   /* ... or any of the template arguments is a dependent type or
   29549              :         an expression that is type-dependent or value-dependent.  */
   29550    126189828 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   29551    372188086 :            && (any_dependent_template_arguments_p
   29552    119404814 :                (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
   29553              :     return true;
   29554              : 
   29555              :   /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
   29556              :      dependent; if the argument of the `typeof' expression is not
   29557              :      type-dependent, then it should already been have resolved.  */
   29558    179689833 :   if (TREE_CODE (type) == TYPEOF_TYPE
   29559    158788653 :       || TREE_CODE (type) == DECLTYPE_TYPE
   29560    152037975 :       || TREE_CODE (type) == TRAIT_TYPE)
   29561              :     return true;
   29562              : 
   29563              :   /* A template argument pack is dependent if any of its packed
   29564              :      arguments are.  */
   29565    151776613 :   if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
   29566              :     {
   29567          227 :       tree args = ARGUMENT_PACK_ARGS (type);
   29568          275 :       for (tree arg : tree_vec_range (args))
   29569          218 :         if (dependent_template_arg_p (arg))
   29570          170 :           return true;
   29571              :     }
   29572              : 
   29573              :   /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
   29574              :      be template parameters.  This includes pack-index-specifiers.  */
   29575    151776443 :   if (TREE_CODE (type) == TYPE_PACK_EXPANSION
   29576    139292046 :       || TREE_CODE (type) == PACK_INDEX_TYPE)
   29577              :     return true;
   29578              : 
   29579    139291938 :   if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
   29580              :     return true;
   29581              : 
   29582              :   /* A splice-scope-specifier is dependent if its splice-specifier
   29583              :      or splice-specialization-specifier is dependent.  */
   29584     56706124 :   if (TREE_CODE (type) == SPLICE_SCOPE)
   29585              :     return true;
   29586              : 
   29587     56705959 :   if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
   29588              :     return true;
   29589              : 
   29590              :   /* The standard does not specifically mention types that are local
   29591              :      to template functions or local classes, but they should be
   29592              :      considered dependent too.  For example:
   29593              : 
   29594              :        template <int I> void f() {
   29595              :          enum E { a = I };
   29596              :          S<sizeof (E)> s;
   29597              :        }
   29598              : 
   29599              :      The size of `E' cannot be known until the value of `I' has been
   29600              :      determined.  Therefore, `E' must be considered dependent.  */
   29601     56705959 :   scope = TYPE_CONTEXT (type);
   29602     56705959 :   if (scope && TYPE_P (scope))
   29603      2923726 :     return dependent_type_p (scope);
   29604              :   /* Don't use type_dependent_expression_p here, as it can lead
   29605              :      to infinite recursion trying to determine whether a lambda
   29606              :      nested in a lambda is dependent (c++/47687).  */
   29607     51133660 :   else if (scope && TREE_CODE (scope) == FUNCTION_DECL
   29608      1225097 :            && DECL_LANG_SPECIFIC (scope)
   29609      1225097 :            && DECL_TEMPLATE_INFO (scope)
   29610       945776 :            && (any_dependent_template_arguments_p
   29611       945776 :                (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
   29612              :     return true;
   29613              : 
   29614              :   /* Other types are non-dependent.  */
   29615              :   return false;
   29616              : }
   29617              : 
   29618              : /* Returns TRUE if TYPE is dependent, in the sense of
   29619              :    [temp.dep.type].  Note that a NULL type is considered dependent.  */
   29620              : 
   29621              : bool
   29622  19607178843 : dependent_type_p (tree type)
   29623              : {
   29624              :   /* If there are no template parameters in scope, then there can't be
   29625              :      any dependent types.  */
   29626  19607178843 :   if (!processing_template_decl)
   29627              :     {
   29628              :       /* If we are not processing a template, then nobody should be
   29629              :          providing us with a dependent type.  */
   29630   3682946479 :       gcc_assert (type);
   29631   3682946479 :       gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)
   29632              :                   || seen_error ());
   29633   3682946479 :       return false;
   29634              :     }
   29635              : 
   29636              :   /* If the type is NULL, we have not computed a type for the entity
   29637              :      in question; in that case, the type is dependent.  */
   29638  15924232364 :   if (!type)
   29639              :     return true;
   29640              : 
   29641              :   /* Erroneous types can be considered non-dependent.  */
   29642  15647431655 :   if (type == error_mark_node)
   29643              :     return false;
   29644              : 
   29645              :   /* If we have not already computed the appropriate value for TYPE,
   29646              :      do so now.  */
   29647  15647431524 :   if (!TYPE_DEPENDENT_P_VALID (type))
   29648              :     {
   29649    579849337 :       TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
   29650    579849337 :       TYPE_DEPENDENT_P_VALID (type) = 1;
   29651              :     }
   29652              : 
   29653  15647431524 :   return TYPE_DEPENDENT_P (type);
   29654              : }
   29655              : 
   29656              : /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
   29657              :    lookup.  In other words, a dependent type that is not the current
   29658              :    instantiation.  */
   29659              : 
   29660              : bool
   29661   1361659671 : dependent_scope_p (tree scope)
   29662              : {
   29663   1361659671 :   return (scope && TYPE_P (scope) && dependent_type_p (scope)
   29664   2189571668 :           && !currently_open_class (scope));
   29665              : }
   29666              : 
   29667              : /* True if we might find more declarations in SCOPE during instantiation than
   29668              :    we can when parsing the template.  */
   29669              : 
   29670              : bool
   29671    437482241 : dependentish_scope_p (tree scope)
   29672              : {
   29673    437482241 :   return dependent_scope_p (scope) || any_dependent_bases_p (scope);
   29674              : }
   29675              : 
   29676              : /* Returns TRUE if NS is a dependent namespace, in which we can't do any
   29677              :    lookup.  */
   29678              : 
   29679              : bool
   29680    624201143 : dependent_namespace_p (tree ns)
   29681              : {
   29682    624201143 :   if (TREE_CODE (ns) == NAMESPACE_DECL)
   29683    491763507 :     ns = ORIGINAL_NAMESPACE (ns);
   29684    624201143 :   return TREE_CODE (ns) == SPLICE_EXPR;
   29685              : }
   29686              : 
   29687              : /* T is a SCOPE_REF.  Return whether it represents a non-static member of
   29688              :    an unknown base of 'this' (and is therefore instantiation-dependent).  */
   29689              : 
   29690              : static bool
   29691      4235851 : unknown_base_ref_p (tree t)
   29692              : {
   29693      4235851 :   if (!current_class_ptr)
   29694              :     return false;
   29695              : 
   29696      1845732 :   tree mem = TREE_OPERAND (t, 1);
   29697      1845732 :   if (shared_member_p (mem))
   29698              :     return false;
   29699              : 
   29700            7 :   tree cur = current_nonlambda_class_type ();
   29701            7 :   if (!any_dependent_bases_p (cur))
   29702              :     return false;
   29703              : 
   29704            0 :   tree ctx = TREE_OPERAND (t, 0);
   29705            0 :   if (DERIVED_FROM_P (ctx, cur))
   29706              :     return false;
   29707              : 
   29708              :   return true;
   29709              : }
   29710              : 
   29711              : /* T is a SCOPE_REF; return whether we need to consider it
   29712              :     instantiation-dependent so that we can check access at instantiation
   29713              :     time even though we know which member it resolves to.  */
   29714              : 
   29715              : static bool
   29716      9095803 : instantiation_dependent_scope_ref_p (tree t)
   29717              : {
   29718      9095803 :   if (DECL_P (TREE_OPERAND (t, 1))
   29719      4235863 :       && CLASS_TYPE_P (TREE_OPERAND (t, 0))
   29720      4235863 :       && !dependent_scope_p (TREE_OPERAND (t, 0))
   29721      4235851 :       && !unknown_base_ref_p (t)
   29722     13331654 :       && accessible_in_template_p (TREE_OPERAND (t, 0),
   29723      4235851 :                                    TREE_OPERAND (t, 1)))
   29724              :     return false;
   29725              :   else
   29726      4860174 :     return true;
   29727              : }
   29728              : 
   29729              : /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
   29730              :    [temp.dep.constexpr].  EXPRESSION is already known to be a constant
   29731              :    expression.  */
   29732              : 
   29733              : /* Note that this predicate is not appropriate for general expressions;
   29734              :    only constant expressions (that satisfy potential_constant_expression)
   29735              :    can be tested for value dependence.  */
   29736              : 
   29737              : bool
   29738   1381047501 : value_dependent_expression_p (tree expression)
   29739              : {
   29740   1409887170 :   if (!processing_template_decl || expression == NULL_TREE)
   29741              :     return false;
   29742              : 
   29743              :   /* A type-dependent expression is also value-dependent.  */
   29744   1114268096 :   if (type_dependent_expression_p (expression))
   29745              :     return true;
   29746              : 
   29747    963004887 :   switch (TREE_CODE (expression))
   29748              :     {
   29749       835830 :     case BASELINK:
   29750              :       /* A dependent member function of the current instantiation.  */
   29751       835830 :       return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
   29752              : 
   29753     28374824 :     case FUNCTION_DECL:
   29754              :       /* A dependent member function of the current instantiation.  */
   29755     56748895 :       if (DECL_CLASS_SCOPE_P (expression)
   29756     46718616 :           && dependent_type_p (DECL_CONTEXT (expression)))
   29757              :         return true;
   29758              :       break;
   29759              : 
   29760              :     case IDENTIFIER_NODE:
   29761              :       /* A name that has not been looked up -- must be dependent.  */
   29762              :       return true;
   29763              : 
   29764              :     case TEMPLATE_PARM_INDEX:
   29765              :       /* A non-type template parm.  */
   29766              :       return true;
   29767              : 
   29768       628983 :     case CONST_DECL:
   29769              :       /* A non-type template parm.  */
   29770       628983 :       if (DECL_TEMPLATE_PARM_P (expression))
   29771              :         return true;
   29772       628983 :       return value_dependent_expression_p (DECL_INITIAL (expression));
   29773              : 
   29774     19981807 :     case VAR_DECL:
   29775              :        /* A constant with literal type and is initialized
   29776              :           with an expression that is value-dependent.  */
   29777     19981807 :       if (DECL_DEPENDENT_INIT_P (expression))
   29778              :         return true;
   29779     10759011 :       if (DECL_HAS_VALUE_EXPR_P (expression))
   29780              :         {
   29781        63274 :           tree value_expr = DECL_VALUE_EXPR (expression);
   29782        63274 :           if (value_dependent_expression_p (value_expr)
   29783              :               /* __PRETTY_FUNCTION__ inside a template function is dependent
   29784              :                  on the name of the function.  */
   29785        63274 :               || (DECL_PRETTY_FUNCTION_P (expression)
   29786              :                   /* It might be used in a template, but not a template
   29787              :                      function, in which case its DECL_VALUE_EXPR will be
   29788              :                      "top level".  */
   29789            5 :                   && value_expr == error_mark_node))
   29790              :             return true;
   29791              :         }
   29792     10695737 :       else if (TYPE_REF_P (TREE_TYPE (expression)))
   29793              :         /* FIXME cp_finish_decl doesn't fold reference initializers.  */
   29794              :         return true;
   29795              :       /* We have a constexpr variable and we're processing a template.  When
   29796              :          there's lifetime extension involved (for which finish_compound_literal
   29797              :          used to create a temporary), we'll not be able to evaluate the
   29798              :          variable until instantiating, so pretend it's value-dependent.  */
   29799     10683817 :       else if (DECL_DECLARED_CONSTEXPR_P (expression)
   29800     10683817 :                && !TREE_CONSTANT (expression))
   29801              :         return true;
   29802              :       return false;
   29803              : 
   29804     31829981 :     case DYNAMIC_CAST_EXPR:
   29805     31829981 :     case STATIC_CAST_EXPR:
   29806     31829981 :     case CONST_CAST_EXPR:
   29807     31829981 :     case REINTERPRET_CAST_EXPR:
   29808     31829981 :     case CAST_EXPR:
   29809     31829981 :     case IMPLICIT_CONV_EXPR:
   29810              :       /* These expressions are value-dependent if the type to which
   29811              :          the cast occurs is dependent or the expression being casted
   29812              :          is value-dependent.  */
   29813     31829981 :       {
   29814     31829981 :         tree type = TREE_TYPE (expression);
   29815              : 
   29816     31829981 :         if (dependent_type_p (type))
   29817              :           return true;
   29818              : 
   29819              :         /* A functional cast has a list of operands.  */
   29820     31829981 :         expression = TREE_OPERAND (expression, 0);
   29821     31829981 :         if (!expression)
   29822              :           {
   29823              :             /* If there are no operands, it must be an expression such
   29824              :                as "int()". This should not happen for aggregate types
   29825              :                because it would form non-constant expressions.  */
   29826          137 :             gcc_assert (cxx_dialect >= cxx11
   29827              :                         || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
   29828              : 
   29829              :             return false;
   29830              :           }
   29831              : 
   29832     31829844 :         if (TREE_CODE (expression) == TREE_LIST)
   29833      3619132 :           return any_value_dependent_elements_p (expression);
   29834              : 
   29835     28210712 :         if (TREE_CODE (type) == REFERENCE_TYPE
   29836     28210712 :             && has_value_dependent_address (expression))
   29837              :           return true;
   29838              : 
   29839     28210686 :         return value_dependent_expression_p (expression);
   29840              :       }
   29841              : 
   29842      9235651 :     case SIZEOF_EXPR:
   29843      9235651 :       if (SIZEOF_EXPR_TYPE_P (expression))
   29844            4 :         return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
   29845      9235647 :       if (tree p = TREE_OPERAND (expression, 0))
   29846      9235647 :         if (PACK_EXPANSION_P (p)
   29847      9235647 :             && DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (p)))
   29848              :           {
   29849          804 :             tree d = PACK_EXPANSION_PATTERN (p);
   29850          804 :             if (DECL_HAS_VALUE_EXPR_P (d))
   29851              :               {
   29852          804 :                 d = DECL_VALUE_EXPR (d);
   29853              :                 /* [temp.dep.constexpr]/4:
   29854              :                    Expressions of the following form are value-dependent:
   29855              :                    sizeof ... ( identifier )
   29856              :                    unless the identifier is a structured binding pack whose
   29857              :                    initializer is not dependent.  */
   29858          804 :                 if (TREE_CODE (d) == TREE_VEC
   29859          804 :                     && !type_dependent_expression_p (TREE_VEC_ELT (d, 0)))
   29860              :                   return false;
   29861              :               }
   29862              :           }
   29863              :       /* FALLTHRU */
   29864      9983929 :     case ALIGNOF_EXPR:
   29865      9983929 :     case TYPEID_EXPR:
   29866              :       /* A `sizeof' expression is value-dependent if the operand is
   29867              :          type-dependent or is a pack expansion.  */
   29868      9983929 :       expression = TREE_OPERAND (expression, 0);
   29869      9983929 :       if (PACK_EXPANSION_P (expression))
   29870              :         return true;
   29871      5175058 :       else if (TYPE_P (expression))
   29872      5101089 :         return dependent_type_p (expression);
   29873        73969 :       return instantiation_dependent_uneval_expression_p (expression);
   29874              : 
   29875            0 :     case AT_ENCODE_EXPR:
   29876              :       /* An 'encode' expression is value-dependent if the operand is
   29877              :          type-dependent.  */
   29878            0 :       expression = TREE_OPERAND (expression, 0);
   29879            0 :       return dependent_type_p (expression);
   29880              : 
   29881       586375 :     case NOEXCEPT_EXPR:
   29882       586375 :       expression = TREE_OPERAND (expression, 0);
   29883       586375 :       return instantiation_dependent_uneval_expression_p (expression);
   29884              : 
   29885      2938629 :     case SCOPE_REF:
   29886              :       /* All instantiation-dependent expressions should also be considered
   29887              :          value-dependent.  */
   29888      2938629 :       return instantiation_dependent_scope_ref_p (expression);
   29889              : 
   29890      1200373 :     case COMPONENT_REF:
   29891      1200373 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   29892      1200373 :               || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
   29893              : 
   29894            0 :     case NONTYPE_ARGUMENT_PACK:
   29895              :       /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
   29896              :          is value-dependent.  */
   29897            0 :       for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
   29898            0 :         if (value_dependent_expression_p (arg))
   29899            0 :           return true;
   29900            0 :       return false;
   29901              : 
   29902     19025940 :     case TRAIT_EXPR:
   29903     19025940 :       {
   29904     19025940 :         if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
   29905              :           return true;
   29906              : 
   29907        46181 :         tree type2 = TRAIT_EXPR_TYPE2 (expression);
   29908        46181 :         if (!type2)
   29909              :           return false;
   29910              : 
   29911        46177 :         if (TREE_CODE (type2) != TREE_VEC)
   29912        46133 :           return dependent_type_p (type2);
   29913              : 
   29914           56 :         for (tree arg : tree_vec_range (type2))
   29915           41 :           if (dependent_type_p (arg))
   29916           29 :             return true;
   29917              : 
   29918           15 :         return false;
   29919              :       }
   29920              : 
   29921           30 :     case MODOP_EXPR:
   29922           30 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   29923           30 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
   29924              : 
   29925       971538 :     case ARRAY_REF:
   29926       971538 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   29927       971538 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
   29928              : 
   29929       247403 :     case ADDR_EXPR:
   29930       247403 :       {
   29931       247403 :         tree op = TREE_OPERAND (expression, 0);
   29932       247403 :         return (value_dependent_expression_p (op)
   29933       247403 :                 || has_value_dependent_address (op));
   29934              :       }
   29935              : 
   29936              :     case REQUIRES_EXPR:
   29937              :       /* Treat all requires-expressions as value-dependent so
   29938              :          we don't try to fold them.  */
   29939              :       return true;
   29940              : 
   29941            0 :     case TYPE_REQ:
   29942            0 :       return dependent_type_p (TREE_OPERAND (expression, 0));
   29943              : 
   29944      4556751 :     case CALL_EXPR:
   29945      4556751 :       {
   29946      4556751 :         if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
   29947              :           return true;
   29948      4215843 :         tree fn = get_callee_fndecl (expression);
   29949      4215843 :         int i, nargs;
   29950      4215843 :         nargs = call_expr_nargs (expression);
   29951      5491474 :         for (i = 0; i < nargs; ++i)
   29952              :           {
   29953      2041736 :             tree op = CALL_EXPR_ARG (expression, i);
   29954              :             /* In a call to a constexpr member function, look through the
   29955              :                implicit ADDR_EXPR on the object argument so that it doesn't
   29956              :                cause the call to be considered value-dependent.  We also
   29957              :                look through it in potential_constant_expression.  */
   29958        22074 :             if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
   29959        22074 :                 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   29960      2041738 :                 && TREE_CODE (op) == ADDR_EXPR)
   29961            1 :               op = TREE_OPERAND (op, 0);
   29962      2041736 :             if (value_dependent_expression_p (op))
   29963              :               return true;
   29964              :           }
   29965      3483088 :         if (flag_reflection && !fn && CALL_EXPR_FN (expression))
   29966              :           {
   29967        33350 :             fn = MAYBE_BASELINK_FUNCTIONS (CALL_EXPR_FN (expression));
   29968        33350 :             if (fn && TREE_CODE (fn) != FUNCTION_DECL)
   29969         3541 :               fn = NULL_TREE;
   29970              :           }
   29971              :         /* [meta.reflection.access.context]/8: An invocation of current that
   29972              :            appears at a program point P is value-dependent if eval-point(P)
   29973              :            is enclosed by a scope corresponding to a templated entity.  */
   29974      3449738 :         if (flag_reflection
   29975        33967 :             && fn
   29976        30426 :             && metafunction_p (fn))
   29977              :           {
   29978         1660 :             if (id_equal (DECL_NAME (fn), "current")
   29979           16 :                 && DECL_CLASS_SCOPE_P (fn)
   29980         1676 :                 && id_equal (TYPE_IDENTIFIER (DECL_CONTEXT (fn)),
   29981              :                              "access_context"))
   29982              :               return true;
   29983              :             /* Similarly for these 3 metafns.  */
   29984         1644 :             if (id_equal (DECL_NAME (fn), "current_function")
   29985         1644 :                 || id_equal (DECL_NAME (fn), "current_class")
   29986         3288 :                 || id_equal (DECL_NAME (fn), "current_namespace"))
   29987              :               return true;
   29988              :           }
   29989              : 
   29990              :         return false;
   29991              :       }
   29992              : 
   29993      8749032 :     case TEMPLATE_ID_EXPR:
   29994      8749032 :       return concept_definition_p (TREE_OPERAND (expression, 0))
   29995      8749032 :         && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
   29996              : 
   29997      3585172 :     case CONSTRUCTOR:
   29998      3585172 :       {
   29999      3585172 :         unsigned ix;
   30000      3585172 :         tree val;
   30001      3585172 :         if (dependent_type_p (TREE_TYPE (expression)))
   30002              :           return true;
   30003      3737745 :         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
   30004       153267 :           if (value_dependent_expression_p (val))
   30005              :             return true;
   30006              :         return false;
   30007              :       }
   30008              : 
   30009              :     case STMT_EXPR:
   30010              :       /* Treat a GNU statement expression as dependent to avoid crashing
   30011              :          under instantiate_non_dependent_expr; it can't be constant.  */
   30012              :       return true;
   30013              : 
   30014            8 :     case NEW_EXPR:
   30015            8 :     case VEC_NEW_EXPR:
   30016              :       /* The second operand is a type, which type_dependent_expression_p
   30017              :          (and therefore value_dependent_expression_p) doesn't want to see.  */
   30018            8 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   30019            8 :               || value_dependent_expression_p (TREE_OPERAND (expression, 2))
   30020           16 :               || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
   30021              : 
   30022         8465 :     case REFLECT_EXPR:
   30023              :       /* [temp.dep.constexpr] A reflect-expression is value-dependent
   30024              :          if it contains a dependent nested-name-specifier, type-id,
   30025              :          namespace-name, or template-name, or if it contains
   30026              :          a value-dependent or type-dependent id-expression.  */
   30027         8465 :       if (REFLECT_EXPR_KIND (expression) == REFLECT_BASE)
   30028              :         /* Direct base relationship isn't value-dependent and calling
   30029              :            uses_template_parms on TREE_BINFO leads to ICEs.  */
   30030              :         return false;
   30031         8372 :       if (REFLECT_EXPR_KIND (expression) == REFLECT_DATA_MEMBER_SPEC)
   30032              :         {
   30033              :           /* Data member description is value dependent if the type is
   30034              :              dependent, other optional fields shouldn't be ever dependent.  */
   30035          136 :           tree h = REFLECT_EXPR_HANDLE (expression);
   30036          136 :           return dependent_type_p (TREE_VEC_ELT (h, 0));
   30037              :         }
   30038         8236 :       return uses_template_parms (REFLECT_EXPR_HANDLE (expression));
   30039              : 
   30040    716549947 :     default:
   30041              :       /* A constant expression is value-dependent if any subexpression is
   30042              :          value-dependent.  */
   30043    716549947 :       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
   30044              :         {
   30045     71031337 :         case tcc_reference:
   30046     71031337 :         case tcc_unary:
   30047     71031337 :         case tcc_comparison:
   30048     71031337 :         case tcc_binary:
   30049     71031337 :         case tcc_expression:
   30050     71031337 :         case tcc_vl_exp:
   30051     71031337 :           {
   30052     71031337 :             int i, len = cp_tree_operand_length (expression);
   30053              : 
   30054    121871034 :             for (i = 0; i < len; i++)
   30055              :               {
   30056     80720256 :                 tree t = TREE_OPERAND (expression, i);
   30057              : 
   30058              :                 /* In some cases, some of the operands may be missing.
   30059              :                    (For example, in the case of PREDECREMENT_EXPR, the
   30060              :                    amount to increment by may be missing.)  That doesn't
   30061              :                    make the expression dependent.  */
   30062     80720256 :                 if (t && value_dependent_expression_p (t))
   30063              :                   return true;
   30064              :               }
   30065              :           }
   30066              :           break;
   30067              :         default:
   30068              :           break;
   30069              :         }
   30070              :       break;
   30071              :     }
   30072              : 
   30073              :   /* The expression is not value-dependent.  */
   30074              :   return false;
   30075              : }
   30076              : 
   30077              : /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
   30078              :    [temp.dep.expr].  Note that an expression with no type is
   30079              :    considered dependent.  Other parts of the compiler arrange for an
   30080              :    expression with type-dependent subexpressions to have no type, so
   30081              :    this function doesn't have to be fully recursive.  */
   30082              : 
   30083              : bool
   30084   4415002495 : type_dependent_expression_p (tree expression)
   30085              : {
   30086   4415005909 :   if (!processing_template_decl)
   30087              :     return false;
   30088              : 
   30089   3714268503 :   if (expression == NULL_TREE || expression == error_mark_node)
   30090              :     return false;
   30091              : 
   30092   3713129743 :   gcc_checking_assert (!TYPE_P (expression));
   30093              : 
   30094   3713129743 :   STRIP_ANY_LOCATION_WRAPPER (expression);
   30095              : 
   30096              :   /* Assume a TU-local entity is not dependent, we'll error later when
   30097              :      instantiating anyway.  */
   30098   3713129743 :   if (TREE_CODE (expression) == TU_LOCAL_ENTITY)
   30099              :     return false;
   30100              : 
   30101              :   /* An unresolved name is always dependent.  */
   30102              :   if (identifier_p (expression)
   30103              :       || TREE_CODE (expression) == USING_DECL)
   30104              :     return true;
   30105              : 
   30106              :   /* A lambda-expression in template context is dependent.  dependent_type_p is
   30107              :      true for a lambda in the scope of a class or function template, but that
   30108              :      doesn't cover all template contexts, like a default template argument.  */
   30109              :   if (TREE_CODE (expression) == LAMBDA_EXPR)
   30110              :     return true;
   30111              : 
   30112              :   /* A fold expression is type-dependent. */
   30113              :   if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
   30114              :       || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
   30115              :       || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
   30116              :       || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
   30117              :     return true;
   30118              : 
   30119              :   /* Some expression forms are never type-dependent.  */
   30120              :   if (TREE_CODE (expression) == SIZEOF_EXPR
   30121              :       || TREE_CODE (expression) == ALIGNOF_EXPR
   30122              :       || TREE_CODE (expression) == AT_ENCODE_EXPR
   30123              :       || TREE_CODE (expression) == NOEXCEPT_EXPR
   30124              :       || TREE_CODE (expression) == TRAIT_EXPR
   30125              :       || TREE_CODE (expression) == TYPEID_EXPR
   30126              :       || TREE_CODE (expression) == DELETE_EXPR
   30127              :       || TREE_CODE (expression) == VEC_DELETE_EXPR
   30128              :       || TREE_CODE (expression) == THROW_EXPR
   30129              :       || TREE_CODE (expression) == REQUIRES_EXPR
   30130              :       || REFLECT_EXPR_P (expression))
   30131              :     return false;
   30132              : 
   30133              :   /* The types of these expressions depends only on the type to which
   30134              :      the cast occurs.  */
   30135              :   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
   30136              :       || TREE_CODE (expression) == STATIC_CAST_EXPR
   30137              :       || TREE_CODE (expression) == CONST_CAST_EXPR
   30138              :       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
   30139              :       || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
   30140              :       || TREE_CODE (expression) == CAST_EXPR)
   30141     65735836 :     return dependent_type_p (TREE_TYPE (expression));
   30142              : 
   30143              :   /* The types of these expressions depends only on the type created
   30144              :      by the expression.  */
   30145              :   if (TREE_CODE (expression) == NEW_EXPR
   30146              :       || TREE_CODE (expression) == VEC_NEW_EXPR)
   30147              :     {
   30148              :       /* For NEW_EXPR tree nodes created inside a template, either
   30149              :          the object type itself or a TREE_LIST may appear as the
   30150              :          operand 1.  */
   30151       745423 :       tree type = TREE_OPERAND (expression, 1);
   30152       745423 :       if (TREE_CODE (type) == TREE_LIST)
   30153              :         /* This is an array type.  We need to check array dimensions
   30154              :            as well.  */
   30155            0 :         return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
   30156            0 :                || value_dependent_expression_p
   30157            0 :                     (TREE_OPERAND (TREE_VALUE (type), 1));
   30158              :       /* Array type whose dimension has to be deduced.  */
   30159       745423 :       else if (TREE_CODE (type) == ARRAY_TYPE
   30160       745423 :                && TREE_OPERAND (expression, 2) == NULL_TREE)
   30161              :         return true;
   30162              :       else
   30163       745411 :         return dependent_type_p (type);
   30164              :     }
   30165              : 
   30166              :   if (TREE_CODE (expression) == SCOPE_REF)
   30167              :     {
   30168    106367086 :       tree scope = TREE_OPERAND (expression, 0);
   30169    106367086 :       tree name = TREE_OPERAND (expression, 1);
   30170              : 
   30171              :       /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
   30172              :          contains an identifier associated by name lookup with one or more
   30173              :          declarations declared with a dependent type, or...a
   30174              :          nested-name-specifier or qualified-id that names a member of an
   30175              :          unknown specialization.  */
   30176    106367086 :       return (type_dependent_expression_p (name)
   30177    106367086 :               || dependent_scope_p (scope));
   30178              :     }
   30179              : 
   30180              :   if (TREE_CODE (expression) == TEMPLATE_DECL
   30181    298656979 :       && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
   30182    298656976 :     return uses_outer_template_parms (expression);
   30183              : 
   30184   3101325364 :   if (TREE_CODE (expression) == STMT_EXPR)
   30185          191 :     expression = stmt_expr_value_expr (expression);
   30186              : 
   30187   3101325364 :   if (BRACE_ENCLOSED_INITIALIZER_P (expression))
   30188              :     {
   30189     12694545 :       for (auto &elt : CONSTRUCTOR_ELTS (expression))
   30190      1917830 :         if (type_dependent_expression_p (elt.value))
   30191              :           return true;
   30192              :       return false;
   30193              :     }
   30194              : 
   30195              :   /* A static data member of the current instantiation with incomplete
   30196              :      array type is type-dependent, as the definition and specializations
   30197              :      can have different bounds.  */
   30198   3092569872 :   if (VAR_P (expression)
   30199    424989821 :       && DECL_CLASS_SCOPE_P (expression)
   30200     51548092 :       && dependent_type_p (DECL_CONTEXT (expression))
   30201   3114773672 :       && VAR_HAD_UNKNOWN_BOUND (expression))
   30202              :     return true;
   30203              : 
   30204              :   /* An array of unknown bound depending on a variadic parameter, eg:
   30205              : 
   30206              :      template<typename... Args>
   30207              :        void foo (Args... args)
   30208              :        {
   30209              :          int arr[] = { args... };
   30210              :        }
   30211              : 
   30212              :      template<int... vals>
   30213              :        void bar ()
   30214              :        {
   30215              :          int arr[] = { vals... };
   30216              :        }
   30217              : 
   30218              :      If the array has no length and has an initializer, it must be that
   30219              :      we couldn't determine its length in cp_complete_array_type because
   30220              :      it is dependent.  */
   30221    424935507 :   if (((VAR_P (expression) && DECL_INITIAL (expression))
   30222   2711022342 :        || COMPOUND_LITERAL_P (expression))
   30223    384000777 :       && TREE_TYPE (expression) != NULL_TREE
   30224    384000777 :       && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
   30225   3093363295 :       && !TYPE_DOMAIN (TREE_TYPE (expression)))
   30226              :    return true;
   30227              : 
   30228              :   /* Pull a FUNCTION_DECL out of a BASELINK if we can.  */
   30229   3092319648 :   if (BASELINK_P (expression))
   30230              :     {
   30231     14327384 :       if (BASELINK_OPTYPE (expression)
   30232     14327384 :           && dependent_type_p (BASELINK_OPTYPE (expression)))
   30233              :         return true;
   30234     14327327 :       expression = BASELINK_FUNCTIONS (expression);
   30235              :     }
   30236              : 
   30237              :   /* A function or variable template-id is type-dependent if it has any
   30238              :      dependent template arguments.  */
   30239   2667579988 :   if (VAR_OR_FUNCTION_DECL_P (expression)
   30240    674133293 :       && DECL_LANG_SPECIFIC (expression)
   30241   3450713802 :       && DECL_TEMPLATE_INFO (expression))
   30242              :     {
   30243              :       /* Consider the innermost template arguments, since those are the ones
   30244              :          that come from the template-id; the template arguments for the
   30245              :          enclosing class do not make it type-dependent unless they are used in
   30246              :          the type of the decl.  */
   30247    153558242 :       if (instantiates_primary_template_p (expression)
   30248    179191630 :           && (any_dependent_template_arguments_p
   30249     25633388 :               (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
   30250              :         return true;
   30251              :     }
   30252              : 
   30253              :   /* Otherwise, if the function decl isn't from a dependent scope, it can't be
   30254              :      type-dependent.  Checking this is important for functions with auto return
   30255              :      type, which looks like a dependent type.  */
   30256   3088675805 :   if (TREE_CODE (expression) == FUNCTION_DECL
   30257    364416635 :       && !(DECL_CLASS_SCOPE_P (expression)
   30258    118666725 :            && dependent_type_p (DECL_CONTEXT (expression)))
   30259    179338636 :       && !(DECL_LANG_SPECIFIC (expression)
   30260    179338636 :            && DECL_UNIQUE_FRIEND_P (expression)
   30261      4065504 :            && (!DECL_FRIEND_CONTEXT (expression)
   30262      3984718 :                || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
   30263   3267755146 :       && !DECL_LOCAL_DECL_P (expression))
   30264              :     {
   30265    178971442 :       gcc_assert (!dependent_type_p (TREE_TYPE (expression))
   30266              :                   || undeduced_auto_decl (expression));
   30267    178971442 :       return false;
   30268              :     }
   30269              : 
   30270              :   /* Otherwise, its constraints could still depend on outer template parameters
   30271              :      from its (dependent) scope.  */
   30272   2909704363 :   if (TREE_CODE (expression) == FUNCTION_DECL
   30273              :       /* As an optimization, check this cheaper sufficient condition first.
   30274              :          (At this point we've established that we're looking at a member of
   30275              :          a dependent class, so it makes sense to start treating say undeduced
   30276              :          auto as dependent.)  */
   30277     66778468 :       && !dependent_type_p (TREE_TYPE (expression))
   30278   2912780801 :       && uses_outer_template_parms_in_constraints (expression))
   30279              :     return true;
   30280              : 
   30281              :   /* Always dependent, on the number of arguments if nothing else.  */
   30282   2909704357 :   if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
   30283              :     return true;
   30284              : 
   30285              :   /* [temp.dep.expr]: "A pack-index-expression is type-dependent if its
   30286              :      id-expression is type-dependent."  */
   30287   2905346616 :   if (TREE_CODE (expression) == PACK_INDEX_EXPR)
   30288         3261 :     return type_dependent_expression_p (PACK_INDEX_PACK (expression));
   30289              : 
   30290   2905343355 :   if (TREE_TYPE (expression) == unknown_type_node)
   30291              :     {
   30292    211007877 :       if (TREE_CODE (expression) == ADDR_EXPR)
   30293          153 :         return type_dependent_expression_p (TREE_OPERAND (expression, 0));
   30294    211007724 :       if (TREE_CODE (expression) == COMPONENT_REF
   30295    166819526 :           || TREE_CODE (expression) == OFFSET_REF)
   30296              :         {
   30297     44188369 :           if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0)))
   30298              :             return true;
   30299     43936138 :           expression = TREE_OPERAND (expression, 1);
   30300   1115127273 :           if (identifier_p (expression))
   30301              :             return false;
   30302              :         }
   30303              :       /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
   30304    210755493 :       if (TREE_CODE (expression) == SCOPE_REF)
   30305              :         return false;
   30306              : 
   30307    210755493 :       if (BASELINK_P (expression))
   30308              :         {
   30309     43936138 :           if (BASELINK_OPTYPE (expression)
   30310     43936138 :               && dependent_type_p (BASELINK_OPTYPE (expression)))
   30311              :             return true;
   30312     43936132 :           expression = BASELINK_FUNCTIONS (expression);
   30313              :         }
   30314              : 
   30315    210755487 :       if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
   30316              :         {
   30317     50614039 :           tree args = TREE_OPERAND (expression, 1);
   30318     50614039 :           if (any_dependent_template_arguments_p (args))
   30319              :             return true;
   30320              :           /* Arguments of a function template-id aren't necessarily coerced
   30321              :              yet so we must conservatively assume that the address (and not
   30322              :              just value) of the argument matters as per [temp.dep.temp]/3.  */
   30323      9017076 :           for (tree arg : tree_vec_range (args))
   30324      5125462 :             if (has_value_dependent_address (arg))
   30325        18458 :               return true;
   30326      3891614 :           expression = TREE_OPERAND (expression, 0);
   30327    288430510 :           if (identifier_p (expression))
   30328              :             return true;
   30329              :         }
   30330              : 
   30331    164033059 :       gcc_assert (OVL_P (expression));
   30332              : 
   30333    513427867 :       for (lkp_iterator iter (expression); iter; ++iter)
   30334    401251802 :         if (type_dependent_expression_p (*iter))
   30335     51856994 :           return true;
   30336              : 
   30337    112176065 :       return false;
   30338              :     }
   30339              : 
   30340              :   /* The type of a non-type template parm declared with a placeholder type
   30341              :      depends on the corresponding template argument, even though
   30342              :      placeholders are not normally considered dependent.  */
   30343   2694335478 :   if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
   30344   2694335478 :       && is_auto (TREE_TYPE (expression)))
   30345              :     return true;
   30346              : 
   30347   2694134283 :   gcc_assert (TREE_CODE (expression) != TYPE_DECL);
   30348              : 
   30349              :   /* Dependent type attributes might not have made it from the decl to
   30350              :      the type yet.  */
   30351   2694134283 :   if (DECL_P (expression)
   30352   2694134283 :       && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
   30353              :     return true;
   30354              : 
   30355   2694134238 :   return (dependent_type_p (TREE_TYPE (expression)));
   30356              : }
   30357              : 
   30358              : /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
   30359              :    type-dependent if the expression refers to a member of the current
   30360              :    instantiation and the type of the referenced member is dependent, or the
   30361              :    class member access expression refers to a member of an unknown
   30362              :    specialization.
   30363              : 
   30364              :    This function returns true if the OBJECT in such a class member access
   30365              :    expression is of an unknown specialization.  */
   30366              : 
   30367              : bool
   30368    339672380 : type_dependent_object_expression_p (tree object)
   30369              : {
   30370              :   /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
   30371              :      dependent.  */
   30372    339672380 :   if (TREE_CODE (object) == IDENTIFIER_NODE)
   30373              :     return true;
   30374    339672380 :   tree scope = TREE_TYPE (object);
   30375    339672380 :   return (!scope || dependent_scope_p (scope));
   30376              : }
   30377              : 
   30378              : /* walk_tree callback function for instantiation_dependent_expression_p,
   30379              :    below.  Returns non-zero if a dependent subexpression is found.  */
   30380              : 
   30381              : static tree
   30382    442842713 : instantiation_dependent_r (tree *tp, int *walk_subtrees,
   30383              :                            void * /*data*/)
   30384              : {
   30385    442842713 :   if (TYPE_P (*tp))
   30386              :     {
   30387              :       /* We don't have to worry about decltype currently because decltype
   30388              :          of an instantiation-dependent expr is a dependent type.  This
   30389              :          might change depending on the resolution of DR 1172.  */
   30390     21903566 :       *walk_subtrees = false;
   30391     21903566 :       return NULL_TREE;
   30392              :     }
   30393    420939147 :   enum tree_code code = TREE_CODE (*tp);
   30394    420939147 :   switch (code)
   30395              :     {
   30396              :       /* Don't treat an argument list as dependent just because it has no
   30397              :          TREE_TYPE.  */
   30398              :     case TREE_LIST:
   30399              :     case TREE_VEC:
   30400              :     case NONTYPE_ARGUMENT_PACK:
   30401              :       return NULL_TREE;
   30402              : 
   30403     31514133 :     case TEMPLATE_PARM_INDEX:
   30404     31514133 :       if (dependent_type_p (TREE_TYPE (*tp)))
   30405         5050 :         return *tp;
   30406     31509083 :       if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
   30407              :         return *tp;
   30408              :       /* We'll check value-dependence separately.  */
   30409              :       return NULL_TREE;
   30410              : 
   30411              :       /* Handle expressions with type operands.  */
   30412      4167302 :     case SIZEOF_EXPR:
   30413      4167302 :     case ALIGNOF_EXPR:
   30414      4167302 :     case TYPEID_EXPR:
   30415      4167302 :     case AT_ENCODE_EXPR:
   30416      4167302 :       {
   30417      4167302 :         tree op = TREE_OPERAND (*tp, 0);
   30418      4167302 :         if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
   30419            0 :           op = TREE_TYPE (op);
   30420      4167302 :         else if (code == SIZEOF_EXPR
   30421      4063799 :                  && PACK_EXPANSION_P (op)
   30422      6569734 :                  && DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (op)))
   30423              :           {
   30424          538 :             tree d = PACK_EXPANSION_PATTERN (op);
   30425          538 :             if (DECL_HAS_VALUE_EXPR_P (d))
   30426              :               {
   30427          538 :                 d = DECL_VALUE_EXPR (d);
   30428          538 :                 if (TREE_CODE (d) == TREE_VEC
   30429          538 :                     && !type_dependent_expression_p (TREE_VEC_ELT (d, 0)))
   30430              :                   {
   30431          382 :                     *walk_subtrees = 0;
   30432          382 :                     return NULL_TREE;
   30433              :                   }
   30434              :               }
   30435              :           }
   30436      4166920 :         if (TYPE_P (op))
   30437              :           {
   30438      4103296 :             if (dependent_type_p (op))
   30439      3790213 :               return *tp;
   30440              :             else
   30441              :               {
   30442       313083 :                 *walk_subtrees = false;
   30443       313083 :                 return NULL_TREE;
   30444              :               }
   30445              :           }
   30446              :         break;
   30447              :       }
   30448              : 
   30449       535339 :     case COMPONENT_REF:
   30450       535339 :       if (identifier_p (TREE_OPERAND (*tp, 1)))
   30451              :         /* In a template, finish_class_member_access_expr creates a
   30452              :            COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
   30453              :            type-dependent, so that we can check access control at
   30454              :            instantiation time (PR 42277).  See also Core issue 1273.  */
   30455              :         return *tp;
   30456              :       break;
   30457              : 
   30458      6157174 :     case SCOPE_REF:
   30459      6157174 :       if (instantiation_dependent_scope_ref_p (*tp))
   30460      4860152 :         return *tp;
   30461              :       else
   30462              :         break;
   30463              : 
   30464              :       /* Treat statement-expressions as dependent.  */
   30465              :     case BIND_EXPR:
   30466              :       return *tp;
   30467              : 
   30468              :       /* Treat requires-expressions as dependent. */
   30469              :     case REQUIRES_EXPR:
   30470              :       return *tp;
   30471              : 
   30472      5111109 :     case CONSTRUCTOR:
   30473      5111109 :       if (CONSTRUCTOR_IS_DEPENDENT (*tp))
   30474              :         return *tp;
   30475              :       break;
   30476              : 
   30477     31693435 :     case TEMPLATE_DECL:
   30478     31693435 :     case FUNCTION_DECL:
   30479              :       /* Before C++17, a noexcept-specifier isn't part of the function type
   30480              :          so it doesn't affect type dependence, but we still want to consider it
   30481              :          for instantiation dependence.  */
   30482     31693435 :       if (cxx_dialect < cxx17
   30483        11148 :           && DECL_DECLARES_FUNCTION_P (*tp)
   30484     31704582 :           && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
   30485          463 :         return *tp;
   30486              :       break;
   30487              : 
   30488              :     default:
   30489              :       break;
   30490              :     }
   30491              : 
   30492    373524913 :   if (type_dependent_expression_p (*tp))
   30493     28686207 :     return *tp;
   30494              :   else
   30495              :     return NULL_TREE;
   30496              : }
   30497              : 
   30498              : /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
   30499              :    sense defined by the ABI:
   30500              : 
   30501              :    "An expression is instantiation-dependent if it is type-dependent
   30502              :    or value-dependent, or it has a subexpression that is type-dependent
   30503              :    or value-dependent."
   30504              : 
   30505              :    Except don't actually check value-dependence for unevaluated expressions,
   30506              :    because in sizeof(i) we don't care about the value of i.  Checking
   30507              :    type-dependence will in turn check value-dependence of array bounds/template
   30508              :    arguments as needed.  */
   30509              : 
   30510              : bool
   30511   1401268902 : instantiation_dependent_uneval_expression_p (tree expression)
   30512              : {
   30513   1401268902 :   tree result;
   30514              : 
   30515   1401268902 :   if (!processing_template_decl)
   30516              :     return false;
   30517              : 
   30518    367888512 :   if (expression == error_mark_node)
   30519              :     return false;
   30520              : 
   30521    367888498 :   result = cp_walk_tree_without_duplicates (&expression,
   30522              :                                             instantiation_dependent_r, NULL);
   30523    367888498 :   return result != NULL_TREE;
   30524              : }
   30525              : 
   30526              : /* As above, but also check value-dependence of the expression as a whole.  */
   30527              : 
   30528              : bool
   30529   1326093240 : instantiation_dependent_expression_p (tree expression)
   30530              : {
   30531   1326093240 :   return (instantiation_dependent_uneval_expression_p (expression)
   30532   1326093240 :           || (processing_template_decl
   30533    293987969 :               && potential_constant_expression (expression)
   30534    293987860 :               && value_dependent_expression_p (expression)));
   30535              : }
   30536              : 
   30537              : /* Like type_dependent_expression_p, but it also works while not processing
   30538              :    a template definition, i.e. during substitution or mangling.  */
   30539              : 
   30540              : bool
   30541      8077041 : type_dependent_expression_p_push (tree expr)
   30542              : {
   30543      8077041 :   bool b;
   30544      8077041 :   ++processing_template_decl;
   30545      8077041 :   b = type_dependent_expression_p (expr);
   30546      8077041 :   --processing_template_decl;
   30547      8077041 :   return b;
   30548              : }
   30549              : 
   30550              : /* Returns TRUE if ARGS contains a type-dependent expression.  */
   30551              : 
   30552              : bool
   30553    130948362 : any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
   30554              : {
   30555    130948362 :   if (!processing_template_decl || !args)
   30556              :     return false;
   30557              : 
   30558    127624509 :   for (tree arg : *args)
   30559     98425233 :     if (type_dependent_expression_p (arg))
   30560              :       return true;
   30561              : 
   30562              :   return false;
   30563              : }
   30564              : 
   30565              : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   30566              :    expressions) contains any type-dependent expressions.  */
   30567              : 
   30568              : bool
   30569            0 : any_type_dependent_elements_p (const_tree list)
   30570              : {
   30571            0 :   for (; list; list = TREE_CHAIN (list))
   30572            0 :     if (type_dependent_expression_p (TREE_VALUE (list)))
   30573              :       return true;
   30574              : 
   30575              :   return false;
   30576              : }
   30577              : 
   30578              : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   30579              :    expressions) contains any value-dependent expressions.  */
   30580              : 
   30581              : bool
   30582      3639231 : any_value_dependent_elements_p (const_tree list)
   30583              : {
   30584      5637867 :   for (; list; list = TREE_CHAIN (list))
   30585      3639251 :     if (value_dependent_expression_p (TREE_VALUE (list)))
   30586              :       return true;
   30587              : 
   30588              :   return false;
   30589              : }
   30590              : 
   30591              : /* Returns TRUE if the ARG (a template argument) is dependent.  */
   30592              : 
   30593              : bool
   30594   4984503575 : dependent_template_arg_p (tree arg)
   30595              : {
   30596   4984503575 :   if (!processing_template_decl)
   30597              :     return false;
   30598              : 
   30599              :   /* Assume a template argument that was wrongly written by the user
   30600              :      is dependent. This is consistent with what
   30601              :      any_dependent_template_arguments_p [that calls this function]
   30602              :      does.  */
   30603   4592219543 :   if (!arg || arg == error_mark_node)
   30604              :     return true;
   30605              : 
   30606   4592218970 :   if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   30607            0 :     arg = argument_pack_select_arg (arg);
   30608              : 
   30609   4592218970 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   30610              :     return true;
   30611   4591901440 :   if (TREE_CODE (arg) == TEMPLATE_DECL)
   30612              :     {
   30613     14593674 :       if (DECL_TEMPLATE_PARM_P (arg))
   30614              :         return true;
   30615              :       /* A member template of a dependent class is not necessarily
   30616              :          type-dependent, but it is a dependent template argument because it
   30617              :          will be a member of an unknown specialization to that template.  */
   30618     14590347 :       tree scope = CP_DECL_CONTEXT (arg);
   30619     14590347 :       return TYPE_P (scope) && dependent_type_p (scope);
   30620              :     }
   30621   4577307766 :   else if (ARGUMENT_PACK_P (arg))
   30622              :     {
   30623    194285436 :       tree args = ARGUMENT_PACK_ARGS (arg);
   30624    412733927 :       for (tree arg : tree_vec_range (args))
   30625    281005123 :         if (dependent_template_arg_p (arg))
   30626     62556632 :           return true;
   30627    131728804 :       return false;
   30628              :     }
   30629   4383022330 :   else if (TYPE_P (arg))
   30630   3868678022 :     return dependent_type_p (arg);
   30631              :   else
   30632    514344308 :     return value_dependent_expression_p (arg);
   30633              : }
   30634              : 
   30635              : /* Identify any expressions that use function parms.  */
   30636              : 
   30637              : static tree
   30638    492060338 : find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
   30639              : {
   30640    492060338 :   tree t = *tp;
   30641    492060338 :   if (TREE_CODE (t) == PARM_DECL)
   30642              :     {
   30643       344993 :       *walk_subtrees = 0;
   30644       344993 :       return t;
   30645              :     }
   30646              :   return NULL_TREE;
   30647              : }
   30648              : 
   30649              : /* Returns true if a type specialization formed using the template
   30650              :    arguments ARGS needs to use structural equality.  */
   30651              : 
   30652              : bool
   30653    102290601 : any_template_arguments_need_structural_equality_p (tree args)
   30654              : {
   30655    102290601 :   int i;
   30656    102290601 :   int j;
   30657              : 
   30658    102290601 :   if (!args)
   30659              :     return false;
   30660    102290601 :   if (args == error_mark_node)
   30661              :     return true;
   30662              : 
   30663    403508954 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   30664              :     {
   30665    104750560 :       tree level = TMPL_ARGS_LEVEL (args, i + 1);
   30666    287072281 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   30667              :         {
   30668    183989026 :           tree arg = TREE_VEC_ELT (level, j);
   30669    183989026 :           tree packed_args = NULL_TREE;
   30670    183989026 :           int k, len = 1;
   30671              : 
   30672    183989026 :           if (ARGUMENT_PACK_P (arg))
   30673              :             {
   30674              :               /* Look inside the argument pack.  */
   30675     14911090 :               packed_args = ARGUMENT_PACK_ARGS (arg);
   30676     14911090 :               len = TREE_VEC_LENGTH (packed_args);
   30677              :             }
   30678              : 
   30679    377917458 :           for (k = 0; k < len; ++k)
   30680              :             {
   30681    195595737 :               if (packed_args)
   30682     26517801 :                 arg = TREE_VEC_ELT (packed_args, k);
   30683              : 
   30684    195595737 :               if (error_operand_p (arg))
   30685      1667305 :                 return true;
   30686    195595737 :               else if (TREE_CODE (arg) == TEMPLATE_DECL)
   30687      3969869 :                 continue;
   30688    191625868 :               else if (arg == any_targ_node)
   30689              :                 /* An any_targ_node argument (added by add_defaults_to_ttp)
   30690              :                    makes the corresponding specialization not canonicalizable,
   30691              :                    since template_args_equal always return true for it.  We
   30692              :                    may see this when called from bind_template_template_parm.  */
   30693              :                 return true;
   30694              :               /* Checking current_function_decl because this structural
   30695              :                  comparison is only necessary for redeclaration.  */
   30696    191625864 :               else if (!current_function_decl
   30697    170267117 :                        && dependent_template_arg_p (arg)
   30698    268817419 :                        && (cp_walk_tree_without_duplicates
   30699              :                            (&arg, find_parm_usage_r, NULL)))
   30700              :                 /* The identity of a class template specialization that uses
   30701              :                    a function parameter depends on the identity of the function.
   30702              :                    And if this specialization appeared in the trailing return
   30703              :                    type thereof, we don't know the identity of the function
   30704              :                    (e.g. if it's a redeclaration or a new function) until we
   30705              :                    form its signature and go through duplicate_decls.  Thus
   30706              :                    it's unsafe to decide on a canonical type now (which depends
   30707              :                    on the DECL_CONTEXT of the function parameter, which can get
   30708              :                    mutated after the fact by duplicate_decls), so just require
   30709              :                    structural equality in this case (PR52830).  */
   30710              :                 return true;
   30711    191280871 :               else if (TYPE_P (arg)
   30712    157130642 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg)
   30713    211669753 :                        && (dependent_alias_template_spec_p (arg, nt_opaque)
   30714     19068839 :                            || dependent_opaque_alias_p (arg)))
   30715              :                 /* Require structural equality for specializations written
   30716              :                    in terms of a dependent alias template specialization.  */
   30717      1320079 :                 return true;
   30718     52432753 :               else if (CLASS_TYPE_P (arg)
   30719     51748098 :                        && TYPE_TEMPLATE_INFO (arg)
   30720    230246866 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg))
   30721              :                 /* Require structural equality for specializations written
   30722              :                    in terms of a class template specialization that itself
   30723              :                    needs structural equality.  */
   30724              :                 return true;
   30725              :             }
   30726              :         }
   30727              :     }
   30728              : 
   30729              :   return false;
   30730              : }
   30731              : 
   30732              : /* Returns true if ARGS (a collection of template arguments) contains
   30733              :    any dependent arguments.  */
   30734              : 
   30735              : bool
   30736   2917518553 : any_dependent_template_arguments_p (const_tree args)
   30737              : {
   30738   2917518553 :   if (args == error_mark_node)
   30739              :     return true;
   30740   2917518553 :   if (!processing_template_decl || !args)
   30741              :     return false;
   30742              : 
   30743   6834844456 :   for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
   30744              :     {
   30745   2736856233 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   30746   5861374908 :       for (tree arg : tree_vec_range (const_cast<tree> (level)))
   30747   4156566103 :         if (dependent_template_arg_p (arg))
   30748   1032047428 :           return true;
   30749              :     }
   30750              : 
   30751              :   return false;
   30752              : }
   30753              : 
   30754              : /* Returns true if ARGS contains any errors.  */
   30755              : 
   30756              : bool
   30757     25585678 : any_erroneous_template_args_p (const_tree args)
   30758              : {
   30759     25585678 :   int i;
   30760     25585678 :   int j;
   30761              : 
   30762     25585678 :   if (args == error_mark_node)
   30763              :     return true;
   30764              : 
   30765     25585651 :   if (args && TREE_CODE (args) != TREE_VEC)
   30766              :     {
   30767     25585651 :       if (tree ti = get_template_info (args))
   30768     19888903 :         args = TI_ARGS (ti);
   30769              :       else
   30770              :         args = NULL_TREE;
   30771              :     }
   30772              : 
   30773     19888903 :   if (!args)
   30774      5696748 :     return false;
   30775              : 
   30776     79442935 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   30777              :     {
   30778     20001296 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   30779     54089038 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   30780     34087890 :         if (error_operand_p (TREE_VEC_ELT (level, j)))
   30781              :           return true;
   30782              :     }
   30783              : 
   30784              :   return false;
   30785              : }
   30786              : 
   30787              : /* Returns TRUE if the template TMPL is type-dependent.  */
   30788              : 
   30789              : bool
   30790      1771084 : dependent_template_p (tree tmpl)
   30791              : {
   30792      1771084 :   if (TREE_CODE (tmpl) == OVERLOAD)
   30793              :     {
   30794      1761779 :       for (lkp_iterator iter (tmpl); iter; ++iter)
   30795      1194282 :         if (dependent_template_p (*iter))
   30796            0 :           return true;
   30797       567497 :       return false;
   30798              :     }
   30799              : 
   30800              :   /* Template template parameters are dependent.  */
   30801       972838 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
   30802      2176425 :       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   30803              :     return true;
   30804              :   /* So are names that have not been looked up.  */
   30805      1212796 :   if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
   30806              :     return true;
   30807              :   return false;
   30808              : }
   30809              : 
   30810              : /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
   30811              : 
   30812              : bool
   30813       576802 : dependent_template_id_p (tree tmpl, tree args)
   30814              : {
   30815       576802 :   return (dependent_template_p (tmpl)
   30816       576802 :           || any_dependent_template_arguments_p (args));
   30817              : }
   30818              : 
   30819              : /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
   30820              :    are dependent.  BODY is the body to use for loop transforming
   30821              :    constructs.  */
   30822              : 
   30823              : bool
   30824        21035 : dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv, tree body)
   30825              : {
   30826        21035 :   int i, k;
   30827              : 
   30828        21035 :   if (!processing_template_decl)
   30829              :     return false;
   30830              : 
   30831         1821 :   for (i = 0, k = 0; i < TREE_VEC_LENGTH (declv); i++)
   30832              :     {
   30833         1209 :       tree decl = TREE_VEC_ELT (declv, i);
   30834         1209 :       tree init = TREE_VEC_ELT (initv, i);
   30835         1209 :       tree cond = TREE_VEC_ELT (condv, i);
   30836         1209 :       tree incr = TREE_VEC_ELT (incrv, i);
   30837              : 
   30838         1209 :       if (decl == NULL_TREE)
   30839              :         {
   30840           10 :           tree stmt = body;
   30841           10 :           int j = c_omp_find_generated_loop (stmt, k++, cp_walk_subtrees);
   30842           10 :           init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   30843           10 :           decl = TREE_OPERAND (init, 0);
   30844           10 :           cond = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   30845           10 :           incr = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   30846              :         }
   30847              : 
   30848         1209 :       if (type_dependent_expression_p (decl)
   30849         1209 :           || TREE_CODE (decl) == SCOPE_REF)
   30850              :         return true;
   30851              : 
   30852          875 :       if (init && type_dependent_expression_p (init))
   30853              :         return true;
   30854              : 
   30855          875 :       if (cond == global_namespace)
   30856              :         return true;
   30857              : 
   30858          802 :       if (type_dependent_expression_p (cond))
   30859              :         return true;
   30860              : 
   30861          778 :       if (COMPARISON_CLASS_P (cond)
   30862          778 :           && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
   30863          774 :               || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
   30864           11 :         return true;
   30865              : 
   30866          767 :       if (TREE_CODE (incr) == MODOP_EXPR)
   30867              :         {
   30868            1 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
   30869            1 :               || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
   30870            0 :             return true;
   30871              :         }
   30872          766 :       else if (type_dependent_expression_p (incr))
   30873              :         return true;
   30874          766 :       else if (TREE_CODE (incr) == MODIFY_EXPR)
   30875              :         {
   30876          233 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
   30877              :             return true;
   30878          233 :           else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
   30879              :             {
   30880          232 :               tree t = TREE_OPERAND (incr, 1);
   30881          232 :               if (type_dependent_expression_p (TREE_OPERAND (t, 0))
   30882          232 :                   || type_dependent_expression_p (TREE_OPERAND (t, 1)))
   30883            5 :                 return true;
   30884              : 
   30885              :               /* If this loop has a class iterator with != comparison
   30886              :                  with increment other than i++/++i/i--/--i, make sure the
   30887              :                  increment is constant.  */
   30888          454 :               if (CLASS_TYPE_P (TREE_TYPE (decl))
   30889          288 :                   && TREE_CODE (cond) == NE_EXPR)
   30890              :                 {
   30891            8 :                   if (TREE_OPERAND (t, 0) == decl)
   30892            8 :                     t = TREE_OPERAND (t, 1);
   30893              :                   else
   30894            0 :                     t = TREE_OPERAND (t, 0);
   30895            8 :                   if (TREE_CODE (t) != INTEGER_CST)
   30896              :                     return true;
   30897              :                 }
   30898              :             }
   30899              :         }
   30900              :     }
   30901              : 
   30902              :   return false;
   30903              : }
   30904              : 
   30905              : /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
   30906              :    TYPENAME_TYPE corresponds.  Returns the original TYPENAME_TYPE if
   30907              :    no such TYPE can be found.  Note that this function peers inside
   30908              :    uninstantiated templates and therefore should be used only in
   30909              :    extremely limited situations.  ONLY_CURRENT_P restricts this
   30910              :    peering to the currently open classes hierarchy (which is required
   30911              :    when comparing types).  */
   30912              : 
   30913              : tree
   30914    237149777 : resolve_typename_type (tree type, bool only_current_p)
   30915              : {
   30916    237149777 :   tree scope;
   30917    237149777 :   tree name;
   30918    237149777 :   tree decl;
   30919    237149777 :   int quals;
   30920    237149777 :   tree pushed_scope;
   30921    237149777 :   tree result;
   30922              : 
   30923    237149777 :   gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
   30924              : 
   30925    237149777 :   scope = TYPE_CONTEXT (type);
   30926              :   /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope.  */
   30927    237149777 :   gcc_checking_assert (uses_template_parms (scope));
   30928              : 
   30929              :   /* Usually the non-qualified identifier of a TYPENAME_TYPE is
   30930              :      TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
   30931              :      TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
   30932              :      representing the typedef. In that case TYPE_IDENTIFIER (type) is
   30933              :      not the non-qualified identifier of the TYPENAME_TYPE anymore.
   30934              :      So by getting the TYPE_IDENTIFIER of the _main declaration_ of
   30935              :      the TYPENAME_TYPE instead, we avoid messing up with a possible
   30936              :      typedef variant case.  */
   30937    237149777 :   name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
   30938              : 
   30939              :   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
   30940              :      it first before we can figure out what NAME refers to.  */
   30941    237149777 :   if (TREE_CODE (scope) == TYPENAME_TYPE)
   30942              :     {
   30943      5792769 :       if (TYPENAME_IS_RESOLVING_P (scope))
   30944              :         /* Given a class template A with a dependent base with nested type C,
   30945              :            typedef typename A::C::C C will land us here, as trying to resolve
   30946              :            the initial A::C leads to the local C typedef, which leads back to
   30947              :            A::C::C.  So we break the recursion now.  */
   30948              :         return type;
   30949              :       else
   30950      5792769 :         scope = resolve_typename_type (scope, only_current_p);
   30951              :     }
   30952              :   /* If we don't know what SCOPE refers to, then we cannot resolve the
   30953              :      TYPENAME_TYPE.  */
   30954    237149777 :   if (!CLASS_TYPE_P (scope))
   30955              :     return type;
   30956              :   /* If this is a typedef, we don't want to look inside (c++/11987).  */
   30957    159649510 :   if (typedef_variant_p (type))
   30958              :     return type;
   30959              :   /* If SCOPE isn't the template itself, it will not have a valid
   30960              :      TYPE_FIELDS list.  */
   30961    111963056 :   if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
   30962              :     /* scope is either the template itself or a compatible instantiation
   30963              :        like X<T>, so look up the name in the original template.  */
   30964     77757626 :     scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
   30965              :   /* If scope has no fields, it can't be a current instantiation.  Check this
   30966              :      before currently_open_class to avoid infinite recursion (71515).  */
   30967    111963056 :   if (!TYPE_FIELDS (scope))
   30968              :     return type;
   30969              :   /* If the SCOPE is not the current instantiation, there's no reason
   30970              :      to look inside it.  */
   30971     77647610 :   if (only_current_p && !currently_open_class (scope))
   30972              :     return type;
   30973              :   /* Enter the SCOPE so that name lookup will be resolved as if we
   30974              :      were in the class definition.  In particular, SCOPE will no
   30975              :      longer be considered a dependent type.  */
   30976        55746 :   pushed_scope = push_scope (scope);
   30977              :   /* Look up the declaration.  */
   30978        55746 :   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
   30979              :                         tf_warning_or_error);
   30980              : 
   30981        55746 :   result = NULL_TREE;
   30982              : 
   30983              :   /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
   30984              :      find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
   30985        55746 :   tree fullname = TYPENAME_TYPE_FULLNAME (type);
   30986        55746 :   if (!decl)
   30987              :     /*nop*/;
   30988        55720 :   else if (identifier_p (fullname)
   30989        55714 :            && TREE_CODE (decl) == TYPE_DECL)
   30990              :     {
   30991        55714 :       result = TREE_TYPE (decl);
   30992        55714 :       if (result == error_mark_node)
   30993           29 :         result = NULL_TREE;
   30994              :     }
   30995            6 :   else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
   30996            6 :            && DECL_CLASS_TEMPLATE_P (decl))
   30997              :     {
   30998              :       /* Obtain the template and the arguments.  */
   30999            3 :       tree tmpl = TREE_OPERAND (fullname, 0);
   31000            3 :       if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
   31001              :         {
   31002              :           /* We get here with a plain identifier because a previous tentative
   31003              :              parse of the nested-name-specifier as part of a ptr-operator saw
   31004              :              ::template X<A>.  The use of ::template is necessary in a
   31005              :              ptr-operator, but wrong in a declarator-id.
   31006              : 
   31007              :              [temp.names]: In a qualified-id of a declarator-id, the keyword
   31008              :              template shall not appear at the top level.  */
   31009            3 :           pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
   31010              :                    "keyword %<template%> not allowed in declarator-id");
   31011            3 :           tmpl = decl;
   31012              :         }
   31013            3 :       tree args = TREE_OPERAND (fullname, 1);
   31014              :       /* Instantiate the template.  */
   31015            3 :       result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
   31016              :                                       tf_error | tf_user);
   31017            3 :       result = adjust_type_for_entering_scope (result);
   31018            3 :       if (result == error_mark_node)
   31019           29 :         result = NULL_TREE;
   31020              :     }
   31021              : 
   31022              :   /* Leave the SCOPE.  */
   31023        55746 :   if (pushed_scope)
   31024        55728 :     pop_scope (pushed_scope);
   31025              : 
   31026              :   /* If we failed to resolve it, return the original typename.  */
   31027        55746 :   if (!result)
   31028              :     return type;
   31029              : 
   31030              :   /* If lookup found a typename type, resolve that too.  */
   31031        55717 :   if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
   31032              :     {
   31033              :       /* Ill-formed programs can cause infinite recursion here, so we
   31034              :          must catch that.  */
   31035            3 :       TYPENAME_IS_RESOLVING_P (result) = 1;
   31036            3 :       result = resolve_typename_type (result, only_current_p);
   31037            3 :       TYPENAME_IS_RESOLVING_P (result) = 0;
   31038              :     }
   31039              : 
   31040              :   /* Qualify the resulting type.  */
   31041        55717 :   quals = cp_type_quals (type);
   31042        55717 :   if (quals)
   31043            0 :     result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
   31044              : 
   31045              :   return result;
   31046              : }
   31047              : 
   31048              : /* Returns a type which represents 'auto' or 'decltype(auto)'.  We use a
   31049              :    TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
   31050              :    by default.  If set_canonical is true, we set TYPE_CANONICAL on it.  */
   31051              : 
   31052              : static tree
   31053     29974806 : make_auto_1 (tree name, bool set_canonical, int level = -1)
   31054              : {
   31055     29974806 :   if (level == -1)
   31056     20722177 :     level = current_template_depth + 1;
   31057     29974806 :   tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
   31058     29974806 :   TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
   31059     29974806 :   TYPE_STUB_DECL (au) = TYPE_NAME (au);
   31060     29974806 :   TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
   31061     29974806 :     (0, level, level, TYPE_NAME (au), NULL_TREE);
   31062     29974806 :   if (set_canonical)
   31063     19327342 :     TYPE_CANONICAL (au) = canonical_type_parameter (au);
   31064     29974806 :   DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
   31065     29974806 :   SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
   31066     29974806 :   if (name == decltype_auto_identifier)
   31067      2003779 :     AUTO_IS_DECLTYPE (au) = true;
   31068              : 
   31069     29974806 :   return au;
   31070              : }
   31071              : 
   31072              : tree
   31073       594831 : make_decltype_auto (void)
   31074              : {
   31075       594831 :   return make_auto_1 (decltype_auto_identifier, true);
   31076              : }
   31077              : 
   31078              : tree
   31079     18693988 : make_auto (void)
   31080              : {
   31081     18693988 :   return make_auto_1 (auto_identifier, true);
   31082              : }
   31083              : 
   31084              : /* Return a C++17 deduction placeholder for class template TMPL.
   31085              :    There are represented as an 'auto' with the special level 0 and
   31086              :    CLASS_PLACEHOLDER_TEMPLATE set.  */
   31087              : 
   31088              : tree
   31089      9214106 : make_template_placeholder (tree tmpl)
   31090              : {
   31091      9214106 :   tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
   31092      9214106 :   CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
   31093              :   /* Our canonical type depends on the placeholder.  */
   31094      9214106 :   TYPE_CANONICAL (t) = canonical_type_parameter (t);
   31095      9214106 :   return t;
   31096              : }
   31097              : 
   31098              : /* True iff T is a C++17 class template deduction placeholder.  */
   31099              : 
   31100              : bool
   31101   1746292669 : template_placeholder_p (tree t)
   31102              : {
   31103   1789236970 :   return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
   31104              : }
   31105              : 
   31106              : /* Return an auto for an explicit cast expression auto(x).
   31107              :    Like CTAD placeholders, these have level 0 so that they're
   31108              :    not accidentally replaced via tsubst and are always directly
   31109              :    resolved via do_auto_deduction.  */
   31110              : 
   31111              : tree
   31112        38523 : make_cast_auto ()
   31113              : {
   31114        38523 :   return make_auto_1 (auto_identifier, true, /*level=*/0);
   31115              : }
   31116              : 
   31117              : /* Make a "constrained auto" type-specifier. This is an auto or
   31118              :   decltype(auto) type with constraints that must be associated after
   31119              :   deduction.  The constraint is formed from the given concept CON
   31120              :   and its optional sequence of template arguments ARGS.
   31121              : 
   31122              :   TYPE must be the result of make_auto_type or make_decltype_auto_type. */
   31123              : 
   31124              : static tree
   31125      1433076 : make_constrained_placeholder_type (tree type, tree con, tree args)
   31126              : {
   31127              :   /* Build the constraint. */
   31128      1433076 :   tree tmpl = DECL_TI_TEMPLATE (con);
   31129      1433076 :   ++processing_template_decl;
   31130      1433076 :   tree expr = build_concept_check (tmpl, type, args, tf_warning_or_error);
   31131      1433076 :   --processing_template_decl;
   31132              : 
   31133      1433076 :   PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
   31134      1433076 :     = build_tree_list (current_template_parms, expr);
   31135              : 
   31136              :   /* Our canonical type depends on the constraint.  */
   31137      1433076 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   31138              : 
   31139      1433076 :   return type;
   31140              : }
   31141              : 
   31142              : /* Make a "constrained auto" type-specifier.  */
   31143              : 
   31144              : tree
   31145        24137 : make_constrained_auto (tree con, tree args)
   31146              : {
   31147        24137 :   tree type = make_auto_1 (auto_identifier, false);
   31148        24137 :   return make_constrained_placeholder_type (type, con, args);
   31149              : }
   31150              : 
   31151              : /* Make a "constrained decltype(auto)" type-specifier.  */
   31152              : 
   31153              : tree
   31154      1408939 : make_constrained_decltype_auto (tree con, tree args)
   31155              : {
   31156      1408939 :   tree type = make_auto_1 (decltype_auto_identifier, false);
   31157      1408939 :   return make_constrained_placeholder_type (type, con, args);
   31158              : }
   31159              : 
   31160              : /* Create an "auto..." type-specifier.  */
   31161              : 
   31162              : tree
   31163          181 : make_auto_pack ()
   31164              : {
   31165          181 :   tree type = make_auto_1 (auto_identifier, false);
   31166          181 :   TEMPLATE_TYPE_PARAMETER_PACK (type) = true;
   31167              :   /* Our canonical type depends on being a pack.  */
   31168          181 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   31169          181 :   return type;
   31170              : }
   31171              : 
   31172              : /* Returns true if the placeholder type constraint T has any dependent
   31173              :    (explicit) template arguments.  */
   31174              : 
   31175              : static bool
   31176           59 : placeholder_type_constraint_dependent_p (tree t)
   31177              : {
   31178           59 :   gcc_assert (concept_check_p (t));
   31179           59 :   tree args = TREE_OPERAND (t, 1);
   31180           59 :   tree first = TREE_VEC_ELT (args, 0);
   31181           59 :   if (ARGUMENT_PACK_P (first))
   31182              :     {
   31183           18 :       args = expand_template_argument_pack (args);
   31184           18 :       first = TREE_VEC_ELT (args, 0);
   31185              :     }
   31186           59 :   gcc_checking_assert (is_auto (first));
   31187           77 :   for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
   31188           42 :     if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
   31189              :       return true;
   31190              :   return false;
   31191              : }
   31192              : 
   31193              : /* Prepare and return a concept definition.  */
   31194              : 
   31195              : tree
   31196      2607937 : start_concept_definition (cp_expr id)
   31197              : {
   31198      2607937 :   gcc_assert (identifier_p (id));
   31199      2607937 :   gcc_assert (processing_template_decl);
   31200              : 
   31201      2607937 :   location_t loc = id.get_location();
   31202              : 
   31203              :   /* A concept-definition shall not have associated constraints.  */
   31204      2607937 :   if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
   31205              :     {
   31206            6 :       error_at (loc, "a concept cannot be constrained");
   31207            6 :       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
   31208              :     }
   31209              : 
   31210              :   /* A concept-definition shall appear in namespace scope.  Templates
   31211              :      aren't allowed in block scope, so we only need to check for class
   31212              :      scope.  */
   31213      2607937 :   if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
   31214              :     {
   31215            6 :       error_at (loc, "concept %qE not in namespace scope", *id);
   31216            6 :       return error_mark_node;
   31217              :     }
   31218              : 
   31219      2607931 :   if (current_template_depth > 1)
   31220              :     {
   31221            3 :       error_at (loc, "concept %qE has multiple template parameter lists", *id);
   31222            3 :       return error_mark_node;
   31223              :     }
   31224              : 
   31225              :   /* Initially build the concept declaration; its type is bool.  */
   31226      2607928 :   tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
   31227      2607928 :   DECL_CONTEXT (decl) = current_scope ();
   31228      2607928 :   TREE_PUBLIC (decl) = true;
   31229              : 
   31230      2607928 :   return decl;
   31231              : }
   31232              : 
   31233              : /* Finish building a concept definition. Like other templates, the
   31234              :    CONCEPT_DECL node is wrapped by a TEMPLATE_DECL.  This returns the
   31235              :    the TEMPLATE_DECL. */
   31236              : 
   31237              : tree
   31238      2607928 : finish_concept_definition (tree decl, tree init, tree attrs)
   31239              : {
   31240      2607928 :   DECL_INITIAL (decl) = init;
   31241              : 
   31242      2607928 :   if (attrs)
   31243           27 :     cplus_decl_attributes (&decl, attrs, 0);
   31244              : 
   31245      2607928 :   set_originating_module (decl, false);
   31246      2607928 :   check_module_decl_linkage (decl);
   31247              : 
   31248              :   /* Push the enclosing template.  */
   31249      2607928 :   return push_template_decl (decl);
   31250              : }
   31251              : 
   31252              : /* Given type ARG, return std::initializer_list<ARG>.  */
   31253              : 
   31254              : static tree
   31255          396 : listify (tree arg)
   31256              : {
   31257          396 :   tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
   31258              : 
   31259          396 :   if (std_init_list == error_mark_node
   31260          396 :       || !DECL_CLASS_TEMPLATE_P (std_init_list))
   31261              :     {
   31262           21 :       gcc_rich_location richloc (input_location);
   31263           21 :       maybe_add_include_fixit (&richloc, "<initializer_list>", false);
   31264           21 :       error_at (&richloc,
   31265              :                 "deducing from brace-enclosed initializer list"
   31266              :                 " requires %<#include <initializer_list>%>");
   31267              : 
   31268           21 :       return error_mark_node;
   31269           21 :     }
   31270          375 :   tree argvec = make_tree_vec (1);
   31271          375 :   TREE_VEC_ELT (argvec, 0) = arg;
   31272              : 
   31273          375 :   return lookup_template_class (std_init_list, argvec, NULL_TREE,
   31274          375 :                                 NULL_TREE, tf_warning_or_error);
   31275              : }
   31276              : 
   31277              : /* Replace auto in TYPE with std::initializer_list<auto>.  */
   31278              : 
   31279              : static tree
   31280          396 : listify_autos (tree type, tree auto_node)
   31281              : {
   31282          396 :   tree init_auto = listify (strip_top_quals (auto_node));
   31283          396 :   tree argvec = make_tree_vec (1);
   31284          396 :   TREE_VEC_ELT (argvec, 0) = init_auto;
   31285          396 :   if (processing_template_decl)
   31286            3 :     argvec = add_to_template_args (current_template_args (), argvec);
   31287          396 :   return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
   31288              : }
   31289              : 
   31290              : /* Hash traits for hashing possibly constrained 'auto'
   31291              :    TEMPLATE_TYPE_PARMs for use by do_auto_deduction.  */
   31292              : 
   31293              : struct auto_hash : default_hash_traits<tree>
   31294              : {
   31295              :   static inline hashval_t hash (tree);
   31296              :   static inline bool equal (tree, tree);
   31297              : };
   31298              : 
   31299              : /* Hash the 'auto' T.  */
   31300              : 
   31301              : inline hashval_t
   31302              : auto_hash::hash (tree t)
   31303              : {
   31304              :   if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
   31305              :     /* Matching constrained-type-specifiers denote the same template
   31306              :        parameter, so hash the constraint.  */
   31307              :     return iterative_hash_placeholder_constraint (c, 0);
   31308              :   else
   31309              :     /* But unconstrained autos are all separate, so just hash the pointer.  */
   31310              :     return iterative_hash_object (t, 0);
   31311              : }
   31312              : 
   31313              : /* Compare two 'auto's.  */
   31314              : 
   31315              : inline bool
   31316              : auto_hash::equal (tree t1, tree t2)
   31317              : {
   31318              :   if (t1 == t2)
   31319              :     return true;
   31320              : 
   31321              :   tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
   31322              :   tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
   31323              : 
   31324              :   /* Two unconstrained autos are distinct.  */
   31325              :   if (!c1 || !c2)
   31326              :     return false;
   31327              : 
   31328              :   return equivalent_placeholder_constraints (c1, c2);
   31329              : }
   31330              : 
   31331              : /* The stem for deduction guide names.  */
   31332              : const char *const dguide_base = "__dguide_";
   31333              : 
   31334              : /* Return the name for a deduction guide for class template TMPL.  */
   31335              : 
   31336              : tree
   31337      1313494 : dguide_name (tree tmpl)
   31338              : {
   31339      1313494 :   tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
   31340      1313494 :   tree tname = TYPE_IDENTIFIER (type);
   31341      1313494 :   char *buf = (char *) alloca (1 + strlen (dguide_base)
   31342              :                                + IDENTIFIER_LENGTH (tname));
   31343      1313494 :   memcpy (buf, dguide_base, strlen (dguide_base));
   31344      1313494 :   memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
   31345      1313494 :           IDENTIFIER_LENGTH (tname) + 1);
   31346      1313494 :   tree dname = get_identifier (buf);
   31347      1313494 :   TREE_TYPE (dname) = type;
   31348      1313494 :   return dname;
   31349              : }
   31350              : 
   31351              : /* True if NAME is the name of a deduction guide.  */
   31352              : 
   31353              : bool
   31354   1103174947 : dguide_name_p (tree name)
   31355              : {
   31356   1103174947 :   return (TREE_CODE (name) == IDENTIFIER_NODE
   31357   1103174945 :           && TREE_TYPE (name)
   31358   1164787138 :           && startswith (IDENTIFIER_POINTER (name), dguide_base));
   31359              : }
   31360              : 
   31361              : /* True if FN is a deduction guide.  */
   31362              : 
   31363              : bool
   31364    715636878 : deduction_guide_p (const_tree fn)
   31365              : {
   31366    715636878 :   if (DECL_P (fn))
   31367    715597732 :     if (tree name = DECL_NAME (fn))
   31368    715597620 :       return dguide_name_p (name);
   31369              :   return false;
   31370              : }
   31371              : 
   31372              : /* True if FN is the copy deduction guide, i.e. A(A)->A.  */
   31373              : 
   31374              : bool
   31375         2059 : copy_guide_p (const_tree fn)
   31376              : {
   31377         2059 :   gcc_assert (deduction_guide_p (fn));
   31378         2059 :   if (!DECL_ARTIFICIAL (fn))
   31379              :     return false;
   31380         2059 :   tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
   31381         2059 :   return (TREE_CHAIN (parms) == void_list_node
   31382         2059 :           && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
   31383              : }
   31384              : 
   31385              : /* True if FN is a guide generated from a constructor template.  */
   31386              : 
   31387              : bool
   31388           52 : template_guide_p (const_tree fn)
   31389              : {
   31390           52 :   gcc_assert (deduction_guide_p (fn));
   31391           52 :   if (!DECL_ARTIFICIAL (fn))
   31392              :     return false;
   31393           52 :   tree tmpl = DECL_TI_TEMPLATE (fn);
   31394           52 :   if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
   31395           40 :     return PRIMARY_TEMPLATE_P (org);
   31396              :   return false;
   31397              : }
   31398              : 
   31399              : /* True if FN is an aggregate initialization guide or the copy deduction
   31400              :    guide.  */
   31401              : 
   31402              : bool
   31403           12 : builtin_guide_p (const_tree fn)
   31404              : {
   31405           12 :   if (!deduction_guide_p (fn))
   31406              :     return false;
   31407           12 :   if (!DECL_ARTIFICIAL (fn))
   31408              :     /* Explicitly declared.  */
   31409              :     return false;
   31410           12 :   if (DECL_ABSTRACT_ORIGIN (fn))
   31411              :     /* Derived from a constructor.  */
   31412            0 :     return false;
   31413              :   return true;
   31414              : }
   31415              : 
   31416              : /* True if FN is a C++23 inherited guide.  */
   31417              : 
   31418              : bool
   31419         6152 : inherited_guide_p (const_tree fn)
   31420              : {
   31421         6152 :   gcc_assert (deduction_guide_p (fn));
   31422         6152 :   return LANG_DECL_FN_CHECK (fn)->context != NULL_TREE;
   31423              : }
   31424              : 
   31425              : /* Set the base class BASE from which the transformed guide FN
   31426              :    was inherited as part of C++23 inherited CTAD.  */
   31427              : 
   31428              : static void
   31429          214 : set_inherited_guide_context (const_tree fn, tree base)
   31430              : {
   31431          214 :   gcc_assert (deduction_guide_p (fn));
   31432          214 :   LANG_DECL_FN_CHECK (fn)->context = base;
   31433          214 : }
   31434              : 
   31435              : /* OLDDECL is a _DECL for a template parameter.  Return a similar parameter at
   31436              :    LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
   31437              :    template parameter types.  Note that the handling of template template
   31438              :    parameters relies on current_template_parms being set appropriately for the
   31439              :    new template.  */
   31440              : 
   31441              : static tree
   31442        81307 : rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
   31443              :                        tree tsubst_args, tsubst_flags_t complain)
   31444              : {
   31445        81307 :   if (olddecl == error_mark_node)
   31446              :     return error_mark_node;
   31447              : 
   31448        81304 :   tree oldidx = get_template_parm_index (olddecl);
   31449              : 
   31450        81304 :   tree newtype;
   31451        81304 :   if (TREE_CODE (olddecl) == TYPE_DECL
   31452         4413 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   31453              :     {
   31454        76926 :       tree oldtype = TREE_TYPE (olddecl);
   31455        76926 :       newtype = cxx_make_type (TREE_CODE (oldtype));
   31456        76926 :       TYPE_MAIN_VARIANT (newtype) = newtype;
   31457        76926 :     }
   31458              :   else
   31459              :     {
   31460         4378 :       newtype = TREE_TYPE (olddecl);
   31461         4378 :       if (type_uses_auto (newtype))
   31462              :         {
   31463              :           // Substitute once to fix references to other template parameters.
   31464            6 :           newtype = tsubst (newtype, tsubst_args,
   31465              :                             complain|tf_partial, NULL_TREE);
   31466              :           // Now substitute again to reduce the level of the auto.
   31467            6 :           newtype = tsubst (newtype, current_template_args (),
   31468              :                             complain, NULL_TREE);
   31469              :         }
   31470              :       else
   31471         4372 :         newtype = tsubst (newtype, tsubst_args,
   31472              :                           complain, NULL_TREE);
   31473              :     }
   31474              : 
   31475        81304 :   tree newdecl
   31476        81304 :     = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
   31477        81304 :                   DECL_NAME (olddecl), newtype);
   31478        81304 :   SET_DECL_TEMPLATE_PARM_P (newdecl);
   31479              : 
   31480        81304 :   tree newidx;
   31481        81304 :   if (TREE_CODE (olddecl) == TYPE_DECL
   31482         4413 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   31483              :     {
   31484        76926 :       newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
   31485        76926 :         = build_template_parm_index (index, level, level,
   31486              :                                      newdecl, newtype);
   31487       153852 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   31488        76926 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   31489        76926 :       TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
   31490              : 
   31491        76926 :       if (TREE_CODE (olddecl) == TEMPLATE_DECL)
   31492              :         {
   31493           35 :           tree newresult
   31494           35 :             = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
   31495           35 :                                    DECL_NAME (olddecl), newtype);
   31496           35 :           DECL_ARTIFICIAL (newresult) = true;
   31497           35 :           DECL_TEMPLATE_RESULT (newdecl) = newresult;
   31498              :           // First create a copy (ttargs) of tsubst_args with an
   31499              :           // additional level for the template template parameter's own
   31500              :           // template parameters (ttparms).
   31501           35 :           tree ttparms = (INNERMOST_TEMPLATE_PARMS
   31502           35 :                           (DECL_TEMPLATE_PARMS (olddecl)));
   31503           70 :           const int depth = TMPL_ARGS_DEPTH (tsubst_args);
   31504           35 :           tree ttargs = make_tree_vec (depth + 1);
   31505           91 :           for (int i = 0; i < depth; ++i)
   31506          112 :             TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
   31507           35 :           TREE_VEC_ELT (ttargs, depth)
   31508           35 :             = template_parms_level_to_args (ttparms);
   31509              :           // Substitute ttargs into ttparms to fix references to
   31510              :           // other template parameters.
   31511           35 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   31512              :                                                  complain|tf_partial);
   31513              :           // Now substitute again with args based on tparms, to reduce
   31514              :           // the level of the ttparms.
   31515           35 :           ttargs = current_template_args ();
   31516           35 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   31517              :                                                  complain);
   31518              :           // Finally, tack the adjusted parms onto tparms.
   31519           35 :           ttparms = tree_cons (size_int (level + 1), ttparms,
   31520              :                                copy_node (current_template_parms));
   31521              :           // As with all template template parms, the parameter list captured
   31522              :           // by this template template parm that corresponds to its own level
   31523              :           // should be empty.  This avoids infinite recursion when structurally
   31524              :           // comparing two such rewritten template template parms (PR102479).
   31525           35 :           gcc_assert (!TREE_VEC_LENGTH
   31526              :                       (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
   31527           35 :           gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
   31528           35 :           TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
   31529              :           // All done.
   31530           35 :           DECL_TEMPLATE_PARMS (newdecl) = ttparms;
   31531           35 :           DECL_TEMPLATE_INFO (newresult)
   31532           70 :             = build_template_info (newdecl, template_parms_to_args (ttparms));
   31533              :         }
   31534              : 
   31535        76926 :       if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
   31536            0 :         SET_TYPE_STRUCTURAL_EQUALITY (newtype);
   31537              :       else
   31538        76926 :         TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
   31539              :     }
   31540              :   else
   31541              :     {
   31542         4378 :       tree oldconst = TEMPLATE_PARM_DECL (oldidx);
   31543         4378 :       tree newconst
   31544         4378 :         = build_decl (DECL_SOURCE_LOCATION (oldconst),
   31545         4378 :                       TREE_CODE (oldconst),
   31546         4378 :                       DECL_NAME (oldconst), newtype);
   31547        17512 :       TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
   31548         4378 :         = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
   31549         4378 :       SET_DECL_TEMPLATE_PARM_P (newconst);
   31550         4378 :       newidx = build_template_parm_index (index, level, level,
   31551              :                                           newconst, newtype);
   31552         8756 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   31553         4378 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   31554         4378 :       DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
   31555              :     }
   31556              : 
   31557              :   return newdecl;
   31558              : }
   31559              : 
   31560              : /* As rewrite_template_parm, but for the whole TREE_LIST representing a
   31561              :    template parameter.  */
   31562              : 
   31563              : static tree
   31564        81307 : rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
   31565              :                     tree targs, unsigned targs_index, tsubst_flags_t complain)
   31566              : {
   31567        81307 :   tree olddecl = TREE_VALUE (oldelt);
   31568        81307 :   tree newdecl = rewrite_template_parm (olddecl, index, level,
   31569              :                                         targs, complain);
   31570        81307 :   if (newdecl == error_mark_node)
   31571              :     return error_mark_node;
   31572        81304 :   tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
   31573              :                                      targs, complain, NULL_TREE);
   31574        81304 :   tree list = build_tree_list (newdef, newdecl);
   31575       162608 :   TEMPLATE_PARM_CONSTRAINTS (list)
   31576        81304 :     = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
   31577              :                               targs, complain, NULL_TREE);
   31578       162608 :   int depth = TMPL_ARGS_DEPTH (targs);
   31579       162608 :   TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
   31580        81304 :   return list;
   31581              : }
   31582              : 
   31583              : /* Returns a C++17 class deduction guide template based on the constructor
   31584              :    CTOR.  As a special case, CTOR can be a RECORD_TYPE for an implicit default
   31585              :    guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
   31586              :    aggregate initialization guide.  OUTER_ARGS are the template arguments
   31587              :    for the enclosing scope of the class.  */
   31588              : 
   31589              : static tree
   31590        63238 : build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
   31591              : {
   31592        63238 :   tree tparms, targs, fparms, fargs, ci;
   31593        63238 :   bool memtmpl = false;
   31594        63238 :   bool explicit_p;
   31595        63238 :   location_t loc;
   31596        63238 :   tree fn_tmpl = NULL_TREE;
   31597              : 
   31598        63238 :   if (outer_args)
   31599              :     {
   31600          931 :       ++processing_template_decl;
   31601          931 :       type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
   31602          931 :       --processing_template_decl;
   31603              :     }
   31604              : 
   31605        63238 :   if (!DECL_DECLARES_FUNCTION_P (ctor))
   31606              :     {
   31607         7039 :       if (TYPE_P (ctor))
   31608              :         {
   31609         5538 :           bool copy_p = TYPE_REF_P (ctor);
   31610         5538 :           if (copy_p)
   31611         4898 :             fparms = tree_cons (NULL_TREE, type, void_list_node);
   31612              :           else
   31613          640 :             fparms = void_list_node;
   31614              :         }
   31615         1501 :       else if (TREE_CODE (ctor) == TREE_LIST)
   31616              :         fparms = ctor;
   31617              :       else
   31618            0 :         gcc_unreachable ();
   31619              : 
   31620         7039 :       tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
   31621         7039 :       tparms = DECL_TEMPLATE_PARMS (ctmpl);
   31622         7039 :       targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   31623         7039 :       ci = NULL_TREE;
   31624         7039 :       fargs = NULL_TREE;
   31625         7039 :       loc = DECL_SOURCE_LOCATION (ctmpl);
   31626         7039 :       explicit_p = false;
   31627              :     }
   31628              :   else
   31629              :     {
   31630        56199 :       ++processing_template_decl;
   31631        56199 :       bool ok = true;
   31632              : 
   31633        56199 :       complain |= tf_dguide;
   31634              : 
   31635        56199 :       fn_tmpl
   31636        56199 :         = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
   31637        10026 :            : DECL_TI_TEMPLATE (ctor));
   31638        56199 :       if (outer_args)
   31639          737 :         fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
   31640        56199 :       ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
   31641              : 
   31642        56199 :       tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
   31643              :       /* If type is a member class template, DECL_TI_ARGS (ctor) will have
   31644              :          fully specialized args for the enclosing class.  Strip those off, as
   31645              :          the deduction guide won't have those template parameters.  */
   31646       112398 :       targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
   31647        56199 :                                                 TMPL_PARMS_DEPTH (tparms));
   31648              :       /* Discard the 'this' parameter.  */
   31649        56199 :       fparms = FUNCTION_ARG_CHAIN (ctor);
   31650        56199 :       fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
   31651        56199 :       ci = get_constraints (ctor);
   31652        56199 :       loc = DECL_SOURCE_LOCATION (ctor);
   31653        56199 :       explicit_p = DECL_NONCONVERTING_P (ctor);
   31654              : 
   31655        56199 :       if (PRIMARY_TEMPLATE_P (fn_tmpl))
   31656              :         {
   31657        46173 :           memtmpl = true;
   31658              : 
   31659              :           /* For a member template constructor, we need to flatten the two
   31660              :              template parameter lists into one, and then adjust the function
   31661              :              signature accordingly.  This gets...complicated.  */
   31662        46173 :           tree save_parms = current_template_parms;
   31663              : 
   31664              :           /* For a member template we should have two levels of parms/args, one
   31665              :              for the class and one for the constructor.  We stripped
   31666              :              specialized args for further enclosing classes above.  */
   31667        46173 :           const int depth = 2;
   31668        92346 :           gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
   31669              : 
   31670              :           /* Template args for translating references to the two-level template
   31671              :              parameters into references to the one-level template parameters we
   31672              :              are creating.  */
   31673        46173 :           tree tsubst_args = copy_node (targs);
   31674        46173 :           TMPL_ARGS_LEVEL (tsubst_args, depth)
   31675        92346 :             = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
   31676              : 
   31677              :           /* Template parms for the constructor template.  */
   31678        46173 :           tree ftparms = TREE_VALUE (tparms);
   31679        46173 :           unsigned flen = TREE_VEC_LENGTH (ftparms);
   31680              :           /* Template parms for the class template.  */
   31681        46173 :           tparms = TREE_CHAIN (tparms);
   31682        46173 :           tree ctparms = TREE_VALUE (tparms);
   31683        46173 :           unsigned clen = TREE_VEC_LENGTH (ctparms);
   31684              :           /* Template parms for the deduction guide start as a copy of the
   31685              :              template parms for the class.  We set current_template_parms for
   31686              :              lookup_template_class_1.  */
   31687        46173 :           current_template_parms = tparms = copy_node (tparms);
   31688        46173 :           tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
   31689       102648 :           for (unsigned i = 0; i < clen; ++i)
   31690        56475 :             TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
   31691              : 
   31692              :           /* Now we need to rewrite the constructor parms to append them to the
   31693              :              class parms.  */
   31694       126286 :           for (unsigned i = 0; i < flen; ++i)
   31695              :             {
   31696        80113 :               unsigned index = i + clen;
   31697        80113 :               unsigned level = 1;
   31698        80113 :               tree oldelt = TREE_VEC_ELT (ftparms, i);
   31699        80113 :               tree newelt
   31700        80113 :                 = rewrite_tparm_list (oldelt, index, level,
   31701              :                                       tsubst_args, i, complain);
   31702        80113 :               if (newelt == error_mark_node)
   31703            3 :                 ok = false;
   31704        80113 :               TREE_VEC_ELT (new_vec, index) = newelt;
   31705              :             }
   31706              : 
   31707              :           /* Now we have a final set of template parms to substitute into the
   31708              :              function signature.  */
   31709        46173 :           targs = template_parms_to_args (tparms);
   31710        46173 :           fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
   31711              :                                      complain, ctor);
   31712        46173 :           if (fparms == error_mark_node)
   31713            0 :             ok = false;
   31714        46173 :           if (ci)
   31715              :             {
   31716        44664 :               if (outer_args)
   31717              :                 /* FIXME: We'd like to avoid substituting outer template
   31718              :                    arguments into the constraint ahead of time, but the
   31719              :                    construction of tsubst_args assumes that outer arguments
   31720              :                    are already substituted in.  */
   31721          426 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   31722        44664 :               ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
   31723              :             }
   31724              : 
   31725              :           /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
   31726              :              cp_unevaluated_operand.  */
   31727        46173 :           cp_evaluated ev;
   31728        46173 :           fargs = tsubst (fargs, tsubst_args, complain, ctor);
   31729        46173 :           current_template_parms = save_parms;
   31730        46173 :         }
   31731              :       else
   31732              :         {
   31733              :           /* Substitute in the same arguments to rewrite class members into
   31734              :              references to members of an unknown specialization.  */
   31735        10026 :           cp_evaluated ev;
   31736        10026 :           fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
   31737        10026 :          if (fparms == error_mark_node)
   31738            3 :            ok = false;
   31739        10026 :           fargs = tsubst (fargs, targs, complain, ctor);
   31740        10026 :           if (ci)
   31741              :             {
   31742         4663 :               if (outer_args)
   31743              :                 /* FIXME: As above.  */
   31744            6 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   31745         4663 :               ci = tsubst_constraint_info (ci, targs, complain, ctor);
   31746              :             }
   31747        10026 :         }
   31748              : 
   31749        56199 :       --processing_template_decl;
   31750        56199 :       if (!ok)
   31751            6 :         return error_mark_node;
   31752              :     }
   31753              : 
   31754        63232 :   if (!memtmpl)
   31755              :     {
   31756              :       /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE.  */
   31757        17062 :       tparms = copy_node (tparms);
   31758        17062 :       INNERMOST_TEMPLATE_PARMS (tparms)
   31759        17062 :         = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
   31760              :     }
   31761              : 
   31762        63232 :   tree fntype = cp_build_function_type (type, fparms);
   31763        63232 :   tree ded_fn = build_lang_decl_loc (loc,
   31764              :                                      FUNCTION_DECL,
   31765              :                                      dguide_name (type), fntype);
   31766        63232 :   DECL_ARGUMENTS (ded_fn) = fargs;
   31767        63232 :   DECL_ARTIFICIAL (ded_fn) = true;
   31768        63232 :   DECL_NONCONVERTING_P (ded_fn) = explicit_p;
   31769        63232 :   tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
   31770        63232 :   DECL_ARTIFICIAL (ded_tmpl) = true;
   31771        63232 :   DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
   31772        63232 :   DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
   31773        63232 :   if (DECL_P (ctor))
   31774        56193 :     DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
   31775        63232 :   if (ci)
   31776        49327 :     set_constraints (ded_tmpl, ci);
   31777              : 
   31778              :   return ded_tmpl;
   31779              : }
   31780              : 
   31781              : /* Add to LIST the member types for the reshaped initializer CTOR.  */
   31782              : 
   31783              : static tree
   31784         2323 : collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
   31785              : {
   31786         2323 :   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
   31787         2323 :   tree idx, val; unsigned i;
   31788         9331 :   FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
   31789              :     {
   31790         3054 :       tree ftype = elt ? elt : TREE_TYPE (idx);
   31791          881 :       if (BRACE_ENCLOSED_INITIALIZER_P (val)
   31792         7889 :           && CONSTRUCTOR_BRACES_ELIDED_P (val))
   31793              :         {
   31794          831 :           tree subelt = NULL_TREE;
   31795          831 :           if (TREE_CODE (ftype) == ARRAY_TYPE)
   31796          825 :             subelt = TREE_TYPE (ftype);
   31797          831 :           list = collect_ctor_idx_types (val, list, subelt);
   31798          831 :           continue;
   31799          831 :         }
   31800         6177 :       tree arg = NULL_TREE;
   31801         6177 :       if (i == v->length() - 1
   31802         6177 :           && PACK_EXPANSION_P (ftype))
   31803              :         /* Give the trailing pack expansion parameter a default argument to
   31804              :            match aggregate initialization behavior, even if we deduce the
   31805              :            length of the pack separately to more than we have initializers. */
   31806           16 :         arg = build_constructor (init_list_type_node, NULL);
   31807              :       /* if ei is of array type and xi is a braced-init-list or string literal,
   31808              :          Ti is an rvalue reference to the declared type of ei */
   31809         6177 :       STRIP_ANY_LOCATION_WRAPPER (val);
   31810         6177 :       if (TREE_CODE (ftype) == ARRAY_TYPE
   31811         6177 :           && (BRACE_ENCLOSED_INITIALIZER_P (val)
   31812            6 :               || TREE_CODE (val) == STRING_CST))
   31813              :         {
   31814           33 :           if (TREE_CODE (val) == STRING_CST)
   31815            6 :             ftype = cp_build_qualified_type
   31816            6 :               (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
   31817           33 :           ftype = (cp_build_reference_type
   31818           39 :                    (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
   31819              :         }
   31820         6177 :       list = tree_cons (arg, ftype, list);
   31821              :     }
   31822              : 
   31823         2323 :   return list;
   31824              : }
   31825              : 
   31826              : /* Return whether ETYPE is, or is derived from, a specialization of TMPL.  */
   31827              : 
   31828              : static bool
   31829         4094 : is_spec_or_derived (tree etype, tree tmpl)
   31830              : {
   31831         4094 :   if (!etype || !CLASS_TYPE_P (etype))
   31832              :     return false;
   31833              : 
   31834         1640 :   etype = cv_unqualified (etype);
   31835         1640 :   tree type = TREE_TYPE (tmpl);
   31836         1640 :   tree tparms = (INNERMOST_TEMPLATE_PARMS
   31837         1640 :                  (DECL_TEMPLATE_PARMS (tmpl)));
   31838         1640 :   tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   31839         1640 :   int err = unify (tparms, targs, type, etype,
   31840              :                    UNIFY_ALLOW_DERIVED, /*explain*/false);
   31841         1640 :   ggc_free (targs);
   31842         1640 :   return !err;
   31843              : }
   31844              : 
   31845              : /* Return a C++20 aggregate deduction candidate for TYPE initialized from
   31846              :    INIT.  */
   31847              : 
   31848              : static tree
   31849         6218 : maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
   31850              : {
   31851         6218 :   if (cxx_dialect < cxx20)
   31852              :     return NULL_TREE;
   31853              : 
   31854         6046 :   if (init == NULL_TREE)
   31855              :     return NULL_TREE;
   31856              : 
   31857         5977 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   31858              :     {
   31859          143 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   31860          143 :       tree tinfo = get_template_info (under);
   31861          143 :       if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
   31862            3 :         return alias_ctad_tweaks (tmpl, guide);
   31863              :       return NULL_TREE;
   31864              :     }
   31865              : 
   31866              :   /* We might be creating a guide for a class member template, e.g.,
   31867              : 
   31868              :        template<typename U> struct A {
   31869              :          template<typename T> struct B { T t; };
   31870              :        };
   31871              : 
   31872              :      At this point, A will have been instantiated.  Below, we need to
   31873              :      use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types.  */
   31874         5834 :   const bool member_template_p
   31875         5834 :     = (DECL_TEMPLATE_INFO (tmpl)
   31876         5834 :        && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
   31877         5834 :   tree type = TREE_TYPE (tmpl);
   31878         5834 :   tree template_type = (member_template_p
   31879         5834 :                         ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
   31880         1071 :                         : type);
   31881         5834 :   if (!CP_AGGREGATE_TYPE_P (template_type))
   31882              :     return NULL_TREE;
   31883              : 
   31884              :   /* No aggregate candidate for copy-initialization.  */
   31885         1712 :   if (args->length() == 1)
   31886              :     {
   31887         1624 :       tree val = (*args)[0];
   31888         1624 :       if (is_spec_or_derived (TREE_TYPE (val), tmpl))
   31889              :         return NULL_TREE;
   31890              :     }
   31891              : 
   31892              :   /* If we encounter a problem, we just won't add the candidate.  */
   31893         1606 :   tsubst_flags_t complain = tf_none;
   31894              : 
   31895         1606 :   tree parms = NULL_TREE;
   31896         1606 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   31897              :     {
   31898         1503 :       init = reshape_init (template_type, init, complain);
   31899         1503 :       if (init == error_mark_node)
   31900              :         return NULL_TREE;
   31901         1492 :       parms = collect_ctor_idx_types (init, parms);
   31902              :     }
   31903          103 :   else if (TREE_CODE (init) == TREE_LIST)
   31904              :     {
   31905          101 :       int len = list_length (init);
   31906          121 :       for (tree binfo : BINFO_BASE_BINFOS (TYPE_BINFO (template_type)))
   31907              :         {
   31908           20 :           if (!len)
   31909              :             break;
   31910           20 :           parms = tree_cons (NULL_TREE, BINFO_TYPE (binfo), parms);
   31911           20 :           --len;
   31912              :         }
   31913          101 :       for (tree field = TYPE_FIELDS (template_type);
   31914          342 :            len;
   31915          241 :            --len, field = DECL_CHAIN (field))
   31916              :         {
   31917          245 :           field = next_aggregate_field (field);
   31918          245 :           if (!field)
   31919              :             return NULL_TREE;
   31920          241 :           tree ftype = finish_decltype_type (field, true, complain);
   31921          241 :           parms = tree_cons (NULL_TREE, ftype, parms);
   31922              :         }
   31923              :     }
   31924              :   else
   31925              :     /* Aggregate initialization doesn't apply to an initializer expression.  */
   31926              :     return NULL_TREE;
   31927              : 
   31928              :   /* If we're creating a deduction guide for a member class template,
   31929              :      we've used the original template pattern type for the reshape_init
   31930              :      above; this is done because we want PARMS to be a template parameter
   31931              :      type, something that can be deduced when used as a function template
   31932              :      parameter.  At this point the outer class template has already been
   31933              :      partially instantiated (we deferred the deduction until the enclosing
   31934              :      scope is non-dependent).  Therefore we have to partially instantiate
   31935              :      PARMS, so that its template level is properly reduced and we don't get
   31936              :      mismatches when deducing types using the guide with PARMS.  */
   31937         1589 :   if (member_template_p)
   31938              :     {
   31939           26 :       ++processing_template_decl;
   31940           26 :       parms = tsubst (parms, outer_template_args (tmpl), complain, init);
   31941           26 :       --processing_template_decl;
   31942              :     }
   31943              : 
   31944         1589 :   if (parms)
   31945              :     {
   31946         1501 :       tree last = parms;
   31947         1501 :       parms = nreverse (parms);
   31948         1501 :       TREE_CHAIN (last) = void_list_node;
   31949         1501 :       tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
   31950         1501 :       return guide;
   31951              :     }
   31952              : 
   31953              :   return NULL_TREE;
   31954              : }
   31955              : 
   31956              : /* UGUIDES are the deduction guides for the underlying template of alias
   31957              :    template TMPL; adjust them to be deduction guides for TMPL.
   31958              : 
   31959              :    This routine also handles C++23 inherited CTAD, in which case TMPL is a
   31960              :    TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
   31961              :    the template parameter list of the alias template (equivalently, of the
   31962              :    derived class) and TREE_VALUE the defining-type-id (equivalently, the
   31963              :    base whose guides we're inheriting).  UGUIDES are the base's guides.  */
   31964              : 
   31965              : static tree
   31966          169 : alias_ctad_tweaks (tree tmpl, tree uguides)
   31967              : {
   31968              :   /* [over.match.class.deduct]: When resolving a placeholder for a deduced
   31969              :      class type (9.2.8.2) where the template-name names an alias template A,
   31970              :      the defining-type-id of A must be of the form
   31971              : 
   31972              :      typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   31973              : 
   31974              :      as specified in 9.2.8.2. The guides of A are the set of functions or
   31975              :      function templates formed as follows. For each function or function
   31976              :      template f in the guides of the template named by the simple-template-id
   31977              :      of the defining-type-id, the template arguments of the return type of f
   31978              :      are deduced from the defining-type-id of A according to the process in
   31979              :      13.10.2.5 with the exception that deduction does not fail if not all
   31980              :      template arguments are deduced. Let g denote the result of substituting
   31981              :      these deductions into f. If substitution succeeds, form a function or
   31982              :      function template f' with the following properties and add it to the set
   31983              :      of guides of A:
   31984              : 
   31985              :      * The function type of f' is the function type of g.
   31986              : 
   31987              :      * If f is a function template, f' is a function template whose template
   31988              :      parameter list consists of all the template parameters of A (including
   31989              :      their default template arguments) that appear in the above deductions or
   31990              :      (recursively) in their default template arguments, followed by the
   31991              :      template parameters of f that were not deduced (including their default
   31992              :      template arguments), otherwise f' is not a function template.
   31993              : 
   31994              :      * The associated constraints (13.5.2) are the conjunction of the
   31995              :      associated constraints of g and a constraint that is satisfied if and only
   31996              :      if the arguments of A are deducible (see below) from the return type.
   31997              : 
   31998              :      * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
   31999              :      be so as well.
   32000              : 
   32001              :      * If f was generated from a deduction-guide (12.4.1.8), then f' is
   32002              :      considered to be so as well.
   32003              : 
   32004              :      * The explicit-specifier of f' is the explicit-specifier of g (if
   32005              :      any).  */
   32006              : 
   32007          169 :   enum { alias, inherited } ctad_kind;
   32008          169 :   tree atype, fullatparms, utype, name;
   32009          169 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   32010              :     {
   32011          123 :       ctad_kind = alias;
   32012          123 :       atype = TREE_TYPE (tmpl);
   32013          123 :       fullatparms = DECL_TEMPLATE_PARMS (tmpl);
   32014          123 :       utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32015          123 :       name = dguide_name (tmpl);
   32016              :     }
   32017              :   else
   32018              :     {
   32019           46 :       ctad_kind = inherited;
   32020           46 :       atype = NULL_TREE;
   32021           46 :       fullatparms = TREE_PURPOSE (tmpl);
   32022           46 :       utype = TREE_VALUE (tmpl);
   32023           46 :       name = dguide_name (TPARMS_PRIMARY_TEMPLATE
   32024              :                           (INNERMOST_TEMPLATE_PARMS (fullatparms)));
   32025              :     }
   32026              : 
   32027          169 :   tsubst_flags_t complain = tf_partial;
   32028          169 :   tree aguides = NULL_TREE;
   32029          169 :   tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
   32030          169 :   unsigned natparms = TREE_VEC_LENGTH (atparms);
   32031          881 :   for (tree f : lkp_range (uguides))
   32032              :     {
   32033          543 :       tree in_decl = f;
   32034          543 :       location_t loc = DECL_SOURCE_LOCATION (f);
   32035          543 :       tree ret = TREE_TYPE (TREE_TYPE (f));
   32036          543 :       tree fprime = f;
   32037          543 :       if (TREE_CODE (f) == TEMPLATE_DECL)
   32038              :         {
   32039          534 :           processing_template_decl_sentinel ptds (/*reset*/false);
   32040          534 :           ++processing_template_decl;
   32041              : 
   32042              :           /* Deduce template arguments for f from the type-id of A.  */
   32043          534 :           tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
   32044          534 :           unsigned len = TREE_VEC_LENGTH (ftparms);
   32045          534 :           tree targs = make_tree_vec (len);
   32046          534 :           int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
   32047          534 :           if (err)
   32048              :             /* CWG2664: Discard any deductions, still build the guide.  */
   32049           32 :             for (unsigned i = 0; i < len; ++i)
   32050           16 :               TREE_VEC_ELT (targs, i) = NULL_TREE;
   32051              : 
   32052              :           /* The number of parms for f' is the number of parms of A used in
   32053              :              the deduced arguments plus non-deduced parms of f.  */
   32054              :           unsigned ndlen = 0;
   32055              :           unsigned j;
   32056         2064 :           for (unsigned i = 0; i < len; ++i)
   32057         1530 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   32058          193 :               ++ndlen;
   32059          534 :           find_template_parameter_info ftpi (fullatparms);
   32060          534 :           ftpi.find_in_recursive (targs);
   32061          534 :           unsigned nusedatparms = ftpi.num_found ();
   32062          534 :           unsigned nfparms = nusedatparms + ndlen;
   32063          534 :           tree gtparms = make_tree_vec (nfparms);
   32064              : 
   32065              :           /* Set current_template_parms as in build_deduction_guide.  */
   32066          534 :           auto ctp = make_temp_override (current_template_parms);
   32067          534 :           current_template_parms = copy_node (fullatparms);
   32068          534 :           TREE_VALUE (current_template_parms) = gtparms;
   32069              : 
   32070          534 :           j = 0;
   32071          534 :           unsigned level = 1;
   32072              : 
   32073              :           /* First copy over the used parms of A.  */
   32074          534 :           tree atargs = make_tree_vec (natparms);
   32075         1608 :           for (unsigned i = 0; i < natparms; ++i)
   32076              :             {
   32077         1074 :               tree elt = TREE_VEC_ELT (atparms, i);
   32078         1074 :               if (ftpi.found (elt))
   32079              :                 {
   32080         1001 :                   unsigned index = j++;
   32081         1001 :                   tree nelt = rewrite_tparm_list (elt, index, level,
   32082              :                                                   atargs, i, complain);
   32083         1001 :                   TREE_VEC_ELT (gtparms, index) = nelt;
   32084              :                 }
   32085              :             }
   32086          534 :           gcc_checking_assert (j == nusedatparms);
   32087              : 
   32088              :           /* Adjust the deduced template args for f to refer to the A parms
   32089              :              with their new indexes.  */
   32090          534 :           if (nusedatparms && nusedatparms != natparms)
   32091           18 :             targs = tsubst_template_args (targs, atargs, complain, in_decl);
   32092              : 
   32093              :           /* Now rewrite the non-deduced parms of f.  */
   32094         1322 :           for (unsigned i = 0; ndlen && i < len; ++i)
   32095          788 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   32096              :               {
   32097          193 :                 --ndlen;
   32098          193 :                 unsigned index = j++;
   32099          193 :                 tree oldlist = TREE_VEC_ELT (ftparms, i);
   32100          193 :                 tree list = rewrite_tparm_list (oldlist, index, level,
   32101              :                                                 targs, i, complain);
   32102          193 :                 TREE_VEC_ELT (gtparms, index) = list;
   32103              :               }
   32104          534 :           gtparms = build_tree_list (size_one_node, gtparms);
   32105              : 
   32106              :           /* Substitute the deduced arguments plus the rewritten template
   32107              :              parameters into f to get g.  This covers the type, copyness,
   32108              :              guideness, and explicit-specifier.  */
   32109          534 :           tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain,
   32110              :                                /*use_spec_table=*/false);
   32111          534 :           if (g == error_mark_node)
   32112            3 :             continue;
   32113          531 :           DECL_NAME (g) = name;
   32114          531 :           if (nfparms == 0)
   32115              :             {
   32116              :               /* The targs are all non-dependent, so g isn't a template.  */
   32117           26 :               fprime = g;
   32118           26 :               ret = TREE_TYPE (TREE_TYPE (fprime));
   32119           26 :               goto non_template;
   32120              :             }
   32121          505 :           DECL_USE_TEMPLATE (g) = 0;
   32122          505 :           fprime = build_template_decl (g, gtparms, false);
   32123          505 :           DECL_TEMPLATE_RESULT (fprime) = g;
   32124          505 :           TREE_TYPE (fprime) = TREE_TYPE (g);
   32125          505 :           tree gtargs = template_parms_to_args (gtparms);
   32126          505 :           DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
   32127          505 :           DECL_PRIMARY_TEMPLATE (fprime) = fprime;
   32128              : 
   32129              :           /* Substitute the associated constraints.  */
   32130          505 :           tree ci = get_constraints (f);
   32131          505 :           if (ci)
   32132              :             {
   32133          119 :               if (tree outer_targs = outer_template_args (f))
   32134            6 :                 ci = tsubst_constraint_info (ci, outer_targs,
   32135              :                                              complain & ~tf_partial, in_decl);
   32136          119 :               ci = tsubst_constraint_info (ci, targs, complain, in_decl);
   32137              :             }
   32138          505 :           if (ci == error_mark_node)
   32139            0 :             continue;
   32140              : 
   32141              :           /* Add a constraint that the return type matches the instantiation of
   32142              :              A with the same template arguments.  */
   32143          505 :           ret = TREE_TYPE (TREE_TYPE (fprime));
   32144          505 :           if (ctad_kind == alias
   32145              :               /* Use template_args_equal instead of same_type_p to get the
   32146              :                  comparing_dependent_aliases behavior.  */
   32147          505 :               && !template_args_equal (atype, ret))
   32148              :             {
   32149           79 :               tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
   32150           79 :               ci = append_constraint (ci, same);
   32151              :             }
   32152              : 
   32153          505 :           if (ci)
   32154              :             {
   32155          186 :               remove_constraints (fprime);
   32156          186 :               set_constraints (fprime, ci);
   32157              :             }
   32158         1068 :         }
   32159              :       else
   32160              :         {
   32161              :           /* For a non-template deduction guide, if the arguments of A aren't
   32162              :              deducible from the return type, don't add the candidate.  */
   32163            9 :         non_template:
   32164           38 :           if (ctad_kind == alias
   32165           35 :               && !type_targs_deducible_from (tmpl, ret))
   32166            3 :             continue;
   32167              :         }
   32168              : 
   32169              :       /* Rewrite the return type of the inherited guide in terms of the
   32170              :          derived class.  This is specified as replacing the return type R
   32171              :          with typename CC<R>::type where the partially specialized CC maps a
   32172              :          base class specialization to a specialization of the derived class
   32173              :          having such a base (inducing substitution failure if no such derived
   32174              :          class exists).
   32175              : 
   32176              :          As specified this mapping would be done at instantiation time using
   32177              :          non-dependent template arguments, but we do it ahead of time using
   32178              :          the generic arguments.  This seems to be good enough since generic
   32179              :          deduction should succeed only if concrete deduction would.  */
   32180          537 :       if (ctad_kind == inherited)
   32181              :         {
   32182          224 :           processing_template_decl_sentinel ptds (/*reset*/false);
   32183          224 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   32184          202 :             ++processing_template_decl;
   32185              : 
   32186          224 :           tree targs = type_targs_deducible_from (tmpl, ret);
   32187          224 :           if (!targs)
   32188           10 :             continue;
   32189              : 
   32190          214 :           if (TREE_CODE (f) != TEMPLATE_DECL)
   32191            4 :             fprime = copy_decl (fprime);
   32192          214 :           tree fntype = TREE_TYPE (fprime);
   32193          214 :           ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), targs,
   32194              :                                        in_decl, NULL_TREE, complain);
   32195          214 :           fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype),
   32196          214 :                                         TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
   32197          214 :           TREE_TYPE (fprime) = fntype;
   32198          214 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   32199          202 :             TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype;
   32200          214 :           set_inherited_guide_context (fprime, utype);
   32201          224 :         }
   32202              : 
   32203          527 :       aguides = lookup_add (fprime, aguides);
   32204              :     }
   32205              : 
   32206          169 :   return aguides;
   32207              : }
   32208              : 
   32209              : /* CTOR is a using-decl inheriting the constructors of some base of the class
   32210              :    template TMPL; adjust the base's guides be deduction guides for TMPL.  */
   32211              : 
   32212              : static tree
   32213           52 : inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain)
   32214              : {
   32215              :   /* [over.match.class.deduct]: In addition, if C is defined and inherits
   32216              :      constructors ([namespace.udecl]) from a direct base class denoted in the
   32217              :      base-specifier-list by a class-or-decltype B, let A be an alias template
   32218              :      whose template parameter list is that of C and whose defining-type-id is
   32219              :      B.  If A is a deducible template ([dcl.type.simple]), the set contains the
   32220              :      guides of A with the return type R of each guide replaced with typename
   32221              :      CC::type given a class template
   32222              : 
   32223              :      template <typename> class CC;
   32224              : 
   32225              :      whose primary template is not defined and with a single partial
   32226              :      specialization whose template parameter list is that of A and whose
   32227              :      template argument list is a specialization of A with the template argument
   32228              :      list of A ([temp.dep.type]) having a member typedef type designating a
   32229              :      template specialization with the template argument list of A but with C as
   32230              :      the template.  */
   32231              : 
   32232           52 :   tree scope = USING_DECL_SCOPE (ctor);
   32233           52 :   if (TREE_CODE (scope) == TYPENAME_TYPE
   32234           52 :       && (TYPE_IDENTIFIER (TYPE_CONTEXT (scope))
   32235            6 :           == TYPENAME_TYPE_FULLNAME (scope)))
   32236              :     /* Recognize using B<T>::B::B as an inherited constructor.  */
   32237              :     /* FIXME: Also recognize using C::B::B?  We might have to call
   32238              :        resolve_typename_type for that.  */
   32239            4 :     scope = TYPE_CONTEXT (scope);
   32240           50 :   if (!CLASS_TYPE_P (scope)
   32241           50 :       || !CLASSTYPE_TEMPLATE_INFO (scope)
   32242           98 :       || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
   32243              :     return NULL_TREE;
   32244              : 
   32245           46 :   tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope);
   32246           46 :   bool any_dguides_p;
   32247           46 :   tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope),
   32248              :                                        any_dguides_p, complain);
   32249           46 :   return alias_ctad_tweaks (t, uguides);
   32250              : }
   32251              : 
   32252              : /* If template arguments for TMPL can be deduced from TYPE, return
   32253              :    the deduced arguments, otherwise return NULL_TREE.
   32254              :    Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
   32255              :    [over.match.class.deduct].
   32256              : 
   32257              :    This check is specified in terms of partial specialization, so the behavior
   32258              :    should be parallel to that of get_partial_spec_bindings.  */
   32259              : 
   32260              : tree
   32261          322 : type_targs_deducible_from (tree tmpl, tree type)
   32262              : {
   32263          322 :   tree tparms, ttype;
   32264          322 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   32265              :     {
   32266              :       /* If tmpl is a class template, this is trivial: it's deducible if
   32267              :          TYPE is a specialization of TMPL.  */
   32268           98 :       if (DECL_CLASS_TEMPLATE_P (tmpl))
   32269              :         {
   32270            0 :           if (CLASS_TYPE_P (type)
   32271            0 :               && CLASSTYPE_TEMPLATE_INFO (type)
   32272            0 :               && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
   32273            0 :             return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   32274              :           else
   32275              :             return NULL_TREE;
   32276              :         }
   32277              : 
   32278              :       /* Otherwise it's an alias template.  */
   32279           98 :       tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
   32280           98 :       ttype = TREE_TYPE (tmpl);
   32281              :     }
   32282              :   else
   32283              :     {
   32284              :       /* TMPL is a synthetic alias template represented as a TREE_LIST as
   32285              :          per alias_ctad_tweaks.  */
   32286          224 :       tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
   32287          224 :       ttype = TREE_VALUE (tmpl);
   32288          224 :       tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype);
   32289          216 :       if (!ti)
   32290              :         /* TTYPE is a typedef to a template-id.  */
   32291            8 :         ti = TYPE_TEMPLATE_INFO (ttype);
   32292          224 :       tmpl = TI_TEMPLATE (ti);
   32293              :     }
   32294              : 
   32295          322 :   int len = TREE_VEC_LENGTH (tparms);
   32296          322 :   tree targs = make_tree_vec (len);
   32297          322 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   32298              : 
   32299          322 :  again:
   32300          322 :   if (unify (tparms, targs, ttype, type,
   32301              :              UNIFY_ALLOW_NONE, false))
   32302              :     return NULL_TREE;
   32303              : 
   32304              :   /* We don't fail on an undeduced targ the second time through (like
   32305              :      get_partial_spec_bindings) because we're going to try defaults.  */
   32306         1111 :   for (int i =  0; i < len; ++i)
   32307          814 :     if (! TREE_VEC_ELT (targs, i))
   32308              :       {
   32309           56 :         tree tparm = TREE_VEC_ELT (tparms, i);
   32310           56 :         tparm = TREE_VALUE (tparm);
   32311              : 
   32312           56 :         if (!tried_array_deduction
   32313           44 :             && TREE_CODE (tparm) == TYPE_DECL)
   32314              :           {
   32315           34 :             try_array_deduction (tparms, targs, ttype);
   32316           34 :             tried_array_deduction = true;
   32317           34 :             if (TREE_VEC_ELT (targs, i))
   32318            0 :               goto again;
   32319              :           }
   32320              :         /* If the type parameter is a parameter pack, then it will be deduced
   32321              :            to an empty parameter pack.  This is another case that doesn't model
   32322              :            well as partial specialization.  */
   32323           56 :         if (template_parameter_pack_p (tparm))
   32324              :           {
   32325            6 :             tree arg;
   32326            6 :             if (TREE_CODE (tparm) == PARM_DECL)
   32327              :               {
   32328            6 :                 arg = make_node (NONTYPE_ARGUMENT_PACK);
   32329            6 :                 TREE_CONSTANT (arg) = 1;
   32330              :               }
   32331              :             else
   32332            0 :               arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   32333            6 :             ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   32334            6 :             TREE_VEC_ELT (targs, i) = arg;
   32335              :           }
   32336              :       }
   32337              : 
   32338              :   /* Maybe add in default template args.  This seems like a flaw in the
   32339              :      specification in terms of partial specialization, since it says the
   32340              :      partial specialization has the template parameter list of A, but a
   32341              :      partial specialization can't have default targs.  */
   32342          297 :   targs = coerce_template_parms (tparms, targs, tmpl, tf_none);
   32343          297 :   if (targs == error_mark_node)
   32344              :     return NULL_TREE;
   32345              : 
   32346              :   /* I believe we don't need the template_template_parm_bindings_ok_p call
   32347              :      because coerce_template_parms did coerce_template_template_parms.  */
   32348              : 
   32349          271 :   if (!constraints_satisfied_p (tmpl, targs))
   32350              :     return NULL_TREE;
   32351              : 
   32352              :   return targs;
   32353              : }
   32354              : 
   32355              : /* Return artificial deduction guides built from the constructors of class
   32356              :    template TMPL.  */
   32357              : 
   32358              : static tree
   32359         4898 : ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
   32360              : {
   32361         4898 :   tree outer_args = outer_template_args (tmpl);
   32362         4898 :   tree type = TREE_TYPE (most_general_template (tmpl));
   32363              : 
   32364         4898 :   tree cands = NULL_TREE;
   32365              : 
   32366       113424 :   for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
   32367              :     {
   32368              :       /* We handle C++23 inherited CTAD below.  */
   32369        56231 :       if (iter.using_p ())
   32370           32 :         continue;
   32371              : 
   32372        56199 :       tree guide = build_deduction_guide (type, *iter, outer_args, complain);
   32373        56199 :       cands = lookup_add (guide, cands);
   32374              :     }
   32375              : 
   32376         4898 :   if (cxx_dialect >= cxx23)
   32377              :     /* FIXME: CLASSTYPE_CONSTRUCTORS doesn't contain inherited constructors if
   32378              :        e.g. the class also has a user-defined constructor.  So instead iterate
   32379              :        over TYPE_FIELDS manually to robustly find all relevant using-decls.  */
   32380       156791 :     for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
   32381       152942 :       if (TREE_CODE (field) == USING_DECL
   32382       152942 :           && DECL_NAME (field) == ctor_identifier)
   32383              :         {
   32384           52 :           tree uguides = inherited_ctad_tweaks (tmpl, field, complain);
   32385           52 :           if (uguides)
   32386           42 :             cands = lookup_add (uguides, cands);
   32387              :         }
   32388              : 
   32389              :   /* Add implicit default constructor deduction guide.  */
   32390         4898 :   if (!TYPE_HAS_USER_CONSTRUCTOR (type))
   32391              :     {
   32392          640 :       tree guide = build_deduction_guide (type, type, outer_args,
   32393              :                                           complain);
   32394          640 :       cands = lookup_add (guide, cands);
   32395              :     }
   32396              : 
   32397              :   /* Add copy guide.  */
   32398         4898 :   {
   32399         4898 :     tree gtype = build_reference_type (type);
   32400         4898 :     tree guide = build_deduction_guide (type, gtype, outer_args,
   32401              :                                         complain);
   32402         4898 :     cands = lookup_add (guide, cands);
   32403              :   }
   32404              : 
   32405         4898 :   return cands;
   32406              : }
   32407              : 
   32408              : static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
   32409              : 
   32410              : /* Return the non-aggregate deduction guides for deducible template TMPL.  The
   32411              :    aggregate candidate is added separately because it depends on the
   32412              :    initializer.  Set ANY_DGUIDES_P if we find a non-implicit deduction
   32413              :    guide.  */
   32414              : 
   32415              : static tree
   32416        29210 : deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
   32417              : {
   32418        29210 :   tree guides = NULL_TREE;
   32419        29210 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   32420              :     {
   32421          218 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32422          218 :       tree tinfo = get_template_info (under);
   32423          218 :       guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
   32424              :                                      complain);
   32425              :     }
   32426              :   else
   32427              :     {
   32428        28992 :       guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
   32429              :                                       dguide_name (tmpl),
   32430              :                                       LOOK_want::ANY_REACHABLE,
   32431              :                                       /*complain=*/false);
   32432        28992 :       if (guides == error_mark_node)
   32433              :         guides = NULL_TREE;
   32434              :       else
   32435        22873 :         any_dguides_p = true;
   32436              :     }
   32437              : 
   32438              :   /* Cache the deduction guides for a template.  We also remember the result of
   32439              :      lookup, and rebuild everything if it changes; should be very rare.  */
   32440              :   /* FIXME: Also rebuild if this is a class template that inherits guides from a
   32441              :      base class, and lookup for the latter changed.  */
   32442        29210 :   tree_pair_p cache = NULL;
   32443        58420 :   if (tree_pair_p &r
   32444        29210 :       = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
   32445              :     {
   32446        24218 :       cache = r;
   32447        24218 :       if (cache->purpose == guides)
   32448        24192 :         return cache->value;
   32449              :     }
   32450              :   else
   32451              :     {
   32452         4992 :       r = cache = ggc_cleared_alloc<tree_pair_s> ();
   32453         4992 :       cache->purpose = guides;
   32454              :     }
   32455              : 
   32456         5018 :   tree cands = NULL_TREE;
   32457         5018 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   32458          120 :     cands = alias_ctad_tweaks (tmpl, guides);
   32459              :   else
   32460              :     {
   32461         4898 :       cands = ctor_deduction_guides_for (tmpl, complain);
   32462        16778 :       for (lkp_iterator it (guides); it; ++it)
   32463        11880 :         cands = lookup_add (*it, cands);
   32464              :     }
   32465              : 
   32466         5018 :   cache->value = cands;
   32467         5018 :   return cands;
   32468              : }
   32469              : 
   32470              : /* Return whether TMPL is a (class template argument-) deducible template.  */
   32471              : 
   32472              : bool
   32473    883848939 : ctad_template_p (tree tmpl)
   32474              : {
   32475              :   /* A deducible template is either a class template or is an alias template
   32476              :      whose defining-type-id is of the form
   32477              : 
   32478              :       typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   32479              : 
   32480              :      where the nested-name-specifier (if any) is non-dependent and the
   32481              :      template-name of the simple-template-id names a deducible template.  */
   32482              : 
   32483     13996377 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   32484    891954922 :       && IDENTIFIER_TRAIT_P (DECL_NAME (tmpl)))
   32485              :     /* Don't consider CTAD for templates with the same name as a trait; that
   32486              :        is ambiguous with e.g. __is_invocable(_Fn,_Args...).  */
   32487              :     return false;
   32488     13853547 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   32489    889646901 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   32490              :     return true;
   32491    875590931 :   if (!DECL_ALIAS_TEMPLATE_P (tmpl))
   32492              :     return false;
   32493       132690 :   tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32494       132690 :   if (tree tinfo = get_template_info (orig))
   32495        25199 :     return ctad_template_p (TI_TEMPLATE (tinfo));
   32496              :   return false;
   32497              : }
   32498              : 
   32499              : /* Deduce template arguments for the class template placeholder PTYPE for
   32500              :    template TMPL based on the initializer INIT, and return the resulting
   32501              :    type.  */
   32502              : 
   32503              : static tree
   32504       115852 : do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
   32505              :                     int flags, tsubst_flags_t complain)
   32506              : {
   32507              :   /* We should have handled this in the caller.  */
   32508       115852 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   32509              :     return ptype;
   32510              : 
   32511              :   /* If the class was erroneous, don't try to deduce, because that
   32512              :      can generate a lot of diagnostic.  */
   32513       115741 :   if (TREE_TYPE (tmpl)
   32514       115741 :       && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
   32515       231479 :       && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
   32516              :     return ptype;
   32517              : 
   32518              :   /* Wait until the enclosing scope is non-dependent.  */
   32519       231474 :   if (DECL_CLASS_SCOPE_P (tmpl)
   32520       117671 :       && dependent_type_p (DECL_CONTEXT (tmpl)))
   32521              :     return ptype;
   32522              : 
   32523              :   /* Initializing one placeholder from another.  */
   32524       115716 :   if (init
   32525       115582 :       && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
   32526        99898 :           || (TREE_CODE (init) == EXPR_PACK_EXPANSION
   32527            3 :               && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
   32528              :                   == TEMPLATE_PARM_INDEX)))
   32529        15687 :       && is_auto (TREE_TYPE (init))
   32530       131401 :       && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
   32531         7057 :     return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
   32532              : 
   32533       108659 :   if (!ctad_template_p (tmpl))
   32534              :     {
   32535            3 :       if (complain & tf_error)
   32536            3 :         error ("non-deducible template %qT used without template arguments", tmpl);
   32537            3 :       return error_mark_node;
   32538              :     }
   32539       108656 :   else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
   32540              :     {
   32541            7 :       if (complain & tf_error)
   32542              :         {
   32543              :           /* Be permissive with equivalent alias templates.  */
   32544            7 :           tree u = get_underlying_template (tmpl);
   32545            7 :           auto_diagnostic_group d;
   32546            1 :           const enum diagnostics::kind dk = ((u == tmpl)
   32547            7 :                                              ? diagnostics::kind::error
   32548              :                                              : diagnostics::kind::pedwarn);
   32549            7 :           bool complained
   32550            7 :             = emit_diagnostic (dk, input_location, 0,
   32551              :                                "alias template deduction only available "
   32552              :                                "with %<-std=c++20%> or %<-std=gnu++20%>");
   32553            7 :           if (u == tmpl)
   32554            6 :             return error_mark_node;
   32555            1 :           else if (complained)
   32556              :             {
   32557            1 :               inform (input_location, "use %qD directly instead", u);
   32558            1 :               tmpl = u;
   32559              :             }
   32560            7 :         }
   32561              :       else
   32562            0 :         return error_mark_node;
   32563              :     }
   32564              : 
   32565              :   /* Wait until the initializer is non-dependent.  */
   32566       108650 :   if (type_dependent_expression_p (init))
   32567              :     return ptype;
   32568              : 
   32569        28958 :   if (outer_targs)
   32570              :     {
   32571        10155 :       int args_depth = TMPL_ARGS_DEPTH (outer_targs);
   32572        10155 :       int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   32573        10155 :       if (parms_depth > 1)
   32574              :         {
   32575              :           /* Substitute outer arguments into this CTAD template from the
   32576              :              current instantiation.  */
   32577            3 :           int want = std::min (args_depth, parms_depth - 1);
   32578            3 :           outer_targs = strip_innermost_template_args (outer_targs,
   32579              :                                                        args_depth - want);
   32580            3 :           tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
   32581            3 :           if (tmpl == error_mark_node)
   32582              :             return error_mark_node;
   32583              :         }
   32584              :     }
   32585              : 
   32586              :   /* Don't bother with the alias rules for an equivalent template.  */
   32587        28958 :   tmpl = get_underlying_template (tmpl);
   32588              : 
   32589        28958 :   tree type = TREE_TYPE (tmpl);
   32590              : 
   32591        28958 :   bool try_list_cand = false;
   32592        28958 :   bool list_init_p = false;
   32593              : 
   32594        28958 :   releasing_vec rv_args = NULL;
   32595        28958 :   vec<tree,va_gc> *&args = *&rv_args;
   32596        28958 :   if (init == NULL_TREE)
   32597          133 :     args = make_tree_vector ();
   32598        28825 :   else if (BRACE_ENCLOSED_INITIALIZER_P (init))
   32599              :     {
   32600         6748 :       list_init_p = true;
   32601         6748 :       try_list_cand = true;
   32602         6748 :       if (CONSTRUCTOR_NELTS (init) == 1
   32603         5862 :           && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
   32604              :         {
   32605              :           /* As an exception, the first phase in 16.3.1.7 (considering the
   32606              :              initializer list as a single argument) is omitted if the
   32607              :              initializer list consists of a single expression of type cv U,
   32608              :              where U is a specialization of C or a class derived from a
   32609              :              specialization of C.  */
   32610         2470 :           tree elt = CONSTRUCTOR_ELT (init, 0)->value;
   32611         2470 :           if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
   32612           36 :             try_list_cand = false;
   32613              :         }
   32614           36 :       if (try_list_cand || is_std_init_list (type))
   32615         6712 :         args = make_tree_vector_single (init);
   32616              :       else
   32617           36 :         args = make_tree_vector_from_ctor (init);
   32618              :     }
   32619        22077 :   else if (TREE_CODE (init) == TREE_LIST)
   32620         9744 :     args = make_tree_vector_from_list (init);
   32621              :   else
   32622        12333 :     args = make_tree_vector_single (init);
   32623              : 
   32624              :   /* Do this now to avoid problems with erroneous args later on.  */
   32625        28958 :   args = resolve_args (args, complain);
   32626        28958 :   if (args == NULL)
   32627           12 :     return error_mark_node;
   32628              : 
   32629        28946 :   bool any_dguides_p = false;
   32630        28946 :   tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
   32631        28946 :   if (cands == error_mark_node)
   32632              :     return error_mark_node;
   32633              : 
   32634              :   /* Prune explicit deduction guides in copy-initialization context (but
   32635              :      not copy-list-initialization).  */
   32636        28940 :   bool elided = false;
   32637        28940 :   if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
   32638              :     {
   32639        69359 :       for (lkp_iterator iter (cands); !elided && iter; ++iter)
   32640        58092 :         if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   32641         1920 :           elided = true;
   32642              : 
   32643        11267 :       if (elided)
   32644              :         {
   32645              :           /* Found a nonconverting guide, prune the candidates.  */
   32646         1920 :           tree pruned = NULL_TREE;
   32647        32054 :           for (lkp_iterator iter (cands); iter; ++iter)
   32648        30134 :             if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   32649        28134 :               pruned = lookup_add (*iter, pruned);
   32650              : 
   32651         1920 :           cands = pruned;
   32652              :         }
   32653              :     }
   32654              : 
   32655        28940 :   if (!any_dguides_p)
   32656         6075 :     if (tree guide = maybe_aggr_guide (tmpl, init, args))
   32657         1501 :       cands = lookup_add (guide, cands);
   32658              : 
   32659        28940 :   tree fndecl = error_mark_node;
   32660              : 
   32661              :   /* If this is list-initialization and the class has a list guide, first
   32662              :      try deducing from the list as a single argument, as [over.match.list].  */
   32663        28940 :   if (try_list_cand)
   32664              :     {
   32665         6712 :       tree list_cands = NULL_TREE;
   32666       111630 :       for (tree dg : lkp_range (cands))
   32667        98206 :         if (is_list_ctor (dg))
   32668         1280 :           list_cands = lookup_add (dg, list_cands);
   32669         6712 :       if (list_cands)
   32670          923 :         fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
   32671         6712 :       if (fndecl == error_mark_node)
   32672              :         {
   32673              :           /* That didn't work, now try treating the list as a sequence of
   32674              :              arguments.  */
   32675         6125 :           release_tree_vector (args);
   32676         6125 :           args = make_tree_vector_from_ctor (init);
   32677         6125 :           args = resolve_args (args, complain);
   32678         6125 :           if (args == NULL)
   32679            0 :             return error_mark_node;
   32680              :         }
   32681              :     }
   32682              : 
   32683        28940 :   if (elided && !cands)
   32684              :     {
   32685            0 :       error ("cannot deduce template arguments for copy-initialization"
   32686              :              " of %qT, as it has no non-explicit deduction guides or "
   32687              :              "user-declared constructors", type);
   32688            0 :       return error_mark_node;
   32689              :     }
   32690        28940 :   else if (!cands && fndecl == error_mark_node)
   32691              :     {
   32692            0 :       error ("cannot deduce template arguments of %qT, as it has no viable "
   32693              :              "deduction guides", type);
   32694            0 :       return error_mark_node;
   32695              :     }
   32696              : 
   32697        28940 :   if (fndecl == error_mark_node)
   32698        28353 :     fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
   32699              : 
   32700        28940 :   if (fndecl == error_mark_node)
   32701              :     {
   32702          368 :       if (complain & tf_warning_or_error)
   32703              :         {
   32704          193 :           auto_diagnostic_group d;
   32705          193 :           error ("class template argument deduction failed:");
   32706          193 :           perform_dguide_overload_resolution (cands, args, complain);
   32707          193 :           if (elided)
   32708           27 :             inform (input_location, "explicit deduction guides not considered "
   32709              :                     "for copy-initialization");
   32710          193 :         }
   32711          368 :       return error_mark_node;
   32712              :     }
   32713              :   /* [over.match.list]/1: In copy-list-initialization, if an explicit
   32714              :      constructor is chosen, the initialization is ill-formed.  */
   32715        28572 :   else if (flags & LOOKUP_ONLYCONVERTING)
   32716              :     {
   32717        11388 :       if (DECL_NONCONVERTING_P (fndecl))
   32718              :         {
   32719           35 :           if (complain & tf_warning_or_error)
   32720              :             {
   32721              :               // TODO: Pass down location from cp_finish_decl.
   32722           35 :               auto_diagnostic_group d;
   32723           35 :               error ("class template argument deduction for %qT failed: "
   32724              :                      "explicit deduction guide selected in "
   32725              :                      "copy-list-initialization", type);
   32726           35 :               inform (DECL_SOURCE_LOCATION (fndecl),
   32727              :                       "explicit deduction guide declared here");
   32728              : 
   32729           35 :             }
   32730           35 :           return error_mark_node;
   32731              :         }
   32732              :     }
   32733              : 
   32734              :   /* If CTAD succeeded but the type doesn't have any explicit deduction
   32735              :      guides, this deduction might not be what the user intended.  */
   32736        28537 :   if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
   32737              :     {
   32738         5529 :       auto_diagnostic_group d;
   32739         5529 :       if ((!DECL_IN_SYSTEM_HEADER (fndecl)
   32740            6 :            || global_dc->m_warn_system_headers)
   32741         5535 :           && warning (OPT_Wctad_maybe_unsupported,
   32742              :                       "%qT may not intend to support class template argument "
   32743              :                       "deduction", type))
   32744           24 :         inform (input_location, "add a deduction guide to suppress this "
   32745              :                 "warning");
   32746         5529 :     }
   32747              : 
   32748        28537 :   return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
   32749        28537 :                                   cp_type_quals (ptype));
   32750        28958 : }
   32751              : 
   32752              : /* Return true if INIT is an unparenthesized id-expression or an
   32753              :    unparenthesized class member access.  Used for the argument of
   32754              :    decltype(auto).  */
   32755              : 
   32756              : bool
   32757      8915803 : unparenthesized_id_or_class_member_access_p (tree init)
   32758              : {
   32759      8915803 :   STRIP_ANY_LOCATION_WRAPPER (init);
   32760              : 
   32761              :   /* We need to be able to tell '(r)' and 'r' apart (when it's of
   32762              :      reference type).  Only the latter is an id-expression.  */
   32763      1062585 :   if (REFERENCE_REF_P (init)
   32764      9492934 :       && !REF_PARENTHESIZED_P (init))
   32765         3304 :     init = TREE_OPERAND (init, 0);
   32766      8915803 :   return (DECL_P (init)
   32767      8915803 :           || ((TREE_CODE (init) == COMPONENT_REF
   32768      8915335 :                || TREE_CODE (init) == SCOPE_REF)
   32769          235 :               && !REF_PARENTHESIZED_P (init)));
   32770              : }
   32771              : 
   32772              : /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
   32773              :    from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
   32774              :    The CONTEXT determines the context in which auto deduction is performed
   32775              :    and is used to control error diagnostics.  FLAGS are the LOOKUP_* flags.
   32776              : 
   32777              :    OUTER_TARGS is used during template argument deduction (context == adc_unify)
   32778              :    to properly substitute the result.  It's also used in the adc_unify and
   32779              :    adc_requirement contexts to communicate the necessary template arguments
   32780              :    to satisfaction.  OUTER_TARGS is ignored in other contexts.
   32781              : 
   32782              :    Additionally for adc_unify contexts TMPL is the template for which TYPE
   32783              :    is a template parameter type.
   32784              : 
   32785              :    For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
   32786              :    scope may be appended to the list of deduced template arguments prior to
   32787              :    determining constraint satisfaction as appropriate.  */
   32788              : 
   32789              : tree
   32790     24052593 : do_auto_deduction (tree type, tree init, tree auto_node,
   32791              :                    tsubst_flags_t complain /* = tf_warning_or_error */,
   32792              :                    auto_deduction_context context /* = adc_unspecified */,
   32793              :                    tree outer_targs /* = NULL_TREE */,
   32794              :                    int flags /* = LOOKUP_NORMAL */,
   32795              :                    tree tmpl /* = NULL_TREE */)
   32796              : {
   32797     24052593 :   if (type == error_mark_node || init == error_mark_node)
   32798              :     return error_mark_node;
   32799              : 
   32800     24051539 :   if (init && type_dependent_expression_p (init)
   32801     34165602 :       && context != adc_unify)
   32802              :     /* Defining a subset of type-dependent expressions that we can deduce
   32803              :        from ahead of time isn't worth the trouble.  */
   32804              :     return type;
   32805              : 
   32806              :   /* Similarly, we can't deduce from another undeduced decl.  */
   32807     14037133 :   if (init && undeduced_auto_decl (init))
   32808              :     return type;
   32809              : 
   32810              :   /* We may be doing a partial substitution, but we still want to replace
   32811              :      auto_node.  */
   32812     14037122 :   complain &= ~tf_partial;
   32813              : 
   32814     14037122 :   if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
   32815              :     {
   32816              :       /* We don't recurse here because we can't deduce from a nested
   32817              :          initializer_list.  */
   32818         7433 :       if (CONSTRUCTOR_ELTS (init))
   32819        19890 :         for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
   32820              :           {
   32821        13383 :             elt.value = resolve_nondeduced_context (elt.value, complain);
   32822        13383 :             if (!mark_single_function (elt.value, complain))
   32823           14 :               return error_mark_node;
   32824              :           }
   32825              :     }
   32826     14029689 :   else if (init)
   32827              :     {
   32828     14029521 :       init = resolve_nondeduced_context (init, complain);
   32829     14029521 :       if (!mark_single_function (init, complain))
   32830            0 :         return error_mark_node;
   32831              :     }
   32832              : 
   32833              :   /* In C++23, we must deduce the type to int&& for code like
   32834              :        decltype(auto) f(int&& x) { return (x); }
   32835              :      or
   32836              :        auto&& f(int x) { return x; }
   32837              :      so we use treat_lvalue_as_rvalue_p.  But don't do it for
   32838              :        decltype(auto) f(int x) { return x; }
   32839              :      where we should deduce 'int' rather than 'int&&'; transmogrifying
   32840              :      INIT to an rvalue would break that.  */
   32841     14037108 :   tree r;
   32842     14037108 :   if (cxx_dialect >= cxx23
   32843      2994796 :       && context == adc_return_type
   32844       441985 :       && (!AUTO_IS_DECLTYPE (auto_node)
   32845       102103 :           || !unparenthesized_id_or_class_member_access_p (init))
   32846     14479054 :       && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
   32847              :                                         /*return*/true)))
   32848        22217 :     init = r;
   32849              : 
   32850     14037108 :   if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   32851              :     /* C++17 class template argument deduction.  */
   32852       115852 :     return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
   32853              : 
   32854     13921256 :   if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
   32855              :     /* Nothing we can do with this, even in deduction context.  */
   32856              :     return type;
   32857              : 
   32858     13912130 :   location_t loc = cp_expr_loc_or_input_loc (init);
   32859              : 
   32860              :   /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
   32861              :      with either a new invented type template parameter U or, if the
   32862              :      initializer is a braced-init-list (8.5.4), with
   32863              :      std::initializer_list<U>.  */
   32864     13912130 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   32865              :     {
   32866          581 :       if (!DIRECT_LIST_INIT_P (init))
   32867          390 :         type = listify_autos (type, auto_node);
   32868          191 :       else if (CONSTRUCTOR_NELTS (init) == 1)
   32869          185 :         init = CONSTRUCTOR_ELT (init, 0)->value;
   32870              :       else
   32871              :         {
   32872            6 :           if (complain & tf_warning_or_error)
   32873              :             {
   32874            6 :               auto_diagnostic_group d;
   32875            6 :               if (permerror (loc, "direct-list-initialization of "
   32876              :                              "%<auto%> requires exactly one element"))
   32877            6 :                 inform (loc,
   32878              :                         "for deduction to %<std::initializer_list%>, use copy-"
   32879              :                         "list-initialization (i.e. add %<=%> before the %<{%>)");
   32880            6 :             }
   32881            6 :           type = listify_autos (type, auto_node);
   32882              :         }
   32883              :     }
   32884              : 
   32885     13912130 :   if (type == error_mark_node || init == error_mark_node)
   32886              :     return error_mark_node;
   32887              : 
   32888     13912109 :   tree targs;
   32889     13912109 :   if (context == adc_decomp_type
   32890     13912109 :       && auto_node == type
   32891     13912109 :       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
   32892              :     {
   32893              :       /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
   32894              :          and initializer has array type, deduce cv-qualified array type.  */
   32895         3302 :       targs = make_tree_vec (1);
   32896         3302 :       TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
   32897              :     }
   32898     13908807 :   else if (AUTO_IS_DECLTYPE (auto_node))
   32899              :     {
   32900      8813700 :       const bool id = unparenthesized_id_or_class_member_access_p (init);
   32901      8813700 :       tree deduced = finish_decltype_type (init, id, complain);
   32902      8813700 :       deduced = canonicalize_type_argument (deduced, complain);
   32903      8813700 :       if (deduced == error_mark_node)
   32904              :         return error_mark_node;
   32905      8813697 :       targs = make_tree_vec (1);
   32906      8813697 :       TREE_VEC_ELT (targs, 0) = deduced;
   32907              :     }
   32908              :   else
   32909              :     {
   32910      5095107 :       if (error_operand_p (init))
   32911              :         return error_mark_node;
   32912              : 
   32913      5095095 :       tree parms = build_tree_list (NULL_TREE, type);
   32914      5095095 :       tree tparms = make_tree_vec (1);
   32915      5095095 :       TREE_VEC_ELT (tparms, 0)
   32916      5095095 :         = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
   32917              : 
   32918      5095095 :       targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   32919      5095095 :       int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
   32920              :                                        DEDUCE_CALL,
   32921              :                                        NULL, /*explain_p=*/false);
   32922      5095095 :       if (val > 0)
   32923              :         {
   32924           73 :           if (processing_template_decl)
   32925              :             /* Try again at instantiation time.  */
   32926              :             return type;
   32927           64 :           if (type && type != error_mark_node
   32928           64 :               && (complain & tf_error))
   32929              :             /* If type is error_mark_node a diagnostic must have been
   32930              :                emitted by now.  Also, having a mention to '<type error>'
   32931              :                in the diagnostic is not really useful to the user.  */
   32932              :             {
   32933           61 :               if (cfun
   32934           31 :                   && FNDECL_USED_AUTO (current_function_decl)
   32935           12 :                   && (auto_node
   32936           12 :                       == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
   32937           73 :                   && LAMBDA_FUNCTION_P (current_function_decl))
   32938            6 :                 error_at (loc, "unable to deduce lambda return type from %qE",
   32939              :                           init);
   32940              :               else
   32941           55 :                 error_at (loc, "unable to deduce %qT from %qE", type, init);
   32942           61 :               type_unification_real (tparms, targs, parms, &init, 1, 0,
   32943              :                                      DEDUCE_CALL,
   32944              :                                      NULL, /*explain_p=*/true);
   32945              :             }
   32946           64 :           return error_mark_node;
   32947              :         }
   32948              :     }
   32949              : 
   32950              :   /* Check any placeholder constraints against the deduced type. */
   32951     13912021 :   if (processing_template_decl && context == adc_unify)
   32952              :     /* Constraints will be checked after deduction.  */;
   32953     13907461 :   else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
   32954              :     {
   32955      8705188 :       if (context == adc_unify)
   32956              :         /* Simple constrained auto NTTPs should have gotten their constraint
   32957              :            moved to the template's associated constraints.  */
   32958            0 :         gcc_checking_assert (type != auto_node);
   32959              : 
   32960      8705188 :       if (processing_template_decl)
   32961              :         {
   32962           59 :           gcc_checking_assert (context == adc_variable_type
   32963              :                                || context == adc_return_type
   32964              :                                || context == adc_decomp_type);
   32965           59 :           gcc_checking_assert (!type_dependent_expression_p (init));
   32966              :           /* If the constraint is dependent, we need to wait until
   32967              :              instantiation time to resolve the placeholder.  */
   32968           59 :           if (placeholder_type_constraint_dependent_p (constr))
   32969              :             return type;
   32970              :         }
   32971              : 
   32972      8705164 :       if (context == adc_return_type
   32973      8705164 :           || context == adc_variable_type
   32974      8705164 :           || context == adc_decomp_type)
   32975          328 :         if (tree fn = current_function_decl)
   32976          236 :           if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
   32977              :             {
   32978          110 :               outer_targs = DECL_TEMPLATE_INFO (fn)
   32979          110 :                 ? DECL_TI_ARGS (fn) : NULL_TREE;
   32980          122 :               if (LAMBDA_FUNCTION_P (fn))
   32981              :                 {
   32982              :                   /* As in satisfy_declaration_constraints.  */
   32983           12 :                   tree regen_args = lambda_regenerating_args (fn);
   32984           12 :                   if (outer_targs)
   32985            0 :                     outer_targs = add_to_template_args (regen_args, outer_targs);
   32986              :                   else
   32987              :                     outer_targs = regen_args;
   32988              :                 }
   32989              :             }
   32990              : 
   32991      8705164 :       tree full_targs = outer_targs;
   32992      8705164 :       if (context == adc_unify && tmpl)
   32993            0 :         full_targs = add_outermost_template_args (tmpl, full_targs);
   32994      8705164 :       full_targs = add_to_template_args (full_targs, targs);
   32995              : 
   32996              :       /* HACK: Compensate for callers not always communicating all levels of
   32997              :          outer template arguments by filling in the outermost missing levels
   32998              :          with dummy levels before checking satisfaction.  We'll still crash
   32999              :          if the constraint depends on a template argument belonging to one of
   33000              :          these missing levels, but this hack otherwise allows us to handle a
   33001              :          large subset of possible constraints (including all non-dependent
   33002              :          constraints).  */
   33003      8705164 :       if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
   33004     17410541 :                                 - TMPL_ARGS_DEPTH (full_targs)))
   33005              :         {
   33006            0 :           tree dummy_levels = make_tree_vec (missing_levels);
   33007            0 :           for (int i = 0; i < missing_levels; ++i)
   33008            0 :             TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
   33009            0 :           full_targs = add_to_template_args (dummy_levels, full_targs);
   33010              :         }
   33011              : 
   33012      8705164 :       if (!constraints_satisfied_p (auto_node, full_targs))
   33013              :         {
   33014        21730 :           if (complain & tf_warning_or_error)
   33015              :             {
   33016          113 :               auto_diagnostic_group d;
   33017          113 :               switch (context)
   33018              :                 {
   33019            0 :                 case adc_unspecified:
   33020            0 :                 case adc_unify:
   33021            0 :                   error_at (loc, "placeholder constraints not satisfied");
   33022            0 :                   break;
   33023           73 :                 case adc_variable_type:
   33024           73 :                 case adc_decomp_type:
   33025           73 :                   error_at (loc, "deduced initializer does not satisfy "
   33026              :                             "placeholder constraints");
   33027           73 :                   break;
   33028           28 :                 case adc_return_type:
   33029           28 :                   error_at (loc, "deduced return type does not satisfy "
   33030              :                             "placeholder constraints");
   33031           28 :                   break;
   33032           12 :                 case adc_requirement:
   33033           12 :                   error_at (loc, "deduced expression type does not satisfy "
   33034              :                             "placeholder constraints");
   33035           12 :                   break;
   33036              :                 }
   33037          113 :               diagnose_constraints (loc, auto_node, full_targs);
   33038          113 :             }
   33039        21730 :           return error_mark_node;
   33040              :         }
   33041              :     }
   33042              : 
   33043     13890267 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 0)
   33044              :     {
   33045              :       /* Substitute this level-less auto via tsubst by temporarily
   33046              :          overriding its level to 1.  */
   33047        96449 :       TEMPLATE_TYPE_LEVEL (auto_node) = 1;
   33048        96449 :       type = tsubst (type, targs, complain, NULL_TREE);
   33049        96449 :       TEMPLATE_TYPE_LEVEL (auto_node) = 0;
   33050        96449 :       return type;
   33051              :     }
   33052              : 
   33053     13793818 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
   33054              :     /* The outer template arguments are already substituted into type
   33055              :        (but we still may have used them for constraint checking above).  */;
   33056       947096 :   else if (context == adc_unify)
   33057        14695 :     targs = add_to_template_args (outer_targs, targs);
   33058       932401 :   else if (processing_template_decl)
   33059       932398 :     targs = add_to_template_args (current_template_args (), targs);
   33060     13793818 :   return tsubst (type, targs, complain, NULL_TREE);
   33061              : }
   33062              : 
   33063              : /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
   33064              :    result.  */
   33065              : 
   33066              : tree
   33067    187114376 : splice_late_return_type (tree type, tree late_return_type)
   33068              : {
   33069    187114376 :   if (late_return_type)
   33070              :     {
   33071      3493280 :       gcc_assert (is_auto (type) || seen_error ());
   33072      3493280 :       return late_return_type;
   33073              :     }
   33074              : 
   33075    183621096 :   if (tree auto_node = find_type_usage (type, is_auto))
   33076      4711693 :     if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
   33077              :       {
   33078              :         /* In an abbreviated function template we didn't know we were dealing
   33079              :            with a function template when we saw the auto return type, so rebuild
   33080              :            the return type using an auto with the correct level.  */
   33081          101 :         tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
   33082          101 :         tree auto_vec = make_tree_vec (1);
   33083          101 :         TREE_VEC_ELT (auto_vec, 0) = new_auto;
   33084          101 :         tree targs = add_outermost_template_args (current_template_args (),
   33085              :                                                   auto_vec);
   33086              :         /* Also rebuild the constraint info in terms of the new auto.  */
   33087          101 :         if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
   33088            8 :           PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
   33089            8 :             = build_tree_list (current_template_parms,
   33090            4 :                                tsubst_constraint (TREE_VALUE (ci), targs,
   33091              :                                                   tf_none, NULL_TREE));
   33092          101 :         TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
   33093          101 :         return tsubst (type, targs, tf_none, NULL_TREE);
   33094              :       }
   33095              :   return type;
   33096              : }
   33097              : 
   33098              : /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
   33099              :    'decltype(auto)' or a deduced class template.  */
   33100              : 
   33101              : bool
   33102   9648401563 : is_auto (const_tree type)
   33103              : {
   33104   9648401563 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   33105   9648401563 :       && (TYPE_IDENTIFIER (type) == auto_identifier
   33106   4188148786 :           || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
   33107              :     return true;
   33108              :   else
   33109   9171858129 :     return false;
   33110              : }
   33111              : 
   33112              : /* for_each_template_parm callback for type_uses_auto.  */
   33113              : 
   33114              : int
   33115            0 : is_auto_r (tree tp, void */*data*/)
   33116              : {
   33117            0 :   return is_auto (tp);
   33118              : }
   33119              : 
   33120              : /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
   33121              :    a use of `auto'.  Returns NULL_TREE otherwise.  */
   33122              : 
   33123              : tree
   33124   3593988919 : type_uses_auto (tree type)
   33125              : {
   33126   3593988919 :   if (type == NULL_TREE)
   33127              :     return NULL_TREE;
   33128              : 
   33129              :   /* For parameter packs, check the contents of the pack.  */
   33130   3588926816 :   if (PACK_EXPANSION_P (type))
   33131      6089174 :     type = PACK_EXPANSION_PATTERN (type);
   33132              : 
   33133   3588926816 :   return find_type_usage (type, is_auto);
   33134              : }
   33135              : 
   33136              : /* Recursively walk over && expressions searching for EXPR. Return a reference
   33137              :    to that expression.  */
   33138              : 
   33139           62 : static tree *find_template_requirement (tree *t, tree key)
   33140              : {
   33141           62 :   if (*t == key)
   33142              :     return t;
   33143           24 :   if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
   33144              :     {
   33145           12 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
   33146              :         return p;
   33147           12 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
   33148              :         return p;
   33149              :     }
   33150              :   return 0;
   33151              : }
   33152              : 
   33153              : /* Convert the generic type parameters in PARM that match the types given in the
   33154              :    range [START_IDX, END_IDX) from the current_template_parms into generic type
   33155              :    packs.  */
   33156              : 
   33157              : tree
   33158        62011 : convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
   33159              : {
   33160        62011 :   tree current = current_template_parms;
   33161        62011 :   int depth = TMPL_PARMS_DEPTH (current);
   33162        62011 :   current = INNERMOST_TEMPLATE_PARMS (current);
   33163        62011 :   tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
   33164              : 
   33165        63758 :   for (int i = 0; i < start_idx; ++i)
   33166         3494 :     TREE_VEC_ELT (replacement, i)
   33167         1747 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   33168              : 
   33169       124034 :   for (int i = start_idx; i < end_idx; ++i)
   33170              :     {
   33171              :       /* Create a distinct parameter pack type from the current parm and add it
   33172              :          to the replacement args to tsubst below into the generic function
   33173              :          parameter.  */
   33174        62023 :       tree node = TREE_VEC_ELT (current, i);
   33175        62023 :       tree o = TREE_TYPE (TREE_VALUE (node));
   33176        62023 :       tree t = copy_type (o);
   33177        62023 :       TEMPLATE_TYPE_PARM_INDEX (t)
   33178        62023 :         = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
   33179              :                                       t, 0, 0, tf_none);
   33180        62023 :       TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
   33181        62023 :       TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
   33182        62023 :       TYPE_MAIN_VARIANT (t) = t;
   33183        62023 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
   33184        62023 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
   33185        62023 :       TREE_VEC_ELT (replacement, i) = t;
   33186              : 
   33187              :       /* Replace the current template parameter with new pack.  */
   33188        62023 :       TREE_VALUE (node) = TREE_CHAIN (t);
   33189              : 
   33190              :       /* Surgically adjust the associated constraint of adjusted parameter
   33191              :          and it's corresponding contribution to the current template
   33192              :          requirements.  */
   33193        62023 :       if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
   33194              :         {
   33195           38 :           gcc_assert (concept_check_p (constr));
   33196           38 :           TREE_VEC_ELT (TREE_OPERAND (constr, 1), 0) = t;
   33197              :           /* Use UNKNOWN_LOCATION so write_template_args can tell the
   33198              :              difference between this and a fold the user wrote.  */
   33199           38 :           location_t loc = UNKNOWN_LOCATION;
   33200           38 :           tree fold = finish_left_unary_fold_expr (loc, constr,
   33201              :                                                    TRUTH_ANDIF_EXPR);
   33202           38 :           TEMPLATE_PARM_CONSTRAINTS (node) = fold;
   33203              : 
   33204              :           /* If there was a constraint, we also need to replace that in
   33205              :              the template requirements, which we've already built.  */
   33206           38 :           tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
   33207           38 :           reqs = find_template_requirement (reqs, constr);
   33208           38 :           *reqs = fold;
   33209              :         }
   33210              :     }
   33211              : 
   33212        62011 :   for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
   33213            0 :     TREE_VEC_ELT (replacement, i)
   33214            0 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   33215              : 
   33216              :   /* If there are more levels then build up the replacement with the outer
   33217              :      template parms.  */
   33218        62011 :   if (depth > 1)
   33219        61594 :     replacement = add_to_template_args (template_parms_to_args
   33220        61594 :                                         (TREE_CHAIN (current_template_parms)),
   33221              :                                         replacement);
   33222              : 
   33223        62011 :   return tsubst (parm, replacement, tf_none, NULL_TREE);
   33224              : }
   33225              : 
   33226              : /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
   33227              :    0..N-1.  */
   33228              : 
   33229              : void
   33230        84199 : declare_integer_pack (void)
   33231              : {
   33232        84199 :   tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
   33233              :                                build_function_type_list (integer_type_node,
   33234              :                                                          integer_type_node,
   33235              :                                                          NULL_TREE),
   33236              :                                NULL_TREE, ECF_CONST);
   33237        84199 :   DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
   33238        84199 :   set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
   33239              :                               CP_BUILT_IN_INTEGER_PACK);
   33240        84199 : }
   33241              : 
   33242              : /* Walk the decl or type specialization table calling FN on each
   33243              :    entry.  */
   33244              : 
   33245              : void
   33246         5556 : walk_specializations (bool decls_p,
   33247              :                       void (*fn) (bool decls_p, spec_entry *entry, void *data),
   33248              :                       void *data)
   33249              : {
   33250         5556 :   spec_hash_table *table = decls_p ? decl_specializations
   33251              :     : type_specializations;
   33252         5556 :   spec_hash_table::iterator end (table->end ());
   33253      2426098 :   for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
   33254      1210271 :     fn (decls_p, *iter, data);
   33255         5556 : }
   33256              : 
   33257              : /* Lookup the specialization of *ELT, in the decl or type
   33258              :    specialization table.  Return the SPEC that's already there, or
   33259              :    NULL if nothing.  */
   33260              : 
   33261              : tree
   33262      2143524 : match_mergeable_specialization (bool decl_p, spec_entry *elt)
   33263              : {
   33264      2143524 :   hash_table<spec_hasher> *specializations
   33265              :     = decl_p ? decl_specializations : type_specializations;
   33266      2143524 :   auto *slot = specializations->find_slot (elt, NO_INSERT);
   33267              : 
   33268      2143524 :   if (slot)
   33269      1623247 :     return (*slot)->spec;
   33270              : 
   33271              :   return NULL_TREE;
   33272              : }
   33273              : 
   33274              : /* Return flags encoding whether SPEC is on the instantiation and/or
   33275              :    specialization lists of TMPL.  */
   33276              : 
   33277              : unsigned
   33278       491086 : get_mergeable_specialization_flags (bool decl_p, tree tmpl, tree decl)
   33279              : {
   33280       491086 :   unsigned flags = 0;
   33281              : 
   33282       491086 :   tree spec = decl_p ? decl : TREE_TYPE (decl);
   33283       491086 :   for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
   33284      9233245 :        inst; inst = TREE_CHAIN (inst))
   33285      9229219 :     if (TREE_VALUE (inst) == spec)
   33286              :       {
   33287              :         flags |= 1;
   33288              :         break;
   33289              :       }
   33290              : 
   33291       982172 :   if (CLASS_TYPE_P (TREE_TYPE (decl))
   33292       206132 :       && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
   33293       692017 :       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   33294              :     /* Only need to search if DECL is a partial specialization.  */
   33295        14249 :     for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   33296        39610 :          part; part = TREE_CHAIN (part))
   33297        25361 :       if (TREE_VALUE (part) == decl)
   33298              :         {
   33299            0 :           flags |= 2;
   33300            0 :           break;
   33301              :         }
   33302              : 
   33303       491086 :   return flags;
   33304              : }
   33305              : 
   33306              : /* Add a new specialization described by SPEC.  DECL is the
   33307              :    maybe-template decl and FLAGS is as returned from
   33308              :    get_mergeable_specialization_flags.  */
   33309              : 
   33310              : void
   33311       225069 : add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
   33312              :                               unsigned flags)
   33313              : {
   33314       225069 :   if (decl_p)
   33315              :     {
   33316       161062 :       auto *slot = decl_specializations->find_slot (elt, INSERT);
   33317              : 
   33318       161062 :       gcc_checking_assert (!*slot);
   33319       161062 :       auto entry = ggc_alloc<spec_entry> ();
   33320       161062 :       *entry = *elt;
   33321       161062 :       *slot = entry;
   33322              :     }
   33323              :   else
   33324              :     {
   33325        64007 :       auto *slot = type_specializations->find_slot (elt, INSERT);
   33326              : 
   33327              :       /* We don't distinguish different constrained partial type
   33328              :          specializations, so there could be duplicates.  In that case we
   33329              :          must propagate TYPE_CANONICAL so that they are treated as the
   33330              :          same type.  Everything else must be new.   */
   33331        64007 :       if (*slot)
   33332              :         {
   33333          188 :           gcc_checking_assert (flags & 2);
   33334          188 :           TYPE_CANONICAL (elt->spec) = TYPE_CANONICAL ((*slot)->spec);
   33335              :         }
   33336              :       else
   33337              :         {
   33338        63819 :           auto entry = ggc_alloc<spec_entry> ();
   33339        63819 :           *entry = *elt;
   33340        63819 :           *slot = entry;
   33341              :         }
   33342              :     }
   33343              : 
   33344       225069 :   if (flags & 1)
   33345       218560 :     DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
   33346       437120 :       = tree_cons (elt->args, elt->spec,
   33347       218560 :                    DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
   33348              : 
   33349       225069 :   if (flags & 2)
   33350              :     {
   33351              :       /* A partial specialization.  */
   33352         4595 :       tree cons = tree_cons (elt->args, decl,
   33353         4595 :                              DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
   33354         4595 :       TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
   33355         4595 :       DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
   33356         4595 :       set_defining_module_for_partial_spec (STRIP_TEMPLATE (decl));
   33357              :     }
   33358       225069 : }
   33359              : 
   33360              : struct expansion_stmt_bc
   33361              : {
   33362              :   tree break_label;
   33363              :   tree continue_label;
   33364              :   hash_set<tree> *pset;
   33365              :   location_t loc;
   33366              :   bool in_switch;
   33367              : };
   33368              : 
   33369              : /* Helper function for finish_expansion_stmt.  Find BREAK_STMT (not
   33370              :    nested inside of other WHILE_STMT, FOR_STMT, DO_STMT, TEMPLATE_FOR_STMT
   33371              :    or SWITCH_STMT) or CONTINUE_STMT (not nested inside those except
   33372              :    perhaps SWITCH_STMT) and replace them with GOTO_EXPR to lazily created
   33373              :    label.  */
   33374              : 
   33375              : static tree
   33376        56295 : expansion_stmt_find_bc_r (tree *tp, int *walk_subtrees, void *data)
   33377              : {
   33378        56295 :   tree t = *tp;
   33379        56295 :   expansion_stmt_bc *bc_data = (expansion_stmt_bc *) data;
   33380        56295 :   switch (TREE_CODE (t))
   33381              :     {
   33382            0 :     case WHILE_STMT:
   33383            0 :       *walk_subtrees = 0;
   33384            0 :       for (int i = 0; i < TREE_CODE_LENGTH (WHILE_STMT); ++i)
   33385            0 :         if (&TREE_OPERAND (t, i) != &WHILE_BODY (t))
   33386            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33387              :                         data, bc_data->pset);
   33388              :       break;
   33389            0 :     case FOR_STMT:
   33390            0 :       *walk_subtrees = 0;
   33391            0 :       for (int i = 0; i < TREE_CODE_LENGTH (FOR_STMT); ++i)
   33392            0 :         if (&TREE_OPERAND (t, i) != &FOR_BODY (t))
   33393            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33394              :                         data, bc_data->pset);
   33395              :       break;
   33396            0 :     case DO_STMT:
   33397            0 :       *walk_subtrees = 0;
   33398            0 :       for (int i = 0; i < TREE_CODE_LENGTH (DO_STMT); ++i)
   33399            0 :         if (&TREE_OPERAND (t, i) != &DO_BODY (t))
   33400            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33401              :                         data, bc_data->pset);
   33402              :       break;
   33403            0 :     case TEMPLATE_FOR_STMT:
   33404            0 :       *walk_subtrees = 0;
   33405            0 :       for (int i = 0; i < TREE_CODE_LENGTH (TEMPLATE_FOR_STMT); ++i)
   33406            0 :         if (&TREE_OPERAND (t, i) != &TEMPLATE_FOR_BODY (t))
   33407            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33408              :                         data, bc_data->pset);
   33409              :       break;
   33410           12 :     case SWITCH_STMT:
   33411           12 :       if (!bc_data->in_switch)
   33412              :         {
   33413           12 :           *walk_subtrees = 0;
   33414           72 :           for (int i = 0; i < TREE_CODE_LENGTH (SWITCH_STMT); ++i)
   33415           60 :             if (&TREE_OPERAND (t, i) != &SWITCH_STMT_BODY (t))
   33416           48 :               cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33417              :                             data, bc_data->pset);
   33418           12 :           bc_data->in_switch = true;
   33419           12 :           cp_walk_tree (&SWITCH_STMT_BODY (t), expansion_stmt_find_bc_r,
   33420              :                         data, bc_data->pset);
   33421           12 :           bc_data->in_switch = false;
   33422              :         }
   33423              :       break;
   33424          231 :     case BREAK_STMT:
   33425          231 :       if (!bc_data->in_switch)
   33426              :         {
   33427          183 :           if (!bc_data->break_label)
   33428              :             {
   33429           39 :               bc_data->break_label = create_artificial_label (bc_data->loc);
   33430           39 :               TREE_USED (bc_data->break_label) = 1;
   33431           39 :               LABEL_DECL_BREAK (bc_data->break_label) = true;
   33432              :             }
   33433          183 :           *tp = build1_loc (EXPR_LOCATION (t), GOTO_EXPR, void_type_node,
   33434              :                             bc_data->break_label);
   33435              :         }
   33436              :       break;
   33437          137 :     case CONTINUE_STMT:
   33438          137 :       if (!bc_data->continue_label)
   33439              :         {
   33440          137 :           bc_data->continue_label = create_artificial_label (bc_data->loc);
   33441          137 :           TREE_USED (bc_data->continue_label) = 1;
   33442          137 :           LABEL_DECL_CONTINUE (bc_data->continue_label) = true;
   33443              :         }
   33444          137 :       *tp = build1_loc (EXPR_LOCATION (t), GOTO_EXPR, void_type_node,
   33445              :                         bc_data->continue_label);
   33446          137 :       break;
   33447        55915 :     default:
   33448        55915 :       if (TYPE_P (t))
   33449          450 :         *walk_subtrees = 0;
   33450              :       break;
   33451              :     }
   33452        56295 :   return NULL_TREE;
   33453              : }
   33454              : 
   33455              : /* Finish an expansion-statement.  */
   33456              : 
   33457              : void
   33458         1026 : finish_expansion_stmt (tree expansion_stmt, tree args,
   33459              :                        tsubst_flags_t complain, tree in_decl)
   33460              : {
   33461         1026 :   tree expansion_init = TEMPLATE_FOR_EXPR (expansion_stmt);
   33462         1026 :   if (error_operand_p (expansion_init))
   33463           48 :     return;
   33464              : 
   33465         1026 :   enum expansion_stmt_kind {
   33466              :     esk_none,
   33467              :     esk_iterating,
   33468              :     esk_destructuring,
   33469              :     esk_enumerating
   33470         1026 :   } kind = esk_none;
   33471              : 
   33472         1026 :   unsigned HOST_WIDE_INT n = 0;
   33473         1026 :   tree range_decl = TEMPLATE_FOR_DECL (expansion_stmt);
   33474         1026 :   bool is_decomp = false;
   33475         1026 :   if (TREE_CODE (range_decl) == TREE_VEC)
   33476              :     {
   33477          295 :       is_decomp = true;
   33478          295 :       range_decl = TREE_VEC_ELT (range_decl, 0);
   33479              :     }
   33480         1026 :   if (error_operand_p (range_decl))
   33481              :     return;
   33482              : 
   33483          990 :   location_t loc = DECL_SOURCE_LOCATION (range_decl);
   33484          990 :   tree begin = NULL_TREE, begin_minus_begin_type = NULL_TREE;
   33485          990 :   auto_vec<tree, 8> destruct_decls;
   33486          990 :   bool is_lvalue = false;
   33487          990 :   if (BRACE_ENCLOSED_INITIALIZER_P (expansion_init))
   33488              :     {
   33489              :       /* Enumerating expansion statements.  */
   33490          513 :       kind = esk_enumerating;
   33491          513 :       n = CONSTRUCTOR_NELTS (expansion_init);
   33492              :     }
   33493          954 :   else if (TYPE_REF_P (TREE_TYPE (expansion_init))
   33494          477 :            ? TREE_CODE (TREE_TYPE (TREE_TYPE (expansion_init))) != ARRAY_TYPE
   33495          477 :            : TREE_CODE (TREE_TYPE (expansion_init)) != ARRAY_TYPE)
   33496              :     {
   33497          204 :       tree range_temp, begin_expr, end_expr, iter_type;
   33498          204 :       range_temp = convert_from_reference (build_range_temp (expansion_init));
   33499          204 :       iter_type = cp_perform_range_for_lookup (range_temp, &begin_expr,
   33500              :                                                &end_expr, tf_none);
   33501          204 :       if (iter_type != error_mark_node
   33502           87 :           || (begin_expr != error_mark_node && end_expr != error_mark_node))
   33503          117 :         kind = esk_iterating;
   33504              :     }
   33505          600 :   if (kind == esk_iterating)
   33506              :     {
   33507              :       /* Iterating expansion statements.  */
   33508          117 :       tree exprs[2];
   33509          117 :       begin = cp_build_range_for_decls (loc, expansion_init, exprs, range_decl);
   33510          117 :       if (!error_operand_p (begin)
   33511          115 :           && !error_operand_p (exprs[0])
   33512          232 :           && !error_operand_p (exprs[1]))
   33513              :         {
   33514              :           /* In the standard this is all evaluated inside of a consteval
   33515              :              lambda.  So, force in_immediate_context () around this.  */
   33516          115 :           in_consteval_if_p_temp_override icip;
   33517          115 :           in_consteval_if_p = true;
   33518          115 :           tree b = exprs[0], e = exprs[1];
   33519              :           /* The begin-expr and end-expr expressions will be usually wrapped
   33520              :              in TARGET_EXPR if they return a class iterator.  The b
   33521              :              and e artificial variables need to have cv-unqualified type
   33522              :              so that e.g. b can be incremented, so unwrap the TARGET_EXPRs
   33523              :              and force TARGET_EXPR with the cv-unqualified type which is
   33524              :              a hack replacement for a VAR_DECL in a lambda.  */
   33525          115 :           tree btype = cv_unqualified (TREE_TYPE (b));
   33526          115 :           tree etype = cv_unqualified (TREE_TYPE (e));
   33527          115 :           if (TREE_CODE (b) == TARGET_EXPR)
   33528           58 :             b = TARGET_EXPR_INITIAL (b);
   33529          115 :           if (TREE_CODE (e) == TARGET_EXPR)
   33530           58 :             e = TARGET_EXPR_INITIAL (e);
   33531          115 :           b = force_target_expr (btype, b, tf_warning_or_error);
   33532          115 :           e = force_target_expr (etype, e, tf_warning_or_error);
   33533          115 :           tree w = build_stmt (loc, WHILE_STMT, NULL_TREE, NULL_TREE,
   33534              :                                NULL_TREE, NULL_TREE, NULL_TREE);
   33535          115 :           tree r = get_target_expr (build_zero_cst (ptrdiff_type_node));
   33536          115 :           tree binc = build_x_unary_op (loc, PREINCREMENT_EXPR,
   33537          115 :                                         TARGET_EXPR_SLOT (b), NULL_TREE,
   33538              :                                         tf_warning_or_error);
   33539          230 :           tree rinc = build2 (PREINCREMENT_EXPR, ptrdiff_type_node,
   33540          115 :                               TARGET_EXPR_SLOT (r),
   33541              :                               build_int_cst (ptrdiff_type_node, 1));
   33542          115 :           WHILE_BODY (w) = build_compound_expr (loc, binc, rinc);
   33543          115 :           WHILE_COND (w) = build_x_binary_op (loc, NE_EXPR, b, ERROR_MARK,
   33544              :                                               e, ERROR_MARK, NULL_TREE, NULL,
   33545              :                                               tf_warning_or_error);
   33546          115 :           {
   33547          115 :             warning_sentinel wur (warn_unused_result);
   33548          115 :             e = build_compound_expr (loc, b, e);
   33549          115 :             e = build_compound_expr (loc, r, e);
   33550          115 :             e = build_compound_expr (loc, e, w);
   33551          115 :             e = build_compound_expr (loc, e, TARGET_EXPR_SLOT (r));
   33552          115 :           }
   33553          115 :           e = fold_build_cleanup_point_expr (TREE_TYPE (e), e);
   33554          115 :           e = cxx_constant_value (e);
   33555          115 :           if (tree_fits_uhwi_p (e))
   33556           91 :             n = tree_to_uhwi (e);
   33557          115 :         }
   33558              :     }
   33559          873 :   else if (kind == esk_none)
   33560              :     {
   33561          360 :       kind = esk_destructuring;
   33562          360 :       HOST_WIDE_INT sz = cp_decomp_size (loc, TREE_TYPE (expansion_init),
   33563              :                                          tf_warning_or_error);
   33564          360 :       if (sz < 0)
   33565           12 :         return;
   33566          348 :       n = sz;
   33567          348 :       is_lvalue = lvalue_p (expansion_init);
   33568          348 :       tree auto_node = make_auto ();
   33569          348 :       tree decomp_type = cp_build_reference_type (auto_node, true);
   33570          348 :       decomp_type = do_auto_deduction (decomp_type, expansion_init, auto_node);
   33571          348 :       tree decl = build_decl (loc, VAR_DECL, NULL_TREE, decomp_type);
   33572          348 :       TREE_USED (decl) = 1;
   33573          348 :       DECL_ARTIFICIAL (decl) = 1;
   33574          348 :       DECL_DECLARED_CONSTEXPR_P (decl)
   33575          348 :         = DECL_DECLARED_CONSTEXPR_P (range_decl);
   33576          348 :       if (DECL_DECLARED_CONSTEXPR_P (decl))
   33577           62 :         TREE_READONLY (decl) = 1;
   33578          348 :       if (n)
   33579          310 :         fit_decomposition_lang_decl (decl, NULL_TREE);
   33580          348 :       pushdecl (decl);
   33581          348 :       cp_decomp this_decomp;
   33582          348 :       this_decomp.count = n;
   33583          348 :       destruct_decls.safe_grow (n, true);
   33584         1340 :       for (unsigned HOST_WIDE_INT i = 0; i < n; ++i)
   33585              :         {
   33586          992 :           tree this_type = make_auto ();
   33587          992 :           if (DECL_DECLARED_CONSTEXPR_P (decl))
   33588          122 :             this_type = cp_build_qualified_type (this_type, TYPE_QUAL_CONST);
   33589          992 :           tree this_decl = build_decl (loc, VAR_DECL, NULL_TREE, this_type);
   33590          992 :           TREE_USED (this_decl) = 1;
   33591          992 :           DECL_ARTIFICIAL (this_decl) = 1;
   33592          992 :           DECL_DECLARED_CONSTEXPR_P (this_decl)
   33593          992 :             = DECL_DECLARED_CONSTEXPR_P (decl);
   33594          992 :           if (DECL_DECLARED_CONSTEXPR_P (decl))
   33595          122 :             TREE_READONLY (this_decl) = 1;
   33596          992 :           pushdecl (this_decl);
   33597          992 :           this_decomp.decl = this_decl;
   33598          992 :           destruct_decls[i] = this_decl;
   33599              :         }
   33600          348 :       DECL_NAME (decl) = for_range__identifier;
   33601          386 :       cp_finish_decl (decl, expansion_init,
   33602              :                       /*is_constant_init*/false, NULL_TREE,
   33603              :                       LOOKUP_ONLYCONVERTING, n ? &this_decomp : NULL);
   33604          348 :       DECL_NAME (decl) = NULL_TREE;
   33605              :     }
   33606              : 
   33607          978 :   expansion_stmt_bc bc_data = { NULL_TREE, NULL_TREE, NULL, loc, false };
   33608              : 
   33609         3112 :   for (unsigned HOST_WIDE_INT i = 0; i < n; ++i)
   33610              :     {
   33611         2134 :       tree scope = do_pushlevel (sk_block);
   33612         6402 :       bool revert_outer
   33613         2134 :         = (current_binding_level->level_chain
   33614         2134 :            && current_binding_level->level_chain->kind == sk_template_for);
   33615              :       /* Don't diagnose redeclaration of for-range-declaration decls.
   33616              :          The sk_template_for block is reused for the originally parsed
   33617              :          source as well as the lowered one.  In the original one
   33618              :          redeclaration of the for-range-declaration decls in the substatement
   33619              :          should be diagnosed (i.e. declarations of the same name in sk_block
   33620              :          of the body vs. declarations in sk_template_for block).  In the
   33621              :          lowered case, the sk_block added by do_pushlevel (sk_block) above
   33622              :          will be block in the lowering of each Si.  Those blocks do redeclare
   33623              :          for-range-declaration, so temporarily change sk_template_for
   33624              :          kind to sk_block to avoid it being diagnosed as invalid.  */
   33625         2134 :       if (revert_outer)
   33626         2134 :         current_binding_level->level_chain->kind = sk_block;
   33627         2134 :       tree type = TREE_TYPE (range_decl);
   33628         2134 :       if (args)
   33629         1153 :         type = tsubst (type, args, complain | tf_tst_ok, in_decl);
   33630         2134 :       if (DECL_DECLARED_CONSTEXPR_P (range_decl) && !TYPE_REF_P (type))
   33631          442 :         type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
   33632         2134 :       tree decl = build_decl (loc, VAR_DECL, DECL_NAME (range_decl), type);
   33633         2134 :       DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (range_decl);
   33634         2134 :       TREE_USED (decl) |= TREE_USED (range_decl);
   33635         2134 :       DECL_READ_P (decl) |= DECL_READ_P (range_decl);
   33636         2134 :       if (args)
   33637         1153 :         apply_late_template_attributes (&decl, DECL_ATTRIBUTES (decl),
   33638              :                                         /*flags=*/0, args, complain,
   33639              :                                         in_decl);
   33640              : 
   33641         2134 :       DECL_DECLARED_CONSTEXPR_P (decl)
   33642         2134 :         = DECL_DECLARED_CONSTEXPR_P (range_decl);
   33643         2134 :       if (DECL_DECLARED_CONSTEXPR_P (decl))
   33644          442 :         TREE_READONLY (decl) = 1;
   33645         2134 :       pushdecl (decl);
   33646         2134 :       tree init = NULL_TREE;
   33647         2134 :       switch (kind)
   33648              :         {
   33649          751 :         case esk_enumerating:
   33650          751 :           init = CONSTRUCTOR_ELT (expansion_init, i)->value;
   33651          751 :           break;
   33652          391 :         case esk_iterating:
   33653          391 :           tree iter_init, auto_node, iter_type, iter, it;
   33654          391 :           it = build_int_cst (ptrdiff_type_node, i);
   33655          391 :           if (begin_minus_begin_type == NULL_TREE)
   33656              :             {
   33657           83 :               ++cp_unevaluated_operand;
   33658           83 :               ++c_inhibit_evaluation_warnings;
   33659           83 :               tree begin_minus_begin
   33660          166 :                 = build_x_binary_op (loc, MINUS_EXPR, begin, TREE_CODE (begin),
   33661           83 :                                      begin, TREE_CODE (begin), NULL_TREE, NULL,
   33662              :                                      tf_warning_or_error);
   33663           83 :               --cp_unevaluated_operand;
   33664           83 :               --c_inhibit_evaluation_warnings;
   33665           83 :               begin_minus_begin_type
   33666           83 :                 = finish_decltype_type (begin_minus_begin, false,
   33667              :                                         tf_warning_or_error);
   33668              :             }
   33669          391 :           it = build_constructor_single (init_list_type_node, NULL_TREE, it);
   33670          391 :           CONSTRUCTOR_IS_DIRECT_INIT (it) = true;
   33671          391 :           it = finish_compound_literal (begin_minus_begin_type, it,
   33672              :                                         tf_warning_or_error, fcl_functional);
   33673          391 :           iter_init
   33674          391 :             = build_x_binary_op (loc, PLUS_EXPR, begin, ERROR_MARK, it,
   33675              :                                  ERROR_MARK, NULL_TREE, NULL,
   33676              :                                  tf_warning_or_error);
   33677          391 :           auto_node = make_auto ();
   33678          391 :           iter_type = do_auto_deduction (auto_node, iter_init, auto_node);
   33679          391 :           if (DECL_DECLARED_CONSTEXPR_P (range_decl)
   33680          391 :               && !TYPE_REF_P (iter_type))
   33681          136 :             iter_type = cp_build_qualified_type (iter_type, TYPE_QUAL_CONST);
   33682          391 :           iter = build_decl (loc, VAR_DECL, get_identifier ("__for_iter "),
   33683              :                              iter_type);
   33684          391 :           TREE_USED (iter) = 1;
   33685          391 :           DECL_ARTIFICIAL (iter) = 1;
   33686          391 :           if (DECL_DECLARED_CONSTEXPR_P (range_decl))
   33687              :             {
   33688          136 :               TREE_STATIC (iter) = 1;
   33689          136 :               DECL_DECLARED_CONSTEXPR_P (iter) = 1;
   33690          136 :               TREE_READONLY (iter) = 1;
   33691              :             }
   33692          391 :           pushdecl (iter);
   33693          391 :           cp_finish_decl (iter, iter_init, /*is_constant_init*/false,
   33694              :                           NULL_TREE, LOOKUP_ONLYCONVERTING);
   33695          391 :           init = build_x_indirect_ref (loc, iter, RO_UNARY_STAR, NULL_TREE,
   33696              :                                        tf_warning_or_error);
   33697          391 :           break;
   33698          992 :         case esk_destructuring:
   33699          992 :           init = convert_from_reference (destruct_decls[i]);
   33700          992 :           if (!is_lvalue)
   33701              :             {
   33702           71 :               tree ctype;
   33703           71 :               if (DECL_HAS_VALUE_EXPR_P (destruct_decls[i]))
   33704           36 :                 ctype = unlowered_expr_type (destruct_decls[i]);
   33705              :               else
   33706           35 :                 ctype = lookup_decomp_type (destruct_decls[i]);
   33707           71 :               ctype = cp_build_reference_type (ctype, /*rval=*/true);
   33708           71 :               init = build_static_cast (loc, ctype, init,
   33709              :                                         tf_warning_or_error);
   33710           71 :               init = convert_from_reference (init);
   33711              :             }
   33712              :           break;
   33713              :         default:
   33714              :           gcc_unreachable ();
   33715              :         }
   33716         2134 :       cp_decomp this_decomp = {};
   33717         2134 :       if (is_decomp)
   33718              :         {
   33719          604 :           fit_decomposition_lang_decl (decl, NULL_TREE);
   33720          604 :           tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   33721          604 :           this_decomp.count = TREE_VEC_LENGTH (v) - 1;
   33722         1655 :           for (unsigned i = 0; i < this_decomp.count; ++i)
   33723              :             {
   33724         1051 :               tree this_type = make_auto ();
   33725         1051 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   33726            0 :                 this_type = cp_build_qualified_type (this_type,
   33727              :                                                      TYPE_QUAL_CONST);
   33728         1051 :               tree this_decl
   33729         1051 :                 = build_decl (loc, VAR_DECL,
   33730         1051 :                               DECL_NAME (TREE_VEC_ELT (v, i + 1)),
   33731         1051 :                               this_type);
   33732         1051 :               TREE_USED (this_decl) = 1;
   33733         1051 :               DECL_ARTIFICIAL (this_decl) = 1;
   33734         2102 :               DECL_ATTRIBUTES (this_decl)
   33735         1051 :                 = DECL_ATTRIBUTES (TREE_VEC_ELT (v, i + 1));
   33736         1051 :               TREE_USED (this_decl) |= TREE_USED (TREE_VEC_ELT (v, i + 1));
   33737         1051 :               DECL_READ_P (this_decl) |= DECL_READ_P (TREE_VEC_ELT (v, i + 1));
   33738         1051 :               if (DECL_PACK_P (TREE_VEC_ELT (v, i + 1)))
   33739              :                 {
   33740           37 :                   tree dtype = cxx_make_type (DECLTYPE_TYPE);
   33741           37 :                   DECLTYPE_TYPE_EXPR (dtype) = this_decl;
   33742           37 :                   DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (dtype) = 1;
   33743           37 :                   SET_TYPE_STRUCTURAL_EQUALITY (dtype);
   33744           37 :                   tree type = cxx_make_type (TYPE_PACK_EXPANSION);
   33745           37 :                   PACK_EXPANSION_PATTERN (type) = dtype;
   33746           37 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
   33747           74 :                   PACK_EXPANSION_PARAMETER_PACKS (type) = this_decl;
   33748           37 :                   TREE_TYPE (this_decl) = type;
   33749              :                 }
   33750         1051 :               if (args)
   33751          840 :                 apply_late_template_attributes (&this_decl,
   33752          840 :                                                 DECL_ATTRIBUTES (this_decl),
   33753              :                                                 /*flags=*/0, args,
   33754              :                                                 complain, in_decl);
   33755         1051 :               DECL_DECLARED_CONSTEXPR_P (this_decl)
   33756         1051 :                 = DECL_DECLARED_CONSTEXPR_P (decl);
   33757         1051 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   33758            0 :                 TREE_READONLY (this_decl) = 1;
   33759         1051 :               pushdecl (this_decl);
   33760         1051 :               this_decomp.decl = this_decl;
   33761              :             }
   33762              :         }
   33763         2134 :       cp_finish_decl (decl, init, false, NULL_TREE,
   33764              :                       LOOKUP_ONLYCONVERTING, is_decomp ? &this_decomp : NULL);
   33765         2134 :       if (revert_outer)
   33766         2134 :         current_binding_level->level_chain->kind = sk_template_for;
   33767         2134 :       tree targs = args;
   33768         2134 :       if (args == NULL_TREE)
   33769              :         {
   33770          981 :           targs = make_tree_vec (1);
   33771          981 :           TREE_VEC_ELT (targs, 0) = build_int_cst (ptrdiff_type_node, i + 1);
   33772              :         }
   33773          981 :       if (args != NULL_TREE
   33774          981 :           || push_tinst_level_loc (expansion_stmt, targs, loc))
   33775              :         {
   33776         2134 :           local_specialization_stack lss (lss_copy);
   33777         2134 :           register_local_specialization (decl, range_decl);
   33778         2134 :           if (is_decomp)
   33779              :             {
   33780          604 :               tree d = this_decomp.decl;
   33781          604 :               unsigned int cnt = this_decomp.count;
   33782          604 :               tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   33783         1655 :               for (unsigned int i = 0; i < cnt; ++i, d = DECL_CHAIN (d))
   33784         1051 :                 register_local_specialization (d, TREE_VEC_ELT (v, cnt - i));
   33785              :             }
   33786         4268 :           tsubst_stmt (TEMPLATE_FOR_BODY (expansion_stmt),
   33787              :                        targs, complain, in_decl ? in_decl : range_decl);
   33788         2134 :           if (args == NULL_TREE)
   33789          981 :             pop_tinst_level ();
   33790         2134 :         }
   33791         2134 :       tree stmt = do_poplevel (scope);
   33792         2134 :       if (stmt)
   33793              :         {
   33794         2134 :           add_stmt (stmt);
   33795         2134 :           hash_set<tree> pset;
   33796         2134 :           bc_data.continue_label = NULL_TREE;
   33797         2134 :           bc_data.pset = &pset;
   33798         2134 :           cp_walk_tree (&stmt, expansion_stmt_find_bc_r, &bc_data, &pset);
   33799         2134 :           if (bc_data.continue_label)
   33800          137 :             add_stmt (build1 (LABEL_EXPR, void_type_node,
   33801              :                               bc_data.continue_label));
   33802         2134 :         }
   33803              :     }
   33804          978 :   if (bc_data.break_label)
   33805           39 :     add_stmt (build1 (LABEL_EXPR, void_type_node, bc_data.break_label));
   33806          978 :   if (args == NULL_TREE)
   33807              :     {
   33808          429 :       TREE_TYPE (range_decl) = error_mark_node;
   33809          429 :       if (DECL_HAS_VALUE_EXPR_P (range_decl))
   33810              :         {
   33811            0 :           SET_DECL_VALUE_EXPR (range_decl, NULL_TREE);
   33812            0 :           DECL_HAS_VALUE_EXPR_P (range_decl) = 0;
   33813              :         }
   33814          429 :       if (is_decomp)
   33815              :         {
   33816           16 :           tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   33817           59 :           for (int i = 1; i < TREE_VEC_LENGTH (v); ++i)
   33818              :             {
   33819           43 :               tree d = TREE_VEC_ELT (v, i);
   33820           43 :               TREE_TYPE (d) = error_mark_node;
   33821           43 :               if (DECL_HAS_VALUE_EXPR_P (d))
   33822              :                 {
   33823           43 :                   SET_DECL_VALUE_EXPR (d, NULL_TREE);
   33824           43 :                   DECL_HAS_VALUE_EXPR_P (d) = 0;
   33825              :                 }
   33826              :             }
   33827              :         }
   33828              :     }
   33829          990 : }
   33830              : 
   33831              : /* Perform the appropriate conversion of the argument of
   33832              :    std::meta::reflect_constant.   EXPR is the argument, TYPE is its type.
   33833              :    Mainly, the point is to check that the type is valid in this context
   33834              :    and maybe replace the argument with a reference to the corresponding
   33835              :    template parameter object.  */
   33836              : 
   33837              : tree
   33838         2098 : convert_reflect_constant_arg (tree type, tree expr)
   33839              : {
   33840         2098 :   if (invalid_nontype_parm_type_p (type, tf_none))
   33841            0 :     return error_mark_node;
   33842              : 
   33843         2098 :   expr = convert_nontype_argument (type, expr, tf_none);
   33844         2098 :   if (!expr)
   33845           21 :     return error_mark_node;
   33846              :   return expr;
   33847              : }
   33848              : 
   33849              : /* Set up the hash tables for template instantiations.  */
   33850              : 
   33851              : void
   33852        98396 : init_template_processing (void)
   33853              : {
   33854        98396 :   decl_specializations = hash_table<spec_hasher>::create_ggc (37);
   33855        98396 :   type_specializations = hash_table<spec_hasher>::create_ggc (37);
   33856              : 
   33857        98396 :   if (cxx_dialect >= cxx11)
   33858        84199 :     declare_integer_pack ();
   33859        98396 : }
   33860              : 
   33861              : /* Print stats about the template hash tables for -fstats.  */
   33862              : 
   33863              : void
   33864            0 : print_template_statistics (void)
   33865              : {
   33866            0 :   fprintf (stderr, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   33867              :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   33868            0 :            (fmt_size_t) decl_specializations->size (),
   33869            0 :            (fmt_size_t) decl_specializations->elements (),
   33870              :            decl_specializations->collisions ());
   33871            0 :   fprintf (stderr, "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   33872              :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   33873            0 :            (fmt_size_t) type_specializations->size (),
   33874            0 :            (fmt_size_t) type_specializations->elements (),
   33875              :            type_specializations->collisions ());
   33876            0 : }
   33877              : 
   33878              : #if CHECKING_P
   33879              : 
   33880              : namespace selftest {
   33881              : 
   33882              : /* Verify that type_dependent_expression_p () works correctly, even
   33883              :    in the presence of location wrapper nodes.  */
   33884              : 
   33885              : static void
   33886            1 : test_type_dependent_expression_p ()
   33887              : {
   33888            1 :   location_t loc = BUILTINS_LOCATION;
   33889              : 
   33890            1 :   tree name = get_identifier ("foo");
   33891              : 
   33892              :   /* If no templates are involved, nothing is type-dependent.  */
   33893            1 :   gcc_assert (!processing_template_decl);
   33894            1 :   ASSERT_FALSE (type_dependent_expression_p (name));
   33895              : 
   33896            1 :   ++processing_template_decl;
   33897              : 
   33898              :   /* Within a template, an unresolved name is always type-dependent.  */
   33899            1 :   ASSERT_TRUE (type_dependent_expression_p (name));
   33900              : 
   33901              :   /* Ensure it copes with NULL_TREE and errors.  */
   33902            1 :   ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
   33903            1 :   ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
   33904              : 
   33905              :   /* A USING_DECL in a template should be type-dependent, even if wrapped
   33906              :      with a location wrapper (PR c++/83799).  */
   33907            1 :   tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
   33908            1 :   TREE_TYPE (using_decl) = integer_type_node;
   33909            1 :   ASSERT_TRUE (type_dependent_expression_p (using_decl));
   33910            1 :   tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
   33911            1 :   ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
   33912            1 :   ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
   33913              : 
   33914            1 :   --processing_template_decl;
   33915            1 : }
   33916              : 
   33917              : /* Run all of the selftests within this file.  */
   33918              : 
   33919              : void
   33920            1 : cp_pt_cc_tests ()
   33921              : {
   33922            1 :   test_type_dependent_expression_p ();
   33923            1 : }
   33924              : 
   33925              : } // namespace selftest
   33926              : 
   33927              : #endif /* #if CHECKING_P */
   33928              : 
   33929              : #include "gt-cp-pt.h"
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.