LCOV - code coverage report
Current view: top level - gcc/cp - pt.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.2 % 15091 14364
Test Date: 2026-03-28 14:25:54 Functions: 98.7 % 477 471
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     68135561 : local_specialization_stack::local_specialization_stack (lss_policy policy)
      90     68135561 :   : saved (local_specializations)
      91              : {
      92     68135561 :   if (policy == lss_nop)
      93              :     ;
      94     44421792 :   else if (policy == lss_blank || !saved)
      95     40841230 :     local_specializations = new hash_map<tree, tree>;
      96              :   else
      97      3580562 :     local_specializations = new hash_map<tree, tree>(*saved);
      98     68135561 : }
      99              : 
     100     68132852 : local_specialization_stack::~local_specialization_stack ()
     101              : {
     102     68132852 :   if (local_specializations != saved)
     103              :     {
     104     88838166 :       delete local_specializations;
     105     44419083 :       local_specializations = saved;
     106              :     }
     107     68132852 : }
     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    353883542 : push_access_scope (tree t)
     234              : {
     235    353883542 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
     236              :               || TREE_CODE (t) == TYPE_DECL);
     237              : 
     238    592237087 :   if (DECL_FRIEND_CONTEXT (t))
     239     11091242 :     push_nested_class (DECL_FRIEND_CONTEXT (t));
     240     77235323 :   else if (DECL_IMPLICIT_TYPEDEF_P (t)
     241    371229333 :            && CLASS_TYPE_P (TREE_TYPE (t)))
     242     22891412 :     push_nested_class (TREE_TYPE (t));
     243    325446509 :   else if (DECL_CLASS_SCOPE_P (t))
     244     64385240 :     push_nested_class (DECL_CONTEXT (t));
     245    261061269 :   else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
     246              :     /* An artificial deduction guide should have the same access as
     247              :        the constructor.  */
     248       109486 :     push_nested_class (TREE_TYPE (TREE_TYPE (t)));
     249              :   else
     250    260951783 :     push_to_top_level ();
     251              : 
     252    353883542 :   if (TREE_CODE (t) == FUNCTION_DECL)
     253              :     {
     254    238892724 :       vec_safe_push (saved_access_scope, current_function_decl);
     255    238892724 :       current_function_decl = t;
     256              :     }
     257    353883542 : }
     258              : 
     259              : /* Restore the scope set up by push_access_scope.  T is the node we
     260              :    are processing.  */
     261              : 
     262              : void
     263    353880842 : pop_access_scope (tree t)
     264              : {
     265    353880842 :   if (TREE_CODE (t) == FUNCTION_DECL)
     266    238890024 :     current_function_decl = saved_access_scope->pop();
     267              : 
     268    592231687 :   if (DECL_FRIEND_CONTEXT (t)
     269    348335221 :       || (DECL_IMPLICIT_TYPEDEF_P (t)
     270     22891412 :           && CLASS_TYPE_P (TREE_TYPE (t)))
     271    325443809 :       || DECL_CLASS_SCOPE_P (t)
     272    480468603 :       || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
     273     92931759 :     pop_nested_class ();
     274              :   else
     275    260949083 :     pop_from_top_level ();
     276    353880842 : }
     277              : 
     278              : /* Return current function, ignoring temporary overrides
     279              :    of current_function_decl by push_access_scope.  */
     280              : 
     281              : tree
     282           70 : current_function_decl_without_access_scope ()
     283              : {
     284           70 :   if (vec_safe_length (saved_access_scope))
     285            1 :     return (*saved_access_scope)[0];
     286              :   else
     287           69 :     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     20987918 : finish_member_template_decl (tree decl)
     297              : {
     298     20987918 :   if (decl == error_mark_node)
     299              :     return error_mark_node;
     300              : 
     301     20987836 :   gcc_assert (DECL_P (decl));
     302              : 
     303     20987836 :   if (TREE_CODE (decl) == TYPE_DECL)
     304              :     {
     305      1249177 :       tree type;
     306              : 
     307      1249177 :       type = TREE_TYPE (decl);
     308      1249177 :       if (type == error_mark_node)
     309              :         return error_mark_node;
     310      1249177 :       if (MAYBE_CLASS_TYPE_P (type)
     311      1249177 :           && CLASSTYPE_TEMPLATE_INFO (type)
     312      1249177 :           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     313              :         {
     314       726060 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     315       726060 :           check_member_template (tmpl);
     316       726060 :           return tmpl;
     317              :         }
     318              :       return NULL_TREE;
     319              :     }
     320     19738659 :   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     19738656 :   else if (DECL_TEMPLATE_INFO (decl))
     324              :     {
     325     19738653 :       if (!DECL_TEMPLATE_SPECIALIZATION (decl))
     326              :         {
     327     19697617 :           check_member_template (DECL_TI_TEMPLATE (decl));
     328     19697617 :           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    715507542 : build_template_info (tree template_decl, tree template_args)
     344              : {
     345    715507542 :   tree result = make_node (TEMPLATE_INFO);
     346    715507542 :   TI_TEMPLATE (result) = template_decl;
     347    715507542 :   TI_ARGS (result) = template_args;
     348    715507542 :   return result;
     349              : }
     350              : 
     351              : /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE.  */
     352              : 
     353              : static tree
     354   2939456478 : decl_template_info (const_tree decl)
     355              : {
     356              :   /* This needs to match template_info_decl_check.  */
     357   2939456478 :   if (DECL_LANG_SPECIFIC (decl))
     358   2197715765 :     switch (TREE_CODE (decl))
     359              :       {
     360   1712960506 :       case FUNCTION_DECL:
     361   1712960506 :         if (DECL_THUNK_P (decl))
     362              :           break;
     363   2197712235 :         gcc_fallthrough ();
     364   2197712235 :       case VAR_DECL:
     365   2197712235 :       case FIELD_DECL:
     366   2197712235 :       case TYPE_DECL:
     367   2197712235 :       case CONCEPT_DECL:
     368   2197712235 :       case TEMPLATE_DECL:
     369   2197712235 :         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   3816679990 : get_template_info (const_tree t)
     381              : {
     382   3816679990 :   tree tinfo = NULL_TREE;
     383              : 
     384   3816679990 :   if (!t || t == error_mark_node)
     385              :     return NULL;
     386              : 
     387   3816679988 :   if (TREE_CODE (t) == NAMESPACE_DECL
     388   3669190315 :       || TREE_CODE (t) == PARM_DECL)
     389              :     return NULL;
     390              : 
     391   3669190179 :   if (DECL_P (t))
     392   2939456478 :     tinfo = decl_template_info (t);
     393              : 
     394   3669190179 :   if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
     395    736404068 :     t = TREE_TYPE (t);
     396              : 
     397   3669190179 :   if (OVERLOAD_TYPE_P (t))
     398   1430088516 :     tinfo = TYPE_TEMPLATE_INFO (t);
     399   2239101663 :   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    776795555 : template_class_depth (tree type)
     425              : {
     426    776795555 :   int depth;
     427              : 
     428   1093393058 :   for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
     429              :     {
     430    316597503 :       tree tinfo = get_template_info (type);
     431              : 
     432    316597503 :       if (tinfo
     433    273379398 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
     434    273379380 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
     435    581505316 :           && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
     436    237385445 :         ++depth;
     437              : 
     438    316597503 :       if (DECL_P (type))
     439              :         {
     440      5133377 :           if (tree fctx = DECL_FRIEND_CONTEXT (type))
     441              :             type = fctx;
     442              :           else
     443      2666637 :             type = CP_DECL_CONTEXT (type);
     444              :         }
     445    629077329 :       else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
     446      4405006 :         type = LAMBDA_TYPE_EXTRA_SCOPE (type);
     447              :       else
     448    311714133 :         type = CP_TYPE_CONTEXT (type);
     449              :     }
     450              : 
     451    776795555 :   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    168810000 : instantiates_primary_template_p (tree node)
     459              : {
     460    168810000 :   tree tinfo = get_template_info (node);
     461    168810000 :   if (!tinfo)
     462              :     return false;
     463              : 
     464    168746259 :   tree tmpl = TI_TEMPLATE (tinfo);
     465    168746259 :   if (PRIMARY_TEMPLATE_P (tmpl))
     466              :     return true;
     467              : 
     468    135758736 :   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    192094010 : inline_needs_template_parms (tree decl, bool nsdmi)
     489              : {
     490    192094010 :   if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
     491              :     return false;
     492              : 
     493    159089895 :   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
     494    159089895 :           > (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     44904587 : push_inline_template_parms_recursive (tree parmlist, int levels)
     504              : {
     505     44904587 :   tree parms = TREE_VALUE (parmlist);
     506     44904587 :   int i;
     507              : 
     508     44904587 :   if (levels > 1)
     509       612577 :     push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
     510              : 
     511     44904587 :   ++processing_template_decl;
     512     89809174 :   current_template_parms
     513     44904587 :     = tree_cons (size_int (current_template_depth + 1),
     514              :                  parms, current_template_parms);
     515     89809174 :   TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
     516     44904587 :     = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
     517     44904587 :   TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
     518              : 
     519     44904587 :   begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
     520              :                NULL);
     521    122853758 :   for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
     522              :     {
     523     77949171 :       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
     524              : 
     525     77949171 :       if (error_operand_p (parm))
     526           37 :         continue;
     527              : 
     528     77949134 :       gcc_assert (DECL_P (parm));
     529              : 
     530     77949134 :       switch (TREE_CODE (parm))
     531              :         {
     532     74055498 :         case TYPE_DECL:
     533     74055498 :         case TEMPLATE_DECL:
     534     74055498 :           pushdecl (parm);
     535     74055498 :           break;
     536              : 
     537      3893636 :         case PARM_DECL:
     538              :           /* Push the CONST_DECL.  */
     539      3893636 :           pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
     540      3893636 :           break;
     541              : 
     542            0 :         default:
     543            0 :           gcc_unreachable ();
     544              :         }
     545              :     }
     546     44904587 : }
     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    192094010 : maybe_begin_member_template_processing (tree decl)
     554              : {
     555    192094010 :   tree parms;
     556    192094010 :   int levels = 0;
     557    192094010 :   bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
     558              : 
     559    192094010 :   if (nsdmi || decl_specialization_friend_p (decl))
     560              :     {
     561      2209906 :       tree ctx = nsdmi ? DECL_CONTEXT (decl) : DECL_CHAIN (decl);
     562      2209906 :       decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
     563              :               /* Disregard full specializations (c++/60999).  */
     564      1641492 :               && uses_template_parms (ctx)
     565      3841027 :               ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
     566              :     }
     567              : 
     568    192094010 :   if (inline_needs_template_parms (decl, nsdmi))
     569              :     {
     570     44292010 :       parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
     571     44292010 :       levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
     572              : 
     573     44292010 :       if (DECL_TEMPLATE_SPECIALIZATION (decl))
     574              :         {
     575            0 :           --levels;
     576            0 :           parms = TREE_CHAIN (parms);
     577              :         }
     578              : 
     579     44292010 :       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    192094010 :   inline_parm_levels.safe_push (levels);
     585    192094010 : }
     586              : 
     587              : /* Undo the effects of maybe_begin_member_template_processing.  */
     588              : 
     589              : void
     590    192094438 : maybe_end_member_template_processing (void)
     591              : {
     592    192094438 :   int i;
     593    192094438 :   int last;
     594              : 
     595    192094438 :   if (inline_parm_levels.length () == 0)
     596              :     return;
     597              : 
     598    192094010 :   last = inline_parm_levels.pop ();
     599    236998597 :   for (i = 0; i < last; ++i)
     600              :     {
     601     44904587 :       --processing_template_decl;
     602     44904587 :       current_template_parms = TREE_CHAIN (current_template_parms);
     603     44904587 :       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     82945237 : add_to_template_args (tree args, tree extra_args)
     612              : {
     613     82945237 :   tree new_args;
     614     82945237 :   int extra_depth;
     615     82945237 :   int i;
     616     82945237 :   int j;
     617              : 
     618     82945237 :   if (args == NULL_TREE || extra_args == error_mark_node)
     619              :     return extra_args;
     620              : 
     621    131942128 :   extra_depth = TMPL_ARGS_DEPTH (extra_args);
     622     65971064 :   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
     623              : 
     624    227108558 :   for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
     625     66438694 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
     626              : 
     627    131942128 :   for (j = 1; j <= extra_depth; ++j, ++i)
     628    131942128 :     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   1547137408 : add_outermost_template_args (tree args, tree extra_args)
     644              : {
     645   1547137408 :   tree new_args;
     646              : 
     647   1547137408 :   if (!args)
     648              :     return extra_args;
     649   1547137408 :   if (TREE_CODE (args) == TEMPLATE_DECL)
     650              :     {
     651    510227374 :       tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
     652    510227374 :       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   6624180890 :   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   7661090924 :   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    102454280 :   TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
     666              : 
     667     51227140 :   new_args = add_to_template_args (args, extra_args);
     668              : 
     669              :   /* Now, we restore ARGS to its full dimensions.  */
     670    102454280 :   TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
     671              : 
     672     51227140 :   return new_args;
     673              : }
     674              : 
     675              : /* Return the N levels of innermost template arguments from the ARGS.  */
     676              : 
     677              : tree
     678   6989952175 : get_innermost_template_args (tree args, int n)
     679              : {
     680   6989952175 :   tree new_args;
     681   6989952175 :   int extra_levels;
     682   6989952175 :   int i;
     683              : 
     684   6989952175 :   gcc_assert (n >= 0);
     685              : 
     686              :   /* If N is 1, just return the innermost set of template arguments.  */
     687   6989952175 :   if (n == 1)
     688  13979694986 :     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       209364 :   extra_levels = TMPL_ARGS_DEPTH (args) - n;
     693       104682 :   gcc_assert (extra_levels >= 0);
     694       104682 :   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       641374 : strip_innermost_template_args (tree args, int extra_levels)
     711              : {
     712       641374 :   tree new_args;
     713      1282748 :   int n = TMPL_ARGS_DEPTH (args) - extra_levels;
     714       641374 :   int i;
     715              : 
     716       641374 :   gcc_assert (n >= 0);
     717              : 
     718              :   /* If N is 1, just return the outermost set of template arguments.  */
     719       641374 :   if (n == 1)
     720      1274212 :     return TMPL_ARGS_LEVEL (args, 1);
     721              : 
     722              :   /* If we're not removing anything, just return the arguments we were
     723              :      given.  */
     724         4268 :   gcc_assert (extra_levels >= 0);
     725         4268 :   if (extra_levels == 0)
     726              :     return args;
     727              : 
     728              :   /* Make a new set of arguments, not containing the inner arguments.  */
     729         4268 :   new_args = make_tree_vec (n);
     730        17072 :   for (i = 1; i <= n; ++i)
     731        17072 :     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     87876565 : 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     87876565 :   begin_scope (sk_template_parms, NULL);
     758     87876565 :   ++processing_template_decl;
     759     87876565 :   ++processing_template_parmlist;
     760     87876565 :   note_template_header (0);
     761              : 
     762              :   /* Add a dummy parameter level while we process the parameter list.  */
     763    175753130 :   current_template_parms
     764     87876565 :     = tree_cons (size_int (current_template_depth + 1),
     765              :                  make_tree_vec (0),
     766              :                  current_template_parms);
     767     87876565 : }
     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      5786691 : check_specialization_scope (void)
     775              : {
     776      5786691 :   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      5786691 :   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      5786667 :   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      5786691 : begin_specialization (void)
     814              : {
     815      5786691 :   begin_scope (sk_template_spec, NULL);
     816      5786691 :   note_template_header (1);
     817     11573382 :   return check_specialization_scope ();
     818              : }
     819              : 
     820              : /* Called at then end of processing a declaration preceded by
     821              :    template<>.  */
     822              : 
     823              : void
     824      5786691 : end_specialization (void)
     825              : {
     826      5786691 :   finish_scope ();
     827      5786691 :   reset_specialization ();
     828      5786691 : }
     829              : 
     830              : /* Any template <>'s that we have seen thus far are not referring to a
     831              :    function specialization.  */
     832              : 
     833              : void
     834    161173848 : reset_specialization (void)
     835              : {
     836    161173848 :   processing_specialization = 0;
     837    161173848 :   template_header_count = 0;
     838    161173848 : }
     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     93663256 : note_template_header (int specialization)
     845              : {
     846     93663256 :   processing_specialization = specialization;
     847     93663256 :   template_header_count++;
     848            0 : }
     849              : 
     850              : /* We're beginning an explicit instantiation.  */
     851              : 
     852              : void
     853      2686959 : begin_explicit_instantiation (void)
     854              : {
     855      2686959 :   gcc_assert (!processing_explicit_instantiation);
     856      2686959 :   processing_explicit_instantiation = true;
     857      2686959 : }
     858              : 
     859              : 
     860              : void
     861      2686956 : end_explicit_instantiation (void)
     862              : {
     863      2686956 :   gcc_assert (processing_explicit_instantiation);
     864      2686956 :   processing_explicit_instantiation = false;
     865      2686956 : }
     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     13418698 : check_specialization_namespace (tree tmpl)
     874              : {
     875     13418698 :   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     13418698 :   if (current_scope() != DECL_CONTEXT (tmpl)
     885     13418698 :       && !at_namespace_scope_p ())
     886              :     {
     887           12 :       error ("specialization of %qD must appear at namespace scope", tmpl);
     888           12 :       return false;
     889              :     }
     890              : 
     891     13418686 :   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      2671999 : check_explicit_instantiation_namespace (tree spec)
     910              : {
     911      2671999 :   tree ns;
     912              : 
     913              :   /* DR 275: An explicit instantiation shall appear in an enclosing
     914              :      namespace of its template.  */
     915      2671999 :   ns = decl_namespace_context (spec);
     916      2671999 :   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      2671999 : }
     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     33212594 : 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     33212594 :   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     22288297 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     960              :     {
     961     22288297 :       tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     962     22288297 :       tree args = CLASSTYPE_TI_ARGS (type);
     963              : 
     964              :       /* If there are no template parameters, this cannot be a new
     965              :          partial template specialization?  */
     966     22288297 :       if (!current_template_parms)
     967              :         return false;
     968              : 
     969              :       /* The injected-class-name is not a new partial specialization.  */
     970     14002734 :       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     14002734 :       tree type_constr = current_template_constraints ();
     976              : 
     977     14002734 :       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     14002682 :       tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
     987     14628533 :       while (specs)
     988              :         {
     989     14421345 :           tree spec_tmpl = TREE_VALUE (specs);
     990     14421345 :           tree spec_args = TREE_PURPOSE (specs);
     991     14421345 :           tree spec_constr = get_constraints (spec_tmpl);
     992     14421345 :           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     14421345 :           if (comp_template_args (args, spec_args)
     997     14135856 :               && comp_template_parms (spec_parms, current_template_parms)
     998     28557198 :               && equivalent_constraints (type_constr, spec_constr))
     999              :             {
    1000     13795494 :               type = TREE_TYPE (spec_tmpl);
    1001     13795494 :               return false;
    1002              :             }
    1003       625851 :           specs = TREE_CHAIN (specs);
    1004              :         }
    1005              : 
    1006              :       /* Create a new type node (and corresponding type decl)
    1007              :          for the newly declared specialization.  */
    1008       207188 :       tree t = make_class_type (TREE_CODE (type));
    1009       207188 :       CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
    1010       207188 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
    1011       207188 :       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       207188 :       TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
    1017              : 
    1018              :       /* Build the corresponding type decl.  */
    1019       207188 :       tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
    1020       207188 :       DECL_CONTEXT (d) = TYPE_CONTEXT (t);
    1021       207188 :       DECL_SOURCE_LOCATION (d) = input_location;
    1022       207188 :       TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl));
    1023              : 
    1024       207188 :       set_instantiating_module (d);
    1025       207188 :       DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
    1026              : 
    1027       207188 :       type = t;
    1028       207188 :       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     72189705 : maybe_process_partial_specialization (tree type)
    1039              : {
    1040     72189705 :   tree context;
    1041              : 
    1042     72189705 :   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     72188941 :   if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
    1048              :     return type;
    1049              : 
    1050              :   /* An injected-class-name is not a specialization.  */
    1051     70407287 :   if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
    1052              :     return type;
    1053              : 
    1054     70407281 :   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     70407278 :   context = TYPE_CONTEXT (type);
    1062              : 
    1063     70407278 :   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     70407272 :   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     33212594 :       if (maybe_new_partial_specialization (type))
    1088              :         {
    1089     11131485 :           if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
    1090     11131485 :               && !at_namespace_scope_p ())
    1091           12 :             return error_mark_node;
    1092     11131473 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1093     11131473 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1094     11131473 :           if (processing_template_decl)
    1095              :             {
    1096      6897805 :               tree decl = push_template_decl (TYPE_MAIN_DECL (type));
    1097      6897805 :               if (decl == error_mark_node)
    1098              :                 return error_mark_node;
    1099      6897717 :               return TREE_TYPE (decl);
    1100              :             }
    1101              :         }
    1102     22081109 :       else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
    1103            0 :         error ("specialization of %qT after instantiation", type);
    1104         6964 :       else if (errorcount && !processing_specialization
    1105         5100 :                 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
    1106     22086209 :                && !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     36319404 :   else if (CLASS_TYPE_P (type)
    1113     36319404 :            && !CLASSTYPE_USE_TEMPLATE (type)
    1114     36319404 :            && CLASSTYPE_TEMPLATE_INFO (type)
    1115     24116006 :            && context && CLASS_TYPE_P (context)
    1116     39646286 :            && 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      1954116 :       if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
    1135      1954116 :           && !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     35240562 :   else if (processing_specialization)
    1209              :     {
    1210              :        /* Someday C++0x may allow for enum template specialization.  */
    1211           18 :       if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
    1212           37 :           && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
    1213            9 :         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     63507617 :   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    787250262 : verify_unstripped_args_1 (tree inner)
    1230              : {
    1231   2291769525 :   for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
    1232              :     {
    1233   1504519263 :       tree arg = TREE_VEC_ELT (inner, i);
    1234   1504519263 :       if (TREE_CODE (arg) == TEMPLATE_DECL || REFLECT_EXPR_P (arg))
    1235              :         /* OK */;
    1236   1503061105 :       else if (TYPE_P (arg))
    1237   1365980208 :         gcc_assert (strip_typedefs (arg, NULL) == arg);
    1238    137080897 :       else if (ARGUMENT_PACK_P (arg))
    1239      3030201 :         verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
    1240    134050696 :       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    134050398 :         gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
    1245              :     }
    1246    787250262 : }
    1247              : 
    1248              : static void
    1249    886828276 : verify_unstripped_args (tree args)
    1250              : {
    1251    886828276 :   ++processing_template_decl;
    1252    886828276 :   if (!any_dependent_template_arguments_p (args))
    1253    784220061 :     verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
    1254    886828276 :   --processing_template_decl;
    1255    886828276 : }
    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    886828276 : retrieve_specialization (tree tmpl, tree args, hashval_t hash)
    1273              : {
    1274    886828276 :   if (tmpl == NULL_TREE)
    1275              :     return NULL_TREE;
    1276              : 
    1277    886828276 :   if (args == error_mark_node)
    1278              :     return NULL_TREE;
    1279              : 
    1280    886828276 :   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   1773656552 :   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    886828276 :   if (flag_checking)
    1291    886828276 :     verify_unstripped_args (args);
    1292              : 
    1293              :   /* Lambda functions in templates aren't instantiated normally, but through
    1294              :      tsubst_lambda_expr.  */
    1295    886828276 :   if (lambda_fn_in_template_p (tmpl))
    1296              :     return NULL_TREE;
    1297              : 
    1298    886828276 :   spec_entry elt;
    1299    886828276 :   elt.tmpl = tmpl;
    1300    886828276 :   elt.args = args;
    1301    886828276 :   elt.hash = hash;
    1302              : 
    1303    886828276 :   spec_hash_table *specializations;
    1304    886828276 :   if (DECL_CLASS_TEMPLATE_P (tmpl))
    1305      2343336 :     specializations = type_specializations;
    1306              :   else
    1307    884484940 :     specializations = decl_specializations;
    1308              : 
    1309    886828276 :   if (spec_entry *found = specializations->find (&elt))
    1310    442174293 :     return found->spec;
    1311              : 
    1312              :   return NULL_TREE;
    1313              : }
    1314              : 
    1315              : /* Like retrieve_specialization, but for local declarations.  */
    1316              : 
    1317              : tree
    1318    269896620 : retrieve_local_specialization (tree tmpl)
    1319              : {
    1320    269896620 :   if (local_specializations == NULL)
    1321              :     return NULL_TREE;
    1322              : 
    1323    264384360 :   tree *slot = local_specializations->get (tmpl);
    1324    264384360 :   return slot ? *slot : NULL_TREE;
    1325              : }
    1326              : 
    1327              : /* Returns nonzero iff DECL is a specialization of TMPL.  */
    1328              : 
    1329              : int
    1330      1141099 : is_specialization_of (tree decl, tree tmpl)
    1331              : {
    1332      1141099 :   tree t;
    1333              : 
    1334      1141099 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    1335              :     {
    1336       822689 :       for (t = decl;
    1337       748184 :            t != NULL_TREE;
    1338       448228 :            t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
    1339       748184 :         if (t == tmpl)
    1340              :           return 1;
    1341              :     }
    1342              :   else
    1343              :     {
    1344       767376 :       gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    1345              : 
    1346      1383149 :       for (t = TREE_TYPE (decl);
    1347      1383149 :            t != NULL_TREE;
    1348      1138884 :            t = CLASSTYPE_USE_TEMPLATE (t)
    1349      1138884 :              ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
    1350      1383149 :         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      1657544 : is_specialization_of_friend (tree decl, tree friend_decl)
    1362              : {
    1363      1657544 :   bool need_template = true;
    1364      1657544 :   int template_depth;
    1365              : 
    1366      1657544 :   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      1657544 :   if (TREE_CODE (friend_decl) == FUNCTION_DECL
    1373       516640 :       && DECL_CLASS_SCOPE_P (friend_decl)
    1374           54 :       && DECL_TEMPLATE_INFO (friend_decl)
    1375      1657598 :       && !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      1657490 :   else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
    1382      1657490 :            && !PRIMARY_TEMPLATE_P (friend_decl))
    1383              :     need_template = false;
    1384              : 
    1385              :   /* There is nothing to do if this is not a template friend.  */
    1386      1657544 :   if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
    1387              :     return false;
    1388              : 
    1389      1140958 :   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       596875 :   template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
    1413       596875 :   if (template_depth
    1414          147 :       && DECL_CLASS_SCOPE_P (decl)
    1415       597016 :       && 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    296986515 : register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
    1552              :                          hashval_t hash)
    1553              : {
    1554    296986515 :   tree fn;
    1555              : 
    1556    296986515 :   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    296986515 :   spec_entry elt;
    1561    296986515 :   elt.tmpl = tmpl;
    1562    296986515 :   elt.args = args;
    1563    296986515 :   elt.spec = spec;
    1564    296986515 :   elt.hash = hash;
    1565              : 
    1566    296986515 :   spec_entry **slot = decl_specializations->find_slot (&elt, INSERT);
    1567    296986515 :   if (*slot)
    1568       620986 :     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    296986515 :   if (fn == spec)
    1578              :     return spec;
    1579    296985503 :   else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
    1580              :     {
    1581       543966 :       if (DECL_TEMPLATE_INSTANTIATION (fn))
    1582              :         {
    1583       543720 :           if (DECL_ODR_USED (fn)
    1584       543720 :               || 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       543714 :               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       543714 :               DECL_INITIAL (fn) = NULL_TREE;
    1617       543714 :               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       726635 :               FOR_EACH_CLONE (clone, fn)
    1630              :                 {
    1631       182921 :                   DECL_DECLARED_INLINE_P (clone)
    1632       182921 :                     = DECL_DECLARED_INLINE_P (fn);
    1633       365842 :                   DECL_SOURCE_LOCATION (clone)
    1634       182921 :                     = DECL_SOURCE_LOCATION (fn);
    1635       182921 :                   DECL_DELETED_FN (clone)
    1636       365842 :                     = DECL_DELETED_FN (fn);
    1637              :                 }
    1638       543714 :               check_specialization_namespace (tmpl);
    1639              : 
    1640       543714 :               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    296441537 :   else if (fn)
    1660        76008 :     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    296365529 :   if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
    1665    298109028 :       && !check_specialization_namespace (tmpl))
    1666            0 :     DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
    1667              : 
    1668    296365529 :   spec_entry *entry = ggc_alloc<spec_entry> ();
    1669    296365529 :   gcc_assert (tmpl && args && spec);
    1670    296365529 :   *entry = elt;
    1671    296365529 :   *slot = entry;
    1672    124852981 :   if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
    1673     27446241 :        && PRIMARY_TEMPLATE_P (tmpl)
    1674     26524030 :        && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
    1675    284910503 :       || module_maybe_has_cmi_p ()
    1676    580805473 :       || 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     60857886 :     DECL_TEMPLATE_INSTANTIATIONS (tmpl)
    1687     60857886 :       = 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  15249559601 : spec_hasher::equal (spec_entry *e1, spec_entry *e2)
    1706              : {
    1707  15249559601 :   int equal;
    1708              : 
    1709  15249559601 :   ++comparing_specializations;
    1710  15249559601 :   ++comparing_dependent_aliases;
    1711  15249559601 :   ++processing_template_decl;
    1712  16220195485 :   equal = (e1->tmpl == e2->tmpl
    1713  15249559601 :            && comp_template_args (e1->args, e2->args));
    1714    970635884 :   if (equal && flag_concepts
    1715              :       /* tmpl could be a FIELD_DECL for a capture pack.  */
    1716    964898269 :       && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
    1717    964898269 :       && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
    1718     38216391 :       && uses_template_parms (e1->args))
    1719              :     {
    1720              :       /* Partial specializations of a variable template can be distinguished by
    1721              :          constraints.  */
    1722         6230 :       tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
    1723         6230 :       tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
    1724         6230 :       equal = equivalent_constraints (c1, c2);
    1725              :     }
    1726  15249559601 :   --processing_template_decl;
    1727  15249559601 :   --comparing_dependent_aliases;
    1728  15249559601 :   --comparing_specializations;
    1729              : 
    1730  15249559601 :   return equal;
    1731              : }
    1732              : 
    1733              : /* Returns a hash for a template TMPL and template arguments ARGS.  */
    1734              : 
    1735              : static hashval_t
    1736   1494571025 : hash_tmpl_and_args (tree tmpl, tree args)
    1737              : {
    1738   1494571025 :   hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
    1739   1494571025 :   return iterative_hash_template_arg (args, val);
    1740              : }
    1741              : 
    1742              : hashval_t
    1743   1450826037 : spec_hasher::hash (tree tmpl, tree args)
    1744              : {
    1745   1450826037 :   ++comparing_specializations;
    1746   1450826037 :   hashval_t val = hash_tmpl_and_args (tmpl, args);
    1747   1450826037 :   --comparing_specializations;
    1748   1450826037 :   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  13841509842 : spec_hasher::hash (spec_entry *e)
    1756              : {
    1757  13841509842 :   if (e->hash == 0)
    1758    897842822 :     e->hash = hash (e->tmpl, e->args);
    1759  13841509842 :   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  17504614710 : iterative_hash_template_arg (tree arg, hashval_t val)
    1769              : {
    1770  17504614710 :   if (arg == NULL_TREE)
    1771    826957856 :     return iterative_hash_hashval_t (0, val);
    1772              : 
    1773  16677656854 :   if (!TYPE_P (arg))
    1774              :     /* Strip nop-like things, but not the same as STRIP_NOPS.  */
    1775   4052849446 :     while (CONVERT_EXPR_P (arg)
    1776              :            || TREE_CODE (arg) == NON_LVALUE_EXPR
    1777   4052849446 :            || class_nttp_const_wrapper_p (arg))
    1778      3588953 :       arg = TREE_OPERAND (arg, 0);
    1779              : 
    1780  16677656854 :   enum tree_code code = TREE_CODE (arg);
    1781              : 
    1782  16677656854 :   val = iterative_hash_hashval_t (code, val);
    1783              : 
    1784  16677656854 :   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    440299214 :     case IDENTIFIER_NODE:
    1797    440299214 :       return iterative_hash_hashval_t (IDENTIFIER_HASH_VALUE (arg), val);
    1798              : 
    1799   2671940600 :     case TREE_VEC:
    1800   6973919610 :       for (tree elt : tree_vec_range (arg))
    1801   4301979010 :         val = iterative_hash_template_arg (elt, val);
    1802   2671940600 :       return val;
    1803              : 
    1804    235839448 :     case TYPE_PACK_EXPANSION:
    1805    235839448 :     case EXPR_PACK_EXPANSION:
    1806    235839448 :       val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
    1807    235839448 :       return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
    1808              : 
    1809         2755 :     case PACK_INDEX_TYPE:
    1810         2755 :     case PACK_INDEX_EXPR:
    1811         2755 :       val = iterative_hash_template_arg (PACK_INDEX_PACK (arg), val);
    1812         2755 :       return iterative_hash_template_arg (PACK_INDEX_INDEX (arg), val);
    1813              : 
    1814    534413885 :     case TYPE_ARGUMENT_PACK:
    1815    534413885 :     case NONTYPE_ARGUMENT_PACK:
    1816    534413885 :       return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
    1817              : 
    1818              :     case TREE_LIST:
    1819      1518570 :       for (; arg; arg = TREE_CHAIN (arg))
    1820       759288 :         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      3856377 :     case CONSTRUCTOR:
    1829      3856377 :       {
    1830      3856377 :         iterative_hash_template_arg (TREE_TYPE (arg), val);
    1831      4832322 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
    1832              :           {
    1833       431329 :             val = iterative_hash_template_arg (e.index, val);
    1834       431329 :             val = iterative_hash_template_arg (e.value, val);
    1835              :           }
    1836              :         return val;
    1837              :       }
    1838              : 
    1839      2871928 :     case PARM_DECL:
    1840      2871928 :       if (!DECL_ARTIFICIAL (arg))
    1841              :         {
    1842      2830589 :           val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
    1843      2830589 :           val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
    1844              :         }
    1845      2871928 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1846              : 
    1847    439876427 :     case TEMPLATE_DECL:
    1848    439876427 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
    1849      9785289 :         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        25640 :     case PTRMEM_CST:
    1856        25640 :       val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
    1857        25640 :       return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
    1858              : 
    1859     21355365 :     case TEMPLATE_PARM_INDEX:
    1860     21355365 :       val = iterative_hash_template_arg
    1861     21355365 :         (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
    1862     21355365 :       val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
    1863     21355365 :       return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
    1864              : 
    1865      2205380 :     case TRAIT_EXPR:
    1866      2205380 :       val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
    1867      2205380 :       val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
    1868      2205380 :       return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
    1869              : 
    1870      1577673 :     case BASELINK:
    1871      1577673 :       val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
    1872              :                                          val);
    1873      1577673 :       return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
    1874      1577673 :                                           val);
    1875              : 
    1876        35431 :     case MODOP_EXPR:
    1877        35431 :       val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
    1878        35431 :       code = TREE_CODE (TREE_OPERAND (arg, 1));
    1879        35431 :       val = iterative_hash_object (code, val);
    1880        35431 :       return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
    1881              : 
    1882          226 :     case LAMBDA_EXPR:
    1883              :       /* [temp.over.link] Two lambda-expressions are never considered
    1884              :          equivalent.
    1885              : 
    1886              :          So just hash the closure type.  */
    1887          226 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1888              : 
    1889      7911678 :     case CAST_EXPR:
    1890      7911678 :     case IMPLICIT_CONV_EXPR:
    1891      7911678 :     case STATIC_CAST_EXPR:
    1892      7911678 :     case REINTERPRET_CAST_EXPR:
    1893      7911678 :     case CONST_CAST_EXPR:
    1894      7911678 :     case DYNAMIC_CAST_EXPR:
    1895      7911678 :     case NEW_EXPR:
    1896      7911678 :       val = iterative_hash_template_arg (TREE_TYPE (arg), val);
    1897              :       /* Now hash operands as usual.  */
    1898      7911678 :       break;
    1899              : 
    1900    121664737 :     case CALL_EXPR:
    1901    121664737 :       {
    1902    121664737 :         tree fn = CALL_EXPR_FN (arg);
    1903    121664737 :         if (tree name = call_expr_dependent_name (arg))
    1904              :           {
    1905    116597851 :             if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
    1906     59578482 :               val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
    1907              :             fn = name;
    1908              :           }
    1909    121664737 :         val = iterative_hash_template_arg (fn, val);
    1910    121664737 :         call_expr_arg_iterator ai;
    1911    184513758 :         for (tree x = first_call_expr_arg (arg, &ai); x;
    1912     62849021 :              x = next_call_expr_arg (&ai))
    1913     62849021 :           val = iterative_hash_template_arg (x, val);
    1914    121664737 :         return val;
    1915              :       }
    1916              : 
    1917         2513 :     case REFLECT_EXPR:
    1918         2513 :       val = iterative_hash_hashval_t (REFLECT_EXPR_KIND (arg), val);
    1919         2513 :       if (REFLECT_EXPR_KIND (arg) == REFLECT_BASE)
    1920              :         {
    1921           43 :           tree binfo = REFLECT_EXPR_HANDLE (arg);
    1922           43 :           val = iterative_hash_template_arg (BINFO_TYPE (binfo), val);
    1923           43 :           val = iterative_hash_template_arg (direct_base_derived (binfo), val);
    1924           43 :           return val;
    1925              :         }
    1926              :       /* Now hash operands as usual.  */
    1927              :       break;
    1928              : 
    1929              :     default:
    1930              :       break;
    1931              :     }
    1932              : 
    1933  12631023406 :   char tclass = TREE_CODE_CLASS (code);
    1934  12631023406 :   switch (tclass)
    1935              :     {
    1936  11867122560 :     case tcc_type:
    1937  11867122560 :       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     43744988 :           tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
    1945     43744988 :           return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
    1946              :         }
    1947              : 
    1948  11823377572 :       switch (code)
    1949              :         {
    1950     64359234 :         case DECLTYPE_TYPE:
    1951     64359234 :           val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
    1952     64359234 :           break;
    1953              : 
    1954    345205250 :         case TYPENAME_TYPE:
    1955    345205250 :           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    300300356 :               tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
    1963    300300356 :               tree fullname = TYPENAME_TYPE_FULLNAME (arg);
    1964    300300356 :               val = iterative_hash_template_arg (context, val);
    1965    300300356 :               val = iterative_hash_template_arg (fullname, val);
    1966              :             }
    1967              :           break;
    1968              : 
    1969  11413813088 :         default:
    1970  11413813088 :           if (tree canonical = TYPE_CANONICAL (arg))
    1971  11238087152 :             val = iterative_hash_hashval_t (TYPE_HASH (canonical), val);
    1972    175725936 :           else if (tree ti = TYPE_TEMPLATE_INFO (arg))
    1973              :             {
    1974     32785699 :               val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
    1975     32785699 :               val = iterative_hash_template_arg (TI_ARGS (ti), val);
    1976              :             }
    1977              :           break;
    1978              :         }
    1979              : 
    1980              :       return val;
    1981              : 
    1982    704767864 :     case tcc_declaration:
    1983    704767864 :     case tcc_constant:
    1984    704767864 :       return iterative_hash_expr (arg, val);
    1985              : 
    1986     59132982 :     default:
    1987     59132982 :       gcc_assert (IS_EXPR_CODE_CLASS (tclass));
    1988    160918645 :       for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
    1989    101785663 :         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       619959 : reregister_specialization (tree spec, tree tinfo, tree new_spec)
    2002              : {
    2003       619959 :   spec_entry *entry;
    2004       619959 :   spec_entry elt;
    2005              : 
    2006       619959 :   elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
    2007       619959 :   elt.args = TI_ARGS (tinfo);
    2008              : 
    2009       619959 :   entry = decl_specializations->find (&elt);
    2010       619959 :   if (entry != NULL)
    2011              :     {
    2012       619959 :       gcc_assert (entry->spec == spec || entry->spec == new_spec);
    2013       619959 :       gcc_assert (new_spec != NULL_TREE);
    2014       619959 :       entry->spec = new_spec;
    2015              : 
    2016              :       /* We need to also remove SPEC from DECL_TEMPLATE_INSTANTIATIONS
    2017              :          if it was placed there.  */
    2018       619959 :       for (tree *inst = &DECL_TEMPLATE_INSTANTIATIONS (elt.tmpl);
    2019       766393 :            *inst; inst = &TREE_CHAIN (*inst))
    2020       146449 :         if (TREE_VALUE (*inst) == spec)
    2021              :           {
    2022           15 :             *inst = TREE_CHAIN (*inst);
    2023           15 :             break;
    2024              :           }
    2025              : 
    2026       619959 :       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    100339265 : register_local_specialization (tree spec, tree tmpl)
    2037              : {
    2038    100339265 :   gcc_assert (tmpl != spec);
    2039    100339265 :   local_specializations->put (tmpl, spec);
    2040    100339265 : }
    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     17652273 : explicit_class_specialization_p (tree type)
    2057              : {
    2058     17652273 :   if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
    2059              :     return false;
    2060       486482 :   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         5187 : inform_num_candidates (location_t loc, int num_candidates)
    2083              : {
    2084         5187 :   inform_n (loc,
    2085              :             num_candidates, "there is %i candidate", "there are %i candidates",
    2086              :             num_candidates);
    2087         5187 : }
    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      4306673 : 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      4306673 :   tree fns;
    2212      4306673 :   tree targs;
    2213      4306673 :   tree explicit_targs;
    2214      4306673 :   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      4306673 :   tree templates = NULL_TREE;
    2222      4306673 :   int header_count;
    2223      4306673 :   cp_binding_level *b;
    2224              : 
    2225      4306673 :   *targs_out = NULL_TREE;
    2226              : 
    2227      4306673 :   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      4306670 :   if (!VAR_P (decl)
    2234      2563017 :       && template_count && DECL_CLASS_SCOPE_P (decl)
    2235      5154049 :       && template_class_depth (DECL_CONTEXT (decl)) > 0)
    2236              :     {
    2237            3 :       gcc_assert (errorcount);
    2238            3 :       return error_mark_node;
    2239              :     }
    2240              : 
    2241      4306667 :   fns = TREE_OPERAND (template_id, 0);
    2242      4306667 :   explicit_targs = TREE_OPERAND (template_id, 1);
    2243              : 
    2244      4306667 :   if (fns == error_mark_node)
    2245              :     return error_mark_node;
    2246              : 
    2247              :   /* Check for baselinks.  */
    2248      4306667 :   if (BASELINK_P (fns))
    2249            0 :     fns = BASELINK_FUNCTIONS (fns);
    2250              : 
    2251      4306667 :   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      4306652 :   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      4306649 :   header_count = 0;
    2266      4306649 :   for (b = current_binding_level;
    2267      6596348 :        b->kind == sk_template_parms;
    2268      2289699 :        b = b->level_chain)
    2269      2289699 :     ++header_count;
    2270              : 
    2271      4306649 :   tree orig_fns = fns;
    2272      4306649 :   bool header_mismatch = false;
    2273              : 
    2274      4306649 :   if (variable_template_p (fns))
    2275              :     {
    2276      1743650 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
    2277      1743650 :       targs = coerce_template_parms (parms, explicit_targs, fns,
    2278              :                                      tf_warning_or_error);
    2279      1743650 :       if (targs != error_mark_node
    2280      1743650 :           && constraints_satisfied_p (fns, targs))
    2281      1743641 :         templates = tree_cons (targs, fns, templates);
    2282              :     }
    2283     17440088 :   else for (lkp_iterator iter (fns); iter; ++iter)
    2284              :     {
    2285     14877089 :       tree fn = *iter;
    2286              : 
    2287     14877089 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2288              :         {
    2289     14297560 :           tree decl_arg_types;
    2290     14297560 :           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     14297560 :           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     14297554 :           if (current_binding_level->kind == sk_template_parms
    2327       181520 :               && !current_binding_level->explicit_spec_p
    2328     14298601 :               && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
    2329         1047 :                   != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
    2330              :                                       (current_template_parms))))
    2331            3 :             continue;
    2332              : 
    2333              :           /* DECL might be a specialization of FN.  */
    2334     14297551 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2335     14297551 :           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     14297551 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
    2342              :             {
    2343       383764 :               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       767324 :               if (type_memfn_rqual (TREE_TYPE (decl))
    2349       383662 :                   != 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     14297411 :           decl_arg_types
    2358     14297411 :             = skip_artificial_parms_for (decl, decl_arg_types);
    2359     14297411 :           fn_arg_types
    2360     14297411 :             = 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     14297411 :           if (tsk == tsk_template)
    2371              :             {
    2372         1044 :               if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
    2373         1044 :                                         current_template_parms))
    2374            0 :                 continue;
    2375         1044 :               if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2376              :                                 TREE_TYPE (TREE_TYPE (fn))))
    2377            0 :                 continue;
    2378         1044 :               if (!compparms (fn_arg_types, decl_arg_types))
    2379            3 :                 continue;
    2380              : 
    2381         1041 :               tree freq = get_constraints (fn);
    2382         1041 :               tree dreq = get_constraints (decl);
    2383         1041 :               if (!freq != !dreq)
    2384            0 :                 continue;
    2385         1041 :               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         1012 :               candidates = tree_cons (NULL_TREE, fn, candidates);
    2397         1012 :               continue;
    2398         1012 :             }
    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     14296367 :           push_deferring_access_checks (dk_no_check);
    2405     14296367 :           targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
    2406     14296367 :           pop_deferring_access_checks ();
    2407              : 
    2408     14296367 :           if (!targs)
    2409              :             /* We cannot deduce template arguments that when used to
    2410              :                specialize TMPL will produce DECL.  */
    2411     12157544 :             continue;
    2412              : 
    2413      2138823 :           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      2138820 :           templates = tree_cons (targs, fn, templates);
    2420              :         }
    2421       579529 :       else if (need_member_template)
    2422              :         /* FN is an ordinary member function, and we need a
    2423              :            specialization of a member template.  */
    2424              :         ;
    2425       579483 :       else if (TREE_CODE (fn) != FUNCTION_DECL)
    2426              :         /* We can get IDENTIFIER_NODEs here in certain erroneous
    2427              :            cases.  */
    2428              :         ;
    2429       579483 :       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       569163 :       else if (DECL_ARTIFICIAL (fn))
    2434              :         /* Cannot specialize functions that are created implicitly.  */
    2435              :         ;
    2436              :       else
    2437              :         {
    2438       569006 :           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       569006 :           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       569000 :           if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2457              :                             TREE_TYPE (TREE_TYPE (fn))))
    2458              :             /* The return types differ.  */
    2459         1026 :             continue;
    2460              : 
    2461              :           /* Adjust the type of DECL in case FN is a static member.  */
    2462       567974 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2463       567974 :           if (DECL_STATIC_FUNCTION_P (fn)
    2464       567974 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2465          190 :             decl_arg_types = TREE_CHAIN (decl_arg_types);
    2466              : 
    2467       567974 :           if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
    2468              :                          decl_arg_types))
    2469       104590 :             continue;
    2470              : 
    2471       463384 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
    2472       926589 :               && (type_memfn_rqual (TREE_TYPE (decl))
    2473       463205 :                   != 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       463378 :           if (flag_concepts && !constraints_satisfied_p (fn))
    2479           21 :             continue;
    2480              : 
    2481              :           // Add the candidate.
    2482       463357 :           candidates = tree_cons (NULL_TREE, fn, candidates);
    2483              :         }
    2484              :     }
    2485              : 
    2486      4306640 :   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        20217 :       tree tmpl = most_specialized_instantiation (templates);
    2520        20217 :       if (tmpl != error_mark_node)
    2521              :         {
    2522        20214 :           templates = tmpl;
    2523        20214 :           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      4306649 :   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      4306649 :   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      3842073 :   else if ((templates && TREE_CHAIN (templates))
    2554      4306427 :            || (candidates && TREE_CHAIN (candidates))
    2555      8612857 :            || (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      4306427 :   if (candidates)
    2567              :     {
    2568       464357 :       tree fn = TREE_VALUE (candidates);
    2569       464357 :       *targs_out = copy_node (DECL_TI_ARGS (fn));
    2570              : 
    2571              :       /* Propagate the candidate's constraints to the declaration.  */
    2572       464357 :       if (tsk != tsk_template)
    2573       463345 :         set_constraints (decl, get_constraints (fn));
    2574              : 
    2575              :       /* DECL is a re-declaration or partial instantiation of a template
    2576              :          function.  */
    2577       464357 :       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       463345 :       return DECL_TI_TEMPLATE (fn);
    2582              :     }
    2583              : 
    2584              :   /* It was a specialization of a template.  */
    2585      3842070 :   tree tmpl = TREE_VALUE (templates);
    2586      3842070 :   *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
    2587              : 
    2588              :   /* Propagate the template's constraints to the declaration.  */
    2589      3842070 :   if (tsk != tsk_template)
    2590      2908931 :     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       200605 : copy_default_args_to_explicit_spec_1 (tree spec_types,
    2601              :                                       tree tmpl_types)
    2602              : {
    2603       200605 :   tree new_spec_types;
    2604              : 
    2605       200605 :   if (!spec_types)
    2606              :     return NULL_TREE;
    2607              : 
    2608       200605 :   if (spec_types == void_list_node)
    2609              :     return void_list_node;
    2610              : 
    2611              :   /* Substitute into the rest of the list.  */
    2612       120014 :   new_spec_types =
    2613       120014 :     copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
    2614       120014 :                                           TREE_CHAIN (tmpl_types));
    2615              : 
    2616              :   /* Add the default argument for this parameter.  */
    2617       240028 :   return hash_tree_cons (TREE_PURPOSE (tmpl_types),
    2618       120014 :                          TREE_VALUE (spec_types),
    2619       120014 :                          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       619947 : copy_default_args_to_explicit_spec (tree decl)
    2635              : {
    2636       619947 :   tree tmpl;
    2637       619947 :   tree spec_types;
    2638       619947 :   tree tmpl_types;
    2639       619947 :   tree new_spec_types;
    2640       619947 :   tree old_type;
    2641       619947 :   tree new_type;
    2642       619947 :   tree t;
    2643       619947 :   tree object_type = NULL_TREE;
    2644       619947 :   tree in_charge = NULL_TREE;
    2645       619947 :   tree vtt = NULL_TREE;
    2646              : 
    2647              :   /* See if there's anything we need to do.  */
    2648       619947 :   tmpl = DECL_TI_TEMPLATE (decl);
    2649       619947 :   tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
    2650      2431855 :   for (t = tmpl_types; t; t = TREE_CHAIN (t))
    2651      1892499 :     if (TREE_PURPOSE (t))
    2652              :       break;
    2653       619947 :   if (!t)
    2654              :     return;
    2655              : 
    2656        80591 :   old_type = TREE_TYPE (decl);
    2657        80591 :   spec_types = TYPE_ARG_TYPES (old_type);
    2658              : 
    2659        80591 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2660              :     {
    2661              :       /* Remove the this pointer, but remember the object's type for
    2662              :          CV quals.  */
    2663        80591 :       object_type = TREE_TYPE (TREE_VALUE (spec_types));
    2664        80591 :       spec_types = TREE_CHAIN (spec_types);
    2665        80591 :       tmpl_types = TREE_CHAIN (tmpl_types);
    2666              : 
    2667        80591 :       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        80591 :       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        80591 :   new_spec_types =
    2683        80591 :     copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
    2684              : 
    2685              :   /* Compute the new FUNCTION_TYPE.  */
    2686        80591 :   if (object_type)
    2687              :     {
    2688        80591 :       if (vtt)
    2689            0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
    2690            0 :                                          TREE_VALUE (vtt),
    2691              :                                          new_spec_types);
    2692              : 
    2693        80591 :       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        80591 :       new_type = build_method_type_directly (object_type,
    2700        80591 :                                              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        80591 :   new_type = cp_build_type_attribute_variant (new_type,
    2706        80591 :                                               TYPE_ATTRIBUTES (old_type));
    2707        80591 :   new_type = cxx_copy_lang_qualifiers (new_type, old_type);
    2708              : 
    2709        80591 :   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     33654567 : num_template_headers_for_class (tree ctype)
    2717              : {
    2718     33654567 :   int num_templates = 0;
    2719              : 
    2720     51077302 :   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     23915456 :       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     17652273 :       if (explicit_class_specialization_p (ctype))
    2738              :         break;
    2739     17422735 :       if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
    2740     17116350 :         ++num_templates;
    2741              : 
    2742     17422735 :       ctype = TYPE_CONTEXT (ctype);
    2743              :     }
    2744              : 
    2745     33654567 :   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      4595775 : check_template_variable (tree decl)
    2753              : {
    2754      4595775 :   tree ctx = CP_DECL_CONTEXT (decl);
    2755      4595775 :   int wanted = num_template_headers_for_class (ctx);
    2756      9191547 :   if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
    2757      9191538 :       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
    2758              :     {
    2759      4272635 :       if (cxx_dialect < cxx14)
    2760         4014 :         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      4272635 :       ++wanted;
    2766              :     }
    2767      4595775 :   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      4595775 : }
    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     15245623 : check_unqualified_spec_or_inst (tree t, location_t loc)
    2794              : {
    2795     15245623 :   tree tmpl = most_general_template (t);
    2796     30491246 :   if (DECL_NAMESPACE_SCOPE_P (tmpl)
    2797     29968064 :       && !is_nested_namespace (current_namespace,
    2798     14722441 :                                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     15245623 : }
    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      1430325 : warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
    2819              : {
    2820      1430325 :   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      1430325 :   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      1430325 :   auto_vec<const char *> mismatches;
    2834      1430325 :   unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
    2835              :                                                  blacklist, mismatches);
    2836              : 
    2837      1430325 :   if (!nattrs)
    2838      1430298 :     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      1430325 : }
    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    242781811 : check_explicit_specialization (tree declarator,
    2893              :                                tree decl,
    2894              :                                int template_count,
    2895              :                                int flags,
    2896              :                                tree attrlist)
    2897              : {
    2898    242781811 :   int have_def = flags & 2;
    2899    242781811 :   int is_friend = flags & 4;
    2900    242781811 :   bool is_concept = flags & 8;
    2901    242781811 :   int specialization = 0;
    2902    242781811 :   int explicit_instantiation = 0;
    2903    242781811 :   int member_specialization = 0;
    2904    242781811 :   tree ctype = DECL_CLASS_CONTEXT (decl);
    2905    242781811 :   tree dname = DECL_NAME (decl);
    2906    242781811 :   tmpl_spec_kind tsk;
    2907              : 
    2908    242781811 :   if (is_friend)
    2909              :     {
    2910        76022 :       if (!processing_specialization)
    2911              :         tsk = tsk_none;
    2912              :       else
    2913              :         tsk = tsk_excessive_parms;
    2914              :     }
    2915              :   else
    2916    242705789 :     tsk = current_tmpl_spec_kind (template_count);
    2917              : 
    2918    242705789 :   switch (tsk)
    2919              :     {
    2920    189011516 :     case tsk_none:
    2921    189011516 :       if (processing_specialization && !VAR_P (decl))
    2922              :         {
    2923       442316 :           specialization = 1;
    2924       442316 :           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2925              :         }
    2926    188569200 :       else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
    2927    188569200 :                || (DECL_LANG_SPECIFIC (decl)
    2928    127520344 :                    && DECL_IMPLICIT_INSTANTIATION (decl)))
    2929              :         {
    2930        76034 :           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      1915593 :     case tsk_expl_inst:
    2960      1915593 :       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       912093 :     case tsk_expl_spec:
    2979       912093 :       if (is_concept)
    2980            0 :         error ("explicit specialization declared %<concept%>");
    2981              : 
    2982       912093 :       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       912081 :       SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2988       912081 :       if (ctype)
    2989              :         member_specialization = 1;
    2990              :       else
    2991              :         specialization = 1;
    2992              :       break;
    2993              : 
    2994     50942609 :     case tsk_template:
    2995     50942609 :       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       933166 :           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       933154 :           else if (variable_template_p (TREE_OPERAND (declarator, 0)))
    3005              :             {
    3006              :               /* Partial specialization of variable template.  */
    3007       933139 :               SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    3008       933139 :               specialization = 1;
    3009       933139 :               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       933139 :         ok:;
    3021              :         }
    3022              : 
    3023     50942582 :       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       442316 :   if ((specialization || member_specialization)
    3041              :       /* This doesn't apply to variable templates.  */
    3042     52373001 :       && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
    3043              :     {
    3044       621053 :       tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
    3045      2636953 :       for (; t; t = TREE_CHAIN (t))
    3046      2015903 :         if (TREE_PURPOSE (t))
    3047              :           {
    3048            3 :             permerror (input_location,
    3049              :                        "default argument specified in explicit specialization");
    3050            3 :             break;
    3051              :           }
    3052              :     }
    3053              : 
    3054    242781772 :   if (specialization || member_specialization || explicit_instantiation)
    3055              :     {
    3056      4280178 :       tree tmpl = NULL_TREE;
    3057      4280178 :       tree targs = NULL_TREE;
    3058      4280178 :       bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
    3059      4280178 :       bool found_hidden = false;
    3060              : 
    3061              :       /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
    3062      4280178 :       if (!was_template_id)
    3063              :         {
    3064      1643854 :           tree fns;
    3065              : 
    3066      1643854 :           gcc_assert (identifier_p (declarator));
    3067      1643854 :           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       796446 :               gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
    3074              : 
    3075              :               /* Find the namespace binding, using the declaration
    3076              :                  context.  */
    3077       796446 :               fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
    3078              :                                            LOOK_want::NORMAL, true);
    3079       796446 :               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       796446 :               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      1643854 :           declarator = lookup_template_function (fns, NULL_TREE);
    3098              :         }
    3099              : 
    3100      4280178 :       if (declarator == error_mark_node)
    3101      1916714 :         return error_mark_node;
    3102              : 
    3103      4280156 :       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      4280156 :       else if (ctype != NULL_TREE
    3130      4280156 :                && (identifier_p (TREE_OPERAND (declarator, 0))))
    3131              :         {
    3132              :           // We'll match variable templates in start_decl.
    3133       847408 :           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       847324 :           tree name = TREE_OPERAND (declarator, 0);
    3139              : 
    3140       847324 :           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       847324 :           tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
    3166              :                                         ? conv_op_identifier : name);
    3167              : 
    3168       847324 :           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       847324 :             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      4280072 :       tmpl = determine_specialization (declarator, decl,
    3186              :                                        &targs,
    3187              :                                        member_specialization,
    3188              :                                        template_count,
    3189              :                                        tsk);
    3190              : 
    3191      4280072 :       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      4279835 :           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      4279835 :           if (!ctype && !is_friend
    3208      4279835 :               && CP_DECL_CONTEXT (decl) == current_namespace)
    3209      3301347 :             check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
    3210              : 
    3211      4279835 :           tree gen_tmpl = most_general_template (tmpl);
    3212              : 
    3213      4279835 :           if (explicit_instantiation)
    3214              :             {
    3215              :               /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
    3216              :                  is done by do_decl_instantiation later.  */
    3217              : 
    3218      1915359 :               int arg_depth = TMPL_ARGS_DEPTH (targs);
    3219      1915359 :               int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
    3220              : 
    3221      1915359 :               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       382908 :                   int i;
    3229       382908 :                   tree new_targs;
    3230              : 
    3231       382908 :                   new_targs = make_tree_vec (parm_depth);
    3232       765816 :                   for (i = arg_depth - parm_depth; i < arg_depth; ++i)
    3233       765816 :                     TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
    3234       382908 :                       = TREE_VEC_ELT (targs, i);
    3235       382908 :                   targs = new_targs;
    3236              :                 }
    3237              : 
    3238      1915359 :               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      2364476 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    3245       620959 :               && DECL_STATIC_FUNCTION_P (tmpl)
    3246      2365582 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    3247         1097 :             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      2364476 :           if (tsk == tsk_template && !was_template_id)
    3253              :             {
    3254         1012 :               tree result = DECL_TEMPLATE_RESULT (tmpl);
    3255         1012 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    3256         1012 :               DECL_INITIAL (result) = NULL_TREE;
    3257         1012 :               if (have_def)
    3258              :                 {
    3259         1000 :                   tree parm;
    3260         1000 :                   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    3261         2000 :                   DECL_SOURCE_LOCATION (result)
    3262         1000 :                     = DECL_SOURCE_LOCATION (decl);
    3263              :                   /* We want to use the argument list specified in the
    3264              :                      definition, not in the original declaration.  */
    3265         1000 :                   DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
    3266         2068 :                   for (parm = DECL_ARGUMENTS (result); parm;
    3267         1068 :                        parm = DECL_CHAIN (parm))
    3268         1068 :                     DECL_CONTEXT (parm) = result;
    3269              :                 }
    3270         1012 :               decl = register_specialization (tmpl, gen_tmpl, targs,
    3271              :                                               is_friend, 0);
    3272         1012 :               if (flag_contracts)
    3273           31 :                 remove_fn_contract_specifiers (result);
    3274         1012 :               return decl;
    3275              :             }
    3276              : 
    3277              :           /* Set up the DECL_TEMPLATE_INFO for DECL.  */
    3278      2363464 :           DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
    3279              : 
    3280      2363464 :           if (was_template_id)
    3281      1820237 :             TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
    3282              : 
    3283              :           /* Inherit default function arguments from the template
    3284              :              DECL is specializing.  */
    3285      2363464 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    3286       619947 :             copy_default_args_to_explicit_spec (decl);
    3287              : 
    3288              :           /* This specialization has the same protection as the
    3289              :              template it specializes.  */
    3290      2363464 :           TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
    3291      2363464 :           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      2363464 :           if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
    3309              :             {
    3310       101632 :               tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
    3311       101632 :               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       101632 :               TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
    3316       101632 :               if (! TREE_PUBLIC (decl))
    3317              :                 {
    3318           37 :                   DECL_INTERFACE_KNOWN (decl) = 1;
    3319           37 :                   DECL_NOT_REALLY_EXTERN (decl) = 1;
    3320              :                 }
    3321       101632 :               DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
    3322       101632 :               if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
    3323              :                 {
    3324        11732 :                   DECL_VISIBILITY_SPECIFIED (decl) = 1;
    3325        11732 :                   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      2363464 :           if (DECL_NAMESPACE_SCOPE_P (decl))
    3341      1920845 :             DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
    3342              : 
    3343      2363464 :           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        76008 :             SET_DECL_IMPLICIT_INSTANTIATION (decl);
    3348      2287456 :           else if (TREE_CODE (decl) == FUNCTION_DECL)
    3349              :             /* A specialization is not necessarily COMDAT.  */
    3350       543939 :             DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
    3351       543939 :                                   && DECL_DECLARED_INLINE_P (decl));
    3352      1743517 :           else if (VAR_P (decl))
    3353      1743517 :             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      2363464 :           if (!processing_template_decl)
    3359              :             {
    3360      1430325 :               warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
    3361              : 
    3362      1430325 :               decl = register_specialization (decl, gen_tmpl, targs,
    3363              :                                               is_friend, 0);
    3364              :             }
    3365              : 
    3366      2363464 :           if (flag_contracts
    3367       285419 :               && decl != error_mark_node
    3368      2648875 :               && DECL_TEMPLATE_SPECIALIZATION (decl))
    3369       276709 :             remove_fn_contract_specifiers (decl);
    3370              : 
    3371              :           /* A 'structor should already have clones.  */
    3372      2983390 :           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    168810178 : comp_template_parms (const_tree parms1, const_tree parms2)
    3389              : {
    3390    168810178 :   if (parms1 == parms2)
    3391              :     return 1;
    3392              : 
    3393    153432709 :   tree t1 = TREE_VALUE (parms1);
    3394    153432709 :   tree t2 = TREE_VALUE (parms2);
    3395    153432709 :   int i;
    3396              : 
    3397    153432709 :   gcc_assert (TREE_CODE (t1) == TREE_VEC);
    3398    153432709 :   gcc_assert (TREE_CODE (t2) == TREE_VEC);
    3399              : 
    3400    153432709 :   if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
    3401              :     return 0;
    3402              : 
    3403    157058753 :   for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
    3404              :     {
    3405    106833486 :       tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
    3406    106833486 :       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    106833486 :       if (error_operand_p (parm1) || error_operand_p (parm2))
    3411              :         return 1;
    3412              : 
    3413    106833462 :       if (TREE_CODE (parm1) != TREE_CODE (parm2))
    3414              :         return 0;
    3415              : 
    3416    193743616 :       if (TREE_CODE (parm1) == TYPE_DECL
    3417    105770074 :           && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1))
    3418    104885589 :               == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2))))
    3419     87973542 :         continue;
    3420     17796532 :       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   1376728371 : template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
    3432              : {
    3433   1376728371 :   tree req1 = TREE_TYPE (parm1);
    3434   1376728371 :   tree req2 = TREE_TYPE (parm2);
    3435   1376728371 :   if (!req1 != !req2)
    3436              :     return false;
    3437   1375034502 :   if (req1)
    3438       967609 :     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   1558624355 : template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
    3446              : {
    3447   1558624355 :   tree decl1 = TREE_VALUE (parm1);
    3448   1558624355 :   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   1558624355 :   if (error_operand_p (decl1) || error_operand_p (decl2))
    3453              :     return true;
    3454              : 
    3455              :   /* ... they declare parameters of the same kind.  */
    3456   1558624352 :   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   1499978719 :   if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1)
    3463   1499978719 :       != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2))
    3464              :     return false;
    3465              : 
    3466              :   /* ... if either declares a pack, they both do.  */
    3467   1499962465 :   if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
    3468              :     return false;
    3469              : 
    3470   1372376577 :   if (TREE_CODE (decl1) == PARM_DECL)
    3471              :     {
    3472              :       /* ... if they declare non-type parameters, the types are equivalent.  */
    3473      2823858 :       if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
    3474              :         return false;
    3475              :     }
    3476   1369552719 :   else if (TREE_CODE (decl2) == TEMPLATE_DECL)
    3477              :     {
    3478              :       /* ... if they declare template template parameters, their template
    3479              :          parameter lists are equivalent.  */
    3480        47190 :       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   1372265235 :   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   2679709711 : template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
    3502              : {
    3503   2679709711 :   if (parms1 == parms2)
    3504              :     return true;
    3505              : 
    3506   2679709711 :   tree list1 = TREE_VALUE (parms1);
    3507   2679709711 :   tree list2 = TREE_VALUE (parms2);
    3508              : 
    3509   2679709711 :   if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
    3510              :     return 0;
    3511              : 
    3512   2369731979 :   for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
    3513              :     {
    3514   1558624355 :       tree parm1 = TREE_VEC_ELT (list1, i);
    3515   1558624355 :       tree parm2 = TREE_VEC_ELT (list2, i);
    3516   1558624355 :       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    874599477 : template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
    3527              : {
    3528    874599477 :   tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
    3529    874599477 :   tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
    3530    874599477 :   if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
    3531              :     return false;
    3532    733147523 :   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   2679709711 : template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
    3546              : {
    3547   2679709711 :   tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
    3548   2679709711 :   tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
    3549              : 
    3550              :   /* ... have the same number of template parameters, and their
    3551              :      corresponding parameters are equivalent.  */
    3552   2679709711 :   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    811107624 :   return template_requirements_equivalent_p (parms1, parms2);
    3558              : }
    3559              : 
    3560              : /* Determine whether PARM is a parameter pack.  */
    3561              : 
    3562              : bool
    3563   7867263793 : template_parameter_pack_p (const_tree parm)
    3564              : {
    3565              :   /* Determine if we have a non-type template parameter pack.  */
    3566   7867263793 :   if (TREE_CODE (parm) == PARM_DECL)
    3567    570003661 :     return (DECL_TEMPLATE_PARM_P (parm)
    3568    570003661 :             && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
    3569   7297260132 :   if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
    3570      1283929 :     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   7295976203 :   if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
    3575   7014592193 :     parm = TREE_TYPE (parm);
    3576              : 
    3577              :   /* Otherwise it must be a type template parameter.  */
    3578   7295976203 :   return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    3579   7295976203 :            || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    3580   7295976203 :           && TEMPLATE_TYPE_PARAMETER_PACK (parm));
    3581              : }
    3582              : 
    3583              : /* Determine if T is a function parameter pack.  */
    3584              : 
    3585              : bool
    3586      1820940 : function_parameter_pack_p (const_tree t)
    3587              : {
    3588      1820940 :   if (t && TREE_CODE (t) == PARM_DECL)
    3589      1820940 :     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      1717692 : get_function_template_decl (const_tree primary_func_tmpl_inst)
    3598              : {
    3599      1717692 :   if (! primary_func_tmpl_inst
    3600      1717692 :       || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
    3601      3435384 :       || ! primary_template_specialization_p (primary_func_tmpl_inst))
    3602      1243834 :     return NULL;
    3603              : 
    3604       473858 :   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       640988 : function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
    3612              : {
    3613       640988 :   if (DECL_ARTIFICIAL (param_decl)
    3614       640988 :       || !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       640988 :   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      1424781 : 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      1424781 :   char numbuf[NUMBUF_LEN];
    3654      1424781 :   char* newname;
    3655      1424781 :   int newname_len;
    3656              : 
    3657      1424781 :   if (name == NULL_TREE)
    3658              :     return name;
    3659      1418970 :   snprintf (numbuf, NUMBUF_LEN, "%i", i);
    3660      1418970 :   newname_len = IDENTIFIER_LENGTH (name)
    3661      1418970 :                 + strlen (numbuf) + 2;
    3662      1418970 :   newname = (char*)alloca (newname_len);
    3663      1418970 :   snprintf (newname, newname_len,
    3664      1418970 :             "%s#%i", IDENTIFIER_POINTER (name), i);
    3665      1418970 :   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    175591362 : primary_template_specialization_p (const_tree t)
    3673              : {
    3674    175591362 :   if (!t)
    3675              :     return false;
    3676              : 
    3677    175591362 :   if (VAR_OR_FUNCTION_DECL_P (t))
    3678     85928918 :     return (DECL_LANG_SPECIFIC (t)
    3679     85928808 :             && DECL_USE_TEMPLATE (t)
    3680     80422625 :             && DECL_TEMPLATE_INFO (t)
    3681    166351543 :             && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
    3682     89662444 :   else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    3683     89662379 :     return (CLASSTYPE_TEMPLATE_INFO (t)
    3684     89662224 :             && CLASSTYPE_USE_TEMPLATE (t)
    3685    179324603 :             && 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     62513105 : template_template_parameter_p (const_tree parm)
    3695              : {
    3696     62513105 :   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   2406362590 : template_type_parameter_p (const_tree parm)
    3704              : {
    3705   2406362590 :   return (parm
    3706   2406362590 :           && (TREE_CODE (parm) == TYPE_DECL
    3707   1189100221 :               || TREE_CODE (parm) == TEMPLATE_DECL)
    3708   3623805389 :           && 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    192765612 : get_primary_template_innermost_parameters (const_tree t)
    3716              : {
    3717    192765612 :   tree parms = NULL, template_info = NULL;
    3718              : 
    3719    192765612 :   if ((template_info = get_template_info (t))
    3720    192765612 :       && primary_template_specialization_p (t))
    3721     89600089 :     parms = INNERMOST_TEMPLATE_PARMS
    3722              :         (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
    3723              : 
    3724    192765612 :   return parms;
    3725              : }
    3726              : 
    3727              : /* Returns the template arguments of T if T is a template instantiation,
    3728              :    NULL otherwise.  */
    3729              : 
    3730              : tree
    3731     43390032 : get_template_innermost_arguments (const_tree t)
    3732              : {
    3733     43390032 :   tree args = NULL, template_info = NULL;
    3734              : 
    3735     43390032 :   if ((template_info = get_template_info (t))
    3736     86780064 :       && TI_ARGS (template_info))
    3737     43390032 :     args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
    3738              : 
    3739     43390032 :   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     66847986 : get_template_argument_pack_elems (const_tree t)
    3747              : {
    3748     66847986 :   if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
    3749     66847986 :       && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
    3750              :     return NULL;
    3751              : 
    3752      5348669 :   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     11437506 : argument_pack_select_arg (tree t)
    3760              : {
    3761     11437506 :   tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
    3762     11437506 :   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     11437506 :   if (PACK_EXPANSION_P (arg))
    3773              :     {
    3774              :       /* Make sure we aren't throwing away arg info.  */
    3775       251800 :       gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
    3776       251800 :       arg = PACK_EXPANSION_PATTERN (arg);
    3777              :     }
    3778              : 
    3779     11437506 :   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       892664 : preserve_args (tree args, bool cow_p = true)
    3788              : {
    3789       892664 :   if (!args)
    3790              :     return NULL_TREE;
    3791              : 
    3792      2339156 :   for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    3793              :     {
    3794      1446494 :       tree t = TREE_VEC_ELT (args, i);
    3795      1446494 :       tree r;
    3796      1446494 :       if (!t)
    3797              :         r = NULL_TREE;
    3798      1446305 :       else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
    3799           74 :         r = argument_pack_select_arg (t);
    3800      1446231 :       else if (TREE_CODE (t) == TREE_VEC)
    3801       322000 :         r = preserve_args (t, cow_p);
    3802              :       else
    3803              :         r = t;
    3804      1446305 :       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    513583997 : builtin_pack_fn_p (tree fn)
    3823              : {
    3824    513583997 :   if (!fn
    3825    513489315 :       || TREE_CODE (fn) != FUNCTION_DECL
    3826    741907005 :       || !DECL_IS_UNDECLARED_BUILTIN (fn))
    3827              :     return false;
    3828              : 
    3829      8517178 :   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    355071234 : builtin_pack_call_p (tree call)
    3840              : {
    3841    355071234 :   if (TREE_CODE (call) != CALL_EXPR)
    3842              :     return false;
    3843    262187775 :   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        99736 : expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
    3850              :                      tree in_decl)
    3851              : {
    3852        99736 :   tree ohi = CALL_EXPR_ARG (call, 0);
    3853        99736 :   tree hi = tsubst_expr (ohi, args, complain, in_decl);
    3854              : 
    3855        99736 :   if (instantiation_dependent_expression_p (hi))
    3856              :     {
    3857        97631 :       if (hi != ohi)
    3858              :         {
    3859        97631 :           call = copy_node (call);
    3860        97631 :           CALL_EXPR_ARG (call, 0) = hi;
    3861              :         }
    3862        97631 :       tree ex = make_pack_expansion (call, complain);
    3863        97631 :       tree vec = make_tree_vec (1);
    3864        97631 :       TREE_VEC_ELT (vec, 0) = ex;
    3865        97631 :       return vec;
    3866              :     }
    3867              :   else
    3868              :     {
    3869         2105 :       hi = instantiate_non_dependent_expr (hi, complain);
    3870         2105 :       hi = cxx_constant_value (hi, complain);
    3871         2105 :       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         2105 :       int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
    3877              : 
    3878         2105 :       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         2093 :       tree vec = make_tree_vec (len);
    3888              : 
    3889        10048 :       for (int i = 0; i < len; ++i)
    3890         7955 :         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        99736 : expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
    3901              :                           tree in_decl)
    3902              : {
    3903        99736 :   if (!builtin_pack_call_p (call))
    3904              :     return NULL_TREE;
    3905              : 
    3906        99736 :   tree fn = CALL_EXPR_FN (call);
    3907              : 
    3908        99736 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3909        99736 :     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   7563286412 : has_extra_args_mechanism_p (const_tree t)
    3919              : {
    3920   7563286412 :   return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS  */
    3921   7530033054 :           || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS  */
    3922   7526861892 :           || (TREE_CODE (t) == IF_STMT
    3923      1169092 :               && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS  */
    3924  15089685490 :           || 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        98813 : tree_extra_args (tree t)
    3931              : {
    3932        98813 :   gcc_checking_assert (has_extra_args_mechanism_p (t));
    3933              : 
    3934        98813 :   if (PACK_EXPANSION_P (t))
    3935         2587 :     return PACK_EXPANSION_EXTRA_ARGS (t);
    3936        96226 :   else if (TREE_CODE (t) == REQUIRES_EXPR)
    3937          767 :     return REQUIRES_EXPR_EXTRA_ARGS (t);
    3938        95459 :   else if (TREE_CODE (t) == IF_STMT
    3939        95459 :            && IF_STMT_CONSTEXPR_P (t))
    3940        94880 :     return IF_STMT_EXTRA_ARGS (t);
    3941          579 :   else if (TREE_CODE (t) == LAMBDA_EXPR)
    3942          579 :     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   2111053476 : 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   7755365459 : find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
    3967              : {
    3968   7755365459 :   tree t = *tp;
    3969   7755365459 :   struct find_parameter_pack_data* ppd =
    3970              :     (struct find_parameter_pack_data*)data;
    3971   7755365459 :   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   7755365459 :   if (TYPE_P (t) && typedef_variant_p (t))
    3981              :     {
    3982              :       /* But do look at arguments for an alias template.  */
    3983    197257880 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    3984    149236020 :         cp_walk_tree (&TI_ARGS (tinfo),
    3985              :                       &find_parameter_packs_r,
    3986              :                       ppd, ppd->visited);
    3987    197257880 :       *walk_subtrees = 0;
    3988    197257880 :       return NULL_TREE;
    3989              :     }
    3990              : 
    3991              :   /* Identify whether this is a parameter pack or not.  */
    3992   7558107579 :   switch (TREE_CODE (t))
    3993              :     {
    3994     71996603 :     case TEMPLATE_PARM_INDEX:
    3995     71996603 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    3996              :         parameter_pack_p = true;
    3997              :       break;
    3998              : 
    3999   1219506236 :     case TEMPLATE_TYPE_PARM:
    4000   1219506236 :       t = TYPE_MAIN_VARIANT (t);
    4001              :       /* FALLTHRU */
    4002   1220362873 :     case TEMPLATE_TEMPLATE_PARM:
    4003   1220362873 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    4004              :         parameter_pack_p = true;
    4005              :       break;
    4006              : 
    4007    443250858 :     case FIELD_DECL:
    4008    443250858 :     case PARM_DECL:
    4009    443250858 :       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      3477368 :           *walk_subtrees = 0;
    4014      3477368 :           parameter_pack_p = true;
    4015              :         }
    4016              :       break;
    4017              : 
    4018    200662571 :     case VAR_DECL:
    4019    200662571 :       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         4365 :           *walk_subtrees = 0;
    4024         4365 :           parameter_pack_p = true;
    4025              :         }
    4026    200658206 :       else if (variable_template_specialization_p (t))
    4027              :         {
    4028       198355 :           cp_walk_tree (&DECL_TI_ARGS (t),
    4029              :                         find_parameter_packs_r,
    4030              :                         ppd, ppd->visited);
    4031       198355 :           *walk_subtrees = 0;
    4032              :         }
    4033              :       break;
    4034              : 
    4035    261988300 :     case CALL_EXPR:
    4036    261988300 :       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      3680088 :   if (parameter_pack_p)
    4049              :     {
    4050              :       /* Add this parameter pack to the list.  */
    4051     66965149 :       *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
    4052              :     }
    4053              : 
    4054   7558107579 :   if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
    4055      5067907 :     ppd->found_extra_args_tree_p = true;
    4056              : 
    4057   7558107579 :   if (TYPE_P (t))
    4058   2355516215 :     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   7558107579 :   switch (TREE_CODE (t))
    4064              :     {
    4065       414127 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    4066              :       /* Check the template itself.  */
    4067       414127 :       cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
    4068              :                     &find_parameter_packs_r, ppd, ppd->visited);
    4069       414127 :       return NULL_TREE;
    4070              : 
    4071     71996603 :     case TEMPLATE_PARM_INDEX:
    4072     71996603 :       if (parameter_pack_p)
    4073      3080051 :         WALK_SUBTREE (TREE_TYPE (t));
    4074              :       return NULL_TREE;
    4075              : 
    4076      7357595 :     case DECL_EXPR:
    4077      7357595 :       {
    4078      7357595 :         tree decl = DECL_EXPR_DECL (t);
    4079              :         /* Ignore the declaration of a capture proxy for a parameter pack.  */
    4080      7357595 :         if (is_capture_proxy (decl))
    4081      2301464 :           *walk_subtrees = 0;
    4082      7357595 :         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        84037 :           cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
    4086              :                         &find_parameter_packs_r, ppd, ppd->visited);
    4087              :         return NULL_TREE;
    4088              :       }
    4089              : 
    4090    221688029 :     case TEMPLATE_DECL:
    4091    221688029 :       if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
    4092              :         return NULL_TREE;
    4093         4067 :       cp_walk_tree (&TREE_TYPE (t),
    4094              :                     &find_parameter_packs_r, ppd, ppd->visited);
    4095         4067 :       return NULL_TREE;
    4096              : 
    4097     33248184 :     case TYPE_PACK_EXPANSION:
    4098     33248184 :     case EXPR_PACK_EXPANSION:
    4099     33248184 :       *walk_subtrees = 0;
    4100     33248184 :       return NULL_TREE;
    4101              : 
    4102        11942 :     case PACK_INDEX_TYPE:
    4103        11942 :     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        11942 :       cp_walk_tree (&PACK_INDEX_INDEX (t), &find_parameter_packs_r, ppd,
    4107              :                     ppd->visited);
    4108        11942 :       *walk_subtrees = 0;
    4109        11942 :       return NULL_TREE;
    4110              : 
    4111     36249403 :     case INTEGER_TYPE:
    4112     36249403 :       cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
    4113              :                     ppd, ppd->visited);
    4114     36249403 :       *walk_subtrees = 0;
    4115     36249403 :       return NULL_TREE;
    4116              : 
    4117    232887973 :     case IDENTIFIER_NODE:
    4118    232887973 :       cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
    4119              :                     ppd->visited);
    4120    232887973 :       *walk_subtrees = 0;
    4121    232887973 :       return NULL_TREE;
    4122              : 
    4123      1625225 :     case LAMBDA_EXPR:
    4124      1625225 :       {
    4125              :         /* Since we defer implicit capture, look in the parms and body.  */
    4126      1625225 :         tree fn = lambda_function (t);
    4127      1625225 :         cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
    4128              :                       ppd->visited);
    4129      1625225 :         cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
    4130              :                       ppd->visited);
    4131      1625225 :         return NULL_TREE;
    4132              :       }
    4133              : 
    4134      9377525 :     case DECLTYPE_TYPE:
    4135      9377525 :       cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
    4136              :                     ppd, ppd->visited);
    4137      9377525 :       *walk_subtrees = 0;
    4138      9377525 :       return NULL_TREE;
    4139              : 
    4140       933301 :     case IF_STMT:
    4141       933301 :       cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
    4142              :                     ppd, ppd->visited);
    4143       933301 :       cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
    4144              :                     ppd, ppd->visited);
    4145       933301 :       cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
    4146              :                     ppd, ppd->visited);
    4147              :       /* Don't walk into IF_STMT_EXTRA_ARGS.  */
    4148       933301 :       *walk_subtrees = 0;
    4149       933301 :       return NULL_TREE;
    4150              : 
    4151           83 :     case TAG_DEFN:
    4152           83 :       t = TREE_TYPE (t);
    4153           83 :       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           45 :           if (TYPE_BINFO (t))
    4158           51 :             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          123 :         for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
    4165           85 :           cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
    4166              :                         &find_parameter_packs_r,
    4167              :                         ppd, ppd->visited);
    4168              :       return NULL_TREE;
    4169              : 
    4170      6301565 :     case FUNCTION_TYPE:
    4171      6301565 :     case METHOD_TYPE:
    4172      6301565 :       WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
    4173      6301565 :       break;
    4174              : 
    4175              :     default:
    4176              :       return NULL_TREE;
    4177              :     }
    4178              : 
    4179              : #undef WALK_SUBTREE
    4180              : 
    4181      6301565 :   return NULL_TREE;
    4182              : }
    4183              : 
    4184              : /* Determines if the expression or type T uses any parameter packs.  */
    4185              : tree
    4186     13021682 : uses_parameter_packs (tree t)
    4187              : {
    4188     13021682 :   tree parameter_packs = NULL_TREE;
    4189     13021682 :   struct find_parameter_pack_data ppd;
    4190     13021682 :   ppd.parameter_packs = &parameter_packs;
    4191     13021682 :   ppd.visited = new hash_set<tree>;
    4192     13021682 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4193     26043364 :   delete ppd.visited;
    4194     13021682 :   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     61817284 : make_pack_expansion (tree arg, tsubst_flags_t complain)
    4204              : {
    4205     61817284 :   tree result;
    4206     61817284 :   tree parameter_packs = NULL_TREE;
    4207     61817284 :   bool for_types = false;
    4208     61817284 :   struct find_parameter_pack_data ppd;
    4209              : 
    4210     61817284 :   if (!arg || arg == error_mark_node)
    4211              :     return arg;
    4212              : 
    4213     61817269 :   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     61817235 :   if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
    4284     54522215 :     for_types = true;
    4285              : 
    4286              :   /* Build the PACK_EXPANSION_* node.  */
    4287     61817235 :   result = for_types
    4288     54522215 :      ? cxx_make_type (TYPE_PACK_EXPANSION)
    4289      7295020 :      : make_node (EXPR_PACK_EXPANSION);
    4290     61817235 :   PACK_EXPANSION_PATTERN (result) = arg;
    4291     61817235 :   if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
    4292              :     {
    4293              :       /* Propagate type and const-expression information.  */
    4294      7295020 :       TREE_TYPE (result) = TREE_TYPE (arg);
    4295      7295020 :       TREE_CONSTANT (result) = TREE_CONSTANT (arg);
    4296              :       /* Mark this read now, since the expansion might be length 0.  */
    4297      7295020 :       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     54522215 :     SET_TYPE_STRUCTURAL_EQUALITY (result);
    4303              : 
    4304              :   /* Determine which parameter packs will be expanded.  */
    4305     61817235 :   ppd.parameter_packs = &parameter_packs;
    4306     61817235 :   ppd.visited = new hash_set<tree>;
    4307     61817235 :   cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
    4308    123634470 :   delete ppd.visited;
    4309              : 
    4310              :   /* Make sure we found some parameter packs.  */
    4311     61817235 :   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    116339331 :   PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
    4323              : 
    4324     61817154 :   PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
    4325     61817154 :   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        77447 :     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        12569 : make_pack_index (tree pack, tree index)
    4341              : {
    4342        12569 :   if (pack == error_mark_node)
    4343              :     return error_mark_node;
    4344              : 
    4345        12556 :   bool for_types;
    4346        12556 :   if (TREE_CODE (pack) == TYPE_PACK_EXPANSION)
    4347              :     for_types = true;
    4348         8597 :   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         1486 :       gcc_checking_assert (TREE_CODE (pack) == TREE_VEC);
    4354         1486 :       for_types = TYPE_P (TREE_VEC_ELT (pack, 0));
    4355              :     }
    4356              : 
    4357         1486 :   tree t = (for_types
    4358         5445 :             ? cxx_make_type (PACK_INDEX_TYPE)
    4359         8349 :             : make_node (PACK_INDEX_EXPR));
    4360        12556 :   PACK_INDEX_PACK (t) = pack;
    4361        12556 :   PACK_INDEX_INDEX (t) = index;
    4362        12556 :   if (TREE_CODE (t) == PACK_INDEX_TYPE)
    4363         4207 :     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   2036208241 : check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
    4383              : {
    4384   2036208241 :   tree parameter_packs = NULL_TREE;
    4385   2036208241 :   struct find_parameter_pack_data ppd;
    4386              : 
    4387   2036208241 :   if (!processing_template_decl || !t || t == error_mark_node)
    4388              :     return false;
    4389              : 
    4390   1292234588 :   if (TREE_CODE (t) == TYPE_DECL)
    4391            0 :     t = TREE_TYPE (t);
    4392              : 
    4393   1292234588 :   ppd.parameter_packs = &parameter_packs;
    4394   1292234588 :   ppd.visited = new hash_set<tree>;
    4395   1292234588 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4396   2584469176 :   delete ppd.visited;
    4397              : 
    4398   1292234588 :   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   1028012675 : expand_template_argument_pack (tree args)
    4467              : {
    4468   1028012675 :   if (args == error_mark_node)
    4469              :     return error_mark_node;
    4470              : 
    4471   1028012654 :   tree result_args = NULL_TREE;
    4472   2056025308 :   int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
    4473   1028012654 :   int num_result_args = -1;
    4474   1028012654 :   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   2518932405 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4479              :     {
    4480   1490919792 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4481   1490919792 :       if (arg == NULL_TREE)
    4482              :         return args;
    4483   1490919751 :       if (ARGUMENT_PACK_P (arg))
    4484              :         {
    4485     91723405 :           int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
    4486     91723405 :           if (num_result_args < 0)
    4487     91723405 :             num_result_args = in_arg + num_packed;
    4488              :           else
    4489            0 :             num_result_args += num_packed;
    4490              :         }
    4491              :       else
    4492              :         {
    4493   1399196346 :           if (num_result_args >= 0)
    4494           26 :             num_result_args++;
    4495              :         }
    4496              :     }
    4497              : 
    4498              :   /* If no expansion is necessary, we're done.  */
    4499   1028012613 :   if (num_result_args < 0)
    4500              :     return args;
    4501              : 
    4502              :   /* Expand arguments.  */
    4503     91723405 :   result_args = make_tree_vec (num_result_args);
    4504     91723405 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
    4505     89248177 :     non_default_args_count =
    4506     89248177 :       GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
    4507    202495414 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4508              :     {
    4509    110772009 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4510    110772009 :       if (ARGUMENT_PACK_P (arg))
    4511              :         {
    4512     91723405 :           tree packed = ARGUMENT_PACK_ARGS (arg);
    4513     91723405 :           int i, num_packed = TREE_VEC_LENGTH (packed);
    4514    252777960 :           for (i = 0; i < num_packed; ++i, ++out_arg)
    4515    161054555 :             TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
    4516     91723405 :           if (non_default_args_count > 0)
    4517     89248173 :             non_default_args_count += num_packed - 1;
    4518              :         }
    4519              :       else
    4520              :         {
    4521     19048604 :           TREE_VEC_ELT (result_args, out_arg) = arg;
    4522     19048604 :           ++out_arg;
    4523              :         }
    4524              :     }
    4525     91723405 :   if (non_default_args_count >= 0)
    4526     89248177 :     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   1778851782 : check_template_shadow (tree decl)
    4540              : {
    4541   1778851782 :   tree olddecl;
    4542              : 
    4543              :   /* If we're not in a template, we can't possibly shadow a template
    4544              :      parameter.  */
    4545   1778851782 :   if (!current_template_parms)
    4546              :     return true;
    4547              : 
    4548              :   /* Figure out what we're shadowing.  */
    4549    915566254 :   decl = OVL_FIRST (decl);
    4550    915566254 :   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    915566254 :   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     68266440 :   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    209969885 : build_template_parm_index (int index,
    4593              :                            int level,
    4594              :                            int orig_level,
    4595              :                            tree decl,
    4596              :                            tree type)
    4597              : {
    4598    209969885 :   tree t = make_node (TEMPLATE_PARM_INDEX);
    4599    209969885 :   TEMPLATE_PARM_IDX (t) = index;
    4600    209969885 :   TEMPLATE_PARM_LEVEL (t) = level;
    4601    209969885 :   TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
    4602    209969885 :   TEMPLATE_PARM_DECL (t) = decl;
    4603    209969885 :   TREE_TYPE (t) = type;
    4604    209969885 :   TREE_CONSTANT (t) = TREE_CONSTANT (decl);
    4605    209969885 :   TREE_READONLY (t) = TREE_READONLY (decl);
    4606              : 
    4607    209969885 :   return t;
    4608              : }
    4609              : 
    4610              : struct ctp_hasher : ggc_ptr_hash<tree_node>
    4611              : {
    4612   1139132757 :   static hashval_t hash (tree t)
    4613              :   {
    4614   1139132757 :     ++comparing_specializations;
    4615   1139132757 :     tree_code code = TREE_CODE (t);
    4616   1139132757 :     hashval_t val = iterative_hash_object (code, 0);
    4617   1139132757 :     val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
    4618   1139132757 :     val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
    4619   1139132757 :     if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
    4620              :       {
    4621    767397537 :         val
    4622    767397537 :           = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val);
    4623    767397537 :         if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
    4624    160758598 :           val = iterative_hash_placeholder_constraint (c, val);
    4625              :       }
    4626   1139132757 :     if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
    4627    213067837 :       val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
    4628   1139132757 :     --comparing_specializations;
    4629   1139132757 :     return val;
    4630              :   }
    4631              : 
    4632   1378145583 :   static bool equal (tree t, tree u)
    4633              :   {
    4634   1378145583 :     ++comparing_specializations;
    4635   1378145583 :     bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
    4636   1378145583 :     --comparing_specializations;
    4637   1378145583 :     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    199222079 : canonical_type_parameter (tree type)
    4649              : {
    4650    199222079 :   if (ctp_table == NULL)
    4651        98033 :     ctp_table = hash_table<ctp_hasher>::create_ggc (61);
    4652              : 
    4653    199222079 :   tree& slot = *ctp_table->find_slot (type, INSERT);
    4654    199222079 :   if (slot == NULL_TREE)
    4655      2573867 :     slot = type;
    4656    199222079 :   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     22033766 : reduce_template_parm_level (tree index, tree type, int levels, tree args,
    4666              :                             tsubst_flags_t complain)
    4667              : {
    4668     22033766 :   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
    4669      6184348 :       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
    4670      6184348 :           != TEMPLATE_PARM_LEVEL (index) - levels)
    4671     28035647 :       || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
    4672              :     {
    4673     16035783 :       tree orig_decl = TEMPLATE_PARM_DECL (index);
    4674              : 
    4675     16035783 :       tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
    4676     16035783 :                               TREE_CODE (orig_decl), DECL_NAME (orig_decl),
    4677              :                               type);
    4678     16035783 :       TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
    4679     16035783 :       TREE_READONLY (decl) = TREE_READONLY (orig_decl);
    4680     16035783 :       DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
    4681     16035783 :       DECL_ARTIFICIAL (decl) = 1;
    4682     16035783 :       SET_DECL_TEMPLATE_PARM_P (decl);
    4683              : 
    4684     16035783 :       tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
    4685     16035783 :                                             TEMPLATE_PARM_LEVEL (index) - levels,
    4686     16035783 :                                             TEMPLATE_PARM_ORIG_LEVEL (index),
    4687              :                                             decl, type);
    4688     16035783 :       TEMPLATE_PARM_DESCENDANTS (index) = tpi;
    4689     32071566 :       TEMPLATE_PARM_PARAMETER_PACK (tpi)
    4690     16035783 :         = TEMPLATE_PARM_PARAMETER_PACK (index);
    4691              : 
    4692              :       /* Template template parameters need this.  */
    4693     16035783 :       tree inner = decl;
    4694     16035783 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
    4695              :         {
    4696         1776 :           inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl),
    4697         1776 :                                        TYPE_DECL, DECL_NAME (decl), type);
    4698         1776 :           DECL_TEMPLATE_RESULT (decl) = inner;
    4699         1776 :           DECL_ARTIFICIAL (inner) = true;
    4700         1776 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl),
    4701              :                                               args, complain);
    4702         1776 :           DECL_TEMPLATE_PARMS (decl) = parms;
    4703         1776 :           tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl);
    4704         1776 :           DECL_TEMPLATE_INFO (inner)
    4705         3552 :             = build_template_info (DECL_TI_TEMPLATE (orig_inner),
    4706              :                                    template_parms_to_args (parms));
    4707              :         }
    4708              : 
    4709              :       /* Attach the TPI to the decl.  */
    4710     16035783 :       if (TREE_CODE (inner) == TYPE_DECL)
    4711     15282277 :         TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
    4712              :       else
    4713       753506 :         DECL_INITIAL (decl) = tpi;
    4714              :     }
    4715              : 
    4716     22033766 :   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    163701652 : process_template_parm (tree list, location_t parm_loc, tree parm,
    4727              :                        bool is_non_type, bool is_parameter_pack)
    4728              : {
    4729    163701652 :   gcc_assert (TREE_CODE (parm) == TREE_LIST);
    4730    163701652 :   tree prev = NULL_TREE;
    4731    163701652 :   int idx = 0;
    4732              : 
    4733    163701652 :   if (list)
    4734              :     {
    4735     75510362 :       prev = tree_last (list);
    4736              : 
    4737     75510362 :       tree p = TREE_VALUE (prev);
    4738     75510362 :       if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
    4739     70197572 :         idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
    4740      5312790 :       else if (TREE_CODE (p) == PARM_DECL)
    4741      5312770 :         idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
    4742              : 
    4743     75510362 :       ++idx;
    4744              :     }
    4745              : 
    4746    163701652 :   tree decl = NULL_TREE;
    4747    163701652 :   tree defval = TREE_PURPOSE (parm);
    4748    163701652 :   tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
    4749              : 
    4750    163701652 :   if (is_non_type)
    4751              :     {
    4752     10644255 :       parm = TREE_VALUE (parm);
    4753              : 
    4754     10644255 :       SET_DECL_TEMPLATE_PARM_P (parm);
    4755              : 
    4756     10644255 :       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     10644175 :           TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
    4763     10644175 :           if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
    4764          100 :             TREE_TYPE (parm) = error_mark_node;
    4765     10644075 :           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     10644093 :                    && 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     10644255 :       TREE_CONSTANT (parm) = 1;
    4782     10644255 :       TREE_READONLY (parm) = 1;
    4783     10644255 :       decl = build_decl (parm_loc,
    4784     10644255 :                          CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
    4785     10644255 :       TREE_CONSTANT (decl) = 1;
    4786     10644255 :       TREE_READONLY (decl) = 1;
    4787     10644255 :       DECL_INITIAL (parm) = DECL_INITIAL (decl)
    4788     21288510 :         = build_template_parm_index (idx, current_template_depth,
    4789     10644255 :                                      current_template_depth,
    4790     10644255 :                                      decl, TREE_TYPE (parm));
    4791              : 
    4792     10644255 :       TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
    4793     10644255 :         = is_parameter_pack;
    4794              :     }
    4795              :   else
    4796              :     {
    4797    153057397 :       tree t;
    4798    153057397 :       parm = TREE_VALUE (TREE_VALUE (parm));
    4799              : 
    4800    153057397 :       if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
    4801              :         {
    4802       380444 :           t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
    4803              :           /* This is for distinguishing between real templates and template
    4804              :              template parameters */
    4805       380444 :           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       380444 :           tree result = DECL_TEMPLATE_RESULT (parm);
    4810       380444 :           TREE_TYPE (result) = t;
    4811       380444 :           tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
    4812       380444 :           tree tinfo = build_template_info (parm, args);
    4813       380444 :           retrofit_lang_decl (result);
    4814       380444 :           DECL_TEMPLATE_INFO (result) = tinfo;
    4815              : 
    4816       380444 :           decl = parm;
    4817       380444 :         }
    4818              :       else
    4819              :         {
    4820    152676953 :           t = cxx_make_type (TEMPLATE_TYPE_PARM);
    4821              :           /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
    4822    152676953 :           decl = build_decl (parm_loc,
    4823              :                              TYPE_DECL, parm, t);
    4824              :         }
    4825              : 
    4826    153057397 :       TYPE_NAME (t) = decl;
    4827    153057397 :       TYPE_STUB_DECL (t) = decl;
    4828    153057397 :       parm = decl;
    4829    153057397 :       TEMPLATE_TYPE_PARM_INDEX (t)
    4830    153057397 :         = build_template_parm_index (idx, current_template_depth,
    4831    153057397 :                                      current_template_depth,
    4832    153057397 :                                      decl, TREE_TYPE (parm));
    4833    153057397 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
    4834    153057397 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
    4835              :     }
    4836    163701652 :   DECL_ARTIFICIAL (decl) = 1;
    4837    163701652 :   SET_DECL_TEMPLATE_PARM_P (decl);
    4838              : 
    4839    163701652 :   if (TREE_CODE (parm) == TEMPLATE_DECL
    4840    163701652 :       && !uses_outer_template_parms (parm))
    4841       380411 :     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    163701652 :   tree reqs = finish_shorthand_constraint (parm, constr, is_non_type);
    4847              : 
    4848    163701652 :   decl = pushdecl (decl);
    4849    163701652 :   if (!is_non_type)
    4850    153057397 :     parm = decl;
    4851              : 
    4852              :   /* Build the parameter node linking the parameter declaration,
    4853              :      its default argument (if any), and its constraints (if any). */
    4854    163701652 :   parm = build_tree_list (defval, parm);
    4855    163701652 :   TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
    4856              : 
    4857    163701652 :   if (prev)
    4858     75510362 :     TREE_CHAIN (prev) = parm;
    4859              :   else
    4860              :     list = parm;
    4861              : 
    4862    163701652 :   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     87876565 : end_template_parm_list (tree parms)
    4872              : {
    4873     87876565 :   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     87876565 :   current_template_parms = TREE_CHAIN (current_template_parms);
    4879              : 
    4880    175753130 :   current_template_parms
    4881     87876565 :     = tree_cons (size_int (current_template_depth + 1),
    4882              :                  saved_parmlist, current_template_parms);
    4883              : 
    4884    251122169 :   for (unsigned ix = 0; parms; ix++)
    4885              :     {
    4886    163245604 :       tree parm = parms;
    4887    163245604 :       parms = TREE_CHAIN (parms);
    4888    163245604 :       TREE_CHAIN (parm) = NULL_TREE;
    4889              : 
    4890    163245604 :       TREE_VEC_ELT (saved_parmlist, ix) = parm;
    4891              :     }
    4892              : 
    4893     87876565 :   --processing_template_parmlist;
    4894              : 
    4895     87876565 :   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     88191447 : end_template_decl (void)
    4912              : {
    4913     88191447 :   reset_specialization ();
    4914              : 
    4915     88191447 :   if (! processing_template_decl)
    4916              :     return;
    4917              : 
    4918              :   /* This matches the pushlevel in begin_template_parm_list.  */
    4919     88191444 :   finish_scope ();
    4920              : 
    4921     88191444 :   --processing_template_decl;
    4922     88191444 :   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    813403961 : template_parm_to_arg (tree t)
    4932              : {
    4933    813403961 :   if (!t)
    4934              :     return NULL_TREE;
    4935              : 
    4936    813403885 :   if (TREE_CODE (t) == TREE_LIST)
    4937    794041844 :     t = TREE_VALUE (t);
    4938              : 
    4939    813403885 :   if (error_operand_p (t))
    4940          619 :     return error_mark_node;
    4941              : 
    4942    813403266 :   if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
    4943              :     {
    4944    807631646 :       if (TREE_CODE (t) == TYPE_DECL
    4945     60024759 :           || TREE_CODE (t) == TEMPLATE_DECL)
    4946    748654294 :         t = TREE_TYPE (t);
    4947              :       else
    4948     58977352 :         t = DECL_INITIAL (t);
    4949              :     }
    4950              : 
    4951    813403266 :   gcc_assert (TEMPLATE_PARM_P (t));
    4952              : 
    4953    813403266 :   if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
    4954    813403266 :       || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
    4955              :     {
    4956    754281144 :       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     24354008 :           tree vec = make_tree_vec (1);
    4961     24354008 :           if (CHECKING_P)
    4962     24354008 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4963              : 
    4964     24354008 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4965              : 
    4966     24354008 :           t = cxx_make_type (TYPE_ARGUMENT_PACK);
    4967     24354008 :           ARGUMENT_PACK_ARGS (t) = vec;
    4968              :         }
    4969              :     }
    4970              :   else
    4971              :     {
    4972     59122122 :       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      1459750 :           tree vec = make_tree_vec (1);
    4977      1459750 :           if (CHECKING_P)
    4978      1459750 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4979              : 
    4980      1459750 :           t = convert_from_reference (t);
    4981      1459750 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4982              : 
    4983      1459750 :           t  = make_node (NONTYPE_ARGUMENT_PACK);
    4984      1459750 :           ARGUMENT_PACK_ARGS (t) = vec;
    4985              :         }
    4986              :       else
    4987     57662372 :         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   2068445097 : template_arg_to_parm (tree t)
    4997              : {
    4998   2068445097 :   if (t == NULL_TREE)
    4999              :     return NULL_TREE;
    5000              : 
    5001   2068445097 :   if (ARGUMENT_PACK_P (t))
    5002              :     {
    5003    234767014 :       tree args = ARGUMENT_PACK_ARGS (t);
    5004    234767014 :       if (TREE_VEC_LENGTH (args) == 1
    5005    234767014 :           && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
    5006     91960030 :         t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
    5007              :     }
    5008              : 
    5009   2068445097 :   if (REFERENCE_REF_P (t))
    5010         1722 :     t = TREE_OPERAND (t, 0);
    5011              : 
    5012   2068445097 :   if (TEMPLATE_PARM_P (t))
    5013              :     return t;
    5014              :   else
    5015    585749093 :     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    388343866 : template_parms_level_to_args (tree parms)
    5023              : {
    5024    388343866 :   parms = copy_node (parms);
    5025    388343866 :   TREE_TYPE (parms) = NULL_TREE;
    5026   1176443445 :   for (tree& parm : tree_vec_range (parms))
    5027    788099579 :     parm = template_parm_to_arg (parm);
    5028              : 
    5029    388343866 :   if (CHECKING_P)
    5030    388343866 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
    5031              : 
    5032    388343866 :   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    315228192 : template_parms_to_args (tree parms)
    5041              : {
    5042    315228192 :   tree header;
    5043    315228192 :   tree args = NULL_TREE;
    5044    315228192 :   int length = TMPL_PARMS_DEPTH (parms);
    5045    315228192 :   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    315228192 :   if (length > 1)
    5051     52389834 :     args = make_tree_vec (length);
    5052              : 
    5053    683638423 :   for (header = parms; header; header = TREE_CHAIN (header))
    5054              :     {
    5055    368410231 :       tree a = template_parms_level_to_args (TREE_VALUE (header));
    5056              : 
    5057    368410231 :       if (length > 1)
    5058    105571873 :         TREE_VEC_ELT (args, --l) = a;
    5059              :       else
    5060              :         args = a;
    5061              :     }
    5062              : 
    5063    315228192 :   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    314208541 : 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     47703517 : generic_targs_for (tree tmpl)
    5080              : {
    5081     47703517 :   if (tmpl == NULL_TREE)
    5082              :     return NULL_TREE;
    5083     47703517 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
    5084     95407034 :       || 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     47588805 :   else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
    5090     47588805 :     if (tree ti = get_template_info (result))
    5091     47588805 :       return TI_ARGS (ti);
    5092       114712 :   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       188071 : outer_template_args (const_tree decl)
    5103              : {
    5104       188071 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    5105        12850 :     decl = DECL_TEMPLATE_RESULT (decl);
    5106       188071 :   tree ti = get_template_info (decl);
    5107       188071 :   if (!ti)
    5108              :     return NULL_TREE;
    5109       188071 :   tree args = TI_ARGS (ti);
    5110       188071 :   if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)))
    5111              :     return args;
    5112       376142 :   if (TMPL_ARGS_DEPTH (args) == 1)
    5113              :     return NULL_TREE;
    5114       175432 :   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    173355865 : maybe_update_decl_type (tree orig_type, tree scope)
    5122              : {
    5123    173355865 :   tree type = orig_type;
    5124              : 
    5125    173355865 :   if (type == NULL_TREE)
    5126              :     return type;
    5127              : 
    5128    165272139 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5129     78941135 :     type = TREE_TYPE (type);
    5130              : 
    5131      7947336 :   if (scope && TYPE_P (scope) && dependent_type_p (scope)
    5132      6644461 :       && dependent_type_p (type)
    5133              :       /* Don't bother building up the args in this case.  */
    5134    168579556 :       && 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      2372848 :       tree args = current_template_args ();
    5141      2372848 :       tree auto_node = type_uses_auto (type);
    5142      2372848 :       tree pushed;
    5143      2372848 :       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      2372848 :       pushed = push_scope (scope);
    5150      2372848 :       type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
    5151      2372848 :       if (pushed)
    5152      2372845 :         pop_scope (scope);
    5153              :     }
    5154              : 
    5155    165272139 :   if (type == error_mark_node)
    5156              :     return orig_type;
    5157              : 
    5158    165271404 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5159              :     {
    5160     78941102 :       if (same_type_p (type, TREE_TYPE (orig_type)))
    5161              :         type = orig_type;
    5162              :       else
    5163        65403 :         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    178104880 : build_template_decl (tree decl, tree parms, bool member_template_p)
    5174              : {
    5175    178104880 :   gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
    5176              : 
    5177    178104880 :   tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
    5178    178104880 :   SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
    5179    178104880 :   DECL_TEMPLATE_PARMS (tmpl) = parms;
    5180    178104880 :   DECL_TEMPLATE_RESULT (tmpl) = decl;
    5181    178104880 :   DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
    5182    178104880 :   TREE_TYPE (tmpl) = TREE_TYPE (decl);
    5183    178104880 :   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    5184    178104880 :   DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
    5185              : 
    5186              :   /* Propagate module information from the decl.  */
    5187    178104880 :   DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
    5188              : 
    5189    178104880 :   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     14982806 : mark_template_parm (tree t, void* data)
    5221              : {
    5222     14982806 :   int level;
    5223     14982806 :   int idx;
    5224     14982806 :   struct template_parm_data* tpd = (struct template_parm_data*) data;
    5225              : 
    5226     14982806 :   template_parm_level_and_index (t, &level, &idx);
    5227              : 
    5228     14982806 :   if (level == tpd->level)
    5229              :     {
    5230     14959080 :       tpd->parms[idx] = 1;
    5231     14959080 :       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     14982806 :   if (cxx_dialect >= cxx17
    5236     14883841 :       && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5237      1775553 :     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     14982806 :   return 0;
    5246              : }
    5247              : 
    5248              : /* Process the partial specialization DECL.  */
    5249              : 
    5250              : static tree
    5251      7830908 : process_partial_specialization (tree decl)
    5252              : {
    5253      7830908 :   tree type = TREE_TYPE (decl);
    5254      7830908 :   tree tinfo = get_template_info (decl);
    5255      7830908 :   tree maintmpl = TI_TEMPLATE (tinfo);
    5256      7830908 :   tree specargs = TI_ARGS (tinfo);
    5257      7830908 :   tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
    5258      7830908 :   tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
    5259      7830908 :   tree inner_parms;
    5260      7830908 :   tree inst;
    5261      7830908 :   int nargs = TREE_VEC_LENGTH (inner_args);
    5262      7830908 :   int ntparms;
    5263      7830908 :   int  i;
    5264      7830908 :   struct template_parm_data tpd;
    5265      7830908 :   struct template_parm_data tpd2;
    5266              : 
    5267      7830908 :   gcc_assert (current_template_parms);
    5268              : 
    5269      7830908 :   inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
    5270      7830908 :   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      7830908 :   tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
    5303      7830908 :   tpd.parms = XALLOCAVEC (int, ntparms);
    5304      7830908 :   memset (tpd.parms, 0, sizeof (int) * ntparms);
    5305              : 
    5306      7830908 :   tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5307      7830908 :   memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
    5308     23163718 :   for (i = 0; i < nargs; ++i)
    5309              :     {
    5310     15332810 :       tpd.current_arg = i;
    5311     15332810 :       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      7830908 :   {
    5319      7830908 :     auto_diagnostic_group d;
    5320      7830908 :     bool did_error_intro = false;
    5321     29742681 :     for (i = 0; i < ntparms; ++i)
    5322     14080865 :       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      7830908 :     if (did_error_intro)
    5338           55 :       return error_mark_node;
    5339      7830908 :   }
    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      7830853 :   tree main_args
    5346      7830853 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
    5347      7830853 :   if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
    5348      7830853 :       && (!flag_concepts
    5349       635269 :           || !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      7830853 :   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      7830850 :   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      7830841 :   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     14844158 :            && TMPL_ARGS_DEPTH (specargs) == 1
    5392     15252914 :            && !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      7830841 :   tpd2.parms = 0;
    5411     23163550 :   for (i = 0; i < nargs; ++i)
    5412              :     {
    5413     15332709 :       tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
    5414     15332709 :       tree arg = TREE_VEC_ELT (inner_args, i);
    5415     15332709 :       tree packed_args = NULL_TREE;
    5416     15332709 :       int j, len = 1;
    5417              : 
    5418     15332709 :       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       743555 :           packed_args = ARGUMENT_PACK_ARGS (arg);
    5423       743555 :           len = TREE_VEC_LENGTH (packed_args);
    5424              :         }
    5425              : 
    5426     31034176 :       for (j = 0; j < len; j++)
    5427              :         {
    5428     15701467 :           if (packed_args)
    5429              :             /* Get the Jth argument in the parameter pack.  */
    5430      1112313 :             arg = TREE_VEC_ELT (packed_args, j);
    5431              : 
    5432     15701467 :           if (PACK_EXPANSION_P (arg))
    5433              :             {
    5434              :               /* Pack expansions must come at the end of the
    5435              :                  argument list.  */
    5436       436286 :               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     15701467 :           if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5449              :             /* We only care about the pattern.  */
    5450        65111 :             arg = PACK_EXPANSION_PATTERN (arg);
    5451              : 
    5452     15701467 :           if (/* These first two lines are the `non-type' bit.  */
    5453     15701467 :               !TYPE_P (arg)
    5454      4190390 :               && 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      3939961 :               && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
    5459     18513044 :               && !((REFERENCE_REF_P (arg)
    5460      2811553 :                     || 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      2811492 :               tree type = TREE_TYPE (parm);
    5467              : 
    5468      2811492 :               if (!tpd2.parms)
    5469              :                 {
    5470              :                   /* We haven't yet initialized TPD2.  Do so now.  */
    5471      1739669 :                   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      1739669 :                   tpd2.parms = XALLOCAVEC (int, nargs);
    5476      1739669 :                   tpd2.level =
    5477      1739669 :                     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      2811492 :               tpd2.current_arg = i;
    5484      2811492 :               tpd2.arg_uses_template_parms[i] = 0;
    5485      2811492 :               memset (tpd2.parms, 0, sizeof (int) * nargs);
    5486      2811492 :               for_each_template_parm (type,
    5487              :                                       &mark_template_parm,
    5488              :                                       &tpd2,
    5489              :                                       NULL,
    5490              :                                       /*include_nondeduced_p=*/false);
    5491              : 
    5492      2811492 :               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      7830841 :   if (TREE_CODE (decl) == TYPE_DECL)
    5518      6897705 :     gcc_assert (!COMPLETE_TYPE_P (type));
    5519              : 
    5520              :   // Build the template decl.
    5521      7830841 :   tree tmpl = build_template_decl (decl, current_template_parms,
    5522      7830841 :                                    DECL_MEMBER_TEMPLATE_P (maintmpl));
    5523      7830841 :   SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    5524      7830841 :   DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
    5525      7830841 :   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     21911613 :   for (i = 0; i < ntparms; ++i)
    5530              :     {
    5531     14080772 :       tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
    5532     14080772 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    5533       168416 :         DECL_CONTEXT (parm) = tmpl;
    5534              :     }
    5535              : 
    5536      7830841 :   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       933136 :       tree reg = register_specialization (decl, maintmpl, specargs, false, 0);
    5541       933136 :       if (reg != decl)
    5542              :         /* Redeclaration.  */
    5543              :         return reg;
    5544              :     }
    5545              :   else
    5546      6897705 :     associate_classtype_constraints (type);
    5547              : 
    5548     15661664 :   DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
    5549      7830832 :     = tree_cons (specargs, tmpl,
    5550      7830832 :                  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
    5551      7830832 :   TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
    5552              :   /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL.  */
    5553      7830832 :   gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
    5554      7830832 :   TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE);
    5555              : 
    5556      7830832 :   set_defining_module_for_partial_spec (decl);
    5557              : 
    5558     59566503 :   for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
    5559     51735671 :        inst = TREE_CHAIN (inst))
    5560              :     {
    5561     51735671 :       tree instance = TREE_VALUE (inst);
    5562     54711060 :       if (TYPE_P (instance)
    5563     51735671 :           ? (COMPLETE_TYPE_P (instance)
    5564     48760282 :              && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
    5565      2975389 :           : DECL_TEMPLATE_INSTANTIATION (instance))
    5566              :         {
    5567       126874 :           tree partial_ti = most_specialized_partial_spec (instance, tf_none,
    5568              :                                                            /*rechecking=*/true);
    5569       126874 :           tree inst_decl = (DECL_P (instance)
    5570       126874 :                             ? instance : TYPE_NAME (instance));
    5571       126874 :           if (!partial_ti)
    5572              :             /* OK */;
    5573        63306 :           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        63303 :           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    422002308 : get_template_parm_index (tree parm)
    5592              : {
    5593    422002308 :   if (TREE_CODE (parm) == PARM_DECL
    5594    422002308 :       || TREE_CODE (parm) == CONST_DECL)
    5595    194704029 :     parm = DECL_INITIAL (parm);
    5596    227298279 :   else if (TREE_CODE (parm) == TYPE_DECL
    5597     10693117 :            || TREE_CODE (parm) == TEMPLATE_DECL)
    5598    216609569 :     parm = TREE_TYPE (parm);
    5599    422002308 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    5600    195946279 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
    5601    195946216 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    5602    226111337 :     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
    5603    422002308 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
    5604    422002308 :   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         6302 : 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         6302 :   if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
    5615              :     return;
    5616         6287 :   else if (TREE_CODE (parm) == PARM_DECL)
    5617              :     {
    5618         6245 :       cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
    5619              :                     ppd, ppd->visited);
    5620         6245 :       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    221753727 : fixed_parameter_pack_p (tree parm)
    5643              : {
    5644              :   /* This can only be true in a member template.  */
    5645    221753727 :   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      3070386 :   if (!template_parameter_pack_p (parm))
    5649              :     return NULL_TREE;
    5650              :   /* A type parm can't refer to another parm.  */
    5651      3070386 :   if (TREE_CODE (parm) == TYPE_DECL)
    5652              :     return NULL_TREE;
    5653              : 
    5654         6269 :   tree parameter_packs = NULL_TREE;
    5655         6269 :   struct find_parameter_pack_data ppd;
    5656         6269 :   ppd.parameter_packs = &parameter_packs;
    5657         6269 :   ppd.visited = new hash_set<tree>;
    5658              : 
    5659         6269 :   fixed_parameter_pack_p_1 (parm, &ppd);
    5660              : 
    5661        12538 :   delete ppd.visited;
    5662         6269 :   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    245570405 : check_default_tmpl_args (tree decl, tree parms, bool is_primary,
    5681              :                          bool is_partial, int is_friend_decl)
    5682              : {
    5683    245570405 :   const char *msg;
    5684    245570405 :   int last_level_to_check;
    5685    245570405 :   tree parm_level;
    5686    245570405 :   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    245570405 :   if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
    5696    245570405 :       || (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     61630937 :   if ((DECL_IMPLICIT_TYPEDEF_P (decl)
    5703     37915242 :        && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5704    305858036 :       || (TREE_CODE (decl) == FUNCTION_DECL
    5705    186820620 :           && 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    241572267 :   if (current_class_type
    5711    189834566 :       && !TYPE_BEING_DEFINED (current_class_type)
    5712     68346538 :       && DECL_LANG_SPECIFIC (decl)
    5713     68265454 :       && 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    140868011 :       && (DECL_FUNCTION_MEMBER_P (decl)
    5717     67961772 :           ? 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    377495811 :       && (!DECL_FUNCTION_MEMBER_P (decl)
    5724     67961772 :           || 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    180827528 :   if (is_primary
    5734     75151063 :       && ((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     60136999 :       for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
    5742              :         {
    5743     31483344 :           tree inner_parms = TREE_VALUE (parm_level);
    5744     31483344 :           int ntparms = TREE_VEC_LENGTH (inner_parms);
    5745     31483344 :           int seen_def_arg_p = 0;
    5746     31483344 :           int i;
    5747              : 
    5748     84213429 :           for (i = 0; i < ntparms; ++i)
    5749              :             {
    5750     52730085 :               tree parm = TREE_VEC_ELT (inner_parms, i);
    5751              : 
    5752     52730085 :               if (parm == error_mark_node)
    5753            0 :                 continue;
    5754              : 
    5755     52730085 :               if (TREE_PURPOSE (parm))
    5756              :                 seen_def_arg_p = 1;
    5757     48304741 :               else if (seen_def_arg_p
    5758     48304741 :                        && !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     48304720 :               else if (!is_partial
    5767     48304720 :                        && !is_friend_decl
    5768              :                        /* Don't complain about an enclosing partial
    5769              :                           specialization.  */
    5770     31722045 :                        && parm_level == parms
    5771     28797562 :                        && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
    5772     24700864 :                        && i < ntparms - 1
    5773      9785051 :                        && template_parameter_pack_p (TREE_VALUE (parm))
    5774              :                        /* A fixed parameter pack will be partially
    5775              :                           instantiated into a fixed length list.  */
    5776     48304759 :                        && !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    180827528 :   if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
    5795              :       || is_partial
    5796    180416620 :       || !is_primary
    5797     67183141 :       || 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     65374421 :     parms = TREE_CHAIN (parms);
    5816              : 
    5817              :   /* Figure out what error message to issue.  */
    5818    180827528 :   if (is_friend_decl == 2)
    5819              :     msg = G_("default template arguments may not be used in function template "
    5820              :              "friend re-declaration");
    5821    180626933 :   else if (is_friend_decl)
    5822              :     msg = G_("default template arguments may not be used in template "
    5823              :              "friend declarations");
    5824    179018808 :   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    178619982 :   else if (is_partial)
    5828              :     msg = G_("default template arguments may not be used in "
    5829              :              "partial specializations");
    5830    170789038 :   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    100558045 :   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     85843231 :     last_level_to_check = template_class_depth (current_class_type) + 1;
    5850              :   else
    5851              :     /* Check everything.  */
    5852              :     last_level_to_check = 0;
    5853              : 
    5854    100558045 :   for (parm_level = parms;
    5855    223796530 :        parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
    5856     18882257 :        parm_level = TREE_CHAIN (parm_level))
    5857              :     {
    5858     18882260 :       tree inner_parms = TREE_VALUE (parm_level);
    5859     18882260 :       int i;
    5860     18882260 :       int ntparms;
    5861              : 
    5862     18882260 :       ntparms = TREE_VEC_LENGTH (inner_parms);
    5863     58021408 :       for (i = 0; i < ntparms; ++i)
    5864              :         {
    5865     39139151 :           if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
    5866            0 :             continue;
    5867              : 
    5868     39139151 :           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     18882257 :       if (msg)
    5889     18882229 :         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      2370900 : template_parm_this_level_p (tree t, void* data)
    5903              : {
    5904      2370900 :   int this_level = *(int *)data;
    5905      2370900 :   int level;
    5906              : 
    5907      2370900 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5908       438582 :     level = TEMPLATE_PARM_LEVEL (t);
    5909              :   else
    5910      1932318 :     level = TEMPLATE_TYPE_LEVEL (t);
    5911      2370900 :   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     11433478 : template_parm_outer_level (tree t, void *data)
    5921              : {
    5922     11433478 :   int this_level = *(int *)data;
    5923     11433478 :   int level;
    5924              : 
    5925     11433478 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5926       277426 :     level = TEMPLATE_PARM_LEVEL (t);
    5927              :   else
    5928     11156052 :     level = TEMPLATE_TYPE_LEVEL (t);
    5929     11433478 :   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    318743877 : push_template_decl (tree decl, bool is_friend)
    5941              : {
    5942    318743877 :   if (decl == error_mark_node || !current_template_parms)
    5943              :     return error_mark_node;
    5944              : 
    5945              :   /* See if this is a partial specialization.  */
    5946     74280149 :   bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
    5947     20387185 :                       && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
    5948     20052319 :                       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
    5949    386126193 :                      || (VAR_P (decl)
    5950     60859008 :                          && DECL_LANG_SPECIFIC (decl)
    5951      6919284 :                          && DECL_TEMPLATE_SPECIALIZATION (decl)
    5952       933151 :                          && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
    5953              : 
    5954              :   /* No surprising friend functions.  */
    5955    318743849 :   gcc_checking_assert (is_friend
    5956              :                        || !(TREE_CODE (decl) == FUNCTION_DECL
    5957              :                             && DECL_UNIQUE_FRIEND_P (decl)));
    5958              : 
    5959    318743849 :   tree ctx;
    5960    318743849 :   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      8840980 :     ctx = CP_DECL_CONTEXT (decl);
    5964    309902869 :   else if (CP_DECL_CONTEXT (decl)
    5965    309902869 :            && 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    258684694 :     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     51218175 :     ctx = current_scope ();
    5973              : 
    5974    318743849 :   if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
    5975     59992550 :     ctx = NULL_TREE;
    5976              : 
    5977    318743849 :   if (!DECL_CONTEXT (decl))
    5978          133 :     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
    5979              : 
    5980              :   /* See if this is a primary template.  */
    5981    318743849 :   bool is_primary = false;
    5982    318743849 :   if (is_friend && ctx
    5983    318743849 :       && 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    338631018 :   else if (DECL_IMPLICIT_TYPEDEF_P (decl) && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5989              :     /* Lambdas are not primary.  */
    5990              :     ;
    5991              :   else
    5992    317646021 :     is_primary = template_parm_scope_p ();
    5993              : 
    5994              :   /* True if the template is a member template, in the sense of
    5995              :      [temp.mem].  */
    5996    317646021 :   bool member_template_p = false;
    5997              : 
    5998    317646021 :   if (is_primary)
    5999              :     {
    6000     77334253 :       warning (OPT_Wtemplates, "template %qD declared", decl);
    6001              : 
    6002     77334253 :       if (DECL_CLASS_SCOPE_P (decl))
    6003              :         member_template_p = true;
    6004              : 
    6005     77334253 :       if (TREE_CODE (decl) == TYPE_DECL
    6006     77334253 :           && IDENTIFIER_ANON_P (DECL_NAME (decl)))
    6007              :         {
    6008            6 :           error ("template class without a name");
    6009            6 :           return error_mark_node;
    6010              :         }
    6011     77334247 :       else if (TREE_CODE (decl) == FUNCTION_DECL)
    6012              :         {
    6013     49198045 :           if (member_template_p)
    6014              :             {
    6015     19938124 :               if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
    6016            6 :                 error ("member template %qD may not have virt-specifiers", decl);
    6017              :             }
    6018     98396090 :           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     98396078 :           if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
    6028     49200691 :               && (!prototype_p (TREE_TYPE (decl))
    6029         2652 :                   || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
    6030         2652 :                   || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    6031         2652 :                   || (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     22124696 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6044     45846244 :                && CLASS_TYPE_P (TREE_TYPE (decl)))
    6045              :         /* Class template.  */;
    6046     10426166 :       else if (TREE_CODE (decl) == TYPE_DECL
    6047     10426166 :                && TYPE_DECL_ALIAS_P (decl))
    6048              :         /* alias-declaration */
    6049      4414651 :         gcc_assert (!DECL_ARTIFICIAL (decl));
    6050      6011515 :       else if (VAR_P (decl))
    6051              :         /* C++14 variable template. */;
    6052      2549351 :       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     74280134 :   bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
    6062    372636780 :                   && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
    6063    231835045 :                       || (VAR_OR_FUNCTION_DECL_P (decl)
    6064    187238198 :                           && 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    252222218 :   if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
    6071    243856763 :     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    318743819 :   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    181055335 :       tree type = TREE_TYPE (decl);
    6081    181055335 :       tree arg = DECL_ARGUMENTS (decl);
    6082    181055335 :       tree argtype = TYPE_ARG_TYPES (type);
    6083              : 
    6084    574914012 :       while (arg && argtype)
    6085              :         {
    6086    393858677 :           if (!DECL_PACK_P (arg)
    6087    783098075 :               && 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    393858677 :           arg = DECL_CHAIN (arg);
    6098    393858677 :           argtype = TREE_CHAIN (argtype);
    6099              :         }
    6100              : 
    6101              :       /* Check for bare parameter packs in the return type and the
    6102              :          exception specifiers.  */
    6103    181055335 :       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    181055335 :       if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
    6109            0 :         TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
    6110              :     }
    6111              :   else
    6112              :     {
    6113    275376968 :       if (check_for_bare_parameter_packs (is_typedef_decl (decl)
    6114              :                                           ? DECL_ORIGINAL_TYPE (decl)
    6115     83795602 :                                           : TREE_TYPE (decl)))
    6116              :         {
    6117           64 :           TREE_TYPE (decl) = error_mark_node;
    6118           64 :           return error_mark_node;
    6119              :         }
    6120              : 
    6121      7830911 :       if (is_partial && VAR_P (decl)
    6122    138621559 :           && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
    6123            3 :         return error_mark_node;
    6124              :     }
    6125              : 
    6126    318743752 :   if (is_partial)
    6127      7830908 :     return process_partial_specialization (decl);
    6128              : 
    6129    310912844 :   tree args = current_template_args ();
    6130    310912844 :   tree tmpl = NULL_TREE;
    6131    310912844 :   bool new_template_p = false;
    6132    310912844 :   if (local_p)
    6133              :     {
    6134              :       /* Does not get a template head.  */
    6135     66521592 :       tmpl = NULL_TREE;
    6136     66521592 :       gcc_checking_assert (!is_primary);
    6137              :     }
    6138    244391252 :   else if (!ctx
    6139    191629668 :            || TREE_CODE (ctx) == FUNCTION_DECL
    6140    190445483 :            || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
    6141     68346725 :            || (DECL_IMPLICIT_TYPEDEF_P (decl)
    6142       162972 :                && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    6143    312737826 :            || (is_friend && !(DECL_LANG_SPECIFIC (decl)
    6144           84 :                               && DECL_TEMPLATE_INFO (decl))))
    6145              :     {
    6146    176044765 :       if (DECL_LANG_SPECIFIC (decl)
    6147    159460147 :           && DECL_TEMPLATE_INFO (decl)
    6148    182052858 :           && DECL_TI_TEMPLATE (decl))
    6149      6008093 :         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     55455523 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6154     13408036 :                && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
    6155    170098092 :                && 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        30710 :           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        61420 :           tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
    6166              :         }
    6167              :       else
    6168              :         {
    6169    170005962 :           tmpl = build_template_decl (decl, current_template_parms,
    6170              :                                       member_template_p);
    6171    170005962 :           new_template_p = true;
    6172              : 
    6173    170005962 :           if (DECL_LANG_SPECIFIC (decl)
    6174    170005962 :               && 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     68346487 :       tree a, t, current, parms;
    6187     68346487 :       int i;
    6188     68346487 :       tree tinfo = get_template_info (decl);
    6189              : 
    6190     68346487 :       if (!tinfo)
    6191              :         {
    6192           15 :           error ("template definition of non-template %q#D", decl);
    6193           15 :           return error_mark_node;
    6194              :         }
    6195              : 
    6196     68346472 :       tmpl = TI_TEMPLATE (tinfo);
    6197              : 
    6198     68346472 :       if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    6199     67961772 :           && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
    6200     67961772 :           && DECL_TEMPLATE_SPECIALIZATION (decl)
    6201     68346490 :           && 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     68346460 :       parms = DECL_TEMPLATE_PARMS (tmpl);
    6228     68346460 :       i = TMPL_PARMS_DEPTH (parms);
    6229    190834165 :       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    151005671 :         for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
    6238              :           {
    6239    165318464 :             a = TMPL_ARGS_LEVEL (args, i);
    6240     82659232 :             t = INNERMOST_TEMPLATE_PARMS (parms);
    6241              : 
    6242     82659232 :             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     82659217 :             if (current == decl)
    6257              :               current = ctx;
    6258     14312778 :             else if (current == NULL_TREE)
    6259              :               /* Can happen in erroneous input.  */
    6260              :               break;
    6261              :             else
    6262     14312778 :               current = get_containing_scope (current);
    6263              :           }
    6264              : 
    6265              :       /* Check that the parms are used in the appropriate qualifying scopes
    6266              :          in the declarator.  */
    6267    136692878 :       if (!comp_template_args
    6268     68346439 :           (TI_ARGS (tinfo),
    6269     68346439 :            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     68346436 :       if (flag_concepts)
    6285              :         {
    6286     67484560 :           tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
    6287     67484560 :           tree scope_parms = current_template_parms;
    6288     67484560 :           if (PRIMARY_TEMPLATE_P (tmpl))
    6289              :             {
    6290     18175673 :               decl_parms = TREE_CHAIN (decl_parms);
    6291     18175673 :               scope_parms = TREE_CHAIN (scope_parms);
    6292              :             }
    6293    130976395 :           while (decl_parms)
    6294              :             {
    6295     63491853 :               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     63491835 :               decl_parms = TREE_CHAIN (decl_parms);
    6302     63491835 :               scope_parms = TREE_CHAIN (scope_parms);
    6303              :             }
    6304              :         }
    6305              :     }
    6306              : 
    6307    488782402 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6308              : 
    6309    310912793 :   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    170005962 :       if (!ctx
    6317    170005962 :           && !(is_friend && template_class_depth (current_class_type) > 0))
    6318              :         {
    6319     44653289 :           tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
    6320     44653289 :           if (pushed == error_mark_node)
    6321              :             return error_mark_node;
    6322              : 
    6323              :           /* pushdecl may have found an existing template.  */
    6324     44653232 :           if (pushed != tmpl)
    6325              :             {
    6326      2976650 :               decl = DECL_TEMPLATE_RESULT (pushed);
    6327      2976650 :               tmpl = NULL_TREE;
    6328              :             }
    6329              :         }
    6330    125352673 :       else if (is_friend)
    6331              :         {
    6332              :           /* Record this decl as belonging to the current class.  It's
    6333              :              not chained onto anything else.  */
    6334      3325306 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
    6335      3325306 :           gcc_checking_assert (!DECL_CHAIN (tmpl));
    6336      3325306 :           DECL_CHAIN (tmpl) = current_scope ();
    6337              :         }
    6338              :     }
    6339    140906831 :   else if (tmpl)
    6340              :     /* The type may have been completed, or (erroneously) changed.  */
    6341     74385239 :     TREE_TYPE (tmpl) = TREE_TYPE (decl);
    6342              : 
    6343    244391144 :   if (tmpl)
    6344              :     {
    6345    241414494 :       if (is_primary)
    6346              :         {
    6347     66526617 :           tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6348              : 
    6349     66526617 :           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     66526617 :           parms = INNERMOST_TEMPLATE_PARMS (parms);
    6354    186332571 :           for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
    6355              :             {
    6356    119805954 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6357    119805954 :               if (TREE_CODE (parm) == TEMPLATE_DECL)
    6358       207506 :                 DECL_CONTEXT (parm) = tmpl;
    6359              :             }
    6360              : 
    6361     66526617 :           if (TREE_CODE (decl) == TYPE_DECL
    6362     66526617 :               && TYPE_DECL_ALIAS_P (decl))
    6363              :             {
    6364      4414621 :               if (tree constr
    6365      4414621 :                   = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
    6366              :                 {
    6367              :                   /* ??? Why don't we do this here for all templates?  */
    6368       258816 :                   constr = build_constraints (constr, NULL_TREE);
    6369       258816 :                   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    241414494 :       if (DECL_TEMPLATE_INFO (tmpl))
    6379         1003 :         args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
    6380              : 
    6381    241414494 :       tree info = build_template_info (tmpl, args);
    6382              : 
    6383    241414494 :       if (DECL_IMPLICIT_TYPEDEF_P (decl))
    6384     13489344 :         SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
    6385              :       else
    6386              :         {
    6387    227925150 :           retrofit_lang_decl (decl);
    6388    227925150 :           DECL_TEMPLATE_INFO (decl) = info;
    6389              :         }
    6390              :     }
    6391              : 
    6392    310912736 :   if (is_typedef_decl (decl)
    6393     53892864 :       && (dependent_opaque_alias_p (TREE_TYPE (decl))
    6394     53892699 :           || dependent_alias_template_spec_p (TREE_TYPE (decl), nt_opaque)))
    6395              :     {
    6396              :       /* Manually mark such aliases as dependent so that dependent_type_p_r
    6397              :          doesn't have to handle them.  */
    6398       588868 :       TYPE_DEPENDENT_P_VALID (TREE_TYPE (decl)) = true;
    6399       588868 :       TYPE_DEPENDENT_P (TREE_TYPE (decl)) = true;
    6400              :       /* The identity of such aliases is hairy; see structural_comptypes.  */
    6401       588868 :       SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (decl));
    6402              :     }
    6403              : 
    6404    310912736 :   if (flag_implicit_templates
    6405    310212336 :       && !is_friend
    6406    301386609 :       && TREE_PUBLIC (decl)
    6407    177055392 :       && VAR_OR_FUNCTION_DECL_P (decl))
    6408              :     /* Set DECL_COMDAT on template instantiations; if we force
    6409              :        them to be emitted by explicit instantiation,
    6410              :        mark_needed will tell cgraph to do the right thing.  */
    6411    174395321 :     DECL_COMDAT (decl) = true;
    6412              : 
    6413    552327230 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6414              : 
    6415              :   return decl;
    6416              : }
    6417              : 
    6418              : /* FN is an inheriting constructor that inherits from the constructor
    6419              :    template INHERITED; turn FN into a constructor template with a matching
    6420              :    template header.  */
    6421              : 
    6422              : tree
    6423       114903 : add_inherited_template_parms (tree fn, tree inherited)
    6424              : {
    6425       114903 :   tree inner_parms
    6426       114903 :     = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
    6427       114903 :   inner_parms = copy_node (inner_parms);
    6428       114903 :   tree parms
    6429       114903 :     = tree_cons (size_int (current_template_depth + 1),
    6430              :                  inner_parms, current_template_parms);
    6431       114903 :   tree tmpl = build_template_decl (fn, parms, /*member*/true);
    6432       114903 :   tree args = template_parms_to_args (parms);
    6433       114903 :   DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
    6434       114903 :   DECL_ARTIFICIAL (tmpl) = true;
    6435       114903 :   DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6436       114903 :   return tmpl;
    6437              : }
    6438              : 
    6439              : /* Called when a class template TYPE is redeclared with the indicated
    6440              :    template PARMS, e.g.:
    6441              : 
    6442              :      template <class T> struct S;
    6443              :      template <class T> struct S {};  */
    6444              : 
    6445              : bool
    6446      2651273 : redeclare_class_template (tree type, tree parms, tree cons)
    6447              : {
    6448      2651273 :   tree tmpl;
    6449      2651273 :   tree tmpl_parms;
    6450      2651273 :   int i;
    6451              : 
    6452      2651273 :   if (!TYPE_TEMPLATE_INFO (type))
    6453              :     {
    6454            0 :       error ("%qT is not a template type", type);
    6455            0 :       return false;
    6456              :     }
    6457              : 
    6458      2651273 :   tmpl = TYPE_TI_TEMPLATE (type);
    6459      2651273 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6460              :     /* The type is nested in some template class.  Nothing to worry
    6461              :        about here; there are no new template parameters for the nested
    6462              :        type.  */
    6463              :     return true;
    6464              : 
    6465      2651273 :   if (!parms)
    6466              :     {
    6467            3 :       error ("template specifiers not specified in declaration of %qD",
    6468              :              tmpl);
    6469            3 :       return false;
    6470              :     }
    6471              : 
    6472      2651270 :   parms = INNERMOST_TEMPLATE_PARMS (parms);
    6473      2651270 :   tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
    6474              : 
    6475      2651270 :   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
    6476              :     {
    6477           21 :       auto_diagnostic_group d;
    6478           21 :       error_n (input_location, TREE_VEC_LENGTH (parms),
    6479              :                "redeclared with %d template parameter",
    6480              :                "redeclared with %d template parameters",
    6481           21 :                TREE_VEC_LENGTH (parms));
    6482           42 :       inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
    6483              :                 "previous declaration %qD used %d template parameter",
    6484              :                 "previous declaration %qD used %d template parameters",
    6485           21 :                 tmpl, TREE_VEC_LENGTH (tmpl_parms));
    6486           21 :       return false;
    6487           21 :     }
    6488              : 
    6489      7114379 :   for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
    6490              :     {
    6491      4463178 :       tree tmpl_parm;
    6492      4463178 :       tree parm;
    6493              : 
    6494      4463178 :       if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
    6495      4463178 :           || TREE_VEC_ELT (parms, i) == error_mark_node)
    6496            0 :         continue;
    6497              : 
    6498      4463178 :       tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
    6499      4463178 :       if (error_operand_p (tmpl_parm))
    6500              :         return false;
    6501              : 
    6502      4463163 :       parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6503              : 
    6504              :       /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
    6505              :          TEMPLATE_DECL.  */
    6506      4463163 :       if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
    6507      4463151 :           || (TREE_CODE (tmpl_parm) != TYPE_DECL
    6508       486543 :               && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
    6509      4463142 :           || (TREE_CODE (tmpl_parm) != PARM_DECL
    6510      3977177 :               && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
    6511      3977177 :                   != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
    6512      8926302 :           || (TREE_CODE (tmpl_parm) == PARM_DECL
    6513       485965 :               && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
    6514       485965 :                   != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
    6515              :         {
    6516           27 :           auto_diagnostic_group d;
    6517           27 :           error ("template parameter %q+#D", tmpl_parm);
    6518           27 :           if (DECL_P (parm))
    6519           24 :             inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
    6520              :           else
    6521            3 :             inform (input_location, "redeclared here");
    6522           27 :           return false;
    6523           27 :         }
    6524              : 
    6525              :       /* The parameters can be declared to introduce different
    6526              :          constraints.  */
    6527      4463136 :       tree p1 = TREE_VEC_ELT (tmpl_parms, i);
    6528      4463136 :       tree p2 = TREE_VEC_ELT (parms, i);
    6529      4463136 :       if (!template_parameter_constraints_equivalent_p (p1, p2))
    6530              :         {
    6531            6 :           auto_diagnostic_group d;
    6532            6 :           error ("declaration of template parameter %q+#D with different "
    6533              :                  "constraints", parm);
    6534            6 :           inform (DECL_SOURCE_LOCATION (tmpl_parm),
    6535              :                   "original declaration appeared here");
    6536            6 :           return false;
    6537            6 :         }
    6538              : 
    6539              :       /* Give each template template parm in this redeclaration a
    6540              :          DECL_CONTEXT of the template for which they are a parameter.  */
    6541      4463130 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    6542              :         {
    6543          569 :           gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
    6544              :                                || DECL_CONTEXT (parm) == tmpl);
    6545          569 :           DECL_CONTEXT (parm) = tmpl;
    6546              :         }
    6547              :     }
    6548              : 
    6549      2651201 :   if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
    6550              :     return false;
    6551              : 
    6552      2651198 :   tree ci = get_constraints (tmpl);
    6553      2729945 :   tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
    6554      2729948 :   tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
    6555              : 
    6556              :   /* Two classes with different constraints declare different entities.  */
    6557      2651198 :   if (!cp_tree_equal (req1, req2))
    6558              :     {
    6559           15 :       auto_diagnostic_group d;
    6560           15 :       error_at (input_location, "redeclaration of %q#D with different "
    6561              :                                 "constraints", tmpl);
    6562           15 :       inform (DECL_SOURCE_LOCATION (tmpl),
    6563              :               "original declaration appeared here");
    6564           15 :       return false;
    6565           15 :     }
    6566              : 
    6567              :     return true;
    6568              : }
    6569              : 
    6570              : /* The actual substitution part of instantiate_non_dependent_expr,
    6571              :    to be used when the caller has already checked
    6572              :     !instantiation_dependent_uneval_expression_p (expr)
    6573              :    and cleared processing_template_decl.  */
    6574              : 
    6575              : tree
    6576     71095034 : instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
    6577              : {
    6578     71095034 :   return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE);
    6579              : }
    6580              : 
    6581              : /* Instantiate the non-dependent expression EXPR.  */
    6582              : 
    6583              : tree
    6584    140369803 : instantiate_non_dependent_expr (tree expr,
    6585              :                                 tsubst_flags_t complain /* = tf_error */)
    6586              : {
    6587    140369803 :   if (expr == NULL_TREE)
    6588              :     return NULL_TREE;
    6589              : 
    6590    140369803 :   if (processing_template_decl)
    6591              :     {
    6592              :       /* The caller should have checked this already.  */
    6593     36599116 :       gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
    6594     36599116 :       processing_template_decl_sentinel s;
    6595     36599116 :       expr = instantiate_non_dependent_expr_internal (expr, complain);
    6596     36599116 :     }
    6597              :   return expr;
    6598              : }
    6599              : 
    6600              : /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
    6601              :    expression is dependent or non-constant.  */
    6602              : 
    6603              : tree
    6604    178972063 : instantiate_non_dependent_or_null (tree expr)
    6605              : {
    6606    178972063 :   if (expr == NULL_TREE)
    6607              :     return NULL_TREE;
    6608    166656424 :   if (processing_template_decl)
    6609              :     {
    6610       492322 :       if (!is_nondependent_constant_expression (expr))
    6611              :         expr = NULL_TREE;
    6612              :       else
    6613              :         {
    6614       486263 :           processing_template_decl_sentinel s;
    6615       486263 :           expr = instantiate_non_dependent_expr_internal (expr, tf_error);
    6616       486263 :         }
    6617              :     }
    6618              :   return expr;
    6619              : }
    6620              : 
    6621              : /* True iff T is a specialization of a variable template.  */
    6622              : 
    6623              : bool
    6624    278748305 : variable_template_specialization_p (tree t)
    6625              : {
    6626    278748305 :   if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
    6627              :     return false;
    6628     30093024 :   tree tmpl = DECL_TI_TEMPLATE (t);
    6629     30093024 :   return variable_template_p (tmpl);
    6630              : }
    6631              : 
    6632              : /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
    6633              :    template declaration, or a TYPE_DECL for an alias declaration.  */
    6634              : 
    6635              : bool
    6636    148652344 : alias_type_or_template_p (tree t)
    6637              : {
    6638    148652344 :   if (t == NULL_TREE)
    6639              :     return false;
    6640            0 :   return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
    6641    148652344 :           || (TYPE_P (t)
    6642    148652344 :               && TYPE_NAME (t)
    6643    148652344 :               && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    6644    290145365 :           || DECL_ALIAS_TEMPLATE_P (t));
    6645              : }
    6646              : 
    6647              : /* If T is a specialization of an alias template, return it; otherwise return
    6648              :    NULL_TREE.  If TRANSPARENT_TYPEDEFS is true, look through other aliases.  */
    6649              : 
    6650              : tree
    6651  12655332610 : alias_template_specialization_p (const_tree t,
    6652              :                                  bool transparent_typedefs)
    6653              : {
    6654  12655332610 :   if (!TYPE_P (t))
    6655              :     return NULL_TREE;
    6656              : 
    6657              :   /* It's an alias template specialization if it's an alias and its
    6658              :      TYPE_NAME is a specialization of a primary template.  */
    6659  12655332552 :   if (typedef_variant_p (t))
    6660              :     {
    6661    831636478 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6662    759182572 :         if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    6663              :           return const_cast<tree> (t);
    6664    530298124 :       if (transparent_typedefs && !dependent_opaque_alias_p (t))
    6665       121782 :         return alias_template_specialization_p (DECL_ORIGINAL_TYPE
    6666              :                                                 (TYPE_NAME (t)),
    6667       121782 :                                                 transparent_typedefs);
    6668              :     }
    6669              : 
    6670              :   return NULL_TREE;
    6671              : }
    6672              : 
    6673              : /* A cache of the result of complex_alias_template_p.  */
    6674              : 
    6675              : static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
    6676              : 
    6677              : /* Data structure for complex_alias_template_*.  */
    6678              : 
    6679              : struct uses_all_template_parms_data
    6680              : {
    6681              :   int level;
    6682              :   tree *seen;
    6683              : };
    6684              : 
    6685              : /* walk_tree callback for complex_alias_template_p.  */
    6686              : 
    6687              : static tree
    6688     56956072 : complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
    6689              : {
    6690     56956072 :   tree t = *tp;
    6691     56956072 :   auto &data = *(struct uses_all_template_parms_data*)data_;
    6692              : 
    6693     56956072 :   switch (TREE_CODE (t))
    6694              :     {
    6695     10688710 :     case TEMPLATE_TYPE_PARM:
    6696     10688710 :     case TEMPLATE_PARM_INDEX:
    6697     10688710 :     case TEMPLATE_TEMPLATE_PARM:
    6698     10688710 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    6699     10688710 :       {
    6700     10688710 :         tree idx = get_template_parm_index (t);
    6701     10688710 :         if (TEMPLATE_PARM_LEVEL (idx) == data.level)
    6702      9056191 :           data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
    6703              :       }
    6704              : 
    6705     56956072 :     default:;
    6706              :     }
    6707              : 
    6708     56956072 :   if (!PACK_EXPANSION_P (t))
    6709              :     return 0;
    6710              : 
    6711              :   /* An alias template with a pack expansion that expands a pack from the
    6712              :      enclosing class needs to be considered complex, to avoid confusion with
    6713              :      the same pack being used as an argument to the alias's own template
    6714              :      parameter (91966).  */
    6715      2932442 :   for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
    6716       961767 :        pack = TREE_CHAIN (pack))
    6717              :     {
    6718      1050295 :       tree parm_pack = TREE_VALUE (pack);
    6719      1050295 :       if (!TEMPLATE_PARM_P (parm_pack))
    6720        37199 :         continue;
    6721      1013096 :       int idx, level;
    6722      1013096 :       template_parm_level_and_index (parm_pack, &level, &idx);
    6723      1013096 :       if (level < data.level)
    6724        88528 :         return t;
    6725              : 
    6726              :       /* Consider the expanded packs to be used outside the expansion...  */
    6727       924568 :       data.seen[idx] = boolean_true_node;
    6728              :     }
    6729              : 
    6730              :   /* ...but don't walk into the pattern.  Consider PR104008:
    6731              : 
    6732              :      template <typename T, typename... Ts>
    6733              :      using IsOneOf = disjunction<is_same<T, Ts>...>;
    6734              : 
    6735              :      where IsOneOf seemingly uses all of its template parameters in its
    6736              :      expansion (and does not expand a pack from the enclosing class), so the
    6737              :      alias was not marked as complex.  However, if it is used like
    6738              :      "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
    6739              :      expansion.  So only Ts is considered used by the pack expansion.  */
    6740       949972 :   *walk_subtrees = false;
    6741              : 
    6742       949972 :   return 0;
    6743              : }
    6744              : 
    6745              : /* An alias template is complex from a SFINAE perspective if a template-id
    6746              :    using that alias can be ill-formed when the expansion is not, as with
    6747              :    the void_t template.
    6748              : 
    6749              :    If this predicate returns true in the ordinary case, the out parameter
    6750              :    SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
    6751              :    the I'th template parameter of the alias template is used in the alias.  */
    6752              : 
    6753              : static bool
    6754    143399791 : complex_alias_template_p (const_tree tmpl, tree *seen_out)
    6755              : {
    6756    143399791 :   tmpl = most_general_template (tmpl);
    6757    143399791 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6758              :     return false;
    6759              : 
    6760              :   /* A renaming alias isn't complex.  */
    6761    103168389 :   if (get_underlying_template (const_cast<tree> (tmpl)) != tmpl)
    6762              :     return false;
    6763              : 
    6764              :   /* Any other constrained alias is complex.  */
    6765     95631614 :   if (get_constraints (tmpl))
    6766              :     return true;
    6767              : 
    6768              :   /* An alias with dependent type attributes is complex.  */
    6769     74222054 :   if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
    6770              :     return true;
    6771              : 
    6772     74221979 :   if (!complex_alias_tmpl_info)
    6773        92390 :     complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
    6774              : 
    6775     74221979 :   if (tree *slot = complex_alias_tmpl_info->get (tmpl))
    6776              :     {
    6777     67739587 :       tree result = *slot;
    6778     67739587 :       if (result == boolean_false_node)
    6779              :         return false;
    6780      8720469 :       if (result == boolean_true_node)
    6781              :         return true;
    6782      7956935 :       gcc_assert (TREE_CODE (result) == TREE_VEC);
    6783      7956935 :       if (seen_out)
    6784      7956935 :         *seen_out = result;
    6785      7956935 :       return true;
    6786              :     }
    6787              : 
    6788      6482392 :   struct uses_all_template_parms_data data;
    6789      6482392 :   tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6790      6482392 :   tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6791      6482392 :   data.level = TMPL_PARMS_DEPTH (parms);
    6792      6482392 :   int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
    6793      6482392 :   tree seen = make_tree_vec (len);
    6794      6482392 :   data.seen = TREE_VEC_BEGIN (seen);
    6795     16074102 :   for (int i = 0; i < len; ++i)
    6796      9591710 :     data.seen[i] = boolean_false_node;
    6797              : 
    6798      6482392 :   if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
    6799              :     {
    6800        88528 :       complex_alias_tmpl_info->put (tmpl, boolean_true_node);
    6801        88528 :       return true;
    6802              :     }
    6803              : 
    6804     15062030 :   for (int i = 0; i < len; ++i)
    6805      9136329 :     if (data.seen[i] != boolean_true_node)
    6806              :       {
    6807       468163 :         complex_alias_tmpl_info->put (tmpl, seen);
    6808       468163 :         if (seen_out)
    6809       468163 :           *seen_out = seen;
    6810       468163 :         return true;
    6811              :       }
    6812              : 
    6813      5925701 :   complex_alias_tmpl_info->put (tmpl, boolean_false_node);
    6814      5925701 :   return false;
    6815              : }
    6816              : 
    6817              : /* If T is a specialization of a complex alias template with a dependent
    6818              :    argument for an unused template parameter, return it; otherwise return
    6819              :    NULL_TREE.  If T is a typedef to such a specialization, return the
    6820              :    specialization.  This predicate is usually checked alongside
    6821              :    dependent_opaque_alias_p.  Whereas dependent_opaque_alias_p checks
    6822              :    type equivalence of an alias vs its expansion, this predicate more
    6823              :    broadly checks SFINAE equivalence.  */
    6824              : 
    6825              : tree
    6826    383573305 : dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
    6827              : {
    6828    383573305 :   if (t == error_mark_node)
    6829              :     return NULL_TREE;
    6830    328288842 :   gcc_assert (TYPE_P (t));
    6831              : 
    6832    328288842 :   if (!processing_template_decl || !typedef_variant_p (t))
    6833              :     return NULL_TREE;
    6834              : 
    6835    156997794 :   if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6836              :     {
    6837    143399791 :       tree seen = NULL_TREE;
    6838    143399791 :       if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
    6839              :         {
    6840     30686795 :           tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
    6841     30686795 :           if (!seen)
    6842              :             {
    6843     22261697 :               if (any_dependent_template_arguments_p (args))
    6844     30686795 :                 return const_cast<tree> (t);
    6845              :             }
    6846              :           else
    6847              :             {
    6848      8425098 :               gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
    6849     14501914 :               for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    6850     12395976 :                 if (TREE_VEC_ELT (seen, i) != boolean_true_node
    6851     12395976 :                     && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
    6852              :                   return const_cast<tree> (t);
    6853              :             }
    6854              : 
    6855      2190126 :           return NULL_TREE;
    6856              :         }
    6857              :     }
    6858              : 
    6859    126310999 :   if (transparent_typedefs && !dependent_opaque_alias_p (t))
    6860              :     {
    6861      4381320 :       tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
    6862      4381320 :       return dependent_alias_template_spec_p (utype, transparent_typedefs);
    6863              :     }
    6864              : 
    6865              :   return NULL_TREE;
    6866              : }
    6867              : 
    6868              : /* Return true if substituting into T would yield a different type than
    6869              :    substituting into its expansion.  This predicate is usually checked
    6870              :    alongside dependent_alias_template_spec_p.  */
    6871              : 
    6872              : bool
    6873    603152023 : dependent_opaque_alias_p (const_tree t)
    6874              : {
    6875    603152023 :   return (TYPE_P (t)
    6876    603152023 :           && typedef_variant_p (t)
    6877   1183271318 :           && (any_dependent_type_attributes_p (DECL_ATTRIBUTES
    6878              :                                                (TYPE_NAME (t)))
    6879              :               /* Treat a dependent decltype(lambda) alias as opaque so that we
    6880              :                  don't prematurely strip it when used as a template argument.
    6881              :                  Otherwise substitution into each occurrence of the (stripped)
    6882              :                  alias would incorrectly yield a distinct lambda type.  */
    6883    580118628 :               || (TREE_CODE (t) == DECLTYPE_TYPE
    6884     22577878 :                   && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == LAMBDA_EXPR
    6885          110 :                   && !typedef_variant_p (DECL_ORIGINAL_TYPE (TYPE_NAME (t))))));
    6886              : }
    6887              : 
    6888              : /* Return the number of innermost template parameters in TMPL.  */
    6889              : 
    6890              : static int
    6891     85653898 : num_innermost_template_parms (const_tree tmpl)
    6892              : {
    6893     85653898 :   tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6894     85653898 :   return TREE_VEC_LENGTH (parms);
    6895              : }
    6896              : 
    6897              : /* Return either TMPL or another template that it is equivalent to under DR
    6898              :    1286: An alias that just changes the name of a template is equivalent to
    6899              :    the other template.  */
    6900              : 
    6901              : static tree
    6902    119457922 : get_underlying_template (tree tmpl)
    6903              : {
    6904    119457922 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
    6905    126996333 :   while (DECL_ALIAS_TEMPLATE_P (tmpl))
    6906              :     {
    6907              :       /* Determine if the alias is equivalent to an underlying template.  */
    6908    110653026 :       tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6909              :       /* The underlying type may have been ill-formed. Don't proceed.  */
    6910    110653026 :       if (!orig_type)
    6911              :         break;
    6912    110653023 :       tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
    6913     42828691 :       if (!tinfo)
    6914              :         break;
    6915              : 
    6916     42827033 :       tree underlying = TI_TEMPLATE (tinfo);
    6917     42827033 :       if (!PRIMARY_TEMPLATE_P (underlying)
    6918     85653982 :           || (num_innermost_template_parms (tmpl)
    6919     42826949 :               != num_innermost_template_parms (underlying)))
    6920              :         break;
    6921              : 
    6922              :       /* Does the alias add cv-quals?  */
    6923     23387238 :       if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
    6924              :         break;
    6925              : 
    6926     23387215 :       tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
    6927     23387215 :       if (!comp_template_args (TI_ARGS (tinfo), alias_args))
    6928              :         break;
    6929              : 
    6930              :       /* Are any default template arguments equivalent?  */
    6931      7538623 :       tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6932      7538623 :       tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
    6933      7538623 :       const int nparms = TREE_VEC_LENGTH (aparms);
    6934     16196796 :       for (int i = 0; i < nparms; ++i)
    6935              :         {
    6936      8658314 :           tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
    6937      8658314 :           tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
    6938      8658314 :           if (!template_args_equal (adefarg, udefarg))
    6939          141 :             goto top_break;
    6940              :         }
    6941              : 
    6942              :       /* If TMPL adds or changes any constraints, it isn't equivalent.  I think
    6943              :          it's appropriate to treat a less-constrained alias as equivalent.  */
    6944      7538482 :       if (!at_least_as_constrained (underlying, tmpl))
    6945              :         break;
    6946              : 
    6947              :       /* If TMPL adds dependent type attributes, it isn't equivalent.  */
    6948      7538419 :       if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
    6949              :         break;
    6950              : 
    6951              :       /* Alias is equivalent.  Strip it and repeat.  */
    6952              :       tmpl = underlying;
    6953              :     }
    6954     15850347 :   top_break:;
    6955              : 
    6956    119457922 :   return tmpl;
    6957              : }
    6958              : 
    6959              : /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
    6960              :    must be a reference-to-function or a pointer-to-function type, as specified
    6961              :    in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
    6962              :    and check that the resulting function has external linkage.  */
    6963              : 
    6964              : static tree
    6965         1011 : convert_nontype_argument_function (tree type, tree expr,
    6966              :                                    tsubst_flags_t complain)
    6967              : {
    6968         1011 :   tree fns = expr;
    6969         1011 :   tree fn, fn_no_ptr;
    6970         1011 :   linkage_kind linkage;
    6971              : 
    6972         1011 :   fn = instantiate_type (type, fns, tf_none);
    6973         1011 :   if (fn == error_mark_node)
    6974              :     return error_mark_node;
    6975              : 
    6976         1006 :   if (value_dependent_expression_p (fn))
    6977           21 :     goto accept;
    6978              : 
    6979          985 :   fn_no_ptr = fn;
    6980          985 :   if (REFERENCE_REF_P (fn_no_ptr))
    6981           12 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    6982          985 :   fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
    6983          985 :   if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
    6984          857 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    6985          985 :   if (BASELINK_P (fn_no_ptr))
    6986            0 :     fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
    6987              : 
    6988              :   /* [temp.arg.nontype]/1
    6989              : 
    6990              :      A template-argument for a non-type, non-template template-parameter
    6991              :      shall be one of:
    6992              :      [...]
    6993              :      -- the address of an object or function with external [C++11: or
    6994              :         internal] linkage.  */
    6995              : 
    6996          985 :   STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
    6997          985 :   if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
    6998              :     {
    6999           35 :       if (complain & tf_error)
    7000              :         {
    7001           20 :           auto_diagnostic_group d;
    7002           20 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    7003           20 :           tree c;
    7004           20 :           if (cxx_dialect >= cxx17
    7005           20 :               && (c = cxx_constant_value (fn),
    7006           15 :                   c == error_mark_node))
    7007              :             ;
    7008              :           else
    7009              :             {
    7010            5 :               error_at (loc, "%qE is not a valid template argument for "
    7011              :                         "type %qT", expr, type);
    7012            5 :               if (TYPE_PTR_P (type))
    7013            5 :                 inform (loc, "it must be the address of a function "
    7014              :                         "with external linkage");
    7015              :               else
    7016            0 :                 inform (loc, "it must be the name of a function with "
    7017              :                         "external linkage");
    7018              :             }
    7019           20 :         }
    7020           35 :       return NULL_TREE;
    7021              :     }
    7022              : 
    7023          950 :   linkage = decl_linkage (fn_no_ptr);
    7024          950 :   if ((cxx_dialect < cxx11 && linkage != lk_external)
    7025          949 :       || (cxx_dialect < cxx17 && linkage == lk_none))
    7026              :     {
    7027            5 :       if (complain & tf_error)
    7028              :         {
    7029            2 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    7030            2 :           if (cxx_dialect >= cxx11)
    7031            2 :             error_at (loc, "%qE is not a valid template argument for type "
    7032              :                       "%qT because %qD has no linkage",
    7033              :                       expr, type, fn_no_ptr);
    7034              :           else
    7035            0 :             error_at (loc, "%qE is not a valid template argument for type "
    7036              :                       "%qT because %qD does not have external linkage",
    7037              :                       expr, type, fn_no_ptr);
    7038              :         }
    7039            5 :       return NULL_TREE;
    7040              :     }
    7041              : 
    7042          945 :  accept:
    7043          966 :   if (TYPE_REF_P (type))
    7044              :     {
    7045          104 :       if (REFERENCE_REF_P (fn))
    7046           11 :         fn = TREE_OPERAND (fn, 0);
    7047              :       else
    7048           93 :         fn = build_address (fn);
    7049              :     }
    7050          966 :   if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
    7051           93 :     fn = build_nop (type, fn);
    7052              : 
    7053              :   return fn;
    7054              : }
    7055              : 
    7056              : /* Subroutine of convert_nontype_argument.
    7057              :    Check if EXPR of type TYPE is a valid pointer-to-member constant.
    7058              :    Emit an error otherwise.  */
    7059              : 
    7060              : static bool
    7061         1225 : check_valid_ptrmem_cst_expr (tree type, tree expr,
    7062              :                              tsubst_flags_t complain)
    7063              : {
    7064         1225 :   tree orig_expr = expr;
    7065         1225 :   STRIP_NOPS (expr);
    7066         1225 :   if (null_ptr_cst_p (expr))
    7067              :     return true;
    7068         1225 :   if (TREE_CODE (expr) == PTRMEM_CST
    7069         1225 :       && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
    7070              :                       PTRMEM_CST_CLASS (expr)))
    7071              :     return true;
    7072          130 :   if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
    7073              :     return true;
    7074           37 :   if (processing_template_decl
    7075            0 :       && TREE_CODE (expr) == ADDR_EXPR
    7076           37 :       && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
    7077              :     return true;
    7078           37 :   if (complain & tf_error)
    7079              :     {
    7080           19 :       auto_diagnostic_group d;
    7081           19 :       location_t loc = cp_expr_loc_or_input_loc (orig_expr);
    7082           19 :       error_at (loc, "%qE is not a valid template argument for type %qT",
    7083              :                 orig_expr, type);
    7084           19 :       if (TREE_CODE (expr) != PTRMEM_CST)
    7085           10 :         inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
    7086              :       else
    7087            9 :         inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
    7088           19 :     }
    7089              :   return false;
    7090              : }
    7091              : 
    7092              : /* Returns TRUE iff the address of OP is value-dependent.
    7093              : 
    7094              :    14.6.2.4 [temp.dep.temp]:
    7095              :    A non-integral non-type template-argument is dependent if its type is
    7096              :    dependent or it has either of the following forms
    7097              :      qualified-id
    7098              :      & qualified-id
    7099              :    and contains a nested-name-specifier which specifies a class-name that
    7100              :    names a dependent type.
    7101              : 
    7102              :    We generalize this to just say that the address of a member of a
    7103              :    dependent class is value-dependent; the above doesn't cover the
    7104              :    address of a static data member named with an unqualified-id.  */
    7105              : 
    7106              : static bool
    7107      5324819 : has_value_dependent_address (tree op)
    7108              : {
    7109      5324819 :   STRIP_ANY_LOCATION_WRAPPER (op);
    7110              : 
    7111              :   /* We could use get_inner_reference here, but there's no need;
    7112              :      this is only relevant for template non-type arguments, which
    7113              :      can only be expressed as &id-expression.  */
    7114      5324819 :   if (DECL_P (op))
    7115              :     {
    7116       226014 :       tree ctx = CP_DECL_CONTEXT (op);
    7117              : 
    7118       226014 :       if (TYPE_P (ctx) && dependent_type_p (ctx))
    7119              :         return true;
    7120              : 
    7121       207581 :       if (VAR_P (op)
    7122        38506 :           && TREE_STATIC (op)
    7123        10166 :           && TREE_CODE (ctx) == FUNCTION_DECL
    7124       207979 :           && type_dependent_expression_p (ctx))
    7125              :         return true;
    7126              :     }
    7127              : 
    7128              :   return false;
    7129              : }
    7130              : 
    7131              : /* The next set of functions are used for providing helpful explanatory
    7132              :    diagnostics for failed overload resolution.  Their messages should be
    7133              :    indented by two spaces for consistency with the messages in
    7134              :    call.cc  */
    7135              : 
    7136              : static int
    7137            0 : unify_success (bool /*explain_p*/)
    7138              : {
    7139            0 :   return 0;
    7140              : }
    7141              : 
    7142              : /* Other failure functions should call this one, to provide a single function
    7143              :    for setting a breakpoint on.  */
    7144              : 
    7145              : static int
    7146            0 : unify_invalid (bool /*explain_p*/)
    7147              : {
    7148            0 :   return 1;
    7149              : }
    7150              : 
    7151              : static int
    7152       193191 : unify_parameter_deduction_failure (bool explain_p, tree parm)
    7153              : {
    7154            0 :   if (explain_p)
    7155          268 :     inform (input_location,
    7156              :             "  couldn%'t deduce template parameter %qD", parm);
    7157            0 :   return unify_invalid (explain_p);
    7158              : }
    7159              : 
    7160              : static int
    7161      3859879 : unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
    7162              : {
    7163            0 :   if (explain_p)
    7164           56 :     inform (input_location,
    7165              :             "  types %qT and %qT have incompatible cv-qualifiers",
    7166              :             parm, arg);
    7167            0 :   return unify_invalid (explain_p);
    7168              : }
    7169              : 
    7170              : static int
    7171    370402161 : unify_type_mismatch (bool explain_p, tree parm, tree arg)
    7172              : {
    7173         6191 :   if (explain_p)
    7174          511 :     inform (input_location, "  mismatched types %qT and %qT", parm, arg);
    7175         6191 :   return unify_invalid (explain_p);
    7176              : }
    7177              : 
    7178              : static int
    7179       507866 : unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
    7180              : {
    7181            0 :   if (explain_p)
    7182            0 :     inform (input_location,
    7183              :             "  template parameter %qD is not a parameter pack, but "
    7184              :             "argument %qD is",
    7185              :             parm, arg);
    7186            0 :   return unify_invalid (explain_p);
    7187              : }
    7188              : 
    7189              : static int
    7190            3 : unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
    7191              : {
    7192            0 :   if (explain_p)
    7193            0 :     inform (input_location,
    7194              :             "  template argument %qE does not match "
    7195              :             "pointer-to-member constant %qE",
    7196              :             arg, parm);
    7197            0 :   return unify_invalid (explain_p);
    7198              : }
    7199              : 
    7200              : static int
    7201           14 : unify_expression_unequal (bool explain_p, tree parm, tree arg)
    7202              : {
    7203            3 :   if (explain_p)
    7204            0 :     inform (input_location, "  %qE is not equivalent to %qE", parm, arg);
    7205            3 :   return unify_invalid (explain_p);
    7206              : }
    7207              : 
    7208              : static int
    7209           18 : unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
    7210              : {
    7211            0 :   if (explain_p)
    7212            3 :     inform (input_location,
    7213              :             "  inconsistent parameter pack deduction with %qT and %qT",
    7214              :             old_arg, new_arg);
    7215           18 :   return unify_invalid (explain_p);
    7216              : }
    7217              : 
    7218              : static int
    7219      1260700 : unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
    7220              : {
    7221      1260700 :   if (explain_p)
    7222              :     {
    7223           60 :       if (TYPE_P (parm))
    7224           54 :         inform (input_location,
    7225              :                 "  deduced conflicting types for parameter %qT (%qT and %qT)",
    7226              :                 parm, first, second);
    7227              :       else
    7228            6 :         inform (input_location,
    7229              :                 "  deduced conflicting values for non-type parameter "
    7230              :                 "%qE (%qE and %qE)", parm, first, second);
    7231              :     }
    7232      1260700 :   return unify_invalid (explain_p);
    7233              : }
    7234              : 
    7235              : static int
    7236           20 : unify_vla_arg (bool explain_p, tree arg)
    7237              : {
    7238            0 :   if (explain_p)
    7239            9 :     inform (input_location,
    7240              :             "  variable-sized array type %qT is not "
    7241              :             "a valid template argument",
    7242              :             arg);
    7243            0 :   return unify_invalid (explain_p);
    7244              : }
    7245              : 
    7246              : static int
    7247            6 : unify_method_type_error (bool explain_p, tree arg)
    7248              : {
    7249            0 :   if (explain_p)
    7250            0 :     inform (input_location,
    7251              :             "  member function type %qT is not a valid template argument",
    7252              :             arg);
    7253            0 :   return unify_invalid (explain_p);
    7254              : }
    7255              : 
    7256              : static int
    7257      4075742 : unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
    7258              : {
    7259      4075742 :   if (explain_p)
    7260              :     {
    7261            3 :       if (least_p)
    7262            0 :         inform_n (input_location, wanted,
    7263              :                   "  candidate expects at least %d argument, %d provided",
    7264              :                   "  candidate expects at least %d arguments, %d provided",
    7265              :                   wanted, have);
    7266              :       else
    7267            3 :         inform_n (input_location, wanted,
    7268              :                   "  candidate expects %d argument, %d provided",
    7269              :                   "  candidate expects %d arguments, %d provided",
    7270              :                   wanted, have);
    7271              :     }
    7272      4075742 :   return unify_invalid (explain_p);
    7273              : }
    7274              : 
    7275              : static int
    7276        47158 : unify_too_many_arguments (bool explain_p, int have, int wanted)
    7277              : {
    7278            0 :   return unify_arity (explain_p, have, wanted);
    7279              : }
    7280              : 
    7281              : static int
    7282        23675 : unify_too_few_arguments (bool explain_p, int have, int wanted,
    7283              :                          bool least_p = false)
    7284              : {
    7285            0 :   return unify_arity (explain_p, have, wanted, least_p);
    7286              : }
    7287              : 
    7288              : static int
    7289      2794865 : unify_arg_conversion (bool explain_p, tree to_type,
    7290              :                       tree from_type, tree arg)
    7291              : {
    7292      2794865 :   if (explain_p)
    7293          239 :     inform (cp_expr_loc_or_input_loc (arg),
    7294              :             "  cannot convert %qE (type %qT) to type %qT",
    7295              :             arg, from_type, to_type);
    7296      2794865 :   return unify_invalid (explain_p);
    7297              : }
    7298              : 
    7299              : static int
    7300    222876470 : unify_no_common_base (bool explain_p, enum template_base_result r,
    7301              :                       tree parm, tree arg)
    7302              : {
    7303    222876470 :   if (explain_p)
    7304          852 :     switch (r)
    7305              :       {
    7306            3 :       case tbr_ambiguous_baseclass:
    7307            3 :         inform (input_location, "  %qT is an ambiguous base class of %qT",
    7308              :                 parm, arg);
    7309            3 :         break;
    7310          849 :       default:
    7311          849 :         inform (input_location, "  %qT is not derived from %qT", arg, parm);
    7312          849 :         break;
    7313              :       }
    7314    222876470 :   return unify_invalid (explain_p);
    7315              : }
    7316              : 
    7317              : static int
    7318           24 : unify_inconsistent_template_template_parameters (bool explain_p)
    7319              : {
    7320           24 :   if (explain_p)
    7321           10 :     inform (input_location,
    7322              :             "  template parameters of a template template argument are "
    7323              :             "inconsistent with other deduced template arguments");
    7324           24 :   return unify_invalid (explain_p);
    7325              : }
    7326              : 
    7327              : static int
    7328          948 : unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
    7329              : {
    7330            0 :   if (explain_p)
    7331            0 :     inform (input_location,
    7332              :             "  cannot deduce a template for %qT from non-template type %qT",
    7333              :             parm, arg);
    7334            0 :   return unify_invalid (explain_p);
    7335              : }
    7336              : 
    7337              : static int
    7338     11938953 : unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
    7339              : {
    7340            0 :   if (explain_p)
    7341            9 :     inform (input_location,
    7342              :             "  template argument %qE does not match %qE", arg, parm);
    7343            0 :   return unify_invalid (explain_p);
    7344              : }
    7345              : 
    7346              : /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
    7347              :    argument for TYPE, points to an unsuitable object.
    7348              : 
    7349              :    Also adjust the type of the index in C++20 array subobject references.  */
    7350              : 
    7351              : static bool
    7352        81063 : invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
    7353              : {
    7354        81090 :   switch (TREE_CODE (expr))
    7355              :     {
    7356           27 :     CASE_CONVERT:
    7357           27 :       return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
    7358           27 :                                        complain);
    7359              : 
    7360            0 :     case TARGET_EXPR:
    7361            0 :       return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
    7362            0 :                                        complain);
    7363              : 
    7364        39606 :     case CONSTRUCTOR:
    7365        39606 :       {
    7366       164659 :         for (auto &e: CONSTRUCTOR_ELTS (expr))
    7367        52850 :           if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
    7368              :             return true;
    7369              :       }
    7370              :       break;
    7371              : 
    7372         1631 :     case ADDR_EXPR:
    7373         1631 :       {
    7374         1631 :         tree decl = TREE_OPERAND (expr, 0);
    7375              : 
    7376         1631 :         if (cxx_dialect >= cxx20)
    7377         1515 :           while (TREE_CODE (decl) == COMPONENT_REF
    7378         1515 :                  || TREE_CODE (decl) == ARRAY_REF)
    7379              :             {
    7380          149 :               tree &op = TREE_OPERAND (decl, 1);
    7381          149 :               if (TREE_CODE (decl) == ARRAY_REF
    7382           56 :                   && TREE_CODE (op) == INTEGER_CST)
    7383              :                 /* Canonicalize array offsets to ptrdiff_t; how they were
    7384              :                    written doesn't matter for subobject identity.  */
    7385           56 :                 op = fold_convert (ptrdiff_type_node, op);
    7386          149 :               decl = TREE_OPERAND (decl, 0);
    7387              :             }
    7388              : 
    7389         1631 :         if (!VAR_OR_FUNCTION_DECL_P (decl))
    7390              :           {
    7391           23 :             if (complain & tf_error)
    7392           21 :               error_at (cp_expr_loc_or_input_loc (expr),
    7393              :                         "%qE is not a valid template argument of type %qT "
    7394              :                         "because %qE is not a variable or function",
    7395              :                         expr, type, decl);
    7396           23 :             return true;
    7397              :           }
    7398         1608 :         else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
    7399              :           {
    7400            2 :             if (complain & tf_error)
    7401            3 :               error_at (cp_expr_loc_or_input_loc (expr),
    7402              :                         "%qE is not a valid template argument of type %qT "
    7403              :                         "in C++98 because %qD does not have external linkage",
    7404              :                         expr, type, decl);
    7405            2 :             return true;
    7406              :           }
    7407         1606 :         else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
    7408         1606 :                  && decl_linkage (decl) == lk_none)
    7409              :           {
    7410            7 :             if (complain & tf_error)
    7411           14 :               error_at (cp_expr_loc_or_input_loc (expr),
    7412              :                         "%qE is not a valid template argument of type %qT "
    7413              :                         "because %qD has no linkage", expr, type, decl);
    7414            7 :             return true;
    7415              :           }
    7416              :         /* C++17: For a non-type template-parameter of reference or pointer
    7417              :            type, the value of the constant expression shall not refer to (or
    7418              :            for a pointer type, shall not be the address of):
    7419              :            * a subobject (4.5),
    7420              :            * a temporary object (15.2),
    7421              :            * a string literal (5.13.5),
    7422              :            * the result of a typeid expression (8.2.8), or
    7423              :            * a predefined __func__ variable (11.4.1).  */
    7424         1587 :         else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)
    7425         1654 :                  && !DECL_NTTP_OBJECT_P (decl))
    7426              :           {
    7427            0 :             gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl));
    7428            0 :             if (complain & tf_error)
    7429            0 :               error ("the address of %qD is not a valid template argument",
    7430              :                      decl);
    7431            0 :             return true;
    7432              :           }
    7433         1599 :         else if (cxx_dialect < cxx20
    7434         1851 :                  && !(same_type_ignoring_top_level_qualifiers_p
    7435          252 :                       (strip_array_types (TREE_TYPE (type)),
    7436          252 :                        strip_array_types (TREE_TYPE (decl)))))
    7437              :           {
    7438            6 :             if (complain & tf_error)
    7439            4 :               error ("the address of the %qT subobject of %qD is not a "
    7440            4 :                      "valid template argument", TREE_TYPE (type), decl);
    7441            6 :             return true;
    7442              :           }
    7443         1593 :         else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
    7444              :           {
    7445            1 :             if (complain & tf_error)
    7446            1 :               error ("the address of %qD is not a valid template argument "
    7447              :                      "because it does not have static storage duration",
    7448              :                      decl);
    7449            1 :             return true;
    7450              :           }
    7451              :       }
    7452              :       break;
    7453              : 
    7454        39826 :     default:
    7455        39826 :       if (!INDIRECT_TYPE_P (type))
    7456              :         /* We're only concerned about pointers and references here.  */;
    7457          188 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7458              :         /* Null pointer values are OK in C++11.  */;
    7459              :       else
    7460              :         {
    7461           17 :           tree c;
    7462           17 :           if (!(complain & tf_error))
    7463              :             ;
    7464           11 :           else if (cxx_dialect >= cxx17
    7465           11 :                    && (c = cxx_constant_value (expr),
    7466            7 :                        c == error_mark_node))
    7467              :             ;
    7468            4 :           else if (VAR_P (expr))
    7469            2 :             error ("%qD is not a valid template argument "
    7470              :                    "because %qD is a variable, not the address of "
    7471              :                    "a variable", expr, expr);
    7472              :           else
    7473            2 :             error ("%qE is not a valid template argument for %qT "
    7474              :                    "because it is not the address of a variable",
    7475              :                    expr, type);
    7476           17 :           return true;
    7477              :         }
    7478              :     }
    7479              :   return false;
    7480              : 
    7481              : }
    7482              : 
    7483              : /* Return a VAR_DECL for the C++20 template parameter object corresponding to
    7484              :    template argument EXPR.  */
    7485              : 
    7486              : static tree
    7487        26632 : create_template_parm_object (tree expr, tsubst_flags_t complain)
    7488              : {
    7489        26632 :   tree orig = expr;
    7490        26632 :   if (TREE_CODE (expr) == TARGET_EXPR)
    7491        26632 :     expr = TARGET_EXPR_INITIAL (expr);
    7492              : 
    7493        26632 :   if (!TREE_CONSTANT (expr))
    7494              :     {
    7495           29 :       if ((complain & tf_error)
    7496           29 :           && require_rvalue_constant_expression (orig))
    7497           14 :         cxx_constant_value (orig);
    7498           29 :       return error_mark_node;
    7499              :     }
    7500        26603 :   if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
    7501            3 :     return error_mark_node;
    7502              : 
    7503              :   /* This is no longer a compound literal.  */
    7504        26600 :   gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
    7505              : 
    7506        26600 :   return get_template_parm_object (expr, mangle_template_parm_object (expr));
    7507              : }
    7508              : 
    7509              : /* The template arguments corresponding to template parameter objects of types
    7510              :    that contain pointers to members.  */
    7511              : 
    7512              : static GTY(()) hash_map<tree, tree> *tparm_obj_values;
    7513              : 
    7514              : /* Find or build an nttp object for (already-validated) EXPR with name
    7515              :    NAME.  When CHECK_INIT is false we don't need to process the initialiser,
    7516              :    it's already been done.  */
    7517              : 
    7518              : tree
    7519        27017 : get_template_parm_object (tree expr, tree name, bool check_init/*=true*/)
    7520              : {
    7521        27017 :   tree decl = get_global_binding (name);
    7522        27017 :   if (decl)
    7523              :     return decl;
    7524              : 
    7525         2058 :   tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
    7526         2058 :   decl = create_temporary_var (type);
    7527         2058 :   DECL_NTTP_OBJECT_P (decl) = true;
    7528         2058 :   DECL_CONTEXT (decl) = NULL_TREE;
    7529         2058 :   TREE_STATIC (decl) = true;
    7530         2058 :   DECL_DECLARED_CONSTEXPR_P (decl) = true;
    7531         2058 :   TREE_READONLY (decl) = true;
    7532         2058 :   DECL_NAME (decl) = name;
    7533         2058 :   SET_DECL_ASSEMBLER_NAME (decl, name);
    7534         2058 :   comdat_linkage (decl);
    7535              : 
    7536         2058 :   if (!zero_init_p (type))
    7537              :     {
    7538              :       /* If EXPR contains any PTRMEM_CST, they will get clobbered by
    7539              :          lower_var_init before we're done mangling.  So store the original
    7540              :          value elsewhere.  We only need to unshare EXPR if it's not yet
    7541              :          been processed.  */
    7542           36 :       tree copy = check_init ? unshare_constructor (expr) : expr;
    7543           36 :       hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
    7544              :     }
    7545              : 
    7546         2058 :   if (!check_init)
    7547              :     {
    7548              :       /* The EXPR is the already processed initializer, set it on the NTTP
    7549              :          object now so that cp_finish_decl doesn't do it again later.  */
    7550            5 :       gcc_checking_assert (reduced_constant_expression_p (expr));
    7551            5 :       DECL_INITIAL (decl) = expr;
    7552            5 :       DECL_INITIALIZED_P (decl) = true;
    7553            5 :       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
    7554              :       /* FIXME setting TREE_CONSTANT on refs breaks the back end.  */
    7555            5 :       if (!TYPE_REF_P (type))
    7556            5 :         TREE_CONSTANT (decl) = true;
    7557              :     }
    7558              : 
    7559         2058 :   pushdecl_top_level_and_finish (decl, expr);
    7560              : 
    7561         2058 :   return decl;
    7562              : }
    7563              : 
    7564              : /* Return the actual template argument corresponding to template parameter
    7565              :    object VAR.  */
    7566              : 
    7567              : tree
    7568        51666 : tparm_object_argument (tree var)
    7569              : {
    7570        51666 :   if (zero_init_p (TREE_TYPE (var)))
    7571        51631 :     return DECL_INITIAL (var);
    7572           35 :   return *(tparm_obj_values->get (var));
    7573              : }
    7574              : 
    7575              : /* Attempt to convert the non-type template parameter EXPR to the
    7576              :    indicated TYPE.  If the conversion is successful, return the
    7577              :    converted value.  If the conversion is unsuccessful, return
    7578              :    NULL_TREE if we issued an error message, or error_mark_node if we
    7579              :    did not.  If tf_error is not set in COMPLAIN, whether NULL_TREE
    7580              :    or error_mark_node is returned doesn't matter.  We issue error
    7581              :    messages for out-and-out bad template parameters, but not simply
    7582              :    because the conversion failed, since we might be just trying to
    7583              :    do argument deduction.  Both TYPE and EXPR must be non-dependent.
    7584              : 
    7585              :    The conversion follows the special rules described in
    7586              :    [temp.arg.nontype], and it is much more strict than an implicit
    7587              :    conversion.
    7588              : 
    7589              :    This function is called twice for each template argument (see
    7590              :    lookup_template_class for a more accurate description of this
    7591              :    problem). This means that we need to handle expressions which
    7592              :    are not valid in a C++ source, but can be created from the
    7593              :    first call (for instance, casts to perform conversions). These
    7594              :    hacks can go away after we fix the double coercion problem.  */
    7595              : 
    7596              : static tree
    7597    207120956 : convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
    7598              : {
    7599    207120956 :   tree expr_type;
    7600    207120956 :   location_t loc = cp_expr_loc_or_input_loc (expr);
    7601              : 
    7602              :   /* Detect immediately string literals as invalid non-type argument.
    7603              :      This special-case is not needed for correctness (we would easily
    7604              :      catch this later), but only to provide better diagnostic for this
    7605              :      common user mistake. As suggested by DR 100, we do not mention
    7606              :      linkage issues in the diagnostic as this is not the point.  */
    7607    207120956 :   if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
    7608              :     {
    7609            6 :       if (complain & tf_error)
    7610            6 :         error ("%qE is not a valid template argument for type %qT "
    7611              :                "because string literals can never be used in this context",
    7612              :                expr, type);
    7613            6 :       return NULL_TREE;
    7614              :     }
    7615              : 
    7616              :   /* Add the ADDR_EXPR now for the benefit of
    7617              :      value_dependent_expression_p.  */
    7618         2318 :   if (TYPE_PTROBV_P (type)
    7619    207122310 :       && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
    7620              :     {
    7621          104 :       expr = decay_conversion (expr, complain);
    7622          104 :       if (expr == error_mark_node)
    7623              :         return error_mark_node;
    7624              :     }
    7625              : 
    7626              :   /* If we are in a template, EXPR may be non-dependent, but still
    7627              :      have a syntactic, rather than semantic, form.  For example, EXPR
    7628              :      might be a SCOPE_REF, rather than the VAR_DECL to which the
    7629              :      SCOPE_REF refers.  Preserving the qualifying scope is necessary
    7630              :      so that access checking can be performed when the template is
    7631              :      instantiated -- but here we need the resolved form so that we can
    7632              :      convert the argument.  */
    7633    207120950 :   bool non_dep = false;
    7634          641 :   if (TYPE_REF_OBJ_P (type)
    7635    207121469 :       && has_value_dependent_address (expr))
    7636              :     /* If we want the address and it's value-dependent, don't fold.  */;
    7637    207120923 :   else if (processing_template_decl
    7638    207120923 :            && !instantiation_dependent_expression_p (expr))
    7639              :     non_dep = true;
    7640    207120950 :   if (error_operand_p (expr))
    7641          705 :     return error_mark_node;
    7642    207120245 :   expr_type = TREE_TYPE (expr);
    7643              : 
    7644              :   /* If the argument is non-dependent, perform any conversions in
    7645              :      non-dependent context as well.  */
    7646    207120245 :   processing_template_decl_sentinel s (non_dep);
    7647     18037771 :   if (non_dep)
    7648     18037771 :     expr = instantiate_non_dependent_expr_internal (expr, complain);
    7649              : 
    7650    207120245 :   bool val_dep_p = value_dependent_expression_p (expr);
    7651    207120245 :   if (val_dep_p)
    7652     23591732 :     expr = canonicalize_expr_argument (expr, complain);
    7653              :   else
    7654    183528513 :     STRIP_ANY_LOCATION_WRAPPER (expr);
    7655              : 
    7656              :   /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
    7657              :      to a non-type argument of "nullptr".  */
    7658    207120245 :   if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
    7659           99 :     expr = fold_simple (convert (type, expr));
    7660              : 
    7661              :   /* In C++11, integral or enumeration non-type template arguments can be
    7662              :      arbitrary constant expressions.  Pointer and pointer to
    7663              :      member arguments can be general constant expressions that evaluate
    7664              :      to a null value, but otherwise still need to be of a specific form.  */
    7665    207120245 :   if (cxx_dialect >= cxx11)
    7666              :     {
    7667    207087256 :       if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
    7668              :         /* A PTRMEM_CST is already constant, and a valid template
    7669              :            argument for a parameter of pointer to member type, we just want
    7670              :            to leave it in that form rather than lower it to a
    7671              :            CONSTRUCTOR.  */;
    7672    207086370 :       else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7673        31069 :                || cxx_dialect >= cxx17)
    7674              :         {
    7675              :           /* C++17: A template-argument for a non-type template-parameter shall
    7676              :              be a converted constant expression (8.20) of the type of the
    7677              :              template-parameter.  */
    7678    207086105 :           expr = build_converted_constant_expr (type, expr, complain);
    7679    207086105 :           if (expr == error_mark_node)
    7680              :             /* Make sure we return NULL_TREE only if we have really issued
    7681              :                an error, as described above.  */
    7682         1069 :             return (complain & tf_error) ? NULL_TREE : error_mark_node;
    7683    207085506 :           else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
    7684              :             {
    7685      3486709 :               IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
    7686      3486709 :               return expr;
    7687              :             }
    7688    203598797 :           expr = maybe_constant_value (expr, NULL_TREE, mce_true);
    7689    203598797 :           expr = convert_from_reference (expr);
    7690              :           /* EXPR may have become value-dependent.  */
    7691    203598797 :           val_dep_p = value_dependent_expression_p (expr);
    7692              :         }
    7693           74 :       else if (TYPE_PTR_OR_PTRMEM_P (type)
    7694          314 :                || NULLPTR_TYPE_P (type))
    7695              :         {
    7696          222 :           tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
    7697          253 :           if ((TYPE_PTR_P (type) || NULLPTR_TYPE_P (type))
    7698          228 :               ? integer_zerop (folded)
    7699           25 :               : null_member_pointer_value_p (folded))
    7700    203632937 :             expr = folded;
    7701              :         }
    7702              :     }
    7703              : 
    7704    203632937 :   if (TYPE_REF_P (type))
    7705          621 :     expr = mark_lvalue_use (expr);
    7706              :   else
    7707    203632316 :     expr = mark_rvalue_use (expr);
    7708              : 
    7709              :   /* HACK: Due to double coercion, we can get a
    7710              :      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
    7711              :      which is the tree that we built on the first call (see
    7712              :      below when coercing to reference to object or to reference to
    7713              :      function). We just strip everything and get to the arg.
    7714              :      See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
    7715              :      for examples.  */
    7716    203632937 :   if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
    7717              :     {
    7718              :       /* Check this before we strip *& to avoid redundancy.  */
    7719          621 :       if (!mark_single_function (expr, complain))
    7720            0 :         return error_mark_node;
    7721              : 
    7722          621 :       tree probe_type, probe = expr;
    7723          621 :       if (REFERENCE_REF_P (probe))
    7724          546 :         probe = TREE_OPERAND (probe, 0);
    7725          621 :       probe_type = TREE_TYPE (probe);
    7726          621 :       if (TREE_CODE (probe) == NOP_EXPR)
    7727              :         {
    7728              :           /* ??? Maybe we could use convert_from_reference here, but we
    7729              :              would need to relax its constraints because the NOP_EXPR
    7730              :              could actually change the type to something more cv-qualified,
    7731              :              and this is not folded by convert_from_reference.  */
    7732          506 :           tree addr = TREE_OPERAND (probe, 0);
    7733          506 :           if (TYPE_REF_P (probe_type)
    7734          506 :               && TREE_CODE (addr) == ADDR_EXPR
    7735          504 :               && TYPE_PTR_P (TREE_TYPE (addr))
    7736         1010 :               && (same_type_ignoring_top_level_qualifiers_p
    7737          504 :                   (TREE_TYPE (probe_type),
    7738          504 :                    TREE_TYPE (TREE_TYPE (addr)))))
    7739              :             {
    7740          494 :               expr = TREE_OPERAND (addr, 0);
    7741          494 :               expr_type = TREE_TYPE (probe_type);
    7742              :             }
    7743              :         }
    7744              :     }
    7745              : 
    7746              :   /* [temp.arg.nontype]/5, bullet 1
    7747              : 
    7748              :      For a non-type template-parameter of integral or enumeration type,
    7749              :      integral promotions (_conv.prom_) and integral conversions
    7750              :      (_conv.integral_) are applied.  */
    7751    203632937 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7752        31813 :       || SCALAR_FLOAT_TYPE_P (type))
    7753              :     {
    7754    203601404 :       if (cxx_dialect < cxx11)
    7755              :         {
    7756        32551 :           tree t = build_converted_constant_expr (type, expr, complain);
    7757        32551 :           t = maybe_constant_value (t);
    7758        32551 :           if (t != error_mark_node)
    7759    203601404 :             expr = t;
    7760              :         }
    7761              : 
    7762    203601404 :       if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
    7763           11 :         return error_mark_node;
    7764              : 
    7765              :       /* Notice that there are constant expressions like '4 % 0' which
    7766              :          do not fold into integer constants.  */
    7767    203601393 :       if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
    7768              :         {
    7769          165 :           if (complain & tf_error)
    7770              :             {
    7771           70 :               int errs = errorcount, warns = warningcount + werrorcount;
    7772           70 :               if (!require_potential_constant_expression (expr))
    7773           19 :                 expr = error_mark_node;
    7774              :               else
    7775           51 :                 expr = cxx_constant_value (expr);
    7776           70 :               if (errorcount > errs || warningcount + werrorcount > warns)
    7777           57 :                 inform (loc, "in template argument for type %qT", type);
    7778           70 :               if (expr == error_mark_node)
    7779              :                 return NULL_TREE;
    7780              :               /* else cxx_constant_value complained but gave us
    7781              :                  a real constant, so go ahead.  */
    7782            3 :               if (!CONSTANT_CLASS_P (expr))
    7783              :                 {
    7784              :                   /* Some assemble time constant expressions like
    7785              :                      (intptr_t)&&lab1 - (intptr_t)&&lab2 or
    7786              :                      4 + (intptr_t)&&var satisfy reduced_constant_expression_p
    7787              :                      as we can emit them into .rodata initializers of
    7788              :                      variables, yet they can't fold into an INTEGER_CST at
    7789              :                      compile time.  Refuse them here.  */
    7790            0 :                   gcc_checking_assert (reduced_constant_expression_p (expr));
    7791            0 :                   error_at (loc, "template argument %qE for type %qT not "
    7792              :                                  "a compile-time constant", expr, type);
    7793            0 :                   return NULL_TREE;
    7794              :                 }
    7795              :             }
    7796              :           else
    7797              :             return NULL_TREE;
    7798              :         }
    7799              : 
    7800              :       /* Avoid typedef problems.  */
    7801    203601231 :       if (TREE_TYPE (expr) != type)
    7802      1153143 :         expr = fold_convert (type, expr);
    7803              :     }
    7804              :   /* [temp.arg.nontype]/5, bullet 2
    7805              : 
    7806              :      For a non-type template-parameter of type pointer to object,
    7807              :      qualification conversions (_conv.qual_) and the array-to-pointer
    7808              :      conversion (_conv.array_) are applied.  */
    7809        31533 :   else if (TYPE_PTROBV_P (type))
    7810              :     {
    7811         1314 :       tree decayed = expr;
    7812              : 
    7813              :       /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
    7814              :          decay_conversion or an explicit cast.  If it's a problematic cast,
    7815              :          we'll complain about it below.  */
    7816         1314 :       if (TREE_CODE (expr) == NOP_EXPR)
    7817              :         {
    7818          185 :           tree probe = expr;
    7819          185 :           STRIP_NOPS (probe);
    7820          185 :           if (TREE_CODE (probe) == ADDR_EXPR
    7821          185 :               && TYPE_PTR_P (TREE_TYPE (probe)))
    7822              :             {
    7823          182 :               expr = probe;
    7824          182 :               expr_type = TREE_TYPE (expr);
    7825              :             }
    7826              :         }
    7827              : 
    7828              :       /* [temp.arg.nontype]/1  (TC1 version, DR 49):
    7829              : 
    7830              :          A template-argument for a non-type, non-template template-parameter
    7831              :          shall be one of: [...]
    7832              : 
    7833              :          -- the name of a non-type template-parameter;
    7834              :          -- the address of an object or function with external linkage, [...]
    7835              :             expressed as "& id-expression" where the & is optional if the name
    7836              :             refers to a function or array, or if the corresponding
    7837              :             template-parameter is a reference.
    7838              : 
    7839              :         Here, we do not care about functions, as they are invalid anyway
    7840              :         for a parameter of type pointer-to-object.  */
    7841              : 
    7842         1314 :       if (val_dep_p)
    7843              :         /* Non-type template parameters are OK.  */
    7844              :         ;
    7845         1253 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7846              :         /* Null pointer values are OK in C++11.  */;
    7847         1167 :       else if (TREE_CODE (expr) != ADDR_EXPR
    7848           42 :                && !INDIRECT_TYPE_P (expr_type))
    7849              :         /* Other values, like integer constants, might be valid
    7850              :            non-type arguments of some other type.  */
    7851           25 :         return error_mark_node;
    7852         1142 :       else if (invalid_tparm_referent_p (type, expr, complain))
    7853              :         return NULL_TREE;
    7854              : 
    7855         1240 :       expr = decayed;
    7856              : 
    7857         1240 :       expr = perform_qualification_conversions (type, expr);
    7858         1240 :       if (expr == error_mark_node)
    7859              :         return error_mark_node;
    7860              :     }
    7861              :   /* [temp.arg.nontype]/5, bullet 3
    7862              : 
    7863              :      For a non-type template-parameter of type reference to object, no
    7864              :      conversions apply. The type referred to by the reference may be more
    7865              :      cv-qualified than the (otherwise identical) type of the
    7866              :      template-argument. The template-parameter is bound directly to the
    7867              :      template-argument, which must be an lvalue.  */
    7868        30219 :   else if (TYPE_REF_OBJ_P (type))
    7869              :     {
    7870          507 :       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
    7871              :                                                       expr_type))
    7872            0 :         return error_mark_node;
    7873              : 
    7874          507 :       if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
    7875              :         {
    7876            2 :           if (complain & tf_error)
    7877            1 :             error ("%qE is not a valid template argument for type %qT "
    7878              :                    "because of conflicts in cv-qualification", expr, type);
    7879            2 :           return NULL_TREE;
    7880              :         }
    7881              : 
    7882          505 :       if (!lvalue_p (expr))
    7883              :         {
    7884            6 :           if (complain & tf_error)
    7885            6 :             error ("%qE is not a valid template argument for type %qT "
    7886              :                    "because it is not an lvalue", expr, type);
    7887            6 :           return NULL_TREE;
    7888              :         }
    7889              : 
    7890              :       /* [temp.arg.nontype]/1
    7891              : 
    7892              :          A template-argument for a non-type, non-template template-parameter
    7893              :          shall be one of: [...]
    7894              : 
    7895              :          -- the address of an object or function with external linkage.  */
    7896          499 :       if (INDIRECT_REF_P (expr)
    7897          499 :           && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
    7898              :         {
    7899           31 :           expr = TREE_OPERAND (expr, 0);
    7900           31 :           if (DECL_P (expr))
    7901              :             {
    7902            5 :               if (complain & tf_error)
    7903            3 :                 error ("%q#D is not a valid template argument for type %qT "
    7904              :                        "because a reference variable does not have a constant "
    7905              :                        "address", expr, type);
    7906            5 :               return NULL_TREE;
    7907              :             }
    7908              :         }
    7909              : 
    7910          494 :       if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
    7911              :         /* OK, dependent reference.  We don't want to ask whether a DECL is
    7912              :            itself value-dependent, since what we want here is its address.  */;
    7913              :       else
    7914              :         {
    7915          468 :           expr = build_address (expr);
    7916              : 
    7917          468 :           if (invalid_tparm_referent_p (type, expr, complain))
    7918              :             return NULL_TREE;
    7919              :         }
    7920              : 
    7921          490 :       if (!same_type_p (type, TREE_TYPE (expr)))
    7922          464 :         expr = build_nop (type, expr);
    7923              :     }
    7924              :   /* [temp.arg.nontype]/5, bullet 4
    7925              : 
    7926              :      For a non-type template-parameter of type pointer to function, only
    7927              :      the function-to-pointer conversion (_conv.func_) is applied. If the
    7928              :      template-argument represents a set of overloaded functions (or a
    7929              :      pointer to such), the matching function is selected from the set
    7930              :      (_over.over_).  */
    7931        29712 :   else if (TYPE_PTRFN_P (type))
    7932              :     {
    7933              :       /* If the argument is a template-id, we might not have enough
    7934              :          context information to decay the pointer.  */
    7935          940 :       if (!type_unknown_p (expr_type))
    7936              :         {
    7937          681 :           expr = decay_conversion (expr, complain);
    7938          681 :           if (expr == error_mark_node)
    7939              :             return error_mark_node;
    7940              :         }
    7941              : 
    7942          936 :       if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7943              :         /* Null pointer values are OK in C++11.  */
    7944           39 :         return perform_qualification_conversions (type, expr);
    7945              : 
    7946          897 :       expr = convert_nontype_argument_function (type, expr, complain);
    7947          897 :       if (!expr || expr == error_mark_node)
    7948              :         return expr;
    7949              :     }
    7950              :   /* [temp.arg.nontype]/5, bullet 5
    7951              : 
    7952              :      For a non-type template-parameter of type reference to function, no
    7953              :      conversions apply. If the template-argument represents a set of
    7954              :      overloaded functions, the matching function is selected from the set
    7955              :      (_over.over_).  */
    7956        28772 :   else if (TYPE_REFFN_P (type))
    7957              :     {
    7958          114 :       if (TREE_CODE (expr) == ADDR_EXPR)
    7959              :         {
    7960            0 :           if (complain & tf_error)
    7961              :             {
    7962            0 :               auto_diagnostic_group d;
    7963            0 :               error ("%qE is not a valid template argument for type %qT "
    7964              :                      "because it is a pointer", expr, type);
    7965            0 :               inform (input_location, "try using %qE instead",
    7966            0 :                       TREE_OPERAND (expr, 0));
    7967            0 :             }
    7968            0 :           return NULL_TREE;
    7969              :         }
    7970              : 
    7971          114 :       expr = convert_nontype_argument_function (type, expr, complain);
    7972          114 :       if (!expr || expr == error_mark_node)
    7973              :         return expr;
    7974              :     }
    7975              :   /* [temp.arg.nontype]/5, bullet 6
    7976              : 
    7977              :      For a non-type template-parameter of type pointer to member function,
    7978              :      no conversions apply. If the template-argument represents a set of
    7979              :      overloaded member functions, the matching member function is selected
    7980              :      from the set (_over.over_).  */
    7981        28658 :   else if (TYPE_PTRMEMFUNC_P (type))
    7982              :     {
    7983          765 :       expr = instantiate_type (type, expr, tf_none);
    7984          765 :       if (expr == error_mark_node)
    7985              :         return error_mark_node;
    7986              : 
    7987              :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    7988              :          expression must be a pointer-to-member constant.  */
    7989          736 :       if (!val_dep_p
    7990          736 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    7991              :         return NULL_TREE;
    7992              : 
    7993              :       /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
    7994              :          into a CONSTRUCTOR, so build up a new PTRMEM_CST instead.  */
    7995          719 :       if (fnptr_conv_p (type, TREE_TYPE (expr)))
    7996            3 :         expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
    7997              :     }
    7998              :   /* [temp.arg.nontype]/5, bullet 7
    7999              : 
    8000              :      For a non-type template-parameter of type pointer to data member,
    8001              :      qualification conversions (_conv.qual_) are applied.  */
    8002        27893 :   else if (TYPE_PTRDATAMEM_P (type))
    8003              :     {
    8004              :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    8005              :          expression must be a pointer-to-member constant.  */
    8006          524 :       if (!val_dep_p
    8007          524 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    8008              :         return NULL_TREE;
    8009              : 
    8010          504 :       expr = perform_qualification_conversions (type, expr);
    8011          504 :       if (expr == error_mark_node)
    8012              :         return expr;
    8013              :     }
    8014        27369 :   else if (NULLPTR_TYPE_P (type))
    8015              :     {
    8016           27 :       if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
    8017              :         {
    8018            0 :           if (complain & tf_error)
    8019            0 :             error ("%qE is not a valid template argument for type %qT "
    8020            0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    8021            0 :           return NULL_TREE;
    8022              :         }
    8023           27 :       if (!integer_zerop (expr) && !val_dep_p)
    8024              :         {
    8025            2 :           if (complain & tf_error)
    8026              :             {
    8027            1 :               expr = cxx_constant_value (expr);
    8028            1 :               if (expr == error_mark_node)
    8029              :                 return NULL_TREE;
    8030            0 :               gcc_assert (integer_zerop (expr));
    8031              :             }
    8032              :           else
    8033              :             return NULL_TREE;
    8034              :         }
    8035           25 :       return expr;
    8036              :     }
    8037        27342 :   else if (CLASS_TYPE_P (type))
    8038              :     {
    8039              :       /* Replace the argument with a reference to the corresponding template
    8040              :          parameter object.  */
    8041        26784 :       if (!val_dep_p)
    8042        26632 :         expr = create_template_parm_object (expr, complain);
    8043        26784 :       if (expr == error_mark_node)
    8044              :         return NULL_TREE;
    8045              :     }
    8046          558 :   else if (REFLECTION_TYPE_P (type))
    8047              :     {
    8048          558 :       if (!REFLECTION_TYPE_P (TREE_TYPE (expr)))
    8049              :         {
    8050            0 :           if (complain & tf_error)
    8051            0 :             error ("%qE is not a valid template argument for type %qT "
    8052            0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    8053            0 :           return NULL_TREE;
    8054              :         }
    8055          558 :       if (!REFLECT_EXPR_P (expr) && !val_dep_p)
    8056              :         {
    8057            4 :           if (complain & tf_error)
    8058              :             {
    8059            2 :               expr = cxx_constant_value (expr);
    8060            2 :               if (expr == error_mark_node)
    8061              :                 return NULL_TREE;
    8062            0 :               gcc_assert (REFLECT_EXPR_P (expr));
    8063              :             }
    8064              :           else
    8065              :             return NULL_TREE;
    8066              :         }
    8067          554 :       return expr;
    8068              :     }
    8069              :   /* A template non-type parameter must be one of the above.  */
    8070              :   else
    8071            0 :     gcc_unreachable ();
    8072              : 
    8073              :   /* Sanity check: did we actually convert the argument to the
    8074              :      right type?  */
    8075    203631895 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p
    8076              :               (type, TREE_TYPE (expr)));
    8077    203631895 :   return convert_from_reference (expr);
    8078    207120245 : }
    8079              : 
    8080              : /* Subroutine of coerce_template_template_parms, which returns true if
    8081              :    PARM and ARG match using the rule for the template parameters of
    8082              :    template template parameters.  Both PARM and ARG are template parameters;
    8083              :    the rest of the arguments are the same as for
    8084              :    coerce_template_template_parms.  */
    8085              : 
    8086              : static bool
    8087      1593157 : coerce_template_template_parm (tree parm, tree arg, tsubst_flags_t complain,
    8088              :                                tree in_decl, tree outer_args)
    8089              : {
    8090      1593157 :   if (arg == NULL_TREE || error_operand_p (arg)
    8091      3186308 :       || parm == NULL_TREE || error_operand_p (parm))
    8092              :     return false;
    8093              : 
    8094      1593151 :   if (TREE_CODE (arg) != TREE_CODE (parm))
    8095              :     return false;
    8096              : 
    8097      1592804 :   switch (TREE_CODE (parm))
    8098              :     {
    8099           13 :     case TEMPLATE_DECL:
    8100              :       /* We encounter instantiations of templates like
    8101              :          template <template <template <class> class> class TT>
    8102              :          class C;  */
    8103           13 :       {
    8104           13 :         if (!coerce_template_template_parms
    8105           13 :             (parm, arg, complain, in_decl, outer_args))
    8106              :           return false;
    8107              :       }
    8108              :       /* Fall through.  */
    8109              : 
    8110      1589148 :     case TYPE_DECL:
    8111      1589148 :       if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
    8112      1589148 :           && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8113              :         /* Argument is a parameter pack but parameter is not.  */
    8114              :         return false;
    8115              :       break;
    8116              : 
    8117         3656 :     case PARM_DECL:
    8118              :       /* The tsubst call is used to handle cases such as
    8119              : 
    8120              :            template <int> class C {};
    8121              :            template <class T, template <T> class TT> class D {};
    8122              :            D<int, C> d;
    8123              : 
    8124              :          i.e. the parameter list of TT depends on earlier parameters.  */
    8125         3656 :       if (!uses_template_parms (TREE_TYPE (arg)))
    8126              :         {
    8127         3627 :           ++processing_template_decl;
    8128         3627 :           tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
    8129         3627 :           --processing_template_decl;
    8130         3627 :           if (!uses_template_parms (t)
    8131         3627 :               && !same_type_p (t, TREE_TYPE (arg)))
    8132              :             return false;
    8133              :         }
    8134              : 
    8135         3632 :       if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
    8136         3632 :           && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8137              :         /* Argument is a parameter pack but parameter is not.  */
    8138              :         return false;
    8139              : 
    8140              :       break;
    8141              : 
    8142            0 :     default:
    8143            0 :       gcc_unreachable ();
    8144              :     }
    8145              : 
    8146              :   return true;
    8147              : }
    8148              : 
    8149              : /* Coerce template argument list ARGLIST for use with template
    8150              :    template-parameter TEMPL.  */
    8151              : 
    8152              : static tree
    8153       334491 : coerce_template_args_for_ttp (tree templ, tree arglist,
    8154              :                               tsubst_flags_t complain)
    8155              : {
    8156              :   /* Consider an example where a template template parameter declared as
    8157              : 
    8158              :      template <class T, class U = std::allocator<T> > class TT
    8159              : 
    8160              :      The template parameter level of T and U are one level larger than
    8161              :      of TT.  To proper process the default argument of U, say when an
    8162              :      instantiation `TT<int>' is seen, we need to build the full
    8163              :      arguments containing {int} as the innermost level.  Outer levels,
    8164              :      available when not appearing as default template argument, can be
    8165              :      obtained from the arguments of the enclosing template.
    8166              : 
    8167              :      Suppose that TT is later substituted with std::vector.  The above
    8168              :      instantiation is `TT<int, std::allocator<T> >' with TT at
    8169              :      level 1, and T at level 2, while the template arguments at level 1
    8170              :      becomes {std::vector} and the inner level 2 is {int}.  */
    8171              : 
    8172       334491 :   tree outer = DECL_CONTEXT (templ);
    8173       334491 :   if (outer)
    8174       164257 :     outer = generic_targs_for (outer);
    8175       170234 :   else if (current_template_parms)
    8176              :     {
    8177              :       /* This is an argument of the current template, so we haven't set
    8178              :          DECL_CONTEXT yet.  We can also get here when level-lowering a
    8179              :          bound ttp. */
    8180              :       tree relevant_template_parms;
    8181              : 
    8182              :       /* Parameter levels that are greater than the level of the given
    8183              :          template template parm are irrelevant.  */
    8184              :       relevant_template_parms = current_template_parms;
    8185       164879 :       while (TMPL_PARMS_DEPTH (relevant_template_parms)
    8186       329758 :              != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
    8187            3 :         relevant_template_parms = TREE_CHAIN (relevant_template_parms);
    8188              : 
    8189       164876 :       outer = template_parms_to_args (relevant_template_parms);
    8190              :     }
    8191              : 
    8192       334491 :   if (outer)
    8193       329133 :     arglist = add_to_template_args (outer, arglist);
    8194              : 
    8195       334491 :   tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
    8196       334491 :   return coerce_template_parms (parmlist, arglist, templ, complain);
    8197              : }
    8198              : 
    8199              : /* A cache of template template parameters with match-all default
    8200              :    arguments.  */
    8201              : static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
    8202              : 
    8203              : /* T is a bound template template-parameter.  Copy its arguments into default
    8204              :    arguments of the template template-parameter's template parameters.  */
    8205              : 
    8206              : static tree
    8207           48 : add_defaults_to_ttp (tree otmpl)
    8208              : {
    8209           82 :   if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
    8210           18 :     return *c;
    8211              : 
    8212           30 :   tree ntmpl = copy_node (otmpl);
    8213              : 
    8214           30 :   tree ntype = copy_node (TREE_TYPE (otmpl));
    8215           30 :   TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
    8216           30 :   TYPE_MAIN_VARIANT (ntype) = ntype;
    8217           30 :   TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
    8218           30 :   TYPE_NAME (ntype) = ntmpl;
    8219           30 :   SET_TYPE_STRUCTURAL_EQUALITY (ntype);
    8220              : 
    8221           30 :   tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
    8222           30 :     = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
    8223           30 :   TEMPLATE_PARM_DECL (idx) = ntmpl;
    8224           30 :   TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
    8225              : 
    8226           30 :   tree oparms = DECL_TEMPLATE_PARMS (otmpl);
    8227           30 :   tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
    8228           30 :   TREE_CHAIN (parms) = TREE_CHAIN (oparms);
    8229           30 :   tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
    8230           68 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    8231              :     {
    8232           38 :       tree o = TREE_VEC_ELT (vec, i);
    8233           38 :       if (!template_parameter_pack_p (TREE_VALUE (o)))
    8234              :         {
    8235           34 :           tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
    8236           34 :           TREE_PURPOSE (n) = any_targ_node;
    8237              :         }
    8238              :     }
    8239              : 
    8240           30 :   tree oresult = DECL_TEMPLATE_RESULT (otmpl);
    8241           30 :   tree gen_otmpl = DECL_TI_TEMPLATE (oresult);
    8242           30 :   tree gen_ntmpl;
    8243           30 :   if (gen_otmpl == otmpl)
    8244              :     gen_ntmpl = ntmpl;
    8245              :   else
    8246            0 :     gen_ntmpl = add_defaults_to_ttp (gen_otmpl);
    8247              : 
    8248           30 :   tree nresult = copy_decl (oresult);
    8249           30 :   DECL_TEMPLATE_INFO (nresult)
    8250           30 :     = build_template_info (gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult)));
    8251           30 :   DECL_TEMPLATE_RESULT (ntmpl) = nresult;
    8252              : 
    8253           30 :   hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
    8254           30 :   return ntmpl;
    8255              : }
    8256              : 
    8257              : /* ARG is a bound potential template template-argument, and PARGS is a list
    8258              :    of arguments for the corresponding template template-parameter.  Adjust
    8259              :    PARGS as appropriate for application to ARG's template, and if ARG is a
    8260              :    BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
    8261              :    arguments to the template template parameter.  */
    8262              : 
    8263              : static tree
    8264       183515 : coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
    8265              : {
    8266       183515 :   ++processing_template_decl;
    8267       183515 :   tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
    8268       183515 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8269              :     {
    8270              :       /* When comparing two template template-parameters in partial ordering,
    8271              :          rewrite the one currently being used as an argument to have default
    8272              :          arguments for all parameters.  */
    8273           48 :       arg_tmpl = add_defaults_to_ttp (arg_tmpl);
    8274           48 :       pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
    8275           48 :       if (pargs != error_mark_node)
    8276           44 :         arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
    8277           88 :                                            TYPE_TI_ARGS (arg));
    8278              :     }
    8279              :   else
    8280              :     {
    8281       183467 :       tree aparms
    8282       183467 :         = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
    8283       183467 :       pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
    8284              :     }
    8285       183515 :   --processing_template_decl;
    8286       183515 :   return pargs;
    8287              : }
    8288              : 
    8289              : /* Subroutine of unify for the case when PARM is a
    8290              :    BOUND_TEMPLATE_TEMPLATE_PARM.  */
    8291              : 
    8292              : static int
    8293       183713 : unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
    8294              :                       bool explain_p)
    8295              : {
    8296       183713 :   tree parmvec = TYPE_TI_ARGS (parm);
    8297       367426 :   tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
    8298              : 
    8299              :   /* The template template parm might be variadic and the argument
    8300              :      not, so flatten both argument lists.  */
    8301       183713 :   parmvec = expand_template_argument_pack (parmvec);
    8302       183713 :   argvec = expand_template_argument_pack (argvec);
    8303              : 
    8304       183713 :   if (flag_new_ttp)
    8305              :     {
    8306              :       /* In keeping with P0522R0, adjust P's template arguments
    8307              :          to apply to A's template; then flatten it again.  */
    8308       183515 :       tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
    8309       183515 :       nparmvec = expand_template_argument_pack (nparmvec);
    8310              : 
    8311       183515 :       if (unify (tparms, targs, nparmvec, argvec,
    8312              :                  UNIFY_ALLOW_NONE, explain_p))
    8313              :         return 1;
    8314              : 
    8315              :       /* If the P0522 adjustment eliminated a pack expansion, deduce
    8316              :          empty packs.  */
    8317       183241 :       if (flag_new_ttp
    8318       183241 :           && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
    8319       183241 :           && unify_pack_expansion (tparms, targs, parmvec, argvec,
    8320              :                                    DEDUCE_EXACT, /*sub*/true, explain_p))
    8321              :         return 1;
    8322              :     }
    8323              :   else
    8324              :     {
    8325              :       /* Deduce arguments T, i from TT<T> or TT<i>.
    8326              :          We check each element of PARMVEC and ARGVEC individually
    8327              :          rather than the whole TREE_VEC since they can have
    8328              :          different number of elements, which is allowed under N2555.  */
    8329              : 
    8330          198 :       int len = TREE_VEC_LENGTH (parmvec);
    8331              : 
    8332              :       /* Check if the parameters end in a pack, making them
    8333              :          variadic.  */
    8334          198 :       int parm_variadic_p = 0;
    8335          198 :       if (len > 0
    8336          198 :           && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
    8337              :         parm_variadic_p = 1;
    8338              : 
    8339          400 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8340              :         /* If the template argument list of P contains a pack
    8341              :            expansion that is not the last template argument, the
    8342              :            entire template argument list is a non-deduced
    8343              :            context.  */
    8344          204 :         if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
    8345       183420 :           return unify_success (explain_p);
    8346              : 
    8347          196 :       if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
    8348            2 :         return unify_too_few_arguments (explain_p,
    8349            2 :                                         TREE_VEC_LENGTH (argvec), len);
    8350              : 
    8351          377 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8352          198 :         if (unify (tparms, targs,
    8353          198 :                    TREE_VEC_ELT (parmvec, i),
    8354          198 :                    TREE_VEC_ELT (argvec, i),
    8355              :                    UNIFY_ALLOW_NONE, explain_p))
    8356              :           return 1;
    8357              : 
    8358          179 :       if (parm_variadic_p
    8359          179 :           && unify_pack_expansion (tparms, targs,
    8360              :                                    parmvec, argvec,
    8361              :                                    DEDUCE_EXACT,
    8362              :                                    /*subr=*/true, explain_p))
    8363              :         return 1;
    8364              :     }
    8365              : 
    8366              :   return 0;
    8367              : }
    8368              : 
    8369              : /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
    8370              :    template template parameters.
    8371              : 
    8372              :    Consider the example:
    8373              :      template <class T> class A;
    8374              :      template<template <class U> class TT> class B;
    8375              : 
    8376              :    For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
    8377              :    and OUTER_ARGS contains A.  */
    8378              : 
    8379              : static int
    8380      9988891 : coerce_template_template_parms (tree parm_tmpl,
    8381              :                                 tree arg_tmpl,
    8382              :                                 tsubst_flags_t complain,
    8383              :                                 tree in_decl,
    8384              :                                 tree outer_args)
    8385              : {
    8386      9988891 :   int nparms, nargs, i;
    8387      9988891 :   tree parm, arg;
    8388      9988891 :   int variadic_p = 0;
    8389              : 
    8390      9988891 :   tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl);
    8391      9988891 :   tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl);
    8392      9988891 :   tree gen_arg_tmpl = most_general_template (arg_tmpl);
    8393      9988891 :   tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl);
    8394              : 
    8395      9988891 :   nparms = TREE_VEC_LENGTH (parm_parms);
    8396      9988891 :   nargs = TREE_VEC_LENGTH (arg_parms);
    8397              : 
    8398      9988891 :   if (flag_new_ttp)
    8399              :     {
    8400              :       /* P0522R0: A template template-parameter P is at least as specialized as
    8401              :          a template template-argument A if, given the following rewrite to two
    8402              :          function templates, the function template corresponding to P is at
    8403              :          least as specialized as the function template corresponding to A
    8404              :          according to the partial ordering rules for function templates
    8405              :          ([temp.func.order]). Given an invented class template X with the
    8406              :          template parameter list of A (including default arguments):
    8407              : 
    8408              :          * Each of the two function templates has the same template parameters,
    8409              :          respectively, as P or A.
    8410              : 
    8411              :          * Each function template has a single function parameter whose type is
    8412              :          a specialization of X with template arguments corresponding to the
    8413              :          template parameters from the respective function template where, for
    8414              :          each template parameter PP in the template parameter list of the
    8415              :          function template, a corresponding template argument AA is formed. If
    8416              :          PP declares a parameter pack, then AA is the pack expansion
    8417              :          PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
    8418              : 
    8419              :          If the rewrite produces an invalid type, then P is not at least as
    8420              :          specialized as A.  */
    8421              : 
    8422              :       /* So coerce P's args to apply to A's parms, and then deduce between A's
    8423              :          args and the converted args.  If that succeeds, A is at least as
    8424              :          specialized as P, so they match.*/
    8425      9967270 :       processing_template_decl_sentinel ptds (/*reset*/false);
    8426      9967270 :       ++processing_template_decl;
    8427              : 
    8428      9967270 :       tree pargs = template_parms_level_to_args (parm_parms);
    8429              : 
    8430              :       /* PARM and ARG might be at different template depths, and we want to
    8431              :          pass the right additional levels of args when coercing PARGS to
    8432              :          ARG_PARMS in case we need to do any substitution into non-type
    8433              :          template parameter types.
    8434              : 
    8435              :          OUTER_ARGS are not the right outer levels in this case, as they are
    8436              :          the args we're building up for PARM, and for the coercion we want the
    8437              :          args for ARG.  If DECL_CONTEXT isn't set for a template template
    8438              :          parameter, we can assume that it's in the current scope.  */
    8439      9967270 :       tree ctx = DECL_CONTEXT (arg_tmpl);
    8440      9967270 :       if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8441       426328 :         ctx = current_scope ();
    8442      9967270 :       tree scope_args = NULL_TREE;
    8443      9967270 :       if (tree tinfo = get_template_info (ctx))
    8444       781582 :         scope_args = TI_ARGS (tinfo);
    8445      9967270 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8446              :         {
    8447       495662 :           int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl));
    8448       573760 :           int scope_depth = TMPL_ARGS_DEPTH (scope_args);
    8449       495662 :           tree full_pargs = make_tree_vec (level + 1);
    8450              : 
    8451              :         /* Only use as many levels from the scope as needed
    8452              :            (excluding the level of ARG).  */
    8453       515045 :           for (int i = 0; i < level - 1; ++i)
    8454        19383 :             if (i < scope_depth)
    8455        37804 :               TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1);
    8456              :             else
    8457          481 :               TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0);
    8458              : 
    8459              :           /* Add the arguments that appear at the levels of ARG.  */
    8460       495662 :           tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl));
    8461       991324 :           adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1);
    8462       495662 :           TREE_VEC_ELT (full_pargs, level - 1) = adjacent;
    8463              : 
    8464       495662 :           TREE_VEC_ELT (full_pargs, level) = pargs;
    8465       495662 :           pargs = full_pargs;
    8466              :         }
    8467              :       else
    8468      9471608 :         pargs = add_to_template_args (scope_args, pargs);
    8469              : 
    8470      9967270 :       pargs = coerce_template_parms (gen_arg_parms, pargs,
    8471              :                                      NULL_TREE, tf_none);
    8472      9967270 :       if (pargs != error_mark_node)
    8473              :         {
    8474      9966243 :           tree targs = make_tree_vec (nargs);
    8475      9966243 :           tree aargs = template_parms_level_to_args (arg_parms);
    8476      9966243 :           if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
    8477              :                       /*explain*/false))
    8478      8976054 :             return 1;
    8479              :         }
    8480      9967270 :     }
    8481              : 
    8482              :   /* Determine whether we have a parameter pack at the end of the
    8483              :      template template parameter's template parameter list.  */
    8484      1012837 :   if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
    8485              :     {
    8486      1012837 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
    8487              : 
    8488      1012837 :       if (error_operand_p (parm))
    8489              :         return 0;
    8490              : 
    8491      1012831 :       switch (TREE_CODE (parm))
    8492              :         {
    8493      1008201 :         case TEMPLATE_DECL:
    8494      1008201 :         case TYPE_DECL:
    8495      1008201 :           if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8496       997569 :             variadic_p = 1;
    8497              :           break;
    8498              : 
    8499         4630 :         case PARM_DECL:
    8500         4630 :           if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8501       997569 :             variadic_p = 1;
    8502              :           break;
    8503              : 
    8504            0 :         default:
    8505            0 :           gcc_unreachable ();
    8506              :         }
    8507              :     }
    8508              : 
    8509      1012831 :   if (nargs != nparms
    8510       483977 :       && !(variadic_p && nargs >= nparms - 1))
    8511              :     return 0;
    8512              : 
    8513              :   /* Check all of the template parameters except the parameter pack at
    8514              :      the end (if any).  */
    8515      1180173 :   for (i = 0; i < nparms - variadic_p; ++i)
    8516              :     {
    8517       168430 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
    8518       168430 :           || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8519            0 :         continue;
    8520              : 
    8521       168430 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8522       168430 :       arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8523              : 
    8524       168430 :       if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8525              :                                           outer_args))
    8526              :         return 0;
    8527              : 
    8528              :     }
    8529              : 
    8530      1011743 :   if (variadic_p)
    8531              :     {
    8532              :       /* Check each of the template parameters in the template
    8533              :          argument against the template parameter pack at the end of
    8534              :          the template template parameter.  */
    8535       997565 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
    8536              :         return 0;
    8537              : 
    8538       997565 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8539              : 
    8540      2422268 :       for (; i < nargs; ++i)
    8541              :         {
    8542      1424727 :           if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8543            0 :             continue;
    8544              : 
    8545      1424727 :           arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8546              : 
    8547      1424727 :           if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8548              :                                               outer_args))
    8549              :             return 0;
    8550              :         }
    8551              :     }
    8552              : 
    8553              :   return 1;
    8554              : }
    8555              : 
    8556              : /* Verifies that the deduced template arguments (in TARGS) for the
    8557              :    template template parameters (in TPARMS) represent valid bindings,
    8558              :    by comparing the template parameter list of each template argument
    8559              :    to the template parameter list of its corresponding template
    8560              :    template parameter, in accordance with DR150. This
    8561              :    routine can only be called after all template arguments have been
    8562              :    deduced. It will return TRUE if all of the template template
    8563              :    parameter bindings are okay, FALSE otherwise.  */
    8564              : bool
    8565    107011998 : template_template_parm_bindings_ok_p (tree tparms, tree targs)
    8566              : {
    8567    107011998 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
    8568    107011998 :   bool ret = true;
    8569              : 
    8570              :   /* We're dealing with template parms in this process.  */
    8571    107011998 :   ++processing_template_decl;
    8572              : 
    8573    107011998 :   targs = INNERMOST_TEMPLATE_ARGS (targs);
    8574              : 
    8575    268619387 :   for (i = 0; i < ntparms; ++i)
    8576              :     {
    8577    161607425 :       tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
    8578    161607425 :       tree targ = TREE_VEC_ELT (targs, i);
    8579              : 
    8580    161607425 :       if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
    8581              :         {
    8582       769826 :           tree packed_args = NULL_TREE;
    8583       769826 :           int idx, len = 1;
    8584              : 
    8585       769826 :           if (ARGUMENT_PACK_P (targ))
    8586              :             {
    8587              :               /* Look inside the argument pack.  */
    8588          145 :               packed_args = ARGUMENT_PACK_ARGS (targ);
    8589          145 :               len = TREE_VEC_LENGTH (packed_args);
    8590              :             }
    8591              : 
    8592      1539638 :           for (idx = 0; idx < len; ++idx)
    8593              :             {
    8594       769848 :               if (packed_args)
    8595              :                 /* Extract the next argument from the argument
    8596              :                    pack.  */
    8597          167 :                 targ = TREE_VEC_ELT (packed_args, idx);
    8598              : 
    8599       769848 :               if (PACK_EXPANSION_P (targ))
    8600              :                 /* Look at the pattern of the pack expansion.  */
    8601           40 :                 targ = PACK_EXPANSION_PATTERN (targ);
    8602              : 
    8603              :               /* Extract the template parameters from the template
    8604              :                  argument.  */
    8605       769848 :               if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
    8606       119758 :                 targ = TYPE_NAME (targ);
    8607              : 
    8608              :               /* Verify that we can coerce the template template
    8609              :                  parameters from the template argument to the template
    8610              :                  parameter.  This requires an exact match.  */
    8611       769848 :               if (TREE_CODE (targ) == TEMPLATE_DECL
    8612      1539687 :                   && !coerce_template_template_parms
    8613       769839 :                        (tparm,
    8614              :                         targ,
    8615              :                         tf_none,
    8616              :                         tparm,
    8617              :                         targs))
    8618              :                 {
    8619           36 :                   ret = false;
    8620           36 :                   goto out;
    8621              :                 }
    8622              :             }
    8623              :         }
    8624              :     }
    8625              : 
    8626    107011962 :  out:
    8627              : 
    8628    107011998 :   --processing_template_decl;
    8629    107011998 :   return ret;
    8630              : }
    8631              : 
    8632              : /* Since type attributes aren't mangled, we need to strip them from
    8633              :    template type arguments.  */
    8634              : 
    8635              : tree
    8636   1802694363 : canonicalize_type_argument (tree arg, tsubst_flags_t complain)
    8637              : {
    8638   1802694363 :   if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
    8639              :     return arg;
    8640    729603129 :   bool removed_attributes = false;
    8641    729603129 :   tree canon = strip_typedefs (arg, &removed_attributes);
    8642    729603129 :   if (removed_attributes
    8643          650 :       && (complain & tf_warning))
    8644           15 :     warning (OPT_Wignored_attributes,
    8645              :              "ignoring attributes on template argument %qT", arg);
    8646              :   return canon;
    8647              : }
    8648              : 
    8649              : /* And from inside dependent non-type arguments like sizeof(Type).  */
    8650              : 
    8651              : static tree
    8652     39663066 : canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
    8653              : {
    8654     39663066 :   if (!arg || arg == error_mark_node)
    8655              :     return arg;
    8656     39663066 :   bool removed_attributes = false;
    8657     39663066 :   tree canon = strip_typedefs_expr (arg, &removed_attributes);
    8658     39663066 :   if (removed_attributes
    8659            0 :       && (complain & tf_warning))
    8660            0 :     warning (OPT_Wignored_attributes,
    8661              :              "ignoring attributes in template argument %qE", arg);
    8662              :   return canon;
    8663              : }
    8664              : 
    8665              : /* A template declaration can be substituted for a constrained
    8666              :    template template parameter only when the argument is no more
    8667              :    constrained than the parameter.  */
    8668              : 
    8669              : static bool
    8670      9217957 : is_compatible_template_arg (tree parm, tree arg, tree args)
    8671              : {
    8672      9217957 :   tree parm_cons = get_constraints (parm);
    8673              : 
    8674              :   /* For now, allow constrained template template arguments
    8675              :      and unconstrained template template parameters.  */
    8676      9217957 :   if (parm_cons == NULL_TREE)
    8677              :     return true;
    8678              : 
    8679              :   /* If the template parameter is constrained, we need to rewrite its
    8680              :      constraints in terms of the ARG's template parameters. This ensures
    8681              :      that all of the template parameter types will have the same depth.
    8682              : 
    8683              :      Note that this is only valid when coerce_template_template_parm is
    8684              :      true for the innermost template parameters of PARM and ARG. In other
    8685              :      words, because coercion is successful, this conversion will be valid.  */
    8686           87 :   tree new_args = NULL_TREE;
    8687           87 :   if (parm_cons)
    8688              :     {
    8689           87 :       tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
    8690           87 :       new_args = template_parms_level_to_args (aparms);
    8691           87 :       new_args = add_to_template_args (args, new_args);
    8692           87 :       ++processing_template_decl;
    8693           87 :       parm_cons = tsubst_constraint_info (parm_cons, new_args,
    8694              :                                           tf_none, NULL_TREE);
    8695           87 :       --processing_template_decl;
    8696           87 :       if (parm_cons == error_mark_node)
    8697              :         return false;
    8698              :     }
    8699              : 
    8700           87 :   return ttp_subsumes (parm_cons, arg);
    8701              : }
    8702              : 
    8703              : /* We can't fully resolve ARG given as a non-type template argument to TYPE,
    8704              :    because one of them is dependent.  But we need to represent the
    8705              :    conversion for the benefit of cp_tree_equal.  */
    8706              : 
    8707              : static tree
    8708     16071334 : maybe_convert_nontype_argument (tree type, tree arg, bool force)
    8709              : {
    8710              :   /* Auto parms get no conversion.  */
    8711     16071334 :   if (type_uses_auto (type))
    8712              :     return arg;
    8713              :   /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
    8714              :      That would complicate other things, and it doesn't seem necessary.  */
    8715     15970882 :   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    8716              :     return arg;
    8717              :   /* We don't need or want to add this conversion now if we're going to use the
    8718              :      argument for deduction.  */
    8719     14803403 :   if (!value_dependent_expression_p (arg))
    8720              :     force = false;
    8721     14803226 :   else if (!force)
    8722              :     return arg;
    8723              : 
    8724      1631599 :   type = cv_unqualified (type);
    8725      1631599 :   tree argtype = TREE_TYPE (arg);
    8726      1631599 :   if (argtype && same_type_p (type, argtype))
    8727              :     return arg;
    8728              : 
    8729      1631563 :   arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
    8730      1631563 :   IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
    8731      1631563 :   IMPLICIT_CONV_EXPR_FORCED (arg) = force;
    8732      1631563 :   return arg;
    8733              : }
    8734              : 
    8735              : /* True if we need an IMPLICIT_CONV_EXPR for converting EXPR to TYPE, possibly
    8736              :    in a FORCED context (i.e. alias or concept).  */
    8737              : 
    8738              : static bool
    8739    229077889 : dependent_implicit_conv_p (tree type, tree expr, bool forced)
    8740              : {
    8741    456878268 :   return (dependent_type_p (type) || type_dependent_expression_p (expr)
    8742    437224902 :           || (forced
    8743     83876528 :               && !(same_type_ignoring_top_level_qualifiers_p
    8744     83876528 :                    (TREE_TYPE (expr), type))
    8745       297525 :               && value_dependent_expression_p (expr)));
    8746              : }
    8747              : 
    8748              : /* Convert the indicated template ARG as necessary to match the
    8749              :    indicated template PARM.  Returns the converted ARG, or
    8750              :    error_mark_node if the conversion was unsuccessful.  Error and
    8751              :    warning messages are issued under control of COMPLAIN.  This
    8752              :    conversion is for the Ith parameter in the parameter list.  ARGS is
    8753              :    the full set of template arguments deduced so far.  */
    8754              : 
    8755              : static tree
    8756   1597514956 : convert_template_argument (tree parm,
    8757              :                            tree arg,
    8758              :                            tree args,
    8759              :                            tsubst_flags_t complain,
    8760              :                            int i,
    8761              :                            tree in_decl)
    8762              : {
    8763   1597514956 :   tree orig_arg;
    8764   1597514956 :   tree val;
    8765   1597514956 :   int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
    8766              : 
    8767   1597514956 :   if (parm == error_mark_node || error_operand_p (arg))
    8768              :     return error_mark_node;
    8769              : 
    8770   1570029079 :   if (arg == any_targ_node)
    8771              :     return arg;
    8772              : 
    8773   1570029067 :   if (TREE_CODE (arg) == TREE_LIST
    8774   1570029067 :       && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
    8775              :     {
    8776              :       /* The template argument was the name of some
    8777              :          member function.  That's usually
    8778              :          invalid, but static members are OK.  In any
    8779              :          case, grab the underlying fields/functions
    8780              :          and issue an error later if required.  */
    8781            0 :       TREE_TYPE (arg) = unknown_type_node;
    8782              :     }
    8783              : 
    8784   1570029067 :   orig_arg = arg;
    8785              : 
    8786   1570029067 :   requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
    8787   3140058134 :   requires_type = (TREE_CODE (parm) == TYPE_DECL
    8788   1570029067 :                    || requires_tmpl_type);
    8789              : 
    8790              :   /* When determining whether an argument pack expansion is a template,
    8791              :      look at the pattern.  */
    8792   1570029067 :   if (PACK_EXPANSION_P (arg))
    8793     19884880 :     arg = PACK_EXPANSION_PATTERN (arg);
    8794              : 
    8795              :   /* Deal with an injected-class-name used as a template template arg.  */
    8796   1570029067 :   if (requires_tmpl_type && CLASS_TYPE_P (arg))
    8797              :     {
    8798          815 :       tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
    8799          815 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    8800              :         {
    8801           39 :           if (cxx_dialect >= cxx11)
    8802              :             /* OK under DR 1004.  */;
    8803            2 :           else if (complain & tf_warning_or_error)
    8804            2 :             pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
    8805            2 :                      " used as template template argument", TYPE_NAME (arg));
    8806            0 :           else if (flag_pedantic_errors)
    8807   1570029028 :             t = arg;
    8808              : 
    8809              :           arg = t;
    8810              :         }
    8811              :     }
    8812              : 
    8813   3149329261 :   is_tmpl_type =
    8814   1570029067 :     ((TREE_CODE (arg) == TEMPLATE_DECL
    8815      9034412 :       && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
    8816   1560994655 :      || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
    8817   1560994646 :      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8818   3130838827 :      || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
    8819              : 
    8820      9271127 :   if (is_tmpl_type
    8821      9271127 :       && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8822      9086241 :           || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
    8823       236706 :     arg = TYPE_STUB_DECL (arg);
    8824              : 
    8825   1570029067 :   is_type = TYPE_P (arg) || is_tmpl_type;
    8826              : 
    8827      4111025 :   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
    8828   1570029085 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
    8829              :     {
    8830           12 :       if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
    8831              :         {
    8832            3 :           if (complain & tf_error)
    8833            3 :             error ("invalid use of destructor %qE as a type", orig_arg);
    8834            3 :           return error_mark_node;
    8835              :         }
    8836              : 
    8837            9 :       permerror (input_location,
    8838              :                  "to refer to a type member of a template parameter, "
    8839              :                  "use %<typename %E%>", orig_arg);
    8840              : 
    8841            9 :       orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
    8842            9 :                                      TREE_OPERAND (arg, 1),
    8843              :                                      typename_type,
    8844              :                                      complain);
    8845            9 :       arg = orig_arg;
    8846            9 :       is_type = 1;
    8847              :     }
    8848   1570029064 :   if (is_type != requires_type)
    8849              :     {
    8850      4116771 :       if (in_decl)
    8851              :         {
    8852      4116355 :           if (complain & tf_error)
    8853              :             {
    8854          130 :               auto_diagnostic_group d;
    8855          130 :               error ("type/value mismatch at argument %d in template "
    8856              :                      "parameter list for %qD",
    8857              :                      i + 1, in_decl);
    8858          130 :               if (is_type)
    8859              :                 {
    8860              :                   /* The template argument is a type, but we're expecting
    8861              :                      an expression.  */
    8862           55 :                   inform (input_location,
    8863              :                           "  expected a constant of type %qT, got %qT",
    8864           40 :                           TREE_TYPE (parm),
    8865           40 :                           (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
    8866              :                   /* [temp.arg]/2: "In a template-argument, an ambiguity
    8867              :                      between a type-id and an expression is resolved to a
    8868              :                      type-id, regardless of the form of the corresponding
    8869              :                      template-parameter."  So give the user a clue.  */
    8870           40 :                   if (TREE_CODE (arg) == FUNCTION_TYPE)
    8871            6 :                     inform (input_location, "  ambiguous template argument "
    8872              :                             "for non-type template parameter is treated as "
    8873              :                             "function type");
    8874              :                 }
    8875           90 :               else if (requires_tmpl_type)
    8876            9 :                 inform (input_location,
    8877              :                         "  expected a class template, got %qE", orig_arg);
    8878              :               else
    8879           81 :                 inform (input_location,
    8880              :                         "  expected a type, got %qE", orig_arg);
    8881          130 :             }
    8882              :         }
    8883      4116771 :       return error_mark_node;
    8884              :     }
    8885   1565912293 :   if (is_tmpl_type ^ requires_tmpl_type)
    8886              :     {
    8887         1035 :       if (in_decl && (complain & tf_error))
    8888              :         {
    8889           15 :           auto_diagnostic_group d;
    8890           15 :           error ("type/value mismatch at argument %d in template "
    8891              :                  "parameter list for %qD",
    8892              :                  i + 1, in_decl);
    8893           15 :           if (is_tmpl_type)
    8894            6 :             inform (input_location,
    8895            6 :                     "  expected a type, got %qT", DECL_NAME (arg));
    8896              :           else
    8897            9 :             inform (input_location,
    8898              :                     "  expected a class template, got %qT", orig_arg);
    8899           15 :         }
    8900         1035 :       return error_mark_node;
    8901              :     }
    8902              : 
    8903   1565911258 :   if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
    8904              :     /* We already did the appropriate conversion when packing args.  */
    8905              :     val = orig_arg;
    8906   1565911219 :   else if (is_type)
    8907              :     {
    8908   1376522072 :       if (requires_tmpl_type)
    8909              :         {
    8910      9270853 :           if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
    8911              :             /* The number of argument required is not known yet.
    8912              :                Just accept it for now.  */
    8913              :             val = orig_arg;
    8914              :           else
    8915              :             {
    8916              :               /* Strip alias templates that are equivalent to another
    8917              :                  template.  */
    8918      9219039 :               arg = get_underlying_template (arg);
    8919              : 
    8920      9219039 :               if (coerce_template_template_parms (parm, arg,
    8921              :                                                   complain, in_decl,
    8922              :                                                   args))
    8923              :                 {
    8924      9217957 :                   val = arg;
    8925              : 
    8926              :                   /* TEMPLATE_TEMPLATE_PARM node is preferred over
    8927              :                      TEMPLATE_DECL.  */
    8928      9217957 :                   if (val != error_mark_node)
    8929              :                     {
    8930      9217957 :                       if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
    8931       381371 :                         val = TREE_TYPE (val);
    8932      9217957 :                       if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
    8933          107 :                         val = make_pack_expansion (val, complain);
    8934              :                     }
    8935              :                 }
    8936              :               else
    8937              :                 {
    8938         1082 :                   if (in_decl && (complain & tf_error))
    8939              :                     {
    8940           39 :                       auto_diagnostic_group d;
    8941           39 :                       error ("type/value mismatch at argument %d in "
    8942              :                              "template parameter list for %qD",
    8943              :                              i + 1, in_decl);
    8944           39 :                       inform (input_location,
    8945              :                               "  expected a template of type %qD, got %qT",
    8946              :                               parm, orig_arg);
    8947           39 :                     }
    8948              : 
    8949         1082 :                   val = error_mark_node;
    8950              :                 }
    8951              : 
    8952              :               // Check that the constraints are compatible before allowing the
    8953              :               // substitution.
    8954      9219039 :               if (val != error_mark_node)
    8955      9217957 :                 if (!is_compatible_template_arg (parm, arg, args))
    8956              :                   {
    8957           18 :                     if (in_decl && (complain & tf_error))
    8958              :                       {
    8959           15 :                         auto_diagnostic_group d;
    8960           15 :                         error ("constraint mismatch at argument %d in "
    8961              :                                "template parameter list for %qD",
    8962              :                                i + 1, in_decl);
    8963           15 :                         inform (input_location, "  expected %qD but got %qD",
    8964              :                                 parm, arg);
    8965           15 :                       }
    8966           18 :                     val = error_mark_node;
    8967              :                   }
    8968              :             }
    8969              :         }
    8970              :       else
    8971              :         val = orig_arg;
    8972              :       /* We only form one instance of each template specialization.
    8973              :          Therefore, if we use a non-canonical variant (i.e., a
    8974              :          typedef), any future messages referring to the type will use
    8975              :          the typedef, which is confusing if those future uses do not
    8976              :          themselves also use the typedef.  */
    8977   1376522072 :       if (TYPE_P (val))
    8978   1367684404 :         val = canonicalize_type_argument (val, complain);
    8979              :     }
    8980              :   else
    8981              :     {
    8982    189389147 :       tree t = TREE_TYPE (parm);
    8983              : 
    8984    378778294 :       if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
    8985    378778294 :           > TMPL_ARGS_DEPTH (args))
    8986              :         /* We don't have enough levels of args to do any substitution.  This
    8987              :            can happen in the context of -fnew-ttp-matching.  */;
    8988    189388665 :       else if (tree a = type_uses_auto (t))
    8989              :         {
    8990       118939 :           t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
    8991              :                                  LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
    8992       118939 :           if (t == error_mark_node)
    8993              :             return error_mark_node;
    8994              :         }
    8995              :       else
    8996    189269726 :         t = tsubst (t, args, complain, in_decl);
    8997              : 
    8998              :       /* Perform array-to-pointer and function-to-pointer conversion
    8999              :          as per [temp.param]/10.  */
    9000    189389129 :       t = type_decays_to (t);
    9001              : 
    9002    189389129 :       if (invalid_nontype_parm_type_p (t, complain))
    9003           96 :         return error_mark_node;
    9004              : 
    9005              :       /* Drop top-level cv-qualifiers on the substituted/deduced type of
    9006              :          this non-type template parameter, as per [temp.param]/6.  */
    9007    189389033 :       t = cv_unqualified (t);
    9008              : 
    9009    189389033 :       if (t != TREE_TYPE (parm))
    9010      5484106 :         t = canonicalize_type_argument (t, complain);
    9011              : 
    9012              :       /* We need to handle arguments for alias or concept templates
    9013              :          differently: we need to force building an IMPLICIT_CONV_EXPR, because
    9014              :          these arguments are going to be substituted directly into the
    9015              :          dependent type; they might not get another chance at
    9016              :          convert_nontype_argument.  But if the argument ends up here again for
    9017              :          a template that isn't one of those, remove the conversion for
    9018              :          consistency between naming the same dependent type directly or through
    9019              :          an alias.  */
    9020    189389033 :       bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9021    135390045 :                                     || concept_definition_p (in_decl));
    9022    137432594 :       if (!force_conv
    9023    137432594 :           && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR
    9024      3829032 :           && IMPLICIT_CONV_EXPR_FORCED (orig_arg)
    9025      3828882 :           && same_type_p (TREE_TYPE (orig_arg), t))
    9026      3828879 :         orig_arg = TREE_OPERAND (orig_arg, 0);
    9027              : 
    9028    189389033 :       if (!dependent_implicit_conv_p (t, orig_arg, force_conv))
    9029              :         /* We used to call digest_init here.  However, digest_init
    9030              :            will report errors, which we don't want when complain
    9031              :            is zero.  More importantly, digest_init will try too
    9032              :            hard to convert things: for example, `0' should not be
    9033              :            converted to pointer type at this point according to
    9034              :            the standard.  Accepting this is not merely an
    9035              :            extension, since deciding whether or not these
    9036              :            conversions can occur is part of determining which
    9037              :            function template to call, or whether a given explicit
    9038              :            argument specification is valid.  */
    9039    173317699 :         val = convert_nontype_argument (t, orig_arg, complain);
    9040              :       else
    9041              :         {
    9042     16071334 :           val = canonicalize_expr_argument (orig_arg, complain);
    9043     16071334 :           val = maybe_convert_nontype_argument (t, val, force_conv);
    9044              :         }
    9045              : 
    9046    189389033 :       if (val == NULL_TREE)
    9047          429 :         val = error_mark_node;
    9048    189388604 :       else if (val == error_mark_node && (complain & tf_error))
    9049           57 :         error_at (cp_expr_loc_or_input_loc (orig_arg),
    9050              :                   "could not convert template argument %qE from %qT to %qT",
    9051           41 :                   orig_arg, TREE_TYPE (orig_arg), t);
    9052              : 
    9053    189389033 :       if (INDIRECT_REF_P (val))
    9054              :         {
    9055              :           /* Reject template arguments that are references to built-in
    9056              :              functions with no library fallbacks.  */
    9057         3319 :           const_tree inner = TREE_OPERAND (val, 0);
    9058         3319 :           const_tree innertype = TREE_TYPE (inner);
    9059         3319 :           if (innertype
    9060         1777 :               && TYPE_REF_P (innertype)
    9061         1768 :               && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
    9062           96 :               && TREE_OPERAND_LENGTH (inner) > 0
    9063         3385 :               && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
    9064            4 :               return error_mark_node;
    9065              :         }
    9066              : 
    9067    189389029 :       if (TREE_CODE (val) == SCOPE_REF)
    9068              :         {
    9069              :           /* Strip typedefs from the SCOPE_REF.  */
    9070      7793388 :           tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
    9071      7793388 :           tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
    9072              :                                                    complain);
    9073     15586776 :           val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
    9074      7793388 :                                       QUALIFIED_NAME_IS_TEMPLATE (val));
    9075              :         }
    9076              :     }
    9077              : 
    9078              :   return val;
    9079              : }
    9080              : 
    9081              : /* Coerces the remaining template arguments in INNER_ARGS (from
    9082              :    ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
    9083              :    Returns the coerced argument pack. PARM_IDX is the position of this
    9084              :    parameter in the template parameter list. ARGS is the original
    9085              :    template argument list.  */
    9086              : static tree
    9087    110876868 : coerce_template_parameter_pack (tree parms,
    9088              :                                 int parm_idx,
    9089              :                                 tree args,
    9090              :                                 tree inner_args,
    9091              :                                 int arg_idx,
    9092              :                                 tree new_args,
    9093              :                                 int* lost,
    9094              :                                 tree in_decl,
    9095              :                                 tsubst_flags_t complain)
    9096              : {
    9097    110876868 :   tree parm = TREE_VEC_ELT (parms, parm_idx);
    9098    221753736 :   int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    9099    110876868 :   tree packed_args;
    9100    110876868 :   tree argument_pack;
    9101    110876868 :   tree packed_parms = NULL_TREE;
    9102              : 
    9103    110876868 :   if (arg_idx > nargs)
    9104              :     arg_idx = nargs;
    9105              : 
    9106    110876868 :   if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
    9107              :     {
    9108              :       /* When the template parameter is a non-type template parameter pack
    9109              :          or template template parameter pack whose type or template
    9110              :          parameters use parameter packs, we know exactly how many arguments
    9111              :          we are looking for.  Build a vector of the instantiated decls for
    9112              :          these template parameters in PACKED_PARMS.  */
    9113              :       /* We can't use make_pack_expansion here because it would interpret a
    9114              :          _DECL as a use rather than a declaration.  */
    9115           87 :       tree decl = TREE_VALUE (parm);
    9116           87 :       tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
    9117           87 :       PACK_EXPANSION_PATTERN (exp) = decl;
    9118          174 :       PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
    9119           87 :       SET_TYPE_STRUCTURAL_EQUALITY (exp);
    9120              : 
    9121           87 :       TREE_VEC_LENGTH (args)--;
    9122           87 :       packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
    9123           84 :       TREE_VEC_LENGTH (args)++;
    9124              : 
    9125           84 :       if (packed_parms == error_mark_node)
    9126              :         return error_mark_node;
    9127              : 
    9128              :       /* If we're doing a partial instantiation of a member template,
    9129              :          verify that all of the types used for the non-type
    9130              :          template parameter pack are, in fact, valid for non-type
    9131              :          template parameters.  */
    9132           84 :       if (arg_idx < nargs
    9133           84 :           && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
    9134              :         {
    9135           42 :           int j, len = TREE_VEC_LENGTH (packed_parms);
    9136           92 :           for (j = 0; j < len; ++j)
    9137              :             {
    9138           52 :               tree t = TREE_VEC_ELT (packed_parms, j);
    9139           52 :               if (TREE_CODE (t) == PARM_DECL
    9140           52 :                   && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
    9141            2 :                 return error_mark_node;
    9142              :             }
    9143              :           /* We don't know how many args we have yet, just
    9144              :              use the unconverted ones for now.  */
    9145              :           return NULL_TREE;
    9146              :         }
    9147              : 
    9148           42 :       packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
    9149              :     }
    9150              :   else
    9151    110876781 :     packed_args = make_tree_vec (nargs - arg_idx);
    9152              : 
    9153              :   /* Convert the remaining arguments, which will be a part of the
    9154              :      parameter pack "parm".  */
    9155    110876823 :   int first_pack_arg = arg_idx;
    9156    304446685 :   for (; arg_idx < nargs; ++arg_idx)
    9157              :     {
    9158    193569874 :       tree arg = TREE_VEC_ELT (inner_args, arg_idx);
    9159    193569874 :       tree actual_parm = TREE_VALUE (parm);
    9160    193569874 :       int pack_idx = arg_idx - first_pack_arg;
    9161              : 
    9162    193569874 :       if (packed_parms)
    9163              :         {
    9164              :           /* Once we've packed as many args as we have types, stop.  */
    9165           72 :           if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
    9166              :             break;
    9167           60 :           else if (PACK_EXPANSION_P (arg))
    9168              :             /* We don't know how many args we have yet, just
    9169              :                use the unconverted ones for now.  */
    9170              :             return NULL_TREE;
    9171              :           else
    9172           60 :             actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
    9173              :         }
    9174              : 
    9175    193569862 :       if (arg == error_mark_node)
    9176              :         {
    9177          749 :           if (complain & tf_error)
    9178          746 :             error ("template argument %d is invalid", arg_idx + 1);
    9179              :         }
    9180              :       else
    9181    193569113 :         arg = convert_template_argument (actual_parm,
    9182              :                                          arg, new_args, complain, parm_idx,
    9183              :                                          in_decl);
    9184    193569862 :       if (arg == error_mark_node)
    9185          917 :         (*lost)++;
    9186    193569862 :       TREE_VEC_ELT (packed_args, pack_idx) = arg;
    9187              :     }
    9188              : 
    9189    110876823 :   if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
    9190    110876823 :       && TREE_VEC_LENGTH (packed_args) > 0)
    9191              :     {
    9192            3 :       if (complain & tf_error)
    9193            3 :         error ("wrong number of template arguments (%d, should be %d)",
    9194            3 :                arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
    9195            3 :       return error_mark_node;
    9196              :     }
    9197              : 
    9198    110876820 :   if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
    9199    110876820 :       || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
    9200    108238081 :     argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
    9201              :   else
    9202              :     {
    9203      2638739 :       argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
    9204      2638739 :       TREE_CONSTANT (argument_pack) = 1;
    9205              :     }
    9206              : 
    9207    110876820 :   ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
    9208    110876820 :   if (CHECKING_P)
    9209    110876820 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
    9210              :                                          TREE_VEC_LENGTH (packed_args));
    9211    110876820 :   return argument_pack;
    9212              : }
    9213              : 
    9214              : /* Returns the number of pack expansions in the template argument vector
    9215              :    ARGS.  */
    9216              : 
    9217              : static int
    9218   2295395912 : pack_expansion_args_count (tree args)
    9219              : {
    9220   2295395912 :   int i;
    9221   2295395912 :   int count = 0;
    9222   2295395912 :   if (args)
    9223   6189710957 :     for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
    9224              :       {
    9225   3894315045 :         tree elt = TREE_VEC_ELT (args, i);
    9226   3894315045 :         if (elt && PACK_EXPANSION_P (elt))
    9227     30139854 :           ++count;
    9228              :       }
    9229   2295395912 :   return count;
    9230              : }
    9231              : 
    9232              : /* Convert all template arguments to their appropriate types, and
    9233              :    return a vector containing the innermost resulting template
    9234              :    arguments.  If any error occurs, return error_mark_node. Error and
    9235              :    warning messages are issued under control of COMPLAIN.
    9236              : 
    9237              :    If PARMS represents all template parameters levels, this function
    9238              :    returns a vector of vectors representing all the resulting argument
    9239              :    levels.  Note that in this case, only the innermost arguments are
    9240              :    coerced because the outermost ones are supposed to have been coerced
    9241              :    already.  Otherwise, if PARMS represents only (the innermost) vector
    9242              :    of parameters, this function returns a vector containing just the
    9243              :    innermost resulting arguments.
    9244              : 
    9245              :    If REQUIRE_ALL_ARGS is false, argument deduction will be performed
    9246              :    for arguments not specified in ARGS.  If REQUIRE_ALL_ARGS is true,
    9247              :    arguments not specified in ARGS must have default arguments which
    9248              :    we'll use to fill in ARGS.  */
    9249              : 
    9250              : tree
    9251   1019264075 : coerce_template_parms (tree parms,
    9252              :                        tree args,
    9253              :                        tree in_decl,
    9254              :                        tsubst_flags_t complain,
    9255              :                        bool require_all_args /* = true */)
    9256              : {
    9257   1019264075 :   int nparms, nargs, parm_idx, arg_idx, lost = 0;
    9258   1019264075 :   tree orig_inner_args;
    9259   1019264075 :   tree inner_args;
    9260              : 
    9261              :   /* When used as a boolean value, indicates whether this is a
    9262              :      variadic template parameter list. Since it's an int, we can also
    9263              :      subtract it from nparms to get the number of non-variadic
    9264              :      parameters.  */
    9265   1019264075 :   int variadic_p = 0;
    9266   1019264075 :   int variadic_args_p = 0;
    9267   1019264075 :   int post_variadic_parms = 0;
    9268              : 
    9269              :   /* Adjustment to nparms for fixed parameter packs.  */
    9270   1019264075 :   int fixed_pack_adjust = 0;
    9271   1019264075 :   int fixed_packs = 0;
    9272   1019264075 :   int missing = 0;
    9273              : 
    9274              :   /* Likewise for parameters with default arguments.  */
    9275   1019264075 :   int default_p = 0;
    9276              : 
    9277   1019264075 :   if (args == error_mark_node)
    9278              :     return error_mark_node;
    9279              : 
    9280   1019264075 :   bool return_full_args = false;
    9281   1019264075 :   if (TREE_CODE (parms) == TREE_LIST)
    9282              :     {
    9283    811212927 :       if (TMPL_PARMS_DEPTH (parms) > 1)
    9284              :         {
    9285     26897166 :           gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
    9286              :           return_full_args = true;
    9287              :         }
    9288    811212927 :       parms = INNERMOST_TEMPLATE_PARMS (parms);
    9289              :     }
    9290              : 
    9291   1019264075 :   nparms = TREE_VEC_LENGTH (parms);
    9292              : 
    9293              :   /* Determine if there are any parameter packs or default arguments.  */
    9294   2513989848 :   for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
    9295              :     {
    9296   1494725773 :       tree parm = TREE_VEC_ELT (parms, parm_idx);
    9297   1494725773 :       if (variadic_p)
    9298         9874 :         ++post_variadic_parms;
    9299   1494725773 :       if (template_parameter_pack_p (TREE_VALUE (parm)))
    9300    115574597 :         ++variadic_p;
    9301   1494725773 :       if (TREE_PURPOSE (parm))
    9302    207489583 :         ++default_p;
    9303              :     }
    9304              : 
    9305   1019264075 :   inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
    9306              :   /* If there are no parameters that follow a parameter pack, we need to
    9307              :      expand any argument packs so that we can deduce a parameter pack from
    9308              :      some non-packed args followed by an argument pack, as in variadic85.C.
    9309              :      If there are such parameters, we need to leave argument packs intact
    9310              :      so the arguments are assigned properly.  This can happen when dealing
    9311              :      with a nested class inside a partial specialization of a class
    9312              :      template, as in variadic92.C, or when deducing a template parameter pack
    9313              :      from a sub-declarator, as in variadic114.C.  */
    9314   1019264075 :   if (!post_variadic_parms)
    9315   1019255867 :     inner_args = expand_template_argument_pack (inner_args);
    9316              : 
    9317              :   /* Count any pack expansion args.  */
    9318   1019264075 :   variadic_args_p = pack_expansion_args_count (inner_args);
    9319              : 
    9320   2038528150 :   nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    9321     76159669 :   if ((nargs - variadic_args_p > nparms && !variadic_p)
    9322   1093058548 :       || (nargs < nparms - variadic_p
    9323              :           && require_all_args
    9324     23446965 :           && !variadic_args_p
    9325     13480634 :           && (TREE_VEC_ELT (parms, nargs) != error_mark_node
    9326     13480634 :               && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
    9327              :     {
    9328      2365344 :     bad_nargs:
    9329      2365344 :       if (complain & tf_error)
    9330              :         {
    9331           86 :           auto_diagnostic_group d;
    9332           86 :           if (variadic_p || default_p)
    9333              :             {
    9334           12 :               nparms -= variadic_p + default_p;
    9335           12 :               error ("wrong number of template arguments "
    9336              :                      "(%d, should be at least %d)", nargs, nparms);
    9337              :             }
    9338              :           else
    9339           74 :              error ("wrong number of template arguments "
    9340              :                     "(%d, should be %d)", nargs, nparms);
    9341              : 
    9342           86 :           if (in_decl)
    9343           86 :             inform (DECL_SOURCE_LOCATION (in_decl),
    9344              :                     "provided for %qD", in_decl);
    9345           86 :         }
    9346              : 
    9347      2365344 :       return error_mark_node;
    9348              :     }
    9349              :   /* We can't pass a pack expansion to a non-pack parameter of an alias
    9350              :      template (DR 1430).  */
    9351   1016898740 :   else if (in_decl
    9352   1006930272 :            && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9353    827829163 :                || concept_definition_p (in_decl))
    9354    234139047 :            && variadic_args_p
    9355   1021168915 :            && nargs - variadic_args_p < nparms - variadic_p)
    9356              :     {
    9357           24 :       if (complain & tf_error)
    9358              :         {
    9359           27 :           for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
    9360              :             {
    9361           27 :               tree arg = TREE_VEC_ELT (inner_args, i);
    9362           27 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    9363              : 
    9364           27 :               if (PACK_EXPANSION_P (arg)
    9365           27 :                   && !template_parameter_pack_p (parm))
    9366              :                 {
    9367           24 :                   auto_diagnostic_group d;
    9368           24 :                   if (DECL_ALIAS_TEMPLATE_P (in_decl))
    9369           12 :                     error_at (location_of (arg),
    9370              :                               "pack expansion argument for non-pack parameter "
    9371              :                               "%qD of alias template %qD", parm, in_decl);
    9372              :                   else
    9373           12 :                     error_at (location_of (arg),
    9374              :                               "pack expansion argument for non-pack parameter "
    9375              :                               "%qD of concept %qD", parm, in_decl);
    9376           24 :                   inform (DECL_SOURCE_LOCATION (parm), "declared here");
    9377           24 :                   goto found;
    9378           24 :                 }
    9379              :             }
    9380            0 :           gcc_unreachable ();
    9381           24 :         found:;
    9382              :         }
    9383           24 :       return error_mark_node;
    9384              :     }
    9385              : 
    9386              :   /* We need to evaluate the template arguments, even though this
    9387              :      template-id may be nested within a "sizeof".  */
    9388   1016898716 :   cp_evaluated ev;
    9389              : 
    9390   1016898716 :   tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
    9391   1016898716 :   tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
    9392   1016898716 :   int pack_adjust = 0;
    9393   2488147930 :   for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
    9394              :     {
    9395   1485988680 :       tree arg;
    9396   1485988680 :       tree parm;
    9397              : 
    9398              :       /* Get the Ith template parameter.  */
    9399   1485988680 :       parm = TREE_VEC_ELT (parms, parm_idx);
    9400              : 
    9401   1485988680 :       if (parm == error_mark_node)
    9402              :         {
    9403            0 :           TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
    9404            0 :           continue;
    9405              :         }
    9406              : 
    9407              :       /* Calculate the next argument.  */
    9408   1485988680 :       if (arg_idx < nargs)
    9409   1454112404 :         arg = TREE_VEC_ELT (inner_args, arg_idx);
    9410              :       else
    9411              :         arg = NULL_TREE;
    9412              : 
    9413   1485988680 :       if (template_parameter_pack_p (TREE_VALUE (parm))
    9414    114971856 :           && (arg || require_all_args || !(complain & tf_partial))
    9415   1596865587 :           && !(arg && ARGUMENT_PACK_P (arg)))
    9416              :         {
    9417              :           /* Some arguments will be placed in the
    9418              :              template parameter pack PARM.  */
    9419    110876868 :           arg = coerce_template_parameter_pack (parms, parm_idx, args,
    9420              :                                                 inner_args, arg_idx,
    9421              :                                                 new_args, &lost,
    9422              :                                                 in_decl, complain);
    9423              : 
    9424    110876865 :           if (arg == NULL_TREE)
    9425              :             {
    9426              :               /* We don't know how many args we have yet, just use the
    9427              :                  unconverted (and still packed) ones for now.  */
    9428           40 :               ggc_free (new_inner_args);
    9429           40 :               new_inner_args = strip_typedefs (orig_inner_args,
    9430              :                                                /*remove_attrs=*/nullptr,
    9431              :                                                STF_KEEP_INJ_CLASS_NAME);
    9432           40 :               arg_idx = nargs;
    9433           40 :               break;
    9434              :             }
    9435              : 
    9436    110876825 :           TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
    9437              : 
    9438              :           /* Store this argument.  */
    9439    110876825 :           if (arg == error_mark_node)
    9440              :             {
    9441            5 :               lost++;
    9442              :               /* We are done with all of the arguments.  */
    9443            5 :               arg_idx = nargs;
    9444            5 :               break;
    9445              :             }
    9446              :           else
    9447              :             {
    9448    110876820 :               pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
    9449    110876820 :               arg_idx += pack_adjust;
    9450    110876820 :               if (fixed_parameter_pack_p (TREE_VALUE (parm)))
    9451              :                 {
    9452           39 :                   ++fixed_packs;
    9453           39 :                   fixed_pack_adjust += pack_adjust;
    9454              :                 }
    9455              :             }
    9456              : 
    9457    110876820 :           continue;
    9458              :         }
    9459   1375111812 :       else if (arg)
    9460              :         {
    9461   1345322017 :           if (PACK_EXPANSION_P (arg))
    9462              :             {
    9463              :               /* "If every valid specialization of a variadic template
    9464              :                  requires an empty template parameter pack, the template is
    9465              :                  ill-formed, no diagnostic required."  So check that the
    9466              :                  pattern works with this parameter.  */
    9467      1151096 :               tree pattern = PACK_EXPANSION_PATTERN (arg);
    9468      1151096 :               tree conv = convert_template_argument (TREE_VALUE (parm),
    9469              :                                                      pattern, new_args,
    9470              :                                                      complain, parm_idx,
    9471              :                                                      in_decl);
    9472      1151096 :               if (conv == error_mark_node)
    9473              :                 {
    9474            9 :                   if (complain & tf_error)
    9475            6 :                     inform (input_location, "so any instantiation with a "
    9476              :                             "non-empty parameter pack would be ill-formed");
    9477            9 :                   ++lost;
    9478              :                 }
    9479      1151087 :               else if (TYPE_P (conv) && !TYPE_P (pattern))
    9480              :                 /* Recover from missing typename.  */
    9481            9 :                 TREE_VEC_ELT (inner_args, arg_idx)
    9482           18 :                   = make_pack_expansion (conv, complain);
    9483              : 
    9484              :               /* We don't know how many args we have yet, just
    9485              :                  use the unconverted (but unpacked) ones for now.  */
    9486      1151096 :               ggc_free (new_inner_args);
    9487      1151096 :               new_inner_args = strip_typedefs (inner_args,
    9488              :                                                /*remove_attrs=*/nullptr,
    9489              :                                                STF_KEEP_INJ_CLASS_NAME);
    9490      1151096 :               arg_idx = nargs;
    9491      1151096 :               break;
    9492              :             }
    9493              :         }
    9494     29789795 :       else if (require_all_args)
    9495              :         {
    9496              :           /* There must be a default arg in this case.  */
    9497     16201473 :           arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
    9498     16201473 :                                      complain | (processing_template_decl
    9499     16201473 :                                                  ? tf_partial : tf_none),
    9500              :                                      in_decl);
    9501              :           /* The position of the first default template argument,
    9502              :              is also the number of non-defaulted arguments in NEW_INNER_ARGS.
    9503              :              Record that.  */
    9504     16201473 :           if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9505     13480542 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9506              :                                                  arg_idx - pack_adjust);
    9507              :         }
    9508              :       else
    9509              :         break;
    9510              : 
    9511   1360372394 :       if (arg == error_mark_node)
    9512              :         {
    9513         1005 :           if (complain & tf_error)
    9514          977 :             error ("template argument %d is invalid", arg_idx + 1);
    9515              :         }
    9516   1360371389 :       else if (!arg)
    9517              :         {
    9518              :           /* This can occur if there was an error in the template
    9519              :              parameter list itself (which we would already have
    9520              :              reported) that we are trying to recover from, e.g., a class
    9521              :              template with a parameter list such as
    9522              :              template<typename..., typename> (cpp0x/variadic150.C).  */
    9523           35 :           ++lost;
    9524              : 
    9525              :           /* This can also happen with a fixed parameter pack (71834).  */
    9526           35 :           if (arg_idx >= nargs)
    9527            6 :             ++missing;
    9528              :         }
    9529              :       else
    9530   1360371354 :         arg = convert_template_argument (TREE_VALUE (parm),
    9531              :                                          arg, new_args, complain,
    9532              :                                          parm_idx, in_decl);
    9533              : 
    9534   1360372394 :       if (arg == error_mark_node)
    9535      4120848 :         lost++;
    9536              : 
    9537   1360372394 :       TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
    9538              :     }
    9539              : 
    9540   1016898713 :   if (missing || arg_idx < nargs - variadic_args_p)
    9541              :     {
    9542              :       /* If we had fixed parameter packs, we didn't know how many arguments we
    9543              :          actually needed earlier; now we do.  */
    9544            9 :       nparms += fixed_pack_adjust;
    9545            9 :       variadic_p -= fixed_packs;
    9546            9 :       goto bad_nargs;
    9547              :     }
    9548              : 
    9549   1016898704 :   if (arg_idx < nargs)
    9550              :     {
    9551              :       /* We had some pack expansion arguments that will only work if the packs
    9552              :          are empty, but wait until instantiation time to complain.
    9553              :          See variadic-ttp3.C.  */
    9554              : 
    9555              :       /* Except that we can't provide empty packs to alias templates or
    9556              :          concepts when there are no corresponding parameters. Basically,
    9557              :          we can get here with this:
    9558              : 
    9559              :              template<typename T> concept C = true;
    9560              : 
    9561              :              template<typename... Args>
    9562              :                requires C<Args...>
    9563              :              void f();
    9564              : 
    9565              :          When parsing C<Args...>, we try to form a concept check of
    9566              :          C<?, Args...>. Without the extra check for substituting an empty
    9567              :          pack past the last parameter, we can accept the check as valid.
    9568              : 
    9569              :          FIXME: This may be valid for alias templates (but I doubt it).
    9570              : 
    9571              :          FIXME: The error could be better also.   */
    9572       320994 :       if (in_decl && concept_definition_p (in_decl))
    9573              :         {
    9574            0 :           if (complain & tf_error)
    9575            0 :             error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
    9576              :                       "too many arguments");
    9577   1016898704 :           return error_mark_node;
    9578              :         }
    9579              : 
    9580       213743 :       int len = nparms + (nargs - arg_idx);
    9581       213743 :       tree args = make_tree_vec (len);
    9582       213743 :       int i = 0;
    9583       641232 :       for (; i < nparms; ++i)
    9584       213746 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
    9585       427486 :       for (; i < len; ++i, ++arg_idx)
    9586       213743 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
    9587              :                                                arg_idx - pack_adjust);
    9588       213743 :       new_inner_args = args;
    9589              :     }
    9590              : 
    9591   1016898704 :   if (lost)
    9592              :     {
    9593      4121055 :       gcc_assert (!(complain & tf_error) || seen_error ());
    9594      4121055 :       return error_mark_node;
    9595              :     }
    9596              : 
    9597   1012777649 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9598    998146066 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9599              :                                          TREE_VEC_LENGTH (new_inner_args));
    9600              : 
    9601              :   /* If we expanded packs in inner_args and aren't returning it now, the
    9602              :      expanded vec is garbage.  */
    9603   1012777649 :   if (inner_args != new_inner_args
    9604   1011645943 :       && inner_args != orig_inner_args)
    9605     90657121 :     ggc_free (inner_args);
    9606              : 
    9607   1012777649 :   return return_full_args ? new_args : new_inner_args;
    9608              : }
    9609              : 
    9610              : /* Returns true if T is a wrapper to make a C++20 template parameter
    9611              :    object const.  */
    9612              : 
    9613              : static bool
    9614   9781833577 : class_nttp_const_wrapper_p (tree t)
    9615              : {
    9616   9781833577 :   if (cxx_dialect < cxx20)
    9617              :     return false;
    9618   9725057377 :   return (TREE_CODE (t) == VIEW_CONVERT_EXPR
    9619          633 :           && CP_TYPE_CONST_P (TREE_TYPE (t))
    9620   9725058010 :           && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
    9621              : }
    9622              : 
    9623              : /* Returns 1 if template args OT and NT are equivalent.  */
    9624              : 
    9625              : int
    9626   4970267354 : template_args_equal (tree ot, tree nt)
    9627              : {
    9628   4970267354 :   if (nt == ot)
    9629              :     return 1;
    9630   2866615129 :   if (nt == NULL_TREE || ot == NULL_TREE)
    9631              :     return false;
    9632   2866286485 :   if (nt == any_targ_node || ot == any_targ_node)
    9633              :     return true;
    9634              : 
    9635   2866286481 :   if (class_nttp_const_wrapper_p (nt))
    9636          212 :     nt = TREE_OPERAND (nt, 0);
    9637   2866286481 :   if (class_nttp_const_wrapper_p (ot))
    9638          165 :     ot = TREE_OPERAND (ot, 0);
    9639              : 
    9640              :   /* DR 1558: Don't treat an alias template specialization with dependent
    9641              :      arguments as equivalent to its underlying type when used as a template
    9642              :      argument; we need them to be distinct so that we substitute into the
    9643              :      specialization arguments at instantiation time.  And aliases can't be
    9644              :      equivalent without being ==, so we don't need to look any deeper.
    9645              : 
    9646              :      During partial ordering, however, we need to treat them normally so we can
    9647              :      order uses of the same alias with different cv-qualification (79960).  */
    9648   2866286481 :   auto cso = make_temp_override (comparing_dependent_aliases);
    9649   2866286481 :   if (!comparing_for_partial_ordering)
    9650   2860880319 :     ++comparing_dependent_aliases;
    9651              : 
    9652   2866286481 :   if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
    9653              :     /* For member templates */
    9654    158651185 :     return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
    9655   2774964988 :   else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
    9656     24938202 :     return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
    9657     23450476 :             && template_args_equal (PACK_EXPANSION_PATTERN (ot),
    9658     23450476 :                                     PACK_EXPANSION_PATTERN (nt))
    9659     39687739 :             && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
    9660     39687739 :                                     PACK_EXPANSION_EXTRA_ARGS (nt)));
    9661   2749486516 :   else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
    9662    608905361 :     return cp_tree_equal (ot, nt);
    9663   2140581155 :   else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
    9664            0 :     gcc_unreachable ();
    9665   2140581155 :   else if (TYPE_P (nt) || TYPE_P (ot))
    9666              :     {
    9667   1875062180 :       if (!(TYPE_P (nt) && TYPE_P (ot)))
    9668              :         return false;
    9669   1855836293 :       return same_type_p (ot, nt);
    9670              :     }
    9671              :   else
    9672              :     {
    9673              :       /* Try to treat a template non-type argument that has been converted
    9674              :          to the parameter type as equivalent to one that hasn't yet.  */
    9675    265518975 :       for (enum tree_code code1 = TREE_CODE (ot);
    9676    267259140 :            CONVERT_EXPR_CODE_P (code1)
    9677    267259140 :              || code1 == NON_LVALUE_EXPR;
    9678      1740165 :            code1 = TREE_CODE (ot))
    9679      1740165 :         ot = TREE_OPERAND (ot, 0);
    9680              : 
    9681    265518975 :       for (enum tree_code code2 = TREE_CODE (nt);
    9682    267317762 :            CONVERT_EXPR_CODE_P (code2)
    9683    267317762 :              || code2 == NON_LVALUE_EXPR;
    9684      1798787 :            code2 = TREE_CODE (nt))
    9685      1798787 :         nt = TREE_OPERAND (nt, 0);
    9686              : 
    9687    265518975 :       return cp_tree_equal (ot, nt);
    9688              :     }
    9689   2866286481 : }
    9690              : 
    9691              : /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
    9692              :    template arguments.  Returns false otherwise, and updates OLDARG_PTR and
    9693              :    NEWARG_PTR with the offending arguments if they are non-NULL.  */
    9694              : 
    9695              : bool
    9696   3468566699 : comp_template_args (tree oldargs, tree newargs,
    9697              :                     tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
    9698              : {
    9699   3468566699 :   if (oldargs == newargs)
    9700              :     return true;
    9701              : 
    9702   2616448227 :   if (!oldargs || !newargs)
    9703              :     return false;
    9704              : 
    9705   2616448226 :   if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    9706              :     return false;
    9707              : 
    9708   4126231300 :   for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
    9709              :     {
    9710   3133785396 :       tree nt = TREE_VEC_ELT (newargs, i);
    9711   3133785396 :       tree ot = TREE_VEC_ELT (oldargs, i);
    9712              : 
    9713   3133785396 :       if (! template_args_equal (ot, nt))
    9714              :         {
    9715   1622846944 :           if (oldarg_ptr != NULL)
    9716           12 :             *oldarg_ptr = ot;
    9717   1622846944 :           if (newarg_ptr != NULL)
    9718           12 :             *newarg_ptr = nt;
    9719   1622846944 :           return false;
    9720              :         }
    9721              :     }
    9722              :   return true;
    9723              : }
    9724              : 
    9725              : static bool
    9726     25196647 : comp_template_args_porder (tree oargs, tree nargs)
    9727              : {
    9728     25196647 :   ++comparing_for_partial_ordering;
    9729     25196647 :   bool equal = comp_template_args (oargs, nargs);
    9730     25196647 :   --comparing_for_partial_ordering;
    9731     25196647 :   return equal;
    9732              : }
    9733              : 
    9734              : /* Implement a freelist interface for objects of type T.
    9735              : 
    9736              :    Head is a separate object, rather than a regular member, so that we
    9737              :    can define it as a GTY deletable pointer, which is highly
    9738              :    desirable.  A data member could be declared that way, but then the
    9739              :    containing object would implicitly get GTY((user)), which would
    9740              :    prevent us from instantiating freelists as global objects.
    9741              :    Although this way we can create freelist global objects, they're
    9742              :    such thin wrappers that instantiating temporaries at every use
    9743              :    loses nothing and saves permanent storage for the freelist object.
    9744              : 
    9745              :    Member functions next, anew, poison and reinit have default
    9746              :    implementations that work for most of the types we're interested
    9747              :    in, but if they don't work for some type, they should be explicitly
    9748              :    specialized.  See the comments before them for requirements, and
    9749              :    the example specializations for the tree_list_freelist.  */
    9750              : template <typename T>
    9751              : class freelist
    9752              : {
    9753              :   /* Return the next object in a chain.  We could just do type
    9754              :      punning, but if we access the object with its underlying type, we
    9755              :      avoid strict-aliasing trouble.  This needs only work between
    9756              :      poison and reinit.  */
    9757   1232004472 :   static T *&next (T *obj) { return obj->next; }
    9758              : 
    9759              :   /* Return a newly allocated, uninitialized or minimally-initialized
    9760              :      object of type T.  Any initialization performed by anew should
    9761              :      either remain across the life of the object and the execution of
    9762              :      poison, or be redone by reinit.  */
    9763     19004049 :   static T *anew () { return ggc_alloc<T> (); }
    9764              : 
    9765              :   /* Optionally scribble all over the bits holding the object, so that
    9766              :      they become (mostly?) uninitialized memory.  This is called while
    9767              :      preparing to make the object part of the free list.  */
    9768   1249081798 :   static void poison (T *obj) {
    9769   1249081798 :     T *p ATTRIBUTE_UNUSED = obj;
    9770   1249081798 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9771              : 
    9772              : #ifdef ENABLE_GC_CHECKING
    9773              :     /* Poison the data, to indicate the data is garbage.  */
    9774              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
    9775   1249081798 :     memset (p, 0xa5, sizeof (*p));
    9776              : #endif
    9777              :     /* Let valgrind know the object is free.  */
    9778              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
    9779              : 
    9780              :     /* Let valgrind know the next portion of the object is available,
    9781              :        but uninitialized.  */
    9782              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9783              :   }
    9784              : 
    9785              :   /* Bring an object that underwent at least one lifecycle after anew
    9786              :      and before the most recent free and poison, back to a usable
    9787              :      state, reinitializing whatever is needed for it to be
    9788              :      functionally equivalent to an object just allocated and returned
    9789              :      by anew.  This may poison or clear the next field, used by
    9790              :      freelist housekeeping after poison was called.  */
    9791   1232004472 :   static void reinit (T *obj) {
    9792   1232004472 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9793              : 
    9794              : #ifdef ENABLE_GC_CHECKING
    9795   1232004472 :     memset (q, 0xa5, sizeof (*q));
    9796              : #endif
    9797              :     /* Let valgrind know the entire object is available, but
    9798              :        uninitialized.  */
    9799              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
    9800              :   }
    9801              : 
    9802              :   /* Reference a GTY-deletable pointer that points to the first object
    9803              :      in the free list proper.  */
    9804              :   T *&head;
    9805              : public:
    9806              :   /* Construct a freelist object chaining objects off of HEAD.  */
    9807   2500092521 :   freelist (T *&head) : head(head) {}
    9808              : 
    9809              :   /* Add OBJ to the free object list.  The former head becomes OBJ's
    9810              :      successor.  */
    9811   1249082543 :   void free (T *obj)
    9812              :   {
    9813   1249082543 :     poison (obj);
    9814   1249082543 :     next (obj) = head;
    9815   1249082543 :     head = obj;
    9816   1249082543 :   }
    9817              : 
    9818              :   /* Take an object from the free list, if one is available, or
    9819              :      allocate a new one.  Objects taken from the free list should be
    9820              :      regarded as filled with garbage, except for bits that are
    9821              :      configured to be preserved across free and alloc.  */
    9822   1251009978 :   T *alloc ()
    9823              :   {
    9824   1251009978 :     if (head)
    9825              :       {
    9826   1232005137 :         T *obj = head;
    9827   1232005137 :         head = next (head);
    9828   1232005137 :         reinit (obj);
    9829   1232005137 :         return obj;
    9830              :       }
    9831              :     else
    9832     19004841 :       return anew ();
    9833              :   }
    9834              : };
    9835              : 
    9836              : /* Explicitly specialize the interfaces for freelist<tree_node>: we
    9837              :    want to allocate a TREE_LIST using the usual interface, and ensure
    9838              :    TREE_CHAIN remains functional.  Alas, we have to duplicate a bit of
    9839              :    build_tree_list logic in reinit, so this could go out of sync.  */
    9840              : template <>
    9841              : inline tree &
    9842         2155 : freelist<tree_node>::next (tree obj)
    9843              : {
    9844         2155 :   return TREE_CHAIN (obj);
    9845              : }
    9846              : template <>
    9847              : inline tree
    9848          792 : freelist<tree_node>::anew ()
    9849              : {
    9850          792 :   return build_tree_list (NULL, NULL);
    9851              : }
    9852              : template <>
    9853              : inline void
    9854          745 : freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
    9855              : {
    9856          745 :   int size ATTRIBUTE_UNUSED = sizeof (tree_list);
    9857          745 :   tree p ATTRIBUTE_UNUSED = obj;
    9858          745 :   tree_base *b ATTRIBUTE_UNUSED = &obj->base;
    9859          745 :   tree *q ATTRIBUTE_UNUSED = &next (obj);
    9860              : 
    9861              : #ifdef ENABLE_GC_CHECKING
    9862          745 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9863              : 
    9864              :   /* Poison the data, to indicate the data is garbage.  */
    9865          745 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
    9866          745 :   memset (p, 0xa5, size);
    9867              : #endif
    9868              :   /* Let valgrind know the object is free.  */
    9869          745 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
    9870              :   /* But we still want to use the TREE_CODE and TREE_CHAIN parts.  */
    9871          745 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9872          745 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9873              : 
    9874              : #ifdef ENABLE_GC_CHECKING
    9875          745 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
    9876              :   /* Keep TREE_CHAIN functional.  */
    9877          745 :   TREE_SET_CODE (obj, TREE_LIST);
    9878              : #else
    9879              :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9880              : #endif
    9881          745 : }
    9882              : template <>
    9883              : inline void
    9884          665 : freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
    9885              : {
    9886          665 :   tree_common *c ATTRIBUTE_UNUSED = &obj->common;
    9887              : 
    9888              : #ifdef ENABLE_GC_CHECKING
    9889          665 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9890          665 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9891          665 :   memset (obj, 0, sizeof (tree_list));
    9892              : #endif
    9893              : 
    9894              :   /* Let valgrind know the entire object is available, but
    9895              :      uninitialized.  */
    9896          665 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9897              : 
    9898              : #ifdef ENABLE_GC_CHECKING
    9899          665 :   TREE_SET_CODE (obj, TREE_LIST);
    9900              : #else
    9901              :   TREE_CHAIN (obj) = NULL_TREE;
    9902              :   TREE_TYPE (obj) = NULL_TREE;
    9903              : #endif
    9904          665 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c)));
    9905          665 : }
    9906              : 
    9907              : /* Point to the first object in the TREE_LIST freelist.  */
    9908              : static GTY((deletable)) tree tree_list_freelist_head;
    9909              : /* Return the/an actual TREE_LIST freelist.  */
    9910              : static inline freelist<tree_node>
    9911         2202 : tree_list_freelist ()
    9912              : {
    9913         2202 :   return tree_list_freelist_head;
    9914              : }
    9915              : 
    9916              : /* Point to the first object in the tinst_level freelist.  */
    9917              : static GTY((deletable)) tinst_level *tinst_level_freelist_head;
    9918              : /* Return the/an actual tinst_level freelist.  */
    9919              : static inline freelist<tinst_level>
    9920   2450402874 : tinst_level_freelist ()
    9921              : {
    9922   2450402874 :   return tinst_level_freelist_head;
    9923              : }
    9924              : 
    9925              : /* Point to the first object in the pending_template freelist.  */
    9926              : static GTY((deletable)) pending_template *pending_template_freelist_head;
    9927              : /* Return the/an actual pending_template freelist.  */
    9928              : static inline freelist<pending_template>
    9929     49687445 : pending_template_freelist ()
    9930              : {
    9931     49687445 :   return pending_template_freelist_head;
    9932              : }
    9933              : 
    9934              : /* Build the TREE_LIST object out of a split list, store it
    9935              :    permanently, and return it.  */
    9936              : tree
    9937         1457 : tinst_level::to_list ()
    9938              : {
    9939         1457 :   gcc_assert (split_list_p ());
    9940         1457 :   tree ret = tree_list_freelist ().alloc ();
    9941         1457 :   TREE_PURPOSE (ret) = tldcl;
    9942         1457 :   TREE_VALUE (ret) = targs;
    9943         1457 :   tldcl = ret;
    9944         1457 :   targs = NULL;
    9945         1457 :   gcc_assert (tree_list_p ());
    9946         1457 :   return ret;
    9947              : }
    9948              : 
    9949              : const unsigned short tinst_level::refcount_infinity;
    9950              : 
    9951              : /* Increment OBJ's refcount unless it is already infinite.  */
    9952              : static tinst_level *
    9953   3748527543 : inc_refcount_use (tinst_level *obj)
    9954              : {
    9955   3125051473 :   if (obj && obj->refcount != tinst_level::refcount_infinity)
    9956   3125051473 :     ++obj->refcount;
    9957   3748527543 :   return obj;
    9958              : }
    9959              : 
    9960              : /* Release storage for OBJ and node, if it's a TREE_LIST.  */
    9961              : void
    9962   1224538793 : tinst_level::free (tinst_level *obj)
    9963              : {
    9964   1224538793 :   if (obj->tree_list_p ())
    9965          745 :     tree_list_freelist ().free (obj->get_node ());
    9966   1224538793 :   tinst_level_freelist ().free (obj);
    9967   1224538793 : }
    9968              : 
    9969              : /* Decrement OBJ's refcount if not infinite.  If it reaches zero, release
    9970              :    OBJ's DECL and OBJ, and start over with the tinst_level object that
    9971              :    used to be referenced by OBJ's NEXT.  */
    9972              : static void
    9973   3795978194 : dec_refcount_use (tinst_level *obj)
    9974              : {
    9975   3795978194 :   while (obj
    9976   3123285788 :          && obj->refcount != tinst_level::refcount_infinity
    9977   8143802775 :          && !--obj->refcount)
    9978              :     {
    9979   1224538793 :       tinst_level *next = obj->next;
    9980   1224538793 :       tinst_level::free (obj);
    9981   1224538793 :       obj = next;
    9982              :     }
    9983   3795978194 : }
    9984              : 
    9985              : /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
    9986              :    and of the former PTR.  Omitting the second argument is equivalent
    9987              :    to passing (T*)NULL; this is allowed because passing the
    9988              :    zero-valued integral constant NULL confuses type deduction and/or
    9989              :    overload resolution.  */
    9990              : template <typename T>
    9991              : static void
    9992   3795978194 : set_refcount_ptr (T *& ptr, T *obj = NULL)
    9993              : {
    9994   3748527543 :   T *save = ptr;
    9995   3795978194 :   ptr = inc_refcount_use (obj);
    9996   3748527543 :   dec_refcount_use (save);
    9997   3748527543 : }
    9998              : 
    9999              : static void
   10000     31214674 : add_pending_template (tree d)
   10001              : {
   10002     31214674 :   tree ti = (TYPE_P (d)
   10003     31214674 :              ? CLASSTYPE_TEMPLATE_INFO (d)
   10004     31214674 :              : DECL_TEMPLATE_INFO (d));
   10005     31214674 :   struct pending_template *pt;
   10006     31214674 :   int level;
   10007              : 
   10008     31214674 :   if (TI_PENDING_TEMPLATE_FLAG (ti))
   10009              :     return;
   10010              : 
   10011              :   /* We are called both from instantiate_decl, where we've already had a
   10012              :      tinst_level pushed, and instantiate_template, where we haven't.
   10013              :      Compensate.  */
   10014     25144440 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   10015     50288880 :   level = !current_tinst_level
   10016     50288880 :     || current_tinst_level->maybe_get_node () != d;
   10017              : 
   10018            0 :   if (level)
   10019            0 :     push_tinst_level (d);
   10020              : 
   10021     25144440 :   pt = pending_template_freelist ().alloc ();
   10022     25144440 :   pt->next = NULL;
   10023     25144440 :   pt->tinst = NULL;
   10024     25144440 :   set_refcount_ptr (pt->tinst, current_tinst_level);
   10025     25144440 :   if (last_pending_template)
   10026     25113374 :     last_pending_template->next = pt;
   10027              :   else
   10028        31066 :     pending_templates = pt;
   10029              : 
   10030     25144440 :   last_pending_template = pt;
   10031              : 
   10032     25144440 :   TI_PENDING_TEMPLATE_FLAG (ti) = 1;
   10033              : 
   10034     25144440 :   if (level)
   10035            0 :     pop_tinst_level ();
   10036              : }
   10037              : 
   10038              : /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
   10039              :    ARGLIST.  Valid choices for FNS are given in the cp-tree.def
   10040              :    documentation for TEMPLATE_ID_EXPR.  */
   10041              : 
   10042              : tree
   10043     81805661 : lookup_template_function (tree fns, tree arglist)
   10044              : {
   10045     81805661 :   if (fns == error_mark_node || arglist == error_mark_node)
   10046              :     return error_mark_node;
   10047              : 
   10048     81805629 :   gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
   10049              : 
   10050     81805629 :   if (!is_overloaded_fn (fns) && !identifier_p (fns))
   10051              :     {
   10052            5 :       error ("%q#D is not a function template", fns);
   10053            5 :       return error_mark_node;
   10054              :     }
   10055              : 
   10056     81805624 :   if (BASELINK_P (fns))
   10057              :     {
   10058       908406 :       fns = copy_node (fns);
   10059      1816812 :       BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
   10060              :                                          unknown_type_node,
   10061       908406 :                                          BASELINK_FUNCTIONS (fns),
   10062              :                                          arglist);
   10063       908406 :       return fns;
   10064              :     }
   10065              : 
   10066     80897218 :   return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
   10067              : }
   10068              : 
   10069              : /* Within the scope of a template class S<T>, the name S gets bound
   10070              :    (in build_self_reference) to a TYPE_DECL for the class, not a
   10071              :    TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
   10072              :    or one of its enclosing classes, and that type is a template,
   10073              :    return the associated TEMPLATE_DECL.  Otherwise, the original
   10074              :    DECL is returned.
   10075              : 
   10076              :    Also handle the case when DECL is a TREE_LIST of ambiguous
   10077              :    injected-class-names from different bases.  */
   10078              : 
   10079              : tree
   10080    697762734 : maybe_get_template_decl_from_type_decl (tree decl)
   10081              : {
   10082    697762734 :   if (decl == NULL_TREE)
   10083              :     return decl;
   10084              : 
   10085              :   /* DR 176: A lookup that finds an injected-class-name (10.2
   10086              :      [class.member.lookup]) can result in an ambiguity in certain cases
   10087              :      (for example, if it is found in more than one base class). If all of
   10088              :      the injected-class-names that are found refer to specializations of
   10089              :      the same class template, and if the name is followed by a
   10090              :      template-argument-list, the reference refers to the class template
   10091              :      itself and not a specialization thereof, and is not ambiguous.  */
   10092    697762734 :   if (TREE_CODE (decl) == TREE_LIST)
   10093              :     {
   10094              :       tree t, tmpl = NULL_TREE;
   10095        36216 :       for (t = decl; t; t = TREE_CHAIN (t))
   10096              :         {
   10097        36114 :           tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
   10098        36114 :           if (!tmpl)
   10099              :             tmpl = elt;
   10100        18057 :           else if (tmpl != elt)
   10101              :             break;
   10102              :         }
   10103        18057 :       if (tmpl && t == NULL_TREE)
   10104              :         return tmpl;
   10105              :       else
   10106              :         return decl;
   10107              :     }
   10108              : 
   10109    697744677 :   return (decl != NULL_TREE
   10110    697744677 :           && DECL_SELF_REFERENCE_P (decl)
   10111      6434525 :           && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
   10112      6434525 :     ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
   10113              : }
   10114              : 
   10115              : /* If TYPE is the generic implicit instantiation A<T>, return the primary
   10116              :    template type A<T> (which is suitable for entering into, e.g. for defining
   10117              :    a member of A<T>), otherwise return TYPE.  */
   10118              : 
   10119              : tree
   10120    601275243 : adjust_type_for_entering_scope (tree type)
   10121              : {
   10122    599024029 :   if (CLASS_TYPE_P (type)
   10123    599024026 :       && dependent_type_p (type)
   10124     48014755 :       && TYPE_TEMPLATE_INFO (type)
   10125              :       /* We detect the generic implicit instantiation A<T> by inspecting
   10126              :          TYPE_CANONICAL, which lookup_template_class sets to the primary
   10127              :          template type A<T>.  */
   10128    649289982 :       && TYPE_CANONICAL (type) == TREE_TYPE (TYPE_TI_TEMPLATE (type)))
   10129     18410589 :     type = TYPE_CANONICAL (type);
   10130              : 
   10131    601275243 :   return type;
   10132              : }
   10133              : 
   10134              : /* Convenience wrapper over tsubst that does adjust_type_for_entering_scope
   10135              :    on the result.  */
   10136              : 
   10137              : static tree
   10138    582843048 : tsubst_entering_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   10139              : {
   10140    582843048 :   t = tsubst (t, args, complain, in_decl);
   10141    582843048 :   return adjust_type_for_entering_scope (t);
   10142              : }
   10143              : 
   10144              : /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
   10145              :    parameters, find the desired type.
   10146              : 
   10147              :    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
   10148              : 
   10149              :    If D1 is an identifier and CONTEXT is non-NULL, then the lookup is
   10150              :    carried out in CONTEXT. Currently, only namespaces are supported for
   10151              :    CONTEXT.
   10152              : 
   10153              :    If D1 is an identifier and CONTEXT is NULL, the lookup is performed
   10154              :    in the innermost non-namespace binding.
   10155              : 
   10156              :    Otherwise CONTEXT is ignored and no lookup is carried out.
   10157              : 
   10158              :    IN_DECL, if non-NULL, is the template declaration we are trying to
   10159              :    instantiate.
   10160              : 
   10161              :    Issue error and warning messages under control of COMPLAIN.
   10162              : 
   10163              :    ??? Note that this function is currently called *twice* for each
   10164              :    template-id: the first time from the parser, while creating the
   10165              :    incomplete type (finish_template_type), and the second type during the
   10166              :    real instantiation (instantiate_template_class). This is surely something
   10167              :    that we want to avoid. It also causes some problems with argument
   10168              :    coercion (see convert_nontype_argument for more information on this).  */
   10169              : 
   10170              : tree
   10171   1248288727 : lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
   10172              :                        tsubst_flags_t complain)
   10173              : {
   10174   1248288727 :   auto_timevar tv (TV_TEMPLATE_INST);
   10175              : 
   10176   1248288727 :   tree templ = NULL_TREE, parmlist;
   10177   1248288727 :   tree t;
   10178   1248288727 :   spec_entry **slot;
   10179   1248288727 :   spec_entry *entry;
   10180              : 
   10181   1248288727 :   if (identifier_p (d1) && context)
   10182              :     {
   10183       316674 :       gcc_checking_assert (TREE_CODE (context) == NAMESPACE_DECL);
   10184       316674 :       push_decl_namespace (context);
   10185       316674 :       templ = lookup_name (d1, LOOK_where::NAMESPACE, LOOK_want::NORMAL);
   10186       316674 :       pop_decl_namespace ();
   10187              :     }
   10188   1247972053 :   else if (identifier_p (d1))
   10189              :     {
   10190            0 :       tree value = innermost_non_namespace_value (d1);
   10191            0 :       if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
   10192              :         templ = value;
   10193              :       else
   10194              :         {
   10195            0 :           templ = lookup_name (d1);
   10196            0 :           templ = maybe_get_template_decl_from_type_decl (templ);
   10197              :         }
   10198              :     }
   10199   1247972053 :   else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
   10200              :     {
   10201            0 :       tree type = TREE_TYPE (d1);
   10202              : 
   10203              :       /* If we are declaring a constructor, say A<T>::A<T>, we will get
   10204              :          an implicit typename for the second A.  Deal with it.  */
   10205            0 :       if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
   10206            0 :         type = TREE_TYPE (type);
   10207              : 
   10208            0 :       if (CLASSTYPE_TEMPLATE_INFO (type))
   10209              :         {
   10210            0 :           templ = CLASSTYPE_TI_TEMPLATE (type);
   10211            0 :           d1 = DECL_NAME (templ);
   10212              :         }
   10213              :     }
   10214   1247972053 :   else if (TREE_CODE (d1) == ENUMERAL_TYPE
   10215   1247972053 :            || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
   10216              :     {
   10217   1065296578 :       templ = TYPE_TI_TEMPLATE (d1);
   10218   1065296578 :       d1 = DECL_NAME (templ);
   10219              :     }
   10220    182675475 :   else if (DECL_TYPE_TEMPLATE_P (d1))
   10221              :     {
   10222    182675475 :       templ = d1;
   10223    182675475 :       d1 = DECL_NAME (templ);
   10224              :     }
   10225            0 :   else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
   10226              :     {
   10227            0 :       templ = d1;
   10228            0 :       d1 = DECL_NAME (templ);
   10229              :     }
   10230              : 
   10231              :   /* Issue an error message if we didn't find a template.  */
   10232   1248288727 :   if (! templ)
   10233              :     {
   10234         1459 :       if (complain & tf_error)
   10235            0 :         error ("%qT is not a template", d1);
   10236         1459 :       return error_mark_node;
   10237              :     }
   10238              : 
   10239   1248287268 :   if (TREE_CODE (templ) != TEMPLATE_DECL
   10240              :          /* Make sure it's a user visible template, if it was named by
   10241              :             the user.  */
   10242   1248287268 :       || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
   10243    181603655 :           && !PRIMARY_TEMPLATE_P (templ)))
   10244              :     {
   10245            0 :       if (complain & tf_error)
   10246              :         {
   10247            0 :           error ("non-template type %qT used as a template", d1);
   10248            0 :           if (in_decl)
   10249            0 :             error ("for template declaration %q+D", in_decl);
   10250              :         }
   10251            0 :       return error_mark_node;
   10252              :     }
   10253              : 
   10254   1248287268 :   complain &= ~tf_user;
   10255              : 
   10256              :   /* An alias that just changes the name of a template is equivalent to the
   10257              :      other template, so if any of the arguments are pack expansions, strip
   10258              :      the alias to avoid problems with a pack expansion passed to a non-pack
   10259              :      alias template parameter (DR 1430).  */
   10260   1248287268 :   if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
   10261      7028530 :     templ = get_underlying_template (templ);
   10262              : 
   10263   1248287268 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
   10264              :     {
   10265       334443 :       tree parm;
   10266       334443 :       tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
   10267       334443 :       if (arglist2 == error_mark_node
   10268       334443 :           || (!uses_template_parms (arglist2)
   10269        11766 :               && check_instantiated_args (templ, arglist2, complain)))
   10270           18 :         return error_mark_node;
   10271              : 
   10272       334425 :       parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
   10273       334425 :       return parm;
   10274              :     }
   10275              :   else
   10276              :     {
   10277   1247952825 :       tree template_type = TREE_TYPE (templ);
   10278   1247952825 :       tree gen_tmpl;
   10279   1247952825 :       tree type_decl;
   10280   1247952825 :       tree found = NULL_TREE;
   10281   1247952825 :       int arg_depth;
   10282   1247952825 :       int parm_depth;
   10283   1247952825 :       int is_dependent_type;
   10284   1247952825 :       int use_partial_inst_tmpl = false;
   10285              : 
   10286   1247952825 :       if (template_type == error_mark_node)
   10287              :         /* An error occurred while building the template TEMPL, and a
   10288              :            diagnostic has most certainly been emitted for that
   10289              :            already.  Let's propagate that error.  */
   10290              :         return error_mark_node;
   10291              : 
   10292   1247952822 :       gen_tmpl = most_general_template (templ);
   10293   1247952822 :       if (modules_p ())
   10294      3184021 :         lazy_load_pendings (gen_tmpl);
   10295              : 
   10296   1247952822 :       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
   10297   1247952822 :       parm_depth = TMPL_PARMS_DEPTH (parmlist);
   10298   2495905644 :       arg_depth = TMPL_ARGS_DEPTH (arglist);
   10299              : 
   10300   1247952822 :       if (arg_depth == 1 && parm_depth > 1)
   10301              :         {
   10302              :           /* We've been given an incomplete set of template arguments.
   10303              :              For example, given:
   10304              : 
   10305              :                template <class T> struct S1 {
   10306              :                  template <class U> struct S2 {};
   10307              :                  template <class U> struct S2<U*> {};
   10308              :                 };
   10309              : 
   10310              :              we will be called with an ARGLIST of `U*', but the
   10311              :              TEMPLATE will be `template <class T> template
   10312              :              <class U> struct S1<T>::S2'.  We must fill in the missing
   10313              :              arguments.  */
   10314      6753637 :           tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
   10315      6753637 :           arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
   10316     13507274 :           arg_depth = TMPL_ARGS_DEPTH (arglist);
   10317              :         }
   10318              : 
   10319              :       /* Now we should have enough arguments.  */
   10320   1247952822 :       gcc_assert (parm_depth == arg_depth);
   10321              : 
   10322   1247952822 :       if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
   10323              :         {
   10324              :           /* The user referred to a specialization of an alias
   10325              :             template represented by GEN_TMPL.
   10326              : 
   10327              :             [temp.alias]/2 says:
   10328              : 
   10329              :                 When a template-id refers to the specialization of an
   10330              :                 alias template, it is equivalent to the associated
   10331              :                 type obtained by substitution of its
   10332              :                 template-arguments for the template-parameters in the
   10333              :                 type-id of the alias template.  */
   10334              : 
   10335     38734142 :           t = instantiate_alias_template (gen_tmpl, arglist, complain);
   10336              :           /* Note that the call above (by indirectly calling
   10337              :              register_specialization in tsubst_decl) registers the
   10338              :              TYPE_DECL representing the specialization of the alias
   10339              :              template.  So next time someone substitutes ARGLIST for
   10340              :              the template parms into the alias template (GEN_TMPL),
   10341              :              she'll get that TYPE_DECL back.  */
   10342              : 
   10343     38734142 :           if (t == error_mark_node)
   10344              :             return error_mark_node;
   10345     38724143 :           return TREE_TYPE (t);
   10346              :         }
   10347              : 
   10348              :       /* From here on, we're only interested in the most general
   10349              :          template.  */
   10350              : 
   10351              :       /* Shortcut looking up the current class scope again.  */
   10352   1209218680 :       for (tree cur = current_nonlambda_class_type ();
   10353   2221697032 :            cur != NULL_TREE;
   10354   1012478352 :            cur = get_containing_scope (cur))
   10355              :         {
   10356   1662167073 :           if (!CLASS_TYPE_P (cur))
   10357    698913306 :             continue;
   10358              : 
   10359    963253767 :           tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
   10360   2723500180 :           if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
   10361              :             break;
   10362              : 
   10363    883618763 :           if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
   10364   1472526621 :               && comp_template_args (arglist, TI_ARGS (ti)))
   10365              :             return cur;
   10366              :         }
   10367              : 
   10368              :       /* Calculate the BOUND_ARGS.  These will be the args that are
   10369              :          actually tsubst'd into the definition to create the
   10370              :          instantiation.  */
   10371    639164963 :       if (PRIMARY_TEMPLATE_P (gen_tmpl))
   10372    632112003 :         arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
   10373              : 
   10374    639164960 :       if (arglist == error_mark_node)
   10375              :         /* We were unable to bind the arguments.  */
   10376              :         return error_mark_node;
   10377              : 
   10378              :       /* In the scope of a template class, explicit references to the
   10379              :          template class refer to the type of the template, not any
   10380              :          instantiation of it.  For example, in:
   10381              : 
   10382              :            template <class T> class C { void f(C<T>); }
   10383              : 
   10384              :          the `C<T>' is just the same as `C'.  Outside of the
   10385              :          class, however, such a reference is an instantiation.
   10386              :          One can use adjust_type_for_entering_scope to make
   10387              :          this adjustment as needed.  */
   10388    639163270 :       if (!PRIMARY_TEMPLATE_P (gen_tmpl)
   10389    639163270 :           || currently_open_class (template_type))
   10390              :         {
   10391     10613707 :           tree tinfo = TYPE_TEMPLATE_INFO (template_type);
   10392              : 
   10393     21227414 :           if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
   10394              :             return template_type;
   10395              :         }
   10396              : 
   10397              :       /* If we already have this specialization, return it.  */
   10398    639124312 :       spec_entry elt;
   10399    639124312 :       elt.tmpl = gen_tmpl;
   10400    639124312 :       elt.args = arglist;
   10401    639124312 :       entry = type_specializations->find (&elt);
   10402              : 
   10403    639124312 :       if (entry)
   10404    526024675 :         return entry->spec;
   10405              : 
   10406              :       /* If the template's constraints are not satisfied,
   10407              :          then we cannot form a valid type.
   10408              : 
   10409              :          Note that the check is deferred until after the hash
   10410              :          lookup. This prevents redundant checks on previously
   10411              :          instantiated specializations. */
   10412    113099637 :       if (flag_concepts
   10413    113099637 :           && !constraints_satisfied_p (gen_tmpl, arglist))
   10414              :         {
   10415          728 :           if (complain & tf_error)
   10416              :             {
   10417           86 :               auto_diagnostic_group d;
   10418           86 :               error ("template constraint failure for %qD", gen_tmpl);
   10419           86 :               diagnose_constraints (input_location, gen_tmpl, arglist);
   10420           86 :             }
   10421          728 :           return error_mark_node;
   10422              :         }
   10423              : 
   10424    113098909 :       is_dependent_type = uses_template_parms (arglist);
   10425              : 
   10426              :       /* If the deduced arguments are invalid, then the binding
   10427              :          failed.  */
   10428    113098909 :       if (!is_dependent_type
   10429    113098909 :           && check_instantiated_args (gen_tmpl,
   10430              :                                       INNERMOST_TEMPLATE_ARGS (arglist),
   10431              :                                       complain))
   10432            8 :         return error_mark_node;
   10433              : 
   10434    113098901 :       if (!is_dependent_type
   10435     54535828 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   10436      3597650 :           && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
   10437    115162575 :           && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
   10438              :         /* This occurs when the user has tried to define a tagged type
   10439              :            in a scope that forbids it.  We emitted an error during the
   10440              :            parse.  We didn't complete the bail out then, so here we
   10441              :            are.  */
   10442           12 :         return error_mark_node;
   10443              : 
   10444    113098889 :       context = DECL_CONTEXT (gen_tmpl);
   10445    113098889 :       if (context && TYPE_P (context))
   10446              :         {
   10447      5748437 :           if (!uses_template_parms (DECL_CONTEXT (templ)))
   10448              :             /* If the context of the partially instantiated template is
   10449              :                already non-dependent, then we might as well use it.  */
   10450      1805354 :             context = DECL_CONTEXT (templ);
   10451              :           else
   10452              :             {
   10453      3943083 :               context = tsubst_entering_scope (context, arglist,
   10454              :                                                complain, in_decl);
   10455              :               /* Try completing the enclosing context if it's not already so.  */
   10456      3943083 :               if (context != error_mark_node
   10457      3943083 :                   && !COMPLETE_TYPE_P (context))
   10458              :                 {
   10459      3627715 :                   context = complete_type (context);
   10460      3627715 :                   if (COMPLETE_TYPE_P (context))
   10461              :                     {
   10462              :                       /* Completion could have caused us to register the desired
   10463              :                          specialization already, so check the table again.  */
   10464            3 :                       entry = type_specializations->find (&elt);
   10465            3 :                       if (entry)
   10466            3 :                         return entry->spec;
   10467              :                     }
   10468              :                 }
   10469              :             }
   10470              :         }
   10471              :       else
   10472    107350452 :         context = tsubst (context, arglist, complain, in_decl);
   10473              : 
   10474    113098886 :       if (context == error_mark_node)
   10475              :         return error_mark_node;
   10476              : 
   10477    113098883 :       if (!context)
   10478            0 :         context = global_namespace;
   10479              : 
   10480              :       /* Create the type.  */
   10481    113098883 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE)
   10482              :         {
   10483       430040 :           if (!is_dependent_type)
   10484              :             {
   10485       430037 :               set_current_access_from_decl (TYPE_NAME (template_type));
   10486       860074 :               t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
   10487       430037 :                               tsubst (ENUM_UNDERLYING_TYPE (template_type),
   10488              :                                       arglist, complain, in_decl),
   10489       430037 :                               tsubst_attributes (TYPE_ATTRIBUTES (template_type),
   10490              :                                                  arglist, complain, in_decl),
   10491       430037 :                               SCOPED_ENUM_P (template_type), NULL);
   10492              : 
   10493       430037 :               if (t == error_mark_node)
   10494              :                 return t;
   10495              :             }
   10496              :           else
   10497              :             {
   10498              :               /* We don't want to call start_enum for this type, since
   10499              :                  the values for the enumeration constants may involve
   10500              :                  template parameters.  And, no one should be interested
   10501              :                  in the enumeration constants for such a type.  */
   10502            3 :               t = cxx_make_type (ENUMERAL_TYPE);
   10503            6 :               SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
   10504              :             }
   10505       860038 :           SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
   10506       860068 :           ENUM_FIXED_UNDERLYING_TYPE_P (t)
   10507       430034 :             = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
   10508              :         }
   10509    112668843 :       else if (CLASS_TYPE_P (template_type))
   10510              :         {
   10511              :           /* Lambda closures are regenerated in tsubst_lambda_expr, not
   10512              :              instantiated here.  */
   10513    225170328 :           gcc_assert (!LAMBDA_TYPE_P (template_type));
   10514              : 
   10515    112668843 :           t = make_class_type (TREE_CODE (template_type));
   10516    338006529 :           CLASSTYPE_DECLARED_CLASS (t)
   10517    112668843 :             = CLASSTYPE_DECLARED_CLASS (template_type);
   10518    112668843 :           SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
   10519              : 
   10520              :           /* A local class.  Make sure the decl gets registered properly.  */
   10521    112668843 :           if (context == current_function_decl)
   10522       656204 :             if (pushtag (DECL_NAME (gen_tmpl), t)
   10523       656204 :                 == error_mark_node)
   10524              :               return error_mark_node;
   10525              : 
   10526    112668831 :           if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
   10527              :             /* This instantiation is another name for the primary
   10528              :                template type. Set the TYPE_CANONICAL field
   10529              :                appropriately. */
   10530      5898918 :             TYPE_CANONICAL (t) = template_type;
   10531    106769913 :           else if (any_template_arguments_need_structural_equality_p (arglist))
   10532      1676868 :             SET_TYPE_STRUCTURAL_EQUALITY (t);
   10533              :         }
   10534              :       else
   10535            0 :         gcc_unreachable ();
   10536              : 
   10537              :       /* If we called start_enum or pushtag above, this information
   10538              :          will already be set up.  */
   10539    113098865 :       type_decl = TYPE_NAME (t);
   10540    113098865 :       if (!type_decl)
   10541              :         {
   10542    112012642 :           TYPE_CONTEXT (t) = FROB_CONTEXT (context);
   10543              : 
   10544    112012642 :           type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
   10545    112012642 :           DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
   10546    224025284 :           DECL_SOURCE_LOCATION (type_decl)
   10547    112012642 :             = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
   10548              :         }
   10549              : 
   10550    113098865 :       set_instantiating_module (type_decl);
   10551              :       /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
   10552              :          of export flag.  We want to propagate this because it might
   10553              :          be a friend declaration that pushes a new hidden binding.  */
   10554    113098865 :       DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
   10555              : 
   10556    113098865 :       if (CLASS_TYPE_P (template_type))
   10557              :         {
   10558    112668831 :           TREE_PRIVATE (type_decl)
   10559    112668831 :             = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
   10560    112668831 :           TREE_PROTECTED (type_decl)
   10561    112668831 :             = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
   10562    112668831 :           if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
   10563              :             {
   10564    111482993 :               DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
   10565    111482993 :               DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
   10566              :             }
   10567              :         }
   10568              : 
   10569    113098865 :       if (OVERLOAD_TYPE_P (t))
   10570              :         {
   10571              :           static const char *tags[] = {"abi_tag", "may_alias"};
   10572              : 
   10573    339296595 :           for (unsigned ix = 0; ix != 2; ix++)
   10574              :             {
   10575    226197730 :               tree attributes
   10576    226197730 :                 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
   10577              : 
   10578    226197730 :               if (attributes)
   10579           48 :                 TYPE_ATTRIBUTES (t)
   10580           96 :                   = tree_cons (TREE_PURPOSE (attributes),
   10581           48 :                                TREE_VALUE (attributes),
   10582           48 :                                TYPE_ATTRIBUTES (t));
   10583              :             }
   10584              :         }
   10585              : 
   10586              :       /* Let's consider the explicit specialization of a member
   10587              :          of a class template specialization that is implicitly instantiated,
   10588              :          e.g.:
   10589              :              template<class T>
   10590              :              struct S
   10591              :              {
   10592              :                template<class U> struct M {}; //#0
   10593              :              };
   10594              : 
   10595              :              template<>
   10596              :              template<>
   10597              :              struct S<int>::M<char> //#1
   10598              :              {
   10599              :                int i;
   10600              :              };
   10601              :         [temp.expl.spec]/4 says this is valid.
   10602              : 
   10603              :         In this case, when we write:
   10604              :         S<int>::M<char> m;
   10605              : 
   10606              :         M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
   10607              :         the one of #0.
   10608              : 
   10609              :         When we encounter #1, we want to store the partial instantiation
   10610              :         of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
   10611              : 
   10612              :         For all cases other than this "explicit specialization of member of a
   10613              :         class template", we just want to store the most general template into
   10614              :         the CLASSTYPE_TI_TEMPLATE of M.
   10615              : 
   10616              :         This case of "explicit specialization of member of a class template"
   10617              :         only happens when:
   10618              :         1/ the enclosing class is an instantiation of, and therefore not
   10619              :         the same as, the context of the most general template, and
   10620              :         2/ we aren't looking at the partial instantiation itself, i.e.
   10621              :         the innermost arguments are not the same as the innermost parms of
   10622              :         the most general template.
   10623              : 
   10624              :         So it's only when 1/ and 2/ happens that we want to use the partial
   10625              :         instantiation of the member template in lieu of its most general
   10626              :         template.  */
   10627              : 
   10628    113098865 :       if (PRIMARY_TEMPLATE_P (gen_tmpl)
   10629    222070400 :           && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
   10630              :           /* the enclosing class must be an instantiation...  */
   10631      2690118 :           && CLASS_TYPE_P (context)
   10632    115788738 :           && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
   10633              :         {
   10634      1698825 :           TREE_VEC_LENGTH (arglist)--;
   10635      1698825 :           ++processing_template_decl;
   10636      1698825 :           tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl));
   10637      1698825 :           tree partial_inst_args =
   10638      1698825 :             tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
   10639              :                     arglist, complain, NULL_TREE);
   10640      1698825 :           --processing_template_decl;
   10641      1698825 :           TREE_VEC_LENGTH (arglist)++;
   10642      1698825 :           if (partial_inst_args == error_mark_node)
   10643              :             return error_mark_node;
   10644      3397644 :           use_partial_inst_tmpl =
   10645              :             /*...and we must not be looking at the partial instantiation
   10646              :              itself. */
   10647      1698822 :             !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
   10648              :                                  partial_inst_args);
   10649              :         }
   10650              : 
   10651      1698822 :       if (!use_partial_inst_tmpl)
   10652              :         /* This case is easy; there are no member templates involved.  */
   10653              :         found = gen_tmpl;
   10654              :       else
   10655              :         {
   10656              :           /* This is a full instantiation of a member template.  Find
   10657              :              the partial instantiation of which this is an instance.  */
   10658              : 
   10659              :           /* Temporarily reduce by one the number of levels in the ARGLIST
   10660              :              so as to avoid comparing the last set of arguments.  */
   10661       943235 :           TREE_VEC_LENGTH (arglist)--;
   10662              :           /* We don't use COMPLAIN in the following call because this isn't
   10663              :              the immediate context of deduction.  For instance, tf_partial
   10664              :              could be set here as we might be at the beginning of template
   10665              :              argument deduction when any explicitly specified template
   10666              :              arguments are substituted into the function type.  tf_partial
   10667              :              could lead into trouble because we wouldn't find the partial
   10668              :              instantiation that might have been created outside tf_partial
   10669              :              context, because the levels of template parameters wouldn't
   10670              :              match, because in a tf_partial context, tsubst doesn't reduce
   10671              :              TEMPLATE_PARM_LEVEL.  */
   10672       943235 :           found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
   10673       943235 :           TREE_VEC_LENGTH (arglist)++;
   10674       943235 :           found = (TREE_CODE (found) == TEMPLATE_DECL
   10675       943235 :                    ? found
   10676            0 :                    : CLASSTYPE_TI_TEMPLATE (found));
   10677              : 
   10678       943235 :           if (DECL_CLASS_TEMPLATE_P (found)
   10679      1886470 :               && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
   10680              :             {
   10681              :               /* If this partial instantiation is specialized, we want to
   10682              :                  use it for hash table lookup.  */
   10683            9 :               elt.tmpl = found;
   10684            9 :               elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
   10685            9 :               elt.hash = 0; /* Recalculate after changing tmpl/args.  */
   10686              :             }
   10687              :         }
   10688              : 
   10689              :       /* Build template info for the new specialization.  */
   10690    113098862 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
   10691              : 
   10692    113098862 :       elt.spec = t;
   10693    113098862 :       slot = type_specializations->find_slot (&elt, INSERT);
   10694    113098862 :       gcc_checking_assert (*slot == NULL);
   10695    113098862 :       entry = ggc_alloc<spec_entry> ();
   10696    113098862 :       *entry = elt;
   10697    113098862 :       *slot = entry;
   10698              : 
   10699              :       /* Note this use of the partial instantiation so we can check it
   10700              :          later in maybe_process_partial_specialization.  */
   10701    226197724 :       DECL_TEMPLATE_INSTANTIATIONS (found)
   10702    113098862 :         = tree_cons (arglist, t,
   10703    113098862 :                      DECL_TEMPLATE_INSTANTIATIONS (found));
   10704              : 
   10705    113098862 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE
   10706    113098862 :           && !uses_template_parms (current_nonlambda_scope ()))
   10707              :         /* Now that the type has been registered on the instantiations
   10708              :            list, we set up the enumerators.  Because the enumeration
   10709              :            constants may involve the enumeration type itself, we make
   10710              :            sure to register the type first, and then create the
   10711              :            constants.  That way, doing tsubst_expr for the enumeration
   10712              :            constants won't result in recursive calls here; we'll find
   10713              :            the instantiation and exit above.  */
   10714       430034 :         tsubst_enum (template_type, t, arglist);
   10715              : 
   10716    113098817 :       if (CLASS_TYPE_P (template_type) && is_dependent_type)
   10717              :         /* If the type makes use of template parameters, the
   10718              :            code that generates debugging information will crash.  */
   10719     58563061 :         DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
   10720              : 
   10721              :       /* Possibly limit visibility based on template args.  */
   10722    113098817 :       TREE_PUBLIC (type_decl) = 1;
   10723    113098817 :       determine_visibility (type_decl);
   10724              : 
   10725    113098817 :       inherit_targ_abi_tags (t);
   10726              : 
   10727    113098817 :       return t;
   10728              :     }
   10729   1248288679 : }
   10730              : 
   10731              : /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.  */
   10732              : 
   10733              : tree
   10734     40830852 : lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
   10735              : {
   10736     40830852 :   tree gen_templ = most_general_template (templ);
   10737     40830852 :   tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
   10738     40830852 :   arglist = add_outermost_template_args (templ, arglist);
   10739     40830852 :   arglist = coerce_template_parms (parms, arglist, templ, complain);
   10740     40830852 :   if (arglist == error_mark_node)
   10741              :     return error_mark_node;
   10742              : 
   10743              :   /* The type of the expression is NULL_TREE since the template-id could refer
   10744              :      to an explicit or partial specialization. */
   10745     40830800 :   return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
   10746              : }
   10747              : 
   10748              : /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
   10749              :    not dependent.  */
   10750              : 
   10751              : tree
   10752     39087723 : finish_template_variable (tree var, tsubst_flags_t complain)
   10753              : {
   10754     39087723 :   tree templ = TREE_OPERAND (var, 0);
   10755     39087723 :   tree arglist = TREE_OPERAND (var, 1);
   10756              : 
   10757              :   /* If the template or arguments are dependent, then we
   10758              :      can't resolve the TEMPLATE_ID_EXPR yet.  */
   10759     39087723 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
   10760     39087723 :       || any_dependent_template_arguments_p (arglist))
   10761     12027188 :     return var;
   10762              : 
   10763     27060535 :   if (flag_concepts && !constraints_satisfied_p (templ, arglist))
   10764              :     {
   10765           21 :       if (complain & tf_error)
   10766              :         {
   10767           18 :           auto_diagnostic_group d;
   10768           18 :           error ("use of invalid variable template %qE", var);
   10769           18 :           diagnose_constraints (location_of (var), templ, arglist);
   10770           18 :         }
   10771           21 :       return error_mark_node;
   10772              :     }
   10773              : 
   10774     27060514 :   return instantiate_template (templ, arglist, complain);
   10775              : }
   10776              : 
   10777              : /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
   10778              :    TARGS template args, and instantiate it if it's not dependent.  */
   10779              : 
   10780              : tree
   10781     27735634 : lookup_and_finish_template_variable (tree templ, tree targs,
   10782              :                                      tsubst_flags_t complain)
   10783              : {
   10784     27735634 :   tree var = lookup_template_variable (templ, targs, complain);
   10785     27735634 :   if (var == error_mark_node)
   10786              :     return error_mark_node;
   10787     27735634 :   var = finish_template_variable (var, complain);
   10788     27735634 :   mark_used (var, complain);
   10789     27735634 :   return var;
   10790              : }
   10791              : 
   10792              : /* If the set of template parameters PARMS contains a template parameter
   10793              :    at the given LEVEL and INDEX, then return this parameter.  Otherwise
   10794              :    return NULL_TREE.  */
   10795              : 
   10796              : static tree
   10797     73426559 : corresponding_template_parameter_list (tree parms, int level, int index)
   10798              : {
   10799     78590322 :   while (TMPL_PARMS_DEPTH (parms) > level)
   10800      5163763 :     parms = TREE_CHAIN (parms);
   10801              : 
   10802     73426559 :   if (TMPL_PARMS_DEPTH (parms) != level
   10803     73426559 :       || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
   10804              :     return NULL_TREE;
   10805              : 
   10806     71582567 :   return TREE_VEC_ELT (TREE_VALUE (parms), index);
   10807              : }
   10808              : 
   10809              : /* Return the TREE_LIST for the template parameter from PARMS that positionally
   10810              :    corresponds to the template parameter PARM, or else return NULL_TREE.  */
   10811              : 
   10812              : static tree
   10813     73426559 : corresponding_template_parameter_list (tree parms, tree parm)
   10814              : {
   10815     73426559 :   int level, index;
   10816     73426559 :   template_parm_level_and_index (parm, &level, &index);
   10817     73426559 :   return corresponding_template_parameter_list (parms, level, index);
   10818              : }
   10819              : 
   10820              : /* As above, but pull out the actual parameter.  */
   10821              : 
   10822              : static tree
   10823     73425566 : corresponding_template_parameter (tree parms, tree parm)
   10824              : {
   10825     73425566 :   tree list = corresponding_template_parameter_list (parms, parm);
   10826     73425566 :   if (!list)
   10827              :     return NULL_TREE;
   10828              : 
   10829     71581574 :   tree t = TREE_VALUE (list);
   10830              :   /* As in template_parm_to_arg.  */
   10831     71581574 :   if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
   10832     69861510 :     t = TREE_TYPE (t);
   10833              :   else
   10834      1720064 :     t = DECL_INITIAL (t);
   10835              : 
   10836     71581574 :   gcc_assert (TEMPLATE_PARM_P (t));
   10837              :   return t;
   10838              : }
   10839              : 
   10840              : struct pair_fn_data
   10841              : {
   10842              :   tree_fn_t fn;
   10843              :   tree_fn_t any_fn;
   10844              :   void *data;
   10845              :   /* True when we should also visit template parameters that occur in
   10846              :      non-deduced contexts.  */
   10847              :   bool include_nondeduced_p;
   10848              :   hash_set<tree> *visited;
   10849              : };
   10850              : 
   10851              : /* Called from for_each_template_parm via walk_tree.  */
   10852              : 
   10853              : static tree
   10854    953732761 : for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
   10855              : {
   10856    953732761 :   tree t = *tp;
   10857    953732761 :   struct pair_fn_data *pfd = (struct pair_fn_data *) d;
   10858    953732761 :   tree_fn_t fn = pfd->fn;
   10859    953732761 :   void *data = pfd->data;
   10860    953732761 :   tree result = NULL_TREE;
   10861              : 
   10862              : #define WALK_SUBTREE(NODE)                                              \
   10863              :   do                                                                    \
   10864              :     {                                                                   \
   10865              :       result = for_each_template_parm (NODE, fn, data, pfd->visited, \
   10866              :                                        pfd->include_nondeduced_p,    \
   10867              :                                        pfd->any_fn);                 \
   10868              :       if (result) goto out;                                             \
   10869              :     }                                                                   \
   10870              :   while (0)
   10871              : 
   10872    953732761 :   if (pfd->any_fn && (*pfd->any_fn)(t, data))
   10873              :     return t;
   10874              : 
   10875    953732761 :   if (TYPE_P (t)
   10876    405055792 :       && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
   10877    404680422 :     WALK_SUBTREE (TYPE_CONTEXT (t));
   10878              : 
   10879    953523281 :   switch (TREE_CODE (t))
   10880              :     {
   10881     68022209 :     case RECORD_TYPE:
   10882     68022209 :       if (TYPE_PTRMEMFUNC_P (t))
   10883              :         break;
   10884              :       /* Fall through.  */
   10885              : 
   10886     68552224 :     case UNION_TYPE:
   10887     68552224 :     case ENUMERAL_TYPE:
   10888     68552224 :       if (!TYPE_TEMPLATE_INFO (t))
   10889      7432353 :         *walk_subtrees = 0;
   10890              :       else
   10891     61119871 :         WALK_SUBTREE (TYPE_TI_ARGS (t));
   10892              :       break;
   10893              : 
   10894     10154870 :     case METHOD_TYPE:
   10895              :       /* Since we're not going to walk subtrees, we have to do this
   10896              :          explicitly here.  */
   10897     10154870 :       WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
   10898              :       /* Fall through.  */
   10899              : 
   10900      2207154 :     case FUNCTION_TYPE:
   10901              :       /* Check the return type.  */
   10902      2207154 :       WALK_SUBTREE (TREE_TYPE (t));
   10903              : 
   10904              :       /* Check the parameter types.  Since default arguments are not
   10905              :          instantiated until they are needed, the TYPE_ARG_TYPES may
   10906              :          contain expressions that involve template parameters.  But,
   10907              :          no-one should be looking at them yet.  And, once they're
   10908              :          instantiated, they don't contain template parameters, so
   10909              :          there's no point in looking at them then, either.  */
   10910      1995868 :       {
   10911      1995868 :         tree parm;
   10912              : 
   10913      6635774 :         for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
   10914      4678798 :           WALK_SUBTREE (TREE_VALUE (parm));
   10915              : 
   10916              :         /* Since we've already handled the TYPE_ARG_TYPES, we don't
   10917              :            want walk_tree walking into them itself.  */
   10918      1956976 :         *walk_subtrees = 0;
   10919              :       }
   10920              : 
   10921      1956976 :       if (flag_noexcept_type)
   10922              :         {
   10923      1950921 :           tree spec = TYPE_RAISES_EXCEPTIONS (t);
   10924      1950921 :           if (spec)
   10925       516855 :             WALK_SUBTREE (TREE_PURPOSE (spec));
   10926              :         }
   10927              :       break;
   10928              : 
   10929     11044854 :     case TYPEOF_TYPE:
   10930     11044854 :     case DECLTYPE_TYPE:
   10931     11044854 :       if (pfd->include_nondeduced_p
   10932     11044854 :           && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
   10933              :                                      pfd->visited,
   10934              :                                      pfd->include_nondeduced_p,
   10935              :                                      pfd->any_fn))
   10936         1724 :         return error_mark_node;
   10937     11043130 :       *walk_subtrees = false;
   10938     11043130 :       break;
   10939              : 
   10940            6 :     case TRAIT_TYPE:
   10941            6 :       if (pfd->include_nondeduced_p)
   10942              :         {
   10943            0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
   10944            0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
   10945              :         }
   10946            6 :       *walk_subtrees = false;
   10947            6 :       break;
   10948              : 
   10949      1469940 :     case FUNCTION_DECL:
   10950      1469940 :     case VAR_DECL:
   10951      1469940 :       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   10952       203569 :         WALK_SUBTREE (DECL_TI_ARGS (t));
   10953              :       break;
   10954              : 
   10955      6539202 :     case PARM_DECL:
   10956      6539202 :       WALK_SUBTREE (TREE_TYPE (t));
   10957              :       break;
   10958              : 
   10959       456401 :     case CONST_DECL:
   10960       456401 :       if (DECL_TEMPLATE_PARM_P (t))
   10961            0 :         WALK_SUBTREE (DECL_INITIAL (t));
   10962       456401 :       if (DECL_CONTEXT (t)
   10963       456401 :           && pfd->include_nondeduced_p)
   10964        58347 :         WALK_SUBTREE (DECL_CONTEXT (t));
   10965              :       break;
   10966              : 
   10967       273096 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   10968              :       /* Record template parameters such as `T' inside `TT<T>'.  */
   10969       273096 :       WALK_SUBTREE (TYPE_TI_ARGS (t));
   10970              :       /* Fall through.  */
   10971              : 
   10972    147768430 :     case TEMPLATE_TEMPLATE_PARM:
   10973    147768430 :     case TEMPLATE_TYPE_PARM:
   10974    147768430 :     case TEMPLATE_PARM_INDEX:
   10975    147768430 :       if (fn && (*fn)(t, data))
   10976              :         return t;
   10977    137182257 :       else if (!fn)
   10978              :         return t;
   10979              :       break;
   10980              : 
   10981     34215932 :     case TEMPLATE_DECL:
   10982              :       /* A template template parameter is encountered.  */
   10983     34215932 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   10984           20 :         WALK_SUBTREE (TREE_TYPE (t));
   10985              : 
   10986              :       /* Already substituted template template parameter */
   10987     34215932 :       *walk_subtrees = 0;
   10988     34215932 :       break;
   10989              : 
   10990     40658737 :     case TYPENAME_TYPE:
   10991              :       /* A template-id in a TYPENAME_TYPE might be a deduced context after
   10992              :          partial instantiation.  */
   10993     40658737 :       WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
   10994     40658737 :       *walk_subtrees = 0;
   10995     40658737 :       break;
   10996              : 
   10997     18282705 :     case INDIRECT_REF:
   10998     18282705 :     case COMPONENT_REF:
   10999              :       /* If there's no type, then this thing must be some expression
   11000              :          involving template parameters.  */
   11001     18282705 :       if (!fn && !TREE_TYPE (t))
   11002            0 :         return error_mark_node;
   11003              :       break;
   11004              : 
   11005      1583112 :     case CONSTRUCTOR:
   11006      1583112 :     case TRAIT_EXPR:
   11007      1583112 :     case PLUS_EXPR:
   11008      1583112 :     case MULT_EXPR:
   11009      1583112 :     case SCOPE_REF:
   11010              :       /* These are non-deduced contexts.  */
   11011      1583112 :       if (!pfd->include_nondeduced_p)
   11012        84698 :         *walk_subtrees = 0;
   11013              :       break;
   11014              : 
   11015      8616728 :     case MODOP_EXPR:
   11016      8616728 :     case CAST_EXPR:
   11017      8616728 :     case IMPLICIT_CONV_EXPR:
   11018      8616728 :     case REINTERPRET_CAST_EXPR:
   11019      8616728 :     case CONST_CAST_EXPR:
   11020      8616728 :     case STATIC_CAST_EXPR:
   11021      8616728 :     case DYNAMIC_CAST_EXPR:
   11022      8616728 :     case ARROW_EXPR:
   11023      8616728 :     case DOTSTAR_EXPR:
   11024      8616728 :     case TYPEID_EXPR:
   11025      8616728 :     case PSEUDO_DTOR_EXPR:
   11026      8616728 :       if (!fn)
   11027            0 :         return error_mark_node;
   11028              :       break;
   11029              : 
   11030            7 :     case SPLICE_SCOPE:
   11031            7 :       WALK_SUBTREE (SPLICE_SCOPE_EXPR (t));
   11032            7 :       *walk_subtrees = 0;
   11033            7 :       break;
   11034              : 
   11035              :     default:
   11036              :       break;
   11037              :     }
   11038              : 
   11039              :   #undef WALK_SUBTREE
   11040              : 
   11041              :   /* We didn't find any template parameters we liked.  */
   11042              :  out:
   11043              :   return result;
   11044              : }
   11045              : 
   11046              : /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
   11047              :    BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
   11048              :    call FN with the parameter and the DATA.
   11049              :    If FN returns nonzero, the iteration is terminated, and
   11050              :    for_each_template_parm returns 1.  Otherwise, the iteration
   11051              :    continues.  If FN never returns a nonzero value, the value
   11052              :    returned by for_each_template_parm is 0.  If FN is NULL, it is
   11053              :    considered to be the function which always returns 1.
   11054              : 
   11055              :    If INCLUDE_NONDEDUCED_P, then this routine will also visit template
   11056              :    parameters that occur in non-deduced contexts.  When false, only
   11057              :    visits those template parameters that can be deduced.  */
   11058              : 
   11059              : static tree
   11060    787930207 : for_each_template_parm (tree t, tree_fn_t fn, void* data,
   11061              :                         hash_set<tree> *visited,
   11062              :                         bool include_nondeduced_p,
   11063              :                         tree_fn_t any_fn)
   11064              : {
   11065    787930207 :   struct pair_fn_data pfd;
   11066    787930207 :   tree result;
   11067              : 
   11068              :   /* Set up.  */
   11069    787930207 :   pfd.fn = fn;
   11070    787930207 :   pfd.any_fn = any_fn;
   11071    787930207 :   pfd.data = data;
   11072    787930207 :   pfd.include_nondeduced_p = include_nondeduced_p;
   11073              : 
   11074              :   /* Walk the tree.  (Conceptually, we would like to walk without
   11075              :      duplicates, but for_each_template_parm_r recursively calls
   11076              :      for_each_template_parm, so we would need to reorganize a fair
   11077              :      bit to use walk_tree_without_duplicates, so we keep our own
   11078              :      visited list.)  */
   11079    787930207 :   if (visited)
   11080    754078650 :     pfd.visited = visited;
   11081              :   else
   11082     33851557 :     pfd.visited = new hash_set<tree>;
   11083    787930207 :   result = cp_walk_tree (&t,
   11084              :                          for_each_template_parm_r,
   11085              :                          &pfd,
   11086              :                          pfd.visited);
   11087              : 
   11088              :   /* Clean up.  */
   11089    787930207 :   if (!visited)
   11090              :     {
   11091     67703114 :       delete pfd.visited;
   11092     33851557 :       pfd.visited = 0;
   11093              :     }
   11094              : 
   11095    787930207 :   return result;
   11096              : }
   11097              : 
   11098     43581230 : struct find_template_parameter_info
   11099              : {
   11100     43581230 :   explicit find_template_parameter_info (tree ctx_parms)
   11101     87162460 :     : ctx_parms (ctx_parms),
   11102     43581230 :       max_depth (TMPL_PARMS_DEPTH (ctx_parms))
   11103     43581230 :   {}
   11104              : 
   11105              :   hash_set<tree> visited;
   11106              :   hash_set<tree> parms;
   11107              :   tree parm_list = NULL_TREE;
   11108              :   tree *parm_list_tail = &parm_list;
   11109              :   tree ctx_parms;
   11110              :   int max_depth;
   11111              : 
   11112              :   tree find_in (tree);
   11113              :   tree find_in_recursive (tree);
   11114              :   bool found (tree);
   11115          526 :   unsigned num_found () { return parms.elements (); }
   11116              : };
   11117              : 
   11118              : /* Appends the declaration of T to the list in DATA.  */
   11119              : 
   11120              : static int
   11121     80846624 : keep_template_parm (tree t, void* data)
   11122              : {
   11123     80846624 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   11124              : 
   11125              :   /* Template parameters declared within the expression are not part of
   11126              :      the parameter mapping. For example, in this concept:
   11127              : 
   11128              :        template<typename T>
   11129              :        concept C = requires { <expr> } -> same_as<int>;
   11130              : 
   11131              :      the return specifier same_as<int> declares a new decltype parameter
   11132              :      that must not be part of the parameter mapping. The same is true
   11133              :      for generic lambda parameters, lambda template parameters, etc.  */
   11134     80846624 :   int level;
   11135     80846624 :   int index;
   11136     80846624 :   template_parm_level_and_index (t, &level, &index);
   11137     80846624 :   if (level == 0 || level > ftpi->max_depth)
   11138              :     return 0;
   11139              : 
   11140     73425566 :   if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
   11141              :     /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
   11142              :        BOUND_TEMPLATE_TEMPLATE_PARM itself.  */
   11143       213835 :     t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
   11144              : 
   11145              :   /* This template parameter might be an argument to a cached dependent
   11146              :      specalization that was formed earlier inside some other template, in
   11147              :      which case the parameter is not among the ones that are in-scope.
   11148              :      Look in CTX_PARMS to find the corresponding in-scope template
   11149              :      parameter, and use it instead.  */
   11150     73425566 :   if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
   11151     71581574 :     t = in_scope;
   11152              : 
   11153              :   /* Arguments like const T yield parameters like const T. This means that
   11154              :      a template-id like X<T, const T> would yield two distinct parameters:
   11155              :      T and const T. Adjust types to their unqualified versions.  */
   11156     73425566 :   if (TYPE_P (t))
   11157     71705502 :     t = TYPE_MAIN_VARIANT (t);
   11158     73425566 :   if (!ftpi->parms.add (t))
   11159              :     {
   11160              :       /* Append T to PARM_LIST.  */
   11161     65142484 :       tree node = build_tree_list (NULL_TREE, t);
   11162     65142484 :       *ftpi->parm_list_tail = node;
   11163     65142484 :       ftpi->parm_list_tail = &TREE_CHAIN (node);
   11164              :     }
   11165              : 
   11166              :   /* Verify the parameter we found has a valid index.  */
   11167     73425566 :   if (flag_checking)
   11168              :     {
   11169     73425566 :       tree parms = ftpi->ctx_parms;
   11170     78589329 :       while (TMPL_PARMS_DEPTH (parms) > level)
   11171      5163763 :         parms = TREE_CHAIN (parms);
   11172     73425566 :       if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
   11173     71581574 :         gcc_assert (index < len);
   11174              :     }
   11175              : 
   11176              :   return 0;
   11177              : }
   11178              : 
   11179              : /* Ensure that we recursively examine certain terms that are not normally
   11180              :    visited in for_each_template_parm_r.  */
   11181              : 
   11182              : static int
   11183    617856270 : any_template_parm_r (tree t, void *data)
   11184              : {
   11185    617856270 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   11186              : 
   11187              : #define WALK_SUBTREE(NODE)                                              \
   11188              :   do                                                                    \
   11189              :     {                                                                   \
   11190              :       for_each_template_parm (NODE, keep_template_parm, data,           \
   11191              :                               &ftpi->visited, true,                      \
   11192              :                               any_template_parm_r);                     \
   11193              :     }                                                                   \
   11194              :   while (0)
   11195              : 
   11196              :   /* A mention of a member alias/typedef is a use of all of its template
   11197              :      arguments, including those from the enclosing class, so we don't use
   11198              :      alias_template_specialization_p here.  */
   11199    617856270 :   if (TYPE_P (t) && typedef_variant_p (t))
   11200      9560003 :     if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
   11201      9559927 :       WALK_SUBTREE (TI_ARGS (tinfo));
   11202              : 
   11203    617856270 :   switch (TREE_CODE (t))
   11204              :     {
   11205     78826710 :     case TEMPLATE_TYPE_PARM:
   11206              :       /* Type constraints of a placeholder type may contain parameters.  */
   11207     78826710 :       if (is_auto (t))
   11208      9265022 :         if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
   11209      9242671 :           WALK_SUBTREE (constr);
   11210              :       break;
   11211              : 
   11212     37785760 :     case TEMPLATE_ID_EXPR:
   11213              :       /* Search through references to variable templates.  */
   11214     37785760 :       WALK_SUBTREE (TREE_OPERAND (t, 0));
   11215     37785760 :       WALK_SUBTREE (TREE_OPERAND (t, 1));
   11216     37785760 :       break;
   11217              : 
   11218              :     case TEMPLATE_PARM_INDEX:
   11219              :       /* No need to consider template parameters within the type of an NTTP:
   11220              :          substitution into an NTTP is done directly with the corresponding
   11221              :          template argument, and its type only comes into play earlier during
   11222              :          coercion.  */
   11223              :       break;
   11224              : 
   11225     33910237 :     case TEMPLATE_DECL:
   11226              :       /* If T is a member template that shares template parameters with
   11227              :          ctx_parms, we need to mark all those parameters for mapping.
   11228              :          To that end, it should suffice to just walk the DECL_CONTEXT of
   11229              :          the template (assuming the template is not overly general).  */
   11230     33910237 :       WALK_SUBTREE (DECL_CONTEXT (t));
   11231     33910237 :       break;
   11232              : 
   11233          130 :     case LAMBDA_EXPR:
   11234          130 :       {
   11235              :         /* TREE_STATIC on LAMBDA_EXPR_EXTRA_ARGS means a full set of
   11236              :            arguments, so we can just look there; they will replace
   11237              :            any template parms in the rest of the LAMBDA_EXPR.  */
   11238          130 :         if (tree args = LAMBDA_EXPR_EXTRA_ARGS (t))
   11239              :           {
   11240           55 :             WALK_SUBTREE (args);
   11241              :             /* Without TREE_STATIC the args are just outer levels, so we'd
   11242              :                still need to look through the lambda for just inner
   11243              :                parameters.  Hopefully that's not necessary.  */
   11244           55 :             gcc_checking_assert (TREE_STATIC (args));
   11245              :             return 0;
   11246              :           }
   11247              :         /* Look in the parms and body.  */
   11248           75 :         tree fn = lambda_function (t);
   11249           75 :         WALK_SUBTREE (TREE_TYPE (fn));
   11250           75 :         WALK_SUBTREE (DECL_SAVED_TREE (fn));
   11251              :       }
   11252           75 :       break;
   11253              : 
   11254     17522857 :     case IDENTIFIER_NODE:
   11255     17522857 :       if (IDENTIFIER_CONV_OP_P (t))
   11256              :         /* The conversion-type-id of a conversion operator may be dependent.  */
   11257         3886 :         WALK_SUBTREE (TREE_TYPE (t));
   11258              :       break;
   11259              : 
   11260           18 :     case CONVERT_EXPR:
   11261           18 :       if (is_dummy_object (t))
   11262           15 :         WALK_SUBTREE (TREE_TYPE (t));
   11263              :       break;
   11264              : 
   11265              :     default:
   11266              :       break;
   11267              :     }
   11268              : 
   11269              :   /* Keep walking.  */
   11270              :   return 0;
   11271              : }
   11272              : 
   11273              : /* Look through T for template parameters.  */
   11274              : 
   11275              : tree
   11276     43582223 : find_template_parameter_info::find_in (tree t)
   11277              : {
   11278     43582223 :   return for_each_template_parm (t, keep_template_parm, this, &visited,
   11279              :                                  /*include_nondeduced*/true,
   11280     43582223 :                                  any_template_parm_r);
   11281              : }
   11282              : 
   11283              : /* As above, but also recursively look into the default arguments of template
   11284              :    parameters we found.  Used for alias CTAD.  */
   11285              : 
   11286              : tree
   11287          526 : find_template_parameter_info::find_in_recursive (tree t)
   11288              : {
   11289          526 :   if (tree r = find_in (t))
   11290              :     return r;
   11291              :   /* Since newly found parms are added to the end of the list, we
   11292              :      can just walk it until we reach the end.  */
   11293         1519 :   for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
   11294              :     {
   11295          993 :       tree parm = TREE_VALUE (pl);
   11296          993 :       tree list = corresponding_template_parameter_list (ctx_parms, parm);
   11297          993 :       if (tree r = find_in (TREE_PURPOSE (list)))
   11298              :         return r;
   11299              :     }
   11300              :   return NULL_TREE;
   11301              : }
   11302              : 
   11303              : /* True if PARM was found by a previous call to find_in.  PARM can be a
   11304              :    TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P.  */
   11305              : 
   11306              : bool
   11307         1066 : find_template_parameter_info::found (tree parm)
   11308              : {
   11309         1066 :   if (TREE_CODE (parm) == TREE_LIST)
   11310         1066 :     parm = TREE_VALUE (parm);
   11311         1066 :   if (TREE_CODE (parm) == TYPE_DECL
   11312         1066 :       || TREE_CODE (parm) == TEMPLATE_DECL)
   11313         1023 :     parm = TREE_TYPE (parm);
   11314              :   else
   11315           43 :     parm = DECL_INITIAL (parm);
   11316         1066 :   gcc_checking_assert (TEMPLATE_PARM_P (parm));
   11317         1066 :   return parms.contains (parm);
   11318              : }
   11319              : 
   11320              : /* Returns a list of unique template parameters found within T, where CTX_PARMS
   11321              :    are the template parameters in scope.  */
   11322              : 
   11323              : tree
   11324     43580794 : find_template_parameters (tree t, tree ctx_parms)
   11325              : {
   11326     43580794 :   if (!ctx_parms)
   11327              :     return NULL_TREE;
   11328              : 
   11329     43580704 :   find_template_parameter_info ftpi (ctx_parms);
   11330     43580704 :   ftpi.find_in (t);
   11331     43580704 :   return ftpi.parm_list;
   11332     43580704 : }
   11333              : 
   11334              : /* Returns true if T depends on any template parameter.  */
   11335              : 
   11336              : bool
   11337   4825945779 : uses_template_parms (tree t)
   11338              : {
   11339   4825945779 :   if (t == NULL_TREE || t == error_mark_node)
   11340              :     return false;
   11341              : 
   11342              :   /* Namespaces can't depend on any template parameters.  */
   11343   4482688855 :   if (TREE_CODE (t) == NAMESPACE_DECL)
   11344              :     return false;
   11345              : 
   11346   4482688365 :   processing_template_decl_sentinel ptds (/*reset*/false);
   11347   4482688365 :   ++processing_template_decl;
   11348              : 
   11349   4482688365 :   if (TYPE_P (t))
   11350   2507879778 :     return dependent_type_p (t);
   11351   1974808587 :   else if (TREE_CODE (t) == TREE_VEC)
   11352   1634701080 :     return any_dependent_template_arguments_p (t);
   11353    340107507 :   else if (TREE_CODE (t) == TREE_LIST)
   11354    149650545 :     return (uses_template_parms (TREE_VALUE (t))
   11355    149650545 :             || uses_template_parms (TREE_CHAIN (t)));
   11356    190456962 :   else if (TREE_CODE (t) == TYPE_DECL)
   11357           93 :     return dependent_type_p (TREE_TYPE (t));
   11358              :   else
   11359    190456869 :     return instantiation_dependent_expression_p (t);
   11360   4482688365 : }
   11361              : 
   11362              : /* Returns true if T depends on any template parameter with level LEVEL.  */
   11363              : 
   11364              : bool
   11365      2252643 : uses_template_parms_level (tree t, int level)
   11366              : {
   11367      2252643 :   return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
   11368      2252643 :                                  /*include_nondeduced_p=*/true);
   11369              : }
   11370              : 
   11371              : /* Returns true if the signature of DECL depends on any template parameter from
   11372              :    its enclosing class.  */
   11373              : 
   11374              : static bool
   11375    295520994 : uses_outer_template_parms (tree decl)
   11376              : {
   11377    295520994 :   int depth;
   11378    295520994 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11379       380447 :     depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
   11380              :   else
   11381    295140547 :     depth = template_class_depth (CP_DECL_CONTEXT (decl));
   11382    295520994 :   if (depth == 0)
   11383              :     return false;
   11384     10956902 :   if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
   11385              :                               &depth, NULL, /*include_nondeduced_p=*/true))
   11386              :     return true;
   11387       979665 :   if (PRIMARY_TEMPLATE_P (decl)
   11388       979665 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11389              :     {
   11390       979665 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
   11391      2394187 :       for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
   11392              :         {
   11393      1414567 :           tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
   11394      1414567 :           tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
   11395      1414567 :           if (TREE_CODE (parm) == PARM_DECL
   11396      1414567 :               && for_each_template_parm (TREE_TYPE (parm),
   11397              :                                          template_parm_outer_level,
   11398              :                                          &depth, NULL, /*nondeduced*/true))
   11399              :             return true;
   11400      1414528 :           if (TREE_CODE (parm) == TEMPLATE_DECL
   11401      1414528 :               && uses_outer_template_parms (parm))
   11402              :             return true;
   11403      1414528 :           if (defarg
   11404      1414528 :               && for_each_template_parm (defarg, template_parm_outer_level,
   11405              :                                          &depth, NULL, /*nondeduced*/true))
   11406              :             return true;
   11407              :         }
   11408              :     }
   11409       979620 :   if (uses_outer_template_parms_in_constraints (decl))
   11410              :     return true;
   11411              :   return false;
   11412              : }
   11413              : 
   11414              : /* Returns true if the constraints of DECL depend on any template parameters
   11415              :    from its enclosing scope.  */
   11416              : 
   11417              : bool
   11418      4637427 : uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/)
   11419              : {
   11420      4637427 :   tree ci = get_constraints (decl);
   11421      4637427 :   if (ci)
   11422        42308 :     ci = CI_ASSOCIATED_CONSTRAINTS (ci);
   11423        42308 :   if (!ci)
   11424              :     return false;
   11425        42308 :   if (!ctx)
   11426              :     {
   11427        12755 :       if (tree fc = DECL_FRIEND_CONTEXT (decl))
   11428              :         ctx = fc;
   11429              :       else
   11430          108 :         ctx = CP_DECL_CONTEXT (decl);
   11431              :     }
   11432        42308 :   int depth = template_class_depth (ctx);
   11433        42308 :   if (depth == 0)
   11434              :     return false;
   11435        42284 :   return for_each_template_parm (ci, template_parm_outer_level,
   11436        42284 :                                  &depth, NULL, /*nondeduced*/true);
   11437              : }
   11438              : 
   11439              : /* Returns TRUE iff INST is an instantiation we don't need to do in an
   11440              :    ill-formed translation unit, i.e. a variable or function that isn't
   11441              :    usable in a constant expression.  */
   11442              : 
   11443              : static inline bool
   11444      7848239 : neglectable_inst_p (tree d)
   11445              : {
   11446      7565392 :   return (d && DECL_P (d)
   11447      6587626 :           && !undeduced_auto_decl (d)
   11448     14223123 :           && !(TREE_CODE (d) == FUNCTION_DECL
   11449      6374884 :                ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
   11450       927503 :                : decl_maybe_constant_var_p (d)));
   11451              : }
   11452              : 
   11453              : /* Returns TRUE iff we should refuse to instantiate DECL because it's
   11454              :    neglectable and instantiated from within an erroneous instantiation.  */
   11455              : 
   11456              : static bool
   11457    154565404 : limit_bad_template_recursion (tree decl)
   11458              : {
   11459    154565404 :   struct tinst_level *lev = current_tinst_level;
   11460    154565404 :   int errs = errorcount + sorrycount;
   11461    154565404 :   if (errs == 0 || !neglectable_inst_p (decl))
   11462    151651429 :     return false;
   11463              : 
   11464              :   /* Avoid instantiating members of an ill-formed class.  */
   11465      2913975 :   bool refuse
   11466      5827923 :     = (DECL_CLASS_SCOPE_P (decl)
   11467      5012509 :        && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
   11468              : 
   11469              :   if (!refuse)
   11470              :     {
   11471      3460618 :       for (; lev; lev = lev->next)
   11472      5825355 :         if (neglectable_inst_p (lev->maybe_get_node ()))
   11473              :           break;
   11474      2912838 :       refuse = (lev && errs > lev->errors);
   11475              :     }
   11476              : 
   11477              :   if (refuse)
   11478              :     {
   11479              :       /* Don't warn about it not being defined.  */
   11480         3134 :       suppress_warning (decl, OPT_Wunused);
   11481         3134 :       tree clone;
   11482         5406 :       FOR_EACH_CLONE (clone, decl)
   11483         2272 :         suppress_warning (clone, OPT_Wunused);
   11484              :     }
   11485              :   return refuse;
   11486              : }
   11487              : 
   11488              : static int tinst_depth;
   11489              : extern int max_tinst_depth;
   11490              : int depth_reached;
   11491              : int tinst_dump_id;
   11492              : 
   11493              : static GTY(()) struct tinst_level *last_error_tinst_level;
   11494              : 
   11495              : /* We're starting to instantiate D; record the template instantiation context
   11496              :    at LOC for diagnostics and to restore it later.  */
   11497              : 
   11498              : bool
   11499   1225866175 : push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
   11500              : {
   11501   1225866175 :   struct tinst_level *new_level;
   11502              : 
   11503   1225866175 :   if (tinst_depth >= max_tinst_depth)
   11504              :     {
   11505              :       /* Tell error.cc not to try to instantiate any templates.  */
   11506           48 :       at_eof = 3;
   11507           48 :       fatal_error (input_location,
   11508              :                    "template instantiation depth exceeds maximum of %d"
   11509              :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   11510              :                    max_tinst_depth);
   11511              :       return false;
   11512              :     }
   11513              : 
   11514              :   /* If the current instantiation caused problems, don't let it instantiate
   11515              :      anything else.  Do allow deduction substitution and decls usable in
   11516              :      constant expressions.  */
   11517   1225866127 :   if (!targs && limit_bad_template_recursion (tldcl))
   11518              :     {
   11519              :       /* Avoid no_linkage_errors and unused function (and all other)
   11520              :          warnings for this decl.  */
   11521         2046 :       suppress_warning (tldcl);
   11522         2046 :       return false;
   11523              :     }
   11524              : 
   11525              :   /* When not -quiet, dump template instantiations other than functions, since
   11526              :      announce_function will take care of those.  */
   11527   1225864081 :   if (!quiet_flag && !targs
   11528            0 :       && TREE_CODE (tldcl) != TREE_LIST
   11529            0 :       && TREE_CODE (tldcl) != FUNCTION_DECL)
   11530            0 :     fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
   11531              : 
   11532   1225864081 :   new_level = tinst_level_freelist ().alloc ();
   11533   1225864081 :   new_level->tldcl = tldcl;
   11534   1225864081 :   new_level->targs = targs;
   11535   1225864081 :   new_level->locus = loc;
   11536   1225864081 :   new_level->errors = errorcount + sorrycount;
   11537   1225864081 :   new_level->had_errors = false;
   11538   1225864081 :   new_level->next = NULL;
   11539   1225864081 :   new_level->refcount = 0;
   11540   1225864081 :   new_level->path = new_level->visible = nullptr;
   11541   1225864081 :   set_refcount_ptr (new_level->next, current_tinst_level);
   11542   1225864081 :   set_refcount_ptr (current_tinst_level, new_level);
   11543              : 
   11544   1225864081 :   if (cxx_dump_pretty_printer pp {tinst_dump_id})
   11545              :     {
   11546              : #if __GNUC__ >= 10
   11547            0 : #pragma GCC diagnostic push
   11548            0 : #pragma GCC diagnostic ignored "-Wformat-diag"
   11549              : #endif
   11550            0 :       bool list_p = new_level->list_p ();
   11551            0 :       if ((list_p || TREE_CODE (tldcl) == TEMPLATE_FOR_STMT)
   11552            0 :           && !pp.has_flag (TDF_DETAILS))
   11553              :         /* Skip non-instantiations unless -details.  */;
   11554              :       else
   11555              :         {
   11556            0 :           if (tinst_depth == 0)
   11557            0 :             pp_newline (&pp);
   11558            0 :           if (loc && pp.has_flag (TDF_LINENO))
   11559              :             {
   11560            0 :               for (int i = 0; i < tinst_depth; ++i)
   11561            0 :                 pp_space (&pp);
   11562            0 :               const expanded_location el = expand_location (loc);
   11563            0 :               pp_printf (&pp, "%s:%d:%d", el.file, el.line, el.column);
   11564            0 :               pp_newline (&pp);
   11565              :             }
   11566            0 :           for (int i = 0; i < tinst_depth; ++i)
   11567            0 :             pp_space (&pp);
   11568            0 :           if (TREE_CODE (tldcl) == TEMPLATE_FOR_STMT)
   11569            0 :             pp_printf (&pp, "I template for");
   11570            0 :           else if (list_p)
   11571            0 :             pp_printf (&pp, "S %S", new_level->get_node ());
   11572              :           else
   11573            0 :             pp_printf (&pp, "I %D", tldcl);
   11574            0 :           pp_newline (&pp);
   11575              :         }
   11576              : #if __GNUC__ >= 10
   11577              : #pragma GCC diagnostic pop
   11578              : #endif
   11579   1225864081 :     }
   11580              : 
   11581   1225864081 :   ++tinst_depth;
   11582   1225864081 :   if (GATHER_STATISTICS && (tinst_depth > depth_reached))
   11583              :     depth_reached = tinst_depth;
   11584              : 
   11585   1225864081 :   return true;
   11586              : }
   11587              : 
   11588              : /* We're starting substitution of TMPL<ARGS>; record the template
   11589              :    substitution context for diagnostics and to restore it later.  */
   11590              : 
   11591              : bool
   11592   1094207564 : push_tinst_level (tree tmpl, tree args)
   11593              : {
   11594   1094207564 :   return push_tinst_level_loc (tmpl, args, input_location);
   11595              : }
   11596              : 
   11597              : /* We're starting to instantiate D; record INPUT_LOCATION and the
   11598              :    template instantiation context for diagnostics and to restore it
   11599              :    later.  */
   11600              : 
   11601              : bool
   11602    131503066 : push_tinst_level (tree d)
   11603              : {
   11604    131503066 :   return push_tinst_level_loc (d, input_location);
   11605              : }
   11606              : 
   11607              : /* Likewise, but record LOC as the program location.  */
   11608              : 
   11609              : bool
   11610    131657776 : push_tinst_level_loc (tree d, location_t loc)
   11611              : {
   11612    131657776 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   11613    131657776 :   return push_tinst_level_loc (d, NULL, loc);
   11614              : }
   11615              : 
   11616              : /* We're done instantiating this template; return to the instantiation
   11617              :    context.  */
   11618              : 
   11619              : void
   11620   1248741949 : pop_tinst_level (void)
   11621              : {
   11622              :   /* Restore the filename and line number stashed away when we started
   11623              :      this instantiation.  */
   11624   1248741949 :   input_location = current_tinst_level->locus;
   11625   1248741949 :   if (unsigned errs = errorcount + sorrycount)
   11626     34389353 :     if (errs > current_tinst_level->errors)
   11627       266094 :       current_tinst_level->had_errors = true;
   11628   1248741949 :   set_refcount_ptr (current_tinst_level, current_tinst_level->next);
   11629   1248741949 :   --tinst_depth;
   11630   1248741949 : }
   11631              : 
   11632              : /* True if the instantiation represented by LEVEL is complete.  */
   11633              : 
   11634              : static bool
   11635     51451215 : tinst_complete_p (struct tinst_level *level)
   11636              : {
   11637     51451215 :   gcc_assert (!level->list_p ());
   11638     51451215 :   tree node = level->get_node ();
   11639     51451215 :   if (TYPE_P (node))
   11640            0 :     return COMPLETE_TYPE_P (node);
   11641              :   else
   11642     51451215 :     return (DECL_TEMPLATE_INSTANTIATED (node)
   11643     51451215 :             || DECL_TEMPLATE_SPECIALIZATION (node));
   11644              : }
   11645              : 
   11646              : /* We're instantiating a deferred template; restore the template
   11647              :    instantiation context in which the instantiation was requested, which
   11648              :    is one step out from LEVEL.  Return the corresponding DECL or TYPE.  */
   11649              : 
   11650              : static tree
   11651     22907655 : reopen_tinst_level (struct tinst_level *level)
   11652              : {
   11653     22907655 :   struct tinst_level *t;
   11654              : 
   11655     22907655 :   tinst_depth = 0;
   11656    127567730 :   for (t = level; t; t = t->next)
   11657    104660075 :     ++tinst_depth;
   11658              : 
   11659     22907655 :   set_refcount_ptr (current_tinst_level, level);
   11660     22907655 :   pop_tinst_level ();
   11661     22907655 :   if (current_tinst_level && !current_tinst_level->had_errors)
   11662     18282290 :     current_tinst_level->errors = errorcount+sorrycount;
   11663              : 
   11664     22907655 :   if (cxx_dump_pretty_printer pp {tinst_dump_id})
   11665              :     {
   11666              : #if __GNUC__ >= 10
   11667            0 : #pragma GCC diagnostic push
   11668            0 : #pragma GCC diagnostic ignored "-Wformat-diag"
   11669              : #endif
   11670              :       /* Dump the reopened instantiation context.  */
   11671            0 :       t = current_tinst_level;
   11672            0 :       if (!pp.has_flag (TDF_DETAILS))
   11673              :         /* Skip non-instantiations unless -details.  */
   11674            0 :         while (t && t->list_p ())
   11675            0 :           t = t->next;
   11676            0 :       if (t)
   11677              :         {
   11678            0 :           static tree last_ctx = NULL_TREE;
   11679            0 :           tree ctx = t->get_node ();
   11680            0 :           if (ctx != last_ctx)
   11681              :             {
   11682            0 :               last_ctx = ctx;
   11683            0 :               pp_newline (&pp);
   11684            0 :               if (TREE_CODE (t->tldcl) == TEMPLATE_FOR_STMT)
   11685            0 :                 pp_printf (&pp, "RI template for");
   11686            0 :               else if (t->list_p ())
   11687            0 :                 pp_printf (&pp, "RS %S", ctx);
   11688              :               else
   11689            0 :                 pp_printf (&pp, "RI %D", ctx);
   11690            0 :               pp_newline (&pp);
   11691              :             }
   11692              :         }
   11693              : #if __GNUC__ >= 10
   11694              : #pragma GCC diagnostic pop
   11695              : #endif
   11696     22907655 :     }
   11697              : 
   11698     22907655 :   tree decl = level->maybe_get_node ();
   11699     22907655 :   if (decl && modules_p ())
   11700              :     {
   11701              :       /* An instantiation is in module purview only if it had an explicit
   11702              :          instantiation definition in module purview; mark_decl_instantiated uses
   11703              :          set_instantiating_module to set the flag in that case.  */
   11704        43702 :       if (DECL_MODULE_PURVIEW_P (decl))
   11705        21205 :         module_kind |= MK_PURVIEW;
   11706              :       else
   11707        22497 :         module_kind &= ~MK_PURVIEW;
   11708              :     }
   11709     22907655 :   return decl;
   11710              : }
   11711              : 
   11712              : /* Returns the TINST_LEVEL which gives the original instantiation
   11713              :    context.  */
   11714              : 
   11715              : struct tinst_level *
   11716          142 : outermost_tinst_level (void)
   11717              : {
   11718          142 :   struct tinst_level *level = current_tinst_level;
   11719          142 :   if (level)
   11720           18 :     while (level->next)
   11721              :       level = level->next;
   11722          142 :   return level;
   11723              : }
   11724              : 
   11725              : /* True iff T is a friend function declaration that is not itself a template
   11726              :    and is not defined in a class template.  */
   11727              : 
   11728              : bool
   11729    159298379 : non_templated_friend_p (tree t)
   11730              : {
   11731    159298379 :   if (t && TREE_CODE (t) == FUNCTION_DECL
   11732    318273555 :       && DECL_UNIQUE_FRIEND_P (t))
   11733              :     {
   11734      2965479 :       tree ti = DECL_TEMPLATE_INFO (t);
   11735      2965479 :       if (!ti)
   11736              :         return true;
   11737              :       /* DECL_FRIEND_CONTEXT is set for a friend defined in class.  */
   11738      5930958 :       if (DECL_FRIEND_CONTEXT (t))
   11739              :         return false;
   11740              :       /* Non-templated friends in a class template are still represented with a
   11741              :          TEMPLATE_DECL; check that its primary template is the befriending
   11742              :          class.  Note that DECL_PRIMARY_TEMPLATE is null for
   11743              :          template <class T> friend A<T>::f(); */
   11744      1099748 :       tree tmpl = TI_TEMPLATE (ti);
   11745      1099748 :       tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
   11746      1099748 :       return ((primary || DECL_NAMESPACE_SCOPE_P (t)) && primary != tmpl);
   11747              :     }
   11748              :   else
   11749              :     return false;
   11750              : }
   11751              : 
   11752              : /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
   11753              :    vector of template arguments, as for tsubst.
   11754              : 
   11755              :    Returns an appropriate tsubst'd friend declaration.  */
   11756              : 
   11757              : static tree
   11758      2676609 : tsubst_friend_function (tree decl, tree args)
   11759              : {
   11760      2676609 :   tree new_friend;
   11761              : 
   11762      2676609 :   if (decl_specialization_friend_p (decl))
   11763              :     /* This was a friend declared with an explicit template
   11764              :        argument list, e.g.:
   11765              : 
   11766              :        friend void f<>(T);
   11767              : 
   11768              :        to indicate that f was a template instantiation, not a new
   11769              :        function declaration.  Now, we have to figure out what
   11770              :        instantiation of what template.  */
   11771              :     {
   11772        26601 :       tree template_id, arglist, fns;
   11773        26601 :       tree new_args;
   11774        26601 :       tree tmpl;
   11775        26601 :       tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
   11776              : 
   11777              :       /* Friend functions are looked up in the containing namespace scope.
   11778              :          We must enter that scope, to avoid finding member functions of the
   11779              :          current class with same name.  */
   11780        26601 :       push_nested_namespace (ns);
   11781        26601 :       fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
   11782              :                          tf_warning_or_error, NULL_TREE);
   11783        26601 :       pop_nested_namespace (ns);
   11784        26601 :       arglist = tsubst (DECL_TI_ARGS (decl), args,
   11785              :                         tf_warning_or_error, NULL_TREE);
   11786        26601 :       template_id = lookup_template_function (fns, arglist);
   11787              : 
   11788        26601 :       new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11789        26601 :       tmpl = determine_specialization (template_id, new_friend,
   11790              :                                        &new_args,
   11791              :                                        /*need_member_template=*/0,
   11792        26601 :                                        TREE_VEC_LENGTH (args),
   11793              :                                        tsk_none);
   11794        26601 :       return instantiate_template (tmpl, new_args, tf_error);
   11795              :     }
   11796              : 
   11797      2650008 :   new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11798      2650008 :   if (new_friend == error_mark_node)
   11799              :     return error_mark_node;
   11800              : 
   11801              :   /* The NEW_FRIEND will look like an instantiation, to the
   11802              :      compiler, but is not an instantiation from the point of view of
   11803              :      the language.  For example, we might have had:
   11804              : 
   11805              :      template <class T> struct S {
   11806              :        template <class U> friend void f(T, U);
   11807              :      };
   11808              : 
   11809              :      Then, in S<int>, template <class U> void f(int, U) is not an
   11810              :      instantiation of anything.  */
   11811              : 
   11812      2650008 :   DECL_USE_TEMPLATE (new_friend) = 0;
   11813      2650008 :   if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11814              :     {
   11815      1705879 :       DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
   11816      1705879 :       DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
   11817      3411758 :       DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
   11818      1705879 :         = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
   11819              : 
   11820              :       /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
   11821              :          match in decls_match.  */
   11822      1705879 :       tree parms = DECL_TEMPLATE_PARMS (new_friend);
   11823      1705879 :       tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
   11824      1705879 :       treqs = maybe_substitute_reqs_for (treqs, new_friend);
   11825      1705879 :       if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
   11826              :         {
   11827       175207 :           TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
   11828              :           /* As well as each TEMPLATE_PARM_CONSTRAINTS.  */
   11829       175207 :           tsubst_each_template_parm_constraints (parms, args,
   11830              :                                                  tf_warning_or_error);
   11831              :         }
   11832              :     }
   11833              : 
   11834              :   /* The mangled name for the NEW_FRIEND is incorrect.  The function
   11835              :      is not a template instantiation and should not be mangled like
   11836              :      one.  Therefore, we forget the mangling here; we'll recompute it
   11837              :      later if we need it.  */
   11838      2650008 :   if (TREE_CODE (new_friend) != TEMPLATE_DECL)
   11839              :     {
   11840       944129 :       SET_DECL_RTL (new_friend, NULL);
   11841       944129 :       SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
   11842              :     }
   11843              : 
   11844      2650008 :   if (DECL_NAMESPACE_SCOPE_P (new_friend))
   11845              :     {
   11846      2649933 :       tree old_decl;
   11847      2649933 :       tree ns;
   11848              : 
   11849              :       /* We must save some information from NEW_FRIEND before calling
   11850              :          duplicate decls since that function will free NEW_FRIEND if
   11851              :          possible.  */
   11852      2649933 :       tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
   11853      2649933 :       tree new_friend_result_template_info = NULL_TREE;
   11854      2649933 :       bool new_friend_is_defn =
   11855              :         (new_friend_template_info
   11856      2649933 :          && (DECL_INITIAL (DECL_TEMPLATE_RESULT
   11857              :                            (template_for_substitution (new_friend)))
   11858      2649933 :              != NULL_TREE));
   11859      2649933 :       tree not_tmpl = new_friend;
   11860              : 
   11861      2649933 :       if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11862              :         {
   11863              :           /* This declaration is a `primary' template.  */
   11864      1705831 :           DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
   11865              : 
   11866      1705831 :           not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
   11867      1705831 :           new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
   11868              :         }
   11869              : 
   11870              :       /* We need to propagate module attachment for the new friend from the
   11871              :          owner of this template.  */
   11872      2649933 :       propagate_defining_module (new_friend, decl);
   11873              : 
   11874              :       /* Inside pushdecl_namespace_level, we will push into the
   11875              :          current namespace. However, the friend function should go
   11876              :          into the namespace of the template.  */
   11877      2649933 :       ns = decl_namespace_context (new_friend);
   11878      2649933 :       push_nested_namespace (ns);
   11879      2649933 :       old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
   11880      2649933 :       pop_nested_namespace (ns);
   11881              : 
   11882      2649933 :       if (old_decl == error_mark_node)
   11883              :         return error_mark_node;
   11884              : 
   11885      2649912 :       if (old_decl != new_friend)
   11886              :         {
   11887              :           /* This new friend declaration matched an existing
   11888              :              declaration.  For example, given:
   11889              : 
   11890              :                template <class T> void f(T);
   11891              :                template <class U> class C {
   11892              :                  template <class T> friend void f(T) {}
   11893              :                };
   11894              : 
   11895              :              the friend declaration actually provides the definition
   11896              :              of `f', once C has been instantiated for some type.  So,
   11897              :              old_decl will be the out-of-class template declaration,
   11898              :              while new_friend is the in-class definition.
   11899              : 
   11900              :              But, if `f' was called before this point, the
   11901              :              instantiation of `f' will have DECL_TI_ARGS corresponding
   11902              :              to `T' but not to `U', references to which might appear
   11903              :              in the definition of `f'.  Previously, the most general
   11904              :              template for an instantiation of `f' was the out-of-class
   11905              :              version; now it is the in-class version.  Therefore, we
   11906              :              run through all specialization of `f', adding to their
   11907              :              DECL_TI_ARGS appropriately.  In particular, they need a
   11908              :              new set of outer arguments, corresponding to the
   11909              :              arguments for this class instantiation.
   11910              : 
   11911              :              The same situation can arise with something like this:
   11912              : 
   11913              :                friend void f(int);
   11914              :                template <class T> class C {
   11915              :                  friend void f(T) {}
   11916              :                };
   11917              : 
   11918              :              when `C<int>' is instantiated.  Now, `f(int)' is defined
   11919              :              in the class.  */
   11920              : 
   11921       957937 :           if (!new_friend_is_defn)
   11922              :             /* On the other hand, if the in-class declaration does
   11923              :                *not* provide a definition, then we don't want to alter
   11924              :                existing definitions.  We can just leave everything
   11925              :                alone.  */
   11926              :             ;
   11927              :           else
   11928              :             {
   11929           66 :               tree old_template = most_general_template (old_decl);
   11930           66 :               tree new_template = TI_TEMPLATE (new_friend_template_info);
   11931           66 :               tree new_args = TI_ARGS (new_friend_template_info);
   11932              : 
   11933              :               /* Overwrite whatever template info was there before, if
   11934              :                  any, with the new template information pertaining to
   11935              :                  the declaration.  */
   11936           66 :               DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
   11937              : 
   11938           66 :               if (TREE_CODE (old_decl) != TEMPLATE_DECL)
   11939              :                 {
   11940              :                   /* We should have called reregister_specialization in
   11941              :                      duplicate_decls.  */
   11942           33 :                   gcc_assert (retrieve_specialization (new_template,
   11943              :                                                        new_args, 0)
   11944              :                               == old_decl);
   11945              : 
   11946              :                   /* Instantiate it if the global has already been used.  */
   11947           33 :                   if (DECL_ODR_USED (old_decl))
   11948            3 :                     instantiate_decl (old_decl, /*defer_ok=*/true,
   11949              :                                       /*expl_inst_class_mem_p=*/false);
   11950              :                 }
   11951              :               else
   11952              :                 {
   11953           33 :                   tree t;
   11954              : 
   11955              :                   /* Indicate that the old function template is a partial
   11956              :                      instantiation.  */
   11957           33 :                   DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
   11958           33 :                     = new_friend_result_template_info;
   11959              : 
   11960           33 :                   gcc_assert (new_template
   11961              :                               == most_general_template (new_template));
   11962           33 :                   gcc_assert (new_template != old_decl);
   11963              : 
   11964              :                   /* Reassign any specializations already in the hash table
   11965              :                      to the new more general template, and add the
   11966              :                      additional template args.  */
   11967           33 :                   for (t = DECL_TEMPLATE_INSTANTIATIONS (old_template);
   11968          252 :                        t != NULL_TREE;
   11969          219 :                        t = TREE_CHAIN (t))
   11970              :                     {
   11971          219 :                       tree spec = TREE_VALUE (t);
   11972          219 :                       spec_entry elt;
   11973              : 
   11974          219 :                       elt.tmpl = old_decl;
   11975          219 :                       elt.args = DECL_TI_ARGS (spec);
   11976          219 :                       elt.spec = NULL_TREE;
   11977              : 
   11978          438 :                       if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (DECL_TI_ARGS (spec))
   11979          429 :                           && !is_specialization_of_friend (spec, new_template))
   11980          192 :                         continue;
   11981              : 
   11982           27 :                       decl_specializations->remove_elt (&elt);
   11983              : 
   11984           27 :                       tree& spec_args = DECL_TI_ARGS (spec);
   11985           54 :                       spec_args = add_outermost_template_args
   11986           27 :                         (new_args, INNERMOST_TEMPLATE_ARGS (spec_args));
   11987              : 
   11988           27 :                       register_specialization
   11989           27 :                         (spec, new_template, spec_args, true, 0);
   11990              : 
   11991              :                     }
   11992           33 :                   DECL_TEMPLATE_INSTANTIATIONS (old_template) = NULL_TREE;
   11993              :                 }
   11994              :             }
   11995              : 
   11996              :           /* The information from NEW_FRIEND has been merged into OLD_DECL
   11997              :              by duplicate_decls.  */
   11998              :           new_friend = old_decl;
   11999              :         }
   12000              : 
   12001              :       /* We've just introduced a namespace-scope function without
   12002              :          necessarily having opened the enclosing namespace, so
   12003              :          make sure the namespace is declared in this TU as well.  */
   12004      2649912 :       if (modules_p ())
   12005         5318 :         for (tree ctx = DECL_CONTEXT (new_friend);
   12006        11618 :              TREE_CODE (ctx) == NAMESPACE_DECL; ctx = DECL_CONTEXT (ctx))
   12007         6300 :           expose_existing_namespace (ctx);
   12008              :     }
   12009              :   else
   12010              :     {
   12011           75 :       tree context = DECL_CONTEXT (new_friend);
   12012           75 :       bool dependent_p;
   12013              : 
   12014              :       /* In the code
   12015              :            template <class T> class C {
   12016              :              template <class U> friend void C1<U>::f (); // case 1
   12017              :              friend void C2<T>::f ();                      // case 2
   12018              :            };
   12019              :          we only need to make sure CONTEXT is a complete type for
   12020              :          case 2.  To distinguish between the two cases, we note that
   12021              :          CONTEXT of case 1 remains dependent type after tsubst while
   12022              :          this isn't true for case 2.  */
   12023           75 :       ++processing_template_decl;
   12024           75 :       dependent_p = dependent_type_p (context);
   12025           75 :       --processing_template_decl;
   12026              : 
   12027           75 :       if (!dependent_p
   12028           75 :           && !complete_type_or_else (context, NULL_TREE))
   12029            0 :         return error_mark_node;
   12030              : 
   12031           75 :       if (COMPLETE_TYPE_P (context))
   12032              :         {
   12033           69 :           tree fn = new_friend;
   12034              :           /* do_friend adds the TEMPLATE_DECL for any member friend
   12035              :              template even if it isn't a member template, i.e.
   12036              :                template <class T> friend A<T>::f();
   12037              :              Look through it in that case.  */
   12038           69 :           if (TREE_CODE (fn) == TEMPLATE_DECL
   12039           69 :               && !PRIMARY_TEMPLATE_P (fn))
   12040           24 :             fn = DECL_TEMPLATE_RESULT (fn);
   12041              :           /* Check to see that the declaration is really present, and,
   12042              :              possibly obtain an improved declaration.  */
   12043           69 :           fn = check_classfn (context, fn, NULL_TREE);
   12044              : 
   12045           69 :           if (fn)
   12046      2676609 :             new_friend = fn;
   12047              :         }
   12048              :     }
   12049              : 
   12050              :   return new_friend;
   12051              : }
   12052              : 
   12053              : /* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
   12054              :    template arguments, as for tsubst.
   12055              : 
   12056              :    Returns an appropriate tsubst'd friend type or error_mark_node on
   12057              :    failure.  */
   12058              : 
   12059              : static tree
   12060       602596 : tsubst_friend_class (tree friend_tmpl, tree args)
   12061              : {
   12062       602596 :   tree tmpl;
   12063              : 
   12064       602596 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
   12065              :     {
   12066            3 :       tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
   12067            3 :       return TREE_TYPE (tmpl);
   12068              :     }
   12069              : 
   12070       602593 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) == 1)
   12071              :     /* The template has already been fully substituted, e.g. for
   12072              : 
   12073              :          template <typename> friend class ::C;
   12074              : 
   12075              :        so we can just return it directly.  */
   12076       125996 :     return TREE_TYPE (friend_tmpl);
   12077              : 
   12078       476597 :   tree context = CP_DECL_CONTEXT (friend_tmpl);
   12079       476597 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   12080       476205 :     push_nested_namespace (context);
   12081              :   else
   12082              :     {
   12083          392 :       context = tsubst (context, args, tf_error, NULL_TREE);
   12084          392 :       push_nested_class (context);
   12085              :     }
   12086              : 
   12087       476597 :   tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
   12088              :                       LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
   12089              : 
   12090       476597 :   if (!tmpl)
   12091              :     /* If we didn't find by name lookup, the type may still exist but as a
   12092              :        'hidden' import; we should check for this too to avoid accidentally
   12093              :        instantiating a duplicate.  */
   12094          262 :     tmpl = lookup_imported_hidden_friend (friend_tmpl);
   12095              : 
   12096       476597 :   if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
   12097              :     {
   12098              :       /* The friend template has already been declared.  Just
   12099              :          check to see that the declarations match, and install any new
   12100              :          default parameters.  We must tsubst the default parameters,
   12101              :          of course.  We only need the innermost template parameters
   12102              :          because that is all that redeclare_class_template will look
   12103              :          at.  */
   12104              : 
   12105       476347 :       if (modules_p ())
   12106              :         /* Check that the existing declaration's module attachment is
   12107              :            compatible with the attachment of the friend template.  */
   12108         1172 :         module_may_redeclare (tmpl, friend_tmpl);
   12109              : 
   12110       476347 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (friend_tmpl))
   12111              :         {
   12112       476344 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
   12113              :                                               args, tf_warning_or_error);
   12114       476344 :           tsubst_each_template_parm_constraints (parms, args,
   12115              :                                                  tf_warning_or_error);
   12116       476344 :           location_t saved_input_location = input_location;
   12117       476344 :           input_location = DECL_SOURCE_LOCATION (friend_tmpl);
   12118       476344 :           tree cons = get_constraints (friend_tmpl);
   12119       476344 :           ++processing_template_decl;
   12120       476344 :           cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
   12121       476344 :                                          DECL_FRIEND_CONTEXT (friend_tmpl));
   12122       476344 :           --processing_template_decl;
   12123       476344 :           redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
   12124       476344 :           input_location = saved_input_location;
   12125              :         }
   12126              :     }
   12127              :   else
   12128              :     {
   12129              :       /* The friend template has not already been declared.  In this
   12130              :          case, the instantiation of the template class will cause the
   12131              :          injection of this template into the namespace scope.  */
   12132          250 :       tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
   12133              : 
   12134          250 :       if (tmpl != error_mark_node)
   12135              :         {
   12136              :           /* The new TMPL is not an instantiation of anything, so we
   12137              :              forget its origins.  It is also not a specialization of
   12138              :              anything.  We don't reset CLASSTYPE_TI_TEMPLATE
   12139              :              for the new type because that is supposed to be the
   12140              :              corresponding template decl, i.e., TMPL.  */
   12141          245 :           spec_entry elt;
   12142          245 :           elt.tmpl = friend_tmpl;
   12143          245 :           elt.args = CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl));
   12144          245 :           elt.spec = TREE_TYPE (tmpl);
   12145          245 :           type_specializations->remove_elt (&elt);
   12146              : 
   12147          245 :           DECL_USE_TEMPLATE (tmpl) = 0;
   12148          245 :           DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
   12149          245 :           CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
   12150          245 :           CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
   12151          245 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
   12152          245 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = false;
   12153              : 
   12154              :           /* Substitute into and set the constraints on the new declaration.  */
   12155          245 :           if (tree ci = get_constraints (friend_tmpl))
   12156              :             {
   12157            6 :               ++processing_template_decl;
   12158            6 :               ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
   12159            6 :                                            DECL_FRIEND_CONTEXT (friend_tmpl));
   12160            6 :               --processing_template_decl;
   12161            6 :               set_constraints (tmpl, ci);
   12162            6 :               tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
   12163              :                                                      args, tf_warning_or_error);
   12164              :             }
   12165              : 
   12166              :           /* We need to propagate the attachment of the original template to the
   12167              :              newly instantiated template type.  */
   12168          245 :           propagate_defining_module (tmpl, friend_tmpl);
   12169              : 
   12170              :           /* Inject this template into the enclosing namspace scope.  */
   12171          245 :           tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
   12172              :         }
   12173              :     }
   12174              : 
   12175       476597 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   12176       476205 :     pop_nested_namespace (context);
   12177              :   else
   12178          392 :     pop_nested_class ();
   12179              : 
   12180       476597 :   return TREE_TYPE (tmpl);
   12181              : }
   12182              : 
   12183              : /* Returns zero if TYPE cannot be completed later due to circularity.
   12184              :    Otherwise returns one.  */
   12185              : 
   12186              : static int
   12187      8843673 : can_complete_type_without_circularity (tree type)
   12188              : {
   12189      8844073 :   if (type == NULL_TREE || type == error_mark_node)
   12190              :     return 0;
   12191      8844073 :   else if (COMPLETE_TYPE_P (type))
   12192              :     return 1;
   12193       815284 :   else if (TREE_CODE (type) == ARRAY_TYPE)
   12194          400 :     return can_complete_type_without_circularity (TREE_TYPE (type));
   12195       814879 :   else if (CLASS_TYPE_P (type)
   12196      1629763 :            && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
   12197              :     return 0;
   12198              :   else
   12199       814875 :     return 1;
   12200              : }
   12201              : 
   12202              : static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
   12203              :                                 tsubst_flags_t, tree);
   12204              : 
   12205              : /* Instantiate the contract statement.  */
   12206              : 
   12207              : static tree
   12208          261 : tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
   12209              :                  tree in_decl)
   12210              : {
   12211          261 :   tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
   12212          261 :   bool auto_p  = type_uses_auto (type);
   12213              : 
   12214          261 :   tree r = copy_node (t);
   12215              : 
   12216              :   /* Rebuild the result variable, if present.  */
   12217          261 :   tree oldvar = NULL_TREE;
   12218          261 :   tree newvar = NULL_TREE;
   12219          261 :   if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
   12220              :     {
   12221           42 :       oldvar = POSTCONDITION_IDENTIFIER (t);
   12222           42 :       if (oldvar == error_mark_node)
   12223            2 :         return invalidate_contract (r);
   12224              : 
   12225           40 :       newvar = copy_node (oldvar);
   12226           40 :       TREE_TYPE (newvar) = type;
   12227           40 :       DECL_CONTEXT (newvar) = decl;
   12228           40 :       POSTCONDITION_IDENTIFIER (r) = newvar;
   12229              : 
   12230              :       /* Make sure the postcondition is valid.  */
   12231           40 :       location_t loc = DECL_SOURCE_LOCATION (oldvar);
   12232           40 :       if (!auto_p)
   12233           28 :         if (!check_postcondition_result (decl, type, loc))
   12234            0 :           return invalidate_contract (r);
   12235              :     }
   12236              : 
   12237              :   /* Instantiate the condition.  If the return type is undeduced, process
   12238              :      the expression as if inside a template to avoid spurious type errors.  */
   12239          259 :   begin_scope (sk_contract, decl);
   12240          259 :   bool old_pc = processing_postcondition;
   12241          259 :   processing_postcondition = POSTCONDITION_P (t);
   12242          259 :   if (auto_p)
   12243           12 :     ++processing_template_decl;
   12244          259 :   if (newvar)
   12245              :     /* Make the variable available for lookup.  */
   12246           40 :     register_local_specialization (newvar, oldvar);
   12247              : 
   12248              :   /* Contract conditions have a wider application of location wrappers than
   12249              :      other trees (which will not work with the generic handling in tsubst_expr),
   12250              :      remove the wrapper here...  */
   12251          259 :   location_t cond_l = EXPR_LOCATION (CONTRACT_CONDITION (t));
   12252          259 :   tree cond_t = tree_strip_any_location_wrapper (CONTRACT_CONDITION (t));
   12253              : 
   12254              :   /* ... and substitute the contained expression.  */
   12255          259 :   cond_t = tsubst_expr (cond_t, args, complain, in_decl);
   12256              : 
   12257              :   /* Convert to bool, if possible, and then re-apply a location wrapper
   12258              :      when required.  */
   12259          259 :   cp_expr new_condition (cond_t, cond_l);
   12260          259 :   CONTRACT_CONDITION (r) = finish_contract_condition (new_condition);
   12261              : 
   12262              :   /* At present, the semantic, kind and comment cannot be dependent.  */
   12263          259 :   gcc_checking_assert
   12264              :     (!type_dependent_expression_p (CONTRACT_EVALUATION_SEMANTIC (r))
   12265              :      && !type_dependent_expression_p (CONTRACT_ASSERTION_KIND (r))
   12266              :      && !type_dependent_expression_p (CONTRACT_COMMENT (r)));
   12267              : 
   12268          259 :   if (auto_p)
   12269           12 :     --processing_template_decl;
   12270          259 :   processing_postcondition = old_pc;
   12271          259 :   gcc_checking_assert (scope_chain && scope_chain->bindings
   12272              :                        && scope_chain->bindings->kind == sk_contract);
   12273          259 :   pop_bindings_and_leave_scope ();
   12274              : 
   12275          259 :   return r;
   12276              : }
   12277              : 
   12278              : /* Update T instantiating a contract specifier.  */
   12279              : 
   12280              : static void
   12281          260 : tsubst_contract_specifier (tree decl, tree t, tree args,
   12282              :                            tsubst_flags_t complain, tree in_decl)
   12283              : {
   12284              :   /* For non-specializations, adjust the current declaration to the most general
   12285              :      version of in_decl. Because we defer the instantiation of contracts as long
   12286              :      as possible, they are still written in terms of the parameters (and return
   12287              :      type) of the most general template.  */
   12288          260 :   tree tmpl = DECL_TI_TEMPLATE (in_decl);
   12289          260 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
   12290          248 :     in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
   12291          260 :   local_specialization_stack specs (lss_copy);
   12292          260 :   register_parameter_specializations (in_decl, decl);
   12293              : 
   12294              :   /* Get the contract to be instantiated.  */
   12295          260 :   tree contract = CONTRACT_STATEMENT (t);
   12296              : 
   12297              :   /* Use the complete set of template arguments for instantiation. The
   12298              :      contract may not have been instantiated and still refer to outer levels
   12299              :      of template parameters.  */
   12300          260 :   args = DECL_TI_ARGS (decl);
   12301              : 
   12302              :   /* For member functions, make this available for semantic analysis.  */
   12303          260 :   tree save_ccp = current_class_ptr;
   12304          260 :   tree save_ccr = current_class_ref;
   12305          260 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
   12306              :     {
   12307          184 :       tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
   12308          184 :       tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
   12309          184 :       inject_this_parameter (this_type, cp_type_quals (this_type));
   12310              :     }
   12311              : 
   12312          260 :   contract = tsubst_contract (decl, contract, args, complain, in_decl);
   12313              : 
   12314          260 :   current_class_ptr = save_ccp;
   12315          260 :   current_class_ref = save_ccr;
   12316              : 
   12317              :   /* Rebuild the attribute.  */
   12318          260 :   TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
   12319          260 : }
   12320              : 
   12321              : /* For unsubstituted list of contracts in SPECIFIERS, instantiate contracts
   12322              :  for DECL and set the list as contracts for decl. Substitution creates a deep
   12323              :  copy of the contract.  */
   12324              : 
   12325              : void
   12326          140 : tsubst_contract_specifiers (tree specfiers, tree decl, tree args,
   12327              :                             tsubst_flags_t complain, tree in_decl)
   12328              : {
   12329          140 :   tree subst_contract_list = NULL_TREE;
   12330          400 :   for (tree spec = specfiers; spec; spec = TREE_CHAIN (spec))
   12331              :     {
   12332          260 :       tree nc = copy_node (spec);
   12333          260 :       tsubst_contract_specifier (decl, nc, args, complain, in_decl);
   12334          260 :       TREE_CHAIN (nc) = subst_contract_list;
   12335          260 :       subst_contract_list = nc;
   12336              :     }
   12337          140 :   if (flag_contracts)
   12338          140 :     set_fn_contract_specifiers (decl, nreverse (subst_contract_list));
   12339          140 : }
   12340              : 
   12341              : /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
   12342              :    T or a new TREE_LIST, possibly a chain in the case of a pack expansion.  */
   12343              : 
   12344              : static tree
   12345      1078704 : tsubst_attribute (tree t, tree *decl_p, tree args,
   12346              :                   tsubst_flags_t complain, tree in_decl)
   12347              : {
   12348      1078704 :   gcc_assert (ATTR_IS_DEPENDENT (t));
   12349              : 
   12350      1078704 :   tree val = TREE_VALUE (t);
   12351      1078704 :   if (val == NULL_TREE)
   12352              :     /* Nothing to do.  */;
   12353       227110 :   else if ((flag_openmp || flag_openmp_simd)
   12354       227734 :            && is_attribute_p ("omp declare simd",
   12355          345 :                               get_attribute_name (t)))
   12356              :     {
   12357          105 :       tree clauses = TREE_VALUE (val);
   12358          105 :       clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
   12359              :                                     complain, in_decl);
   12360          105 :       c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   12361          105 :       clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   12362          105 :       tree parms = DECL_ARGUMENTS (*decl_p);
   12363          105 :       clauses
   12364          105 :         = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
   12365          105 :       if (clauses)
   12366          105 :         val = build_tree_list (NULL_TREE, clauses);
   12367              :       else
   12368              :         val = NULL_TREE;
   12369              :     }
   12370       227284 :   else if (flag_openmp
   12371       227524 :            && is_attribute_p ("omp declare variant base",
   12372          240 :                               get_attribute_name (t)))
   12373              :     {
   12374          213 :       ++cp_unevaluated_operand;
   12375          213 :       tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
   12376          213 :       --cp_unevaluated_operand;
   12377          213 :       tree chain = TREE_CHAIN (val);
   12378          213 :       location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
   12379          213 :       tree ctx = copy_list (TREE_VALUE (val));
   12380          213 :       tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
   12381          213 :       if (append_args_list
   12382           63 :           && TREE_VALUE (append_args_list)
   12383          276 :           && TREE_CHAIN (TREE_VALUE (append_args_list)))
   12384              :         {
   12385           54 :           append_args_list = TREE_VALUE (append_args_list);
   12386           54 :           append_args_list = TREE_VALUE (TREE_CHAIN (append_args_list));
   12387          153 :           for (; append_args_list;
   12388           99 :                append_args_list = TREE_CHAIN (append_args_list))
   12389              :              {
   12390           99 :               tree pref_list = TREE_VALUE (append_args_list);
   12391           99 :               if (pref_list == NULL_TREE || TREE_CODE (pref_list) != TREE_LIST)
   12392           63 :                 continue;
   12393           36 :               tree fr_list = TREE_VALUE (pref_list);
   12394           36 :               int len = TREE_VEC_LENGTH (fr_list);
   12395          105 :               for (int i = 0; i < len; i++)
   12396              :                 {
   12397           69 :                   tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
   12398              :                   /* Preserve NOP_EXPR to have a location.  */
   12399           69 :                   if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
   12400            0 :                     TREE_OPERAND (*fr_expr, 0)
   12401            0 :                       = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
   12402              :                                      in_decl);
   12403              :                   else
   12404           69 :                     *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
   12405              :                 }
   12406              :             }
   12407              :         }
   12408          423 :       for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
   12409              :         {
   12410          219 :           enum omp_tss_code set = OMP_TSS_CODE (tss);
   12411          219 :           tree selectors = NULL_TREE;
   12412          429 :           for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
   12413          210 :                ts = TREE_CHAIN (ts))
   12414              :             {
   12415          219 :               tree properties = NULL_TREE;
   12416          219 :               tree scoreval = NULL_TREE;
   12417              :               /* FIXME: The body of this loop should really be dispatching
   12418              :                  according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
   12419              :                  of having hard-wired knowledge of specific selectors.  */
   12420          219 :               if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
   12421          219 :                   && set == OMP_TRAIT_SET_CONSTRUCT)
   12422              :                 {
   12423            0 :                   tree clauses = OMP_TS_PROPERTIES (ts);
   12424            0 :                   clauses = tsubst_omp_clauses (clauses,
   12425              :                                                 C_ORT_OMP_DECLARE_SIMD, args,
   12426              :                                                 complain, in_decl);
   12427            0 :                   c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   12428            0 :                   clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   12429            0 :                   properties = clauses;
   12430              :                 }
   12431              :               else
   12432              :                 {
   12433          219 :                   tree v = OMP_TS_SCORE (ts);
   12434           45 :                   if (v)
   12435              :                     {
   12436           45 :                       v = tsubst_expr (v, args, complain, in_decl);
   12437           45 :                       v = fold_non_dependent_expr (v);
   12438           90 :                       if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   12439           87 :                           || TREE_CODE (v) != INTEGER_CST)
   12440              :                         {
   12441            3 :                           location_t loc
   12442            3 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   12443              :                                                   match_loc);
   12444            3 :                           error_at (loc, "score argument must be "
   12445              :                                     "constant integer expression");
   12446            3 :                           return NULL_TREE;
   12447              :                         }
   12448           42 :                       else if (tree_int_cst_sgn (v) < 0)
   12449              :                         {
   12450            3 :                           location_t loc
   12451            3 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   12452              :                                                   match_loc);
   12453            3 :                           error_at (loc, "score argument must be "
   12454              :                                     "non-negative");
   12455            3 :                           return NULL_TREE;
   12456              :                         }
   12457              :                       scoreval = v;
   12458              :                     }
   12459          213 :                   properties = copy_list (OMP_TS_PROPERTIES (ts));
   12460          350 :                   for (tree p = properties; p; p = TREE_CHAIN (p))
   12461          140 :                     if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
   12462           80 :                       continue;
   12463           60 :                     else if (OMP_TP_VALUE (p))
   12464              :                       {
   12465           60 :                         bool allow_string
   12466           60 :                           = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
   12467           60 :                              || set != OMP_TRAIT_SET_USER);
   12468           60 :                         tree v = OMP_TP_VALUE (p);
   12469           60 :                         if (TREE_CODE (v) == STRING_CST && allow_string)
   12470            0 :                           continue;
   12471           60 :                         v = tsubst_expr (v, args, complain, in_decl);
   12472           60 :                         v = fold_non_dependent_expr (v);
   12473          120 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   12474          117 :                             || !tree_fits_shwi_p (v))
   12475              :                           {
   12476            3 :                             location_t loc
   12477            3 :                               = cp_expr_loc_or_loc (OMP_TP_VALUE (p),
   12478              :                                                     match_loc);
   12479            3 :                             if (allow_string)
   12480            0 :                               error_at (loc, "property must be constant "
   12481              :                                              "integer expression or string "
   12482              :                                              "literal");
   12483              :                             else
   12484            3 :                               error_at (loc, "property must be constant "
   12485              :                                              "integer expression");
   12486            3 :                             return NULL_TREE;
   12487              :                           }
   12488           57 :                         OMP_TP_VALUE (p) = v;
   12489              :                       }
   12490              :                 }
   12491          210 :               selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
   12492              :                                                properties, selectors);
   12493              :             }
   12494          210 :           OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
   12495              :         }
   12496          204 :       if (varid == error_mark_node)
   12497              :         val = error_mark_node;
   12498              :       else
   12499          198 :         val = tree_cons (varid, ctx, chain);
   12500              :     }
   12501              :   /* If the first attribute argument is an identifier, don't
   12502              :      pass it through tsubst.  Attributes like mode, format,
   12503              :      cleanup and several target specific attributes expect it
   12504              :      unmodified.  */
   12505       227071 :   else if (get_attribute_namespace (t) == gnu_identifier
   12506       227071 :            && attribute_takes_identifier_p (get_attribute_name (t)))
   12507              :     {
   12508          109 :       tree chain
   12509          109 :         = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
   12510          109 :       if (chain != TREE_CHAIN (val))
   12511           12 :         val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
   12512              :     }
   12513       226962 :   else if (PACK_EXPANSION_P (val))
   12514              :     {
   12515              :       /* An attribute pack expansion.  */
   12516           20 :       tree purp = TREE_PURPOSE (t);
   12517           20 :       tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
   12518           20 :       if (pack == error_mark_node)
   12519              :         return error_mark_node;
   12520           17 :       int len = TREE_VEC_LENGTH (pack);
   12521           17 :       tree list = NULL_TREE;
   12522           17 :       tree *q = &list;
   12523           39 :       for (int i = 0; i < len; ++i)
   12524              :         {
   12525           22 :           tree elt = TREE_VEC_ELT (pack, i);
   12526           22 :           *q = build_tree_list (purp, elt);
   12527           22 :           q = &TREE_CHAIN (*q);
   12528              :         }
   12529           17 :       return list;
   12530              :     }
   12531              :   else
   12532       226942 :     val = tsubst_expr (val, args, complain, in_decl);
   12533              : 
   12534      1078675 :   if (val == error_mark_node)
   12535              :     return error_mark_node;
   12536      1078654 :   if (val != TREE_VALUE (t))
   12537       226604 :     return build_tree_list (TREE_PURPOSE (t), val);
   12538              :   return t;
   12539              : }
   12540              : 
   12541              : /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
   12542              :    unchanged or a new TREE_LIST chain.  */
   12543              : 
   12544              : static tree
   12545       430041 : tsubst_attributes (tree attributes, tree args,
   12546              :                    tsubst_flags_t complain, tree in_decl)
   12547              : {
   12548       430041 :   tree last_dep = NULL_TREE;
   12549              : 
   12550       430049 :   for (tree t = attributes; t; t = TREE_CHAIN (t))
   12551           24 :     if (ATTR_IS_DEPENDENT (t))
   12552              :       {
   12553           16 :         last_dep = t;
   12554           16 :         attributes = copy_list (attributes);
   12555           16 :         break;
   12556              :       }
   12557              : 
   12558       430041 :   if (last_dep)
   12559           32 :     for (tree *p = &attributes; *p; )
   12560              :       {
   12561           16 :         tree t = *p;
   12562           16 :         if (ATTR_IS_DEPENDENT (t))
   12563              :           {
   12564           16 :             tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
   12565           16 :             if (subst != t)
   12566              :               {
   12567            6 :                 *p = subst;
   12568            9 :                 while (*p)
   12569            3 :                   p = &TREE_CHAIN (*p);
   12570            6 :                 *p = TREE_CHAIN (t);
   12571            6 :                 continue;
   12572              :               }
   12573              :           }
   12574           10 :         p = &TREE_CHAIN (*p);
   12575              :       }
   12576              : 
   12577       430041 :   return attributes;
   12578              : }
   12579              : 
   12580              : /* Apply any attributes which had to be deferred until instantiation
   12581              :    time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
   12582              :    ARGS, COMPLAIN, IN_DECL are as tsubst.  Returns true normally,
   12583              :    false on error.  */
   12584              : 
   12585              : static bool
   12586   1565660567 : apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
   12587              :                                 tree args, tsubst_flags_t complain, tree in_decl)
   12588              : {
   12589   1565660567 :   tree t;
   12590   1565660567 :   tree *p;
   12591              : 
   12592   1565660567 :   if (attributes == NULL_TREE)
   12593              :     return true;
   12594              : 
   12595     34432277 :   if (DECL_P (*decl_p))
   12596              :     {
   12597     34410071 :       if (TREE_TYPE (*decl_p) == error_mark_node)
   12598              :         return false;
   12599     34410068 :       p = &DECL_ATTRIBUTES (*decl_p);
   12600              :       /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
   12601              :          to our attributes parameter.  */
   12602     34410068 :       gcc_assert (*p == attributes);
   12603              :     }
   12604        22206 :   else if (FUNC_OR_METHOD_TYPE_P (*decl_p)
   12605         2506 :            || (attr_flags & ATTR_FLAG_TYPE_IN_PLACE) == 0)
   12606              :     p = NULL;
   12607              :   else
   12608              :     {
   12609         2479 :       p = &TYPE_ATTRIBUTES (*decl_p);
   12610              :       /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
   12611              :          lookup_template_class_1, and should be preserved.  */
   12612         2479 :       gcc_assert (*p != attributes);
   12613         2956 :       while (*p)
   12614          477 :         p = &TREE_CHAIN (*p);
   12615              :     }
   12616              : 
   12617              :   /* save_template_attributes puts the dependent attributes at the beginning of
   12618              :      the list; find the non-dependent ones.  */
   12619     35510962 :   for (t = attributes; t; t = TREE_CHAIN (t))
   12620     35135329 :     if (!ATTR_IS_DEPENDENT (t))
   12621              :       break;
   12622     34432274 :   tree nondep = t;
   12623              : 
   12624              :   /* Apply any non-dependent attributes.  */
   12625     34432274 :   if (p)
   12626     34412547 :     *p = nondep;
   12627        19727 :   else if (nondep)
   12628        19665 :     *decl_p = cp_build_type_attribute_variant (*decl_p, nondep);
   12629              : 
   12630     34432274 :   if (nondep == attributes)
   12631              :     return true;
   12632              : 
   12633              :   /* And then any dependent ones.  */
   12634      1078611 :   tree late_attrs = NULL_TREE;
   12635      1078611 :   tree *q = &late_attrs;
   12636      2157275 :   for (t = attributes; t != nondep; t = TREE_CHAIN (t))
   12637              :     {
   12638      1078688 :       *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
   12639      1078688 :       if (*q == error_mark_node)
   12640              :         return false;
   12641      1078664 :       if (*q == t)
   12642              :         {
   12643       852040 :           *q = copy_node (t);
   12644       852040 :           TREE_CHAIN (*q) = NULL_TREE;
   12645              :         }
   12646      2157327 :       while (*q)
   12647      1078663 :         q = &TREE_CHAIN (*q);
   12648              :     }
   12649              : 
   12650              :   /* cplus_decl_attributes can add some attributes implicitly.  For templates,
   12651              :      those attributes should have been added already when those templates were
   12652              :      parsed, and shouldn't be added based on from which context they are
   12653              :      first time instantiated.  */
   12654      1078587 :   auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
   12655      1078587 :   auto o2 = make_temp_override (optimization_current_node,
   12656      1078587 :                                 optimization_default_node);
   12657      1078587 :   auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
   12658      1078587 :   auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
   12659      1078587 :                                 NULL);
   12660      1078587 :   auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
   12661      1078587 :   auto o6 = make_temp_override (target_option_current_node,
   12662      1078587 :                                 target_option_default_node);
   12663              : 
   12664      1078587 :   cplus_decl_attributes (decl_p, late_attrs, attr_flags);
   12665              : 
   12666      1078587 :   return true;
   12667      1078587 : }
   12668              : 
   12669              : /* The template TMPL is being instantiated with the template arguments TARGS.
   12670              :    Perform the access checks that we deferred when parsing the template.  */
   12671              : 
   12672              : static void
   12673     72699716 : perform_instantiation_time_access_checks (tree tmpl, tree targs)
   12674              : {
   12675     72699716 :   unsigned i;
   12676     72699716 :   deferred_access_check *chk;
   12677              : 
   12678     72699716 :   if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
   12679     72699716 :     return;
   12680              : 
   12681    145399432 :   if (vec<deferred_access_check, va_gc> *access_checks
   12682     72699716 :       = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
   12683          105 :     FOR_EACH_VEC_ELT (*access_checks, i, chk)
   12684              :       {
   12685           57 :         tree decl = chk->decl;
   12686           57 :         tree diag_decl = chk->diag_decl;
   12687           57 :         tree type_scope = TREE_TYPE (chk->binfo);
   12688              : 
   12689           57 :         if (uses_template_parms (type_scope))
   12690            6 :           type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
   12691              : 
   12692              :         /* Make access check error messages point to the location
   12693              :            of the use of the typedef.  */
   12694           57 :         iloc_sentinel ils (chk->loc);
   12695           57 :         perform_or_defer_access_check (TYPE_BINFO (type_scope),
   12696              :                                        decl, diag_decl, tf_warning_or_error);
   12697           57 :       }
   12698              : }
   12699              : 
   12700              : /* If the template T that we're about to instantiate contained errors at
   12701              :    parse time that we downgraded into warnings or suppressed, diagnose the
   12702              :    error now to render the TU ill-formed (if the TU has not already been
   12703              :    deemed ill-formed by an earlier error).  */
   12704              : 
   12705              : static void
   12706    122698675 : maybe_diagnose_erroneous_template (tree t)
   12707              : {
   12708    122698675 :   if (erroneous_templates && !(seen_error) ())
   12709         1050 :     if (location_t *error_loc = erroneous_templates->get (t))
   12710              :       {
   12711           15 :         auto_diagnostic_group d;
   12712           15 :         location_t decl_loc = location_of (t);
   12713           15 :         error_at (decl_loc, "instantiating erroneous template");
   12714           15 :         inform (*error_loc, "first error appeared here");
   12715           15 :       }
   12716    122698675 : }
   12717              : 
   12718              : tree
   12719   1031683955 : instantiate_class_template (tree type)
   12720              : {
   12721   1031683955 :   auto_timevar tv (TV_TEMPLATE_INST);
   12722              : 
   12723   1031683955 :   tree templ, args, pattern, t, member;
   12724   1031683955 :   tree typedecl;
   12725   1031683955 :   tree pbinfo;
   12726   1031683955 :   tree base_list;
   12727   1031683955 :   unsigned int saved_maximum_field_alignment;
   12728   1031683955 :   tree fn_context;
   12729              : 
   12730   1031683955 :   if (type == error_mark_node)
   12731              :     return error_mark_node;
   12732              : 
   12733   2044772933 :   if (COMPLETE_OR_OPEN_TYPE_P (type)
   12734   1088367170 :       || uses_template_parms (type))
   12735    983961242 :     return type;
   12736              : 
   12737              :   /* Figure out which template is being instantiated.  */
   12738     47722713 :   templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
   12739     47722713 :   gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
   12740              : 
   12741              :   /* Mark the type as in the process of being defined.  */
   12742     47722713 :   TYPE_BEING_DEFINED (type) = 1;
   12743              : 
   12744              :   /* We may be in the middle of deferred access check.  Disable
   12745              :      it now.  */
   12746     47722713 :   deferring_access_check_sentinel acs (dk_no_deferred);
   12747              : 
   12748              :   /* Determine what specialization of the original template to
   12749              :      instantiate.  */
   12750     47722713 :   t = most_specialized_partial_spec (type, tf_warning_or_error);
   12751     47722713 :   if (t == error_mark_node)
   12752              :     return error_mark_node;
   12753     47722698 :   else if (t)
   12754              :     {
   12755              :       /* This TYPE is actually an instantiation of a partial
   12756              :          specialization.  We replace the innermost set of ARGS with
   12757              :          the arguments appropriate for substitution.  For example,
   12758              :          given:
   12759              : 
   12760              :            template <class T> struct S {};
   12761              :            template <class T> struct S<T*> {};
   12762              : 
   12763              :          and supposing that we are instantiating S<int*>, ARGS will
   12764              :          presently be {int*} -- but we need {int}.  */
   12765      9995233 :       pattern = TREE_TYPE (TI_TEMPLATE (t));
   12766      9995233 :       args = TI_ARGS (t);
   12767              :     }
   12768              :   else
   12769              :     {
   12770     37727465 :       pattern = TREE_TYPE (templ);
   12771     37727465 :       args = CLASSTYPE_TI_ARGS (type);
   12772              :     }
   12773              : 
   12774              :   /* If the template we're instantiating is incomplete, then clearly
   12775              :      there's nothing we can do.  */
   12776     47722698 :   if (!COMPLETE_TYPE_P (pattern))
   12777              :     {
   12778              :       /* We can try again later.  */
   12779      3113505 :       TYPE_BEING_DEFINED (type) = 0;
   12780      3113505 :       return type;
   12781              :     }
   12782              : 
   12783              :   /* If we've recursively instantiated too many templates, stop.  */
   12784     44609193 :   if (! push_tinst_level (type))
   12785            0 :     return type;
   12786              : 
   12787     54604347 :   maybe_diagnose_erroneous_template (t ? TI_TEMPLATE (t) : templ);
   12788              : 
   12789     44609172 :   int saved_unevaluated_operand = cp_unevaluated_operand;
   12790     44609172 :   int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
   12791              : 
   12792     44609172 :   fn_context = decl_function_context (TYPE_MAIN_DECL (type));
   12793              :   /* Also avoid push_to_top_level for a lambda in an NSDMI.  */
   12794     88459873 :   if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
   12795            0 :     fn_context = error_mark_node;
   12796     44609172 :   if (!fn_context)
   12797     43952922 :     push_to_top_level ();
   12798              :   else
   12799              :     {
   12800       656250 :       cp_unevaluated_operand = 0;
   12801       656250 :       c_inhibit_evaluation_warnings = 0;
   12802              :     }
   12803              : 
   12804     44609172 :   mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
   12805              : 
   12806              :   /* Use #pragma pack from the template context.  */
   12807     44609172 :   saved_maximum_field_alignment = maximum_field_alignment;
   12808     44609172 :   maximum_field_alignment = TYPE_PRECISION (pattern);
   12809              : 
   12810     44609172 :   SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
   12811              : 
   12812              :   /* Set the input location to the most specialized template definition.
   12813              :      This is needed if tsubsting causes an error.  */
   12814     44609172 :   typedecl = TYPE_MAIN_DECL (pattern);
   12815     44609172 :   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
   12816     44609172 :     DECL_SOURCE_LOCATION (typedecl);
   12817              : 
   12818     44609172 :   set_instantiating_module (TYPE_NAME (type));
   12819              : 
   12820     44609172 :   TYPE_PACKED (type) = TYPE_PACKED (pattern);
   12821     44609172 :   SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
   12822     44609172 :   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
   12823     44609172 :   CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
   12824     44609172 :   if (ANON_AGGR_TYPE_P (pattern))
   12825        98299 :     SET_ANON_AGGR_TYPE_P (type);
   12826     44609172 :   if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
   12827              :     {
   12828     43728563 :       CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
   12829     43728563 :       CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
   12830              :       /* Adjust visibility for template arguments.  */
   12831     43728563 :       determine_visibility (TYPE_MAIN_DECL (type));
   12832              :     }
   12833     44609172 :   if (CLASS_TYPE_P (type))
   12834     44609172 :     CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
   12835              : 
   12836     44609172 :   pbinfo = TYPE_BINFO (pattern);
   12837              : 
   12838              :   /* We should never instantiate a nested class before its enclosing
   12839              :      class; we need to look up the nested class by name before we can
   12840              :      instantiate it, and that lookup should instantiate the enclosing
   12841              :      class.  */
   12842     44609172 :   gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
   12843              :               || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
   12844              : 
   12845              :   /* When instantiating nested lambdas, ensure that they get the mangling
   12846              :      scope of the new class type.  */
   12847     44609172 :   start_lambda_scope (TYPE_NAME (type));
   12848              : 
   12849     44609172 :   base_list = NULL_TREE;
   12850              :   /* Defer access checking while we substitute into the types named in
   12851              :      the base-clause.  */
   12852     44609172 :   push_deferring_access_checks (dk_deferred);
   12853     44609172 :   if (BINFO_N_BASE_BINFOS (pbinfo))
   12854              :     {
   12855              :       tree pbase_binfo;
   12856              :       int i;
   12857              : 
   12858              :       /* Substitute into each of the bases to determine the actual
   12859              :          basetypes.  */
   12860     46363813 :       for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
   12861              :         {
   12862     23436040 :           tree base;
   12863     23436040 :           tree access = BINFO_BASE_ACCESS (pbinfo, i);
   12864     23436040 :           tree annotations = NULL_TREE;
   12865     23436040 :           tree expanded_bases = NULL_TREE;
   12866     23436040 :           int idx, len = 1;
   12867              : 
   12868     23436040 :           if (i + BINFO_BASE_BINFOS (pbinfo)->length ()
   12869     23436040 :               < vec_safe_length (BINFO_BASE_ACCESSES (pbinfo)))
   12870            6 :             annotations
   12871            6 :               = BINFO_BASE_ACCESS (pbinfo,
   12872              :                                    i + BINFO_BASE_BINFOS (pbinfo)->length ());
   12873              : 
   12874     23436040 :           if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
   12875              :             {
   12876          483 :               expanded_bases
   12877          483 :                 = tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
   12878              :                                          args, tf_error, NULL_TREE);
   12879          483 :               if (expanded_bases == error_mark_node)
   12880            0 :                 continue;
   12881              : 
   12882          483 :               len = TREE_VEC_LENGTH (expanded_bases);
   12883              :             }
   12884              : 
   12885     23436040 :           if (annotations)
   12886            4 :             annotations = tsubst_attributes (annotations, args,
   12887              :                                              tf_warning_or_error, NULL_TREE);
   12888              : 
   12889     46872690 :           for (idx = 0; idx < len; idx++)
   12890              :             {
   12891     23436650 :               if (expanded_bases)
   12892              :                 /* Extract the already-expanded base class.  */
   12893         1093 :                 base = TREE_VEC_ELT (expanded_bases, idx);
   12894              :               else
   12895              :                 /* Substitute to figure out the base class.  */
   12896     23435557 :                 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
   12897              :                                NULL_TREE);
   12898              : 
   12899     23436650 :               if (base == error_mark_node)
   12900           32 :                 continue;
   12901              : 
   12902     23436618 :               tree acc = access;
   12903     23436618 :               if (annotations)
   12904              :                 /* Make sure each direct base from the pack has its unique
   12905              :                    set of annotations.  */
   12906            7 :                 acc = build_tree_list (access, idx == 0 ? annotations
   12907            2 :                                                : copy_list (annotations));
   12908     23436618 :               base_list = tree_cons (acc, base, base_list);
   12909     23436618 :               if (BINFO_VIRTUAL_P (pbase_binfo))
   12910        41344 :                 TREE_TYPE (base_list) = integer_type_node;
   12911              :             }
   12912              :         }
   12913              : 
   12914              :       /* The list is now in reverse order; correct that.  */
   12915     22927773 :       base_list = nreverse (base_list);
   12916              :     }
   12917              :   /* Now call xref_basetypes to set up all the base-class
   12918              :      information.  */
   12919     44609172 :   xref_basetypes (type, base_list);
   12920              : 
   12921     44609172 :   apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
   12922              :                                   (int) ATTR_FLAG_TYPE_IN_PLACE,
   12923              :                                   args, tf_error, NULL_TREE);
   12924     44609172 :   fixup_attribute_variants (type);
   12925              : 
   12926              :   /* Now that our base classes are set up, enter the scope of the
   12927              :      class, so that name lookups into base classes, etc. will work
   12928              :      correctly.  This is precisely analogous to what we do in
   12929              :      begin_class_definition when defining an ordinary non-template
   12930              :      class, except we also need to push the enclosing classes.  */
   12931     44609172 :   push_nested_class (type);
   12932              : 
   12933              :   /* Now check accessibility of the types named in its base-clause,
   12934              :      relative to the scope of the class.  */
   12935     44609172 :   pop_to_parent_deferring_access_checks ();
   12936              : 
   12937              :   /* A vector to hold members marked with attribute used. */
   12938     44609172 :   auto_vec<tree> used;
   12939              : 
   12940              :   /* Now members are processed in the order of declaration.  */
   12941     44609172 :   for (member = CLASSTYPE_DECL_LIST (pattern);
   12942    275567837 :        member; member = TREE_CHAIN (member))
   12943              :     {
   12944    230961416 :       tree t = TREE_VALUE (member);
   12945              : 
   12946    230961416 :       if (TREE_PURPOSE (member))
   12947              :         {
   12948    227168077 :           if (TYPE_P (t))
   12949              :             {
   12950      3803584 :               if (LAMBDA_TYPE_P (t))
   12951              :                 /* A closure type for a lambda in an NSDMI or default argument.
   12952              :                    Ignore it; it will be regenerated when needed.  */
   12953         4264 :                 continue;
   12954              : 
   12955              :               /* If the member is a class template, we've
   12956              :                  already substituted its type.  */
   12957      1733045 :               if (CLASS_TYPE_P (t)
   12958      3895833 :                   && CLASSTYPE_IS_TEMPLATE (t))
   12959       755606 :                 continue;
   12960              : 
   12961      1407182 :               tree newtag = tsubst (t, args, tf_error, NULL_TREE);
   12962      1407182 :               if (newtag == error_mark_node)
   12963            0 :                 continue;
   12964              : 
   12965      1407182 :               if (TREE_CODE (newtag) != ENUMERAL_TYPE)
   12966              :                 {
   12967       977439 :                   tree name = TYPE_IDENTIFIER (t);
   12968              : 
   12969              :                   /* Now, install the tag.  We don't use pushtag
   12970              :                      because that does too much work -- creating an
   12971              :                      implicit typedef, which we've already done.  */
   12972       977439 :                   set_identifier_type_value (name, TYPE_NAME (newtag));
   12973       977439 :                   maybe_add_class_template_decl_list (type, newtag, false);
   12974       977439 :                   TREE_PUBLIC (TYPE_NAME (newtag)) = true;
   12975       977439 :                   determine_visibility (TYPE_NAME (newtag));
   12976              :                 }
   12977              :             }
   12978    225001025 :           else if (DECL_DECLARES_FUNCTION_P (t))
   12979              :             {
   12980    111081373 :               tree r;
   12981              : 
   12982    111081373 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12983     21798713 :                 ++processing_template_decl;
   12984    111081373 :               r = tsubst (t, args, tf_error, NULL_TREE);
   12985    111081373 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12986     21798713 :                 --processing_template_decl;
   12987              : 
   12988    111081373 :               set_current_access_from_decl (r);
   12989    111081373 :               finish_member_declaration (r);
   12990              :               /* Instantiate members marked with attribute used.  */
   12991    111081373 :               if (r != error_mark_node && DECL_PRESERVE_P (r))
   12992           24 :                 used.safe_push (r);
   12993    111081373 :               if (TREE_CODE (r) == FUNCTION_DECL
   12994    111081373 :                   && DECL_OMP_DECLARE_REDUCTION_P (r))
   12995          134 :                 cp_check_omp_declare_reduction (r);
   12996              :             }
   12997    113919652 :           else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
   12998    117723283 :                    && LAMBDA_TYPE_P (TREE_TYPE (t)))
   12999              :             /* A closure type for a lambda in an NSDMI or default argument.
   13000              :                Ignore it; it will be regenerated when needed.  */;
   13001              :           else
   13002              :             {
   13003              :               /* Build new TYPE_FIELDS.  */
   13004    113915384 :               if (TREE_CODE (t) == STATIC_ASSERT)
   13005      5246346 :                 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE);
   13006    108669038 :               else if (TREE_CODE (t) != CONST_DECL)
   13007              :                 {
   13008    108669038 :                   tree r;
   13009    108669038 :                   tree vec = NULL_TREE;
   13010    108669038 :                   int len = 1;
   13011              : 
   13012    108669038 :                   gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
   13013              :                   /* The file and line for this declaration, to
   13014              :                      assist in error message reporting.  Since we
   13015              :                      called push_tinst_level above, we don't need to
   13016              :                      restore these.  */
   13017    108669038 :                   input_location = DECL_SOURCE_LOCATION (t);
   13018              : 
   13019    108669038 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   13020      3223332 :                     ++processing_template_decl;
   13021    108669038 :                   r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
   13022    108668987 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   13023      3223332 :                     --processing_template_decl;
   13024              : 
   13025    108668987 :                   if (TREE_CODE (r) == TREE_VEC)
   13026              :                     {
   13027              :                       /* A capture pack became multiple fields.  */
   13028          268 :                       vec = r;
   13029          268 :                       len = TREE_VEC_LENGTH (vec);
   13030              :                     }
   13031              : 
   13032    217335678 :                   for (int i = 0; i < len; ++i)
   13033              :                     {
   13034    108669391 :                       if (vec)
   13035          672 :                         r = TREE_VEC_ELT (vec, i);
   13036    108669391 :                       if (VAR_P (r))
   13037              :                         {
   13038              :                           /* In [temp.inst]:
   13039              : 
   13040              :                              [t]he initialization (and any associated
   13041              :                              side-effects) of a static data member does
   13042              :                              not occur unless the static data member is
   13043              :                              itself used in a way that requires the
   13044              :                              definition of the static data member to
   13045              :                              exist.
   13046              : 
   13047              :                              Therefore, we do not substitute into the
   13048              :                              initialized for the static data member here.  */
   13049      3864841 :                           finish_static_data_member_decl
   13050      3864841 :                             (r,
   13051              :                              /*init=*/NULL_TREE,
   13052              :                              /*init_const_expr_p=*/false,
   13053              :                              /*asmspec_tree=*/NULL_TREE,
   13054              :                              /*flags=*/0);
   13055              :                           /* Instantiate members marked with attribute used. */
   13056      3864841 :                           if (r != error_mark_node && DECL_PRESERVE_P (r))
   13057            6 :                             used.safe_push (r);
   13058              :                         }
   13059    104804550 :                       else if (TREE_CODE (r) == FIELD_DECL)
   13060              :                         {
   13061              :                           /* Determine whether R has a valid type and can be
   13062              :                              completed later.  If R is invalid, then its type
   13063              :                              is replaced by error_mark_node.  */
   13064      8843673 :                           tree rtype = TREE_TYPE (r);
   13065      8843673 :                           if (can_complete_type_without_circularity (rtype))
   13066      8843664 :                             complete_type (rtype);
   13067              : 
   13068      8840973 :                           if (!complete_or_array_type_p (rtype))
   13069              :                             {
   13070              :                               /* If R's type couldn't be completed and
   13071              :                                  it isn't a flexible array member (whose
   13072              :                                  type is incomplete by definition) give
   13073              :                                  an error.  */
   13074           29 :                               cxx_incomplete_type_error (r, rtype);
   13075           29 :                               TREE_TYPE (r) = error_mark_node;
   13076              :                             }
   13077      8840944 :                           else if (TREE_CODE (rtype) == ARRAY_TYPE
   13078       260610 :                                    && TYPE_DOMAIN (rtype) == NULL_TREE
   13079      8840944 :                                    && (TREE_CODE (type) == UNION_TYPE
   13080           48 :                                        || TREE_CODE (type) == QUAL_UNION_TYPE))
   13081              :                             {
   13082            4 :                               error ("flexible array member %qD in union", r);
   13083            4 :                               TREE_TYPE (r) = error_mark_node;
   13084              :                             }
   13085      8840940 :                           else if (!verify_type_context (input_location,
   13086              :                                                          TCTX_FIELD, rtype))
   13087            0 :                             TREE_TYPE (r) = error_mark_node;
   13088              :                         }
   13089              : 
   13090              :                       /* If it is a TYPE_DECL for a class-scoped
   13091              :                          ENUMERAL_TYPE, such a thing will already have
   13092              :                          been added to the field list by tsubst_enum
   13093              :                          in finish_member_declaration case above.  */
   13094    108666691 :                       if (!(TREE_CODE (r) == TYPE_DECL
   13095     91078022 :                             && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
   13096       646585 :                             && DECL_ARTIFICIAL (r)))
   13097              :                         {
   13098    108236948 :                           set_current_access_from_decl (r);
   13099    108236948 :                           finish_member_declaration (r);
   13100              :                         }
   13101              :                     }
   13102              :                 }
   13103              :             }
   13104              :         }
   13105              :       else
   13106              :         {
   13107      3793339 :           if (TREE_CODE (t) == TU_LOCAL_ENTITY)
   13108              :             /* Ignore.  */;
   13109      3279205 :           else if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
   13110      6469909 :                    || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   13111              :             {
   13112              :               /* Build new CLASSTYPE_FRIEND_CLASSES.  */
   13113              : 
   13114      1116688 :               tree friend_type = t;
   13115      1116688 :               if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   13116              :                 {
   13117              :                   /* template <class T> friend class C;  */
   13118       602596 :                   friend_type = tsubst_friend_class (friend_type, args);
   13119              :                 }
   13120       514092 :               else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
   13121              :                 {
   13122              :                   /* template <class T> friend class C::D;  */
   13123           15 :                   friend_type = tsubst (friend_type, args,
   13124              :                                         tf_warning_or_error, NULL_TREE);
   13125           15 :                   if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   13126           12 :                     friend_type = TREE_TYPE (friend_type);
   13127              :                 }
   13128       514077 :               else if (TREE_CODE (friend_type) == TYPENAME_TYPE
   13129       514077 :                        || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
   13130              :                 {
   13131              :                   /* This could be either
   13132              : 
   13133              :                        friend class T::C;
   13134              : 
   13135              :                      when dependent_type_p is false or
   13136              : 
   13137              :                        template <class U> friend class T::C;
   13138              : 
   13139              :                      otherwise.  */
   13140              :                   /* Bump processing_template_decl in case this is something like
   13141              :                      template <class T> friend struct A<T>::B.  */
   13142         2172 :                   ++processing_template_decl;
   13143         2172 :                   friend_type = tsubst (friend_type, args,
   13144              :                                         tf_warning_or_error, NULL_TREE);
   13145         2172 :                   --processing_template_decl;
   13146              :                 }
   13147       511905 :               else if (PACK_EXPANSION_P (friend_type))
   13148              :                 {
   13149           24 :                   friend_type = tsubst_pack_expansion (friend_type, args,
   13150              :                                                        tf_warning_or_error,
   13151              :                                                        NULL_TREE);
   13152           24 :                   if (friend_type != error_mark_node)
   13153              :                     {
   13154           24 :                       unsigned int len = TREE_VEC_LENGTH (friend_type);
   13155           63 :                       for (unsigned int idx = 0; idx < len; ++idx)
   13156           39 :                         if (TREE_VEC_ELT (friend_type, idx) != error_mark_node)
   13157           39 :                           make_friend_class (type,
   13158           39 :                                              TREE_VEC_ELT (friend_type, idx),
   13159              :                                              /*complain=*/false);
   13160              :                     }
   13161           24 :                   friend_type = error_mark_node;
   13162              :                 }
   13163       511881 :               else if (uses_template_parms (friend_type))
   13164              :                 /* friend class C<T>;  */
   13165       473541 :                 friend_type = tsubst (friend_type, args,
   13166              :                                       tf_warning_or_error, NULL_TREE);
   13167              : 
   13168              :               /* Otherwise it's
   13169              : 
   13170              :                    friend class C;
   13171              : 
   13172              :                  where C is already declared or
   13173              : 
   13174              :                    friend class C<int>;
   13175              : 
   13176              :                  We don't have to do anything in these cases.  */
   13177              : 
   13178      1116688 :               if (friend_type != error_mark_node)
   13179      1116644 :                 make_friend_class (type, friend_type, /*complain=*/false);
   13180              :             }
   13181              :           else
   13182              :             {
   13183              :               /* Build new DECL_FRIENDLIST.  */
   13184      2676609 :               tree r;
   13185              : 
   13186              :               /* The file and line for this declaration, to
   13187              :                  assist in error message reporting.  Since we
   13188              :                  called push_tinst_level above, we don't need to
   13189              :                  restore these.  */
   13190      2676609 :               input_location = DECL_SOURCE_LOCATION (t);
   13191              : 
   13192      2676609 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13193              :                 {
   13194      1705879 :                   ++processing_template_decl;
   13195      1705879 :                   push_deferring_access_checks (dk_no_check);
   13196              :                 }
   13197              : 
   13198      2676609 :               r = tsubst_friend_function (t, args);
   13199      2676609 :               add_friend (type, r, /*complain=*/false);
   13200      2676609 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13201              :                 {
   13202      1705879 :                   pop_deferring_access_checks ();
   13203      1705879 :                   --processing_template_decl;
   13204              :                 }
   13205              :             }
   13206              :         }
   13207              :     }
   13208              : 
   13209     44606421 :   if (fn_context)
   13210              :     {
   13211              :       /* Restore these before substituting into the lambda capture
   13212              :          initializers.  */
   13213       656250 :       cp_unevaluated_operand = saved_unevaluated_operand;
   13214       656250 :       c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
   13215              :     }
   13216              : 
   13217              :   /* Set the file and line number information to whatever is given for
   13218              :      the class itself.  This puts error messages involving generated
   13219              :      implicit functions at a predictable point, and the same point
   13220              :      that would be used for non-template classes.  */
   13221     44606421 :   input_location = DECL_SOURCE_LOCATION (typedecl);
   13222              : 
   13223     44606421 :   unreverse_member_declarations (type);
   13224     44606421 :   finish_struct_1 (type);
   13225     44606421 :   TYPE_BEING_DEFINED (type) = 0;
   13226              : 
   13227     44606421 :   finish_lambda_scope ();
   13228              : 
   13229              :   /* Remember if instantiating this class ran into errors, so we can avoid
   13230              :      instantiating member functions in limit_bad_template_recursion.  We set
   13231              :      this flag even if the problem was in another instantiation triggered by
   13232              :      this one, as that will likely also cause trouble for member functions.  */
   13233     44606421 :   if (errorcount + sorrycount > current_tinst_level->errors)
   13234         1299 :     CLASSTYPE_ERRONEOUS (type) = true;
   13235              : 
   13236              :   /* We don't instantiate default arguments for member functions.  14.7.1:
   13237              : 
   13238              :      The implicit instantiation of a class template specialization causes
   13239              :      the implicit instantiation of the declarations, but not of the
   13240              :      definitions or default arguments, of the class member functions,
   13241              :      member classes, static data members and member templates....  */
   13242              : 
   13243     44606421 :   perform_instantiation_time_access_checks (pattern, args);
   13244     44606421 :   perform_deferred_access_checks (tf_warning_or_error);
   13245              : 
   13246              :   /* Now that we've gone through all the members, instantiate those
   13247              :      marked with attribute used.  We must do this in the context of
   13248              :      the class -- not the context we pushed from, as that might be
   13249              :      inside a template and change the behaviour of mark_used.  */
   13250     44606505 :   for (tree x : used)
   13251           30 :     mark_used (x);
   13252              : 
   13253     44606421 :   pop_nested_class ();
   13254     44606421 :   maximum_field_alignment = saved_maximum_field_alignment;
   13255     44606421 :   if (!fn_context)
   13256     43950171 :     pop_from_top_level ();
   13257     44606421 :   pop_tinst_level ();
   13258              : 
   13259              :   /* The vtable for a template class can be emitted in any translation
   13260              :      unit in which the class is instantiated.  When there is no key
   13261              :      method, however, finish_struct_1 will already have added TYPE to
   13262              :      the keyed_classes.  */
   13263     44606421 :   if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
   13264            0 :     vec_safe_push (keyed_classes, type);
   13265              : 
   13266     44606421 :   return type;
   13267   1076287604 : }
   13268              : 
   13269              : tree
   13270   3627084760 : tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13271              : {
   13272   3627084760 :   tree r;
   13273              : 
   13274   3627084760 :   if (!t)
   13275              :     r = t;
   13276   3590719925 :   else if (TYPE_P (t))
   13277   3267813705 :     r = tsubst (t, args, complain, in_decl);
   13278              :   else
   13279              :     {
   13280    322906220 :       if (!(complain & tf_warning))
   13281    210527377 :         ++c_inhibit_evaluation_warnings;
   13282    322906220 :       r = tsubst_expr (t, args, complain, in_decl);
   13283    322906184 :       if (!(complain & tf_warning))
   13284    210527347 :         --c_inhibit_evaluation_warnings;
   13285              :     }
   13286              : 
   13287   3627084724 :   return r;
   13288              : }
   13289              : 
   13290              : /* Given a function parameter pack TMPL_PARM and some function parameters
   13291              :    instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
   13292              :    and set *SPEC_P to point at the next point in the list.  */
   13293              : 
   13294              : tree
   13295       704232 : extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
   13296              : {
   13297              :   /* Collect all of the extra "packed" parameters into an
   13298              :      argument pack.  */
   13299       704232 :   tree argpack;
   13300       704232 :   tree spec_parm = *spec_p;
   13301       704232 :   int len;
   13302              : 
   13303      1225343 :   for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
   13304       521480 :     if (tmpl_parm
   13305       521480 :         && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
   13306              :       break;
   13307              : 
   13308       704232 :   spec_parm = *spec_p;
   13309       704232 :   if (len == 1 && DECL_PACK_P (spec_parm))
   13310              :     {
   13311              :       /* The instantiation is still a parameter pack; don't wrap it in a
   13312              :          NONTYPE_ARGUMENT_PACK.  */
   13313         2779 :       argpack = spec_parm;
   13314         2779 :       spec_parm = DECL_CHAIN (spec_parm);
   13315              :     }
   13316              :   else
   13317              :     {
   13318              :       /* Fill in PARMVEC with all of the parameters.  */
   13319       701453 :       tree parmvec = make_tree_vec (len);
   13320       701453 :       argpack = make_node (NONTYPE_ARGUMENT_PACK);
   13321      1219785 :       for (int i = 0; i < len; i++)
   13322              :         {
   13323       518332 :           tree elt = spec_parm;
   13324       518332 :           if (DECL_PACK_P (elt))
   13325            0 :             elt = make_pack_expansion (elt);
   13326       518332 :           TREE_VEC_ELT (parmvec, i) = elt;
   13327       518332 :           spec_parm = DECL_CHAIN (spec_parm);
   13328              :         }
   13329              : 
   13330              :       /* Build the argument packs.  */
   13331       701453 :       ARGUMENT_PACK_ARGS (argpack) = parmvec;
   13332              :     }
   13333       704232 :   *spec_p = spec_parm;
   13334              : 
   13335       704232 :   return argpack;
   13336              : }
   13337              : 
   13338              : /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
   13339              :    NONTYPE_ARGUMENT_PACK.  */
   13340              : 
   13341              : static tree
   13342         1003 : make_fnparm_pack (tree spec_parm)
   13343              : {
   13344            0 :   return extract_fnparm_pack (NULL_TREE, &spec_parm);
   13345              : }
   13346              : 
   13347              : /* Return 1 if the Ith element of the argument pack ARG_PACK is a
   13348              :    pack expansion with no extra args, 2 if it has extra args, or 0
   13349              :    if it is not a pack expansion.  */
   13350              : 
   13351              : static int
   13352     23261713 : argument_pack_element_is_expansion_p (tree arg_pack, int i)
   13353              : {
   13354     23261713 :   if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   13355              :     /* We're being called before this happens in tsubst_pack_expansion.  */
   13356            0 :     arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   13357     23261713 :   tree vec = ARGUMENT_PACK_ARGS (arg_pack);
   13358     23261713 :   if (i >= TREE_VEC_LENGTH (vec))
   13359              :     return 0;
   13360     23261713 :   tree elt = TREE_VEC_ELT (vec, i);
   13361     23261713 :   if (DECL_P (elt))
   13362              :     /* A decl pack is itself an expansion.  */
   13363      1126755 :     elt = TREE_TYPE (elt);
   13364     23261713 :   if (!PACK_EXPANSION_P (elt))
   13365              :     return 0;
   13366       410190 :   if (PACK_EXPANSION_EXTRA_ARGS (elt))
   13367            3 :     return 2;
   13368              :   return 1;
   13369              : }
   13370              : 
   13371              : 
   13372              : /* Creates and return an ARGUMENT_PACK_SELECT tree node.  */
   13373              : 
   13374              : static tree
   13375      6472467 : make_argument_pack_select (tree arg_pack, unsigned index)
   13376              : {
   13377      6472467 :   tree aps = make_node (ARGUMENT_PACK_SELECT);
   13378              : 
   13379      6472467 :   ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
   13380      6472467 :   ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   13381              : 
   13382      6472467 :   return aps;
   13383              : }
   13384              : 
   13385              : /*  This is a subroutine of tsubst_pack_expansion.
   13386              : 
   13387              :     It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
   13388              :     mechanism to store the (non complete list of) arguments of the
   13389              :     substitution and return a non substituted pack expansion, in order
   13390              :     to wait for when we have enough arguments to really perform the
   13391              :     substitution.  */
   13392              : 
   13393              : static bool
   13394     23715587 : use_pack_expansion_extra_args_p (tree t,
   13395              :                                  tree parm_packs,
   13396              :                                  int arg_pack_len,
   13397              :                                  bool has_empty_arg)
   13398              : {
   13399     23715587 :   if (has_empty_arg
   13400     23715587 :       && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
   13401              :     return true;
   13402              : 
   13403              :   /* If one pack has an expansion and another pack has a normal
   13404              :      argument or if one pack has an empty argument and an another
   13405              :      one hasn't then tsubst_pack_expansion cannot perform the
   13406              :      substitution and need to fall back on the
   13407              :      PACK_EXPANSION_EXTRA mechanism.  */
   13408     23715576 :   if (parm_packs == NULL_TREE)
   13409              :     return false;
   13410      7100645 :   else if (has_empty_arg)
   13411              :     {
   13412              :       /* If all the actual packs are pack expansions, we can still
   13413              :          subsitute directly.  */
   13414          782 :       for (tree p = parm_packs; p; p = TREE_CHAIN (p))
   13415              :         {
   13416          782 :           tree a = TREE_VALUE (p);
   13417          782 :           if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
   13418            0 :             a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
   13419          782 :           a = ARGUMENT_PACK_ARGS (a);
   13420          782 :           if (TREE_VEC_LENGTH (a) == 1)
   13421          169 :             a = TREE_VEC_ELT (a, 0);
   13422          782 :           if (PACK_EXPANSION_P (a))
   13423            0 :             continue;
   13424              :           return true;
   13425              :         }
   13426              :       return false;
   13427              :     }
   13428              : 
   13429     18215471 :   for (int i = 0 ; i < arg_pack_len; ++i)
   13430              :     {
   13431              :       bool has_expansion_arg = false;
   13432              :       bool has_non_expansion_arg = false;
   13433     12003167 :       for (tree parm_pack = parm_packs;
   13434     23118787 :            parm_pack;
   13435     12003167 :            parm_pack = TREE_CHAIN (parm_pack))
   13436              :         {
   13437     12003170 :           tree arg = TREE_VALUE (parm_pack);
   13438              : 
   13439     12003170 :           int exp = argument_pack_element_is_expansion_p (arg, i);
   13440     12003170 :           if (exp == 2)
   13441              :             /* We can't substitute a pack expansion with extra args into
   13442              :                our pattern.  */
   13443              :             return true;
   13444     12003167 :           else if (exp)
   13445              :             has_expansion_arg = true;
   13446              :           else
   13447     11798069 :             has_non_expansion_arg = true;
   13448              :         }
   13449              : 
   13450     11115617 :       if (has_expansion_arg && has_non_expansion_arg)
   13451              :         {
   13452              :           /* We can get here with:
   13453              : 
   13454              :               template <class... Ts> struct X {
   13455              :                 template <class... Us> using Y = Z<void(Ts, Us)...>;
   13456              :               };
   13457              :               template <class A, class... P>
   13458              :               using foo = X<int, int>::Y<A, P...>;
   13459              : 
   13460              :              where we compare int and A and then the second int and P...,
   13461              :              whose expansion-ness doesn't match, but that's OK.  */
   13462              :           return true;
   13463              :         }
   13464              :     }
   13465              :   return false;
   13466              : }
   13467              : 
   13468              : /* [temp.variadic]/6 says that:
   13469              : 
   13470              :        The instantiation of a pack expansion [...]
   13471              :        produces a list E1,E2, ..., En, where N is the number of elements
   13472              :        in the pack expansion parameters.
   13473              : 
   13474              :    This subroutine of tsubst_pack_expansion produces one of these Ei.
   13475              : 
   13476              :    PATTERN is the pattern of the pack expansion.  PARM_PACKS is a
   13477              :    TREE_LIST in which each TREE_PURPOSE is a parameter pack of
   13478              :    PATTERN, and each TREE_VALUE is its corresponding argument pack.
   13479              :    INDEX is the index 'i' of the element Ei to produce.  ARGS,
   13480              :    COMPLAIN, and IN_DECL are the same parameters as for the
   13481              :    tsubst_pack_expansion function.
   13482              : 
   13483              :    The function returns the resulting Ei upon successful completion,
   13484              :    or error_mark_node.
   13485              : 
   13486              :    Note that this function possibly modifies the ARGS parameter, so
   13487              :    it's the responsibility of the caller to restore it.  */
   13488              : 
   13489              : static tree
   13490     10371008 : gen_elem_of_pack_expansion_instantiation (tree pattern,
   13491              :                                           tree parm_packs,
   13492              :                                           unsigned index,
   13493              :                                           tree args /* This parm gets
   13494              :                                                        modified.  */,
   13495              :                                           tsubst_flags_t complain,
   13496              :                                           tree in_decl)
   13497              : {
   13498     10371008 :   tree t;
   13499     10371008 :   bool ith_elem_is_expansion = false;
   13500              : 
   13501              :   /* For each parameter pack, change the substitution of the parameter
   13502              :      pack to the ith argument in its argument pack, then expand the
   13503              :      pattern.  */
   13504     21629551 :   for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
   13505              :     {
   13506     11258543 :       tree parm = TREE_PURPOSE (pack);
   13507     11258543 :       tree arg_pack = TREE_VALUE (pack);
   13508     11258543 :       tree aps;                 /* instance of ARGUMENT_PACK_SELECT.  */
   13509              : 
   13510     22517086 :       ith_elem_is_expansion |=
   13511     11258543 :         argument_pack_element_is_expansion_p (arg_pack, index);
   13512              : 
   13513              :       /* Select the Ith argument from the pack.  */
   13514     11258543 :       if (TREE_CODE (parm) == PARM_DECL
   13515     10714555 :           || VAR_P (parm)
   13516     10695339 :           || TREE_CODE (parm) == FIELD_DECL)
   13517              :         {
   13518       563204 :           if (index == 0)
   13519              :             {
   13520       416754 :               aps = make_argument_pack_select (arg_pack, index);
   13521       416754 :               if (!mark_used (parm, complain) && !(complain & tf_error))
   13522            0 :                 return error_mark_node;
   13523       416754 :               register_local_specialization (aps, parm);
   13524              :             }
   13525              :           else
   13526       146450 :             aps = retrieve_local_specialization (parm);
   13527              :         }
   13528              :       else
   13529              :         {
   13530     10695339 :           int idx, level;
   13531     10695339 :           template_parm_level_and_index (parm, &level, &idx);
   13532              : 
   13533     10695339 :           if (index == 0)
   13534              :             {
   13535      6055713 :               aps = make_argument_pack_select (arg_pack, index);
   13536              :               /* Update the corresponding argument.  */
   13537     12111426 :               TMPL_ARG (args, level, idx) = aps;
   13538              :             }
   13539              :           else
   13540              :             /* Re-use the ARGUMENT_PACK_SELECT.  */
   13541      9279252 :             aps = TMPL_ARG (args, level, idx);
   13542              :         }
   13543     11258543 :       ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   13544              :     }
   13545              : 
   13546              :   /* Substitute into the PATTERN with the (possibly altered)
   13547              :      arguments.  */
   13548     10371008 :   if (pattern == in_decl)
   13549              :     /* Expanding a fixed parameter pack from
   13550              :        coerce_template_parameter_pack.  */
   13551          115 :     t = tsubst_decl (pattern, args, complain);
   13552     10370893 :   else if (pattern == error_mark_node)
   13553              :     t = error_mark_node;
   13554     10370890 :   else if (!TYPE_P (pattern))
   13555      1818140 :     t = tsubst_expr (pattern, args, complain, in_decl);
   13556              :   else
   13557              :     {
   13558      8552750 :       t = tsubst (pattern, args, complain, in_decl);
   13559      8552750 :       if (is_auto (t) && !ith_elem_is_expansion)
   13560              :         /* When expanding the fake auto... pack expansion from add_capture, we
   13561              :            need to mark that the expansion is no longer a pack.  */
   13562          163 :         TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
   13563              :     }
   13564              : 
   13565              :   /*  If the Ith argument pack element is a pack expansion, then
   13566              :       the Ith element resulting from the substituting is going to
   13567              :       be a pack expansion as well.  */
   13568     10371008 :   if (ith_elem_is_expansion)
   13569       181522 :     t = make_pack_expansion (t, complain);
   13570              : 
   13571              :   return t;
   13572              : }
   13573              : 
   13574              : /* When the unexpanded parameter pack in a fold expression expands to an empty
   13575              :    sequence, the value of the expression is as follows; the program is
   13576              :    ill-formed if the operator is not listed in this table.
   13577              : 
   13578              :    &&   true
   13579              :    ||   false
   13580              :    ,    void()  */
   13581              : 
   13582              : tree
   13583        41281 : expand_empty_fold (tree t, tsubst_flags_t complain)
   13584              : {
   13585        41281 :   tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
   13586        41281 :   if (!FOLD_EXPR_MODIFY_P (t))
   13587        41248 :     switch (code)
   13588              :       {
   13589        39306 :       case TRUTH_ANDIF_EXPR:
   13590        39306 :         return boolean_true_node;
   13591           58 :       case TRUTH_ORIF_EXPR:
   13592           58 :         return boolean_false_node;
   13593         1830 :       case COMPOUND_EXPR:
   13594         1830 :         return void_node;
   13595              :       default:
   13596              :         break;
   13597              :       }
   13598              : 
   13599           87 :   if (complain & tf_error)
   13600           87 :     error_at (location_of (t),
   13601              :               "fold of empty expansion over %O", code);
   13602           87 :   return error_mark_node;
   13603              : }
   13604              : 
   13605              : /* Given a fold-expression T and a current LEFT and RIGHT operand,
   13606              :    form an expression that combines the two terms using the
   13607              :    operator of T. */
   13608              : 
   13609              : static tree
   13610       495444 : fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
   13611              : {
   13612       495444 :   tree_code code = FOLD_EXPR_OP (t);
   13613              : 
   13614       495444 :   tree lookups = templated_operator_saved_lookups (t);
   13615              : 
   13616              :   // Handle compound assignment operators.
   13617       495444 :   if (FOLD_EXPR_MODIFY_P (t))
   13618          911 :     return build_x_modify_expr (input_location, left, code, right,
   13619              :                                 lookups, complain);
   13620              : 
   13621       494533 :   warning_sentinel s(warn_parentheses);
   13622       494533 :   switch (code)
   13623              :     {
   13624         3380 :     case COMPOUND_EXPR:
   13625         3380 :       return build_x_compound_expr (input_location, left, right,
   13626         3380 :                                     lookups, complain);
   13627       491153 :     default:
   13628       491153 :       return build_x_binary_op (input_location, code,
   13629       491153 :                                 left, TREE_CODE (left),
   13630       491153 :                                 right, TREE_CODE (right),
   13631              :                                 lookups, /*overload=*/NULL,
   13632              :                                 complain);
   13633              :     }
   13634       494533 : }
   13635              : 
   13636              : /* Substitute ARGS into the pack of a fold expression T. */
   13637              : 
   13638              : static inline tree
   13639       397920 : tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13640              : {
   13641       397920 :   return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
   13642              : }
   13643              : 
   13644              : /* Substitute ARGS into the pack of a fold expression T. */
   13645              : 
   13646              : static inline tree
   13647         7427 : tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13648              : {
   13649         7427 :   return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
   13650              : }
   13651              : 
   13652              : /* Expand a PACK of arguments into a grouped as left fold.
   13653              :    Given a pack containing elements A0, A1, ..., An and an
   13654              :    operator @, this builds the expression:
   13655              : 
   13656              :       ((A0 @ A1) @ A2) ... @ An
   13657              : 
   13658              :    Note that PACK must not be empty.
   13659              : 
   13660              :    The operator is defined by the original fold expression T. */
   13661              : 
   13662              : static tree
   13663        10029 : expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
   13664              : {
   13665        10029 :   tree left = TREE_VEC_ELT (pack, 0);
   13666        24964 :   for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
   13667              :     {
   13668        14935 :       tree right = TREE_VEC_ELT (pack, i);
   13669        14935 :       left = fold_expression (t, left, right, complain);
   13670              :     }
   13671        10029 :   return left;
   13672              : }
   13673              : 
   13674              : /* Substitute into a unary left fold expression. */
   13675              : 
   13676              : static tree
   13677         4859 : tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13678              :                         tree in_decl)
   13679              : {
   13680         4859 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13681         4859 :   if (pack == error_mark_node)
   13682              :     return error_mark_node;
   13683         4859 :   if (PACK_EXPANSION_P (pack))
   13684              :     {
   13685         1006 :       tree r = copy_node (t);
   13686         1006 :       FOLD_EXPR_PACK (r) = pack;
   13687         1006 :       return r;
   13688              :     }
   13689         3853 :   if (TREE_VEC_LENGTH (pack) == 0)
   13690          176 :     return expand_empty_fold (t, complain);
   13691              :   else
   13692         3677 :     return expand_left_fold (t, pack, complain);
   13693              : }
   13694              : 
   13695              : /* Substitute into a binary left fold expression.
   13696              : 
   13697              :    Do ths by building a single (non-empty) vector of argumnts and
   13698              :    building the expression from those elements. */
   13699              : 
   13700              : static tree
   13701         6499 : tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13702              :                          tree in_decl)
   13703              : {
   13704         6499 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13705         6499 :   if (pack == error_mark_node)
   13706              :     return error_mark_node;
   13707         6499 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13708         6499 :   if (init == error_mark_node)
   13709              :     return error_mark_node;
   13710              : 
   13711         6499 :   if (PACK_EXPANSION_P (pack))
   13712              :     {
   13713          147 :       tree r = copy_node (t);
   13714          147 :       FOLD_EXPR_PACK (r) = pack;
   13715          147 :       FOLD_EXPR_INIT (r) = init;
   13716          147 :       return r;
   13717              :     }
   13718              : 
   13719         6352 :   tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
   13720         6352 :   TREE_VEC_ELT (vec, 0) = init;
   13721        11928 :   for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
   13722         5576 :     TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
   13723              : 
   13724         6352 :   return expand_left_fold (t, vec, complain);
   13725              : }
   13726              : 
   13727              : /* Expand a PACK of arguments into a grouped as right fold.
   13728              :    Given a pack containing elementns A0, A1, ..., and an
   13729              :    operator @, this builds the expression:
   13730              : 
   13731              :       A0@ ... (An-2 @ (An-1 @ An))
   13732              : 
   13733              :    Note that PACK must not be empty.
   13734              : 
   13735              :    The operator is defined by the original fold expression T. */
   13736              : 
   13737              : tree
   13738       338782 : expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
   13739              : {
   13740              :   // Build the expression.
   13741       338782 :   int n = TREE_VEC_LENGTH (pack);
   13742       338782 :   tree right = TREE_VEC_ELT (pack, n - 1);
   13743       819291 :   for (--n; n != 0; --n)
   13744              :     {
   13745       480509 :       tree left = TREE_VEC_ELT (pack, n - 1);
   13746       480509 :       right = fold_expression (t, left, right, complain);
   13747              :     }
   13748       338782 :   return right;
   13749              : }
   13750              : 
   13751              : /* Substitute into a unary right fold expression. */
   13752              : 
   13753              : static tree
   13754       385634 : tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13755              :                          tree in_decl)
   13756              : {
   13757       385634 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13758       385634 :   if (pack == error_mark_node)
   13759              :     return error_mark_node;
   13760       383745 :   if (PACK_EXPANSION_P (pack))
   13761              :     {
   13762         4696 :       tree r = copy_node (t);
   13763         4696 :       FOLD_EXPR_PACK (r) = pack;
   13764         4696 :       return r;
   13765              :     }
   13766       379049 :   if (TREE_VEC_LENGTH (pack) == 0)
   13767        41105 :     return expand_empty_fold (t, complain);
   13768              :   else
   13769       337944 :     return expand_right_fold (t, pack, complain);
   13770              : }
   13771              : 
   13772              : /* Substitute into a binary right fold expression.
   13773              : 
   13774              :    Do ths by building a single (non-empty) vector of arguments and
   13775              :    building the expression from those elements. */
   13776              : 
   13777              : static tree
   13778          928 : tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13779              :                          tree in_decl)
   13780              : {
   13781          928 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13782          928 :   if (pack == error_mark_node)
   13783              :     return error_mark_node;
   13784          928 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13785          928 :   if (init == error_mark_node)
   13786              :     return error_mark_node;
   13787              : 
   13788          928 :   if (PACK_EXPANSION_P (pack))
   13789              :     {
   13790           90 :       tree r = copy_node (t);
   13791           90 :       FOLD_EXPR_PACK (r) = pack;
   13792           90 :       FOLD_EXPR_INIT (r) = init;
   13793           90 :       return r;
   13794              :     }
   13795              : 
   13796          838 :   int n = TREE_VEC_LENGTH (pack);
   13797          838 :   tree vec = make_tree_vec (n + 1);
   13798         2191 :   for (int i = 0; i < n; ++i)
   13799         1353 :     TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
   13800          838 :   TREE_VEC_ELT (vec, n) = init;
   13801              : 
   13802          838 :   return expand_right_fold (t, vec, complain);
   13803              : }
   13804              : 
   13805              : /* Walk through the pattern of a pack expansion, adding everything in
   13806              :    local_specializations to a list.  */
   13807              : 
   13808              : class el_data
   13809              : {
   13810              : public:
   13811              :   /* Set of variables declared within the pattern.  */
   13812              :   hash_set<tree> internal;
   13813              :   /* Set of AST nodes that have been visited by the traversal.  */
   13814              :   hash_set<tree> visited;
   13815              :   /* List of local_specializations used within the pattern.  */
   13816              :   tree extra;
   13817              :   tsubst_flags_t complain;
   13818              :   /* True iff we don't want to walk into unevaluated contexts.  */
   13819              :   bool skip_unevaluated_operands = false;
   13820              :   /* The unevaluated contexts that we avoided walking.  */
   13821              :   auto_vec<tree> skipped_trees;
   13822              : 
   13823        30755 :   el_data (tsubst_flags_t c)
   13824        30755 :     : extra (NULL_TREE), complain (c) {}
   13825              : };
   13826              : static tree
   13827      5110996 : extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
   13828              : {
   13829      5110996 :   el_data &data = *reinterpret_cast<el_data*>(data_);
   13830      5110996 :   tree *extra = &data.extra;
   13831      5110996 :   tsubst_flags_t complain = data.complain;
   13832              : 
   13833      5110996 :   if (data.skip_unevaluated_operands
   13834      5110996 :       && unevaluated_p (TREE_CODE (*tp)))
   13835              :     {
   13836        30976 :       data.skipped_trees.safe_push (*tp);
   13837        30976 :       *walk_subtrees = 0;
   13838        30976 :       return NULL_TREE;
   13839              :     }
   13840              : 
   13841      5080020 :   if (TYPE_P (*tp) && typedef_variant_p (*tp))
   13842              :     /* Remember local typedefs (85214).  */
   13843        48127 :     tp = &TYPE_NAME (*tp);
   13844              : 
   13845      5080020 :   if (has_extra_args_mechanism_p (*tp))
   13846              :     /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
   13847              :        potentially see a previously captured local in an evaluated context
   13848              :        that's really only used in an unevaluated context (PR114303).  This
   13849              :        means callers of build_extra_args need to clear *_EXTRA_ARGS of the
   13850              :        outermost tree.  Nested *_EXTRA_ARGS should naturally be empty since
   13851              :        the outermost (extra-args) tree will intercept any substitution before
   13852              :        a nested tree can.  */
   13853        98813 :     gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE
   13854              :                         /* Except a lambda nested inside an extra-args tree
   13855              :                            can have extra args if we deferred partial
   13856              :                            substitution into it at template parse time.  But
   13857              :                            we don't walk LAMBDA_EXPR_EXTRA_ARGS anyway.  */
   13858              :                          || TREE_CODE (*tp) == LAMBDA_EXPR);
   13859              : 
   13860      5080020 :   if (TREE_CODE (*tp) == DECL_EXPR)
   13861              :     {
   13862       121773 :       tree decl = DECL_EXPR_DECL (*tp);
   13863       121773 :       data.internal.add (decl);
   13864       120553 :       if (DECL_DECOMPOSITION_P (decl)
   13865       121779 :           && TREE_TYPE (decl) != error_mark_node)
   13866              :         {
   13867            6 :           gcc_assert (DECL_NAME (decl) == NULL_TREE);
   13868            6 :           for (tree decl2 = DECL_CHAIN (decl);
   13869           18 :                decl2
   13870           12 :                && DECL_DECOMPOSITION_P (decl2)
   13871           12 :                && DECL_NAME (decl2)
   13872           30 :                && TREE_TYPE (decl2) != error_mark_node;
   13873           12 :                decl2 = DECL_CHAIN (decl2))
   13874              :             {
   13875           12 :               gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
   13876           12 :               data.internal.add (decl2);
   13877              :             }
   13878              :         }
   13879              :     }
   13880      4958247 :   else if (TREE_CODE (*tp) == LAMBDA_EXPR)
   13881              :     {
   13882              :       /* Since we defer implicit capture, look in the parms and body.  */
   13883          579 :       tree fn = lambda_function (*tp);
   13884          579 :       cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
   13885              :                     &data.visited);
   13886          579 :       cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
   13887              :                     &data.visited);
   13888              :     }
   13889      4957668 :   else if (tree spec = retrieve_local_specialization (*tp))
   13890              :     {
   13891        88516 :       if (data.internal.contains (*tp))
   13892              :         /* Don't mess with variables declared within the pattern.  */
   13893              :         return NULL_TREE;
   13894        88501 :       if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13895              :         {
   13896              :           /* Maybe pull out the PARM_DECL for a partial instantiation.  */
   13897          334 :           tree args = ARGUMENT_PACK_ARGS (spec);
   13898          334 :           if (TREE_VEC_LENGTH (args) == 1)
   13899              :             {
   13900           34 :               tree elt = TREE_VEC_ELT (args, 0);
   13901           34 :               if (PACK_EXPANSION_P (elt))
   13902            0 :                 elt = PACK_EXPANSION_PATTERN (elt);
   13903           34 :               if (DECL_PACK_P (elt))
   13904              :                 spec = elt;
   13905              :             }
   13906          334 :           if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13907              :             {
   13908              :               /* Handle lambda capture here, since we aren't doing any
   13909              :                  substitution now, and so tsubst_copy won't call
   13910              :                  process_outer_var_ref.  */
   13911          334 :               tree args = ARGUMENT_PACK_ARGS (spec);
   13912          334 :               int len = TREE_VEC_LENGTH (args);
   13913         1263 :               for (int i = 0; i < len; ++i)
   13914              :                 {
   13915          929 :                   tree arg = TREE_VEC_ELT (args, i);
   13916          929 :                   tree carg = arg;
   13917          929 :                   if (outer_automatic_var_p (arg))
   13918          929 :                     carg = process_outer_var_ref (arg, complain);
   13919          929 :                   if (carg != arg)
   13920              :                     {
   13921              :                       /* Make a new NONTYPE_ARGUMENT_PACK of the capture
   13922              :                          proxies.  */
   13923          929 :                       if (i == 0)
   13924              :                         {
   13925          328 :                           spec = copy_node (spec);
   13926          328 :                           args = copy_node (args);
   13927          328 :                           ARGUMENT_PACK_ARGS (spec) = args;
   13928          328 :                           register_local_specialization (spec, *tp);
   13929              :                         }
   13930          929 :                       TREE_VEC_ELT (args, i) = carg;
   13931              :                     }
   13932              :                 }
   13933              :             }
   13934              :         }
   13935        88501 :       if (outer_automatic_var_p (spec))
   13936           21 :         spec = process_outer_var_ref (spec, complain);
   13937        88501 :       *extra = tree_cons (*tp, spec, *extra);
   13938              :     }
   13939              :   return NULL_TREE;
   13940              : }
   13941              : static tree
   13942        30755 : extract_local_specs (tree pattern, tsubst_flags_t complain)
   13943              : {
   13944        30755 :   el_data data (complain);
   13945              :   /* Walk the pattern twice, ignoring unevaluated operands the first time
   13946              :      around, so that if a local specialization appears in both an evaluated
   13947              :      and unevaluated context we prefer to process it in the evaluated context
   13948              :      (since e.g. process_outer_var_ref is a no-op inside an unevaluated
   13949              :      context).  */
   13950        30755 :   data.skip_unevaluated_operands = true;
   13951        30755 :   cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
   13952              :   /* Now walk the unevaluated contexts we skipped the first time around.  */
   13953        30755 :   data.skip_unevaluated_operands = false;
   13954       121725 :   for (tree t : data.skipped_trees)
   13955              :     {
   13956        30976 :       data.visited.remove (t);
   13957        30976 :       cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
   13958              :     }
   13959        61510 :   return data.extra;
   13960        30755 : }
   13961              : 
   13962              : /* Extract any uses of local_specializations from PATTERN and add them to ARGS
   13963              :    for use in PACK_EXPANSION_EXTRA_ARGS.  */
   13964              : 
   13965              : tree
   13966        31919 : build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
   13967              : {
   13968              :   /* Make a copy of the extra arguments so that they won't get changed
   13969              :      out from under us.  */
   13970        31919 :   tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
   13971        31919 :   if ((complain & tf_partial) || TREE_STATIC (args))
   13972              :     /* Remember whether this is a partial substitution.  */
   13973         1360 :     TREE_STATIC (extra) = true;
   13974        31919 :   if (local_specializations)
   13975        30755 :     if (tree locals = extract_local_specs (pattern, complain))
   13976        29862 :       extra = tree_cons (NULL_TREE, extra, locals);
   13977        31919 :   return extra;
   13978              : }
   13979              : 
   13980              : /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
   13981              :    normal template args to ARGS.  */
   13982              : 
   13983              : tree
   13984    117185515 : add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
   13985              : {
   13986    117185515 :   if (!extra)
   13987              :     return args;
   13988              : 
   13989       533096 :   if (TREE_CODE (extra) == TREE_LIST)
   13990              :     {
   13991      2127779 :       for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
   13992              :         {
   13993              :           /* The partial instantiation involved local declarations collected in
   13994              :              extract_local_specs; map from the general template to our local
   13995              :              context.  */
   13996      1595006 :           tree gen = TREE_PURPOSE (elt);
   13997      1595006 :           tree inst = TREE_VALUE (elt);
   13998      1595006 :           if (DECL_P (inst))
   13999      1594672 :             if (tree local = retrieve_local_specialization (inst))
   14000      1595006 :               inst = local;
   14001              :           /* else inst is already a full instantiation of the pack.  */
   14002      1595006 :           register_local_specialization (inst, gen);
   14003      1595006 :           if (is_normal_capture_proxy (gen))
   14004       706236 :             register_local_specialization (inst, DECL_CAPTURED_VARIABLE (gen));
   14005              :         }
   14006       532773 :       gcc_assert (!TREE_PURPOSE (extra));
   14007       532773 :       extra = TREE_VALUE (extra);
   14008              :     }
   14009       533096 :   if (TREE_STATIC (extra))
   14010              :     {
   14011              :       /* This is a partial substitution into e.g. a requires-expr or lambda-expr
   14012              :          inside a default template argument; we expect 'extra' to be a full set
   14013              :          of template arguments for the template context, so it suffices to just
   14014              :          substitute into them.  */
   14015          104 :       args = tsubst_template_args (extra, args, complain, in_decl);
   14016          104 :       if (processing_template_decl)
   14017              :         /* A templated substitution into a partial substitution is still a
   14018              :            partial substitution.  */
   14019           12 :         TREE_STATIC (args) = true;
   14020              :     }
   14021              :   else
   14022       532992 :     args = add_to_template_args (extra, args);
   14023              :   return args;
   14024              : }
   14025              : 
   14026              : /* Substitute ARGS into T, which is a pack expansion
   14027              :    (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION).  Returns a
   14028              :    TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
   14029              :    (if only a partial substitution could be performed) or
   14030              :    ERROR_MARK_NODE if there was an error.  */
   14031              : 
   14032              : tree
   14033     92188749 : tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   14034              :                        tree in_decl)
   14035              : {
   14036     92188749 :   tree pattern;
   14037     92188749 :   tree pack, packs = NULL_TREE;
   14038     92188749 :   bool unsubstituted_packs = false;
   14039     92188749 :   int i, len = -1;
   14040     92188749 :   tree result;
   14041     92188749 :   bool need_local_specializations = false;
   14042     92188749 :   int levels;
   14043              : 
   14044     92188749 :   gcc_assert (PACK_EXPANSION_P (t));
   14045     92188749 :   pattern = PACK_EXPANSION_PATTERN (t);
   14046              : 
   14047              :   /* Add in any args remembered from an earlier partial instantiation.  */
   14048     92188749 :   args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
   14049              : 
   14050    184377495 :   levels = TMPL_ARGS_DEPTH (args);
   14051              : 
   14052              :   /* Determine the argument packs that will instantiate the parameter
   14053              :      packs used in the expansion expression. While we're at it,
   14054              :      compute the number of arguments to be expanded and make sure it
   14055              :      is consistent.  */
   14056    268420465 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
   14057     90046771 :        pack = TREE_CHAIN (pack))
   14058              :     {
   14059     92983225 :       tree parm_pack = TREE_VALUE (pack);
   14060     92983225 :       tree arg_pack = NULL_TREE;
   14061     92983225 :       tree orig_arg = NULL_TREE;
   14062     92983225 :       int level = 0;
   14063              : 
   14064     92983225 :       if (TREE_CODE (parm_pack) == BASES)
   14065              :         {
   14066           27 :           gcc_assert (parm_pack == pattern);
   14067           27 :           tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl);
   14068           27 :           if (BASES_DIRECT (parm_pack))
   14069      2936451 :             return calculate_direct_bases (type, complain);
   14070              :           else
   14071           12 :             return calculate_bases (type, complain);
   14072              :         }
   14073     92983198 :       else if (builtin_pack_call_p (parm_pack))
   14074              :         {
   14075        99739 :           if (parm_pack != pattern)
   14076              :             {
   14077            3 :               if (complain & tf_error)
   14078            3 :                 sorry ("%qE is not the entire pattern of the pack expansion",
   14079              :                        parm_pack);
   14080            3 :               return error_mark_node;
   14081              :             }
   14082        99736 :           return expand_builtin_pack_call (parm_pack, args,
   14083        99736 :                                            complain, in_decl);
   14084              :         }
   14085     92883459 :       else if (TREE_CODE (parm_pack) == PARM_DECL)
   14086              :         {
   14087              :           /* We know we have correct local_specializations if this
   14088              :              expansion is at function scope, or if we're dealing with a
   14089              :              local parameter in a requires expression; for the latter,
   14090              :              tsubst_requires_expr set it up appropriately.  */
   14091       740145 :           if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
   14092       738998 :             arg_pack = retrieve_local_specialization (parm_pack);
   14093              :           else
   14094              :             /* We can't rely on local_specializations for a parameter
   14095              :                name used later in a function declaration (such as in a
   14096              :                late-specified return type).  Even if it exists, it might
   14097              :                have the wrong value for a recursive call.  */
   14098              :             need_local_specializations = true;
   14099              : 
   14100       738998 :           if (!arg_pack)
   14101              :             {
   14102              :               /* This parameter pack was used in an unevaluated context.  Just
   14103              :                  make a dummy decl, since it's only used for its type.  */
   14104         1148 :               ++cp_unevaluated_operand;
   14105         1148 :               arg_pack = tsubst_decl (parm_pack, args, complain);
   14106         1148 :               --cp_unevaluated_operand;
   14107         1148 :               if (arg_pack && DECL_PACK_P (arg_pack))
   14108              :                 /* Partial instantiation of the parm_pack, we can't build
   14109              :                    up an argument pack yet.  */
   14110              :                 arg_pack = NULL_TREE;
   14111              :               else
   14112         1003 :                 arg_pack = make_fnparm_pack (arg_pack);
   14113              :             }
   14114       738997 :           else if (DECL_PACK_P (arg_pack))
   14115              :             /* This argument pack isn't fully instantiated yet.  */
   14116              :             arg_pack = NULL_TREE;
   14117              :         }
   14118     92143314 :       else if (is_capture_proxy (parm_pack))
   14119              :         {
   14120          387 :           arg_pack = retrieve_local_specialization (parm_pack);
   14121          387 :           if (DECL_DECOMPOSITION_P (arg_pack))
   14122              :             {
   14123          128 :               orig_arg = arg_pack;
   14124          128 :               goto expand_sb_pack;
   14125              :             }
   14126          259 :           if (DECL_PACK_P (arg_pack))
   14127              :             arg_pack = NULL_TREE;
   14128              :         }
   14129     92142927 :       else if (DECL_DECOMPOSITION_P (parm_pack))
   14130              :         {
   14131         4920 :           orig_arg = retrieve_local_specialization (parm_pack);
   14132         5048 :         expand_sb_pack:
   14133         5048 :           gcc_assert (DECL_DECOMPOSITION_P (orig_arg));
   14134         5048 :           if (TREE_TYPE (orig_arg) == error_mark_node)
   14135              :             return error_mark_node;
   14136         5048 :           gcc_assert (DECL_HAS_VALUE_EXPR_P (orig_arg));
   14137         5048 :           arg_pack = DECL_VALUE_EXPR (orig_arg);
   14138         5048 :           if (TREE_CODE (arg_pack) != ARRAY_REF)
   14139              :             {
   14140              :               /* Structured binding packs when initializer is non-dependent
   14141              :                  should have their DECL_VALUE_EXPR set to a TREE_VEC.  See
   14142              :                  cp_finish_decomp comment above the packv variable for
   14143              :                  details.  */
   14144         5045 :               tree vec = make_tree_vec (TREE_VEC_LENGTH (arg_pack) - 2);
   14145         5045 :               if (TREE_VEC_LENGTH (vec))
   14146         4623 :                 memcpy (TREE_VEC_BEGIN (vec), &TREE_VEC_ELT (arg_pack, 2),
   14147         4623 :                         TREE_VEC_LENGTH (vec) * sizeof (tree));
   14148         5045 :               arg_pack = make_node (NONTYPE_ARGUMENT_PACK);
   14149         5045 :               ARGUMENT_PACK_ARGS (arg_pack) = vec;
   14150              :             }
   14151              :           else
   14152              :             {
   14153              :               /* If the structured binding pack has type dependent
   14154              :                  base, we can't expand it yet.  */
   14155            3 :               tree base = TREE_OPERAND (arg_pack, 0);
   14156            3 :               gcc_assert (VAR_P (base)
   14157              :                           && type_dependent_expression_p (base));
   14158              :               arg_pack = NULL_TREE;
   14159              :             }
   14160              :         }
   14161              :       else
   14162              :         {
   14163     92138007 :           int idx;
   14164     92138007 :           template_parm_level_and_index (parm_pack, &level, &idx);
   14165     92138007 :           if (level <= levels)
   14166    161002118 :             arg_pack = TMPL_ARG (args, level, idx);
   14167              : 
   14168     75525609 :           if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
   14169     80501080 :               && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
   14170              :             arg_pack = NULL_TREE;
   14171              :         }
   14172              : 
   14173     92139010 :       if (orig_arg == NULL_TREE)
   14174              :         orig_arg = arg_pack;
   14175     92883459 :       if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   14176         4148 :         arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   14177              : 
   14178     76266335 :       if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
   14179              :         /* This can only happen if we forget to expand an argument
   14180              :            pack somewhere else. Just return an error, silently.  */
   14181              :         {
   14182            9 :           result = make_tree_vec (1);
   14183            9 :           TREE_VEC_ELT (result, 0) = error_mark_node;
   14184            9 :           return result;
   14185              :         }
   14186              : 
   14187     92883450 :       if (arg_pack)
   14188              :         {
   14189     76266326 :           int my_len
   14190     76266326 :             = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
   14191              : 
   14192              :           /* Don't bother trying to do a partial substitution with
   14193              :              incomplete packs; we'll try again after deduction.  */
   14194     76266326 :           if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
   14195              :             return t;
   14196              : 
   14197     73429685 :           if (len < 0)
   14198              :             len = my_len;
   14199       792291 :           else if (len != my_len)
   14200              :             {
   14201           35 :               if (!(complain & tf_error))
   14202              :                 /* Fail quietly.  */;
   14203           13 :               else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
   14204            7 :                 error ("mismatched argument pack lengths while expanding %qT",
   14205              :                        pattern);
   14206              :               else
   14207            6 :                 error ("mismatched argument pack lengths while expanding %qE",
   14208              :                        pattern);
   14209           35 :               return error_mark_node;
   14210              :             }
   14211              : 
   14212              :           /* Keep track of the parameter packs and their corresponding
   14213              :              argument packs.  */
   14214     73429650 :           packs = tree_cons (parm_pack, arg_pack, packs);
   14215     73429650 :           TREE_TYPE (packs) = orig_arg;
   14216              :         }
   14217              :       else
   14218              :         {
   14219              :           /* We can't substitute for this parameter pack.  We use a flag as
   14220              :              well as the missing_level counter because function parameter
   14221              :              packs don't have a level.  */
   14222     16617124 :           gcc_assert (processing_template_decl || is_auto (parm_pack)
   14223              :                       || args == NULL_TREE);
   14224              :           unsubstituted_packs = true;
   14225              :         }
   14226              :     }
   14227              : 
   14228              :   /* If the expansion is just T..., return the matching argument pack, unless
   14229              :      we need to call convert_from_reference on all the elements.  This is an
   14230              :      important optimization; see c++/68422.  */
   14231     89252295 :   if (!unsubstituted_packs
   14232    161888866 :       && TREE_PURPOSE (packs) == pattern)
   14233              :     {
   14234     65552372 :       tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
   14235              : 
   14236              :       /* If the argument pack is a single pack expansion, pull it out.  */
   14237     65552372 :       if (TREE_VEC_LENGTH (args) == 1
   14238     65552372 :           && pack_expansion_args_count (args))
   14239              :         {
   14240      1721007 :           tree arg = TREE_VEC_ELT (args, 0);
   14241      1721007 :           if (PACK_EXPANSION_SIZEOF_P (t)
   14242      1721007 :               && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
   14243              :             /* Except if this isn't a simple sizeof...(T) which gets sZ
   14244              :                mangling, keep the TREE_VEC to get sP mangling.  */;
   14245              :           else
   14246      1720998 :             return TREE_VEC_ELT (args, 0);
   14247              :         }
   14248              : 
   14249              :       /* Types need no adjustment, nor does sizeof..., and if we still have
   14250              :          some pack expansion args we won't do anything yet.  */
   14251     63831374 :       if (TREE_CODE (t) == TYPE_PACK_EXPANSION
   14252      3719135 :           || PACK_EXPANSION_SIZEOF_P (t)
   14253     67545920 :           || pack_expansion_args_count (args))
   14254              :         return args;
   14255              :       /* Also optimize expression pack expansions if we can tell that the
   14256              :          elements won't have reference type.  */
   14257      3631398 :       tree type = TREE_TYPE (pattern);
   14258      3631398 :       if (type && !TYPE_REF_P (type)
   14259              :           && !PACK_EXPANSION_P (type)
   14260              :           && !WILDCARD_TYPE_P (type))
   14261              :         return args;
   14262              :       /* Otherwise use the normal path so we get convert_from_reference.  */
   14263              :     }
   14264              : 
   14265              :   /* We cannot expand this expansion expression, because we don't have
   14266              :      all of the argument packs we need.  */
   14267     23715587 :   if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
   14268              :     {
   14269              :       /* We got some full packs, but we can't substitute them in until we
   14270              :          have values for all the packs.  So remember these until then.  */
   14271              : 
   14272          805 :       t = make_pack_expansion (pattern, complain);
   14273          805 :       PACK_EXPANSION_EXTRA_ARGS (t)
   14274          805 :         = build_extra_args (pattern, args, complain);
   14275          805 :       return t;
   14276              :     }
   14277              : 
   14278              :   /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
   14279              :      type, so create our own local specializations map; the current map is
   14280              :      either NULL or (in the case of recursive unification) might have
   14281              :      bindings that we don't want to use or alter.  */
   14282     23714782 :   local_specialization_stack lss (need_local_specializations
   14283     71143333 :                                   ? lss_blank : lss_nop);
   14284              : 
   14285     23714782 :   if (unsubstituted_packs)
   14286              :     {
   14287              :       /* There were no real arguments, we're just replacing a parameter
   14288              :          pack with another version of itself. Substitute into the
   14289              :          pattern and return a PACK_EXPANSION_*. The caller will need to
   14290              :          deal with that.  */
   14291     16614931 :       if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
   14292       514558 :         result = tsubst_expr (pattern, args, complain, in_decl);
   14293              :       else
   14294     16100373 :         result = tsubst (pattern, args, complain, in_decl);
   14295     16614931 :       result = make_pack_expansion (result, complain);
   14296     16614931 :       if (result == error_mark_node)
   14297              :         return error_mark_node;
   14298     16614928 :       PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
   14299     16614928 :       PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
   14300     16614928 :       if (PACK_EXPANSION_AUTO_P (t))
   14301              :         {
   14302              :           /* This is a fake auto... pack expansion created in add_capture with
   14303              :              _PACKS that don't appear in the pattern.  Copy one over.  */
   14304            6 :           packs = PACK_EXPANSION_PARAMETER_PACKS (t);
   14305            3 :           pack = retrieve_local_specialization (TREE_VALUE (packs));
   14306            3 :           gcc_checking_assert (DECL_PACK_P (pack));
   14307            6 :           PACK_EXPANSION_PARAMETER_PACKS (result)
   14308            3 :             = build_tree_list (NULL_TREE, pack);
   14309            3 :           PACK_EXPANSION_AUTO_P (result) = true;
   14310              :         }
   14311     16614928 :       return result;
   14312              :     }
   14313              : 
   14314      7099851 :   gcc_assert (len >= 0);
   14315              : 
   14316              :   /* For each argument in each argument pack, substitute into the
   14317              :      pattern.  */
   14318      7099851 :   result = make_tree_vec (len);
   14319      7099851 :   tree elem_args = copy_template_args (args);
   14320     23861528 :   for (i = 0; i < len; ++i)
   14321              :     {
   14322     10371008 :       t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
   14323              :                                                     i,
   14324              :                                                     elem_args, complain,
   14325              :                                                     in_decl);
   14326     10371008 :       TREE_VEC_ELT (result, i) = t;
   14327     10371008 :       if (t == error_mark_node)
   14328              :         {
   14329              :           result = error_mark_node;
   14330              :           break;
   14331              :         }
   14332              :     }
   14333              : 
   14334              :   /* Update ARGS to restore the substitution from parameter packs to
   14335              :      their argument packs.  */
   14336     14991900 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   14337              :     {
   14338      7892049 :       tree parm = TREE_PURPOSE (pack);
   14339              : 
   14340      7892049 :       if (TREE_CODE (parm) == PARM_DECL
   14341      7158817 :           || VAR_P (parm)
   14342      7153709 :           || TREE_CODE (parm) == FIELD_DECL)
   14343       738340 :         register_local_specialization (TREE_TYPE (pack), parm);
   14344              :       else
   14345              :         {
   14346      7153709 :           int idx, level;
   14347              : 
   14348      7153709 :           if (TREE_VALUE (pack) == NULL_TREE)
   14349            0 :             continue;
   14350              : 
   14351      7153709 :           template_parm_level_and_index (parm, &level, &idx);
   14352              : 
   14353              :           /* Update the corresponding argument.  */
   14354     14307418 :           if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   14355      1208328 :             TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx)
   14356      2416656 :               = TREE_TYPE (pack);
   14357              :           else
   14358      5945381 :             TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
   14359              :         }
   14360              :     }
   14361              : 
   14362              :   /* If the dependent pack arguments were such that we end up with only a
   14363              :      single pack expansion again, there's no need to keep it in a TREE_VEC.  */
   14364      1922691 :   if (len == 1 && TREE_CODE (result) == TREE_VEC
   14365      9020387 :       && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
   14366       172543 :     return TREE_VEC_ELT (result, 0);
   14367              : 
   14368              :   return result;
   14369              : }
   14370              : 
   14371              : /* Substitute ARGS into T, which is a TREE_VEC.  This function creates a new
   14372              :    TREE_VEC rather than substituting the elements in-place.  */
   14373              : 
   14374              : static tree
   14375         3906 : tsubst_tree_vec (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14376              : {
   14377         3906 :   const int len = TREE_VEC_LENGTH (t);
   14378         3906 :   tree r = make_tree_vec (len);
   14379        17818 :   for (int i = 0; i < len; ++i)
   14380              :     {
   14381        13912 :       tree arg = TREE_VEC_ELT (t, i);
   14382        13912 :       if (TYPE_P (arg))
   14383         2161 :         TREE_VEC_ELT (r, i) = tsubst (arg, args, complain, in_decl);
   14384              :       else
   14385        11751 :         TREE_VEC_ELT (r, i) = tsubst_expr (arg, args, complain, in_decl);
   14386              :     }
   14387         3906 :   return r;
   14388              : }
   14389              : 
   14390              : /* Substitute ARGS into T, which is a pack index (i.e., PACK_INDEX_TYPE or
   14391              :    PACK_INDEX_EXPR).  Returns a single type or expression, a PACK_INDEX_*
   14392              :    node if only a partial substitution could be performed, or ERROR_MARK_NODE
   14393              :    if there was an error.  */
   14394              : 
   14395              : static tree
   14396        13499 : tsubst_pack_index (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14397              : {
   14398        13499 :   tree pack = PACK_INDEX_PACK (t);
   14399        13499 :   if (PACK_EXPANSION_P (pack))
   14400         9593 :     pack = tsubst_pack_expansion (pack, args, complain, in_decl);
   14401              :   else
   14402              :     {
   14403              :       /* PACK can be {*args#0} whose args#0's value-expr refers to
   14404              :          a partially instantiated closure.  Let tsubst find the
   14405              :          fully-instantiated one.  */
   14406         3906 :       gcc_assert (TREE_CODE (pack) == TREE_VEC);
   14407         3906 :       pack = tsubst_tree_vec (pack, args, complain, in_decl);
   14408              :     }
   14409        13499 :   if (TREE_CODE (pack) == TREE_VEC && TREE_VEC_LENGTH (pack) == 0)
   14410              :     {
   14411            5 :       if (complain & tf_error)
   14412            5 :         error ("cannot index an empty pack");
   14413            5 :       return error_mark_node;
   14414              :     }
   14415        13494 :   tree index = tsubst_expr (PACK_INDEX_INDEX (t), args, complain, in_decl);
   14416        13494 :   const bool parenthesized_p = (TREE_CODE (t) == PACK_INDEX_EXPR
   14417        13494 :                                 && PACK_INDEX_PARENTHESIZED_P (t));
   14418        13494 :   tree r;
   14419        13494 :   if (!value_dependent_expression_p (index) && TREE_CODE (pack) == TREE_VEC)
   14420        10599 :     r = pack_index_element (index, pack, parenthesized_p, complain);
   14421              :   else
   14422         2895 :     r = make_pack_index (pack, index);
   14423        13494 :   if (TREE_CODE (t) == PACK_INDEX_TYPE)
   14424         2691 :     r = cp_build_qualified_type (r, cp_type_quals (t) | cp_type_quals (r),
   14425              :                                  complain | tf_ignore_bad_quals);
   14426              :   return r;
   14427              : }
   14428              : 
   14429              : /* Make an argument pack out of the TREE_VEC VEC.  */
   14430              : 
   14431              : static tree
   14432           27 : make_argument_pack (tree vec)
   14433              : {
   14434           27 :   tree pack;
   14435              : 
   14436           27 :   if (TYPE_P (TREE_VEC_ELT (vec, 0)))
   14437           15 :     pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   14438              :   else
   14439              :     {
   14440           12 :       pack = make_node (NONTYPE_ARGUMENT_PACK);
   14441           12 :       TREE_CONSTANT (pack) = 1;
   14442              :     }
   14443           27 :   ARGUMENT_PACK_ARGS (pack) = vec;
   14444           27 :   return pack;
   14445              : }
   14446              : 
   14447              : /* Return an exact copy of template args T that can be modified
   14448              :    independently.  */
   14449              : 
   14450              : tree
   14451     29011266 : copy_template_args (tree t)
   14452              : {
   14453     29011266 :   if (t == error_mark_node)
   14454              :     return t;
   14455              : 
   14456     29011266 :   int len = TREE_VEC_LENGTH (t);
   14457     29011266 :   tree new_vec = make_tree_vec (len);
   14458              : 
   14459     94850995 :   for (int i = 0; i < len; ++i)
   14460              :     {
   14461     65839729 :       tree elt = TREE_VEC_ELT (t, i);
   14462     65839729 :       if (elt && TREE_CODE (elt) == TREE_VEC)
   14463      1891800 :         elt = copy_template_args (elt);
   14464     65839729 :       TREE_VEC_ELT (new_vec, i) = elt;
   14465              :     }
   14466              : 
   14467     58022532 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
   14468     29011266 :     = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   14469              : 
   14470     29011266 :   return new_vec;
   14471              : }
   14472              : 
   14473              : /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg.  */
   14474              : 
   14475              : tree
   14476    143733407 : tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
   14477              :                       tree in_decl)
   14478              : {
   14479              :   /* This flag is used only during deduction, and we don't expect to
   14480              :      substitute such ARGUMENT_PACKs.  */
   14481    143733407 :   gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
   14482              : 
   14483              :   /* Substitute into each of the arguments.  */
   14484    143733407 :   tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
   14485              :                                          args, complain, in_decl);
   14486    143733407 :   if (pack_args == error_mark_node)
   14487              :     return error_mark_node;
   14488              : 
   14489    142898112 :   if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
   14490              :     return orig_arg;
   14491              : 
   14492              :   /* If we're substituting into a generic ARGUMENT_PACK for a variadic
   14493              :      template parameter, we might be able to avoid allocating a new
   14494              :      ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
   14495              :      if the substituted result is identical to it.  */
   14496    138454254 :   if (tree parm = template_arg_to_parm (orig_arg))
   14497              :     {
   14498     55239575 :       int level, index;
   14499     55239575 :       template_parm_level_and_index (parm, &level, &index);
   14500    163332626 :       if (TMPL_ARGS_DEPTH (args) >= level)
   14501     97388968 :         if (tree arg = TMPL_ARG (args, level, index))
   14502     44662416 :           if (TREE_CODE (arg) == TREE_CODE (orig_arg)
   14503     44662416 :               && ARGUMENT_PACK_ARGS (arg) == pack_args)
   14504              :             {
   14505     43360436 :               gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
   14506     43360436 :               return arg;
   14507              :             }
   14508              :     }
   14509              : 
   14510     95093818 :   tree new_arg;
   14511     95093818 :   if (TYPE_P (orig_arg))
   14512              :     {
   14513     94415665 :       new_arg = cxx_make_type (TREE_CODE (orig_arg));
   14514     94415665 :       SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
   14515              :     }
   14516              :   else
   14517              :     {
   14518       678153 :       new_arg = make_node (TREE_CODE (orig_arg));
   14519       678153 :       TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
   14520              :     }
   14521     95093818 :   ARGUMENT_PACK_ARGS (new_arg) = pack_args;
   14522              :   return new_arg;
   14523              : }
   14524              : 
   14525              : /* Substitute ARGS into the vector or list of template arguments T.  */
   14526              : 
   14527              : tree
   14528   1961243620 : tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14529              : {
   14530   1961243620 :   if (t == error_mark_node)
   14531              :     return error_mark_node;
   14532              : 
   14533              :   /* In "sizeof(X<I>)" we need to evaluate "I".  */
   14534   1961243620 :   cp_evaluated ev;
   14535              : 
   14536   1961243620 :   const int len = TREE_VEC_LENGTH (t);
   14537   1961243620 :   tree *elts = XALLOCAVEC (tree, len);
   14538   1961243620 :   int expanded_len_adjust = 0;
   14539              : 
   14540              :   /* True iff the substituted result is identical to T.  */
   14541   1961243620 :   bool const_subst_p = true;
   14542              : 
   14543   5666083627 :   for (int i = 0; i < len; i++)
   14544              :     {
   14545   3706821865 :       tree orig_arg = TREE_VEC_ELT (t, i);
   14546   3706821865 :       tree new_arg;
   14547              : 
   14548   3706821865 :       if (!orig_arg)
   14549              :         new_arg = NULL_TREE;
   14550   3706821799 :       else if (TREE_CODE (orig_arg) == TREE_VEC)
   14551     89098710 :         new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
   14552   3617723089 :       else if (PACK_EXPANSION_P (orig_arg))
   14553              :         {
   14554              :           /* Substitute into an expansion expression.  */
   14555     78977485 :           new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
   14556              : 
   14557     78977485 :           if (TREE_CODE (new_arg) == TREE_VEC)
   14558              :             /* Add to the expanded length adjustment the number of
   14559              :                expanded arguments. We subtract one from this
   14560              :                measurement, because the argument pack expression
   14561              :                itself is already counted as 1 in
   14562              :                LEN. EXPANDED_LEN_ADJUST can actually be negative, if
   14563              :                the argument pack is empty.  */
   14564     62255611 :             expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
   14565              :         }
   14566   3538745604 :       else if (ARGUMENT_PACK_P (orig_arg))
   14567    139335667 :         new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
   14568              :       else
   14569   3399409937 :         new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
   14570              : 
   14571   3706821829 :       if (new_arg == error_mark_node)
   14572              :         return error_mark_node;
   14573              : 
   14574   3704840007 :       elts[i] = new_arg;
   14575   3704840007 :       if (new_arg != orig_arg)
   14576   3564149143 :         const_subst_p = false;
   14577              :     }
   14578              : 
   14579   1959261762 :   if (const_subst_p)
   14580              :     return t;
   14581              : 
   14582   1939338747 :   tree maybe_reuse = NULL_TREE;
   14583              : 
   14584              :   /* If ARGS and T are both multi-level, the substituted result may be
   14585              :      identical to ARGS.  */
   14586   1939338747 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
   14587     88883522 :       && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
   14588     10376046 :       && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
   14589              :     maybe_reuse = args;
   14590              :   /* If T appears to be a vector of generic template arguments, the
   14591              :      substituted result may be identical to the corresponding level
   14592              :      from ARGS.  */
   14593   1929990843 :   else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
   14594              :     {
   14595   1427456429 :       int level, index;
   14596   1427456429 :       template_parm_level_and_index (parm, &level, &index);
   14597   4030366563 :       if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
   14598   2588689336 :         maybe_reuse = TMPL_ARGS_LEVEL (args, level);
   14599              :     }
   14600              : 
   14601              :   /* If the substituted result is identical to MAYBE_REUSE, return
   14602              :      it and avoid allocating a new TREE_VEC, as an optimization.  */
   14603    133111761 :   if (maybe_reuse != NULL_TREE
   14604   1303692572 :       && TREE_VEC_LENGTH (maybe_reuse) == len
   14605   2460211311 :       && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
   14606              :     return maybe_reuse;
   14607              : 
   14608              :   /* If T consists of only a pack expansion for which substitution yielded
   14609              :      a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
   14610              :      of effectively making a copy.  */
   14611    813214751 :   if (len == 1
   14612    494067959 :       && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
   14613    878010281 :       && TREE_CODE (elts[0]) == TREE_VEC)
   14614              :     return elts[0];
   14615              : 
   14616              :   /* Make space for the expanded arguments coming from template
   14617              :      argument packs.  */
   14618    760800093 :   tree r = make_tree_vec (len + expanded_len_adjust);
   14619              :   /* T can contain TREE_VECs. That happens if T contains the
   14620              :      arguments for a member template.
   14621              :      In that case each TREE_VEC in T represents a level of template
   14622              :      arguments, and T won't carry any non defaulted argument count.
   14623              :      It will rather be the nested TREE_VECs that will carry one.
   14624              :      In other words, T carries a non defaulted argument count only
   14625              :      if it doesn't contain any nested TREE_VEC.  */
   14626    760800093 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
   14627              :     {
   14628    724484872 :       int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   14629    724484872 :       count += expanded_len_adjust;
   14630    724484872 :       SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
   14631              :     }
   14632              : 
   14633              :   int out = 0;
   14634   1918227755 :   for (int i = 0; i < len; i++)
   14635              :     {
   14636   1157427662 :       tree orig_arg = TREE_VEC_ELT (t, i);
   14637   1157427662 :       if (orig_arg
   14638   1157427635 :           && PACK_EXPANSION_P (orig_arg)
   14639     23044972 :           && TREE_CODE (elts[i]) == TREE_VEC)
   14640              :         {
   14641              :           /* Now expand the template argument pack "in place".  */
   14642     24020791 :           for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
   14643     14179838 :             TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
   14644              :         }
   14645              :       else
   14646              :         {
   14647   1147586709 :           TREE_VEC_ELT (r, out) = elts[i];
   14648   1147586709 :           out++;
   14649              :         }
   14650              :     }
   14651    760800093 :   gcc_assert (out == TREE_VEC_LENGTH (r));
   14652              : 
   14653              :   return r;
   14654   1961243584 : }
   14655              : 
   14656              : /* Substitute ARGS into one level PARMS of template parameters.  */
   14657              : 
   14658              : static tree
   14659     27934483 : tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
   14660              : {
   14661     27934483 :   if (parms == error_mark_node)
   14662              :     return error_mark_node;
   14663              : 
   14664     27934483 :   tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
   14665              : 
   14666     69584031 :   for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
   14667              :     {
   14668     41649548 :       tree tuple = TREE_VEC_ELT (parms, i);
   14669              : 
   14670     41649548 :       if (tuple == error_mark_node)
   14671            0 :         continue;
   14672              : 
   14673     41649548 :       TREE_VEC_ELT (new_vec, i) =
   14674     41649548 :         tsubst_template_parm (tuple, args, complain);
   14675              :     }
   14676              : 
   14677              :   return new_vec;
   14678              : }
   14679              : 
   14680              : /* Return the result of substituting ARGS into the template parameters
   14681              :    given by PARMS.  If there are m levels of ARGS and m + n levels of
   14682              :    PARMS, then the result will contain n levels of PARMS.  For
   14683              :    example, if PARMS is `template <class T> template <class U>
   14684              :    template <T*, U, class V>' and ARGS is {{int}, {double}} then the
   14685              :    result will be `template <int*, double, class V>'.  */
   14686              : 
   14687              : static tree
   14688     28041735 : tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
   14689              : {
   14690     28041735 :   tree r = NULL_TREE;
   14691     28041735 :   tree* new_parms;
   14692              : 
   14693              :   /* When substituting into a template, we must set
   14694              :      PROCESSING_TEMPLATE_DECL as the template parameters may be
   14695              :      dependent if they are based on one-another, and the dependency
   14696              :      predicates are short-circuit outside of templates.  */
   14697     28041735 :   ++processing_template_decl;
   14698              : 
   14699     28041735 :   for (new_parms = &r;
   14700    167708294 :        parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
   14701     55868826 :        new_parms = &(TREE_CHAIN (*new_parms)),
   14702     27934413 :          parms = TREE_CHAIN (parms))
   14703              :     {
   14704     27934413 :       tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
   14705              :                                                   args, complain);
   14706     55868826 :       *new_parms =
   14707     55868822 :         tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
   14708              :                              - TMPL_ARGS_DEPTH (args)),
   14709              :                    new_vec, NULL_TREE);
   14710     55868826 :       TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
   14711     55868826 :         = TEMPLATE_PARMS_CONSTRAINTS (parms);
   14712              :     }
   14713              : 
   14714     28041735 :   --processing_template_decl;
   14715              : 
   14716     28041735 :   return r;
   14717              : }
   14718              : 
   14719              : /* Return the result of substituting ARGS into one template parameter
   14720              :    given by T. T Must be a TREE_LIST which TREE_VALUE is the template
   14721              :    parameter and which TREE_PURPOSE is the default argument of the
   14722              :    template parameter.  */
   14723              : 
   14724              : static tree
   14725     41649548 : tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
   14726              : {
   14727     41649548 :   tree default_value, parm_decl;
   14728              : 
   14729     41649548 :   if (args == NULL_TREE
   14730     41649548 :       || t == NULL_TREE
   14731     41649544 :       || t == error_mark_node)
   14732              :     return t;
   14733              : 
   14734     41649544 :   gcc_assert (TREE_CODE (t) == TREE_LIST);
   14735              : 
   14736     41649544 :   default_value = TREE_PURPOSE (t);
   14737     41649544 :   parm_decl = TREE_VALUE (t);
   14738              : 
   14739     41649544 :   parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
   14740     41649544 :   if (TREE_CODE (parm_decl) == PARM_DECL
   14741     41649544 :       && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
   14742           28 :     parm_decl = error_mark_node;
   14743     41649544 :   default_value = tsubst_template_arg (default_value, args,
   14744              :                                        complain, NULL_TREE);
   14745              : 
   14746     41649544 :   tree r = build_tree_list (default_value, parm_decl);
   14747     41649544 :   TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
   14748     41649544 :   return r;
   14749              : }
   14750              : 
   14751              : /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
   14752              :    parameter in PARMS for sake of declaration matching.  */
   14753              : 
   14754              : static void
   14755       651557 : tsubst_each_template_parm_constraints (tree parms, tree args,
   14756              :                                        tsubst_flags_t complain)
   14757              : {
   14758       651557 :   ++processing_template_decl;
   14759      1303114 :   for (; parms; parms = TREE_CHAIN (parms))
   14760              :     {
   14761       651557 :       tree level = TREE_VALUE (parms);
   14762      2047070 :       for (tree parm : tree_vec_range (level))
   14763      2791026 :         TEMPLATE_PARM_CONSTRAINTS (parm)
   14764      2791026 :           = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
   14765              :                                complain, NULL_TREE);
   14766              :     }
   14767       651557 :   --processing_template_decl;
   14768       651557 : }
   14769              : 
   14770              : /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
   14771              :    indexed in reverse order of the parameters.  */
   14772              : 
   14773              : static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
   14774              : 
   14775              : /* Return a reference to the vec* of defarg insts for FN.  */
   14776              : 
   14777              : static vec<tree,va_gc> *&
   14778      1322800 : defarg_insts_for (tree fn)
   14779              : {
   14780      1322800 :   if (!defarg_inst)
   14781        12822 :     defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
   14782      1322800 :   tree_vec_map in = { { fn }, nullptr };
   14783      1322800 :   tree_vec_map **slot
   14784      1322800 :     = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
   14785      1322800 :   if (!*slot)
   14786              :     {
   14787       625841 :       *slot = ggc_alloc<tree_vec_map> ();
   14788       625841 :       **slot = in;
   14789              :     }
   14790      1322800 :   return (*slot)->to;
   14791              : }
   14792              : 
   14793              : /* Substitute into the default argument ARG (a default argument for
   14794              :    FN), which has the indicated TYPE.  */
   14795              : 
   14796              : tree
   14797      1353972 : tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
   14798              :                          tsubst_flags_t complain)
   14799              : {
   14800      1353972 :   int errs = errorcount + sorrycount;
   14801              : 
   14802              :   /* This can happen in invalid code.  */
   14803      1353972 :   if (TREE_CODE (arg) == DEFERRED_PARSE)
   14804              :     return arg;
   14805              : 
   14806              :   /* Shortcut {}.  */
   14807        31269 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg)
   14808      1385144 :       && CONSTRUCTOR_NELTS (arg) == 0)
   14809              :     return arg;
   14810              : 
   14811      1322800 :   tree parm = FUNCTION_FIRST_USER_PARM (fn);
   14812      1322800 :   parm = chain_index (parmnum, parm);
   14813      1322800 :   tree parmtype = TREE_TYPE (parm);
   14814      1322800 :   if (DECL_BY_REFERENCE (parm))
   14815            3 :     parmtype = TREE_TYPE (parmtype);
   14816      1322800 :   if (parmtype == error_mark_node)
   14817              :     return error_mark_node;
   14818              : 
   14819      1322800 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
   14820              : 
   14821              :   /* Remember the location of the pointer to the vec rather than the location
   14822              :      of the particular element, in case the vec grows in tsubst_expr.  */
   14823      1322800 :   vec<tree,va_gc> *&defs = defarg_insts_for (fn);
   14824              :   /* Index in reverse order to avoid allocating space for initial parameters
   14825              :      that don't have default arguments.  */
   14826      1322800 :   unsigned ridx = list_length (parm);
   14827      2019759 :   if (vec_safe_length (defs) < ridx)
   14828       625984 :     vec_safe_grow_cleared (defs, ridx);
   14829       696816 :   else if (tree inst = (*defs)[ridx - 1])
   14830              :     return inst;
   14831              : 
   14832              :   /* This default argument came from a template.  Instantiate the
   14833              :      default argument here, not in tsubst.  In the case of
   14834              :      something like:
   14835              : 
   14836              :        template <class T>
   14837              :        struct S {
   14838              :          static T t();
   14839              :          void f(T = t());
   14840              :        };
   14841              : 
   14842              :      we must be careful to do name lookup in the scope of S<T>,
   14843              :      rather than in the current class.  */
   14844       672416 :   push_to_top_level ();
   14845       672416 :   push_access_scope (fn);
   14846       672416 :   push_deferring_access_checks (dk_no_deferred);
   14847              :   /* So in_immediate_context knows this is a default argument.  */
   14848       672416 :   begin_scope (sk_function_parms, fn);
   14849       672416 :   start_lambda_scope (parm);
   14850              : 
   14851              :   /* The default argument expression may cause implicitly defined
   14852              :      member functions to be synthesized, which will result in garbage
   14853              :      collection.  We must treat this situation as if we were within
   14854              :      the body of function so as to avoid collecting live data on the
   14855              :      stack.  */
   14856       672416 :   ++function_depth;
   14857       672416 :   arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
   14858       672416 :   --function_depth;
   14859              : 
   14860       672416 :   finish_lambda_scope ();
   14861              : 
   14862              :   /* Make sure the default argument is reasonable.  */
   14863       672416 :   arg = check_default_argument (type, arg, complain);
   14864              : 
   14865       672416 :   if (errorcount+sorrycount > errs
   14866       672416 :       && (complain & tf_warning_or_error))
   14867           15 :     inform (input_location,
   14868              :             "  when instantiating default argument for call to %qD", fn);
   14869              : 
   14870       672416 :   leave_scope ();
   14871       672416 :   pop_deferring_access_checks ();
   14872       672416 :   pop_access_scope (fn);
   14873       672416 :   pop_from_top_level ();
   14874              : 
   14875       672416 :   if (arg != error_mark_node && !cp_unevaluated_operand)
   14876       670015 :     (*defs)[ridx - 1] = arg;
   14877              : 
   14878              :   return arg;
   14879              : }
   14880              : 
   14881              : /* Substitute into all the default arguments for FN.  */
   14882              : 
   14883              : static void
   14884       922211 : tsubst_default_arguments (tree fn, tsubst_flags_t complain)
   14885              : {
   14886       922211 :   tree arg;
   14887       922211 :   tree tmpl_args;
   14888              : 
   14889       922211 :   tmpl_args = DECL_TI_ARGS (fn);
   14890              : 
   14891              :   /* If this function is not yet instantiated, we certainly don't need
   14892              :      its default arguments.  */
   14893       922211 :   if (uses_template_parms (tmpl_args))
   14894              :     return;
   14895              :   /* Don't do this again for clones.  */
   14896       922211 :   if (DECL_CLONED_FUNCTION_P (fn))
   14897              :     return;
   14898              : 
   14899       922211 :   int i = 0;
   14900       922211 :   for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
   14901      3659811 :        arg;
   14902      2737600 :        arg = TREE_CHAIN (arg), ++i)
   14903      2737600 :     if (TREE_PURPOSE (arg))
   14904           12 :       TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
   14905           12 :                                                     TREE_VALUE (arg),
   14906           12 :                                                     TREE_PURPOSE (arg),
   14907              :                                                     complain);
   14908              : }
   14909              : 
   14910              : /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier.  */
   14911              : static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
   14912              : 
   14913              : /* Store a pair to EXPLICIT_SPECIFIER_MAP.  */
   14914              : 
   14915              : void
   14916      1534909 : store_explicit_specifier (tree v, tree t)
   14917              : {
   14918      1534909 :   if (!explicit_specifier_map)
   14919        12264 :     explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
   14920      1534909 :   DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
   14921      1534909 :   explicit_specifier_map->put (v, t);
   14922      1534909 : }
   14923              : 
   14924              : /* Lookup an element in EXPLICIT_SPECIFIER_MAP.  */
   14925              : 
   14926              : tree
   14927      1882716 : lookup_explicit_specifier (tree v)
   14928              : {
   14929      1882716 :   return *explicit_specifier_map->get (v);
   14930              : }
   14931              : 
   14932              : /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
   14933              :    FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
   14934              :    are ARG_TYPES, and exception specification is RAISES, and otherwise is
   14935              :    identical to T.  */
   14936              : 
   14937              : static tree
   14938    174610171 : rebuild_function_or_method_type (tree t, tree args, tree return_type,
   14939              :                                  tree arg_types, tree raises,
   14940              :                                  tsubst_flags_t complain)
   14941              : {
   14942    174610171 :   gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
   14943              : 
   14944    174610171 :   tree new_type;
   14945    174610171 :   if (TREE_CODE (t) == FUNCTION_TYPE)
   14946              :     {
   14947     68022914 :       new_type = cp_build_function_type (return_type, arg_types);
   14948     68022914 :       new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
   14949              :     }
   14950              :   else
   14951              :     {
   14952    106587257 :       tree r = TREE_TYPE (TREE_VALUE (arg_types));
   14953              :       /* Don't pick up extra function qualifiers from the basetype.  */
   14954    106587257 :       r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
   14955    106587257 :       if (! MAYBE_CLASS_TYPE_P (r))
   14956              :         {
   14957              :           /* [temp.deduct]
   14958              : 
   14959              :              Type deduction may fail for any of the following
   14960              :              reasons:
   14961              : 
   14962              :              -- Attempting to create "pointer to member of T" when T
   14963              :              is not a class type.  */
   14964            0 :           if (complain & tf_error)
   14965            0 :             error ("creating pointer to member function of non-class type %qT",
   14966              :                    r);
   14967            0 :           return error_mark_node;
   14968              :         }
   14969              : 
   14970    106587257 :       new_type = build_method_type_directly (r, return_type,
   14971    106587257 :                                              TREE_CHAIN (arg_types));
   14972              :     }
   14973    174610171 :   if (!apply_late_template_attributes (&new_type, TYPE_ATTRIBUTES (t), 0,
   14974              :                                        args, complain, NULL_TREE))
   14975            0 :     return error_mark_node;
   14976              : 
   14977    174610171 :   cp_ref_qualifier rqual = type_memfn_rqual (t);
   14978    174610171 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   14979    174610171 :   return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
   14980              : }
   14981              : 
   14982              : /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
   14983              :    each of its formal parameters.  If there is a disagreement then rebuild
   14984              :    DECL's function type according to its formal parameter types, as part of a
   14985              :    resolution for Core issues 1001/1322.  */
   14986              : 
   14987              : static void
   14988    148946345 : maybe_rebuild_function_decl_type (tree decl, tree args)
   14989              : {
   14990    148946345 :   bool function_type_needs_rebuilding = false;
   14991    148946345 :   if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
   14992              :     {
   14993    107116165 :       tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
   14994    396882391 :       while (parm_type_list && parm_type_list != void_list_node)
   14995              :         {
   14996    182650167 :           tree parm_type = TREE_VALUE (parm_type_list);
   14997    182650167 :           tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   14998    182650167 :           if (!same_type_p (parm_type, formal_parm_type_unqual))
   14999              :             {
   15000              :               function_type_needs_rebuilding = true;
   15001              :               break;
   15002              :             }
   15003              : 
   15004    182650061 :           parm_list = DECL_CHAIN (parm_list);
   15005    182650061 :           parm_type_list = TREE_CHAIN (parm_type_list);
   15006              :         }
   15007              :     }
   15008              : 
   15009    107116165 :   if (!function_type_needs_rebuilding)
   15010    148946239 :     return;
   15011              : 
   15012          106 :   const tree fntype = TREE_TYPE (decl);
   15013          106 :   tree parm_list = DECL_ARGUMENTS (decl);
   15014          106 :   tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
   15015          106 :   tree new_parm_type_list = NULL_TREE;
   15016          106 :   tree *q = &new_parm_type_list;
   15017          136 :   for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
   15018              :     {
   15019           30 :       *q = copy_node (old_parm_type_list);
   15020           30 :       parm_list = DECL_CHAIN (parm_list);
   15021           30 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   15022           30 :       q = &TREE_CHAIN (*q);
   15023              :     }
   15024          221 :   while (old_parm_type_list && old_parm_type_list != void_list_node)
   15025              :     {
   15026          115 :       *q = copy_node (old_parm_type_list);
   15027          115 :       tree *new_parm_type = &TREE_VALUE (*q);
   15028          115 :       tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   15029          115 :       if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
   15030          106 :         *new_parm_type = formal_parm_type_unqual;
   15031              : 
   15032          115 :       parm_list = DECL_CHAIN (parm_list);
   15033          115 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   15034          115 :       q = &TREE_CHAIN (*q);
   15035              :     }
   15036          106 :   if (old_parm_type_list == void_list_node)
   15037          106 :     *q = void_list_node;
   15038              : 
   15039          106 :   TREE_TYPE (decl)
   15040          212 :     = rebuild_function_or_method_type (fntype, args,
   15041          106 :                                        TREE_TYPE (fntype), new_parm_type_list,
   15042          106 :                                        TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
   15043              : }
   15044              : 
   15045              : /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL.  */
   15046              : 
   15047              : static tree
   15048    151714248 : tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
   15049              :                       tree lambda_fntype, bool use_spec_table = true)
   15050              : {
   15051    151714248 :   tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
   15052    151714248 :   hashval_t hash = 0;
   15053    151714248 :   tree in_decl = t;
   15054              : 
   15055              :   /* Nobody should be tsubst'ing into non-template functions.  */
   15056    151714248 :   gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
   15057              :               || DECL_LOCAL_DECL_P (t));
   15058              : 
   15059    151714248 :   if (DECL_LOCAL_DECL_P (t))
   15060              :     {
   15061          381 :       if (tree spec = retrieve_local_specialization (t))
   15062              :         return spec;
   15063              :     }
   15064    151713867 :   else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
   15065              :     {
   15066              :       /* If T is not dependent, just return it.  */
   15067    151687266 :       if (!uses_template_parms (DECL_TI_ARGS (t))
   15068    151687332 :           && !LAMBDA_FUNCTION_P (t))
   15069              :         return t;
   15070              : 
   15071              :       /* A non-templated friend doesn't get DECL_TEMPLATE_INFO.  */
   15072    151138406 :       if (non_templated_friend_p (t))
   15073        21915 :         goto friend_case;
   15074              : 
   15075              :       /* Calculate the most general template of which R is a
   15076              :          specialization.  */
   15077    151116491 :       gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
   15078              : 
   15079              :       /* We're substituting a lambda function under tsubst_lambda_expr but not
   15080              :          directly from it; find the matching function we're already inside.
   15081              :          But don't do this if T is a generic lambda with a single level of
   15082              :          template parms, as in that case we're doing a normal instantiation. */
   15083    154128851 :       if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
   15084    151706431 :           && (!generic_lambda_fn_p (t)
   15085       589916 :               || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
   15086           57 :         return enclosing_instantiation_of (t);
   15087              : 
   15088              :       /* Calculate the complete set of arguments used to
   15089              :          specialize R.  */
   15090    151116434 :       if (use_spec_table && !lambda_fntype)
   15091              :         {
   15092     90861412 :           argvec = tsubst_template_args (DECL_TI_ARGS
   15093              :                                          (DECL_TEMPLATE_RESULT
   15094              :                                           (DECL_TI_TEMPLATE (t))),
   15095              :                                          args, complain, in_decl);
   15096     90861412 :           if (argvec == error_mark_node)
   15097              :             return error_mark_node;
   15098              : 
   15099              :           /* Check to see if we already have this specialization.  */
   15100     90861412 :           hash = spec_hasher::hash (gen_tmpl, argvec);
   15101     90861412 :           if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
   15102              :             /* The spec for these args might be a partial instantiation of the
   15103              :                template, but here what we want is the FUNCTION_DECL.  */
   15104       656422 :             return STRIP_TEMPLATE (spec);
   15105              :         }
   15106              :       else
   15107              :         argvec = args;
   15108              :     }
   15109              :   else
   15110              :     {
   15111              :       /* This special case arises when we have something like this:
   15112              : 
   15113              :          template <class T> struct S {
   15114              :            friend void f<int>(int, double);
   15115              :          };
   15116              : 
   15117              :          Here, the DECL_TI_TEMPLATE for the friend declaration
   15118              :          will be an IDENTIFIER_NODE.  We are being called from
   15119              :          tsubst_friend_function, and we want only to create a
   15120              :          new decl (R) with appropriate types so that we can call
   15121              :          determine_specialization.  */
   15122        26601 :     friend_case:
   15123              :       gen_tmpl = NULL_TREE;
   15124              :       argvec = NULL_TREE;
   15125              :     }
   15126              : 
   15127              :   /* Make sure tsubst_decl substitutes all the parameters.  */
   15128    301009682 :   cp_evaluated ev;
   15129              : 
   15130    150508909 :   tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
   15131       538727 :                   : NULL_TREE);
   15132    150508909 :   tree ctx = closure ? closure : DECL_CONTEXT (t);
   15133    150508909 :   bool member = ctx && TYPE_P (ctx);
   15134              : 
   15135              :   /* If this is a static or xobj lambda, remove the 'this' pointer added in
   15136              :      tsubst_lambda_expr now that we know the closure type.  */
   15137    150508909 :   if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15138          162 :     lambda_fntype = static_fn_type (lambda_fntype);
   15139              : 
   15140    150508909 :   if (member && !closure)
   15141    119390056 :     ctx = tsubst_entering_scope (ctx, args, complain, t);
   15142              : 
   15143    150508909 :   tree type = (lambda_fntype ? lambda_fntype
   15144    149970182 :                : tsubst (TREE_TYPE (t), args,
   15145              :                          complain | tf_fndecl_type, in_decl));
   15146    150500773 :   if (type == error_mark_node)
   15147              :     return error_mark_node;
   15148              : 
   15149              :   /* If we hit excessive deduction depth, the type is bogus even if
   15150              :      it isn't error_mark_node, so don't build a decl.  */
   15151    148946372 :   if (excessive_deduction_depth)
   15152              :     return error_mark_node;
   15153              : 
   15154              :   /* We do NOT check for matching decls pushed separately at this
   15155              :      point, as they may not represent instantiations of this
   15156              :      template, and in any case are considered separate under the
   15157              :      discrete model.  */
   15158    148946372 :   tree r = copy_decl (t);
   15159    148946372 :   DECL_USE_TEMPLATE (r) = 0;
   15160    148946372 :   TREE_TYPE (r) = type;
   15161              :   /* Clear out the mangled name and RTL for the instantiation.  */
   15162    148946372 :   SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   15163    148946372 :   SET_DECL_RTL (r, NULL);
   15164              :   /* Leave DECL_INITIAL set on deleted instantiations.  */
   15165    148946372 :   if (!DECL_DELETED_FN (r))
   15166    145846404 :     DECL_INITIAL (r) = NULL_TREE;
   15167    148946372 :   DECL_CONTEXT (r) = ctx;
   15168    148946372 :   set_instantiating_module (r);
   15169              : 
   15170              :   /* Handle explicit(dependent-expr).  */
   15171    148946372 :   if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
   15172              :     {
   15173      1877198 :       tree spec = lookup_explicit_specifier (t);
   15174      1877198 :       spec = tsubst_expr (spec, args, complain, in_decl);
   15175      1877198 :       spec = build_explicit_specifier (spec, complain);
   15176      1877198 :       if (spec == error_mark_node)
   15177              :         return error_mark_node;
   15178      1877177 :       if (instantiation_dependent_expression_p (spec))
   15179      1146594 :         store_explicit_specifier (r, spec);
   15180              :       else
   15181              :         {
   15182       730583 :           DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
   15183       730583 :           DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
   15184              :         }
   15185              :     }
   15186              : 
   15187              :   /* OpenMP UDRs have the only argument a reference to the declared
   15188              :      type.  We want to diagnose if the declared type is a reference,
   15189              :      which is invalid, but as references to references are usually
   15190              :      quietly merged, diagnose it here.  */
   15191    148946351 :   if (DECL_OMP_DECLARE_REDUCTION_P (t))
   15192              :     {
   15193          267 :       tree argtype
   15194          267 :         = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
   15195          267 :       argtype = tsubst (argtype, args, complain, in_decl);
   15196          267 :       if (TYPE_REF_P (argtype))
   15197            6 :         error_at (DECL_SOURCE_LOCATION (t),
   15198              :                   "reference type %qT in "
   15199              :                   "%<#pragma omp declare reduction%>", argtype);
   15200          267 :       if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   15201          210 :         DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
   15202              :                                           argtype);
   15203              :     }
   15204              : 
   15205    148946351 :   if (member && DECL_CONV_FN_P (r))
   15206              :     /* Type-conversion operator.  Reconstruct the name, in
   15207              :        case it's the name of one of the template's parameters.  */
   15208      1450449 :     DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
   15209              : 
   15210    148946351 :   tree parms = DECL_ARGUMENTS (t);
   15211    148946351 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15212       538565 :     parms = DECL_CHAIN (parms);
   15213    148946351 :   parms = tsubst (parms, args, complain, t);
   15214    148946351 :   if (parms == error_mark_node)
   15215              :     return error_mark_node;
   15216    438053833 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   15217    289107488 :     DECL_CONTEXT (parm) = r;
   15218    148946345 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15219              :     {
   15220       538565 :       tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
   15221       538565 :       DECL_NAME (tparm) = closure_identifier;
   15222       538565 :       DECL_CHAIN (tparm) = parms;
   15223       538565 :       parms = tparm;
   15224              :     }
   15225    148946345 :   DECL_ARGUMENTS (r) = parms;
   15226    148946345 :   DECL_RESULT (r) = NULL_TREE;
   15227              : 
   15228    148946345 :   maybe_rebuild_function_decl_type (r, args);
   15229              : 
   15230    148946345 :   TREE_STATIC (r) = 0;
   15231    148946345 :   TREE_PUBLIC (r) = TREE_PUBLIC (t);
   15232    148946345 :   DECL_EXTERNAL (r) = 1;
   15233              :   /* If this is an instantiation of a function with internal
   15234              :      linkage, we already know what object file linkage will be
   15235              :      assigned to the instantiation.  */
   15236    148946345 :   DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
   15237    148946345 :   DECL_DEFER_OUTPUT (r) = 0;
   15238    148946345 :   DECL_CHAIN (r) = NULL_TREE;
   15239    148946345 :   DECL_PENDING_INLINE_INFO (r) = 0;
   15240    148946345 :   DECL_PENDING_INLINE_P (r) = 0;
   15241    148946345 :   DECL_SAVED_TREE (r) = NULL_TREE;
   15242    148946345 :   DECL_STRUCT_FUNCTION (r) = NULL;
   15243    148946345 :   TREE_USED (r) = 0;
   15244              :   /* We'll re-clone as appropriate in instantiate_template.  */
   15245    148946345 :   DECL_CLONED_FUNCTION (r) = NULL_TREE;
   15246              : 
   15247              :   /* If we aren't complaining now, return on error before we register
   15248              :      the specialization so that we'll complain eventually.  */
   15249    148946345 :   if ((complain & tf_error) == 0
   15250     34649122 :       && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   15251    152054521 :       && !grok_op_properties (r, /*complain=*/false))
   15252            3 :     return error_mark_node;
   15253              : 
   15254              :   /* If we are looking at an xobj lambda, we might need to check the type of
   15255              :      its xobj parameter.  */
   15256    151957762 :   if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r))
   15257              :     {
   15258          608 :       tree closure_obj = DECL_CONTEXT (r);
   15259          608 :       tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj);
   15260          608 :       tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r));
   15261              : 
   15262          608 :       if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
   15263          433 :             || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)))
   15264              :         /* If a lambda has an empty capture clause, an xobj parameter of
   15265              :            unrelated type is not an error.  */;
   15266          467 :       else if (dependent_type_p (obj_param))
   15267              :         /* If we are coming from tsubst_lambda_expr we might not have
   15268              :            substituted into our xobj parameter yet.  We can't error out until
   15269              :            we know what the type really is so do nothing...
   15270              :            ...but if we are instantiating the call op for real and we don't
   15271              :            have a real type then something has gone incredibly wrong.  */
   15272          103 :         gcc_assert (lambda_fntype);
   15273              :       else
   15274              :         {
   15275              :           /* We have a lambda with captures, and know the type of the xobj
   15276              :              parameter, time to check it.  */
   15277          364 :           tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param));
   15278          364 :           if (!same_or_base_type_p (closure_obj, obj_param_type))
   15279              :             {
   15280              :               /* This error does not emit when the lambda's call operator
   15281              :                  template is instantiated by taking its address, such as in
   15282              :                  the following case:
   15283              : 
   15284              :                  auto f = [x = 0](this auto&&){};
   15285              :                  int (*fp)(int&) = &decltype(f)::operator();
   15286              : 
   15287              :                  It only emits when explicitly calling the call operator with
   15288              :                  an explicit template parameter:
   15289              : 
   15290              :                  template<typename T>
   15291              :                  struct S : T {
   15292              :                    using T::operator();
   15293              :                    operator int() const {return {};}
   15294              :                  };
   15295              : 
   15296              :                  auto s = S{[x = 0](this auto&&) {}};
   15297              :                  s.operator()<int>();
   15298              : 
   15299              :                  This is due to resolve_address_of_overloaded_function being
   15300              :                  deficient at reporting candidates when overload resolution
   15301              :                  fails.
   15302              : 
   15303              :                  This diagnostic will be active in the first case if/when
   15304              :                  resolve_address_of_overloaded_function is fixed to properly
   15305              :                  emit candidates upon failure to resolve to an overload.  */
   15306           36 :               if (complain & tf_error)
   15307            2 :                 error ("a lambda with captures may not have an explicit "
   15308              :                        "object parameter of an unrelated type");
   15309           36 :               return error_mark_node;
   15310              :             }
   15311              :         }
   15312              :     }
   15313              : 
   15314              :   /* Associate the constraints directly with the instantiation. We
   15315              :      don't substitute through the constraints; that's only done when
   15316              :      they are checked.  */
   15317    148946306 :   if (tree ci = get_constraints (t))
   15318      9394857 :     set_constraints (r, ci);
   15319              : 
   15320              :   /* copy_decl () does not know about contract specifiers.  NOTE these are not
   15321              :      substituted at this point.  */
   15322    148946306 :   if (tree ctrct = get_fn_contract_specifiers (t))
   15323          205 :     set_fn_contract_specifiers (r, ctrct);
   15324              : 
   15325              :   /* The parms have now been substituted, check for incorrect const cases.  */
   15326    148946306 :   check_postconditions_in_redecl (t, r);
   15327              : 
   15328    294559788 :   if (DECL_FRIEND_CONTEXT (t))
   15329      3725884 :     SET_DECL_FRIEND_CONTEXT (r,
   15330              :                              tsubst (DECL_FRIEND_CONTEXT (t),
   15331              :                                      args, complain, in_decl));
   15332              : 
   15333    148946306 :   if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15334              :                                        args, complain, in_decl))
   15335           15 :     return error_mark_node;
   15336              : 
   15337              :   /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
   15338              :      this in the special friend case mentioned above where
   15339              :      GEN_TMPL is NULL.  */
   15340    148946291 :   if (gen_tmpl && !closure)
   15341              :     {
   15342    148358670 :       DECL_TEMPLATE_INFO (r)
   15343    148358670 :         = build_template_info (gen_tmpl, argvec);
   15344    148358670 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   15345              : 
   15346    148358670 :       if (use_spec_table)
   15347              :         {
   15348     90204845 :           tree new_r
   15349     90204845 :             = register_specialization (r, gen_tmpl, argvec, false, hash);
   15350     90204845 :           if (new_r != r)
   15351              :             /* We instantiated this while substituting into
   15352              :                the type earlier (template/friend54.C).  */
   15353              :             return new_r;
   15354              :         }
   15355              : 
   15356              :       /* We're not supposed to instantiate default arguments
   15357              :          until they are called, for a template.  But, for a
   15358              :          declaration like:
   15359              : 
   15360              :          template <class T> void f ()
   15361              :          { extern void g(int i = T()); }
   15362              : 
   15363              :          we should do the substitution when the template is
   15364              :          instantiated.  We handle the member function case in
   15365              :          instantiate_class_template since the default arguments
   15366              :          might refer to other members of the class.  */
   15367    148358670 :       if (!member
   15368     29152562 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   15369    149280881 :           && !uses_template_parms (argvec))
   15370       922211 :         tsubst_default_arguments (r, complain);
   15371              :     }
   15372       587621 :   else if (DECL_LOCAL_DECL_P (r))
   15373              :     {
   15374          381 :       if (!cp_unevaluated_operand)
   15375          381 :         register_local_specialization (r, t);
   15376              :     }
   15377              :   else
   15378       587240 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15379              : 
   15380              :   /* Copy the list of befriending classes.  */
   15381    148946291 :   for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
   15382    149979371 :        *friends;
   15383      1033080 :        friends = &TREE_CHAIN (*friends))
   15384              :     {
   15385      1033080 :       *friends = copy_node (*friends);
   15386      1033080 :       TREE_VALUE (*friends)
   15387      2066160 :         = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
   15388              :     }
   15389              : 
   15390    297892582 :   if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
   15391              :     {
   15392     27443782 :       maybe_retrofit_in_chrg (r);
   15393     54887564 :       if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
   15394            6 :         return error_mark_node;
   15395              :       /* If this is an instantiation of a member template, clone it.
   15396              :          If it isn't, that'll be handled by
   15397              :          clone_constructors_and_destructors.  */
   15398     54887546 :       if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
   15399     10098244 :         clone_cdtor (r, /*update_methods=*/false);
   15400              :     }
   15401    121502509 :   else if ((complain & tf_error) != 0
   15402     88334599 :            && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   15403    142409563 :            && !grok_op_properties (r, /*complain=*/true))
   15404            3 :     return error_mark_node;
   15405              : 
   15406              :   /* Possibly limit visibility based on template args.  */
   15407    148946282 :   DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   15408    148946282 :   if (DECL_VISIBILITY_SPECIFIED (t))
   15409              :     {
   15410    119716027 :       DECL_VISIBILITY_SPECIFIED (r) = 0;
   15411    119716027 :       DECL_ATTRIBUTES (r)
   15412    239432054 :         = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   15413              :     }
   15414    148946282 :   determine_visibility (r);
   15415    148946282 :   if (DECL_SECTION_NAME (t))
   15416            6 :     set_decl_section_name (r, t);
   15417    153553456 :   if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
   15418        42463 :       && COMPLETE_TYPE_P (DECL_CONTEXT (r))
   15419    148946282 :       && !processing_template_decl)
   15420            0 :     defaulted_late_check (r);
   15421              : 
   15422    148946282 :   if (flag_openmp)
   15423       195035 :     if (tree attr = lookup_attribute ("omp declare variant base",
   15424       195035 :                                       DECL_ATTRIBUTES (r)))
   15425          191 :       omp_declare_variant_finalize (r, attr);
   15426              : 
   15427    148946282 :   return r;
   15428              : }
   15429              : 
   15430              : /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL.  */
   15431              : 
   15432              : static tree
   15433     28415545 : tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
   15434              :                       tree lambda_fntype, tree lambda_tparms)
   15435              : {
   15436              :   /* We can get here when processing a member function template,
   15437              :      member class template, or template template parameter.  */
   15438     28415545 :   tree decl = DECL_TEMPLATE_RESULT (t);
   15439     28415545 :   tree in_decl = t;
   15440     28415545 :   tree spec;
   15441     28415545 :   tree tmpl_args;
   15442     28415545 :   tree full_args = NULL_TREE;
   15443     28415545 :   tree r;
   15444     28415545 :   hashval_t hash = 0;
   15445              : 
   15446     28415545 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   15447              :     {
   15448              :       /* Template template parameter is treated here.  */
   15449        18304 :       tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15450        18304 :       if (new_type == error_mark_node)
   15451              :         r = error_mark_node;
   15452              :       /* If we get a real template back, return it.  This can happen in
   15453              :          the context of most_specialized_partial_spec.  */
   15454        18304 :       else if (TREE_CODE (new_type) == TEMPLATE_DECL)
   15455              :         r = new_type;
   15456              :       else
   15457              :         /* The new TEMPLATE_DECL was built in
   15458              :            reduce_template_parm_level.  */
   15459        36608 :         r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
   15460        18304 :       return r;
   15461              :     }
   15462              : 
   15463     28397241 :   if (!lambda_fntype)
   15464              :     {
   15465              :       /* We might already have an instance of this template.
   15466              :          The ARGS are for the surrounding class type, so the
   15467              :          full args contain the tsubst'd args for the context,
   15468              :          plus the innermost args from the template decl.  */
   15469     28318638 :       tmpl_args = DECL_CLASS_TEMPLATE_P (t)
   15470      2343351 :         ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
   15471     25975287 :         : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
   15472              :       /* Because this is a template, the arguments will still be
   15473              :          dependent, even after substitution.  If
   15474              :          PROCESSING_TEMPLATE_DECL is not set, the dependency
   15475              :          predicates will short-circuit.  */
   15476     28318638 :       ++processing_template_decl;
   15477     28318638 :       full_args = tsubst_template_args (tmpl_args, args,
   15478              :                                         complain, in_decl);
   15479     28318638 :       --processing_template_decl;
   15480     28318638 :       if (full_args == error_mark_node)
   15481              :         return error_mark_node;
   15482              : 
   15483              :       /* If this is a default template template argument,
   15484              :          tsubst might not have changed anything.  */
   15485     28318620 :       if (full_args == tmpl_args)
   15486              :         return t;
   15487              : 
   15488     28318620 :       hash = spec_hasher::hash (t, full_args);
   15489     28318620 :       spec = retrieve_specialization (t, full_args, hash);
   15490     28318620 :       if (spec != NULL_TREE)
   15491              :         {
   15492       943769 :           if (TYPE_P (spec))
   15493              :             /* Type partial instantiations are stored as the type by
   15494              :                lookup_template_class_1, not here as the template.  */
   15495       943207 :             spec = CLASSTYPE_TI_TEMPLATE (spec);
   15496          562 :           else if (TREE_CODE (spec) != TEMPLATE_DECL)
   15497          466 :             spec = DECL_TI_TEMPLATE (spec);
   15498       943769 :           return spec;
   15499              :         }
   15500              :     }
   15501              : 
   15502              :   /* Make a new template decl.  It will be similar to the
   15503              :      original, but will record the current template arguments.
   15504              :      We also create a new function declaration, which is just
   15505              :      like the old one, but points to this new template, rather
   15506              :      than the old one.  */
   15507     27453454 :   r = copy_decl (t);
   15508     27453454 :   gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
   15509     27453454 :   DECL_CHAIN (r) = NULL_TREE;
   15510              : 
   15511              :   // Build new template info linking to the original template decl.
   15512     27453454 :   if (!lambda_fntype)
   15513              :     {
   15514     27374851 :       DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   15515     27374851 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   15516              :     }
   15517              :   else
   15518        78603 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15519              : 
   15520              :   /* The template parameters for this new template are all the
   15521              :      template parameters for the old template, except the
   15522              :      outermost level of parameters.  */
   15523     27453454 :   auto tparm_guard = make_temp_override (current_template_parms);
   15524     54906908 :   DECL_TEMPLATE_PARMS (r)
   15525     54906908 :     = current_template_parms
   15526     27453454 :     = (lambda_tparms
   15527     27453454 :        ? lambda_tparms
   15528     27374851 :        : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
   15529              :                                 complain));
   15530              : 
   15531     27453454 :   bool class_p = false;
   15532     27453454 :   tree inner = decl;
   15533     27453454 :   ++processing_template_decl;
   15534     27453454 :   if (TREE_CODE (inner) == FUNCTION_DECL)
   15535     23583833 :     inner = tsubst_function_decl (inner, args, complain, lambda_fntype,
   15536              :                                   /*use_spec_table=*/false);
   15537              :   else
   15538              :     {
   15539      3869621 :       if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
   15540              :         {
   15541      1400129 :           class_p = true;
   15542      1400129 :           inner = TREE_TYPE (inner);
   15543              :         }
   15544      3869621 :       if (class_p)
   15545      1400129 :         inner = tsubst_entering_scope (inner, args, complain, in_decl);
   15546              :       else
   15547      2469492 :         inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false);
   15548              :     }
   15549     27453454 :   --processing_template_decl;
   15550     27453454 :   if (inner == error_mark_node)
   15551              :     return error_mark_node;
   15552              : 
   15553     27453396 :   if (class_p)
   15554              :     {
   15555              :       /* For a partial specialization, we need to keep pointing to
   15556              :          the primary template.  */
   15557      1400111 :       if (!DECL_TEMPLATE_SPECIALIZATION (t))
   15558              :         {
   15559       755860 :           CLASSTYPE_TI_TEMPLATE (inner) = r;
   15560       755860 :           CLASSTYPE_USE_TEMPLATE (inner) = 0;
   15561              :         }
   15562              : 
   15563      1400111 :       DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
   15564      1400111 :       inner = TYPE_MAIN_DECL (inner);
   15565              :     }
   15566     26053285 :   else if (lambda_fntype)
   15567              :     {
   15568        78603 :       tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
   15569        78603 :       DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
   15570              :     }
   15571              :   else
   15572              :     {
   15573     25974682 :       DECL_TI_TEMPLATE (inner) = r;
   15574              :       /* Set DECL_TI_ARGS to the full set of template arguments,
   15575              :          which tsubst_function_decl / tsubst_decl didn't do due to
   15576              :          use_spec_table=false.  */
   15577     25974682 :       DECL_TI_ARGS (inner) = full_args;
   15578     25974682 :       DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
   15579              :     }
   15580              : 
   15581     27453396 :   DECL_TEMPLATE_RESULT (r) = inner;
   15582     27453396 :   TREE_TYPE (r) = TREE_TYPE (inner);
   15583     27453396 :   DECL_CONTEXT (r) = DECL_CONTEXT (inner);
   15584              : 
   15585     27453396 :   if (modules_p ())
   15586              :     {
   15587              :       /* Propagate module information from the decl.  */
   15588        58560 :       DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
   15589        58560 :       if (DECL_LANG_SPECIFIC (inner))
   15590              :         /* If this is a constrained template, the above tsubst of
   15591              :            inner can find the unconstrained template, which may have
   15592              :            come from an import.  This is ok, because we don't
   15593              :            register this instantiation (see below).  */
   15594        56402 :         gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
   15595              :                              || (TEMPLATE_PARMS_CONSTRAINTS
   15596              :                                  (DECL_TEMPLATE_PARMS (t))));
   15597              :     }
   15598              : 
   15599     27453396 :   DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
   15600     27453396 :   DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
   15601              : 
   15602     27453396 :   if (PRIMARY_TEMPLATE_P (t))
   15603     26807089 :     DECL_PRIMARY_TEMPLATE (r) = r;
   15604              : 
   15605     27453396 :   if (!lambda_fntype && !class_p)
   15606              :     {
   15607              :       /* Record this non-type partial instantiation.  */
   15608              :       /* FIXME we'd like to always register the TEMPLATE_DECL, or always
   15609              :          the DECL_TEMPLATE_RESULT, but it seems the modules code relies
   15610              :          on this current behavior.  */
   15611     25974682 :       if (TREE_CODE (inner) == FUNCTION_DECL)
   15612     23505190 :         register_specialization (r, t, full_args, false, hash);
   15613              :       else
   15614      2469492 :         register_specialization (inner, t, full_args, false, hash);
   15615              :     }
   15616              : 
   15617              :   return r;
   15618     27453454 : }
   15619              : 
   15620              : /* True if FN is the op() for a lambda in an uninstantiated template.  */
   15621              : 
   15622              : bool
   15623   1208783081 : lambda_fn_in_template_p (tree fn)
   15624              : {
   15625   1219585548 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15626              :     return false;
   15627      2917611 :   tree closure = DECL_CONTEXT (fn);
   15628      2917611 :   return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
   15629              : }
   15630              : 
   15631              : /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
   15632              :    which the above is true.  */
   15633              : 
   15634              : bool
   15635   1088673047 : regenerated_lambda_fn_p (tree fn)
   15636              : {
   15637   1098847555 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15638              :     return false;
   15639      3579824 :   tree closure = DECL_CONTEXT (fn);
   15640      3579824 :   tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
   15641      3579824 :   return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
   15642              : }
   15643              : 
   15644              : /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
   15645              :    If T is not a regenerated LAMBDA_EXPR, return T.  */
   15646              : 
   15647              : tree
   15648      1637152 : most_general_lambda (tree t)
   15649              : {
   15650      3277952 :   while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15651      1640800 :     t = TI_TEMPLATE (ti);
   15652      1637152 :   return t;
   15653              : }
   15654              : 
   15655              : /* Return the set of template arguments used to regenerate the lambda T
   15656              :    from its most general lambda.  */
   15657              : 
   15658              : tree
   15659      1040984 : lambda_regenerating_args (tree t)
   15660              : {
   15661      2081968 :   if (LAMBDA_FUNCTION_P (t))
   15662      1040984 :     t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
   15663      1040984 :   gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
   15664      1040984 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15665      1040984 :     return TI_ARGS (ti);
   15666              :   else
   15667              :     return NULL_TREE;
   15668              : }
   15669              : 
   15670              : /* We're instantiating a variable from template function TCTX.  Return the
   15671              :    corresponding current enclosing scope.  We can match them up using
   15672              :    DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
   15673              :    the DECL_SOURCE_LOCATION for a function instantiation is updated to match
   15674              :    the template definition in regenerate_decl_from_template.  */
   15675              : 
   15676              : static tree
   15677       132251 : enclosing_instantiation_of (tree tctx)
   15678              : {
   15679       132251 :   tree fn = current_function_decl;
   15680              : 
   15681              :   /* We shouldn't ever need to do this for other artificial functions.  */
   15682       132933 :   gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
   15683              : 
   15684       132254 :   for (; fn; fn = decl_function_context (fn))
   15685       132254 :     if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
   15686       132251 :       return fn;
   15687            0 :   gcc_unreachable ();
   15688              : }
   15689              : 
   15690              : /* Substitute the ARGS into the T, which is a _DECL.  Return the
   15691              :    result of the substitution.  Issue error and warning messages under
   15692              :    control of COMPLAIN.  The flag USE_SPEC_TABLE controls if we look up
   15693              :    and insert into the specializations table or if we can assume it's
   15694              :    the caller's responsibility; this is used by instantiate_template
   15695              :    to avoid doing some redundant work.  */
   15696              : 
   15697              : static tree
   15698    894821718 : tsubst_decl (tree t, tree args, tsubst_flags_t complain,
   15699              :              bool use_spec_table /* = true */)
   15700              : {
   15701              : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   15702    894821718 :   location_t saved_loc;
   15703    894821718 :   tree r = NULL_TREE;
   15704    894821718 :   tree in_decl = t;
   15705    894821718 :   hashval_t hash = 0;
   15706              : 
   15707    894821718 :   if (t == error_mark_node)
   15708              :     return error_mark_node;
   15709              : 
   15710              :   /* Set the filename and linenumber to improve error-reporting.  */
   15711    894821715 :   saved_loc = input_location;
   15712    894821715 :   input_location = DECL_SOURCE_LOCATION (t);
   15713              : 
   15714    894821715 :   switch (TREE_CODE (t))
   15715              :     {
   15716     28336942 :     case TEMPLATE_DECL:
   15717     28336942 :       r = tsubst_template_decl (t, args, complain,
   15718              :                                 /*lambda_fntype=*/NULL_TREE,
   15719              :                                 /*lambda_tparms=*/NULL_TREE);
   15720     28336942 :       break;
   15721              : 
   15722    127670291 :     case FUNCTION_DECL:
   15723    127670291 :       r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE,
   15724              :                                 use_spec_table);
   15725    127662155 :       break;
   15726              : 
   15727    338038292 :     case PARM_DECL:
   15728    338038292 :       {
   15729    338038292 :         tree type = NULL_TREE;
   15730    338038292 :         int i, len = 1;
   15731    338038292 :         tree expanded_types = NULL_TREE;
   15732    338038292 :         tree prev_r = NULL_TREE;
   15733    338038292 :         tree first_r = NULL_TREE;
   15734              : 
   15735    338038292 :         if (DECL_PACK_P (t))
   15736              :           {
   15737              :             /* If there is a local specialization that isn't a
   15738              :                parameter pack, it means that we're doing a "simple"
   15739              :                substitution from inside tsubst_pack_expansion. Just
   15740              :                return the local specialization (which will be a single
   15741              :                parm).  */
   15742      3722329 :             tree spec = retrieve_local_specialization (t);
   15743      3722329 :             if (spec
   15744           39 :                 && TREE_CODE (spec) == PARM_DECL
   15745      3722338 :                 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
   15746            0 :               RETURN (spec);
   15747              : 
   15748              :             /* Expand the TYPE_PACK_EXPANSION that provides the types for
   15749              :                the parameters in this function parameter pack.  */
   15750      3722329 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   15751              :                                                     complain, in_decl);
   15752      3722329 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   15753              :               {
   15754      1659115 :                 len = TREE_VEC_LENGTH (expanded_types);
   15755              : 
   15756              :                 /* Zero-length parameter packs are boring. Just substitute
   15757              :                    into the chain.  */
   15758      1659115 :                 if (len == 0 && !cp_unevaluated_operand)
   15759       608679 :                   RETURN (tsubst (TREE_CHAIN (t), args, complain,
   15760              :                                   TREE_CHAIN (t)));
   15761              :               }
   15762              :             else
   15763              :               {
   15764              :                 /* All we did was update the type. Make a note of that.  */
   15765              :                 type = expanded_types;
   15766              :                 expanded_types = NULL_TREE;
   15767              :               }
   15768              :           }
   15769              : 
   15770              :         /* Loop through all of the parameters we'll build. When T is
   15771              :            a function parameter pack, LEN is the number of expanded
   15772              :            types in EXPANDED_TYPES; otherwise, LEN is 1.  */
   15773    337429613 :         r = NULL_TREE;
   15774    675232603 :         for (i = 0; i < len; ++i)
   15775              :           {
   15776    337802996 :             prev_r = r;
   15777    337802996 :             r = copy_node (t);
   15778    337802996 :             if (DECL_TEMPLATE_PARM_P (t))
   15779      2144494 :               SET_DECL_TEMPLATE_PARM_P (r);
   15780              : 
   15781    337802996 :             if (expanded_types)
   15782              :               /* We're on the Ith parameter of the function parameter
   15783              :                  pack.  */
   15784              :               {
   15785              :                 /* Get the Ith type.  */
   15786      1423819 :                 type = TREE_VEC_ELT (expanded_types, i);
   15787              : 
   15788              :                 /* Rename the parameter to include the index.  */
   15789      1423819 :                 DECL_NAME (r)
   15790      2847638 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   15791              :               }
   15792    336379177 :             else if (!type)
   15793              :               /* We're dealing with a normal parameter.  */
   15794    334315963 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15795              : 
   15796    337802996 :             if (type == error_mark_node && !(complain & tf_error))
   15797            6 :               RETURN (error_mark_node);
   15798              : 
   15799    337802990 :             type = type_decays_to (type);
   15800    337802990 :             TREE_TYPE (r) = type;
   15801    337802990 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15802              : 
   15803    337802990 :             if (DECL_INITIAL (r))
   15804              :               {
   15805     49352914 :                 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
   15806     47208420 :                   DECL_INITIAL (r) = TREE_TYPE (r);
   15807              :                 else
   15808      2144494 :                   DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
   15809              :                                              complain, in_decl);
   15810              :               }
   15811              : 
   15812    337802990 :             DECL_CONTEXT (r) = NULL_TREE;
   15813              : 
   15814    337802990 :             if (!DECL_TEMPLATE_PARM_P (r))
   15815    335658496 :               DECL_ARG_TYPE (r) = type_passed_as (type);
   15816              : 
   15817    337802990 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15818              :                                                  args, complain, in_decl))
   15819            0 :               return error_mark_node;
   15820              : 
   15821              :             /* Keep track of the first new parameter we
   15822              :                generate. That's what will be returned to the
   15823              :                caller.  */
   15824    337802990 :             if (!first_r)
   15825    337429488 :               first_r = r;
   15826              : 
   15827              :             /* Build a proper chain of parameters when substituting
   15828              :                into a function parameter pack.  */
   15829    337802990 :             if (prev_r)
   15830       373502 :               DECL_CHAIN (prev_r) = r;
   15831              :           }
   15832              : 
   15833              :         /* If cp_unevaluated_operand is set, we're just looking for a
   15834              :            single dummy parameter, so don't keep going.  */
   15835    337429607 :         if (DECL_CHAIN (t) && !cp_unevaluated_operand)
   15836              :           {
   15837    170314386 :             tree chain = tsubst (DECL_CHAIN (t), args,
   15838    170314386 :                                  complain, DECL_CHAIN (t));
   15839    170314386 :             if (chain == error_mark_node)
   15840            3 :               RETURN (error_mark_node);
   15841    170314383 :             DECL_CHAIN (r) = chain;
   15842              :           }
   15843              : 
   15844              :         /* FIRST_R contains the start of the chain we've built.  */
   15845    337429604 :         r = first_r;
   15846              :       }
   15847    337429604 :       break;
   15848              : 
   15849     10041122 :     case FIELD_DECL:
   15850     10041122 :       {
   15851     10041122 :         tree type = NULL_TREE;
   15852     10041122 :         tree vec = NULL_TREE;
   15853     10041122 :         tree expanded_types = NULL_TREE;
   15854     10041122 :         int len = 1;
   15855              : 
   15856     10041122 :         if (PACK_EXPANSION_P (TREE_TYPE (t)))
   15857              :           {
   15858              :             /* This field is a lambda capture pack.  Return a TREE_VEC of
   15859              :                the expanded fields to instantiate_class_template_1.  */
   15860          361 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   15861              :                                                     complain, in_decl);
   15862          361 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   15863              :               {
   15864          352 :                 len = TREE_VEC_LENGTH (expanded_types);
   15865          352 :                 vec = make_tree_vec (len);
   15866              :               }
   15867              :             else
   15868              :               {
   15869              :                 /* All we did was update the type. Make a note of that.  */
   15870              :                 type = expanded_types;
   15871              :                 expanded_types = NULL_TREE;
   15872              :               }
   15873              :           }
   15874              : 
   15875     20082393 :         for (int i = 0; i < len; ++i)
   15876              :           {
   15877     10041432 :             r = copy_decl (t);
   15878     10041432 :             if (expanded_types)
   15879              :               {
   15880          662 :                 type = TREE_VEC_ELT (expanded_types, i);
   15881          662 :                 DECL_NAME (r)
   15882         1324 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   15883              :               }
   15884     10040770 :             else if (!type)
   15885     10040761 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15886              : 
   15887     10041426 :             if (type == error_mark_node)
   15888          155 :               RETURN (error_mark_node);
   15889     10041271 :             TREE_TYPE (r) = type;
   15890     10041271 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15891              : 
   15892     10041271 :             if (DECL_C_BIT_FIELD (r))
   15893              :               /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
   15894              :                  number of bits.  */
   15895       483568 :               DECL_BIT_FIELD_REPRESENTATIVE (r)
   15896       483568 :                 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
   15897              :                                complain, in_decl);
   15898     10041271 :             if (DECL_INITIAL (t))
   15899              :               {
   15900              :                 /* Set up DECL_TEMPLATE_INFO so that we can get at the
   15901              :                    NSDMI in perform_member_init.  Still set DECL_INITIAL
   15902              :                    so that we know there is one.  */
   15903       927133 :                 DECL_INITIAL (r) = void_node;
   15904       927133 :                 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
   15905       927133 :                 retrofit_lang_decl (r);
   15906       927133 :                 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   15907              :               }
   15908              :             /* We don't have to set DECL_CONTEXT here; it is set by
   15909              :                finish_member_declaration.  */
   15910     10041271 :             DECL_CHAIN (r) = NULL_TREE;
   15911              : 
   15912     10041271 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15913              :                                                  args, complain, in_decl))
   15914            0 :               return error_mark_node;
   15915              : 
   15916     10041271 :             if (vec)
   15917          662 :               TREE_VEC_ELT (vec, i) = r;
   15918              :           }
   15919              : 
   15920     10040961 :         if (vec)
   15921          352 :           r = vec;
   15922              :       }
   15923              :       break;
   15924              : 
   15925      1658958 :     case USING_DECL:
   15926              :       /* We reach here only for member using decls.  We also need to check
   15927              :          uses_template_parms because DECL_DEPENDENT_P is not set for a
   15928              :          using-declaration that designates a member of the current
   15929              :          instantiation (c++/53549).  */
   15930      1658958 :       if (DECL_DEPENDENT_P (t)
   15931      1658958 :           || uses_template_parms (USING_DECL_SCOPE (t)))
   15932              :         {
   15933              :           /* True iff this using-decl was written as a pack expansion
   15934              :              (and a pack appeared in its scope or name).  If a pack
   15935              :              appeared in both, we expand the packs separately and
   15936              :              manually merge them.  */
   15937      1658725 :           bool variadic_p = false;
   15938              : 
   15939      1658725 :           tree scope = USING_DECL_SCOPE (t);
   15940      1658725 :           if (PACK_EXPANSION_P (scope))
   15941              :             {
   15942          262 :               scope = tsubst_pack_expansion (scope, args,
   15943              :                                              complain | tf_qualifying_scope,
   15944              :                                              in_decl);
   15945          262 :               variadic_p = true;
   15946              :             }
   15947              :           else
   15948      1658463 :             scope = tsubst_scope (scope, args, complain, in_decl);
   15949              : 
   15950      1658725 :           tree name = DECL_NAME (t);
   15951      1658725 :           if (IDENTIFIER_CONV_OP_P (name)
   15952      1658725 :               && PACK_EXPANSION_P (TREE_TYPE (name)))
   15953              :             {
   15954           33 :               name = tsubst_pack_expansion (TREE_TYPE (name), args,
   15955              :                                             complain, in_decl);
   15956           33 :               if (name == error_mark_node)
   15957              :                 {
   15958            0 :                   r = error_mark_node;
   15959            0 :                   break;
   15960              :                 }
   15961           84 :               for (tree& elt : tree_vec_range (name))
   15962           51 :                 elt = make_conv_op_name (elt);
   15963           33 :               variadic_p = true;
   15964              :             }
   15965              :           else
   15966      3317384 :             name = tsubst_name (name, args, complain, in_decl);
   15967              : 
   15968      1658725 :           int len;
   15969      1658725 :           if (!variadic_p)
   15970              :             len = 1;
   15971          271 :           else if (TREE_CODE (scope) == TREE_VEC
   15972          262 :                    && TREE_CODE (name) == TREE_VEC)
   15973              :             {
   15974           24 :               if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
   15975              :                 {
   15976            3 :                   error ("mismatched argument pack lengths (%d vs %d)",
   15977            3 :                          TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
   15978            3 :                   r = error_mark_node;
   15979            3 :                   break;
   15980              :                 }
   15981              :               len = TREE_VEC_LENGTH (scope);
   15982              :             }
   15983          247 :           else if (TREE_CODE (scope) == TREE_VEC)
   15984          238 :             len = TREE_VEC_LENGTH (scope);
   15985              :           else /* TREE_CODE (name) == TREE_VEC  */
   15986            9 :             len = TREE_VEC_LENGTH (name);
   15987              : 
   15988      1658722 :           r = make_tree_vec (len);
   15989      3317818 :           for (int i = 0; i < len; ++i)
   15990              :             {
   15991      1659126 :               tree escope = (TREE_CODE (scope) == TREE_VEC
   15992      1659126 :                              ? TREE_VEC_ELT (scope, i)
   15993          660 :                              : scope);
   15994      1659126 :               tree ename = (TREE_CODE (name) == TREE_VEC
   15995      1659126 :                             ? TREE_VEC_ELT (name, i)
   15996           45 :                             : name);
   15997      1659126 :               tree elt = do_class_using_decl (escope, ename);
   15998      1659126 :               if (!elt)
   15999              :                 {
   16000           30 :                   r = error_mark_node;
   16001           30 :                   break;
   16002              :                 }
   16003      1659096 :               TREE_PROTECTED (elt) = TREE_PROTECTED (t);
   16004      1659096 :               TREE_PRIVATE (elt) = TREE_PRIVATE (t);
   16005      1659096 :               TREE_VEC_ELT (r, i) = elt;
   16006              :             }
   16007              : 
   16008      1658722 :           if (!variadic_p && r != error_mark_node)
   16009      1658424 :             r = TREE_VEC_ELT (r, 0);
   16010              :         }
   16011              :       else
   16012              :         {
   16013          233 :           r = copy_node (t);
   16014          233 :           DECL_CHAIN (r) = NULL_TREE;
   16015              :         }
   16016              :       break;
   16017              : 
   16018    282478220 :     case TYPE_DECL:
   16019    282478220 :     case VAR_DECL:
   16020    282478220 :       {
   16021    282478220 :         tree argvec = NULL_TREE;
   16022    282478220 :         tree gen_tmpl = NULL_TREE;
   16023    282478220 :         tree tmpl = NULL_TREE;
   16024    282478220 :         tree type = NULL_TREE;
   16025              : 
   16026    282478220 :         if (TREE_TYPE (t) == error_mark_node)
   16027           11 :           RETURN (error_mark_node);
   16028              : 
   16029    282478209 :         if (TREE_CODE (t) == TYPE_DECL
   16030    282478209 :             && (TREE_CODE (TREE_TYPE (t)) == TU_LOCAL_ENTITY
   16031    225597475 :                 || t == TYPE_MAIN_DECL (TREE_TYPE (t))))
   16032              :           {
   16033              :             /* If this is the canonical decl, we don't have to
   16034              :                mess with instantiations, and often we can't (for
   16035              :                typename, template type parms and such).  Note that
   16036              :                TYPE_NAME is not correct for the above test if
   16037              :                we've copied the type for a typedef.  */
   16038     41561431 :             type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16039     41561386 :             if (type == error_mark_node)
   16040            9 :               RETURN (error_mark_node);
   16041     41561377 :             r = TYPE_NAME (type);
   16042     41561377 :             break;
   16043              :           }
   16044              : 
   16045              :         /* Check to see if we already have the specialization we
   16046              :            need.  */
   16047    240916778 :         tree spec = NULL_TREE;
   16048    240916778 :         bool local_p = false;
   16049    240916778 :         tree ctx = DECL_CONTEXT (t);
   16050     56880731 :         if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
   16051    297797425 :             && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
   16052              :           {
   16053    209894156 :             local_p = false;
   16054    209894156 :             if (DECL_CLASS_SCOPE_P (t))
   16055              :               {
   16056    135214680 :                 ctx = tsubst_entering_scope (ctx, args, complain, in_decl);
   16057    135214680 :                 if (DECL_SELF_REFERENCE_P (t))
   16058              :                   /* The context and type of an injected-class-name are
   16059              :                      the same, so we don't need to substitute both.  */
   16060              :                   type = ctx;
   16061              :                 /* If CTX is unchanged, then T is in fact the
   16062              :                    specialization we want.  That situation occurs when
   16063              :                    referencing a static data member within in its own
   16064              :                    class.  We can use pointer equality, rather than
   16065              :                    same_type_p, because DECL_CONTEXT is always
   16066              :                    canonical...  */
   16067    135214680 :                 if (ctx == DECL_CONTEXT (t)
   16068              :                     /* ... unless T is a member template; in which
   16069              :                        case our caller can be willing to create a
   16070              :                        specialization of that template represented
   16071              :                        by T.  */
   16072    135214680 :                     && !(DECL_TI_TEMPLATE (t)
   16073     33331023 :                          && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
   16074              :                   spec = t;
   16075              :               }
   16076              : 
   16077              :             if (!spec)
   16078              :               {
   16079    205149928 :                 tmpl = DECL_TI_TEMPLATE (t);
   16080    205149928 :                 if (use_spec_table)
   16081              :                   {
   16082     97147678 :                     argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
   16083     97147678 :                     if (argvec == error_mark_node)
   16084            0 :                       RETURN (error_mark_node);
   16085     97147678 :                     gen_tmpl = most_general_template (tmpl);
   16086     97147678 :                     hash = spec_hasher::hash (gen_tmpl, argvec);
   16087     97147678 :                     spec = retrieve_specialization (gen_tmpl, argvec, hash);
   16088              :                   }
   16089              :                 else
   16090              :                   argvec = args;
   16091              :               }
   16092              :           }
   16093              :         else
   16094              :           {
   16095     31022622 :             if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
   16096              :               /* Subsequent calls to pushdecl will fill this in.  */
   16097              :               ctx = NULL_TREE;
   16098              :             /* A local variable.  */
   16099     31022622 :             local_p = true;
   16100              :             /* Unless this is a reference to a static variable from an
   16101              :                enclosing function, in which case we need to fill it in now.  */
   16102     31022622 :             if (TREE_STATIC (t))
   16103              :               {
   16104       131566 :                 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
   16105       131566 :                 if (fn != current_function_decl)
   16106     31022622 :                   ctx = fn;
   16107              :               }
   16108     31022622 :             spec = retrieve_local_specialization (t);
   16109              :           }
   16110              :         /* If we already have the specialization we need, there is
   16111              :            nothing more to do.  */
   16112    236172550 :         if (spec)
   16113              :           {
   16114      8356541 :             r = spec;
   16115      8356541 :             break;
   16116              :           }
   16117              : 
   16118              :         /* Create a new node for the specialization we need.  */
   16119    232560237 :         if (type == NULL_TREE)
   16120              :           {
   16121    187951068 :             if (is_typedef_decl (t))
   16122              :               type = DECL_ORIGINAL_TYPE (t);
   16123              :             else
   16124     48524340 :               type = TREE_TYPE (t);
   16125    187951068 :             if (VAR_P (t)
   16126     48524193 :                 && VAR_HAD_UNKNOWN_BOUND (t)
   16127    187951445 :                 && type != error_mark_node)
   16128          377 :               type = strip_array_domain (type);
   16129    187951068 :             tsubst_flags_t tcomplain = complain;
   16130    187951068 :             if (VAR_P (t))
   16131     48524193 :               tcomplain |= tf_tst_ok;
   16132    187951068 :             if (DECL_DECOMPOSITION_P (t) && DECL_PACK_P (t))
   16133              :               type = NULL_TREE;
   16134              :             else
   16135    187947718 :               type = tsubst (type, args, tcomplain, in_decl);
   16136              :             /* Substituting the type might have recursively instantiated this
   16137              :                same alias (c++/86171).  */
   16138     48926519 :             if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   16139    233012745 :                 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
   16140              :               {
   16141            0 :                 r = spec;
   16142            0 :                 break;
   16143              :               }
   16144              :           }
   16145    232560237 :         if (type == error_mark_node && !(complain & tf_error))
   16146     55274067 :           RETURN (error_mark_node);
   16147    177286170 :         r = copy_decl (t);
   16148    177286170 :         if (VAR_P (r))
   16149              :           {
   16150     48524193 :             DECL_INITIALIZED_P (r) = 0;
   16151     48524193 :             DECL_TEMPLATE_INSTANTIATED (r) = 0;
   16152     48524193 :             if (DECL_DECOMPOSITION_P (t) && DECL_PACK_P (t))
   16153              :               {
   16154         3350 :                 tree dtype = cxx_make_type (DECLTYPE_TYPE);
   16155         3350 :                 DECLTYPE_TYPE_EXPR (dtype) = r;
   16156         3350 :                 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (dtype) = 1;
   16157         3350 :                 SET_TYPE_STRUCTURAL_EQUALITY (dtype);
   16158         3350 :                 type = cxx_make_type (TYPE_PACK_EXPANSION);
   16159         3350 :                 PACK_EXPANSION_PATTERN (type) = dtype;
   16160         3350 :                 SET_TYPE_STRUCTURAL_EQUALITY (type);
   16161         6700 :                 PACK_EXPANSION_PARAMETER_PACKS (type) = r;
   16162              :               }
   16163     48524193 :             if (TREE_CODE (type) == FUNCTION_TYPE)
   16164              :               {
   16165              :                 /* It may seem that this case cannot occur, since:
   16166              : 
   16167              :                    typedef void f();
   16168              :                    void g() { f x; }
   16169              : 
   16170              :                    declares a function, not a variable.  However:
   16171              : 
   16172              :                    typedef void f();
   16173              :                    template <typename T> void g() { T t; }
   16174              :                    template void g<f>();
   16175              : 
   16176              :                    is an attempt to declare a variable with function
   16177              :                    type.  */
   16178            3 :                 error ("variable %qD has function type",
   16179              :                        /* R is not yet sufficiently initialized, so we
   16180              :                           just use its name.  */
   16181            3 :                        DECL_NAME (r));
   16182            3 :                 RETURN (error_mark_node);
   16183              :               }
   16184     48524190 :             type = complete_type (type);
   16185              :             /* Wait until cp_finish_decl to set this again, to handle
   16186              :                circular dependency (template/instantiate6.C). */
   16187     48524187 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
   16188     48524187 :             type = check_var_type (DECL_NAME (r), type,
   16189     48524187 :                                    DECL_SOURCE_LOCATION (r));
   16190     48524187 :             if (DECL_HAS_VALUE_EXPR_P (t))
   16191              :               {
   16192       822828 :                 tree ve = DECL_VALUE_EXPR (t);
   16193              :                 /* If the DECL_VALUE_EXPR is converted to the declared type,
   16194              :                    preserve the identity so that gimplify_type_sizes works.  */
   16195       822828 :                 bool nop = (TREE_CODE (ve) == NOP_EXPR);
   16196       822828 :                 if (nop)
   16197            3 :                   ve = TREE_OPERAND (ve, 0);
   16198       822828 :                 ve = tsubst_expr (ve, args, complain, in_decl);
   16199       822828 :                 if (REFERENCE_REF_P (ve))
   16200              :                   {
   16201       467651 :                     gcc_assert (TYPE_REF_P (type));
   16202       467651 :                     ve = TREE_OPERAND (ve, 0);
   16203              :                   }
   16204       822828 :                 if (nop)
   16205            3 :                   ve = build_nop (type, ve);
   16206       822825 :                 else if (DECL_LANG_SPECIFIC (t)
   16207       822640 :                          && DECL_OMP_PRIVATIZED_MEMBER (t)
   16208          183 :                          && TREE_CODE (ve) == COMPONENT_REF
   16209          183 :                          && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
   16210       823008 :                          && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
   16211            6 :                   type = TREE_TYPE (ve);
   16212              :                 else
   16213       822819 :                   gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
   16214              :                                        == TYPE_MAIN_VARIANT (type));
   16215       822828 :                 SET_DECL_VALUE_EXPR (r, ve);
   16216              :               }
   16217              :           }
   16218    128761977 :         else if (DECL_SELF_REFERENCE_P (t))
   16219     44609169 :           SET_DECL_SELF_REFERENCE_P (r);
   16220    177286164 :         TREE_TYPE (r) = type;
   16221    177286164 :         cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   16222    177286164 :         DECL_CONTEXT (r) = ctx;
   16223              :         /* Clear out the mangled name and RTL for the instantiation.  */
   16224    177286164 :         SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   16225    177286164 :         if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
   16226    177286164 :           SET_DECL_RTL (r, NULL);
   16227    177286164 :         set_instantiating_module (r);
   16228              : 
   16229              :         /* The initializer must not be expanded until it is required;
   16230              :            see [temp.inst].  */
   16231    177286164 :         DECL_INITIAL (r) = NULL_TREE;
   16232    177286164 :         DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
   16233    177286164 :         if (VAR_P (r))
   16234              :           {
   16235     48524187 :             if (DECL_LANG_SPECIFIC (r))
   16236     22647101 :               SET_DECL_DEPENDENT_INIT_P (r, false);
   16237              : 
   16238     48524187 :             SET_DECL_MODE (r, VOIDmode);
   16239              : 
   16240              :             /* Possibly limit visibility based on template args.  */
   16241     48524187 :             DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   16242     48524187 :             if (DECL_VISIBILITY_SPECIFIED (t))
   16243              :               {
   16244            0 :                 DECL_VISIBILITY_SPECIFIED (r) = 0;
   16245            0 :                 DECL_ATTRIBUTES (r)
   16246            0 :                   = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   16247              :               }
   16248     48524187 :             determine_visibility (r);
   16249     27862584 :             if ((!local_p || TREE_STATIC (t))
   16250     20793169 :                 && !(flag_openmp && DECL_LANG_SPECIFIC (t)
   16251        29956 :                      && DECL_OMP_DECLARE_MAPPER_P (t))
   16252     69317353 :                 && DECL_SECTION_NAME (t))
   16253            9 :               set_decl_section_name (r, t);
   16254              :           }
   16255              : 
   16256    177286164 :         if (!local_p)
   16257              :           {
   16258              :             /* A static data member declaration is always marked
   16259              :                external when it is declared in-class, even if an
   16260              :                initializer is present.  We mimic the non-template
   16261              :                processing here.  */
   16262    146263871 :             DECL_EXTERNAL (r) = 1;
   16263    146263871 :             if (DECL_NAMESPACE_SCOPE_P (t))
   16264     45903873 :               DECL_NOT_REALLY_EXTERN (r) = 1;
   16265              : 
   16266    146263871 :             DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
   16267    146263871 :             SET_DECL_IMPLICIT_INSTANTIATION (r);
   16268    146263871 :             if (use_spec_table)
   16269     93535688 :               register_specialization (r, gen_tmpl, argvec, false, hash);
   16270              :           }
   16271              :         else
   16272              :           {
   16273     31022293 :             if (DECL_LANG_SPECIFIC (r))
   16274      1988696 :               DECL_TEMPLATE_INFO (r) = NULL_TREE;
   16275     31022293 :             if (!cp_unevaluated_operand)
   16276     31022293 :               register_local_specialization (r, t);
   16277              :           }
   16278              : 
   16279    177286164 :         if (VAR_P (r)
   16280     48524187 :             && CP_DECL_THREAD_LOCAL_P (r)
   16281    177286293 :             && !processing_template_decl)
   16282          117 :           set_decl_tls_model (r, decl_default_tls_model (r));
   16283              : 
   16284    177286164 :         DECL_CHAIN (r) = NULL_TREE;
   16285              : 
   16286    177286164 :         if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
   16287              :                                              /*flags=*/0,
   16288              :                                              args, complain, in_decl))
   16289            3 :           return error_mark_node;
   16290              : 
   16291              :         /* Preserve a typedef that names a type.  */
   16292    306047997 :         if (is_typedef_decl (r) && type != error_mark_node)
   16293              :           {
   16294    128761474 :             DECL_ORIGINAL_TYPE (r) = NULL_TREE;
   16295    128761474 :             set_underlying_type (r);
   16296              : 
   16297              :             /* common_handle_aligned_attribute doesn't apply the alignment
   16298              :                to DECL_ORIGINAL_TYPE.  */
   16299    128761474 :             if (TYPE_USER_ALIGN (TREE_TYPE (t)))
   16300          259 :               TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
   16301          259 :                                                   TYPE_ALIGN (TREE_TYPE (t)));
   16302              : 
   16303              :             /* Preserve structural-ness of a partially instantiated typedef.  */
   16304    128761474 :             if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t))
   16305    128761474 :                 && dependent_type_p (TREE_TYPE (r)))
   16306     15072501 :               SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (r));
   16307              :           }
   16308              : 
   16309    177286161 :         if (flag_openmp
   16310       294813 :             && VAR_P (t)
   16311        73002 :             && DECL_LANG_SPECIFIC (t)
   16312        32431 :             && DECL_OMP_DECLARE_MAPPER_P (t)
   16313    177286165 :             && strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   16314            4 :           DECL_NAME (r) = omp_mapper_id (DECL_NAME (t), TREE_TYPE (r));
   16315              : 
   16316    177286161 :         layout_decl (r, 0);
   16317              :       }
   16318    177286161 :       break;
   16319              : 
   16320    106597890 :     case NAMESPACE_DECL:
   16321    106597890 :       if (dependent_namespace_p (t))
   16322            1 :         r = tsubst_expr (ORIGINAL_NAMESPACE (t), args, complain, in_decl);
   16323              :       else
   16324    106597889 :         r = t;
   16325              :       break;
   16326              : 
   16327            0 :     default:
   16328            0 :       gcc_unreachable ();
   16329              :     }
   16330              : #undef RETURN
   16331              : 
   16332    894813522 :  out:
   16333              :   /* Restore the file and line information.  */
   16334    894813522 :   input_location = saved_loc;
   16335              : 
   16336    894813522 :   return r;
   16337              : }
   16338              : 
   16339              : /* Substitute into the complete parameter type list PARMS.  */
   16340              : 
   16341              : tree
   16342      6956165 : tsubst_function_parms (tree parms,
   16343              :                        tree args,
   16344              :                        tsubst_flags_t complain,
   16345              :                        tree in_decl)
   16346              : {
   16347      6956165 :   return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
   16348              : }
   16349              : 
   16350              : /* Substitute into the ARG_TYPES of a function type.
   16351              :    If END is a TREE_CHAIN, leave it and any following types
   16352              :    un-substituted.  */
   16353              : 
   16354              : static tree
   16355    506474942 : tsubst_arg_types (tree arg_types,
   16356              :                   tree args,
   16357              :                   tree end,
   16358              :                   tsubst_flags_t complain,
   16359              :                   tree in_decl)
   16360              : {
   16361    506474942 :   tree type = NULL_TREE;
   16362    506474942 :   int len = 1;
   16363    506474942 :   tree expanded_args = NULL_TREE;
   16364              : 
   16365    506474942 :   if (!arg_types || arg_types == void_list_node || arg_types == end)
   16366              :     return arg_types;
   16367              : 
   16368    321960983 :   if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
   16369              :     {
   16370              :       /* For a pack expansion, perform substitution on the
   16371              :          entire expression. Later on, we'll handle the arguments
   16372              :          one-by-one.  */
   16373      6090688 :       expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
   16374              :                                             args, complain, in_decl);
   16375              : 
   16376      6090688 :       if (TREE_CODE (expanded_args) == TREE_VEC)
   16377              :         /* So that we'll spin through the parameters, one by one.  */
   16378      3573110 :         len = TREE_VEC_LENGTH (expanded_args);
   16379              :       else
   16380              :         {
   16381              :           /* We only partially substituted into the parameter
   16382              :              pack. Our type is TYPE_PACK_EXPANSION.  */
   16383              :           type = expanded_args;
   16384              :           expanded_args = NULL_TREE;
   16385              :         }
   16386              :     }
   16387              :   else
   16388    315870295 :     type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
   16389              : 
   16390              :   /* Check if a substituted type is erroneous before substituting into
   16391              :      the rest of the chain.  */
   16392    642483878 :   for (int i = 0; i < len; i++)
   16393              :     {
   16394    320529073 :       if (expanded_args)
   16395      2141200 :         type = TREE_VEC_ELT (expanded_args, i);
   16396              : 
   16397    320529073 :       if (type == error_mark_node)
   16398              :         return error_mark_node;
   16399    320522927 :       if (VOID_TYPE_P (type))
   16400              :         {
   16401           32 :           if (complain & tf_error)
   16402              :             {
   16403            6 :               error ("invalid parameter type %qT", type);
   16404            6 :               if (in_decl)
   16405            3 :                 error ("in declaration %q+D", in_decl);
   16406              :             }
   16407           32 :           return error_mark_node;
   16408              :         }
   16409              :     }
   16410              : 
   16411              :   /* We do not substitute into default arguments here.  The standard
   16412              :      mandates that they be instantiated only when needed, which is
   16413              :      done in build_over_call.  */
   16414    321954805 :   tree default_arg = TREE_PURPOSE (arg_types);
   16415              : 
   16416              :   /* Except that we do substitute default arguments under tsubst_lambda_expr,
   16417              :      since the new op() won't have any associated template arguments for us
   16418              :      to refer to later.  */
   16419    321954805 :   if (lambda_fn_in_template_p (in_decl)
   16420    321954805 :       || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
   16421    291457165 :           && DECL_LOCAL_DECL_P (in_decl)))
   16422       514275 :     default_arg = tsubst_expr (default_arg, args, complain, in_decl);
   16423              : 
   16424    321954805 :   tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
   16425    321954805 :                                                args, end, complain, in_decl);
   16426    321954805 :   if (remaining_arg_types == error_mark_node)
   16427              :     return error_mark_node;
   16428              : 
   16429    642461407 :   for (int i = len-1; i >= 0; i--)
   16430              :     {
   16431    320514750 :       if (expanded_args)
   16432      2141197 :         type = TREE_VEC_ELT (expanded_args, i);
   16433              : 
   16434              :       /* Do array-to-pointer, function-to-pointer conversion, and ignore
   16435              :          top-level qualifiers as required.  */
   16436    320514750 :       type = cv_unqualified (type_decays_to (type));
   16437              : 
   16438    320514750 :       if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
   16439              :         {
   16440              :           /* We've instantiated a template before its default arguments
   16441              :              have been parsed.  This can happen for a nested template
   16442              :              class, and is not an error unless we require the default
   16443              :              argument in a call of this function.  */
   16444            3 :           remaining_arg_types
   16445            3 :             = tree_cons (default_arg, type, remaining_arg_types);
   16446            3 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
   16447              :                          remaining_arg_types);
   16448              :         }
   16449              :       else
   16450    320514747 :         remaining_arg_types
   16451    320514747 :           = hash_tree_cons (default_arg, type, remaining_arg_types);
   16452              :     }
   16453              : 
   16454    321946657 :   return remaining_arg_types;
   16455              : }
   16456              : 
   16457              : /* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
   16458              :    *not* handle the exception-specification for FNTYPE, because the
   16459              :    initial substitution of explicitly provided template parameters
   16460              :    during argument deduction forbids substitution into the
   16461              :    exception-specification:
   16462              : 
   16463              :      [temp.deduct]
   16464              : 
   16465              :      All references in the function type of the function template to  the
   16466              :      corresponding template parameters are replaced by the specified tem-
   16467              :      plate argument values.  If a substitution in a template parameter or
   16468              :      in  the function type of the function template results in an invalid
   16469              :      type, type deduction fails.  [Note: The equivalent  substitution  in
   16470              :      exception specifications is done only when the function is instanti-
   16471              :      ated, at which point a program is  ill-formed  if  the  substitution
   16472              :      results in an invalid type.]  */
   16473              : 
   16474              : static tree
   16475    176193871 : tsubst_function_type (tree t,
   16476              :                       tree args,
   16477              :                       tsubst_flags_t complain,
   16478              :                       tree in_decl)
   16479              : {
   16480    176193871 :   tree return_type;
   16481    176193871 :   tree arg_types = NULL_TREE;
   16482              : 
   16483              :   /* The TYPE_CONTEXT is not used for function/method types.  */
   16484    176193871 :   gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
   16485              : 
   16486              :   /* DR 1227: Mixing immediate and non-immediate contexts in deduction
   16487              :      failure.  */
   16488    176193871 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   16489              : 
   16490    176193871 :   if (late_return_type_p)
   16491              :     {
   16492              :       /* Substitute the argument types.  */
   16493     16105016 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   16494              :                                     complain, in_decl);
   16495     16105016 :       if (arg_types == error_mark_node)
   16496              :         return error_mark_node;
   16497              : 
   16498     16104992 :       tree save_ccp = current_class_ptr;
   16499     16104992 :       tree save_ccr = current_class_ref;
   16500     16104992 :       tree this_type = (TREE_CODE (t) == METHOD_TYPE
   16501     17773787 :                         ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
   16502      1668795 :       bool do_inject = this_type && CLASS_TYPE_P (this_type);
   16503      1668795 :       if (do_inject)
   16504              :         {
   16505              :           /* DR 1207: 'this' is in scope in the trailing return type.  */
   16506      1668795 :           inject_this_parameter (this_type, cp_type_quals (this_type));
   16507              :         }
   16508              : 
   16509              :       /* Substitute the return type.  */
   16510     16104992 :       return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16511              : 
   16512     16102292 :       if (do_inject)
   16513              :         {
   16514      1668795 :           current_class_ptr = save_ccp;
   16515      1668795 :           current_class_ref = save_ccr;
   16516              :         }
   16517              :     }
   16518              :   else
   16519              :     /* Substitute the return type.  */
   16520    160088855 :     return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16521              : 
   16522    176183014 :   if (return_type == error_mark_node)
   16523              :     return error_mark_node;
   16524              :   /* DR 486 clarifies that creation of a function type with an
   16525              :      invalid return type is a deduction failure.  */
   16526    174616361 :   if (TREE_CODE (return_type) == ARRAY_TYPE
   16527    174616263 :       || TREE_CODE (return_type) == FUNCTION_TYPE)
   16528              :     {
   16529          214 :       if (complain & tf_error)
   16530              :         {
   16531           26 :           if (TREE_CODE (return_type) == ARRAY_TYPE)
   16532           12 :             error ("function returning an array");
   16533              :           else
   16534           14 :             error ("function returning a function");
   16535              :         }
   16536          214 :       return error_mark_node;
   16537              :     }
   16538              : 
   16539    174616147 :   if (!late_return_type_p)
   16540              :     {
   16541              :       /* Substitute the argument types.  */
   16542    159400371 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   16543              :                                     complain, in_decl);
   16544    159400371 :       if (arg_types == error_mark_node)
   16545              :         return error_mark_node;
   16546              :     }
   16547              : 
   16548              :   /* Construct a new type node and return it.  */
   16549    174610065 :   return rebuild_function_or_method_type (t, args, return_type, arg_types,
   16550    174610065 :                                           /*raises=*/NULL_TREE, complain);
   16551              : }
   16552              : 
   16553              : /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
   16554              :    ARGS into that specification, and return the substituted
   16555              :    specification.  If there is no specification, return NULL_TREE.  */
   16556              : 
   16557              : static tree
   16558    174610042 : tsubst_exception_specification (tree fntype,
   16559              :                                 tree args,
   16560              :                                 tsubst_flags_t complain,
   16561              :                                 tree in_decl,
   16562              :                                 bool defer_ok)
   16563              : {
   16564    174610042 :   tree specs;
   16565    174610042 :   tree new_specs;
   16566              : 
   16567    174610042 :   specs = TYPE_RAISES_EXCEPTIONS (fntype);
   16568    174610042 :   new_specs = NULL_TREE;
   16569    237004554 :   if (specs && TREE_PURPOSE (specs))
   16570              :     {
   16571              :       /* A noexcept-specifier.  */
   16572     62362304 :       tree expr = TREE_PURPOSE (specs);
   16573     62362304 :       if (TREE_CODE (expr) == INTEGER_CST)
   16574     54127826 :         new_specs = expr;
   16575      8234478 :       else if (defer_ok)
   16576              :         {
   16577              :           /* Defer instantiation of noexcept-specifiers to avoid
   16578              :              excessive instantiations (c++/49107).  */
   16579      8193673 :           new_specs = make_node (DEFERRED_NOEXCEPT);
   16580      8193673 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   16581              :             {
   16582              :               /* We already partially instantiated this member template,
   16583              :                  so combine the new args with the old.  */
   16584           27 :               DEFERRED_NOEXCEPT_PATTERN (new_specs)
   16585           27 :                 = DEFERRED_NOEXCEPT_PATTERN (expr);
   16586           54 :               DEFERRED_NOEXCEPT_ARGS (new_specs)
   16587           54 :                 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
   16588              :             }
   16589              :           else
   16590              :             {
   16591      8193646 :               DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
   16592      8193646 :               DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
   16593              :             }
   16594              :         }
   16595              :       else
   16596              :         {
   16597        40805 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   16598              :             {
   16599            0 :               args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
   16600              :                                            args);
   16601            0 :               expr = DEFERRED_NOEXCEPT_PATTERN (expr);
   16602              :             }
   16603        40805 :           new_specs = tsubst_expr (expr, args, complain, in_decl);
   16604              :         }
   16605     62362304 :       new_specs = build_noexcept_spec (new_specs, complain);
   16606              :       /* We've instantiated a template before a noexcept-specifier
   16607              :          contained therein has been parsed.  This can happen for
   16608              :          a nested template class:
   16609              : 
   16610              :           struct S {
   16611              :             template<typename> struct B { B() noexcept(...); };
   16612              :             struct A : B<int> { ... use B() ... };
   16613              :           };
   16614              : 
   16615              :          where completing B<int> will trigger instantiating the
   16616              :          noexcept, even though we only parse it at the end of S.  */
   16617     62362304 :       if (UNPARSED_NOEXCEPT_SPEC_P (specs))
   16618              :         {
   16619           15 :           gcc_checking_assert (defer_ok);
   16620           15 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
   16621              :         }
   16622              :     }
   16623    112247738 :   else if (specs)
   16624              :     {
   16625        32208 :       if (! TREE_VALUE (specs))
   16626        32147 :         new_specs = specs;
   16627              :       else
   16628          118 :         while (specs)
   16629              :           {
   16630           61 :             tree spec;
   16631           61 :             int i, len = 1;
   16632           61 :             tree expanded_specs = NULL_TREE;
   16633              : 
   16634           61 :             if (PACK_EXPANSION_P (TREE_VALUE (specs)))
   16635              :               {
   16636              :                 /* Expand the pack expansion type.  */
   16637           13 :                 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
   16638              :                                                        args, complain,
   16639              :                                                        in_decl);
   16640              : 
   16641           13 :                 if (expanded_specs == error_mark_node)
   16642              :                   return error_mark_node;
   16643           11 :                 else if (TREE_CODE (expanded_specs) == TREE_VEC)
   16644            4 :                   len = TREE_VEC_LENGTH (expanded_specs);
   16645              :                 else
   16646              :                   {
   16647              :                     /* We're substituting into a member template, so
   16648              :                        we got a TYPE_PACK_EXPANSION back.  Add that
   16649              :                        expansion and move on.  */
   16650            7 :                     gcc_assert (TREE_CODE (expanded_specs)
   16651              :                                 == TYPE_PACK_EXPANSION);
   16652            7 :                     new_specs = add_exception_specifier (new_specs,
   16653              :                                                          expanded_specs,
   16654              :                                                          complain);
   16655            7 :                     specs = TREE_CHAIN (specs);
   16656            7 :                     continue;
   16657              :                   }
   16658              :               }
   16659              : 
   16660          110 :             for (i = 0; i < len; ++i)
   16661              :               {
   16662           60 :                 if (expanded_specs)
   16663           12 :                   spec = TREE_VEC_ELT (expanded_specs, i);
   16664              :                 else
   16665           48 :                   spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
   16666           60 :                 if (spec == error_mark_node)
   16667              :                   return spec;
   16668           58 :                 new_specs = add_exception_specifier (new_specs, spec,
   16669              :                                                      complain);
   16670              :               }
   16671              : 
   16672           50 :             specs = TREE_CHAIN (specs);
   16673              :           }
   16674              :     }
   16675    174610038 :   return new_specs;
   16676              : }
   16677              : 
   16678              : /* Substitute through a TREE_LIST of types or expressions, handling pack
   16679              :    expansions.  */
   16680              : 
   16681              : tree
   16682     36814151 : tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16683              : {
   16684     36814151 :   if (t == void_list_node)
   16685              :     return t;
   16686              : 
   16687     36814133 :   tree purpose = TREE_PURPOSE (t);
   16688     36814133 :   tree purposevec = NULL_TREE;
   16689     36814133 :   if (!purpose)
   16690              :     ;
   16691            2 :   else if (PACK_EXPANSION_P (purpose))
   16692              :     {
   16693            0 :       purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
   16694            0 :       if (TREE_CODE (purpose) == TREE_VEC)
   16695            0 :         purposevec = purpose;
   16696              :     }
   16697            2 :   else if (TYPE_P (purpose))
   16698            0 :     purpose = tsubst (purpose, args, complain, in_decl);
   16699              :   else
   16700            2 :     purpose = tsubst_expr (purpose, args, complain, in_decl);
   16701     36814133 :   if (purpose == error_mark_node || purposevec == error_mark_node)
   16702              :     return error_mark_node;
   16703              : 
   16704     36814133 :   tree value = TREE_VALUE (t);
   16705     36814133 :   tree valuevec = NULL_TREE;
   16706     36814133 :   if (!value)
   16707              :     ;
   16708     36814133 :   else if (PACK_EXPANSION_P (value))
   16709              :     {
   16710       651697 :       value = tsubst_pack_expansion (value, args, complain, in_decl);
   16711       651697 :       if (TREE_CODE (value) == TREE_VEC)
   16712       648072 :         valuevec = value;
   16713              :     }
   16714     36162436 :   else if (TYPE_P (value))
   16715           24 :     value = tsubst (value, args, complain, in_decl);
   16716              :   else
   16717     36162412 :     value = tsubst_expr (value, args, complain, in_decl);
   16718     36814133 :   if (value == error_mark_node || valuevec == error_mark_node)
   16719              :     return error_mark_node;
   16720              : 
   16721     36786510 :   tree chain = TREE_CHAIN (t);
   16722     36786510 :   if (!chain)
   16723              :     ;
   16724      1400387 :   else if (TREE_CODE (chain) == TREE_LIST)
   16725      1400387 :     chain = tsubst_tree_list (chain, args, complain, in_decl);
   16726            0 :   else if (TYPE_P (chain))
   16727            0 :     chain = tsubst (chain, args, complain, in_decl);
   16728              :   else
   16729            0 :     chain = tsubst_expr (chain, args, complain, in_decl);
   16730     36786510 :   if (chain == error_mark_node)
   16731              :     return error_mark_node;
   16732              : 
   16733     36786510 :   if (purpose == TREE_PURPOSE (t)
   16734     36786508 :       && value == TREE_VALUE (t)
   16735     37428466 :       && chain == TREE_CHAIN (t))
   16736              :     return t;
   16737              : 
   16738     36144770 :   int len;
   16739              :   /* Determine the number of arguments.  */
   16740     36144770 :   if (purposevec)
   16741              :     {
   16742            0 :       len = TREE_VEC_LENGTH (purposevec);
   16743            0 :       gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
   16744              :     }
   16745     36144770 :   else if (valuevec)
   16746       648072 :     len = TREE_VEC_LENGTH (valuevec);
   16747              :   else
   16748              :     len = 1;
   16749              : 
   16750     72127467 :   for (int i = len; i-- > 0; )
   16751              :     {
   16752     35982697 :       if (purposevec)
   16753            0 :         purpose = TREE_VEC_ELT (purposevec, i);
   16754     35982697 :       if (valuevec)
   16755       485999 :         value = TREE_VEC_ELT (valuevec, i);
   16756              : 
   16757     35982697 :       if (value && TYPE_P (value))
   16758           26 :         chain = hash_tree_cons (purpose, value, chain);
   16759              :       else
   16760     35982671 :         chain = tree_cons (purpose, value, chain);
   16761              :     }
   16762              : 
   16763              :   return chain;
   16764              : }
   16765              : 
   16766              : /* Substitute ARGS into T, which is a splice scope.  */
   16767              : 
   16768              : static tree
   16769          166 : tsubst_splice_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16770              : {
   16771          166 :   tree r = tsubst (SPLICE_SCOPE_EXPR (t), args, complain, in_decl);
   16772          166 :   if (r == error_mark_node)
   16773              :     return r;
   16774          151 :   if (dependent_splice_p (r))
   16775            1 :     return make_splice_scope (r, SPLICE_SCOPE_TYPE_P (t));
   16776          300 :   if (SPLICE_SCOPE_TYPE_P (t)
   16777          150 :       ? !valid_splice_type_p (r)
   16778           69 :       : !valid_splice_scope_p (r))
   16779              :     {
   16780           24 :       if (complain & tf_error)
   16781              :         {
   16782           24 :           const location_t loc = EXPR_LOCATION (SPLICE_SCOPE_EXPR (t));
   16783           24 :           if (SPLICE_SCOPE_TYPE_P (t))
   16784           13 :             error_at (loc, "%qE is not usable in a splice type", r);
   16785              :           else
   16786           11 :             error_at (loc, "%qE is not usable in a splice scope", r);
   16787              :         }
   16788           24 :       return error_mark_node;
   16789              :     }
   16790              : 
   16791              :   return r;
   16792              : }
   16793              : 
   16794              : /* Substitute ARGS into T, which is a splice expression.  */
   16795              : 
   16796              : static tree
   16797          685 : tsubst_splice_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16798              : {
   16799          685 :   tree op = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl);
   16800          685 :   if (op == error_mark_node)
   16801              :     return error_mark_node;
   16802          679 :   op = splice (op);
   16803          679 :   if (op == error_mark_node)
   16804              :     return error_mark_node;
   16805          677 :   if (dependent_splice_p (op))
   16806              :     {
   16807            4 :       if (SPLICE_EXPR_EXPRESSION_P (t))
   16808            6 :         SET_SPLICE_EXPR_EXPRESSION_P (op);
   16809            4 :       if (SPLICE_EXPR_MEMBER_ACCESS_P (t))
   16810            4 :         SET_SPLICE_EXPR_MEMBER_ACCESS_P (op, true);
   16811            4 :       if (SPLICE_EXPR_ADDRESS_P (t))
   16812            0 :         SET_SPLICE_EXPR_ADDRESS_P (op, true);
   16813            4 :       return op;
   16814              :     }
   16815          673 :   if (SPLICE_EXPR_EXPRESSION_P (t)
   16816          673 :       && !check_splice_expr (input_location, UNKNOWN_LOCATION, op,
   16817          514 :                              SPLICE_EXPR_ADDRESS_P (t),
   16818          514 :                              SPLICE_EXPR_MEMBER_ACCESS_P (t),
   16819              :                              (complain & tf_error)))
   16820              :     return error_mark_node;
   16821              : 
   16822          656 :   if (SPLICE_EXPR_ADDRESS_P (t))
   16823              :     {
   16824           15 :       if (BASELINK_P (op))
   16825            3 :         op = build_offset_ref (BINFO_TYPE (BASELINK_ACCESS_BINFO (op)), op,
   16826              :                                /*address_p=*/true, complain);
   16827           12 :       else if (DECL_NONSTATIC_MEMBER_P (op))
   16828            4 :         op = build_offset_ref (DECL_CONTEXT (op), op,
   16829              :                                /*address_p=*/true, complain);
   16830              :     }
   16831              : 
   16832          656 :   if (outer_automatic_var_p (op))
   16833            0 :     op = process_outer_var_ref (op, complain);
   16834              :   /* Like in cp_parser_splice_expression, for foo.[: bar :]
   16835              :      cp_parser_postfix_dot_deref_expression wants to see only
   16836              :      certain kind of entities.  */
   16837          656 :   if (SPLICE_EXPR_MEMBER_ACCESS_P (t))
   16838         1113 :     gcc_assert (TREE_CODE (op) == FIELD_DECL
   16839              :                 || VAR_P (op)
   16840              :                 || TREE_CODE (op) == CONST_DECL
   16841              :                 || TREE_CODE (op) == FUNCTION_DECL
   16842              :                 || DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (op))
   16843              :                 || variable_template_p (op)
   16844              :                 || BASELINK_P (op)
   16845              :                 || TREE_CODE (op) == TEMPLATE_ID_EXPR
   16846              :                 || TREE_CODE (op) == TREE_BINFO);
   16847              : 
   16848              :   return op;
   16849              : }
   16850              : 
   16851              : /* Return true iff we're in an expansion statement.  */
   16852              : 
   16853              : static bool
   16854     44065670 : in_expansion_stmt_p ()
   16855              : {
   16856     44065670 :   if (in_expansion_stmt)
   16857              :     return true;
   16858              : 
   16859              :   /* In instantiations in_expansion_stmt is false.  */
   16860     44065670 :   for (cp_binding_level *b = current_binding_level;
   16861    124930930 :        b && b->kind != sk_function_parms;
   16862     80865260 :        b = b->level_chain)
   16863     80865453 :     if (b->kind == sk_template_for)
   16864              :       return true;
   16865              :   return false;
   16866              : }
   16867              : 
   16868              : /* Take the tree structure T and replace template parameters used
   16869              :    therein with the argument vector ARGS.  IN_DECL is an associated
   16870              :    decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
   16871              :    Issue error and warning messages under control of COMPLAIN.  Note
   16872              :    that we must be relatively non-tolerant of extensions here, in
   16873              :    order to preserve conformance; if we allow substitutions that
   16874              :    should not be allowed, we may allow argument deductions that should
   16875              :    not succeed, and therefore report ambiguous overload situations
   16876              :    where there are none.  In theory, we could allow the substitution,
   16877              :    but indicate that it should have failed, and allow our caller to
   16878              :    make sure that the right thing happens, but we don't try to do this
   16879              :    yet.
   16880              : 
   16881              :    This function is used for dealing with types, decls and the like;
   16882              :    for expressions, use tsubst_expr or tsubst_copy.  */
   16883              : 
   16884              : tree
   16885   8059622360 : tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16886              : {
   16887   8059622360 :   enum tree_code code;
   16888   8059622360 :   tree type, r = NULL_TREE;
   16889              : 
   16890   8059622360 :   if (t == NULL_TREE || t == error_mark_node
   16891   8001029989 :       || t == integer_type_node
   16892   7907364916 :       || t == void_type_node
   16893   7810526580 :       || t == char_type_node
   16894   7792098074 :       || t == unknown_type_node
   16895   7792098069 :       || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
   16896              :     return t;
   16897              : 
   16898              :   /* Any instantiation of a template containing a TU-local entity is an
   16899              :      exposure, so always issue a diagnostic irrespective of complain.  */
   16900   7791912214 :   if (instantiating_tu_local_entity (t))
   16901           28 :     return error_mark_node;
   16902              : 
   16903   7791912186 :   tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
   16904   7791912186 :   complain &= ~tf_tst_ok;
   16905              : 
   16906   7791912186 :   tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
   16907   7791912186 :   complain &= ~tf_qualifying_scope;
   16908              : 
   16909   7791912186 :   if (DECL_P (t))
   16910    719117266 :     return tsubst_decl (t, args, complain);
   16911              : 
   16912   7072794920 :   if (args == NULL_TREE)
   16913              :     return t;
   16914              : 
   16915   7009271693 :   code = TREE_CODE (t);
   16916              : 
   16917   7009271693 :   gcc_assert (code != IDENTIFIER_NODE);
   16918   7009271693 :   type = TREE_TYPE (t);
   16919              : 
   16920   7009271693 :   gcc_assert (type != unknown_type_node);
   16921              : 
   16922   7009271693 :   if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   16923              :     return d;
   16924              : 
   16925              :   /* Reuse typedefs.  We need to do this to handle dependent attributes,
   16926              :      such as attribute aligned.  */
   16927   7009182820 :   if (TYPE_P (t)
   16928   7009182820 :       && typedef_variant_p (t))
   16929              :     {
   16930    352270582 :       tree decl = TYPE_NAME (t);
   16931              : 
   16932    352270582 :       if (alias_template_specialization_p (t, nt_opaque))
   16933              :         {
   16934              :           /* DECL represents an alias template and we want to
   16935              :              instantiate it.  */
   16936    141191838 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   16937    141191838 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   16938    141191838 :           r = instantiate_alias_template (tmpl, gen_args, complain);
   16939              :         }
   16940    422157488 :       else if (DECL_CLASS_SCOPE_P (decl)
   16941    169537412 :                && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
   16942    375283570 :                && uses_template_parms (DECL_CONTEXT (decl)))
   16943              :         {
   16944    163959949 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   16945    163959949 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   16946    163959949 :           r = retrieve_specialization (tmpl, gen_args, 0);
   16947              :         }
   16948     94237590 :       else if ((DECL_FUNCTION_SCOPE_P (decl)
   16949      3073208 :                 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
   16950      3073205 :                 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
   16951              :                /* The { } of an expansion-statement is considered a template
   16952              :                   definition.  */
   16953     91184465 :                || in_expansion_stmt_p ())
   16954      3053318 :         r = retrieve_local_specialization (decl);
   16955              :       else
   16956              :         /* The typedef is from a non-template context.  */
   16957              :         return t;
   16958              : 
   16959    308205102 :       if (r)
   16960              :         {
   16961    308204215 :           r = TREE_TYPE (r);
   16962    308204215 :           r = cp_build_qualified_type
   16963    308204215 :             (r, cp_type_quals (t) | cp_type_quals (r),
   16964              :              complain | tf_ignore_bad_quals);
   16965    308204215 :           return r;
   16966              :         }
   16967              :       else
   16968              :         {
   16969              :           /* We don't have an instantiation yet, so drop the typedef.  */
   16970          887 :           int quals = cp_type_quals (t);
   16971          887 :           t = DECL_ORIGINAL_TYPE (decl);
   16972          887 :           t = cp_build_qualified_type (t, quals,
   16973              :                                        complain | tf_ignore_bad_quals);
   16974              :         }
   16975              :     }
   16976              : 
   16977   6656913125 :   bool fndecl_type = (complain & tf_fndecl_type);
   16978   6656913125 :   complain &= ~tf_fndecl_type;
   16979              : 
   16980   6656913125 :   if (type
   16981   6656913125 :       && code != TYPENAME_TYPE
   16982   1151383060 :       && code != TEMPLATE_TYPE_PARM
   16983   1151383060 :       && code != TEMPLATE_PARM_INDEX
   16984              :       && code != IDENTIFIER_NODE
   16985    933292817 :       && code != FUNCTION_TYPE
   16986    863850344 :       && code != METHOD_TYPE
   16987    863850344 :       && code != PACK_INDEX_TYPE)
   16988    757096298 :     type = tsubst (type, args, complain, in_decl);
   16989   6656913125 :   if (type == error_mark_node)
   16990              :     return error_mark_node;
   16991              : 
   16992   6656907755 :   switch (code)
   16993              :     {
   16994   1175631751 :     case RECORD_TYPE:
   16995   1175631751 :       if (TYPE_PTRMEMFUNC_P (t))
   16996        31425 :         return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
   16997              :       /* Fall through.  */
   16998   1203789659 :     case UNION_TYPE:
   16999   1203789659 :     case ENUMERAL_TYPE:
   17000   1203789659 :       if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
   17001              :         {
   17002              :           /* Figure out what arguments are appropriate for the
   17003              :              type we are trying to find.  For example, given:
   17004              : 
   17005              :                template <class T> struct S;
   17006              :                template <class T, class U> void f(T, U) { S<U> su; }
   17007              : 
   17008              :              and supposing that we are instantiating f<int, double>,
   17009              :              then our ARGS will be {int, double}, but, when looking up
   17010              :              S we only want {double}.  */
   17011   2130814852 :           tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
   17012              :                                               complain, in_decl);
   17013   1065407390 :           if (argvec == error_mark_node)
   17014              :             return error_mark_node;
   17015              : 
   17016   1065296578 :           tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
   17017              :                                           complain);
   17018   1065296533 :           return cp_build_qualified_type (r, cp_type_quals (t), complain);
   17019              :         }
   17020              :       else
   17021              :         /* This is not a template type, so there's nothing to do.  */
   17022    138382233 :         return t;
   17023              : 
   17024              :     case ERROR_MARK:
   17025              :     case IDENTIFIER_NODE:
   17026              :     case VOID_TYPE:
   17027              :     case OPAQUE_TYPE:
   17028              :     case REAL_TYPE:
   17029              :     case COMPLEX_TYPE:
   17030              :     case VECTOR_TYPE:
   17031              :     case BOOLEAN_TYPE:
   17032              :     case NULLPTR_TYPE:
   17033              :     case META_TYPE:
   17034              :     case LANG_TYPE:
   17035              :       return t;
   17036              : 
   17037    122994481 :     case INTEGER_TYPE:
   17038    122994481 :       if (t == integer_type_node)
   17039              :         return t;
   17040              : 
   17041    122994469 :       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
   17042    122994469 :           && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
   17043              :         return t;
   17044              : 
   17045       920651 :       {
   17046       920651 :         tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
   17047              : 
   17048       920651 :         max = tsubst_expr (omax, args, complain, in_decl);
   17049              : 
   17050              :         /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
   17051              :            needed.  */
   17052       920651 :         if (TREE_CODE (max) == NOP_EXPR
   17053       231847 :             && TREE_SIDE_EFFECTS (omax)
   17054       920714 :             && !TREE_TYPE (max))
   17055            0 :           TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
   17056              : 
   17057              :         /* If we're in a partial instantiation, preserve the magic NOP_EXPR
   17058              :            with TREE_SIDE_EFFECTS that indicates this is not an integral
   17059              :            constant expression.  */
   17060       920651 :         if (processing_template_decl
   17061       920651 :             && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
   17062              :           {
   17063            0 :             gcc_assert (TREE_CODE (max) == NOP_EXPR);
   17064            0 :             TREE_SIDE_EFFECTS (max) = 1;
   17065              :           }
   17066              : 
   17067       920651 :         return compute_array_index_type (NULL_TREE, max, complain);
   17068              :       }
   17069              : 
   17070   3163255900 :     case TEMPLATE_TYPE_PARM:
   17071   3163255900 :       if (TEMPLATE_TYPE_LEVEL (t) == 0)
   17072              :         {
   17073              :           /* This is either an ordinary level-less auto or a CTAD placeholder
   17074              :              auto.  These get replaced only via do_auto_deduction which, in the
   17075              :              ordinary case, temporarily overrides its level to 1 before calling
   17076              :              tsubst.  CTAD placeholders are replaced via do_class_deduction.  */
   17077       228874 :           gcc_checking_assert (is_auto (t));
   17078       228874 :           tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
   17079       228874 :           if (!tmpl)
   17080              :             /* Ordinary level-less auto has nothing to substitute.  */
   17081              :             return t;
   17082              : 
   17083              :           /* Substitute the template of this CTAD placeholder.  */
   17084       110053 :           tmpl = tsubst_expr (tmpl, args, complain, in_decl);
   17085       110053 :           if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   17086         2238 :             tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
   17087              : 
   17088       110053 :           if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
   17089         2543 :             return make_template_placeholder (tmpl);
   17090              :           else
   17091              :             return t;
   17092              :         }
   17093              :       /* Fall through.  */
   17094   3384423282 :     case TEMPLATE_TEMPLATE_PARM:
   17095   3384423282 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   17096   3384423282 :     case TEMPLATE_PARM_INDEX:
   17097   3384423282 :       {
   17098   3384423282 :         int idx;
   17099   3384423282 :         int level;
   17100   3384423282 :         int levels;
   17101   3384423282 :         tree arg = NULL_TREE;
   17102              : 
   17103   3384423282 :         r = NULL_TREE;
   17104              : 
   17105   3384423282 :         gcc_assert (TREE_VEC_LENGTH (args) > 0);
   17106   3384423282 :         template_parm_level_and_index (t, &level, &idx);
   17107              : 
   17108   3384423282 :         levels = TMPL_ARGS_DEPTH (args);
   17109   3384423282 :         if (level <= levels
   17110   9771731958 :             && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
   17111              :           {
   17112   3193654295 :             arg = TMPL_ARG (args, level, idx);
   17113              : 
   17114              :             /* See through ARGUMENT_PACK_SELECT arguments. */
   17115   3193654295 :             if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   17116     10873774 :               arg = argument_pack_select_arg (arg);
   17117              :           }
   17118              : 
   17119   3384423282 :         if (arg == error_mark_node)
   17120              :           return error_mark_node;
   17121   3384423228 :         else if (arg != NULL_TREE)
   17122              :           {
   17123   3173038544 :             if (ARGUMENT_PACK_P (arg))
   17124              :               /* If ARG is an argument pack, we don't actually want to
   17125              :                  perform a substitution here, because substitutions
   17126              :                  for argument packs are only done
   17127              :                  element-by-element. We can get to this point when
   17128              :                  substituting the type of a non-type template
   17129              :                  parameter pack, when that type actually contains
   17130              :                  template parameter packs from an outer template, e.g.,
   17131              : 
   17132              :                  template<typename... Types> struct A {
   17133              :                    template<Types... Values> struct B { };
   17134              :                  };  */
   17135              :               return t;
   17136              : 
   17137   3173038236 :             if (code == TEMPLATE_TYPE_PARM)
   17138              :               {
   17139   2960305861 :                 int quals;
   17140              : 
   17141   2960305861 :                 gcc_assert (TYPE_P (arg));
   17142              : 
   17143   2960305861 :                 quals = cp_type_quals (arg) | cp_type_quals (t);
   17144              : 
   17145   2960305861 :                 return cp_build_qualified_type
   17146   2960305861 :                   (arg, quals, complain | tf_ignore_bad_quals);
   17147              :               }
   17148    212732375 :             else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
   17149              :               {
   17150              :                 /* We are processing a type constructed from a
   17151              :                    template template parameter.  */
   17152      1470098 :                 tree argvec = tsubst (TYPE_TI_ARGS (t),
   17153              :                                       args, complain, in_decl);
   17154       735049 :                 if (argvec == error_mark_node)
   17155              :                   return error_mark_node;
   17156              : 
   17157       734234 :                 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
   17158              :                             || TREE_CODE (arg) == TEMPLATE_DECL
   17159              :                             || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
   17160              : 
   17161       734234 :                 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
   17162              :                   /* Consider this code:
   17163              : 
   17164              :                         template <template <class> class Template>
   17165              :                         struct Internal {
   17166              :                         template <class Arg> using Bind = Template<Arg>;
   17167              :                         };
   17168              : 
   17169              :                         template <template <class> class Template, class Arg>
   17170              :                         using Instantiate = Template<Arg>; //#0
   17171              : 
   17172              :                         template <template <class> class Template,
   17173              :                                   class Argument>
   17174              :                         using Bind =
   17175              :                           Instantiate<Internal<Template>::template Bind,
   17176              :                                       Argument>; //#1
   17177              : 
   17178              :                      When #1 is parsed, the
   17179              :                      BOUND_TEMPLATE_TEMPLATE_PARM representing the
   17180              :                      parameter `Template' in #0 matches the
   17181              :                      UNBOUND_CLASS_TEMPLATE representing the argument
   17182              :                      `Internal<Template>::template Bind'; We then want
   17183              :                      to assemble the type `Bind<Argument>' that can't
   17184              :                      be fully created right now, because
   17185              :                      `Internal<Template>' not being complete, the Bind
   17186              :                      template cannot be looked up in that context.  So
   17187              :                      we need to "store" `Bind<Argument>' for later
   17188              :                      when the context of Bind becomes complete.  Let's
   17189              :                      store that in a TYPENAME_TYPE.  */
   17190            6 :                   return make_typename_type (TYPE_CONTEXT (arg),
   17191              :                                              build_nt (TEMPLATE_ID_EXPR,
   17192            6 :                                                        TYPE_IDENTIFIER (arg),
   17193              :                                                        argvec),
   17194              :                                              typename_type,
   17195            6 :                                              complain);
   17196              : 
   17197              :                 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
   17198              :                    are resolving nested-types in the signature of a
   17199              :                    member function templates.  Otherwise ARG is a
   17200              :                    TEMPLATE_DECL and is the real template to be
   17201              :                    instantiated.  */
   17202       734228 :                 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   17203          127 :                   arg = TYPE_NAME (arg);
   17204              : 
   17205       734228 :                 r = lookup_template_class (arg,
   17206              :                                            argvec, in_decl,
   17207       734228 :                                            DECL_CONTEXT (arg),
   17208              :                                            complain);
   17209       734228 :                 return cp_build_qualified_type
   17210       734228 :                   (r, cp_type_quals (t) | cp_type_quals (r), complain);
   17211              :               }
   17212    211997326 :             else if (code == TEMPLATE_TEMPLATE_PARM)
   17213              :               return arg;
   17214              :             else
   17215              :               /* TEMPLATE_PARM_INDEX.  */
   17216    209486533 :               return convert_from_reference (unshare_expr (arg));
   17217              :           }
   17218              : 
   17219    211384684 :         if (level == 1)
   17220              :           /* This can happen during the attempted tsubst'ing in
   17221              :              unify.  This means that we don't yet have any information
   17222              :              about the template parameter in question.  */
   17223              :           return t;
   17224              : 
   17225              :         /* Early in template argument deduction substitution, we don't
   17226              :            want to reduce the level of 'auto', or it will be confused
   17227              :            with a normal template parm in subsequent deduction.
   17228              :            Similarly, don't reduce the level of template parameters to
   17229              :            avoid mismatches when deducing their types.  */
   17230    190768947 :         if (complain & tf_partial)
   17231              :           return t;
   17232              : 
   17233              :         /* If we get here, we must have been looking at a parm for a
   17234              :            more deeply nested template.  Make a new version of this
   17235              :            template parameter, but with a lower level.  */
   17236    190117198 :         int quals;
   17237    190117198 :         switch (code)
   17238              :           {
   17239    183354866 :           case TEMPLATE_TYPE_PARM:
   17240    183354866 :           case TEMPLATE_TEMPLATE_PARM:
   17241    183354866 :             quals = cp_type_quals (t);
   17242    183354866 :             if (quals)
   17243              :               {
   17244     11990290 :                 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
   17245     11990290 :                 t = TYPE_MAIN_VARIANT (t);
   17246              :               }
   17247              : 
   17248    183354866 :             if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
   17249    168316246 :               if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
   17250    168316246 :                   && (code == TEMPLATE_TYPE_PARM
   17251        47704 :                       || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
   17252              :                 /* Cache lowering a type parameter or a simple template
   17253              :                    template parameter.  */
   17254    168133632 :                 r = TREE_TYPE (d);
   17255              : 
   17256    183354866 :             if (!r)
   17257              :               {
   17258     15221234 :                 r = copy_type (t);
   17259     15221234 :                 TEMPLATE_TYPE_PARM_INDEX (r)
   17260     15221234 :                   = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
   17261              :                                                 r, levels, args, complain);
   17262     15221234 :                 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
   17263     15221234 :                 TYPE_MAIN_VARIANT (r) = r;
   17264     15221234 :                 TYPE_POINTER_TO (r) = NULL_TREE;
   17265     15221234 :                 TYPE_REFERENCE_TO (r) = NULL_TREE;
   17266              : 
   17267     15221234 :                 if (code == TEMPLATE_TYPE_PARM)
   17268     15219458 :                   if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
   17269              :                     /* Propagate constraints on placeholders since they are
   17270              :                        only instantiated during satisfaction.  */
   17271       501774 :                     PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
   17272              : 
   17273     15221234 :                 if (TYPE_STRUCTURAL_EQUALITY_P (t))
   17274            0 :                   SET_TYPE_STRUCTURAL_EQUALITY (r);
   17275              :                 else
   17276     15221234 :                   TYPE_CANONICAL (r) = canonical_type_parameter (r);
   17277              :               }
   17278              : 
   17279    183354866 :             if (quals)
   17280     11990290 :               r = cp_build_qualified_type (r, quals,
   17281              :                                            complain | tf_ignore_bad_quals);
   17282              :             break;
   17283              : 
   17284        10837 :           case BOUND_TEMPLATE_TEMPLATE_PARM:
   17285        10837 :             {
   17286        10837 :               tree tinfo = TYPE_TEMPLATE_INFO (t);
   17287              :               /* We might need to substitute into the types of non-type
   17288              :                  template parameters.  This also lowers the level of
   17289              :                  the ttp appropriately.  */
   17290        10837 :               tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
   17291              :                                   complain, in_decl);
   17292        10837 :               if (tmpl == error_mark_node)
   17293              :                 return error_mark_node;
   17294        10837 :               tree argvec = tsubst (TI_ARGS (tinfo), args,
   17295              :                                     complain, in_decl);
   17296        10837 :               if (argvec == error_mark_node)
   17297              :                 return error_mark_node;
   17298        10837 :               r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE,
   17299              :                                          complain);
   17300        10837 :               r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   17301        10837 :               break;
   17302              :             }
   17303              : 
   17304      6751495 :           case TEMPLATE_PARM_INDEX:
   17305              :             /* OK, now substitute the type of the non-type parameter.  We
   17306              :                couldn't do it earlier because it might be an auto parameter,
   17307              :                and we wouldn't need to if we had an argument.  */
   17308      6751495 :             type = tsubst (type, args, complain, in_decl);
   17309      6751495 :             if (type == error_mark_node)
   17310              :               return error_mark_node;
   17311      6751489 :             r = reduce_template_parm_level (t, type, levels, args, complain);
   17312      6751489 :             break;
   17313              : 
   17314            0 :           default:
   17315            0 :             gcc_unreachable ();
   17316              :           }
   17317              : 
   17318    190117192 :         return r;
   17319              :       }
   17320              : 
   17321           26 :     case TREE_LIST:
   17322           26 :       return tsubst_tree_list (t, args, complain, in_decl);
   17323              : 
   17324            0 :     case TREE_BINFO:
   17325              :       /* We should never be tsubsting a binfo.  */
   17326            0 :       gcc_unreachable ();
   17327              : 
   17328    434537748 :     case TREE_VEC:
   17329              :       /* A vector of template arguments.  */
   17330    434537748 :       gcc_assert (!type);
   17331    434537748 :       return tsubst_template_args (t, args, complain, in_decl);
   17332              : 
   17333    709723174 :     case POINTER_TYPE:
   17334    709723174 :     case REFERENCE_TYPE:
   17335    709723174 :       {
   17336    709723174 :         if (type == TREE_TYPE (t)
   17337     40393177 :             && TREE_CODE (type) != METHOD_TYPE
   17338    750115775 :             && (TYPE_ATTRIBUTES (t) == NULL_TREE
   17339            6 :                 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
   17340              :           return t;
   17341              : 
   17342              :         /* [temp.deduct]
   17343              : 
   17344              :            Type deduction may fail for any of the following
   17345              :            reasons:
   17346              : 
   17347              :            -- Attempting to create a pointer to reference type.
   17348              :            -- Attempting to create a reference to a reference type or
   17349              :               a reference to void.
   17350              : 
   17351              :           Core issue 106 says that creating a reference to a reference
   17352              :           during instantiation is no longer a cause for failure. We
   17353              :           only enforce this check in strict C++98 mode.  */
   17354    669330579 :         if ((TYPE_REF_P (type)
   17355     42953498 :              && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
   17356    669330393 :             || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
   17357              :           {
   17358         1316 :             static location_t last_loc;
   17359              : 
   17360              :             /* We keep track of the last time we issued this error
   17361              :                message to avoid spewing a ton of messages during a
   17362              :                single bad template instantiation.  */
   17363         1316 :             if (complain & tf_error
   17364           84 :                 && last_loc != input_location)
   17365              :               {
   17366           84 :                 if (VOID_TYPE_P (type))
   17367            8 :                   error ("forming reference to void");
   17368           76 :                else if (code == POINTER_TYPE)
   17369           76 :                  error ("forming pointer to reference type %qT", type);
   17370              :                else
   17371            0 :                   error ("forming reference to reference type %qT", type);
   17372           84 :                 last_loc = input_location;
   17373              :               }
   17374              : 
   17375         1316 :             return error_mark_node;
   17376              :           }
   17377    669329263 :         else if (TREE_CODE (type) == FUNCTION_TYPE
   17378    669329263 :                  && (type_memfn_quals (type) != TYPE_UNQUALIFIED
   17379      7336948 :                      || type_memfn_rqual (type) != REF_QUAL_NONE))
   17380              :           {
   17381           76 :             if (complain & tf_error)
   17382              :               {
   17383           11 :                 if (code == POINTER_TYPE)
   17384            6 :                   error ("forming pointer to qualified function type %qT",
   17385              :                          type);
   17386              :                 else
   17387            5 :                   error ("forming reference to qualified function type %qT",
   17388              :                          type);
   17389              :               }
   17390           76 :             return error_mark_node;
   17391              :           }
   17392    669329187 :         else if (code == POINTER_TYPE)
   17393              :           {
   17394    285845909 :             r = build_pointer_type (type);
   17395    285845909 :             if (TREE_CODE (type) == METHOD_TYPE)
   17396        31419 :               r = build_ptrmemfunc_type (r);
   17397              :           }
   17398    383483278 :         else if (TYPE_REF_P (type))
   17399              :           /* In C++0x, during template argument substitution, when there is an
   17400              :              attempt to create a reference to a reference type, reference
   17401              :              collapsing is applied as described in [14.3.1/4 temp.arg.type]:
   17402              : 
   17403              :              "If a template-argument for a template-parameter T names a type
   17404              :              that is a reference to a type A, an attempt to create the type
   17405              :              'lvalue reference to cv T' creates the type 'lvalue reference to
   17406              :              A,' while an attempt to create the type type rvalue reference to
   17407              :              cv T' creates the type T"
   17408              :           */
   17409     85906624 :           r = cp_build_reference_type (TREE_TYPE (type),
   17410     42953312 :                                        TYPE_REF_IS_RVALUE (t)
   17411     42953312 :                                        && TYPE_REF_IS_RVALUE (type));
   17412              :         else
   17413    340529966 :           r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
   17414    669329187 :         r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   17415              : 
   17416    669329187 :         if (r != error_mark_node)
   17417              :           /* Will this ever be needed for TYPE_..._TO values?  */
   17418    669329187 :           layout_type (r);
   17419              : 
   17420    669329187 :         if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
   17421              :                                              /*flags=*/0,
   17422              :                                              args, complain, in_decl))
   17423            0 :           return error_mark_node;
   17424              : 
   17425    669329187 :         return r;
   17426              :       }
   17427       195697 :     case OFFSET_TYPE:
   17428       195697 :       {
   17429       195697 :         r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
   17430       195697 :         if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
   17431              :           {
   17432              :             /* [temp.deduct]
   17433              : 
   17434              :                Type deduction may fail for any of the following
   17435              :                reasons:
   17436              : 
   17437              :                -- Attempting to create "pointer to member of T" when T
   17438              :                   is not a class type.  */
   17439            0 :             if (complain & tf_error)
   17440            0 :               error ("creating pointer to member of non-class type %qT", r);
   17441            0 :             return error_mark_node;
   17442              :           }
   17443       195697 :         if (TYPE_REF_P (type))
   17444              :           {
   17445            6 :             if (complain & tf_error)
   17446            3 :               error ("creating pointer to member reference type %qT", type);
   17447            6 :             return error_mark_node;
   17448              :           }
   17449       195691 :         if (VOID_TYPE_P (type))
   17450              :           {
   17451            3 :             if (complain & tf_error)
   17452            3 :               error ("creating pointer to member of type void");
   17453            3 :             return error_mark_node;
   17454              :           }
   17455       195688 :         gcc_assert (TREE_CODE (type) != METHOD_TYPE);
   17456       195688 :         if (TREE_CODE (type) == FUNCTION_TYPE)
   17457              :           {
   17458              :             /* The type of the implicit object parameter gets its
   17459              :                cv-qualifiers from the FUNCTION_TYPE. */
   17460       147078 :             tree memptr;
   17461       147078 :             tree method_type
   17462       147078 :               = build_memfn_type (type, r, type_memfn_quals (type),
   17463              :                                   type_memfn_rqual (type));
   17464       147078 :             memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
   17465       147078 :             return cp_build_qualified_type (memptr, cp_type_quals (t),
   17466       147078 :                                             complain);
   17467              :           }
   17468              :         else
   17469        48610 :           return cp_build_qualified_type (build_ptrmem_type (r, type),
   17470              :                                           cp_type_quals (t),
   17471        48610 :                                           complain);
   17472              :       }
   17473    176193826 :     case FUNCTION_TYPE:
   17474    176193826 :     case METHOD_TYPE:
   17475    176193826 :       {
   17476    176193826 :         tree fntype;
   17477    176193826 :         tree specs;
   17478    176193826 :         fntype = tsubst_function_type (t, args, complain, in_decl);
   17479    176182993 :         if (fntype == error_mark_node)
   17480              :           return error_mark_node;
   17481              : 
   17482              :         /* Substitute the exception specification.  */
   17483    174610020 :         specs = tsubst_exception_specification (t, args, complain, in_decl,
   17484              :                                                 /*defer_ok*/fndecl_type);
   17485    174610020 :         if (specs == error_mark_node)
   17486              :           return error_mark_node;
   17487    174610007 :         if (specs)
   17488     62394475 :           fntype = build_exception_variant (fntype, specs);
   17489              :         return fntype;
   17490              :       }
   17491     12647426 :     case ARRAY_TYPE:
   17492     12647426 :       {
   17493     12647426 :         tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
   17494     12647426 :         if (domain == error_mark_node)
   17495              :           return error_mark_node;
   17496              : 
   17497              :         /* As an optimization, we avoid regenerating the array type if
   17498              :            it will obviously be the same as T.  */
   17499     12647262 :         if (type == TREE_TYPE (t)
   17500      9829916 :             && domain == TYPE_DOMAIN (t)
   17501     22261184 :             && (TYPE_ATTRIBUTES (t) == NULL_TREE
   17502            3 :                 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
   17503              :           return t;
   17504              : 
   17505              :         /* These checks should match the ones in create_array_type_for_decl.
   17506              : 
   17507              :            [temp.deduct]
   17508              : 
   17509              :            The deduction may fail for any of the following reasons:
   17510              : 
   17511              :            -- Attempting to create an array with an element type that
   17512              :               is void, a function type, or a reference type, or [DR337]
   17513              :               an abstract class type.  */
   17514      3033343 :         if (VOID_TYPE_P (type)
   17515      3033343 :             || TREE_CODE (type) == FUNCTION_TYPE
   17516      3033333 :             || (TREE_CODE (type) == ARRAY_TYPE
   17517         1485 :                 && TYPE_DOMAIN (type) == NULL_TREE)
   17518      6066668 :             || TYPE_REF_P (type))
   17519              :           {
   17520           18 :             if (complain & tf_error)
   17521            3 :               error ("creating array of %qT", type);
   17522           18 :             return error_mark_node;
   17523              :           }
   17524              : 
   17525      3033325 :         if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
   17526              :                                   !(complain & tf_error)))
   17527            0 :           return error_mark_node;
   17528              : 
   17529      3033325 :         r = build_cplus_array_type (type, domain);
   17530              : 
   17531      3033325 :         if (!valid_array_size_p (input_location, r, in_decl,
   17532              :                                  (complain & tf_error)))
   17533           15 :           return error_mark_node;
   17534              : 
   17535      3033310 :         if (TYPE_USER_ALIGN (t))
   17536              :           {
   17537            6 :             SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
   17538            6 :             TYPE_USER_ALIGN (r) = 1;
   17539              :           }
   17540              : 
   17541      3033310 :         if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
   17542              :                                              /*flags=*/0,
   17543              :                                              args, complain, in_decl))
   17544            0 :           return error_mark_node;
   17545              : 
   17546      3033310 :         return r;
   17547              :       }
   17548              : 
   17549    301438159 :     case TYPENAME_TYPE:
   17550    301438159 :       {
   17551    301438159 :         tree ctx = TYPE_CONTEXT (t);
   17552    301438159 :         if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
   17553              :           {
   17554            6 :             ctx = tsubst_pack_expansion (ctx, args,
   17555              :                                          complain | tf_qualifying_scope,
   17556              :                                          in_decl);
   17557            6 :             if (ctx == error_mark_node)
   17558              :               return error_mark_node;
   17559            6 :             if (TREE_VEC_LENGTH (ctx) > 1)
   17560              :                 {
   17561            0 :                   if (complain & tf_error)
   17562            0 :                     error ("%qD expanded to more than one element",
   17563            0 :                            TYPENAME_TYPE_FULLNAME (t));
   17564            0 :                   return error_mark_node;
   17565              :                 }
   17566            6 :             if (TREE_VEC_LENGTH (ctx) == 0)
   17567              :               {
   17568            3 :                 if (complain & tf_error)
   17569            6 :                   error ("%qD is instantiated for an empty pack",
   17570            3 :                          TYPENAME_TYPE_FULLNAME (t));
   17571            3 :                 return error_mark_node;
   17572              :               }
   17573            3 :             ctx = TREE_VEC_ELT (ctx, 0);
   17574              :           }
   17575              :         else
   17576    301438153 :           ctx = tsubst_entering_scope (ctx, args,
   17577              :                                        complain | tf_qualifying_scope,
   17578              :                                        in_decl);
   17579    301438156 :         if (ctx == error_mark_node)
   17580              :           return error_mark_node;
   17581              : 
   17582    301356690 :         tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args,
   17583              :                               complain, in_decl);
   17584    301356690 :         if (f == error_mark_node)
   17585              :           return error_mark_node;
   17586              : 
   17587              :         /* We had [:X:]:: which was substituted into a NAMESPACE_DECL and not
   17588              :            a type.  */
   17589    301356446 :         if (TREE_CODE (ctx) == NAMESPACE_DECL)
   17590              :           {
   17591           34 :             if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
   17592              :               {
   17593            6 :                 tree d = TREE_OPERAND (f, 0);
   17594            6 :                 tree n = TREE_OPERAND (f, 1);
   17595            6 :                 f = lookup_template_class (d, n, in_decl, ctx, complain);
   17596            6 :                 if (f == error_mark_node)
   17597              :                   return error_mark_node;
   17598              :               }
   17599              :             else
   17600              :               {
   17601           28 :                 gcc_assert (TREE_CODE (f) == IDENTIFIER_NODE);
   17602           28 :                 tree decl = lookup_qualified_name (ctx, f);
   17603           28 :                 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
   17604              :                   {
   17605            2 :                     qualified_name_lookup_error (ctx, f, decl, input_location);
   17606            2 :                     return error_mark_node;
   17607              :                   }
   17608           26 :                 if (TREE_CODE (decl) == NAMESPACE_DECL)
   17609              :                   return decl;
   17610              :                 else
   17611              :                   {
   17612           15 :                     gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL);
   17613           15 :                     f = TREE_TYPE (decl);
   17614              :                   }
   17615              :               }
   17616           21 :             return cp_build_qualified_type
   17617           21 :                     (f, cp_type_quals (f) | cp_type_quals (t), complain);
   17618              :           }
   17619              : 
   17620    299343630 :         if (!MAYBE_CLASS_TYPE_P (ctx))
   17621              :           {
   17622        11623 :             if (complain & tf_error)
   17623           67 :               error ("%qT is not a class, struct, or union type", ctx);
   17624        11623 :             return error_mark_node;
   17625              :           }
   17626    301344789 :         else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
   17627              :           {
   17628              :             /* Normally, make_typename_type does not require that the CTX
   17629              :                have complete type in order to allow things like:
   17630              : 
   17631              :                  template <class T> struct S { typename S<T>::X Y; };
   17632              : 
   17633              :                But, such constructs have already been resolved by this
   17634              :                point, so here CTX really should have complete type, unless
   17635              :                it's a partial instantiation.  */
   17636    273756775 :             if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
   17637         3200 :               return error_mark_node;
   17638              :           }
   17639              : 
   17640    301341589 :         enum tag_types tag_type = get_typename_tag (t);
   17641    301341589 :         tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
   17642    301341589 :         tcomplain |= tst_ok_flag | qualifying_scope_flag;
   17643    301341589 :         f = make_typename_type (ctx, f, tag_type, tcomplain);
   17644    301341589 :         if (f == error_mark_node)
   17645              :           return f;
   17646    271986728 :         if (TREE_CODE (f) == TYPE_DECL)
   17647              :           {
   17648    244163867 :             complain |= tf_ignore_bad_quals;
   17649    244163867 :             f = TREE_TYPE (f);
   17650              :           }
   17651              : 
   17652    271986728 :         if (!WILDCARD_TYPE_P (f))
   17653              :           {
   17654    244791138 :             if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
   17655              :               {
   17656            3 :                 if (complain & tf_error)
   17657            3 :                   error ("%qT resolves to %qT, which is not an enumeration type",
   17658              :                          t, f);
   17659              :                 else
   17660              :                   return error_mark_node;
   17661              :               }
   17662    241860329 :             else if (TYPENAME_IS_CLASS_OR_STRUCT_P (t)
   17663    244791191 :                      && !NON_UNION_CLASS_TYPE_P (f))
   17664              :               {
   17665           41 :                 if (complain & tf_error)
   17666            6 :                   error ("%qT resolves to %qT, which is not a non-union "
   17667              :                          "class type", t, f);
   17668              :                 else
   17669              :                   return error_mark_node;
   17670              :               }
   17671    244791094 :             else if (TYPENAME_IS_UNION_P (t) && !UNION_TYPE_P (f))
   17672              :               {
   17673            3 :                 if (complain & tf_error)
   17674            3 :                   error ("%qT resolves to %qT, which is not a union type",
   17675              :                          t, f);
   17676              :                 else
   17677              :                   return error_mark_node;
   17678              :               }
   17679              :           }
   17680              : 
   17681    271986693 :         return cp_build_qualified_type
   17682    271986693 :           (f, cp_type_quals (f) | cp_type_quals (t), complain);
   17683              :       }
   17684              : 
   17685       110086 :     case UNBOUND_CLASS_TEMPLATE:
   17686       110086 :       {
   17687       110086 :         tree name = TYPE_IDENTIFIER (t);
   17688       110086 :         if (name == error_mark_node)
   17689              :           return error_mark_node;
   17690              : 
   17691       110086 :         tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
   17692       110086 :         parm_list = tsubst_template_parms (parm_list, args, complain);
   17693       110086 :         if (parm_list == error_mark_node)
   17694              :           return error_mark_node;
   17695              : 
   17696       110101 :         if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
   17697           15 :           ++processing_template_decl;
   17698       110086 :         tree ctx = tsubst_entering_scope (TYPE_CONTEXT (t), args,
   17699              :                                           complain, in_decl);
   17700       110101 :         if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
   17701           15 :           --processing_template_decl;
   17702       110086 :         if (ctx == error_mark_node)
   17703              :           return error_mark_node;
   17704              : 
   17705       110086 :         return make_unbound_class_template (ctx, name, parm_list, complain);
   17706              :       }
   17707              : 
   17708          222 :     case TYPEOF_TYPE:
   17709          222 :       {
   17710          222 :         tree type;
   17711              : 
   17712          222 :         ++cp_unevaluated_operand;
   17713          222 :         ++c_inhibit_evaluation_warnings;
   17714              : 
   17715          222 :         type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
   17716              : 
   17717          222 :         --cp_unevaluated_operand;
   17718          222 :         --c_inhibit_evaluation_warnings;
   17719              : 
   17720          222 :         type = finish_typeof (type);
   17721          222 :         return cp_build_qualified_type (type,
   17722          222 :                                         cp_type_quals (t)
   17723          222 :                                         | cp_type_quals (type),
   17724          222 :                                         complain);
   17725              :       }
   17726              : 
   17727     45402467 :     case DECLTYPE_TYPE:
   17728     45402467 :       {
   17729     45402467 :         tree type;
   17730              : 
   17731     45402467 :         ++cp_unevaluated_operand;
   17732     45402467 :         ++c_inhibit_evaluation_warnings;
   17733              : 
   17734     45402467 :         type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
   17735              :                             complain|tf_decltype, in_decl);
   17736              : 
   17737     45391667 :         --cp_unevaluated_operand;
   17738     45391667 :         --c_inhibit_evaluation_warnings;
   17739              : 
   17740     45391667 :         if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
   17741       922050 :           type = lambda_capture_field_type (type,
   17742              :                                             false /*explicit_init*/,
   17743       461025 :                                             DECLTYPE_FOR_REF_CAPTURE (t));
   17744     44930642 :         else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
   17745          187 :           type = lambda_proxy_type (type);
   17746              :         else
   17747              :           {
   17748     44930455 :             bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
   17749      2865991 :             if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
   17750     44930455 :                 && EXPR_P (type))
   17751              :               /* In a template ~id could be either a complement expression
   17752              :                  or an unqualified-id naming a destructor; if instantiating
   17753              :                  it produces an expression, it's not an id-expression or
   17754              :                  member access.  */
   17755              :               id = false;
   17756     44930455 :             type = finish_decltype_type (type, id, complain);
   17757              :           }
   17758     45391667 :         return cp_build_qualified_type (type,
   17759     45391667 :                                         cp_type_quals (t)
   17760     45391667 :                                         | cp_type_quals (type),
   17761     45391667 :                                         complain | tf_ignore_bad_quals);
   17762              :       }
   17763              : 
   17764      9045004 :     case TRAIT_TYPE:
   17765      9045004 :       {
   17766      9045004 :         tree type1 = TRAIT_TYPE_TYPE1 (t);
   17767      9045004 :         if (TYPE_P (type1))
   17768      8938855 :           type1 = tsubst (type1, args, complain, in_decl);
   17769              :         else
   17770       106149 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   17771      9045004 :         tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
   17772      9045004 :         type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
   17773      9045004 :         return cp_build_qualified_type (type,
   17774      9045004 :                                         cp_type_quals (t) | cp_type_quals (type),
   17775      9045004 :                                         complain | tf_ignore_bad_quals);
   17776              :       }
   17777              : 
   17778            0 :     case TYPE_ARGUMENT_PACK:
   17779            0 :     case NONTYPE_ARGUMENT_PACK:
   17780            0 :       return tsubst_argument_pack (t, args, complain, in_decl);
   17781              : 
   17782         2693 :     case PACK_INDEX_TYPE:
   17783         2693 :       return tsubst_pack_index (t, args, complain, in_decl);
   17784              : 
   17785          166 :     case SPLICE_SCOPE:
   17786          166 :       return tsubst_splice_scope (t, args, complain, in_decl);
   17787              : 
   17788          142 :     case SPLICE_EXPR:
   17789              :       /* We are coming from tsubst_splice_scope for [:R:]
   17790              :          where R needs to expand to a type or scope.  */
   17791          142 :       gcc_checking_assert (!SPLICE_EXPR_EXPRESSION_P (t));
   17792          142 :       return tsubst_splice_expr (t, args, complain, in_decl);
   17793              : 
   17794           24 :     case TEMPLATE_ID_EXPR:
   17795           24 :       {
   17796              :         /* We end up here coming from tsubst_splice_scope for
   17797              :            [:R:]<int>.  R needs to expand to a type or scope.  */
   17798           24 :         tree templ = TREE_OPERAND (t, 0);
   17799           24 :         gcc_assert (TREE_CODE (templ) == SPLICE_EXPR);
   17800           24 :         templ = tsubst_splice_expr (templ, args, complain, in_decl);
   17801           24 :         if (templ == error_mark_node)
   17802              :           return error_mark_node;
   17803           18 :         if (!DECL_TYPE_TEMPLATE_P (templ)
   17804           24 :             && !DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
   17805              :           {
   17806            6 :             if (complain & tf_error)
   17807              :               {
   17808            3 :                 auto_diagnostic_group d;
   17809            6 :                 error_at (cp_expr_loc_or_input_loc (TREE_OPERAND (t, 0)),
   17810              :                           "expected a reflection of a type template");
   17811            3 :                 inform_tree_category (templ);
   17812            3 :               }
   17813            6 :             return error_mark_node;
   17814              :           }
   17815           16 :         tree targs = TREE_OPERAND (t, 1);
   17816           16 :         if (targs)
   17817           16 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   17818           16 :         if (targs == error_mark_node)
   17819              :           return error_mark_node;
   17820           16 :         tree r = finish_template_type (templ, targs, /*entering_scope=*/false);
   17821           16 :         if (TREE_CODE (r) == TYPE_DECL)
   17822           14 :           r = TREE_TYPE (r);
   17823              :         return r;
   17824              :       }
   17825              : 
   17826            0 :     case VOID_CST:
   17827            0 :     case INTEGER_CST:
   17828            0 :     case REAL_CST:
   17829            0 :     case STRING_CST:
   17830            0 :     case PLUS_EXPR:
   17831            0 :     case MINUS_EXPR:
   17832            0 :     case NEGATE_EXPR:
   17833            0 :     case NOP_EXPR:
   17834            0 :     case INDIRECT_REF:
   17835            0 :     case ADDR_EXPR:
   17836            0 :     case CALL_EXPR:
   17837            0 :     case ARRAY_REF:
   17838            0 :     case SCOPE_REF:
   17839            0 :     case OMP_ARRAY_SECTION:
   17840              :       /* We should use one of the expression tsubsts for these codes.  */
   17841            0 :       gcc_unreachable ();
   17842              : 
   17843            0 :     default:
   17844            0 :       sorry ("use of %qs in template", get_tree_code_name (code));
   17845            0 :       return error_mark_node;
   17846              :     }
   17847              : }
   17848              : 
   17849              : /* Convenience wrapper over tsubst for substituting into the LHS
   17850              :    of the :: scope resolution operator.  */
   17851              : 
   17852              : static tree
   17853     94728482 : tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17854              : {
   17855     94728482 :   gcc_checking_assert (TYPE_P (t) || TREE_CODE (t) == NAMESPACE_DECL);
   17856     94728482 :   return tsubst (t, args, complain | tf_qualifying_scope, in_decl);
   17857              : }
   17858              : 
   17859              : /* Convenience wrapper over tsubst for substituting into an id-expression
   17860              :    without resolving its terminal name.  */
   17861              : 
   17862              : static tree
   17863    439209042 : tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17864              : {
   17865    346139791 :   return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl);
   17866              : }
   17867              : 
   17868              : /* OLDFNS is a lookup set of member functions from some class template, and
   17869              :    NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
   17870              :    of that class template.  Return the subset of NEWFNS which are
   17871              :    specializations of a function from OLDFNS.  */
   17872              : 
   17873              : static tree
   17874      4742653 : filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
   17875              : {
   17876              :   /* Record all member functions from the old lookup set OLDFNS into
   17877              :      VISIBLE_SET.  */
   17878      4742653 :   hash_set<tree> visible_set;
   17879      4742653 :   bool seen_dep_using = false;
   17880     14271561 :   for (tree fn : lkp_range (oldfns))
   17881              :     {
   17882      4786255 :       if (TREE_CODE (fn) == USING_DECL)
   17883              :         {
   17884              :           /* Imprecisely handle dependent using-decl by keeping all members
   17885              :              in the new lookup set that are defined in a base class, i.e.
   17886              :              members that could plausibly have been introduced by this
   17887              :              dependent using-decl.
   17888              :              FIXME: Track which members are introduced by a dependent
   17889              :              using-decl precisely, perhaps by performing another lookup
   17890              :              from the substituted USING_DECL_SCOPE.  */
   17891            0 :           gcc_checking_assert (DECL_DEPENDENT_P (fn));
   17892              :           seen_dep_using = true;
   17893              :         }
   17894              :       else
   17895      4786255 :         visible_set.add (fn);
   17896              :     }
   17897              : 
   17898              :   /* Returns true iff (a less specialized version of) FN appeared in
   17899              :      the old lookup set OLDFNS.  */
   17900      9531863 :   auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
   17901      4789210 :     if (DECL_CONTEXT (fn) != newtype)
   17902              :       /* FN is a member function from a base class, introduced via a
   17903              :          using-decl; if it might have been introduced by a dependent
   17904              :          using-decl then just conservatively keep it, otherwise look
   17905              :          in the old lookup set for FN exactly.  */
   17906           33 :       return seen_dep_using || visible_set.contains (fn);
   17907      4789177 :     else if (TREE_CODE (fn) == TEMPLATE_DECL)
   17908              :       /* FN is a member function template from the current class;
   17909              :          look in the old lookup set for the TEMPLATE_DECL from which
   17910              :          it was specialized.  */
   17911      2444868 :       return visible_set.contains (DECL_TI_TEMPLATE (fn));
   17912              :     else
   17913              :       /* FN is a non-template member function from the current class;
   17914              :          look in the old lookup set for the FUNCTION_DECL from which
   17915              :          it was specialized.  */
   17916      2344309 :       return visible_set.contains (DECL_TEMPLATE_RESULT
   17917      2344309 :                                    (DECL_TI_TEMPLATE (fn)));
   17918      4742653 :   };
   17919              : 
   17920      4742653 :   bool lookup_changed_p = false;
   17921     14271402 :   for (tree fn : lkp_range (newfns))
   17922      4786258 :     if (!visible_p (fn))
   17923              :       {
   17924              :         lookup_changed_p = true;
   17925              :         break;
   17926              :       }
   17927      4742653 :   if (!lookup_changed_p)
   17928              :     return newfns;
   17929              : 
   17930              :   /* Filter out from NEWFNS the member functions that weren't
   17931              :      previously visible according to OLDFNS.  */
   17932          162 :   tree filtered_fns = NULL_TREE;
   17933          162 :   unsigned filtered_size = 0;
   17934         3276 :   for (tree fn : lkp_range (newfns))
   17935         2952 :     if (visible_p (fn))
   17936              :       {
   17937          162 :         filtered_fns = lookup_add (fn, filtered_fns);
   17938          162 :         filtered_size++;
   17939              :       }
   17940          162 :   gcc_checking_assert (seen_dep_using
   17941              :                        ? filtered_size >= visible_set.elements ()
   17942              :                        : filtered_size == visible_set.elements ());
   17943              : 
   17944              :   return filtered_fns;
   17945      4742653 : }
   17946              : 
   17947              : /* tsubst a BASELINK.  OBJECT_TYPE, if non-NULL, is the type of the
   17948              :    expression on the left-hand side of the "." or "->" operator.  We
   17949              :    only do the lookup if we had a dependent BASELINK.  Otherwise we
   17950              :    adjust it onto the instantiated heirarchy.  */
   17951              : 
   17952              : static tree
   17953     26851176 : tsubst_baselink (tree baselink, tree object_type,
   17954              :                  tree args, tsubst_flags_t complain, tree in_decl)
   17955              : {
   17956     26851176 :   bool qualified_p = BASELINK_QUALIFIED_P (baselink);
   17957     26851176 :   tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
   17958     26851176 :   qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
   17959              : 
   17960     26851176 :   tree optype = BASELINK_OPTYPE (baselink);
   17961     26851176 :   optype = tsubst (optype, args, complain, in_decl);
   17962              : 
   17963     26851176 :   tree template_args = NULL_TREE;
   17964     26851176 :   bool template_id_p = false;
   17965     26851176 :   tree fns = BASELINK_FUNCTIONS (baselink);
   17966     26851176 :   if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
   17967              :     {
   17968      3899554 :       template_id_p = true;
   17969      3899554 :       template_args = TREE_OPERAND (fns, 1);
   17970      3899554 :       fns = TREE_OPERAND (fns, 0);
   17971      3899554 :       if (template_args)
   17972      3899554 :         template_args = tsubst_template_args (template_args, args,
   17973              :                                               complain, in_decl);
   17974      3899554 :       if (template_args == error_mark_node)
   17975              :         return error_mark_node;
   17976              :     }
   17977              : 
   17978     26851150 :   tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
   17979     26851150 :   binfo_type = tsubst (binfo_type, args, complain, in_decl);
   17980     26851150 :   bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
   17981     52863271 :                       || optype != BASELINK_OPTYPE (baselink));
   17982              : 
   17983      8224889 :   if (dependent_p)
   17984              :     {
   17985     18626261 :       tree name = OVL_NAME (fns);
   17986     18626261 :       if (IDENTIFIER_CONV_OP_P (name))
   17987           12 :         name = make_conv_op_name (optype);
   17988              : 
   17989              :       /* See maybe_dependent_member_ref.  */
   17990     18626261 :       if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
   17991              :         {
   17992        85410 :           if (template_id_p)
   17993        84975 :             name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
   17994              :                            template_args);
   17995        85410 :           return build_qualified_name (NULL_TREE, qualifying_scope, name,
   17996        85410 :                                        /* ::template */false);
   17997              :         }
   17998              : 
   17999     18540851 :       if (name == complete_dtor_identifier)
   18000              :         /* Treat as-if non-dependent below.  */
   18001            0 :         dependent_p = false;
   18002              : 
   18003     18540851 :       bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
   18004     18540851 :       baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
   18005              :                                   complain);
   18006     18540851 :       if (!baselink)
   18007              :         {
   18008            6 :           if (complain & tf_error)
   18009              :             {
   18010            6 :               if (constructor_name_p (name, qualifying_scope))
   18011            0 :                 error ("cannot call constructor %<%T::%D%> directly",
   18012              :                        qualifying_scope, name);
   18013              :               else
   18014              :                 /* Lookup succeeded at parse time, but failed during
   18015              :                    instantiation; must be because we're trying to refer to it
   18016              :                    while forming its declaration (c++/120204).  */
   18017            6 :                 error ("declaration of %<%T::%D%> depends on itself",
   18018              :                        qualifying_scope, name);
   18019              :             }
   18020            6 :           return error_mark_node;
   18021              :         }
   18022              : 
   18023     18540845 :       if (maybe_incomplete)
   18024              :         {
   18025              :           /* Filter out from the new lookup set those functions which didn't
   18026              :              appear in the original lookup set (in a less specialized form).
   18027              :              This is needed to preserve the consistency of member lookup
   18028              :              performed in an incomplete-class context, within which
   18029              :              later-declared members ought to remain invisible.  */
   18030      4742653 :           BASELINK_FUNCTIONS (baselink)
   18031      4742653 :             = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
   18032              :                                    binfo_type);
   18033      4742653 :           BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
   18034              :         }
   18035              : 
   18036     18540845 :       fns = BASELINK_FUNCTIONS (baselink);
   18037              :     }
   18038              :   else
   18039              :     {
   18040              :       /* We're going to overwrite pieces below, make a duplicate.  */
   18041      8224889 :       baselink = copy_node (baselink);
   18042              : 
   18043      8224889 :       if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
   18044              :         {
   18045              :           /* The decl we found was from non-dependent scope, but we still need
   18046              :              to update the binfos for the instantiated qualifying_scope.  */
   18047       225711 :           BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
   18048       225711 :           BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
   18049              :                                                    ba_unique, nullptr, complain);
   18050              :         }
   18051              :     }
   18052              : 
   18053              :   /* If lookup found a single function, mark it as used at this point.
   18054              :      (If lookup found multiple functions the one selected later by
   18055              :      overload resolution will be marked as used at that point.)  */
   18056     26765734 :   if (!template_id_p && !really_overloaded_fn (fns))
   18057              :     {
   18058     17748003 :       tree fn = OVL_FIRST (fns);
   18059     17748003 :       bool ok = mark_used (fn, complain);
   18060     17748003 :       if (!ok && !(complain & tf_error))
   18061            6 :         return error_mark_node;
   18062     17747994 :       if (ok && BASELINK_P (baselink))
   18063              :         /* We might have instantiated an auto function.  */
   18064     17747994 :         TREE_TYPE (baselink) = TREE_TYPE (fn);
   18065              :     }
   18066              : 
   18067     26765728 :   if (BASELINK_P (baselink))
   18068              :     {
   18069              :       /* Add back the template arguments, if present.  */
   18070     26765728 :       if (template_id_p)
   18071      7629106 :         BASELINK_FUNCTIONS (baselink)
   18072      3814553 :           = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
   18073              : 
   18074              :       /* Update the conversion operator type.  */
   18075     26765728 :       BASELINK_OPTYPE (baselink) = optype;
   18076              :     }
   18077              : 
   18078     26765728 :   if (!object_type)
   18079       588574 :     object_type = current_class_type;
   18080              : 
   18081     26765728 :   if (qualified_p || !dependent_p)
   18082              :     {
   18083      8281741 :       baselink = adjust_result_of_qualified_name_lookup (baselink,
   18084              :                                                          qualifying_scope,
   18085              :                                                          object_type);
   18086      8281741 :       if (!qualified_p)
   18087              :         /* We need to call adjust_result_of_qualified_name_lookup in case the
   18088              :            destructor names a base class, but we unset BASELINK_QUALIFIED_P
   18089              :            so that we still get virtual function binding.  */
   18090      8119642 :         BASELINK_QUALIFIED_P (baselink) = false;
   18091              :     }
   18092              : 
   18093              :   return baselink;
   18094              : }
   18095              : 
   18096              : /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
   18097              :    true if the qualified-id will be a postfix-expression in-and-of
   18098              :    itself; false if more of the postfix-expression follows the
   18099              :    QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
   18100              :    of "&".  NAME_LOOKUP_P is true if we intend to perform name lookup.  */
   18101              : 
   18102              : static tree
   18103     94219494 : tsubst_qualified_id (tree qualified_id, tree args,
   18104              :                      tsubst_flags_t complain, tree in_decl,
   18105              :                      bool done, bool address_p, bool name_lookup_p = true)
   18106              : {
   18107     94219494 :   tree expr;
   18108     94219494 :   tree scope;
   18109     94219494 :   tree name;
   18110     94219494 :   bool is_template;
   18111     94219494 :   tree template_args;
   18112     94219494 :   location_t loc = EXPR_LOCATION (qualified_id);
   18113              : 
   18114     94219494 :   gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
   18115              : 
   18116              :   /* Figure out what name to look up.  */
   18117     94219494 :   name = TREE_OPERAND (qualified_id, 1);
   18118     94219494 :   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
   18119              :     {
   18120        63757 :       is_template = true;
   18121        63757 :       template_args = TREE_OPERAND (name, 1);
   18122        63757 :       if (template_args)
   18123        63757 :         template_args = tsubst_template_args (template_args, args,
   18124              :                                               complain, in_decl);
   18125        63757 :       if (template_args == error_mark_node)
   18126              :         return error_mark_node;
   18127        63749 :       name = TREE_OPERAND (name, 0);
   18128              :     }
   18129              :   else
   18130              :     {
   18131              :       is_template = false;
   18132              :       template_args = NULL_TREE;
   18133              :     }
   18134              : 
   18135              :   /* Substitute into the qualifying scope.  When there are no ARGS, we
   18136              :      are just trying to simplify a non-dependent expression.  In that
   18137              :      case the qualifying scope may be dependent, and, in any case,
   18138              :      substituting will not help.  */
   18139     94219486 :   scope = TREE_OPERAND (qualified_id, 0);
   18140     94219486 :   if (args)
   18141              :     {
   18142     93069251 :       scope = tsubst_scope (scope, args, complain, in_decl);
   18143     93069251 :       expr = tsubst_name (name, args, complain, in_decl);
   18144              :     }
   18145              :   else
   18146              :     expr = name;
   18147              : 
   18148     94219486 :   if (dependent_scope_p (scope) || dependent_namespace_p (scope))
   18149              :     {
   18150     10803644 :       if (TREE_CODE (expr) == SCOPE_REF)
   18151              :         /* We built one in tsubst_baselink.  */
   18152            6 :         gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
   18153              :       else
   18154              :         {
   18155     10803638 :           if (is_template)
   18156        10974 :             expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
   18157              :                                      template_args);
   18158     10803638 :           expr = build_qualified_name (NULL_TREE, scope, expr,
   18159     10803638 :                                        QUALIFIED_NAME_IS_TEMPLATE
   18160              :                                        (qualified_id));
   18161              :         }
   18162     10803644 :       REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
   18163     10803644 :       return expr;
   18164              :     }
   18165              : 
   18166     83415842 :   if (!BASELINK_P (name) && !DECL_P (expr))
   18167              :     {
   18168     77438254 :       if (TREE_CODE (expr) == BIT_NOT_EXPR)
   18169              :         {
   18170              :           /* A BIT_NOT_EXPR is used to represent a destructor.  */
   18171            3 :           if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
   18172              :             {
   18173            0 :               error ("qualifying type %qT does not match destructor name ~%qT",
   18174            0 :                      scope, TREE_OPERAND (expr, 0));
   18175            0 :               expr = error_mark_node;
   18176              :             }
   18177              :           else
   18178            3 :             expr = lookup_qualified_name (scope, complete_dtor_identifier,
   18179              :                                           LOOK_want::NORMAL, false);
   18180              :         }
   18181              :       else
   18182     77438251 :         expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
   18183     77438200 :       if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
   18184              :                      ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
   18185              :         {
   18186           30 :           if (complain & tf_error)
   18187              :             {
   18188           24 :               auto_diagnostic_group d;
   18189           24 :               error ("dependent-name %qE is parsed as a non-type, but "
   18190              :                      "instantiation yields a type", qualified_id);
   18191           24 :               inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
   18192           24 :             }
   18193           30 :           return error_mark_node;
   18194              :         }
   18195              :     }
   18196              : 
   18197     83415758 :   if (DECL_P (expr))
   18198              :     {
   18199     77887688 :       if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
   18200              :                                                 scope, complain))
   18201           17 :         return error_mark_node;
   18202              :       /* Remember that there was a reference to this entity.  */
   18203     77887671 :       if (!mark_used (expr, complain) && !(complain & tf_error))
   18204            0 :         return error_mark_node;
   18205              :     }
   18206              : 
   18207     83407650 :   if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
   18208              :     {
   18209        63463 :       if (complain & tf_error)
   18210          151 :         qualified_name_lookup_error (scope,
   18211          151 :                                      TREE_OPERAND (qualified_id, 1),
   18212              :                                      expr, input_location);
   18213        63463 :       return error_mark_node;
   18214              :     }
   18215              : 
   18216     83344187 :   if (is_template)
   18217              :     {
   18218        49620 :       if (variable_template_p (expr))
   18219         9852 :         expr = lookup_and_finish_template_variable (expr, template_args,
   18220              :                                                     complain);
   18221              :       else
   18222        39768 :         expr = lookup_template_function (expr, template_args);
   18223              :     }
   18224              : 
   18225     83344187 :   if (expr == error_mark_node && complain & tf_error)
   18226            3 :     qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
   18227              :                                  expr, input_location);
   18228              :   /* For ^^S::mem, we do not want to create the dummy object that
   18229              :      finish_non_static_data_member would give us.  */
   18230     83344184 :   else if (TYPE_P (scope) && name_lookup_p)
   18231              :     {
   18232     83344150 :       expr = (adjust_result_of_qualified_name_lookup
   18233     83344150 :               (expr, scope, current_nonlambda_class_type ()));
   18234     83344150 :       expr = (finish_qualified_id_expr
   18235     83344156 :               (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
   18236     83344150 :                QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
   18237              :                /*template_arg_p=*/false, complain));
   18238              :     }
   18239              : 
   18240              :   /* Expressions do not generally have reference type.  */
   18241     83344187 :   if (TREE_CODE (expr) != SCOPE_REF
   18242              :       /* However, if we're about to form a pointer-to-member, we just
   18243              :          want the referenced member referenced.  */
   18244     80289706 :       && TREE_CODE (expr) != OFFSET_REF)
   18245     80260379 :     expr = convert_from_reference (expr);
   18246              : 
   18247     83344187 :   if (REF_PARENTHESIZED_P (qualified_id))
   18248          125 :     expr = force_paren_expr (expr);
   18249              : 
   18250     83344187 :   expr = maybe_wrap_with_location (expr, loc);
   18251              : 
   18252     83344187 :   return expr;
   18253              : }
   18254              : 
   18255              : /* tsubst the initializer for a VAR_DECL.  INIT is the unsubstituted
   18256              :    initializer, DECL is the substituted VAR_DECL.  Other arguments are as
   18257              :    for tsubst.  */
   18258              : 
   18259              : static tree
   18260     49388610 : tsubst_init (tree init, tree decl, tree args,
   18261              :              tsubst_flags_t complain, tree in_decl)
   18262              : {
   18263     49388610 :   if (!init)
   18264              :     return NULL_TREE;
   18265              : 
   18266     41341205 :   init = tsubst_expr (init, args, complain, in_decl);
   18267              : 
   18268     41341205 :   tree type = TREE_TYPE (decl);
   18269              : 
   18270     41341205 :   if (!init && type != error_mark_node)
   18271              :     {
   18272           74 :       if (tree auto_node = type_uses_auto (type))
   18273              :         {
   18274            8 :           if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   18275              :             {
   18276            6 :               if (complain & tf_error)
   18277            6 :                 error ("initializer for %q#D expands to an empty list "
   18278              :                        "of expressions", decl);
   18279            6 :               return error_mark_node;
   18280              :             }
   18281              :         }
   18282           66 :       else if (!dependent_type_p (type))
   18283              :         {
   18284              :           /* If we had an initializer but it
   18285              :              instantiated to nothing,
   18286              :              value-initialize the object.  This will
   18287              :              only occur when the initializer was a
   18288              :              pack expansion where the parameter packs
   18289              :              used in that expansion were of length
   18290              :              zero.  */
   18291           66 :           init = build_value_init (type, complain);
   18292           66 :           if (TREE_CODE (init) == AGGR_INIT_EXPR)
   18293           45 :             init = get_target_expr (init, complain);
   18294           66 :           if (TREE_CODE (init) == TARGET_EXPR)
   18295           45 :             TARGET_EXPR_DIRECT_INIT_P (init) = true;
   18296              :         }
   18297              :     }
   18298              : 
   18299              :   return init;
   18300              : }
   18301              : 
   18302              : /* If T is a reference to a dependent member of the current instantiation C and
   18303              :    we are trying to refer to that member in a partial instantiation of C,
   18304              :    return a SCOPE_REF; otherwise, return NULL_TREE.
   18305              : 
   18306              :    This can happen when forming a C++17 deduction guide, as in PR96199.  */
   18307              : 
   18308              : static tree
   18309   8942955369 : maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
   18310              :                             tree in_decl)
   18311              : {
   18312   8942955369 :   if (!(complain & tf_dguide))
   18313              :     return NULL_TREE;
   18314              : 
   18315      4644764 :   tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
   18316      4644764 :   if (!decl || !DECL_P (decl))
   18317              :     return NULL_TREE;
   18318              : 
   18319      2573489 :   tree ctx = context_for_name_lookup (decl);
   18320      2573489 :   if (!CLASS_TYPE_P (ctx))
   18321              :     return NULL_TREE;
   18322              : 
   18323       248022 :   ctx = tsubst (ctx, args, complain, in_decl);
   18324       248022 :   if (!dependent_scope_p (ctx))
   18325              :     return NULL_TREE;
   18326              : 
   18327       244041 :   if (TYPE_P (t))
   18328              :     {
   18329       150949 :       bool stripped = false;
   18330       150949 :       if (typedef_variant_p (t))
   18331              :         {
   18332              :           /* Since this transformation may undesirably turn a deduced context
   18333              :              into a non-deduced one, we'd rather strip typedefs than perform
   18334              :              the transformation.  */
   18335        88065 :           tree u = strip_typedefs (t);
   18336        88065 :           if (u != t)
   18337              :             {
   18338       150949 :               stripped = true;
   18339       150949 :               t = u;
   18340              :             }
   18341              :         }
   18342       150949 :       decl = TYPE_NAME (t);
   18343       150949 :       if (decl)
   18344       150349 :         decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
   18345       150349 :       if (!decl)
   18346              :         {
   18347       131441 :           if (stripped)
   18348              :             /* The original type was an alias from the current instantiation
   18349              :                which we stripped to something outside it.  At this point we
   18350              :                need to commit to using the stripped type rather than deferring
   18351              :                to the caller (which would use the original type), to ensure
   18352              :                eligible bits of the stripped type get transformed.  */
   18353        69365 :             return tsubst (t, args, complain, in_decl);
   18354              :           else
   18355              :             /* The original type wasn't a typedef, and we decided it doesn't
   18356              :                need rewriting, so just let the caller (tsubst) substitute it
   18357              :                normally.  */
   18358              :             return NULL_TREE;
   18359              :         }
   18360        19508 :       return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
   18361        19508 :                                       complain);
   18362              :     }
   18363              : 
   18364        93092 :   tree name = DECL_NAME (t);
   18365        93092 :   tree fullname = name;
   18366        93092 :   if (instantiates_primary_template_p (t))
   18367              :     {
   18368        18708 :       tree tinfo = get_template_info (t);
   18369        18708 :       name = DECL_NAME (TI_TEMPLATE (tinfo));
   18370        18708 :       tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
   18371        18708 :       targs = tsubst_template_args (targs, args, complain, in_decl);
   18372        18708 :       fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
   18373              :     }
   18374              : 
   18375        93092 :   if (TREE_CODE (t) == TYPE_DECL)
   18376              :     {
   18377        83171 :       if (!is_typedef_decl (t)
   18378        64471 :           && TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
   18379        63663 :           && TYPE_NAME (TREE_TYPE (t)) == t)
   18380              :         /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
   18381              :            scope, but it doesn't need to be rewritten again.  */
   18382              :         return NULL_TREE;
   18383        19508 :       tree type = build_typename_type (ctx, name, fullname, typename_type);
   18384        19508 :       return TYPE_NAME (type);
   18385              :     }
   18386         9921 :   else if (DECL_TYPE_TEMPLATE_P (t))
   18387           24 :     return make_unbound_class_template (ctx, name,
   18388           24 :                                         NULL_TREE, complain);
   18389              :   else
   18390         9897 :     return build_qualified_name (NULL_TREE, ctx, fullname,
   18391         9897 :                                  TREE_CODE (t) == TEMPLATE_DECL);
   18392              : }
   18393              : 
   18394              : /* Helper function for tsubst_omp_clauses, used for instantiation of
   18395              :    OMP_CLAUSE_DECL of clauses.  */
   18396              : 
   18397              : static tree
   18398         6237 : tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
   18399              :                         tree in_decl, tree *iterator_cache)
   18400              : {
   18401         6237 :   if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
   18402              :     return decl;
   18403              : 
   18404              :   /* Handle OpenMP iterators.  */
   18405         6189 :   if (OMP_ITERATOR_DECL_P (decl))
   18406              :     {
   18407          135 :       tree ret;
   18408          135 :       if (iterator_cache[0] == TREE_PURPOSE (decl))
   18409           20 :         ret = iterator_cache[1];
   18410              :       else
   18411              :         {
   18412          115 :           tree *tp = &ret;
   18413          115 :           begin_scope (sk_omp, NULL);
   18414          254 :           for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
   18415              :             {
   18416          139 :               *tp = copy_node (it);
   18417          139 :               TREE_VEC_ELT (*tp, 0)
   18418          139 :                 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
   18419          139 :               DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
   18420          139 :               pushdecl (TREE_VEC_ELT (*tp, 0));
   18421          139 :               TREE_VEC_ELT (*tp, 1)
   18422          139 :                 = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl);
   18423          139 :               TREE_VEC_ELT (*tp, 2)
   18424          139 :                 = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl);
   18425          139 :               TREE_VEC_ELT (*tp, 3)
   18426          139 :                 = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl);
   18427          139 :               TREE_CHAIN (*tp) = NULL_TREE;
   18428          139 :               tp = &TREE_CHAIN (*tp);
   18429              :             }
   18430          115 :           TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
   18431          115 :           iterator_cache[0] = TREE_PURPOSE (decl);
   18432          115 :           iterator_cache[1] = ret;
   18433              :         }
   18434          135 :       return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
   18435              :                                                            args, complain,
   18436              :                                                            in_decl, NULL));
   18437              :     }
   18438              : 
   18439              :   /* Handle an OpenMP array section represented as a TREE_LIST (or
   18440              :      OMP_CLAUSE_DOACROSS_KIND).  An OMP_CLAUSE_DOACROSS (with a depend
   18441              :      kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
   18442              :      TREE_LIST.  We can handle it exactly the same as an array section
   18443              :      (purpose, value, and a chain), even though the nomenclature
   18444              :      (low_bound, length, etc) is different.  */
   18445         6054 :   if (TREE_CODE (decl) == TREE_LIST)
   18446              :     {
   18447           27 :       tree low_bound
   18448           27 :         = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl);
   18449           27 :       tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl);
   18450           27 :       tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
   18451              :                                            in_decl, NULL);
   18452           27 :       if (TREE_PURPOSE (decl) == low_bound
   18453           27 :           && TREE_VALUE (decl) == length
   18454           36 :           && TREE_CHAIN (decl) == chain)
   18455              :         return decl;
   18456           18 :       tree ret = tree_cons (low_bound, length, chain);
   18457           18 :       OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
   18458           18 :         = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
   18459           18 :       return ret;
   18460              :     }
   18461         6027 :   else if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
   18462              :     {
   18463         1855 :       tree low_bound
   18464         1855 :         = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl);
   18465         1855 :       tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain,
   18466              :                                  in_decl);
   18467         1855 :       tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args,
   18468              :                                            complain, in_decl, NULL);
   18469         1855 :       if (TREE_OPERAND (decl, 0) == base
   18470          594 :           && TREE_OPERAND (decl, 1) == low_bound
   18471         2337 :           && TREE_OPERAND (decl, 2) == length)
   18472              :         return decl;
   18473         1382 :       return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound,
   18474         1382 :                      length);
   18475              :     }
   18476         4172 :   tree ret = tsubst_stmt (decl, args, complain, in_decl);
   18477              :   /* Undo convert_from_reference tsubst_expr could have called.  */
   18478         4172 :   if (decl
   18479         4172 :       && REFERENCE_REF_P (ret)
   18480          561 :       && !REFERENCE_REF_P (decl))
   18481          551 :     ret = TREE_OPERAND (ret, 0);
   18482              :   return ret;
   18483              : }
   18484              : 
   18485              : /* Like tsubst_copy, but specifically for OpenMP clauses.  */
   18486              : 
   18487              : static tree
   18488         4793 : tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
   18489              :                     tree args, tsubst_flags_t complain, tree in_decl)
   18490              : {
   18491         4793 :   tree new_clauses = NULL_TREE, nc, oc;
   18492         4793 :   tree linear_no_step = NULL_TREE;
   18493         4793 :   tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
   18494              : 
   18495        10752 :   for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
   18496              :     {
   18497         5959 :       nc = copy_node (oc);
   18498         5959 :       OMP_CLAUSE_CHAIN (nc) = new_clauses;
   18499         5959 :       new_clauses = nc;
   18500              : 
   18501         5959 :       switch (OMP_CLAUSE_CODE (nc))
   18502              :         {
   18503          190 :         case OMP_CLAUSE_LASTPRIVATE:
   18504          190 :           if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
   18505              :             {
   18506           38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
   18507           38 :               tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
   18508              :                            complain, in_decl);
   18509           38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc)
   18510           76 :                 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
   18511              :             }
   18512              :           /* FALLTHRU */
   18513         2932 :         case OMP_CLAUSE_PRIVATE:
   18514         2932 :         case OMP_CLAUSE_SHARED:
   18515         2932 :         case OMP_CLAUSE_FIRSTPRIVATE:
   18516         2932 :         case OMP_CLAUSE_COPYIN:
   18517         2932 :         case OMP_CLAUSE_COPYPRIVATE:
   18518         2932 :         case OMP_CLAUSE_UNIFORM:
   18519         2932 :         case OMP_CLAUSE_DEPEND:
   18520         2932 :         case OMP_CLAUSE_DOACROSS:
   18521         2932 :         case OMP_CLAUSE_AFFINITY:
   18522         2932 :         case OMP_CLAUSE_FROM:
   18523         2932 :         case OMP_CLAUSE_TO:
   18524         2932 :         case OMP_CLAUSE_MAP:
   18525         2932 :         case OMP_CLAUSE__CACHE_:
   18526         2932 :         case OMP_CLAUSE_NONTEMPORAL:
   18527         2932 :         case OMP_CLAUSE_USE_DEVICE_PTR:
   18528         2932 :         case OMP_CLAUSE_USE_DEVICE_ADDR:
   18529         2932 :         case OMP_CLAUSE_IS_DEVICE_PTR:
   18530         2932 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
   18531         2932 :         case OMP_CLAUSE_INCLUSIVE:
   18532         2932 :         case OMP_CLAUSE_EXCLUSIVE:
   18533         5864 :           OMP_CLAUSE_DECL (nc)
   18534         2932 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18535              :                                       in_decl, iterator_cache);
   18536         2932 :           break;
   18537          150 :         case OMP_CLAUSE_NUM_TEAMS:
   18538          150 :           if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
   18539           66 :             OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
   18540          132 :               = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
   18541              :                              complain, in_decl);
   18542              :           /* FALLTHRU */
   18543          762 :         case OMP_CLAUSE_TILE:
   18544          762 :         case OMP_CLAUSE_IF:
   18545          762 :         case OMP_CLAUSE_SELF:
   18546          762 :         case OMP_CLAUSE_NUM_THREADS:
   18547          762 :         case OMP_CLAUSE_SCHEDULE:
   18548          762 :         case OMP_CLAUSE_COLLAPSE:
   18549          762 :         case OMP_CLAUSE_FINAL:
   18550          762 :         case OMP_CLAUSE_DEVICE:
   18551          762 :         case OMP_CLAUSE_DIST_SCHEDULE:
   18552          762 :         case OMP_CLAUSE_THREAD_LIMIT:
   18553          762 :         case OMP_CLAUSE_SAFELEN:
   18554          762 :         case OMP_CLAUSE_SIMDLEN:
   18555          762 :         case OMP_CLAUSE_NUM_TASKS:
   18556          762 :         case OMP_CLAUSE_GRAINSIZE:
   18557          762 :         case OMP_CLAUSE_PRIORITY:
   18558          762 :         case OMP_CLAUSE_ORDERED:
   18559          762 :         case OMP_CLAUSE_HINT:
   18560          762 :         case OMP_CLAUSE_FILTER:
   18561          762 :         case OMP_CLAUSE_NUM_GANGS:
   18562          762 :         case OMP_CLAUSE_NUM_WORKERS:
   18563          762 :         case OMP_CLAUSE_VECTOR_LENGTH:
   18564          762 :         case OMP_CLAUSE_WORKER:
   18565          762 :         case OMP_CLAUSE_VECTOR:
   18566          762 :         case OMP_CLAUSE_ASYNC:
   18567          762 :         case OMP_CLAUSE_WAIT:
   18568          762 :         case OMP_CLAUSE_DETACH:
   18569          762 :         case OMP_CLAUSE_DYN_GROUPPRIVATE:
   18570          762 :           OMP_CLAUSE_OPERAND (nc, 0)
   18571          762 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18572          762 :           break;
   18573           18 :         case OMP_CLAUSE_PARTIAL:
   18574           18 :           OMP_CLAUSE_PARTIAL_EXPR (nc)
   18575           18 :             = tsubst_expr (OMP_CLAUSE_PARTIAL_EXPR (oc), args, complain,
   18576              :                            in_decl);
   18577           18 :           break;
   18578           36 :         case OMP_CLAUSE_SIZES:
   18579           36 :           OMP_CLAUSE_SIZES_LIST (nc)
   18580           36 :             = tsubst_expr (OMP_CLAUSE_SIZES_LIST (oc), args, complain,
   18581              :                            in_decl);
   18582           36 :           break;
   18583            6 :         case OMP_CLAUSE_NOCONTEXT:
   18584            6 :           OMP_CLAUSE_NOCONTEXT_EXPR (nc)
   18585            6 :             = tsubst_expr (OMP_CLAUSE_NOCONTEXT_EXPR (oc), args, complain,
   18586              :                            in_decl);
   18587            6 :           break;
   18588            6 :         case OMP_CLAUSE_NOVARIANTS:
   18589            6 :           OMP_CLAUSE_NOVARIANTS_EXPR (nc)
   18590            6 :             = tsubst_expr (OMP_CLAUSE_NOVARIANTS_EXPR (oc), args, complain,
   18591              :                            in_decl);
   18592            6 :           break;
   18593          890 :         case OMP_CLAUSE_REDUCTION:
   18594          890 :         case OMP_CLAUSE_IN_REDUCTION:
   18595          890 :         case OMP_CLAUSE_TASK_REDUCTION:
   18596          890 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
   18597              :             {
   18598           45 :               tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
   18599           45 :               if (TREE_CODE (placeholder) == SCOPE_REF)
   18600              :                 {
   18601            5 :                   tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
   18602              :                                        complain, in_decl);
   18603            5 :                   OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
   18604           10 :                     = build_qualified_name (NULL_TREE, scope,
   18605            5 :                                             TREE_OPERAND (placeholder, 1),
   18606              :                                             false);
   18607              :                 }
   18608              :               else
   18609           40 :                 gcc_assert (identifier_p (placeholder));
   18610              :             }
   18611         1780 :           OMP_CLAUSE_DECL (nc)
   18612          890 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18613              :                                       in_decl, NULL);
   18614          890 :           break;
   18615           66 :         case OMP_CLAUSE_GANG:
   18616           66 :         case OMP_CLAUSE_ALIGNED:
   18617          132 :           OMP_CLAUSE_DECL (nc)
   18618           66 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18619              :                                       in_decl, NULL);
   18620           66 :           OMP_CLAUSE_OPERAND (nc, 1)
   18621           66 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18622           66 :           break;
   18623          205 :         case OMP_CLAUSE_ALLOCATE:
   18624          410 :           OMP_CLAUSE_DECL (nc)
   18625          205 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18626              :                                       in_decl, NULL);
   18627          205 :           OMP_CLAUSE_OPERAND (nc, 1)
   18628          205 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18629          205 :           OMP_CLAUSE_OPERAND (nc, 2)
   18630          205 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   18631          205 :           break;
   18632          127 :         case OMP_CLAUSE_LINEAR:
   18633          254 :           OMP_CLAUSE_DECL (nc)
   18634          127 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18635              :                                       in_decl, NULL);
   18636          127 :           if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
   18637              :             {
   18638            2 :               gcc_assert (!linear_no_step);
   18639              :               linear_no_step = nc;
   18640              :             }
   18641          125 :           else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
   18642            0 :             OMP_CLAUSE_LINEAR_STEP (nc)
   18643            0 :               = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
   18644              :                                         complain, in_decl, NULL);
   18645              :           else
   18646          125 :             OMP_CLAUSE_LINEAR_STEP (nc)
   18647          250 :               = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args,
   18648              :                              complain, in_decl);
   18649              :           break;
   18650           18 :         case OMP_CLAUSE_USES_ALLOCATORS:
   18651           18 :           OMP_CLAUSE_OPERAND (nc, 0)
   18652           18 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18653           18 :           OMP_CLAUSE_OPERAND (nc, 1)
   18654           18 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   18655           18 :           OMP_CLAUSE_OPERAND (nc, 2)
   18656           18 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   18657           18 :           break;
   18658          138 :         case OMP_CLAUSE_INIT:
   18659          138 :           if (ort == C_ORT_OMP_INTEROP
   18660          138 :               && OMP_CLAUSE_INIT_PREFER_TYPE (nc)
   18661           66 :               && TREE_CODE (OMP_CLAUSE_INIT_PREFER_TYPE (nc)) == TREE_LIST
   18662          192 :               && (OMP_CLAUSE_CHAIN (nc)  == NULL_TREE
   18663           42 :                   || OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (nc) ) != OMP_CLAUSE_INIT
   18664           36 :                   || (OMP_CLAUSE_INIT_PREFER_TYPE (nc)
   18665           36 :                       != OMP_CLAUSE_INIT_PREFER_TYPE (OMP_CLAUSE_CHAIN (nc) ))))
   18666              :             {
   18667           30 :               tree pref_list = OMP_CLAUSE_INIT_PREFER_TYPE (nc);
   18668           30 :               tree fr_list = TREE_VALUE (pref_list);
   18669           30 :               int len = TREE_VEC_LENGTH (fr_list);
   18670          144 :               for (int i = 0; i < len; i++)
   18671              :                 {
   18672          114 :                   tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
   18673              :                   /* Preserve NOP_EXPR to have a location.  */
   18674          114 :                   if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
   18675           42 :                     TREE_OPERAND (*fr_expr, 0)
   18676           84 :                       = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
   18677              :                                      in_decl);
   18678              :                   else
   18679           72 :                     *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
   18680              :                 }
   18681              :             }
   18682              :           /* FALLTHRU */
   18683          192 :         case OMP_CLAUSE_DESTROY:
   18684          192 :         case OMP_CLAUSE_USE:
   18685          192 :         case OMP_CLAUSE_INTEROP:
   18686          192 :           OMP_CLAUSE_OPERAND (nc, 0)
   18687          192 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18688          192 :           break;
   18689              :         case OMP_CLAUSE_NOWAIT:
   18690              :         case OMP_CLAUSE_DEFAULT:
   18691              :         case OMP_CLAUSE_UNTIED:
   18692              :         case OMP_CLAUSE_MERGEABLE:
   18693              :         case OMP_CLAUSE_INBRANCH:
   18694              :         case OMP_CLAUSE_NOTINBRANCH:
   18695              :         case OMP_CLAUSE_PROC_BIND:
   18696              :         case OMP_CLAUSE_FOR:
   18697              :         case OMP_CLAUSE_PARALLEL:
   18698              :         case OMP_CLAUSE_SECTIONS:
   18699              :         case OMP_CLAUSE_TASKGROUP:
   18700              :         case OMP_CLAUSE_NOGROUP:
   18701              :         case OMP_CLAUSE_THREADS:
   18702              :         case OMP_CLAUSE_SIMD:
   18703              :         case OMP_CLAUSE_DEFAULTMAP:
   18704              :         case OMP_CLAUSE_ORDER:
   18705              :         case OMP_CLAUSE_BIND:
   18706              :         case OMP_CLAUSE_INDEPENDENT:
   18707              :         case OMP_CLAUSE_AUTO:
   18708              :         case OMP_CLAUSE_SEQ:
   18709              :         case OMP_CLAUSE_IF_PRESENT:
   18710              :         case OMP_CLAUSE_FINALIZE:
   18711              :         case OMP_CLAUSE_NOHOST:
   18712              :         case OMP_CLAUSE_FULL:
   18713              :           break;
   18714            0 :         default:
   18715            0 :           gcc_unreachable ();
   18716              :         }
   18717         5959 :       if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
   18718         5066 :         switch (OMP_CLAUSE_CODE (nc))
   18719              :           {
   18720         1850 :           case OMP_CLAUSE_SHARED:
   18721         1850 :           case OMP_CLAUSE_PRIVATE:
   18722         1850 :           case OMP_CLAUSE_FIRSTPRIVATE:
   18723         1850 :           case OMP_CLAUSE_LASTPRIVATE:
   18724         1850 :           case OMP_CLAUSE_COPYPRIVATE:
   18725         1850 :           case OMP_CLAUSE_LINEAR:
   18726         1850 :           case OMP_CLAUSE_REDUCTION:
   18727         1850 :           case OMP_CLAUSE_IN_REDUCTION:
   18728         1850 :           case OMP_CLAUSE_TASK_REDUCTION:
   18729         1850 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   18730         1850 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   18731         1850 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   18732         1850 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   18733         1850 :           case OMP_CLAUSE_INCLUSIVE:
   18734         1850 :           case OMP_CLAUSE_EXCLUSIVE:
   18735         1850 :           case OMP_CLAUSE_ALLOCATE:
   18736              :             /* tsubst_expr on SCOPE_REF results in returning
   18737              :                finish_non_static_data_member result.  Undo that here.  */
   18738         1850 :             if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
   18739         1850 :                 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
   18740              :                     == IDENTIFIER_NODE))
   18741              :               {
   18742           79 :                 tree t = OMP_CLAUSE_DECL (nc);
   18743           79 :                 tree v = t;
   18744          464 :                 while (v)
   18745          464 :                   switch (TREE_CODE (v))
   18746              :                     {
   18747          385 :                     case COMPONENT_REF:
   18748          385 :                     case MEM_REF:
   18749          385 :                     case INDIRECT_REF:
   18750          385 :                     CASE_CONVERT:
   18751          385 :                     case POINTER_PLUS_EXPR:
   18752          385 :                       v = TREE_OPERAND (v, 0);
   18753          385 :                       continue;
   18754           77 :                     case PARM_DECL:
   18755           77 :                       if (DECL_CONTEXT (v) == current_function_decl
   18756           77 :                           && DECL_ARTIFICIAL (v)
   18757          154 :                           && DECL_NAME (v) == this_identifier)
   18758           77 :                         OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
   18759              :                       /* FALLTHRU */
   18760              :                     default:
   18761              :                       v = NULL_TREE;
   18762              :                       break;
   18763              :                     }
   18764              :               }
   18765         1771 :             else if (VAR_P (OMP_CLAUSE_DECL (oc))
   18766          913 :                      && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
   18767          197 :                      && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
   18768          197 :                      && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
   18769         1968 :                      && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
   18770              :               {
   18771          197 :                 tree decl = OMP_CLAUSE_DECL (nc);
   18772          197 :                 if (VAR_P (decl))
   18773              :                   {
   18774          197 :                     retrofit_lang_decl (decl);
   18775          197 :                     DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
   18776              :                   }
   18777              :               }
   18778              :             break;
   18779              :           default:
   18780              :             break;
   18781              :           }
   18782              :     }
   18783              : 
   18784         4793 :   new_clauses = nreverse (new_clauses);
   18785         4793 :   if (ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_OMP_DECLARE_MAPPER)
   18786              :     {
   18787         4686 :       if (ort == C_ORT_OMP_TARGET)
   18788          825 :         new_clauses = c_omp_instantiate_mappers (new_clauses);
   18789         4686 :       new_clauses = finish_omp_clauses (new_clauses, ort);
   18790         4686 :       if (linear_no_step)
   18791            2 :         for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
   18792            2 :           if (nc == linear_no_step)
   18793              :             {
   18794            2 :               OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
   18795            2 :               break;
   18796              :             }
   18797              :     }
   18798         4793 :   return new_clauses;
   18799              : }
   18800              : 
   18801              : /* Like tsubst_copy, but specifically for OpenMP context selectors.  */
   18802              : static tree
   18803           58 : tsubst_omp_context_selector (tree ctx, tree args, tsubst_flags_t complain,
   18804              :                              tree in_decl)
   18805              : {
   18806           58 :   tree new_ctx = NULL_TREE;
   18807          128 :   for (tree set = ctx; set; set = TREE_CHAIN (set))
   18808              :     {
   18809           70 :       tree selectors = NULL_TREE;
   18810          140 :       for (tree sel = OMP_TSS_TRAIT_SELECTORS (set); sel;
   18811           70 :            sel = TREE_CHAIN (sel))
   18812              :         {
   18813           70 :           enum omp_ts_code code = OMP_TS_CODE (sel);
   18814           70 :           tree properties = NULL_TREE;
   18815           70 :           tree score = OMP_TS_SCORE (sel);
   18816            6 :           tree t;
   18817              : 
   18818            6 :           if (score)
   18819              :             {
   18820            6 :               score = tsubst_expr (score, args, complain, in_decl);
   18821            6 :               score = fold_non_dependent_expr (score);
   18822            6 :               if (!value_dependent_expression_p (score)
   18823            6 :                   && !type_dependent_expression_p (score))
   18824              :                 {
   18825           12 :                   if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
   18826           12 :                       || TREE_CODE (score) != INTEGER_CST)
   18827              :                     {
   18828            0 :                       error_at (cp_expr_loc_or_input_loc (score),
   18829              :                                 "%<score%> argument must "
   18830              :                                 "be constant integer expression");
   18831            0 :                       score = NULL_TREE;
   18832              :                     }
   18833            6 :                   else if (tree_int_cst_sgn (score) < 0)
   18834              :                     {
   18835            0 :                       error_at (cp_expr_loc_or_input_loc (score),
   18836              :                                 "%<score%> argument must be non-negative");
   18837            0 :                       score = NULL_TREE;
   18838              :                     }
   18839              :                 }
   18840              :             }
   18841              : 
   18842           70 :           enum omp_tp_type property_kind
   18843           70 :             = omp_ts_map[OMP_TS_CODE (sel)].tp_type;
   18844           70 :           switch (property_kind)
   18845              :               {
   18846           70 :               case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
   18847           70 :               case OMP_TRAIT_PROPERTY_BOOL_EXPR:
   18848          140 :                 t = tsubst_expr (OMP_TP_VALUE (OMP_TS_PROPERTIES (sel)),
   18849              :                                  args, complain, in_decl);
   18850           70 :                 t = fold_non_dependent_expr (t);
   18851           70 :                 if (!value_dependent_expression_p (t)
   18852           70 :                     && !type_dependent_expression_p (t))
   18853              :                   {
   18854           70 :                     if (property_kind == OMP_TRAIT_PROPERTY_BOOL_EXPR)
   18855           58 :                       t = maybe_convert_cond (t);
   18856              :                     else
   18857              :                       {
   18858           12 :                         t = convert_from_reference (t);
   18859           12 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
   18860              :                           {
   18861            0 :                             error_at (cp_expr_loc_or_input_loc (t),
   18862              :                                       "property must be integer expression");
   18863            0 :                             t = error_mark_node;
   18864              :                           }
   18865              :                       }
   18866              :                   }
   18867           70 :                 if (t != error_mark_node
   18868           70 :                     && !processing_template_decl
   18869           70 :                     && TREE_CODE (t) != CLEANUP_POINT_EXPR)
   18870           55 :                   t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   18871           70 :                 properties = make_trait_property (NULL_TREE, t, NULL_TREE);
   18872           70 :                 break;
   18873            0 :               case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
   18874            0 :                 if (OMP_TS_CODE (sel) == OMP_TRAIT_CONSTRUCT_SIMD)
   18875            0 :                   properties = tsubst_omp_clauses (OMP_TS_PROPERTIES (sel),
   18876              :                                                    C_ORT_OMP_DECLARE_SIMD,
   18877              :                                                    args, complain, in_decl);
   18878              :                 break;
   18879            0 :               default:
   18880              :                 /* Nothing to do here, just copy.  */
   18881            0 :                 for (tree prop = OMP_TS_PROPERTIES (sel);
   18882            0 :                      prop; prop = TREE_CHAIN (prop))
   18883            0 :                   properties = make_trait_property (OMP_TP_NAME (prop),
   18884            0 :                                                     OMP_TP_VALUE (prop),
   18885              :                                                     properties);
   18886              :               }
   18887           70 :           selectors = make_trait_selector (code, score, properties, selectors);
   18888              :         }
   18889           70 :       new_ctx = make_trait_set_selector (OMP_TSS_CODE (set),
   18890              :                                          nreverse (selectors),
   18891              :                                          new_ctx);
   18892              :     }
   18893           58 :   return nreverse (new_ctx);
   18894              : }
   18895              : 
   18896              : 
   18897              : /* Like tsubst_expr, but unshare TREE_LIST nodes.  */
   18898              : 
   18899              : static tree
   18900         9558 : tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
   18901              :                           tree in_decl)
   18902              : {
   18903              : #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
   18904              : 
   18905         9558 :   tree purpose, value, chain;
   18906              : 
   18907         9558 :   if (t == NULL)
   18908              :     return t;
   18909              : 
   18910         7379 :   if (TREE_CODE (t) != TREE_LIST)
   18911         3805 :     return tsubst_expr (t, args, complain, in_decl);
   18912              : 
   18913         3574 :   if (t == void_list_node)
   18914              :     return t;
   18915              : 
   18916         3574 :   purpose = TREE_PURPOSE (t);
   18917         3574 :   if (purpose)
   18918         1872 :     purpose = RECUR (purpose);
   18919         3574 :   value = TREE_VALUE (t);
   18920         3574 :   if (value)
   18921              :     {
   18922         3574 :       if (TREE_CODE (value) != LABEL_DECL)
   18923         3571 :         value = RECUR (value);
   18924              :       else
   18925              :         {
   18926            3 :           value = lookup_label (DECL_NAME (value));
   18927            3 :           gcc_assert (TREE_CODE (value) == LABEL_DECL);
   18928            3 :           TREE_USED (value) = 1;
   18929              :         }
   18930              :     }
   18931         3574 :   chain = TREE_CHAIN (t);
   18932         3574 :   if (chain && chain != void_type_node)
   18933         1039 :     chain = RECUR (chain);
   18934         3574 :   return tree_cons (purpose, value, chain);
   18935              : #undef RECUR
   18936              : }
   18937              : 
   18938              : /* Used to temporarily communicate the list of #pragma omp parallel
   18939              :    clauses to #pragma omp for instantiation if they are combined
   18940              :    together.  */
   18941              : 
   18942              : static tree *omp_parallel_combined_clauses;
   18943              : 
   18944              : static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
   18945              :                                  cp_decomp *);
   18946              : 
   18947              : /* Substitute one OMP_FOR iterator.  */
   18948              : 
   18949              : static bool
   18950         1253 : tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
   18951              :                          tree initv, tree condv, tree incrv, tree *clauses,
   18952              :                          tree args, tsubst_flags_t complain, tree in_decl)
   18953              : {
   18954              : #define RECUR(NODE)                             \
   18955              :   tsubst_stmt ((NODE), args, complain, in_decl)
   18956         1253 :   tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
   18957         1253 :   bool ret = false;
   18958              : 
   18959         1253 :   init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
   18960         1253 :   gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
   18961              : 
   18962         1253 :   decl = TREE_OPERAND (init, 0);
   18963         1253 :   init = TREE_OPERAND (init, 1);
   18964         1253 :   tree decl_expr = NULL_TREE;
   18965         1253 :   bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
   18966         1253 :   if (range_for)
   18967              :     {
   18968          164 :       bool decomp = false;
   18969          164 :       if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
   18970              :         {
   18971           41 :           tree v = DECL_VALUE_EXPR (decl);
   18972           41 :           if ((TREE_CODE (v) == ARRAY_REF
   18973           41 :                && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
   18974           41 :               || (TREE_CODE (v) == TREE_VEC
   18975            0 :                   && DECL_DECOMPOSITION_P (TREE_VEC_ELT (v, 0))))
   18976              :             {
   18977           41 :               v = (TREE_CODE (v) == ARRAY_REF
   18978           41 :                    ? TREE_OPERAND (v, 0) : TREE_VEC_ELT (v, 0));
   18979           41 :               cp_decomp decomp_d = { NULL_TREE, 0 };
   18980           41 :               tree d = tsubst_decl (v, args, complain);
   18981           41 :               maybe_push_decl (d);
   18982           41 :               d = tsubst_decomp_names (d, v, args, complain,
   18983              :                                        in_decl, &decomp_d);
   18984           41 :               decomp = true;
   18985           41 :               if (d == error_mark_node)
   18986            0 :                 decl = error_mark_node;
   18987              :               else
   18988          155 :                 for (unsigned int i = 0; i < decomp_d.count; i++)
   18989              :                   {
   18990          114 :                     if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl))
   18991              :                       {
   18992          114 :                         tree v = build_nt (ARRAY_REF, d,
   18993          114 :                                            size_int (decomp_d.count - i - 1),
   18994              :                                            NULL_TREE, NULL_TREE);
   18995          114 :                         SET_DECL_VALUE_EXPR (decomp_d.decl, v);
   18996          114 :                         DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1;
   18997              :                       }
   18998          114 :                     fit_decomposition_lang_decl (decomp_d.decl, d);
   18999          114 :                     decomp_d.decl = DECL_CHAIN (decomp_d.decl);
   19000              :                   }
   19001              :             }
   19002              :         }
   19003          164 :       decl = tsubst_decl (decl, args, complain);
   19004          164 :       if (!decomp)
   19005          123 :         maybe_push_decl (decl);
   19006              :     }
   19007         1089 :   else if (init && TREE_CODE (init) == DECL_EXPR)
   19008              :     {
   19009              :       /* We need to jump through some hoops to handle declarations in the
   19010              :          init-statement, since we might need to handle auto deduction,
   19011              :          but we need to keep control of initialization.  */
   19012          223 :       decl_expr = init;
   19013          223 :       init = DECL_INITIAL (DECL_EXPR_DECL (init));
   19014          223 :       decl = tsubst_decl (decl, args, complain);
   19015              :     }
   19016              :   else
   19017              :     {
   19018          866 :       if (TREE_CODE (decl) == SCOPE_REF)
   19019              :         {
   19020            8 :           decl = RECUR (decl);
   19021            8 :           if (TREE_CODE (decl) == COMPONENT_REF)
   19022              :             {
   19023              :               tree v = decl;
   19024           24 :               while (v)
   19025           24 :                 switch (TREE_CODE (v))
   19026              :                   {
   19027           20 :                   case COMPONENT_REF:
   19028           20 :                   case MEM_REF:
   19029           20 :                   case INDIRECT_REF:
   19030           20 :                   CASE_CONVERT:
   19031           20 :                   case POINTER_PLUS_EXPR:
   19032           20 :                     v = TREE_OPERAND (v, 0);
   19033           20 :                     continue;
   19034            4 :                   case PARM_DECL:
   19035            4 :                     if (DECL_CONTEXT (v) == current_function_decl
   19036            4 :                         && DECL_ARTIFICIAL (v)
   19037            8 :                         && DECL_NAME (v) == this_identifier)
   19038              :                       {
   19039            4 :                         decl = TREE_OPERAND (decl, 1);
   19040            4 :                         decl = omp_privatize_field (decl, false);
   19041              :                       }
   19042              :                     /* FALLTHRU */
   19043              :                   default:
   19044              :                     v = NULL_TREE;
   19045              :                     break;
   19046              :                   }
   19047              :             }
   19048              :         }
   19049              :       else
   19050          858 :         decl = RECUR (decl);
   19051              :     }
   19052         1253 :   if (init && TREE_CODE (init) == TREE_VEC)
   19053              :     {
   19054           12 :       init = copy_node (init);
   19055           12 :       TREE_VEC_ELT (init, 0)
   19056           12 :         = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
   19057           12 :       TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
   19058           12 :       TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
   19059              :     }
   19060              :   else
   19061         1241 :     init = RECUR (init);
   19062              : 
   19063         1253 :   if (orig_declv && OMP_FOR_ORIG_DECLS (t))
   19064              :     {
   19065          618 :       tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   19066          618 :       if (TREE_CODE (o) == TREE_LIST)
   19067           92 :         TREE_VEC_ELT (orig_declv, i)
   19068          184 :           = tree_cons (RECUR (TREE_PURPOSE (o)),
   19069           92 :                        RECUR (TREE_VALUE (o)),
   19070              :                        NULL_TREE);
   19071              :       else
   19072          526 :         TREE_VEC_ELT (orig_declv, i) = RECUR (o);
   19073              :     }
   19074              : 
   19075         1253 :   if (range_for)
   19076              :     {
   19077          164 :       tree this_pre_body = NULL_TREE;
   19078          164 :       tree orig_init = NULL_TREE;
   19079          164 :       tree orig_decl = NULL_TREE;
   19080          164 :       tree init_sl = NULL_TREE;
   19081          164 :       cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
   19082              :                                 orig_init, cond, incr, true);
   19083          164 :       if (orig_decl)
   19084              :         {
   19085          159 :           if (orig_declv == NULL_TREE)
   19086          143 :             orig_declv = copy_node (declv);
   19087          159 :           TREE_VEC_ELT (orig_declv, i) = orig_decl;
   19088          159 :           ret = true;
   19089              :         }
   19090            5 :       else if (orig_declv)
   19091            0 :         TREE_VEC_ELT (orig_declv, i) = decl;
   19092              :     }
   19093              : 
   19094         1253 :   tree auto_node = type_uses_auto (TREE_TYPE (decl));
   19095         1253 :   if (!range_for && auto_node && init)
   19096            2 :     TREE_TYPE (decl)
   19097            4 :       = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
   19098              : 
   19099         1253 :   gcc_assert (!type_dependent_expression_p (decl));
   19100              : 
   19101         1253 :   if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
   19102              :     {
   19103         1058 :       if (decl_expr)
   19104              :         {
   19105              :           /* Declare the variable, but don't let that initialize it.  */
   19106          172 :           tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
   19107          172 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
   19108          172 :           RECUR (decl_expr);
   19109          172 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
   19110              :         }
   19111              : 
   19112         1058 :       if (!range_for)
   19113              :         {
   19114          894 :           cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   19115          894 :           if (COMPARISON_CLASS_P (cond)
   19116          894 :               && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
   19117              :             {
   19118           12 :               tree lhs = RECUR (TREE_OPERAND (cond, 0));
   19119           12 :               tree rhs = copy_node (TREE_OPERAND (cond, 1));
   19120           12 :               TREE_VEC_ELT (rhs, 0)
   19121           12 :                 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
   19122           12 :               TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
   19123           12 :               TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
   19124           12 :               cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
   19125              :                              lhs, rhs);
   19126              :             }
   19127              :           else
   19128          882 :             cond = RECUR (cond);
   19129          894 :           incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   19130          894 :           if (TREE_CODE (incr) == MODIFY_EXPR)
   19131              :             {
   19132          211 :               tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19133          211 :               tree rhs = RECUR (TREE_OPERAND (incr, 1));
   19134          211 :               incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
   19135              :                                           NOP_EXPR, rhs, NULL_TREE, complain);
   19136              :             }
   19137              :           else
   19138          683 :             incr = RECUR (incr);
   19139          894 :           if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   19140           18 :             TREE_VEC_ELT (orig_declv, i) = decl;
   19141              :         }
   19142         1058 :       TREE_VEC_ELT (declv, i) = decl;
   19143         1058 :       TREE_VEC_ELT (initv, i) = init;
   19144         1058 :       TREE_VEC_ELT (condv, i) = cond;
   19145         1058 :       TREE_VEC_ELT (incrv, i) = incr;
   19146         1058 :       return ret;
   19147              :     }
   19148              : 
   19149          195 :   if (decl_expr)
   19150              :     {
   19151              :       /* Declare and initialize the variable.  */
   19152           51 :       RECUR (decl_expr);
   19153           51 :       init = NULL_TREE;
   19154              :     }
   19155          144 :   else if (init)
   19156              :     {
   19157           61 :       tree *pc;
   19158           61 :       int j;
   19159          109 :       for (j = ((omp_parallel_combined_clauses == NULL
   19160          170 :                 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
   19161              :         {
   19162          210 :           for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
   19163              :             {
   19164           80 :               if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
   19165           80 :                   && OMP_CLAUSE_DECL (*pc) == decl)
   19166              :                 break;
   19167           76 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
   19168           76 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19169              :                 {
   19170           31 :                   if (j)
   19171              :                     break;
   19172              :                   /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
   19173            1 :                   tree c = *pc;
   19174            1 :                   *pc = OMP_CLAUSE_CHAIN (c);
   19175            1 :                   OMP_CLAUSE_CHAIN (c) = *clauses;
   19176            1 :                   *clauses = c;
   19177              :                 }
   19178           45 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
   19179           45 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19180              :                 {
   19181            0 :                   error ("iteration variable %qD should not be firstprivate",
   19182              :                          decl);
   19183            0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   19184              :                 }
   19185           45 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
   19186           45 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19187              :                 {
   19188            0 :                   error ("iteration variable %qD should not be reduction",
   19189              :                          decl);
   19190            0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   19191              :                 }
   19192              :               else
   19193           45 :                 pc = &OMP_CLAUSE_CHAIN (*pc);
   19194              :             }
   19195           82 :           if (*pc)
   19196              :             break;
   19197              :         }
   19198           61 :       if (*pc == NULL_TREE)
   19199              :         {
   19200           27 :           tree c = build_omp_clause (input_location,
   19201           27 :                                      TREE_CODE (t) == OMP_LOOP
   19202              :                                      ? OMP_CLAUSE_LASTPRIVATE
   19203              :                                      : OMP_CLAUSE_PRIVATE);
   19204           27 :           OMP_CLAUSE_DECL (c) = decl;
   19205           27 :           c = finish_omp_clauses (c, C_ORT_OMP);
   19206           27 :           if (c)
   19207              :             {
   19208           27 :               OMP_CLAUSE_CHAIN (c) = *clauses;
   19209           27 :               *clauses = c;
   19210              :             }
   19211              :         }
   19212              :     }
   19213          195 :   cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   19214          195 :   if (COMPARISON_CLASS_P (cond))
   19215              :     {
   19216          195 :       tree op0 = RECUR (TREE_OPERAND (cond, 0));
   19217          195 :       tree op1 = RECUR (TREE_OPERAND (cond, 1));
   19218          195 :       cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
   19219              :     }
   19220              :   else
   19221            0 :     cond = RECUR (cond);
   19222          195 :   incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   19223          195 :   switch (TREE_CODE (incr))
   19224              :     {
   19225           85 :     case PREINCREMENT_EXPR:
   19226           85 :     case PREDECREMENT_EXPR:
   19227           85 :     case POSTINCREMENT_EXPR:
   19228           85 :     case POSTDECREMENT_EXPR:
   19229           85 :       incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
   19230           85 :                      RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
   19231           85 :       break;
   19232          110 :     case MODIFY_EXPR:
   19233          110 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   19234          110 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   19235              :         {
   19236          110 :           tree rhs = TREE_OPERAND (incr, 1);
   19237          110 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19238          110 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   19239          110 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   19240          110 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19241          110 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   19242              :                                  rhs0, rhs1));
   19243              :         }
   19244              :       else
   19245            0 :         incr = RECUR (incr);
   19246              :       break;
   19247            0 :     case MODOP_EXPR:
   19248            0 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   19249            0 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   19250              :         {
   19251            0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19252            0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19253            0 :                          build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
   19254            0 :                                  TREE_TYPE (decl), lhs,
   19255            0 :                                  RECUR (TREE_OPERAND (incr, 2))));
   19256              :         }
   19257            0 :       else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
   19258            0 :                && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
   19259            0 :                    || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
   19260              :         {
   19261            0 :           tree rhs = TREE_OPERAND (incr, 2);
   19262            0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19263            0 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   19264            0 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   19265            0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19266            0 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   19267              :                                  rhs0, rhs1));
   19268              :         }
   19269              :       else
   19270            0 :         incr = RECUR (incr);
   19271              :       break;
   19272            0 :     default:
   19273            0 :       incr = RECUR (incr);
   19274            0 :       break;
   19275              :     }
   19276              : 
   19277          195 :   if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   19278            5 :     TREE_VEC_ELT (orig_declv, i) = decl;
   19279          195 :   TREE_VEC_ELT (declv, i) = decl;
   19280          195 :   TREE_VEC_ELT (initv, i) = init;
   19281          195 :   TREE_VEC_ELT (condv, i) = cond;
   19282          195 :   TREE_VEC_ELT (incrv, i) = incr;
   19283          195 :   return false;
   19284              : #undef RECUR
   19285              : }
   19286              : 
   19287              : /* Helper function of tsubst_expr, find OMP_TEAMS inside
   19288              :    of OMP_TARGET's body.  */
   19289              : 
   19290              : static tree
   19291          164 : tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
   19292              : {
   19293          164 :   *walk_subtrees = 0;
   19294          164 :   switch (TREE_CODE (*tp))
   19295              :     {
   19296              :     case OMP_TEAMS:
   19297              :       return *tp;
   19298           82 :     case BIND_EXPR:
   19299           82 :     case STATEMENT_LIST:
   19300           82 :       *walk_subtrees = 1;
   19301           82 :       break;
   19302              :     default:
   19303              :       break;
   19304              :     }
   19305              :   return NULL_TREE;
   19306              : }
   19307              : 
   19308              : /* Helper function for tsubst_expr.  For decomposition declaration
   19309              :    artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
   19310              :    also the corresponding decls representing the identifiers
   19311              :    of the decomposition declaration.  Return DECL if successful
   19312              :    or error_mark_node otherwise, set *FIRST to the first decl
   19313              :    in the list chained through DECL_CHAIN and *CNT to the number
   19314              :    of such decls.  */
   19315              : 
   19316              : static tree
   19317        69177 : tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
   19318              :                      tsubst_flags_t complain, tree in_decl, cp_decomp *decomp)
   19319              : {
   19320        69177 :   tree decl2, decl3, prev = decl;
   19321        69177 :   decomp->count = 0;
   19322        69177 :   gcc_assert (DECL_NAME (decl) == NULL_TREE);
   19323        69177 :   for (decl2 = DECL_CHAIN (pattern_decl);
   19324              :        decl2
   19325       137118 :        && DECL_DECOMPOSITION_P (decl2)
   19326       341409 :        && DECL_NAME (decl2);
   19327       135829 :        decl2 = DECL_CHAIN (decl2))
   19328              :     {
   19329       135838 :       if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0)
   19330              :         {
   19331            9 :           gcc_assert (errorcount);
   19332              :           return error_mark_node;
   19333              :         }
   19334       135829 :       decomp->count++;
   19335       135829 :       gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
   19336       135829 :       gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
   19337       135829 :       tree v = DECL_VALUE_EXPR (decl2);
   19338       135829 :       DECL_HAS_VALUE_EXPR_P (decl2) = 0;
   19339       135829 :       SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
   19340       135829 :       decl3 = tsubst (decl2, args, complain, in_decl);
   19341       135829 :       SET_DECL_VALUE_EXPR (decl2, v);
   19342       135829 :       DECL_HAS_VALUE_EXPR_P (decl2) = 1;
   19343       135829 :       if (VAR_P (decl3))
   19344       135829 :         DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
   19345              :       else
   19346              :         {
   19347            0 :           gcc_assert (errorcount);
   19348            0 :           decl = error_mark_node;
   19349            0 :           continue;
   19350              :         }
   19351       135829 :       maybe_push_decl (decl3);
   19352       135829 :       if (error_operand_p (decl3))
   19353            0 :         decl = error_mark_node;
   19354       135829 :       else if (decl != error_mark_node
   19355       135829 :                && DECL_CHAIN (decl3) != prev
   19356       135829 :                && decl != prev)
   19357              :         {
   19358            0 :           gcc_assert (errorcount);
   19359              :           decl = error_mark_node;
   19360              :         }
   19361              :       else
   19362              :         prev = decl3;
   19363              :     }
   19364        69168 :   decomp->decl = prev;
   19365        69168 :   return decl;
   19366              : }
   19367              : 
   19368              : /* Return the proper local_specialization for init-capture pack DECL.  */
   19369              : 
   19370              : static tree
   19371          162 : lookup_init_capture_pack (tree decl)
   19372              : {
   19373              :   /* We handle normal pack captures by forwarding to the specialization of the
   19374              :      captured parameter.  We can't do that for pack init-captures; we need them
   19375              :      to have their own local_specialization.  We created the individual
   19376              :      VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
   19377              :      when we process the DECL_EXPR for the pack init-capture in the template.
   19378              :      So, how do we find them?  We don't know the capture proxy pack when
   19379              :      building the individual resulting proxies, and we don't know the
   19380              :      individual proxies when instantiating the pack.  What we have in common is
   19381              :      the FIELD_DECL.
   19382              : 
   19383              :      So...when we instantiate the FIELD_DECL, we stick the result in
   19384              :      local_specializations.  Then at the DECL_EXPR we look up that result, see
   19385              :      how many elements it has, synthesize the names, and look them up.  */
   19386              : 
   19387          162 :   tree cname = DECL_NAME (decl);
   19388          162 :   tree val = DECL_VALUE_EXPR (decl);
   19389          162 :   tree field = TREE_OPERAND (val, 1);
   19390          162 :   gcc_assert (TREE_CODE (field) == FIELD_DECL);
   19391          162 :   tree fpack = retrieve_local_specialization (field);
   19392          162 :   if (fpack == error_mark_node)
   19393              :     return error_mark_node;
   19394              : 
   19395          162 :   int len = 1;
   19396          162 :   tree vec = NULL_TREE;
   19397          162 :   tree r = NULL_TREE;
   19398          162 :   if (TREE_CODE (fpack) == TREE_VEC)
   19399              :     {
   19400          159 :       len = TREE_VEC_LENGTH (fpack);
   19401          159 :       vec = make_tree_vec (len);
   19402          159 :       r = make_node (NONTYPE_ARGUMENT_PACK);
   19403          159 :       ARGUMENT_PACK_ARGS (r) = vec;
   19404              :     }
   19405          465 :   for (int i = 0; i < len; ++i)
   19406              :     {
   19407          303 :       tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
   19408          303 :       tree elt = lookup_name (ename);
   19409          303 :       if (vec)
   19410          300 :         TREE_VEC_ELT (vec, i) = elt;
   19411              :       else
   19412              :         r = elt;
   19413              :     }
   19414              :   return r;
   19415              : }
   19416              : 
   19417              : /* T is an operand of a template tree being substituted.  Return whether
   19418              :    T is dependent such that we should suppress some warnings that would
   19419              :    make sense if the substituted expression were written directly, like
   19420              :      template <int I> bool f() { return I == 2; }
   19421              :    We don't want to warn when instantiating f that comparing two constants
   19422              :    always has the same value.
   19423              : 
   19424              :    This is a more limited concept of dependence than instantiation-dependent;
   19425              :    here we don't care whether substitution could fail.  */
   19426              : 
   19427              : static bool
   19428    142383986 : dependent_operand_p (tree t)
   19429              : {
   19430    143696173 :   while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
   19431      1312187 :     t = TREE_OPERAND (t, 0);
   19432              : 
   19433    142383986 :   ++processing_template_decl;
   19434    142383986 :   bool r = (potential_constant_expression (t)
   19435    142383986 :             ? value_dependent_expression_p (t)
   19436    142383986 :             : type_dependent_expression_p (t));
   19437    142383986 :   --processing_template_decl;
   19438    142383986 :   return r;
   19439              : }
   19440              : 
   19441              : /* A superset of tsubst_expr that also handles statement trees.  */
   19442              : 
   19443              : static tree
   19444    521525897 : tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   19445              : {
   19446              : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   19447              : #define RECUR(NODE)                             \
   19448              :   tsubst_stmt ((NODE), args, complain, in_decl)
   19449              : 
   19450    521525897 :   tree stmt, tmp;
   19451    521525897 :   tree r;
   19452    521525897 :   location_t loc;
   19453              : 
   19454    521525897 :   if (t == NULL_TREE || t == error_mark_node)
   19455              :     return t;
   19456              : 
   19457    515785473 :   loc = input_location;
   19458    515785473 :   if (location_t eloc = cp_expr_location (t))
   19459    446373025 :     input_location = eloc;
   19460    515785473 :   if (STATEMENT_CODE_P (TREE_CODE (t)))
   19461     77468008 :     current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
   19462              : 
   19463    515785473 :   switch (TREE_CODE (t))
   19464              :     {
   19465     64400912 :     case STATEMENT_LIST:
   19466     64400912 :       {
   19467    335991001 :         for (tree stmt : tsi_range (t))
   19468    271590092 :           RECUR (stmt);
   19469              :         break;
   19470              :       }
   19471              : 
   19472      3511926 :     case CTOR_INITIALIZER:
   19473      3511926 :       finish_mem_initializers (tsubst_initializer_list
   19474      3511926 :                                (TREE_OPERAND (t, 0), args));
   19475      3511926 :       break;
   19476              : 
   19477     27967707 :     case RETURN_EXPR:
   19478     27967707 :       finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
   19479     27967704 :       break;
   19480              : 
   19481          176 :     case CO_RETURN_EXPR:
   19482          176 :       finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
   19483          176 :       break;
   19484              : 
   19485     38535456 :     case EXPR_STMT:
   19486     38535456 :       tmp = RECUR (EXPR_STMT_EXPR (t));
   19487     38535453 :       if (EXPR_STMT_STMT_EXPR_RESULT (t))
   19488          160 :         finish_stmt_expr_expr (tmp, cur_stmt_expr);
   19489              :       else
   19490     38535293 :         finish_expr_stmt (tmp);
   19491              :       break;
   19492              : 
   19493        88840 :     case USING_STMT:
   19494        88840 :       finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
   19495        88840 :       break;
   19496              : 
   19497            0 :     case PRECONDITION_STMT:
   19498            0 :     case POSTCONDITION_STMT:
   19499            0 :       gcc_unreachable ();
   19500              : 
   19501            1 :     case ASSERTION_STMT:
   19502            1 :       {
   19503            1 :         r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
   19504            1 :         if (r != error_mark_node)
   19505            1 :           add_stmt (r);
   19506            1 :         RETURN (r);
   19507              :       }
   19508              :       break;
   19509              : 
   19510     33689053 :     case DECL_EXPR:
   19511     33689053 :       {
   19512     33689053 :         tree decl, pattern_decl;
   19513     33689053 :         tree init;
   19514              : 
   19515     33689053 :         pattern_decl = decl = DECL_EXPR_DECL (t);
   19516     33689053 :         if (TREE_CODE (decl) == LABEL_DECL)
   19517           39 :           finish_label_decl (DECL_NAME (decl));
   19518     33689014 :         else if (TREE_CODE (decl) == USING_DECL)
   19519              :           {
   19520       488912 :             tree scope = USING_DECL_SCOPE (decl);
   19521       488912 :             if (DECL_DEPENDENT_P (decl))
   19522              :               {
   19523            9 :                 scope = tsubst (scope, args, complain, in_decl);
   19524            9 :                 if (!MAYBE_CLASS_TYPE_P (scope)
   19525            6 :                     && TREE_CODE (scope) != ENUMERAL_TYPE)
   19526              :                   {
   19527            3 :                     if (complain & tf_error)
   19528            3 :                       error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
   19529              :                                 "class, namespace, or enumeration", scope);
   19530            3 :                     return error_mark_node;
   19531              :                   }
   19532            6 :                 finish_nonmember_using_decl (scope, DECL_NAME (decl));
   19533              :               }
   19534              :             else
   19535              :               {
   19536              :                 /* This is a non-dependent using-decl, and we'll have
   19537              :                    used the names it found during template parsing.  We do
   19538              :                    not want to do the lookup again, because we might not
   19539              :                    find the things we found then.  */
   19540       488903 :                 gcc_checking_assert (scope == tsubst (scope, args,
   19541              :                                                       complain, in_decl));
   19542              :                 /* We still need to push the bindings so that we can look up
   19543              :                    this name later.  */
   19544       977806 :                 push_using_decl_bindings (DECL_NAME (decl),
   19545       488903 :                                           USING_DECL_DECLS (decl));
   19546              :               }
   19547              :           }
   19548     33200102 :         else if (is_capture_proxy (decl)
   19549     33200102 :                  && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
   19550              :           {
   19551              :             /* We're in tsubst_lambda_expr, we've already inserted a new
   19552              :                capture proxy, so look it up and register it.  */
   19553      1197297 :             tree inst;
   19554      1197297 :             if (!DECL_PACK_P (decl))
   19555              :               {
   19556      1196936 :                 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
   19557              :                                     LOOK_want::HIDDEN_LAMBDA);
   19558      1196936 :                 gcc_assert (inst != decl && is_capture_proxy (inst));
   19559              :               }
   19560          361 :             else if (is_normal_capture_proxy (decl))
   19561              :               {
   19562          199 :                 inst = (retrieve_local_specialization
   19563          199 :                         (DECL_CAPTURED_VARIABLE (decl)));
   19564          199 :                 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
   19565              :                             || DECL_PACK_P (inst));
   19566              :               }
   19567              :             else
   19568          162 :               inst = lookup_init_capture_pack (decl);
   19569              : 
   19570      1197297 :             register_local_specialization (inst, decl);
   19571      1197297 :             break;
   19572              :           }
   19573     32002805 :         else if (DECL_PRETTY_FUNCTION_P (decl))
   19574        27724 :           decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
   19575        27724 :                                   DECL_NAME (decl),
   19576              :                                   true/*DECL_PRETTY_FUNCTION_P (decl)*/);
   19577      4348428 :         else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   19578     34352180 :                  && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   19579              :           /* Don't copy the old closure; we'll create a new one in
   19580              :              tsubst_lambda_expr.  */
   19581              :           break;
   19582              :         else
   19583              :           {
   19584     31442824 :             init = DECL_INITIAL (decl);
   19585     31442824 :             decl = tsubst (decl, args, complain, in_decl);
   19586     31442821 :             if (decl != error_mark_node)
   19587              :               {
   19588              :                 /* By marking the declaration as instantiated, we avoid
   19589              :                    trying to instantiate it.  Since instantiate_decl can't
   19590              :                    handle local variables, and since we've already done
   19591              :                    all that needs to be done, that's the right thing to
   19592              :                    do.  */
   19593     31442809 :                 if (VAR_P (decl))
   19594     27626266 :                   DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   19595     27626266 :                 if (VAR_P (decl) && !DECL_NAME (decl)
   19596     31512017 :                     && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   19597              :                   /* Anonymous aggregates are a special case.  */
   19598           33 :                   finish_anon_union (decl);
   19599     31442776 :                 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
   19600              :                   {
   19601       822545 :                     DECL_CONTEXT (decl) = current_function_decl;
   19602       822545 :                     insert_capture_proxy (decl);
   19603              :                   }
   19604     30620231 :                 else if (DECL_IMPLICIT_TYPEDEF_P (t))
   19605              :                   /* We already did a pushtag.  */;
   19606      3816543 :                 else if (VAR_OR_FUNCTION_DECL_P (decl)
   19607     30620612 :                          && DECL_LOCAL_DECL_P (decl))
   19608              :                   {
   19609          465 :                     if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   19610          133 :                       DECL_CONTEXT (decl) = NULL_TREE;
   19611          465 :                     decl = pushdecl (decl);
   19612          465 :                     if (TREE_CODE (decl) == FUNCTION_DECL
   19613          372 :                         && DECL_OMP_DECLARE_REDUCTION_P (decl)
   19614          589 :                         && cp_check_omp_declare_reduction (decl))
   19615           88 :                       instantiate_body (pattern_decl, args, decl, true);
   19616              :                   }
   19617              :                 else
   19618              :                   {
   19619     30619766 :                     bool const_init = false;
   19620     30619766 :                     cp_decomp decomp_d, *decomp = NULL;
   19621     30619766 :                     tree asmspec_tree = NULL_TREE;
   19622     30619766 :                     maybe_push_decl (decl);
   19623              : 
   19624     30619766 :                     if (VAR_P (decl)
   19625     26803604 :                         && DECL_LANG_SPECIFIC (decl)
   19626     31646099 :                         && DECL_OMP_PRIVATIZED_MEMBER (decl))
   19627              :                       break;
   19628              : 
   19629     26803421 :                     if (DECL_DECOMPOSITION_P (decl)
   19630     30688353 :                         && TREE_TYPE (pattern_decl) != error_mark_node)
   19631              :                       {
   19632        68770 :                         decomp = &decomp_d;
   19633        68770 :                         if (tsubst_decomp_names (decl, pattern_decl, args,
   19634              :                                                  complain, in_decl, decomp)
   19635        68770 :                             == error_mark_node)
   19636     30550822 :                           decomp = NULL;
   19637              :                       }
   19638              : 
   19639     30619583 :                     init = tsubst_init (init, decl, args, complain, in_decl);
   19640              : 
   19641     30619583 :                     if (VAR_P (decl))
   19642     26803421 :                       const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
   19643              :                                     (pattern_decl));
   19644              : 
   19645              :                     /* In a non-template function, VLA type declarations are
   19646              :                        handled in grokdeclarator; for templates, handle them
   19647              :                        now.  */
   19648     30619583 :                     predeclare_vla (decl);
   19649              : 
   19650     57423004 :                     if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
   19651              :                       {
   19652            6 :                         tree id = DECL_ASSEMBLER_NAME (pattern_decl);
   19653            6 :                         const char *asmspec = IDENTIFIER_POINTER (id);
   19654            6 :                         gcc_assert (asmspec[0] == '*');
   19655            6 :                         asmspec_tree
   19656            6 :                           = build_string (IDENTIFIER_LENGTH (id) - 1,
   19657              :                                           asmspec + 1);
   19658            6 :                         TREE_TYPE (asmspec_tree) = char_array_type_node;
   19659              :                       }
   19660              : 
   19661     30619583 :                     cp_finish_decl (decl, init, const_init, asmspec_tree, 0,
   19662              :                                     decomp);
   19663              :                   }
   19664              :               }
   19665              :           }
   19666              : 
   19667              :         break;
   19668              :       }
   19669              : 
   19670      1431696 :     case FOR_STMT:
   19671      1431696 :       stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
   19672      1431696 :       RECUR (FOR_INIT_STMT (t));
   19673      1431696 :       finish_init_stmt (stmt);
   19674      1431696 :       tmp = RECUR (FOR_COND (t));
   19675      1431696 :       finish_for_cond (tmp, stmt, false, 0, false);
   19676      1431696 :       tmp = RECUR (FOR_EXPR (t));
   19677      1431696 :       finish_for_expr (tmp, stmt);
   19678      1431696 :       {
   19679      1431696 :         bool prev = note_iteration_stmt_body_start ();
   19680      1431696 :         RECUR (FOR_BODY (t));
   19681      1431696 :         note_iteration_stmt_body_end (prev);
   19682              :       }
   19683      1431696 :       finish_for_stmt (stmt);
   19684      1431696 :       break;
   19685              : 
   19686        97294 :     case RANGE_FOR_STMT:
   19687        97294 :       {
   19688              :         /* Construct another range_for, if this is not a final
   19689              :            substitution (for inside a generic lambda of a
   19690              :            template).  Otherwise convert to a regular for.  */
   19691        97294 :         tree decl, expr;
   19692       194588 :         stmt = (processing_template_decl
   19693        97294 :                 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
   19694        97040 :                 : begin_for_stmt (NULL_TREE, NULL_TREE));
   19695        97294 :         RECUR (RANGE_FOR_INIT_STMT (t));
   19696        97294 :         decl = RANGE_FOR_DECL (t);
   19697        97294 :         decl = tsubst (decl, args, complain, in_decl);
   19698        97294 :         maybe_push_decl (decl);
   19699        97294 :         expr = RECUR (RANGE_FOR_EXPR (t));
   19700              : 
   19701        97294 :         cp_decomp decomp_d, *decomp = NULL;
   19702        97294 :         if (DECL_DECOMPOSITION_P (decl))
   19703              :           {
   19704          366 :             decomp = &decomp_d;
   19705          366 :             decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
   19706              :                                         complain, in_decl, decomp);
   19707              :           }
   19708              : 
   19709        97294 :         tree unroll = RECUR (RANGE_FOR_UNROLL (t));
   19710        97294 :         if (unroll)
   19711           33 :           unroll
   19712           33 :             = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)),
   19713              :                                       unroll);
   19714        97294 :         if (processing_template_decl)
   19715              :           {
   19716          254 :             RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
   19717          254 :             RANGE_FOR_UNROLL (stmt) = unroll;
   19718          254 :             RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t);
   19719          254 :             finish_range_for_decl (stmt, decl, expr);
   19720          254 :             if (decomp && decl != error_mark_node)
   19721            3 :               cp_finish_decomp (decl, decomp);
   19722              :           }
   19723              :         else
   19724       194080 :           stmt = cp_convert_range_for (stmt, decl, expr, decomp,
   19725        97040 :                                        RANGE_FOR_IVDEP (t), unroll,
   19726        97040 :                                        RANGE_FOR_NOVECTOR (t));
   19727              : 
   19728        97294 :         bool prev = note_iteration_stmt_body_start ();
   19729        97294 :         RECUR (RANGE_FOR_BODY (t));
   19730        97294 :         note_iteration_stmt_body_end (prev);
   19731        97294 :         finish_for_stmt (stmt);
   19732              :       }
   19733        97294 :       break;
   19734              : 
   19735       685208 :     case WHILE_STMT:
   19736       685208 :       stmt = begin_while_stmt ();
   19737       685208 :       tmp = RECUR (WHILE_COND (t));
   19738       685208 :       finish_while_stmt_cond (tmp, stmt, false, 0, false);
   19739       685208 :       {
   19740       685208 :         bool prev = note_iteration_stmt_body_start ();
   19741       685208 :         RECUR (WHILE_BODY (t));
   19742       685208 :         note_iteration_stmt_body_end (prev);
   19743              :       }
   19744       685208 :       finish_while_stmt (stmt);
   19745       685208 :       break;
   19746              : 
   19747      1062740 :     case DO_STMT:
   19748      1062740 :       stmt = begin_do_stmt ();
   19749      1062740 :       {
   19750      1062740 :         bool prev = note_iteration_stmt_body_start ();
   19751      1062740 :         RECUR (DO_BODY (t));
   19752      1062740 :         note_iteration_stmt_body_end (prev);
   19753              :       }
   19754      1062740 :       finish_do_body (stmt);
   19755      1062740 :       tmp = RECUR (DO_COND (t));
   19756      1062740 :       finish_do_stmt (tmp, stmt, false, 0, false);
   19757      1062740 :       break;
   19758              : 
   19759          556 :     case TEMPLATE_FOR_STMT:
   19760          556 :       {
   19761          556 :         tree init;
   19762          556 :         stmt = build_stmt (EXPR_LOCATION (t), TEMPLATE_FOR_STMT, NULL_TREE,
   19763              :                            NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
   19764          556 :         TEMPLATE_FOR_SCOPE (stmt) = begin_template_for_scope (&init);
   19765          556 :         TEMPLATE_FOR_INIT_STMT (stmt) = init;
   19766          556 :         RECUR (TEMPLATE_FOR_INIT_STMT (t));
   19767          556 :         TEMPLATE_FOR_EXPR (stmt) = RECUR (TEMPLATE_FOR_EXPR (t));
   19768          556 :         if (processing_template_decl)
   19769              :           {
   19770           10 :             tree orig_decl = TEMPLATE_FOR_DECL (t);
   19771           10 :             if (TREE_CODE (orig_decl) == TREE_VEC)
   19772            0 :               orig_decl = TREE_VEC_ELT (orig_decl, 0);
   19773           10 :             tree decl = tsubst (orig_decl, args, complain, in_decl);
   19774           10 :             maybe_push_decl (decl);
   19775              : 
   19776           10 :             cp_decomp decomp_d, *decomp = NULL;
   19777           10 :             if (DECL_DECOMPOSITION_P (decl))
   19778              :               {
   19779            0 :                 decomp = &decomp_d;
   19780            0 :                 decl = tsubst_decomp_names (decl, orig_decl, args,
   19781              :                                             complain, in_decl, decomp);
   19782            0 :                 if (decl != error_mark_node)
   19783              :                   {
   19784            0 :                     tree v = make_tree_vec (decomp->count + 1);
   19785            0 :                     TREE_VEC_ELT (v, 0) = decl;
   19786            0 :                     decl = decomp->decl;
   19787            0 :                     for (unsigned i = 0; i < decomp->count; ++i)
   19788              :                       {
   19789            0 :                         TREE_VEC_ELT (v, decomp->count - i) = decl;
   19790            0 :                         decl = DECL_CHAIN (decl);
   19791              :                       }
   19792              :                     decl = v;
   19793              :                   }
   19794              :               }
   19795           10 :             TEMPLATE_FOR_DECL (stmt) = decl;
   19796           10 :             TEMPLATE_FOR_INIT_STMT (stmt) = pop_stmt_list (init);
   19797           10 :             add_stmt (stmt);
   19798           10 :             TEMPLATE_FOR_BODY (stmt) = do_pushlevel (sk_block);
   19799           10 :             bool prev = note_iteration_stmt_body_start ();
   19800           10 :             RECUR (TEMPLATE_FOR_BODY (t));
   19801           10 :             note_iteration_stmt_body_end (prev);
   19802           10 :             TEMPLATE_FOR_BODY (stmt)
   19803           20 :               = do_poplevel (TEMPLATE_FOR_BODY (stmt));
   19804              :           }
   19805              :         else
   19806              :           {
   19807          546 :             TEMPLATE_FOR_DECL (stmt) = TEMPLATE_FOR_DECL (t);
   19808          546 :             TEMPLATE_FOR_BODY (stmt) = TEMPLATE_FOR_BODY (t);
   19809          546 :             finish_expansion_stmt (stmt, args, complain, in_decl);
   19810              :           }
   19811          556 :         add_stmt (do_poplevel (TEMPLATE_FOR_SCOPE (stmt)));
   19812              :       }
   19813          556 :       break;
   19814              : 
   19815     28547316 :     case IF_STMT:
   19816     28547316 :       stmt = begin_if_stmt ();
   19817     28547316 :       IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
   19818     28547316 :       IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
   19819     28547316 :       if (IF_STMT_CONSTEXPR_P (t))
   19820      9913912 :         args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
   19821     28547316 :       {
   19822     28547316 :         tree cond = IF_COND (t);
   19823     28547316 :         bool was_dep = dependent_operand_p (cond);
   19824     28547316 :         cond = RECUR (cond);
   19825     28547316 :         warning_sentinel s1(warn_address, was_dep);
   19826     28547316 :         tmp = finish_if_stmt_cond (cond, stmt);
   19827     28547316 :       }
   19828     28547316 :       if (IF_STMT_CONSTEXPR_P (t)
   19829     28547316 :           && instantiation_dependent_expression_p (tmp))
   19830              :         {
   19831              :           /* We're partially instantiating a generic lambda, but the condition
   19832              :              of the constexpr if is still dependent.  Don't substitute into the
   19833              :              branches now, just remember the template arguments.  */
   19834        29674 :           do_poplevel (IF_SCOPE (stmt));
   19835        29674 :           IF_SCOPE (stmt) = NULL_TREE;
   19836        29674 :           IF_COND (stmt) = IF_COND (t);
   19837        29674 :           THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
   19838        29674 :           ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
   19839        29674 :           IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
   19840        29674 :           add_stmt (stmt);
   19841        29674 :           break;
   19842              :         }
   19843     28517642 :       if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
   19844              :         /* Don't instantiate the THEN_CLAUSE. */;
   19845     23067091 :       else if (IF_STMT_CONSTEVAL_P (t))
   19846              :         {
   19847         3965 :           bool save_in_consteval_if_p = in_consteval_if_p;
   19848         3965 :           in_consteval_if_p = true;
   19849         3965 :           RECUR (THEN_CLAUSE (t));
   19850         3965 :           in_consteval_if_p = save_in_consteval_if_p;
   19851              :         }
   19852              :       else
   19853              :         {
   19854     23063126 :           tree folded = fold_non_dependent_expr (tmp, complain);
   19855     23063126 :           bool inhibit = integer_zerop (folded);
   19856     23063126 :           if (inhibit)
   19857        51745 :             ++c_inhibit_evaluation_warnings;
   19858     23063126 :           RECUR (THEN_CLAUSE (t));
   19859     23063126 :           if (inhibit)
   19860        51745 :             --c_inhibit_evaluation_warnings;
   19861              :         }
   19862     28517642 :       finish_then_clause (stmt);
   19863              : 
   19864     28517642 :       if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
   19865              :         /* Don't instantiate the ELSE_CLAUSE. */;
   19866     24083971 :       else if (ELSE_CLAUSE (t))
   19867              :         {
   19868     10422677 :           tree folded = fold_non_dependent_expr (tmp, complain);
   19869     10422677 :           bool inhibit = integer_nonzerop (folded);
   19870     10422677 :           begin_else_clause (stmt);
   19871     10422677 :           if (inhibit)
   19872        37430 :             ++c_inhibit_evaluation_warnings;
   19873     10422677 :           RECUR (ELSE_CLAUSE (t));
   19874     10422677 :           if (inhibit)
   19875        37430 :             --c_inhibit_evaluation_warnings;
   19876     10422677 :           finish_else_clause (stmt);
   19877              :         }
   19878              : 
   19879     28517642 :       finish_if_stmt (stmt);
   19880     28517642 :       break;
   19881              : 
   19882     66287868 :     case BIND_EXPR:
   19883     66287868 :       if (BIND_EXPR_BODY_BLOCK (t))
   19884      5047722 :         stmt = begin_function_body ();
   19885              :       else
   19886    122377340 :         stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
   19887              :                                     ? BCS_TRY_BLOCK : 0);
   19888              : 
   19889     66287868 :       RECUR (BIND_EXPR_BODY (t));
   19890              : 
   19891     66287859 :       if (BIND_EXPR_BODY_BLOCK (t))
   19892      5047722 :         finish_function_body (stmt);
   19893              :       else
   19894     61240137 :         finish_compound_stmt (stmt);
   19895              :       break;
   19896              : 
   19897      2033765 :     case BREAK_STMT:
   19898      2033765 :       finish_break_stmt ();
   19899      2033765 :       break;
   19900              : 
   19901         1426 :     case CONTINUE_STMT:
   19902         1426 :       finish_continue_stmt ();
   19903         1426 :       break;
   19904              : 
   19905       608892 :     case SWITCH_STMT:
   19906       608892 :       stmt = begin_switch_stmt ();
   19907       608892 :       tmp = RECUR (SWITCH_STMT_COND (t));
   19908       608892 :       finish_switch_cond (tmp, stmt);
   19909       608892 :       RECUR (SWITCH_STMT_BODY (t));
   19910       608892 :       finish_switch_stmt (stmt);
   19911       608892 :       break;
   19912              : 
   19913      4567939 :     case CASE_LABEL_EXPR:
   19914      4567939 :       {
   19915      4567939 :         tree decl = CASE_LABEL (t);
   19916      4567939 :         tree low = RECUR (CASE_LOW (t));
   19917      4567939 :         tree high = RECUR (CASE_HIGH (t));
   19918      4567939 :         tree l = finish_case_label (EXPR_LOCATION (t), low, high);
   19919      4567939 :         if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
   19920              :           {
   19921      4567930 :             tree label = CASE_LABEL (l);
   19922      4567930 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   19923      4567930 :             if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   19924            6 :               cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   19925              :           }
   19926              :       }
   19927              :       break;
   19928              : 
   19929          275 :     case LABEL_EXPR:
   19930          275 :       {
   19931          275 :         tree decl = LABEL_EXPR_LABEL (t);
   19932          275 :         tree label = finish_label_stmt (DECL_NAME (decl));
   19933          275 :         if (TREE_CODE (label) == LABEL_DECL)
   19934              :           {
   19935          275 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   19936          275 :             copy_warning (label, decl);
   19937              :           }
   19938          275 :         if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   19939           18 :           cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   19940              :       }
   19941          275 :       break;
   19942              : 
   19943          475 :     case GOTO_EXPR:
   19944          475 :       tmp = GOTO_DESTINATION (t);
   19945          475 :       if (TREE_CODE (tmp) != LABEL_DECL)
   19946              :         /* Computed goto's must be tsubst'd into.  On the other hand,
   19947              :            non-computed gotos must not be; the identifier in question
   19948              :            will have no binding.  */
   19949           23 :         tmp = RECUR (tmp);
   19950              :       else
   19951          452 :         tmp = DECL_NAME (tmp);
   19952          475 :       finish_goto_stmt (tmp);
   19953          475 :       break;
   19954              : 
   19955          769 :     case ASM_EXPR:
   19956          769 :       {
   19957          769 :         tree string = RECUR (ASM_STRING (t));
   19958          769 :         tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
   19959              :                                                  complain, in_decl);
   19960          769 :         tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
   19961              :                                                 complain, in_decl);
   19962          769 :         tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
   19963              :                                                   complain, in_decl);
   19964          769 :         tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
   19965              :                                                 complain, in_decl);
   19966         1538 :         tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
   19967              :                                outputs, inputs, clobbers, labels,
   19968          769 :                                ASM_INLINE_P (t), false);
   19969          769 :         tree asm_expr = tmp;
   19970          769 :         if (asm_expr != error_mark_node)
   19971              :           {
   19972          660 :             if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
   19973          660 :               asm_expr = TREE_OPERAND (asm_expr, 0);
   19974          660 :             ASM_BASIC_P (asm_expr) = ASM_BASIC_P (t);
   19975              :           }
   19976              :       }
   19977              :       break;
   19978              : 
   19979       102952 :     case TRY_BLOCK:
   19980       102952 :       if (CLEANUP_P (t))
   19981              :         {
   19982            0 :           stmt = begin_try_block ();
   19983            0 :           RECUR (TRY_STMTS (t));
   19984            0 :           finish_cleanup_try_block (stmt);
   19985            0 :           finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
   19986              :         }
   19987              :       else
   19988              :         {
   19989       102952 :           tree compound_stmt = NULL_TREE;
   19990              : 
   19991       102952 :           if (FN_TRY_BLOCK_P (t))
   19992           42 :             stmt = begin_function_try_block (&compound_stmt);
   19993              :           else
   19994       102910 :             stmt = begin_try_block ();
   19995              : 
   19996       102952 :           RECUR (TRY_STMTS (t));
   19997              : 
   19998       102952 :           if (FN_TRY_BLOCK_P (t))
   19999           42 :             finish_function_try_block (stmt);
   20000              :           else
   20001       102910 :             finish_try_block (stmt);
   20002              : 
   20003       102952 :           RECUR (TRY_HANDLERS (t));
   20004       102952 :           if (FN_TRY_BLOCK_P (t))
   20005           42 :             finish_function_handler_sequence (stmt, compound_stmt);
   20006              :           else
   20007       102910 :             finish_handler_sequence (stmt);
   20008              :         }
   20009              :       break;
   20010              : 
   20011       103955 :     case HANDLER:
   20012       103955 :       {
   20013       103955 :         tree decl = HANDLER_PARMS (t);
   20014              : 
   20015       103955 :         if (decl)
   20016              :           {
   20017         2599 :             decl = tsubst (decl, args, complain, in_decl);
   20018              :             /* Prevent instantiate_decl from trying to instantiate
   20019              :                this variable.  We've already done all that needs to be
   20020              :                done.  */
   20021         2599 :             if (decl != error_mark_node)
   20022         2596 :               DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   20023              :           }
   20024       103955 :         stmt = begin_handler ();
   20025       103955 :         finish_handler_parms (decl, stmt);
   20026       103955 :         RECUR (HANDLER_BODY (t));
   20027       103955 :         finish_handler (stmt);
   20028              :       }
   20029       103955 :       break;
   20030              : 
   20031       656428 :     case TAG_DEFN:
   20032       656428 :       tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
   20033       656428 :       if (dependent_type_p (tmp))
   20034              :         /* This is a partial instantiation, try again when full.  */
   20035           21 :         add_stmt (build_min (TAG_DEFN, tmp));
   20036       656407 :       else if (CLASS_TYPE_P (tmp))
   20037              :         {
   20038              :           /* Local classes are not independent templates; they are
   20039              :              instantiated along with their containing function.  And this
   20040              :              way we don't have to deal with pushing out of one local class
   20041              :              to instantiate a member of another local class.  */
   20042              :           /* Closures are handled by the LAMBDA_EXPR.  */
   20043      1312238 :           gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
   20044       656158 :           complete_type (tmp);
   20045       656158 :           tree save_ccp = current_class_ptr;
   20046       656158 :           tree save_ccr = current_class_ref;
   20047      4622153 :           for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
   20048      3965995 :             if ((VAR_P (fld)
   20049      3965992 :                  || (TREE_CODE (fld) == FUNCTION_DECL
   20050      2653754 :                      && !DECL_ARTIFICIAL (fld)))
   20051      6619733 :                 && DECL_TEMPLATE_INSTANTIATION (fld))
   20052      2653741 :               instantiate_decl (fld, /*defer_ok=*/false,
   20053              :                                 /*expl_inst_class=*/false);
   20054      1312254 :             else if (TREE_CODE (fld) == FIELD_DECL)
   20055       655870 :               maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
   20056       656158 :           current_class_ptr = save_ccp;
   20057       656158 :           current_class_ref = save_ccr;
   20058              :         }
   20059              :       break;
   20060              : 
   20061      6292153 :     case STATIC_ASSERT:
   20062      6292153 :       {
   20063      6292153 :         tree condition, message;
   20064              : 
   20065      6292153 :         ++c_inhibit_evaluation_warnings;
   20066      6292153 :         condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
   20067              :                                  complain, in_decl);
   20068      6292153 :         message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args,
   20069              :                                complain, in_decl);
   20070      6292153 :         if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST
   20071      6292153 :             && TREE_CODE (message) == STRING_CST)
   20072            0 :           message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t),
   20073            0 :                                 PAREN_EXPR, TREE_TYPE (message), message);
   20074      6292153 :         --c_inhibit_evaluation_warnings;
   20075              : 
   20076      6292153 :         finish_static_assert (condition, message,
   20077      6292153 :                               STATIC_ASSERT_SOURCE_LOCATION (t),
   20078              :                               /*member_p=*/false, /*show_expr_p=*/true,
   20079      6292153 :                               CONSTEVAL_BLOCK_P (t));
   20080              :       }
   20081      6292153 :       break;
   20082              : 
   20083          245 :     case OACC_KERNELS:
   20084          245 :     case OACC_PARALLEL:
   20085          245 :     case OACC_SERIAL:
   20086          245 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC_TARGET, args,
   20087              :                                 complain, in_decl);
   20088          245 :       stmt = begin_omp_parallel ();
   20089          245 :       RECUR (OMP_BODY (t));
   20090          245 :       finish_omp_construct (TREE_CODE (t), stmt, tmp);
   20091          245 :       break;
   20092              : 
   20093          811 :     case OMP_PARALLEL:
   20094          811 :       r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
   20095          811 :       tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
   20096              :                                 complain, in_decl);
   20097          811 :       if (OMP_PARALLEL_COMBINED (t))
   20098          423 :         omp_parallel_combined_clauses = &tmp;
   20099          811 :       stmt = begin_omp_parallel ();
   20100          811 :       RECUR (OMP_PARALLEL_BODY (t));
   20101          811 :       gcc_assert (omp_parallel_combined_clauses == NULL);
   20102         1622 :       OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
   20103          811 :         = OMP_PARALLEL_COMBINED (t);
   20104          811 :       pop_omp_privatization_clauses (r);
   20105          811 :       break;
   20106              : 
   20107          355 :     case OMP_TASK:
   20108          355 :       if (OMP_TASK_BODY (t) == NULL_TREE)
   20109              :         {
   20110            0 :           tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   20111              :                                     complain, in_decl);
   20112            0 :           t = copy_node (t);
   20113            0 :           OMP_TASK_CLAUSES (t) = tmp;
   20114            0 :           add_stmt (t);
   20115            0 :           break;
   20116              :         }
   20117          355 :       r = push_omp_privatization_clauses (false);
   20118          355 :       tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   20119              :                                 complain, in_decl);
   20120          355 :       stmt = begin_omp_task ();
   20121          355 :       RECUR (OMP_TASK_BODY (t));
   20122          355 :       finish_omp_task (tmp, stmt);
   20123          355 :       pop_omp_privatization_clauses (r);
   20124          355 :       break;
   20125              : 
   20126         1219 :     case OMP_FOR:
   20127         1219 :     case OMP_LOOP:
   20128         1219 :     case OMP_SIMD:
   20129         1219 :     case OMP_DISTRIBUTE:
   20130         1219 :     case OMP_TASKLOOP:
   20131         1219 :     case OMP_TILE:
   20132         1219 :     case OMP_UNROLL:
   20133         1219 :     case OACC_LOOP:
   20134         1219 :       {
   20135         1219 :         tree clauses, body, pre_body;
   20136         1219 :         tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
   20137         1219 :         tree orig_declv = NULL_TREE;
   20138         1219 :         tree incrv = NULL_TREE;
   20139         1219 :         enum c_omp_region_type ort = C_ORT_OMP;
   20140         1219 :         bool any_range_for = false;
   20141         1219 :         int i;
   20142              : 
   20143         1219 :         if (TREE_CODE (t) == OACC_LOOP)
   20144           51 :           ort = C_ORT_ACC;
   20145              : 
   20146         1219 :         r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
   20147         1219 :         clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
   20148              :                                       in_decl);
   20149         1219 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20150              :           {
   20151         1059 :             declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20152         1059 :             if (OMP_FOR_ORIG_DECLS (t))
   20153          541 :               orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20154         1059 :             initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20155         1059 :             condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20156         1059 :             incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20157              :           }
   20158              : 
   20159         1219 :         keep_next_level (true);
   20160         1219 :         stmt = begin_omp_structured_block ();
   20161              : 
   20162         1219 :         pre_body = push_stmt_list ();
   20163         1219 :         RECUR (OMP_FOR_PRE_BODY (t));
   20164         1219 :         pre_body = pop_stmt_list (pre_body);
   20165              : 
   20166         1219 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20167         2351 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20168              :             {
   20169         1292 :               if (TREE_VEC_ELT (OMP_FOR_INIT (t), i))
   20170         1253 :                 any_range_for
   20171         1253 :                   |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
   20172              :                                               condv, incrv, &clauses, args,
   20173              :                                               complain, in_decl);
   20174              :               else
   20175              :                 {
   20176           39 :                   TREE_VEC_ELT (declv, i) = global_namespace;
   20177           39 :                   TREE_VEC_ELT (initv, i) = NULL_TREE;
   20178           39 :                   TREE_VEC_ELT (condv, i) = NULL_TREE;
   20179           39 :                   TREE_VEC_ELT (incrv, i) = NULL_TREE;
   20180           39 :                   if (orig_declv)
   20181           30 :                     TREE_VEC_ELT (orig_declv, i) = NULL_TREE;
   20182              :                 }
   20183              :             }
   20184         1219 :         omp_parallel_combined_clauses = NULL;
   20185              : 
   20186         1219 :         if (any_range_for)
   20187              :           {
   20188          143 :             gcc_assert (orig_declv);
   20189          143 :             body = begin_omp_structured_block ();
   20190          468 :             for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20191          182 :               if (TREE_VEC_ELT (declv, i) != global_namespace
   20192          182 :                   && TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
   20193          159 :                   && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
   20194          341 :                   && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
   20195          159 :                 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
   20196          159 :                                          TREE_VEC_ELT (declv, i));
   20197              :           }
   20198              :         else
   20199         1076 :           body = push_stmt_list ();
   20200         1219 :         RECUR (OMP_FOR_BODY (t));
   20201         1219 :         if (any_range_for)
   20202          143 :           body = finish_omp_structured_block (body);
   20203              :         else
   20204         1076 :           body = pop_stmt_list (body);
   20205              : 
   20206         1219 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20207         1059 :           t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
   20208              :                               orig_declv, initv, condv, incrv, body, pre_body,
   20209              :                               NULL, clauses);
   20210              :         else
   20211              :           {
   20212          160 :             t = make_node (TREE_CODE (t));
   20213          160 :             TREE_TYPE (t) = void_type_node;
   20214          160 :             OMP_FOR_BODY (t) = body;
   20215          160 :             OMP_FOR_PRE_BODY (t) = pre_body;
   20216          160 :             OMP_FOR_CLAUSES (t) = clauses;
   20217          160 :             SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
   20218          160 :             add_stmt (t);
   20219              :           }
   20220              : 
   20221         1219 :         add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
   20222              :                                         t));
   20223         1219 :         pop_omp_privatization_clauses (r);
   20224              : 
   20225         1219 :         if (any_range_for && !processing_template_decl && t)
   20226          267 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20227          144 :             if (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i)
   20228          144 :                 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t),
   20229              :                                             i)) == TREE_LIST)
   20230              :               {
   20231          144 :                 tree v = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   20232          144 :                 if (TREE_CHAIN (v) == NULL_TREE
   20233          144 :                     || TREE_CODE (TREE_CHAIN (v)) != TREE_VEC)
   20234            5 :                   continue;
   20235          139 :                 v = TREE_VEC_ELT (TREE_CHAIN (v), 0);
   20236          139 :                 gcc_assert (VAR_P (v) && DECL_NAME (v) == NULL_TREE);
   20237          139 :                 DECL_NAME (v) = for_range_identifier;
   20238              :               }
   20239              :       }
   20240         1219 :       break;
   20241              : 
   20242           19 :     case OMP_SECTIONS:
   20243           19 :     case OMP_MASKED:
   20244           19 :       omp_parallel_combined_clauses = NULL;
   20245              :       /* FALLTHRU */
   20246          472 :     case OMP_SINGLE:
   20247          472 :     case OMP_SCOPE:
   20248          472 :     case OMP_TEAMS:
   20249          472 :     case OMP_CRITICAL:
   20250          472 :     case OMP_TASKGROUP:
   20251          472 :     case OMP_SCAN:
   20252          472 :       r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
   20253          472 :                                           && OMP_TEAMS_COMBINED (t));
   20254          472 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
   20255              :                                 in_decl);
   20256          472 :       if (TREE_CODE (t) == OMP_TEAMS)
   20257              :         {
   20258          242 :           keep_next_level (true);
   20259          242 :           stmt = begin_omp_structured_block ();
   20260          242 :           RECUR (OMP_BODY (t));
   20261          242 :           stmt = finish_omp_structured_block (stmt);
   20262              :         }
   20263              :       else
   20264              :         {
   20265          230 :           stmt = push_stmt_list ();
   20266          230 :           RECUR (OMP_BODY (t));
   20267          230 :           stmt = pop_stmt_list (stmt);
   20268              :         }
   20269              : 
   20270          472 :       if (TREE_CODE (t) == OMP_CRITICAL
   20271           18 :           && tmp != NULL_TREE
   20272          484 :           && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
   20273              :         {
   20274            6 :           error_at (OMP_CLAUSE_LOCATION (tmp),
   20275              :                     "%<#pragma omp critical%> with %<hint%> clause requires "
   20276              :                     "a name, except when %<omp_sync_hint_none%> is used");
   20277            6 :           RETURN (error_mark_node);
   20278              :         }
   20279          466 :       t = copy_node (t);
   20280          466 :       OMP_BODY (t) = stmt;
   20281          466 :       OMP_CLAUSES (t) = tmp;
   20282          466 :       add_stmt (t);
   20283          466 :       pop_omp_privatization_clauses (r);
   20284          466 :       break;
   20285              : 
   20286          114 :     case OMP_DEPOBJ:
   20287          114 :       r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
   20288          114 :       if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
   20289              :         {
   20290          105 :           enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
   20291          105 :           if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
   20292              :             {
   20293           58 :               tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
   20294              :                                         args, complain, in_decl);
   20295           58 :               if (tmp == NULL_TREE)
   20296            6 :                 tmp = error_mark_node;
   20297              :             }
   20298              :           else
   20299              :             {
   20300           94 :               kind = (enum omp_clause_depend_kind)
   20301           47 :                      tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
   20302           47 :               tmp = NULL_TREE;
   20303              :             }
   20304          105 :           finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
   20305              :         }
   20306              :       else
   20307            9 :         finish_omp_depobj (EXPR_LOCATION (t), r,
   20308              :                            OMP_CLAUSE_DEPEND_INVALID,
   20309            9 :                            OMP_DEPOBJ_CLAUSES (t));
   20310              :       break;
   20311              : 
   20312          851 :     case OACC_DATA:
   20313          851 :     case OMP_TARGET_DATA:
   20314          851 :     case OMP_TARGET:
   20315         1687 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
   20316              :                                 TREE_CODE (t) == OACC_DATA
   20317              :                                 ? C_ORT_ACC
   20318              :                                 : TREE_CODE (t) == OMP_TARGET
   20319          836 :                                 ? C_ORT_OMP_TARGET : C_ORT_OMP,
   20320              :                                 args, complain, in_decl);
   20321          851 :       keep_next_level (true);
   20322          851 :       stmt = begin_omp_structured_block ();
   20323              : 
   20324          851 :       RECUR (OMP_BODY (t));
   20325          851 :       stmt = finish_omp_structured_block (stmt);
   20326              : 
   20327          851 :       t = copy_node (t);
   20328          851 :       OMP_BODY (t) = stmt;
   20329          851 :       OMP_CLAUSES (t) = tmp;
   20330              : 
   20331          851 :       if (TREE_CODE (t) == OMP_TARGET)
   20332          825 :         finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
   20333              :                                    &OMP_CLAUSES (t));
   20334              : 
   20335          851 :       if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
   20336              :         {
   20337           82 :           tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
   20338           82 :           if (teams)
   20339              :             /* For combined target teams, ensure the num_teams and
   20340              :                thread_limit clause expressions are evaluated on the host,
   20341              :                before entering the target construct.  */
   20342          160 :             for (tree c = OMP_TEAMS_CLAUSES (teams);
   20343          160 :                  c; c = OMP_CLAUSE_CHAIN (c))
   20344           78 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
   20345           78 :                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
   20346          141 :                 for (int i = 0;
   20347          219 :                      i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
   20348          141 :                   if (OMP_CLAUSE_OPERAND (c, i)
   20349          141 :                       && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
   20350              :                     {
   20351           42 :                       tree expr = OMP_CLAUSE_OPERAND (c, i);
   20352           42 :                       expr = force_target_expr (TREE_TYPE (expr), expr,
   20353              :                                                 tf_none);
   20354           42 :                       if (expr == error_mark_node)
   20355            0 :                         continue;
   20356           42 :                       tmp = TARGET_EXPR_SLOT (expr);
   20357           42 :                       add_stmt (expr);
   20358           42 :                       OMP_CLAUSE_OPERAND (c, i) = expr;
   20359           42 :                       tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   20360              :                                                   OMP_CLAUSE_FIRSTPRIVATE);
   20361           42 :                       OMP_CLAUSE_DECL (tc) = tmp;
   20362           42 :                       OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
   20363           42 :                       OMP_TARGET_CLAUSES (t) = tc;
   20364              :                     }
   20365              :         }
   20366          851 :       add_stmt (t);
   20367          851 :       break;
   20368              : 
   20369            1 :     case OACC_DECLARE:
   20370            1 :       t = copy_node (t);
   20371            1 :       tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
   20372              :                                 complain, in_decl);
   20373            1 :       OACC_DECLARE_CLAUSES (t) = tmp;
   20374            1 :       add_stmt (t);
   20375            1 :       break;
   20376              : 
   20377          168 :     case OMP_TARGET_UPDATE:
   20378          168 :     case OMP_TARGET_ENTER_DATA:
   20379          168 :     case OMP_TARGET_EXIT_DATA:
   20380          168 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
   20381              :                                 complain, in_decl);
   20382          168 :       t = copy_node (t);
   20383          168 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   20384          168 :       add_stmt (t);
   20385          168 :       break;
   20386              : 
   20387          395 :     case OACC_CACHE:
   20388          395 :     case OACC_ENTER_DATA:
   20389          395 :     case OACC_EXIT_DATA:
   20390          395 :     case OACC_UPDATE:
   20391          395 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
   20392              :                                 complain, in_decl);
   20393          395 :       t = copy_node (t);
   20394          395 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   20395          395 :       add_stmt (t);
   20396          395 :       break;
   20397              : 
   20398           33 :     case OMP_ORDERED:
   20399           33 :       tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
   20400              :                                 complain, in_decl);
   20401           33 :       if (OMP_BODY (t))
   20402              :         {
   20403            3 :           stmt = push_stmt_list ();
   20404            3 :           RECUR (OMP_BODY (t));
   20405            3 :           stmt = pop_stmt_list (stmt);
   20406              :         }
   20407              :       else
   20408           30 :         stmt = NULL_TREE;
   20409              : 
   20410           33 :       t = copy_node (t);
   20411           33 :       OMP_BODY (t) = stmt;
   20412           33 :       OMP_ORDERED_CLAUSES (t) = tmp;
   20413           33 :       add_stmt (t);
   20414           33 :       break;
   20415              : 
   20416           73 :     case OMP_MASTER:
   20417           73 :     case OMP_STRUCTURED_BLOCK:
   20418           73 :       omp_parallel_combined_clauses = NULL;
   20419              :       /* FALLTHRU */
   20420           92 :     case OMP_SECTION:
   20421           92 :       stmt = push_stmt_list ();
   20422           92 :       RECUR (OMP_BODY (t));
   20423           92 :       stmt = pop_stmt_list (stmt);
   20424              : 
   20425           92 :       t = copy_node (t);
   20426           92 :       OMP_BODY (t) = stmt;
   20427           92 :       add_stmt (t);
   20428           92 :       break;
   20429              : 
   20430           12 :     case OMP_DISPATCH:
   20431           12 :       tmp = tsubst_omp_clauses (OMP_DISPATCH_CLAUSES (t), C_ORT_OMP, args,
   20432              :                                 complain, in_decl);
   20433           12 :       stmt = RECUR (OMP_DISPATCH_BODY (t));
   20434           12 :       t = copy_node (t);
   20435           12 :       OMP_DISPATCH_BODY (t) = stmt;
   20436           12 :       OMP_DISPATCH_CLAUSES (t) = tmp;
   20437           12 :       add_stmt (t);
   20438           12 :       break;
   20439              : 
   20440          609 :     case OMP_ATOMIC:
   20441          609 :       gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
   20442          609 :       tmp = NULL_TREE;
   20443          609 :       if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
   20444           30 :         tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
   20445              :                                   complain, in_decl);
   20446          609 :       if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
   20447              :         {
   20448          184 :           tree op1 = TREE_OPERAND (t, 1);
   20449          184 :           tree rhs1 = NULL_TREE;
   20450          184 :           tree r = NULL_TREE;
   20451          184 :           tree lhs, rhs;
   20452          184 :           if (TREE_CODE (op1) == COMPOUND_EXPR)
   20453              :             {
   20454           99 :               rhs1 = RECUR (TREE_OPERAND (op1, 0));
   20455           99 :               op1 = TREE_OPERAND (op1, 1);
   20456              :             }
   20457          184 :           if (TREE_CODE (op1) == COND_EXPR)
   20458              :             {
   20459           46 :               gcc_assert (rhs1 == NULL_TREE);
   20460           46 :               tree c = TREE_OPERAND (op1, 0);
   20461           46 :               if (TREE_CODE (c) == MODIFY_EXPR)
   20462              :                 {
   20463           14 :                   r = RECUR (TREE_OPERAND (c, 0));
   20464           14 :                   c = TREE_OPERAND (c, 1);
   20465              :                 }
   20466           46 :               gcc_assert (TREE_CODE (c) == EQ_EXPR);
   20467           46 :               rhs = RECUR (TREE_OPERAND (c, 1));
   20468           46 :               lhs = RECUR (TREE_OPERAND (op1, 2));
   20469           46 :               rhs1 = RECUR (TREE_OPERAND (op1, 1));
   20470              :             }
   20471              :           else
   20472              :             {
   20473          138 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   20474          138 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   20475              :             }
   20476          184 :           finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
   20477              :                              lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
   20478          184 :                              tmp, OMP_ATOMIC_MEMORY_ORDER (t),
   20479          184 :                              OMP_ATOMIC_WEAK (t));
   20480              :         }
   20481              :       else
   20482              :         {
   20483          425 :           tree op1 = TREE_OPERAND (t, 1);
   20484          425 :           tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
   20485          425 :           tree rhs1 = NULL_TREE, r = NULL_TREE;
   20486          425 :           enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
   20487          425 :           enum tree_code opcode = NOP_EXPR;
   20488          425 :           if (code == OMP_ATOMIC_READ)
   20489              :             {
   20490          166 :               v = RECUR (TREE_OPERAND (op1, 0));
   20491          166 :               lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   20492              :             }
   20493          259 :           else if (code == OMP_ATOMIC_CAPTURE_OLD
   20494          259 :                    || code == OMP_ATOMIC_CAPTURE_NEW)
   20495              :             {
   20496          233 :               tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
   20497          233 :               v = RECUR (TREE_OPERAND (op1, 0));
   20498          233 :               lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   20499          233 :               if (TREE_CODE (op11) == COMPOUND_EXPR)
   20500              :                 {
   20501           91 :                   rhs1 = RECUR (TREE_OPERAND (op11, 0));
   20502           91 :                   op11 = TREE_OPERAND (op11, 1);
   20503              :                 }
   20504          233 :               if (TREE_CODE (op11) == COND_EXPR)
   20505              :                 {
   20506           78 :                   gcc_assert (rhs1 == NULL_TREE);
   20507           78 :                   tree c = TREE_OPERAND (op11, 0);
   20508           78 :                   if (TREE_CODE (c) == MODIFY_EXPR)
   20509              :                     {
   20510           36 :                       r = RECUR (TREE_OPERAND (c, 0));
   20511           36 :                       c = TREE_OPERAND (c, 1);
   20512              :                     }
   20513           78 :                   gcc_assert (TREE_CODE (c) == EQ_EXPR);
   20514           78 :                   rhs = RECUR (TREE_OPERAND (c, 1));
   20515           78 :                   lhs = RECUR (TREE_OPERAND (op11, 2));
   20516           78 :                   rhs1 = RECUR (TREE_OPERAND (op11, 1));
   20517              :                 }
   20518              :               else
   20519              :                 {
   20520          155 :                   lhs = RECUR (TREE_OPERAND (op11, 0));
   20521          155 :                   rhs = RECUR (TREE_OPERAND (op11, 1));
   20522              :                 }
   20523          233 :               opcode = TREE_CODE (op11);
   20524          233 :               if (opcode == MODIFY_EXPR)
   20525           11 :                 opcode = NOP_EXPR;
   20526              :             }
   20527              :           else
   20528              :             {
   20529           26 :               code = OMP_ATOMIC;
   20530           26 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   20531           26 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   20532              :             }
   20533          425 :           finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
   20534              :                              lhs1, rhs1, r, tmp,
   20535          425 :                              OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
   20536              :         }
   20537              :       break;
   20538              : 
   20539           54 :     case OMP_METADIRECTIVE:
   20540           54 :       {
   20541           54 :         tree variants = NULL_TREE;
   20542          166 :         for (tree v = OMP_METADIRECTIVE_VARIANTS (t); v; v = TREE_CHAIN (v))
   20543              :           {
   20544          112 :             tree ctx = OMP_METADIRECTIVE_VARIANT_SELECTOR (v);
   20545          112 :             tree directive = OMP_METADIRECTIVE_VARIANT_DIRECTIVE (v);
   20546          112 :             tree body = OMP_METADIRECTIVE_VARIANT_BODY (v);
   20547          112 :             tree s;
   20548              : 
   20549              :             /* CTX is null if this is the default variant.  */
   20550          112 :             if (ctx)
   20551              :               {
   20552           58 :                 ctx = tsubst_omp_context_selector (ctx, args, complain,
   20553              :                                                    in_decl);
   20554              :                 /* Remove the selector from further consideration if it can be
   20555              :                    evaluated as a non-match at this point.  */
   20556           58 :                 if (omp_context_selector_matches (ctx, NULL_TREE, false) == 0)
   20557           16 :                   continue;
   20558              :               }
   20559           96 :             s = push_stmt_list ();
   20560           96 :             RECUR (directive);
   20561           96 :             directive = pop_stmt_list (s);
   20562           96 :             if (body)
   20563              :               {
   20564           36 :                 s = push_stmt_list ();
   20565           36 :                 RECUR (body);
   20566           36 :                 body = pop_stmt_list (s);
   20567              :               }
   20568           96 :             variants
   20569           96 :               = chainon (variants,
   20570              :                          make_omp_metadirective_variant (ctx, directive,
   20571              :                                                          body));
   20572              :           }
   20573           54 :         t = copy_node (t);
   20574           54 :         OMP_METADIRECTIVE_VARIANTS (t) = variants;
   20575              : 
   20576              :         /* Try to resolve the metadirective early.  */
   20577           54 :         vec<struct omp_variant> candidates
   20578           54 :           = omp_early_resolve_metadirective (t);
   20579           54 :         if (!candidates.is_empty ())
   20580           54 :           t = c_omp_expand_variant_construct (candidates);
   20581           54 :         add_stmt (t);
   20582           54 :         break;
   20583              :       }
   20584              : 
   20585            0 :     case OMP_DECLARE_MAPPER:
   20586            0 :       {
   20587            0 :         t = copy_node (t);
   20588              : 
   20589            0 :         tree decl = OMP_DECLARE_MAPPER_DECL (t);
   20590            0 :         decl = tsubst (decl, args, complain, in_decl);
   20591            0 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20592            0 :         tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t);
   20593            0 :         clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_MAPPER, args,
   20594              :                                       complain, in_decl);
   20595            0 :         TREE_TYPE (t) = type;
   20596            0 :         OMP_DECLARE_MAPPER_DECL (t) = decl;
   20597            0 :         OMP_DECLARE_MAPPER_CLAUSES (t) = clauses;
   20598            0 :         RETURN (t);
   20599              :       }
   20600              : 
   20601           80 :     case TRANSACTION_EXPR:
   20602           80 :       {
   20603           80 :         int flags = 0;
   20604           80 :         flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
   20605           80 :         flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
   20606              : 
   20607           80 :         if (TRANSACTION_EXPR_IS_STMT (t))
   20608              :           {
   20609           47 :             tree body = TRANSACTION_EXPR_BODY (t);
   20610           47 :             tree noex = NULL_TREE;
   20611           47 :             if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
   20612              :               {
   20613           12 :                 noex = MUST_NOT_THROW_COND (body);
   20614           12 :                 if (noex == NULL_TREE)
   20615            3 :                   noex = boolean_true_node;
   20616           12 :                 body = TREE_OPERAND (body, 0);
   20617              :               }
   20618           47 :             stmt = begin_transaction_stmt (input_location, NULL, flags);
   20619           47 :             RECUR (body);
   20620           47 :             finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
   20621              :           }
   20622              :         else
   20623              :           {
   20624           33 :             stmt = build_transaction_expr (EXPR_LOCATION (t),
   20625           33 :                                            RECUR (TRANSACTION_EXPR_BODY (t)),
   20626              :                                            flags, NULL_TREE);
   20627           33 :             RETURN (stmt);
   20628              :           }
   20629              :       }
   20630           47 :       break;
   20631              : 
   20632           36 :     case OMP_INTEROP:
   20633           36 :       tmp = tsubst_omp_clauses (OMP_INTEROP_CLAUSES (t), C_ORT_OMP_INTEROP,
   20634              :                                 args, complain, in_decl);
   20635           36 :       t = copy_node (t);
   20636           36 :       OMP_INTEROP_CLAUSES (t) = tmp;
   20637           36 :       add_stmt (t);
   20638           36 :       break;
   20639              : 
   20640           21 :     case MUST_NOT_THROW_EXPR:
   20641           21 :       {
   20642           21 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20643           21 :         tree cond = RECUR (MUST_NOT_THROW_COND (t));
   20644           21 :         stmt = build_must_not_throw_expr (op0, cond);
   20645           21 :         if (stmt && TREE_CODE (stmt) == MUST_NOT_THROW_EXPR)
   20646              :           {
   20647           18 :             MUST_NOT_THROW_NOEXCEPT_P (stmt) = MUST_NOT_THROW_NOEXCEPT_P (t);
   20648           18 :             MUST_NOT_THROW_THROW_P (stmt) = MUST_NOT_THROW_THROW_P (t);
   20649           18 :             MUST_NOT_THROW_CATCH_P (stmt) = MUST_NOT_THROW_CATCH_P (t);
   20650              :           }
   20651           21 :         RETURN (stmt);
   20652              :       }
   20653              : 
   20654            0 :     case EXPR_PACK_EXPANSION:
   20655            0 :       error ("invalid use of pack expansion expression");
   20656            0 :       RETURN (error_mark_node);
   20657              : 
   20658            0 :     case NONTYPE_ARGUMENT_PACK:
   20659            0 :       error ("use %<...%> to expand argument pack");
   20660            0 :       RETURN (error_mark_node);
   20661              : 
   20662       458023 :     case COMPOUND_EXPR:
   20663       458023 :       tmp = RECUR (TREE_OPERAND (t, 0));
   20664       458023 :       if (tmp == NULL_TREE)
   20665              :         /* If the first operand was a statement, we're done with it.  */
   20666       387227 :         RETURN (RECUR (TREE_OPERAND (t, 1)));
   20667        70796 :       RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
   20668              :                                     RECUR (TREE_OPERAND (t, 1)),
   20669              :                                     templated_operator_saved_lookups (t),
   20670              :                                     complain));
   20671              : 
   20672       564159 :     case PREDICT_EXPR:
   20673       564159 :       RETURN (add_stmt (copy_node (t)));
   20674              : 
   20675         9798 :     case ANNOTATE_EXPR:
   20676         9798 :       {
   20677              :         /* Although ANNOTATE_EXPR is an expression, it can only appear in
   20678              :            WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
   20679              :            using tsubst_stmt rather than tsubst_expr and can contain
   20680              :            DECL_EXPRs.  */
   20681         9798 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   20682         9798 :         tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   20683         9798 :         tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
   20684         9798 :         if (TREE_CODE (op2) == INTEGER_CST
   20685         9798 :             && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
   20686         9783 :           op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
   20687              :                                         op3);
   20688         9798 :         RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
   20689              :                             TREE_TYPE (op1), op1, op2, op3));
   20690              :       }
   20691              : 
   20692    234072147 :     default:
   20693    234072147 :       gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
   20694              : 
   20695    234072147 :       RETURN (tsubst_expr (t, args, complain, in_decl));
   20696              :     }
   20697              : 
   20698    280681261 :   RETURN (NULL_TREE);
   20699    515785443 :  out:
   20700    515785443 :   input_location = loc;
   20701    515785443 :   return r;
   20702              : #undef RECUR
   20703              : #undef RETURN
   20704              : }
   20705              : 
   20706              : /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
   20707              :    function.  For description of the body see comment above
   20708              :    cp_parser_omp_declare_reduction_exprs.  */
   20709              : 
   20710              : static void
   20711          104 : tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   20712              : {
   20713          104 :   if (t == NULL_TREE || t == error_mark_node)
   20714            0 :     return;
   20715              : 
   20716          104 :   gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
   20717              : 
   20718          104 :   tree_stmt_iterator tsi;
   20719          104 :   int i;
   20720          104 :   tree stmts[7];
   20721          104 :   memset (stmts, 0, sizeof stmts);
   20722          104 :   for (i = 0, tsi = tsi_start (t);
   20723          619 :        i < 7 && !tsi_end_p (tsi);
   20724          515 :        i++, tsi_next (&tsi))
   20725          515 :     stmts[i] = tsi_stmt (tsi);
   20726          104 :   gcc_assert (tsi_end_p (tsi));
   20727              : 
   20728          104 :   if (i >= 3)
   20729              :     {
   20730          104 :       gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
   20731              :                   && TREE_CODE (stmts[1]) == DECL_EXPR);
   20732          104 :       tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
   20733              :                              args, complain, in_decl);
   20734          104 :       tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
   20735              :                             args, complain, in_decl);
   20736              :       /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
   20737              :          expect to be pushing it.  */
   20738          104 :       DECL_CONTEXT (omp_out) = current_function_decl;
   20739          104 :       DECL_CONTEXT (omp_in) = current_function_decl;
   20740          104 :       keep_next_level (true);
   20741          104 :       tree block = begin_omp_structured_block ();
   20742          104 :       tsubst_stmt (stmts[2], args, complain, in_decl);
   20743          104 :       block = finish_omp_structured_block (block);
   20744          104 :       block = maybe_cleanup_point_expr_void (block);
   20745          104 :       add_decl_expr (omp_out);
   20746          104 :       copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
   20747          104 :       add_decl_expr (omp_in);
   20748          104 :       finish_expr_stmt (block);
   20749              :     }
   20750          104 :   if (i >= 6)
   20751              :     {
   20752           57 :       gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
   20753              :                   && TREE_CODE (stmts[4]) == DECL_EXPR);
   20754           57 :       tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
   20755              :                               args, complain, in_decl);
   20756           57 :       tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
   20757              :                               args, complain, in_decl);
   20758           57 :       DECL_CONTEXT (omp_priv) = current_function_decl;
   20759           57 :       DECL_CONTEXT (omp_orig) = current_function_decl;
   20760           57 :       keep_next_level (true);
   20761           57 :       tree block = begin_omp_structured_block ();
   20762           57 :       tsubst_stmt (stmts[5], args, complain, in_decl);
   20763           57 :       block = finish_omp_structured_block (block);
   20764           57 :       block = maybe_cleanup_point_expr_void (block);
   20765           57 :       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
   20766           57 :       add_decl_expr (omp_priv);
   20767           57 :       add_decl_expr (omp_orig);
   20768           57 :       finish_expr_stmt (block);
   20769           57 :       if (i == 7)
   20770           32 :         add_decl_expr (omp_orig);
   20771              :     }
   20772              : }
   20773              : 
   20774              : /* T is a postfix-expression that is not being used in a function
   20775              :    call.  Return the substituted version of T.  */
   20776              : 
   20777              : static tree
   20778     67055995 : tsubst_non_call_postfix_expression (tree t, tree args,
   20779              :                                     tsubst_flags_t complain,
   20780              :                                     tree in_decl)
   20781              : {
   20782     67055995 :   if (TREE_CODE (t) == SCOPE_REF)
   20783        34222 :     t = tsubst_qualified_id (t, args, complain, in_decl,
   20784              :                              /*done=*/false, /*address_p=*/false);
   20785              :   else
   20786     67021773 :     t = tsubst_expr (t, args, complain, in_decl);
   20787              : 
   20788     67055995 :   return t;
   20789              : }
   20790              : 
   20791              : /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
   20792              :    LAMBDA_EXPR_CAPTURE_LIST passed in LIST.  Do deduction for a previously
   20793              :    dependent init-capture.  EXPLICIT_P is true if the original list had
   20794              :    explicit captures.  */
   20795              : 
   20796              : static void
   20797      1197598 : prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
   20798              :                      tsubst_flags_t complain)
   20799              : {
   20800      1197598 :   if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
   20801              :     {
   20802          498 :       tree type = NULL_TREE;
   20803          498 :       if (!init)
   20804              :         {
   20805            3 :           if (complain & tf_error)
   20806            3 :             error ("empty initializer in lambda init-capture");
   20807            3 :           init = error_mark_node;
   20808              :         }
   20809          495 :       else if (TREE_CODE (init) == TREE_LIST)
   20810            0 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   20811          498 :       if (!type)
   20812          498 :         type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
   20813          498 :       TREE_TYPE (field) = type;
   20814          498 :       cp_apply_type_quals_to_decl (cp_type_quals (type), field);
   20815              :     }
   20816      1197598 :   list = tree_cons (field, init, list);
   20817      1197598 :   LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
   20818      1197598 : }
   20819              : 
   20820              : /* T is a LAMBDA_EXPR.  Generate a new LAMBDA_EXPR for the current
   20821              :    instantiation context.  Instantiating a pack expansion containing a lambda
   20822              :    might result in multiple lambdas all based on the same lambda in the
   20823              :    template.  */
   20824              : 
   20825              : tree
   20826       539423 : tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   20827              : {
   20828       539423 :   tree oldfn = lambda_function (t);
   20829       539423 :   in_decl = oldfn;
   20830              : 
   20831       539423 :   args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
   20832       539423 :   if (processing_template_decl
   20833       539423 :       && (!in_template_context || (complain & tf_partial)
   20834         1400 :           || LAMBDA_EXPR_EXTRA_ARGS (t)))
   20835              :     {
   20836              :       /* Defer templated substitution into a lambda-expr if we lost the
   20837              :          necessary template context.  This may happen for a lambda-expr
   20838              :          used as a default template argument.
   20839              : 
   20840              :          Defer dependent substitution as well so that we don't prematurely
   20841              :          lower the level of a deduced return type or any other auto or
   20842              :          template parameter belonging to the lambda.
   20843              : 
   20844              :          Finally, if a substitution into this lambda was previously
   20845              :          deferred, keep deferring until the final (non-templated)
   20846              :          substitution.  */
   20847          678 :       t = copy_node (t);
   20848          678 :       LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
   20849          678 :       LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain);
   20850          678 :       return t;
   20851              :     }
   20852              : 
   20853       538745 :   tree r = build_lambda_expr ();
   20854              : 
   20855       538745 :   LAMBDA_EXPR_LOCATION (r) = LAMBDA_EXPR_LOCATION (t);
   20856       538745 :   LAMBDA_EXPR_CONSTEVAL_BLOCK_P (r) = LAMBDA_EXPR_CONSTEVAL_BLOCK_P (t);
   20857       538745 :   LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
   20858       538745 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   20859         2718 :     LAMBDA_EXPR_REGEN_INFO (r)
   20860         1359 :       = build_template_info (t, add_to_template_args (TI_ARGS (ti),
   20861              :                                                       preserve_args (args)));
   20862              :   else
   20863      1074772 :     LAMBDA_EXPR_REGEN_INFO (r)
   20864       537386 :       = build_template_info (t, preserve_args (args));
   20865              : 
   20866       538745 :   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
   20867              : 
   20868       538745 :   vec<tree,va_gc>* field_packs = NULL;
   20869       538745 :   unsigned name_independent_cnt = 0;
   20870      1736042 :   for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
   20871      1197297 :        cap = TREE_CHAIN (cap))
   20872              :     {
   20873      1197303 :       tree ofield = TREE_PURPOSE (cap);
   20874      1197303 :       tree init = TREE_VALUE (cap);
   20875      1197303 :       if (PACK_EXPANSION_P (init))
   20876          361 :         init = tsubst_pack_expansion (init, args, complain, in_decl);
   20877              :       else
   20878      1196942 :         init = tsubst_expr (init, args, complain, in_decl);
   20879              : 
   20880      1197303 :       if (init == error_mark_node)
   20881            6 :         return error_mark_node;
   20882              : 
   20883      1197297 :       if (init && TREE_CODE (init) == TREE_LIST)
   20884           12 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   20885              : 
   20886      1197297 :       if (!processing_template_decl
   20887      1196176 :           && init && TREE_CODE (init) != TREE_VEC
   20888      2393118 :           && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
   20889              :         {
   20890              :           /* For a VLA, simply tsubsting the field type won't work, we need to
   20891              :              go through add_capture again.  XXX do we want to do this for all
   20892              :              captures?  */
   20893            9 :           tree name = (get_identifier
   20894              :                        (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
   20895            9 :           tree ftype = TREE_TYPE (ofield);
   20896            9 :           bool by_ref = (TYPE_REF_P (ftype)
   20897            9 :                          || (TREE_CODE (ftype) == DECLTYPE_TYPE
   20898            0 :                              && DECLTYPE_FOR_REF_CAPTURE (ftype)));
   20899            9 :           add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield),
   20900              :                        &name_independent_cnt);
   20901            9 :           continue;
   20902            9 :         }
   20903              : 
   20904      1197288 :       if (PACK_EXPANSION_P (ofield))
   20905          349 :         ofield = PACK_EXPANSION_PATTERN (ofield);
   20906      1197288 :       tree field = tsubst_decl (ofield, args, complain);
   20907              : 
   20908      1197288 :       if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
   20909              :         {
   20910              :           /* Remember these for when we've pushed local_specializations.  */
   20911          162 :           vec_safe_push (field_packs, ofield);
   20912          162 :           vec_safe_push (field_packs, field);
   20913              :         }
   20914              : 
   20915      1197288 :       if (field == error_mark_node)
   20916              :         return error_mark_node;
   20917              : 
   20918      1197288 :       if (TREE_CODE (field) == TREE_VEC)
   20919              :         {
   20920          352 :           int len = TREE_VEC_LENGTH (field);
   20921          352 :           gcc_assert (TREE_CODE (init) == TREE_VEC
   20922              :                       && TREE_VEC_LENGTH (init) == len);
   20923         1014 :           for (int i = 0; i < len; ++i)
   20924         1324 :             prepend_one_capture (TREE_VEC_ELT (field, i),
   20925          662 :                                  TREE_VEC_ELT (init, i),
   20926          662 :                                  LAMBDA_EXPR_CAPTURE_LIST (r),
   20927          662 :                                  LAMBDA_CAPTURE_EXPLICIT_P (cap),
   20928              :                                  complain);
   20929              :         }
   20930              :       else
   20931              :         {
   20932      2393872 :           prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
   20933      1196936 :                                LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
   20934              : 
   20935      1196936 :           if (id_equal (DECL_NAME (field), "__this"))
   20936       112390 :             LAMBDA_EXPR_THIS_CAPTURE (r) = field;
   20937              :         }
   20938              :     }
   20939              : 
   20940       538739 :   tree type = begin_lambda_type (r);
   20941       538739 :   if (type == error_mark_node)
   20942              :     {
   20943            0 :       gcc_checking_assert (!(complain & tf_error) || seen_error ());
   20944            0 :       return error_mark_node;
   20945              :     }
   20946              : 
   20947       538739 :   if (LAMBDA_EXPR_EXTRA_SCOPE (t)
   20948              :       /* When evaluating a concept we instantiate any lambda bodies
   20949              :          in the context of the evaluation.  For ABI reasons don't
   20950              :          record a scope for this instantiated lambda so we don't
   20951              :          throw off the scope counter.  */
   20952       538739 :       && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (t)) != CONCEPT_DECL)
   20953       537330 :     record_lambda_scope (r);
   20954       538739 :   if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
   20955              :     /* If we're pushed into another scope (PR105652), fix it.  */
   20956         2370 :     TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
   20957         2370 :       = TYPE_CONTEXT (TREE_TYPE (t));
   20958       538739 :   record_lambda_scope_discriminator (r);
   20959              : 
   20960              :   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
   20961       538739 :   determine_visibility (TYPE_NAME (type));
   20962              : 
   20963       538739 :   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
   20964              : 
   20965       538739 :   tree oldtmpl = (generic_lambda_fn_p (oldfn)
   20966       538739 :                   ? DECL_TI_TEMPLATE (oldfn)
   20967        78603 :                   : NULL_TREE);
   20968              : 
   20969        78603 :   tree tparms = NULL_TREE;
   20970        78603 :   if (oldtmpl)
   20971        78603 :     tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
   20972              : 
   20973       538739 :   tree fntype = static_fn_type (oldfn);
   20974              : 
   20975       538739 :   begin_scope (sk_lambda, NULL_TREE);
   20976              : 
   20977              :   /* Like in cp_parser_lambda_expression, we need to bring the captures
   20978              :      into the lambda scope.  */
   20979       538739 :   tree ns = decl_namespace_context (type);
   20980       538739 :   push_nested_namespace (ns);
   20981       538739 :   push_nested_class (type);
   20982       538739 :   tree dummy_fco = maybe_add_dummy_lambda_op (r);
   20983       538739 :   pop_nested_class ();
   20984       538739 :   pop_nested_namespace (ns);
   20985       538739 :   push_capture_proxies (r, /*early_p=*/true);
   20986              : 
   20987       538739 :   tree saved_ctp = current_template_parms;
   20988       538739 :   if (oldtmpl)
   20989              :     {
   20990        78603 :       ++processing_template_decl;
   20991        78603 :       current_template_parms = tparms;
   20992              :     }
   20993       538739 :   fntype = tsubst (fntype, args, complain, in_decl);
   20994       538739 :   if (oldtmpl)
   20995              :     {
   20996        78603 :       current_template_parms = saved_ctp;
   20997        78603 :       --processing_template_decl;
   20998              :     }
   20999              : 
   21000              :   /* We are about to create the real operator(), so get rid of the old one.  */
   21001       538739 :   if (dummy_fco)
   21002       446040 :     remove_dummy_lambda_op (dummy_fco, r);
   21003              : 
   21004       538739 :   if (fntype == error_mark_node)
   21005              :     r = error_mark_node;
   21006              :   else
   21007              :     {
   21008              :       /* Fix the type of 'this'.
   21009              :          For static and xobj member functions we use this to transport the
   21010              :          lambda's closure type.  It appears that in the regular case the
   21011              :          object parameter is still pulled off, and then re-added again anyway.
   21012              :          So perhaps we could do something better here?  */
   21013       538727 :       fntype = build_memfn_type (fntype, type,
   21014              :                                  type_memfn_quals (fntype),
   21015              :                                  type_memfn_rqual (fntype));
   21016       538727 :       tree inst = (oldtmpl
   21017       538727 :                    ? tsubst_template_decl (oldtmpl, args, complain,
   21018              :                                            fntype, tparms)
   21019       460124 :                    : tsubst_function_decl (oldfn, args, complain, fntype));
   21020       538727 :       if (inst == error_mark_node)
   21021              :         {
   21022            0 :           r = error_mark_node;
   21023            0 :           goto out;
   21024              :         }
   21025       538727 :       finish_member_declaration (inst);
   21026       538727 :       record_lambda_scope_sig_discriminator (r, inst);
   21027              : 
   21028       617330 :       tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
   21029              : 
   21030              :       /* Let finish_function set this.  */
   21031       538727 :       DECL_DECLARED_CONSTEXPR_P (fn) = false;
   21032              : 
   21033              :       /* The body of a lambda-expression is not a subexpression of the
   21034              :          enclosing expression.  */
   21035       538727 :       cp_evaluated ev;
   21036              : 
   21037              :       /* Now we're done with the parameter-declaration-clause, and should
   21038              :          assume "const" unless "mutable" was present.  */
   21039       538727 :       LAMBDA_EXPR_CONST_QUAL_P (r) = LAMBDA_EXPR_CONST_QUAL_P (t);
   21040              : 
   21041       538727 :       bool nested = cfun;
   21042       538727 :       if (nested)
   21043       532669 :         push_function_context ();
   21044              :       else
   21045              :         /* Still increment function_depth so that we don't GC in the
   21046              :            middle of an expression.  */
   21047         6058 :         ++function_depth;
   21048              : 
   21049       538727 :       local_specialization_stack s (lss_copy);
   21050              : 
   21051       538727 :       bool save_in_consteval_if_p = in_consteval_if_p;
   21052       538727 :       in_consteval_if_p = false;
   21053              : 
   21054       538727 :       tree body = start_lambda_function (fn, r);
   21055              : 
   21056              :       /* Now record them for lookup_init_capture_pack.  */
   21057       538727 :       int fplen = vec_safe_length (field_packs);
   21058       538889 :       for (int i = 0; i < fplen; )
   21059              :         {
   21060          162 :           tree pack = (*field_packs)[i++];
   21061          162 :           tree inst = (*field_packs)[i++];
   21062          162 :           register_local_specialization (inst, pack);
   21063              :         }
   21064       538727 :       release_tree_vector (field_packs);
   21065              : 
   21066       538727 :       register_parameter_specializations (oldfn, fn);
   21067              : 
   21068       538727 :       if (oldtmpl)
   21069              :         {
   21070              :           /* We might not partially instantiate some parts of the function, so
   21071              :              copy these flags from the original template.  */
   21072        78603 :           language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
   21073        78603 :           current_function_returns_value = ol->returns_value;
   21074        78603 :           current_function_returns_null = ol->returns_null;
   21075        78603 :           current_function_returns_abnormally = ol->returns_abnormally;
   21076        78603 :           current_function_infinite_loop = ol->infinite_loop;
   21077              :         }
   21078              : 
   21079              :       /* [temp.deduct] A lambda-expression appearing in a function type or a
   21080              :          template parameter is not considered part of the immediate context for
   21081              :          the purposes of template argument deduction. */
   21082       538727 :       if (!emitting_diagnostic_p ())
   21083       538706 :         complain = tf_warning_or_error;
   21084              : 
   21085       538727 :       tree saved = DECL_SAVED_TREE (oldfn);
   21086       538727 :       if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
   21087              :         /* We already have a body block from start_lambda_function, we don't
   21088              :            need another to confuse NRV (91217).  */
   21089       538727 :         saved = BIND_EXPR_BODY (saved);
   21090              : 
   21091       538727 :       tsubst_stmt (saved, args, complain, r);
   21092              : 
   21093       538727 :       finish_lambda_function (body);
   21094              : 
   21095       538727 :       in_consteval_if_p = save_in_consteval_if_p;
   21096              : 
   21097       538727 :       if (nested)
   21098       532669 :         pop_function_context ();
   21099              :       else
   21100         6058 :         --function_depth;
   21101              : 
   21102              :       /* The capture list was built up in reverse order; fix that now.  */
   21103       538727 :       LAMBDA_EXPR_CAPTURE_LIST (r)
   21104       538727 :         = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
   21105              : 
   21106       538727 :       maybe_add_lambda_conv_op (type);
   21107       538727 :     }
   21108              : 
   21109       538739 : out:
   21110       538739 :   pop_bindings_and_leave_scope ();
   21111       538739 :   finish_struct (type, /*attr*/NULL_TREE);
   21112              : 
   21113       538739 :   insert_pending_capture_proxies ();
   21114              : 
   21115       538739 :   return r;
   21116              : }
   21117              : 
   21118              : /* Subroutine of maybe_fold_fn_template_args.  */
   21119              : 
   21120              : static bool
   21121     51438382 : fold_targs_r (tree targs, tsubst_flags_t complain)
   21122              : {
   21123     51438382 :   int len = TREE_VEC_LENGTH (targs);
   21124    108441447 :   for (int i = 0; i < len; ++i)
   21125              :     {
   21126     57003068 :       tree &elt = TREE_VEC_ELT (targs, i);
   21127     57003068 :       if (!elt || TYPE_P (elt)
   21128       648716 :           || TREE_CODE (elt) == TEMPLATE_DECL)
   21129     56355140 :         continue;
   21130       647928 :       if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
   21131              :         {
   21132            0 :           if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
   21133              :             return false;
   21134              :         }
   21135       647928 :       else if (/* We can only safely preevaluate scalar prvalues.  */
   21136      1295850 :                SCALAR_TYPE_P (TREE_TYPE (elt))
   21137       644741 :                && !glvalue_p (elt)
   21138      1284288 :                && !TREE_CONSTANT (elt))
   21139              :         {
   21140         3284 :           elt = cxx_constant_value (elt, complain);
   21141         3284 :           if (elt == error_mark_node)
   21142              :             return false;
   21143              :         }
   21144              :     }
   21145              : 
   21146              :   return true;
   21147              : }
   21148              : 
   21149              : /* Try to do constant evaluation of any explicit template arguments in FN
   21150              :    before overload resolution, to get any errors only once.  Return true iff
   21151              :    we didn't have any problems folding.  */
   21152              : 
   21153              : static bool
   21154    164220972 : maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
   21155              : {
   21156    164220972 :   if (processing_template_decl || fn == NULL_TREE)
   21157              :     return true;
   21158    156074010 :   if (fn == error_mark_node)
   21159              :     return false;
   21160    155726216 :   if (TREE_CODE (fn) == OFFSET_REF
   21161    155662124 :       || TREE_CODE (fn) == COMPONENT_REF)
   21162     29423228 :     fn = TREE_OPERAND (fn, 1);
   21163    155726216 :   if (BASELINK_P (fn))
   21164     40908288 :     fn = BASELINK_FUNCTIONS (fn);
   21165    155726216 :   if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
   21166              :     return true;
   21167     51438382 :   tree targs = TREE_OPERAND (fn, 1);
   21168     51438382 :   if (targs == NULL_TREE)
   21169              :     return true;
   21170     51438382 :   if (targs == error_mark_node)
   21171              :     return false;
   21172     51438382 :   return fold_targs_r (targs, complain);
   21173              : }
   21174              : 
   21175              : /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling.  */
   21176              : 
   21177              : static void
   21178    164277223 : tsubst_call_args (tree t, tree args, tsubst_flags_t complain,
   21179              :                   tree in_decl, releasing_vec &call_args)
   21180              : {
   21181    164277223 :   unsigned int nargs = call_expr_nargs (t);
   21182    282029067 :   for (unsigned int i = 0; i < nargs; ++i)
   21183              :     {
   21184    117751847 :       tree arg = CALL_EXPR_ARG (t, i);
   21185              : 
   21186    117751847 :       if (!PACK_EXPANSION_P (arg))
   21187    117173754 :         vec_safe_push (call_args, tsubst_expr (arg, args, complain, in_decl));
   21188              :       else
   21189              :         {
   21190              :           /* Expand the pack expansion and push each entry onto CALL_ARGS.  */
   21191       578093 :           arg = tsubst_pack_expansion (arg, args, complain, in_decl);
   21192       578093 :           if (TREE_CODE (arg) == TREE_VEC)
   21193              :             {
   21194       523629 :               unsigned int len, j;
   21195              : 
   21196       523629 :               len = TREE_VEC_LENGTH (arg);
   21197       918589 :               for (j = 0; j < len; ++j)
   21198              :                 {
   21199       394960 :                   tree value = TREE_VEC_ELT (arg, j);
   21200       394960 :                   if (value != NULL_TREE)
   21201       394960 :                     value = convert_from_reference (value);
   21202       394960 :                   vec_safe_push (call_args, value);
   21203              :                 }
   21204              :             }
   21205              :           else
   21206              :             /* A partial substitution.  Add one entry.  */
   21207        54464 :             vec_safe_push (call_args, arg);
   21208              :         }
   21209              :     }
   21210    164277220 : }
   21211              : 
   21212              : /* Like tsubst but deals with expressions and performs semantic
   21213              :    analysis.  */
   21214              : 
   21215              : tree
   21216   2377643103 : tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   21217              : {
   21218              : #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
   21219              : #define RECUR(NODE)                                             \
   21220              :   tsubst_expr (NODE, args, complain, in_decl)
   21221              : 
   21222   2377643103 :   tree retval, op1;
   21223   2377643103 :   location_t save_loc;
   21224              : 
   21225   2377643103 :   if (t == NULL_TREE || t == error_mark_node)
   21226              :     return t;
   21227              : 
   21228   2372742421 :   save_loc = input_location;
   21229   2372742421 :   if (location_t eloc = cp_expr_location (t))
   21230    985518250 :     input_location = eloc;
   21231              : 
   21232              :   /* N3276 decltype magic only applies to calls at the top level or on the
   21233              :      right side of a comma.  */
   21234   2372742421 :   tsubst_flags_t decltype_flag = (complain & tf_decltype);
   21235   2372742421 :   complain &= ~tf_decltype;
   21236              : 
   21237              :   /* This flag only applies to id-expressions at the top level, and
   21238              :      controls resolution thereof.  */
   21239   2372742421 :   tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup);
   21240   2372742421 :   complain &= ~tf_no_name_lookup;
   21241              : 
   21242   2372742421 :   if (instantiating_tu_local_entity (t))
   21243           52 :     RETURN (error_mark_node);
   21244              : 
   21245   2372742369 :   if (!no_name_lookup_flag)
   21246   1933533327 :     if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   21247              :       return d;
   21248              : 
   21249   2372732448 :   switch (TREE_CODE (t))
   21250              :     {
   21251       167212 :     case USING_DECL:
   21252       167212 :       t = DECL_NAME (t);
   21253              :       /* Fall through.  */
   21254    415094699 :     case IDENTIFIER_NODE:
   21255    415094699 :       {
   21256    415094699 :         tree decl;
   21257    415094699 :         cp_id_kind idk;
   21258    415094699 :         const char *error_msg;
   21259              : 
   21260    415094699 :         if (IDENTIFIER_CONV_OP_P (t))
   21261              :           {
   21262         1595 :             tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21263         1595 :             t = make_conv_op_name (new_type);
   21264              :           }
   21265              : 
   21266    415094699 :         if (no_name_lookup_flag)
   21267    414857653 :           RETURN (t);
   21268              : 
   21269              :         /* Look up the name.  */
   21270       237046 :         decl = lookup_name (t);
   21271              : 
   21272              :         /* By convention, expressions use ERROR_MARK_NODE to indicate
   21273              :            failure, not NULL_TREE.  */
   21274       237046 :         if (decl == NULL_TREE)
   21275           57 :           decl = error_mark_node;
   21276              : 
   21277       237046 :         decl = finish_id_expression (t, decl, NULL_TREE,
   21278              :                                      &idk,
   21279              :                                      /*i_c_e_p=*/false,
   21280              :                                      /*allow_i_c_e_p=*/true,
   21281              :                                      /*non_i_c_e_p=*/nullptr,
   21282              :                                      /*template_p=*/false,
   21283              :                                      /*done=*/true,
   21284              :                                      /*address_p=*/false,
   21285              :                                      /*template_arg_p=*/false,
   21286              :                                      &error_msg,
   21287              :                                      input_location);
   21288       237046 :         if (error_msg)
   21289            0 :           error (error_msg);
   21290       237046 :         if (identifier_p (decl))
   21291              :           {
   21292           57 :             if (complain & tf_error)
   21293           54 :               unqualified_name_lookup_error (decl);
   21294           57 :             decl = error_mark_node;
   21295              :           }
   21296    415094699 :         RETURN (decl);
   21297              :       }
   21298              : 
   21299     88003882 :     case TEMPLATE_ID_EXPR:
   21300     88003882 :       {
   21301     88003882 :         tree object;
   21302     88003882 :         tree templ = TREE_OPERAND (t, 0);
   21303     88003882 :         tree targs = TREE_OPERAND (t, 1);
   21304              : 
   21305     88003882 :         if (no_name_lookup_flag)
   21306      4876494 :           templ = tsubst_name (templ, args, complain, in_decl);
   21307              :         else
   21308     83127388 :           templ = tsubst_expr (templ, args, complain, in_decl);
   21309              : 
   21310     88003882 :         if (targs)
   21311     88003882 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   21312     88003882 :         if (targs == error_mark_node)
   21313         2665 :           RETURN (error_mark_node);
   21314              : 
   21315     88001217 :         if (TREE_CODE (templ) == SCOPE_REF)
   21316              :           {
   21317           44 :             tree name = TREE_OPERAND (templ, 1);
   21318           44 :             tree tid = lookup_template_function (name, targs);
   21319           44 :             TREE_OPERAND (templ, 1) = tid;
   21320           44 :             RETURN (templ);
   21321              :           }
   21322              : 
   21323    119965656 :         if (concept_definition_p (templ))
   21324              :           {
   21325      4238790 :             tree check = build_concept_check (templ, targs, complain);
   21326      4238790 :             if (check == error_mark_node)
   21327              :               RETURN (error_mark_node);
   21328     83762383 :             RETURN (check);
   21329              :           }
   21330              : 
   21331     83762383 :         if (variable_template_p (templ))
   21332              :           {
   21333     27725691 :             if (no_name_lookup_flag)
   21334           59 :               RETURN (lookup_template_variable (templ, targs, complain));
   21335              : 
   21336     27725632 :             tree r = lookup_and_finish_template_variable (templ, targs,
   21337              :                                                           complain);
   21338     27725632 :             r = convert_from_reference (r);
   21339     27725632 :             r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
   21340     27725632 :             RETURN (r);
   21341              :           }
   21342              : 
   21343     56036692 :         if (TREE_CODE (templ) == COMPONENT_REF)
   21344              :           {
   21345            0 :             object = TREE_OPERAND (templ, 0);
   21346            0 :             templ = TREE_OPERAND (templ, 1);
   21347              :           }
   21348              :         else
   21349              :           object = NULL_TREE;
   21350              : 
   21351     56036692 :         tree tid = lookup_template_function (templ, targs);
   21352     56036692 :         protected_set_expr_location (tid, EXPR_LOCATION (t));
   21353              : 
   21354     56036692 :         if (object)
   21355            0 :           RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
   21356              :                          object, tid, NULL_TREE));
   21357     56036692 :         else if (no_name_lookup_flag)
   21358      4876188 :           RETURN (tid);
   21359     51160504 :         else if (identifier_p (templ))
   21360              :           {
   21361              :             /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
   21362              :                name lookup found nothing when parsing the template name.  */
   21363            0 :             gcc_assert (cxx_dialect >= cxx20 || seen_error ());
   21364            0 :             RETURN (tid);
   21365              :           }
   21366              :         else
   21367     51160504 :           RETURN (baselink_for_fns (tid));
   21368              :       }
   21369              : 
   21370     77984082 :     case INDIRECT_REF:
   21371     77984082 :       {
   21372     77984082 :         tree r = RECUR (TREE_OPERAND (t, 0));
   21373              : 
   21374     77984082 :         if (REFERENCE_REF_P (t))
   21375              :           {
   21376              :             /* A type conversion to reference type will be enclosed in
   21377              :                such an indirect ref, but the substitution of the cast
   21378              :                will have also added such an indirect ref.  */
   21379     46729478 :             r = convert_from_reference (r);
   21380              :           }
   21381              :         else
   21382     31254604 :           r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
   21383              :                                     templated_operator_saved_lookups (t),
   21384              :                                     complain|decltype_flag);
   21385              : 
   21386     77984082 :         if (REF_PARENTHESIZED_P (t))
   21387         9446 :           r = force_paren_expr (r);
   21388              : 
   21389     77984082 :         RETURN (r);
   21390              :       }
   21391              : 
   21392            9 :     case MEM_REF:
   21393            9 :       {
   21394            9 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21395            9 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   21396            9 :         tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21397            9 :         RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1));
   21398              :       }
   21399              : 
   21400     28357833 :     case NOP_EXPR:
   21401     28357833 :       {
   21402     28357833 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21403     28357833 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21404     28357830 :         RETURN (build_nop (type, op0));
   21405              :       }
   21406              : 
   21407     39688859 :     case IMPLICIT_CONV_EXPR:
   21408     39688859 :       {
   21409     39688859 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21410     39688859 :         if (type == error_mark_node)
   21411            3 :           RETURN (error_mark_node);
   21412     39688856 :         tree expr = RECUR (TREE_OPERAND (t, 0));
   21413     79377712 :         if (dependent_implicit_conv_p (type, expr,
   21414     39688856 :                                        IMPLICIT_CONV_EXPR_FORCED (t)))
   21415              :           {
   21416      4860711 :             retval = copy_node (t);
   21417      4860711 :             TREE_TYPE (retval) = type;
   21418      4860711 :             TREE_OPERAND (retval, 0) = expr;
   21419      4860711 :             RETURN (retval);
   21420              :           }
   21421     34828145 :         if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
   21422              :           {
   21423     33802261 :             tree r = convert_nontype_argument (type, expr, complain);
   21424     33802261 :             if (r == NULL_TREE)
   21425           28 :               r = error_mark_node;
   21426     33802261 :             RETURN (r);
   21427              :           }
   21428      1025884 :         int flags = LOOKUP_IMPLICIT;
   21429      1025884 :         if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
   21430       311951 :           flags = LOOKUP_NORMAL;
   21431      1025884 :         if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
   21432        21550 :           flags |= LOOKUP_NO_NARROWING;
   21433      1025884 :         RETURN (perform_implicit_conversion_flags (type, expr, complain,
   21434              :                                                   flags));
   21435              :       }
   21436              : 
   21437        38558 :     case CONVERT_EXPR:
   21438        38558 :       {
   21439        38558 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21440        38558 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21441        38558 :         if (op0 == error_mark_node)
   21442           21 :           RETURN (error_mark_node);
   21443        38537 :         RETURN (build1 (CONVERT_EXPR, type, op0));
   21444              :       }
   21445              : 
   21446     38954345 :     case CAST_EXPR:
   21447     38954345 :     case REINTERPRET_CAST_EXPR:
   21448     38954345 :     case CONST_CAST_EXPR:
   21449     38954345 :     case DYNAMIC_CAST_EXPR:
   21450     38954345 :     case STATIC_CAST_EXPR:
   21451     38954345 :       {
   21452     38954345 :         tree type;
   21453     38954345 :         tree op, r = NULL_TREE;
   21454              : 
   21455     38954345 :         tsubst_flags_t tcomplain = complain;
   21456     38954345 :         if (TREE_CODE (t) == CAST_EXPR)
   21457     30877926 :           tcomplain |= tf_tst_ok;
   21458     38954345 :         type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   21459              : 
   21460     38954345 :         op = RECUR (TREE_OPERAND (t, 0));
   21461              : 
   21462     38954345 :         warning_sentinel s(warn_useless_cast);
   21463     38954345 :         warning_sentinel s2(warn_ignored_qualifiers);
   21464     38954345 :         warning_sentinel s3(warn_int_in_bool_context);
   21465     38954345 :         switch (TREE_CODE (t))
   21466              :           {
   21467     30877926 :           case CAST_EXPR:
   21468     30877926 :             r = build_functional_cast (input_location, type, op, complain);
   21469     30877926 :             break;
   21470       100567 :           case REINTERPRET_CAST_EXPR:
   21471       100567 :             r = build_reinterpret_cast (input_location, type, op, complain);
   21472       100567 :             break;
   21473       185439 :           case CONST_CAST_EXPR:
   21474       185439 :             r = build_const_cast (input_location, type, op, complain);
   21475       185439 :             break;
   21476        20718 :           case DYNAMIC_CAST_EXPR:
   21477        20718 :             r = build_dynamic_cast (input_location, type, op, complain);
   21478        20718 :             break;
   21479      7769695 :           case STATIC_CAST_EXPR:
   21480      7769695 :             r = build_static_cast (input_location, type, op, complain);
   21481      7769695 :             if (IMPLICIT_RVALUE_P (t))
   21482            0 :               set_implicit_rvalue_p (r);
   21483              :             break;
   21484            0 :           default:
   21485            0 :             gcc_unreachable ();
   21486              :           }
   21487              : 
   21488     38954342 :         RETURN (r);
   21489     38954342 :       }
   21490              : 
   21491        72409 :     case BIT_CAST_EXPR:
   21492        72409 :       {
   21493        72409 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21494        72409 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21495        72409 :         RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
   21496              :       }
   21497              : 
   21498      1524100 :     case POSTDECREMENT_EXPR:
   21499      1524100 :     case POSTINCREMENT_EXPR:
   21500      1524100 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21501              :                                                 args, complain, in_decl);
   21502      1524100 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
   21503              :                                 templated_operator_saved_lookups (t),
   21504              :                                 complain|decltype_flag));
   21505              : 
   21506       685234 :     case BIT_NOT_EXPR:
   21507       685234 :       if (identifier_p (TREE_OPERAND (t, 0)))
   21508              :         {
   21509           23 :           gcc_checking_assert (no_name_lookup_flag);
   21510           23 :           RETURN (t);
   21511              :         }
   21512       685211 :       else if (TYPE_P (TREE_OPERAND (t, 0)))
   21513              :         {
   21514       443196 :           gcc_checking_assert (no_name_lookup_flag);
   21515       443196 :           tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
   21516       443196 :           RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0));
   21517              :         }
   21518              :       /* Fall through.  */
   21519     17460964 :     case PREDECREMENT_EXPR:
   21520     17460964 :     case PREINCREMENT_EXPR:
   21521     17460964 :     case NEGATE_EXPR:
   21522     17460964 :     case ABS_EXPR:
   21523     17460964 :     case TRUTH_NOT_EXPR:
   21524     17460964 :     case UNARY_PLUS_EXPR:  /* Unary + */
   21525     17460964 :     case REALPART_EXPR:
   21526     17460964 :     case IMAGPART_EXPR:
   21527     17460964 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t),
   21528              :                                 RECUR (TREE_OPERAND (t, 0)),
   21529              :                                 templated_operator_saved_lookups (t),
   21530              :                                 complain|decltype_flag));
   21531              : 
   21532         1980 :     case EXCESS_PRECISION_EXPR:
   21533         1980 :       {
   21534         1980 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21535         1980 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21536         1980 :         if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
   21537            0 :           RETURN (op0);
   21538         1980 :         RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
   21539              :                             type, op0));
   21540              :       }
   21541              : 
   21542            0 :     case FIX_TRUNC_EXPR:
   21543              :       /* convert_like should have created an IMPLICIT_CONV_EXPR.  */
   21544            0 :       gcc_unreachable ();
   21545              : 
   21546      1142520 :     case ADDR_EXPR:
   21547      1142520 :       op1 = TREE_OPERAND (t, 0);
   21548      1142520 :       if (TREE_CODE (op1) == LABEL_DECL)
   21549           31 :         RETURN (finish_label_address_expr (DECL_NAME (op1),
   21550              :                                           EXPR_LOCATION (op1)));
   21551      1142489 :       if (TREE_CODE (op1) == SCOPE_REF)
   21552        71730 :         op1 = tsubst_qualified_id (op1, args, complain, in_decl,
   21553              :                                    /*done=*/true, /*address_p=*/true);
   21554              :       else
   21555      1070759 :         op1 = tsubst_non_call_postfix_expression (op1, args, complain,
   21556              :                                                   in_decl);
   21557      1142489 :       RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
   21558              :                                 templated_operator_saved_lookups (t),
   21559              :                                 complain|decltype_flag));
   21560              : 
   21561     94100678 :     case PLUS_EXPR:
   21562     94100678 :     case MINUS_EXPR:
   21563     94100678 :     case MULT_EXPR:
   21564     94100678 :     case TRUNC_DIV_EXPR:
   21565     94100678 :     case CEIL_DIV_EXPR:
   21566     94100678 :     case FLOOR_DIV_EXPR:
   21567     94100678 :     case ROUND_DIV_EXPR:
   21568     94100678 :     case EXACT_DIV_EXPR:
   21569     94100678 :     case BIT_AND_EXPR:
   21570     94100678 :     case BIT_IOR_EXPR:
   21571     94100678 :     case BIT_XOR_EXPR:
   21572     94100678 :     case TRUNC_MOD_EXPR:
   21573     94100678 :     case FLOOR_MOD_EXPR:
   21574     94100678 :     case TRUTH_ANDIF_EXPR:
   21575     94100678 :     case TRUTH_ORIF_EXPR:
   21576     94100678 :     case TRUTH_AND_EXPR:
   21577     94100678 :     case TRUTH_OR_EXPR:
   21578     94100678 :     case RSHIFT_EXPR:
   21579     94100678 :     case LSHIFT_EXPR:
   21580     94100678 :     case EQ_EXPR:
   21581     94100678 :     case NE_EXPR:
   21582     94100678 :     case MAX_EXPR:
   21583     94100678 :     case MIN_EXPR:
   21584     94100678 :     case LE_EXPR:
   21585     94100678 :     case GE_EXPR:
   21586     94100678 :     case LT_EXPR:
   21587     94100678 :     case GT_EXPR:
   21588     94100678 :     case SPACESHIP_EXPR:
   21589     94100678 :     case MEMBER_REF:
   21590     94100678 :     case DOTSTAR_EXPR:
   21591     94100678 :       {
   21592              :         /* If either OP0 or OP1 was value- or type-dependent, suppress
   21593              :            warnings that depend on the range of the types involved.  */
   21594     94100678 :         tree op0 = TREE_OPERAND (t, 0);
   21595     94100678 :         tree op1 = TREE_OPERAND (t, 1);
   21596     94100678 :         const bool was_dep = (dependent_operand_p (op0)
   21597     94100678 :                               || dependent_operand_p (op1));
   21598     94100678 :         op0 = RECUR (op0);
   21599     94100678 :         op1 = RECUR (op1);
   21600              : 
   21601     94100633 :         warning_sentinel s1(warn_type_limits, was_dep);
   21602     94100633 :         warning_sentinel s2(warn_div_by_zero, was_dep);
   21603     94100633 :         warning_sentinel s3(warn_logical_op, was_dep);
   21604     94100633 :         warning_sentinel s4(warn_tautological_compare, was_dep);
   21605     94100633 :         warning_sentinel s5(warn_address, was_dep);
   21606              : 
   21607     94100633 :         tree r = build_x_binary_op
   21608    366159678 :           (input_location, TREE_CODE (t),
   21609              :            op0,
   21610     94100633 :            (warning_suppressed_p (TREE_OPERAND (t, 0))
   21611              :             ? ERROR_MARK
   21612     83857779 :             : TREE_CODE (TREE_OPERAND (t, 0))),
   21613              :            op1,
   21614     94100633 :            (warning_suppressed_p (TREE_OPERAND (t, 1))
   21615              :             ? ERROR_MARK
   21616     81686915 :             : TREE_CODE (TREE_OPERAND (t, 1))),
   21617              :            templated_operator_saved_lookups (t),
   21618              :            /*overload=*/NULL,
   21619              :            complain|decltype_flag);
   21620     94100603 :         if (EXPR_P (r))
   21621     94014654 :           copy_warning (r, t);
   21622              : 
   21623     94100603 :         RETURN (r);
   21624     94100603 :       }
   21625              : 
   21626            6 :     case POINTER_PLUS_EXPR:
   21627            6 :       {
   21628            6 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21629            6 :         if (op0 == error_mark_node)
   21630            0 :           RETURN (error_mark_node);
   21631            6 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21632            6 :         if (op1 == error_mark_node)
   21633            0 :           RETURN (error_mark_node);
   21634            6 :         RETURN (fold_build_pointer_plus (op0, op1));
   21635              :       }
   21636              : 
   21637     88647315 :     case SCOPE_REF:
   21638     88647315 :       if (no_name_lookup_flag)
   21639              :         {
   21640          768 :           tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
   21641          768 :           tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl);
   21642          768 :           RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1,
   21643              :                                         QUALIFIED_NAME_IS_TEMPLATE (t)));
   21644              :         }
   21645              :       else
   21646     88646547 :         RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
   21647              :                                     /*address_p=*/false));
   21648              : 
   21649      9727030 :     case BASELINK:
   21650      9727030 :       RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
   21651              :                                args, complain, in_decl));
   21652              : 
   21653      3411464 :     case ARRAY_REF:
   21654      3411464 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21655              :                                                 args, complain, in_decl);
   21656      3411464 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
   21657      3411464 :           && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
   21658         2898 :               == ovl_op_identifier (ARRAY_REF)))
   21659              :         {
   21660          202 :           tree c = TREE_OPERAND (t, 1);
   21661          202 :           releasing_vec index_exp_list;
   21662          202 :           tsubst_call_args (c, args, complain, in_decl, index_exp_list);
   21663              : 
   21664          202 :           tree r;
   21665          202 :           if (vec_safe_length (index_exp_list) == 1
   21666          239 :               && !PACK_EXPANSION_P (index_exp_list[0]))
   21667           37 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   21668           37 :                                  index_exp_list[0], NULL,
   21669              :                                  complain | decltype_flag);
   21670              :           else
   21671          165 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   21672              :                                  NULL_TREE, &index_exp_list,
   21673              :                                  complain | decltype_flag);
   21674          202 :           RETURN (r);
   21675          202 :         }
   21676      3411262 :       RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
   21677              :                                  RECUR (TREE_OPERAND (t, 1)),
   21678              :                                  complain|decltype_flag));
   21679              : 
   21680           30 :     case OMP_ARRAY_SECTION:
   21681           30 :       {
   21682           30 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21683           30 :         tree op1 = NULL_TREE, op2 = NULL_TREE;
   21684           30 :         if (op0 == error_mark_node)
   21685            0 :           RETURN (error_mark_node);
   21686           30 :         if (TREE_OPERAND (t, 1))
   21687              :           {
   21688           27 :             op1 = RECUR (TREE_OPERAND (t, 1));
   21689           27 :             if (op1 == error_mark_node)
   21690            0 :               RETURN (error_mark_node);
   21691              :           }
   21692           30 :         if (TREE_OPERAND (t, 2))
   21693              :           {
   21694           30 :             op2 = RECUR (TREE_OPERAND (t, 2));
   21695           30 :             if (op2 == error_mark_node)
   21696            0 :               RETURN (error_mark_node);
   21697              :           }
   21698           30 :         RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2));
   21699              :       }
   21700              : 
   21701            2 :     case OMP_DECLARE_MAPPER:
   21702            2 :       {
   21703            2 :         t = copy_node (t);
   21704              : 
   21705            2 :         tree decl = OMP_DECLARE_MAPPER_DECL (t);
   21706            2 :         DECL_OMP_DECLARE_MAPPER_P (decl) = 1;
   21707            2 :         decl = tsubst (decl, args, complain, in_decl);
   21708            2 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21709            2 :         tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t);
   21710            2 :         clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_MAPPER, args,
   21711              :                                       complain, in_decl);
   21712            2 :         TREE_TYPE (t) = type;
   21713            2 :         OMP_DECLARE_MAPPER_DECL (t) = decl;
   21714            2 :         OMP_DECLARE_MAPPER_CLAUSES (t) = clauses;
   21715            2 :         RETURN (t);
   21716              :       }
   21717              : 
   21718      8475035 :     case SIZEOF_EXPR:
   21719     15208400 :       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
   21720     15203388 :           || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
   21721              :         {
   21722      1746694 :           tree expanded, op = TREE_OPERAND (t, 0);
   21723      1746694 :           int len = 0;
   21724              : 
   21725      1746694 :           if (SIZEOF_EXPR_TYPE_P (t))
   21726            0 :             op = TREE_TYPE (op);
   21727              : 
   21728      1746694 :           ++cp_unevaluated_operand;
   21729      1746694 :           ++c_inhibit_evaluation_warnings;
   21730              :           /* We only want to compute the number of arguments.  */
   21731      1746694 :           if (PACK_EXPANSION_P (op))
   21732              :             {
   21733      1746682 :               expanded = NULL_TREE;
   21734      1746682 :               if (DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (op)))
   21735              :                 {
   21736          387 :                   tree d = PACK_EXPANSION_PATTERN (op);
   21737          387 :                   if (DECL_HAS_VALUE_EXPR_P (d))
   21738              :                     {
   21739          387 :                       d = DECL_VALUE_EXPR (d);
   21740          387 :                       if (TREE_CODE (d) == TREE_VEC)
   21741              :                         {
   21742          200 :                           tree b = TREE_VEC_ELT (d, 0);
   21743          200 :                           if (!type_dependent_expression_p_push (b))
   21744              :                             {
   21745          200 :                               expanded = void_node;
   21746          200 :                               len = TREE_VEC_LENGTH (d) - 2;
   21747              :                             }
   21748              :                         }
   21749              :                     }
   21750              :                 }
   21751          200 :               if (!expanded)
   21752      1746482 :                 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
   21753              :             }
   21754              :           else
   21755           12 :             expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
   21756              :                                              args, complain, in_decl);
   21757      1746694 :           --cp_unevaluated_operand;
   21758      1746694 :           --c_inhibit_evaluation_warnings;
   21759              : 
   21760      1746694 :           if (TREE_CODE (expanded) == TREE_VEC)
   21761              :             {
   21762      1062114 :               len = TREE_VEC_LENGTH (expanded);
   21763              :               /* Set TREE_USED for the benefit of -Wunused.  */
   21764      3088093 :               for (int i = 0; i < len; i++)
   21765      2025979 :                 if (DECL_P (TREE_VEC_ELT (expanded, i)))
   21766         5733 :                   TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
   21767              :             }
   21768              : 
   21769      1746694 :           if (expanded == error_mark_node)
   21770            0 :             RETURN (error_mark_node);
   21771      1746694 :           else if (PACK_EXPANSION_P (expanded)
   21772      1746694 :                    || (TREE_CODE (expanded) == TREE_VEC
   21773      1062114 :                        && pack_expansion_args_count (expanded)))
   21774              : 
   21775              :             {
   21776       684407 :               if (PACK_EXPANSION_P (expanded))
   21777              :                 /* OK.  */;
   21778              :               else
   21779           27 :                 expanded = make_argument_pack (expanded);
   21780              : 
   21781       684407 :               if (TYPE_P (expanded))
   21782       684172 :                 RETURN (cxx_sizeof_or_alignof_type (input_location,
   21783              :                                                     expanded, SIZEOF_EXPR,
   21784              :                                                     false,
   21785              :                                                     complain & tf_error));
   21786              :               else
   21787          235 :                 RETURN (cxx_sizeof_or_alignof_expr (input_location,
   21788              :                                                     expanded, SIZEOF_EXPR,
   21789              :                                                     false,
   21790              :                                                     complain & tf_error));
   21791              :             }
   21792              :           else
   21793      1062287 :             RETURN (build_int_cst (size_type_node, len));
   21794              :         }
   21795              :       /* Fall through */
   21796              : 
   21797      7477097 :     case ALIGNOF_EXPR:
   21798      7477097 :       {
   21799      7477097 :         tree r;
   21800              : 
   21801      7477097 :         op1 = TREE_OPERAND (t, 0);
   21802      7477097 :         if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
   21803            0 :           op1 = TREE_TYPE (op1);
   21804      7477097 :         bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
   21805      7477097 :                             && ALIGNOF_EXPR_STD_P (t));
   21806      7477097 :         if (!args)
   21807              :           {
   21808              :             /* When there are no ARGS, we are trying to evaluate a
   21809              :                non-dependent expression from the parser.  Trying to do
   21810              :                the substitutions may not work.  */
   21811       150205 :             if (!TYPE_P (op1))
   21812          320 :               op1 = TREE_TYPE (op1);
   21813              :           }
   21814              :         else
   21815              :           {
   21816      7326892 :             ++cp_unevaluated_operand;
   21817      7326892 :             ++c_inhibit_evaluation_warnings;
   21818      7326892 :             if (TYPE_P (op1))
   21819      6856917 :               op1 = tsubst (op1, args, complain, in_decl);
   21820              :             else
   21821       469975 :               op1 = tsubst_expr (op1, args, complain, in_decl);
   21822      7326892 :             --cp_unevaluated_operand;
   21823      7326892 :             --c_inhibit_evaluation_warnings;
   21824              :           }
   21825      7477097 :         if (TYPE_P (op1))
   21826      7007103 :           r = cxx_sizeof_or_alignof_type (input_location,
   21827      7007103 :                                           op1, TREE_CODE (t), std_alignof,
   21828              :                                           complain & tf_error);
   21829              :         else
   21830       469994 :           r = cxx_sizeof_or_alignof_expr (input_location,
   21831       469994 :                                           op1, TREE_CODE (t), std_alignof,
   21832              :                                           complain & tf_error);
   21833      7477097 :         if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
   21834              :           {
   21835      6727606 :             if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
   21836              :               {
   21837      6727498 :                 if (!processing_template_decl && TYPE_P (op1))
   21838              :                   {
   21839      5938432 :                     r = build_min (SIZEOF_EXPR, size_type_node,
   21840              :                                    build1 (NOP_EXPR, op1, error_mark_node));
   21841      5938432 :                     SIZEOF_EXPR_TYPE_P (r) = 1;
   21842              :                   }
   21843              :                 else
   21844       789066 :                   r = build_min (SIZEOF_EXPR, size_type_node, op1);
   21845      6727498 :                 TREE_SIDE_EFFECTS (r) = 0;
   21846      6727498 :                 TREE_READONLY (r) = 1;
   21847              :               }
   21848      6727606 :             SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
   21849      6727606 :             copy_warning (r, t);
   21850              :           }
   21851      7477097 :         RETURN (r);
   21852              :       }
   21853              : 
   21854            0 :     case AT_ENCODE_EXPR:
   21855            0 :       {
   21856            0 :         op1 = TREE_OPERAND (t, 0);
   21857            0 :         ++cp_unevaluated_operand;
   21858            0 :         ++c_inhibit_evaluation_warnings;
   21859            0 :         op1 = tsubst (op1, args, complain, in_decl);
   21860            0 :         --cp_unevaluated_operand;
   21861            0 :         --c_inhibit_evaluation_warnings;
   21862            0 :         RETURN (objc_build_encode_expr (op1));
   21863              :       }
   21864              : 
   21865      1200621 :     case NOEXCEPT_EXPR:
   21866      1200621 :       op1 = TREE_OPERAND (t, 0);
   21867      1200621 :       ++cp_unevaluated_operand;
   21868      1200621 :       ++c_inhibit_evaluation_warnings;
   21869      1200621 :       ++cp_noexcept_operand;
   21870      1200621 :       op1 = tsubst_expr (op1, args, complain, in_decl);
   21871      1200621 :       --cp_unevaluated_operand;
   21872      1200621 :       --c_inhibit_evaluation_warnings;
   21873      1200621 :       --cp_noexcept_operand;
   21874      1200621 :       RETURN (finish_noexcept_expr (op1, complain));
   21875              : 
   21876     24383446 :     case MODOP_EXPR:
   21877     24383446 :       {
   21878     24383446 :         warning_sentinel s(warn_div_by_zero);
   21879     24383446 :         tree lhs = RECUR (TREE_OPERAND (t, 0));
   21880     24383446 :         tree rhs = RECUR (TREE_OPERAND (t, 2));
   21881              : 
   21882     24383446 :         tree r = build_x_modify_expr
   21883     24383446 :           (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
   21884              :            templated_operator_saved_lookups (t),
   21885              :            complain|decltype_flag);
   21886              :         /* TREE_NO_WARNING must be set if either the expression was
   21887              :            parenthesized or it uses an operator such as >>= rather
   21888              :            than plain assignment.  In the former case, it was already
   21889              :            set and must be copied.  In the latter case,
   21890              :            build_x_modify_expr sets it and it must not be reset
   21891              :            here.  */
   21892     24383446 :         if (warning_suppressed_p (t, OPT_Wparentheses))
   21893        12954 :           suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
   21894              : 
   21895     24383446 :         RETURN (r);
   21896     24383446 :       }
   21897              : 
   21898      5683083 :     case ARROW_EXPR:
   21899      5683083 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21900              :                                                 args, complain, in_decl);
   21901              :       /* Remember that there was a reference to this entity.  */
   21902      5683083 :       if (DECL_P (op1)
   21903      5683083 :           && !mark_used (op1, complain) && !(complain & tf_error))
   21904            0 :         RETURN (error_mark_node);
   21905      5683083 :       RETURN (build_x_arrow (input_location, op1, complain));
   21906              : 
   21907       876708 :     case NEW_EXPR:
   21908       876708 :       {
   21909       876708 :         tree placement = RECUR (TREE_OPERAND (t, 0));
   21910       876708 :         tree init = RECUR (TREE_OPERAND (t, 3));
   21911       876708 :         vec<tree, va_gc> *placement_vec;
   21912       876708 :         vec<tree, va_gc> *init_vec;
   21913       876708 :         tree ret;
   21914       876708 :         location_t loc = EXPR_LOCATION (t);
   21915              : 
   21916       876708 :         if (placement == NULL_TREE)
   21917       266311 :           placement_vec = NULL;
   21918       610397 :         else if (placement == error_mark_node)
   21919            3 :           RETURN (error_mark_node);
   21920              :         else
   21921              :           {
   21922       610394 :             placement_vec = make_tree_vector ();
   21923      1220788 :             for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
   21924       610394 :               vec_safe_push (placement_vec, TREE_VALUE (placement));
   21925              :           }
   21926              : 
   21927              :         /* If there was an initializer in the original tree, but it
   21928              :            instantiated to an empty list, then we should pass a
   21929              :            non-NULL empty vector to tell build_new that it was an
   21930              :            empty initializer() rather than no initializer.  This can
   21931              :            only happen when the initializer is a pack expansion whose
   21932              :            parameter packs are of length zero.  */
   21933       876705 :         if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
   21934       256917 :           init_vec = NULL;
   21935       619788 :         else if (init == error_mark_node)
   21936            6 :           RETURN (error_mark_node);
   21937              :         else
   21938              :           {
   21939       619782 :             init_vec = make_tree_vector ();
   21940       619782 :             if (init == void_node)
   21941         9488 :               gcc_assert (init_vec != NULL);
   21942              :             else
   21943              :               {
   21944      1113093 :                 for (; init != NULL_TREE; init = TREE_CHAIN (init))
   21945       502799 :                   vec_safe_push (init_vec, TREE_VALUE (init));
   21946              :               }
   21947              :           }
   21948              : 
   21949              :         /* Avoid passing an enclosing decl to valid_array_size_p.  */
   21950       876699 :         in_decl = NULL_TREE;
   21951              : 
   21952       876699 :         tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
   21953       876699 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   21954       876699 :         ret = build_new (loc, &placement_vec, op1, op2,
   21955       876699 :                          &init_vec, NEW_EXPR_USE_GLOBAL (t),
   21956              :                          complain);
   21957              : 
   21958       876699 :         if (placement_vec != NULL)
   21959       876483 :           release_tree_vector (placement_vec);
   21960       876699 :         if (init_vec != NULL)
   21961       490571 :           release_tree_vector (init_vec);
   21962              : 
   21963       876708 :         RETURN (ret);
   21964              :       }
   21965              : 
   21966        49702 :     case DELETE_EXPR:
   21967        49702 :       {
   21968        49702 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21969        49702 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21970        49702 :         RETURN (delete_sanity (input_location, op0, op1,
   21971              :                                DELETE_EXPR_USE_VEC (t),
   21972              :                                DELETE_EXPR_USE_GLOBAL (t),
   21973              :                                complain));
   21974              :       }
   21975              : 
   21976       172414 :     case COMPOUND_EXPR:
   21977       172414 :       {
   21978       172414 :         tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args,
   21979              :                                 complain & ~tf_decltype, in_decl);
   21980       172414 :         RETURN (build_x_compound_expr (EXPR_LOCATION (t),
   21981              :                                        op0,
   21982              :                                        RECUR (TREE_OPERAND (t, 1)),
   21983              :                                        templated_operator_saved_lookups (t),
   21984              :                                        complain|decltype_flag));
   21985              :       }
   21986              : 
   21987    164716255 :     case CALL_EXPR:
   21988    164716255 :       {
   21989    164716255 :         tree function;
   21990    164716255 :         unsigned int nargs;
   21991    164716255 :         bool qualified_p;
   21992    164716255 :         bool koenig_p;
   21993    164716255 :         tree ret;
   21994              : 
   21995    164716255 :         function = CALL_EXPR_FN (t);
   21996              :         /* Internal function with no arguments.  */
   21997    164716255 :         if (function == NULL_TREE && call_expr_nargs (t) == 0)
   21998    164354952 :           RETURN (t);
   21999              : 
   22000              :         /* When we parsed the expression, we determined whether or
   22001              :            not Koenig lookup should be performed.  */
   22002    164277120 :         koenig_p = KOENIG_LOOKUP_P (t);
   22003    164277120 :         if (function == NULL_TREE)
   22004              :           {
   22005              :             koenig_p = false;
   22006              :             qualified_p = false;
   22007              :           }
   22008    164276511 :         else if (TREE_CODE (function) == SCOPE_REF)
   22009              :           {
   22010      5466974 :             qualified_p = true;
   22011      5466974 :             function = tsubst_qualified_id (function, args, complain, in_decl,
   22012              :                                             /*done=*/false,
   22013              :                                             /*address_p=*/false);
   22014              :           }
   22015    158809537 :         else if (CALL_EXPR_STATIC_CHAIN (t)
   22016           99 :                  && TREE_CODE (function) == FUNCTION_DECL
   22017    158809636 :                  && fndecl_built_in_p (function, BUILT_IN_CLASSIFY_TYPE))
   22018              :           {
   22019           99 :             tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain,
   22020              :                                 in_decl);
   22021           99 :             if (dependent_type_p (type))
   22022              :               {
   22023            0 :                 ret = build_vl_exp (CALL_EXPR, 4);
   22024            0 :                 CALL_EXPR_FN (ret) = function;
   22025            0 :                 CALL_EXPR_STATIC_CHAIN (ret) = type;
   22026            0 :                 CALL_EXPR_ARG (ret, 0)
   22027            0 :                   = build_min (SIZEOF_EXPR, size_type_node, type);
   22028            0 :                 TREE_TYPE (ret) = integer_type_node;
   22029              :               }
   22030              :             else
   22031           99 :               ret = build_int_cst (integer_type_node, type_to_class (type));
   22032           99 :             RETURN (ret);
   22033              :           }
   22034    158809438 :         else if (koenig_p
   22035    158809438 :                  && (identifier_p (function)
   22036     10399312 :                      || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22037      3061823 :                          && identifier_p (TREE_OPERAND (function, 0)))))
   22038              :           {
   22039              :             /* Do nothing; calling tsubst_expr on an identifier
   22040              :                would incorrectly perform unqualified lookup again.
   22041              : 
   22042              :                Note that we can also have an IDENTIFIER_NODE if the earlier
   22043              :                unqualified lookup found a dependent local extern declaration
   22044              :                (as per finish_call_expr); in that case koenig_p will be false
   22045              :                and we do want to do the lookup again to find the substituted
   22046              :                declaration.  */
   22047       179598 :             qualified_p = false;
   22048              : 
   22049       179598 :             if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
   22050         4704 :               function = tsubst_name (function, args, complain, in_decl);
   22051              :           }
   22052              :         else
   22053              :           {
   22054    158629840 :             if (TREE_CODE (function) == COMPONENT_REF)
   22055              :               {
   22056     30613454 :                 tree op = TREE_OPERAND (function, 1);
   22057              : 
   22058     30613454 :                 qualified_p = (TREE_CODE (op) == SCOPE_REF
   22059     30613454 :                                || (BASELINK_P (op)
   22060     17124093 :                                    && BASELINK_QUALIFIED_P (op)));
   22061              :               }
   22062              :             else
   22063              :               qualified_p = false;
   22064              : 
   22065    158629840 :             if (TREE_CODE (function) == ADDR_EXPR
   22066    158629840 :                 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
   22067              :               /* Avoid error about taking the address of a constructor.  */
   22068       143198 :               function = TREE_OPERAND (function, 0);
   22069              : 
   22070    158629840 :             function = tsubst_expr (function, args, complain, in_decl);
   22071              : 
   22072    158629840 :             if (BASELINK_P (function))
   22073      9642299 :               qualified_p = true;
   22074              :           }
   22075              : 
   22076    164277021 :         nargs = call_expr_nargs (t);
   22077    164277021 :         releasing_vec call_args;
   22078    164277021 :         tsubst_call_args (t, args, complain, in_decl, call_args);
   22079              : 
   22080              :         /* Stripped-down processing for a call in a thunk.  Specifically, in
   22081              :            the thunk template for a generic lambda.  */
   22082    164277018 :         if (call_from_lambda_thunk_p (t))
   22083              :           {
   22084              :             /* Now that we've expanded any packs, the number of call args
   22085              :                might be different.  */
   22086          105 :             unsigned int cargs = call_args->length ();
   22087          105 :             tree thisarg = NULL_TREE;
   22088          105 :             if (TREE_CODE (function) == COMPONENT_REF)
   22089              :               {
   22090          105 :                 thisarg = TREE_OPERAND (function, 0);
   22091          105 :                 if (TREE_CODE (thisarg) == INDIRECT_REF)
   22092          105 :                   thisarg = TREE_OPERAND (thisarg, 0);
   22093          105 :                 function = TREE_OPERAND (function, 1);
   22094          105 :                 if (TREE_CODE (function) == BASELINK)
   22095          105 :                   function = BASELINK_FUNCTIONS (function);
   22096              :               }
   22097              :             /* We aren't going to do normal overload resolution, so force the
   22098              :                template-id to resolve.  */
   22099          105 :             function = resolve_nondeduced_context (function, complain);
   22100          327 :             for (unsigned i = 0; i < cargs; ++i)
   22101              :               {
   22102              :                 /* In a thunk, pass through args directly, without any
   22103              :                    conversions.  */
   22104          222 :                 tree arg = (*call_args)[i];
   22105          222 :                 while (TREE_CODE (arg) != PARM_DECL)
   22106            0 :                   arg = TREE_OPERAND (arg, 0);
   22107          222 :                 (*call_args)[i] = arg;
   22108              :               }
   22109          105 :             if (thisarg)
   22110              :               {
   22111              :                 /* If there are no other args, just push 'this'.  */
   22112          105 :                 if (cargs == 0)
   22113            9 :                   vec_safe_push (call_args, thisarg);
   22114              :                 else
   22115              :                   {
   22116              :                     /* Otherwise, shift the other args over to make room.  */
   22117           96 :                     tree last = (*call_args)[cargs - 1];
   22118           96 :                     vec_safe_push (call_args, last);
   22119          222 :                     for (int i = cargs - 1; i > 0; --i)
   22120          126 :                       (*call_args)[i] = (*call_args)[i - 1];
   22121           96 :                     (*call_args)[0] = thisarg;
   22122              :                   }
   22123              :               }
   22124          105 :             ret = build_call_a (function, call_args->length (),
   22125              :                                 call_args->address ());
   22126              :             /* The thunk location is not interesting.  */
   22127          105 :             SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
   22128          105 :             CALL_FROM_THUNK_P (ret) = true;
   22129          105 :             if (CLASS_TYPE_P (TREE_TYPE (ret)))
   22130            9 :               CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
   22131              : 
   22132          105 :             RETURN (ret);
   22133              :           }
   22134              : 
   22135              :         /* We do not perform argument-dependent lookup if normal
   22136              :            lookup finds a non-function, in accordance with the
   22137              :            resolution of DR 218.  */
   22138    164276913 :         if (koenig_p
   22139     10574203 :             && ((is_overloaded_fn (function)
   22140              :                  /* If lookup found a member function, the Koenig lookup is
   22141              :                     not appropriate, even if an unqualified-name was used
   22142              :                     to denote the function.  */
   22143     10394586 :                  && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
   22144     10578907 :                 || identifier_p (function)
   22145              :                 /* C++20 P0846: Lookup found nothing.  */
   22146         4723 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22147         4704 :                     && identifier_p (TREE_OPERAND (function, 0))))
   22148              :             /* Only do this when substitution turns a dependent call
   22149              :                into a non-dependent call.  */
   22150     10574184 :             && type_dependent_expression_p_push (t)
   22151    172911268 :             && !any_type_dependent_arguments_p (call_args))
   22152      8520782 :           function = perform_koenig_lookup (function, call_args, tf_none);
   22153              : 
   22154    164276913 :         if (function != NULL_TREE
   22155       157318 :             && (identifier_p (function)
   22156    164118994 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22157     51156072 :                     && identifier_p (TREE_OPERAND (function, 0))
   22158          368 :                     && !any_dependent_template_arguments_p (TREE_OPERAND
   22159              :                                                             (function, 1))))
   22160    164434231 :             && !any_type_dependent_arguments_p (call_args))
   22161              :           {
   22162        55947 :             bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
   22163        55947 :             if (template_id_p)
   22164            6 :               function = TREE_OPERAND (function, 0);
   22165        55947 :             if (koenig_p && (complain & tf_warning_or_error))
   22166              :               {
   22167              :                 /* For backwards compatibility and good diagnostics, try
   22168              :                    the unqualified lookup again if we aren't in SFINAE
   22169              :                    context.  */
   22170           57 :                 tree unq = tsubst_expr (function, args, complain, in_decl);
   22171           57 :                 if (unq == error_mark_node)
   22172           33 :                   RETURN (error_mark_node);
   22173              : 
   22174           24 :                 if (unq != function)
   22175              :                   {
   22176           24 :                     auto_diagnostic_group d;
   22177           24 :                     char const *const msg
   22178              :                       = G_("%qD was not declared in this scope, "
   22179              :                            "and no declarations were found by "
   22180              :                            "argument-dependent lookup at the point "
   22181              :                            "of instantiation");
   22182              : 
   22183           24 :                     bool in_lambda = (current_class_type
   22184           36 :                                       && LAMBDA_TYPE_P (current_class_type));
   22185              :                     /* In a lambda fn, we have to be careful to not
   22186              :                        introduce new this captures.  Legacy code can't
   22187              :                        be using lambdas anyway, so it's ok to be
   22188              :                        stricter.  Be strict with C++20 template-id ADL too.
   22189              :                        And be strict if we're already failing anyway.  */
   22190           24 :                     bool strict = in_lambda || template_id_p || seen_error ();
   22191           24 :                     bool diag = true;
   22192           24 :                     if (strict)
   22193           15 :                       error_at (cp_expr_loc_or_input_loc (t),
   22194              :                                 msg, function);
   22195              :                     else
   22196            9 :                       diag = permerror (cp_expr_loc_or_input_loc (t),
   22197              :                                         msg, function);
   22198           24 :                     if (diag)
   22199              :                       {
   22200           24 :                         tree fn = unq;
   22201              : 
   22202           24 :                         if (INDIRECT_REF_P (fn))
   22203            3 :                           fn = TREE_OPERAND (fn, 0);
   22204           24 :                         if (is_overloaded_fn (fn))
   22205           21 :                           fn = get_first_fn (fn);
   22206              : 
   22207           24 :                         if (!DECL_P (fn))
   22208              :                           /* Can't say anything more.  */;
   22209           21 :                         else if (DECL_CLASS_SCOPE_P (fn))
   22210              :                           {
   22211            9 :                             location_t loc = cp_expr_loc_or_input_loc (t);
   22212            9 :                             inform (loc,
   22213              :                                     "declarations in dependent base %qT are "
   22214              :                                     "not found by unqualified lookup",
   22215            9 :                                     DECL_CLASS_CONTEXT (fn));
   22216            9 :                             if (current_class_ptr)
   22217            6 :                               inform (loc,
   22218              :                                       "use %<this->%D%> instead", function);
   22219              :                             else
   22220            3 :                               inform (loc,
   22221              :                                       "use %<%T::%D%> instead",
   22222              :                                       current_class_name, function);
   22223              :                           }
   22224              :                         else
   22225           12 :                           inform (DECL_SOURCE_LOCATION (fn),
   22226              :                                   "%qD declared here, later in the "
   22227              :                                   "translation unit", fn);
   22228           24 :                         if (strict)
   22229           15 :                           RETURN (error_mark_node);
   22230              :                       }
   22231              : 
   22232            9 :                     function = unq;
   22233           24 :                   }
   22234              :               }
   22235        55899 :             if (identifier_p (function))
   22236              :               {
   22237        55890 :                 if (complain & tf_error)
   22238            0 :                   unqualified_name_lookup_error (function);
   22239        55890 :                 RETURN (error_mark_node);
   22240              :               }
   22241              :           }
   22242              : 
   22243              :         /* Remember that there was a reference to this entity.  */
   22244    164220975 :         if (function != NULL_TREE
   22245    164220366 :             && DECL_P (function)
   22246    177945075 :             && !mark_used (function, complain) && !(complain & tf_error))
   22247            3 :           RETURN (error_mark_node);
   22248              : 
   22249    164220972 :         if (!maybe_fold_fn_template_args (function, complain))
   22250       347797 :           return error_mark_node;
   22251              : 
   22252              :         /* Put back tf_decltype for the actual call.  */
   22253    163873175 :         complain |= decltype_flag;
   22254              : 
   22255    163873175 :         if (function == NULL_TREE)
   22256          609 :           switch (CALL_EXPR_IFN (t))
   22257              :             {
   22258           45 :             case IFN_LAUNDER:
   22259           45 :               gcc_assert (nargs == 1);
   22260           45 :               if (vec_safe_length (call_args) != 1)
   22261              :                 {
   22262            6 :                   error_at (cp_expr_loc_or_input_loc (t),
   22263              :                             "wrong number of arguments to "
   22264              :                             "%<__builtin_launder%>");
   22265            6 :                   ret = error_mark_node;
   22266              :                 }
   22267              :               else
   22268           39 :                 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
   22269           39 :                                               (*call_args)[0], complain);
   22270              :               break;
   22271              : 
   22272          125 :             case IFN_VEC_CONVERT:
   22273          125 :               gcc_assert (nargs == 1);
   22274          125 :               if (vec_safe_length (call_args) != 1)
   22275              :                 {
   22276            0 :                   error_at (cp_expr_loc_or_input_loc (t),
   22277              :                             "wrong number of arguments to "
   22278              :                             "%<__builtin_convertvector%>");
   22279            0 :                   ret = error_mark_node;
   22280            0 :                   break;
   22281              :                 }
   22282          125 :               ret = cp_build_vec_convert ((*call_args)[0], input_location,
   22283          125 :                                           tsubst (TREE_TYPE (t), args,
   22284              :                                                   complain, in_decl),
   22285              :                                           complain);
   22286          125 :               if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
   22287           15 :                 RETURN (ret);
   22288              :               break;
   22289              : 
   22290          344 :             case IFN_SHUFFLEVECTOR:
   22291          344 :               {
   22292          344 :                 ret = build_x_shufflevector (input_location, call_args,
   22293              :                                              complain);
   22294          344 :                 if (ret != error_mark_node)
   22295          341 :                   RETURN (ret);
   22296              :                 break;
   22297              :               }
   22298              : 
   22299           83 :             case IFN_ASSUME:
   22300           83 :               gcc_assert (nargs == 1);
   22301           83 :               if (vec_safe_length (call_args) != 1)
   22302              :                 {
   22303            0 :                   error_at (cp_expr_loc_or_input_loc (t),
   22304              :                             "wrong number of arguments to "
   22305              :                             "%<assume%> attribute");
   22306            0 :                   ret = error_mark_node;
   22307              :                 }
   22308              :               else
   22309              :                 {
   22310           83 :                   tree &arg = (*call_args)[0];
   22311           83 :                   if (!type_dependent_expression_p (arg))
   22312           83 :                     arg = contextual_conv_bool (arg, tf_warning_or_error);
   22313           83 :                   if (error_operand_p (arg))
   22314              :                     {
   22315            6 :                       ret = error_mark_node;
   22316            6 :                       break;
   22317              :                     }
   22318           77 :                   ret = build_assume_call (EXPR_LOCATION (t), arg);
   22319           77 :                   RETURN (ret);
   22320              :                 }
   22321            0 :               break;
   22322              : 
   22323           12 :             case IFN_GOMP_DISPATCH:
   22324           24 :               ret = build_call_expr_internal_loc (EXPR_LOCATION (t),
   22325              :                                                   IFN_GOMP_DISPATCH,
   22326           12 :                                                   TREE_TYPE (call_args[0]), 1,
   22327           12 :                                                   call_args[0]);
   22328           12 :               RETURN (ret);
   22329              :               break;
   22330              : 
   22331            0 :             default:
   22332              :               /* Unsupported internal function with arguments.  */
   22333            0 :               gcc_unreachable ();
   22334              :             }
   22335    163872566 :         else if (TREE_CODE (function) == OFFSET_REF
   22336              :                  || TREE_CODE (function) == DOTSTAR_EXPR
   22337              :                  || TREE_CODE (function) == MEMBER_REF)
   22338        82394 :           ret = build_offset_ref_call_from_tree (function, &call_args,
   22339              :                                                  complain);
   22340    163790172 :         else if (concept_check_p (function))
   22341              :           /* Calls to concepts should have been previously diagnosed.  */
   22342            0 :           gcc_assert (false);
   22343              :         else
   22344    163790172 :           ret = finish_call_expr (function, &call_args,
   22345              :                                   /*disallow_virtual=*/qualified_p,
   22346              :                                   koenig_p,
   22347              :                                   complain);
   22348              : 
   22349    163859227 :         if (ret != error_mark_node)
   22350              :           {
   22351    163387627 :             bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
   22352    163387627 :             bool ord = CALL_EXPR_ORDERED_ARGS (t);
   22353    163387627 :             bool rev = CALL_EXPR_REVERSE_ARGS (t);
   22354    163387627 :             bool mtc = false;
   22355    163387627 :             if (TREE_CODE (t) == CALL_EXPR)
   22356    163387627 :               mtc = CALL_EXPR_MUST_TAIL_CALL (t);
   22357    163387627 :             if (op || ord || rev || mtc)
   22358       721740 :               if (tree call = extract_call_expr (ret))
   22359              :                 {
   22360       721740 :                   CALL_EXPR_OPERATOR_SYNTAX (call) = op;
   22361       721740 :                   CALL_EXPR_ORDERED_ARGS (call) = ord;
   22362       721740 :                   CALL_EXPR_REVERSE_ARGS (call) = rev;
   22363       721740 :                   if (TREE_CODE (call) == CALL_EXPR)
   22364       721464 :                     CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
   22365          276 :                   else if (TREE_CODE (call) == AGGR_INIT_EXPR)
   22366          276 :                     AGGR_INIT_EXPR_MUST_TAIL (call) = mtc;
   22367              :                 }
   22368    163387627 :             if (CALL_FROM_NEW_OR_DELETE_P (t))
   22369              :               {
   22370       191720 :                 tree call = extract_call_expr (ret);
   22371       191720 :                 tree fn = cp_get_callee_fndecl_nofold (call);
   22372       383440 :                 if (fn ? !DECL_IS_REPLACEABLE_OPERATOR (fn)
   22373            0 :                        : !processing_template_decl)
   22374              :                   {
   22375            9 :                     auto_diagnostic_group d;
   22376            9 :                     location_t loc = cp_expr_loc_or_input_loc (t);
   22377            9 :                     if (!fn || IDENTIFIER_NEW_OP_P (DECL_NAME (fn)))
   22378            6 :                       error_at (loc, "call to %<__builtin_operator_new%> "
   22379              :                                      "does not select replaceable global "
   22380              :                                      "allocation function");
   22381              :                     else
   22382            3 :                       error_at (loc, "call to %<__builtin_operator_delete%> "
   22383              :                                      "does not select replaceable global "
   22384              :                                      "deallocation function");
   22385            9 :                     if (fn)
   22386            9 :                       inform (DECL_SOURCE_LOCATION (fn),
   22387              :                               "selected function declared here");
   22388            9 :                   }
   22389       191711 :                 else if (call && TREE_CODE (call) == CALL_EXPR)
   22390       191711 :                   CALL_FROM_NEW_OR_DELETE_P (call) = 1;
   22391              :               }
   22392    163387627 :             if (warning_suppressed_p (t, OPT_Wpessimizing_move))
   22393              :               /* This also suppresses -Wredundant-move.  */
   22394    148738294 :               suppress_warning (ret, OPT_Wpessimizing_move);
   22395              :           }
   22396              : 
   22397    163915718 :         RETURN (ret);
   22398    164263515 :       }
   22399              : 
   22400      5501515 :     case COND_EXPR:
   22401      5501515 :       {
   22402      5501515 :         tree cond = RECUR (TREE_OPERAND (t, 0));
   22403      5501515 :         cond = mark_rvalue_use (cond);
   22404      5501515 :         tree folded_cond = fold_non_dependent_expr (cond, complain);
   22405      5501515 :         tree exp1, exp2;
   22406              : 
   22407      5501515 :         if (TREE_CODE (folded_cond) == INTEGER_CST)
   22408              :           {
   22409      4036637 :             if (integer_zerop (folded_cond))
   22410              :               {
   22411      3371256 :                 ++c_inhibit_evaluation_warnings;
   22412      3371256 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   22413      3371256 :                 --c_inhibit_evaluation_warnings;
   22414      3371256 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   22415              :               }
   22416              :             else
   22417              :               {
   22418       665381 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   22419       665381 :                 ++c_inhibit_evaluation_warnings;
   22420       665381 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   22421       665381 :                 --c_inhibit_evaluation_warnings;
   22422              :               }
   22423              :             cond = folded_cond;
   22424              :           }
   22425              :         else
   22426              :           {
   22427      1464878 :             exp1 = RECUR (TREE_OPERAND (t, 1));
   22428      1464878 :             exp2 = RECUR (TREE_OPERAND (t, 2));
   22429              :           }
   22430              : 
   22431      5501515 :         warning_sentinel s(warn_duplicated_branches);
   22432      5501515 :         RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
   22433              :                                          cond, exp1, exp2, complain));
   22434      5501515 :       }
   22435              : 
   22436           18 :     case PSEUDO_DTOR_EXPR:
   22437           18 :       {
   22438           18 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22439           18 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   22440           18 :         tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
   22441           18 :         RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
   22442              :                                                input_location, complain));
   22443              :       }
   22444              : 
   22445     35413738 :     case TREE_LIST:
   22446     35413738 :       RETURN (tsubst_tree_list (t, args, complain, in_decl));
   22447              : 
   22448     55366589 :     case COMPONENT_REF:
   22449     55366589 :       {
   22450     55366589 :         tree object;
   22451     55366589 :         tree object_type;
   22452     55366589 :         tree member;
   22453     55366589 :         tree r;
   22454              : 
   22455     55366589 :         object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   22456              :                                                      args, complain, in_decl);
   22457              :         /* Remember that there was a reference to this entity.  */
   22458     55366589 :         if (DECL_P (object)
   22459     55366589 :             && !mark_used (object, complain) && !(complain & tf_error))
   22460            0 :           RETURN (error_mark_node);
   22461     55366589 :         object_type = TREE_TYPE (object);
   22462              : 
   22463     55366589 :         member = TREE_OPERAND (t, 1);
   22464     55366589 :         const bool splice_p = dependent_splice_p (member);
   22465     55366589 :         if (BASELINK_P (member))
   22466     17124146 :           member = tsubst_baselink (member,
   22467     17124146 :                                     non_reference (TREE_TYPE (object)),
   22468              :                                     args, complain, in_decl);
   22469              :         else
   22470     38242443 :           member = tsubst_name (member, args, complain, in_decl);
   22471     55366589 :         if (member == error_mark_node)
   22472           20 :           RETURN (error_mark_node);
   22473              : 
   22474     54881795 :         if (object_type && TYPE_PTRMEMFUNC_P (object_type)
   22475     55366593 :             && TREE_CODE (member) == FIELD_DECL)
   22476              :           {
   22477            3 :             r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
   22478            3 :             RETURN (r);
   22479              :           }
   22480     55366566 :         else if (TREE_CODE (member) == FIELD_DECL)
   22481              :           {
   22482              :             /* Assume access of this FIELD_DECL has already been checked; we
   22483              :                don't recheck it to avoid bogus access errors when substituting
   22484              :                a reduced constant initializer (97740).  */
   22485     14178565 :             gcc_checking_assert (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
   22486              :                                  || dependent_splice_p (TREE_OPERAND (t, 1)));
   22487     14178565 :             push_deferring_access_checks (dk_deferred);
   22488     14178565 :             r = finish_non_static_data_member (member, object, NULL_TREE,
   22489              :                                                complain);
   22490     14178565 :             pop_deferring_access_checks ();
   22491     14178565 :             if (REF_PARENTHESIZED_P (t))
   22492         2785 :               r = force_paren_expr (r);
   22493     14178565 :             RETURN (r);
   22494              :           }
   22495     41188001 :         else if (type_dependent_expression_p (object))
   22496              :           /* We can't do much here.  */;
   22497     40532443 :         else if (!CLASS_TYPE_P (object_type))
   22498              :           {
   22499       422249 :             if (scalarish_type_p (object_type))
   22500              :               {
   22501       260041 :                 tree s = NULL_TREE;
   22502       260041 :                 tree dtor = member;
   22503              : 
   22504       260041 :                 if (TREE_CODE (dtor) == SCOPE_REF)
   22505              :                   {
   22506           15 :                     s = TREE_OPERAND (dtor, 0);
   22507           15 :                     dtor = TREE_OPERAND (dtor, 1);
   22508              :                   }
   22509       260041 :                 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
   22510              :                   {
   22511       164371 :                     dtor = TREE_OPERAND (dtor, 0);
   22512       164371 :                     if (TYPE_P (dtor))
   22513       164371 :                       RETURN (finish_pseudo_destructor_expr
   22514              :                               (object, s, dtor, input_location, complain));
   22515              :                   }
   22516              :               }
   22517              :           }
   22518     40110194 :         else if (TREE_CODE (member) == SCOPE_REF
   22519          753 :                  && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR
   22520     40110212 :                  && identifier_p (TREE_OPERAND (TREE_OPERAND (member, 1), 0)))
   22521              :           {
   22522              :             /* Lookup the template functions now that we know what the
   22523              :                scope is.  */
   22524           15 :             tree scope = TREE_OPERAND (member, 0);
   22525           15 :             tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
   22526           15 :             tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
   22527           15 :             member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
   22528              :                                             /*complain=*/false);
   22529           15 :             if (BASELINK_P (member))
   22530              :               {
   22531           30 :                 BASELINK_FUNCTIONS (member)
   22532           15 :                   = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
   22533              :                               args);
   22534           15 :                 member = (adjust_result_of_qualified_name_lookup
   22535           15 :                           (member, BINFO_TYPE (BASELINK_BINFO (member)),
   22536              :                            object_type));
   22537              :               }
   22538              :             else
   22539              :               {
   22540            0 :                 qualified_name_lookup_error (scope, tmpl, member,
   22541              :                                              input_location);
   22542           15 :                 RETURN (error_mark_node);
   22543              :               }
   22544              :           }
   22545     40110179 :         else if (TREE_CODE (member) == SCOPE_REF
   22546          738 :                  && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
   22547     40110182 :                  && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
   22548              :           {
   22549            3 :             if (complain & tf_error)
   22550              :               {
   22551            3 :                 if (TYPE_P (TREE_OPERAND (member, 0)))
   22552            3 :                   error ("%qT is not a class or namespace",
   22553            3 :                          TREE_OPERAND (member, 0));
   22554              :                 else
   22555            0 :                   error ("%qD is not a class or namespace",
   22556            0 :                          TREE_OPERAND (member, 0));
   22557              :               }
   22558            3 :             RETURN (error_mark_node);
   22559              :           }
   22560     40110176 :         else if (splice_p && BASELINK_P (member))
   22561              :           /* We need to call adjust_result_of_qualified_name_lookup when
   22562              :              we have obj->[:^^T::fn:], but we don't set BASELINK_QUALIFIED_P
   22563              :              so that we still get virtual function binding.  */
   22564            6 :           member = (adjust_result_of_qualified_name_lookup
   22565            6 :                     (member, NULL_TREE, object_type));
   22566              : 
   22567     41023627 :         r = finish_class_member_access_expr (object, member,
   22568              :                                              /*template_p=*/false,
   22569              :                                              complain,
   22570     41023627 :                                              COMPONENT_REF_SPLICE_P (t));
   22571     41023627 :         if (REF_PARENTHESIZED_P (t))
   22572         1063 :           r = force_paren_expr (r);
   22573     41023627 :         RETURN (r);
   22574              :       }
   22575              : 
   22576        22855 :     case THROW_EXPR:
   22577        22855 :       RETURN (build_throw
   22578              :        (input_location, RECUR (TREE_OPERAND (t, 0)), complain));
   22579              : 
   22580      6577298 :     case CONSTRUCTOR:
   22581      6577298 :       {
   22582      6577298 :         vec<constructor_elt, va_gc> *n;
   22583      6577298 :         constructor_elt *ce;
   22584      6577298 :         unsigned HOST_WIDE_INT idx;
   22585      6577298 :         bool process_index_p;
   22586      6577298 :         int newlen;
   22587      6577298 :         bool need_copy_p = false;
   22588      6577298 :         tree r;
   22589              : 
   22590      6577298 :         tsubst_flags_t tcomplain = complain;
   22591      6577298 :         if (COMPOUND_LITERAL_P (t))
   22592      3434867 :           tcomplain |= tf_tst_ok;
   22593      6577298 :         tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   22594      6577298 :         if (type == error_mark_node)
   22595           20 :           RETURN (error_mark_node);
   22596              : 
   22597              :         /* We do not want to process the index of aggregate
   22598              :            initializers as they are identifier nodes which will be
   22599              :            looked up by digest_init.  */
   22600      6577278 :         process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
   22601              : 
   22602      6577278 :         if (null_member_pointer_value_p (t))
   22603              :           {
   22604           20 :             gcc_assert (same_type_p (type, TREE_TYPE (t)));
   22605           20 :             RETURN (t);
   22606              :           }
   22607              : 
   22608      6577258 :         n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
   22609      6577258 :         newlen = vec_safe_length (n);
   22610     10144670 :         FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
   22611              :           {
   22612      3567412 :             if (ce->index && process_index_p
   22613              :                 /* An identifier index is looked up in the type
   22614              :                    being initialized, not the current scope.  */
   22615          492 :                 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
   22616          459 :               ce->index = RECUR (ce->index);
   22617              : 
   22618      3567412 :             if (PACK_EXPANSION_P (ce->value))
   22619              :               {
   22620              :                 /* Substitute into the pack expansion.  */
   22621        12747 :                 ce->value = tsubst_pack_expansion (ce->value, args, complain,
   22622              :                                                   in_decl);
   22623              : 
   22624        12747 :                 if (ce->value == error_mark_node
   22625        12744 :                     || PACK_EXPANSION_P (ce->value))
   22626              :                   ;
   22627        12002 :                 else if (TREE_VEC_LENGTH (ce->value) == 1)
   22628              :                   /* Just move the argument into place.  */
   22629         3389 :                   ce->value = TREE_VEC_ELT (ce->value, 0);
   22630              :                 else
   22631              :                   {
   22632              :                     /* Update the length of the final CONSTRUCTOR
   22633              :                        arguments vector, and note that we will need to
   22634              :                        copy.*/
   22635         8613 :                     newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
   22636         8613 :                     need_copy_p = true;
   22637              :                   }
   22638              :               }
   22639              :             else
   22640      3554665 :               ce->value = RECUR (ce->value);
   22641              :           }
   22642              : 
   22643      6577258 :         if (need_copy_p)
   22644              :           {
   22645         8591 :             vec<constructor_elt, va_gc> *old_n = n;
   22646              : 
   22647         8591 :             vec_alloc (n, newlen);
   22648        17487 :             FOR_EACH_VEC_ELT (*old_n, idx, ce)
   22649              :               {
   22650         8896 :                 if (TREE_CODE (ce->value) == TREE_VEC)
   22651              :                   {
   22652         8613 :                     int i, len = TREE_VEC_LENGTH (ce->value);
   22653        40914 :                     for (i = 0; i < len; ++i)
   22654        32301 :                       CONSTRUCTOR_APPEND_ELT (n, 0,
   22655              :                                               TREE_VEC_ELT (ce->value, i));
   22656              :                   }
   22657              :                 else
   22658         9179 :                   CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
   22659              :               }
   22660              :           }
   22661              : 
   22662      6577258 :         r = build_constructor (init_list_type_node, n);
   22663      6577258 :         CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
   22664     13154516 :         CONSTRUCTOR_IS_DESIGNATED_INIT (r)
   22665      6577258 :           = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
   22666              : 
   22667      6577258 :         if (TREE_HAS_CONSTRUCTOR (t))
   22668              :           {
   22669      3434847 :             fcl_t cl = fcl_functional;
   22670      3434847 :             if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
   22671          372 :               cl = fcl_c99;
   22672      3434847 :             RETURN (finish_compound_literal (type, r, complain, cl));
   22673              :           }
   22674              : 
   22675      3142411 :         TREE_TYPE (r) = type;
   22676      6577298 :         RETURN (r);
   22677              :       }
   22678              : 
   22679        71851 :     case TYPEID_EXPR:
   22680        71851 :       {
   22681        71851 :         tree operand_0 = TREE_OPERAND (t, 0);
   22682        71851 :         if (TYPE_P (operand_0))
   22683              :           {
   22684        71436 :             operand_0 = tsubst (operand_0, args, complain, in_decl);
   22685        71436 :             RETURN (get_typeid (operand_0, complain));
   22686              :           }
   22687              :         else
   22688              :           {
   22689          415 :             operand_0 = RECUR (operand_0);
   22690          415 :             RETURN (build_typeid (operand_0, complain));
   22691              :           }
   22692              :       }
   22693              : 
   22694    242818177 :     case FUNCTION_DECL:
   22695    242818177 :     case PARM_DECL:
   22696    242818177 :     case VAR_DECL:
   22697    242818177 :       if (!args)
   22698      2506917 :         RETURN (t);
   22699    240311260 :       tree r;
   22700    135718821 :       if (VAR_OR_FUNCTION_DECL_P (t)
   22701    250829558 :           && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   22702      8905217 :         r = tsubst_decl (t, args, complain);
   22703    231406043 :       else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
   22704              :         {
   22705              :           /* Local specialization will usually have been created when
   22706              :              we instantiated the DECL_EXPR_DECL. */
   22707          358 :           r = retrieve_local_specialization (t);
   22708          358 :           if (!r)
   22709              :             {
   22710              :               /* We're in a generic lambda referencing a local extern
   22711              :                  from an outer block-scope of a non-template.  */
   22712           18 :               gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
   22713              :               r = t;
   22714              :             }
   22715              :         }
   22716    231405685 :       else if (local_variable_p (t)
   22717    231405685 :                && ((r = retrieve_local_specialization (t))
   22718      2492188 :                    || TREE_CODE (t) == PARM_DECL
   22719        38318 :                    || uses_template_parms (DECL_CONTEXT (t))))
   22720              :         {
   22721    218069734 :           if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
   22722              :             {
   22723              :               /* We get here for a use of 'this' in an NSDMI.  */
   22724      2793901 :               if (DECL_NAME (t) == this_identifier && current_class_ptr
   22725      3133673 :                   && !LAMBDA_TYPE_P (TREE_TYPE (TREE_TYPE (current_class_ptr))))
   22726       339903 :                 RETURN (current_class_ptr);
   22727              : 
   22728              :               /* Parameters of non-templates map to themselves (e.g. in
   22729              :                  expansion statement body).  */
   22730      2113967 :               if (DECL_CONTEXT (t) && !uses_template_parms (DECL_CONTEXT (t)))
   22731           31 :                 RETURN (t);
   22732              : 
   22733              :               /* This can happen for a parameter name used later in a function
   22734              :                  declaration (such as in a late-specified return type).  Just
   22735              :                  make a dummy decl, since it's only used for its type.  */
   22736      2113936 :               gcc_assert (cp_unevaluated_operand);
   22737      2113936 :               r = tsubst_decl (t, args, complain);
   22738              :               /* Give it the template pattern as its context; its true context
   22739              :                  hasn't been instantiated yet and this is good enough for
   22740              :                  mangling.  */
   22741      2113936 :               DECL_CONTEXT (r) = DECL_CONTEXT (t);
   22742              :             }
   22743          659 :           else if (r == NULL_TREE)
   22744              :             {
   22745              :               /* First try name lookup to find the instantiation.  */
   22746          659 :               if (DECL_NAME (t))
   22747          653 :                 r = lookup_name (DECL_NAME (t));
   22748          659 :               if (r)
   22749              :                 {
   22750          653 :                   if (!VAR_P (r))
   22751              :                     {
   22752              :                       /* During error-recovery we may find a non-variable,
   22753              :                          even an OVERLOAD: just bail out and avoid ICEs and
   22754              :                          duplicate diagnostics (c++/62207).  */
   22755            3 :                       gcc_assert (seen_error ());
   22756            3 :                       RETURN (error_mark_node);
   22757              :                     }
   22758          650 :                   if (!is_capture_proxy (r))
   22759              :                     {
   22760              :                       /* Make sure the one we found is the one we want.  */
   22761          628 :                       tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
   22762          628 :                       if (ctx != DECL_CONTEXT (r))
   22763              :                         r = NULL_TREE;
   22764              :                     }
   22765              :                 }
   22766              : 
   22767              :               if (r)
   22768              :                 /* OK */;
   22769              :               else
   22770              :                 {
   22771              :                   /* This can happen for a variable used in a
   22772              :                      late-specified return type of a local lambda, or for a
   22773              :                      local static or constant.  Building a new VAR_DECL
   22774              :                      should be OK in all those cases.  */
   22775            6 :                   r = tsubst_decl (t, args, complain);
   22776            6 :                   if (local_specializations)
   22777              :                     /* Avoid infinite recursion (79640).  */
   22778            6 :                     register_local_specialization (r, t);
   22779            6 :                   if (decl_maybe_constant_var_p (r))
   22780              :                     {
   22781              :                       /* We can't call cp_finish_decl, so handle the
   22782              :                          initializer by hand.  */
   22783            0 :                       tree init = tsubst_init (DECL_INITIAL (t), r, args,
   22784              :                                                complain, in_decl);
   22785            0 :                       if (!processing_template_decl)
   22786            0 :                         init = maybe_constant_init (init);
   22787            0 :                       if (processing_template_decl
   22788            0 :                           ? potential_constant_expression (init)
   22789            0 :                           : reduced_constant_expression_p (init))
   22790            0 :                         DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
   22791            0 :                           = TREE_CONSTANT (r) = true;
   22792            0 :                       DECL_INITIAL (r) = init;
   22793            0 :                       if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
   22794            0 :                         TREE_TYPE (r)
   22795            0 :                           = do_auto_deduction (TREE_TYPE (r), init, auto_node,
   22796              :                                                complain, adc_variable_type);
   22797              :                     }
   22798            6 :                   gcc_assert (cp_unevaluated_operand
   22799              :                               || TREE_STATIC (r)
   22800              :                               || decl_constant_var_p (r)
   22801              :                               || seen_error ());
   22802            6 :                   if (!processing_template_decl
   22803            6 :                       && !TREE_STATIC (r))
   22804            6 :                     r = process_outer_var_ref (r, complain);
   22805              :                 }
   22806              :               /* Remember this for subsequent uses.  */
   22807          656 :               if (local_specializations)
   22808          656 :                 register_local_specialization (r, t);
   22809              :             }
   22810    217729797 :           if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   22811       563658 :             r = argument_pack_select_arg (r);
   22812              :         }
   22813              :       else
   22814              :         r = t;
   22815    239971323 :       if (!mark_used (r, complain))
   22816           24 :         RETURN (error_mark_node);
   22817              : 
   22818    239971296 :       if (!no_name_lookup_flag
   22819    235203813 :           && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL))
   22820              :         {
   22821              :           /* ??? We're doing a subset of finish_id_expression here.  */
   22822    224685578 :           if (tree wrap = maybe_get_tls_wrapper_call (r))
   22823              :             /* Replace an evaluated use of the thread_local variable with
   22824              :                a call to its wrapper.  */
   22825              :             r = wrap;
   22826    224685452 :           else if (outer_automatic_var_p (r))
   22827      1971314 :             r = process_outer_var_ref (r, complain);
   22828              : 
   22829    224685578 :           if (!TYPE_REF_P (TREE_TYPE (t)))
   22830              :             /* If the original type was a reference, we'll be wrapped in
   22831              :                the appropriate INDIRECT_REF.  */
   22832    183686685 :             r = convert_from_reference (r);
   22833              :         }
   22834    239971296 :       RETURN (r);
   22835              : 
   22836      8727175 :     case CONST_DECL:
   22837      8727175 :       {
   22838      8727175 :         tree enum_type;
   22839      8727175 :         tree v;
   22840              : 
   22841      8727175 :         if (DECL_TEMPLATE_PARM_P (t))
   22842           15 :           RETURN (RECUR (DECL_INITIAL (t)));
   22843      8727160 :         if (!args || !uses_template_parms (DECL_CONTEXT (t)))
   22844      8255125 :           RETURN (t);
   22845              : 
   22846              :         /* Unfortunately, we cannot just call lookup_name here.
   22847              :            Consider:
   22848              : 
   22849              :              template <int I> int f() {
   22850              :              enum E { a = I };
   22851              :              struct S { void g() { E e = a; } };
   22852              :              };
   22853              : 
   22854              :            When we instantiate f<7>::S::g(), say, lookup_name is not
   22855              :            clever enough to find f<7>::a.  */
   22856       472035 :         enum_type = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
   22857              : 
   22858       472035 :         for (v = TYPE_VALUES (enum_type);
   22859       584357 :              v != NULL_TREE;
   22860       112322 :              v = TREE_CHAIN (v))
   22861       584357 :           if (TREE_PURPOSE (v) == DECL_NAME (t))
   22862       584357 :             RETURN (TREE_VALUE (v));
   22863              : 
   22864              :           /* We didn't find the name.  That should never happen; if
   22865              :              name-lookup found it during preliminary parsing, we
   22866              :              should find it again here during instantiation.  */
   22867            0 :         gcc_unreachable ();
   22868     17743172 :         RETURN (t);
   22869              :       }
   22870              : 
   22871     17743172 :     case FIELD_DECL:
   22872     17743172 :       if (DECL_CONTEXT (t))
   22873              :         {
   22874     17743172 :           tree ctx;
   22875              : 
   22876     17743172 :           ctx = tsubst_entering_scope (DECL_CONTEXT (t), args,
   22877              :                                        complain, in_decl);
   22878     17743172 :           if (ctx != DECL_CONTEXT (t))
   22879              :             {
   22880     16250557 :               tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
   22881     16250557 :               if (!r)
   22882              :                 {
   22883            0 :                   if (complain & tf_error)
   22884            0 :                     error ("using invalid field %qD", t);
   22885            0 :                   RETURN (error_mark_node);
   22886              :                 }
   22887     16250557 :               RETURN (r);
   22888              :             }
   22889              :         }
   22890      1492615 :       RETURN (t);
   22891              : 
   22892     96443052 :     case OVERLOAD:
   22893     96443052 :       if (modules_p ())
   22894       854831 :         for (tree ovl : lkp_range (t))
   22895       389289 :           if (instantiating_tu_local_entity (ovl))
   22896       389289 :             RETURN (error_mark_node);
   22897     96443040 :       RETURN (t);
   22898              : 
   22899     32639728 :     case TEMPLATE_DECL:
   22900     32639728 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   22901         3132 :         RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
   22902              :                         args, complain, in_decl));
   22903     32636596 :       else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
   22904            0 :         RETURN (tsubst (t, args, complain, in_decl));
   22905     65273192 :       else if (DECL_CLASS_SCOPE_P (t)
   22906     32943289 :                && uses_template_parms (DECL_CONTEXT (t)))
   22907              :         {
   22908              :           /* Template template argument like the following example need
   22909              :              special treatment:
   22910              : 
   22911              :                template <template <class> class TT> struct C {};
   22912              :                template <class T> struct D {
   22913              :                  template <class U> struct E {};
   22914              :                  C<E> c;                          // #1
   22915              :                };
   22916              :                D<int> d;                          // #2
   22917              : 
   22918              :              We are processing the template argument `E' in #1 for
   22919              :              the template instantiation #2.  Originally, `E' is a
   22920              :              TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
   22921              :              have to substitute this with one having context `D<int>'.  */
   22922              : 
   22923       232507 :           tree context = tsubst_entering_scope (DECL_CONTEXT (t), args,
   22924              :                                                 complain, in_decl);
   22925       232507 :           RETURN (lookup_field (context, DECL_NAME(t), 0, false));
   22926              :         }
   22927              :       else
   22928              :         /* Ordinary template template argument.  */
   22929     32404089 :         RETURN (t);
   22930              : 
   22931    215945803 :     case TEMPLATE_PARM_INDEX:
   22932    215945803 :     case TYPE_DECL:
   22933    215945803 :       RETURN (tsubst (t, args, complain, in_decl));
   22934              : 
   22935            0 :     case CLEANUP_POINT_EXPR:
   22936              :       /* We shouldn't have built any of these during initial template
   22937              :          generation.  Instead, they should be built during instantiation
   22938              :          in response to the saved STMT_IS_FULL_EXPR_P setting.  */
   22939            0 :       gcc_unreachable ();
   22940              : 
   22941            0 :     case TARGET_EXPR:
   22942              :       /* TARGET_EXPR represents temporary objects and should not appear in
   22943              :          templated trees.  */
   22944            0 :       gcc_unreachable ();
   22945              : 
   22946          138 :     case OFFSET_REF:
   22947          138 :       {
   22948              :         /* We should only get here for an OFFSET_REF like A::m; a .* in a
   22949              :            template is represented as a DOTSTAR_EXPR.  */
   22950          138 :         gcc_checking_assert
   22951              :           (same_type_p (TREE_TYPE (t), TREE_TYPE (TREE_OPERAND (t, 1))));
   22952          138 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22953          138 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   22954          138 :         tree type = TREE_TYPE (op1);
   22955          138 :         r = build2 (OFFSET_REF, type, op0, op1);
   22956          138 :         PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
   22957          138 :         if (!mark_used (op1, complain)
   22958          138 :             && !(complain & tf_error))
   22959            0 :           RETURN (error_mark_node);
   22960          138 :         RETURN (r);
   22961              :       }
   22962              : 
   22963        10806 :     case PACK_INDEX_EXPR:
   22964        10806 :       RETURN (tsubst_pack_index (t, args, complain, in_decl));
   22965              : 
   22966            0 :     case EXPR_PACK_EXPANSION:
   22967            0 :       error ("invalid use of pack expansion expression");
   22968            0 :       RETURN (error_mark_node);
   22969              : 
   22970            0 :     case NONTYPE_ARGUMENT_PACK:
   22971            0 :       error ("use %<...%> to expand argument pack");
   22972            0 :       RETURN (error_mark_node);
   22973              : 
   22974        40844 :     case VOID_CST:
   22975        40844 :       gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
   22976        40844 :       RETURN (t);
   22977              : 
   22978    157536645 :     case INTEGER_CST:
   22979    157536645 :     case REAL_CST:
   22980    157536645 :     case COMPLEX_CST:
   22981    157536645 :     case VECTOR_CST:
   22982    157536645 :       {
   22983              :         /* Instantiate any typedefs in the type.  */
   22984    157536645 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   22985    157536645 :         r = fold_convert (type, t);
   22986    157536645 :         gcc_assert (TREE_CODE (r) == TREE_CODE (t));
   22987    157536645 :         RETURN (r);
   22988              :       }
   22989              : 
   22990      9245411 :     case STRING_CST:
   22991      9245411 :       {
   22992      9245411 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   22993      9245411 :         r = t;
   22994      9245411 :         if (type != TREE_TYPE (t))
   22995              :           {
   22996            3 :             r = copy_node (t);
   22997            3 :             TREE_TYPE (r) = type;
   22998              :           }
   22999      9245411 :         RETURN (r);
   23000              :       }
   23001              : 
   23002           54 :     case RAW_DATA_CST:
   23003           54 :       {
   23004           54 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23005           54 :         r = copy_node (t);
   23006           54 :         TREE_TYPE (r) = type;
   23007           54 :         RETURN (r);
   23008              :       }
   23009              : 
   23010           26 :     case PTRMEM_CST:
   23011              :       /* These can sometimes show up in a partial instantiation, but never
   23012              :          involve template parms.  */
   23013           26 :       gcc_assert (!uses_template_parms (t));
   23014           26 :       RETURN (t);
   23015              : 
   23016         4859 :     case UNARY_LEFT_FOLD_EXPR:
   23017         4859 :       RETURN (tsubst_unary_left_fold (t, args, complain, in_decl));
   23018       385634 :     case UNARY_RIGHT_FOLD_EXPR:
   23019       385634 :       RETURN (tsubst_unary_right_fold (t, args, complain, in_decl));
   23020         6499 :     case BINARY_LEFT_FOLD_EXPR:
   23021         6499 :       RETURN (tsubst_binary_left_fold (t, args, complain, in_decl));
   23022          928 :     case BINARY_RIGHT_FOLD_EXPR:
   23023          928 :       RETURN (tsubst_binary_right_fold (t, args, complain, in_decl));
   23024            0 :     case PREDICT_EXPR:
   23025            0 :       RETURN (t);
   23026              : 
   23027    130120002 :     case DEBUG_BEGIN_STMT:
   23028              :       /* ??? There's no point in copying it for now, but maybe some
   23029              :          day it will contain more information, such as a pointer back
   23030              :          to the containing function, inlined copy or so.  */
   23031    130120002 :       RETURN (t);
   23032              : 
   23033          113 :     case CO_YIELD_EXPR:
   23034          113 :       RETURN (finish_co_yield_expr (input_location,
   23035              :                                     RECUR (TREE_OPERAND (t, 0))));
   23036              : 
   23037          121 :     case CO_AWAIT_EXPR:
   23038          121 :       RETURN (finish_co_await_expr (input_location,
   23039              :                                     RECUR (TREE_OPERAND (t, 0))));
   23040              : 
   23041           42 :     case VA_ARG_EXPR:
   23042           42 :       {
   23043           42 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23044           42 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23045           42 :         RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
   23046              :       }
   23047              : 
   23048           48 :     case OFFSETOF_EXPR:
   23049           48 :       {
   23050           48 :         tree object_ptr
   23051           48 :           = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   23052           48 :         RETURN (finish_offsetof (object_ptr,
   23053              :                                  RECUR (TREE_OPERAND (t, 0)),
   23054              :                                  EXPR_LOCATION (t)));
   23055              :       }
   23056              : 
   23057       435079 :     case ADDRESSOF_EXPR:
   23058       435079 :       RETURN (cp_build_addressof (EXPR_LOCATION (t),
   23059              :                                   RECUR (TREE_OPERAND (t, 0)), complain));
   23060              : 
   23061     22394273 :     case TRAIT_EXPR:
   23062     22394273 :       {
   23063     22394273 :         tree type1 = TRAIT_EXPR_TYPE1 (t);
   23064     22394273 :         if (TYPE_P (type1))
   23065     22394170 :           type1 = tsubst (type1, args, complain, in_decl);
   23066              :         else
   23067          103 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   23068     22394273 :         tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
   23069              :                              complain, in_decl);
   23070     22394273 :         if (TRAIT_EXPR_KIND (t) == CPTK_STRUCTURED_BINDING_SIZE
   23071          123 :             && type1 != error_mark_node
   23072     22394396 :             && !processing_template_decl)
   23073              :           /* __builtin_structured_binding_size handled separately
   23074              :              to make it SFINAE friendly.  */
   23075          123 :           RETURN (finish_structured_binding_size (TRAIT_EXPR_LOCATION (t),
   23076              :                                                   type1, complain));
   23077     22394150 :         RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
   23078              :                                    TRAIT_EXPR_KIND (t), type1, type2));
   23079              :       }
   23080              : 
   23081          172 :     case STMT_EXPR:
   23082          172 :       {
   23083          172 :         tree old_stmt_expr = cur_stmt_expr;
   23084          172 :         tree stmt_expr = begin_stmt_expr ();
   23085              : 
   23086          172 :         cur_stmt_expr = stmt_expr;
   23087          172 :         tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl);
   23088          172 :         stmt_expr = finish_stmt_expr (stmt_expr, false);
   23089          172 :         cur_stmt_expr = old_stmt_expr;
   23090              : 
   23091              :         /* If the resulting list of expression statement is empty,
   23092              :            fold it further into void_node.  */
   23093          172 :         if (empty_expr_stmt_p (stmt_expr))
   23094            9 :           stmt_expr = void_node;
   23095              : 
   23096          172 :         RETURN (stmt_expr);
   23097              :       }
   23098              : 
   23099       539423 :     case LAMBDA_EXPR:
   23100       539423 :       {
   23101       539423 :         tree r = tsubst_lambda_expr (t, args, complain, in_decl);
   23102              : 
   23103       539423 :         RETURN (build_lambda_object (r));
   23104              :       }
   23105              : 
   23106           12 :     case TRANSACTION_EXPR:
   23107           12 :       gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t));
   23108           12 :       RETURN (tsubst_stmt (t, args, complain, in_decl));
   23109              : 
   23110       246651 :     case PAREN_EXPR:
   23111       246651 :       if (REF_PARENTHESIZED_P (t))
   23112       246650 :         RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
   23113              :       else
   23114              :         /* Recreate the PAREN_EXPR from __builtin_assoc_barrier.  */
   23115              :         {
   23116            1 :           tree op0 = RECUR (TREE_OPERAND (t, 0));
   23117            1 :           RETURN (build1_loc (input_location, PAREN_EXPR,
   23118              :                               TREE_TYPE (op0), op0));
   23119              :         }
   23120              : 
   23121           23 :     case VEC_PERM_EXPR:
   23122           23 :       {
   23123           23 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23124           23 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   23125           23 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   23126           23 :         RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
   23127              :                                        complain));
   23128              :       }
   23129              : 
   23130     14542833 :     case REQUIRES_EXPR:
   23131     14542833 :       {
   23132     14542833 :         complain &= ~tf_warning_or_error;
   23133     14542833 :         tree r = tsubst_requires_expr (t, args, complain, in_decl);
   23134     14540133 :         RETURN (r);
   23135              :       }
   23136              : 
   23137           12 :     case RANGE_EXPR:
   23138              :       /* No need to substitute further, a RANGE_EXPR will always be built
   23139              :          with constant operands.  */
   23140           12 :       RETURN (t);
   23141              : 
   23142    208988286 :     case NON_LVALUE_EXPR:
   23143    208988286 :     case VIEW_CONVERT_EXPR:
   23144    208988286 :       {
   23145    208988286 :         tree op = RECUR (TREE_OPERAND (t, 0));
   23146              : 
   23147    208988286 :         if (location_wrapper_p (t))
   23148              :           /* We need to do this here as well as in tsubst_copy so we get the
   23149              :              other tsubst_copy_and_build semantics for a PARM_DECL operand.  */
   23150    208982107 :           RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
   23151              : 
   23152         6179 :         gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
   23153         6179 :         if (REF_PARENTHESIZED_P (t))
   23154              :           /* force_paren_expr can also create a VIEW_CONVERT_EXPR.  */
   23155            0 :           RETURN (finish_parenthesized_expr (op));
   23156              : 
   23157         6179 :         check_param_in_postcondition (op, EXPR_LOCATION (t));
   23158              : 
   23159         6179 :         if (flag_contracts && processing_contract_condition)
   23160          358 :             op = constify_contract_access (op);
   23161              : 
   23162              :         /* Otherwise, we're dealing with a wrapper to make a C++20 template
   23163              :            parameter object const.  */
   23164         6179 :         if (TREE_TYPE (op) == NULL_TREE
   23165         6179 :             || !CP_TYPE_CONST_P (TREE_TYPE (op)))
   23166              :           {
   23167              :             /* The template argument is not const, presumably because
   23168              :                it is still dependent, and so not the const template parm
   23169              :                object.  */
   23170           82 :             tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23171           82 :             if (TREE_CODE (op) == CONSTRUCTOR
   23172           79 :                 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
   23173              :               {
   23174              :                 /* Don't add a wrapper to these.  */
   23175           12 :                 op = copy_node (op);
   23176           12 :                 TREE_TYPE (op) = type;
   23177              :               }
   23178              :             else
   23179              :               /* Do add a wrapper otherwise (in particular, if op is
   23180              :                  another TEMPLATE_PARM_INDEX).  */
   23181           70 :               op = build1 (VIEW_CONVERT_EXPR, type, op);
   23182              :           }
   23183         6179 :         RETURN (op);
   23184              :       }
   23185              : 
   23186         2020 :     case REFLECT_EXPR:
   23187         2020 :       {
   23188         2020 :         tree h = REFLECT_EXPR_HANDLE (t);
   23189         2020 :         reflect_kind kind = REFLECT_EXPR_KIND (t);
   23190         2020 :         if (TYPE_P (h) || TREE_CODE (h) == NAMESPACE_DECL)
   23191         1802 :           h = tsubst (h, args, complain, in_decl);
   23192          218 :         else if (kind == REFLECT_ANNOTATION)
   23193              :           /* annotations_of should be called on reflections of already
   23194              :              instantiated entities and so no need to tsubst the annotation
   23195              :              attribute and we rely on pointer equality of that.  */
   23196              :           ;
   23197          217 :         else if (TREE_CODE (h) == SCOPE_REF)
   23198           21 :           h = tsubst_qualified_id (h, args, complain, in_decl,
   23199              :                                    /*done=*/true, /*address_p=*/false,
   23200              :                                    /*name_lookup_p=*/false);
   23201              :         else
   23202              :           {
   23203              :             /* [expr.reflect] The id-expression of a reflect-expression is
   23204              :                an unevaluated operand.  */
   23205          196 :             cp_unevaluated u;
   23206          196 :             h = RECUR (h);
   23207          196 :           }
   23208         2020 :         RETURN (get_reflection (EXPR_LOCATION (t), h, kind));
   23209              :       }
   23210              : 
   23211          519 :     case SPLICE_EXPR:
   23212          519 :       RETURN (tsubst_splice_expr (t, args, complain, in_decl));
   23213              : 
   23214            0 :     default:
   23215              :       /* Handle Objective-C++ constructs, if appropriate.  */
   23216            0 :       if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
   23217            0 :         RETURN (subst);
   23218              : 
   23219              :       /* We shouldn't get here, but keep going if !flag_checking.  */
   23220            0 :       if (flag_checking)
   23221            0 :         gcc_unreachable ();
   23222            0 :       RETURN (t);
   23223              :     }
   23224              : 
   23225              : #undef RECUR
   23226              : #undef RETURN
   23227   2372360268 :  out:
   23228   2372360268 :   input_location = save_loc;
   23229   2372360268 :   return retval;
   23230              : }
   23231              : 
   23232              : /* Verify that the instantiated ARGS are valid. For type arguments,
   23233              :    make sure that the type's linkage is ok. For non-type arguments,
   23234              :    make sure they are constants if they are integral or enumerations.
   23235              :    Emit an error under control of COMPLAIN, and return TRUE on error.  */
   23236              : 
   23237              : static bool
   23238    330978068 : check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
   23239              : {
   23240    330978068 :   if (dependent_template_arg_p (t))
   23241              :     return false;
   23242    305189900 :   if (ARGUMENT_PACK_P (t))
   23243              :     {
   23244     18902506 :       tree vec = ARGUMENT_PACK_ARGS (t);
   23245     18902506 :       int len = TREE_VEC_LENGTH (vec);
   23246     18902506 :       bool result = false;
   23247     18902506 :       int i;
   23248              : 
   23249     52259422 :       for (i = 0; i < len; ++i)
   23250     33356916 :         if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
   23251            0 :           result = true;
   23252              :       return result;
   23253              :     }
   23254    286287394 :   else if (TYPE_P (t))
   23255              :     {
   23256              :       /* [basic.link]: A name with no linkage (notably, the name
   23257              :          of a class or enumeration declared in a local scope)
   23258              :          shall not be used to declare an entity with linkage.
   23259              :          This implies that names with no linkage cannot be used as
   23260              :          template arguments
   23261              : 
   23262              :          DR 757 relaxes this restriction for C++0x.  */
   23263    238988990 :       tree nt = (cxx_dialect > cxx98 ? NULL_TREE
   23264       162633 :                  : no_linkage_check (t, /*relaxed_p=*/false));
   23265              : 
   23266       162633 :       if (nt)
   23267              :         {
   23268              :           /* DR 488 makes use of a type with no linkage cause
   23269              :              type deduction to fail.  */
   23270            8 :           if (complain & tf_error)
   23271              :             {
   23272           10 :               if (TYPE_UNNAMED_P (nt))
   23273            0 :                 error ("%qT is/uses unnamed type", t);
   23274              :               else
   23275            5 :                 error ("template argument for %qD uses local type %qT",
   23276              :                        tmpl, t);
   23277              :             }
   23278            8 :           return true;
   23279              :         }
   23280              :       /* In order to avoid all sorts of complications, we do not
   23281              :          allow variably-modified types as template arguments.  */
   23282    238988982 :       else if (variably_modified_type_p (t, NULL_TREE))
   23283              :         {
   23284            6 :           if (complain & tf_error)
   23285            3 :             error ("%qT is a variably modified type", t);
   23286            6 :           return true;
   23287              :         }
   23288              :     }
   23289              :   /* Class template and alias template arguments should be OK.  */
   23290     47298404 :   else if (DECL_TYPE_TEMPLATE_P (t))
   23291              :     ;
   23292              :   /* A non-type argument of integral or enumerated type must be a
   23293              :      constant.  */
   23294     46776681 :   else if (TREE_TYPE (t)
   23295     46776681 :            && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
   23296     46764936 :            && !REFERENCE_REF_P (t)
   23297     93541486 :            && !TREE_CONSTANT (t))
   23298              :     {
   23299            0 :       if (complain & tf_error)
   23300            0 :         error ("integral expression %qE is not constant", t);
   23301            0 :       return true;
   23302              :     }
   23303              :   return false;
   23304              : }
   23305              : 
   23306              : static bool
   23307    196290908 : check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
   23308              : {
   23309    196290908 :   int ix, len = DECL_NTPARMS (tmpl);
   23310    196290908 :   bool result = false;
   23311              : 
   23312    493912060 :   for (ix = 0; ix != len; ix++)
   23313              :     {
   23314    297621152 :       if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
   23315           14 :         result = true;
   23316              :     }
   23317    196290908 :   if (result && (complain & tf_error))
   23318            8 :     error ("  trying to instantiate %qD", tmpl);
   23319    196290908 :   return result;
   23320              : }
   23321              : 
   23322              : /* Call mark_used on each entity within the non-type template arguments in
   23323              :    ARGS for an instantiation of TMPL, to ensure that each such entity is
   23324              :    considered odr-used (and therefore marked for instantiation) regardless of
   23325              :    whether the specialization was first formed in a template context (which
   23326              :    inhibits mark_used).
   23327              : 
   23328              :    This function assumes push_to_top_level has been called beforehand.  */
   23329              : 
   23330              : static void
   23331     91471649 : mark_template_arguments_used (tree tmpl, tree args)
   23332              : {
   23333              :   /* It suffices to do this only when instantiating a primary template.  */
   23334     91471649 :   if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
   23335              :     return;
   23336              : 
   23337              :   /* We already marked outer arguments when specializing the context.  */
   23338     73068855 :   args = INNERMOST_TEMPLATE_ARGS (args);
   23339              : 
   23340    187665089 :   for (tree arg : tree_vec_range (args))
   23341              :     {
   23342              :       /* A (pointer/reference to) function or variable NTTP argument.  */
   23343    114596234 :       if (TREE_CODE (arg) == ADDR_EXPR
   23344    114595013 :           || TREE_CODE (arg) == INDIRECT_REF)
   23345              :         {
   23346         3318 :           while (TREE_CODE (arg) == ADDR_EXPR
   23347         1878 :                  || REFERENCE_REF_P (arg)
   23348         4977 :                  || CONVERT_EXPR_P (arg))
   23349         1878 :             arg = TREE_OPERAND (arg, 0);
   23350         1440 :           if (VAR_OR_FUNCTION_DECL_P (arg))
   23351              :             {
   23352              :               /* Pass tf_none to avoid duplicate diagnostics: if this call
   23353              :                  fails then an earlier call to mark_used for this argument
   23354              :                  must have also failed and emitted a diagnostic.  */
   23355         1426 :               bool ok = mark_used (arg, tf_none);
   23356         1426 :               gcc_checking_assert (ok || seen_error ());
   23357              :             }
   23358              :         }
   23359              :       /* A member function pointer.  */
   23360    114594794 :       else if (TREE_CODE (arg) == PTRMEM_CST)
   23361              :         {
   23362          517 :           bool ok = mark_used (PTRMEM_CST_MEMBER (arg), tf_none);
   23363          517 :           gcc_checking_assert (ok || seen_error ());
   23364              :         }
   23365              :       /* A class NTTP argument.  */
   23366    114594277 :       else if (VAR_P (arg)
   23367    114594277 :                && DECL_NTTP_OBJECT_P (arg))
   23368              :         {
   23369        28516 :           auto mark_used_r = [](tree *tp, int *, void *) {
   23370        20305 :             if (VAR_OR_FUNCTION_DECL_P (*tp))
   23371              :               {
   23372           20 :                 bool ok = mark_used (*tp, tf_none);
   23373           20 :                 gcc_checking_assert (ok || seen_error ());
   23374              :               }
   23375        20305 :             return NULL_TREE;
   23376              :           };
   23377         8211 :           cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
   23378              :                                            mark_used_r, nullptr);
   23379              :         }
   23380              :     }
   23381              : }
   23382              : 
   23383              : /* We're out of SFINAE context now, so generate diagnostics for the access
   23384              :    errors we saw earlier when instantiating D from TMPL and ARGS.  */
   23385              : 
   23386              : static void
   23387            9 : recheck_decl_substitution (tree d, tree tmpl, tree args)
   23388              : {
   23389            9 :   tree pattern = DECL_TEMPLATE_RESULT (tmpl);
   23390            9 :   tree type = TREE_TYPE (pattern);
   23391            9 :   location_t loc = input_location;
   23392              : 
   23393            9 :   push_access_scope (d);
   23394            9 :   push_deferring_access_checks (dk_no_deferred);
   23395            9 :   input_location = DECL_SOURCE_LOCATION (pattern);
   23396            9 :   tsubst (type, args, tf_warning_or_error, d);
   23397            9 :   input_location = loc;
   23398            9 :   pop_deferring_access_checks ();
   23399            9 :   pop_access_scope (d);
   23400            9 : }
   23401              : 
   23402              : /* Instantiate the indicated variable, function, or alias template TMPL with
   23403              :    the template arguments in TARG_PTR.  */
   23404              : 
   23405              : tree
   23406    343662097 : instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
   23407              : {
   23408    343662097 :   auto_timevar tv (TV_TEMPLATE_INST);
   23409              : 
   23410    343662097 :   tree targ_ptr = orig_args;
   23411    343662097 :   tree fndecl;
   23412    343662097 :   tree gen_tmpl;
   23413    343662097 :   bool access_ok = true;
   23414              : 
   23415    343662097 :   if (tmpl == error_mark_node)
   23416              :     return error_mark_node;
   23417              : 
   23418              :   /* The other flags are not relevant anymore here, especially tf_partial
   23419              :      shouldn't be set.  For instance, we may be called while doing a partial
   23420              :      substitution of a template variable, but the type of the variable
   23421              :      template may be auto, in which case we will call do_auto_deduction
   23422              :      in mark_used (which clears tf_partial) and the auto must be properly
   23423              :      reduced at that time for the deduction to work.
   23424              : 
   23425              :      Note that later below we set tf_partial iff there are dependent arguments;
   23426              :      the above is concerned specifically about the non-dependent case.  */
   23427    343662088 :   complain &= tf_warning_or_error;
   23428              : 
   23429    343662088 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
   23430              : 
   23431    343662088 :   if (modules_p ())
   23432       872496 :     lazy_load_pendings (tmpl);
   23433              : 
   23434              :   /* If this function is a clone, handle it specially.  */
   23435    343662088 :   if (DECL_CLONED_FUNCTION_P (tmpl))
   23436              :     {
   23437      7006583 :       tree spec;
   23438      7006583 :       tree clone;
   23439              : 
   23440              :       /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
   23441              :          DECL_CLONED_FUNCTION.  */
   23442      7006583 :       spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
   23443              :                                    targ_ptr, complain);
   23444      7006583 :       if (spec == error_mark_node)
   23445              :         return error_mark_node;
   23446              : 
   23447              :       /* Look for the clone.  */
   23448     13793670 :       FOR_EACH_CLONE (clone, spec)
   23449     13793670 :         if (DECL_NAME (clone) == DECL_NAME (tmpl))
   23450              :           return clone;
   23451              :       /* We should always have found the clone by now.  */
   23452            0 :       gcc_unreachable ();
   23453              :       return NULL_TREE;
   23454              :     }
   23455              : 
   23456    336655505 :   if (targ_ptr == error_mark_node)
   23457              :     return error_mark_node;
   23458              : 
   23459              :   /* Check to see if we already have this specialization.  */
   23460    336655505 :   gen_tmpl = most_general_template (tmpl);
   23461    673311010 :   if (TMPL_ARGS_DEPTH (targ_ptr)
   23462    336655505 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
   23463              :     /* targ_ptr only has the innermost template args, so add the outer ones
   23464              :        from tmpl, which could be either a partial instantiation or gen_tmpl (in
   23465              :        the case of a non-dependent call within a template definition).  */
   23466     13181135 :     targ_ptr = (add_outermost_template_args
   23467     13181135 :                 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
   23468              :                  targ_ptr));
   23469              : 
   23470    336655505 :   hashval_t hash = spec_hasher::hash (gen_tmpl, targ_ptr);
   23471    336655505 :   tree spec = retrieve_specialization (gen_tmpl, targ_ptr, hash);
   23472              : 
   23473    336655505 :   gcc_checking_assert (tmpl == gen_tmpl
   23474              :                        || ((fndecl
   23475              :                             = retrieve_specialization (tmpl, orig_args, 0))
   23476              :                            == spec)
   23477              :                        || fndecl == NULL_TREE);
   23478              : 
   23479    336655505 :   if (spec != NULL_TREE)
   23480              :     {
   23481    194912199 :       if (FNDECL_HAS_ACCESS_ERRORS (spec))
   23482              :         {
   23483           15 :           if (complain & tf_error)
   23484            9 :             recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
   23485           15 :           return error_mark_node;
   23486              :         }
   23487              :       return spec;
   23488              :     }
   23489              : 
   23490    141743306 :   if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
   23491              :                                complain))
   23492            6 :     return error_mark_node;
   23493              : 
   23494              :   /* We are building a FUNCTION_DECL, during which the access of its
   23495              :      parameters and return types have to be checked.  However this
   23496              :      FUNCTION_DECL which is the desired context for access checking
   23497              :      is not built yet.  We solve this chicken-and-egg problem by
   23498              :      deferring all checks until we have the FUNCTION_DECL.  */
   23499    141743300 :   deferring_access_check_sentinel dacs (dk_deferred);
   23500              : 
   23501              :   /* Instantiation of the function happens in the context of the function
   23502              :      template, not the context of the overload resolution we're doing.  */
   23503    141743300 :   push_to_top_level ();
   23504              :   /* If there are dependent arguments, e.g. because we're doing partial
   23505              :      ordering, make sure processing_template_decl stays set.  And set
   23506              :      tf_partial mainly for benefit of instantiation of alias templates
   23507              :      that contain extra-args trees.  */
   23508    141743300 :   if (uses_template_parms (targ_ptr))
   23509              :     {
   23510     18756043 :       ++processing_template_decl;
   23511     18756043 :       complain |= tf_partial;
   23512              :     }
   23513    141743300 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   23514              :     {
   23515     39173886 :       tree ctx;
   23516     39173886 :       if (!uses_template_parms (DECL_CONTEXT (tmpl)))
   23517              :         /* If the context of the partially instantiated template is
   23518              :            already non-dependent, then we might as well use it.  */
   23519     35802704 :         ctx = DECL_CONTEXT (tmpl);
   23520              :       else
   23521      3371182 :         ctx = tsubst_entering_scope (DECL_CONTEXT (gen_tmpl), targ_ptr,
   23522              :                                      complain, gen_tmpl);
   23523     39173886 :       push_nested_class (ctx);
   23524              :     }
   23525              : 
   23526    141743300 :   tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
   23527              : 
   23528    141743300 :   tree partial_ti = NULL_TREE;
   23529    141743300 :   fndecl = NULL_TREE;
   23530    141743300 :   if (VAR_P (pattern))
   23531              :     {
   23532              :       /* We need to determine if we're using a partial or explicit
   23533              :          specialization now, because the type of the variable could be
   23534              :          different.  */
   23535     16661733 :       tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
   23536     16661733 :       partial_ti = most_specialized_partial_spec (tid, complain);
   23537     16661733 :       if (partial_ti == error_mark_node)
   23538              :         pattern = error_mark_node;
   23539     16661730 :       else if (partial_ti)
   23540              :         {
   23541      1090947 :           tree partial_tmpl = TI_TEMPLATE (partial_ti);
   23542      1090947 :           tree partial_args = TI_ARGS (partial_ti);
   23543      1090947 :           tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
   23544      1090947 :           fndecl = tsubst_decl (partial_pat, partial_args, complain,
   23545              :                                 /*use_spec_table=*/false);
   23546              :         }
   23547              :     }
   23548              : 
   23549              :   /* Substitute template parameters to obtain the specialization.  */
   23550      1090947 :   if (fndecl == NULL_TREE)
   23551    140652353 :     fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false);
   23552    141735164 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   23553     39173886 :     pop_nested_class ();
   23554    141735164 :   pop_from_top_level ();
   23555              : 
   23556    141735164 :   if (fndecl == error_mark_node)
   23557              :     return error_mark_node;
   23558              : 
   23559              :   /* Substituting the type might have recursively instantiated this
   23560              :      same alias (c++/117530).  */
   23561     84906800 :   if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   23562    118503761 :       && (spec = retrieve_specialization (gen_tmpl, targ_ptr, hash)))
   23563              :     return spec;
   23564              : 
   23565              :   /* The DECL_TI_TEMPLATE should always be the immediate parent
   23566              :      template, not the most general template.  */
   23567     84906788 :   DECL_TI_TEMPLATE (fndecl) = tmpl;
   23568     84906788 :   DECL_TI_ARGS (fndecl) = targ_ptr;
   23569     84906788 :   if (VAR_P (pattern))
   23570              :     {
   23571              :       /* Now that we we've formed this variable template specialization,
   23572              :          remember the result of most_specialized_partial_spec for it.  */
   23573     16661730 :       TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
   23574              : 
   23575              :       /* And remember if the variable was declared with [].  */
   23576     16661730 :       if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
   23577     16661730 :           && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
   23578           12 :         SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
   23579              :     }
   23580              : 
   23581     84906788 :   fndecl = register_specialization (fndecl, gen_tmpl, targ_ptr, false, hash);
   23582     84906788 :   if (fndecl == error_mark_node)
   23583              :     return error_mark_node;
   23584              : 
   23585     84906788 :   set_instantiating_module (fndecl);
   23586              : 
   23587              :   /* Now we know the specialization, compute access previously
   23588              :      deferred.  Do no access control for inheriting constructors,
   23589              :      as we already checked access for the inherited constructor.  */
   23590     84906788 :   if (!(flag_new_inheriting_ctors
   23591    119553793 :         && DECL_INHERITED_CTOR (fndecl)))
   23592              :     {
   23593     84895349 :       push_access_scope (fndecl);
   23594     84895349 :       if (!perform_deferred_access_checks (complain))
   23595              :         access_ok = false;
   23596     84895349 :       pop_access_scope (fndecl);
   23597              :     }
   23598              : 
   23599              :   /* If we've just instantiated the main entry point for a function,
   23600              :      instantiate all the alternate entry points as well.  We do this
   23601              :      by cloning the instantiation of the main entry point, not by
   23602              :      instantiating the template clones.  */
   23603     84906788 :   if (tree chain = DECL_CHAIN (gen_tmpl))
   23604     84339490 :     if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
   23605       172545 :       clone_cdtor (fndecl, /*update_methods=*/false);
   23606              : 
   23607     84906788 :   if (!access_ok)
   23608              :     {
   23609           83 :       if (!(complain & tf_error))
   23610              :         {
   23611              :           /* Remember to reinstantiate when we're out of SFINAE so the user
   23612              :              can see the errors.  */
   23613           83 :           FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
   23614              :         }
   23615           83 :       return error_mark_node;
   23616              :     }
   23617              : 
   23618              :   return fndecl;
   23619    343653961 : }
   23620              : 
   23621              : /* Instantiate the alias template TMPL with ARGS.  Also push a template
   23622              :    instantiation level, which instantiate_template doesn't do because
   23623              :    functions and variables have sufficient context established by the
   23624              :    callers.  */
   23625              : 
   23626              : static tree
   23627    179925980 : instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
   23628              : {
   23629    179925980 :   if (tmpl == error_mark_node || args == error_mark_node)
   23630              :     return error_mark_node;
   23631              : 
   23632    179100924 :   args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
   23633              :                                 args, tmpl, complain);
   23634    179100924 :   if (args == error_mark_node)
   23635              :     return error_mark_node;
   23636              : 
   23637              :   /* FIXME check for satisfaction in check_instantiated_args.  */
   23638    179100192 :   if (!constraints_satisfied_p (tmpl, args))
   23639              :     {
   23640       209295 :       if (complain & tf_error)
   23641              :         {
   23642           27 :           auto_diagnostic_group d;
   23643           27 :           error ("template constraint failure for %qD", tmpl);
   23644           27 :           diagnose_constraints (input_location, tmpl, args);
   23645           27 :         }
   23646       209295 :       return error_mark_node;
   23647              :     }
   23648              : 
   23649    178890897 :   if (!push_tinst_level (tmpl, args))
   23650            0 :     return error_mark_node;
   23651    178890894 :   tree r = instantiate_template (tmpl, args, complain);
   23652    178890894 :   pop_tinst_level ();
   23653              : 
   23654    178890894 :   if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
   23655              :     {
   23656              :       /* Note this is also done at parse time from push_template_decl.  */
   23657              :       /* An alias template specialization can be dependent
   23658              :          even if its underlying type is not.  */
   23659     11671478 :       TYPE_DEPENDENT_P (d) = true;
   23660     11671478 :       TYPE_DEPENDENT_P_VALID (d) = true;
   23661              :       /* Sometimes a dependent alias spec is equivalent to its expansion,
   23662              :          sometimes not.  So always use structural_comptypes.  */
   23663     11671478 :       SET_TYPE_STRUCTURAL_EQUALITY (d);
   23664              :     }
   23665              : 
   23666              :   return r;
   23667              : }
   23668              : 
   23669              : /* PARM is a template parameter pack for FN.  Returns true iff
   23670              :    PARM is used in a deducible way in the argument list of FN.  */
   23671              : 
   23672              : static bool
   23673      6447583 : pack_deducible_p (tree parm, tree fn)
   23674              : {
   23675      6447583 :   tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
   23676     12878158 :   for (; t; t = TREE_CHAIN (t))
   23677              :     {
   23678      6430901 :       tree type = TREE_VALUE (t);
   23679      6430901 :       tree packs;
   23680      6430901 :       if (!PACK_EXPANSION_P (type))
   23681      6429278 :         continue;
   23682         3246 :       for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
   23683         2923 :            packs; packs = TREE_CHAIN (packs))
   23684         1626 :         if (template_args_equal (TREE_VALUE (packs), parm))
   23685              :           {
   23686              :             /* The template parameter pack is used in a function parameter
   23687              :                pack.  If this is the end of the parameter list, the
   23688              :                template parameter pack is deducible.  */
   23689          326 :             if (TREE_CHAIN (t) == void_list_node)
   23690              :               return true;
   23691              :             else
   23692              :               /* Otherwise, not.  Well, it could be deduced from
   23693              :                  a non-pack parameter, but doing so would end up with
   23694              :                  a deduction mismatch, so don't bother.  */
   23695              :               return false;
   23696              :           }
   23697              :     }
   23698              :   /* The template parameter pack isn't used in any function parameter
   23699              :      packs, but it might be used deeper, e.g. tuple<Args...>.  */
   23700              :   return true;
   23701              : }
   23702              : 
   23703              : /* Subroutine of fn_type_unification: check non-dependent parms for
   23704              :    convertibility.  */
   23705              : 
   23706              : static int
   23707    156992150 : check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
   23708              :                                  tree fn, unification_kind_t strict, int flags,
   23709              :                                  struct conversion **convs, bool explain_p,
   23710              :                                  bool noninst_only_p)
   23711              : {
   23712              :   /* Non-constructor methods need to leave a conversion for 'this', which
   23713              :      isn't included in nargs here.  */
   23714    156992150 :   unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   23715    185677761 :                      && !DECL_CONSTRUCTOR_P (fn));
   23716              : 
   23717    156992150 :   for (unsigned ia = 0;
   23718    354787256 :        parms && parms != void_list_node && ia < nargs; )
   23719              :     {
   23720    201863808 :       tree parm = TREE_VALUE (parms);
   23721              : 
   23722    201863808 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   23723    201863808 :           && (!TREE_CHAIN (parms)
   23724      1273912 :               || TREE_CHAIN (parms) == void_list_node))
   23725              :         /* For a function parameter pack that occurs at the end of the
   23726              :            parameter-declaration-list, the type A of each remaining
   23727              :            argument of the call is compared with the type P of the
   23728              :            declarator-id of the function parameter pack.  */
   23729              :         break;
   23730              : 
   23731    200589971 :       parms = TREE_CHAIN (parms);
   23732              : 
   23733    200589971 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   23734              :         /* For a function parameter pack that does not occur at the
   23735              :            end of the parameter-declaration-list, the type of the
   23736              :            parameter pack is a non-deduced context.  */
   23737           81 :         continue;
   23738              : 
   23739    200589890 :       if (!uses_template_parms (parm))
   23740              :         {
   23741     37852884 :           tree arg = args[ia];
   23742     37852884 :           conversion **conv_p = convs ? &convs[ia+offset] : NULL;
   23743     37852884 :           int lflags = conv_flags (ia, nargs, fn, arg, flags);
   23744              : 
   23745     37852884 :           if (check_non_deducible_conversion (parm, arg, strict, lflags,
   23746              :                                               conv_p, explain_p, noninst_only_p))
   23747              :             return 1;
   23748              :         }
   23749              : 
   23750    197795025 :       ++ia;
   23751              :     }
   23752              : 
   23753              :   return 0;
   23754              : }
   23755              : 
   23756              : /* The FN is a TEMPLATE_DECL for a function.  ARGS is an array with
   23757              :    NARGS elements of the arguments that are being used when calling
   23758              :    it.  TARGS is a vector into which the deduced template arguments
   23759              :    are placed.
   23760              : 
   23761              :    Returns either a FUNCTION_DECL for the matching specialization of FN or
   23762              :    NULL_TREE if no suitable specialization can be found.  If EXPLAIN_P is
   23763              :    true, diagnostics will be printed to explain why it failed.
   23764              : 
   23765              :    If FN is a conversion operator, or we are trying to produce a specific
   23766              :    specialization, RETURN_TYPE is the return type desired.
   23767              : 
   23768              :    The EXPLICIT_TARGS are explicit template arguments provided via a
   23769              :    template-id.
   23770              : 
   23771              :    The parameter STRICT is one of:
   23772              : 
   23773              :    DEDUCE_CALL:
   23774              :      We are deducing arguments for a function call, as in
   23775              :      [temp.deduct.call].  If RETURN_TYPE is non-null, we are
   23776              :      deducing arguments for a call to the result of a conversion
   23777              :      function template, as in [over.call.object].
   23778              : 
   23779              :    DEDUCE_CONV:
   23780              :      We are deducing arguments for a conversion function, as in
   23781              :      [temp.deduct.conv].
   23782              : 
   23783              :    DEDUCE_EXACT:
   23784              :      We are deducing arguments when doing an explicit instantiation
   23785              :      as in [temp.explicit], when determining an explicit specialization
   23786              :      as in [temp.expl.spec], or when taking the address of a function
   23787              :      template, as in [temp.deduct.funcaddr].  */
   23788              : 
   23789              : tree
   23790    735098925 : fn_type_unification (tree fn,
   23791              :                      tree explicit_targs,
   23792              :                      tree targs,
   23793              :                      const tree *args,
   23794              :                      unsigned int nargs,
   23795              :                      tree return_type,
   23796              :                      unification_kind_t strict,
   23797              :                      int flags,
   23798              :                      struct conversion **convs,
   23799              :                      bool explain_p,
   23800              :                      bool decltype_p)
   23801              : {
   23802    735098925 :   tree parms;
   23803    735098925 :   tree fntype;
   23804    735098925 :   tree decl = NULL_TREE;
   23805    735098925 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   23806    735098925 :   bool ok;
   23807    735098925 :   static int deduction_depth;
   23808              :   /* type_unification_real will pass back any access checks from default
   23809              :      template argument substitution.  */
   23810    735098925 :   vec<deferred_access_check, va_gc> *checks = NULL;
   23811              :   /* We don't have all the template args yet.  */
   23812    735098925 :   bool incomplete = true;
   23813              : 
   23814    735098925 :   tree orig_fn = fn;
   23815    735098925 :   if (flag_new_inheriting_ctors)
   23816    735097127 :     fn = strip_inheriting_ctors (fn);
   23817              : 
   23818    735098925 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
   23819    735098925 :   tree r = error_mark_node;
   23820              : 
   23821    735098925 :   tree full_targs = targs;
   23822   1470197850 :   if (TMPL_ARGS_DEPTH (targs)
   23823    735098925 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
   23824        45435 :     full_targs = (add_outermost_template_args
   23825        45435 :                   (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
   23826              :                    targs));
   23827              : 
   23828    735098925 :   if (decltype_p)
   23829     42610477 :     complain |= tf_decltype;
   23830              : 
   23831              :   /* In C++0x, it's possible to have a function template whose type depends
   23832              :      on itself recursively.  This is most obvious with decltype, but can also
   23833              :      occur with enumeration scope (c++/48969).  So we need to catch infinite
   23834              :      recursion and reject the substitution at deduction time; this function
   23835              :      will return error_mark_node for any repeated substitution.
   23836              : 
   23837              :      This also catches excessive recursion such as when f<N> depends on
   23838              :      f<N-1> across all integers, and returns error_mark_node for all the
   23839              :      substitutions back up to the initial one.
   23840              : 
   23841              :      This is, of course, not reentrant.  */
   23842    735098925 :   if (excessive_deduction_depth)
   23843            0 :     return error_mark_node;
   23844    735098925 :   ++deduction_depth;
   23845              : 
   23846    735098925 :   gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
   23847              : 
   23848    735098925 :   fntype = TREE_TYPE (fn);
   23849    735098925 :   if (explicit_targs)
   23850              :     {
   23851              :       /* [temp.deduct]
   23852              : 
   23853              :          The specified template arguments must match the template
   23854              :          parameters in kind (i.e., type, nontype, template), and there
   23855              :          must not be more arguments than there are parameters;
   23856              :          otherwise type deduction fails.
   23857              : 
   23858              :          Nontype arguments must match the types of the corresponding
   23859              :          nontype template parameters, or must be convertible to the
   23860              :          types of the corresponding nontype parameters as specified in
   23861              :          _temp.arg.nontype_, otherwise type deduction fails.
   23862              : 
   23863              :          All references in the function type of the function template
   23864              :          to the corresponding template parameters are replaced by the
   23865              :          specified template argument values.  If a substitution in a
   23866              :          template parameter or in the function type of the function
   23867              :          template results in an invalid type, type deduction fails.  */
   23868     72390563 :       int i, len = TREE_VEC_LENGTH (tparms);
   23869     72390563 :       location_t loc = input_location;
   23870     72390563 :       incomplete = false;
   23871              : 
   23872     72390563 :       if (explicit_targs == error_mark_node)
   23873            0 :         goto fail;
   23874              : 
   23875    144781126 :       if (TMPL_ARGS_DEPTH (explicit_targs)
   23876    144781126 :           < TMPL_ARGS_DEPTH (full_targs))
   23877        17524 :         explicit_targs = add_outermost_template_args (full_targs,
   23878              :                                                       explicit_targs);
   23879              : 
   23880              :       /* Adjust any explicit template arguments before entering the
   23881              :          substitution context.  */
   23882     72390563 :       explicit_targs
   23883     72390563 :         = (coerce_template_parms (tparms, explicit_targs, fn,
   23884              :                                   complain|tf_partial,
   23885              :                                   /*require_all_args=*/false));
   23886     72390563 :       if (explicit_targs == error_mark_node)
   23887      4118652 :         goto fail;
   23888              : 
   23889              :       /* Substitute the explicit args into the function type.  This is
   23890              :          necessary so that, for instance, explicitly declared function
   23891              :          arguments can match null pointed constants.  If we were given
   23892              :          an incomplete set of explicit args, we must not do semantic
   23893              :          processing during substitution as we could create partial
   23894              :          instantiations.  */
   23895    152486828 :       for (i = 0; i < len; i++)
   23896              :         {
   23897     84214917 :           tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
   23898     84214917 :           bool parameter_pack = false;
   23899     84214917 :           tree targ = TREE_VEC_ELT (explicit_targs, i);
   23900              : 
   23901              :           /* Dig out the actual parm.  */
   23902     84214917 :           if (TREE_CODE (parm) == TYPE_DECL
   23903      8010823 :               || TREE_CODE (parm) == TEMPLATE_DECL)
   23904              :             {
   23905     76208459 :               parm = TREE_TYPE (parm);
   23906     76208459 :               parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
   23907              :             }
   23908      8006458 :           else if (TREE_CODE (parm) == PARM_DECL)
   23909              :             {
   23910      8006458 :               parm = DECL_INITIAL (parm);
   23911      8006458 :               parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
   23912              :             }
   23913              : 
   23914     84214917 :           if (targ == NULL_TREE)
   23915              :             /* No explicit argument for this template parameter.  */
   23916              :             incomplete = true;
   23917     70184308 :           else if (parameter_pack && pack_deducible_p (parm, fn))
   23918              :             {
   23919              :               /* Mark the argument pack as "incomplete". We could
   23920              :                  still deduce more arguments during unification.
   23921              :                  We remove this mark in type_unification_real.  */
   23922      6447562 :               ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
   23923      6447562 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ)
   23924      6447562 :                 = ARGUMENT_PACK_ARGS (targ);
   23925              : 
   23926              :               /* We have some incomplete argument packs.  */
   23927      6447562 :               incomplete = true;
   23928              :             }
   23929              :         }
   23930              : 
   23931     68271911 :       if (incomplete)
   23932              :         {
   23933     15918600 :           if (!push_tinst_level (fn, explicit_targs))
   23934              :             {
   23935            0 :               excessive_deduction_depth = true;
   23936            0 :               goto fail;
   23937              :             }
   23938     15918594 :           ++processing_template_decl;
   23939     15918594 :           input_location = DECL_SOURCE_LOCATION (fn);
   23940              :           /* Ignore any access checks; we'll see them again in
   23941              :              instantiate_template and they might have the wrong
   23942              :              access path at this point.  */
   23943     15918594 :           push_deferring_access_checks (dk_deferred);
   23944     15918594 :           tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
   23945     15918594 :           fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
   23946     15915897 :           pop_deferring_access_checks ();
   23947     15915897 :           input_location = loc;
   23948     15915897 :           --processing_template_decl;
   23949     15915897 :           pop_tinst_level ();
   23950              : 
   23951     15915897 :           if (fntype == error_mark_node)
   23952        18535 :             goto fail;
   23953              :         }
   23954              : 
   23955              :       /* Place the explicitly specified arguments in TARGS.  */
   23956     68250673 :       explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
   23957    152423000 :       for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
   23958     84172327 :         TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
   23959     68250673 :       if (!incomplete && CHECKING_P
   23960    120603984 :           && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   23961     52353311 :         SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
   23962              :           (targs, NUM_TMPL_ARGS (explicit_targs));
   23963              :     }
   23964              : 
   23965    730959035 :   if (return_type && strict != DEDUCE_CALL)
   23966              :     {
   23967     14757144 :       tree *new_args = XALLOCAVEC (tree, nargs + 1);
   23968     14757144 :       new_args[0] = return_type;
   23969     14757144 :       memcpy (new_args + 1, args, nargs * sizeof (tree));
   23970     14757144 :       args = new_args;
   23971     14757144 :       ++nargs;
   23972              :     }
   23973              : 
   23974    730959035 :   if (!incomplete)
   23975     52353311 :     goto deduced;
   23976              : 
   23977              :   /* Never do unification on the 'this' parameter.  */
   23978    678605724 :   parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
   23979              : 
   23980    678605724 :   if (return_type && strict == DEDUCE_CALL)
   23981              :     {
   23982              :       /* We're deducing for a call to the result of a template conversion
   23983              :          function.  The parms we really want are in return_type.  */
   23984           15 :       if (INDIRECT_TYPE_P (return_type))
   23985           15 :         return_type = TREE_TYPE (return_type);
   23986           15 :       parms = TYPE_ARG_TYPES (return_type);
   23987              :     }
   23988    678605709 :   else if (return_type)
   23989              :     {
   23990     13829880 :       parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
   23991              :     }
   23992              : 
   23993              :   /* We allow incomplete unification without an error message here
   23994              :      because the standard doesn't seem to explicitly prohibit it.  Our
   23995              :      callers must be ready to deal with unification failures in any
   23996              :      event.  */
   23997              : 
   23998              :   /* If we aren't explaining yet, push tinst context so we can see where
   23999              :      any errors (e.g. from class instantiations triggered by instantiation
   24000              :      of default template arguments) come from.  If we are explaining, this
   24001              :      context is redundant.  */
   24002    678605724 :   if (!explain_p && !push_tinst_level (fn, targs))
   24003              :     {
   24004            0 :       excessive_deduction_depth = true;
   24005            0 :       goto fail;
   24006              :     }
   24007              : 
   24008    678605718 :   ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   24009              :                                full_targs, parms, args, nargs, /*subr=*/0,
   24010              :                                strict, &checks, explain_p);
   24011    678605718 :   if (!explain_p)
   24012    678602999 :     pop_tinst_level ();
   24013    678605718 :   if (!ok)
   24014    596790221 :     goto fail;
   24015              : 
   24016              :   /* Now that we have bindings for all of the template arguments,
   24017              :      ensure that the arguments deduced for the template template
   24018              :      parameters have compatible template parameter lists.  We cannot
   24019              :      check this property before we have deduced all template
   24020              :      arguments, because the template parameter types of a template
   24021              :      template parameter might depend on prior template parameters
   24022              :      deduced after the template template parameter.  The following
   24023              :      ill-formed example illustrates this issue:
   24024              : 
   24025              :        template<typename T, template<T> class C> void f(C<5>, T);
   24026              : 
   24027              :        template<int N> struct X {};
   24028              : 
   24029              :        void g() {
   24030              :          f(X<5>(), 5l); // error: template argument deduction fails
   24031              :        }
   24032              : 
   24033              :      The template parameter list of 'C' depends on the template type
   24034              :      parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
   24035              :      'long'.  Thus, we can't check that 'C' cannot bind to 'X' at the
   24036              :      time that we deduce 'C'.  */
   24037    163630994 :   if (!template_template_parm_bindings_ok_p
   24038     81815497 :            (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
   24039              :     {
   24040           24 :       unify_inconsistent_template_template_parameters (explain_p);
   24041           24 :       goto fail;
   24042              :     }
   24043              : 
   24044     81815473 :  deduced:
   24045              : 
   24046              :   /* As a refinement of CWG2369, check first and foremost non-dependent
   24047              :      conversions that we know are not going to induce template instantiation
   24048              :      (PR99599).  */
   24049    134168784 :   if (strict == DEDUCE_CALL
   24050     80102208 :       && incomplete && flag_concepts
   24051    214041573 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   24052              :                                           convs, explain_p,
   24053              :                                           /*noninst_only_p=*/true))
   24054      2063844 :     goto fail;
   24055              : 
   24056              :   /* CWG2369: Check satisfaction before non-deducible conversions.  */
   24057    132104940 :   if (!constraints_satisfied_p (fn, targs))
   24058              :     {
   24059      2630087 :       if (explain_p)
   24060          663 :         diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
   24061      2630087 :       goto fail;
   24062              :     }
   24063              : 
   24064              :   /* DR 1391: All parameters have args, now check non-dependent parms for
   24065              :      convertibility.  We don't do this if all args were explicitly specified,
   24066              :      as the standard says that we substitute explicit args immediately.  */
   24067    129472153 :   if (incomplete
   24068    129472153 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   24069              :                                           convs, explain_p,
   24070              :                                           /*noninst_only_p=*/false))
   24071       731021 :     goto fail;
   24072              : 
   24073              :   /* All is well so far.  Now, check:
   24074              : 
   24075              :      [temp.deduct]
   24076              : 
   24077              :      When all template arguments have been deduced, all uses of
   24078              :      template parameters in nondeduced contexts are replaced with
   24079              :      the corresponding deduced argument values.  If the
   24080              :      substitution results in an invalid type, as described above,
   24081              :      type deduction fails.  */
   24082    128741132 :   if (!push_tinst_level (fn, targs))
   24083              :     {
   24084            0 :       excessive_deduction_depth = true;
   24085            0 :       goto fail;
   24086              :     }
   24087              : 
   24088              :   /* Also collect access checks from the instantiation.  */
   24089    128741126 :   reopen_deferring_access_checks (checks);
   24090              : 
   24091    128741126 :   decl = instantiate_template (fn, targs, complain);
   24092              : 
   24093    128732990 :   checks = get_deferred_access_checks ();
   24094    128732990 :   pop_deferring_access_checks ();
   24095              : 
   24096    128732990 :   pop_tinst_level ();
   24097              : 
   24098    128732990 :   if (decl == error_mark_node)
   24099      1554349 :     goto fail;
   24100              : 
   24101              :   /* Now perform any access checks encountered during substitution.  */
   24102    127178641 :   push_access_scope (decl);
   24103    127178641 :   ok = perform_access_checks (checks, complain);
   24104    127178641 :   pop_access_scope (decl);
   24105    127178641 :   if (!ok)
   24106           12 :     goto fail;
   24107              : 
   24108              :   /* If we're looking for an exact match, check that what we got
   24109              :      is indeed an exact match.  It might not be if some template
   24110              :      parameters are used in non-deduced contexts.  But don't check
   24111              :      for an exact match if we have dependent template arguments;
   24112              :      in that case we're doing partial ordering, and we already know
   24113              :      that we have two candidates that will provide the actual type.  */
   24114    127178629 :   if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
   24115              :     {
   24116      2180881 :       tree substed = TREE_TYPE (decl);
   24117      2180881 :       unsigned int i;
   24118              : 
   24119      2180881 :       tree sarg
   24120      2180881 :         = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
   24121      2180881 :       if (return_type)
   24122      2180689 :         sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
   24123      9666169 :       for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
   24124      7491479 :         if (!same_type_p (args[i], TREE_VALUE (sarg)))
   24125              :           {
   24126         6191 :             unify_type_mismatch (explain_p, args[i],
   24127         6191 :                                  TREE_VALUE (sarg));
   24128         6191 :             goto fail;
   24129              :           }
   24130      2174690 :       if ((i < nargs || sarg)
   24131              :           /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
   24132              :              doesn't contain the trailing void, and conv fns are always ().  */
   24133      2174690 :           && !DECL_CONV_FN_P (decl))
   24134              :         {
   24135            6 :           unsigned nsargs = i + list_length (sarg);
   24136            6 :           unify_arity (explain_p, nargs, nsargs);
   24137            6 :           goto fail;
   24138              :         }
   24139              :     }
   24140              : 
   24141              :   /* After doing deduction with the inherited constructor, actually return an
   24142              :      instantiation of the inheriting constructor.  */
   24143    127172432 :   if (orig_fn != fn)
   24144        19225 :     decl = instantiate_template (orig_fn, targs, complain);
   24145              : 
   24146              :   r = decl;
   24147              : 
   24148    735085374 :  fail:
   24149    735085374 :   --deduction_depth;
   24150    735085374 :   if (excessive_deduction_depth)
   24151              :     {
   24152            0 :       if (deduction_depth == 0)
   24153              :         /* Reset once we're all the way out.  */
   24154            0 :         excessive_deduction_depth = false;
   24155              :     }
   24156              : 
   24157              :   return r;
   24158              : }
   24159              : 
   24160              : /* Returns true iff PARM is a forwarding reference in the context of
   24161              :    template argument deduction for TMPL.  */
   24162              : 
   24163              : static bool
   24164    721972547 : forwarding_reference_p (tree parm, tree tmpl)
   24165              : {
   24166              :   /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
   24167              :      cv-unqualified template parameter ..."  */
   24168    721972547 :   if (TYPE_REF_P (parm)
   24169    637467097 :       && TYPE_REF_IS_RVALUE (parm)
   24170     18112829 :       && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
   24171    731152424 :       && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
   24172              :     {
   24173      8989699 :       parm = TREE_TYPE (parm);
   24174              :       /* [temp.deduct.call], "... that does not represent a template parameter
   24175              :          of a class template (during class template argument deduction)."  */
   24176      8989699 :       if (tmpl
   24177      8837971 :           && deduction_guide_p (tmpl)
   24178      9039033 :           && DECL_ARTIFICIAL (tmpl))
   24179              :         {
   24180              :           /* Since the template parameters of a synthesized guide consist of
   24181              :              the template parameters of the class template followed by those of
   24182              :              the constructor (if any), we can tell if PARM represents a template
   24183              :              parameter of the class template by comparing its index with the
   24184              :              arity of the class template.  */
   24185        27832 :           tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
   24186        27832 :           if (TEMPLATE_TYPE_IDX (parm)
   24187        27832 :               < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
   24188              :             return false;
   24189              :         }
   24190      8989247 :       return true;
   24191              :     }
   24192              :   return false;
   24193              : }
   24194              : 
   24195              : /* Adjust types before performing type deduction, as described in
   24196              :    [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
   24197              :    sections are symmetric.  PARM is the type of a function parameter
   24198              :    or the return type of the conversion function.  ARG is the type of
   24199              :    the argument passed to the call, or the type of the value
   24200              :    initialized with the result of the conversion function.
   24201              :    ARG_EXPR is the original argument expression, which may be null.  */
   24202              : 
   24203              : static int
   24204    721972547 : maybe_adjust_types_for_deduction (tree tparms,
   24205              :                                   unification_kind_t strict,
   24206              :                                   tree* parm,
   24207              :                                   tree* arg,
   24208              :                                   tree arg_expr)
   24209              : {
   24210    721972547 :   int result = 0;
   24211              : 
   24212    721972547 :   switch (strict)
   24213              :     {
   24214              :     case DEDUCE_CALL:
   24215              :       break;
   24216              : 
   24217       294200 :     case DEDUCE_CONV:
   24218              :       /* [temp.deduct.conv] First remove a reference type on parm.
   24219              :          DRs 322 & 976 affected this.  */
   24220       294200 :       if (TYPE_REF_P (*parm))
   24221           89 :         *parm = TREE_TYPE (*parm);
   24222              : 
   24223              :       /* Swap PARM and ARG throughout the remainder of this
   24224              :          function; the handling is precisely symmetric since PARM
   24225              :          will initialize ARG rather than vice versa.  */
   24226              :       std::swap (parm, arg);
   24227              : 
   24228              :       break;
   24229              : 
   24230     33784688 :     case DEDUCE_EXACT:
   24231              :       /* Core issue #873: Do the DR606 thing (see below) for these cases,
   24232              :          too, but here handle it by stripping the reference from PARM
   24233              :          rather than by adding it to ARG.  */
   24234     33784688 :       if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   24235      1125741 :           && TYPE_REF_P (*arg)
   24236     34802234 :           && !TYPE_REF_IS_RVALUE (*arg))
   24237      1017483 :         *parm = TREE_TYPE (*parm);
   24238              :       /* Nothing else to do in this case.  */
   24239              :       return 0;
   24240              : 
   24241            0 :     default:
   24242            0 :       gcc_unreachable ();
   24243              :     }
   24244              : 
   24245    688187859 :   if (!TYPE_REF_P (*parm))
   24246              :     {
   24247              :       /* [temp.deduct.call]
   24248              : 
   24249              :          If P is not a reference type:
   24250              : 
   24251              :          --If A is an array type, the pointer type produced by the
   24252              :          array-to-pointer standard conversion (_conv.array_) is
   24253              :          used in place of A for type deduction; otherwise,
   24254              : 
   24255              :          --If A is a function type, the pointer type produced by
   24256              :          the function-to-pointer standard conversion
   24257              :          (_conv.func_) is used in place of A for type deduction;
   24258              :          otherwise,
   24259              : 
   24260              :          --If A is a cv-qualified type, the top level
   24261              :          cv-qualifiers of A's type are ignored for type
   24262              :          deduction.  */
   24263     77084776 :       if (TREE_CODE (*arg) == ARRAY_TYPE)
   24264       737346 :         *arg = build_pointer_type (TREE_TYPE (*arg));
   24265     76347430 :       else if (TREE_CODE (*arg) == FUNCTION_TYPE)
   24266         3027 :         *arg = build_pointer_type (*arg);
   24267              :       else
   24268     76344403 :         *arg = TYPE_MAIN_VARIANT (*arg);
   24269              :     }
   24270              : 
   24271              :   /* [temp.deduct.call], "If P is a forwarding reference and the argument is
   24272              :      an lvalue, the type 'lvalue reference to A' is used in place of A for
   24273              :      type deduction."  */
   24274    688187859 :   if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   24275    688187859 :       && (arg_expr ? lvalue_p (arg_expr)
   24276              :           /* try_one_overload doesn't provide an arg_expr, but
   24277              :              functions are always lvalues.  */
   24278           47 :           : TREE_CODE (*arg) == FUNCTION_TYPE))
   24279      5036189 :     *arg = build_reference_type (*arg);
   24280              : 
   24281              :   /* [temp.deduct.call]
   24282              : 
   24283              :      If P is a cv-qualified type, the top level cv-qualifiers
   24284              :      of P's type are ignored for type deduction.  If P is a
   24285              :      reference type, the type referred to by P is used for
   24286              :      type deduction.  */
   24287    688187859 :   *parm = TYPE_MAIN_VARIANT (*parm);
   24288    688187859 :   if (TYPE_REF_P (*parm))
   24289              :     {
   24290    611103083 :       *parm = TREE_TYPE (*parm);
   24291    611103083 :       result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   24292              :     }
   24293              : 
   24294              :   return result;
   24295              : }
   24296              : 
   24297              : /* Return true if computing a conversion from FROM to TO might consider
   24298              :    user-defined conversions, which could lead to arbitrary template
   24299              :    instantiations (e.g. g++.dg/cpp2a/concepts-nondep1.C).  If this predicate
   24300              :    returns false then computing the conversion definitely won't try UDCs.
   24301              : 
   24302              :    Note that this restriction parallels LOOKUP_DEFAULTED for CWG1092, but in
   24303              :    this case we want the early filter to pass instead of fail.  */
   24304              : 
   24305              : static bool
   24306      8476876 : conversion_may_instantiate_p (tree to, tree from)
   24307              : {
   24308      8476876 :   to = non_reference (to);
   24309      8476876 :   from = non_reference (from);
   24310              : 
   24311              :   /* Converting between reference-related types is a standard conversion.  */
   24312      8476876 :   if (reference_related_p (to, from))
   24313              :     return false;
   24314              : 
   24315              :   /* Converting to a non-aggregate class type will consider its
   24316              :      user-declared constructors, which might induce instantiation.  */
   24317      9020930 :   if (CLASS_TYPE_P (complete_type (to))
   24318      6088730 :       && type_has_converting_constructor (to))
   24319              :     return true;
   24320              : 
   24321              :   /* Similarly, converting from a class type will consider its conversion
   24322              :      functions.  */
   24323      7229376 :   if (CLASS_TYPE_P (complete_type (from))
   24324      5129588 :       && TYPE_HAS_CONVERSION (from))
   24325              :     return true;
   24326              : 
   24327              :   /* Otherwise, computing this conversion won't risk arbitrary
   24328              :      template instantiation.  */
   24329              :   return false;
   24330              : }
   24331              : 
   24332              : /* Subroutine of fn_type_unification.  PARM is a function parameter of a
   24333              :    template which doesn't contain any deducible template parameters; check if
   24334              :    ARG is a suitable match for it.  STRICT, FLAGS and EXPLAIN_P are as in
   24335              :    unify_one_argument.  */
   24336              : 
   24337              : static int
   24338     37852884 : check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
   24339              :                                 int flags, struct conversion **conv_p,
   24340              :                                 bool explain_p, bool noninst_only_p)
   24341              : {
   24342     37852884 :   tree type;
   24343              : 
   24344     37852884 :   if (!TYPE_P (arg))
   24345     36854163 :     type = TREE_TYPE (arg);
   24346              :   else
   24347              :     type = arg;
   24348              : 
   24349     37852884 :   if (same_type_p (parm, type))
   24350     35058019 :     return unify_success (explain_p);
   24351              : 
   24352     14128437 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   24353     14128437 :   if (strict == DEDUCE_CONV)
   24354              :     {
   24355           27 :       if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
   24356     35058019 :         return unify_success (explain_p);
   24357              :     }
   24358     14128410 :   else if (strict == DEDUCE_CALL)
   24359              :     {
   24360     14128410 :       if (conv_p && *conv_p)
   24361              :         {
   24362              :           /* This conversion was already computed earlier (when
   24363              :              computing only non-instantiating conversions).  */
   24364      4651153 :           gcc_checking_assert (!noninst_only_p);
   24365     35058019 :           return unify_success (explain_p);
   24366              :         }
   24367              : 
   24368      9477257 :       if (noninst_only_p
   24369      9477257 :           && conversion_may_instantiate_p (parm, type))
   24370     35058019 :         return unify_success (explain_p);
   24371              : 
   24372      8098715 :       bool ok = false;
   24373      8098715 :       tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
   24374      8098715 :       if (conv_p)
   24375              :         /* Avoid recalculating this in add_function_candidate.  */
   24376     16196950 :         ok = (*conv_p
   24377      8098475 :               = good_conversion (parm, type, conv_arg, flags, complain));
   24378              :       else
   24379          240 :         ok = can_convert_arg (parm, type, conv_arg, flags, complain);
   24380      8098715 :       if (ok)
   24381     35058019 :         return unify_success (explain_p);
   24382              :     }
   24383              : 
   24384      2794865 :   if (strict == DEDUCE_EXACT)
   24385            0 :     return unify_type_mismatch (explain_p, parm, arg);
   24386              :   else
   24387      2794865 :     return unify_arg_conversion (explain_p, parm, type, arg);
   24388              : }
   24389              : 
   24390              : static bool uses_deducible_template_parms (tree type);
   24391              : 
   24392              : /* Returns true iff the expression EXPR is one from which a template
   24393              :    argument can be deduced.  In other words, if it's an undecorated
   24394              :    use of a template non-type parameter.  */
   24395              : 
   24396              : static bool
   24397   1215760659 : deducible_expression (tree expr)
   24398              : {
   24399              :   /* Strip implicit conversions and implicit INDIRECT_REFs.  */
   24400   2431523520 :   while (CONVERT_EXPR_P (expr)
   24401   1215762822 :          || TREE_CODE (expr) == VIEW_CONVERT_EXPR
   24402   1215760937 :          || TREE_CODE (expr) == IMPLICIT_CONV_EXPR
   24403   2431526021 :          || REFERENCE_REF_P (expr))
   24404         4639 :     expr = TREE_OPERAND (expr, 0);
   24405   1215760659 :   return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
   24406              : }
   24407              : 
   24408              : /* Returns true iff the array domain DOMAIN uses a template parameter in a
   24409              :    deducible way; that is, if it has a max value of <PARM> - 1.  */
   24410              : 
   24411              : static bool
   24412       365248 : deducible_array_bound (tree domain)
   24413              : {
   24414       365248 :   if (domain == NULL_TREE)
   24415              :     return false;
   24416              : 
   24417       365179 :   tree max = TYPE_MAX_VALUE (domain);
   24418       365179 :   if (TREE_CODE (max) != MINUS_EXPR)
   24419              :     return false;
   24420              : 
   24421       364193 :   return deducible_expression (TREE_OPERAND (max, 0));
   24422              : }
   24423              : 
   24424              : /* Returns true iff the template arguments ARGS use a template parameter
   24425              :    in a deducible way.  */
   24426              : 
   24427              : static bool
   24428    618019627 : deducible_template_args (tree args)
   24429              : {
   24430    644359507 :   for (tree elt : tree_vec_range (args))
   24431              :     {
   24432    628097657 :       bool deducible;
   24433    628097657 :       if (ARGUMENT_PACK_P (elt))
   24434     29640412 :         deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
   24435              :       else
   24436              :         {
   24437    598457245 :           if (PACK_EXPANSION_P (elt))
   24438     29261664 :             elt = PACK_EXPANSION_PATTERN (elt);
   24439    598457245 :           if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
   24440              :             deducible = true;
   24441    598457191 :           else if (TYPE_P (elt))
   24442    596568663 :             deducible = uses_deducible_template_parms (elt);
   24443              :           else
   24444      1888528 :             deducible = deducible_expression (elt);
   24445              :         }
   24446    628097603 :       if (deducible)
   24447    601757777 :         return true;
   24448              :     }
   24449     16261850 :   return false;
   24450              : }
   24451              : 
   24452              : /* Returns true iff TYPE contains any deducible references to template
   24453              :    parameters, as per 14.8.2.5.  */
   24454              : 
   24455              : static bool
   24456   1350189863 : uses_deducible_template_parms (tree type)
   24457              : {
   24458   2002598481 :   if (PACK_EXPANSION_P (type))
   24459           90 :     type = PACK_EXPANSION_PATTERN (type);
   24460              : 
   24461              :   /* T
   24462              :      cv-list T
   24463              :      TT<T>
   24464              :      TT<i>
   24465              :      TT<> */
   24466   2002598481 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   24467   1314806295 :       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24468              :     return true;
   24469              : 
   24470              :   /* T*
   24471              :      T&
   24472              :      T&&  */
   24473   1314805481 :   if (INDIRECT_TYPE_P (type))
   24474    652408618 :     return uses_deducible_template_parms (TREE_TYPE (type));
   24475              : 
   24476              :   /* T[integer-constant ]
   24477              :      type [i]  */
   24478    662396863 :   if (TREE_CODE (type) == ARRAY_TYPE)
   24479      2150338 :     return (uses_deducible_template_parms (TREE_TYPE (type))
   24480      2515087 :             || deducible_array_bound (TYPE_DOMAIN (type)));
   24481              : 
   24482              :   /* T type ::*
   24483              :      type T::*
   24484              :      T T::*
   24485              :      T (type ::*)()
   24486              :      type (T::*)()
   24487              :      type (type ::*)(T)
   24488              :      type (T::*)(T)
   24489              :      T (type ::*)(T)
   24490              :      T (T::*)()
   24491              :      T (T::*)(T) */
   24492    660246525 :   if (TYPE_PTRMEM_P (type))
   24493        22647 :     return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
   24494        22845 :             || (uses_deducible_template_parms
   24495          198 :                 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
   24496              : 
   24497              :   /* template-name <T> (where template-name refers to a class template)
   24498              :      template-name <i> (where template-name refers to a class template) */
   24499    595279897 :   if (CLASS_TYPE_P (type)
   24500    595279885 :       && CLASSTYPE_TEMPLATE_INFO (type)
   24501   1249623059 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
   24502    588379215 :     return deducible_template_args (INNERMOST_TEMPLATE_ARGS
   24503    588379215 :                                     (CLASSTYPE_TI_ARGS (type)));
   24504              : 
   24505              :   /* type (T)
   24506              :      T()
   24507              :      T(T)  */
   24508     71844663 :   if (FUNC_OR_METHOD_TYPE_P (type))
   24509              :     {
   24510     12343175 :       if (uses_deducible_template_parms (TREE_TYPE (type)))
   24511              :         return true;
   24512        59177 :       tree parm = TYPE_ARG_TYPES (type);
   24513        59177 :       if (TREE_CODE (type) == METHOD_TYPE)
   24514          139 :         parm = TREE_CHAIN (parm);
   24515        62712 :       for (; parm; parm = TREE_CHAIN (parm))
   24516        60493 :         if (uses_deducible_template_parms (TREE_VALUE (parm)))
   24517              :           return true;
   24518         2219 :       if (flag_noexcept_type
   24519         2202 :           && TYPE_RAISES_EXCEPTIONS (type)
   24520           18 :           && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
   24521         2237 :           && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
   24522              :         return true;
   24523              :     }
   24524              : 
   24525              :   return false;
   24526              : }
   24527              : 
   24528              : /* Subroutine of type_unification_real and unify_pack_expansion to
   24529              :    handle unification of a single P/A pair.  Parameters are as
   24530              :    for those functions.  */
   24531              : 
   24532              : static int
   24533    780944679 : unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
   24534              :                     int subr, unification_kind_t strict,
   24535              :                     bool explain_p)
   24536              : {
   24537    780944679 :   tree arg_expr = NULL_TREE;
   24538    780944679 :   int arg_strict;
   24539              : 
   24540    780944679 :   if (arg == error_mark_node || parm == error_mark_node)
   24541           81 :     return unify_invalid (explain_p);
   24542    780944667 :   if (arg == unknown_type_node)
   24543              :     /* We can't deduce anything from this, but we might get all the
   24544              :        template args from other function args.  */
   24545     50861127 :     return unify_success (explain_p);
   24546              : 
   24547              :   /* Implicit conversions (Clause 4) will be performed on a function
   24548              :      argument to convert it to the type of the corresponding function
   24549              :      parameter if the parameter type contains no template-parameters that
   24550              :      participate in template argument deduction.  */
   24551    780944667 :   if (strict != DEDUCE_EXACT
   24552    780944667 :       && TYPE_P (parm) && !uses_deducible_template_parms (parm))
   24553              :     /* For function parameters with no deducible template parameters,
   24554              :        just return.  We'll check non-dependent conversions later.  */
   24555     50861127 :     return unify_success (explain_p);
   24556              : 
   24557    730170404 :   switch (strict)
   24558              :     {
   24559              :     case DEDUCE_CALL:
   24560              :       arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
   24561              :                     | UNIFY_ALLOW_MORE_CV_QUAL
   24562              :                     | UNIFY_ALLOW_DERIVED);
   24563              :       break;
   24564              : 
   24565              :     case DEDUCE_CONV:
   24566              :       arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
   24567              :       break;
   24568              : 
   24569              :     case DEDUCE_EXACT:
   24570              :       arg_strict = UNIFY_ALLOW_NONE;
   24571              :       break;
   24572              : 
   24573            0 :     default:
   24574            0 :       gcc_unreachable ();
   24575              :     }
   24576              : 
   24577              :   /* We only do these transformations if this is the top-level
   24578              :      parameter_type_list in a call or declaration matching; in other
   24579              :      situations (nested function declarators, template argument lists) we
   24580              :      won't be comparing a type to an expression, and we don't do any type
   24581              :      adjustments.  */
   24582    730170404 :   if (!subr)
   24583              :     {
   24584    722052121 :       if (!TYPE_P (arg))
   24585              :         {
   24586    687973233 :           gcc_assert (TREE_TYPE (arg) != NULL_TREE);
   24587    687973233 :           if (type_unknown_p (arg))
   24588              :             {
   24589              :               /* [temp.deduct.type] A template-argument can be
   24590              :                  deduced from a pointer to function or pointer
   24591              :                  to member function argument if the set of
   24592              :                  overloaded functions does not contain function
   24593              :                  templates and at most one of a set of
   24594              :                  overloaded functions provides a unique
   24595              :                  match.  */
   24596        86864 :               resolve_overloaded_unification (tparms, targs, parm,
   24597              :                                               arg, strict,
   24598              :                                               arg_strict, explain_p);
   24599              :               /* If a unique match was not found, this is a
   24600              :                  non-deduced context, so we still succeed. */
   24601        86864 :               return unify_success (explain_p);
   24602              :             }
   24603              : 
   24604    687886369 :           arg_expr = arg;
   24605    687886369 :           arg = unlowered_expr_type (arg);
   24606    687886369 :           if (arg == error_mark_node)
   24607           81 :             return unify_invalid (explain_p);
   24608              :         }
   24609              : 
   24610    721965257 :       arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   24611              :                                                       &parm, &arg, arg_expr);
   24612              :     }
   24613              :   else
   24614     16236566 :     if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
   24615      8118283 :         != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
   24616           69 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   24617              : 
   24618              :   /* For deduction from an init-list we need the actual list.  */
   24619    721965257 :   if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
   24620         8381 :     arg = arg_expr;
   24621    730083471 :   return unify (tparms, targs, parm, arg, arg_strict, explain_p);
   24622              : }
   24623              : 
   24624              : /* for_each_template_parm callback that always returns 0.  */
   24625              : 
   24626              : static int
   24627     38134622 : zero_r (tree, void *)
   24628              : {
   24629     38134622 :   return 0;
   24630              : }
   24631              : 
   24632              : /* for_each_template_parm any_fn callback to handle deduction of a template
   24633              :    type argument from the type of an array bound.  */
   24634              : 
   24635              : static int
   24636    218579671 : array_deduction_r (tree t, void *data)
   24637              : {
   24638    218579671 :   tree_pair_p d = (tree_pair_p)data;
   24639    218579671 :   tree &tparms = d->purpose;
   24640    218579671 :   tree &targs = d->value;
   24641              : 
   24642    218579671 :   if (TREE_CODE (t) == ARRAY_TYPE)
   24643          256 :     if (tree dom = TYPE_DOMAIN (t))
   24644          144 :       if (tree max = TYPE_MAX_VALUE (dom))
   24645              :         {
   24646          144 :           if (TREE_CODE (max) == MINUS_EXPR)
   24647           14 :             max = TREE_OPERAND (max, 0);
   24648          144 :           if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
   24649           14 :             unify (tparms, targs, TREE_TYPE (max), size_type_node,
   24650              :                    UNIFY_ALLOW_NONE, /*explain*/false);
   24651              :         }
   24652              : 
   24653              :   /* Keep walking.  */
   24654    218579671 :   return 0;
   24655              : }
   24656              : 
   24657              : /* Try to deduce any not-yet-deduced template type arguments from the type of
   24658              :    an array bound.  This is handled separately from unify because 14.8.2.5 says
   24659              :    "The type of a type parameter is only deduced from an array bound if it is
   24660              :    not otherwise deduced."  */
   24661              : 
   24662              : static void
   24663     40169564 : try_array_deduction (tree tparms, tree targs, tree parm)
   24664              : {
   24665     40169564 :   tree_pair_s data = { tparms, targs };
   24666     40169564 :   hash_set<tree> visited;
   24667     40169564 :   for_each_template_parm (parm, zero_r, &data, &visited,
   24668              :                           /*nondeduced*/false, array_deduction_r);
   24669     40169564 : }
   24670              : 
   24671              : /* Most parms like fn_type_unification.
   24672              : 
   24673              :    If SUBR is 1, we're being called recursively (to unify the
   24674              :    arguments of a function or method parameter of a function
   24675              :    template).
   24676              : 
   24677              :    CHECKS is a pointer to a vector of access checks encountered while
   24678              :    substituting default template arguments.  */
   24679              : 
   24680              : static int
   24681    686907194 : type_unification_real (tree tparms,
   24682              :                        tree full_targs,
   24683              :                        tree xparms,
   24684              :                        const tree *xargs,
   24685              :                        unsigned int xnargs,
   24686              :                        int subr,
   24687              :                        unification_kind_t strict,
   24688              :                        vec<deferred_access_check, va_gc> **checks,
   24689              :                        bool explain_p)
   24690              : {
   24691    686907194 :   tree parm, arg;
   24692    686907194 :   int i;
   24693    686907194 :   int ntparms = TREE_VEC_LENGTH (tparms);
   24694    686907194 :   int saw_undeduced = 0;
   24695    686907194 :   tree parms;
   24696    686907194 :   const tree *args;
   24697    686907194 :   unsigned int nargs;
   24698    686907194 :   unsigned int ia;
   24699              : 
   24700    686907194 :   gcc_assert (TREE_CODE (tparms) == TREE_VEC);
   24701    686907194 :   gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
   24702    686907194 :   gcc_assert (ntparms > 0);
   24703              : 
   24704    686907194 :   tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
   24705              : 
   24706              :   /* Reset the number of non-defaulted template arguments contained
   24707              :      in TARGS.  */
   24708    686907194 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
   24709              : 
   24710    686920744 :  again:
   24711    686920744 :   parms = xparms;
   24712    686920744 :   args = xargs;
   24713    686920744 :   nargs = xnargs;
   24714              : 
   24715              :   /* Only fn_type_unification cares about terminal void.  */
   24716    686920744 :   if (nargs && args[nargs-1] == void_type_node)
   24717     13535791 :     --nargs;
   24718              : 
   24719    686920744 :   ia = 0;
   24720   1566249615 :   while (parms && parms != void_list_node
   24721   1667021992 :          && ia < nargs)
   24722              :     {
   24723    773236410 :       parm = TREE_VALUE (parms);
   24724              : 
   24725    773236410 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   24726    773236410 :           && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
   24727              :         /* For a function parameter pack that occurs at the end of the
   24728              :            parameter-declaration-list, the type A of each remaining
   24729              :            argument of the call is compared with the type P of the
   24730              :            declarator-id of the function parameter pack.  */
   24731              :         break;
   24732              : 
   24733    772390513 :       parms = TREE_CHAIN (parms);
   24734              : 
   24735    772390513 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   24736              :         /* For a function parameter pack that does not occur at the
   24737              :            end of the parameter-declaration-list, the type of the
   24738              :            parameter pack is a non-deduced context.  */
   24739           54 :         continue;
   24740              : 
   24741              :       /* [temp.deduct.conv] only applies to the deduction of the return
   24742              :          type, which is always the first argument here.  Other arguments
   24743              :          (notably, explicit object parameters) should undergo normal
   24744              :          call-like unification.  */
   24745    772390459 :       unification_kind_t kind = strict;
   24746    772390459 :       if (strict == DEDUCE_CONV && ia > 0)
   24747           24 :         kind = DEDUCE_CALL;
   24748              : 
   24749    772390459 :       arg = args[ia];
   24750    772390459 :       ++ia;
   24751              : 
   24752    772390459 :       if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind,
   24753              :                               explain_p))
   24754              :         return 1;
   24755              :     }
   24756              : 
   24757    117894080 :   if (parms
   24758    106938358 :       && parms != void_list_node
   24759    122240966 :       && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
   24760              :     {
   24761      1415904 :       gcc_assert (strict != DEDUCE_CONV);
   24762              : 
   24763              :       /* Unify the remaining arguments with the pack expansion type.  */
   24764      1415904 :       tree argvec;
   24765      1415904 :       tree parmvec = make_tree_vec (1);
   24766              : 
   24767              :       /* Allocate a TREE_VEC and copy in all of the arguments */
   24768      1415904 :       argvec = make_tree_vec (nargs - ia);
   24769      3911499 :       for (i = 0; ia < nargs; ++ia, ++i)
   24770      1079691 :         TREE_VEC_ELT (argvec, i) = args[ia];
   24771              : 
   24772              :       /* Copy the parameter into parmvec.  */
   24773      1415904 :       TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
   24774      1415904 :       if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
   24775              :                                 /*subr=*/subr, explain_p))
   24776              :         return 1;
   24777              : 
   24778              :       /* Advance to the end of the list of parameters.  */
   24779      1415755 :       parms = TREE_CHAIN (parms);
   24780              :     }
   24781              : 
   24782              :   /* Fail if we've reached the end of the parm list, and more args
   24783              :      are present, and the parm list isn't variadic.  */
   24784    117893931 :   if (ia < nargs && parms == void_list_node)
   24785        47158 :     return unify_too_many_arguments (explain_p, nargs, ia);
   24786              :   /* Fail if parms are left and they don't have default values and
   24787              :      they aren't all deduced as empty packs (c++/57397).  This is
   24788              :      consistent with sufficient_parms_p.  */
   24789    106890933 :   if (parms && parms != void_list_node
   24790    120777764 :       && TREE_PURPOSE (parms) == NULL_TREE)
   24791              :     {
   24792              :       unsigned int count = nargs;
   24793              :       tree p = parms;
   24794        23698 :       bool type_pack_p;
   24795        23698 :       do
   24796              :         {
   24797        23698 :           type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
   24798        23698 :           if (!type_pack_p)
   24799        23690 :             count++;
   24800        23698 :           p = TREE_CHAIN (p);
   24801              :         }
   24802        23698 :       while (p && p != void_list_node);
   24803        23679 :       if (count != nargs)
   24804        23673 :         return unify_too_few_arguments (explain_p, ia, count,
   24805        23673 :                                         type_pack_p);
   24806              :     }
   24807              : 
   24808    117823100 :   if (!subr)
   24809              :     {
   24810    117556976 :       tsubst_flags_t complain = (explain_p
   24811    117558255 :                                  ? tf_warning_or_error
   24812              :                                  : tf_none);
   24813    117558255 :       bool tried_array_deduction = (cxx_dialect < cxx17);
   24814              : 
   24815    296248008 :       for (i = 0; i < ntparms; i++)
   24816              :         {
   24817    178882944 :           tree targ = TREE_VEC_ELT (targs, i);
   24818    178882944 :           tree tparm = TREE_VEC_ELT (tparms, i);
   24819              : 
   24820              :           /* Clear the "incomplete" flags on all argument packs now so that
   24821              :              substituting them into later default arguments works.  */
   24822    178882944 :           if (targ && ARGUMENT_PACK_P (targ))
   24823              :             {
   24824      8756735 :               ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
   24825      8756735 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
   24826              :             }
   24827              : 
   24828     51794557 :           if (targ || tparm == error_mark_node)
   24829    135845122 :             continue;
   24830     43037822 :           tparm = TREE_VALUE (tparm);
   24831              : 
   24832     43037822 :           if (TREE_CODE (tparm) == TYPE_DECL
   24833     40603816 :               && !tried_array_deduction)
   24834              :             {
   24835     40169511 :               try_array_deduction (tparms, targs, xparms);
   24836     40169511 :               tried_array_deduction = true;
   24837     40169511 :               if (TREE_VEC_ELT (targs, i))
   24838            3 :                 continue;
   24839              :             }
   24840              : 
   24841              :           /* If this is an undeduced nontype parameter that depends on
   24842              :              a type parameter, try another pass; its type may have been
   24843              :              deduced from a later argument than the one from which
   24844              :              this parameter can be deduced.  */
   24845     43075123 :           if (TREE_CODE (tparm) == PARM_DECL
   24846      2433971 :               && !is_auto (TREE_TYPE (tparm))
   24847      2433744 :               && uses_template_parms (TREE_TYPE (tparm))
   24848     43098474 :               && saw_undeduced < 2)
   24849              :             {
   24850        37304 :               saw_undeduced = 1;
   24851        37304 :               continue;
   24852              :             }
   24853              : 
   24854              :           /* Core issue #226 (C++0x) [temp.deduct]:
   24855              : 
   24856              :              If a template argument has not been deduced, its
   24857              :              default template argument, if any, is used.
   24858              : 
   24859              :              When we are in C++98 mode, TREE_PURPOSE will either
   24860              :              be NULL_TREE or ERROR_MARK_NODE, so we do not need
   24861              :              to explicitly check cxx_dialect here.  */
   24862     43000515 :           if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
   24863              :             /* OK, there is a default argument.  Wait until after the
   24864              :                conversion check to do substitution.  */
   24865     42714852 :             continue;
   24866              : 
   24867              :           /* If the type parameter is a parameter pack, then it will
   24868              :              be deduced to an empty parameter pack.  */
   24869       285663 :           if (template_parameter_pack_p (tparm))
   24870              :             {
   24871        92472 :               tree arg;
   24872              : 
   24873        92472 :               if (TREE_CODE (tparm) == PARM_DECL)
   24874              :                 {
   24875         1117 :                   arg = make_node (NONTYPE_ARGUMENT_PACK);
   24876         1117 :                   TREE_CONSTANT (arg) = 1;
   24877              :                 }
   24878              :               else
   24879        91355 :                 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   24880              : 
   24881        92472 :               ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   24882              : 
   24883        92472 :               TREE_VEC_ELT (targs, i) = arg;
   24884        92472 :               continue;
   24885        92472 :             }
   24886              : 
   24887       193191 :           return unify_parameter_deduction_failure (explain_p, tparm);
   24888              :         }
   24889              : 
   24890              :       /* During partial ordering, we deduce dependent template args.  */
   24891              :       bool any_dependent_targs = false;
   24892              : 
   24893              :       /* Now substitute into the default template arguments.  */
   24894    268160269 :       for (i = 0; i < ntparms; i++)
   24895              :         {
   24896    178294916 :           tree targ = TREE_VEC_ELT (targs, i);
   24897    178294916 :           tree tparm = TREE_VEC_ELT (tparms, i);
   24898              : 
   24899    178294916 :           if (targ)
   24900              :             {
   24901    135834278 :               if (!any_dependent_targs && dependent_template_arg_p (targ))
   24902              :                 any_dependent_targs = true;
   24903    135834278 :               continue;
   24904              :             }
   24905     42460638 :           if (tparm == error_mark_node)
   24906            0 :             continue;
   24907              : 
   24908     42460638 :           tree parm = TREE_VALUE (tparm);
   24909     42460638 :           tree arg = TREE_PURPOSE (tparm);
   24910     42460638 :           reopen_deferring_access_checks (*checks);
   24911     42460638 :           location_t save_loc = input_location;
   24912     42460638 :           if (DECL_P (parm))
   24913     42460634 :             input_location = DECL_SOURCE_LOCATION (parm);
   24914              : 
   24915     42460638 :           if (saw_undeduced == 1
   24916        41893 :               && TREE_CODE (parm) == PARM_DECL
   24917        37804 :               && !is_auto (TREE_TYPE (parm))
   24918     42498442 :               && uses_template_parms (TREE_TYPE (parm)))
   24919              :             {
   24920              :               /* The type of this non-type parameter depends on undeduced
   24921              :                  parameters.  Don't try to use its default argument yet,
   24922              :                  since we might deduce an argument for it on the next pass,
   24923              :                  but do check whether the arguments we already have cause
   24924              :                  substitution failure, so that that happens before we try
   24925              :                  later default arguments (78489).  */
   24926        37239 :               ++processing_template_decl;
   24927        37239 :               tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
   24928              :                                   NULL_TREE);
   24929        37239 :               --processing_template_decl;
   24930        37239 :               if (type == error_mark_node)
   24931        13862 :                 arg = error_mark_node;
   24932              :               else
   24933              :                 arg = NULL_TREE;
   24934              :             }
   24935              :           else
   24936              :             {
   24937              :               /* Even if the call is happening in template context, getting
   24938              :                  here means it's non-dependent, and a default argument is
   24939              :                  considered a separate definition under [temp.decls], so we can
   24940              :                  do this substitution without processing_template_decl.  This
   24941              :                  is important if the default argument contains something that
   24942              :                  might be instantiation-dependent like access (87480).  */
   24943     42423399 :               processing_template_decl_sentinel s (!any_dependent_targs);
   24944              : 
   24945     42423399 :               tree used_tparms = NULL_TREE;
   24946     42423399 :               if (saw_undeduced == 1)
   24947              :                 {
   24948         4654 :                   tree tparms_list = build_tree_list (size_int (1), tparms);
   24949         4654 :                   used_tparms = find_template_parameters (arg, tparms_list);
   24950        12086 :                   for (; used_tparms; used_tparms = TREE_CHAIN (used_tparms))
   24951              :                     {
   24952         2784 :                       int level, index;
   24953         2784 :                       template_parm_level_and_index (TREE_VALUE (used_tparms),
   24954              :                                                      &level, &index);
   24955         2784 :                       if (TREE_VEC_ELT (targs, index) == NULL_TREE)
   24956              :                         break;
   24957              :                     }
   24958              :                 }
   24959              : 
   24960         4654 :               if (!used_tparms)
   24961              :                 {
   24962              :                   /* All template parameters within this default argument are
   24963              :                      deduced, so we can use it.  */
   24964     42423393 :                   arg = tsubst_template_arg (arg, full_targs, complain,
   24965              :                                              NULL_TREE);
   24966     42423393 :                   arg = convert_template_argument (parm, arg, full_targs,
   24967              :                                                    complain, i, NULL_TREE);
   24968              :                 }
   24969              :               else if (saw_undeduced == 1)
   24970              :                 arg = NULL_TREE;
   24971              :               else if (!any_dependent_targs)
   24972              :                 arg = error_mark_node;
   24973     42423399 :             }
   24974              : 
   24975     42460638 :           input_location = save_loc;
   24976     42460638 :           *checks = get_deferred_access_checks ();
   24977     42460638 :           pop_deferring_access_checks ();
   24978              : 
   24979     42460638 :           if (arg == error_mark_node)
   24980              :             return 1;
   24981     14960927 :           else if (arg)
   24982              :             {
   24983     14937544 :               TREE_VEC_ELT (targs, i) = arg;
   24984              :               /* The position of the first default template argument,
   24985              :                  is also the number of non-defaulted arguments in TARGS.
   24986              :                  Record that.  */
   24987     14937544 :               if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   24988     12397692 :                 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
   24989              :             }
   24990              :         }
   24991              : 
   24992     89865353 :       if (saw_undeduced++ == 1)
   24993        13550 :         goto again;
   24994              :     }
   24995              : 
   24996     90116648 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   24997     77501250 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
   24998              : 
   24999     90116648 :   return unify_success (explain_p);
   25000              : }
   25001              : 
   25002              : /* Subroutine of type_unification_real.  Args are like the variables
   25003              :    at the call site.  ARG is an overloaded function (or template-id);
   25004              :    we try deducing template args from each of the overloads, and if
   25005              :    only one succeeds, we go with that.  Modifies TARGS and returns
   25006              :    true on success.  */
   25007              : 
   25008              : static bool
   25009        86864 : resolve_overloaded_unification (tree tparms,
   25010              :                                 tree targs,
   25011              :                                 tree parm,
   25012              :                                 tree arg,
   25013              :                                 unification_kind_t strict,
   25014              :                                 int sub_strict,
   25015              :                                 bool explain_p)
   25016              : {
   25017        86864 :   tree tempargs = copy_node (targs);
   25018        86864 :   int good = 0;
   25019        86864 :   tree goodfn = NULL_TREE;
   25020        86864 :   bool addr_p;
   25021              : 
   25022        86864 :   if (TREE_CODE (arg) == ADDR_EXPR)
   25023              :     {
   25024          658 :       arg = TREE_OPERAND (arg, 0);
   25025          658 :       addr_p = true;
   25026              :     }
   25027              :   else
   25028              :     addr_p = false;
   25029              : 
   25030        86864 :   if (TREE_CODE (arg) == COMPONENT_REF)
   25031              :     /* Handle `&x' where `x' is some static or non-static member
   25032              :        function name.  */
   25033          159 :     arg = TREE_OPERAND (arg, 1);
   25034              : 
   25035        86864 :   if (TREE_CODE (arg) == OFFSET_REF)
   25036          426 :     arg = TREE_OPERAND (arg, 1);
   25037              : 
   25038              :   /* Strip baselink information.  */
   25039        86864 :   if (BASELINK_P (arg))
   25040          591 :     arg = BASELINK_FUNCTIONS (arg);
   25041              : 
   25042        86864 :   if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
   25043              :     {
   25044              :       /* If we got some explicit template args, we need to plug them into
   25045              :          the affected templates before we try to unify, in case the
   25046              :          explicit args will completely resolve the templates in question.  */
   25047              : 
   25048          431 :       int ok = 0;
   25049          431 :       tree expl_subargs = TREE_OPERAND (arg, 1);
   25050          431 :       arg = TREE_OPERAND (arg, 0);
   25051              : 
   25052          919 :       for (lkp_iterator iter (arg); iter; ++iter)
   25053              :         {
   25054          488 :           tree fn = *iter;
   25055          488 :           tree subargs, elem;
   25056              : 
   25057          488 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   25058            0 :             continue;
   25059              : 
   25060          488 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   25061              :                                            expl_subargs, NULL_TREE, tf_none);
   25062          488 :           if (subargs != error_mark_node
   25063          488 :               && !any_dependent_template_arguments_p (subargs))
   25064              :             {
   25065          464 :               fn = instantiate_template (fn, subargs, tf_none);
   25066          464 :               if (!constraints_satisfied_p (fn))
   25067           41 :                 continue;
   25068          423 :               if (undeduced_auto_decl (fn))
   25069              :                 {
   25070              :                   /* Instantiate the function to deduce its return type.  */
   25071           12 :                   ++function_depth;
   25072           12 :                   instantiate_decl (fn, /*defer*/false, /*class*/false);
   25073           12 :                   --function_depth;
   25074              :                 }
   25075              : 
   25076          423 :               if (flag_noexcept_type)
   25077          379 :                 maybe_instantiate_noexcept (fn, tf_none);
   25078              : 
   25079          423 :               elem = TREE_TYPE (fn);
   25080          423 :               if (try_one_overload (tparms, targs, tempargs, parm,
   25081              :                                     elem, strict, sub_strict, addr_p, explain_p)
   25082          423 :                   && (!goodfn || !same_type_p (goodfn, elem)))
   25083              :                 {
   25084          396 :                   goodfn = elem;
   25085          396 :                   ++good;
   25086              :                 }
   25087              :             }
   25088           24 :           else if (subargs)
   25089           24 :             ++ok;
   25090              :         }
   25091              :       /* If no templates (or more than one) are fully resolved by the
   25092              :          explicit arguments, this template-id is a non-deduced context; it
   25093              :          could still be OK if we deduce all template arguments for the
   25094              :          enclosing call through other arguments.  */
   25095          431 :       if (good != 1)
   25096              :         good = ok;
   25097              :     }
   25098        86433 :   else if (!OVL_P (arg))
   25099              :     /* If ARG is, for example, "(0, &f)" then its type will be unknown
   25100              :        -- but the deduction does not succeed because the expression is
   25101              :        not just the function on its own.  */
   25102              :     return false;
   25103              :   else
   25104       173288 :     for (lkp_iterator iter (arg); iter; ++iter)
   25105              :       {
   25106        86855 :         tree fn = *iter;
   25107        86855 :         if (flag_noexcept_type)
   25108        86090 :           maybe_instantiate_noexcept (fn, tf_none);
   25109        86855 :         if (TREE_CODE (fn) == FUNCTION_DECL && !constraints_satisfied_p (fn))
   25110            6 :           continue;
   25111        86849 :         tree elem = TREE_TYPE (fn);
   25112        86849 :         if (try_one_overload (tparms, targs, tempargs, parm, elem,
   25113              :                               strict, sub_strict, addr_p, explain_p)
   25114        86849 :             && (!goodfn || !same_type_p (goodfn, elem)))
   25115              :           {
   25116        86605 :             goodfn = elem;
   25117        86605 :             ++good;
   25118              :           }
   25119              :       }
   25120              : 
   25121              :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   25122              :      to function or pointer to member function argument if the set of
   25123              :      overloaded functions does not contain function templates and at most
   25124              :      one of a set of overloaded functions provides a unique match.
   25125              : 
   25126              :      CWG2918 allows multiple functions to match if they all have the same type,
   25127              :      so that we can choose the most constrained later.
   25128              : 
   25129              :      So if we found multiple possibilities, we return success but don't
   25130              :      deduce anything.  */
   25131              : 
   25132        86504 :   if (good == 1)
   25133              :     {
   25134        86597 :       int i = TREE_VEC_LENGTH (targs);
   25135       334805 :       for (; i--; )
   25136       248208 :         if (TREE_VEC_ELT (tempargs, i))
   25137              :           {
   25138       139434 :             tree old = TREE_VEC_ELT (targs, i);
   25139       139434 :             tree new_ = TREE_VEC_ELT (tempargs, i);
   25140       138780 :             if (new_ && old && ARGUMENT_PACK_P (old)
   25141       139443 :                 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
   25142              :               /* Don't forget explicit template arguments in a pack.  */
   25143            6 :               ARGUMENT_PACK_EXPLICIT_ARGS (new_)
   25144           12 :                 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
   25145       139434 :             TREE_VEC_ELT (targs, i) = new_;
   25146              :           }
   25147              :     }
   25148        86864 :   if (good)
   25149              :     return true;
   25150              : 
   25151              :   return false;
   25152              : }
   25153              : 
   25154              : /* Core DR 115: In contexts where deduction is done and fails, or in
   25155              :    contexts where deduction is not done, if a template argument list is
   25156              :    specified and it, along with any default template arguments, identifies
   25157              :    a single function template specialization, then the template-id is an
   25158              :    lvalue for the function template specialization.  */
   25159              : 
   25160              : tree
   25161   1909685779 : resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
   25162              : {
   25163   1909685779 :   tree expr, offset, baselink;
   25164   1909685779 :   bool addr;
   25165              : 
   25166   1909685779 :   if (!type_unknown_p (orig_expr))
   25167              :     return orig_expr;
   25168              : 
   25169         7107 :   expr = orig_expr;
   25170         7107 :   addr = false;
   25171         7107 :   offset = NULL_TREE;
   25172         7107 :   baselink = NULL_TREE;
   25173              : 
   25174         7107 :   if (TREE_CODE (expr) == ADDR_EXPR)
   25175              :     {
   25176          438 :       expr = TREE_OPERAND (expr, 0);
   25177          438 :       addr = true;
   25178              :     }
   25179         7107 :   if (TREE_CODE (expr) == OFFSET_REF)
   25180              :     {
   25181          108 :       offset = expr;
   25182          108 :       expr = TREE_OPERAND (expr, 1);
   25183              :     }
   25184         7107 :   if (BASELINK_P (expr))
   25185              :     {
   25186          247 :       baselink = expr;
   25187          247 :       expr = BASELINK_FUNCTIONS (expr);
   25188              :     }
   25189              : 
   25190         7107 :   if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
   25191              :     {
   25192         1330 :       int good = 0;
   25193         1330 :       tree goodfn = NULL_TREE;
   25194              : 
   25195              :       /* If we got some explicit template args, we need to plug them into
   25196              :          the affected templates before we try to unify, in case the
   25197              :          explicit args will completely resolve the templates in question.  */
   25198              : 
   25199         1330 :       tree expl_subargs = TREE_OPERAND (expr, 1);
   25200         1330 :       tree arg = TREE_OPERAND (expr, 0);
   25201         1330 :       tree badfn = NULL_TREE;
   25202         1330 :       tree badargs = NULL_TREE;
   25203              : 
   25204         2780 :       for (lkp_iterator iter (arg); iter; ++iter)
   25205              :         {
   25206         1450 :           tree fn = *iter;
   25207         1450 :           tree subargs, elem;
   25208              : 
   25209         1450 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   25210            4 :             continue;
   25211              : 
   25212         1446 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   25213              :                                            expl_subargs, NULL_TREE, tf_none);
   25214         1446 :           if (subargs != error_mark_node
   25215         1446 :               && !any_dependent_template_arguments_p (subargs))
   25216              :             {
   25217         1328 :               elem = instantiate_template (fn, subargs, tf_none);
   25218         1328 :               if (elem == error_mark_node)
   25219              :                 {
   25220              :                   badfn = fn;
   25221              :                   badargs = subargs;
   25222              :                 }
   25223         1289 :               else if (elem && (!goodfn || !decls_match (goodfn, elem))
   25224         2578 :                        && constraints_satisfied_p (elem))
   25225              :                 {
   25226         1244 :                   goodfn = elem;
   25227         1244 :                   ++good;
   25228              :                 }
   25229              :             }
   25230              :         }
   25231         1330 :       if (good == 1)
   25232              :         {
   25233         1160 :           expr = goodfn;
   25234         1160 :           if (baselink)
   25235          188 :             expr = build_baselink (BASELINK_BINFO (baselink),
   25236          188 :                                    BASELINK_ACCESS_BINFO (baselink),
   25237          188 :                                    expr, BASELINK_OPTYPE (baselink));
   25238         1160 :           if (offset)
   25239              :             {
   25240           57 :               tree base
   25241           57 :                 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
   25242           57 :               expr = build_offset_ref (base, expr, addr, complain);
   25243              :             }
   25244         1160 :           if (addr)
   25245          306 :             expr = cp_build_addr_expr (expr, complain);
   25246         1160 :           return expr;
   25247              :         }
   25248          170 :       else if (good == 0 && badargs && (complain & tf_error))
   25249              :         /* There were no good options and at least one bad one, so let the
   25250              :            user know what the problem is.  */
   25251            3 :         instantiate_template (badfn, badargs, complain);
   25252              :     }
   25253              :   return orig_expr;
   25254              : }
   25255              : 
   25256              : /* As above, but error out if the expression remains overloaded.  */
   25257              : 
   25258              : tree
   25259   1385502412 : resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
   25260              : {
   25261   1385502412 :   exp = resolve_nondeduced_context (exp, complain);
   25262   1385502412 :   if (type_unknown_p (exp))
   25263              :     {
   25264          108 :       if (complain & tf_error)
   25265           93 :         cxx_incomplete_type_error (exp, TREE_TYPE (exp));
   25266          108 :       return error_mark_node;
   25267              :     }
   25268              :   return exp;
   25269              : }
   25270              : 
   25271              : /* Subroutine of resolve_overloaded_unification; does deduction for a single
   25272              :    overload.  Fills TARGS with any deduced arguments, or error_mark_node if
   25273              :    different overloads deduce different arguments for a given parm.
   25274              :    ADDR_P is true if the expression for which deduction is being
   25275              :    performed was of the form "& fn" rather than simply "fn".
   25276              : 
   25277              :    Returns 1 on success.  */
   25278              : 
   25279              : static int
   25280        87272 : try_one_overload (tree tparms,
   25281              :                   tree orig_targs,
   25282              :                   tree targs,
   25283              :                   tree parm,
   25284              :                   tree arg,
   25285              :                   unification_kind_t strict,
   25286              :                   int sub_strict,
   25287              :                   bool addr_p,
   25288              :                   bool explain_p)
   25289              : {
   25290        87272 :   int nargs;
   25291        87272 :   tree tempargs;
   25292        87272 :   int i;
   25293              : 
   25294        87272 :   if (arg == error_mark_node)
   25295              :     return 0;
   25296              : 
   25297              :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   25298              :      to function or pointer to member function argument if the set of
   25299              :      overloaded functions does not contain function templates and at most
   25300              :      one of a set of overloaded functions provides a unique match.
   25301              : 
   25302              :      So if this is a template, just return success.  */
   25303              : 
   25304        87271 :   if (uses_template_parms (arg))
   25305              :     return 1;
   25306              : 
   25307         1223 :   if (TREE_CODE (arg) == METHOD_TYPE)
   25308          378 :     arg = build_ptrmemfunc_type (build_pointer_type (arg));
   25309          845 :   else if (addr_p)
   25310          617 :     arg = build_pointer_type (arg);
   25311              : 
   25312         1223 :   sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   25313              :                                                   &parm, &arg, NULL_TREE);
   25314              : 
   25315              :   /* We don't copy orig_targs for this because if we have already deduced
   25316              :      some template args from previous args, unify would complain when we
   25317              :      try to deduce a template parameter for the same argument, even though
   25318              :      there isn't really a conflict.  */
   25319         1223 :   nargs = TREE_VEC_LENGTH (targs);
   25320         1223 :   tempargs = make_tree_vec (nargs);
   25321              : 
   25322         1223 :   if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
   25323              :     return 0;
   25324              : 
   25325              :   /* First make sure we didn't deduce anything that conflicts with
   25326              :      explicitly specified args.  */
   25327         2684 :   for (i = nargs; i--; )
   25328              :     {
   25329         1713 :       tree elt = TREE_VEC_ELT (tempargs, i);
   25330         1713 :       tree oldelt = TREE_VEC_ELT (orig_targs, i);
   25331              : 
   25332         1713 :       if (!elt)
   25333              :         /*NOP*/;
   25334         1135 :       else if (uses_template_parms (elt))
   25335              :         /* Since we're unifying against ourselves, we will fill in
   25336              :            template args used in the function parm list with our own
   25337              :            template parms.  Discard them.  */
   25338            0 :         TREE_VEC_ELT (tempargs, i) = NULL_TREE;
   25339         1135 :       else if (oldelt && ARGUMENT_PACK_P (oldelt))
   25340              :         {
   25341              :           /* Check that the argument at each index of the deduced argument pack
   25342              :              is equivalent to the corresponding explicitly specified argument.
   25343              :              We may have deduced more arguments than were explicitly specified,
   25344              :              and that's OK.  */
   25345              : 
   25346              :           /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
   25347              :              that's wrong if we deduce the same argument pack from multiple
   25348              :              function arguments: it's only incomplete the first time.  */
   25349              : 
   25350           24 :           tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
   25351           24 :           tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
   25352              : 
   25353           24 :           if (TREE_VEC_LENGTH (deduced_pack)
   25354           24 :               < TREE_VEC_LENGTH (explicit_pack))
   25355              :             return 0;
   25356              : 
   25357           30 :           for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
   25358           21 :             if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
   25359           21 :                                       TREE_VEC_ELT (deduced_pack, j)))
   25360              :               return 0;
   25361              :         }
   25362           23 :       else if (oldelt && !template_args_equal (oldelt, elt))
   25363              :         return 0;
   25364              :     }
   25365              : 
   25366         2631 :   for (i = nargs; i--; )
   25367              :     {
   25368         1660 :       tree elt = TREE_VEC_ELT (tempargs, i);
   25369              : 
   25370         1660 :       if (elt)
   25371         1100 :         TREE_VEC_ELT (targs, i) = elt;
   25372              :     }
   25373              : 
   25374              :   return 1;
   25375              : }
   25376              : 
   25377              : /* PARM is a template class (perhaps with unbound template
   25378              :    parameters).  ARG is a fully instantiated type.  If ARG can be
   25379              :    bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
   25380              :    TARGS are as for unify.  */
   25381              : 
   25382              : static tree
   25383    276885967 : try_class_unification (tree tparms, tree targs, tree parm, tree arg,
   25384              :                        bool explain_p)
   25385              : {
   25386    276885967 :   if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   25387              :     return NULL_TREE;
   25388    210550656 :   else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25389              :     /* Matches anything.  */;
   25390    210549888 :   else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
   25391              :     return NULL_TREE;
   25392              : 
   25393              :   /* We need to make a new template argument vector for the call to
   25394              :      unify.  If we used TARGS, we'd clutter it up with the result of
   25395              :      the attempted unification, even if this class didn't work out.
   25396              :      We also don't want to commit ourselves to all the unifications
   25397              :      we've already done, since unification is supposed to be done on
   25398              :      an argument-by-argument basis.  In other words, consider the
   25399              :      following pathological case:
   25400              : 
   25401              :        template <int I, int J, int K>
   25402              :        struct S {};
   25403              : 
   25404              :        template <int I, int J>
   25405              :        struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
   25406              : 
   25407              :        template <int I, int J, int K>
   25408              :        void f(S<I, J, K>, S<I, I, I>);
   25409              : 
   25410              :        void g() {
   25411              :          S<0, 0, 0> s0;
   25412              :          S<0, 1, 2> s2;
   25413              : 
   25414              :          f(s0, s2);
   25415              :        }
   25416              : 
   25417              :      Now, by the time we consider the unification involving `s2', we
   25418              :      already know that we must have `f<0, 0, 0>'.  But, even though
   25419              :      `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
   25420              :      because there are two ways to unify base classes of S<0, 1, 2>
   25421              :      with S<I, I, I>.  If we kept the already deduced knowledge, we
   25422              :      would reject the possibility I=1.  */
   25423     19987521 :   targs = copy_template_args (targs);
   25424     70993189 :   for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
   25425     51005668 :     targ = NULL_TREE;
   25426              : 
   25427     19987521 :   int err;
   25428     19987521 :   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25429          768 :     err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
   25430              :   else
   25431     19986753 :     err = unify (tparms, targs,
   25432     19986753 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   25433     19986753 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)),
   25434              :                  UNIFY_ALLOW_NONE, explain_p);
   25435              : 
   25436     19987521 :   return err ? NULL_TREE : arg;
   25437              : }
   25438              : 
   25439              : /* Given a template type PARM and a class type ARG, find the unique
   25440              :    base type in ARG that is an instance of PARM.  We do not examine
   25441              :    ARG itself; only its base-classes.  If there is not exactly one
   25442              :    appropriate base class, return NULL_TREE.  PARM may be the type of
   25443              :    a partial specialization, as well as a plain template type.  Used
   25444              :    by unify.  */
   25445              : 
   25446              : static enum template_base_result
   25447    223585661 : get_template_base (tree tparms, tree targs, tree parm, tree arg,
   25448              :                    bool explain_p, tree *result)
   25449              : {
   25450    223585661 :   tree rval = NULL_TREE;
   25451    223585661 :   tree binfo;
   25452              : 
   25453    223585661 :   gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
   25454              : 
   25455    223585661 :   binfo = TYPE_BINFO (complete_type (arg));
   25456    223585661 :   if (!binfo)
   25457              :     {
   25458              :       /* The type could not be completed.  */
   25459          146 :       *result = NULL_TREE;
   25460          146 :       return tbr_incomplete_type;
   25461              :     }
   25462              : 
   25463              :   /* Walk in inheritance graph order.  The search order is not
   25464              :      important, and this avoids multiple walks of virtual bases.  */
   25465    258098501 :   for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
   25466              :     {
   25467     69025998 :       tree r = try_class_unification (tparms, targs, parm,
   25468     34512999 :                                       BINFO_TYPE (binfo), explain_p);
   25469              : 
   25470     34512999 :       if (r)
   25471              :         {
   25472              :           /* If there is more than one satisfactory baseclass, then:
   25473              : 
   25474              :                [temp.deduct.call]
   25475              : 
   25476              :               If they yield more than one possible deduced A, the type
   25477              :               deduction fails.
   25478              : 
   25479              :              applies.  */
   25480       709247 :           if (rval && !same_type_p (r, rval))
   25481              :             {
   25482              :               /* [temp.deduct.call]/4.3: If there is a class C that is a
   25483              :                  (direct or indirect) base class of D and derived (directly or
   25484              :                  indirectly) from a class B and that would be a valid deduced
   25485              :                  A, the deduced A cannot be B or pointer to B, respectively. */
   25486           43 :               if (DERIVED_FROM_P (r, rval))
   25487              :                 /* Ignore r.  */
   25488           27 :                 continue;
   25489           16 :               else if (DERIVED_FROM_P (rval, r))
   25490              :                 /* Ignore rval.  */;
   25491              :               else
   25492              :                 {
   25493           13 :                   *result = NULL_TREE;
   25494           13 :                   return tbr_ambiguous_baseclass;
   25495              :                 }
   25496              :             }
   25497              : 
   25498              :           rval = r;
   25499              :         }
   25500              :     }
   25501              : 
   25502    223585502 :   *result = rval;
   25503    223585502 :   return tbr_success;
   25504              : }
   25505              : 
   25506              : /* Returns the level of DECL, which declares a template parameter.  */
   25507              : 
   25508              : static int
   25509    313467090 : template_decl_level (tree decl)
   25510              : {
   25511    313467090 :   switch (TREE_CODE (decl))
   25512              :     {
   25513    296343449 :     case TYPE_DECL:
   25514    296343449 :     case TEMPLATE_DECL:
   25515    296343449 :       return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
   25516              : 
   25517     17123641 :     case PARM_DECL:
   25518     17123641 :       return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
   25519              : 
   25520            0 :     default:
   25521            0 :       gcc_unreachable ();
   25522              :     }
   25523              :   return 0;
   25524              : }
   25525              : 
   25526              : /* Decide whether ARG can be unified with PARM, considering only the
   25527              :    cv-qualifiers of each type, given STRICT as documented for unify.
   25528              :    Returns nonzero iff the unification is OK on that basis.  */
   25529              : 
   25530              : static int
   25531    638830550 : check_cv_quals_for_unify (int strict, tree arg, tree parm)
   25532              : {
   25533    638830550 :   int arg_quals = cp_type_quals (arg);
   25534    638830550 :   int parm_quals = cp_type_quals (parm);
   25535              : 
   25536    638830550 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   25537    302605314 :       && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   25538              :     {
   25539              :       /*  Although a CVR qualifier is ignored when being applied to a
   25540              :           substituted template parameter ([8.3.2]/1 for example), that
   25541              :           does not allow us to unify "const T" with "int&" because both
   25542              :           types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
   25543              :           It is ok when we're allowing additional CV qualifiers
   25544              :           at the outer level [14.8.2.1]/3,1st bullet.  */
   25545    245772992 :       if ((TYPE_REF_P (arg)
   25546    227134775 :            || FUNC_OR_METHOD_TYPE_P (arg))
   25547     18769598 :           && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
   25548              :         return 0;
   25549              : 
   25550    245772685 :       if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
   25551    166647891 :           && (parm_quals & TYPE_QUAL_RESTRICT))
   25552              :         return 0;
   25553              :     }
   25554              : 
   25555    581997921 :   if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   25556    280246135 :       && (arg_quals & parm_quals) != parm_quals)
   25557              :     return 0;
   25558              : 
   25559    636987281 :   if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
   25560    336049496 :       && (parm_quals & arg_quals) != arg_quals)
   25561      2015845 :     return 0;
   25562              : 
   25563              :   return 1;
   25564              : }
   25565              : 
   25566              : /* Determines the LEVEL and INDEX for the template parameter PARM.  */
   25567              : void
   25568  11059951089 : template_parm_level_and_index (tree parm, int* level, int* index)
   25569              : {
   25570  11059951089 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   25571  11059951089 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   25572    338762693 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25573              :     {
   25574  10722207381 :       *index = TEMPLATE_TYPE_IDX (parm);
   25575  10722207381 :       *level = TEMPLATE_TYPE_LEVEL (parm);
   25576              :     }
   25577              :   else
   25578              :     {
   25579    337743708 :       *index = TEMPLATE_PARM_IDX (parm);
   25580    337743708 :       *level = TEMPLATE_PARM_LEVEL (parm);
   25581              :     }
   25582  11059951089 : }
   25583              : 
   25584              : #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP)                    \
   25585              :   do {                                                                  \
   25586              :     if (unify (TP, TA, P, A, S, EP))                                    \
   25587              :       return 1;                                                         \
   25588              :   } while (0)
   25589              : 
   25590              : /* Unifies the remaining arguments in PACKED_ARGS with the pack
   25591              :    expansion at the end of PACKED_PARMS. Returns 0 if the type
   25592              :    deduction succeeds, 1 otherwise. STRICT is the same as in
   25593              :    fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
   25594              :    function call argument list. We'll need to adjust the arguments to make them
   25595              :    types. SUBR tells us if this is from a recursive call to
   25596              :    type_unification_real, or for comparing two template argument
   25597              :    lists. */
   25598              : 
   25599              : static int
   25600      8205845 : unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
   25601              :                       tree packed_args, unification_kind_t strict,
   25602              :                       bool subr, bool explain_p)
   25603              : {
   25604      8205845 :   tree parm
   25605      8205845 :     = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
   25606      8205845 :   tree pattern = PACK_EXPANSION_PATTERN (parm);
   25607      8205845 :   tree pack, packs = NULL_TREE;
   25608      8205845 :   int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
   25609              : 
   25610              :   /* Add in any args remembered from an earlier partial instantiation.  */
   25611      8205845 :   targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
   25612     16411690 :   int levels = TMPL_ARGS_DEPTH (targs);
   25613              : 
   25614      8205845 :   packed_args = expand_template_argument_pack (packed_args);
   25615              : 
   25616      8205845 :   int len = TREE_VEC_LENGTH (packed_args);
   25617              : 
   25618              :   /* Determine the parameter packs we will be deducing from the
   25619              :      pattern, and record their current deductions.  */
   25620     16229011 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
   25621     16411728 :        pack; pack = TREE_CHAIN (pack))
   25622              :     {
   25623      8205883 :       tree parm_pack = TREE_VALUE (pack);
   25624      8205883 :       int idx, level;
   25625              : 
   25626              :       /* Only template parameter packs can be deduced, not e.g. function
   25627              :          parameter packs or __bases or __integer_pack.  */
   25628      8205883 :       if (!TEMPLATE_PARM_P (parm_pack))
   25629       288153 :         continue;
   25630              : 
   25631              :       /* Determine the index and level of this parameter pack.  */
   25632      8205877 :       template_parm_level_and_index (parm_pack, &level, &idx);
   25633      8205877 :       if (level > levels)
   25634       288147 :         continue;
   25635              : 
   25636              :       /* Keep track of the parameter packs and their corresponding
   25637              :          argument packs.  */
   25638     15835460 :       packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
   25639      7917730 :       TREE_TYPE (packs) = make_tree_vec (len - start);
   25640              :     }
   25641              : 
   25642              :   /* Loop through all of the arguments that have not yet been
   25643              :      unified and unify each with the pattern.  */
   25644     16756846 :   for (i = start; i < len; i++)
   25645              :     {
   25646      8554220 :       tree parm;
   25647      8554220 :       bool any_explicit = false;
   25648      8554220 :       tree arg = TREE_VEC_ELT (packed_args, i);
   25649              : 
   25650              :       /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
   25651              :          or the element of its argument pack at the current index if
   25652              :          this argument was explicitly specified.  */
   25653     16820355 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   25654              :         {
   25655      8266135 :           int idx, level;
   25656      8266135 :           tree arg, pargs;
   25657      8266135 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   25658              : 
   25659      8266135 :           arg = NULL_TREE;
   25660      8266135 :           if (TREE_VALUE (pack)
   25661       120858 :               && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
   25662      8266892 :               && (i - start < TREE_VEC_LENGTH (pargs)))
   25663              :             {
   25664          694 :               any_explicit = true;
   25665          694 :               arg = TREE_VEC_ELT (pargs, i - start);
   25666              :             }
   25667     16532270 :           TMPL_ARG (targs, level, idx) = arg;
   25668              :         }
   25669              : 
   25670              :       /* If we had explicit template arguments, substitute them into the
   25671              :          pattern before deduction.  */
   25672      8554220 :       if (any_explicit)
   25673              :         {
   25674              :           /* Some arguments might still be unspecified or dependent.  */
   25675          694 :           bool dependent;
   25676          694 :           ++processing_template_decl;
   25677          694 :           dependent = any_dependent_template_arguments_p (targs);
   25678          694 :           if (!dependent)
   25679          679 :             --processing_template_decl;
   25680         1388 :           parm = tsubst (pattern, targs,
   25681              :                          explain_p ? tf_warning_or_error : tf_none,
   25682              :                          NULL_TREE);
   25683          694 :           if (dependent)
   25684           15 :             --processing_template_decl;
   25685          694 :           if (parm == error_mark_node)
   25686              :             return 1;
   25687              :         }
   25688              :       else
   25689              :         parm = pattern;
   25690              : 
   25691              :       /* Unify the pattern with the current argument.  */
   25692      8554220 :       if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
   25693              :                               explain_p))
   25694              :         return 1;
   25695              : 
   25696              :       /* For each parameter pack, collect the deduced value.  */
   25697     16813911 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   25698              :         {
   25699      8262910 :           int idx, level;
   25700      8262910 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   25701              : 
   25702      8262910 :           TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
   25703     16525820 :             TMPL_ARG (targs, level, idx);
   25704              :         }
   25705              :     }
   25706              : 
   25707              :   /* Verify that the results of unification with the parameter packs
   25708              :      produce results consistent with what we've seen before, and make
   25709              :      the deduced argument packs available.  */
   25710     16117113 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   25711              :     {
   25712      7914505 :       tree old_pack = TREE_VALUE (pack);
   25713      7914505 :       tree new_args = TREE_TYPE (pack);
   25714      7914505 :       int i, len = TREE_VEC_LENGTH (new_args);
   25715      7914505 :       int idx, level;
   25716      7914505 :       bool nondeduced_p = false;
   25717              : 
   25718              :       /* By default keep the original deduced argument pack.
   25719              :          If necessary, more specific code is going to update the
   25720              :          resulting deduced argument later down in this function.  */
   25721      7914505 :       template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   25722     15829010 :       TMPL_ARG (targs, level, idx) = old_pack;
   25723              : 
   25724              :       /* If NEW_ARGS contains any NULL_TREE entries, we didn't
   25725              :          actually deduce anything.  */
   25726     16175054 :       for (i = 0; i < len && !nondeduced_p; ++i)
   25727      8260549 :         if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
   25728         1394 :           nondeduced_p = true;
   25729      7914505 :       if (nondeduced_p)
   25730         1394 :         continue;
   25731              : 
   25732      7913111 :       if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
   25733              :         {
   25734              :           /* If we had fewer function args than explicit template args,
   25735              :              just use the explicits.  */
   25736          299 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   25737          299 :           int explicit_len = TREE_VEC_LENGTH (explicit_args);
   25738          299 :           if (len < explicit_len)
   25739      7913111 :             new_args = explicit_args;
   25740              :         }
   25741              : 
   25742      7913111 :       if (!old_pack)
   25743              :         {
   25744      7720667 :           tree result;
   25745              :           /* Build the deduced *_ARGUMENT_PACK.  */
   25746      7720667 :           if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
   25747              :             {
   25748       182658 :               result = make_node (NONTYPE_ARGUMENT_PACK);
   25749       182658 :               TREE_CONSTANT (result) = 1;
   25750              :             }
   25751              :           else
   25752      7538009 :             result = cxx_make_type (TYPE_ARGUMENT_PACK);
   25753              : 
   25754      7720667 :           ARGUMENT_PACK_ARGS (result) = new_args;
   25755              : 
   25756              :           /* Note the deduced argument packs for this parameter
   25757              :              pack.  */
   25758     15441334 :           TMPL_ARG (targs, level, idx) = result;
   25759              :         }
   25760       192444 :       else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
   25761       192444 :                && (ARGUMENT_PACK_ARGS (old_pack)
   25762          299 :                    == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
   25763              :         {
   25764              :           /* We only had the explicitly-provided arguments before, but
   25765              :              now we have a complete set of arguments.  */
   25766          299 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   25767              : 
   25768          299 :           ARGUMENT_PACK_ARGS (old_pack) = new_args;
   25769          299 :           ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
   25770          299 :           ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
   25771              :         }
   25772              :       else
   25773              :         {
   25774       192145 :           tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
   25775       192145 :           tree old_args = ARGUMENT_PACK_ARGS (old_pack);
   25776       192145 :           temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
   25777              :           /* During template argument deduction for the aggregate deduction
   25778              :              candidate, the number of elements in a trailing parameter pack
   25779              :              is only deduced from the number of remaining function
   25780              :              arguments if it is not otherwise deduced.  */
   25781       192145 :           if (cxx_dialect >= cxx20
   25782       187727 :               && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
   25783              :               /* FIXME This isn't set properly for partial instantiations.  */
   25784           14 :               && TPARMS_PRIMARY_TEMPLATE (tparms)
   25785       192157 :               && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
   25786           12 :             TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
   25787       192145 :           if (!comp_template_args (old_args, new_args,
   25788              :                                    &bad_old_arg, &bad_new_arg))
   25789              :             /* Inconsistent unification of this parameter pack.  */
   25790           21 :             return unify_parameter_pack_inconsistent (explain_p,
   25791              :                                                       bad_old_arg,
   25792              :                                                       bad_new_arg);
   25793       192145 :         }
   25794              :     }
   25795              : 
   25796      8205845 :   return unify_success (explain_p);
   25797              : }
   25798              : 
   25799              : /* Handle unification of the domain of an array.  PARM_DOM and ARG_DOM are
   25800              :    INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs.  The other
   25801              :    parameters and return value are as for unify.  */
   25802              : 
   25803              : static int
   25804       229295 : unify_array_domain (tree tparms, tree targs,
   25805              :                     tree parm_dom, tree arg_dom,
   25806              :                     bool explain_p)
   25807              : {
   25808       229295 :   tree parm_max;
   25809       229295 :   tree arg_max;
   25810       229295 :   bool parm_cst;
   25811       229295 :   bool arg_cst;
   25812              : 
   25813              :   /* Our representation of array types uses "N - 1" as the
   25814              :      TYPE_MAX_VALUE for an array with "N" elements, if "N" is
   25815              :      not an integer constant.  We cannot unify arbitrarily
   25816              :      complex expressions, so we eliminate the MINUS_EXPRs
   25817              :      here.  */
   25818       229295 :   parm_max = TYPE_MAX_VALUE (parm_dom);
   25819       229295 :   parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
   25820       229295 :   if (!parm_cst)
   25821              :     {
   25822       229208 :       gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
   25823       229208 :       parm_max = TREE_OPERAND (parm_max, 0);
   25824              :     }
   25825       229295 :   arg_max = TYPE_MAX_VALUE (arg_dom);
   25826       229295 :   arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
   25827       229295 :   if (!arg_cst)
   25828              :     {
   25829              :       /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
   25830              :          trying to unify the type of a variable with the type
   25831              :          of a template parameter.  For example:
   25832              : 
   25833              :            template <unsigned int N>
   25834              :            void f (char (&) [N]);
   25835              :            int g();
   25836              :            void h(int i) {
   25837              :              char a[g(i)];
   25838              :              f(a);
   25839              :            }
   25840              : 
   25841              :          Here, the type of the ARG will be "int [g(i)]", and
   25842              :          may be a SAVE_EXPR, etc.  */
   25843         2714 :       if (TREE_CODE (arg_max) != MINUS_EXPR)
   25844            6 :         return unify_vla_arg (explain_p, arg_dom);
   25845         2708 :       arg_max = TREE_OPERAND (arg_max, 0);
   25846              :     }
   25847              : 
   25848              :   /* If only one of the bounds used a MINUS_EXPR, compensate
   25849              :      by adding one to the other bound.  */
   25850       229289 :   if (parm_cst && !arg_cst)
   25851            0 :     parm_max = fold_build2_loc (input_location, PLUS_EXPR,
   25852              :                                 integer_type_node,
   25853              :                                 parm_max,
   25854              :                                 integer_one_node);
   25855       229289 :   else if (arg_cst && !parm_cst)
   25856       226494 :     arg_max = fold_build2_loc (input_location, PLUS_EXPR,
   25857              :                                integer_type_node,
   25858              :                                arg_max,
   25859              :                                integer_one_node);
   25860              : 
   25861       229289 :   return unify (tparms, targs, parm_max, arg_max,
   25862       229289 :                 UNIFY_ALLOW_INTEGER, explain_p);
   25863              : }
   25864              : 
   25865              : /* Returns whether T, a P or A in unify, is a type, template or expression.  */
   25866              : 
   25867              : enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
   25868              : 
   25869              : static pa_kind_t
   25870   2364827362 : pa_kind (tree t)
   25871              : {
   25872   2364827362 :   if (PACK_EXPANSION_P (t))
   25873      1245525 :     t = PACK_EXPANSION_PATTERN (t);
   25874   2364827362 :   if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
   25875   2364052259 :       || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
   25876   4728879600 :       || DECL_TYPE_TEMPLATE_P (t))
   25877              :     return pa_tmpl;
   25878   2363512203 :   else if (TYPE_P (t))
   25879              :     return pa_type;
   25880              :   else
   25881    370197683 :     return pa_expr;
   25882              : }
   25883              : 
   25884              : /* Deduce the value of template parameters.  TPARMS is the (innermost)
   25885              :    set of template parameters to a template.  TARGS is the bindings
   25886              :    for those template parameters, as determined thus far; TARGS may
   25887              :    include template arguments for outer levels of template parameters
   25888              :    as well.  PARM is a parameter to a template function, or a
   25889              :    subcomponent of that parameter; ARG is the corresponding argument.
   25890              :    This function attempts to match PARM with ARG in a manner
   25891              :    consistent with the existing assignments in TARGS.  If more values
   25892              :    are deduced, then TARGS is updated.
   25893              : 
   25894              :    Returns 0 if the type deduction succeeds, 1 otherwise.  The
   25895              :    parameter STRICT is a bitwise or of the following flags:
   25896              : 
   25897              :      UNIFY_ALLOW_NONE:
   25898              :        Require an exact match between PARM and ARG.
   25899              :      UNIFY_ALLOW_MORE_CV_QUAL:
   25900              :        Allow the deduced ARG to be more cv-qualified (by qualification
   25901              :        conversion) than ARG.
   25902              :      UNIFY_ALLOW_LESS_CV_QUAL:
   25903              :        Allow the deduced ARG to be less cv-qualified than ARG.
   25904              :      UNIFY_ALLOW_DERIVED:
   25905              :        Allow the deduced ARG to be a template base class of ARG,
   25906              :        or a pointer to a template base class of the type pointed to by
   25907              :        ARG.
   25908              :      UNIFY_ALLOW_INTEGER:
   25909              :        Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
   25910              :        case for more information.
   25911              :      UNIFY_ALLOW_OUTER_LEVEL:
   25912              :        This is the outermost level of a deduction. Used to determine validity
   25913              :        of qualification conversions. A valid qualification conversion must
   25914              :        have const qualified pointers leading up to the inner type which
   25915              :        requires additional CV quals, except at the outer level, where const
   25916              :        is not required [conv.qual]. It would be normal to set this flag in
   25917              :        addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
   25918              :      UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
   25919              :        This is the outermost level of a deduction, and PARM can be more CV
   25920              :        qualified at this point.
   25921              :      UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
   25922              :        This is the outermost level of a deduction, and PARM can be less CV
   25923              :        qualified at this point.  */
   25924              : 
   25925              : static int
   25926   1213507928 : unify (tree tparms, tree targs, tree parm, tree arg, int strict,
   25927              :        bool explain_p)
   25928              : {
   25929   1213507928 :   int idx;
   25930   1213507928 :   tree targ;
   25931   1213507928 :   tree tparm;
   25932   1213507928 :   int strict_in = strict;
   25933   1213501171 :   tsubst_flags_t complain = (explain_p
   25934   1213507928 :                              ? tf_warning_or_error
   25935              :                              : tf_none);
   25936              : 
   25937   1213507928 :   if (arg == error_mark_node)
   25938    435417702 :     return unify_invalid (explain_p);
   25939   1213507928 :   if (arg == unknown_type_node
   25940   1213507928 :       || arg == init_list_type_node)
   25941              :     /* We can't deduce anything from this, but we might get all the
   25942              :        template args from other function args.  */
   25943    447567591 :     return unify_success (explain_p);
   25944              : 
   25945   1213507928 :   if (parm == any_targ_node || arg == any_targ_node)
   25946    447567591 :     return unify_success (explain_p);
   25947              : 
   25948              :   /* Strip conversions that will interfere with NTTP deduction.
   25949              :      I don't think this will do the right thing with respect to types.
   25950              :      But the only case I've seen it in so far has been array bounds, where
   25951              :      signedness is the only information lost, and I think that will be
   25952              :      okay.  VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
   25953              :      finish_id_expression_1, and are also OK.  */
   25954   1213507920 :   if (deducible_expression (parm))
   25955     10026210 :     while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR
   25956     20052676 :            || TREE_CODE (parm) == IMPLICIT_CONV_EXPR)
   25957         4032 :       parm = TREE_OPERAND (parm, 0);
   25958              : 
   25959              :   /* If PARM uses template parameters, then we can't bail out here,
   25960              :      even if ARG == PARM, since we won't record unifications for the
   25961              :      template parameters.  We might need them if we're trying to
   25962              :      figure out which of two things is more specialized.  */
   25963   1213507920 :   if (arg == parm
   25964   1213507920 :       && (DECL_P (parm) || !uses_template_parms (parm)))
   25965     31084863 :     return unify_success (explain_p);
   25966              : 
   25967              :   /* Handle init lists early, so the rest of the function can assume
   25968              :      we're dealing with a type. */
   25969   1182423057 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg))
   25970              :     {
   25971         9376 :       tree elttype;
   25972         9376 :       tree orig_parm = parm;
   25973              : 
   25974         9376 :       if (!is_std_init_list (parm)
   25975         9376 :           && TREE_CODE (parm) != ARRAY_TYPE)
   25976              :         /* We can only deduce from an initializer list argument if the
   25977              :            parameter is std::initializer_list or an array; otherwise this
   25978              :            is a non-deduced context. */
   25979         8845 :         return unify_success (explain_p);
   25980              : 
   25981         2656 :       if (TREE_CODE (parm) == ARRAY_TYPE)
   25982          520 :         elttype = TREE_TYPE (parm);
   25983              :       else
   25984              :         {
   25985         2136 :           elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
   25986              :           /* Deduction is defined in terms of a single type, so just punt
   25987              :              on the (bizarre) std::initializer_list<T...>.  */
   25988         2136 :           if (PACK_EXPANSION_P (elttype))
   25989         8845 :             return unify_success (explain_p);
   25990              :         }
   25991              : 
   25992         2653 :       if (strict != DEDUCE_EXACT
   25993         2653 :           && TYPE_P (elttype)
   25994         5306 :           && !uses_deducible_template_parms (elttype))
   25995              :         /* If ELTTYPE has no deducible template parms, skip deduction from
   25996              :            the list elements.  */;
   25997              :       else
   25998        14389 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
   25999              :           {
   26000         7068 :             tree elt = e.value;
   26001         7068 :             int elt_strict = strict;
   26002              : 
   26003         7068 :             if (elt == error_mark_node)
   26004          467 :               return unify_invalid (explain_p);
   26005              : 
   26006         7068 :             if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
   26007              :               {
   26008         6073 :                 tree type = TREE_TYPE (elt);
   26009         6073 :                 if (type == error_mark_node)
   26010            6 :                   return unify_invalid (explain_p);
   26011              :                 /* It should only be possible to get here for a call.  */
   26012         6067 :                 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
   26013        12134 :                 elt_strict |= maybe_adjust_types_for_deduction
   26014         6067 :                   (tparms, DEDUCE_CALL, &elttype, &type, elt);
   26015         6067 :                 elt = type;
   26016              :               }
   26017              : 
   26018         7062 :           RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
   26019              :                                    explain_p);
   26020              :         }
   26021              : 
   26022         2186 :       if (TREE_CODE (parm) == ARRAY_TYPE
   26023         2186 :           && deducible_array_bound (TYPE_DOMAIN (parm)))
   26024              :         {
   26025              :           /* Also deduce from the length of the initializer list.  */
   26026           64 :           tree max = size_int (count_ctor_elements (arg));
   26027           64 :           tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
   26028           64 :           if (idx == error_mark_node)
   26029          467 :             return unify_invalid (explain_p);
   26030           64 :           return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   26031           64 :                                      idx, explain_p);
   26032              :         }
   26033              : 
   26034              :       /* If the std::initializer_list<T> deduction worked, replace the
   26035              :          deduced A with std::initializer_list<A>.  */
   26036              :       if (orig_parm != parm)
   26037              :         {
   26038              :           idx = TEMPLATE_TYPE_IDX (orig_parm);
   26039              :           targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26040              :           targ = listify (targ);
   26041              :           TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
   26042              :         }
   26043         8845 :       return unify_success (explain_p);
   26044              :     }
   26045              : 
   26046              :   /* If parm and arg aren't the same kind of thing (template, type, or
   26047              :      expression), fail early.  */
   26048   1182413681 :   if (pa_kind (parm) != pa_kind (arg))
   26049    435417702 :     return unify_invalid (explain_p);
   26050              : 
   26051              :   /* Immediately reject some pairs that won't unify because of
   26052              :      cv-qualification mismatches.  */
   26053   1182413521 :   if (TREE_CODE (arg) == TREE_CODE (parm)
   26054    554535336 :       && TYPE_P (arg)
   26055              :       /* It is the elements of the array which hold the cv quals of an array
   26056              :          type, and the elements might be template type parms. We'll check
   26057              :          when we recurse.  */
   26058    378177451 :       && TREE_CODE (arg) != ARRAY_TYPE
   26059              :       /* We check the cv-qualifiers when unifying with template type
   26060              :          parameters below.  We want to allow ARG `const T' to unify with
   26061              :          PARM `T' for example, when computing which of two templates
   26062              :          is more specialized, for example.  */
   26063    377908435 :       && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
   26064   1518619926 :       && !check_cv_quals_for_unify (strict_in, arg, parm))
   26065      2166808 :     return unify_cv_qual_mismatch (explain_p, parm, arg);
   26066              : 
   26067   1180246713 :   if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
   26068    494351189 :       && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
   26069   1485835162 :       && !FUNC_OR_METHOD_TYPE_P (parm))
   26070    305318616 :     strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
   26071              :   /* PMFs recurse at the same level, so don't strip this yet.  */
   26072   1180246713 :   if (!TYPE_PTRMEMFUNC_P (parm))
   26073   1180244055 :     strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
   26074   1180246713 :   strict &= ~UNIFY_ALLOW_DERIVED;
   26075   1180246713 :   strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   26076   1180246713 :   strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
   26077              : 
   26078   1180246713 :   switch (TREE_CODE (parm))
   26079              :     {
   26080              :     case TYPENAME_TYPE:
   26081              :     case SCOPE_REF:
   26082              :     case UNBOUND_CLASS_TEMPLATE:
   26083              :       /* In a type which contains a nested-name-specifier, template
   26084              :          argument values cannot be deduced for template parameters used
   26085              :          within the nested-name-specifier.  */
   26086    447567591 :       return unify_success (explain_p);
   26087              : 
   26088    303442833 :     case TEMPLATE_TYPE_PARM:
   26089    303442833 :     case TEMPLATE_TEMPLATE_PARM:
   26090    303442833 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   26091    303442833 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   26092    303442833 :       if (error_operand_p (tparm))
   26093    435417702 :         return unify_invalid (explain_p);
   26094              : 
   26095    606885658 :       if (TEMPLATE_TYPE_LEVEL (parm)
   26096    303442829 :           != template_decl_level (tparm))
   26097              :         /* The PARM is not one we're trying to unify.  Just check
   26098              :            to see if it matches ARG.  */
   26099              :         {
   26100          261 :           if (TREE_CODE (arg) == TREE_CODE (parm)
   26101          270 :               && (is_auto (parm) ? is_auto (arg)
   26102            9 :                   : same_type_p (parm, arg)))
   26103           39 :             return unify_success (explain_p);
   26104              :           else
   26105          222 :             return unify_type_mismatch (explain_p, parm, arg);
   26106              :         }
   26107    303442568 :       idx = TEMPLATE_TYPE_IDX (parm);
   26108    303442568 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26109    303442568 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
   26110    303442568 :       if (error_operand_p (tparm))
   26111    435417702 :         return unify_invalid (explain_p);
   26112              : 
   26113              :       /* Check for mixed types and values.  */
   26114    303442568 :       if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   26115    302603440 :            && TREE_CODE (tparm) != TYPE_DECL)
   26116    303442568 :           || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   26117       655201 :               && TREE_CODE (tparm) != TEMPLATE_DECL))
   26118            0 :         gcc_unreachable ();
   26119              : 
   26120    303442568 :       if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   26121              :         {
   26122       183927 :           if ((strict_in & UNIFY_ALLOW_DERIVED)
   26123       183927 :               && CLASS_TYPE_P (arg))
   26124              :             {
   26125              :               /* First try to match ARG directly.  */
   26126          768 :               tree t = try_class_unification (tparms, targs, parm, arg,
   26127          768 :                                               explain_p);
   26128          768 :               if (!t)
   26129              :                 {
   26130              :                   /* Otherwise, look for a suitable base of ARG, as below.  */
   26131           40 :                   enum template_base_result r;
   26132           40 :                   r = get_template_base (tparms, targs, parm, arg,
   26133              :                                          explain_p, &t);
   26134           40 :                   if (!t)
   26135           34 :                     return unify_no_common_base (explain_p, r, parm, arg);
   26136            6 :                   arg = t;
   26137              :                 }
   26138              :             }
   26139              :           /* ARG must be constructed from a template class or a template
   26140              :              template parameter.  */
   26141       183159 :           else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
   26142       183159 :                    && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   26143          948 :             return unify_template_deduction_failure (explain_p, parm, arg);
   26144              : 
   26145              :           /* Deduce arguments T, i from TT<T> or TT<i>.  */
   26146       182945 :           if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
   26147              :             return 1;
   26148              : 
   26149       182686 :           arg = TYPE_TI_TEMPLATE (arg);
   26150       182686 :           if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
   26151              :             /* If the template is a template template parameter, use the
   26152              :                TEMPLATE_TEMPLATE_PARM for matching.  */
   26153           41 :             arg = TREE_TYPE (arg);
   26154              : 
   26155              :           /* Fall through to deduce template name.  */
   26156              :         }
   26157              : 
   26158    303441327 :       if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   26159    302786126 :           || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   26160              :         {
   26161              :           /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
   26162              : 
   26163              :           /* Simple cases: Value already set, does match or doesn't.  */
   26164       837887 :           if (targ != NULL_TREE && template_args_equal (targ, arg))
   26165    447567591 :             return unify_success (explain_p);
   26166       819252 :           else if (targ)
   26167        19117 :             return unify_inconsistency (explain_p, parm, targ, arg);
   26168              :         }
   26169              :       else
   26170              :         {
   26171              :           /* If PARM is `const T' and ARG is only `int', we don't have
   26172              :              a match unless we are allowing additional qualification.
   26173              :              If ARG is `const int' and PARM is just `T' that's OK;
   26174              :              that binds `const int' to `T'.  */
   26175    302603440 :           if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
   26176              :                                          arg, parm))
   26177      1691078 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   26178              : 
   26179              :           /* Consider the case where ARG is `const volatile int' and
   26180              :              PARM is `const T'.  Then, T should be `volatile int'.  */
   26181    601824724 :           arg = cp_build_qualified_type
   26182    300912362 :             (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
   26183    300912362 :           if (arg == error_mark_node)
   26184    435417702 :             return unify_invalid (explain_p);
   26185              : 
   26186              :           /* Simple cases: Value already set, does match or doesn't.  */
   26187    300912362 :           if (targ != NULL_TREE && same_type_p (targ, arg))
   26188    447567591 :             return unify_success (explain_p);
   26189    272594510 :           else if (targ)
   26190      1241006 :             return unify_inconsistency (explain_p, parm, targ, arg);
   26191              : 
   26192              :           /* Make sure that ARG is not a variable-sized array.  (Note
   26193              :              that were talking about variable-sized arrays (like
   26194              :              `int[n]'), rather than arrays of unknown size (like
   26195              :              `int[]').)  We'll get very confused by such a type since
   26196              :              the bound of the array is not constant, and therefore
   26197              :              not mangleable.  Besides, such types are not allowed in
   26198              :              ISO C++, so we can do as we please here.  We do allow
   26199              :              them for 'auto' deduction, since that isn't ABI-exposed.  */
   26200    271353504 :           if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
   26201           14 :             return unify_vla_arg (explain_p, arg);
   26202              : 
   26203              :           /* Strip typedefs as in convert_template_argument.  */
   26204    271353490 :           arg = canonicalize_type_argument (arg, tf_none);
   26205              :         }
   26206              : 
   26207              :       /* If ARG is a parameter pack or an expansion, we cannot unify
   26208              :          against it unless PARM is also a parameter pack.  */
   26209    544306610 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   26210    273330790 :           && !template_parameter_pack_p (parm))
   26211       507859 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   26212              : 
   26213              :       /* If the argument deduction results is a METHOD_TYPE,
   26214              :          then there is a problem.
   26215              :          METHOD_TYPE doesn't map to any real C++ type the result of
   26216              :          the deduction cannot be of that type.  */
   26217    271645766 :       if (TREE_CODE (arg) == METHOD_TYPE)
   26218            6 :         return unify_method_type_error (explain_p, arg);
   26219              : 
   26220    271645760 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   26221    271645760 :       return unify_success (explain_p);
   26222              : 
   26223     10024261 :     case TEMPLATE_PARM_INDEX:
   26224     10024261 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   26225     10024261 :       if (error_operand_p (tparm))
   26226    435417702 :         return unify_invalid (explain_p);
   26227              : 
   26228     10024261 :       if (TEMPLATE_PARM_LEVEL (parm)
   26229     10024261 :           != template_decl_level (tparm))
   26230              :         {
   26231              :           /* The PARM is not one we're trying to unify.  Just check
   26232              :              to see if it matches ARG.  */
   26233            3 :           int result = !(TREE_CODE (arg) == TREE_CODE (parm)
   26234            0 :                          && cp_tree_equal (parm, arg));
   26235            3 :           if (result)
   26236            3 :             unify_expression_unequal (explain_p, parm, arg);
   26237            3 :           return result;
   26238              :         }
   26239              : 
   26240     10024258 :       idx = TEMPLATE_PARM_IDX (parm);
   26241     10024258 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26242              : 
   26243     10024258 :       if (targ)
   26244              :         {
   26245         7386 :           if ((strict & UNIFY_ALLOW_INTEGER)
   26246         1195 :               && TREE_TYPE (targ) && TREE_TYPE (arg)
   26247         8581 :               && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
   26248              :             /* We're deducing from an array bound, the type doesn't matter.
   26249              :                This conversion should match the one below.  */
   26250         1195 :             arg = fold (build_nop (TREE_TYPE (targ), arg));
   26251         7386 :           int x = !cp_tree_equal (targ, arg);
   26252         7386 :           if (x)
   26253          577 :             unify_inconsistency (explain_p, parm, targ, arg);
   26254         7386 :           return x;
   26255              :         }
   26256              : 
   26257              :       /* [temp.deduct.type] If, in the declaration of a function template
   26258              :          with a non-type template-parameter, the non-type
   26259              :          template-parameter is used in an expression in the function
   26260              :          parameter-list and, if the corresponding template-argument is
   26261              :          deduced, the template-argument type shall match the type of the
   26262              :          template-parameter exactly, except that a template-argument
   26263              :          deduced from an array bound may be of any integral type.
   26264              :          The non-type parameter might use already deduced type parameters.  */
   26265     10016872 :       tparm = TREE_TYPE (parm);
   26266     30050562 :       if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
   26267              :         /* We don't have enough levels of args to do any substitution.  This
   26268              :            can happen in the context of -fnew-ttp-matching.  */;
   26269              :       else
   26270              :         {
   26271     10011225 :           ++processing_template_decl;
   26272     10011225 :           tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
   26273     10011225 :           --processing_template_decl;
   26274              : 
   26275     10011225 :           if (tree a = type_uses_auto (tparm))
   26276              :             {
   26277         6422 :               tparm = do_auto_deduction (tparm, arg, a,
   26278              :                                          complain, adc_unify, targs,
   26279              :                                          LOOKUP_NORMAL,
   26280         3211 :                                          TPARMS_PRIMARY_TEMPLATE (tparms));
   26281         3211 :               if (tparm == error_mark_node)
   26282              :                 return 1;
   26283              :             }
   26284              :         }
   26285              : 
   26286     10016869 :       if (!TREE_TYPE (arg)
   26287     10016869 :           || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
   26288              :         /* Template-parameter dependent expression.  Just accept it for now.
   26289              :            It will later be processed in convert_template_argument.  */
   26290              :         ;
   26291     10016854 :       else if (same_type_ignoring_top_level_qualifiers_p
   26292     10016854 :                (non_reference (TREE_TYPE (arg)),
   26293              :                 non_reference (tparm)))
   26294              :         /* OK.  Ignore top-level quals here because a class-type template
   26295              :            parameter object is const.  */;
   26296       225761 :       else if ((strict & UNIFY_ALLOW_INTEGER)
   26297       225477 :                && CP_INTEGRAL_TYPE_P (tparm))
   26298              :         /* Convert the ARG to the type of PARM; the deduced non-type
   26299              :            template argument must exactly match the types of the
   26300              :            corresponding parameter.  This conversion should match the
   26301              :            one above.  */
   26302       225462 :         arg = fold (build_nop (tparm, arg));
   26303          299 :       else if (uses_template_parms (tparm))
   26304              :         {
   26305              :           /* We haven't deduced the type of this parameter yet.  */
   26306          200 :           if (cxx_dialect >= cxx17
   26307              :               /* We deduce from array bounds in try_array_deduction.  */
   26308          172 :               && !(strict & UNIFY_ALLOW_INTEGER)
   26309          671 :               && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
   26310              :             {
   26311              :               /* Deduce it from the non-type argument.  As above, ignore
   26312              :                  top-level quals here too.  */
   26313          120 :               tree atype = cv_unqualified (TREE_TYPE (arg));
   26314          120 :               RECUR_AND_CHECK_FAILURE (tparms, targs,
   26315              :                                        tparm, atype,
   26316              :                                        UNIFY_ALLOW_NONE, explain_p);
   26317              :               /* Now check whether the type of this parameter is still
   26318              :                  dependent, and give up if so.  */
   26319          111 :               ++processing_template_decl;
   26320          111 :               tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
   26321          111 :               --processing_template_decl;
   26322          111 :               if (uses_template_parms (tparm))
   26323              :                 return unify_success (explain_p);
   26324              :             }
   26325              :           else
   26326              :             /* Try again later.  */
   26327    447567591 :             return unify_success (explain_p);
   26328              :         }
   26329              :       else
   26330           99 :         return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
   26331              : 
   26332              :       /* If ARG is a parameter pack or an expansion, we cannot unify
   26333              :          against it unless PARM is also a parameter pack.  */
   26334     20033238 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   26335     10084482 :           && !TEMPLATE_PARM_PARAMETER_PACK (parm))
   26336            7 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   26337              : 
   26338     10016612 :       {
   26339     10016612 :         bool removed_attr = false;
   26340     10016612 :         arg = strip_typedefs_expr (arg, &removed_attr);
   26341              :       }
   26342     10016612 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   26343     10016612 :       return unify_success (explain_p);
   26344              : 
   26345           21 :     case PTRMEM_CST:
   26346           21 :      {
   26347              :         /* A pointer-to-member constant can be unified only with
   26348              :          another constant.  */
   26349           21 :       if (TREE_CODE (arg) != PTRMEM_CST)
   26350            3 :         return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
   26351              : 
   26352              :       /* Just unify the class member. It would be useless (and possibly
   26353              :          wrong, depending on the strict flags) to unify also
   26354              :          PTRMEM_CST_CLASS, because we want to be sure that both parm and
   26355              :          arg refer to the same variable, even if through different
   26356              :          classes. For instance:
   26357              : 
   26358              :          struct A { int x; };
   26359              :          struct B : A { };
   26360              : 
   26361              :          Unification of &A::x and &B::x must succeed.  */
   26362           18 :       return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
   26363           18 :                     PTRMEM_CST_MEMBER (arg), strict, explain_p);
   26364              :      }
   26365              : 
   26366     10021206 :     case POINTER_TYPE:
   26367     10021206 :       {
   26368     10021206 :         if (!TYPE_PTR_P (arg))
   26369      5693520 :           return unify_type_mismatch (explain_p, parm, arg);
   26370              : 
   26371              :         /* [temp.deduct.call]
   26372              : 
   26373              :            A can be another pointer or pointer to member type that can
   26374              :            be converted to the deduced A via a qualification
   26375              :            conversion (_conv.qual_).
   26376              : 
   26377              :            We pass down STRICT here rather than UNIFY_ALLOW_NONE.
   26378              :            This will allow for additional cv-qualification of the
   26379              :            pointed-to types if appropriate.  */
   26380              : 
   26381      4327686 :         if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
   26382              :           /* The derived-to-base conversion only persists through one
   26383              :              level of pointers.  */
   26384       623614 :           strict |= (strict_in & UNIFY_ALLOW_DERIVED);
   26385              : 
   26386      4327686 :         return unify (tparms, targs, TREE_TYPE (parm),
   26387      8655372 :                       TREE_TYPE (arg), strict, explain_p);
   26388              :       }
   26389              : 
   26390     33454701 :     case REFERENCE_TYPE:
   26391     33454701 :       if (!TYPE_REF_P (arg)
   26392     33454701 :           || TYPE_REF_IS_RVALUE (parm) != TYPE_REF_IS_RVALUE (arg))
   26393      6574948 :         return unify_type_mismatch (explain_p, parm, arg);
   26394     26879753 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26395     26879753 :                     strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   26396              : 
   26397      3882001 :     case ARRAY_TYPE:
   26398      3882001 :       if (TREE_CODE (arg) != ARRAY_TYPE)
   26399      3612985 :         return unify_type_mismatch (explain_p, parm, arg);
   26400       269016 :       if ((TYPE_DOMAIN (parm) == NULL_TREE)
   26401       269016 :           != (TYPE_DOMAIN (arg) == NULL_TREE))
   26402        11876 :         return unify_type_mismatch (explain_p, parm, arg);
   26403       257140 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26404              :                                strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   26405       256954 :       if (TYPE_DOMAIN (parm) != NULL_TREE)
   26406       458462 :         return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   26407       229231 :                                    TYPE_DOMAIN (arg), explain_p);
   26408              :       return unify_success (explain_p);
   26409              : 
   26410      8332165 :     case REAL_TYPE:
   26411      8332165 :     case COMPLEX_TYPE:
   26412      8332165 :     case VECTOR_TYPE:
   26413      8332165 :     case INTEGER_TYPE:
   26414      8332165 :     case BOOLEAN_TYPE:
   26415      8332165 :     case ENUMERAL_TYPE:
   26416      8332165 :     case VOID_TYPE:
   26417      8332165 :     case OPAQUE_TYPE:
   26418      8332165 :     case NULLPTR_TYPE:
   26419      8332165 :     case META_TYPE:
   26420      8332165 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26421      3144740 :         return unify_type_mismatch (explain_p, parm, arg);
   26422              : 
   26423              :       /* We have already checked cv-qualification at the top of the
   26424              :          function.  */
   26425      5187425 :       if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
   26426      4007297 :         return unify_type_mismatch (explain_p, parm, arg);
   26427              : 
   26428              :       /* As far as unification is concerned, this wins.  Later checks
   26429              :          will invalidate it if necessary.  */
   26430    447567591 :       return unify_success (explain_p);
   26431              : 
   26432              :       /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
   26433              :       /* Type INTEGER_CST can come from ordinary constant template args.  */
   26434     11936585 :     case INTEGER_CST:
   26435     11936585 :     case REAL_CST:
   26436     11936585 :       if (TREE_TYPE (arg) == NULL_TREE
   26437     11936585 :           || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
   26438           12 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   26439     11936573 :       while (CONVERT_EXPR_P (arg))
   26440            0 :         arg = TREE_OPERAND (arg, 0);
   26441              : 
   26442     11936573 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26443         2658 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   26444     11933915 :       return (simple_cst_equal (parm, arg)
   26445     11933915 :               ? unify_success (explain_p)
   26446     11933909 :               : unify_template_argument_mismatch (explain_p, parm, arg));
   26447              : 
   26448    162408025 :     case TREE_VEC:
   26449    162408025 :       {
   26450    162408025 :         int i, len, argslen;
   26451    162408025 :         int parm_variadic_p = 0;
   26452              : 
   26453    162408025 :         if (TREE_CODE (arg) != TREE_VEC)
   26454            0 :           return unify_template_argument_mismatch (explain_p, parm, arg);
   26455              : 
   26456    162408025 :         len = TREE_VEC_LENGTH (parm);
   26457    162408025 :         argslen = TREE_VEC_LENGTH (arg);
   26458              : 
   26459              :         /* Check for pack expansions in the parameters.  */
   26460    476240282 :         for (i = 0; i < len; ++i)
   26461              :           {
   26462    313832270 :             if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
   26463              :               {
   26464      7101573 :                 if (i == len - 1)
   26465              :                   /* We can unify against something with a trailing
   26466              :                      parameter pack.  */
   26467              :                   parm_variadic_p = 1;
   26468              :                 else
   26469              :                   /* [temp.deduct.type]/9: If the template argument list of
   26470              :                      P contains a pack expansion that is not the last
   26471              :                      template argument, the entire template argument list
   26472              :                      is a non-deduced context.  */
   26473    447567591 :                   return unify_success (explain_p);
   26474              :               }
   26475              :           }
   26476              : 
   26477              :         /* If we don't have enough arguments to satisfy the parameters
   26478              :            (not counting the pack expression at the end), or we have
   26479              :            too many arguments for a parameter list that doesn't end in
   26480              :            a pack expression, we can't unify.  */
   26481    162408012 :         if (parm_variadic_p
   26482    162408012 :             ? argslen < len - parm_variadic_p
   26483              :             : argslen != len)
   26484      4004903 :           return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
   26485              : 
   26486              :         /* Unify all of the parameters that precede the (optional)
   26487              :            pack expression.  */
   26488    382005420 :         for (i = 0; i < len - parm_variadic_p; ++i)
   26489              :           {
   26490    271054894 :             RECUR_AND_CHECK_FAILURE (tparms, targs,
   26491              :                                      TREE_VEC_ELT (parm, i),
   26492              :                                      TREE_VEC_ELT (arg, i),
   26493              :                                      UNIFY_ALLOW_NONE, explain_p);
   26494              :           }
   26495    110950526 :         if (parm_variadic_p)
   26496      6786888 :           return unify_pack_expansion (tparms, targs, parm, arg,
   26497              :                                        DEDUCE_EXACT,
   26498      6786888 :                                        /*subr=*/true, explain_p);
   26499    447567591 :         return unify_success (explain_p);
   26500              :       }
   26501              : 
   26502    622550871 :     case RECORD_TYPE:
   26503    622550871 :     case UNION_TYPE:
   26504    622550871 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26505    341266810 :         return unify_type_mismatch (explain_p, parm, arg);
   26506              : 
   26507    281284061 :       if (TYPE_PTRMEMFUNC_P (parm))
   26508              :         {
   26509         2254 :           if (!TYPE_PTRMEMFUNC_P (arg))
   26510          264 :             return unify_type_mismatch (explain_p, parm, arg);
   26511              : 
   26512         5970 :           return unify (tparms, targs,
   26513         1990 :                         TYPE_PTRMEMFUNC_FN_TYPE (parm),
   26514         1990 :                         TYPE_PTRMEMFUNC_FN_TYPE (arg),
   26515         1990 :                         strict, explain_p);
   26516              :         }
   26517    281281807 :       else if (TYPE_PTRMEMFUNC_P (arg))
   26518          322 :         return unify_type_mismatch (explain_p, parm, arg);
   26519              : 
   26520    281281485 :       if (CLASSTYPE_TEMPLATE_INFO (parm))
   26521              :         {
   26522    279225972 :           tree t = NULL_TREE;
   26523              : 
   26524    279225972 :           if (strict_in & UNIFY_ALLOW_DERIVED)
   26525              :             {
   26526              :               /* First, we try to unify the PARM and ARG directly.  */
   26527    242372200 :               t = try_class_unification (tparms, targs,
   26528              :                                          parm, arg, explain_p);
   26529              : 
   26530    242372200 :               if (!t)
   26531              :                 {
   26532              :                   /* Fallback to the special case allowed in
   26533              :                      [temp.deduct.call]:
   26534              : 
   26535              :                        If P is a class, and P has the form
   26536              :                        template-id, then A can be a derived class of
   26537              :                        the deduced A.  Likewise, if P is a pointer to
   26538              :                        a class of the form template-id, A can be a
   26539              :                        pointer to a derived class pointed to by the
   26540              :                        deduced A.  */
   26541    223585621 :                   enum template_base_result r;
   26542    223585621 :                   r = get_template_base (tparms, targs, parm, arg,
   26543              :                                          explain_p, &t);
   26544              : 
   26545    223585621 :                   if (!t)
   26546              :                     {
   26547              :                       /* Don't give the derived diagnostic if we're
   26548              :                          already dealing with the same template.  */
   26549    222876436 :                       bool same_template
   26550    222876436 :                         = (CLASSTYPE_TEMPLATE_INFO (arg)
   26551    222876436 :                            && (CLASSTYPE_TI_TEMPLATE (parm)
   26552    167440847 :                                == CLASSTYPE_TI_TEMPLATE (arg)));
   26553    222876436 :                       return unify_no_common_base (explain_p && !same_template,
   26554    222876436 :                                                    r, parm, arg);
   26555              :                     }
   26556              :                 }
   26557              :             }
   26558     36853772 :           else if (CLASSTYPE_TEMPLATE_INFO (arg)
   26559     36853772 :                    && (CLASSTYPE_TI_TEMPLATE (parm)
   26560     35667779 :                        == CLASSTYPE_TI_TEMPLATE (arg)))
   26561              :             /* Perhaps PARM is something like S<U> and ARG is S<int>.
   26562              :                Then, we should unify `int' and `U'.  */
   26563     32848695 :             t = arg;
   26564              :           else
   26565              :             /* There's no chance of unification succeeding.  */
   26566    283231046 :             return unify_type_mismatch (explain_p, parm, arg);
   26567              : 
   26568     52344459 :           if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
   26569     52344456 :             return unify (tparms, targs,
   26570     52344456 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   26571     52344456 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)),
   26572     52344456 :                           UNIFY_ALLOW_NONE, explain_p);
   26573            3 :           gcc_checking_assert (t == arg);
   26574              :         }
   26575              : 
   26576      2055516 :       if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
   26577      2054336 :         return unify_type_mismatch (explain_p, parm, arg);
   26578    447567591 :       return unify_success (explain_p);
   26579              : 
   26580       269836 :     case METHOD_TYPE:
   26581       269836 :     case FUNCTION_TYPE:
   26582       269836 :       {
   26583       269836 :         unsigned int nargs;
   26584       269836 :         tree *args;
   26585       269836 :         tree a;
   26586       269836 :         unsigned int i;
   26587              : 
   26588       269836 :         if (TREE_CODE (arg) != TREE_CODE (parm))
   26589         1539 :           return unify_type_mismatch (explain_p, parm, arg);
   26590              : 
   26591              :         /* CV qualifications for methods can never be deduced, they must
   26592              :            match exactly.  We need to check them explicitly here,
   26593              :            because type_unification_real treats them as any other
   26594              :            cv-qualified parameter.  */
   26595       268297 :         if (TREE_CODE (parm) == METHOD_TYPE
   26596       270287 :             && (!check_cv_quals_for_unify
   26597         1990 :                 (UNIFY_ALLOW_NONE,
   26598              :                  class_of_this_parm (arg),
   26599              :                  class_of_this_parm (parm))))
   26600         1204 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   26601       267093 :         if (TREE_CODE (arg) == FUNCTION_TYPE
   26602       267093 :             && type_memfn_quals (parm) != type_memfn_quals (arg))
   26603          765 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   26604       266328 :         if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
   26605          570 :           return unify_type_mismatch (explain_p, parm, arg);
   26606              : 
   26607       265758 :         RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
   26608              :                                  TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
   26609              : 
   26610       265036 :         nargs = list_length (TYPE_ARG_TYPES (arg));
   26611       265036 :         args = XALLOCAVEC (tree, nargs);
   26612       265036 :         for (a = TYPE_ARG_TYPES (arg), i = 0;
   26613       908758 :              a != NULL_TREE && a != void_list_node;
   26614       643722 :              a = TREE_CHAIN (a), ++i)
   26615       643722 :           args[i] = TREE_VALUE (a);
   26616       265036 :         nargs = i;
   26617              : 
   26618       265036 :         if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
   26619              :                                    args, nargs, 1, DEDUCE_EXACT,
   26620              :                                    NULL, explain_p))
   26621              :           return 1;
   26622              : 
   26623       264845 :         if (flag_noexcept_type)
   26624              :           {
   26625       261363 :             tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
   26626       261363 :             tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
   26627       261363 :             if (pspec == NULL_TREE) pspec = noexcept_false_spec;
   26628       261363 :             if (aspec == NULL_TREE) aspec = noexcept_false_spec;
   26629       522726 :             if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
   26630       522726 :                 && uses_template_parms (TREE_PURPOSE (pspec)))
   26631          354 :               RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
   26632              :                                        TREE_PURPOSE (aspec),
   26633              :                                        UNIFY_ALLOW_NONE, explain_p);
   26634              :             else
   26635              :               {
   26636       261009 :                 bool pn = nothrow_spec_p (pspec);
   26637       261009 :                 bool an = nothrow_spec_p (aspec);
   26638              :                 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
   26639              :                    /more/ noexcept, since function pointer conversions are the
   26640              :                    reverse of qualification conversions.  */
   26641       261009 :                 if (an == pn
   26642       215834 :                     || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   26643       215828 :                     || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   26644              :                   /* OK.  */;
   26645              :                 else
   26646           50 :                   return unify_type_mismatch (explain_p, parm, arg);
   26647              :               }
   26648              :           }
   26649       264795 :         if (flag_tm)
   26650              :           {
   26651              :             /* As for noexcept.  */
   26652           62 :             bool pn = tx_safe_fn_type_p (parm);
   26653           62 :             bool an = tx_safe_fn_type_p (arg);
   26654           62 :             if (an == pn
   26655            4 :                 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   26656            4 :                 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   26657              :               /* OK.  */;
   26658              :             else
   26659            1 :               return unify_type_mismatch (explain_p, parm, arg);
   26660              :           }
   26661              : 
   26662              :         return 0;
   26663              :       }
   26664              : 
   26665        41578 :     case OFFSET_TYPE:
   26666              :       /* Unify a pointer to member with a pointer to member function, which
   26667              :          deduces the type of the member as a function type. */
   26668        41578 :       if (TYPE_PTRMEMFUNC_P (arg))
   26669              :         {
   26670              :           /* Check top-level cv qualifiers */
   26671        18715 :           if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
   26672           24 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   26673              : 
   26674        18691 :           RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   26675              :                                    TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
   26676              :                                    UNIFY_ALLOW_NONE, explain_p);
   26677              : 
   26678              :           /* Determine the type of the function we are unifying against. */
   26679        18691 :           tree fntype = static_fn_type (arg);
   26680              : 
   26681        18691 :           return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
   26682              :         }
   26683              : 
   26684        22863 :       if (TREE_CODE (arg) != OFFSET_TYPE)
   26685        21314 :         return unify_type_mismatch (explain_p, parm, arg);
   26686         1549 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   26687              :                                TYPE_OFFSET_BASETYPE (arg),
   26688              :                                UNIFY_ALLOW_NONE, explain_p);
   26689         1549 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26690         1549 :                     strict, explain_p);
   26691              : 
   26692            0 :     case CONST_DECL:
   26693              :       /* CONST_DECL should already have been folded to its DECL_INITIAL.  */
   26694            0 :       gcc_unreachable ();
   26695              : 
   26696         2305 :     case FIELD_DECL:
   26697         2305 :     case FUNCTION_DECL:
   26698         2305 :     case TEMPLATE_DECL:
   26699              :       /* Matched cases are handled by the ARG == PARM test above.  */
   26700         2305 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   26701              : 
   26702            0 :     case VAR_DECL:
   26703              :       /* We might get a variable as a non-type template argument in parm if the
   26704              :          corresponding parameter is type-dependent.  Make any necessary
   26705              :          adjustments based on whether arg is a reference.  */
   26706            0 :       if (CONSTANT_CLASS_P (arg))
   26707            0 :         parm = fold_non_dependent_expr (parm, complain);
   26708            0 :       else if (REFERENCE_REF_P (arg))
   26709              :         {
   26710            0 :           tree sub = TREE_OPERAND (arg, 0);
   26711            0 :           STRIP_NOPS (sub);
   26712            0 :           if (TREE_CODE (sub) == ADDR_EXPR)
   26713            0 :             arg = TREE_OPERAND (sub, 0);
   26714              :         }
   26715              :       /* Now use the normal expression code to check whether they match.  */
   26716            0 :       goto expr;
   26717              : 
   26718     13034037 :     case TYPE_ARGUMENT_PACK:
   26719     13034037 :     case NONTYPE_ARGUMENT_PACK:
   26720     13034037 :       return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
   26721     26068074 :                     ARGUMENT_PACK_ARGS (arg), strict, explain_p);
   26722              : 
   26723              :     case TYPEOF_TYPE:
   26724              :     case DECLTYPE_TYPE:
   26725              :     case TRAIT_TYPE:
   26726              :     case PACK_INDEX_TYPE:
   26727              :       /* These are non-deduced contexts.  */
   26728    447567591 :       return unify_success (explain_p);
   26729              : 
   26730              :     case ERROR_MARK:
   26731              :       /* Unification fails if we hit an error node.  */
   26732    435417702 :       return unify_invalid (explain_p);
   26733              : 
   26734           58 :     case INDIRECT_REF:
   26735           58 :       if (REFERENCE_REF_P (parm))
   26736              :         {
   26737           58 :           bool pexp = PACK_EXPANSION_P (arg);
   26738           58 :           if (pexp)
   26739            3 :             arg = PACK_EXPANSION_PATTERN (arg);
   26740           58 :           if (REFERENCE_REF_P (arg))
   26741           58 :             arg = TREE_OPERAND (arg, 0);
   26742           58 :           if (pexp)
   26743            3 :             arg = make_pack_expansion (arg, complain);
   26744           58 :           return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
   26745           58 :                         strict, explain_p);
   26746              :         }
   26747              :       /* FALLTHRU */
   26748              : 
   26749         1047 :     default:
   26750              :       /* An unresolved overload is a nondeduced context.  */
   26751         1047 :       if (is_overloaded_fn (parm) || type_unknown_p (parm))
   26752    447567591 :         return unify_success (explain_p);
   26753         1047 :       gcc_assert (EXPR_P (parm)
   26754              :                   || TREE_CODE (parm) == CONSTRUCTOR
   26755              :                   || TREE_CODE (parm) == LAMBDA_EXPR
   26756              :                   || TREE_CODE (parm) == TRAIT_EXPR);
   26757         1047 :     expr:
   26758              :       /* We must be looking at an expression.  This can happen with
   26759              :          something like:
   26760              : 
   26761              :            template <int I>
   26762              :            void foo(S<I>, S<I + 2>);
   26763              : 
   26764              :          or
   26765              : 
   26766              :            template<typename T>
   26767              :            void foo(A<T, T{}>);
   26768              : 
   26769              :          This is a "non-deduced context":
   26770              : 
   26771              :            [deduct.type]
   26772              : 
   26773              :            The non-deduced contexts are:
   26774              : 
   26775              :            --A non-type template argument or an array bound in which
   26776              :              a subexpression references a template parameter.
   26777              : 
   26778              :          In these cases, we assume deduction succeeded, but don't
   26779              :          actually infer any unifications.  */
   26780              : 
   26781         1047 :       if (!uses_template_parms (parm)
   26782         1047 :           && !template_args_equal (parm, arg))
   26783           11 :         return unify_expression_unequal (explain_p, parm, arg);
   26784              :       else
   26785         1036 :         return unify_success (explain_p);
   26786              :     }
   26787              : }
   26788              : #undef RECUR_AND_CHECK_FAILURE
   26789              : 
   26790              : /* Note that DECL can be defined in this translation unit, if
   26791              :    required.  */
   26792              : 
   26793              : static void
   26794     28168793 : mark_definable (tree decl)
   26795              : {
   26796     28168793 :   tree clone;
   26797     28168793 :   DECL_NOT_REALLY_EXTERN (decl) = 1;
   26798     35457088 :   FOR_EACH_CLONE (clone, decl)
   26799      7288295 :     DECL_NOT_REALLY_EXTERN (clone) = 1;
   26800     28168793 : }
   26801              : 
   26802              : /* DECL is an explicit instantiation definition, ensure that it will
   26803              :    be written out here and that it won't clash with other instantiations
   26804              :    in other translation units.  */
   26805              : 
   26806              : void
   26807        31003 : setup_explicit_instantiation_definition_linkage (tree decl)
   26808              : {
   26809        31003 :   mark_definable (decl);
   26810        31003 :   mark_needed (decl);
   26811              :   /* Always make artificials weak.  */
   26812        31003 :   if (DECL_ARTIFICIAL (decl) && flag_weak)
   26813            0 :     comdat_linkage (decl);
   26814              :   /* We also want to put explicit instantiations in linkonce sections.  */
   26815        31003 :   else if (TREE_PUBLIC (decl))
   26816        30984 :     maybe_make_one_only (decl);
   26817        31003 : }
   26818              : 
   26819              : /* Called if RESULT is explicitly instantiated, or is a member of an
   26820              :    explicitly instantiated class.  */
   26821              : 
   26822              : void
   26823     19029420 : mark_decl_instantiated (tree result, int extern_p)
   26824              : {
   26825     19029420 :   SET_DECL_EXPLICIT_INSTANTIATION (result);
   26826              : 
   26827              :   /* consteval functions are never emitted.  */
   26828     19029420 :   if (TREE_CODE (result) == FUNCTION_DECL
   26829     37734825 :       && DECL_IMMEDIATE_FUNCTION_P (result))
   26830              :     return;
   26831              : 
   26832              :   /* For anonymous namespace we don't need to do anything.  */
   26833     19029414 :   if (decl_internal_context_p (result))
   26834              :     {
   26835          993 :       gcc_assert (!TREE_PUBLIC (result));
   26836              :       return;
   26837              :     }
   26838              : 
   26839     19028421 :   if (TREE_CODE (result) != FUNCTION_DECL)
   26840              :     /* The TREE_PUBLIC flag for function declarations will have been
   26841              :        set correctly by tsubst.  */
   26842       324007 :     TREE_PUBLIC (result) = 1;
   26843              : 
   26844     19028421 :   if (extern_p)
   26845              :     {
   26846     18997454 :       DECL_EXTERNAL (result) = 1;
   26847     18997454 :       DECL_NOT_REALLY_EXTERN (result) = 0;
   26848              :     }
   26849              :   else
   26850              :     {
   26851        30967 :       set_instantiating_module (result);
   26852        30967 :       setup_explicit_instantiation_definition_linkage (result);
   26853        30967 :       if (TREE_CODE (result) == FUNCTION_DECL
   26854        30967 :           && DECL_TEMPLATE_INSTANTIATED (result))
   26855              :         /* If the function has already been instantiated, clear DECL_EXTERNAL,
   26856              :            since start_preparsed_function wouldn't have if we had an earlier
   26857              :            extern explicit instantiation.  */
   26858           94 :         DECL_EXTERNAL (result) = 0;
   26859              :     }
   26860              : 
   26861              :   /* If EXTERN_P, then this function will not be emitted -- unless
   26862              :      followed by an explicit instantiation, at which point its linkage
   26863              :      will be adjusted.  If !EXTERN_P, then this function will be
   26864              :      emitted here.  In neither circumstance do we want
   26865              :      import_export_decl to adjust the linkage.  */
   26866     19028421 :   DECL_INTERFACE_KNOWN (result) = 1;
   26867              : }
   26868              : 
   26869              : /* Subroutine of more_specialized_fn: check whether TARGS is missing any
   26870              :    important template arguments.  If any are missing, we check whether
   26871              :    they're important by using error_mark_node for substituting into any
   26872              :    args that were used for partial ordering (the ones between ARGS and END)
   26873              :    and seeing if it bubbles up.  */
   26874              : 
   26875              : static bool
   26876      8588854 : check_undeduced_parms (tree targs, tree args, tree end)
   26877              : {
   26878      8588854 :   bool found = false;
   26879     26706535 :   for (tree& targ : tree_vec_range (targs))
   26880     18117681 :     if (targ == NULL_TREE)
   26881              :       {
   26882      1921762 :         found = true;
   26883      1921762 :         targ = error_mark_node;
   26884              :       }
   26885      8588854 :   if (found)
   26886              :     {
   26887      1920500 :       tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
   26888      1920500 :       if (substed == error_mark_node)
   26889              :         return true;
   26890              :     }
   26891              :   return false;
   26892              : }
   26893              : 
   26894              : /* Given two function templates PAT1 and PAT2, return:
   26895              : 
   26896              :    1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   26897              :    -1 if PAT2 is more specialized than PAT1.
   26898              :    0 if neither is more specialized.
   26899              : 
   26900              :    LEN indicates the number of parameters we should consider
   26901              :    (defaulted parameters should not be considered).
   26902              : 
   26903              :    The 1998 std underspecified function template partial ordering, and
   26904              :    DR214 addresses the issue.  We take pairs of arguments, one from
   26905              :    each of the templates, and deduce them against each other.  One of
   26906              :    the templates will be more specialized if all the *other*
   26907              :    template's arguments deduce against its arguments and at least one
   26908              :    of its arguments *does* *not* deduce against the other template's
   26909              :    corresponding argument.  Deduction is done as for class templates.
   26910              :    The arguments used in deduction have reference and top level cv
   26911              :    qualifiers removed.  Iff both arguments were originally reference
   26912              :    types *and* deduction succeeds in both directions, an lvalue reference
   26913              :    wins against an rvalue reference and otherwise the template
   26914              :    with the more cv-qualified argument wins for that pairing (if
   26915              :    neither is more cv-qualified, they both are equal).  Unlike regular
   26916              :    deduction, after all the arguments have been deduced in this way,
   26917              :    we do *not* verify the deduced template argument values can be
   26918              :    substituted into non-deduced contexts.
   26919              : 
   26920              :    The logic can be a bit confusing here, because we look at deduce1 and
   26921              :    targs1 to see if pat2 is at least as specialized, and vice versa; if we
   26922              :    can find template arguments for pat1 to make arg1 look like arg2, that
   26923              :    means that arg2 is at least as specialized as arg1.  */
   26924              : 
   26925              : int
   26926      5476192 : more_specialized_fn (tree pat1, tree pat2, int len)
   26927              : {
   26928      5476192 :   tree decl1 = DECL_TEMPLATE_RESULT (pat1);
   26929      5476192 :   tree decl2 = DECL_TEMPLATE_RESULT (pat2);
   26930      5476192 :   tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
   26931      5476192 :   tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
   26932      5476192 :   tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
   26933      5476192 :   tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
   26934      5476192 :   tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
   26935      5476192 :   tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
   26936      5476192 :   tree origs1, origs2;
   26937      5476192 :   bool lose1 = false;
   26938      5476192 :   bool lose2 = false;
   26939              : 
   26940              :   /* C++17 [temp.func.order]/3 (CWG532)
   26941              : 
   26942              :      If only one of the function templates M is a non-static member of some
   26943              :      class A, M is considered to have a new first parameter inserted in its
   26944              :      function parameter list. Given cv as the cv-qualifiers of M (if any), the
   26945              :      new parameter is of type "rvalue reference to cv A" if the optional
   26946              :      ref-qualifier of M is && or if M has no ref-qualifier and the first
   26947              :      parameter of the other template has rvalue reference type. Otherwise, the
   26948              :      new parameter is of type "lvalue reference to cv A".  */
   26949              : 
   26950      5476192 :   if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2))
   26951              :     {
   26952              :       /* Note C++20 DR2445 extended the above to static member functions, but
   26953              :          I think the old G++ behavior of just skipping the object
   26954              :          parameter when comparing to a static member function was better, so
   26955              :          let's stick with that for now.  This is CWG2834.  --jason 2023-12 */
   26956          530 :       if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1))
   26957              :         {
   26958           32 :           len--; /* LEN is the number of significant arguments for DECL1 */
   26959           32 :           args1 = TREE_CHAIN (args1);
   26960              :         }
   26961          498 :       else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2))
   26962           20 :         args2 = TREE_CHAIN (args2);
   26963              :     }
   26964      5475662 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   26965      5475662 :            && DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   26966              :     {
   26967              :       /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
   26968              :          would break e.g.  cpp1y/lambda-generic-variadic5.C.  */
   26969        31149 :       len--;
   26970        31149 :       args1 = TREE_CHAIN (args1);
   26971        31149 :       args2 = TREE_CHAIN (args2);
   26972              :     }
   26973      5444513 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   26974      5444513 :            || DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   26975              :     {
   26976              :       /* The other is a non-member or explicit object member function;
   26977              :          rewrite the implicit object parameter to a reference.  */
   26978          125 :       tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1;
   26979          125 :       tree &nsargs = ns == decl2 ? args2 : args1;
   26980          125 :       tree obtype = TREE_TYPE (TREE_VALUE (nsargs));
   26981              : 
   26982          125 :       nsargs = TREE_CHAIN (nsargs);
   26983              : 
   26984          125 :       cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns));
   26985          125 :       if (rqual == REF_QUAL_NONE)
   26986              :         {
   26987           93 :           tree otherfirst = ns == decl1 ? args2 : args1;
   26988           93 :           otherfirst = TREE_VALUE (otherfirst);
   26989           93 :           if (TREE_CODE (otherfirst) == REFERENCE_TYPE
   26990           93 :               && TYPE_REF_IS_RVALUE (otherfirst))
   26991              :             rqual = REF_QUAL_RVALUE;
   26992              :         }
   26993          125 :       obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE);
   26994          125 :       nsargs = tree_cons (NULL_TREE, obtype, nsargs);
   26995              :     }
   26996              : 
   26997              :   /* If only one is a conversion operator, they are unordered.  */
   26998      5476192 :   if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
   26999              :     return 0;
   27000              : 
   27001              :   /* Consider the return type for a conversion function */
   27002      5476179 :   if (DECL_CONV_FN_P (decl1))
   27003              :     {
   27004            9 :       args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
   27005            9 :       args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
   27006            9 :       len++;
   27007              :     }
   27008              : 
   27009      5476179 :   processing_template_decl++;
   27010              : 
   27011      5476179 :   origs1 = args1;
   27012      5476179 :   origs2 = args2;
   27013              : 
   27014      5476179 :   while (len--
   27015              :          /* Stop when an ellipsis is seen.  */
   27016     14197420 :          && args1 != NULL_TREE && args2 != NULL_TREE)
   27017              :     {
   27018      8784410 :       tree arg1 = TREE_VALUE (args1);
   27019      8784410 :       tree arg2 = TREE_VALUE (args2);
   27020      8784410 :       int deduce1, deduce2;
   27021      8784410 :       int quals1 = -1;
   27022      8784410 :       int quals2 = -1;
   27023      8784410 :       int ref1 = 0;
   27024      8784410 :       int ref2 = 0;
   27025              : 
   27026      8784410 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   27027         2534 :           && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27028              :         {
   27029              :           /* When both arguments are pack expansions, we need only
   27030              :              unify the patterns themselves.  */
   27031          323 :           arg1 = PACK_EXPANSION_PATTERN (arg1);
   27032          323 :           arg2 = PACK_EXPANSION_PATTERN (arg2);
   27033              : 
   27034              :           /* This is the last comparison we need to do.  */
   27035              :           len = 0;
   27036              :         }
   27037              : 
   27038      8784410 :       if (TYPE_REF_P (arg1))
   27039              :         {
   27040      6440591 :           ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
   27041      6440591 :           arg1 = TREE_TYPE (arg1);
   27042      6440591 :           quals1 = cp_type_quals (arg1);
   27043              :         }
   27044              : 
   27045      8784410 :       if (TYPE_REF_P (arg2))
   27046              :         {
   27047      6616426 :           ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
   27048      6616426 :           arg2 = TREE_TYPE (arg2);
   27049      6616426 :           quals2 = cp_type_quals (arg2);
   27050              :         }
   27051              : 
   27052      8784410 :       arg1 = TYPE_MAIN_VARIANT (arg1);
   27053      8784410 :       arg2 = TYPE_MAIN_VARIANT (arg2);
   27054              : 
   27055      8784410 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
   27056              :         {
   27057         2211 :           int i, len2 = remaining_arguments (args2);
   27058         2211 :           tree parmvec = make_tree_vec (1);
   27059         2211 :           tree argvec = make_tree_vec (len2);
   27060         2211 :           tree ta = args2;
   27061              : 
   27062              :           /* Setup the parameter vector, which contains only ARG1.  */
   27063         2211 :           TREE_VEC_ELT (parmvec, 0) = arg1;
   27064              : 
   27065              :           /* Setup the argument vector, which contains the remaining
   27066              :              arguments.  */
   27067         5188 :           for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
   27068         2977 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   27069              : 
   27070         2211 :           deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
   27071              :                                            argvec, DEDUCE_EXACT,
   27072              :                                            /*subr=*/true, /*explain_p=*/false)
   27073              :                      == 0);
   27074              : 
   27075              :           /* We cannot deduce in the other direction, because ARG1 is
   27076              :              a pack expansion but ARG2 is not.  */
   27077         2211 :           deduce2 = 0;
   27078              :         }
   27079      8782199 :       else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27080              :         {
   27081          806 :           int i, len1 = remaining_arguments (args1);
   27082          806 :           tree parmvec = make_tree_vec (1);
   27083          806 :           tree argvec = make_tree_vec (len1);
   27084          806 :           tree ta = args1;
   27085              : 
   27086              :           /* Setup the parameter vector, which contains only ARG1.  */
   27087          806 :           TREE_VEC_ELT (parmvec, 0) = arg2;
   27088              : 
   27089              :           /* Setup the argument vector, which contains the remaining
   27090              :              arguments.  */
   27091         2240 :           for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
   27092         1434 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   27093              : 
   27094          806 :           deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
   27095              :                                            argvec, DEDUCE_EXACT,
   27096              :                                            /*subr=*/true, /*explain_p=*/false)
   27097              :                      == 0);
   27098              : 
   27099              :           /* We cannot deduce in the other direction, because ARG2 is
   27100              :              a pack expansion but ARG1 is not.*/
   27101          806 :           deduce1 = 0;
   27102              :         }
   27103              : 
   27104              :       else
   27105              :         {
   27106              :           /* The normal case, where neither argument is a pack
   27107              :              expansion.  */
   27108      8781393 :           deduce1 = (unify (tparms1, targs1, arg1, arg2,
   27109              :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   27110      8781393 :                      == 0);
   27111      8781393 :           deduce2 = (unify (tparms2, targs2, arg2, arg1,
   27112              :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   27113      8781393 :                      == 0);
   27114              :         }
   27115              : 
   27116              :       /* If we couldn't deduce arguments for tparms1 to make arg1 match
   27117              :          arg2, then arg2 is not as specialized as arg1.  */
   27118      8784410 :       if (!deduce1)
   27119              :         lose2 = true;
   27120      8784410 :       if (!deduce2)
   27121       169131 :         lose1 = true;
   27122              : 
   27123              :       /* "If, for a given type, deduction succeeds in both directions
   27124              :          (i.e., the types are identical after the transformations above)
   27125              :          and both P and A were reference types (before being replaced with
   27126              :          the type referred to above):
   27127              :          - if the type from the argument template was an lvalue reference and
   27128              :          the type from the parameter template was not, the argument type is
   27129              :          considered to be more specialized than the other; otherwise,
   27130              :          - if the type from the argument template is more cv-qualified
   27131              :          than the type from the parameter template (as described above),
   27132              :          the argument type is considered to be more specialized than the other;
   27133              :          otherwise,
   27134              :          - neither type is more specialized than the other."  */
   27135              : 
   27136      8784410 :       if (deduce1 && deduce2)
   27137              :         {
   27138      6460644 :           if (ref1 && ref2 && ref1 != ref2)
   27139              :             {
   27140            3 :               if (ref1 > ref2)
   27141              :                 lose1 = true;
   27142              :               else
   27143            3 :                 lose2 = true;
   27144              :             }
   27145      6460641 :           else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
   27146              :             {
   27147         1015 :               if ((quals1 & quals2) == quals2)
   27148           21 :                 lose2 = true;
   27149         1015 :               if ((quals1 & quals2) == quals1)
   27150          994 :                 lose1 = true;
   27151              :             }
   27152              :         }
   27153              : 
   27154      8784410 :       if (lose1 && lose2)
   27155              :         /* We've failed to deduce something in either direction.
   27156              :            These must be unordered.  */
   27157              :         break;
   27158              : 
   27159      8721241 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   27160      8721202 :           || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27161              :         /* We have already processed all of the arguments in our
   27162              :            handing of the pack expansion type.  */
   27163          187 :         len = 0;
   27164              : 
   27165      8721241 :       args1 = TREE_CHAIN (args1);
   27166      8721241 :       args2 = TREE_CHAIN (args2);
   27167              :     }
   27168              : 
   27169              :   /* "In most cases, all template parameters must have values in order for
   27170              :      deduction to succeed, but for partial ordering purposes a template
   27171              :      parameter may remain without a value provided it is not used in the
   27172              :      types being used for partial ordering."
   27173              : 
   27174              :      Thus, if we are missing any of the targs1 we need to substitute into
   27175              :      origs1, then pat2 is not as specialized as pat1.  This can happen when
   27176              :      there is a nondeduced context.  */
   27177      5476179 :   if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
   27178              :     lose2 = true;
   27179      5476179 :   if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
   27180              :     lose1 = true;
   27181              : 
   27182      5476179 :   processing_template_decl--;
   27183              : 
   27184              :   /* If both deductions succeed, the partial ordering selects the more
   27185              :      constrained template.  */
   27186              :   /* P2113: If the corresponding template-parameters of the
   27187              :      template-parameter-lists are not equivalent ([temp.over.link]) or if
   27188              :      the function parameters that positionally correspond between the two
   27189              :      templates are not of the same type, neither template is more
   27190              :      specialized than the other.  */
   27191      5476179 :   if (!lose1 && !lose2
   27192      3175829 :       && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
   27193      3175829 :                               DECL_TEMPLATE_PARMS (pat2))
   27194      8651886 :       && compparms (origs1, origs2))
   27195              :     {
   27196      3155542 :       int winner = more_constrained (decl1, decl2);
   27197      3155542 :       if (winner > 0)
   27198              :         lose2 = true;
   27199      2091586 :       else if (winner < 0)
   27200       289978 :         lose1 = true;
   27201              :     }
   27202              : 
   27203              :   /* All things being equal, if the next argument is a pack expansion
   27204              :      for one function but not for the other, prefer the
   27205              :      non-variadic function.  FIXME this is bogus; see c++/41958.  */
   27206      5476179 :   if (lose1 == lose2
   27207      1885076 :       && args1 && TREE_VALUE (args1)
   27208      7361249 :       && args2 && TREE_VALUE (args2))
   27209              :     {
   27210      1885061 :       lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
   27211      1885061 :       lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
   27212              :     }
   27213              : 
   27214      5476179 :   if (lose1 == lose2)
   27215              :     return 0;
   27216      3594748 :   else if (!lose1)
   27217              :     return 1;
   27218              :   else
   27219       391573 :     return -1;
   27220              : }
   27221              : 
   27222              : /* Determine which of two partial specializations of TMPL is more
   27223              :    specialized.
   27224              : 
   27225              :    PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
   27226              :    to the first partial specialization.  The TREE_PURPOSE is the
   27227              :    innermost set of template parameters for the partial
   27228              :    specialization.  PAT2 is similar, but for the second template.
   27229              : 
   27230              :    Return 1 if the first partial specialization is more specialized;
   27231              :    -1 if the second is more specialized; 0 if neither is more
   27232              :    specialized.
   27233              : 
   27234              :    See [temp.class.order] for information about determining which of
   27235              :    two templates is more specialized.  */
   27236              : 
   27237              : static int
   27238      1824623 : more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
   27239              : {
   27240      1824623 :   tree targs;
   27241      1824623 :   int winner = 0;
   27242      1824623 :   bool any_deductions = false;
   27243              : 
   27244      1824623 :   tree tmpl1 = TREE_VALUE (pat1);
   27245      1824623 :   tree tmpl2 = TREE_VALUE (pat2);
   27246      1824623 :   tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
   27247      1824623 :   tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
   27248              : 
   27249              :   /* Just like what happens for functions, if we are ordering between
   27250              :      different template specializations, we may encounter dependent
   27251              :      types in the arguments, and we need our dependency check functions
   27252              :      to behave correctly.  */
   27253      1824623 :   ++processing_template_decl;
   27254      1824623 :   targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
   27255      1824623 :   if (targs)
   27256              :     {
   27257       292947 :       --winner;
   27258       292947 :       any_deductions = true;
   27259              :     }
   27260              : 
   27261      1824623 :   targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
   27262      1824623 :   if (targs)
   27263              :     {
   27264      1569880 :       ++winner;
   27265      1569880 :       any_deductions = true;
   27266              :     }
   27267      1824623 :   --processing_template_decl;
   27268              : 
   27269              :   /* If both deductions succeed, the partial ordering selects the more
   27270              :      constrained template.  */
   27271      1824623 :   if (!winner && any_deductions)
   27272        38285 :     winner = more_constrained (tmpl1, tmpl2);
   27273              : 
   27274              :   /* In the case of a tie where at least one of the templates
   27275              :      has a parameter pack at the end, the template with the most
   27276              :      non-packed parameters wins.  */
   27277      1824623 :   if (winner == 0
   27278      1824623 :       && any_deductions
   27279      1824633 :       && (template_args_variadic_p (TREE_PURPOSE (pat1))
   27280           10 :           || template_args_variadic_p (TREE_PURPOSE (pat2))))
   27281              :     {
   27282            0 :       tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
   27283            0 :       tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
   27284            0 :       int len1 = TREE_VEC_LENGTH (args1);
   27285            0 :       int len2 = TREE_VEC_LENGTH (args2);
   27286              : 
   27287              :       /* We don't count the pack expansion at the end.  */
   27288            0 :       if (template_args_variadic_p (TREE_PURPOSE (pat1)))
   27289            0 :         --len1;
   27290            0 :       if (template_args_variadic_p (TREE_PURPOSE (pat2)))
   27291            0 :         --len2;
   27292              : 
   27293            0 :       if (len1 > len2)
   27294              :         return 1;
   27295            0 :       else if (len1 < len2)
   27296              :         return -1;
   27297              :     }
   27298              : 
   27299              :   return winner;
   27300              : }
   27301              : 
   27302              : /* Return the template arguments that will produce the function signature
   27303              :    DECL from the function template FN, with the explicit template
   27304              :    arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
   27305              :    also match.  Return NULL_TREE if no satisfactory arguments could be
   27306              :    found.  */
   27307              : 
   27308              : static tree
   27309     14417937 : get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
   27310              : {
   27311     14417937 :   int ntparms = DECL_NTPARMS (fn);
   27312     14417937 :   tree targs = make_tree_vec (ntparms);
   27313     14417937 :   tree decl_type = TREE_TYPE (decl);
   27314     14417937 :   tree decl_arg_types;
   27315     14417937 :   tree *args;
   27316     14417937 :   unsigned int nargs, ix;
   27317     14417937 :   tree arg;
   27318              : 
   27319     14417937 :   gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
   27320              : 
   27321              :   /* Never do unification on the 'this' parameter.  */
   27322     14417937 :   decl_arg_types = skip_artificial_parms_for (decl,
   27323     14417937 :                                               TYPE_ARG_TYPES (decl_type));
   27324              : 
   27325     14417937 :   nargs = list_length (decl_arg_types);
   27326     14417937 :   args = XALLOCAVEC (tree, nargs);
   27327     14417937 :   for (arg = decl_arg_types, ix = 0;
   27328     56615146 :        arg != NULL_TREE;
   27329     42197209 :        arg = TREE_CHAIN (arg), ++ix)
   27330     42197209 :     args[ix] = TREE_VALUE (arg);
   27331              : 
   27332     28835874 :   if (fn_type_unification (fn, explicit_args, targs,
   27333              :                            args, ix,
   27334            0 :                            (check_rettype || DECL_CONV_FN_P (fn)
   27335     14417937 :                             ? TREE_TYPE (decl_type) : NULL_TREE),
   27336              :                            DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
   27337              :                            /*explain_p=*/false,
   27338              :                            /*decltype*/false)
   27339     14417937 :       == error_mark_node)
   27340     12238389 :     return NULL_TREE;
   27341              : 
   27342              :   return targs;
   27343              : }
   27344              : 
   27345              : /* Return the innermost template arguments that, when applied to a partial
   27346              :    specialization SPEC_TMPL of TMPL, yield the ARGS.
   27347              : 
   27348              :    For example, suppose we have:
   27349              : 
   27350              :      template <class T, class U> struct S {};
   27351              :      template <class T> struct S<T*, int> {};
   27352              : 
   27353              :    Then, suppose we want to get `S<double*, int>'.  SPEC_TMPL will be the
   27354              :    partial specialization and the ARGS will be {double*, int}.  The resulting
   27355              :    vector will be {double}, indicating that `T' is bound to `double'.  */
   27356              : 
   27357              : static tree
   27358     67278177 : get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
   27359              : {
   27360     67278177 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
   27361     67278177 :   tree spec_args
   27362     67278177 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
   27363     67278177 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
   27364     67278177 :   tree deduced_args;
   27365     67278177 :   tree innermost_deduced_args;
   27366              : 
   27367     67278177 :   innermost_deduced_args = make_tree_vec (ntparms);
   27368    134556354 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   27369              :     {
   27370          114 :       deduced_args = copy_node (args);
   27371          228 :       SET_TMPL_ARGS_LEVEL (deduced_args,
   27372              :                            TMPL_ARGS_DEPTH (deduced_args),
   27373              :                            innermost_deduced_args);
   27374              :     }
   27375              :   else
   27376              :     deduced_args = innermost_deduced_args;
   27377              : 
   27378     67278177 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   27379     67278180 :  again:
   27380     67278180 :   if (unify (tparms, deduced_args,
   27381              :              INNERMOST_TEMPLATE_ARGS (spec_args),
   27382              :              INNERMOST_TEMPLATE_ARGS (args),
   27383              :              UNIFY_ALLOW_NONE, /*explain_p=*/false))
   27384              :     return NULL_TREE;
   27385              : 
   27386     72177992 :   for (i =  0; i < ntparms; ++i)
   27387     46853017 :     if (! TREE_VEC_ELT (innermost_deduced_args, i))
   27388              :       {
   27389           26 :         if (!tried_array_deduction)
   27390              :           {
   27391           19 :             try_array_deduction (tparms, innermost_deduced_args,
   27392              :                                  INNERMOST_TEMPLATE_ARGS (spec_args));
   27393           19 :             tried_array_deduction = true;
   27394           19 :             if (TREE_VEC_ELT (innermost_deduced_args, i))
   27395            3 :               goto again;
   27396              :           }
   27397           23 :         return NULL_TREE;
   27398              :       }
   27399              : 
   27400     25324975 :   if (!push_tinst_level (spec_tmpl, deduced_args))
   27401              :     {
   27402            0 :       excessive_deduction_depth = true;
   27403            0 :       return NULL_TREE;
   27404              :     }
   27405              : 
   27406              :   /* Verify that nondeduced template arguments agree with the type
   27407              :      obtained from argument deduction.
   27408              : 
   27409              :      For example:
   27410              : 
   27411              :        struct A { typedef int X; };
   27412              :        template <class T, class U> struct C {};
   27413              :        template <class T> struct C<T, typename T::X> {};
   27414              : 
   27415              :      Then with the instantiation `C<A, int>', we can deduce that
   27416              :      `T' is `A' but unify () does not check whether `typename T::X'
   27417              :      is `int'.  */
   27418     25324975 :   spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
   27419              : 
   27420     25324975 :   if (spec_args != error_mark_node)
   27421     25196647 :     spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
   27422              :                                        INNERMOST_TEMPLATE_ARGS (spec_args),
   27423              :                                        tmpl, tf_none, false);
   27424              : 
   27425     25324975 :   pop_tinst_level ();
   27426              : 
   27427     25324975 :   if (spec_args == error_mark_node
   27428              :       /* We only need to check the innermost arguments; the other
   27429              :          arguments will always agree.  */
   27430     25324975 :       || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
   27431              :                                      INNERMOST_TEMPLATE_ARGS (args)))
   27432       128474 :     return NULL_TREE;
   27433              : 
   27434              :   /* Now that we have bindings for all of the template arguments,
   27435              :      ensure that the arguments deduced for the template template
   27436              :      parameters have compatible template parameter lists.  See the use
   27437              :      of template_template_parm_bindings_ok_p in fn_type_unification
   27438              :      for more information.  */
   27439     25196501 :   if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
   27440              :     return NULL_TREE;
   27441              : 
   27442              :   return deduced_args;
   27443              : }
   27444              : 
   27445              : // Compare two function templates T1 and T2 by deducing bindings
   27446              : // from one against the other. If both deductions succeed, compare
   27447              : // constraints to see which is more constrained.
   27448              : static int
   27449        60785 : more_specialized_inst (tree t1, tree t2)
   27450              : {
   27451        60785 :   int fate = 0;
   27452        60785 :   int count = 0;
   27453              : 
   27454        60785 :   if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
   27455              :     {
   27456          193 :       --fate;
   27457          193 :       ++count;
   27458              :     }
   27459              : 
   27460        60785 :   if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
   27461              :     {
   27462        40532 :       ++fate;
   27463        40532 :       ++count;
   27464              :     }
   27465              : 
   27466              :   // If both deductions succeed, then one may be more constrained.
   27467        60785 :   if (count == 2 && fate == 0)
   27468          126 :     fate = more_constrained (t1, t2);
   27469              : 
   27470        60785 :   return fate;
   27471              : }
   27472              : 
   27473              : /* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
   27474              :    Return the TREE_LIST node with the most specialized template, if
   27475              :    any.  If there is no most specialized template, the error_mark_node
   27476              :    is returned.
   27477              : 
   27478              :    Note that this function does not look at, or modify, the
   27479              :    TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
   27480              :    returned is one of the elements of INSTANTIATIONS, callers may
   27481              :    store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
   27482              :    and retrieve it from the value returned.  */
   27483              : 
   27484              : tree
   27485        55880 : most_specialized_instantiation (tree templates)
   27486              : {
   27487        55880 :   tree fn, champ;
   27488              : 
   27489        55880 :   ++processing_template_decl;
   27490              : 
   27491        55880 :   champ = templates;
   27492        76193 :   for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
   27493              :     {
   27494        20343 :       gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
   27495        20343 :       int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
   27496        20343 :       if (fate == -1)
   27497              :         champ = fn;
   27498        20213 :       else if (!fate)
   27499              :         {
   27500              :           /* Equally specialized, move to next function.  If there
   27501              :              is no next function, nothing's most specialized.  */
   27502        20186 :           fn = TREE_CHAIN (fn);
   27503        20186 :           champ = fn;
   27504        20186 :           if (!fn)
   27505              :             break;
   27506              :         }
   27507              :     }
   27508              : 
   27509        55880 :   if (champ)
   27510              :     /* Now verify that champ is better than everything earlier in the
   27511              :        instantiation list.  */
   27512        96292 :     for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
   27513        40442 :       if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
   27514              :       {
   27515              :         champ = NULL_TREE;
   27516              :         break;
   27517              :       }
   27518              :     }
   27519              : 
   27520        55880 :   processing_template_decl--;
   27521              : 
   27522        55880 :   if (!champ)
   27523           30 :     return error_mark_node;
   27524              : 
   27525              :   return champ;
   27526              : }
   27527              : 
   27528              : /* If DECL is a specialization of some template, return the most
   27529              :    general such template.  Otherwise, returns NULL_TREE.
   27530              : 
   27531              :    For example, given:
   27532              : 
   27533              :      template <class T> struct S { template <class U> void f(U); };
   27534              : 
   27535              :    if TMPL is `template <class U> void S<int>::f(U)' this will return
   27536              :    the full template.  This function will not trace past partial
   27537              :    specializations, however.  For example, given in addition:
   27538              : 
   27539              :      template <class T> struct S<T*> { template <class U> void f(U); };
   27540              : 
   27541              :    if TMPL is `template <class U> void S<int*>::f(U)' this will return
   27542              :    `template <class T> template <class U> S<T*>::f(U)'.  */
   27543              : 
   27544              : tree
   27545   4932144125 : most_general_template (const_tree decl)
   27546              : {
   27547   4932144125 :   if (TREE_CODE (decl) != TEMPLATE_DECL)
   27548              :     {
   27549    941295450 :       if (tree tinfo = get_template_info (decl))
   27550    590654064 :         decl = TI_TEMPLATE (tinfo);
   27551              :       /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
   27552              :          template friend, or a FIELD_DECL for a capture pack.  */
   27553    941295450 :       if (TREE_CODE (decl) != TEMPLATE_DECL)
   27554              :         return NULL_TREE;
   27555              :     }
   27556              : 
   27557   4581500783 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   27558       501187 :     return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl));
   27559              : 
   27560              :   /* Look for more and more general templates.  */
   27561   4641724061 :   while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
   27562              :     {
   27563              :       /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
   27564              :          (See cp-tree.h for details.)  */
   27565     60724777 :       if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
   27566              :         break;
   27567              : 
   27568    121449554 :       if (CLASS_TYPE_P (TREE_TYPE (decl))
   27569     10103658 :           && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
   27570     69587090 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   27571              :         break;
   27572              : 
   27573              :       /* Stop if we run into an explicitly specialized class template.  */
   27574    121448930 :       if (!DECL_NAMESPACE_SCOPE_P (decl)
   27575     55729392 :           && DECL_CONTEXT (decl)
   27576    116453857 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
   27577              :         break;
   27578              : 
   27579     60724465 :       decl = DECL_TI_TEMPLATE (decl);
   27580              :     }
   27581              : 
   27582              :   return const_cast<tree> (decl);
   27583              : }
   27584              : 
   27585              : /* Return the most specialized of the template partial specializations
   27586              :    which can produce TARGET, a specialization of some class or variable
   27587              :    template.  The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
   27588              :    TEMPLATE_DECL node corresponding to the partial specialization, while
   27589              :    the TI_ARGS is the set of template arguments that must be substituted
   27590              :    into the template pattern in order to generate TARGET.  The result is
   27591              :    cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
   27592              :    RECHECKING is true.
   27593              : 
   27594              :    If the choice of partial specialization is ambiguous, a diagnostic
   27595              :    is issued, and the error_mark_node is returned.  If there are no
   27596              :    partial specializations matching TARGET, then NULL_TREE is
   27597              :    returned, indicating that the primary template should be used.  */
   27598              : 
   27599              : tree
   27600    105578383 : most_specialized_partial_spec (tree target, tsubst_flags_t complain,
   27601              :                                bool rechecking /* = false */)
   27602              : {
   27603    105578383 :   tree tinfo = NULL_TREE;
   27604    105578383 :   tree tmpl, args, decl;
   27605    105578383 :   if (TYPE_P (target))
   27606              :     {
   27607     47848240 :       tinfo = CLASSTYPE_TEMPLATE_INFO (target);
   27608     47848240 :       tmpl = TI_TEMPLATE (tinfo);
   27609     47848240 :       args = TI_ARGS (tinfo);
   27610     47848240 :       decl = TYPE_NAME (target);
   27611              :     }
   27612     57730143 :   else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
   27613              :     {
   27614     16661733 :       tmpl = TREE_OPERAND (target, 0);
   27615     16661733 :       args = TREE_OPERAND (target, 1);
   27616     16661733 :       decl = DECL_TEMPLATE_RESULT (tmpl);
   27617              :     }
   27618     41068410 :   else if (VAR_P (target))
   27619              :     {
   27620     41068410 :       tinfo = DECL_TEMPLATE_INFO (target);
   27621     41068410 :       tmpl = TI_TEMPLATE (tinfo);
   27622     41068410 :       args = TI_ARGS (tinfo);
   27623     41068410 :       decl = target;
   27624              :     }
   27625              :   else
   27626            0 :     gcc_unreachable ();
   27627              : 
   27628    105578383 :   if (!PRIMARY_TEMPLATE_P (tmpl))
   27629              :     return NULL_TREE;
   27630              : 
   27631     93555595 :   if (!rechecking
   27632     93555595 :       && tinfo
   27633     93555595 :       && (VAR_P (target) || COMPLETE_TYPE_P (target)))
   27634     33313247 :     return TI_PARTIAL_INFO (tinfo);
   27635              : 
   27636     60242348 :   tree main_tmpl = most_general_template (tmpl);
   27637     60242348 :   tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
   27638     60242348 :   if (!specs)
   27639              :     /* There are no partial specializations of this template.  */
   27640              :     return NULL_TREE;
   27641              : 
   27642     16164874 :   push_access_scope_guard pas (decl);
   27643     16164874 :   deferring_access_check_sentinel acs (dk_no_deferred);
   27644              : 
   27645              :   /* For determining which partial specialization to use, only the
   27646              :      innermost args are interesting.  */
   27647     16164874 :   tree outer_args = NULL_TREE;
   27648     32329748 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   27649              :     {
   27650       465939 :       outer_args = strip_innermost_template_args (args, 1);
   27651       465939 :       args = INNERMOST_TEMPLATE_ARGS (args);
   27652              :     }
   27653              : 
   27654              :   /* The caller hasn't called push_to_top_level yet, but we need
   27655              :      get_partial_spec_bindings to be done in non-template context so that we'll
   27656              :      fully resolve everything.  */
   27657     16164874 :   processing_template_decl_sentinel ptds;
   27658              : 
   27659     16164874 :   tree list = NULL_TREE;
   27660     72371732 :   for (tree t = specs; t; t = TREE_CHAIN (t))
   27661              :     {
   27662     56206864 :       const tree ospec_tmpl = TREE_VALUE (t);
   27663              : 
   27664     56206864 :       tree spec_tmpl;
   27665     56206864 :       if (outer_args)
   27666              :         {
   27667              :           /* Substitute in the template args from the enclosing class.  */
   27668       646489 :           ++processing_template_decl;
   27669       646489 :           spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
   27670       646489 :           --processing_template_decl;
   27671       646489 :           if (spec_tmpl == error_mark_node)
   27672              :             return error_mark_node;
   27673              :         }
   27674              :       else
   27675              :         spec_tmpl = ospec_tmpl;
   27676              : 
   27677     56206858 :       tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
   27678     56206858 :       if (spec_args)
   27679              :         {
   27680     15911606 :           if (outer_args)
   27681       537564 :             spec_args = add_to_template_args (outer_args, spec_args);
   27682              : 
   27683              :           /* Keep the candidate only if its constraints are satisfied.  */
   27684     15911606 :           if (constraints_satisfied_p (ospec_tmpl, spec_args))
   27685     12610090 :             list = tree_cons (spec_args, ospec_tmpl, list);
   27686              :         }
   27687              :     }
   27688              : 
   27689     16164868 :   if (! list)
   27690              :     return NULL_TREE;
   27691              : 
   27692     11149546 :   tree champ = list;
   27693     11149546 :   bool ambiguous_p = false;
   27694     12609999 :   for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
   27695              :     {
   27696      1460465 :       int fate = more_specialized_partial_spec (tmpl, champ, t);
   27697      1460465 :       if (fate == 1)
   27698              :         ;
   27699              :       else
   27700              :         {
   27701       254886 :           if (fate == 0)
   27702              :             {
   27703           91 :               t = TREE_CHAIN (t);
   27704           91 :               if (! t)
   27705              :                 {
   27706              :                   ambiguous_p = true;
   27707              :                   break;
   27708              :                 }
   27709              :             }
   27710              :           champ = t;
   27711              :         }
   27712              :     }
   27713              : 
   27714     11149546 :   if (!ambiguous_p)
   27715     11513689 :     for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
   27716              :       {
   27717       364158 :         int fate = more_specialized_partial_spec (tmpl, champ, t);
   27718       364158 :         if (fate != 1)
   27719              :           {
   27720              :             ambiguous_p = true;
   27721              :             break;
   27722              :           }
   27723              :       }
   27724              : 
   27725     11149534 :   if (ambiguous_p)
   27726              :     {
   27727           15 :       const char *str;
   27728           15 :       char *spaces = NULL;
   27729           15 :       if (!(complain & tf_error))
   27730            3 :         return error_mark_node;
   27731           12 :       auto_diagnostic_group d;
   27732           12 :       if (TYPE_P (target))
   27733            9 :         error ("ambiguous template instantiation for %q#T", target);
   27734              :       else
   27735            3 :         error ("ambiguous template instantiation for %q#D", target);
   27736           12 :       str = ngettext ("candidate is:", "candidates are:", list_length (list));
   27737           45 :       for (tree t = list; t; t = TREE_CHAIN (t))
   27738              :         {
   27739           33 :           tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
   27740           66 :           inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
   27741              :                   "%s %#qS", spaces ? spaces : str, subst);
   27742           33 :           spaces = spaces ? spaces : get_spaces (str);
   27743              :         }
   27744           12 :       free (spaces);
   27745           12 :       return error_mark_node;
   27746           12 :     }
   27747              : 
   27748     11149531 :   tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
   27749     11149531 :   if (!rechecking && tinfo)
   27750      9995281 :     TI_PARTIAL_INFO (tinfo) = result;
   27751              :   return result;
   27752     32329748 : }
   27753              : 
   27754              : /* Explicitly instantiate DECL.  */
   27755              : 
   27756              : void
   27757      1915599 : do_decl_instantiation (tree decl, tree storage)
   27758              : {
   27759      1915599 :   tree result = NULL_TREE;
   27760      1915599 :   int extern_p = 0;
   27761              : 
   27762      1915599 :   if (!decl || decl == error_mark_node)
   27763              :     /* An error occurred, for which grokdeclarator has already issued
   27764              :        an appropriate message.  */
   27765              :     return;
   27766      1915437 :   else if (! DECL_LANG_SPECIFIC (decl))
   27767              :     {
   27768            0 :       error ("explicit instantiation of non-template %q#D", decl);
   27769            0 :       return;
   27770              :     }
   27771              : 
   27772      1915437 :   bool var_templ = (DECL_TEMPLATE_INFO (decl)
   27773      1915437 :                     && variable_template_p (DECL_TI_TEMPLATE (decl)));
   27774              : 
   27775      1915437 :   if (VAR_P (decl) && !var_templ)
   27776              :     {
   27777              :       /* There is an asymmetry here in the way VAR_DECLs and
   27778              :          FUNCTION_DECLs are handled by grokdeclarator.  In the case of
   27779              :          the latter, the DECL we get back will be marked as a
   27780              :          template instantiation, and the appropriate
   27781              :          DECL_TEMPLATE_INFO will be set up.  This does not happen for
   27782              :          VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
   27783              :          should handle VAR_DECLs as it currently handles
   27784              :          FUNCTION_DECLs.  */
   27785           78 :       if (!DECL_CLASS_SCOPE_P (decl))
   27786              :         {
   27787            0 :           error ("%qD is not a static data member of a class template", decl);
   27788            0 :           return;
   27789              :         }
   27790           78 :       result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
   27791           78 :       if (!result || !VAR_P (result))
   27792              :         {
   27793            0 :           error ("no matching template for %qD found", decl);
   27794            0 :           return;
   27795              :         }
   27796           78 :       if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
   27797              :         {
   27798            9 :           error ("type %qT for explicit instantiation %qD does not match "
   27799            3 :                  "declared type %qT", TREE_TYPE (result), decl,
   27800            3 :                  TREE_TYPE (decl));
   27801            3 :           return;
   27802              :         }
   27803              :     }
   27804      1915359 :   else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
   27805              :     {
   27806            0 :       error ("explicit instantiation of %q#D", decl);
   27807            0 :       return;
   27808              :     }
   27809              :   else
   27810              :     result = decl;
   27811              : 
   27812              :   /* Check for various error cases.  Note that if the explicit
   27813              :      instantiation is valid the RESULT will currently be marked as an
   27814              :      *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
   27815              :      until we get here.  */
   27816              : 
   27817      1915434 :   if (DECL_TEMPLATE_SPECIALIZATION (result))
   27818              :     {
   27819              :       /* DR 259 [temp.spec].
   27820              : 
   27821              :          Both an explicit instantiation and a declaration of an explicit
   27822              :          specialization shall not appear in a program unless the explicit
   27823              :          instantiation follows a declaration of the explicit specialization.
   27824              : 
   27825              :          For a given set of template parameters, if an explicit
   27826              :          instantiation of a template appears after a declaration of an
   27827              :          explicit specialization for that template, the explicit
   27828              :          instantiation has no effect.  */
   27829              :       return;
   27830              :     }
   27831      1880217 :   else if (DECL_EXPLICIT_INSTANTIATION (result))
   27832              :     {
   27833              :       /* [temp.spec]
   27834              : 
   27835              :          No program shall explicitly instantiate any template more
   27836              :          than once.
   27837              : 
   27838              :          We check DECL_NOT_REALLY_EXTERN so as not to complain when
   27839              :          the first instantiation was `extern' and the second is not,
   27840              :          and EXTERN_P for the opposite case.  */
   27841          800 :       if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
   27842            6 :         permerror (input_location, "duplicate explicit instantiation of %q#D", result);
   27843              :       /* If an "extern" explicit instantiation follows an ordinary
   27844              :          explicit instantiation, the template is instantiated.  */
   27845              :       if (extern_p)
   27846              :         return;
   27847              :     }
   27848      1879417 :   else if (!DECL_IMPLICIT_INSTANTIATION (result))
   27849              :     {
   27850            0 :       error ("no matching template for %qD found", result);
   27851            0 :       return;
   27852              :     }
   27853      1879417 :   else if (!DECL_TEMPLATE_INFO (result))
   27854              :     {
   27855            0 :       permerror (input_location, "explicit instantiation of non-template %q#D", result);
   27856            0 :       return;
   27857              :     }
   27858              : 
   27859      1880217 :   if (storage == NULL_TREE)
   27860              :     ;
   27861      1876850 :   else if (storage == ridpointers[(int) RID_EXTERN])
   27862              :     {
   27863      1876850 :       if (cxx_dialect == cxx98 && pedantic)
   27864         7392 :         pedwarn (input_location, OPT_Wc__11_extensions,
   27865              :                  "ISO C++ 1998 forbids the use of %<extern%> on explicit "
   27866              :                  "instantiations");
   27867              :       extern_p = 1;
   27868              :     }
   27869              :   else
   27870            0 :     error ("storage class %qD applied to template instantiation", storage);
   27871              : 
   27872      1880217 :   check_explicit_instantiation_namespace (result);
   27873      1880217 :   mark_decl_instantiated (result, extern_p);
   27874      1880217 :   if (! extern_p)
   27875         3367 :     instantiate_decl (result, /*defer_ok=*/true,
   27876              :                       /*expl_inst_class_mem_p=*/false);
   27877              : }
   27878              : 
   27879              : static void
   27880       791782 : mark_class_instantiated (tree t, int extern_p)
   27881              : {
   27882       791782 :   SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
   27883       791782 :   SET_CLASSTYPE_INTERFACE_KNOWN (t);
   27884       791782 :   CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
   27885       791782 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
   27886       791782 :   if (! extern_p)
   27887              :     {
   27888         2712 :       CLASSTYPE_DEBUG_REQUESTED (t) = 1;
   27889         2712 :       rest_of_type_compilation (t, 1);
   27890              :     }
   27891       791782 : }
   27892              : 
   27893              : /* Perform an explicit instantiation of template class T.  STORAGE, if
   27894              :    non-null, is the RID for extern, inline or static.  COMPLAIN is
   27895              :    nonzero if this is called from the parser, zero if called recursively,
   27896              :    since the standard is unclear (as detailed below).  */
   27897              : 
   27898              : void
   27899       852659 : do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
   27900              : {
   27901       852659 :   if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
   27902              :     {
   27903            3 :       if (tree ti = TYPE_TEMPLATE_INFO (t))
   27904            0 :         error ("explicit instantiation of non-class template %qD",
   27905            0 :                TI_TEMPLATE (ti));
   27906              :       else
   27907            3 :         error ("explicit instantiation of non-template type %qT", t);
   27908            3 :       return;
   27909              :     }
   27910              : 
   27911       852656 :   complete_type (t);
   27912              : 
   27913       852656 :   if (!COMPLETE_TYPE_P (t))
   27914              :     {
   27915          226 :       if (complain & tf_error)
   27916            0 :         error ("explicit instantiation of %q#T before definition of template",
   27917              :                t);
   27918          226 :       return;
   27919              :     }
   27920              : 
   27921              :   /* At most one of these will be true.  */
   27922       852430 :   bool extern_p = false;
   27923       852430 :   bool nomem_p = false;
   27924       852430 :   bool static_p = false;
   27925              : 
   27926       852430 :   if (storage != NULL_TREE)
   27927              :     {
   27928       849679 :       if (storage == ridpointers[(int) RID_EXTERN])
   27929              :         {
   27930       849654 :           if (cxx_dialect == cxx98 && pedantic)
   27931         2553 :             pedwarn (input_location, OPT_Wc__11_extensions,
   27932              :                      "ISO C++ 1998 forbids the use of %<extern%> on "
   27933              :                      "explicit instantiations");
   27934              :         }
   27935              :       else
   27936           25 :         pedwarn (input_location, OPT_Wpedantic,
   27937              :                  "ISO C++ forbids the use of %qE"
   27938              :                  " on explicit instantiations", storage);
   27939              : 
   27940       849679 :       if (storage == ridpointers[(int) RID_INLINE])
   27941              :         nomem_p = true;
   27942       849657 :       else if (storage == ridpointers[(int) RID_EXTERN])
   27943              :         extern_p = true;
   27944            3 :       else if (storage == ridpointers[(int) RID_STATIC])
   27945              :         static_p = true;
   27946              :       else
   27947            0 :         error ("storage class %qD applied to template instantiation",
   27948              :                storage);
   27949              :     }
   27950              : 
   27951       852430 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
   27952              :     /* DR 259 [temp.spec].
   27953              : 
   27954              :        Both an explicit instantiation and a declaration of an explicit
   27955              :        specialization shall not appear in a program unless the
   27956              :        explicit instantiation follows a declaration of the explicit
   27957              :        specialization.
   27958              : 
   27959              :        For a given set of template parameters, if an explicit
   27960              :        instantiation of a template appears after a declaration of an
   27961              :        explicit specialization for that template, the explicit
   27962              :        instantiation has no effect.  */
   27963              :     return;
   27964              : 
   27965       791785 :   if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
   27966              :     {
   27967              :       /* We've already instantiated the template.  */
   27968              : 
   27969              :       /* [temp.spec]
   27970              : 
   27971              :          No program shall explicitly instantiate any template more
   27972              :          than once.
   27973              : 
   27974              :          If EXTERN_P then this is ok.  */
   27975            3 :       if (!extern_p && (complain & tf_error))
   27976            3 :         permerror (input_location,
   27977              :                    "duplicate explicit instantiation of %q#T", t);
   27978              : 
   27979            3 :       return;
   27980              :     }
   27981              : 
   27982       791782 :   check_explicit_instantiation_namespace (TYPE_NAME (t));
   27983       791782 :   mark_class_instantiated (t, extern_p);
   27984              : 
   27985       791782 :   if (nomem_p)
   27986              :     return;
   27987              : 
   27988              :   /* In contrast to implicit instantiation, where only the
   27989              :      declarations, and not the definitions, of members are
   27990              :      instantiated, we have here:
   27991              : 
   27992              :          [temp.explicit]
   27993              : 
   27994              :          An explicit instantiation that names a class template
   27995              :          specialization is also an explicit instantiation of the same
   27996              :          kind (declaration or definition) of each of its members (not
   27997              :          including members inherited from base classes and members
   27998              :          that are templates) that has not been previously explicitly
   27999              :          specialized in the translation unit containing the explicit
   28000              :          instantiation, provided that the associated constraints, if
   28001              :          any, of that member are satisfied by the template arguments
   28002              :          of the explicit instantiation.  */
   28003     28971423 :   for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
   28004     28179666 :     if ((VAR_P (fld)
   28005     27855846 :          || (TREE_CODE (fld) == FUNCTION_DECL
   28006     18433812 :              && !static_p
   28007     18433812 :              && user_provided_p (fld)))
   28008     17474032 :         && DECL_TEMPLATE_INSTANTIATION (fld)
   28009     45329517 :         && constraints_satisfied_p (fld))
   28010              :       {
   28011     17149203 :         mark_decl_instantiated (fld, extern_p);
   28012     17149203 :         if (! extern_p)
   28013        28599 :           instantiate_decl (fld, /*defer_ok=*/true,
   28014              :                             /*expl_inst_class_mem_p=*/true);
   28015              :       }
   28016     11030463 :     else if (DECL_IMPLICIT_TYPEDEF_P (fld))
   28017              :       {
   28018        82050 :         tree type = TREE_TYPE (fld);
   28019              : 
   28020        81367 :         if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   28021       163401 :             && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
   28022        81351 :           do_type_instantiation (type, storage, 0);
   28023              :       }
   28024              : }
   28025              : 
   28026              : /* Given a function DECL, which is a specialization of TMPL, modify
   28027              :    DECL to be a re-instantiation of TMPL with the same template
   28028              :    arguments.  TMPL should be the template into which tsubst'ing
   28029              :    should occur for DECL, not the most general template.
   28030              : 
   28031              :    One reason for doing this is a scenario like this:
   28032              : 
   28033              :      template <class T>
   28034              :      void f(const T&, int i);
   28035              : 
   28036              :      void g() { f(3, 7); }
   28037              : 
   28038              :      template <class T>
   28039              :      void f(const T& t, const int i) { }
   28040              : 
   28041              :    Note that when the template is first instantiated, with
   28042              :    instantiate_template, the resulting DECL will have no name for the
   28043              :    first parameter, and the wrong type for the second.  So, when we go
   28044              :    to instantiate the DECL, we regenerate it.  */
   28045              : 
   28046              : static void
   28047     46862389 : regenerate_decl_from_template (tree decl, tree tmpl, tree args)
   28048              : {
   28049              :   /* The arguments used to instantiate DECL, from the most general
   28050              :      template.  */
   28051     46862389 :   tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
   28052              : 
   28053              :   /* Make sure that we can see identifiers, and compute access correctly.  */
   28054     46862389 :   push_access_scope (decl);
   28055              : 
   28056     46862389 :   if (TREE_CODE (decl) == FUNCTION_DECL)
   28057              :     {
   28058     28093362 :       tree specs;
   28059     28093362 :       int args_depth;
   28060     28093362 :       int parms_depth;
   28061              : 
   28062              :       /* Don't bother with this for unique friends that can't be redeclared and
   28063              :          might change type if regenerated (PR69836).  */
   28064     28093362 :       if (DECL_UNIQUE_FRIEND_P (decl))
   28065       159602 :         goto done;
   28066              : 
   28067              :       /* A template with a lambda in the signature also changes type if
   28068              :          regenerated (PR119401).  */
   28069     27933760 :       walk_tree_fn find_lambda
   28070              :         = [](tree *tp, int *, void *)
   28071              :         {
   28072              :           if (TREE_CODE (*tp) == LAMBDA_EXPR)
   28073              :             return *tp;
   28074              :           return NULL_TREE;
   28075              :         };
   28076     27933760 :       if (cp_walk_tree_without_duplicates
   28077              :           (&TREE_TYPE (tmpl), find_lambda, nullptr))
   28078           42 :         goto done;
   28079              : 
   28080              :       /* Use the source location of the definition.  */
   28081     27933718 :       DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
   28082              : 
   28083     55867436 :       args_depth = TMPL_ARGS_DEPTH (args);
   28084     27933718 :       parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   28085     27933718 :       if (args_depth > parms_depth)
   28086            0 :         args = get_innermost_template_args (args, parms_depth);
   28087              : 
   28088              :       /* Instantiate a dynamic exception-specification.  noexcept will be
   28089              :          handled below.  */
   28090     27933718 :       if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
   28091     10591459 :         if (TREE_VALUE (raises))
   28092              :           {
   28093           22 :             specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
   28094              :                                                     args, tf_error, NULL_TREE,
   28095              :                                                     /*defer_ok*/false);
   28096           22 :             if (specs && specs != error_mark_node)
   28097           21 :               TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
   28098              :                                                           specs);
   28099              :           }
   28100              : 
   28101              :       /* Merge parameter declarations.  */
   28102     27933718 :       if (tree pattern_parm
   28103     27933718 :           = skip_artificial_parms_for (code_pattern,
   28104     27933718 :                                        DECL_ARGUMENTS (code_pattern)))
   28105              :         {
   28106     19272833 :           tree *p = &DECL_ARGUMENTS (decl);
   28107     28343093 :           for (int skip = num_artificial_parms_for (decl); skip; --skip)
   28108      9070260 :             p = &DECL_CHAIN (*p);
   28109     19272833 :           tree oldarg = *p;
   28110     19272833 :           *p = tsubst_decl (pattern_parm, args, tf_error);
   28111     52508503 :           for (tree t = *p; t; t = DECL_CHAIN (t))
   28112     33235670 :             DECL_CONTEXT (t) = decl;
   28113              :           /* Mark the old PARM_DECLs in case std::meta::parameters_of has
   28114              :              been called on the old declaration and reflections of those
   28115              :              arguments are held across this point and used later.
   28116              :              Such PARM_DECLs are no longer present in
   28117              :              DECL_ARGUMENTS (DECL_CONTEXT (oldarg)) chain.  */
   28118     19272833 :           if (*p != oldarg)
   28119     52480401 :             for (tree t = oldarg; t; t = DECL_CHAIN (t))
   28120     33235670 :               OLD_PARM_DECL_P (t) = 1;
   28121              :         }
   28122              : 
   28123     27933718 :       if (tree attr = get_fn_contract_specifiers (decl))
   28124              :         {
   28125              :           /* If we're regenerating a specialization, the contracts will have
   28126              :              been copied from the most general template. Replace those with
   28127              :              the ones from the actual specialization.  */
   28128          140 :           tree tmpl = DECL_TI_TEMPLATE (decl);
   28129          140 :           if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
   28130            4 :             attr = get_fn_contract_specifiers (code_pattern);
   28131              : 
   28132          140 :           tsubst_contract_specifiers (attr, decl, args,
   28133              :                                       tf_warning_or_error, code_pattern);
   28134              :         }
   28135              : 
   28136              :       /* Merge additional specifiers from the CODE_PATTERN.  */
   28137     27933718 :       if (DECL_DECLARED_INLINE_P (code_pattern)
   28138     54097770 :           && !DECL_DECLARED_INLINE_P (decl))
   28139        62595 :         DECL_DECLARED_INLINE_P (decl) = 1;
   28140              : 
   28141     27933718 :       maybe_instantiate_noexcept (decl, tf_error);
   28142              :     }
   28143     18769027 :   else if (VAR_P (decl))
   28144              :     {
   28145     18769027 :       start_lambda_scope (decl);
   28146     18769027 :       DECL_INITIAL (decl) =
   28147     18769027 :         tsubst_init (DECL_INITIAL (code_pattern), decl, args,
   28148     18769027 :                      tf_error, DECL_TI_TEMPLATE (decl));
   28149     18769027 :       finish_lambda_scope ();
   28150     18769027 :       if (VAR_HAD_UNKNOWN_BOUND (decl))
   28151          208 :         TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
   28152          208 :                                    tf_error, DECL_TI_TEMPLATE (decl));
   28153              :     }
   28154              :   else
   28155            0 :     gcc_unreachable ();
   28156              : 
   28157     46862389 :  done:
   28158     46862389 :   pop_access_scope (decl);
   28159     46862389 : }
   28160              : 
   28161              : /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
   28162              :    substituted to get DECL.  */
   28163              : 
   28164              : tree
   28165    124417113 : template_for_substitution (tree decl)
   28166              : {
   28167    124417113 :   tree tmpl = DECL_TI_TEMPLATE (decl);
   28168    124417113 :   if (VAR_P (decl))
   28169     24413170 :     if (tree partial = most_specialized_partial_spec (decl, tf_none))
   28170      1092692 :       if (partial != error_mark_node)
   28171      1092692 :         tmpl = TI_TEMPLATE (partial);
   28172              : 
   28173              :   /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
   28174              :      for the instantiation.  This is not always the most general
   28175              :      template.  Consider, for example:
   28176              : 
   28177              :         template <class T>
   28178              :         struct S { template <class U> void f();
   28179              :                    template <> void f<int>(); };
   28180              : 
   28181              :      and an instantiation of S<double>::f<int>.  We want TD to be the
   28182              :      specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
   28183              :   while (/* An instantiation cannot have a definition, so we need a
   28184              :             more general template.  */
   28185    275105216 :          DECL_TEMPLATE_INSTANTIATION (tmpl)
   28186              :            /* We must also deal with friend templates.  Given:
   28187              : 
   28188              :                 template <class T> struct S {
   28189              :                   template <class U> friend void f() {};
   28190              :                 };
   28191              : 
   28192              :               S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
   28193              :               so far as the language is concerned, but that's still
   28194              :               where we get the pattern for the instantiation from.  On
   28195              :               other hand, if the definition comes outside the class, say:
   28196              : 
   28197              :                 template <class T> struct S {
   28198              :                   template <class U> friend void f();
   28199              :                 };
   28200              :                 template <class U> friend void f() {}
   28201              : 
   28202              :               we don't need to look any further.  That's what the check for
   28203              :               DECL_INITIAL is for.  */
   28204    137552608 :           || (TREE_CODE (decl) == FUNCTION_DECL
   28205     99397925 :               && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
   28206      1102369 :               && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
   28207              :     {
   28208              :       /* The present template, TD, should not be a definition.  If it
   28209              :          were a definition, we should be using it!  Note that we
   28210              :          cannot restructure the loop to just keep going until we find
   28211              :          a template with a definition, since that might go too far if
   28212              :          a specialization was declared, but not defined.  */
   28213              : 
   28214              :       /* Fetch the more general template.  */
   28215     13135495 :       tmpl = DECL_TI_TEMPLATE (tmpl);
   28216              :     }
   28217              : 
   28218    124417113 :   return tmpl;
   28219              : }
   28220              : 
   28221              : /* Returns true if we need to instantiate this template instance even if we
   28222              :    know we aren't going to emit it.  */
   28223              : 
   28224              : bool
   28225      1417779 : always_instantiate_p (tree decl)
   28226              : {
   28227              :   /* We always instantiate inline functions so that we can inline them.  An
   28228              :      explicit instantiation declaration prohibits implicit instantiation of
   28229              :      non-inline functions.  With high levels of optimization, we would
   28230              :      normally inline non-inline functions -- but we're not allowed to do
   28231              :      that for "extern template" functions.  Therefore, we check
   28232              :      DECL_DECLARED_INLINE_P, rather than possibly_inlined_p.  */
   28233      1417779 :   return ((TREE_CODE (decl) == FUNCTION_DECL
   28234      1396767 :            && (DECL_DECLARED_INLINE_P (decl)
   28235        51803 :                || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
   28236              :           /* And we need to instantiate static data members so that
   28237              :              their initializers are available in integral constant
   28238              :              expressions.  */
   28239      1469582 :           || (VAR_P (decl)
   28240        21012 :               && decl_maybe_constant_var_p (decl)));
   28241              : }
   28242              : 
   28243              : /* If FN has a noexcept-specifier that hasn't been instantiated yet,
   28244              :    instantiate it now, modifying TREE_TYPE (fn).  Returns false on
   28245              :    error, true otherwise.  */
   28246              : 
   28247              : bool
   28248    561770522 : maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
   28249              : {
   28250    561770522 :   if (fn == error_mark_node)
   28251              :     return false;
   28252              : 
   28253              :   /* Don't instantiate a noexcept-specification from template context.  */
   28254    561770522 :   if (processing_template_decl
   28255    561770522 :       && (!flag_noexcept_type || type_dependent_expression_p (fn)))
   28256       171929 :     return true;
   28257              : 
   28258    561598593 :   tree fntype = TREE_TYPE (fn);
   28259    561598593 :   tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
   28260              : 
   28261    301011101 :   if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
   28262    571539712 :       && DECL_MAYBE_DELETED (fn))
   28263              :     {
   28264        37275 :       if (fn == current_function_decl)
   28265              :         /* We're in start_preparsed_function, keep going.  */
   28266              :         return true;
   28267              : 
   28268           23 :       ++function_depth;
   28269           23 :       maybe_synthesize_method (fn);
   28270           23 :       --function_depth;
   28271           23 :       return !DECL_DELETED_FN (fn);
   28272              :     }
   28273              : 
   28274    862572419 :   if (!spec || !TREE_PURPOSE (spec))
   28275              :     return true;
   28276              : 
   28277    299947318 :   tree noex = TREE_PURPOSE (spec);
   28278    299947318 :   if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   28279    299947318 :       && TREE_CODE (noex) != DEFERRED_PARSE)
   28280              :     return true;
   28281              : 
   28282     12704493 :   tree orig_fn = NULL_TREE;
   28283              :   /* For a member friend template we can get a TEMPLATE_DECL.  Let's use
   28284              :      its FUNCTION_DECL for the rest of this function -- push_access_scope
   28285              :      doesn't accept TEMPLATE_DECLs.  */
   28286     12704493 :   if (DECL_FUNCTION_TEMPLATE_P (fn))
   28287              :     {
   28288           46 :       orig_fn = fn;
   28289           46 :       fn = DECL_TEMPLATE_RESULT (fn);
   28290              :     }
   28291              : 
   28292     12704493 :   if (DECL_CLONED_FUNCTION_P (fn))
   28293              :     {
   28294      5882618 :       tree prime = DECL_CLONED_FUNCTION (fn);
   28295      5882618 :       if (!maybe_instantiate_noexcept (prime, complain))
   28296              :         return false;
   28297      5882517 :       spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
   28298              :     }
   28299      6821875 :   else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
   28300              :     {
   28301      6821875 :       static hash_set<tree>* fns = new hash_set<tree>;
   28302      6821875 :       bool added = false;
   28303      6821875 :       tree pattern = DEFERRED_NOEXCEPT_PATTERN (noex);
   28304      6821875 :       if (pattern == NULL_TREE)
   28305              :         {
   28306      4779140 :           spec = get_defaulted_eh_spec (fn, complain);
   28307      4779140 :           if (spec == error_mark_node)
   28308              :             /* This might have failed because of an unparsed DMI, so
   28309              :                let's try again later.  */
   28310              :             return false;
   28311              :         }
   28312      2042735 :       else if (!(added = !fns->add (fn)))
   28313              :         {
   28314              :           /* If hash_set::add returns true, the element was already there.  */
   28315            3 :           location_t loc = cp_expr_loc_or_loc (pattern,
   28316            3 :                                                DECL_SOURCE_LOCATION (fn));
   28317            3 :           error_at (loc,
   28318              :                     "exception specification of %qD depends on itself",
   28319              :                     fn);
   28320            3 :           spec = noexcept_false_spec;
   28321              :         }
   28322      2042732 :       else if (TREE_CODE (pattern) == DEFERRED_PARSE)
   28323              :         {
   28324            6 :           error ("exception specification of %qD is not available "
   28325              :                  "until end of class definition", fn);
   28326            6 :           spec = noexcept_false_spec;
   28327              :         }
   28328      2042726 :       else if (push_tinst_level (fn))
   28329              :         {
   28330      2042651 :           const bool push_to_top = maybe_push_to_top_level (fn);
   28331      2042651 :           push_access_scope (fn);
   28332      2042651 :           push_deferring_access_checks (dk_no_deferred);
   28333      2042651 :           input_location = DECL_SOURCE_LOCATION (fn);
   28334              : 
   28335      2042651 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   28336      2042651 :               && !DECL_LOCAL_DECL_P (fn))
   28337              :             {
   28338              :               /* If needed, set current_class_ptr for the benefit of
   28339              :                  tsubst_copy/PARM_DECL.  */
   28340      1149353 :               tree this_parm = DECL_ARGUMENTS (fn);
   28341      1149353 :               current_class_ptr = NULL_TREE;
   28342      1149353 :               current_class_ref = cp_build_fold_indirect_ref (this_parm);
   28343      1149353 :               current_class_ptr = this_parm;
   28344              :             }
   28345              : 
   28346              :           /* If this function is represented by a TEMPLATE_DECL, then
   28347              :              the deferred noexcept-specification might still contain
   28348              :              dependent types, even after substitution.  And we need the
   28349              :              dependency check functions to work in build_noexcept_spec.  */
   28350      2042651 :           if (orig_fn)
   28351           46 :             ++processing_template_decl;
   28352              : 
   28353              :           /* Do deferred instantiation of the noexcept-specifier.  */
   28354      2042651 :           noex = tsubst_expr (pattern, DEFERRED_NOEXCEPT_ARGS (noex),
   28355              :                               tf_warning_or_error, fn);
   28356              :           /* Build up the noexcept-specification.  */
   28357      2042651 :           spec = build_noexcept_spec (noex, tf_warning_or_error);
   28358              : 
   28359      2042651 :           if (orig_fn)
   28360           46 :             --processing_template_decl;
   28361              : 
   28362      2042651 :           pop_deferring_access_checks ();
   28363      2042651 :           pop_access_scope (fn);
   28364      2042651 :           pop_tinst_level ();
   28365      2042651 :           maybe_pop_from_top_level (push_to_top);
   28366              :         }
   28367              :       else
   28368           75 :         spec = noexcept_false_spec;
   28369              : 
   28370      2042735 :       if (added)
   28371      2042732 :         fns->remove (fn);
   28372              :     }
   28373              : 
   28374     12704291 :   if (spec == error_mark_node)
   28375              :     {
   28376              :       /* This failed with a hard error, so let's go with false.  */
   28377           25 :       gcc_assert (seen_error ());
   28378           25 :       spec = noexcept_false_spec;
   28379              :     }
   28380              : 
   28381     12704291 :   TREE_TYPE (fn) = build_exception_variant (fntype, spec);
   28382     12704291 :   if (orig_fn)
   28383           46 :     TREE_TYPE (orig_fn) = TREE_TYPE (fn);
   28384              : 
   28385              :   return true;
   28386              : }
   28387              : 
   28388              : /* We're starting to process the function INST, an instantiation of PATTERN;
   28389              :    add their parameters to local_specializations.  */
   28390              : 
   28391              : void
   28392     28632370 : register_parameter_specializations (tree pattern, tree inst)
   28393              : {
   28394     28632370 :   tree tmpl_parm = DECL_ARGUMENTS (pattern);
   28395     28632370 :   tree spec_parm = DECL_ARGUMENTS (inst);
   28396     28632370 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (inst))
   28397              :     {
   28398     15745052 :       register_local_specialization (spec_parm, tmpl_parm);
   28399     15745052 :       spec_parm = skip_artificial_parms_for (inst, spec_parm);
   28400     15745052 :       tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
   28401              :     }
   28402     62887201 :   for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
   28403              :     {
   28404     34254831 :       if (!DECL_PACK_P (tmpl_parm))
   28405              :         {
   28406     33554684 :           register_local_specialization (spec_parm, tmpl_parm);
   28407     33554684 :           spec_parm = DECL_CHAIN (spec_parm);
   28408              :         }
   28409              :       else
   28410              :         {
   28411              :           /* Register the (value) argument pack as a specialization of
   28412              :              TMPL_PARM, then move on.  */
   28413       700147 :           tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
   28414       700147 :           register_local_specialization (argpack, tmpl_parm);
   28415              :         }
   28416              :     }
   28417     28632370 :   gcc_assert (!spec_parm);
   28418     28632370 : }
   28419              : 
   28420              : /* Instantiate the body of D using PATTERN with ARGS.  We have
   28421              :    already determined PATTERN is the correct template to use.
   28422              :    NESTED_P is true if this is a nested function, in which case
   28423              :    PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL.  */
   28424              : 
   28425              : static void
   28426     46862477 : instantiate_body (tree pattern, tree args, tree d, bool nested_p)
   28427              : {
   28428     46862477 :   tree td = NULL_TREE;
   28429     46862477 :   tree code_pattern = pattern;
   28430              : 
   28431     46862477 :   if (!nested_p)
   28432              :     {
   28433     46862389 :       td = pattern;
   28434     46862389 :       code_pattern = DECL_TEMPLATE_RESULT (td);
   28435              :     }
   28436              :   else
   28437              :     /* Only OMP reductions are nested.  */
   28438           88 :     gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
   28439              : 
   28440     46862477 :   vec<tree> omp_privatization_save;
   28441     46862477 :   if (current_function_decl)
   28442     18905961 :     save_omp_privatization_clauses (omp_privatization_save);
   28443              : 
   28444     46862477 :   bool push_to_top = maybe_push_to_top_level (d);
   28445              : 
   28446     46862477 :   mark_template_arguments_used (pattern, args);
   28447              : 
   28448     46862477 :   if (VAR_P (d))
   28449              :     {
   28450              :       /* The variable might be a lambda's extra scope, and that
   28451              :          lambda's visibility depends on D's.  */
   28452     18769027 :       maybe_commonize_var (d);
   28453     18769027 :       determine_visibility (d);
   28454              :     }
   28455              : 
   28456              :   /* Mark D as instantiated so that recursive calls to
   28457              :      instantiate_decl do not try to instantiate it again.  */
   28458     46862477 :   DECL_TEMPLATE_INSTANTIATED (d) = 1;
   28459              : 
   28460     46862477 :   if (td)
   28461              :     /* Regenerate the declaration in case the template has been modified
   28462              :        by a subsequent redeclaration.  */
   28463     46862389 :     regenerate_decl_from_template (d, td, args);
   28464              : 
   28465              :   /* We already set the file and line above.  Reset them now in case
   28466              :      they changed as a result of calling regenerate_decl_from_template.  */
   28467     46862477 :   input_location = DECL_SOURCE_LOCATION (d);
   28468              : 
   28469     46862477 :   if (VAR_P (d))
   28470              :     {
   28471              :       /* Clear out DECL_RTL; whatever was there before may not be right
   28472              :          since we've reset the type of the declaration.  */
   28473     18769027 :       SET_DECL_RTL (d, NULL);
   28474     18769027 :       DECL_IN_AGGR_P (d) = 0;
   28475              : 
   28476              :       /* The initializer is placed in DECL_INITIAL by
   28477              :          regenerate_decl_from_template so we don't need to
   28478              :          push/pop_access_scope again here.  Pull it out so that
   28479              :          cp_finish_decl can process it.  */
   28480     18769027 :       bool const_init = false;
   28481     18769027 :       tree init = DECL_INITIAL (d);
   28482     18769027 :       DECL_INITIAL (d) = NULL_TREE;
   28483     18769027 :       DECL_INITIALIZED_P (d) = 0;
   28484              : 
   28485              :       /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
   28486              :          initializer.  That function will defer actual emission until
   28487              :          we have a chance to determine linkage.  */
   28488     18769027 :       DECL_EXTERNAL (d) = 0;
   28489              : 
   28490              :       /* Enter the scope of D so that access-checking works correctly.  */
   28491     18769027 :       bool enter_context = DECL_CLASS_SCOPE_P (d);
   28492      2223360 :       if (enter_context)
   28493      2223360 :         push_nested_class (DECL_CONTEXT (d));
   28494              : 
   28495     18769027 :       const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   28496     18769027 :       cp_finish_decl (d, init, const_init, NULL_TREE, 0);
   28497              : 
   28498     18769027 :       if (enter_context)
   28499      2223360 :         pop_nested_class ();
   28500              :     }
   28501     28093450 :   else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
   28502           67 :     synthesize_method (d);
   28503     28093383 :   else if (TREE_CODE (d) == FUNCTION_DECL)
   28504              :     {
   28505              :       /* Set up the list of local specializations.  */
   28506     29222730 :       local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
   28507     28093383 :       tree block = NULL_TREE;
   28508              : 
   28509              :       /* Set up context.  */
   28510     28093383 :       if (nested_p)
   28511           88 :         block = push_stmt_list ();
   28512              :       else
   28513              :         {
   28514     28093295 :           start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
   28515              : 
   28516     28093295 :           perform_instantiation_time_access_checks (code_pattern, args);
   28517              :         }
   28518              : 
   28519              :       /* Create substitution entries for the parameters.  */
   28520     28093383 :       register_parameter_specializations (code_pattern, d);
   28521              : 
   28522              :       /* Substitute into the body of the function.  */
   28523     28093383 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   28524          104 :         tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
   28525              :                         tf_warning_or_error, d);
   28526              :       else
   28527              :         {
   28528     28093279 :           tsubst_stmt (DECL_SAVED_TREE (code_pattern), args,
   28529     28093279 :                        tf_warning_or_error, DECL_TI_TEMPLATE (d));
   28530              : 
   28531              :           /* Set the current input_location to the end of the function
   28532              :              so that finish_function knows where we are.  */
   28533     28093270 :           input_location
   28534     28093270 :             = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
   28535              : 
   28536              :           /* Remember if we saw an infinite loop in the template.  */
   28537     28093270 :           current_function_infinite_loop
   28538     28093270 :             = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
   28539              :         }
   28540              : 
   28541              :       /* Finish the function.  */
   28542     28093374 :       if (nested_p)
   28543           88 :         DECL_SAVED_TREE (d) = pop_stmt_list (block);
   28544              :       else
   28545              :         {
   28546     28093286 :           d = finish_function (/*inline_p=*/false);
   28547     28093286 :           expand_or_defer_fn (d);
   28548              :         }
   28549              : 
   28550     28093374 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   28551          104 :         cp_check_omp_declare_reduction (d);
   28552              : 
   28553     28093374 :       check_consteval_only_fn (d);
   28554              : 
   28555     28093374 :       if (int errs = errorcount + sorrycount)
   28556      1095775 :         if (errs > current_tinst_level->errors)
   28557         2311 :           if (function *f = DECL_STRUCT_FUNCTION (d))
   28558         2293 :             f->language->erroneous = true;
   28559     28093374 :     }
   28560              : 
   28561              :   /* We're not deferring instantiation any more.  */
   28562     46862468 :   if (!nested_p)
   28563     46862380 :     TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
   28564              : 
   28565     46862468 :   maybe_pop_from_top_level (push_to_top);
   28566              : 
   28567     46862468 :   if (current_function_decl)
   28568     18905961 :     restore_omp_privatization_clauses (omp_privatization_save);
   28569     46862468 : }
   28570              : 
   28571              : /* Produce the definition of D, a _DECL generated from a template.  If
   28572              :    DEFER_OK is true, then we don't have to actually do the
   28573              :    instantiation now; we just have to do it sometime.  Normally it is
   28574              :    an error if this is an explicit instantiation but D is undefined.
   28575              :    EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
   28576              :    instantiated class template.  */
   28577              : 
   28578              : tree
   28579    188898503 : instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
   28580              : {
   28581    188898503 :   tree tmpl = DECL_TI_TEMPLATE (d);
   28582    188898503 :   tree gen_args;
   28583    188898503 :   tree args;
   28584    188898503 :   tree td;
   28585    188898503 :   tree code_pattern;
   28586    188898503 :   tree spec;
   28587    188898503 :   tree gen_tmpl;
   28588    188898503 :   bool pattern_defined;
   28589    188898503 :   location_t saved_loc = input_location;
   28590    188898503 :   bool external_p;
   28591    188898503 :   bool deleted_p;
   28592              : 
   28593              :   /* This function should only be used to instantiate templates for
   28594              :      functions and static member variables.  */
   28595    188898503 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
   28596              : 
   28597    188898503 :   gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
   28598              : 
   28599    188898503 :   if (modules_p ())
   28600              :     /* We may have a pending instantiation of D itself.  */
   28601       423622 :     lazy_load_pendings (d);
   28602              : 
   28603              :   /* Variables are never deferred; if instantiation is required, they
   28604              :      are instantiated right away.  That allows for better code in the
   28605              :      case that an expression refers to the value of the variable --
   28606              :      if the variable has a constant value the referring expression can
   28607              :      take advantage of that fact.  */
   28608    188898503 :   if (VAR_P (d))
   28609    131109343 :     defer_ok = false;
   28610              : 
   28611              :   /* Don't instantiate cloned functions.  Instead, instantiate the
   28612              :      functions they cloned.  */
   28613    188898503 :   if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
   28614      6248125 :     d = DECL_CLONED_FUNCTION (d);
   28615              : 
   28616    188898503 :   if (DECL_TEMPLATE_INSTANTIATED (d)
   28617     78336771 :       || TREE_TYPE (d) == error_mark_node
   28618     78336771 :       || (TREE_CODE (d) == FUNCTION_DECL
   28619     53773793 :           && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
   28620    267215988 :       || DECL_TEMPLATE_SPECIALIZATION (d))
   28621              :     /* D has already been instantiated or explicitly specialized, so
   28622              :        there's nothing for us to do here.
   28623              : 
   28624              :        It might seem reasonable to check whether or not D is an explicit
   28625              :        instantiation, and, if so, stop here.  But when an explicit
   28626              :        instantiation is deferred until the end of the compilation,
   28627              :        DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
   28628              :        the instantiation.  */
   28629              :     return d;
   28630              : 
   28631              :   /* Check to see whether we know that this template will be
   28632              :      instantiated in some other file, as with "extern template"
   28633              :      extension.  */
   28634     78102279 :   external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
   28635              : 
   28636              :   /* In general, we do not instantiate such templates.  */
   28637       898180 :   if (external_p && !always_instantiate_p (d))
   28638              :     return d;
   28639              : 
   28640     78090729 :   gen_tmpl = most_general_template (tmpl);
   28641     78090729 :   gen_args = DECL_TI_ARGS (d);
   28642              : 
   28643              :   /* We should already have the extra args.  */
   28644     91411303 :   gcc_checking_assert (tmpl == gen_tmpl
   28645              :                        || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
   28646              :                            == TMPL_ARGS_DEPTH (gen_args)));
   28647              :   /* And what's in the hash table should match D.  */
   28648     78090729 :   gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
   28649              :                        == d
   28650              :                        || spec == NULL_TREE);
   28651              : 
   28652              :   /* This needs to happen before any tsubsting.  */
   28653     78090729 :   if (! push_tinst_level (d))
   28654              :     return d;
   28655              : 
   28656     78089503 :   auto_timevar tv (TV_TEMPLATE_INST);
   28657              : 
   28658              :   /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
   28659              :      for the instantiation.  */
   28660     78089503 :   td = template_for_substitution (d);
   28661     78089503 :   args = gen_args;
   28662              : 
   28663     78089503 :   if (variable_template_specialization_p (d))
   28664              :     {
   28665              :       /* Look up an explicit specialization, if any.  */
   28666     16653794 :       tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error);
   28667     16653794 :       if (partial_ti && partial_ti != error_mark_node)
   28668              :         {
   28669      1092386 :           td = TI_TEMPLATE (partial_ti);
   28670      1092386 :           args = TI_ARGS (partial_ti);
   28671              :         }
   28672              :     }
   28673              : 
   28674     78089503 :   maybe_diagnose_erroneous_template (td);
   28675              : 
   28676     78089503 :   code_pattern = DECL_TEMPLATE_RESULT (td);
   28677              : 
   28678              :   /* We should never be trying to instantiate a member of a class
   28679              :      template or partial specialization.  */
   28680     78089503 :   gcc_assert (d != code_pattern);
   28681              : 
   28682    156179006 :   if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
   28683    124206766 :       || DECL_TEMPLATE_SPECIALIZATION (td))
   28684              :     /* In the case of a friend template whose definition is provided
   28685              :        outside the class, we may have too many arguments.  Drop the
   28686              :        ones we don't need.  The same is true for specializations.  */
   28687     31974022 :     args = get_innermost_template_args
   28688     31974022 :       (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
   28689              : 
   28690     78089503 :   if (TREE_CODE (d) == FUNCTION_DECL)
   28691              :     {
   28692     53711125 :       deleted_p = DECL_DELETED_FN (code_pattern);
   28693     53711125 :       pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
   28694     52570736 :                           && DECL_INITIAL (code_pattern) != error_mark_node)
   28695      1140433 :                          || DECL_DEFAULTED_FN (code_pattern)
   28696     54851407 :                          || deleted_p);
   28697              :     }
   28698              :   else
   28699              :     {
   28700     24378378 :       deleted_p = false;
   28701     24378378 :       if (DECL_CLASS_SCOPE_P (code_pattern))
   28702      7831090 :         pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
   28703              :       else
   28704     16547288 :         pattern_defined = ! DECL_EXTERNAL (code_pattern);
   28705              :     }
   28706              : 
   28707              :   /* We may be in the middle of deferred access check.  Disable it now.  */
   28708     78089503 :   push_deferring_access_checks (dk_no_deferred);
   28709              : 
   28710              :   /* Unless an explicit instantiation directive has already determined
   28711              :      the linkage of D, remember that a definition is available for
   28712              :      this entity.  */
   28713     78089503 :   if (pattern_defined
   28714     71341337 :       && !DECL_INTERFACE_KNOWN (d)
   28715    146697483 :       && !DECL_NOT_REALLY_EXTERN (d))
   28716     28137790 :     mark_definable (d);
   28717              : 
   28718     78089503 :   DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
   28719     78089503 :   DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
   28720     78089503 :   input_location = DECL_SOURCE_LOCATION (d);
   28721              : 
   28722              :   /* If D is a member of an explicitly instantiated class template,
   28723              :      and no definition is available, treat it like an implicit
   28724              :      instantiation.  */
   28725     78089503 :   if (!pattern_defined && expl_inst_class_mem_p
   28726     78089503 :       && DECL_EXPLICIT_INSTANTIATION (d))
   28727              :     {
   28728              :       /* Leave linkage flags alone on instantiations with anonymous
   28729              :          visibility.  */
   28730          357 :       if (TREE_PUBLIC (d))
   28731              :         {
   28732          348 :           DECL_NOT_REALLY_EXTERN (d) = 0;
   28733          348 :           DECL_INTERFACE_KNOWN (d) = 0;
   28734              :         }
   28735          357 :       SET_DECL_IMPLICIT_INSTANTIATION (d);
   28736              :     }
   28737              : 
   28738              :   /* Defer all other templates, unless we have been explicitly
   28739              :      forbidden from doing so.  */
   28740     78089503 :   if (/* If there is no definition, we cannot instantiate the
   28741              :          template.  */
   28742              :       ! pattern_defined
   28743              :       /* If it's OK to postpone instantiation, do so.  */
   28744     78089503 :       || defer_ok
   28745              :       /* If this is a static data member that will be defined
   28746              :          elsewhere, we don't want to instantiate the entire data
   28747              :          member, but we do want to instantiate the initializer so that
   28748              :          we can substitute that elsewhere.  */
   28749     46863847 :       || (external_p && VAR_P (d))
   28750              :       /* Handle here a deleted function too, avoid generating
   28751              :          its body (c++/61080).  */
   28752     46862696 :       || deleted_p
   28753              :       /* We need the initializer for an OpenMP declare mapper.  */
   28754    124951900 :       || (VAR_P (d) && DECL_LANG_SPECIFIC (d) && DECL_OMP_DECLARE_MAPPER_P (d)))
   28755              :     {
   28756              :       /* The definition of the static data member is now required so
   28757              :          we must substitute the initializer.  */
   28758     31227114 :       if (VAR_P (d)
   28759      5609351 :           && !DECL_INITIAL (d)
   28760     31695673 :           && DECL_INITIAL (code_pattern))
   28761              :         {
   28762       468020 :           tree ns;
   28763       468020 :           tree init;
   28764       468020 :           bool const_init = false;
   28765       468020 :           bool enter_context = DECL_CLASS_SCOPE_P (d);
   28766              : 
   28767       468020 :           ns = decl_namespace_context (d);
   28768       468020 :           push_nested_namespace (ns);
   28769       468020 :           if (enter_context)
   28770       467778 :             push_nested_class (DECL_CONTEXT (d));
   28771       468020 :           init = tsubst_expr (DECL_INITIAL (code_pattern),
   28772              :                               args,
   28773              :                               tf_warning_or_error, NULL_TREE);
   28774              :           /* If instantiating the initializer involved instantiating this
   28775              :              again, don't call cp_finish_decl twice.  */
   28776       459923 :           if (!DECL_INITIAL (d))
   28777              :             {
   28778              :               /* Make sure the initializer is still constant, in case of
   28779              :                  circular dependency (template/instantiate6.C). */
   28780       459920 :               const_init
   28781       459920 :                 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   28782       459920 :               cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
   28783              :                               /*asmspec_tree=*/NULL_TREE, 0);
   28784              :             }
   28785       457226 :           if (enter_context)
   28786       456984 :             pop_nested_class ();
   28787       457226 :           pop_nested_namespace (ns);
   28788              :         }
   28789              : 
   28790              :       /* We restore the source position here because it's used by
   28791              :          add_pending_template.  */
   28792     31216320 :       input_location = saved_loc;
   28793              : 
   28794     23231831 :       if (at_eof && !pattern_defined
   28795      5773217 :           && DECL_EXPLICIT_INSTANTIATION (d)
   28796     31216335 :           && DECL_NOT_REALLY_EXTERN (d))
   28797              :         /* [temp.explicit]
   28798              : 
   28799              :            The definition of a non-exported function template, a
   28800              :            non-exported member function template, or a non-exported
   28801              :            member function or static data member of a class template
   28802              :            shall be present in every translation unit in which it is
   28803              :            explicitly instantiated.  */
   28804           15 :         permerror (input_location,  "explicit instantiation of %qD "
   28805              :                    "but no definition available", d);
   28806              : 
   28807              :       /* If we're in unevaluated context, we just wanted to get the
   28808              :          constant value; this isn't an odr use, so don't queue
   28809              :          a full instantiation.  */
   28810     31216320 :       if (!cp_unevaluated_operand
   28811              :           /* ??? Historically, we have instantiated inline functions, even
   28812              :              when marked as "extern template".  */
   28813     31215825 :           && !(external_p && VAR_P (d)))
   28814     31214674 :         add_pending_template (d);
   28815              :     }
   28816              :   else
   28817              :     {
   28818     46862389 :       set_instantiating_module (d);
   28819     46862389 :       if (variable_template_p (gen_tmpl))
   28820     16651814 :         note_vague_linkage_variable (d);
   28821     46862389 :       instantiate_body (td, args, d, false);
   28822              :     }
   28823              : 
   28824     78078700 :   pop_deferring_access_checks ();
   28825     78078700 :   pop_tinst_level ();
   28826     78078700 :   input_location = saved_loc;
   28827              : 
   28828     78078700 :   return d;
   28829     78078700 : }
   28830              : 
   28831              : /* Run through the list of templates that we wish we could
   28832              :    instantiate, and instantiate any we can.  RETRIES is the
   28833              :    number of times we retry pending template instantiation.  */
   28834              : 
   28835              : void
   28836       141993 : instantiate_pending_templates (int retries)
   28837              : {
   28838       141993 :   int reconsider;
   28839       141993 :   location_t saved_loc = input_location;
   28840       141993 :   unsigned saved_module_kind = module_kind;
   28841              : 
   28842              :   /* Instantiating templates may trigger vtable generation.  This in turn
   28843              :      may require further template instantiations.  We place a limit here
   28844              :      to avoid infinite loop.  */
   28845       141993 :   if (pending_templates && retries >= max_tinst_depth)
   28846              :     {
   28847            3 :       tree decl = pending_templates->tinst->maybe_get_node ();
   28848              : 
   28849            3 :       fatal_error (input_location,
   28850              :                    "template instantiation depth exceeds maximum of %d"
   28851              :                    " instantiating %q+D, possibly from virtual table generation"
   28852              :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   28853              :                    max_tinst_depth, decl);
   28854              :       if (TREE_CODE (decl) == FUNCTION_DECL)
   28855              :         /* Pretend that we defined it.  */
   28856              :         DECL_INITIAL (decl) = error_mark_node;
   28857              :       return;
   28858              :     }
   28859              : 
   28860       187565 :   do
   28861              :     {
   28862       187565 :       struct pending_template **t = &pending_templates;
   28863       187565 :       struct pending_template *last = NULL;
   28864       187565 :       reconsider = 0;
   28865     28731125 :       while (*t)
   28866              :         {
   28867     28543569 :           struct tinst_level *tinst = (*t)->tinst;
   28868     28543569 :           bool complete = tinst_complete_p (tinst);
   28869              : 
   28870     28543569 :           if (!complete)
   28871              :             {
   28872     22907655 :               tree instantiation = reopen_tinst_level (tinst);
   28873              : 
   28874     22907655 :               if (limit_bad_template_recursion (instantiation))
   28875              :                 /* Do nothing.  */;
   28876     22906567 :               else if (TYPE_P (instantiation))
   28877              :                 {
   28878            0 :                   instantiate_class_template (instantiation);
   28879            0 :                   if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
   28880            0 :                     for (tree fld = TYPE_FIELDS (instantiation);
   28881            0 :                          fld; fld = TREE_CHAIN (fld))
   28882            0 :                       if ((VAR_P (fld)
   28883            0 :                            || (TREE_CODE (fld) == FUNCTION_DECL
   28884            0 :                                && !DECL_ARTIFICIAL (fld)))
   28885            0 :                           && DECL_TEMPLATE_INSTANTIATION (fld))
   28886            0 :                         instantiate_decl (fld,
   28887              :                                           /*defer_ok=*/false,
   28888              :                                           /*expl_inst_class_mem_p=*/false);
   28889              : 
   28890            0 :                   if (COMPLETE_TYPE_P (instantiation))
   28891     18907091 :                     reconsider = 1;
   28892              :                 }
   28893              :               else
   28894              :                 {
   28895     22906567 :                   instantiation
   28896     22906567 :                     = instantiate_decl (instantiation,
   28897              :                                         /*defer_ok=*/false,
   28898              :                                         /*expl_inst_class_mem_p=*/false);
   28899     22906558 :                   if (DECL_TEMPLATE_INSTANTIATED (instantiation))
   28900     18907091 :                     reconsider = 1;
   28901              :                 }
   28902              : 
   28903     22907646 :               complete = tinst_complete_p (tinst);
   28904              : 
   28905     22907646 :               tinst_depth = 0;
   28906     22907646 :               set_refcount_ptr (current_tinst_level);
   28907              :             }
   28908              : 
   28909     22907646 :           if (complete)
   28910              :             {
   28911              :               /* If INSTANTIATION has been instantiated, then we don't
   28912              :                  need to consider it again in the future.  */
   28913     24543005 :               struct pending_template *drop = *t;
   28914     24543005 :               *t = (*t)->next;
   28915     24543005 :               set_refcount_ptr (drop->tinst);
   28916     24543005 :               pending_template_freelist ().free (drop);
   28917              :             }
   28918              :           else
   28919              :             {
   28920      4000555 :               last = *t;
   28921      4000555 :               t = &(*t)->next;
   28922              :             }
   28923              :         }
   28924       187556 :       last_pending_template = last;
   28925              :     }
   28926       187556 :   while (reconsider);
   28927              : 
   28928       141981 :   input_location = saved_loc;
   28929       141981 :   module_kind = saved_module_kind;
   28930              : }
   28931              : 
   28932              : /* Substitute ARGVEC into T, which is a list of initializers for
   28933              :    either base class or a non-static data member.  The TREE_PURPOSEs
   28934              :    are DECLs, and the TREE_VALUEs are the initializer values.  Used by
   28935              :    instantiate_decl.  */
   28936              : 
   28937              : static tree
   28938      3511926 : tsubst_initializer_list (tree t, tree argvec)
   28939              : {
   28940      3511926 :   tree inits = NULL_TREE;
   28941      3511926 :   tree target_ctor = error_mark_node;
   28942              : 
   28943      7745192 :   for (; t; t = TREE_CHAIN (t))
   28944              :     {
   28945      4233269 :       tree decl;
   28946      4233269 :       tree init;
   28947      4233269 :       tree expanded_bases = NULL_TREE;
   28948      4233269 :       tree expanded_arguments = NULL_TREE;
   28949      4233269 :       int i, len = 1;
   28950              : 
   28951      4233269 :       if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
   28952              :         {
   28953           31 :           tree expr;
   28954           31 :           tree arg;
   28955              : 
   28956              :           /* Expand the base class expansion type into separate base
   28957              :              classes.  */
   28958           31 :           expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
   28959              :                                                  tf_warning_or_error,
   28960              :                                                  NULL_TREE);
   28961           31 :           if (expanded_bases == error_mark_node)
   28962            0 :             continue;
   28963              : 
   28964              :           /* We'll be building separate TREE_LISTs of arguments for
   28965              :              each base.  */
   28966           31 :           len = TREE_VEC_LENGTH (expanded_bases);
   28967           31 :           expanded_arguments = make_tree_vec (len);
   28968          108 :           for (i = 0; i < len; i++)
   28969           46 :             TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
   28970              : 
   28971              :           /* Build a dummy EXPR_PACK_EXPANSION that will be used to
   28972              :              expand each argument in the TREE_VALUE of t.  */
   28973           31 :           expr = make_node (EXPR_PACK_EXPANSION);
   28974           31 :           PACK_EXPANSION_LOCAL_P (expr) = true;
   28975           62 :           PACK_EXPANSION_PARAMETER_PACKS (expr) =
   28976           31 :             PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
   28977              : 
   28978           31 :           if (TREE_VALUE (t) == void_type_node)
   28979              :             /* VOID_TYPE_NODE is used to indicate
   28980              :                value-initialization.  */
   28981              :             {
   28982            9 :               for (i = 0; i < len; i++)
   28983            3 :                 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
   28984              :             }
   28985              :           else
   28986              :             {
   28987              :               /* Substitute parameter packs into each argument in the
   28988              :                  TREE_LIST.  */
   28989           25 :               in_base_initializer = 1;
   28990           59 :               for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
   28991              :                 {
   28992           34 :                   tree expanded_exprs;
   28993              : 
   28994              :                   /* Expand the argument.  */
   28995           34 :                   tree value;
   28996           34 :                   if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   28997              :                     value = TREE_VALUE (arg);
   28998              :                   else
   28999              :                     {
   29000           31 :                       value = expr;
   29001           31 :                       PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
   29002              :                     }
   29003           34 :                   expanded_exprs
   29004           34 :                     = tsubst_pack_expansion (value, argvec,
   29005              :                                              tf_warning_or_error,
   29006              :                                              NULL_TREE);
   29007           34 :                   if (expanded_exprs == error_mark_node)
   29008            3 :                     continue;
   29009              : 
   29010              :                   /* Prepend each of the expanded expressions to the
   29011              :                      corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
   29012           95 :                   for (i = 0; i < len; i++)
   29013           64 :                     if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   29014           36 :                       for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
   29015           27 :                         TREE_VEC_ELT (expanded_arguments, i)
   29016           54 :                           = tree_cons (NULL_TREE,
   29017           27 :                                        TREE_VEC_ELT (expanded_exprs, j),
   29018           27 :                                        TREE_VEC_ELT (expanded_arguments, i));
   29019              :                     else
   29020           55 :                       TREE_VEC_ELT (expanded_arguments, i)
   29021          110 :                         = tree_cons (NULL_TREE,
   29022           55 :                                      TREE_VEC_ELT (expanded_exprs, i),
   29023           55 :                                      TREE_VEC_ELT (expanded_arguments, i));
   29024              :                 }
   29025           25 :               in_base_initializer = 0;
   29026              : 
   29027              :               /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
   29028              :                  since we built them backwards.  */
   29029           68 :               for (i = 0; i < len; i++)
   29030              :                 {
   29031           43 :                   TREE_VEC_ELT (expanded_arguments, i) =
   29032           43 :                     nreverse (TREE_VEC_ELT (expanded_arguments, i));
   29033              :                 }
   29034              :             }
   29035              :         }
   29036              : 
   29037      8466550 :       for (i = 0; i < len; ++i)
   29038              :         {
   29039      4233284 :           if (expanded_bases)
   29040              :             {
   29041           46 :               decl = TREE_VEC_ELT (expanded_bases, i);
   29042           46 :               decl = expand_member_init (decl);
   29043           46 :               init = TREE_VEC_ELT (expanded_arguments, i);
   29044              :             }
   29045              :           else
   29046              :             {
   29047      4233238 :               tree tmp;
   29048      4233238 :               if (TYPE_P (TREE_PURPOSE (t)))
   29049       668907 :                 decl = tsubst (TREE_PURPOSE (t), argvec,
   29050              :                                tf_warning_or_error, NULL_TREE);
   29051              :               else
   29052      3564331 :                 decl = tsubst_expr (TREE_PURPOSE (t), argvec,
   29053              :                                     tf_warning_or_error, NULL_TREE);
   29054              : 
   29055      4233238 :               decl = expand_member_init (decl);
   29056      4233238 :               if (decl && !DECL_P (decl))
   29057       668901 :                 in_base_initializer = 1;
   29058              : 
   29059      4233238 :               init = TREE_VALUE (t);
   29060      4233238 :               tmp = init;
   29061      4233238 :               if (init != void_type_node)
   29062      3713824 :                 init = tsubst_expr (init, argvec,
   29063              :                                     tf_warning_or_error, NULL_TREE);
   29064      4233238 :               if (init == NULL_TREE && tmp != NULL_TREE)
   29065              :                 /* If we had an initializer but it instantiated to nothing,
   29066              :                    value-initialize the object.  This will only occur when
   29067              :                    the initializer was a pack expansion where the parameter
   29068              :                    packs used in that expansion were of length zero.  */
   29069          618 :                 init = void_type_node;
   29070      4233238 :               in_base_initializer = 0;
   29071              :             }
   29072              : 
   29073      4233284 :           if (target_ctor != error_mark_node
   29074            3 :               && init != error_mark_node)
   29075              :             {
   29076            3 :               error ("mem-initializer for %qD follows constructor delegation",
   29077              :                      decl);
   29078            3 :               return inits;
   29079              :             }
   29080              :           /* Look for a target constructor. */
   29081      4233281 :           if (init != error_mark_node
   29082      4233269 :               && decl && CLASS_TYPE_P (decl)
   29083      4240672 :               && same_type_p (decl, current_class_type))
   29084              :             {
   29085         7391 :               maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
   29086         7391 :               if (inits)
   29087              :                 {
   29088            0 :                   error ("constructor delegation follows mem-initializer for %qD",
   29089            0 :                          TREE_PURPOSE (inits));
   29090            0 :                   continue;
   29091              :                 }
   29092              :               target_ctor = init;
   29093              :             }
   29094              : 
   29095      4233281 :           if (decl)
   29096              :             {
   29097      4233275 :               init = build_tree_list (decl, init);
   29098              :               /* Carry over the dummy TREE_TYPE node containing the source
   29099              :                  location.  */
   29100      4233275 :               TREE_TYPE (init) = TREE_TYPE (t);
   29101      4233275 :               TREE_CHAIN (init) = inits;
   29102      4233275 :               inits = init;
   29103              :             }
   29104              :         }
   29105              :     }
   29106              :   return inits;
   29107              : }
   29108              : 
   29109              : /* Instantiate an enumerated type.  TAG is the template type, NEWTAG
   29110              :    is the instantiation (which should have been created with
   29111              :    start_enum) and ARGS are the template arguments to use.  */
   29112              : 
   29113              : static void
   29114       430034 : tsubst_enum (tree tag, tree newtag, tree args)
   29115              : {
   29116       430034 :   tree e;
   29117              : 
   29118       430034 :   if (SCOPED_ENUM_P (newtag))
   29119          376 :     begin_scope (sk_scoped_enum, newtag);
   29120              : 
   29121       430034 :   ENUM_BEING_DEFINED_P (newtag) = 1;
   29122              : 
   29123       908037 :   for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
   29124              :     {
   29125       478048 :       tree value;
   29126       478048 :       tree decl = TREE_VALUE (e);
   29127              : 
   29128              :       /* Note that in a template enum, the TREE_VALUE is the
   29129              :          CONST_DECL, not the corresponding INTEGER_CST.  */
   29130       478048 :       value = tsubst_expr (DECL_INITIAL (decl),
   29131              :                            args, tf_warning_or_error, NULL_TREE);
   29132              : 
   29133              :       /* Give this enumeration constant the correct access.  */
   29134       478003 :       set_current_access_from_decl (decl);
   29135              : 
   29136              :       /* Actually build the enumerator itself.  Here we're assuming that
   29137              :          enumerators can't have dependent attributes.  */
   29138       478003 :       tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
   29139       478003 :                                        DECL_ATTRIBUTES (decl),
   29140       478003 :                                        DECL_SOURCE_LOCATION (decl));
   29141              :       /* Attribute deprecated without an argument isn't sticky: it'll
   29142              :          melt into a tree flag, so we need to propagate the flag here,
   29143              :          since we just created a new enumerator.  */
   29144       478003 :       TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
   29145       478003 :       TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
   29146              :     }
   29147              : 
   29148       429989 :   if (SCOPED_ENUM_P (newtag))
   29149          376 :     finish_scope ();
   29150              : 
   29151       429989 :   finish_enum_value_list (newtag);
   29152       429989 :   finish_enum (newtag);
   29153              : 
   29154       429989 :   DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
   29155       429989 :     = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
   29156       429989 :   TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
   29157       429989 :   TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
   29158       429989 : }
   29159              : 
   29160              : /* DECL is a FUNCTION_DECL that is a template specialization.  Return
   29161              :    its type -- but without substituting the innermost set of template
   29162              :    arguments.  So, innermost set of template parameters will appear in
   29163              :    the type.  */
   29164              : 
   29165              : tree
   29166     20714930 : get_mostly_instantiated_function_type (tree decl)
   29167              : {
   29168              :   /* For a function, DECL_TI_TEMPLATE is partially instantiated.  */
   29169     20714930 :   return TREE_TYPE (DECL_TI_TEMPLATE (decl));
   29170              : }
   29171              : 
   29172              : /* Return truthvalue if we're processing a template different from
   29173              :    the last one involved in diagnostics.  */
   29174              : bool
   29175       225522 : problematic_instantiation_changed (void)
   29176              : {
   29177       225522 :   return current_tinst_level != last_error_tinst_level;
   29178              : }
   29179              : 
   29180              : /* Remember current template involved in diagnostics.  */
   29181              : void
   29182         5337 : record_last_problematic_instantiation (void)
   29183              : {
   29184         5337 :   set_refcount_ptr (last_error_tinst_level, current_tinst_level);
   29185         5337 : }
   29186              : 
   29187              : struct tinst_level *
   29188    242829091 : current_instantiation (void)
   29189              : {
   29190    242829091 :   return current_tinst_level;
   29191              : }
   29192              : 
   29193              : /* Return TRUE if current_function_decl is being instantiated, false
   29194              :    otherwise.  */
   29195              : 
   29196              : bool
   29197    237585433 : instantiating_current_function_p (void)
   29198              : {
   29199    237585433 :   return (current_instantiation ()
   29200    237585433 :           && (current_instantiation ()->maybe_get_node ()
   29201      3994358 :               == current_function_decl));
   29202              : }
   29203              : 
   29204              : /* [temp.param] Check that template non-type parm TYPE is of an allowable
   29205              :    type.  Return false for ok, true for disallowed.  Issue error and
   29206              :    inform messages under control of COMPLAIN.  */
   29207              : 
   29208              : static bool
   29209    202178743 : invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
   29210              : {
   29211    202178743 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
   29212              :     return false;
   29213      2447236 :   else if (TYPE_PTR_P (type))
   29214              :     return false;
   29215      2444032 :   else if (TYPE_REF_P (type)
   29216      2444032 :            && !TYPE_REF_IS_RVALUE (type))
   29217              :     return false;
   29218      2442953 :   else if (TYPE_PTRMEM_P (type))
   29219              :     return false;
   29220      2439257 :   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
   29221              :     {
   29222      2284067 :       if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
   29223              :         {
   29224            1 :           if (complain & tf_error)
   29225            1 :             error ("non-type template parameters of deduced class type only "
   29226              :                    "available with %<-std=c++20%> or %<-std=gnu++20%>");
   29227            1 :           return true;
   29228              :         }
   29229              :       return false;
   29230              :     }
   29231       155190 :   else if (TREE_CODE (type) == NULLPTR_TYPE)
   29232              :     return false;
   29233       155141 :   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
   29234           18 :            && cxx_dialect < cxx11)
   29235              :     /* Fall through; before C++11 alias templates, a bound ttp
   29236              :        always instantiates into a class type.  */;
   29237       155141 :   else if (WILDCARD_TYPE_P (type))
   29238              :     /* Any other wildcard type not already handled above is allowed.  */
   29239              :     return false;
   29240              :   else if (TREE_CODE (type) == COMPLEX_TYPE)
   29241              :     /* Fall through.  */;
   29242              :   else if (VOID_TYPE_P (type))
   29243              :     /* Fall through.  */;
   29244        34087 :   else if (cxx_dialect >= cxx20)
   29245              :     {
   29246        34034 :       if (dependent_type_p (type))
   29247              :         return false;
   29248        33957 :       if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
   29249              :         return true;
   29250        33924 :       if (structural_type_p (type))
   29251              :         return false;
   29252           50 :       if (complain & tf_error)
   29253              :         {
   29254           11 :           auto_diagnostic_group d;
   29255           11 :           error ("%qT is not a valid type for a template non-type "
   29256              :                  "parameter because it is not structural", type);
   29257           11 :           structural_type_p (type, true);
   29258           11 :         }
   29259           50 :       return true;
   29260              :     }
   29261           53 :   else if (CLASS_TYPE_P (type))
   29262              :     {
   29263            9 :       if (complain & tf_error)
   29264            7 :         error ("non-type template parameters of class type only available "
   29265              :                "with %<-std=c++20%> or %<-std=gnu++20%>");
   29266            9 :       return true;
   29267              :     }
   29268              : 
   29269          133 :   if (complain & tf_error)
   29270              :     {
   29271          128 :       if (type == error_mark_node)
   29272           11 :         inform (input_location, "invalid template non-type parameter");
   29273              :       else
   29274          117 :         error ("%q#T is not a valid type for a template non-type parameter",
   29275              :                type);
   29276              :     }
   29277              :   return true;
   29278              : }
   29279              : 
   29280              : /* Returns true iff the noexcept-specifier for TYPE is value-dependent.  */
   29281              : 
   29282              : static bool
   29283     18642461 : value_dependent_noexcept_spec_p (tree type)
   29284              : {
   29285     18642461 :   if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
   29286      7519443 :     if (tree noex = TREE_PURPOSE (spec))
   29287              :       /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
   29288              :          affect overload resolution and treating it as dependent breaks
   29289              :          things.  Same for an unparsed noexcept expression.  */
   29290      7519134 :       if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   29291      7519134 :           && TREE_CODE (noex) != DEFERRED_PARSE
   29292      7519134 :           && value_dependent_expression_p (noex))
   29293              :         return true;
   29294              : 
   29295              :   return false;
   29296              : }
   29297              : 
   29298              : /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
   29299              :    Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
   29300              : 
   29301              : static bool
   29302    584277025 : dependent_type_p_r (tree type)
   29303              : {
   29304    584277025 :   tree scope;
   29305              : 
   29306              :   /* [temp.dep.type]
   29307              : 
   29308              :      A type is dependent if it is:
   29309              : 
   29310              :      -- a template parameter. Template template parameters are types
   29311              :         for us (since TYPE_P holds true for them) so we handle
   29312              :         them here.  */
   29313    584277025 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   29314    584277025 :       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
   29315              :     return true;
   29316              :   /* -- a qualified-id with a nested-name-specifier which contains a
   29317              :         class-name that names a dependent type or whose unqualified-id
   29318              :         names a dependent type.  */
   29319    463934252 :   if (TREE_CODE (type) == TYPENAME_TYPE)
   29320              :     return true;
   29321              : 
   29322              :   /* -- a cv-qualified type where the cv-unqualified type is
   29323              :         dependent.
   29324              :      No code is necessary for this bullet; the code below handles
   29325              :      cv-qualified types, and we don't want to strip aliases with
   29326              :      TYPE_MAIN_VARIANT because of DR 1558.  */
   29327              :   /* -- a compound type constructed from any dependent type.  */
   29328    434982867 :   if (TYPE_PTRMEM_P (type))
   29329       918241 :     return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
   29330       918241 :             || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
   29331              :                                            (type)));
   29332    434064626 :   else if (INDIRECT_TYPE_P (type))
   29333     86220449 :     return dependent_type_p (TREE_TYPE (type));
   29334    347844177 :   else if (FUNC_OR_METHOD_TYPE_P (type))
   29335              :     {
   29336     90637701 :       tree arg_type;
   29337              : 
   29338     90637701 :       if (dependent_type_p (TREE_TYPE (type)))
   29339              :         return true;
   29340     53983032 :       for (arg_type = TYPE_ARG_TYPES (type);
   29341    109107948 :            arg_type;
   29342     55124916 :            arg_type = TREE_CHAIN (arg_type))
   29343     90404090 :         if (dependent_type_p (TREE_VALUE (arg_type)))
   29344              :           return true;
   29345     18703858 :       if (cxx_dialect >= cxx17
   29346     18703858 :           && value_dependent_noexcept_spec_p (type))
   29347              :         /* A value-dependent noexcept-specifier makes the type dependent.  */
   29348              :         return true;
   29349     18703819 :       return false;
   29350              :     }
   29351              :   /* -- an array type constructed from any dependent type or whose
   29352              :         size is specified by a constant expression that is
   29353              :         value-dependent.
   29354              : 
   29355              :         We checked for type- and value-dependence of the bounds in
   29356              :         compute_array_index_type, so TYPE_DEPENDENT_P is already set.  */
   29357    257206476 :   if (TREE_CODE (type) == ARRAY_TYPE)
   29358              :     {
   29359        11776 :       if (TYPE_DOMAIN (type)
   29360        11776 :           && dependent_type_p (TYPE_DOMAIN (type)))
   29361              :         return true;
   29362        11776 :       return dependent_type_p (TREE_TYPE (type));
   29363              :     }
   29364              : 
   29365              :   /* -- a template-id in which either the template name is a template
   29366              :      parameter ...  */
   29367    257194700 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   29368              :     return true;
   29369              :   /* ... or any of the template arguments is a dependent type or
   29370              :         an expression that is type-dependent or value-dependent.  */
   29371    131622913 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   29372    381183850 :            && (any_dependent_template_arguments_p
   29373    124226862 :                (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
   29374              :     return true;
   29375              : 
   29376              :   /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
   29377              :      dependent; if the argument of the `typeof' expression is not
   29378              :      type-dependent, then it should already been have resolved.  */
   29379    184354693 :   if (TREE_CODE (type) == TYPEOF_TYPE
   29380    163641708 :       || TREE_CODE (type) == DECLTYPE_TYPE
   29381    156961597 :       || TREE_CODE (type) == TRAIT_TYPE)
   29382              :     return true;
   29383              : 
   29384              :   /* A template argument pack is dependent if any of its packed
   29385              :      arguments are.  */
   29386    156705004 :   if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
   29387              :     {
   29388          223 :       tree args = ARGUMENT_PACK_ARGS (type);
   29389          271 :       for (tree arg : tree_vec_range (args))
   29390          214 :         if (dependent_template_arg_p (arg))
   29391          166 :           return true;
   29392              :     }
   29393              : 
   29394              :   /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
   29395              :      be template parameters.  This includes pack-index-specifiers.  */
   29396    156704838 :   if (TREE_CODE (type) == TYPE_PACK_EXPANSION
   29397    144308667 :       || TREE_CODE (type) == PACK_INDEX_TYPE)
   29398              :     return true;
   29399              : 
   29400    144308571 :   if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
   29401              :     return true;
   29402              : 
   29403              :   /* A splice-scope-specifier is dependent if its splice-specifier
   29404              :      or splice-specialization-specifier is dependent.  */
   29405     62582787 :   if (TREE_CODE (type) == SPLICE_SCOPE)
   29406              :     return true;
   29407              : 
   29408     62582654 :   if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
   29409              :     return true;
   29410              : 
   29411              :   /* The standard does not specifically mention types that are local
   29412              :      to template functions or local classes, but they should be
   29413              :      considered dependent too.  For example:
   29414              : 
   29415              :        template <int I> void f() {
   29416              :          enum E { a = I };
   29417              :          S<sizeof (E)> s;
   29418              :        }
   29419              : 
   29420              :      The size of `E' cannot be known until the value of `I' has been
   29421              :      determined.  Therefore, `E' must be considered dependent.  */
   29422     62582654 :   scope = TYPE_CONTEXT (type);
   29423     62582654 :   if (scope && TYPE_P (scope))
   29424      3208991 :     return dependent_type_p (scope);
   29425              :   /* Don't use type_dependent_expression_p here, as it can lead
   29426              :      to infinite recursion trying to determine whether a lambda
   29427              :      nested in a lambda is dependent (c++/47687).  */
   29428     56769270 :   else if (scope && TREE_CODE (scope) == FUNCTION_DECL
   29429      2000225 :            && DECL_LANG_SPECIFIC (scope)
   29430      2000225 :            && DECL_TEMPLATE_INFO (scope)
   29431      1725322 :            && (any_dependent_template_arguments_p
   29432      1725322 :                (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
   29433              :     return true;
   29434              : 
   29435              :   /* Other types are non-dependent.  */
   29436              :   return false;
   29437              : }
   29438              : 
   29439              : /* Returns TRUE if TYPE is dependent, in the sense of
   29440              :    [temp.dep.type].  Note that a NULL type is considered dependent.  */
   29441              : 
   29442              : bool
   29443  21479080598 : dependent_type_p (tree type)
   29444              : {
   29445              :   /* If there are no template parameters in scope, then there can't be
   29446              :      any dependent types.  */
   29447  21479080598 :   if (!processing_template_decl)
   29448              :     {
   29449              :       /* If we are not processing a template, then nobody should be
   29450              :          providing us with a dependent type.  */
   29451   4399984801 :       gcc_assert (type);
   29452   4399984801 :       gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)
   29453              :                   || seen_error ());
   29454   4399984801 :       return false;
   29455              :     }
   29456              : 
   29457              :   /* If the type is NULL, we have not computed a type for the entity
   29458              :      in question; in that case, the type is dependent.  */
   29459  17079095797 :   if (!type)
   29460              :     return true;
   29461              : 
   29462              :   /* Erroneous types can be considered non-dependent.  */
   29463  16796537750 :   if (type == error_mark_node)
   29464              :     return false;
   29465              : 
   29466              :   /* If we have not already computed the appropriate value for TYPE,
   29467              :      do so now.  */
   29468  16796537619 :   if (!TYPE_DEPENDENT_P_VALID (type))
   29469              :     {
   29470    584277025 :       TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
   29471    584277025 :       TYPE_DEPENDENT_P_VALID (type) = 1;
   29472              :     }
   29473              : 
   29474  16796537619 :   return TYPE_DEPENDENT_P (type);
   29475              : }
   29476              : 
   29477              : /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
   29478              :    lookup.  In other words, a dependent type that is not the current
   29479              :    instantiation.  */
   29480              : 
   29481              : bool
   29482   1458172347 : dependent_scope_p (tree scope)
   29483              : {
   29484   1458172347 :   return (scope && TYPE_P (scope) && dependent_type_p (scope)
   29485   2279417060 :           && !currently_open_class (scope));
   29486              : }
   29487              : 
   29488              : /* True if we might find more declarations in SCOPE during instantiation than
   29489              :    we can when parsing the template.  */
   29490              : 
   29491              : bool
   29492    444225314 : dependentish_scope_p (tree scope)
   29493              : {
   29494    444225314 :   return dependent_scope_p (scope) || any_dependent_bases_p (scope);
   29495              : }
   29496              : 
   29497              : /* Returns TRUE if NS is a dependent namespace, in which we can't do any
   29498              :    lookup.  */
   29499              : 
   29500              : bool
   29501    636998803 : dependent_namespace_p (tree ns)
   29502              : {
   29503    636998803 :   if (TREE_CODE (ns) == NAMESPACE_DECL)
   29504    491146232 :     ns = ORIGINAL_NAMESPACE (ns);
   29505    636998803 :   return TREE_CODE (ns) == SPLICE_EXPR;
   29506              : }
   29507              : 
   29508              : /* T is a SCOPE_REF.  Return whether it represents a non-static member of
   29509              :    an unknown base of 'this' (and is therefore instantiation-dependent).  */
   29510              : 
   29511              : static bool
   29512      4242512 : unknown_base_ref_p (tree t)
   29513              : {
   29514      4242512 :   if (!current_class_ptr)
   29515              :     return false;
   29516              : 
   29517      1826600 :   tree mem = TREE_OPERAND (t, 1);
   29518      1826600 :   if (shared_member_p (mem))
   29519              :     return false;
   29520              : 
   29521            7 :   tree cur = current_nonlambda_class_type ();
   29522            7 :   if (!any_dependent_bases_p (cur))
   29523              :     return false;
   29524              : 
   29525            0 :   tree ctx = TREE_OPERAND (t, 0);
   29526            0 :   if (DERIVED_FROM_P (ctx, cur))
   29527              :     return false;
   29528              : 
   29529              :   return true;
   29530              : }
   29531              : 
   29532              : /* T is a SCOPE_REF; return whether we need to consider it
   29533              :     instantiation-dependent so that we can check access at instantiation
   29534              :     time even though we know which member it resolves to.  */
   29535              : 
   29536              : static bool
   29537      9028897 : instantiation_dependent_scope_ref_p (tree t)
   29538              : {
   29539      9028897 :   if (DECL_P (TREE_OPERAND (t, 1))
   29540      4242524 :       && CLASS_TYPE_P (TREE_OPERAND (t, 0))
   29541      4242524 :       && !dependent_scope_p (TREE_OPERAND (t, 0))
   29542      4242512 :       && !unknown_base_ref_p (t)
   29543     13271409 :       && accessible_in_template_p (TREE_OPERAND (t, 0),
   29544      4242512 :                                    TREE_OPERAND (t, 1)))
   29545              :     return false;
   29546              :   else
   29547      4786607 :     return true;
   29548              : }
   29549              : 
   29550              : /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
   29551              :    [temp.dep.constexpr].  EXPRESSION is already known to be a constant
   29552              :    expression.  */
   29553              : 
   29554              : /* Note that this predicate is not appropriate for general expressions;
   29555              :    only constant expressions (that satisfy potential_constant_expression)
   29556              :    can be tested for value dependence.  */
   29557              : 
   29558              : bool
   29559   1590321682 : value_dependent_expression_p (tree expression)
   29560              : {
   29561   1620099584 :   if (!processing_template_decl || expression == NULL_TREE)
   29562              :     return false;
   29563              : 
   29564              :   /* A type-dependent expression is also value-dependent.  */
   29565   1248440403 :   if (type_dependent_expression_p (expression))
   29566              :     return true;
   29567              : 
   29568   1081596825 :   switch (TREE_CODE (expression))
   29569              :     {
   29570      1728543 :     case BASELINK:
   29571              :       /* A dependent member function of the current instantiation.  */
   29572      1728543 :       return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
   29573              : 
   29574     35135662 :     case FUNCTION_DECL:
   29575              :       /* A dependent member function of the current instantiation.  */
   29576     70270574 :       if (DECL_CLASS_SCOPE_P (expression)
   29577     59811112 :           && dependent_type_p (DECL_CONTEXT (expression)))
   29578              :         return true;
   29579              :       break;
   29580              : 
   29581              :     case IDENTIFIER_NODE:
   29582              :       /* A name that has not been looked up -- must be dependent.  */
   29583              :       return true;
   29584              : 
   29585              :     case TEMPLATE_PARM_INDEX:
   29586              :       /* A non-type template parm.  */
   29587              :       return true;
   29588              : 
   29589      2042175 :     case CONST_DECL:
   29590              :       /* A non-type template parm.  */
   29591      2042175 :       if (DECL_TEMPLATE_PARM_P (expression))
   29592              :         return true;
   29593      2042175 :       return value_dependent_expression_p (DECL_INITIAL (expression));
   29594              : 
   29595     28734311 :     case VAR_DECL:
   29596              :        /* A constant with literal type and is initialized
   29597              :           with an expression that is value-dependent.  */
   29598     28734311 :       if (DECL_DEPENDENT_INIT_P (expression))
   29599              :         return true;
   29600     19250079 :       if (DECL_HAS_VALUE_EXPR_P (expression))
   29601              :         {
   29602       426778 :           tree value_expr = DECL_VALUE_EXPR (expression);
   29603       426778 :           if (value_dependent_expression_p (value_expr)
   29604              :               /* __PRETTY_FUNCTION__ inside a template function is dependent
   29605              :                  on the name of the function.  */
   29606       426778 :               || (DECL_PRETTY_FUNCTION_P (expression)
   29607              :                   /* It might be used in a template, but not a template
   29608              :                      function, in which case its DECL_VALUE_EXPR will be
   29609              :                      "top level".  */
   29610            5 :                   && value_expr == error_mark_node))
   29611              :             return true;
   29612              :         }
   29613     18823301 :       else if (TYPE_REF_P (TREE_TYPE (expression)))
   29614              :         /* FIXME cp_finish_decl doesn't fold reference initializers.  */
   29615              :         return true;
   29616              :       /* We have a constexpr variable and we're processing a template.  When
   29617              :          there's lifetime extension involved (for which finish_compound_literal
   29618              :          used to create a temporary), we'll not be able to evaluate the
   29619              :          variable until instantiating, so pretend it's value-dependent.  */
   29620     18811367 :       else if (DECL_DECLARED_CONSTEXPR_P (expression)
   29621     18811367 :                && !TREE_CONSTANT (expression))
   29622              :         return true;
   29623              :       return false;
   29624              : 
   29625     31697636 :     case DYNAMIC_CAST_EXPR:
   29626     31697636 :     case STATIC_CAST_EXPR:
   29627     31697636 :     case CONST_CAST_EXPR:
   29628     31697636 :     case REINTERPRET_CAST_EXPR:
   29629     31697636 :     case CAST_EXPR:
   29630     31697636 :     case IMPLICIT_CONV_EXPR:
   29631              :       /* These expressions are value-dependent if the type to which
   29632              :          the cast occurs is dependent or the expression being casted
   29633              :          is value-dependent.  */
   29634     31697636 :       {
   29635     31697636 :         tree type = TREE_TYPE (expression);
   29636              : 
   29637     31697636 :         if (dependent_type_p (type))
   29638              :           return true;
   29639              : 
   29640              :         /* A functional cast has a list of operands.  */
   29641     31697636 :         expression = TREE_OPERAND (expression, 0);
   29642     31697636 :         if (!expression)
   29643              :           {
   29644              :             /* If there are no operands, it must be an expression such
   29645              :                as "int()". This should not happen for aggregate types
   29646              :                because it would form non-constant expressions.  */
   29647          137 :             gcc_assert (cxx_dialect >= cxx11
   29648              :                         || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
   29649              : 
   29650              :             return false;
   29651              :           }
   29652              : 
   29653     31697499 :         if (TREE_CODE (expression) == TREE_LIST)
   29654      3961746 :           return any_value_dependent_elements_p (expression);
   29655              : 
   29656     27735753 :         if (TREE_CODE (type) == REFERENCE_TYPE
   29657     27735753 :             && has_value_dependent_address (expression))
   29658              :           return true;
   29659              : 
   29660     27735727 :         return value_dependent_expression_p (expression);
   29661              :       }
   29662              : 
   29663     10470037 :     case SIZEOF_EXPR:
   29664     10470037 :       if (SIZEOF_EXPR_TYPE_P (expression))
   29665            4 :         return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
   29666     10470033 :       if (tree p = TREE_OPERAND (expression, 0))
   29667     10470033 :         if (PACK_EXPANSION_P (p)
   29668     10470033 :             && DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (p)))
   29669              :           {
   29670          795 :             tree d = PACK_EXPANSION_PATTERN (p);
   29671          795 :             if (DECL_HAS_VALUE_EXPR_P (d))
   29672              :               {
   29673          795 :                 d = DECL_VALUE_EXPR (d);
   29674              :                 /* [temp.dep.constexpr]/4:
   29675              :                    Expressions of the following form are value-dependent:
   29676              :                    sizeof ... ( identifier )
   29677              :                    unless the identifier is a structured binding pack whose
   29678              :                    initializer is not dependent.  */
   29679          795 :                 if (TREE_CODE (d) == TREE_VEC
   29680          795 :                     && !type_dependent_expression_p (TREE_VEC_ELT (d, 0)))
   29681              :                   return false;
   29682              :               }
   29683              :           }
   29684              :       /* FALLTHRU */
   29685     11207765 :     case ALIGNOF_EXPR:
   29686     11207765 :     case TYPEID_EXPR:
   29687              :       /* A `sizeof' expression is value-dependent if the operand is
   29688              :          type-dependent or is a pack expansion.  */
   29689     11207765 :       expression = TREE_OPERAND (expression, 0);
   29690     11207765 :       if (PACK_EXPANSION_P (expression))
   29691              :         return true;
   29692      6192610 :       else if (TYPE_P (expression))
   29693      5920561 :         return dependent_type_p (expression);
   29694       272049 :       return instantiation_dependent_uneval_expression_p (expression);
   29695              : 
   29696            0 :     case AT_ENCODE_EXPR:
   29697              :       /* An 'encode' expression is value-dependent if the operand is
   29698              :          type-dependent.  */
   29699            0 :       expression = TREE_OPERAND (expression, 0);
   29700            0 :       return dependent_type_p (expression);
   29701              : 
   29702       591343 :     case NOEXCEPT_EXPR:
   29703       591343 :       expression = TREE_OPERAND (expression, 0);
   29704       591343 :       return instantiation_dependent_uneval_expression_p (expression);
   29705              : 
   29706      2959962 :     case SCOPE_REF:
   29707              :       /* All instantiation-dependent expressions should also be considered
   29708              :          value-dependent.  */
   29709      2959962 :       return instantiation_dependent_scope_ref_p (expression);
   29710              : 
   29711      4520090 :     case COMPONENT_REF:
   29712      4520090 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   29713      4520090 :               || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
   29714              : 
   29715            0 :     case NONTYPE_ARGUMENT_PACK:
   29716              :       /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
   29717              :          is value-dependent.  */
   29718            0 :       for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
   29719            0 :         if (value_dependent_expression_p (arg))
   29720            0 :           return true;
   29721            0 :       return false;
   29722              : 
   29723     19293638 :     case TRAIT_EXPR:
   29724     19293638 :       {
   29725     19293638 :         if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
   29726              :           return true;
   29727              : 
   29728        45403 :         tree type2 = TRAIT_EXPR_TYPE2 (expression);
   29729        45403 :         if (!type2)
   29730              :           return false;
   29731              : 
   29732        45399 :         if (TREE_CODE (type2) != TREE_VEC)
   29733        45355 :           return dependent_type_p (type2);
   29734              : 
   29735           56 :         for (tree arg : tree_vec_range (type2))
   29736           41 :           if (dependent_type_p (arg))
   29737           29 :             return true;
   29738              : 
   29739           15 :         return false;
   29740              :       }
   29741              : 
   29742           30 :     case MODOP_EXPR:
   29743           30 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   29744           30 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
   29745              : 
   29746       980938 :     case ARRAY_REF:
   29747       980938 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   29748       980938 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
   29749              : 
   29750       246332 :     case ADDR_EXPR:
   29751       246332 :       {
   29752       246332 :         tree op = TREE_OPERAND (expression, 0);
   29753       246332 :         return (value_dependent_expression_p (op)
   29754       246332 :                 || has_value_dependent_address (op));
   29755              :       }
   29756              : 
   29757              :     case REQUIRES_EXPR:
   29758              :       /* Treat all requires-expressions as value-dependent so
   29759              :          we don't try to fold them.  */
   29760              :       return true;
   29761              : 
   29762            0 :     case TYPE_REQ:
   29763            0 :       return dependent_type_p (TREE_OPERAND (expression, 0));
   29764              : 
   29765      5994117 :     case CALL_EXPR:
   29766      5994117 :       {
   29767      5994117 :         if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
   29768              :           return true;
   29769      5648547 :         tree fn = get_callee_fndecl (expression);
   29770      5648547 :         int i, nargs;
   29771      5648547 :         nargs = call_expr_nargs (expression);
   29772      7313941 :         for (i = 0; i < nargs; ++i)
   29773              :           {
   29774      2438283 :             tree op = CALL_EXPR_ARG (expression, i);
   29775              :             /* In a call to a constexpr member function, look through the
   29776              :                implicit ADDR_EXPR on the object argument so that it doesn't
   29777              :                cause the call to be considered value-dependent.  We also
   29778              :                look through it in potential_constant_expression.  */
   29779        22003 :             if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
   29780        22003 :                 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   29781      2438285 :                 && TREE_CODE (op) == ADDR_EXPR)
   29782            1 :               op = TREE_OPERAND (op, 0);
   29783      2438283 :             if (value_dependent_expression_p (op))
   29784              :               return true;
   29785              :           }
   29786      4905338 :         if (flag_reflection && !fn && CALL_EXPR_FN (expression))
   29787              :           {
   29788        29680 :             fn = MAYBE_BASELINK_FUNCTIONS (CALL_EXPR_FN (expression));
   29789        29680 :             if (fn && TREE_CODE (fn) != FUNCTION_DECL)
   29790         3226 :               fn = NULL_TREE;
   29791              :           }
   29792              :         /* [meta.reflection.access.context]/8: An invocation of current that
   29793              :            appears at a program point P is value-dependent if eval-point(P)
   29794              :            is enclosed by a scope corresponding to a templated entity.  */
   29795      4875658 :         if (flag_reflection
   29796        30261 :             && fn
   29797        27035 :             && metafunction_p (fn)
   29798         1660 :             && id_equal (DECL_NAME (fn), "current")
   29799           16 :             && DECL_CLASS_SCOPE_P (fn)
   29800      4875674 :             && id_equal (TYPE_IDENTIFIER (DECL_CONTEXT (fn)),
   29801              :                          "access_context"))
   29802              :           return true;
   29803              : 
   29804              :         return false;
   29805              :       }
   29806              : 
   29807      9808258 :     case TEMPLATE_ID_EXPR:
   29808      9808258 :       return concept_definition_p (TREE_OPERAND (expression, 0))
   29809      9808258 :         && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
   29810              : 
   29811      3522153 :     case CONSTRUCTOR:
   29812      3522153 :       {
   29813      3522153 :         unsigned ix;
   29814      3522153 :         tree val;
   29815      3522153 :         if (dependent_type_p (TREE_TYPE (expression)))
   29816              :           return true;
   29817      3673655 :         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
   29818       152188 :           if (value_dependent_expression_p (val))
   29819              :             return true;
   29820              :         return false;
   29821              :       }
   29822              : 
   29823              :     case STMT_EXPR:
   29824              :       /* Treat a GNU statement expression as dependent to avoid crashing
   29825              :          under instantiate_non_dependent_expr; it can't be constant.  */
   29826              :       return true;
   29827              : 
   29828            8 :     case NEW_EXPR:
   29829            8 :     case VEC_NEW_EXPR:
   29830              :       /* The second operand is a type, which type_dependent_expression_p
   29831              :          (and therefore value_dependent_expression_p) doesn't want to see.  */
   29832            8 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   29833            8 :               || value_dependent_expression_p (TREE_OPERAND (expression, 2))
   29834           16 :               || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
   29835              : 
   29836         7296 :     case REFLECT_EXPR:
   29837              :       /* [temp.dep.constexpr] A reflect-expression is value-dependent
   29838              :          if it contains a dependent nested-name-specifier, type-id,
   29839              :          namespace-name, or template-name, or if it contains
   29840              :          a value-dependent or type-dependent id-expression.  */
   29841         7296 :       if (REFLECT_EXPR_KIND (expression) == REFLECT_BASE)
   29842              :         /* Direct base relationship isn't value-dependent and calling
   29843              :            uses_template_parms on TREE_BINFO leads to ICEs.  */
   29844              :         return false;
   29845         7221 :       if (REFLECT_EXPR_KIND (expression) == REFLECT_DATA_MEMBER_SPEC)
   29846              :         {
   29847              :           /* Data member description is value dependent if the type is
   29848              :              dependent, other optional fields shouldn't be ever dependent.  */
   29849          128 :           tree h = REFLECT_EXPR_HANDLE (expression);
   29850          128 :           return dependent_type_p (TREE_VEC_ELT (h, 0));
   29851              :         }
   29852         7093 :       return uses_template_parms (REFLECT_EXPR_HANDLE (expression));
   29853              : 
   29854    808007313 :     default:
   29855              :       /* A constant expression is value-dependent if any subexpression is
   29856              :          value-dependent.  */
   29857    808007313 :       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
   29858              :         {
   29859     90170790 :         case tcc_reference:
   29860     90170790 :         case tcc_unary:
   29861     90170790 :         case tcc_comparison:
   29862     90170790 :         case tcc_binary:
   29863     90170790 :         case tcc_expression:
   29864     90170790 :         case tcc_vl_exp:
   29865     90170790 :           {
   29866     90170790 :             int i, len = cp_tree_operand_length (expression);
   29867              : 
   29868    160108290 :             for (i = 0; i < len; i++)
   29869              :               {
   29870    102422992 :                 tree t = TREE_OPERAND (expression, i);
   29871              : 
   29872              :                 /* In some cases, some of the operands may be missing.
   29873              :                    (For example, in the case of PREDECREMENT_EXPR, the
   29874              :                    amount to increment by may be missing.)  That doesn't
   29875              :                    make the expression dependent.  */
   29876    102422992 :                 if (t && value_dependent_expression_p (t))
   29877              :                   return true;
   29878              :               }
   29879              :           }
   29880              :           break;
   29881              :         default:
   29882              :           break;
   29883              :         }
   29884              :       break;
   29885              :     }
   29886              : 
   29887              :   /* The expression is not value-dependent.  */
   29888              :   return false;
   29889              : }
   29890              : 
   29891              : /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
   29892              :    [temp.dep.expr].  Note that an expression with no type is
   29893              :    considered dependent.  Other parts of the compiler arrange for an
   29894              :    expression with type-dependent subexpressions to have no type, so
   29895              :    this function doesn't have to be fully recursive.  */
   29896              : 
   29897              : bool
   29898   4688539917 : type_dependent_expression_p (tree expression)
   29899              : {
   29900   4688543258 :   if (!processing_template_decl)
   29901              :     return false;
   29902              : 
   29903   3849358734 :   if (expression == NULL_TREE || expression == error_mark_node)
   29904              :     return false;
   29905              : 
   29906   3848239415 :   gcc_checking_assert (!TYPE_P (expression));
   29907              : 
   29908   3848239415 :   STRIP_ANY_LOCATION_WRAPPER (expression);
   29909              : 
   29910              :   /* Assume a TU-local entity is not dependent, we'll error later when
   29911              :      instantiating anyway.  */
   29912   3848239415 :   if (TREE_CODE (expression) == TU_LOCAL_ENTITY)
   29913              :     return false;
   29914              : 
   29915              :   /* An unresolved name is always dependent.  */
   29916              :   if (identifier_p (expression)
   29917              :       || TREE_CODE (expression) == USING_DECL)
   29918              :     return true;
   29919              : 
   29920              :   /* A lambda-expression in template context is dependent.  dependent_type_p is
   29921              :      true for a lambda in the scope of a class or function template, but that
   29922              :      doesn't cover all template contexts, like a default template argument.  */
   29923              :   if (TREE_CODE (expression) == LAMBDA_EXPR)
   29924              :     return true;
   29925              : 
   29926              :   /* A fold expression is type-dependent. */
   29927              :   if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
   29928              :       || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
   29929              :       || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
   29930              :       || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
   29931              :     return true;
   29932              : 
   29933              :   /* Some expression forms are never type-dependent.  */
   29934              :   if (TREE_CODE (expression) == SIZEOF_EXPR
   29935              :       || TREE_CODE (expression) == ALIGNOF_EXPR
   29936              :       || TREE_CODE (expression) == AT_ENCODE_EXPR
   29937              :       || TREE_CODE (expression) == NOEXCEPT_EXPR
   29938              :       || TREE_CODE (expression) == TRAIT_EXPR
   29939              :       || TREE_CODE (expression) == TYPEID_EXPR
   29940              :       || TREE_CODE (expression) == DELETE_EXPR
   29941              :       || TREE_CODE (expression) == VEC_DELETE_EXPR
   29942              :       || TREE_CODE (expression) == THROW_EXPR
   29943              :       || TREE_CODE (expression) == REQUIRES_EXPR
   29944              :       || REFLECT_EXPR_P (expression))
   29945              :     return false;
   29946              : 
   29947              :   /* The types of these expressions depends only on the type to which
   29948              :      the cast occurs.  */
   29949              :   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
   29950              :       || TREE_CODE (expression) == STATIC_CAST_EXPR
   29951              :       || TREE_CODE (expression) == CONST_CAST_EXPR
   29952              :       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
   29953              :       || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
   29954              :       || TREE_CODE (expression) == CAST_EXPR)
   29955     65458274 :     return dependent_type_p (TREE_TYPE (expression));
   29956              : 
   29957              :   /* The types of these expressions depends only on the type created
   29958              :      by the expression.  */
   29959              :   if (TREE_CODE (expression) == NEW_EXPR
   29960              :       || TREE_CODE (expression) == VEC_NEW_EXPR)
   29961              :     {
   29962              :       /* For NEW_EXPR tree nodes created inside a template, either
   29963              :          the object type itself or a TREE_LIST may appear as the
   29964              :          operand 1.  */
   29965       735349 :       tree type = TREE_OPERAND (expression, 1);
   29966       735349 :       if (TREE_CODE (type) == TREE_LIST)
   29967              :         /* This is an array type.  We need to check array dimensions
   29968              :            as well.  */
   29969            0 :         return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
   29970            0 :                || value_dependent_expression_p
   29971            0 :                     (TREE_OPERAND (TREE_VALUE (type), 1));
   29972              :       /* Array type whose dimension has to be deduced.  */
   29973       735349 :       else if (TREE_CODE (type) == ARRAY_TYPE
   29974       735349 :                && TREE_OPERAND (expression, 2) == NULL_TREE)
   29975              :         return true;
   29976              :       else
   29977       735337 :         return dependent_type_p (type);
   29978              :     }
   29979              : 
   29980              :   if (TREE_CODE (expression) == SCOPE_REF)
   29981              :     {
   29982    105813668 :       tree scope = TREE_OPERAND (expression, 0);
   29983    105813668 :       tree name = TREE_OPERAND (expression, 1);
   29984              : 
   29985              :       /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
   29986              :          contains an identifier associated by name lookup with one or more
   29987              :          declarations declared with a dependent type, or...a
   29988              :          nested-name-specifier or qualified-id that names a member of an
   29989              :          unknown specialization.  */
   29990    105813668 :       return (type_dependent_expression_p (name)
   29991    105813668 :               || dependent_scope_p (scope));
   29992              :     }
   29993              : 
   29994              :   if (TREE_CODE (expression) == TEMPLATE_DECL
   29995    295140549 :       && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
   29996    295140547 :     return uses_outer_template_parms (expression);
   29997              : 
   29998   3238956185 :   if (TREE_CODE (expression) == STMT_EXPR)
   29999          191 :     expression = stmt_expr_value_expr (expression);
   30000              : 
   30001   3238956185 :   if (BRACE_ENCLOSED_INITIALIZER_P (expression))
   30002              :     {
   30003     12480318 :       for (auto &elt : CONSTRUCTOR_ELTS (expression))
   30004      1887793 :         if (type_dependent_expression_p (elt.value))
   30005              :           return true;
   30006              :       return false;
   30007              :     }
   30008              : 
   30009              :   /* A static data member of the current instantiation with incomplete
   30010              :      array type is type-dependent, as the definition and specializations
   30011              :      can have different bounds.  */
   30012   3230354250 :   if (VAR_P (expression)
   30013    443623458 :       && DECL_CLASS_SCOPE_P (expression)
   30014     52654928 :       && dependent_type_p (DECL_CONTEXT (expression))
   30015   3252927245 :       && VAR_HAD_UNKNOWN_BOUND (expression))
   30016              :     return true;
   30017              : 
   30018              :   /* An array of unknown bound depending on a variadic parameter, eg:
   30019              : 
   30020              :      template<typename... Args>
   30021              :        void foo (Args... args)
   30022              :        {
   30023              :          int arr[] = { args... };
   30024              :        }
   30025              : 
   30026              :      template<int... vals>
   30027              :        void bar ()
   30028              :        {
   30029              :          int arr[] = { vals... };
   30030              :        }
   30031              : 
   30032              :      If the array has no length and has an initializer, it must be that
   30033              :      we couldn't determine its length in cp_complete_array_type because
   30034              :      it is dependent.  */
   30035    443569520 :   if (((VAR_P (expression) && DECL_INITIAL (expression))
   30036   2834450933 :        || COMPOUND_LITERAL_P (expression))
   30037    398318046 :       && TREE_TYPE (expression) != NULL_TREE
   30038    398318046 :       && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
   30039   3231139971 :       && !TYPE_DOMAIN (TREE_TYPE (expression)))
   30040              :    return true;
   30041              : 
   30042              :   /* Pull a FUNCTION_DECL out of a BASELINK if we can.  */
   30043   3230105376 :   if (BASELINK_P (expression))
   30044              :     {
   30045     15091877 :       if (BASELINK_OPTYPE (expression)
   30046     15091877 :           && dependent_type_p (BASELINK_OPTYPE (expression)))
   30047              :         return true;
   30048     15091820 :       expression = BASELINK_FUNCTIONS (expression);
   30049              :     }
   30050              : 
   30051              :   /* A function or variable template-id is type-dependent if it has any
   30052              :      dependent template arguments.  */
   30053   2786730729 :   if (VAR_OR_FUNCTION_DECL_P (expression)
   30054    705109470 :       && DECL_LANG_SPECIFIC (expression)
   30055   3603492517 :       && DECL_TEMPLATE_INFO (expression))
   30056              :     {
   30057              :       /* Consider the innermost template arguments, since those are the ones
   30058              :          that come from the template-id; the template arguments for the
   30059              :          enclosing class do not make it type-dependent unless they are used in
   30060              :          the type of the decl.  */
   30061    168716908 :       if (instantiates_primary_template_p (expression)
   30062    201685723 :           && (any_dependent_template_arguments_p
   30063     32968815 :               (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
   30064              :         return true;
   30065              :     }
   30066              : 
   30067              :   /* Otherwise, if the function decl isn't from a dependent scope, it can't be
   30068              :      type-dependent.  Checking this is important for functions with auto return
   30069              :      type, which looks like a dependent type.  */
   30070   3226171538 :   if (TREE_CODE (expression) == FUNCTION_DECL
   30071    389153883 :       && !(DECL_CLASS_SCOPE_P (expression)
   30072    131352778 :            && dependent_type_p (DECL_CONTEXT (expression)))
   30073    192126090 :       && !(DECL_LANG_SPECIFIC (expression)
   30074    192126090 :            && DECL_UNIQUE_FRIEND_P (expression)
   30075      4358558 :            && (!DECL_FRIEND_CONTEXT (expression)
   30076      4278840 :                || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
   30077   3417941227 :       && !DECL_LOCAL_DECL_P (expression))
   30078              :     {
   30079    191662997 :       gcc_assert (!dependent_type_p (TREE_TYPE (expression))
   30080              :                   || undeduced_auto_decl (expression));
   30081    191662997 :       return false;
   30082              :     }
   30083              : 
   30084              :   /* Otherwise, its constraints could still depend on outer template parameters
   30085              :      from its (dependent) scope.  */
   30086   3034508541 :   if (TREE_CODE (expression) == FUNCTION_DECL
   30087              :       /* As an optimization, check this cheaper sufficient condition first.
   30088              :          (At this point we've established that we're looking at a member of
   30089              :          a dependent class, so it makes sense to start treating say undeduced
   30090              :          auto as dependent.)  */
   30091     66138108 :       && !dependent_type_p (TREE_TYPE (expression))
   30092   3037562171 :       && uses_outer_template_parms_in_constraints (expression))
   30093              :     return true;
   30094              : 
   30095              :   /* Always dependent, on the number of arguments if nothing else.  */
   30096   3034508535 :   if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
   30097              :     return true;
   30098              : 
   30099              :   /* [temp.dep.expr]: "A pack-index-expression is type-dependent if its
   30100              :      id-expression is type-dependent."  */
   30101   3030217217 :   if (TREE_CODE (expression) == PACK_INDEX_EXPR)
   30102         3188 :     return type_dependent_expression_p (PACK_INDEX_PACK (expression));
   30103              : 
   30104   3030214029 :   if (TREE_TYPE (expression) == unknown_type_node)
   30105              :     {
   30106    208532002 :       if (TREE_CODE (expression) == ADDR_EXPR)
   30107          153 :         return type_dependent_expression_p (TREE_OPERAND (expression, 0));
   30108    208531849 :       if (TREE_CODE (expression) == COMPONENT_REF
   30109    164752354 :           || TREE_CODE (expression) == OFFSET_REF)
   30110              :         {
   30111     43779666 :           if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0)))
   30112              :             return true;
   30113     43532133 :           expression = TREE_OPERAND (expression, 1);
   30114   1265632283 :           if (identifier_p (expression))
   30115              :             return false;
   30116              :         }
   30117              :       /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
   30118    208284316 :       if (TREE_CODE (expression) == SCOPE_REF)
   30119              :         return false;
   30120              : 
   30121    208284316 :       if (BASELINK_P (expression))
   30122              :         {
   30123     43532133 :           if (BASELINK_OPTYPE (expression)
   30124     43532133 :               && dependent_type_p (BASELINK_OPTYPE (expression)))
   30125              :             return true;
   30126     43532127 :           expression = BASELINK_FUNCTIONS (expression);
   30127              :         }
   30128              : 
   30129    208284310 :       if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
   30130              :         {
   30131     49908693 :           tree args = TREE_OPERAND (expression, 1);
   30132     49908693 :           if (any_dependent_template_arguments_p (args))
   30133              :             return true;
   30134              :           /* Arguments of a function template-id aren't necessarily coerced
   30135              :              yet so we must conservatively assume that the address (and not
   30136              :              just value) of the argument matters as per [temp.dep.temp]/3.  */
   30137      8935172 :           for (tree arg : tree_vec_range (args))
   30138      5078241 :             if (has_value_dependent_address (arg))
   30139        18328 :               return true;
   30140      3856931 :           expression = TREE_OPERAND (expression, 0);
   30141    287451652 :           if (identifier_p (expression))
   30142              :             return true;
   30143              :         }
   30144              : 
   30145    162232545 :       gcc_assert (OVL_P (expression));
   30146              : 
   30147    507603306 :       for (lkp_iterator iter (expression); iter; ++iter)
   30148    396718932 :         if (type_dependent_expression_p (*iter))
   30149     51348171 :           return true;
   30150              : 
   30151    110884374 :       return false;
   30152              :     }
   30153              : 
   30154              :   /* The type of a non-type template parm declared with a placeholder type
   30155              :      depends on the corresponding template argument, even though
   30156              :      placeholders are not normally considered dependent.  */
   30157   2821682027 :   if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
   30158   2821682027 :       && is_auto (TREE_TYPE (expression)))
   30159              :     return true;
   30160              : 
   30161   2821510930 :   gcc_assert (TREE_CODE (expression) != TYPE_DECL);
   30162              : 
   30163              :   /* Dependent type attributes might not have made it from the decl to
   30164              :      the type yet.  */
   30165   2821510930 :   if (DECL_P (expression)
   30166   2821510930 :       && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
   30167              :     return true;
   30168              : 
   30169   2821510885 :   return (dependent_type_p (TREE_TYPE (expression)));
   30170              : }
   30171              : 
   30172              : /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
   30173              :    type-dependent if the expression refers to a member of the current
   30174              :    instantiation and the type of the referenced member is dependent, or the
   30175              :    class member access expression refers to a member of an unknown
   30176              :    specialization.
   30177              : 
   30178              :    This function returns true if the OBJECT in such a class member access
   30179              :    expression is of an unknown specialization.  */
   30180              : 
   30181              : bool
   30182    336096149 : type_dependent_object_expression_p (tree object)
   30183              : {
   30184              :   /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
   30185              :      dependent.  */
   30186    336096149 :   if (TREE_CODE (object) == IDENTIFIER_NODE)
   30187              :     return true;
   30188    336096149 :   tree scope = TREE_TYPE (object);
   30189    336096149 :   return (!scope || dependent_scope_p (scope));
   30190              : }
   30191              : 
   30192              : /* walk_tree callback function for instantiation_dependent_expression_p,
   30193              :    below.  Returns non-zero if a dependent subexpression is found.  */
   30194              : 
   30195              : static tree
   30196    458612616 : instantiation_dependent_r (tree *tp, int *walk_subtrees,
   30197              :                            void * /*data*/)
   30198              : {
   30199    458612616 :   if (TYPE_P (*tp))
   30200              :     {
   30201              :       /* We don't have to worry about decltype currently because decltype
   30202              :          of an instantiation-dependent expr is a dependent type.  This
   30203              :          might change depending on the resolution of DR 1172.  */
   30204     21889907 :       *walk_subtrees = false;
   30205     21889907 :       return NULL_TREE;
   30206              :     }
   30207    436722709 :   enum tree_code code = TREE_CODE (*tp);
   30208    436722709 :   switch (code)
   30209              :     {
   30210              :       /* Don't treat an argument list as dependent just because it has no
   30211              :          TREE_TYPE.  */
   30212              :     case TREE_LIST:
   30213              :     case TREE_VEC:
   30214              :     case NONTYPE_ARGUMENT_PACK:
   30215              :       return NULL_TREE;
   30216              : 
   30217     32895915 :     case TEMPLATE_PARM_INDEX:
   30218     32895915 :       if (dependent_type_p (TREE_TYPE (*tp)))
   30219        11637 :         return *tp;
   30220     32884278 :       if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
   30221              :         return *tp;
   30222              :       /* We'll check value-dependence separately.  */
   30223              :       return NULL_TREE;
   30224              : 
   30225              :       /* Handle expressions with type operands.  */
   30226      4245197 :     case SIZEOF_EXPR:
   30227      4245197 :     case ALIGNOF_EXPR:
   30228      4245197 :     case TYPEID_EXPR:
   30229      4245197 :     case AT_ENCODE_EXPR:
   30230      4245197 :       {
   30231      4245197 :         tree op = TREE_OPERAND (*tp, 0);
   30232      4245197 :         if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
   30233            0 :           op = TREE_TYPE (op);
   30234      4245197 :         else if (code == SIZEOF_EXPR
   30235      4143115 :                  && PACK_EXPANSION_P (op)
   30236      6745815 :                  && DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (op)))
   30237              :           {
   30238          528 :             tree d = PACK_EXPANSION_PATTERN (op);
   30239          528 :             if (DECL_HAS_VALUE_EXPR_P (d))
   30240              :               {
   30241          528 :                 d = DECL_VALUE_EXPR (d);
   30242          528 :                 if (TREE_CODE (d) == TREE_VEC
   30243          528 :                     && !type_dependent_expression_p (TREE_VEC_ELT (d, 0)))
   30244              :                   {
   30245          372 :                     *walk_subtrees = 0;
   30246          372 :                     return NULL_TREE;
   30247              :                   }
   30248              :               }
   30249              :           }
   30250      4244825 :         if (TYPE_P (op))
   30251              :           {
   30252      4188636 :             if (dependent_type_p (op))
   30253      3878691 :               return *tp;
   30254              :             else
   30255              :               {
   30256       309945 :                 *walk_subtrees = false;
   30257       309945 :                 return NULL_TREE;
   30258              :               }
   30259              :           }
   30260              :         break;
   30261              :       }
   30262              : 
   30263       529426 :     case COMPONENT_REF:
   30264       529426 :       if (identifier_p (TREE_OPERAND (*tp, 1)))
   30265              :         /* In a template, finish_class_member_access_expr creates a
   30266              :            COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
   30267              :            type-dependent, so that we can check access control at
   30268              :            instantiation time (PR 42277).  See also Core issue 1273.  */
   30269              :         return *tp;
   30270              :       break;
   30271              : 
   30272      6068935 :     case SCOPE_REF:
   30273      6068935 :       if (instantiation_dependent_scope_ref_p (*tp))
   30274      4786585 :         return *tp;
   30275              :       else
   30276              :         break;
   30277              : 
   30278              :       /* Treat statement-expressions as dependent.  */
   30279              :     case BIND_EXPR:
   30280              :       return *tp;
   30281              : 
   30282              :       /* Treat requires-expressions as dependent. */
   30283              :     case REQUIRES_EXPR:
   30284              :       return *tp;
   30285              : 
   30286      5022720 :     case CONSTRUCTOR:
   30287      5022720 :       if (CONSTRUCTOR_IS_DEPENDENT (*tp))
   30288              :         return *tp;
   30289              :       break;
   30290              : 
   30291     38308647 :     case TEMPLATE_DECL:
   30292     38308647 :     case FUNCTION_DECL:
   30293              :       /* Before C++17, a noexcept-specifier isn't part of the function type
   30294              :          so it doesn't affect type dependence, but we still want to consider it
   30295              :          for instantiation dependence.  */
   30296     38308647 :       if (cxx_dialect < cxx17
   30297        10452 :           && DECL_DECLARES_FUNCTION_P (*tp)
   30298     38319098 :           && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
   30299          463 :         return *tp;
   30300              :       break;
   30301              : 
   30302              :     default:
   30303              :       break;
   30304              :     }
   30305              : 
   30306    387963533 :   if (type_dependent_expression_p (*tp))
   30307     28862919 :     return *tp;
   30308              :   else
   30309              :     return NULL_TREE;
   30310              : }
   30311              : 
   30312              : /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
   30313              :    sense defined by the ABI:
   30314              : 
   30315              :    "An expression is instantiation-dependent if it is type-dependent
   30316              :    or value-dependent, or it has a subexpression that is type-dependent
   30317              :    or value-dependent."
   30318              : 
   30319              :    Except don't actually check value-dependence for unevaluated expressions,
   30320              :    because in sizeof(i) we don't care about the value of i.  Checking
   30321              :    type-dependence will in turn check value-dependence of array bounds/template
   30322              :    arguments as needed.  */
   30323              : 
   30324              : bool
   30325   1591584565 : instantiation_dependent_uneval_expression_p (tree expression)
   30326              : {
   30327   1591584565 :   tree result;
   30328              : 
   30329   1591584565 :   if (!processing_template_decl)
   30330              :     return false;
   30331              : 
   30332    382665834 :   if (expression == error_mark_node)
   30333              :     return false;
   30334              : 
   30335    382665820 :   result = cp_walk_tree_without_duplicates (&expression,
   30336              :                                             instantiation_dependent_r, NULL);
   30337    382665820 :   return result != NULL_TREE;
   30338              : }
   30339              : 
   30340              : /* As above, but also check value-dependence of the expression as a whole.  */
   30341              : 
   30342              : bool
   30343   1491524850 : instantiation_dependent_expression_p (tree expression)
   30344              : {
   30345   1491524850 :   return (instantiation_dependent_uneval_expression_p (expression)
   30346   1491524850 :           || (processing_template_decl
   30347    307470063 :               && potential_constant_expression (expression)
   30348    307469972 :               && value_dependent_expression_p (expression)));
   30349              : }
   30350              : 
   30351              : /* Like type_dependent_expression_p, but it also works while not processing
   30352              :    a template definition, i.e. during substitution or mangling.  */
   30353              : 
   30354              : bool
   30355     11301544 : type_dependent_expression_p_push (tree expr)
   30356              : {
   30357     11301544 :   bool b;
   30358     11301544 :   ++processing_template_decl;
   30359     11301544 :   b = type_dependent_expression_p (expr);
   30360     11301544 :   --processing_template_decl;
   30361     11301544 :   return b;
   30362              : }
   30363              : 
   30364              : /* Returns TRUE if ARGS contains a type-dependent expression.  */
   30365              : 
   30366              : bool
   30367    133068156 : any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
   30368              : {
   30369    133068156 :   if (!processing_template_decl || !args)
   30370              :     return false;
   30371              : 
   30372    125988171 :   for (tree arg : *args)
   30373     97135249 :     if (type_dependent_expression_p (arg))
   30374              :       return true;
   30375              : 
   30376              :   return false;
   30377              : }
   30378              : 
   30379              : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   30380              :    expressions) contains any type-dependent expressions.  */
   30381              : 
   30382              : bool
   30383            0 : any_type_dependent_elements_p (const_tree list)
   30384              : {
   30385            0 :   for (; list; list = TREE_CHAIN (list))
   30386            0 :     if (type_dependent_expression_p (TREE_VALUE (list)))
   30387              :       return true;
   30388              : 
   30389              :   return false;
   30390              : }
   30391              : 
   30392              : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   30393              :    expressions) contains any value-dependent expressions.  */
   30394              : 
   30395              : bool
   30396      3981691 : any_value_dependent_elements_p (const_tree list)
   30397              : {
   30398      6074436 :   for (; list; list = TREE_CHAIN (list))
   30399      3981711 :     if (value_dependent_expression_p (TREE_VALUE (list)))
   30400              :       return true;
   30401              : 
   30402              :   return false;
   30403              : }
   30404              : 
   30405              : /* Returns TRUE if the ARG (a template argument) is dependent.  */
   30406              : 
   30407              : bool
   30408   5698876085 : dependent_template_arg_p (tree arg)
   30409              : {
   30410   5698876085 :   if (!processing_template_decl)
   30411              :     return false;
   30412              : 
   30413              :   /* Assume a template argument that was wrongly written by the user
   30414              :      is dependent. This is consistent with what
   30415              :      any_dependent_template_arguments_p [that calls this function]
   30416              :      does.  */
   30417   5201239934 :   if (!arg || arg == error_mark_node)
   30418              :     return true;
   30419              : 
   30420   5201239364 :   if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   30421            0 :     arg = argument_pack_select_arg (arg);
   30422              : 
   30423   5201239364 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   30424              :     return true;
   30425   5200928026 :   if (TREE_CODE (arg) == TEMPLATE_DECL)
   30426              :     {
   30427     14832069 :       if (DECL_TEMPLATE_PARM_P (arg))
   30428              :         return true;
   30429              :       /* A member template of a dependent class is not necessarily
   30430              :          type-dependent, but it is a dependent template argument because it
   30431              :          will be a member of an unknown specialization to that template.  */
   30432     14828829 :       tree scope = CP_DECL_CONTEXT (arg);
   30433     14828829 :       return TYPE_P (scope) && dependent_type_p (scope);
   30434              :     }
   30435   5186095957 :   else if (ARGUMENT_PACK_P (arg))
   30436              :     {
   30437    202903940 :       tree args = ARGUMENT_PACK_ARGS (arg);
   30438    433653145 :       for (tree arg : tree_vec_range (args))
   30439    293740640 :         if (dependent_template_arg_p (arg))
   30440     62991435 :           return true;
   30441    139912505 :       return false;
   30442              :     }
   30443   4983192017 :   else if (TYPE_P (arg))
   30444   4410116636 :     return dependent_type_p (arg);
   30445              :   else
   30446    573075381 :     return value_dependent_expression_p (arg);
   30447              : }
   30448              : 
   30449              : /* Identify any expressions that use function parms.  */
   30450              : 
   30451              : static tree
   30452    488520540 : find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
   30453              : {
   30454    488520540 :   tree t = *tp;
   30455    488520540 :   if (TREE_CODE (t) == PARM_DECL)
   30456              :     {
   30457       362804 :       *walk_subtrees = 0;
   30458       362804 :       return t;
   30459              :     }
   30460              :   return NULL_TREE;
   30461              : }
   30462              : 
   30463              : /* Returns true if a type specialization formed using the template
   30464              :    arguments ARGS needs to use structural equality.  */
   30465              : 
   30466              : bool
   30467    107104382 : any_template_arguments_need_structural_equality_p (tree args)
   30468              : {
   30469    107104382 :   int i;
   30470    107104382 :   int j;
   30471              : 
   30472    107104382 :   if (!args)
   30473              :     return false;
   30474    107104382 :   if (args == error_mark_node)
   30475              :     return true;
   30476              : 
   30477    422363766 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   30478              :     {
   30479    109943842 :       tree level = TMPL_ARGS_LEVEL (args, i + 1);
   30480    299857274 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   30481              :         {
   30482    191590313 :           tree arg = TREE_VEC_ELT (level, j);
   30483    191590313 :           tree packed_args = NULL_TREE;
   30484    191590313 :           int k, len = 1;
   30485              : 
   30486    191590313 :           if (ARGUMENT_PACK_P (arg))
   30487              :             {
   30488              :               /* Look inside the argument pack.  */
   30489     15110565 :               packed_args = ARGUMENT_PACK_ARGS (arg);
   30490     15110565 :               len = TREE_VEC_LENGTH (packed_args);
   30491              :             }
   30492              : 
   30493    393437073 :           for (k = 0; k < len; ++k)
   30494              :             {
   30495    203523641 :               if (packed_args)
   30496     27043893 :                 arg = TREE_VEC_ELT (packed_args, k);
   30497              : 
   30498    203523641 :               if (error_operand_p (arg))
   30499      1676881 :                 return true;
   30500    203523641 :               else if (TREE_CODE (arg) == TEMPLATE_DECL)
   30501      3971011 :                 continue;
   30502    199552630 :               else if (arg == any_targ_node)
   30503              :                 /* An any_targ_node argument (added by add_defaults_to_ttp)
   30504              :                    makes the corresponding specialization not canonicalizable,
   30505              :                    since template_args_equal always return true for it.  We
   30506              :                    may see this when called from bind_template_template_parm.  */
   30507              :                 return true;
   30508              :               /* Checking current_function_decl because this structural
   30509              :                  comparison is only necessary for redeclaration.  */
   30510    199552626 :               else if (!current_function_decl
   30511    176943148 :                        && dependent_template_arg_p (arg)
   30512    276240204 :                        && (cp_walk_tree_without_duplicates
   30513              :                            (&arg, find_parm_usage_r, NULL)))
   30514              :                 /* The identity of a class template specialization that uses
   30515              :                    a function parameter depends on the identity of the function.
   30516              :                    And if this specialization appeared in the trailing return
   30517              :                    type thereof, we don't know the identity of the function
   30518              :                    (e.g. if it's a redeclaration or a new function) until we
   30519              :                    form its signature and go through duplicate_decls.  Thus
   30520              :                    it's unsafe to decide on a canonical type now (which depends
   30521              :                    on the DECL_CONTEXT of the function parameter, which can get
   30522              :                    mutated after the fact by duplicate_decls), so just require
   30523              :                    structural equality in this case (PR52830).  */
   30524              :                 return true;
   30525    199189822 :               else if (TYPE_P (arg)
   30526    164291684 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg)
   30527    219423360 :                        && (dependent_alias_template_spec_p (arg, nt_opaque)
   30528     18921687 :                            || dependent_opaque_alias_p (arg)))
   30529              :                 /* Require structural equality for specializations written
   30530              :                    in terms of a dependent alias template specialization.  */
   30531      1311884 :                 return true;
   30532     56346656 :               else if (CLASS_TYPE_P (arg)
   30533     55667342 :                        && TYPE_TEMPLATE_INFO (arg)
   30534    241861142 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg))
   30535              :                 /* Require structural equality for specializations written
   30536              :                    in terms of a class template specialization that itself
   30537              :                    needs structural equality.  */
   30538              :                 return true;
   30539              :             }
   30540              :         }
   30541              :     }
   30542              : 
   30543              :   return false;
   30544              : }
   30545              : 
   30546              : /* Returns true if ARGS (a collection of template arguments) contains
   30547              :    any dependent arguments.  */
   30548              : 
   30549              : bool
   30550   3469993662 : any_dependent_template_arguments_p (const_tree args)
   30551              : {
   30552   3469993662 :   if (args == error_mark_node)
   30553              :     return true;
   30554   3469993662 :   if (!processing_template_decl || !args)
   30555              :     return false;
   30556              : 
   30557   8085384415 :   for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
   30558              :     {
   30559   3183104316 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   30560   6893862608 :       for (tree arg : tree_vec_range (const_cast<tree> (level)))
   30561   4752901570 :         if (dependent_template_arg_p (arg))
   30562   1042143278 :           return true;
   30563              :     }
   30564              : 
   30565              :   return false;
   30566              : }
   30567              : 
   30568              : /* Returns true if ARGS contains any errors.  */
   30569              : 
   30570              : bool
   30571     25224732 : any_erroneous_template_args_p (const_tree args)
   30572              : {
   30573     25224732 :   int i;
   30574     25224732 :   int j;
   30575              : 
   30576     25224732 :   if (args == error_mark_node)
   30577              :     return true;
   30578              : 
   30579     25224705 :   if (args && TREE_CODE (args) != TREE_VEC)
   30580              :     {
   30581     25224705 :       if (tree ti = get_template_info (args))
   30582     19602210 :         args = TI_ARGS (ti);
   30583              :       else
   30584              :         args = NULL_TREE;
   30585              :     }
   30586              : 
   30587     19602210 :   if (!args)
   30588      5622495 :     return false;
   30589              : 
   30590     78297948 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   30591              :     {
   30592     19712818 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   30593     53347368 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   30594     33634698 :         if (error_operand_p (TREE_VEC_ELT (level, j)))
   30595              :           return true;
   30596              :     }
   30597              : 
   30598              :   return false;
   30599              : }
   30600              : 
   30601              : /* Returns TRUE if the template TMPL is type-dependent.  */
   30602              : 
   30603              : bool
   30604      1753716 : dependent_template_p (tree tmpl)
   30605              : {
   30606      1753716 :   if (TREE_CODE (tmpl) == OVERLOAD)
   30607              :     {
   30608      1744477 :       for (lkp_iterator iter (tmpl); iter; ++iter)
   30609      1182659 :         if (dependent_template_p (*iter))
   30610            0 :           return true;
   30611       561818 :       return false;
   30612              :     }
   30613              : 
   30614              :   /* Template template parameters are dependent.  */
   30615       963479 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
   30616      2155377 :       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   30617              :     return true;
   30618              :   /* So are names that have not been looked up.  */
   30619      1201041 :   if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
   30620              :     return true;
   30621              :   return false;
   30622              : }
   30623              : 
   30624              : /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
   30625              : 
   30626              : bool
   30627       571057 : dependent_template_id_p (tree tmpl, tree args)
   30628              : {
   30629       571057 :   return (dependent_template_p (tmpl)
   30630       571057 :           || any_dependent_template_arguments_p (args));
   30631              : }
   30632              : 
   30633              : /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
   30634              :    are dependent.  BODY is the body to use for loop transforming
   30635              :    constructs.  */
   30636              : 
   30637              : bool
   30638        21035 : dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv, tree body)
   30639              : {
   30640        21035 :   int i, k;
   30641              : 
   30642        21035 :   if (!processing_template_decl)
   30643              :     return false;
   30644              : 
   30645         1821 :   for (i = 0, k = 0; i < TREE_VEC_LENGTH (declv); i++)
   30646              :     {
   30647         1209 :       tree decl = TREE_VEC_ELT (declv, i);
   30648         1209 :       tree init = TREE_VEC_ELT (initv, i);
   30649         1209 :       tree cond = TREE_VEC_ELT (condv, i);
   30650         1209 :       tree incr = TREE_VEC_ELT (incrv, i);
   30651              : 
   30652         1209 :       if (decl == NULL_TREE)
   30653              :         {
   30654           10 :           tree stmt = body;
   30655           10 :           int j = c_omp_find_generated_loop (stmt, k++, cp_walk_subtrees);
   30656           10 :           init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   30657           10 :           decl = TREE_OPERAND (init, 0);
   30658           10 :           cond = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   30659           10 :           incr = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   30660              :         }
   30661              : 
   30662         1209 :       if (type_dependent_expression_p (decl)
   30663         1209 :           || TREE_CODE (decl) == SCOPE_REF)
   30664              :         return true;
   30665              : 
   30666          875 :       if (init && type_dependent_expression_p (init))
   30667              :         return true;
   30668              : 
   30669          875 :       if (cond == global_namespace)
   30670              :         return true;
   30671              : 
   30672          802 :       if (type_dependent_expression_p (cond))
   30673              :         return true;
   30674              : 
   30675          778 :       if (COMPARISON_CLASS_P (cond)
   30676          778 :           && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
   30677          774 :               || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
   30678           11 :         return true;
   30679              : 
   30680          767 :       if (TREE_CODE (incr) == MODOP_EXPR)
   30681              :         {
   30682            1 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
   30683            1 :               || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
   30684            0 :             return true;
   30685              :         }
   30686          766 :       else if (type_dependent_expression_p (incr))
   30687              :         return true;
   30688          766 :       else if (TREE_CODE (incr) == MODIFY_EXPR)
   30689              :         {
   30690          233 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
   30691              :             return true;
   30692          233 :           else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
   30693              :             {
   30694          232 :               tree t = TREE_OPERAND (incr, 1);
   30695          232 :               if (type_dependent_expression_p (TREE_OPERAND (t, 0))
   30696          232 :                   || type_dependent_expression_p (TREE_OPERAND (t, 1)))
   30697            5 :                 return true;
   30698              : 
   30699              :               /* If this loop has a class iterator with != comparison
   30700              :                  with increment other than i++/++i/i--/--i, make sure the
   30701              :                  increment is constant.  */
   30702          454 :               if (CLASS_TYPE_P (TREE_TYPE (decl))
   30703          288 :                   && TREE_CODE (cond) == NE_EXPR)
   30704              :                 {
   30705            8 :                   if (TREE_OPERAND (t, 0) == decl)
   30706            8 :                     t = TREE_OPERAND (t, 1);
   30707              :                   else
   30708            0 :                     t = TREE_OPERAND (t, 0);
   30709            8 :                   if (TREE_CODE (t) != INTEGER_CST)
   30710              :                     return true;
   30711              :                 }
   30712              :             }
   30713              :         }
   30714              :     }
   30715              : 
   30716              :   return false;
   30717              : }
   30718              : 
   30719              : /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
   30720              :    TYPENAME_TYPE corresponds.  Returns the original TYPENAME_TYPE if
   30721              :    no such TYPE can be found.  Note that this function peers inside
   30722              :    uninstantiated templates and therefore should be used only in
   30723              :    extremely limited situations.  ONLY_CURRENT_P restricts this
   30724              :    peering to the currently open classes hierarchy (which is required
   30725              :    when comparing types).  */
   30726              : 
   30727              : tree
   30728    367706441 : resolve_typename_type (tree type, bool only_current_p)
   30729              : {
   30730    367706441 :   tree scope;
   30731    367706441 :   tree name;
   30732    367706441 :   tree decl;
   30733    367706441 :   int quals;
   30734    367706441 :   tree pushed_scope;
   30735    367706441 :   tree result;
   30736              : 
   30737    367706441 :   gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
   30738              : 
   30739    367706441 :   scope = TYPE_CONTEXT (type);
   30740              :   /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope.  */
   30741    367706441 :   gcc_checking_assert (uses_template_parms (scope));
   30742              : 
   30743              :   /* Usually the non-qualified identifier of a TYPENAME_TYPE is
   30744              :      TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
   30745              :      TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
   30746              :      representing the typedef. In that case TYPE_IDENTIFIER (type) is
   30747              :      not the non-qualified identifier of the TYPENAME_TYPE anymore.
   30748              :      So by getting the TYPE_IDENTIFIER of the _main declaration_ of
   30749              :      the TYPENAME_TYPE instead, we avoid messing up with a possible
   30750              :      typedef variant case.  */
   30751    367706441 :   name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
   30752              : 
   30753              :   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
   30754              :      it first before we can figure out what NAME refers to.  */
   30755    367706441 :   if (TREE_CODE (scope) == TYPENAME_TYPE)
   30756              :     {
   30757      5712479 :       if (TYPENAME_IS_RESOLVING_P (scope))
   30758              :         /* Given a class template A with a dependent base with nested type C,
   30759              :            typedef typename A::C::C C will land us here, as trying to resolve
   30760              :            the initial A::C leads to the local C typedef, which leads back to
   30761              :            A::C::C.  So we break the recursion now.  */
   30762              :         return type;
   30763              :       else
   30764      5712479 :         scope = resolve_typename_type (scope, only_current_p);
   30765              :     }
   30766              :   /* If we don't know what SCOPE refers to, then we cannot resolve the
   30767              :      TYPENAME_TYPE.  */
   30768    367706441 :   if (!CLASS_TYPE_P (scope))
   30769              :     return type;
   30770              :   /* If this is a typedef, we don't want to look inside (c++/11987).  */
   30771    233317771 :   if (typedef_variant_p (type))
   30772              :     return type;
   30773              :   /* If SCOPE isn't the template itself, it will not have a valid
   30774              :      TYPE_FIELDS list.  */
   30775    179476519 :   if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
   30776              :     /* scope is either the template itself or a compatible instantiation
   30777              :        like X<T>, so look up the name in the original template.  */
   30778    118762006 :     scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
   30779              :   /* If scope has no fields, it can't be a current instantiation.  Check this
   30780              :      before currently_open_class to avoid infinite recursion (71515).  */
   30781    179476519 :   if (!TYPE_FIELDS (scope))
   30782              :     return type;
   30783              :   /* If the SCOPE is not the current instantiation, there's no reason
   30784              :      to look inside it.  */
   30785    118653190 :   if (only_current_p && !currently_open_class (scope))
   30786              :     return type;
   30787              :   /* Enter the SCOPE so that name lookup will be resolved as if we
   30788              :      were in the class definition.  In particular, SCOPE will no
   30789              :      longer be considered a dependent type.  */
   30790        55344 :   pushed_scope = push_scope (scope);
   30791              :   /* Look up the declaration.  */
   30792        55344 :   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
   30793              :                         tf_warning_or_error);
   30794              : 
   30795        55344 :   result = NULL_TREE;
   30796              : 
   30797              :   /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
   30798              :      find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
   30799        55344 :   tree fullname = TYPENAME_TYPE_FULLNAME (type);
   30800        55344 :   if (!decl)
   30801              :     /*nop*/;
   30802        55318 :   else if (identifier_p (fullname)
   30803        55312 :            && TREE_CODE (decl) == TYPE_DECL)
   30804              :     {
   30805        55312 :       result = TREE_TYPE (decl);
   30806        55312 :       if (result == error_mark_node)
   30807           29 :         result = NULL_TREE;
   30808              :     }
   30809            6 :   else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
   30810            6 :            && DECL_CLASS_TEMPLATE_P (decl))
   30811              :     {
   30812              :       /* Obtain the template and the arguments.  */
   30813            3 :       tree tmpl = TREE_OPERAND (fullname, 0);
   30814            3 :       if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
   30815              :         {
   30816              :           /* We get here with a plain identifier because a previous tentative
   30817              :              parse of the nested-name-specifier as part of a ptr-operator saw
   30818              :              ::template X<A>.  The use of ::template is necessary in a
   30819              :              ptr-operator, but wrong in a declarator-id.
   30820              : 
   30821              :              [temp.names]: In a qualified-id of a declarator-id, the keyword
   30822              :              template shall not appear at the top level.  */
   30823            3 :           pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
   30824              :                    "keyword %<template%> not allowed in declarator-id");
   30825            3 :           tmpl = decl;
   30826              :         }
   30827            3 :       tree args = TREE_OPERAND (fullname, 1);
   30828              :       /* Instantiate the template.  */
   30829            3 :       result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
   30830              :                                       tf_error | tf_user);
   30831            3 :       result = adjust_type_for_entering_scope (result);
   30832            3 :       if (result == error_mark_node)
   30833           29 :         result = NULL_TREE;
   30834              :     }
   30835              : 
   30836              :   /* Leave the SCOPE.  */
   30837        55344 :   if (pushed_scope)
   30838        55326 :     pop_scope (pushed_scope);
   30839              : 
   30840              :   /* If we failed to resolve it, return the original typename.  */
   30841        55344 :   if (!result)
   30842              :     return type;
   30843              : 
   30844              :   /* If lookup found a typename type, resolve that too.  */
   30845        55315 :   if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
   30846              :     {
   30847              :       /* Ill-formed programs can cause infinite recursion here, so we
   30848              :          must catch that.  */
   30849            3 :       TYPENAME_IS_RESOLVING_P (result) = 1;
   30850            3 :       result = resolve_typename_type (result, only_current_p);
   30851            3 :       TYPENAME_IS_RESOLVING_P (result) = 0;
   30852              :     }
   30853              : 
   30854              :   /* Qualify the resulting type.  */
   30855        55315 :   quals = cp_type_quals (type);
   30856        55315 :   if (quals)
   30857            0 :     result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
   30858              : 
   30859              :   return result;
   30860              : }
   30861              : 
   30862              : /* Returns a type which represents 'auto' or 'decltype(auto)'.  We use a
   30863              :    TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
   30864              :    by default.  If set_canonical is true, we set TYPE_CANONICAL on it.  */
   30865              : 
   30866              : static tree
   30867     30061726 : make_auto_1 (tree name, bool set_canonical, int level = -1)
   30868              : {
   30869     30061726 :   if (level == -1)
   30870     20877083 :     level = current_template_depth + 1;
   30871     30061726 :   tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
   30872     30061726 :   TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
   30873     30061726 :   TYPE_STUB_DECL (au) = TYPE_NAME (au);
   30874     30061726 :   TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
   30875     30061726 :     (0, level, level, TYPE_NAME (au), NULL_TREE);
   30876     30061726 :   if (set_canonical)
   30877     19507747 :     TYPE_CANONICAL (au) = canonical_type_parameter (au);
   30878     30061726 :   DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
   30879     30061726 :   SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
   30880     30061726 :   if (name == decltype_auto_identifier)
   30881      1967847 :     AUTO_IS_DECLTYPE (au) = true;
   30882              : 
   30883     30061726 :   return au;
   30884              : }
   30885              : 
   30886              : tree
   30887       585119 : make_decltype_auto (void)
   30888              : {
   30889       585119 :   return make_auto_1 (decltype_auto_identifier, true);
   30890              : }
   30891              : 
   30892              : tree
   30893     18885320 : make_auto (void)
   30894              : {
   30895     18885320 :   return make_auto_1 (auto_identifier, true);
   30896              : }
   30897              : 
   30898              : /* Return a C++17 deduction placeholder for class template TMPL.
   30899              :    There are represented as an 'auto' with the special level 0 and
   30900              :    CLASS_PLACEHOLDER_TEMPLATE set.  */
   30901              : 
   30902              : tree
   30903      9147335 : make_template_placeholder (tree tmpl)
   30904              : {
   30905      9147335 :   tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
   30906      9147335 :   CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
   30907              :   /* Our canonical type depends on the placeholder.  */
   30908      9147335 :   TYPE_CANONICAL (t) = canonical_type_parameter (t);
   30909      9147335 :   return t;
   30910              : }
   30911              : 
   30912              : /* True iff T is a C++17 class template deduction placeholder.  */
   30913              : 
   30914              : bool
   30915   1764956086 : template_placeholder_p (tree t)
   30916              : {
   30917   1813793157 :   return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
   30918              : }
   30919              : 
   30920              : /* Return an auto for an explicit cast expression auto(x).
   30921              :    Like CTAD placeholders, these have level 0 so that they're
   30922              :    not accidentally replaced via tsubst and are always directly
   30923              :    resolved via do_auto_deduction.  */
   30924              : 
   30925              : tree
   30926        37308 : make_cast_auto ()
   30927              : {
   30928        37308 :   return make_auto_1 (auto_identifier, true, /*level=*/0);
   30929              : }
   30930              : 
   30931              : /* Make a "constrained auto" type-specifier. This is an auto or
   30932              :   decltype(auto) type with constraints that must be associated after
   30933              :   deduction.  The constraint is formed from the given concept CON
   30934              :   and its optional sequence of template arguments ARGS.
   30935              : 
   30936              :   TYPE must be the result of make_auto_type or make_decltype_auto_type. */
   30937              : 
   30938              : static tree
   30939      1406375 : make_constrained_placeholder_type (tree type, tree con, tree args)
   30940              : {
   30941              :   /* Build the constraint. */
   30942      1406375 :   tree tmpl = DECL_TI_TEMPLATE (con);
   30943      1406375 :   ++processing_template_decl;
   30944      1406375 :   tree expr = build_concept_check (tmpl, type, args, tf_warning_or_error);
   30945      1406375 :   --processing_template_decl;
   30946              : 
   30947      1406375 :   PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
   30948      1406375 :     = build_tree_list (current_template_parms, expr);
   30949              : 
   30950              :   /* Our canonical type depends on the constraint.  */
   30951      1406375 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   30952              : 
   30953      1406375 :   return type;
   30954              : }
   30955              : 
   30956              : /* Make a "constrained auto" type-specifier.  */
   30957              : 
   30958              : tree
   30959        23656 : make_constrained_auto (tree con, tree args)
   30960              : {
   30961        23656 :   tree type = make_auto_1 (auto_identifier, false);
   30962        23656 :   return make_constrained_placeholder_type (type, con, args);
   30963              : }
   30964              : 
   30965              : /* Make a "constrained decltype(auto)" type-specifier.  */
   30966              : 
   30967              : tree
   30968      1382719 : make_constrained_decltype_auto (tree con, tree args)
   30969              : {
   30970      1382719 :   tree type = make_auto_1 (decltype_auto_identifier, false);
   30971      1382719 :   return make_constrained_placeholder_type (type, con, args);
   30972              : }
   30973              : 
   30974              : /* Create an "auto..." type-specifier.  */
   30975              : 
   30976              : tree
   30977          174 : make_auto_pack ()
   30978              : {
   30979          174 :   tree type = make_auto_1 (auto_identifier, false);
   30980          174 :   TEMPLATE_TYPE_PARAMETER_PACK (type) = true;
   30981              :   /* Our canonical type depends on being a pack.  */
   30982          174 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   30983          174 :   return type;
   30984              : }
   30985              : 
   30986              : /* Returns true if the placeholder type constraint T has any dependent
   30987              :    (explicit) template arguments.  */
   30988              : 
   30989              : static bool
   30990           59 : placeholder_type_constraint_dependent_p (tree t)
   30991              : {
   30992           59 :   gcc_assert (concept_check_p (t));
   30993           59 :   tree args = TREE_OPERAND (t, 1);
   30994           59 :   tree first = TREE_VEC_ELT (args, 0);
   30995           59 :   if (ARGUMENT_PACK_P (first))
   30996              :     {
   30997           18 :       args = expand_template_argument_pack (args);
   30998           18 :       first = TREE_VEC_ELT (args, 0);
   30999              :     }
   31000           59 :   gcc_checking_assert (is_auto (first));
   31001           77 :   for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
   31002           42 :     if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
   31003              :       return true;
   31004              :   return false;
   31005              : }
   31006              : 
   31007              : /* Prepare and return a concept definition.  */
   31008              : 
   31009              : tree
   31010      2549351 : start_concept_definition (cp_expr id)
   31011              : {
   31012      2549351 :   gcc_assert (identifier_p (id));
   31013      2549351 :   gcc_assert (processing_template_decl);
   31014              : 
   31015      2549351 :   location_t loc = id.get_location();
   31016              : 
   31017              :   /* A concept-definition shall not have associated constraints.  */
   31018      2549351 :   if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
   31019              :     {
   31020            6 :       error_at (loc, "a concept cannot be constrained");
   31021            6 :       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
   31022              :     }
   31023              : 
   31024              :   /* A concept-definition shall appear in namespace scope.  Templates
   31025              :      aren't allowed in block scope, so we only need to check for class
   31026              :      scope.  */
   31027      2549351 :   if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
   31028              :     {
   31029            6 :       error_at (loc, "concept %qE not in namespace scope", *id);
   31030            6 :       return error_mark_node;
   31031              :     }
   31032              : 
   31033      2549345 :   if (current_template_depth > 1)
   31034              :     {
   31035            3 :       error_at (loc, "concept %qE has multiple template parameter lists", *id);
   31036            3 :       return error_mark_node;
   31037              :     }
   31038              : 
   31039              :   /* Initially build the concept declaration; its type is bool.  */
   31040      2549342 :   tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
   31041      2549342 :   DECL_CONTEXT (decl) = current_scope ();
   31042      2549342 :   TREE_PUBLIC (decl) = true;
   31043              : 
   31044      2549342 :   return decl;
   31045              : }
   31046              : 
   31047              : /* Finish building a concept definition. Like other templates, the
   31048              :    CONCEPT_DECL node is wrapped by a TEMPLATE_DECL.  This returns the
   31049              :    the TEMPLATE_DECL. */
   31050              : 
   31051              : tree
   31052      2549342 : finish_concept_definition (tree decl, tree init, tree attrs)
   31053              : {
   31054      2549342 :   DECL_INITIAL (decl) = init;
   31055              : 
   31056      2549342 :   if (attrs)
   31057           27 :     cplus_decl_attributes (&decl, attrs, 0);
   31058              : 
   31059      2549342 :   set_originating_module (decl, false);
   31060      2549342 :   check_module_decl_linkage (decl);
   31061              : 
   31062              :   /* Push the enclosing template.  */
   31063      2549342 :   return push_template_decl (decl);
   31064              : }
   31065              : 
   31066              : /* Given type ARG, return std::initializer_list<ARG>.  */
   31067              : 
   31068              : static tree
   31069          396 : listify (tree arg)
   31070              : {
   31071          396 :   tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
   31072              : 
   31073          396 :   if (std_init_list == error_mark_node
   31074          396 :       || !DECL_CLASS_TEMPLATE_P (std_init_list))
   31075              :     {
   31076           21 :       gcc_rich_location richloc (input_location);
   31077           21 :       maybe_add_include_fixit (&richloc, "<initializer_list>", false);
   31078           21 :       error_at (&richloc,
   31079              :                 "deducing from brace-enclosed initializer list"
   31080              :                 " requires %<#include <initializer_list>%>");
   31081              : 
   31082           21 :       return error_mark_node;
   31083           21 :     }
   31084          375 :   tree argvec = make_tree_vec (1);
   31085          375 :   TREE_VEC_ELT (argvec, 0) = arg;
   31086              : 
   31087          375 :   return lookup_template_class (std_init_list, argvec, NULL_TREE,
   31088          375 :                                 NULL_TREE, tf_warning_or_error);
   31089              : }
   31090              : 
   31091              : /* Replace auto in TYPE with std::initializer_list<auto>.  */
   31092              : 
   31093              : static tree
   31094          396 : listify_autos (tree type, tree auto_node)
   31095              : {
   31096          396 :   tree init_auto = listify (strip_top_quals (auto_node));
   31097          396 :   tree argvec = make_tree_vec (1);
   31098          396 :   TREE_VEC_ELT (argvec, 0) = init_auto;
   31099          396 :   if (processing_template_decl)
   31100            3 :     argvec = add_to_template_args (current_template_args (), argvec);
   31101          396 :   return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
   31102              : }
   31103              : 
   31104              : /* Hash traits for hashing possibly constrained 'auto'
   31105              :    TEMPLATE_TYPE_PARMs for use by do_auto_deduction.  */
   31106              : 
   31107              : struct auto_hash : default_hash_traits<tree>
   31108              : {
   31109              :   static inline hashval_t hash (tree);
   31110              :   static inline bool equal (tree, tree);
   31111              : };
   31112              : 
   31113              : /* Hash the 'auto' T.  */
   31114              : 
   31115              : inline hashval_t
   31116              : auto_hash::hash (tree t)
   31117              : {
   31118              :   if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
   31119              :     /* Matching constrained-type-specifiers denote the same template
   31120              :        parameter, so hash the constraint.  */
   31121              :     return iterative_hash_placeholder_constraint (c, 0);
   31122              :   else
   31123              :     /* But unconstrained autos are all separate, so just hash the pointer.  */
   31124              :     return iterative_hash_object (t, 0);
   31125              : }
   31126              : 
   31127              : /* Compare two 'auto's.  */
   31128              : 
   31129              : inline bool
   31130              : auto_hash::equal (tree t1, tree t2)
   31131              : {
   31132              :   if (t1 == t2)
   31133              :     return true;
   31134              : 
   31135              :   tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
   31136              :   tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
   31137              : 
   31138              :   /* Two unconstrained autos are distinct.  */
   31139              :   if (!c1 || !c2)
   31140              :     return false;
   31141              : 
   31142              :   return equivalent_placeholder_constraints (c1, c2);
   31143              : }
   31144              : 
   31145              : /* The stem for deduction guide names.  */
   31146              : const char *const dguide_base = "__dguide_";
   31147              : 
   31148              : /* Return the name for a deduction guide for class template TMPL.  */
   31149              : 
   31150              : tree
   31151      1394771 : dguide_name (tree tmpl)
   31152              : {
   31153      1394771 :   tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
   31154      1394771 :   tree tname = TYPE_IDENTIFIER (type);
   31155      1394771 :   char *buf = (char *) alloca (1 + strlen (dguide_base)
   31156              :                                + IDENTIFIER_LENGTH (tname));
   31157      1394771 :   memcpy (buf, dguide_base, strlen (dguide_base));
   31158      1394771 :   memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
   31159      1394771 :           IDENTIFIER_LENGTH (tname) + 1);
   31160      1394771 :   tree dname = get_identifier (buf);
   31161      1394771 :   TREE_TYPE (dname) = type;
   31162      1394771 :   return dname;
   31163              : }
   31164              : 
   31165              : /* True if NAME is the name of a deduction guide.  */
   31166              : 
   31167              : bool
   31168   1313435279 : dguide_name_p (tree name)
   31169              : {
   31170   1313435279 :   return (TREE_CODE (name) == IDENTIFIER_NODE
   31171   1313435277 :           && TREE_TYPE (name)
   31172   1379328913 :           && startswith (IDENTIFIER_POINTER (name), dguide_base));
   31173              : }
   31174              : 
   31175              : /* True if FN is a deduction guide.  */
   31176              : 
   31177              : bool
   31178    879103246 : deduction_guide_p (const_tree fn)
   31179              : {
   31180    879103246 :   if (DECL_P (fn))
   31181    879081991 :     if (tree name = DECL_NAME (fn))
   31182    879081879 :       return dguide_name_p (name);
   31183              :   return false;
   31184              : }
   31185              : 
   31186              : /* True if FN is the copy deduction guide, i.e. A(A)->A.  */
   31187              : 
   31188              : bool
   31189        18529 : copy_guide_p (const_tree fn)
   31190              : {
   31191        18529 :   gcc_assert (deduction_guide_p (fn));
   31192        18529 :   if (!DECL_ARTIFICIAL (fn))
   31193              :     return false;
   31194        18529 :   tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
   31195        18529 :   return (TREE_CHAIN (parms) == void_list_node
   31196        18529 :           && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
   31197              : }
   31198              : 
   31199              : /* True if FN is a guide generated from a constructor template.  */
   31200              : 
   31201              : bool
   31202           52 : template_guide_p (const_tree fn)
   31203              : {
   31204           52 :   gcc_assert (deduction_guide_p (fn));
   31205           52 :   if (!DECL_ARTIFICIAL (fn))
   31206              :     return false;
   31207           52 :   tree tmpl = DECL_TI_TEMPLATE (fn);
   31208           52 :   if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
   31209           40 :     return PRIMARY_TEMPLATE_P (org);
   31210              :   return false;
   31211              : }
   31212              : 
   31213              : /* True if FN is an aggregate initialization guide or the copy deduction
   31214              :    guide.  */
   31215              : 
   31216              : bool
   31217           12 : builtin_guide_p (const_tree fn)
   31218              : {
   31219           12 :   if (!deduction_guide_p (fn))
   31220              :     return false;
   31221           12 :   if (!DECL_ARTIFICIAL (fn))
   31222              :     /* Explicitly declared.  */
   31223              :     return false;
   31224           12 :   if (DECL_ABSTRACT_ORIGIN (fn))
   31225              :     /* Derived from a constructor.  */
   31226            0 :     return false;
   31227              :   return true;
   31228              : }
   31229              : 
   31230              : /* True if FN is a C++23 inherited guide.  */
   31231              : 
   31232              : bool
   31233        39070 : inherited_guide_p (const_tree fn)
   31234              : {
   31235        39070 :   gcc_assert (deduction_guide_p (fn));
   31236        39070 :   return LANG_DECL_FN_CHECK (fn)->context != NULL_TREE;
   31237              : }
   31238              : 
   31239              : /* Set the base class BASE from which the transformed guide FN
   31240              :    was inherited as part of C++23 inherited CTAD.  */
   31241              : 
   31242              : static void
   31243          214 : set_inherited_guide_context (const_tree fn, tree base)
   31244              : {
   31245          214 :   gcc_assert (deduction_guide_p (fn));
   31246          214 :   LANG_DECL_FN_CHECK (fn)->context = base;
   31247          214 : }
   31248              : 
   31249              : /* OLDDECL is a _DECL for a template parameter.  Return a similar parameter at
   31250              :    LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
   31251              :    template parameter types.  Note that the handling of template template
   31252              :    parameters relies on current_template_parms being set appropriately for the
   31253              :    new template.  */
   31254              : 
   31255              : static tree
   31256       170727 : rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
   31257              :                        tree tsubst_args, tsubst_flags_t complain)
   31258              : {
   31259       170727 :   if (olddecl == error_mark_node)
   31260              :     return error_mark_node;
   31261              : 
   31262       170724 :   tree oldidx = get_template_parm_index (olddecl);
   31263              : 
   31264       170724 :   tree newtype;
   31265       170724 :   if (TREE_CODE (olddecl) == TYPE_DECL
   31266        37382 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   31267              :     {
   31268       133377 :       tree oldtype = TREE_TYPE (olddecl);
   31269       133377 :       newtype = cxx_make_type (TREE_CODE (oldtype));
   31270       133377 :       TYPE_MAIN_VARIANT (newtype) = newtype;
   31271       133377 :     }
   31272              :   else
   31273              :     {
   31274        37347 :       newtype = TREE_TYPE (olddecl);
   31275        37347 :       if (type_uses_auto (newtype))
   31276              :         {
   31277              :           // Substitute once to fix references to other template parameters.
   31278            6 :           newtype = tsubst (newtype, tsubst_args,
   31279              :                             complain|tf_partial, NULL_TREE);
   31280              :           // Now substitute again to reduce the level of the auto.
   31281            6 :           newtype = tsubst (newtype, current_template_args (),
   31282              :                             complain, NULL_TREE);
   31283              :         }
   31284              :       else
   31285        37341 :         newtype = tsubst (newtype, tsubst_args,
   31286              :                           complain, NULL_TREE);
   31287              :     }
   31288              : 
   31289       170724 :   tree newdecl
   31290       170724 :     = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
   31291       170724 :                   DECL_NAME (olddecl), newtype);
   31292       170724 :   SET_DECL_TEMPLATE_PARM_P (newdecl);
   31293              : 
   31294       170724 :   tree newidx;
   31295       170724 :   if (TREE_CODE (olddecl) == TYPE_DECL
   31296        37382 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   31297              :     {
   31298       133377 :       newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
   31299       133377 :         = build_template_parm_index (index, level, level,
   31300              :                                      newdecl, newtype);
   31301       266754 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   31302       133377 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   31303       133377 :       TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
   31304              : 
   31305       133377 :       if (TREE_CODE (olddecl) == TEMPLATE_DECL)
   31306              :         {
   31307           35 :           tree newresult
   31308           35 :             = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
   31309           35 :                                    DECL_NAME (olddecl), newtype);
   31310           35 :           DECL_ARTIFICIAL (newresult) = true;
   31311           35 :           DECL_TEMPLATE_RESULT (newdecl) = newresult;
   31312              :           // First create a copy (ttargs) of tsubst_args with an
   31313              :           // additional level for the template template parameter's own
   31314              :           // template parameters (ttparms).
   31315           35 :           tree ttparms = (INNERMOST_TEMPLATE_PARMS
   31316           35 :                           (DECL_TEMPLATE_PARMS (olddecl)));
   31317           70 :           const int depth = TMPL_ARGS_DEPTH (tsubst_args);
   31318           35 :           tree ttargs = make_tree_vec (depth + 1);
   31319           91 :           for (int i = 0; i < depth; ++i)
   31320          112 :             TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
   31321           35 :           TREE_VEC_ELT (ttargs, depth)
   31322           35 :             = template_parms_level_to_args (ttparms);
   31323              :           // Substitute ttargs into ttparms to fix references to
   31324              :           // other template parameters.
   31325           35 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   31326              :                                                  complain|tf_partial);
   31327              :           // Now substitute again with args based on tparms, to reduce
   31328              :           // the level of the ttparms.
   31329           35 :           ttargs = current_template_args ();
   31330           35 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   31331              :                                                  complain);
   31332              :           // Finally, tack the adjusted parms onto tparms.
   31333           35 :           ttparms = tree_cons (size_int (level + 1), ttparms,
   31334              :                                copy_node (current_template_parms));
   31335              :           // As with all template template parms, the parameter list captured
   31336              :           // by this template template parm that corresponds to its own level
   31337              :           // should be empty.  This avoids infinite recursion when structurally
   31338              :           // comparing two such rewritten template template parms (PR102479).
   31339           35 :           gcc_assert (!TREE_VEC_LENGTH
   31340              :                       (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
   31341           35 :           gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
   31342           35 :           TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
   31343              :           // All done.
   31344           35 :           DECL_TEMPLATE_PARMS (newdecl) = ttparms;
   31345           35 :           DECL_TEMPLATE_INFO (newresult)
   31346           70 :             = build_template_info (newdecl, template_parms_to_args (ttparms));
   31347              :         }
   31348              : 
   31349       133377 :       if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
   31350            0 :         SET_TYPE_STRUCTURAL_EQUALITY (newtype);
   31351              :       else
   31352       133377 :         TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
   31353              :     }
   31354              :   else
   31355              :     {
   31356        37347 :       tree oldconst = TEMPLATE_PARM_DECL (oldidx);
   31357        37347 :       tree newconst
   31358        37347 :         = build_decl (DECL_SOURCE_LOCATION (oldconst),
   31359        37347 :                       TREE_CODE (oldconst),
   31360        37347 :                       DECL_NAME (oldconst), newtype);
   31361       149388 :       TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
   31362        37347 :         = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
   31363        37347 :       SET_DECL_TEMPLATE_PARM_P (newconst);
   31364        37347 :       newidx = build_template_parm_index (index, level, level,
   31365              :                                           newconst, newtype);
   31366        74694 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   31367        37347 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   31368        37347 :       DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
   31369              :     }
   31370              : 
   31371              :   return newdecl;
   31372              : }
   31373              : 
   31374              : /* As rewrite_template_parm, but for the whole TREE_LIST representing a
   31375              :    template parameter.  */
   31376              : 
   31377              : static tree
   31378       170727 : rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
   31379              :                     tree targs, unsigned targs_index, tsubst_flags_t complain)
   31380              : {
   31381       170727 :   tree olddecl = TREE_VALUE (oldelt);
   31382       170727 :   tree newdecl = rewrite_template_parm (olddecl, index, level,
   31383              :                                         targs, complain);
   31384       170727 :   if (newdecl == error_mark_node)
   31385              :     return error_mark_node;
   31386       170724 :   tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
   31387              :                                      targs, complain, NULL_TREE);
   31388       170724 :   tree list = build_tree_list (newdef, newdecl);
   31389       341448 :   TEMPLATE_PARM_CONSTRAINTS (list)
   31390       170724 :     = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
   31391              :                               targs, complain, NULL_TREE);
   31392       341448 :   int depth = TMPL_ARGS_DEPTH (targs);
   31393       341448 :   TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
   31394       170724 :   return list;
   31395              : }
   31396              : 
   31397              : /* Returns a C++17 class deduction guide template based on the constructor
   31398              :    CTOR.  As a special case, CTOR can be a RECORD_TYPE for an implicit default
   31399              :    guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
   31400              :    aggregate initialization guide.  OUTER_ARGS are the template arguments
   31401              :    for the enclosing scope of the class.  */
   31402              : 
   31403              : static tree
   31404       152671 : build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
   31405              : {
   31406       152671 :   tree tparms, targs, fparms, fargs, ci;
   31407       152671 :   bool memtmpl = false;
   31408       152671 :   bool explicit_p;
   31409       152671 :   location_t loc;
   31410       152671 :   tree fn_tmpl = NULL_TREE;
   31411              : 
   31412       152671 :   if (outer_args)
   31413              :     {
   31414          931 :       ++processing_template_decl;
   31415          931 :       type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
   31416          931 :       --processing_template_decl;
   31417              :     }
   31418              : 
   31419       152671 :   if (!DECL_DECLARES_FUNCTION_P (ctor))
   31420              :     {
   31421        14586 :       if (TYPE_P (ctor))
   31422              :         {
   31423        13314 :           bool copy_p = TYPE_REF_P (ctor);
   31424        13314 :           if (copy_p)
   31425        12681 :             fparms = tree_cons (NULL_TREE, type, void_list_node);
   31426              :           else
   31427          633 :             fparms = void_list_node;
   31428              :         }
   31429         1272 :       else if (TREE_CODE (ctor) == TREE_LIST)
   31430              :         fparms = ctor;
   31431              :       else
   31432            0 :         gcc_unreachable ();
   31433              : 
   31434        14586 :       tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
   31435        14586 :       tparms = DECL_TEMPLATE_PARMS (ctmpl);
   31436        14586 :       targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   31437        14586 :       ci = NULL_TREE;
   31438        14586 :       fargs = NULL_TREE;
   31439        14586 :       loc = DECL_SOURCE_LOCATION (ctmpl);
   31440        14586 :       explicit_p = false;
   31441              :     }
   31442              :   else
   31443              :     {
   31444       138085 :       ++processing_template_decl;
   31445       138085 :       bool ok = true;
   31446              : 
   31447       138085 :       complain |= tf_dguide;
   31448              : 
   31449       138085 :       fn_tmpl
   31450       138085 :         = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
   31451        34914 :            : DECL_TI_TEMPLATE (ctor));
   31452       138085 :       if (outer_args)
   31453          737 :         fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
   31454       138085 :       ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
   31455              : 
   31456       138085 :       tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
   31457              :       /* If type is a member class template, DECL_TI_ARGS (ctor) will have
   31458              :          fully specialized args for the enclosing class.  Strip those off, as
   31459              :          the deduction guide won't have those template parameters.  */
   31460       276170 :       targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
   31461       138085 :                                                 TMPL_PARMS_DEPTH (tparms));
   31462              :       /* Discard the 'this' parameter.  */
   31463       138085 :       fparms = FUNCTION_ARG_CHAIN (ctor);
   31464       138085 :       fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
   31465       138085 :       ci = get_constraints (ctor);
   31466       138085 :       loc = DECL_SOURCE_LOCATION (ctor);
   31467       138085 :       explicit_p = DECL_NONCONVERTING_P (ctor);
   31468              : 
   31469       138085 :       if (PRIMARY_TEMPLATE_P (fn_tmpl))
   31470              :         {
   31471       103171 :           memtmpl = true;
   31472              : 
   31473              :           /* For a member template constructor, we need to flatten the two
   31474              :              template parameter lists into one, and then adjust the function
   31475              :              signature accordingly.  This gets...complicated.  */
   31476       103171 :           tree save_parms = current_template_parms;
   31477              : 
   31478              :           /* For a member template we should have two levels of parms/args, one
   31479              :              for the class and one for the constructor.  We stripped
   31480              :              specialized args for further enclosing classes above.  */
   31481       103171 :           const int depth = 2;
   31482       206342 :           gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
   31483              : 
   31484              :           /* Template args for translating references to the two-level template
   31485              :              parameters into references to the one-level template parameters we
   31486              :              are creating.  */
   31487       103171 :           tree tsubst_args = copy_node (targs);
   31488       103171 :           TMPL_ARGS_LEVEL (tsubst_args, depth)
   31489       206342 :             = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
   31490              : 
   31491              :           /* Template parms for the constructor template.  */
   31492       103171 :           tree ftparms = TREE_VALUE (tparms);
   31493       103171 :           unsigned flen = TREE_VEC_LENGTH (ftparms);
   31494              :           /* Template parms for the class template.  */
   31495       103171 :           tparms = TREE_CHAIN (tparms);
   31496       103171 :           tree ctparms = TREE_VALUE (tparms);
   31497       103171 :           unsigned clen = TREE_VEC_LENGTH (ctparms);
   31498              :           /* Template parms for the deduction guide start as a copy of the
   31499              :              template parms for the class.  We set current_template_parms for
   31500              :              lookup_template_class_1.  */
   31501       103171 :           current_template_parms = tparms = copy_node (tparms);
   31502       103171 :           tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
   31503       274304 :           for (unsigned i = 0; i < clen; ++i)
   31504       171133 :             TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
   31505              : 
   31506              :           /* Now we need to rewrite the constructor parms to append them to the
   31507              :              class parms.  */
   31508       272714 :           for (unsigned i = 0; i < flen; ++i)
   31509              :             {
   31510       169543 :               unsigned index = i + clen;
   31511       169543 :               unsigned level = 1;
   31512       169543 :               tree oldelt = TREE_VEC_ELT (ftparms, i);
   31513       169543 :               tree newelt
   31514       169543 :                 = rewrite_tparm_list (oldelt, index, level,
   31515              :                                       tsubst_args, i, complain);
   31516       169543 :               if (newelt == error_mark_node)
   31517            3 :                 ok = false;
   31518       169543 :               TREE_VEC_ELT (new_vec, index) = newelt;
   31519              :             }
   31520              : 
   31521              :           /* Now we have a final set of template parms to substitute into the
   31522              :              function signature.  */
   31523       103171 :           targs = template_parms_to_args (tparms);
   31524       103171 :           fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
   31525              :                                      complain, ctor);
   31526       103171 :           if (fparms == error_mark_node)
   31527            0 :             ok = false;
   31528       103171 :           if (ci)
   31529              :             {
   31530       101680 :               if (outer_args)
   31531              :                 /* FIXME: We'd like to avoid substituting outer template
   31532              :                    arguments into the constraint ahead of time, but the
   31533              :                    construction of tsubst_args assumes that outer arguments
   31534              :                    are already substituted in.  */
   31535          426 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   31536       101680 :               ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
   31537              :             }
   31538              : 
   31539              :           /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
   31540              :              cp_unevaluated_operand.  */
   31541       103171 :           cp_evaluated ev;
   31542       103171 :           fargs = tsubst (fargs, tsubst_args, complain, ctor);
   31543       103171 :           current_template_parms = save_parms;
   31544       103171 :         }
   31545              :       else
   31546              :         {
   31547              :           /* Substitute in the same arguments to rewrite class members into
   31548              :              references to members of an unknown specialization.  */
   31549        34914 :           cp_evaluated ev;
   31550        34914 :           fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
   31551        34914 :          if (fparms == error_mark_node)
   31552            3 :            ok = false;
   31553        34914 :           fargs = tsubst (fargs, targs, complain, ctor);
   31554        34914 :           if (ci)
   31555              :             {
   31556        20973 :               if (outer_args)
   31557              :                 /* FIXME: As above.  */
   31558            6 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   31559        20973 :               ci = tsubst_constraint_info (ci, targs, complain, ctor);
   31560              :             }
   31561        34914 :         }
   31562              : 
   31563       138085 :       --processing_template_decl;
   31564       138085 :       if (!ok)
   31565            6 :         return error_mark_node;
   31566              :     }
   31567              : 
   31568       152665 :   if (!memtmpl)
   31569              :     {
   31570              :       /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE.  */
   31571        49497 :       tparms = copy_node (tparms);
   31572        49497 :       INNERMOST_TEMPLATE_PARMS (tparms)
   31573        49497 :         = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
   31574              :     }
   31575              : 
   31576       152665 :   tree fntype = cp_build_function_type (type, fparms);
   31577       152665 :   tree ded_fn = build_lang_decl_loc (loc,
   31578              :                                      FUNCTION_DECL,
   31579              :                                      dguide_name (type), fntype);
   31580       152665 :   DECL_ARGUMENTS (ded_fn) = fargs;
   31581       152665 :   DECL_ARTIFICIAL (ded_fn) = true;
   31582       152665 :   DECL_NONCONVERTING_P (ded_fn) = explicit_p;
   31583       152665 :   tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
   31584       152665 :   DECL_ARTIFICIAL (ded_tmpl) = true;
   31585       152665 :   DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
   31586       152665 :   DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
   31587       152665 :   if (DECL_P (ctor))
   31588       138079 :     DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
   31589       152665 :   if (ci)
   31590       122653 :     set_constraints (ded_tmpl, ci);
   31591              : 
   31592              :   return ded_tmpl;
   31593              : }
   31594              : 
   31595              : /* Add to LIST the member types for the reshaped initializer CTOR.  */
   31596              : 
   31597              : static tree
   31598         2101 : collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
   31599              : {
   31600         2101 :   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
   31601         2101 :   tree idx, val; unsigned i;
   31602         8467 :   FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
   31603              :     {
   31604         2412 :       tree ftype = elt ? elt : TREE_TYPE (idx);
   31605          881 :       if (BRACE_ENCLOSED_INITIALIZER_P (val)
   31606         7247 :           && CONSTRUCTOR_BRACES_ELIDED_P (val))
   31607              :         {
   31608          831 :           tree subelt = NULL_TREE;
   31609          831 :           if (TREE_CODE (ftype) == ARRAY_TYPE)
   31610          825 :             subelt = TREE_TYPE (ftype);
   31611          831 :           list = collect_ctor_idx_types (val, list, subelt);
   31612          831 :           continue;
   31613          831 :         }
   31614         5535 :       tree arg = NULL_TREE;
   31615         5535 :       if (i == v->length() - 1
   31616         5535 :           && PACK_EXPANSION_P (ftype))
   31617              :         /* Give the trailing pack expansion parameter a default argument to
   31618              :            match aggregate initialization behavior, even if we deduce the
   31619              :            length of the pack separately to more than we have initializers. */
   31620           16 :         arg = build_constructor (init_list_type_node, NULL);
   31621              :       /* if ei is of array type and xi is a braced-init-list or string literal,
   31622              :          Ti is an rvalue reference to the declared type of ei */
   31623         5535 :       STRIP_ANY_LOCATION_WRAPPER (val);
   31624         5535 :       if (TREE_CODE (ftype) == ARRAY_TYPE
   31625         5535 :           && (BRACE_ENCLOSED_INITIALIZER_P (val)
   31626            6 :               || TREE_CODE (val) == STRING_CST))
   31627              :         {
   31628           33 :           if (TREE_CODE (val) == STRING_CST)
   31629            6 :             ftype = cp_build_qualified_type
   31630            6 :               (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
   31631           33 :           ftype = (cp_build_reference_type
   31632           39 :                    (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
   31633              :         }
   31634         5535 :       list = tree_cons (arg, ftype, list);
   31635              :     }
   31636              : 
   31637         2101 :   return list;
   31638              : }
   31639              : 
   31640              : /* Return whether ETYPE is, or is derived from, a specialization of TMPL.  */
   31641              : 
   31642              : static bool
   31643         3829 : is_spec_or_derived (tree etype, tree tmpl)
   31644              : {
   31645         3829 :   if (!etype || !CLASS_TYPE_P (etype))
   31646              :     return false;
   31647              : 
   31648         1608 :   etype = cv_unqualified (etype);
   31649         1608 :   tree type = TREE_TYPE (tmpl);
   31650         1608 :   tree tparms = (INNERMOST_TEMPLATE_PARMS
   31651         1608 :                  (DECL_TEMPLATE_PARMS (tmpl)));
   31652         1608 :   tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   31653         1608 :   int err = unify (tparms, targs, type, etype,
   31654              :                    UNIFY_ALLOW_DERIVED, /*explain*/false);
   31655         1608 :   ggc_free (targs);
   31656         1608 :   return !err;
   31657              : }
   31658              : 
   31659              : /* Return a C++20 aggregate deduction candidate for TYPE initialized from
   31660              :    INIT.  */
   31661              : 
   31662              : static tree
   31663         5996 : maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
   31664              : {
   31665         5996 :   if (cxx_dialect < cxx20)
   31666              :     return NULL_TREE;
   31667              : 
   31668         5824 :   if (init == NULL_TREE)
   31669              :     return NULL_TREE;
   31670              : 
   31671         5755 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   31672              :     {
   31673          137 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   31674          137 :       tree tinfo = get_template_info (under);
   31675          137 :       if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
   31676            3 :         return alias_ctad_tweaks (tmpl, guide);
   31677              :       return NULL_TREE;
   31678              :     }
   31679              : 
   31680              :   /* We might be creating a guide for a class member template, e.g.,
   31681              : 
   31682              :        template<typename U> struct A {
   31683              :          template<typename T> struct B { T t; };
   31684              :        };
   31685              : 
   31686              :      At this point, A will have been instantiated.  Below, we need to
   31687              :      use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types.  */
   31688         5618 :   const bool member_template_p
   31689         5618 :     = (DECL_TEMPLATE_INFO (tmpl)
   31690         5618 :        && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
   31691         5618 :   tree type = TREE_TYPE (tmpl);
   31692         5618 :   tree template_type = (member_template_p
   31693         5618 :                         ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
   31694         1071 :                         : type);
   31695         5618 :   if (!CP_AGGREGATE_TYPE_P (template_type))
   31696              :     return NULL_TREE;
   31697              : 
   31698              :   /* No aggregate candidate for copy-initialization.  */
   31699         1482 :   if (args->length() == 1)
   31700              :     {
   31701         1400 :       tree val = (*args)[0];
   31702         1400 :       if (is_spec_or_derived (TREE_TYPE (val), tmpl))
   31703              :         return NULL_TREE;
   31704              :     }
   31705              : 
   31706              :   /* If we encounter a problem, we just won't add the candidate.  */
   31707         1376 :   tsubst_flags_t complain = tf_none;
   31708              : 
   31709         1376 :   tree parms = NULL_TREE;
   31710         1376 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   31711              :     {
   31712         1281 :       init = reshape_init (template_type, init, complain);
   31713         1281 :       if (init == error_mark_node)
   31714              :         return NULL_TREE;
   31715         1270 :       parms = collect_ctor_idx_types (init, parms);
   31716              :     }
   31717           95 :   else if (TREE_CODE (init) == TREE_LIST)
   31718              :     {
   31719           93 :       int len = list_length (init);
   31720          113 :       for (tree binfo : BINFO_BASE_BINFOS (TYPE_BINFO (template_type)))
   31721              :         {
   31722           20 :           if (!len)
   31723              :             break;
   31724           20 :           parms = tree_cons (NULL_TREE, BINFO_TYPE (binfo), parms);
   31725           20 :           --len;
   31726              :         }
   31727           93 :       for (tree field = TYPE_FIELDS (template_type);
   31728          315 :            len;
   31729          222 :            --len, field = DECL_CHAIN (field))
   31730              :         {
   31731          225 :           field = next_aggregate_field (field);
   31732          225 :           if (!field)
   31733              :             return NULL_TREE;
   31734          222 :           tree ftype = finish_decltype_type (field, true, complain);
   31735          222 :           parms = tree_cons (NULL_TREE, ftype, parms);
   31736              :         }
   31737              :     }
   31738              :   else
   31739              :     /* Aggregate initialization doesn't apply to an initializer expression.  */
   31740              :     return NULL_TREE;
   31741              : 
   31742              :   /* If we're creating a deduction guide for a member class template,
   31743              :      we've used the original template pattern type for the reshape_init
   31744              :      above; this is done because we want PARMS to be a template parameter
   31745              :      type, something that can be deduced when used as a function template
   31746              :      parameter.  At this point the outer class template has already been
   31747              :      partially instantiated (we deferred the deduction until the enclosing
   31748              :      scope is non-dependent).  Therefore we have to partially instantiate
   31749              :      PARMS, so that its template level is properly reduced and we don't get
   31750              :      mismatches when deducing types using the guide with PARMS.  */
   31751         1360 :   if (member_template_p)
   31752              :     {
   31753           26 :       ++processing_template_decl;
   31754           26 :       parms = tsubst (parms, outer_template_args (tmpl), complain, init);
   31755           26 :       --processing_template_decl;
   31756              :     }
   31757              : 
   31758         1360 :   if (parms)
   31759              :     {
   31760         1272 :       tree last = parms;
   31761         1272 :       parms = nreverse (parms);
   31762         1272 :       TREE_CHAIN (last) = void_list_node;
   31763         1272 :       tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
   31764         1272 :       return guide;
   31765              :     }
   31766              : 
   31767              :   return NULL_TREE;
   31768              : }
   31769              : 
   31770              : /* UGUIDES are the deduction guides for the underlying template of alias
   31771              :    template TMPL; adjust them to be deduction guides for TMPL.
   31772              : 
   31773              :    This routine also handles C++23 inherited CTAD, in which case TMPL is a
   31774              :    TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
   31775              :    the template parameter list of the alias template (equivalently, of the
   31776              :    derived class) and TREE_VALUE the defining-type-id (equivalently, the
   31777              :    base whose guides we're inheriting).  UGUIDES are the base's guides.  */
   31778              : 
   31779              : static tree
   31780          166 : alias_ctad_tweaks (tree tmpl, tree uguides)
   31781              : {
   31782              :   /* [over.match.class.deduct]: When resolving a placeholder for a deduced
   31783              :      class type (9.2.8.2) where the template-name names an alias template A,
   31784              :      the defining-type-id of A must be of the form
   31785              : 
   31786              :      typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   31787              : 
   31788              :      as specified in 9.2.8.2. The guides of A are the set of functions or
   31789              :      function templates formed as follows. For each function or function
   31790              :      template f in the guides of the template named by the simple-template-id
   31791              :      of the defining-type-id, the template arguments of the return type of f
   31792              :      are deduced from the defining-type-id of A according to the process in
   31793              :      13.10.2.5 with the exception that deduction does not fail if not all
   31794              :      template arguments are deduced. Let g denote the result of substituting
   31795              :      these deductions into f. If substitution succeeds, form a function or
   31796              :      function template f' with the following properties and add it to the set
   31797              :      of guides of A:
   31798              : 
   31799              :      * The function type of f' is the function type of g.
   31800              : 
   31801              :      * If f is a function template, f' is a function template whose template
   31802              :      parameter list consists of all the template parameters of A (including
   31803              :      their default template arguments) that appear in the above deductions or
   31804              :      (recursively) in their default template arguments, followed by the
   31805              :      template parameters of f that were not deduced (including their default
   31806              :      template arguments), otherwise f' is not a function template.
   31807              : 
   31808              :      * The associated constraints (13.5.2) are the conjunction of the
   31809              :      associated constraints of g and a constraint that is satisfied if and only
   31810              :      if the arguments of A are deducible (see below) from the return type.
   31811              : 
   31812              :      * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
   31813              :      be so as well.
   31814              : 
   31815              :      * If f was generated from a deduction-guide (12.4.1.8), then f' is
   31816              :      considered to be so as well.
   31817              : 
   31818              :      * The explicit-specifier of f' is the explicit-specifier of g (if
   31819              :      any).  */
   31820              : 
   31821          166 :   enum { alias, inherited } ctad_kind;
   31822          166 :   tree atype, fullatparms, utype, name;
   31823          166 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   31824              :     {
   31825          120 :       ctad_kind = alias;
   31826          120 :       atype = TREE_TYPE (tmpl);
   31827          120 :       fullatparms = DECL_TEMPLATE_PARMS (tmpl);
   31828          120 :       utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   31829          120 :       name = dguide_name (tmpl);
   31830              :     }
   31831              :   else
   31832              :     {
   31833           46 :       ctad_kind = inherited;
   31834           46 :       atype = NULL_TREE;
   31835           46 :       fullatparms = TREE_PURPOSE (tmpl);
   31836           46 :       utype = TREE_VALUE (tmpl);
   31837           46 :       name = dguide_name (TPARMS_PRIMARY_TEMPLATE
   31838              :                           (INNERMOST_TEMPLATE_PARMS (fullatparms)));
   31839              :     }
   31840              : 
   31841          166 :   tsubst_flags_t complain = tf_partial;
   31842          166 :   tree aguides = NULL_TREE;
   31843          166 :   tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
   31844          166 :   unsigned natparms = TREE_VEC_LENGTH (atparms);
   31845          867 :   for (tree f : lkp_range (uguides))
   31846              :     {
   31847          535 :       tree in_decl = f;
   31848          535 :       location_t loc = DECL_SOURCE_LOCATION (f);
   31849          535 :       tree ret = TREE_TYPE (TREE_TYPE (f));
   31850          535 :       tree fprime = f;
   31851          535 :       if (TREE_CODE (f) == TEMPLATE_DECL)
   31852              :         {
   31853          526 :           processing_template_decl_sentinel ptds (/*reset*/false);
   31854          526 :           ++processing_template_decl;
   31855              : 
   31856              :           /* Deduce template arguments for f from the type-id of A.  */
   31857          526 :           tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
   31858          526 :           unsigned len = TREE_VEC_LENGTH (ftparms);
   31859          526 :           tree targs = make_tree_vec (len);
   31860          526 :           int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
   31861          526 :           if (err)
   31862              :             /* CWG2664: Discard any deductions, still build the guide.  */
   31863           32 :             for (unsigned i = 0; i < len; ++i)
   31864           16 :               TREE_VEC_ELT (targs, i) = NULL_TREE;
   31865              : 
   31866              :           /* The number of parms for f' is the number of parms of A used in
   31867              :              the deduced arguments plus non-deduced parms of f.  */
   31868              :           unsigned ndlen = 0;
   31869              :           unsigned j;
   31870         2038 :           for (unsigned i = 0; i < len; ++i)
   31871         1512 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   31872          191 :               ++ndlen;
   31873          526 :           find_template_parameter_info ftpi (fullatparms);
   31874          526 :           ftpi.find_in_recursive (targs);
   31875          526 :           unsigned nusedatparms = ftpi.num_found ();
   31876          526 :           unsigned nfparms = nusedatparms + ndlen;
   31877          526 :           tree gtparms = make_tree_vec (nfparms);
   31878              : 
   31879              :           /* Set current_template_parms as in build_deduction_guide.  */
   31880          526 :           auto ctp = make_temp_override (current_template_parms);
   31881          526 :           current_template_parms = copy_node (fullatparms);
   31882          526 :           TREE_VALUE (current_template_parms) = gtparms;
   31883              : 
   31884          526 :           j = 0;
   31885          526 :           unsigned level = 1;
   31886              : 
   31887              :           /* First copy over the used parms of A.  */
   31888          526 :           tree atargs = make_tree_vec (natparms);
   31889         1592 :           for (unsigned i = 0; i < natparms; ++i)
   31890              :             {
   31891         1066 :               tree elt = TREE_VEC_ELT (atparms, i);
   31892         1066 :               if (ftpi.found (elt))
   31893              :                 {
   31894          993 :                   unsigned index = j++;
   31895          993 :                   tree nelt = rewrite_tparm_list (elt, index, level,
   31896              :                                                   atargs, i, complain);
   31897          993 :                   TREE_VEC_ELT (gtparms, index) = nelt;
   31898              :                 }
   31899              :             }
   31900          526 :           gcc_checking_assert (j == nusedatparms);
   31901              : 
   31902              :           /* Adjust the deduced template args for f to refer to the A parms
   31903              :              with their new indexes.  */
   31904          526 :           if (nusedatparms && nusedatparms != natparms)
   31905           18 :             targs = tsubst_template_args (targs, atargs, complain, in_decl);
   31906              : 
   31907              :           /* Now rewrite the non-deduced parms of f.  */
   31908         1308 :           for (unsigned i = 0; ndlen && i < len; ++i)
   31909          782 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   31910              :               {
   31911          191 :                 --ndlen;
   31912          191 :                 unsigned index = j++;
   31913          191 :                 tree oldlist = TREE_VEC_ELT (ftparms, i);
   31914          191 :                 tree list = rewrite_tparm_list (oldlist, index, level,
   31915              :                                                 targs, i, complain);
   31916          191 :                 TREE_VEC_ELT (gtparms, index) = list;
   31917              :               }
   31918          526 :           gtparms = build_tree_list (size_one_node, gtparms);
   31919              : 
   31920              :           /* Substitute the deduced arguments plus the rewritten template
   31921              :              parameters into f to get g.  This covers the type, copyness,
   31922              :              guideness, and explicit-specifier.  */
   31923          526 :           tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain,
   31924              :                                /*use_spec_table=*/false);
   31925          526 :           if (g == error_mark_node)
   31926            3 :             continue;
   31927          523 :           DECL_NAME (g) = name;
   31928          523 :           if (nfparms == 0)
   31929              :             {
   31930              :               /* The targs are all non-dependent, so g isn't a template.  */
   31931           26 :               fprime = g;
   31932           26 :               ret = TREE_TYPE (TREE_TYPE (fprime));
   31933           26 :               goto non_template;
   31934              :             }
   31935          497 :           DECL_USE_TEMPLATE (g) = 0;
   31936          497 :           fprime = build_template_decl (g, gtparms, false);
   31937          497 :           DECL_TEMPLATE_RESULT (fprime) = g;
   31938          497 :           TREE_TYPE (fprime) = TREE_TYPE (g);
   31939          497 :           tree gtargs = template_parms_to_args (gtparms);
   31940          497 :           DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
   31941          497 :           DECL_PRIMARY_TEMPLATE (fprime) = fprime;
   31942              : 
   31943              :           /* Substitute the associated constraints.  */
   31944          497 :           tree ci = get_constraints (f);
   31945          497 :           if (ci)
   31946              :             {
   31947          119 :               if (tree outer_targs = outer_template_args (f))
   31948            6 :                 ci = tsubst_constraint_info (ci, outer_targs,
   31949              :                                              complain & ~tf_partial, in_decl);
   31950          119 :               ci = tsubst_constraint_info (ci, targs, complain, in_decl);
   31951              :             }
   31952          497 :           if (ci == error_mark_node)
   31953            0 :             continue;
   31954              : 
   31955              :           /* Add a constraint that the return type matches the instantiation of
   31956              :              A with the same template arguments.  */
   31957          497 :           ret = TREE_TYPE (TREE_TYPE (fprime));
   31958          497 :           if (ctad_kind == alias
   31959              :               /* Use template_args_equal instead of same_type_p to get the
   31960              :                  comparing_dependent_aliases behavior.  */
   31961          497 :               && !template_args_equal (atype, ret))
   31962              :             {
   31963           70 :               tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
   31964           70 :               ci = append_constraint (ci, same);
   31965              :             }
   31966              : 
   31967          497 :           if (ci)
   31968              :             {
   31969          177 :               remove_constraints (fprime);
   31970          177 :               set_constraints (fprime, ci);
   31971              :             }
   31972         1052 :         }
   31973              :       else
   31974              :         {
   31975              :           /* For a non-template deduction guide, if the arguments of A aren't
   31976              :              deducible from the return type, don't add the candidate.  */
   31977            9 :         non_template:
   31978           38 :           if (ctad_kind == alias
   31979           35 :               && !type_targs_deducible_from (tmpl, ret))
   31980            3 :             continue;
   31981              :         }
   31982              : 
   31983              :       /* Rewrite the return type of the inherited guide in terms of the
   31984              :          derived class.  This is specified as replacing the return type R
   31985              :          with typename CC<R>::type where the partially specialized CC maps a
   31986              :          base class specialization to a specialization of the derived class
   31987              :          having such a base (inducing substitution failure if no such derived
   31988              :          class exists).
   31989              : 
   31990              :          As specified this mapping would be done at instantiation time using
   31991              :          non-dependent template arguments, but we do it ahead of time using
   31992              :          the generic arguments.  This seems to be good enough since generic
   31993              :          deduction should succeed only if concrete deduction would.  */
   31994          529 :       if (ctad_kind == inherited)
   31995              :         {
   31996          224 :           processing_template_decl_sentinel ptds (/*reset*/false);
   31997          224 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   31998          202 :             ++processing_template_decl;
   31999              : 
   32000          224 :           tree targs = type_targs_deducible_from (tmpl, ret);
   32001          224 :           if (!targs)
   32002           10 :             continue;
   32003              : 
   32004          214 :           if (TREE_CODE (f) != TEMPLATE_DECL)
   32005            4 :             fprime = copy_decl (fprime);
   32006          214 :           tree fntype = TREE_TYPE (fprime);
   32007          214 :           ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), targs,
   32008              :                                        in_decl, NULL_TREE, complain);
   32009          214 :           fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype),
   32010          214 :                                         TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
   32011          214 :           TREE_TYPE (fprime) = fntype;
   32012          214 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   32013          202 :             TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype;
   32014          214 :           set_inherited_guide_context (fprime, utype);
   32015          224 :         }
   32016              : 
   32017          519 :       aguides = lookup_add (fprime, aguides);
   32018              :     }
   32019              : 
   32020          166 :   return aguides;
   32021              : }
   32022              : 
   32023              : /* CTOR is a using-decl inheriting the constructors of some base of the class
   32024              :    template TMPL; adjust the base's guides be deduction guides for TMPL.  */
   32025              : 
   32026              : static tree
   32027           52 : inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain)
   32028              : {
   32029              :   /* [over.match.class.deduct]: In addition, if C is defined and inherits
   32030              :      constructors ([namespace.udecl]) from a direct base class denoted in the
   32031              :      base-specifier-list by a class-or-decltype B, let A be an alias template
   32032              :      whose template parameter list is that of C and whose defining-type-id is
   32033              :      B.  If A is a deducible template ([dcl.type.simple]), the set contains the
   32034              :      guides of A with the return type R of each guide replaced with typename
   32035              :      CC::type given a class template
   32036              : 
   32037              :      template <typename> class CC;
   32038              : 
   32039              :      whose primary template is not defined and with a single partial
   32040              :      specialization whose template parameter list is that of A and whose
   32041              :      template argument list is a specialization of A with the template argument
   32042              :      list of A ([temp.dep.type]) having a member typedef type designating a
   32043              :      template specialization with the template argument list of A but with C as
   32044              :      the template.  */
   32045              : 
   32046           52 :   tree scope = USING_DECL_SCOPE (ctor);
   32047           52 :   if (TREE_CODE (scope) == TYPENAME_TYPE
   32048           52 :       && (TYPE_IDENTIFIER (TYPE_CONTEXT (scope))
   32049            6 :           == TYPENAME_TYPE_FULLNAME (scope)))
   32050              :     /* Recognize using B<T>::B::B as an inherited constructor.  */
   32051              :     /* FIXME: Also recognize using C::B::B?  We might have to call
   32052              :        resolve_typename_type for that.  */
   32053            4 :     scope = TYPE_CONTEXT (scope);
   32054           50 :   if (!CLASS_TYPE_P (scope)
   32055           50 :       || !CLASSTYPE_TEMPLATE_INFO (scope)
   32056           98 :       || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
   32057              :     return NULL_TREE;
   32058              : 
   32059           46 :   tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope);
   32060           46 :   bool any_dguides_p;
   32061           46 :   tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope),
   32062              :                                        any_dguides_p, complain);
   32063           46 :   return alias_ctad_tweaks (t, uguides);
   32064              : }
   32065              : 
   32066              : /* If template arguments for TMPL can be deduced from TYPE, return
   32067              :    the deduced arguments, otherwise return NULL_TREE.
   32068              :    Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
   32069              :    [over.match.class.deduct].
   32070              : 
   32071              :    This check is specified in terms of partial specialization, so the behavior
   32072              :    should be parallel to that of get_partial_spec_bindings.  */
   32073              : 
   32074              : tree
   32075          316 : type_targs_deducible_from (tree tmpl, tree type)
   32076              : {
   32077          316 :   tree tparms, ttype;
   32078          316 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   32079              :     {
   32080              :       /* If tmpl is a class template, this is trivial: it's deducible if
   32081              :          TYPE is a specialization of TMPL.  */
   32082           92 :       if (DECL_CLASS_TEMPLATE_P (tmpl))
   32083              :         {
   32084            0 :           if (CLASS_TYPE_P (type)
   32085            0 :               && CLASSTYPE_TEMPLATE_INFO (type)
   32086            0 :               && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
   32087            0 :             return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   32088              :           else
   32089              :             return NULL_TREE;
   32090              :         }
   32091              : 
   32092              :       /* Otherwise it's an alias template.  */
   32093           92 :       tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
   32094           92 :       ttype = TREE_TYPE (tmpl);
   32095              :     }
   32096              :   else
   32097              :     {
   32098              :       /* TMPL is a synthetic alias template represented as a TREE_LIST as
   32099              :          per alias_ctad_tweaks.  */
   32100          224 :       tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
   32101          224 :       ttype = TREE_VALUE (tmpl);
   32102          224 :       tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype);
   32103          216 :       if (!ti)
   32104              :         /* TTYPE is a typedef to a template-id.  */
   32105            8 :         ti = TYPE_TEMPLATE_INFO (ttype);
   32106          224 :       tmpl = TI_TEMPLATE (ti);
   32107              :     }
   32108              : 
   32109          316 :   int len = TREE_VEC_LENGTH (tparms);
   32110          316 :   tree targs = make_tree_vec (len);
   32111          316 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   32112              : 
   32113          316 :  again:
   32114          316 :   if (unify (tparms, targs, ttype, type,
   32115              :              UNIFY_ALLOW_NONE, false))
   32116              :     return NULL_TREE;
   32117              : 
   32118              :   /* We don't fail on an undeduced targ the second time through (like
   32119              :      get_partial_spec_bindings) because we're going to try defaults.  */
   32120         1099 :   for (int i =  0; i < len; ++i)
   32121          808 :     if (! TREE_VEC_ELT (targs, i))
   32122              :       {
   32123           56 :         tree tparm = TREE_VEC_ELT (tparms, i);
   32124           56 :         tparm = TREE_VALUE (tparm);
   32125              : 
   32126           56 :         if (!tried_array_deduction
   32127           44 :             && TREE_CODE (tparm) == TYPE_DECL)
   32128              :           {
   32129           34 :             try_array_deduction (tparms, targs, ttype);
   32130           34 :             tried_array_deduction = true;
   32131           34 :             if (TREE_VEC_ELT (targs, i))
   32132            0 :               goto again;
   32133              :           }
   32134              :         /* If the type parameter is a parameter pack, then it will be deduced
   32135              :            to an empty parameter pack.  This is another case that doesn't model
   32136              :            well as partial specialization.  */
   32137           56 :         if (template_parameter_pack_p (tparm))
   32138              :           {
   32139            6 :             tree arg;
   32140            6 :             if (TREE_CODE (tparm) == PARM_DECL)
   32141              :               {
   32142            6 :                 arg = make_node (NONTYPE_ARGUMENT_PACK);
   32143            6 :                 TREE_CONSTANT (arg) = 1;
   32144              :               }
   32145              :             else
   32146            0 :               arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   32147            6 :             ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   32148            6 :             TREE_VEC_ELT (targs, i) = arg;
   32149              :           }
   32150              :       }
   32151              : 
   32152              :   /* Maybe add in default template args.  This seems like a flaw in the
   32153              :      specification in terms of partial specialization, since it says the
   32154              :      partial specialization has the template parameter list of A, but a
   32155              :      partial specialization can't have default targs.  */
   32156          291 :   targs = coerce_template_parms (tparms, targs, tmpl, tf_none);
   32157          291 :   if (targs == error_mark_node)
   32158              :     return NULL_TREE;
   32159              : 
   32160              :   /* I believe we don't need the template_template_parm_bindings_ok_p call
   32161              :      because coerce_template_parms did coerce_template_template_parms.  */
   32162              : 
   32163          265 :   if (!constraints_satisfied_p (tmpl, targs))
   32164              :     return NULL_TREE;
   32165              : 
   32166              :   return targs;
   32167              : }
   32168              : 
   32169              : /* Return artificial deduction guides built from the constructors of class
   32170              :    template TMPL.  */
   32171              : 
   32172              : static tree
   32173        12681 : ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
   32174              : {
   32175        12681 :   tree outer_args = outer_template_args (tmpl);
   32176        12681 :   tree type = TREE_TYPE (most_general_template (tmpl));
   32177              : 
   32178        12681 :   tree cands = NULL_TREE;
   32179              : 
   32180       277186 :   for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
   32181              :     {
   32182              :       /* We handle C++23 inherited CTAD below.  */
   32183       138117 :       if (iter.using_p ())
   32184           32 :         continue;
   32185              : 
   32186       138085 :       tree guide = build_deduction_guide (type, *iter, outer_args, complain);
   32187       138085 :       cands = lookup_add (guide, cands);
   32188              :     }
   32189              : 
   32190        12681 :   if (cxx_dialect >= cxx23)
   32191              :     /* FIXME: CLASSTYPE_CONSTRUCTORS doesn't contain inherited constructors if
   32192              :        e.g. the class also has a user-defined constructor.  So instead iterate
   32193              :        over TYPE_FIELDS manually to robustly find all relevant using-decls.  */
   32194       152271 :     for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
   32195       148892 :       if (TREE_CODE (field) == USING_DECL
   32196       148892 :           && DECL_NAME (field) == ctor_identifier)
   32197              :         {
   32198           52 :           tree uguides = inherited_ctad_tweaks (tmpl, field, complain);
   32199           52 :           if (uguides)
   32200           42 :             cands = lookup_add (uguides, cands);
   32201              :         }
   32202              : 
   32203              :   /* Add implicit default constructor deduction guide.  */
   32204        12681 :   if (!TYPE_HAS_USER_CONSTRUCTOR (type))
   32205              :     {
   32206          633 :       tree guide = build_deduction_guide (type, type, outer_args,
   32207              :                                           complain);
   32208          633 :       cands = lookup_add (guide, cands);
   32209              :     }
   32210              : 
   32211              :   /* Add copy guide.  */
   32212        12681 :   {
   32213        12681 :     tree gtype = build_reference_type (type);
   32214        12681 :     tree guide = build_deduction_guide (type, gtype, outer_args,
   32215              :                                         complain);
   32216        12681 :     cands = lookup_add (guide, cands);
   32217              :   }
   32218              : 
   32219        12681 :   return cands;
   32220              : }
   32221              : 
   32222              : static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
   32223              : 
   32224              : /* Return the non-aggregate deduction guides for deducible template TMPL.  The
   32225              :    aggregate candidate is added separately because it depends on the
   32226              :    initializer.  Set ANY_DGUIDES_P if we find a non-implicit deduction
   32227              :    guide.  */
   32228              : 
   32229              : static tree
   32230        42208 : deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
   32231              : {
   32232        42208 :   tree guides = NULL_TREE;
   32233        42208 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   32234              :     {
   32235          212 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32236          212 :       tree tinfo = get_template_info (under);
   32237          212 :       guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
   32238              :                                      complain);
   32239              :     }
   32240              :   else
   32241              :     {
   32242        41996 :       guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
   32243              :                                       dguide_name (tmpl),
   32244              :                                       LOOK_want::ANY_REACHABLE,
   32245              :                                       /*complain=*/false);
   32246        41996 :       if (guides == error_mark_node)
   32247              :         guides = NULL_TREE;
   32248              :       else
   32249        36093 :         any_dguides_p = true;
   32250              :     }
   32251              : 
   32252              :   /* Cache the deduction guides for a template.  We also remember the result of
   32253              :      lookup, and rebuild everything if it changes; should be very rare.  */
   32254              :   /* FIXME: Also rebuild if this is a class template that inherits guides from a
   32255              :      base class, and lookup for the latter changed.  */
   32256        42208 :   tree_pair_p cache = NULL;
   32257        84416 :   if (tree_pair_p &r
   32258        42208 :       = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
   32259              :     {
   32260        29436 :       cache = r;
   32261        29436 :       if (cache->purpose == guides)
   32262        29410 :         return cache->value;
   32263              :     }
   32264              :   else
   32265              :     {
   32266        12772 :       r = cache = ggc_cleared_alloc<tree_pair_s> ();
   32267        12772 :       cache->purpose = guides;
   32268              :     }
   32269              : 
   32270        12798 :   tree cands = NULL_TREE;
   32271        12798 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   32272          117 :     cands = alias_ctad_tweaks (tmpl, guides);
   32273              :   else
   32274              :     {
   32275        12681 :       cands = ctor_deduction_guides_for (tmpl, complain);
   32276        65260 :       for (lkp_iterator it (guides); it; ++it)
   32277        52579 :         cands = lookup_add (*it, cands);
   32278              :     }
   32279              : 
   32280        12798 :   cache->value = cands;
   32281        12798 :   return cands;
   32282              : }
   32283              : 
   32284              : /* Return whether TMPL is a (class template argument-) deducible template.  */
   32285              : 
   32286              : bool
   32287    874435622 : ctad_template_p (tree tmpl)
   32288              : {
   32289              :   /* A deducible template is either a class template or is an alias template
   32290              :      whose defining-type-id is of the form
   32291              : 
   32292              :       typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   32293              : 
   32294              :      where the nested-name-specifier (if any) is non-dependent and the
   32295              :      template-name of the simple-template-id names a deducible template.  */
   32296              : 
   32297     13887726 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   32298    882495071 :       && IDENTIFIER_TRAIT_P (DECL_NAME (tmpl)))
   32299              :     /* Don't consider CTAD for templates with the same name as a trait; that
   32300              :        is ambiguous with e.g. __is_invocable(_Fn,_Args...).  */
   32301              :     return false;
   32302     13747536 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   32303    880173097 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   32304              :     return true;
   32305    866226267 :   if (!DECL_ALIAS_TEMPLATE_P (tmpl))
   32306              :     return false;
   32307       130110 :   tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32308       130110 :   if (tree tinfo = get_template_info (orig))
   32309        24694 :     return ctad_template_p (TI_TEMPLATE (tinfo));
   32310              :   return false;
   32311              : }
   32312              : 
   32313              : /* Deduce template arguments for the class template placeholder PTYPE for
   32314              :    template TMPL based on the initializer INIT, and return the resulting
   32315              :    type.  */
   32316              : 
   32317              : static tree
   32318       124293 : do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
   32319              :                     int flags, tsubst_flags_t complain)
   32320              : {
   32321              :   /* We should have handled this in the caller.  */
   32322       124293 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   32323              :     return ptype;
   32324              : 
   32325              :   /* If the class was erroneous, don't try to deduce, because that
   32326              :      can generate a lot of diagnostic.  */
   32327       124186 :   if (TREE_TYPE (tmpl)
   32328       124186 :       && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
   32329       248369 :       && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
   32330              :     return ptype;
   32331              : 
   32332              :   /* Wait until the enclosing scope is non-dependent.  */
   32333       248364 :   if (DECL_CLASS_SCOPE_P (tmpl)
   32334       126114 :       && dependent_type_p (DECL_CONTEXT (tmpl)))
   32335              :     return ptype;
   32336              : 
   32337              :   /* Initializing one placeholder from another.  */
   32338       124161 :   if (init
   32339       124027 :       && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
   32340       116369 :           || (TREE_CODE (init) == EXPR_PACK_EXPANSION
   32341            3 :               && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
   32342              :                   == TEMPLATE_PARM_INDEX)))
   32343         7661 :       && is_auto (TREE_TYPE (init))
   32344       131820 :       && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
   32345         7599 :     return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
   32346              : 
   32347       116562 :   if (!ctad_template_p (tmpl))
   32348              :     {
   32349            3 :       if (complain & tf_error)
   32350            3 :         error ("non-deducible template %qT used without template arguments", tmpl);
   32351            3 :       return error_mark_node;
   32352              :     }
   32353       116559 :   else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
   32354              :     {
   32355            7 :       if (complain & tf_error)
   32356              :         {
   32357              :           /* Be permissive with equivalent alias templates.  */
   32358            7 :           tree u = get_underlying_template (tmpl);
   32359            7 :           auto_diagnostic_group d;
   32360            1 :           const enum diagnostics::kind dk = ((u == tmpl)
   32361            7 :                                              ? diagnostics::kind::error
   32362              :                                              : diagnostics::kind::pedwarn);
   32363            7 :           bool complained
   32364            7 :             = emit_diagnostic (dk, input_location, 0,
   32365              :                                "alias template deduction only available "
   32366              :                                "with %<-std=c++20%> or %<-std=gnu++20%>");
   32367            7 :           if (u == tmpl)
   32368            6 :             return error_mark_node;
   32369            1 :           else if (complained)
   32370              :             {
   32371            1 :               inform (input_location, "use %qD directly instead", u);
   32372            1 :               tmpl = u;
   32373              :             }
   32374            7 :         }
   32375              :       else
   32376            0 :         return error_mark_node;
   32377              :     }
   32378              : 
   32379              :   /* Wait until the initializer is non-dependent.  */
   32380       116553 :   if (type_dependent_expression_p (init))
   32381              :     return ptype;
   32382              : 
   32383        41957 :   if (outer_targs)
   32384              :     {
   32385         7843 :       int args_depth = TMPL_ARGS_DEPTH (outer_targs);
   32386         7843 :       int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   32387         7843 :       if (parms_depth > 1)
   32388              :         {
   32389              :           /* Substitute outer arguments into this CTAD template from the
   32390              :              current instantiation.  */
   32391            3 :           int want = std::min (args_depth, parms_depth - 1);
   32392            3 :           outer_targs = strip_innermost_template_args (outer_targs,
   32393              :                                                        args_depth - want);
   32394            3 :           tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
   32395            3 :           if (tmpl == error_mark_node)
   32396              :             return error_mark_node;
   32397              :         }
   32398              :     }
   32399              : 
   32400              :   /* Don't bother with the alias rules for an equivalent template.  */
   32401        41957 :   tmpl = get_underlying_template (tmpl);
   32402              : 
   32403        41957 :   tree type = TREE_TYPE (tmpl);
   32404              : 
   32405        41957 :   bool try_list_cand = false;
   32406        41957 :   bool list_init_p = false;
   32407              : 
   32408        41957 :   releasing_vec rv_args = NULL;
   32409        41957 :   vec<tree,va_gc> *&args = *&rv_args;
   32410        41957 :   if (init == NULL_TREE)
   32411          133 :     args = make_tree_vector ();
   32412        41824 :   else if (BRACE_ENCLOSED_INITIALIZER_P (init))
   32413              :     {
   32414         6467 :       list_init_p = true;
   32415         6467 :       try_list_cand = true;
   32416         6467 :       if (CONSTRUCTOR_NELTS (init) == 1
   32417         5596 :           && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
   32418              :         {
   32419              :           /* As an exception, the first phase in 16.3.1.7 (considering the
   32420              :              initializer list as a single argument) is omitted if the
   32421              :              initializer list consists of a single expression of type cv U,
   32422              :              where U is a specialization of C or a class derived from a
   32423              :              specialization of C.  */
   32424         2429 :           tree elt = CONSTRUCTOR_ELT (init, 0)->value;
   32425         2429 :           if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
   32426           36 :             try_list_cand = false;
   32427              :         }
   32428           36 :       if (try_list_cand || is_std_init_list (type))
   32429         6431 :         args = make_tree_vector_single (init);
   32430              :       else
   32431           36 :         args = make_tree_vector_from_ctor (init);
   32432              :     }
   32433        35357 :   else if (TREE_CODE (init) == TREE_LIST)
   32434         8866 :     args = make_tree_vector_from_list (init);
   32435              :   else
   32436        26491 :     args = make_tree_vector_single (init);
   32437              : 
   32438              :   /* Do this now to avoid problems with erroneous args later on.  */
   32439        41957 :   args = resolve_args (args, complain);
   32440        41957 :   if (args == NULL)
   32441            7 :     return error_mark_node;
   32442              : 
   32443        41950 :   bool any_dguides_p = false;
   32444        41950 :   tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
   32445        41950 :   if (cands == error_mark_node)
   32446              :     return error_mark_node;
   32447              : 
   32448              :   /* Prune explicit deduction guides in copy-initialization context (but
   32449              :      not copy-list-initialization).  */
   32450        41944 :   bool elided = false;
   32451        41944 :   if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
   32452              :     {
   32453       341096 :       for (lkp_iterator iter (cands); !elided && iter; ++iter)
   32454       315728 :         if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   32455        18390 :           elided = true;
   32456              : 
   32457        25368 :       if (elided)
   32458              :         {
   32459              :           /* Found a nonconverting guide, prune the candidates.  */
   32460        18390 :           tree pruned = NULL_TREE;
   32461       313267 :           for (lkp_iterator iter (cands); iter; ++iter)
   32462       294877 :             if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   32463       276407 :               pruned = lookup_add (*iter, pruned);
   32464              : 
   32465        18390 :           cands = pruned;
   32466              :         }
   32467              :     }
   32468              : 
   32469        41944 :   if (!any_dguides_p)
   32470         5859 :     if (tree guide = maybe_aggr_guide (tmpl, init, args))
   32471         1272 :       cands = lookup_add (guide, cands);
   32472              : 
   32473        41944 :   tree fndecl = error_mark_node;
   32474              : 
   32475              :   /* If this is list-initialization and the class has a list guide, first
   32476              :      try deducing from the list as a single argument, as [over.match.list].  */
   32477        41944 :   if (try_list_cand)
   32478              :     {
   32479         6431 :       tree list_cands = NULL_TREE;
   32480       109423 :       for (tree dg : lkp_range (cands))
   32481        96561 :         if (is_list_ctor (dg))
   32482         1279 :           list_cands = lookup_add (dg, list_cands);
   32483         6431 :       if (list_cands)
   32484          922 :         fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
   32485         6431 :       if (fndecl == error_mark_node)
   32486              :         {
   32487              :           /* That didn't work, now try treating the list as a sequence of
   32488              :              arguments.  */
   32489         5847 :           release_tree_vector (args);
   32490         5847 :           args = make_tree_vector_from_ctor (init);
   32491         5847 :           args = resolve_args (args, complain);
   32492         5847 :           if (args == NULL)
   32493            0 :             return error_mark_node;
   32494              :         }
   32495              :     }
   32496              : 
   32497        41944 :   if (elided && !cands)
   32498              :     {
   32499            0 :       error ("cannot deduce template arguments for copy-initialization"
   32500              :              " of %qT, as it has no non-explicit deduction guides or "
   32501              :              "user-declared constructors", type);
   32502            0 :       return error_mark_node;
   32503              :     }
   32504        41944 :   else if (!cands && fndecl == error_mark_node)
   32505              :     {
   32506            0 :       error ("cannot deduce template arguments of %qT, as it has no viable "
   32507              :              "deduction guides", type);
   32508            0 :       return error_mark_node;
   32509              :     }
   32510              : 
   32511        41944 :   if (fndecl == error_mark_node)
   32512        41360 :     fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
   32513              : 
   32514        41944 :   if (fndecl == error_mark_node)
   32515              :     {
   32516          366 :       if (complain & tf_warning_or_error)
   32517              :         {
   32518          192 :           auto_diagnostic_group d;
   32519          192 :           error ("class template argument deduction failed:");
   32520          192 :           perform_dguide_overload_resolution (cands, args, complain);
   32521          192 :           if (elided)
   32522           27 :             inform (input_location, "explicit deduction guides not considered "
   32523              :                     "for copy-initialization");
   32524          192 :         }
   32525          366 :       return error_mark_node;
   32526              :     }
   32527              :   /* [over.match.list]/1: In copy-list-initialization, if an explicit
   32528              :      constructor is chosen, the initialization is ill-formed.  */
   32529        41578 :   else if (flags & LOOKUP_ONLYCONVERTING)
   32530              :     {
   32531        25489 :       if (DECL_NONCONVERTING_P (fndecl))
   32532              :         {
   32533           35 :           if (complain & tf_warning_or_error)
   32534              :             {
   32535              :               // TODO: Pass down location from cp_finish_decl.
   32536           35 :               auto_diagnostic_group d;
   32537           35 :               error ("class template argument deduction for %qT failed: "
   32538              :                      "explicit deduction guide selected in "
   32539              :                      "copy-list-initialization", type);
   32540           35 :               inform (DECL_SOURCE_LOCATION (fndecl),
   32541              :                       "explicit deduction guide declared here");
   32542              : 
   32543           35 :             }
   32544           35 :           return error_mark_node;
   32545              :         }
   32546              :     }
   32547              : 
   32548              :   /* If CTAD succeeded but the type doesn't have any explicit deduction
   32549              :      guides, this deduction might not be what the user intended.  */
   32550        41543 :   if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
   32551              :     {
   32552         5315 :       auto_diagnostic_group d;
   32553         5315 :       if ((!DECL_IN_SYSTEM_HEADER (fndecl)
   32554            6 :            || global_dc->m_warn_system_headers)
   32555         5321 :           && warning (OPT_Wctad_maybe_unsupported,
   32556              :                       "%qT may not intend to support class template argument "
   32557              :                       "deduction", type))
   32558           24 :         inform (input_location, "add a deduction guide to suppress this "
   32559              :                 "warning");
   32560         5315 :     }
   32561              : 
   32562        41543 :   return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
   32563        41543 :                                   cp_type_quals (ptype));
   32564        41957 : }
   32565              : 
   32566              : /* Return true if INIT is an unparenthesized id-expression or an
   32567              :    unparenthesized class member access.  Used for the argument of
   32568              :    decltype(auto).  */
   32569              : 
   32570              : bool
   32571     11490167 : unparenthesized_id_or_class_member_access_p (tree init)
   32572              : {
   32573     11490167 :   STRIP_ANY_LOCATION_WRAPPER (init);
   32574              : 
   32575              :   /* We need to be able to tell '(r)' and 'r' apart (when it's of
   32576              :      reference type).  Only the latter is an id-expression.  */
   32577      1304956 :   if (REFERENCE_REF_P (init)
   32578     12313417 :       && !REF_PARENTHESIZED_P (init))
   32579         3288 :     init = TREE_OPERAND (init, 0);
   32580     11490167 :   return (DECL_P (init)
   32581     11490167 :           || ((TREE_CODE (init) == COMPONENT_REF
   32582     11489700 :                || TREE_CODE (init) == SCOPE_REF)
   32583          235 :               && !REF_PARENTHESIZED_P (init)));
   32584              : }
   32585              : 
   32586              : /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
   32587              :    from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
   32588              :    The CONTEXT determines the context in which auto deduction is performed
   32589              :    and is used to control error diagnostics.  FLAGS are the LOOKUP_* flags.
   32590              : 
   32591              :    OUTER_TARGS is used during template argument deduction (context == adc_unify)
   32592              :    to properly substitute the result.  It's also used in the adc_unify and
   32593              :    adc_requirement contexts to communicate the necessary template arguments
   32594              :    to satisfaction.  OUTER_TARGS is ignored in other contexts.
   32595              : 
   32596              :    Additionally for adc_unify contexts TMPL is the template for which TYPE
   32597              :    is a template parameter type.
   32598              : 
   32599              :    For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
   32600              :    scope may be appended to the list of deduced template arguments prior to
   32601              :    determining constraint satisfaction as appropriate.  */
   32602              : 
   32603              : tree
   32604     29478160 : do_auto_deduction (tree type, tree init, tree auto_node,
   32605              :                    tsubst_flags_t complain /* = tf_warning_or_error */,
   32606              :                    auto_deduction_context context /* = adc_unspecified */,
   32607              :                    tree outer_targs /* = NULL_TREE */,
   32608              :                    int flags /* = LOOKUP_NORMAL */,
   32609              :                    tree tmpl /* = NULL_TREE */)
   32610              : {
   32611     29478160 :   if (type == error_mark_node || init == error_mark_node)
   32612              :     return error_mark_node;
   32613              : 
   32614     29477170 :   if (init && type_dependent_expression_p (init)
   32615     39493759 :       && context != adc_unify)
   32616              :     /* Defining a subset of type-dependent expressions that we can deduce
   32617              :        from ahead of time isn't worth the trouble.  */
   32618              :     return type;
   32619              : 
   32620              :   /* Similarly, we can't deduce from another undeduced decl.  */
   32621     19562297 :   if (init && undeduced_auto_decl (init))
   32622              :     return type;
   32623              : 
   32624              :   /* We may be doing a partial substitution, but we still want to replace
   32625              :      auto_node.  */
   32626     19562286 :   complain &= ~tf_partial;
   32627              : 
   32628     19562286 :   if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
   32629              :     {
   32630              :       /* We don't recurse here because we can't deduce from a nested
   32631              :          initializer_list.  */
   32632         7145 :       if (CONSTRUCTOR_ELTS (init))
   32633        18899 :         for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
   32634              :           {
   32635        12666 :             elt.value = resolve_nondeduced_context (elt.value, complain);
   32636        12666 :             if (!mark_single_function (elt.value, complain))
   32637           15 :               return error_mark_node;
   32638              :           }
   32639              :     }
   32640     19555141 :   else if (init)
   32641              :     {
   32642     19554973 :       init = resolve_nondeduced_context (init, complain);
   32643     19554973 :       if (!mark_single_function (init, complain))
   32644            0 :         return error_mark_node;
   32645              :     }
   32646              : 
   32647              :   /* In C++23, we must deduce the type to int&& for code like
   32648              :        decltype(auto) f(int&& x) { return (x); }
   32649              :      or
   32650              :        auto&& f(int x) { return x; }
   32651              :      so we use treat_lvalue_as_rvalue_p.  But don't do it for
   32652              :        decltype(auto) f(int x) { return x; }
   32653              :      where we should deduce 'int' rather than 'int&&'; transmogrifying
   32654              :      INIT to an rvalue would break that.  */
   32655     19562271 :   tree r;
   32656     19562271 :   if (cxx_dialect >= cxx23
   32657      2928137 :       && context == adc_return_type
   32658       434922 :       && (!AUTO_IS_DECLTYPE (auto_node)
   32659       100958 :           || !unparenthesized_id_or_class_member_access_p (init))
   32660     19997154 :       && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
   32661              :                                         /*return*/true)))
   32662        21752 :     init = r;
   32663              : 
   32664     19562271 :   if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   32665              :     /* C++17 class template argument deduction.  */
   32666       124293 :     return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
   32667              : 
   32668     19437978 :   if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
   32669              :     /* Nothing we can do with this, even in deduction context.  */
   32670              :     return type;
   32671              : 
   32672     19428921 :   location_t loc = cp_expr_loc_or_input_loc (init);
   32673              : 
   32674              :   /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
   32675              :      with either a new invented type template parameter U or, if the
   32676              :      initializer is a braced-init-list (8.5.4), with
   32677              :      std::initializer_list<U>.  */
   32678     19428921 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   32679              :     {
   32680          577 :       if (!DIRECT_LIST_INIT_P (init))
   32681          390 :         type = listify_autos (type, auto_node);
   32682          187 :       else if (CONSTRUCTOR_NELTS (init) == 1)
   32683          181 :         init = CONSTRUCTOR_ELT (init, 0)->value;
   32684              :       else
   32685              :         {
   32686            6 :           if (complain & tf_warning_or_error)
   32687              :             {
   32688            6 :               auto_diagnostic_group d;
   32689            6 :               if (permerror (loc, "direct-list-initialization of "
   32690              :                              "%<auto%> requires exactly one element"))
   32691            6 :                 inform (loc,
   32692              :                         "for deduction to %<std::initializer_list%>, use copy-"
   32693              :                         "list-initialization (i.e. add %<=%> before the %<{%>)");
   32694            6 :             }
   32695            6 :           type = listify_autos (type, auto_node);
   32696              :         }
   32697              :     }
   32698              : 
   32699     19428921 :   if (type == error_mark_node || init == error_mark_node)
   32700              :     return error_mark_node;
   32701              : 
   32702     19428900 :   tree targs;
   32703     19428900 :   if (context == adc_decomp_type
   32704     19428900 :       && auto_node == type
   32705     19428900 :       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
   32706              :     {
   32707              :       /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
   32708              :          and initializer has array type, deduce cv-qualified array type.  */
   32709         3300 :       targs = make_tree_vec (1);
   32710         3300 :       TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
   32711              :     }
   32712     19425600 :   else if (AUTO_IS_DECLTYPE (auto_node))
   32713              :     {
   32714     11389209 :       const bool id = unparenthesized_id_or_class_member_access_p (init);
   32715     11389209 :       tree deduced = finish_decltype_type (init, id, complain);
   32716     11389209 :       deduced = canonicalize_type_argument (deduced, complain);
   32717     11389209 :       if (deduced == error_mark_node)
   32718              :         return error_mark_node;
   32719     11389206 :       targs = make_tree_vec (1);
   32720     11389206 :       TREE_VEC_ELT (targs, 0) = deduced;
   32721              :     }
   32722              :   else
   32723              :     {
   32724      8036391 :       if (error_operand_p (init))
   32725              :         return error_mark_node;
   32726              : 
   32727      8036379 :       tree parms = build_tree_list (NULL_TREE, type);
   32728      8036379 :       tree tparms = make_tree_vec (1);
   32729      8036379 :       TREE_VEC_ELT (tparms, 0)
   32730      8036379 :         = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
   32731              : 
   32732      8036379 :       targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   32733      8036379 :       int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
   32734              :                                        DEDUCE_CALL,
   32735              :                                        NULL, /*explain_p=*/false);
   32736      8036379 :       if (val > 0)
   32737              :         {
   32738           73 :           if (processing_template_decl)
   32739              :             /* Try again at instantiation time.  */
   32740              :             return type;
   32741           64 :           if (type && type != error_mark_node
   32742           64 :               && (complain & tf_error))
   32743              :             /* If type is error_mark_node a diagnostic must have been
   32744              :                emitted by now.  Also, having a mention to '<type error>'
   32745              :                in the diagnostic is not really useful to the user.  */
   32746              :             {
   32747           61 :               if (cfun
   32748           31 :                   && FNDECL_USED_AUTO (current_function_decl)
   32749           12 :                   && (auto_node
   32750           12 :                       == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
   32751           73 :                   && LAMBDA_FUNCTION_P (current_function_decl))
   32752            6 :                 error_at (loc, "unable to deduce lambda return type from %qE",
   32753              :                           init);
   32754              :               else
   32755           55 :                 error_at (loc, "unable to deduce %qT from %qE", type, init);
   32756           61 :               type_unification_real (tparms, targs, parms, &init, 1, 0,
   32757              :                                      DEDUCE_CALL,
   32758              :                                      NULL, /*explain_p=*/true);
   32759              :             }
   32760           64 :           return error_mark_node;
   32761              :         }
   32762              :     }
   32763              : 
   32764              :   /* Check any placeholder constraints against the deduced type. */
   32765     19428812 :   if (processing_template_decl && context == adc_unify)
   32766              :     /* Constraints will be checked after deduction.  */;
   32767     19417583 :   else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
   32768              :     {
   32769     10455673 :       if (context == adc_unify)
   32770              :         /* Simple constrained auto NTTPs should have gotten their constraint
   32771              :            moved to the template's associated constraints.  */
   32772            0 :         gcc_checking_assert (type != auto_node);
   32773              : 
   32774     10455673 :       if (processing_template_decl)
   32775              :         {
   32776           59 :           gcc_checking_assert (context == adc_variable_type
   32777              :                                || context == adc_return_type
   32778              :                                || context == adc_decomp_type);
   32779           59 :           gcc_checking_assert (!type_dependent_expression_p (init));
   32780              :           /* If the constraint is dependent, we need to wait until
   32781              :              instantiation time to resolve the placeholder.  */
   32782           59 :           if (placeholder_type_constraint_dependent_p (constr))
   32783              :             return type;
   32784              :         }
   32785              : 
   32786     10455649 :       if (context == adc_return_type
   32787     10455649 :           || context == adc_variable_type
   32788     10455649 :           || context == adc_decomp_type)
   32789          328 :         if (tree fn = current_function_decl)
   32790          236 :           if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
   32791              :             {
   32792          110 :               outer_targs = DECL_TEMPLATE_INFO (fn)
   32793          110 :                 ? DECL_TI_ARGS (fn) : NULL_TREE;
   32794          122 :               if (LAMBDA_FUNCTION_P (fn))
   32795              :                 {
   32796              :                   /* As in satisfy_declaration_constraints.  */
   32797           12 :                   tree regen_args = lambda_regenerating_args (fn);
   32798           12 :                   if (outer_targs)
   32799            0 :                     outer_targs = add_to_template_args (regen_args, outer_targs);
   32800              :                   else
   32801              :                     outer_targs = regen_args;
   32802              :                 }
   32803              :             }
   32804              : 
   32805     10455649 :       tree full_targs = outer_targs;
   32806     10455649 :       if (context == adc_unify && tmpl)
   32807            0 :         full_targs = add_outermost_template_args (tmpl, full_targs);
   32808     10455649 :       full_targs = add_to_template_args (full_targs, targs);
   32809              : 
   32810              :       /* HACK: Compensate for callers not always communicating all levels of
   32811              :          outer template arguments by filling in the outermost missing levels
   32812              :          with dummy levels before checking satisfaction.  We'll still crash
   32813              :          if the constraint depends on a template argument belonging to one of
   32814              :          these missing levels, but this hack otherwise allows us to handle a
   32815              :          large subset of possible constraints (including all non-dependent
   32816              :          constraints).  */
   32817     10455649 :       if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
   32818     20911511 :                                 - TMPL_ARGS_DEPTH (full_targs)))
   32819              :         {
   32820            0 :           tree dummy_levels = make_tree_vec (missing_levels);
   32821            0 :           for (int i = 0; i < missing_levels; ++i)
   32822            0 :             TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
   32823            0 :           full_targs = add_to_template_args (dummy_levels, full_targs);
   32824              :         }
   32825              : 
   32826     10455649 :       if (!constraints_satisfied_p (auto_node, full_targs))
   32827              :         {
   32828        21425 :           if (complain & tf_warning_or_error)
   32829              :             {
   32830          113 :               auto_diagnostic_group d;
   32831          113 :               switch (context)
   32832              :                 {
   32833            0 :                 case adc_unspecified:
   32834            0 :                 case adc_unify:
   32835            0 :                   error_at (loc, "placeholder constraints not satisfied");
   32836            0 :                   break;
   32837           73 :                 case adc_variable_type:
   32838           73 :                 case adc_decomp_type:
   32839           73 :                   error_at (loc, "deduced initializer does not satisfy "
   32840              :                             "placeholder constraints");
   32841           73 :                   break;
   32842           28 :                 case adc_return_type:
   32843           28 :                   error_at (loc, "deduced return type does not satisfy "
   32844              :                             "placeholder constraints");
   32845           28 :                   break;
   32846           12 :                 case adc_requirement:
   32847           12 :                   error_at (loc, "deduced expression type does not satisfy "
   32848              :                             "placeholder constraints");
   32849           12 :                   break;
   32850              :                 }
   32851          113 :               diagnose_constraints (loc, auto_node, full_targs);
   32852          113 :             }
   32853        21425 :           return error_mark_node;
   32854              :         }
   32855              :     }
   32856              : 
   32857     19407363 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 0)
   32858              :     {
   32859              :       /* Substitute this level-less auto via tsubst by temporarily
   32860              :          overriding its level to 1.  */
   32861        93180 :       TEMPLATE_TYPE_LEVEL (auto_node) = 1;
   32862        93180 :       type = tsubst (type, targs, complain, NULL_TREE);
   32863        93180 :       TEMPLATE_TYPE_LEVEL (auto_node) = 0;
   32864        93180 :       return type;
   32865              :     }
   32866              : 
   32867     19314183 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
   32868              :     /* The outer template arguments are already substituted into type
   32869              :        (but we still may have used them for constraint checking above).  */;
   32870       944338 :   else if (context == adc_unify)
   32871        21625 :     targs = add_to_template_args (outer_targs, targs);
   32872       922713 :   else if (processing_template_decl)
   32873       922710 :     targs = add_to_template_args (current_template_args (), targs);
   32874     19314183 :   return tsubst (type, targs, complain, NULL_TREE);
   32875              : }
   32876              : 
   32877              : /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
   32878              :    result.  */
   32879              : 
   32880              : tree
   32881    185472555 : splice_late_return_type (tree type, tree late_return_type)
   32882              : {
   32883    185472555 :   if (late_return_type)
   32884              :     {
   32885      3461773 :       gcc_assert (is_auto (type) || seen_error ());
   32886      3461773 :       return late_return_type;
   32887              :     }
   32888              : 
   32889    182010782 :   if (tree auto_node = find_type_usage (type, is_auto))
   32890      5021342 :     if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
   32891              :       {
   32892              :         /* In an abbreviated function template we didn't know we were dealing
   32893              :            with a function template when we saw the auto return type, so rebuild
   32894              :            the return type using an auto with the correct level.  */
   32895           95 :         tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
   32896           95 :         tree auto_vec = make_tree_vec (1);
   32897           95 :         TREE_VEC_ELT (auto_vec, 0) = new_auto;
   32898           95 :         tree targs = add_outermost_template_args (current_template_args (),
   32899              :                                                   auto_vec);
   32900              :         /* Also rebuild the constraint info in terms of the new auto.  */
   32901           95 :         if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
   32902            8 :           PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
   32903            8 :             = build_tree_list (current_template_parms,
   32904            4 :                                tsubst_constraint (TREE_VALUE (ci), targs,
   32905              :                                                   tf_none, NULL_TREE));
   32906           95 :         TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
   32907           95 :         return tsubst (type, targs, tf_none, NULL_TREE);
   32908              :       }
   32909              :   return type;
   32910              : }
   32911              : 
   32912              : /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
   32913              :    'decltype(auto)' or a deduced class template.  */
   32914              : 
   32915              : bool
   32916  10425160939 : is_auto (const_tree type)
   32917              : {
   32918  10425160939 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   32919  10425160939 :       && (TYPE_IDENTIFIER (type) == auto_identifier
   32920   4392875970 :           || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
   32921              :     return true;
   32922              :   else
   32923   9954079148 :     return false;
   32924              : }
   32925              : 
   32926              : /* for_each_template_parm callback for type_uses_auto.  */
   32927              : 
   32928              : int
   32929            0 : is_auto_r (tree tp, void */*data*/)
   32930              : {
   32931            0 :   return is_auto (tp);
   32932              : }
   32933              : 
   32934              : /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
   32935              :    a use of `auto'.  Returns NULL_TREE otherwise.  */
   32936              : 
   32937              : tree
   32938   3893723145 : type_uses_auto (tree type)
   32939              : {
   32940   3893723145 :   if (type == NULL_TREE)
   32941              :     return NULL_TREE;
   32942              : 
   32943              :   /* For parameter packs, check the contents of the pack.  */
   32944   3888372745 :   if (PACK_EXPANSION_P (type))
   32945      6035168 :     type = PACK_EXPANSION_PATTERN (type);
   32946              : 
   32947   3888372745 :   return find_type_usage (type, is_auto);
   32948              : }
   32949              : 
   32950              : /* Recursively walk over && expressions searching for EXPR. Return a reference
   32951              :    to that expression.  */
   32952              : 
   32953           62 : static tree *find_template_requirement (tree *t, tree key)
   32954              : {
   32955           62 :   if (*t == key)
   32956              :     return t;
   32957           24 :   if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
   32958              :     {
   32959           12 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
   32960              :         return p;
   32961           12 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
   32962              :         return p;
   32963              :     }
   32964              :   return 0;
   32965              : }
   32966              : 
   32967              : /* Convert the generic type parameters in PARM that match the types given in the
   32968              :    range [START_IDX, END_IDX) from the current_template_parms into generic type
   32969              :    packs.  */
   32970              : 
   32971              : tree
   32972        61031 : convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
   32973              : {
   32974        61031 :   tree current = current_template_parms;
   32975        61031 :   int depth = TMPL_PARMS_DEPTH (current);
   32976        61031 :   current = INNERMOST_TEMPLATE_PARMS (current);
   32977        61031 :   tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
   32978              : 
   32979        62778 :   for (int i = 0; i < start_idx; ++i)
   32980         3494 :     TREE_VEC_ELT (replacement, i)
   32981         1747 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   32982              : 
   32983       122074 :   for (int i = start_idx; i < end_idx; ++i)
   32984              :     {
   32985              :       /* Create a distinct parameter pack type from the current parm and add it
   32986              :          to the replacement args to tsubst below into the generic function
   32987              :          parameter.  */
   32988        61043 :       tree node = TREE_VEC_ELT (current, i);
   32989        61043 :       tree o = TREE_TYPE (TREE_VALUE (node));
   32990        61043 :       tree t = copy_type (o);
   32991        61043 :       TEMPLATE_TYPE_PARM_INDEX (t)
   32992        61043 :         = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
   32993              :                                       t, 0, 0, tf_none);
   32994        61043 :       TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
   32995        61043 :       TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
   32996        61043 :       TYPE_MAIN_VARIANT (t) = t;
   32997        61043 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
   32998        61043 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
   32999        61043 :       TREE_VEC_ELT (replacement, i) = t;
   33000              : 
   33001              :       /* Replace the current template parameter with new pack.  */
   33002        61043 :       TREE_VALUE (node) = TREE_CHAIN (t);
   33003              : 
   33004              :       /* Surgically adjust the associated constraint of adjusted parameter
   33005              :          and it's corresponding contribution to the current template
   33006              :          requirements.  */
   33007        61043 :       if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
   33008              :         {
   33009           38 :           gcc_assert (concept_check_p (constr));
   33010           38 :           TREE_VEC_ELT (TREE_OPERAND (constr, 1), 0) = t;
   33011              :           /* Use UNKNOWN_LOCATION so write_template_args can tell the
   33012              :              difference between this and a fold the user wrote.  */
   33013           38 :           location_t loc = UNKNOWN_LOCATION;
   33014           38 :           tree fold = finish_left_unary_fold_expr (loc, constr,
   33015              :                                                    TRUTH_ANDIF_EXPR);
   33016           38 :           TEMPLATE_PARM_CONSTRAINTS (node) = fold;
   33017              : 
   33018              :           /* If there was a constraint, we also need to replace that in
   33019              :              the template requirements, which we've already built.  */
   33020           38 :           tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
   33021           38 :           reqs = find_template_requirement (reqs, constr);
   33022           38 :           *reqs = fold;
   33023              :         }
   33024              :     }
   33025              : 
   33026        61031 :   for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
   33027            0 :     TREE_VEC_ELT (replacement, i)
   33028            0 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   33029              : 
   33030              :   /* If there are more levels then build up the replacement with the outer
   33031              :      template parms.  */
   33032        61031 :   if (depth > 1)
   33033        60625 :     replacement = add_to_template_args (template_parms_to_args
   33034        60625 :                                         (TREE_CHAIN (current_template_parms)),
   33035              :                                         replacement);
   33036              : 
   33037        61031 :   return tsubst (parm, replacement, tf_none, NULL_TREE);
   33038              : }
   33039              : 
   33040              : /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
   33041              :    0..N-1.  */
   33042              : 
   33043              : void
   33044        83888 : declare_integer_pack (void)
   33045              : {
   33046        83888 :   tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
   33047              :                                build_function_type_list (integer_type_node,
   33048              :                                                          integer_type_node,
   33049              :                                                          NULL_TREE),
   33050              :                                NULL_TREE, ECF_CONST);
   33051        83888 :   DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
   33052        83888 :   set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
   33053              :                               CP_BUILT_IN_INTEGER_PACK);
   33054        83888 : }
   33055              : 
   33056              : /* Walk the decl or type specialization table calling FN on each
   33057              :    entry.  */
   33058              : 
   33059              : void
   33060         5428 : walk_specializations (bool decls_p,
   33061              :                       void (*fn) (bool decls_p, spec_entry *entry, void *data),
   33062              :                       void *data)
   33063              : {
   33064         5428 :   spec_hash_table *table = decls_p ? decl_specializations
   33065              :     : type_specializations;
   33066         5428 :   spec_hash_table::iterator end (table->end ());
   33067      1517446 :   for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
   33068       756009 :     fn (decls_p, *iter, data);
   33069         5428 : }
   33070              : 
   33071              : /* Lookup the specialization of *ELT, in the decl or type
   33072              :    specialization table.  Return the SPEC that's already there, or
   33073              :    NULL if nothing.  */
   33074              : 
   33075              : tree
   33076      1616094 : match_mergeable_specialization (bool decl_p, spec_entry *elt)
   33077              : {
   33078      1616094 :   hash_table<spec_hasher> *specializations
   33079              :     = decl_p ? decl_specializations : type_specializations;
   33080      1616094 :   auto *slot = specializations->find_slot (elt, NO_INSERT);
   33081              : 
   33082      1616094 :   if (slot)
   33083      1190237 :     return (*slot)->spec;
   33084              : 
   33085              :   return NULL_TREE;
   33086              : }
   33087              : 
   33088              : /* Return flags encoding whether SPEC is on the instantiation and/or
   33089              :    specialization lists of TMPL.  */
   33090              : 
   33091              : unsigned
   33092       346801 : get_mergeable_specialization_flags (bool decl_p, tree tmpl, tree decl)
   33093              : {
   33094       346801 :   unsigned flags = 0;
   33095              : 
   33096       346801 :   tree spec = decl_p ? decl : TREE_TYPE (decl);
   33097       346801 :   for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
   33098      5482626 :        inst; inst = TREE_CHAIN (inst))
   33099      5480126 :     if (TREE_VALUE (inst) == spec)
   33100              :       {
   33101              :         flags |= 1;
   33102              :         break;
   33103              :       }
   33104              : 
   33105       693602 :   if (CLASS_TYPE_P (TREE_TYPE (decl))
   33106       148803 :       && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
   33107       492150 :       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   33108              :     /* Only need to search if DECL is a partial specialization.  */
   33109        11966 :     for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   33110        31520 :          part; part = TREE_CHAIN (part))
   33111        19554 :       if (TREE_VALUE (part) == decl)
   33112              :         {
   33113            0 :           flags |= 2;
   33114            0 :           break;
   33115              :         }
   33116              : 
   33117       346801 :   return flags;
   33118              : }
   33119              : 
   33120              : /* Add a new specialization described by SPEC.  DECL is the
   33121              :    maybe-template decl and FLAGS is as returned from
   33122              :    get_mergeable_specialization_flags.  */
   33123              : 
   33124              : void
   33125       200959 : add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
   33126              :                               unsigned flags)
   33127              : {
   33128       200959 :   if (decl_p)
   33129              :     {
   33130       144246 :       auto *slot = decl_specializations->find_slot (elt, INSERT);
   33131              : 
   33132       144246 :       gcc_checking_assert (!*slot);
   33133       144246 :       auto entry = ggc_alloc<spec_entry> ();
   33134       144246 :       *entry = *elt;
   33135       144246 :       *slot = entry;
   33136              :     }
   33137              :   else
   33138              :     {
   33139        56713 :       auto *slot = type_specializations->find_slot (elt, INSERT);
   33140              : 
   33141              :       /* We don't distinguish different constrained partial type
   33142              :          specializations, so there could be duplicates.  In that case we
   33143              :          must propagate TYPE_CANONICAL so that they are treated as the
   33144              :          same type.  Everything else must be new.   */
   33145        56713 :       if (*slot)
   33146              :         {
   33147          188 :           gcc_checking_assert (flags & 2);
   33148          188 :           TYPE_CANONICAL (elt->spec) = TYPE_CANONICAL ((*slot)->spec);
   33149              :         }
   33150              :       else
   33151              :         {
   33152        56525 :           auto entry = ggc_alloc<spec_entry> ();
   33153        56525 :           *entry = *elt;
   33154        56525 :           *slot = entry;
   33155              :         }
   33156              :     }
   33157              : 
   33158       200959 :   if (flags & 1)
   33159       194822 :     DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
   33160       389644 :       = tree_cons (elt->args, elt->spec,
   33161       194822 :                    DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
   33162              : 
   33163       200959 :   if (flags & 2)
   33164              :     {
   33165              :       /* A partial specialization.  */
   33166         4433 :       tree cons = tree_cons (elt->args, decl,
   33167         4433 :                              DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
   33168         4433 :       TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
   33169         4433 :       DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
   33170         4433 :       set_defining_module_for_partial_spec (STRIP_TEMPLATE (decl));
   33171              :     }
   33172       200959 : }
   33173              : 
   33174              : struct expansion_stmt_bc
   33175              : {
   33176              :   tree break_label;
   33177              :   tree continue_label;
   33178              :   hash_set<tree> *pset;
   33179              :   location_t loc;
   33180              :   bool in_switch;
   33181              : };
   33182              : 
   33183              : /* Helper function for finish_expansion_stmt.  Find BREAK_STMT (not
   33184              :    nested inside of other WHILE_STMT, FOR_STMT, DO_STMT, TEMPLATE_FOR_STMT
   33185              :    or SWITCH_STMT) or CONTINUE_STMT (not nested inside those except
   33186              :    perhaps SWITCH_STMT) and replace them with GOTO_EXPR to lazily created
   33187              :    label.  */
   33188              : 
   33189              : static tree
   33190        50207 : expansion_stmt_find_bc_r (tree *tp, int *walk_subtrees, void *data)
   33191              : {
   33192        50207 :   tree t = *tp;
   33193        50207 :   expansion_stmt_bc *bc_data = (expansion_stmt_bc *) data;
   33194        50207 :   switch (TREE_CODE (t))
   33195              :     {
   33196            0 :     case WHILE_STMT:
   33197            0 :       *walk_subtrees = 0;
   33198            0 :       for (int i = 0; i < TREE_CODE_LENGTH (WHILE_STMT); ++i)
   33199            0 :         if (&TREE_OPERAND (t, i) != &WHILE_BODY (t))
   33200            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33201              :                         data, bc_data->pset);
   33202              :       break;
   33203            0 :     case FOR_STMT:
   33204            0 :       *walk_subtrees = 0;
   33205            0 :       for (int i = 0; i < TREE_CODE_LENGTH (FOR_STMT); ++i)
   33206            0 :         if (&TREE_OPERAND (t, i) != &FOR_BODY (t))
   33207            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33208              :                         data, bc_data->pset);
   33209              :       break;
   33210            0 :     case DO_STMT:
   33211            0 :       *walk_subtrees = 0;
   33212            0 :       for (int i = 0; i < TREE_CODE_LENGTH (DO_STMT); ++i)
   33213            0 :         if (&TREE_OPERAND (t, i) != &DO_BODY (t))
   33214            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33215              :                         data, bc_data->pset);
   33216              :       break;
   33217            0 :     case TEMPLATE_FOR_STMT:
   33218            0 :       *walk_subtrees = 0;
   33219            0 :       for (int i = 0; i < TREE_CODE_LENGTH (TEMPLATE_FOR_STMT); ++i)
   33220            0 :         if (&TREE_OPERAND (t, i) != &TEMPLATE_FOR_BODY (t))
   33221            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33222              :                         data, bc_data->pset);
   33223              :       break;
   33224           12 :     case SWITCH_STMT:
   33225           12 :       if (!bc_data->in_switch)
   33226              :         {
   33227           12 :           *walk_subtrees = 0;
   33228           72 :           for (int i = 0; i < TREE_CODE_LENGTH (SWITCH_STMT); ++i)
   33229           60 :             if (&TREE_OPERAND (t, i) != &SWITCH_STMT_BODY (t))
   33230           48 :               cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33231              :                             data, bc_data->pset);
   33232           12 :           bc_data->in_switch = true;
   33233           12 :           cp_walk_tree (&SWITCH_STMT_BODY (t), expansion_stmt_find_bc_r,
   33234              :                         data, bc_data->pset);
   33235           12 :           bc_data->in_switch = false;
   33236              :         }
   33237              :       break;
   33238          231 :     case BREAK_STMT:
   33239          231 :       if (!bc_data->in_switch)
   33240              :         {
   33241          183 :           if (!bc_data->break_label)
   33242              :             {
   33243           39 :               bc_data->break_label = create_artificial_label (bc_data->loc);
   33244           39 :               TREE_USED (bc_data->break_label) = 1;
   33245           39 :               LABEL_DECL_BREAK (bc_data->break_label) = true;
   33246              :             }
   33247          183 :           *tp = build1_loc (EXPR_LOCATION (t), GOTO_EXPR, void_type_node,
   33248              :                             bc_data->break_label);
   33249              :         }
   33250              :       break;
   33251          137 :     case CONTINUE_STMT:
   33252          137 :       if (!bc_data->continue_label)
   33253              :         {
   33254          137 :           bc_data->continue_label = create_artificial_label (bc_data->loc);
   33255          137 :           TREE_USED (bc_data->continue_label) = 1;
   33256          137 :           LABEL_DECL_CONTINUE (bc_data->continue_label) = true;
   33257              :         }
   33258          137 :       *tp = build1_loc (EXPR_LOCATION (t), GOTO_EXPR, void_type_node,
   33259              :                         bc_data->continue_label);
   33260          137 :       break;
   33261        49827 :     default:
   33262        49827 :       if (TYPE_P (t))
   33263          221 :         *walk_subtrees = 0;
   33264              :       break;
   33265              :     }
   33266        50207 :   return NULL_TREE;
   33267              : }
   33268              : 
   33269              : /* Finish an expansion-statement.  */
   33270              : 
   33271              : void
   33272          987 : finish_expansion_stmt (tree expansion_stmt, tree args,
   33273              :                        tsubst_flags_t complain, tree in_decl)
   33274              : {
   33275          987 :   tree expansion_init = TEMPLATE_FOR_EXPR (expansion_stmt);
   33276          987 :   if (error_operand_p (expansion_init))
   33277           48 :     return;
   33278              : 
   33279          987 :   enum expansion_stmt_kind {
   33280              :     esk_none,
   33281              :     esk_iterating,
   33282              :     esk_destructuring,
   33283              :     esk_enumerating
   33284          987 :   } kind = esk_none;
   33285              : 
   33286          987 :   unsigned HOST_WIDE_INT n = 0;
   33287          987 :   tree range_decl = TEMPLATE_FOR_DECL (expansion_stmt);
   33288          987 :   bool is_decomp = false;
   33289          987 :   if (TREE_CODE (range_decl) == TREE_VEC)
   33290              :     {
   33291          294 :       is_decomp = true;
   33292          294 :       range_decl = TREE_VEC_ELT (range_decl, 0);
   33293              :     }
   33294          987 :   if (error_operand_p (range_decl))
   33295              :     return;
   33296              : 
   33297          951 :   location_t loc = DECL_SOURCE_LOCATION (range_decl);
   33298          951 :   tree begin = NULL_TREE, begin_minus_begin_type = NULL_TREE;
   33299          951 :   auto_vec<tree, 8> destruct_decls;
   33300          951 :   if (BRACE_ENCLOSED_INITIALIZER_P (expansion_init))
   33301              :     {
   33302              :       /* Enumerating expansion statements.  */
   33303          507 :       kind = esk_enumerating;
   33304          507 :       n = CONSTRUCTOR_NELTS (expansion_init);
   33305              :     }
   33306          888 :   else if (TYPE_REF_P (TREE_TYPE (expansion_init))
   33307          444 :            ? TREE_CODE (TREE_TYPE (TREE_TYPE (expansion_init))) != ARRAY_TYPE
   33308          444 :            : TREE_CODE (TREE_TYPE (expansion_init)) != ARRAY_TYPE)
   33309              :     {
   33310          171 :       tree range_temp, begin_expr, end_expr, iter_type;
   33311          171 :       range_temp = convert_from_reference (build_range_temp (expansion_init));
   33312          171 :       iter_type = cp_perform_range_for_lookup (range_temp, &begin_expr,
   33313              :                                                &end_expr, tf_none);
   33314          171 :       if (iter_type != error_mark_node
   33315           83 :           || (begin_expr != error_mark_node && end_expr != error_mark_node))
   33316           88 :         kind = esk_iterating;
   33317              :     }
   33318          590 :   if (kind == esk_iterating)
   33319              :     {
   33320              :       /* Iterating expansion statements.  */
   33321           88 :       tree end;
   33322           88 :       begin = cp_build_range_for_decls (loc, expansion_init, &end, true);
   33323           88 :       if (!error_operand_p (begin) && !error_operand_p (end))
   33324              :         {
   33325              :           /* In the standard this is all evaluated inside of a consteval
   33326              :              lambda.  So, force in_immediate_context () around this.  */
   33327           86 :           in_consteval_if_p_temp_override icip;
   33328           86 :           in_consteval_if_p = true;
   33329           86 :           tree i
   33330           86 :             = build_target_expr_with_type (begin,
   33331           86 :                                            cv_unqualified (TREE_TYPE (begin)),
   33332              :                                            tf_warning_or_error);
   33333           86 :           tree w = build_stmt (loc, WHILE_STMT, NULL_TREE, NULL_TREE,
   33334              :                                NULL_TREE, NULL_TREE, NULL_TREE);
   33335           86 :           tree r = get_target_expr (build_zero_cst (ptrdiff_type_node));
   33336           86 :           tree iinc = build_x_unary_op (loc, PREINCREMENT_EXPR,
   33337           86 :                                         TARGET_EXPR_SLOT (i), NULL_TREE,
   33338              :                                         tf_warning_or_error);
   33339          172 :           tree rinc = build2 (PREINCREMENT_EXPR, ptrdiff_type_node,
   33340           86 :                               TARGET_EXPR_SLOT (r),
   33341              :                               build_int_cst (ptrdiff_type_node, 1));
   33342           86 :           WHILE_BODY (w) = build_compound_expr (loc, iinc, rinc);
   33343           86 :           WHILE_COND (w) = build_x_binary_op (loc, NE_EXPR, i, ERROR_MARK,
   33344              :                                               end, ERROR_MARK, NULL_TREE, NULL,
   33345              :                                               tf_warning_or_error);
   33346           86 :           tree e = build_compound_expr (loc, r, i);
   33347           86 :           e = build_compound_expr (loc, e, w);
   33348           86 :           e = build_compound_expr (loc, e, TARGET_EXPR_SLOT (r));
   33349           86 :           e = cxx_constant_value (e);
   33350           86 :           if (tree_fits_uhwi_p (e))
   33351           71 :             n = tree_to_uhwi (e);
   33352           86 :         }
   33353              :     }
   33354          863 :   else if (kind == esk_none)
   33355              :     {
   33356          356 :       kind = esk_destructuring;
   33357          356 :       HOST_WIDE_INT sz = cp_decomp_size (loc, TREE_TYPE (expansion_init),
   33358              :                                          tf_warning_or_error);
   33359          356 :       if (sz < 0)
   33360           12 :         return;
   33361          344 :       n = sz;
   33362          344 :       tree auto_node = make_auto ();
   33363          344 :       tree decomp_type = cp_build_reference_type (auto_node, true);
   33364          344 :       decomp_type = do_auto_deduction (decomp_type, expansion_init, auto_node);
   33365          344 :       tree decl = build_decl (loc, VAR_DECL, NULL_TREE, decomp_type);
   33366          344 :       TREE_USED (decl) = 1;
   33367          344 :       DECL_ARTIFICIAL (decl) = 1;
   33368          344 :       DECL_DECLARED_CONSTEXPR_P (decl)
   33369          344 :         = DECL_DECLARED_CONSTEXPR_P (range_decl);
   33370          344 :       if (DECL_DECLARED_CONSTEXPR_P (decl))
   33371           61 :         TREE_READONLY (decl) = 1;
   33372          344 :       if (n)
   33373          305 :         fit_decomposition_lang_decl (decl, NULL_TREE);
   33374          344 :       pushdecl (decl);
   33375          344 :       cp_decomp this_decomp;
   33376          344 :       this_decomp.count = n;
   33377          344 :       destruct_decls.safe_grow (n, true);
   33378         1320 :       for (unsigned HOST_WIDE_INT i = 0; i < n; ++i)
   33379              :         {
   33380          976 :           tree this_type = make_auto ();
   33381          976 :           if (DECL_DECLARED_CONSTEXPR_P (decl))
   33382          116 :             this_type = cp_build_qualified_type (this_type, TYPE_QUAL_CONST);
   33383          976 :           tree this_decl = build_decl (loc, VAR_DECL, NULL_TREE, this_type);
   33384          976 :           TREE_USED (this_decl) = 1;
   33385          976 :           DECL_ARTIFICIAL (this_decl) = 1;
   33386          976 :           DECL_DECLARED_CONSTEXPR_P (this_decl)
   33387          976 :             = DECL_DECLARED_CONSTEXPR_P (decl);
   33388          976 :           if (DECL_DECLARED_CONSTEXPR_P (decl))
   33389          116 :             TREE_READONLY (this_decl) = 1;
   33390          976 :           pushdecl (this_decl);
   33391          976 :           this_decomp.decl = this_decl;
   33392          976 :           destruct_decls[i] = this_decl;
   33393              :         }
   33394          344 :       DECL_NAME (decl) = for_range__identifier;
   33395          383 :       cp_finish_decl (decl, expansion_init,
   33396              :                       /*is_constant_init*/false, NULL_TREE,
   33397              :                       LOOKUP_ONLYCONVERTING, n ? &this_decomp : NULL);
   33398          344 :       DECL_NAME (decl) = NULL_TREE;
   33399              :     }
   33400              : 
   33401          939 :   expansion_stmt_bc bc_data = { NULL_TREE, NULL_TREE, NULL, loc, false };
   33402              : 
   33403         2921 :   for (unsigned HOST_WIDE_INT i = 0; i < n; ++i)
   33404              :     {
   33405         1982 :       tree scope = do_pushlevel (sk_block);
   33406         5946 :       bool revert_outer
   33407         1982 :         = (current_binding_level->level_chain
   33408         1982 :            && current_binding_level->level_chain->kind == sk_template_for);
   33409              :       /* Don't diagnose redeclaration of for-range-declaration decls.
   33410              :          The sk_template_for block is reused for the originally parsed
   33411              :          source as well as the lowered one.  In the original one
   33412              :          redeclaration of the for-range-declaration decls in the substatement
   33413              :          should be diagnosed (i.e. declarations of the same name in sk_block
   33414              :          of the body vs. declarations in sk_template_for block).  In the
   33415              :          lowered case, the sk_block added by do_pushlevel (sk_block) above
   33416              :          will be block in the lowering of each Si.  Those blocks do redeclare
   33417              :          for-range-declaration, so temporarily change sk_template_for
   33418              :          kind to sk_block to avoid it being diagnosed as invalid.  */
   33419         1982 :       if (revert_outer)
   33420         1982 :         current_binding_level->level_chain->kind = sk_block;
   33421         1982 :       tree type = TREE_TYPE (range_decl);
   33422         1982 :       if (args)
   33423         1147 :         type = tsubst (type, args, complain | tf_tst_ok, in_decl);
   33424         1982 :       if (DECL_DECLARED_CONSTEXPR_P (range_decl) && !TYPE_REF_P (type))
   33425          345 :         type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
   33426         1982 :       tree decl = build_decl (loc, VAR_DECL, DECL_NAME (range_decl), type);
   33427         1982 :       DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (range_decl);
   33428         1982 :       TREE_USED (decl) |= TREE_USED (range_decl);
   33429         1982 :       DECL_READ_P (decl) |= DECL_READ_P (range_decl);
   33430         1982 :       if (args)
   33431         1147 :         apply_late_template_attributes (&decl, DECL_ATTRIBUTES (decl),
   33432              :                                         /*flags=*/0, args, complain,
   33433              :                                         in_decl);
   33434              : 
   33435         1982 :       DECL_DECLARED_CONSTEXPR_P (decl)
   33436         1982 :         = DECL_DECLARED_CONSTEXPR_P (range_decl);
   33437         1982 :       if (DECL_DECLARED_CONSTEXPR_P (decl))
   33438          345 :         TREE_READONLY (decl) = 1;
   33439         1982 :       pushdecl (decl);
   33440         1982 :       tree init = NULL_TREE;
   33441         1982 :       switch (kind)
   33442              :         {
   33443          739 :         case esk_enumerating:
   33444          739 :           init = CONSTRUCTOR_ELT (expansion_init, i)->value;
   33445          739 :           break;
   33446          267 :         case esk_iterating:
   33447          267 :           tree iter_init, auto_node, iter_type, iter, it;
   33448          267 :           it = build_int_cst (ptrdiff_type_node, i);
   33449          267 :           if (begin_minus_begin_type == NULL_TREE)
   33450              :             {
   33451           61 :               ++cp_unevaluated_operand;
   33452           61 :               ++c_inhibit_evaluation_warnings;
   33453           61 :               tree begin_minus_begin
   33454          122 :                 = build_x_binary_op (loc, MINUS_EXPR, begin, TREE_CODE (begin),
   33455           61 :                                      begin, TREE_CODE (begin), NULL_TREE, NULL,
   33456              :                                      tf_warning_or_error);
   33457           61 :               --cp_unevaluated_operand;
   33458           61 :               --c_inhibit_evaluation_warnings;
   33459           61 :               begin_minus_begin_type
   33460           61 :                 = finish_decltype_type (begin_minus_begin, false,
   33461              :                                         tf_warning_or_error);
   33462              :             }
   33463          267 :           it = build_constructor_single (init_list_type_node, NULL_TREE, it);
   33464          267 :           CONSTRUCTOR_IS_DIRECT_INIT (it) = true;
   33465          267 :           it = finish_compound_literal (begin_minus_begin_type, it,
   33466              :                                         tf_warning_or_error, fcl_functional);
   33467          267 :           iter_init
   33468          267 :             = build_x_binary_op (loc, PLUS_EXPR, begin, ERROR_MARK, it,
   33469              :                                  ERROR_MARK, NULL_TREE, NULL,
   33470              :                                  tf_warning_or_error);
   33471          267 :           auto_node = make_auto ();
   33472          267 :           iter_type = do_auto_deduction (auto_node, iter_init, auto_node);
   33473          267 :           if (!TYPE_REF_P (iter_type))
   33474          267 :             iter_type = cp_build_qualified_type (iter_type, TYPE_QUAL_CONST);
   33475          267 :           iter = build_decl (loc, VAR_DECL, NULL_TREE, iter_type);
   33476          267 :           TREE_USED (iter) = 1;
   33477          267 :           DECL_ARTIFICIAL (iter) = 1;
   33478          267 :           TREE_STATIC (iter) = 1;
   33479          267 :           DECL_DECLARED_CONSTEXPR_P (iter) = 1;
   33480          267 :           pushdecl (iter);
   33481          267 :           cp_finish_decl (iter, iter_init, /*is_constant_init*/false,
   33482              :                           NULL_TREE, LOOKUP_ONLYCONVERTING);
   33483          267 :           init = build_x_indirect_ref (loc, iter, RO_UNARY_STAR, NULL_TREE,
   33484              :                                        tf_warning_or_error);
   33485          267 :           break;
   33486          976 :         case esk_destructuring:
   33487          976 :           init = convert_from_reference (destruct_decls[i]);
   33488          976 :           break;
   33489              :         default:
   33490              :           gcc_unreachable ();
   33491              :         }
   33492         1982 :       cp_decomp this_decomp = {};
   33493         1982 :       if (is_decomp)
   33494              :         {
   33495          603 :           fit_decomposition_lang_decl (decl, NULL_TREE);
   33496          603 :           tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   33497          603 :           this_decomp.count = TREE_VEC_LENGTH (v) - 1;
   33498         1659 :           for (unsigned i = 0; i < this_decomp.count; ++i)
   33499              :             {
   33500         1056 :               tree this_type = make_auto ();
   33501         1056 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   33502            0 :                 this_type = cp_build_qualified_type (this_type,
   33503              :                                                      TYPE_QUAL_CONST);
   33504         1056 :               tree this_decl
   33505         1056 :                 = build_decl (loc, VAR_DECL,
   33506         1056 :                               DECL_NAME (TREE_VEC_ELT (v, i + 1)),
   33507         1056 :                               this_type);
   33508         1056 :               TREE_USED (this_decl) = 1;
   33509         1056 :               DECL_ARTIFICIAL (this_decl) = 1;
   33510         2112 :               DECL_ATTRIBUTES (this_decl)
   33511         1056 :                 = DECL_ATTRIBUTES (TREE_VEC_ELT (v, i + 1));
   33512         1056 :               TREE_USED (this_decl) |= TREE_USED (TREE_VEC_ELT (v, i + 1));
   33513         1056 :               DECL_READ_P (this_decl) |= DECL_READ_P (TREE_VEC_ELT (v, i + 1));
   33514         1056 :               if (DECL_PACK_P (TREE_VEC_ELT (v, i + 1)))
   33515              :                 {
   33516           36 :                   tree dtype = cxx_make_type (DECLTYPE_TYPE);
   33517           36 :                   DECLTYPE_TYPE_EXPR (dtype) = this_decl;
   33518           36 :                   DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (dtype) = 1;
   33519           36 :                   SET_TYPE_STRUCTURAL_EQUALITY (dtype);
   33520           36 :                   tree type = cxx_make_type (TYPE_PACK_EXPANSION);
   33521           36 :                   PACK_EXPANSION_PATTERN (type) = dtype;
   33522           36 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
   33523           72 :                   PACK_EXPANSION_PARAMETER_PACKS (type) = this_decl;
   33524           36 :                   TREE_TYPE (this_decl) = type;
   33525              :                 }
   33526         1056 :               if (args)
   33527          849 :                 apply_late_template_attributes (&this_decl,
   33528          849 :                                                 DECL_ATTRIBUTES (this_decl),
   33529              :                                                 /*flags=*/0, args,
   33530              :                                                 complain, in_decl);
   33531         1056 :               DECL_DECLARED_CONSTEXPR_P (this_decl)
   33532         1056 :                 = DECL_DECLARED_CONSTEXPR_P (decl);
   33533         1056 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   33534            0 :                 TREE_READONLY (this_decl) = 1;
   33535         1056 :               pushdecl (this_decl);
   33536         1056 :               this_decomp.decl = this_decl;
   33537              :             }
   33538              :         }
   33539         1982 :       cp_finish_decl (decl, init, false, NULL_TREE,
   33540              :                       LOOKUP_ONLYCONVERTING, is_decomp ? &this_decomp : NULL);
   33541         1982 :       if (revert_outer)
   33542         1982 :         current_binding_level->level_chain->kind = sk_template_for;
   33543         1982 :       tree targs = args;
   33544         1982 :       if (args == NULL_TREE)
   33545              :         {
   33546          835 :           targs = make_tree_vec (1);
   33547          835 :           TREE_VEC_ELT (targs, 0) = build_int_cst (ptrdiff_type_node, i + 1);
   33548              :         }
   33549          835 :       if (args != NULL_TREE
   33550          835 :           || push_tinst_level_loc (expansion_stmt, targs, loc))
   33551              :         {
   33552         1982 :           local_specialization_stack lss (lss_copy);
   33553         1982 :           register_local_specialization (decl, range_decl);
   33554         1982 :           if (is_decomp)
   33555              :             {
   33556          603 :               tree d = this_decomp.decl;
   33557          603 :               unsigned int cnt = this_decomp.count;
   33558          603 :               tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   33559         1659 :               for (unsigned int i = 0; i < cnt; ++i, d = DECL_CHAIN (d))
   33560         1056 :                 register_local_specialization (d, TREE_VEC_ELT (v, cnt - i));
   33561              :             }
   33562         3964 :           tsubst_stmt (TEMPLATE_FOR_BODY (expansion_stmt),
   33563              :                        targs, complain, in_decl ? in_decl : range_decl);
   33564         1982 :           if (args == NULL_TREE)
   33565          835 :             pop_tinst_level ();
   33566         1982 :         }
   33567         1982 :       tree stmt = do_poplevel (scope);
   33568         1982 :       if (stmt)
   33569              :         {
   33570         1982 :           add_stmt (stmt);
   33571         1982 :           hash_set<tree> pset;
   33572         1982 :           bc_data.continue_label = NULL_TREE;
   33573         1982 :           bc_data.pset = &pset;
   33574         1982 :           cp_walk_tree (&stmt, expansion_stmt_find_bc_r, &bc_data, &pset);
   33575         1982 :           if (bc_data.continue_label)
   33576          137 :             add_stmt (build1 (LABEL_EXPR, void_type_node,
   33577              :                               bc_data.continue_label));
   33578         1982 :         }
   33579              :     }
   33580          939 :   if (bc_data.break_label)
   33581           39 :     add_stmt (build1 (LABEL_EXPR, void_type_node, bc_data.break_label));
   33582          939 :   if (args == NULL_TREE)
   33583              :     {
   33584          393 :       TREE_TYPE (range_decl) = error_mark_node;
   33585          393 :       if (DECL_HAS_VALUE_EXPR_P (range_decl))
   33586              :         {
   33587            0 :           SET_DECL_VALUE_EXPR (range_decl, NULL_TREE);
   33588            0 :           DECL_HAS_VALUE_EXPR_P (range_decl) = 0;
   33589              :         }
   33590          393 :       if (is_decomp)
   33591              :         {
   33592           15 :           tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   33593           57 :           for (int i = 1; i < TREE_VEC_LENGTH (v); ++i)
   33594              :             {
   33595           42 :               tree d = TREE_VEC_ELT (v, i);
   33596           42 :               TREE_TYPE (d) = error_mark_node;
   33597           42 :               if (DECL_HAS_VALUE_EXPR_P (d))
   33598              :                 {
   33599           42 :                   SET_DECL_VALUE_EXPR (d, NULL_TREE);
   33600           42 :                   DECL_HAS_VALUE_EXPR_P (d) = 0;
   33601              :                 }
   33602              :             }
   33603              :         }
   33604              :     }
   33605          951 : }
   33606              : 
   33607              : /* Perform the appropriate conversion of the argument of
   33608              :    std::meta::reflect_constant.   EXPR is the argument, TYPE is its type.
   33609              :    Mainly, the point is to check that the type is valid in this context
   33610              :    and maybe replace the argument with a reference to the corresponding
   33611              :    template parameter object.  */
   33612              : 
   33613              : tree
   33614          996 : convert_reflect_constant_arg (tree type, tree expr)
   33615              : {
   33616          996 :   if (invalid_nontype_parm_type_p (type, tf_none))
   33617            0 :     return error_mark_node;
   33618              : 
   33619          996 :   expr = convert_nontype_argument (type, expr, tf_none);
   33620          996 :   if (!expr)
   33621           21 :     return error_mark_node;
   33622              :   return expr;
   33623              : }
   33624              : 
   33625              : /* Set up the hash tables for template instantiations.  */
   33626              : 
   33627              : void
   33628        98033 : init_template_processing (void)
   33629              : {
   33630        98033 :   decl_specializations = hash_table<spec_hasher>::create_ggc (37);
   33631        98033 :   type_specializations = hash_table<spec_hasher>::create_ggc (37);
   33632              : 
   33633        98033 :   if (cxx_dialect >= cxx11)
   33634        83888 :     declare_integer_pack ();
   33635        98033 : }
   33636              : 
   33637              : /* Print stats about the template hash tables for -fstats.  */
   33638              : 
   33639              : void
   33640            0 : print_template_statistics (void)
   33641              : {
   33642            0 :   fprintf (stderr, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   33643              :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   33644            0 :            (fmt_size_t) decl_specializations->size (),
   33645            0 :            (fmt_size_t) decl_specializations->elements (),
   33646              :            decl_specializations->collisions ());
   33647            0 :   fprintf (stderr, "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   33648              :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   33649            0 :            (fmt_size_t) type_specializations->size (),
   33650            0 :            (fmt_size_t) type_specializations->elements (),
   33651              :            type_specializations->collisions ());
   33652            0 : }
   33653              : 
   33654              : #if CHECKING_P
   33655              : 
   33656              : namespace selftest {
   33657              : 
   33658              : /* Verify that type_dependent_expression_p () works correctly, even
   33659              :    in the presence of location wrapper nodes.  */
   33660              : 
   33661              : static void
   33662            1 : test_type_dependent_expression_p ()
   33663              : {
   33664            1 :   location_t loc = BUILTINS_LOCATION;
   33665              : 
   33666            1 :   tree name = get_identifier ("foo");
   33667              : 
   33668              :   /* If no templates are involved, nothing is type-dependent.  */
   33669            1 :   gcc_assert (!processing_template_decl);
   33670            1 :   ASSERT_FALSE (type_dependent_expression_p (name));
   33671              : 
   33672            1 :   ++processing_template_decl;
   33673              : 
   33674              :   /* Within a template, an unresolved name is always type-dependent.  */
   33675            1 :   ASSERT_TRUE (type_dependent_expression_p (name));
   33676              : 
   33677              :   /* Ensure it copes with NULL_TREE and errors.  */
   33678            1 :   ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
   33679            1 :   ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
   33680              : 
   33681              :   /* A USING_DECL in a template should be type-dependent, even if wrapped
   33682              :      with a location wrapper (PR c++/83799).  */
   33683            1 :   tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
   33684            1 :   TREE_TYPE (using_decl) = integer_type_node;
   33685            1 :   ASSERT_TRUE (type_dependent_expression_p (using_decl));
   33686            1 :   tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
   33687            1 :   ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
   33688            1 :   ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
   33689              : 
   33690            1 :   --processing_template_decl;
   33691            1 : }
   33692              : 
   33693              : /* Run all of the selftests within this file.  */
   33694              : 
   33695              : void
   33696            1 : cp_pt_cc_tests ()
   33697              : {
   33698            1 :   test_type_dependent_expression_p ();
   33699            1 : }
   33700              : 
   33701              : } // namespace selftest
   33702              : 
   33703              : #endif /* #if CHECKING_P */
   33704              : 
   33705              : #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.