LCOV - code coverage report
Current view: top level - gcc/cp - pt.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.2 % 15293 14563
Test Date: 2026-10-03 16:17:38 Functions: 98.8 % 483 477
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              : #include "escaped_string.h"
      54              : 
      55              : /* The type of functions taking a tree, and some additional data, and
      56              :    returning an int.  */
      57              : typedef int (*tree_fn_t) (tree, void*);
      58              : 
      59              : /* The PENDING_TEMPLATES is a list of templates whose instantiations
      60              :    have been deferred, either because their definitions were not yet
      61              :    available, or because we were putting off doing the work.  */
      62              : struct GTY ((chain_next ("%h.next"))) pending_template
      63              : {
      64              :   struct pending_template *next;
      65              :   struct tinst_level *tinst;
      66              : };
      67              : 
      68              : static GTY(()) struct pending_template *pending_templates;
      69              : static GTY(()) struct pending_template *last_pending_template;
      70              : 
      71              : int processing_template_parmlist;
      72              : static int template_header_count;
      73              : 
      74              : static vec<int> inline_parm_levels;
      75              : 
      76              : static GTY(()) struct tinst_level *current_tinst_level;
      77              : 
      78              : static GTY(()) vec<tree, va_gc> *saved_access_scope;
      79              : 
      80              : /* Live only within one (recursive) call to tsubst_expr.  We use
      81              :    this to pass the statement expression node from the STMT_EXPR
      82              :    to the EXPR_STMT that is its result.  */
      83              : static tree cur_stmt_expr;
      84              : 
      85              : // -------------------------------------------------------------------------- //
      86              : // Local Specialization Stack
      87              : //
      88              : // Implementation of the RAII helper for creating new local
      89              : // specializations.
      90    109126933 : local_specialization_stack::local_specialization_stack (lss_policy policy)
      91    109126933 :   : saved (local_specializations)
      92              : {
      93    109126933 :   if (policy == lss_nop)
      94              :     ;
      95     37444229 :   else if (policy == lss_blank || !saved)
      96     34365072 :     local_specializations = new hash_map<tree, tree>;
      97              :   else
      98      3079157 :     local_specializations = new hash_map<tree, tree>(*saved);
      99    109126933 : }
     100              : 
     101    109121524 : local_specialization_stack::~local_specialization_stack ()
     102              : {
     103    109121524 :   if (local_specializations != saved)
     104              :     {
     105     74883040 :       delete local_specializations;
     106     37441520 :       local_specializations = saved;
     107              :     }
     108    109121524 : }
     109              : 
     110              : /* True if we've recursed into fn_type_unification too many times.  */
     111              : static bool excessive_deduction_depth;
     112              : 
     113              : struct spec_hasher : ggc_ptr_hash<spec_entry>
     114              : {
     115              :   static hashval_t hash (tree, tree);
     116              :   static hashval_t hash (spec_entry *);
     117              :   static bool equal (spec_entry *, spec_entry *);
     118              : };
     119              : 
     120              : /* The general template is not in these tables.  */
     121              : typedef hash_table<spec_hasher> spec_hash_table;
     122              : static GTY (()) spec_hash_table *decl_specializations;
     123              : static GTY (()) spec_hash_table *type_specializations;
     124              : 
     125              : /* Contains canonical template parameter types. The vector is indexed by
     126              :    the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
     127              :    TREE_LIST, whose TREE_VALUEs contain the canonical template
     128              :    parameters of various types and levels.  */
     129              : static GTY(()) vec<tree, va_gc> *canonical_template_parms;
     130              : 
     131              : #define UNIFY_ALLOW_NONE 0
     132              : #define UNIFY_ALLOW_MORE_CV_QUAL 1
     133              : #define UNIFY_ALLOW_LESS_CV_QUAL 2
     134              : #define UNIFY_ALLOW_DERIVED 4
     135              : #define UNIFY_ALLOW_INTEGER 8
     136              : #define UNIFY_ALLOW_OUTER_LEVEL 16
     137              : #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
     138              : #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
     139              : 
     140              : enum template_base_result {
     141              :   tbr_incomplete_type,
     142              :   tbr_ambiguous_baseclass,
     143              :   tbr_success
     144              : };
     145              : 
     146              : static bool resolve_overloaded_unification (tree, tree, tree, tree,
     147              :                                             unification_kind_t, int,
     148              :                                             bool);
     149              : static int try_one_overload (tree, tree, tree, tree, tree,
     150              :                              unification_kind_t, int, bool, bool);
     151              : static int unify (tree, tree, tree, tree, int, bool);
     152              : static void add_pending_template (tree);
     153              : static tree reopen_tinst_level (struct tinst_level *);
     154              : static tree tsubst_initializer_list (tree, tree);
     155              : static tree get_partial_spec_bindings (tree, tree, tree);
     156              : static void tsubst_enum (tree, tree, tree);
     157              : static bool check_instantiated_args (tree, tree, tsubst_flags_t);
     158              : static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
     159              :                                            struct conversion **, bool, bool);
     160              : static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
     161              :                                              tree*, tree*, tree);
     162              : static int type_unification_real (tree, tree, tree, const tree *,
     163              :                                   unsigned int, int, unification_kind_t,
     164              :                                   vec<deferred_access_check, va_gc> **,
     165              :                                   bool);
     166              : static void note_template_header (int);
     167              : static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
     168              : static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
     169              : static tree convert_template_argument (tree, tree, tree,
     170              :                                        tsubst_flags_t, int, tree);
     171              : static tree for_each_template_parm (tree, tree_fn_t, void*,
     172              :                                     hash_set<tree> *, bool, tree_fn_t = NULL);
     173              : static tree build_template_parm_index (int, int, int, tree, tree);
     174              : static bool inline_needs_template_parms (tree, bool);
     175              : static void push_inline_template_parms_recursive (tree, int);
     176              : static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
     177              : static int mark_template_parm (tree, void *);
     178              : static int template_parm_this_level_p (tree, void *);
     179              : static tree tsubst_friend_function (tree, tree);
     180              : static tree tsubst_friend_class (tree, tree);
     181              : static int can_complete_type_without_circularity (tree);
     182              : static tree get_bindings (tree, tree, tree, bool);
     183              : static int template_decl_level (tree);
     184              : static int check_cv_quals_for_unify (int, tree, tree);
     185              : static int unify_pack_expansion (tree, tree, tree,
     186              :                                  tree, unification_kind_t, bool, bool);
     187              : static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
     188              : static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
     189              : static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
     190              : static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
     191              : static bool check_specialization_scope (void);
     192              : static tree process_partial_specialization (tree);
     193              : static enum template_base_result get_template_base (tree, tree, tree, tree,
     194              :                                                     bool , tree *);
     195              : static tree try_class_unification (tree, tree, tree, tree, bool);
     196              : static bool class_nttp_const_wrapper_p (tree t);
     197              : static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
     198              :                                            tree, tree);
     199              : static bool template_template_parm_bindings_ok_p (tree, tree);
     200              : static void tsubst_default_arguments (tree, tsubst_flags_t);
     201              : static tree for_each_template_parm_r (tree *, int *, void *);
     202              : static tree copy_default_args_to_explicit_spec_1 (tree, tree);
     203              : static void copy_default_args_to_explicit_spec (tree);
     204              : static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
     205              : static bool dependent_type_p_r (tree);
     206              : static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree);
     207              : static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true);
     208              : static tree tsubst_scope (tree, tree, tsubst_flags_t, tree);
     209              : static tree tsubst_name (tree, tree, tsubst_flags_t, tree);
     210              : static void perform_instantiation_time_access_checks (tree, tree);
     211              : static tree listify (tree);
     212              : static tree listify_autos (tree, tree);
     213              : static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
     214              : static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
     215              : static tree get_underlying_template (tree);
     216              : static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
     217              : static tree canonicalize_expr_argument (tree, tsubst_flags_t);
     218              : static tree make_argument_pack (tree);
     219              : static tree enclosing_instantiation_of (tree tctx);
     220              : static void instantiate_body (tree pattern, tree args, tree d, bool nested);
     221              : static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
     222              : static void mark_template_arguments_used (tree, tree);
     223              : static bool uses_outer_template_parms (tree);
     224              : static tree alias_ctad_tweaks (tree, tree);
     225              : static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t);
     226              : static tree deduction_guides_for (tree, bool&, tsubst_flags_t);
     227              : static void mark_template_arguments_used_1 (tree);
     228              : 
     229              : /* Make the current scope suitable for access checking when we are
     230              :    processing T.  T can be FUNCTION_DECL for instantiated function
     231              :    template, VAR_DECL for static member variable, or TYPE_DECL for
     232              :    for a class or alias template (needed by instantiate_decl).  */
     233              : 
     234              : void
     235    283959480 : push_access_scope (tree t)
     236              : {
     237    283959480 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
     238              :               || TREE_CODE (t) == TYPE_DECL);
     239              : 
     240    464195975 :   if (DECL_FRIEND_CONTEXT (t))
     241      7189106 :     push_nested_class (DECL_FRIEND_CONTEXT (t));
     242     72439346 :   else if (DECL_IMPLICIT_TYPEDEF_P (t)
     243    300424594 :            && CLASS_TYPE_P (TREE_TYPE (t)))
     244     20059667 :     push_nested_class (TREE_TYPE (t));
     245    260305260 :   else if (DECL_CLASS_SCOPE_P (t))
     246     51388370 :     push_nested_class (DECL_CONTEXT (t));
     247    208916890 :   else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
     248              :     /* An artificial deduction guide should have the same access as
     249              :        the constructor.  */
     250        56914 :     push_nested_class (TREE_TYPE (TREE_TYPE (t)));
     251              :   else
     252    208859976 :     push_to_top_level ();
     253              : 
     254    283959480 :   if (TREE_CODE (t) == FUNCTION_DECL)
     255              :     {
     256    180587494 :       vec_safe_push (saved_access_scope, current_function_decl);
     257    180587494 :       current_function_decl = t;
     258              :     }
     259    283959480 : }
     260              : 
     261              : /* Restore the scope set up by push_access_scope.  T is the node we
     262              :    are processing.  */
     263              : 
     264              : void
     265    283956780 : pop_access_scope (tree t)
     266              : {
     267    283956780 :   if (TREE_CODE (t) == FUNCTION_DECL)
     268    180584794 :     current_function_decl = saved_access_scope->pop();
     269              : 
     270    464190575 :   if (DECL_FRIEND_CONTEXT (t)
     271    280362227 :       || (DECL_IMPLICIT_TYPEDEF_P (t)
     272     20059667 :           && CLASS_TYPE_P (TREE_TYPE (t)))
     273    260302560 :       || DECL_CLASS_SCOPE_P (t)
     274    373218718 :       || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
     275     75099504 :     pop_nested_class ();
     276              :   else
     277    208857276 :     pop_from_top_level ();
     278    283956780 : }
     279              : 
     280              : /* Return current function, ignoring temporary overrides
     281              :    of current_function_decl by push_access_scope.  */
     282              : 
     283              : tree
     284          290 : current_function_decl_without_access_scope ()
     285              : {
     286          290 :   if (vec_safe_length (saved_access_scope))
     287            2 :     return (*saved_access_scope)[0];
     288              :   else
     289          288 :     return current_function_decl;
     290              : }
     291              : 
     292              : /* Do any processing required when DECL (a member template
     293              :    declaration) is finished.  Returns the TEMPLATE_DECL corresponding
     294              :    to DECL, unless it is a specialization, in which case the DECL
     295              :    itself is returned.  */
     296              : 
     297              : tree
     298     22896098 : finish_member_template_decl (tree decl)
     299              : {
     300     22896098 :   if (decl == error_mark_node)
     301              :     return error_mark_node;
     302              : 
     303     22896016 :   gcc_assert (DECL_P (decl));
     304              : 
     305     22896016 :   if (TREE_CODE (decl) == TYPE_DECL)
     306              :     {
     307      1387289 :       tree type;
     308              : 
     309      1387289 :       type = TREE_TYPE (decl);
     310      1387289 :       if (type == error_mark_node)
     311              :         return error_mark_node;
     312      1387289 :       if (MAYBE_CLASS_TYPE_P (type)
     313      1387289 :           && CLASSTYPE_TEMPLATE_INFO (type)
     314      1387289 :           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     315              :         {
     316       816559 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     317       816559 :           check_member_template (tmpl);
     318       816559 :           return tmpl;
     319              :         }
     320              :       return NULL_TREE;
     321              :     }
     322     21508727 :   else if (TREE_CODE (decl) == FIELD_DECL)
     323            3 :     error_at (DECL_SOURCE_LOCATION (decl),
     324              :               "data member %qD cannot be a member template", decl);
     325     21508724 :   else if (DECL_TEMPLATE_INFO (decl))
     326              :     {
     327     21508721 :       if (!DECL_TEMPLATE_SPECIALIZATION (decl))
     328              :         {
     329     21463953 :           check_member_template (DECL_TI_TEMPLATE (decl));
     330     21463953 :           return DECL_TI_TEMPLATE (decl);
     331              :         }
     332              :       else
     333              :         return NULL_TREE;
     334              :     }
     335              :   else
     336            3 :     error_at (DECL_SOURCE_LOCATION (decl),
     337              :               "invalid member template declaration %qD", decl);
     338              : 
     339            6 :   return error_mark_node;
     340              : }
     341              : 
     342              : /* Create a template info node.  */
     343              : 
     344              : tree
     345    732722874 : build_template_info (tree template_decl, tree template_args)
     346              : {
     347    732722874 :   tree result = make_node (TEMPLATE_INFO);
     348    732722874 :   TI_TEMPLATE (result) = template_decl;
     349    732722874 :   TI_ARGS (result) = template_args;
     350    732722874 :   return result;
     351              : }
     352              : 
     353              : /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE.  */
     354              : 
     355              : static tree
     356   2813047309 : decl_template_info (const_tree decl)
     357              : {
     358              :   /* This needs to match template_info_decl_check.  */
     359   2813047309 :   if (DECL_LANG_SPECIFIC (decl))
     360   2093687716 :     switch (TREE_CODE (decl))
     361              :       {
     362   1637107150 :       case FUNCTION_DECL:
     363   1637107150 :         if (DECL_THUNK_P (decl))
     364              :           break;
     365   2093683726 :         gcc_fallthrough ();
     366   2093683726 :       case VAR_DECL:
     367   2093683726 :       case FIELD_DECL:
     368   2093683726 :       case TYPE_DECL:
     369   2093683726 :       case CONCEPT_DECL:
     370   2093683726 :       case TEMPLATE_DECL:
     371   2093683726 :         return DECL_TEMPLATE_INFO (decl);
     372              : 
     373              :       default:
     374              :         break;
     375              :       }
     376              :   return NULL_TREE;
     377              : }
     378              : 
     379              : /* Return the template info node corresponding to T, whatever T is.  */
     380              : 
     381              : tree
     382   3720164308 : get_template_info (const_tree t)
     383              : {
     384   3720164308 :   tree tinfo = NULL_TREE;
     385              : 
     386   3720164308 :   if (!t || t == error_mark_node)
     387              :     return NULL;
     388              : 
     389   3720164306 :   if (TREE_CODE (t) == NAMESPACE_DECL
     390   3580792832 :       || TREE_CODE (t) == PARM_DECL)
     391              :     return NULL;
     392              : 
     393   3580792654 :   if (DECL_P (t))
     394   2813047309 :     tinfo = decl_template_info (t);
     395              : 
     396   3580792654 :   if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
     397    715012602 :     t = TREE_TYPE (t);
     398              : 
     399   3580792654 :   if (OVERLOAD_TYPE_P (t))
     400   1441520170 :     tinfo = TYPE_TEMPLATE_INFO (t);
     401   2139272484 :   else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
     402           11 :     tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
     403              : 
     404              :   return tinfo;
     405              : }
     406              : 
     407              : /* Returns the template nesting level of the indicated class TYPE.
     408              : 
     409              :    For example, in:
     410              :      template <class T>
     411              :      struct A
     412              :      {
     413              :        template <class U>
     414              :        struct B {};
     415              :      };
     416              : 
     417              :    A<T>::B<U> has depth two, while A<T> has depth one.
     418              :    Both A<T>::B<int> and A<int>::B<U> have depth one, if
     419              :    they are instantiations, not specializations.
     420              : 
     421              :    This function is guaranteed to return 0 if passed NULL_TREE so
     422              :    that, for example, `template_class_depth (current_class_type)' is
     423              :    always safe.  */
     424              : 
     425              : int
     426    840550390 : template_class_depth (tree type)
     427              : {
     428    840550390 :   int depth;
     429              : 
     430   1183487189 :   for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
     431              :     {
     432    342936799 :       tree tinfo = get_template_info (type);
     433              : 
     434    342936799 :       if (tinfo
     435    296754477 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
     436    296754459 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
     437    630710537 :           && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
     438    258326442 :         ++depth;
     439              : 
     440    342936799 :       if (DECL_P (type))
     441              :         {
     442      5179556 :           if (tree fctx = DECL_FRIEND_CONTEXT (type))
     443              :             type = fctx;
     444              :           else
     445      2682400 :             type = CP_DECL_CONTEXT (type);
     446              :         }
     447    681632962 :       else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
     448      4382188 :         type = LAMBDA_TYPE_EXTRA_SCOPE (type);
     449              :       else
     450    338041834 :         type = CP_TYPE_CONTEXT (type);
     451              :     }
     452              : 
     453    840550390 :   return depth;
     454              : }
     455              : 
     456              : /* Return TRUE if NODE instantiates a template that has arguments of
     457              :    its own, be it directly a primary template or indirectly through a
     458              :    partial specializations.  */
     459              : static bool
     460    167383614 : instantiates_primary_template_p (tree node)
     461              : {
     462    167383614 :   tree tinfo = get_template_info (node);
     463    167383614 :   if (!tinfo)
     464              :     return false;
     465              : 
     466    167367966 :   tree tmpl = TI_TEMPLATE (tinfo);
     467    167367966 :   if (PRIMARY_TEMPLATE_P (tmpl))
     468              :     return true;
     469              : 
     470    138496970 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
     471              :     return false;
     472              : 
     473              :   /* So now we know we have a specialization, but it could be a full
     474              :      or a partial specialization.  To tell which, compare the depth of
     475              :      its template arguments with those of its context.  */
     476              : 
     477            0 :   tree ctxt = DECL_CONTEXT (tmpl);
     478            0 :   tree ctinfo = get_template_info (ctxt);
     479            0 :   if (!ctinfo)
     480              :     return true;
     481              : 
     482            0 :   return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
     483            0 :           > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
     484              : }
     485              : 
     486              : /* Subroutine of maybe_begin_member_template_processing.
     487              :    Returns true if processing DECL needs us to push template parms.  */
     488              : 
     489              : static bool
     490    208077556 : inline_needs_template_parms (tree decl, bool nsdmi)
     491              : {
     492    208077556 :   if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
     493              :     return false;
     494              : 
     495    172633423 :   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
     496    172633423 :           > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
     497              : }
     498              : 
     499              : /* Subroutine of maybe_begin_member_template_processing.
     500              :    Push the template parms in PARMS, starting from LEVELS steps into the
     501              :    chain, and ending at the beginning, since template parms are listed
     502              :    innermost first.  */
     503              : 
     504              : static void
     505     48913720 : push_inline_template_parms_recursive (tree parmlist, int levels)
     506              : {
     507     48913720 :   tree parms = TREE_VALUE (parmlist);
     508     48913720 :   int i;
     509              : 
     510     48913720 :   if (levels > 1)
     511       592129 :     push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
     512              : 
     513     48913720 :   ++processing_template_decl;
     514     97827440 :   current_template_parms
     515     48913720 :     = tree_cons (size_int (current_template_depth + 1),
     516              :                  parms, current_template_parms);
     517     97827440 :   TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
     518     48913720 :     = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
     519     48913720 :   TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
     520              : 
     521     48913720 :   begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
     522              :                NULL);
     523    182512249 :   for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
     524              :     {
     525     84684809 :       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
     526              : 
     527     84684809 :       if (error_operand_p (parm))
     528           64 :         continue;
     529              : 
     530     84684745 :       gcc_assert (DECL_P (parm));
     531              : 
     532     84684745 :       switch (TREE_CODE (parm))
     533              :         {
     534     80362660 :         case TYPE_DECL:
     535     80362660 :         case TEMPLATE_DECL:
     536     80362660 :           pushdecl (parm);
     537     80362660 :           break;
     538              : 
     539      4322085 :         case PARM_DECL:
     540              :           /* Push the CONST_DECL.  */
     541      4322085 :           pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
     542      4322085 :           break;
     543              : 
     544            0 :         default:
     545            0 :           gcc_unreachable ();
     546              :         }
     547              :     }
     548     48913720 : }
     549              : 
     550              : /* Restore the template parameter context for a member template, a
     551              :    friend template defined in a class definition, or a non-template
     552              :    member of template class.  */
     553              : 
     554              : void
     555    208077556 : maybe_begin_member_template_processing (tree decl)
     556              : {
     557    208077556 :   tree parms;
     558    208077556 :   int levels = 0;
     559    208077556 :   bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
     560              : 
     561    208077556 :   if (nsdmi || decl_specialization_friend_p (decl))
     562              :     {
     563      2424164 :       tree ctx = nsdmi ? DECL_CONTEXT (decl) : DECL_CHAIN (decl);
     564      2424164 :       decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
     565              :               /* Disregard full specializations (c++/60999).  */
     566      1817602 :               && uses_template_parms (ctx)
     567      4230919 :               ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
     568              :     }
     569              : 
     570    208077556 :   if (inline_needs_template_parms (decl, nsdmi))
     571              :     {
     572     48321591 :       parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
     573     48321591 :       levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
     574              : 
     575     48321591 :       if (DECL_TEMPLATE_SPECIALIZATION (decl))
     576              :         {
     577            0 :           --levels;
     578            0 :           parms = TREE_CHAIN (parms);
     579              :         }
     580              : 
     581     48321591 :       push_inline_template_parms_recursive (parms, levels);
     582              :     }
     583              : 
     584              :   /* Remember how many levels of template parameters we pushed so that
     585              :      we can pop them later.  */
     586    208077556 :   inline_parm_levels.safe_push (levels);
     587    208077556 : }
     588              : 
     589              : /* Undo the effects of maybe_begin_member_template_processing.  */
     590              : 
     591              : void
     592    208077994 : maybe_end_member_template_processing (void)
     593              : {
     594    208077994 :   int i;
     595    208077994 :   int last;
     596              : 
     597    208077994 :   if (inline_parm_levels.length () == 0)
     598              :     return;
     599              : 
     600    208077556 :   last = inline_parm_levels.pop ();
     601    256991276 :   for (i = 0; i < last; ++i)
     602              :     {
     603     48913720 :       --processing_template_decl;
     604     48913720 :       current_template_parms = TREE_CHAIN (current_template_parms);
     605     48913720 :       poplevel (0, 0, 0);
     606              :     }
     607              : }
     608              : 
     609              : /* Return a new template argument vector which contains all of ARGS,
     610              :    but has as its innermost set of arguments the EXTRA_ARGS.  */
     611              : 
     612              : tree
     613     74151797 : add_to_template_args (tree args, tree extra_args)
     614              : {
     615     74151797 :   tree new_args;
     616     74151797 :   int extra_depth;
     617     74151797 :   int i;
     618     74151797 :   int j;
     619              : 
     620     74151797 :   if (args == NULL_TREE || extra_args == error_mark_node)
     621              :     return extra_args;
     622              : 
     623    112396056 :   extra_depth = TMPL_ARGS_DEPTH (extra_args);
     624     56198028 :   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
     625              : 
     626    191563839 :   for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
     627     56727859 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
     628              : 
     629    112396056 :   for (j = 1; j <= extra_depth; ++j, ++i)
     630    112396056 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
     631              : 
     632              :   return new_args;
     633              : }
     634              : 
     635              : /* Like add_to_template_args, but only the outermost ARGS are added to
     636              :    the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
     637              :    (EXTRA_ARGS) levels are added.  This function is used to combine
     638              :    the template arguments from a partial instantiation with the
     639              :    template arguments used to attain the full instantiation from the
     640              :    partial instantiation.
     641              : 
     642              :    If ARGS is a TEMPLATE_DECL, use its parameters as args.  */
     643              : 
     644              : tree
     645   1386369492 : add_outermost_template_args (tree args, tree extra_args)
     646              : {
     647   1386369492 :   tree new_args;
     648              : 
     649   1386369492 :   if (!args)
     650              :     return extra_args;
     651   1386369492 :   if (TREE_CODE (args) == TEMPLATE_DECL)
     652              :     {
     653    451822867 :       tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
     654    451822867 :       args = TI_ARGS (ti);
     655              :     }
     656              : 
     657              :   /* If there are more levels of EXTRA_ARGS than there are ARGS,
     658              :      something very fishy is going on.  */
     659   5929722034 :   gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
     660              : 
     661              :   /* If *all* the new arguments will be the EXTRA_ARGS, just return
     662              :      them.  */
     663   6864268659 :   if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
     664              :     return extra_args;
     665              : 
     666              :   /* For the moment, we make ARGS look like it contains fewer levels.  */
     667     89115566 :   TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
     668              : 
     669     44557783 :   new_args = add_to_template_args (args, extra_args);
     670              : 
     671              :   /* Now, we restore ARGS to its full dimensions.  */
     672     89115566 :   TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
     673              : 
     674     44557783 :   return new_args;
     675              : }
     676              : 
     677              : /* Return the N levels of innermost template arguments from the ARGS.  */
     678              : 
     679              : tree
     680   6077964915 : get_innermost_template_args (tree args, int n)
     681              : {
     682   6077964915 :   tree new_args;
     683   6077964915 :   int extra_levels;
     684   6077964915 :   int i;
     685              : 
     686   6077964915 :   gcc_assert (n >= 0);
     687              : 
     688              :   /* If N is 1, just return the innermost set of template arguments.  */
     689   6077964915 :   if (n == 1)
     690  12155793418 :     return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
     691              : 
     692              :   /* If we're not removing anything, just return the arguments we were
     693              :      given.  */
     694       136412 :   extra_levels = TMPL_ARGS_DEPTH (args) - n;
     695        68206 :   gcc_assert (extra_levels >= 0);
     696        68206 :   if (extra_levels == 0)
     697              :     return args;
     698              : 
     699              :   /* Make a new set of arguments, not containing the outer arguments.  */
     700          778 :   new_args = make_tree_vec (n);
     701         3112 :   for (i = 1; i <= n; ++i)
     702         3112 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     703              :                          TMPL_ARGS_LEVEL (args, i + extra_levels));
     704              : 
     705              :   return new_args;
     706              : }
     707              : 
     708              : /* The inverse of get_innermost_template_args: Return all but the innermost
     709              :    EXTRA_LEVELS levels of template arguments from the ARGS.  */
     710              : 
     711              : static tree
     712       508694 : strip_innermost_template_args (tree args, int extra_levels)
     713              : {
     714       508694 :   tree new_args;
     715      1017388 :   int n = TMPL_ARGS_DEPTH (args) - extra_levels;
     716       508694 :   int i;
     717              : 
     718       508694 :   gcc_assert (n >= 0);
     719              : 
     720              :   /* If N is 1, just return the outermost set of template arguments.  */
     721       508694 :   if (n == 1)
     722      1007260 :     return TMPL_ARGS_LEVEL (args, 1);
     723              : 
     724              :   /* If we're not removing anything, just return the arguments we were
     725              :      given.  */
     726         5064 :   gcc_assert (extra_levels >= 0);
     727         5064 :   if (extra_levels == 0)
     728              :     return args;
     729              : 
     730              :   /* Make a new set of arguments, not containing the inner arguments.  */
     731         5064 :   new_args = make_tree_vec (n);
     732        20256 :   for (i = 1; i <= n; ++i)
     733        20256 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     734              :                          TMPL_ARGS_LEVEL (args, i));
     735              : 
     736              :   return new_args;
     737              : }
     738              : 
     739              : /* We've got a template header coming up; push to a new level for storing
     740              :    the parms.  */
     741              : 
     742              : void
     743     95679335 : begin_template_parm_list (void)
     744              : {
     745              :   /* We use a non-tag-transparent scope here, which causes pushtag to
     746              :      put tags in this scope, rather than in the enclosing class or
     747              :      namespace scope.  This is the right thing, since we want
     748              :      TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
     749              :      global template class, push_template_decl handles putting the
     750              :      TEMPLATE_DECL into top-level scope.  For a nested template class,
     751              :      e.g.:
     752              : 
     753              :        template <class T> struct S1 {
     754              :          template <class T> struct S2 {};
     755              :        };
     756              : 
     757              :      pushtag contains special code to insert the TEMPLATE_DECL for S2
     758              :      at the right scope.  */
     759     95679335 :   begin_scope (sk_template_parms, NULL);
     760     95679335 :   ++processing_template_decl;
     761     95679335 :   ++processing_template_parmlist;
     762     95679335 :   note_template_header (0);
     763              : 
     764              :   /* Add a dummy parameter level while we process the parameter list.  */
     765    191358670 :   current_template_parms
     766     95679335 :     = tree_cons (size_int (current_template_depth + 1),
     767              :                  make_tree_vec (0),
     768              :                  current_template_parms);
     769     95679335 : }
     770              : 
     771              : /* This routine is called when a specialization is declared.  If it is
     772              :    invalid to declare a specialization here, an error is reported and
     773              :    false is returned, otherwise this routine will return true.  */
     774              : 
     775              : static bool
     776      6440575 : check_specialization_scope (void)
     777              : {
     778      6440575 :   tree scope = current_scope ();
     779              : 
     780              :   /* [temp.expl.spec]
     781              : 
     782              :      An explicit specialization shall be declared in the namespace of
     783              :      which the template is a member, or, for member templates, in the
     784              :      namespace of which the enclosing class or enclosing class
     785              :      template is a member.  An explicit specialization of a member
     786              :      function, member class or static data member of a class template
     787              :      shall be declared in the namespace of which the class template
     788              :      is a member.  */
     789      6440575 :   if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
     790              :     {
     791           24 :       error ("explicit specialization in non-namespace scope %qD", scope);
     792           24 :       return false;
     793              :     }
     794              : 
     795              :   /* [temp.expl.spec]
     796              : 
     797              :      In an explicit specialization declaration for a member of a class
     798              :      template or a member template that appears in namespace scope,
     799              :      the member template and some of its enclosing class templates may
     800              :      remain unspecialized, except that the declaration shall not
     801              :      explicitly specialize a class member template if its enclosing
     802              :      class templates are not explicitly specialized as well.  */
     803      6440551 :   if (current_template_parms)
     804              :     {
     805            6 :       error ("enclosing class templates are not explicitly specialized");
     806            6 :       return false;
     807              :     }
     808              : 
     809              :   return true;
     810              : }
     811              : 
     812              : /* We've just seen template <>.  */
     813              : 
     814              : bool
     815      6440575 : begin_specialization (void)
     816              : {
     817      6440575 :   begin_scope (sk_template_spec, NULL);
     818      6440575 :   note_template_header (1);
     819     12881150 :   return check_specialization_scope ();
     820              : }
     821              : 
     822              : /* Called at then end of processing a declaration preceded by
     823              :    template<>.  */
     824              : 
     825              : void
     826      6440575 : end_specialization (void)
     827              : {
     828      6440575 :   finish_scope ();
     829      6440575 :   reset_specialization ();
     830      6440575 : }
     831              : 
     832              : /* Any template <>'s that we have seen thus far are not referring to a
     833              :    function specialization.  */
     834              : 
     835              : void
     836    174989592 : reset_specialization (void)
     837              : {
     838    174989592 :   processing_specialization = 0;
     839    174989592 :   template_header_count = 0;
     840    174989592 : }
     841              : 
     842              : /* We've just seen a template header.  If SPECIALIZATION is nonzero,
     843              :    it was of the form template <>.  */
     844              : 
     845              : static void
     846    102119910 : note_template_header (int specialization)
     847              : {
     848    102119910 :   processing_specialization = specialization;
     849    102119910 :   template_header_count++;
     850            0 : }
     851              : 
     852              : /* We're beginning an explicit instantiation.  */
     853              : 
     854              : void
     855      2903185 : begin_explicit_instantiation (void)
     856              : {
     857      2903185 :   gcc_assert (!processing_explicit_instantiation);
     858      2903185 :   processing_explicit_instantiation = true;
     859      2903185 : }
     860              : 
     861              : 
     862              : void
     863      2903182 : end_explicit_instantiation (void)
     864              : {
     865      2903182 :   gcc_assert (processing_explicit_instantiation);
     866      2903182 :   processing_explicit_instantiation = false;
     867      2903182 : }
     868              : 
     869              : /* An explicit specialization or partial specialization of TMPL is being
     870              :    declared.  Check that the namespace in which the specialization is
     871              :    occurring is permissible.  Returns false iff it is invalid to
     872              :    specialize TMPL in the current namespace.  */
     873              : 
     874              : static bool
     875     14760707 : check_specialization_namespace (tree tmpl)
     876              : {
     877     14760707 :   tree tpl_ns = decl_namespace_context (tmpl);
     878              : 
     879              :   /* [tmpl.expl.spec]
     880              : 
     881              :      An explicit specialization shall be declared in a namespace enclosing the
     882              :      specialized template. An explicit specialization whose declarator-id is
     883              :      not qualified shall be declared in the nearest enclosing namespace of the
     884              :      template, or, if the namespace is inline (7.3.1), any namespace from its
     885              :      enclosing namespace set.  */
     886     14760707 :   if (current_scope() != DECL_CONTEXT (tmpl)
     887     14760707 :       && !at_namespace_scope_p ())
     888              :     {
     889           12 :       error ("specialization of %qD must appear at namespace scope", tmpl);
     890           12 :       return false;
     891              :     }
     892              : 
     893     14760695 :   if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
     894              :     /* Same or enclosing namespace.  */
     895              :     return true;
     896              :   else
     897              :     {
     898           14 :       auto_diagnostic_group d;
     899           14 :       if (permerror (input_location,
     900              :                      "specialization of %qD in different namespace", tmpl))
     901           11 :         inform (DECL_SOURCE_LOCATION (tmpl),
     902              :                 "  from definition of %q#D", tmpl);
     903           14 :       return false;
     904           14 :     }
     905              : }
     906              : 
     907              : /* SPEC is an explicit instantiation.  Check that it is valid to
     908              :    perform this explicit instantiation in the current namespace.  */
     909              : 
     910              : static void
     911      2965468 : check_explicit_instantiation_namespace (tree spec)
     912              : {
     913      2965468 :   tree ns;
     914              : 
     915              :   /* DR 275: An explicit instantiation shall appear in an enclosing
     916              :      namespace of its template.  */
     917      2965468 :   ns = decl_namespace_context (spec);
     918      2965468 :   if (!is_nested_namespace (current_namespace, ns))
     919            6 :     permerror (input_location, "explicit instantiation of %qD in namespace %qD "
     920              :                "(which does not enclose namespace %qD)",
     921              :                spec, current_namespace, ns);
     922      2965468 : }
     923              : 
     924              : /* Returns true if TYPE is a new partial specialization that needs to be
     925              :    set up.  This may also modify TYPE to point to the correct (new or
     926              :    existing) constrained partial specialization.  */
     927              : 
     928              : static bool
     929     36324254 : maybe_new_partial_specialization (tree& type)
     930              : {
     931              :   /* An implicit instantiation of an incomplete type implies
     932              :      the definition of a new class template.
     933              : 
     934              :         template<typename T>
     935              :           struct S;
     936              : 
     937              :         template<typename T>
     938              :           struct S<T*>;
     939              : 
     940              :      Here, S<T*> is an implicit instantiation of S whose type
     941              :      is incomplete.  */
     942     36324254 :   if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
     943              :     return true;
     944              : 
     945              :   /* It can also be the case that TYPE is a completed specialization.
     946              :      Continuing the previous example, suppose we also declare:
     947              : 
     948              :         template<typename T>
     949              :           requires Integral<T>
     950              :             struct S<T*>;
     951              : 
     952              :      Here, S<T*> refers to the specialization S<T*> defined
     953              :      above. However, we need to differentiate definitions because
     954              :      we intend to define a new partial specialization. In this case,
     955              :      we rely on the fact that the constraints are different for
     956              :      this declaration than that above.
     957              : 
     958              :      Note that we also get here for injected class names and
     959              :      late-parsed template definitions. We must ensure that we
     960              :      do not create new type declarations for those cases.  */
     961     24379203 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     962              :     {
     963     24379203 :       tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     964     24379203 :       tree args = CLASSTYPE_TI_ARGS (type);
     965              : 
     966              :       /* If there are no template parameters, this cannot be a new
     967              :          partial template specialization?  */
     968     24379203 :       if (!current_template_parms)
     969              :         return false;
     970              : 
     971              :       /* The injected-class-name is not a new partial specialization.  */
     972     15208042 :       if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
     973              :         return false;
     974              : 
     975              :       /* If the constraints are not the same as those of the primary
     976              :          then, we can probably create a new specialization.  */
     977     15208042 :       tree type_constr = current_template_constraints ();
     978              : 
     979     15208042 :       if (type == TREE_TYPE (tmpl))
     980              :         {
     981           52 :           tree main_constr = get_constraints (tmpl);
     982           52 :           if (equivalent_constraints (type_constr, main_constr))
     983              :             return false;
     984              :         }
     985              : 
     986              :       /* Also, if there's a pre-existing specialization with matching
     987              :          constraints, then this also isn't new.  */
     988     15207990 :       tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
     989     15907738 :       while (specs)
     990              :         {
     991     15676159 :           tree spec_tmpl = TREE_VALUE (specs);
     992     15676159 :           tree spec_args = TREE_PURPOSE (specs);
     993     15676159 :           tree spec_constr = get_constraints (spec_tmpl);
     994     15676159 :           tree spec_parms = DECL_TEMPLATE_PARMS (spec_tmpl);
     995              :           /* Per [temp.spec.partial.general]/2, two partial specializations
     996              :              declare the same entity if they have equivalent template-heads
     997              :              and template argument lists.  */
     998     15676159 :           if (comp_template_args (args, spec_args)
     999     15358311 :               && comp_template_parms (spec_parms, current_template_parms)
    1000     31034467 :               && equivalent_constraints (type_constr, spec_constr))
    1001              :             {
    1002     14976411 :               type = TREE_TYPE (spec_tmpl);
    1003     14976411 :               return false;
    1004              :             }
    1005       699748 :           specs = TREE_CHAIN (specs);
    1006              :         }
    1007              : 
    1008              :       /* Create a new type node (and corresponding type decl)
    1009              :          for the newly declared specialization.  */
    1010       231579 :       tree t = make_class_type (TREE_CODE (type));
    1011       231579 :       CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
    1012       231579 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
    1013       231579 :       TYPE_CONTEXT (t) = TYPE_CONTEXT (type);
    1014              : 
    1015              :       /* We only need a separate type node for storing the definition of this
    1016              :          partial specialization; uses of S<T*> are unconstrained, so all are
    1017              :          equivalent.  So keep TYPE_CANONICAL the same.  */
    1018       231579 :       TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
    1019              : 
    1020              :       /* Build the corresponding type decl.  */
    1021       231579 :       tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
    1022       231579 :       DECL_CONTEXT (d) = TYPE_CONTEXT (t);
    1023       231579 :       DECL_SOURCE_LOCATION (d) = input_location;
    1024       231579 :       TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl));
    1025              : 
    1026       231579 :       set_instantiating_module (d);
    1027       231579 :       DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
    1028              : 
    1029       231579 :       type = t;
    1030       231579 :       return true;
    1031              :     }
    1032              : 
    1033              :   return false;
    1034              : }
    1035              : 
    1036              : /* Diagnose if SPEC is a specialization of [[clang::no_specializations]]
    1037              :    template.  */
    1038              : 
    1039              : static void
    1040     14760942 : maybe_diagnose_no_specializations (tree spec)
    1041              : {
    1042     14760942 :   tree tmpl;
    1043     14760942 :   if (TREE_CODE (spec) == TYPE_DECL)
    1044     12176644 :     tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (spec));
    1045              :   else
    1046      2584298 :     tmpl = DECL_TI_TEMPLATE (spec);
    1047     14760942 :   tree t = DECL_TEMPLATE_RESULT (tmpl), attr;
    1048     14760942 :   if (TREE_CODE (t) == TYPE_DECL)
    1049     12176644 :     attr = lookup_attribute ("no_specializations",
    1050     12176644 :                              TYPE_ATTRIBUTES (TREE_TYPE (t)));
    1051              :   else
    1052      2584298 :     attr = lookup_attribute ("no_specializations", DECL_ATTRIBUTES (t));
    1053     14760942 :   if (!attr)
    1054     14729486 :     return;
    1055              : 
    1056        31456 :   auto_diagnostic_group d;
    1057        31456 :   escaped_string msg;
    1058        31456 :   tree args = TREE_VALUE (attr);
    1059        31456 :   bool complained;
    1060        31456 :   if (args)
    1061          144 :     msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
    1062        31456 :   if (msg)
    1063          144 :     complained
    1064          144 :       = permerror_opt (DECL_SOURCE_LOCATION (spec),
    1065          144 :                        OPT_Winvalid_specialization,
    1066              :                        "%qD cannot be specialized: %qs",
    1067              :                        spec, (const char *) msg);
    1068              :   else
    1069        31312 :     complained
    1070        31312 :       = permerror_opt (DECL_SOURCE_LOCATION (spec),
    1071        31312 :                        OPT_Winvalid_specialization,
    1072              :                        "%qD cannot be specialized", spec);
    1073        31456 :   if (complained)
    1074          174 :     inform (DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)),
    1075              :             "declared %qs here", "clang::no_specializations");
    1076        31456 : }
    1077              : 
    1078              : /* The TYPE is being declared.  If it is a template type, that means it
    1079              :    is a partial specialization.  Do appropriate error-checking.  */
    1080              : 
    1081              : tree
    1082     78644891 : maybe_process_partial_specialization (tree type)
    1083              : {
    1084     78644891 :   tree context;
    1085              : 
    1086     78644891 :   if (type == error_mark_node)
    1087              :     return error_mark_node;
    1088              : 
    1089              :   /* A lambda that appears in specialization context is not itself a
    1090              :      specialization.  */
    1091     78644127 :   if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
    1092              :     return type;
    1093              : 
    1094              :   /* An injected-class-name is not a specialization.  */
    1095     76963713 :   if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
    1096              :     return type;
    1097              : 
    1098     76963707 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
    1099              :     {
    1100            3 :       error ("name of class shadows template template parameter %qD",
    1101            3 :              TYPE_NAME (type));
    1102            3 :       return error_mark_node;
    1103              :     }
    1104              : 
    1105     76963704 :   context = TYPE_CONTEXT (type);
    1106              : 
    1107     76963704 :   if (TYPE_ALIAS_P (type))
    1108              :     {
    1109            6 :       tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
    1110              : 
    1111           12 :       if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    1112            6 :         error ("specialization of alias template %qD",
    1113            6 :                TI_TEMPLATE (tinfo));
    1114              :       else
    1115            0 :         error ("explicit specialization of non-template %qT", type);
    1116            6 :       return error_mark_node;
    1117              :     }
    1118     76963698 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
    1119              :     {
    1120              :       /* This is for ordinary explicit specialization and partial
    1121              :          specialization of a template class such as:
    1122              : 
    1123              :            template <> class C<int>;
    1124              : 
    1125              :          or:
    1126              : 
    1127              :            template <class T> class C<T*>;
    1128              : 
    1129              :          Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
    1130              : 
    1131     36324254 :       if (maybe_new_partial_specialization (type))
    1132              :         {
    1133     12176630 :           if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
    1134     12176630 :               && !at_namespace_scope_p ())
    1135           12 :             return error_mark_node;
    1136     12176618 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1137     12176618 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1138     12176618 :           maybe_diagnose_no_specializations (TYPE_NAME (type));
    1139     12176618 :           if (processing_template_decl)
    1140              :             {
    1141      7484359 :               tree decl = push_template_decl (TYPE_MAIN_DECL (type));
    1142      7484359 :               if (decl == error_mark_node)
    1143              :                 return error_mark_node;
    1144      7484271 :               return TREE_TYPE (decl);
    1145              :             }
    1146              :         }
    1147     24147624 :       else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
    1148            0 :         error ("specialization of %qT after instantiation", type);
    1149         7370 :       else if (errorcount && !processing_specialization
    1150         5433 :                 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
    1151     24153057 :                && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
    1152              :         /* Trying to define a specialization either without a template<> header
    1153              :            or in an inappropriate place.  We've already given an error, so just
    1154              :            bail now so we don't actually define the specialization.  */
    1155         1893 :         return error_mark_node;
    1156              :     }
    1157     39683180 :   else if (CLASS_TYPE_P (type)
    1158     39683180 :            && !CLASSTYPE_USE_TEMPLATE (type)
    1159     39683180 :            && CLASSTYPE_TEMPLATE_INFO (type)
    1160     26349416 :            && context && CLASS_TYPE_P (context)
    1161     43357463 :            && CLASSTYPE_TEMPLATE_INFO (context))
    1162              :     {
    1163              :       /* This is for an explicit specialization of member class
    1164              :          template according to [temp.expl.spec/18]:
    1165              : 
    1166              :            template <> template <class U> class C<int>::D;
    1167              : 
    1168              :          The context `C<int>' must be an implicit instantiation.
    1169              :          Otherwise this is just a member class template declared
    1170              :          earlier like:
    1171              : 
    1172              :            template <> class C<int> { template <class U> class D; };
    1173              :            template <> template <class U> class C<int>::D;
    1174              : 
    1175              :          In the first case, `C<int>::D' is a specialization of `C<T>::D'
    1176              :          while in the second case, `C<int>::D' is a primary template
    1177              :          and `C<T>::D' may not exist.  */
    1178              : 
    1179      2184762 :       if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
    1180      2184762 :           && !COMPLETE_TYPE_P (type))
    1181              :         {
    1182           32 :           tree t;
    1183           32 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
    1184              : 
    1185           32 :           if (current_namespace
    1186           32 :               != decl_namespace_context (tmpl))
    1187              :             {
    1188            0 :               auto_diagnostic_group d;
    1189            0 :               if (permerror (input_location,
    1190              :                              "specialization of %qD in different namespace",
    1191              :                              type))
    1192            0 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    1193              :                         "from definition of %q#D", tmpl);
    1194            0 :             }
    1195              : 
    1196              :           /* Check for invalid specialization after instantiation:
    1197              : 
    1198              :                template <> template <> class C<int>::D<int>;
    1199              :                template <> template <class U> class C<int>::D;  */
    1200              : 
    1201           32 :           for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
    1202           43 :                t; t = TREE_CHAIN (t))
    1203              :             {
    1204           11 :               tree inst = TREE_VALUE (t);
    1205           11 :               if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
    1206           11 :                   || !COMPLETE_OR_OPEN_TYPE_P (inst))
    1207              :                 {
    1208              :                   /* We already have a full specialization of this partial
    1209              :                      instantiation, or a full specialization has been
    1210              :                      looked up but not instantiated.  Reassign it to the
    1211              :                      new member specialization template.  */
    1212            8 :                   spec_entry elt;
    1213            8 :                   spec_entry *entry;
    1214              : 
    1215            8 :                   elt.tmpl = most_general_template (tmpl);
    1216            8 :                   elt.args = CLASSTYPE_TI_ARGS (inst);
    1217            8 :                   elt.spec = inst;
    1218              : 
    1219            8 :                   type_specializations->remove_elt (&elt);
    1220              : 
    1221            8 :                   elt.tmpl = tmpl;
    1222            8 :                   CLASSTYPE_TI_ARGS (inst)
    1223            8 :                     = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
    1224            8 :                   elt.hash = 0; /* Recalculate after changing tmpl/args.  */
    1225              : 
    1226            8 :                   spec_entry **slot
    1227            8 :                     = type_specializations->find_slot (&elt, INSERT);
    1228            8 :                   entry = ggc_alloc<spec_entry> ();
    1229            8 :                   *entry = elt;
    1230            8 :                   *slot = entry;
    1231              :                 }
    1232              :               else
    1233              :                 /* But if we've had an implicit instantiation, that's a
    1234              :                    problem ([temp.expl.spec]/6).  */
    1235            3 :                 error ("specialization %qT after instantiation %qT",
    1236              :                        type, inst);
    1237              :             }
    1238              : 
    1239              :           /* Make sure that the specialization is valid.  */
    1240           32 :           if (!redeclare_class_template (type, current_template_parms,
    1241              :                                          current_template_constraints ()))
    1242            6 :             return error_mark_node;
    1243              : 
    1244              :           /* Mark TYPE as a specialization.  And as a result, we only
    1245              :              have one level of template argument for the innermost
    1246              :              class template.  */
    1247           26 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1248           26 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1249           26 :           CLASSTYPE_TI_ARGS (type)
    1250           26 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1251           26 :           maybe_diagnose_no_specializations (TYPE_NAME (type));
    1252              :         }
    1253              :     }
    1254     38454682 :   else if (processing_specialization)
    1255              :     {
    1256              :        /* Someday C++0x may allow for enum template specialization.  */
    1257           21 :       if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
    1258           43 :           && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
    1259           12 :         pedwarn (input_location, OPT_Wpedantic, "template specialization "
    1260              :                  "of %qD not allowed by ISO C++", type);
    1261              :       else
    1262              :         {
    1263           12 :           error ("explicit specialization of non-template %qT", type);
    1264           12 :           return error_mark_node;
    1265              :         }
    1266              :     }
    1267              : 
    1268     69477416 :   return type;
    1269              : }
    1270              : 
    1271              : /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
    1272              :    gone through coerce_template_parms by now.  */
    1273              : 
    1274              : static void
    1275    718380495 : verify_unstripped_args_1 (tree inner)
    1276              : {
    1277   2158080153 :   for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
    1278              :     {
    1279   1439699658 :       tree arg = TREE_VEC_ELT (inner, i);
    1280   1439699658 :       if (TREE_CODE (arg) == TEMPLATE_DECL || REFLECT_EXPR_P (arg))
    1281              :         /* OK */;
    1282   1438338774 :       else if (TYPE_P (arg))
    1283   1312851949 :         gcc_assert (strip_typedefs (arg, NULL) == arg);
    1284    125486825 :       else if (ARGUMENT_PACK_P (arg))
    1285      3170867 :         verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
    1286    122315958 :       else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
    1287              :         /* Allow typedefs on the type of a non-type argument, since a
    1288              :            parameter can have them.  */;
    1289              :       else
    1290    122315302 :         gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
    1291              :     }
    1292    718380495 : }
    1293              : 
    1294              : static void
    1295    825410273 : verify_unstripped_args (tree args)
    1296              : {
    1297    825410273 :   ++processing_template_decl;
    1298    825410273 :   if (!any_dependent_template_arguments_p (args))
    1299    715209628 :     verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
    1300    825410273 :   --processing_template_decl;
    1301    825410273 : }
    1302              : 
    1303              : /* Retrieve the specialization (in the sense of [temp.spec] - a
    1304              :    specialization is either an instantiation or an explicit
    1305              :    specialization) of TMPL for the given template ARGS.  If there is
    1306              :    no such specialization, return NULL_TREE.  The ARGS are a vector of
    1307              :    arguments, or a vector of vectors of arguments, in the case of
    1308              :    templates with more than one level of parameters.
    1309              : 
    1310              :    If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
    1311              :    then we search for a partial specialization matching ARGS.  This
    1312              :    parameter is ignored if TMPL is not a class template.
    1313              : 
    1314              :    We can also look up a FIELD_DECL, if it is a lambda capture pack; the
    1315              :    result is a NONTYPE_ARGUMENT_PACK.  */
    1316              : 
    1317              : static tree
    1318    825410273 : retrieve_specialization (tree tmpl, tree args, hashval_t hash)
    1319              : {
    1320    825410273 :   if (tmpl == NULL_TREE)
    1321              :     return NULL_TREE;
    1322              : 
    1323    825410273 :   if (args == error_mark_node)
    1324              :     return NULL_TREE;
    1325              : 
    1326    825410273 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
    1327              :               || TREE_CODE (tmpl) == FIELD_DECL);
    1328              : 
    1329              :   /* There should be as many levels of arguments as there are
    1330              :      levels of parameters.  */
    1331   1650820546 :   gcc_assert (TMPL_ARGS_DEPTH (args)
    1332              :               == (TREE_CODE (tmpl) == TEMPLATE_DECL
    1333              :                   ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
    1334              :                   : template_class_depth (DECL_CONTEXT (tmpl))));
    1335              : 
    1336    825410273 :   if (flag_checking)
    1337    825410273 :     verify_unstripped_args (args);
    1338              : 
    1339              :   /* Lambda functions in templates aren't instantiated normally, but through
    1340              :      tsubst_lambda_expr.  */
    1341    825410273 :   if (lambda_fn_in_template_p (tmpl))
    1342              :     return NULL_TREE;
    1343              : 
    1344    825410273 :   spec_entry elt;
    1345    825410273 :   elt.tmpl = tmpl;
    1346    825410273 :   elt.args = args;
    1347    825410273 :   elt.hash = hash;
    1348              : 
    1349    825410273 :   spec_hash_table *specializations;
    1350    825410273 :   if (DECL_CLASS_TEMPLATE_P (tmpl))
    1351      1808337 :     specializations = type_specializations;
    1352              :   else
    1353    823601936 :     specializations = decl_specializations;
    1354              : 
    1355    825410273 :   if (spec_entry *found = specializations->find (&elt))
    1356    409022200 :     return found->spec;
    1357              : 
    1358              :   return NULL_TREE;
    1359              : }
    1360              : 
    1361              : /* Like retrieve_specialization, but for local declarations.  */
    1362              : 
    1363              : tree
    1364    199146075 : retrieve_local_specialization (tree tmpl)
    1365              : {
    1366    199146075 :   if (local_specializations == NULL)
    1367              :     return NULL_TREE;
    1368              : 
    1369    193516799 :   tree *slot = local_specializations->get (tmpl);
    1370    193516799 :   return slot ? *slot : NULL_TREE;
    1371              : }
    1372              : 
    1373              : /* Returns nonzero iff DECL is a specialization of TMPL.  */
    1374              : 
    1375              : int
    1376       869209 : is_specialization_of (tree decl, tree tmpl)
    1377              : {
    1378       869209 :   tree t;
    1379              : 
    1380       869209 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    1381              :     {
    1382       534560 :       for (t = decl;
    1383       455476 :            t != NULL_TREE;
    1384       306221 :            t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
    1385       455476 :         if (t == tmpl)
    1386              :           return 1;
    1387              :     }
    1388              :   else
    1389              :     {
    1390       642072 :       gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    1391              : 
    1392      1124583 :       for (t = TREE_TYPE (decl);
    1393      1124583 :            t != NULL_TREE;
    1394       893568 :            t = CLASSTYPE_USE_TEMPLATE (t)
    1395       893568 :              ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
    1396      1124583 :         if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
    1397              :           return 1;
    1398              :     }
    1399              : 
    1400              :   return 0;
    1401              : }
    1402              : 
    1403              : /* Returns nonzero iff DECL is a specialization of friend declaration
    1404              :    FRIEND_DECL according to [temp.friend].  */
    1405              : 
    1406              : bool
    1407      1297162 : is_specialization_of_friend (tree decl, tree friend_decl)
    1408              : {
    1409      1297162 :   bool need_template = true;
    1410      1297162 :   int template_depth;
    1411              : 
    1412      1297162 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
    1413              :               || TREE_CODE (decl) == TYPE_DECL);
    1414              : 
    1415              :   /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
    1416              :      of a template class, we want to check if DECL is a specialization
    1417              :      if this.  */
    1418      1297162 :   if (TREE_CODE (friend_decl) == FUNCTION_DECL
    1419       428148 :       && DECL_CLASS_SCOPE_P (friend_decl)
    1420           54 :       && DECL_TEMPLATE_INFO (friend_decl)
    1421      1297216 :       && !DECL_USE_TEMPLATE (friend_decl))
    1422              :     {
    1423              :       /* We want a TEMPLATE_DECL for `is_specialization_of'.  */
    1424           54 :       friend_decl = DECL_TI_TEMPLATE (friend_decl);
    1425           54 :       need_template = false;
    1426              :     }
    1427      1297108 :   else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
    1428      1297108 :            && !PRIMARY_TEMPLATE_P (friend_decl))
    1429              :     need_template = false;
    1430              : 
    1431              :   /* There is nothing to do if this is not a template friend.  */
    1432      1297162 :   if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
    1433              :     return false;
    1434              : 
    1435       869068 :   if (is_specialization_of (decl, friend_decl))
    1436              :     return true;
    1437              : 
    1438              :   /* [temp.friend/6]
    1439              :      A member of a class template may be declared to be a friend of a
    1440              :      non-template class.  In this case, the corresponding member of
    1441              :      every specialization of the class template is a friend of the
    1442              :      class granting friendship.
    1443              : 
    1444              :      For example, given a template friend declaration
    1445              : 
    1446              :        template <class T> friend void A<T>::f();
    1447              : 
    1448              :      the member function below is considered a friend
    1449              : 
    1450              :        template <> struct A<int> {
    1451              :          void f();
    1452              :        };
    1453              : 
    1454              :      For this type of template friend, TEMPLATE_DEPTH below will be
    1455              :      nonzero.  To determine if DECL is a friend of FRIEND, we first
    1456              :      check if the enclosing class is a specialization of another.  */
    1457              : 
    1458       488936 :   template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
    1459       488936 :   if (template_depth
    1460          147 :       && DECL_CLASS_SCOPE_P (decl)
    1461       489077 :       && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
    1462          141 :                                CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
    1463              :     {
    1464              :       /* Next, we check the members themselves.  In order to handle
    1465              :          a few tricky cases, such as when FRIEND_DECL's are
    1466              : 
    1467              :            template <class T> friend void A<T>::g(T t);
    1468              :            template <class T> template <T t> friend void A<T>::h();
    1469              : 
    1470              :          and DECL's are
    1471              : 
    1472              :            void A<int>::g(int);
    1473              :            template <int> void A<int>::h();
    1474              : 
    1475              :          we need to figure out ARGS, the template arguments from
    1476              :          the context of DECL.  This is required for template substitution
    1477              :          of `T' in the function parameter of `g' and template parameter
    1478              :          of `h' in the above examples.  Here ARGS corresponds to `int'.  */
    1479              : 
    1480          138 :       tree context = DECL_CONTEXT (decl);
    1481          138 :       tree args = NULL_TREE;
    1482          138 :       int current_depth = 0;
    1483              : 
    1484          276 :       while (current_depth < template_depth)
    1485              :         {
    1486          138 :           if (CLASSTYPE_TEMPLATE_INFO (context))
    1487              :             {
    1488          138 :               if (current_depth == 0)
    1489          276 :                 args = TYPE_TI_ARGS (context);
    1490              :               else
    1491            0 :                 args = add_to_template_args (TYPE_TI_ARGS (context), args);
    1492          138 :               current_depth++;
    1493              :             }
    1494          138 :           context = TYPE_CONTEXT (context);
    1495              :         }
    1496              : 
    1497          138 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    1498              :         {
    1499           72 :           bool is_template;
    1500           72 :           tree friend_type;
    1501           72 :           tree decl_type;
    1502           72 :           tree friend_args_type;
    1503           72 :           tree decl_args_type;
    1504              : 
    1505              :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1506              :              non-templates.  */
    1507           72 :           is_template = DECL_TEMPLATE_INFO (decl)
    1508           72 :                         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
    1509           72 :           if (need_template ^ is_template)
    1510              :             return false;
    1511           54 :           else if (is_template)
    1512              :             {
    1513              :               /* If both are templates, check template parameter list.  */
    1514           27 :               tree friend_parms
    1515           27 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1516              :                                          args, tf_none);
    1517           27 :               if (!comp_template_parms
    1518           27 :                      (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
    1519              :                       friend_parms))
    1520              :                 return false;
    1521              : 
    1522           18 :               decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
    1523              :             }
    1524              :           else
    1525           27 :             decl_type = TREE_TYPE (decl);
    1526              : 
    1527           45 :           friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
    1528              :                                               tf_none, NULL_TREE);
    1529           45 :           if (friend_type == error_mark_node)
    1530              :             return false;
    1531              : 
    1532              :           /* Check if return types match.  */
    1533           45 :           if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
    1534              :             return false;
    1535              : 
    1536              :           /* Check if function parameter types match, ignoring the
    1537              :              `this' parameter.  */
    1538           39 :           friend_args_type = TYPE_ARG_TYPES (friend_type);
    1539           39 :           decl_args_type = TYPE_ARG_TYPES (decl_type);
    1540           39 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl))
    1541           39 :             friend_args_type = TREE_CHAIN (friend_args_type);
    1542           39 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    1543           39 :             decl_args_type = TREE_CHAIN (decl_args_type);
    1544              : 
    1545           39 :           return compparms (decl_args_type, friend_args_type);
    1546              :         }
    1547              :       else
    1548              :         {
    1549              :           /* DECL is a TYPE_DECL */
    1550           66 :           bool is_template;
    1551           66 :           tree decl_type = TREE_TYPE (decl);
    1552              : 
    1553              :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1554              :              non-templates.  */
    1555           66 :           is_template
    1556           66 :             = CLASSTYPE_TEMPLATE_INFO (decl_type)
    1557           66 :               && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
    1558              : 
    1559           66 :           if (need_template ^ is_template)
    1560              :             return false;
    1561           66 :           else if (is_template)
    1562              :             {
    1563           51 :               tree friend_parms;
    1564              :               /* If both are templates, check the name of the two
    1565              :                  TEMPLATE_DECL's first because is_friend didn't.  */
    1566           51 :               if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
    1567           51 :                   != DECL_NAME (friend_decl))
    1568              :                 return false;
    1569              : 
    1570              :               /* Now check template parameter list.  */
    1571           48 :               friend_parms
    1572           48 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1573              :                                          args, tf_none);
    1574           48 :               return comp_template_parms
    1575           48 :                 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
    1576           48 :                  friend_parms);
    1577              :             }
    1578              :           else
    1579           15 :             return (DECL_NAME (decl)
    1580           15 :                     == DECL_NAME (friend_decl));
    1581              :         }
    1582              :     }
    1583              :   return false;
    1584              : }
    1585              : 
    1586              : /* Register the specialization SPEC as a specialization of TMPL with
    1587              :    the indicated ARGS.  IS_FRIEND indicates whether the specialization
    1588              :    is actually just a friend declaration.  ATTRLIST is the list of
    1589              :    attributes that the specialization is declared with or NULL when
    1590              :    it isn't.  Returns SPEC, or an equivalent prior declaration, if
    1591              :    available.
    1592              : 
    1593              :    We also store instantiations of field packs in the hash table, even
    1594              :    though they are not themselves templates, to make lookup easier.  */
    1595              : 
    1596              : static tree
    1597    291454838 : register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
    1598              :                          hashval_t hash)
    1599              : {
    1600    291454838 :   tree fn;
    1601              : 
    1602    291454838 :   gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
    1603              :               || (TREE_CODE (tmpl) == FIELD_DECL
    1604              :                   && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
    1605              : 
    1606    291454838 :   spec_entry elt;
    1607    291454838 :   elt.tmpl = tmpl;
    1608    291454838 :   elt.args = args;
    1609    291454838 :   elt.spec = spec;
    1610    291454838 :   elt.hash = hash;
    1611              : 
    1612    291454838 :   spec_entry **slot = decl_specializations->find_slot (&elt, INSERT);
    1613    291454838 :   if (*slot)
    1614       668186 :     fn = (*slot)->spec;
    1615              :   else
    1616              :     fn = NULL_TREE;
    1617              : 
    1618              :   /* We can sometimes try to re-register a specialization that we've
    1619              :      already got.  In particular, regenerate_decl_from_template calls
    1620              :      duplicate_decls which will update the specialization list.  But,
    1621              :      we'll still get called again here anyhow.  It's more convenient
    1622              :      to simply allow this than to try to prevent it.  */
    1623    291454838 :   if (fn == spec)
    1624              :     return spec;
    1625    291453635 :   else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
    1626              :     {
    1627       581825 :       if (DECL_TEMPLATE_INSTANTIATION (fn))
    1628              :         {
    1629       581577 :           if (DECL_ODR_USED (fn)
    1630       581577 :               || DECL_EXPLICIT_INSTANTIATION (fn))
    1631              :             {
    1632            6 :               error ("specialization of %qD after instantiation",
    1633              :                      fn);
    1634            6 :               return error_mark_node;
    1635              :             }
    1636              :           else
    1637              :             {
    1638       581571 :               tree clone;
    1639              :               /* This situation should occur only if the first
    1640              :                  specialization is an implicit instantiation, the
    1641              :                  second is an explicit specialization, and the
    1642              :                  implicit instantiation has not yet been used.  That
    1643              :                  situation can occur if we have implicitly
    1644              :                  instantiated a member function and then specialized
    1645              :                  it later.
    1646              : 
    1647              :                  We can also wind up here if a friend declaration that
    1648              :                  looked like an instantiation turns out to be a
    1649              :                  specialization:
    1650              : 
    1651              :                    template <class T> void foo(T);
    1652              :                    class S { friend void foo<>(int) };
    1653              :                    template <> void foo(int);
    1654              : 
    1655              :                  We transform the existing DECL in place so that any
    1656              :                  pointers to it become pointers to the updated
    1657              :                  declaration.
    1658              : 
    1659              :                  If there was a definition for the template, but not
    1660              :                  for the specialization, we want this to look as if
    1661              :                  there were no definition, and vice versa.  */
    1662       581571 :               DECL_INITIAL (fn) = NULL_TREE;
    1663       581571 :               duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1664              : 
    1665              :               /* The call to duplicate_decls will have applied
    1666              :                  [temp.expl.spec]:
    1667              : 
    1668              :                    An explicit specialization of a function template
    1669              :                    is inline only if it is explicitly declared to be,
    1670              :                    and independently of whether its function template
    1671              :                    is.
    1672              : 
    1673              :                 to the primary function; now copy the inline bits to
    1674              :                 the various clones.  */
    1675       776958 :               FOR_EACH_CLONE (clone, fn)
    1676              :                 {
    1677       195387 :                   DECL_DECLARED_INLINE_P (clone)
    1678       195387 :                     = DECL_DECLARED_INLINE_P (fn);
    1679       390774 :                   DECL_SOURCE_LOCATION (clone)
    1680       195387 :                     = DECL_SOURCE_LOCATION (fn);
    1681       195387 :                   DECL_DELETED_FN (clone)
    1682       390774 :                     = DECL_DELETED_FN (fn);
    1683              :                 }
    1684       581571 :               check_specialization_namespace (tmpl);
    1685              : 
    1686       581571 :               return fn;
    1687              :             }
    1688              :         }
    1689          248 :       else if (DECL_TEMPLATE_SPECIALIZATION (fn))
    1690              :         {
    1691          248 :           tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1692          248 :           if (dd == error_mark_node)
    1693              :             /* We've already complained in duplicate_decls.  */
    1694              :             return error_mark_node;
    1695              : 
    1696          227 :           if (dd == NULL_TREE && DECL_INITIAL (spec))
    1697              :             /* Dup decl failed, but this is a new definition. Set the
    1698              :                line number so any errors match this new
    1699              :                definition.  */
    1700            0 :             DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
    1701              : 
    1702              :           return fn;
    1703              :         }
    1704              :     }
    1705    290871810 :   else if (fn)
    1706        85158 :     return duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1707              : 
    1708              :   /* A specialization must be declared in the same namespace as the
    1709              :      template it is specializing.  */
    1710    290786652 :   if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
    1711    292789158 :       && !check_specialization_namespace (tmpl))
    1712            0 :     DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
    1713              : 
    1714    290786652 :   spec_entry *entry = ggc_alloc<spec_entry> ();
    1715    290786652 :   gcc_assert (tmpl && args && spec);
    1716    290786652 :   *entry = elt;
    1717    290786652 :   *slot = entry;
    1718    123620201 :   if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
    1719     25882998 :        && PRIMARY_TEMPLATE_P (tmpl)
    1720     24933474 :        && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
    1721    280261748 :       || module_maybe_has_cmi_p ()
    1722    570346372 :       || variable_template_p (tmpl))
    1723              :     /* If TMPL is a forward declaration of a template function, keep a list
    1724              :        of all specializations in case we need to reassign them to a friend
    1725              :        template later in tsubst_friend_function.
    1726              : 
    1727              :        If we're building a CMI, keep a list for all function templates.
    1728              : 
    1729              :        Also keep a list of all variable template instantiations so that
    1730              :        process_partial_specialization can check whether a later partial
    1731              :        specialization would have used it.  */
    1732     53555076 :     DECL_TEMPLATE_INSTANTIATIONS (tmpl)
    1733     53555076 :       = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
    1734              : 
    1735              :   return spec;
    1736              : }
    1737              : 
    1738              : /* Restricts tree and type comparisons.  */
    1739              : int comparing_specializations;
    1740              : int comparing_dependent_aliases;
    1741              : 
    1742              : /* Whether we are comparing template arguments during partial ordering
    1743              :    (and therefore want the comparison to look through dependent alias
    1744              :    template specializations).  */
    1745              : 
    1746              : static int comparing_for_partial_ordering;
    1747              : 
    1748              : /* Returns true iff two spec_entry nodes are equivalent.  */
    1749              : 
    1750              : bool
    1751  12369170621 : spec_hasher::equal (spec_entry *e1, spec_entry *e2)
    1752              : {
    1753  12369170621 :   int equal;
    1754              : 
    1755  12369170621 :   ++comparing_specializations;
    1756  12369170621 :   ++comparing_dependent_aliases;
    1757  12369170621 :   ++processing_template_decl;
    1758  13238838741 :   equal = (e1->tmpl == e2->tmpl
    1759  12369170621 :            && comp_template_args (e1->args, e2->args));
    1760    869668120 :   if (equal && flag_concepts
    1761              :       /* tmpl could be a FIELD_DECL for a capture pack.  */
    1762    863452673 :       && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
    1763    863452673 :       && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
    1764     33010374 :       && uses_template_parms (e1->args))
    1765              :     {
    1766              :       /* Partial specializations of a variable template can be distinguished by
    1767              :          constraints.  */
    1768         8851 :       tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
    1769         8851 :       tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
    1770         8851 :       equal = equivalent_constraints (c1, c2);
    1771              :     }
    1772  12369170621 :   --processing_template_decl;
    1773  12369170621 :   --comparing_dependent_aliases;
    1774  12369170621 :   --comparing_specializations;
    1775              : 
    1776  12369170621 :   return equal;
    1777              : }
    1778              : 
    1779              : /* Returns a hash for a template TMPL and template arguments ARGS.  */
    1780              : 
    1781              : static hashval_t
    1782   1407716633 : hash_tmpl_and_args (tree tmpl, tree args)
    1783              : {
    1784   1407716633 :   hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
    1785   1407716633 :   return iterative_hash_template_arg (args, val);
    1786              : }
    1787              : 
    1788              : hashval_t
    1789   1323881645 : spec_hasher::hash (tree tmpl, tree args)
    1790              : {
    1791   1323881645 :   ++comparing_specializations;
    1792   1323881645 :   hashval_t val = hash_tmpl_and_args (tmpl, args);
    1793   1323881645 :   --comparing_specializations;
    1794   1323881645 :   return val;
    1795              : }
    1796              : 
    1797              : /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
    1798              :    ignoring SPEC.  */
    1799              : 
    1800              : hashval_t
    1801  11024327664 : spec_hasher::hash (spec_entry *e)
    1802              : {
    1803  11024327664 :   if (e->hash == 0)
    1804    828695606 :     e->hash = hash (e->tmpl, e->args);
    1805  11024327664 :   return e->hash;
    1806              : }
    1807              : 
    1808              : /* Recursively calculate a hash value for a template argument ARG, for use
    1809              :    in the hash tables of template specializations.   We must be
    1810              :    careful to (at least) skip the same entities template_args_equal
    1811              :    does.  */
    1812              : 
    1813              : hashval_t
    1814  14965372811 : iterative_hash_template_arg (tree arg, hashval_t val)
    1815              : {
    1816  14965372811 :   if (arg == NULL_TREE)
    1817    935417516 :     return iterative_hash_hashval_t (0, val);
    1818              : 
    1819  14029955295 :   if (!TYPE_P (arg))
    1820              :     /* Strip nop-like things, but not the same as STRIP_NOPS.  */
    1821   4662692758 :     while (CONVERT_EXPR_P (arg)
    1822              :            || TREE_CODE (arg) == NON_LVALUE_EXPR
    1823   4662692758 :            || class_nttp_const_wrapper_p (arg))
    1824     10679008 :       arg = TREE_OPERAND (arg, 0);
    1825              : 
    1826  14029955295 :   enum tree_code code = TREE_CODE (arg);
    1827              : 
    1828  14029955295 :   val = iterative_hash_hashval_t (code, val);
    1829              : 
    1830  14029955295 :   switch (code)
    1831              :     {
    1832            0 :     case ARGUMENT_PACK_SELECT:
    1833              :       /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
    1834              :          preserving it in a hash table, which is bad because it will change
    1835              :          meaning when gen_elem_of_pack_expansion_instantiation changes the
    1836              :          ARGUMENT_PACK_SELECT_INDEX.  */
    1837            0 :       gcc_unreachable ();
    1838              : 
    1839              :     case ERROR_MARK:
    1840              :       return val;
    1841              : 
    1842   1072012080 :     case IDENTIFIER_NODE:
    1843   1072012080 :       return iterative_hash_hashval_t (IDENTIFIER_HASH_VALUE (arg), val);
    1844              : 
    1845   2390438871 :     case TREE_VEC:
    1846   6319101838 :       for (tree elt : tree_vec_range (arg))
    1847   3928662967 :         val = iterative_hash_template_arg (elt, val);
    1848   2390438871 :       return val;
    1849              : 
    1850    132938922 :     case TYPE_PACK_EXPANSION:
    1851    132938922 :     case EXPR_PACK_EXPANSION:
    1852    132938922 :       val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
    1853    132938922 :       return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
    1854              : 
    1855         4375 :     case PACK_INDEX_TYPE:
    1856         4375 :     case PACK_INDEX_EXPR:
    1857         4375 :       val = iterative_hash_template_arg (PACK_INDEX_PACK (arg), val);
    1858         4375 :       return iterative_hash_template_arg (PACK_INDEX_INDEX (arg), val);
    1859              : 
    1860    384394981 :     case TYPE_ARGUMENT_PACK:
    1861    384394981 :     case NONTYPE_ARGUMENT_PACK:
    1862    384394981 :       return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
    1863              : 
    1864              :     case TREE_LIST:
    1865      1574132 :       for (; arg; arg = TREE_CHAIN (arg))
    1866       787069 :         val = iterative_hash_template_arg (TREE_VALUE (arg), val);
    1867              :       return val;
    1868              : 
    1869          188 :     case OVERLOAD:
    1870          388 :       for (lkp_iterator iter (arg); iter; ++iter)
    1871          200 :         val = iterative_hash_template_arg (*iter, val);
    1872          188 :       return val;
    1873              : 
    1874      4513301 :     case CONSTRUCTOR:
    1875      4513301 :       {
    1876      4513301 :         iterative_hash_template_arg (TREE_TYPE (arg), val);
    1877      5758077 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
    1878              :           {
    1879       564066 :             val = iterative_hash_template_arg (e.index, val);
    1880       564066 :             val = iterative_hash_template_arg (e.value, val);
    1881              :           }
    1882              :         return val;
    1883              :       }
    1884              : 
    1885      5007098 :     case PARM_DECL:
    1886      5007098 :       if (!DECL_ARTIFICIAL (arg))
    1887              :         {
    1888      4962147 :           val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
    1889      4962147 :           val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
    1890              :         }
    1891      5007098 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1892              : 
    1893    574437484 :     case TEMPLATE_DECL:
    1894    574437484 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
    1895     12122182 :         return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1896              :       break;
    1897              : 
    1898            0 :     case TARGET_EXPR:
    1899            0 :       return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
    1900              : 
    1901         8690 :     case PTRMEM_CST:
    1902         8690 :       val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
    1903         8690 :       return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
    1904              : 
    1905     22247206 :     case TEMPLATE_PARM_INDEX:
    1906     22247206 :       val = iterative_hash_template_arg
    1907     22247206 :         (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
    1908     22247206 :       val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
    1909     22247206 :       return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
    1910              : 
    1911      2455734 :     case TRAIT_EXPR:
    1912      2455734 :       val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
    1913      2455734 :       val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
    1914      2455734 :       return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
    1915              : 
    1916      1643732 :     case BASELINK:
    1917      1643732 :       val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
    1918              :                                          val);
    1919      1643732 :       return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
    1920      1643732 :                                           val);
    1921              : 
    1922        73314 :     case MODOP_EXPR:
    1923        73314 :       val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
    1924        73314 :       code = TREE_CODE (TREE_OPERAND (arg, 1));
    1925        73314 :       val = iterative_hash_object (code, val);
    1926        73314 :       return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
    1927              : 
    1928          179 :     case LAMBDA_EXPR:
    1929              :       /* [temp.over.link] Two lambda-expressions are never considered
    1930              :          equivalent.
    1931              : 
    1932              :          So just hash the closure type.  */
    1933          179 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1934              : 
    1935         4023 :     case REQUIRES_EXPR:
    1936         4023 :       val = iterative_hash_template_arg (REQUIRES_EXPR_PARMS (arg), val);
    1937         4023 :       val = iterative_hash_template_arg (REQUIRES_EXPR_REQS (arg), val);
    1938         4023 :       return iterative_hash_template_arg (REQUIRES_EXPR_EXTRA_ARGS (arg), val);
    1939              : 
    1940      8735868 :     case CAST_EXPR:
    1941      8735868 :     case IMPLICIT_CONV_EXPR:
    1942      8735868 :     case STATIC_CAST_EXPR:
    1943      8735868 :     case REINTERPRET_CAST_EXPR:
    1944      8735868 :     case CONST_CAST_EXPR:
    1945      8735868 :     case DYNAMIC_CAST_EXPR:
    1946      8735868 :     case NEW_EXPR:
    1947      8735868 :       val = iterative_hash_template_arg (TREE_TYPE (arg), val);
    1948              :       /* Now hash operands as usual.  */
    1949      8735868 :       break;
    1950              : 
    1951    224795306 :     case CALL_EXPR:
    1952    224795306 :       {
    1953    224795306 :         tree fn = CALL_EXPR_FN (arg);
    1954    224795306 :         if (tree name = call_expr_dependent_name (arg))
    1955              :           {
    1956    218735338 :             if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
    1957    110856156 :               val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
    1958              :             fn = name;
    1959              :           }
    1960    224795306 :         val = iterative_hash_template_arg (fn, val);
    1961    224795306 :         call_expr_arg_iterator ai;
    1962    564541324 :         for (tree x = first_call_expr_arg (arg, &ai); x;
    1963    114950712 :              x = next_call_expr_arg (&ai))
    1964    114950712 :           val = iterative_hash_template_arg (x, val);
    1965    224795306 :         return val;
    1966              :       }
    1967              : 
    1968         5950 :     case REFLECT_EXPR:
    1969         5950 :       val = iterative_hash_hashval_t (REFLECT_EXPR_KIND (arg), val);
    1970         5950 :       if (REFLECT_EXPR_KIND (arg) == REFLECT_BASE)
    1971              :         {
    1972          118 :           tree binfo = REFLECT_EXPR_HANDLE (arg);
    1973          118 :           val = iterative_hash_template_arg (BINFO_TYPE (binfo), val);
    1974          118 :           val = iterative_hash_template_arg (direct_base_derived (binfo), val);
    1975          118 :           return val;
    1976              :         }
    1977              :       /* Now hash operands as usual.  */
    1978              :       break;
    1979              : 
    1980              :     default:
    1981              :       break;
    1982              :     }
    1983              : 
    1984   9776507780 :   char tclass = TREE_CODE_CLASS (code);
    1985   9776507780 :   switch (tclass)
    1986              :     {
    1987   8869902344 :     case tcc_type:
    1988   8869902344 :       if (tree ats = alias_template_specialization_p (arg, nt_transparent))
    1989              :         {
    1990              :           // We want an alias specialization that survived strip_typedefs
    1991              :           // to hash differently from its TYPE_CANONICAL, to avoid hash
    1992              :           // collisions that compare as different in template_args_equal.
    1993              :           // These could be dependent specializations that strip_typedefs
    1994              :           // left alone for example.
    1995     83834988 :           tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
    1996     83834988 :           return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
    1997              :         }
    1998              : 
    1999   8786067356 :       switch (code)
    2000              :         {
    2001    116007725 :         case DECLTYPE_TYPE:
    2002    116007725 :           val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
    2003    116007725 :           break;
    2004              : 
    2005    363657214 :         case TYPENAME_TYPE:
    2006    363657214 :           if (comparing_specializations)
    2007              :             {
    2008              :               /* Hash the components that are relevant to TYPENAME_TYPE
    2009              :                  equivalence as determined by structural_comptypes.  We
    2010              :                  can only coherently do this when comparing_specializations
    2011              :                  is set, because otherwise structural_comptypes tries
    2012              :                  resolving TYPENAME_TYPE via the current instantiation.  */
    2013    321872907 :               tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
    2014    321872907 :               tree fullname = TYPENAME_TYPE_FULLNAME (arg);
    2015    321872907 :               val = iterative_hash_template_arg (context, val);
    2016    321872907 :               val = iterative_hash_template_arg (fullname, val);
    2017              :             }
    2018              :           break;
    2019              : 
    2020   8306402417 :         default:
    2021   8306402417 :           if (tree canonical = TYPE_CANONICAL (arg))
    2022   8146242005 :             val = iterative_hash_hashval_t (TYPE_HASH (canonical), val);
    2023    160160412 :           else if (tree ti = TYPE_TEMPLATE_INFO (arg))
    2024              :             {
    2025      5181474 :               val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
    2026      5181474 :               val = iterative_hash_template_arg (TI_ARGS (ti), val);
    2027              :             }
    2028              :           break;
    2029              :         }
    2030              : 
    2031              :       return val;
    2032              : 
    2033    828189245 :     case tcc_declaration:
    2034    828189245 :     case tcc_constant:
    2035    828189245 :       return iterative_hash_expr (arg, val);
    2036              : 
    2037     78416191 :     default:
    2038     78416191 :       gcc_assert (IS_EXPR_CODE_CLASS (tclass));
    2039    213893151 :       for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
    2040    135476960 :         val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
    2041              :       return val;
    2042              :     }
    2043              : }
    2044              : 
    2045              : /* Unregister the specialization SPEC as a specialization of TMPL.
    2046              :    Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
    2047              :    if the SPEC was listed as a specialization of TMPL.
    2048              : 
    2049              :    Note that SPEC has been ggc_freed, so we can't look inside it.  */
    2050              : 
    2051              : bool
    2052       666968 : reregister_specialization (tree spec, tree tinfo, tree new_spec)
    2053              : {
    2054       666968 :   spec_entry *entry;
    2055       666968 :   spec_entry elt;
    2056              : 
    2057       666968 :   elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
    2058       666968 :   elt.args = TI_ARGS (tinfo);
    2059              : 
    2060       666968 :   entry = decl_specializations->find (&elt);
    2061       666968 :   if (entry != NULL)
    2062              :     {
    2063       666968 :       gcc_assert (entry->spec == spec || entry->spec == new_spec);
    2064       666968 :       gcc_assert (new_spec != NULL_TREE);
    2065       666968 :       entry->spec = new_spec;
    2066              : 
    2067              :       /* We need to also remove SPEC from DECL_TEMPLATE_INSTANTIATIONS
    2068              :          if it was placed there.  */
    2069       666968 :       for (tree *inst = &DECL_TEMPLATE_INSTANTIATIONS (elt.tmpl);
    2070       826735 :            *inst; inst = &TREE_CHAIN (*inst))
    2071       159782 :         if (TREE_VALUE (*inst) == spec)
    2072              :           {
    2073           15 :             *inst = TREE_CHAIN (*inst);
    2074           15 :             break;
    2075              :           }
    2076              : 
    2077              :       return 1;
    2078              :     }
    2079              : 
    2080              :   return 0;
    2081              : }
    2082              : 
    2083              : /* Like register_specialization, but for local declarations.  We are
    2084              :    registering SPEC, an instantiation of TMPL.  */
    2085              : 
    2086              : void
    2087     77796678 : register_local_specialization (tree spec, tree tmpl)
    2088              : {
    2089     77796678 :   gcc_assert (tmpl != spec);
    2090     77796678 :   local_specializations->put (tmpl, spec);
    2091     77796678 : }
    2092              : 
    2093              : /* Registers T as a specialization of itself.  This is used to preserve
    2094              :    the references to already-parsed parameters when instantiating
    2095              :    postconditions.  */
    2096              : 
    2097              : void
    2098          158 : register_local_identity (tree t)
    2099              : {
    2100          158 :   local_specializations->put (t, t);
    2101          158 : }
    2102              : 
    2103              : /* TYPE is a class type.  Returns true if TYPE is an explicitly
    2104              :    specialized class.  */
    2105              : 
    2106              : bool
    2107     19067429 : explicit_class_specialization_p (tree type)
    2108              : {
    2109     19067429 :   if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
    2110              :     return false;
    2111       568970 :   return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
    2112              : }
    2113              : 
    2114              : /* Populate OUT with the overload set FNS, going through all the
    2115              :    overloads for each element of the list.  Alternatively, FNS can be a
    2116              :    TREE_LIST, in which case it will be added together with all the
    2117              :    overloads.  */
    2118              : 
    2119              : static void
    2120         1513 : flatten_candidates (tree fns, auto_vec<tree> &out)
    2121              : {
    2122         1513 :   if (TREE_CODE (fns) == TREE_LIST)
    2123         1110 :     for (; fns; fns = TREE_CHAIN (fns))
    2124          749 :       flatten_candidates (TREE_VALUE (fns), out);
    2125              :   else
    2126         3828 :     for (tree cand : lkp_range (fns))
    2127         1524 :       out.safe_push (cand);
    2128         1513 : }
    2129              : 
    2130              : /* Print a note announcing a list of candidates.  */
    2131              : 
    2132              : void
    2133         5329 : inform_num_candidates (location_t loc, int num_candidates)
    2134              : {
    2135         5329 :   inform_n (loc,
    2136              :             num_candidates, "there is %i candidate", "there are %i candidates",
    2137              :             num_candidates);
    2138         5329 : }
    2139              : 
    2140              : /* Print the list of candidate FNS in an error message.  FNS can also
    2141              :    be a TREE_LIST of non-functions in the case of an ambiguous lookup.
    2142              : 
    2143              :    If CAND_CTXT is non-null, use it for each candidate to allow for
    2144              :    additional per-candidate notes.  */
    2145              : 
    2146              : void
    2147          764 : print_candidates (location_t error_loc,
    2148              :                   tree fns,
    2149              :                   candidate_context *cand_ctxt)
    2150              : {
    2151          764 :   auto_vec<tree> candidates;
    2152          764 :   flatten_candidates (fns, candidates);
    2153              : 
    2154          764 :   auto_diagnostic_nesting_level sentinel;
    2155              : 
    2156         1528 :   inform_num_candidates (error_loc, candidates.length ());
    2157              : 
    2158          764 :   auto_diagnostic_nesting_level sentinel2;
    2159              : 
    2160          764 :   if (candidates.length () == 1)
    2161              :     {
    2162          262 :       tree cand = candidates[0];
    2163          262 :       inform (DECL_SOURCE_LOCATION (cand), "candidate is: %#qD", cand);
    2164          262 :       if (cand_ctxt)
    2165           66 :         cand_ctxt->emit_any_notes_for_candidate (cand);
    2166              :     }
    2167              :   else
    2168              :     {
    2169          502 :       int idx = 0;
    2170         2768 :       for (tree cand : candidates)
    2171              :         {
    2172         1262 :           inform (DECL_SOURCE_LOCATION (cand), "candidate %i: %#qD",
    2173              :                   ++idx, cand);
    2174         1262 :           if (cand_ctxt)
    2175           35 :             cand_ctxt->emit_any_notes_for_candidate (cand);
    2176              :         }
    2177              :     }
    2178          764 : }
    2179              : 
    2180              : /* Get a (possibly) constrained template declaration for the
    2181              :    purpose of ordering candidates.  */
    2182              : static tree
    2183          242 : get_template_for_ordering (tree list)
    2184              : {
    2185          242 :   gcc_assert (TREE_CODE (list) == TREE_LIST);
    2186          242 :   tree f = TREE_VALUE (list);
    2187          242 :   if (f == NULL_TREE)
    2188              :     /* Also handle a list from resolve_address_of_overloaded_function with the
    2189              :        function in TREE_PURPOSE.  */
    2190          194 :     f = TREE_PURPOSE (list);
    2191          242 :   if (tree ti = DECL_TEMPLATE_INFO (f))
    2192           66 :     return TI_TEMPLATE (ti);
    2193              :   return f;
    2194              : }
    2195              : 
    2196              : /* Among candidates having the same signature, return the
    2197              :    most constrained or NULL_TREE if there is no best candidate.
    2198              :    If the signatures of candidates vary (e.g., template
    2199              :    specialization vs. member function), then there can be no
    2200              :    most constrained.
    2201              : 
    2202              :    Note that we don't compare constraints on the functions
    2203              :    themselves, but rather those of their templates. */
    2204              : tree
    2205          109 : most_constrained_function (tree candidates)
    2206              : {
    2207              :   // Try to find the best candidate in a first pass.
    2208          109 :   tree champ = candidates;
    2209          130 :   for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
    2210              :     {
    2211          109 :       int winner = more_constrained (get_template_for_ordering (champ),
    2212              :                                      get_template_for_ordering (c));
    2213          109 :       if (winner == -1)
    2214              :         champ = c; // The candidate is more constrained
    2215           97 :       else if (winner == 0)
    2216              :         return NULL_TREE; // Neither is more constrained
    2217              :     }
    2218              : 
    2219              :   // Verify that the champ is better than previous candidates.
    2220           33 :   for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
    2221           12 :     if (!more_constrained (get_template_for_ordering (champ),
    2222              :                            get_template_for_ordering (c)))
    2223              :       return NULL_TREE;
    2224              :   }
    2225              : 
    2226              :   return champ;
    2227              : }
    2228              : 
    2229              : 
    2230              : /* Returns the template (one of the functions given by TEMPLATE_ID)
    2231              :    which can be specialized to match the indicated DECL with the
    2232              :    explicit template args given in TEMPLATE_ID.  The DECL may be
    2233              :    NULL_TREE if none is available.  In that case, the functions in
    2234              :    TEMPLATE_ID are non-members.
    2235              : 
    2236              :    If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
    2237              :    specialization of a member template.
    2238              : 
    2239              :    The TEMPLATE_COUNT is the number of references to qualifying
    2240              :    template classes that appeared in the name of the function. See
    2241              :    check_explicit_specialization for a more accurate description.
    2242              : 
    2243              :    TSK indicates what kind of template declaration (if any) is being
    2244              :    declared.  TSK_TEMPLATE indicates that the declaration given by
    2245              :    DECL, though a FUNCTION_DECL, has template parameters, and is
    2246              :    therefore a template function.
    2247              : 
    2248              :    The template args (those explicitly specified and those deduced)
    2249              :    are output in a newly created vector *TARGS_OUT.
    2250              : 
    2251              :    If it is impossible to determine the result, an error message is
    2252              :    issued.  The error_mark_node is returned to indicate failure.  */
    2253              : 
    2254              : static tree
    2255      4760925 : determine_specialization (tree template_id,
    2256              :                           tree decl,
    2257              :                           tree* targs_out,
    2258              :                           int need_member_template,
    2259              :                           int template_count,
    2260              :                           tmpl_spec_kind tsk)
    2261              : {
    2262      4760925 :   tree fns;
    2263      4760925 :   tree targs;
    2264      4760925 :   tree explicit_targs;
    2265      4760925 :   tree candidates = NULL_TREE;
    2266              : 
    2267              :   /* A TREE_LIST of templates of which DECL may be a specialization.
    2268              :      The TREE_VALUE of each node is a TEMPLATE_DECL.  The
    2269              :      corresponding TREE_PURPOSE is the set of template arguments that,
    2270              :      when used to instantiate the template, would produce a function
    2271              :      with the signature of DECL.  */
    2272      4760925 :   tree templates = NULL_TREE;
    2273      4760925 :   int header_count;
    2274      4760925 :   cp_binding_level *b;
    2275              : 
    2276      4760925 :   *targs_out = NULL_TREE;
    2277              : 
    2278      4760925 :   if (template_id == error_mark_node || decl == error_mark_node)
    2279              :     return error_mark_node;
    2280              : 
    2281              :   /* We shouldn't be specializing a member template of an
    2282              :      unspecialized class template; we already gave an error in
    2283              :      check_specialization_scope, now avoid crashing.  */
    2284      4760922 :   if (!VAR_P (decl)
    2285      2758144 :       && template_count && DECL_CLASS_SCOPE_P (decl)
    2286      5662993 :       && template_class_depth (DECL_CONTEXT (decl)) > 0)
    2287              :     {
    2288            3 :       gcc_assert (errorcount);
    2289            3 :       return error_mark_node;
    2290              :     }
    2291              : 
    2292      4760919 :   fns = TREE_OPERAND (template_id, 0);
    2293      4760919 :   explicit_targs = TREE_OPERAND (template_id, 1);
    2294              : 
    2295      4760919 :   if (fns == error_mark_node)
    2296              :     return error_mark_node;
    2297              : 
    2298              :   /* Check for baselinks.  */
    2299      4760919 :   if (BASELINK_P (fns))
    2300            0 :     fns = BASELINK_FUNCTIONS (fns);
    2301              : 
    2302      4760919 :   if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
    2303              :     {
    2304           15 :       error_at (DECL_SOURCE_LOCATION (decl),
    2305              :                 "%qD is not a function template", fns);
    2306           15 :       return error_mark_node;
    2307              :     }
    2308      4760904 :   else if (VAR_P (decl) && !variable_template_p (fns))
    2309              :     {
    2310            3 :       error ("%qD is not a variable template", fns);
    2311            3 :       return error_mark_node;
    2312              :     }
    2313              : 
    2314              :   /* Count the number of template headers specified for this
    2315              :      specialization.  */
    2316      4760901 :   header_count = 0;
    2317      4760901 :   for (b = current_binding_level;
    2318      7347850 :        b->kind == sk_template_parms;
    2319      2586949 :        b = b->level_chain)
    2320      2586949 :     ++header_count;
    2321              : 
    2322      4760901 :   tree orig_fns = fns;
    2323      4760901 :   bool header_mismatch = false;
    2324              : 
    2325      4760901 :   if (variable_template_p (fns))
    2326              :     {
    2327      2002775 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
    2328      2002775 :       targs = coerce_template_parms (parms, explicit_targs, fns,
    2329              :                                      tf_warning_or_error);
    2330      2002775 :       if (targs != error_mark_node
    2331      2002775 :           && constraints_satisfied_p (fns, targs))
    2332      2002766 :         templates = tree_cons (targs, fns, templates);
    2333              :     }
    2334     18530485 :   else for (lkp_iterator iter (fns); iter; ++iter)
    2335              :     {
    2336     15772359 :       tree fn = *iter;
    2337              : 
    2338     15772359 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2339              :         {
    2340     15154354 :           tree decl_arg_types;
    2341     15154354 :           tree fn_arg_types;
    2342              : 
    2343              :           /* In case of explicit specialization, we need to check if
    2344              :              the number of template headers appearing in the specialization
    2345              :              is correct. This is usually done in check_explicit_specialization,
    2346              :              but the check done there cannot be exhaustive when specializing
    2347              :              member functions. Consider the following code:
    2348              : 
    2349              :              template <> void A<int>::f(int);
    2350              :              template <> template <> void A<int>::f(int);
    2351              : 
    2352              :              Assuming that A<int> is not itself an explicit specialization
    2353              :              already, the first line specializes "f" which is a non-template
    2354              :              member function, whilst the second line specializes "f" which
    2355              :              is a template member function. So both lines are syntactically
    2356              :              correct, and check_explicit_specialization does not reject
    2357              :              them.
    2358              : 
    2359              :              Here, we can do better, as we are matching the specialization
    2360              :              against the declarations. We count the number of template
    2361              :              headers, and we check if they match TEMPLATE_COUNT + 1
    2362              :              (TEMPLATE_COUNT is the number of qualifying template classes,
    2363              :              plus there must be another header for the member template
    2364              :              itself).
    2365              : 
    2366              :              Notice that if header_count is zero, this is not a
    2367              :              specialization but rather a template instantiation, so there
    2368              :              is no check we can perform here.  */
    2369     15154354 :           if (header_count && header_count != template_count + 1)
    2370              :             {
    2371            6 :               header_mismatch = true;
    2372            6 :               continue;
    2373              :             }
    2374              : 
    2375              :           /* Check that the number of template arguments at the
    2376              :              innermost level for DECL is the same as for FN.  */
    2377     15154348 :           if (current_binding_level->kind == sk_template_parms
    2378       199840 :               && !current_binding_level->explicit_spec_p
    2379     15155586 :               && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
    2380         1238 :                   != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
    2381              :                                       (current_template_parms))))
    2382            3 :             continue;
    2383              : 
    2384              :           /* DECL might be a specialization of FN.  */
    2385     15154345 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2386     15154345 :           fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
    2387              : 
    2388              :           /* For a non-static member function, we need to make sure
    2389              :              that the const qualification is the same.  Since
    2390              :              get_bindings does not try to merge the "this" parameter,
    2391              :              we must do the comparison explicitly.  */
    2392     15154345 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
    2393              :             {
    2394       408979 :               if (!same_type_p (TREE_VALUE (fn_arg_types),
    2395              :                                 TREE_VALUE (decl_arg_types)))
    2396          102 :                 continue;
    2397              : 
    2398              :               /* And the ref-qualification.  */
    2399       817754 :               if (type_memfn_rqual (TREE_TYPE (decl))
    2400       408877 :                   != type_memfn_rqual (TREE_TYPE (fn)))
    2401           38 :                 continue;
    2402              :             }
    2403              : 
    2404              :           /* Skip the "this" parameter and, for constructors of
    2405              :              classes with virtual bases, the VTT parameter.  A
    2406              :              full specialization of a constructor will have a VTT
    2407              :              parameter, but a template never will.  */
    2408     15154205 :           decl_arg_types
    2409     15154205 :             = skip_artificial_parms_for (decl, decl_arg_types);
    2410     15154205 :           fn_arg_types
    2411     15154205 :             = skip_artificial_parms_for (fn, fn_arg_types);
    2412              : 
    2413              :           /* Function templates cannot be specializations; there are
    2414              :              no partial specializations of functions.  Therefore, if
    2415              :              the type of DECL does not match FN, there is no
    2416              :              match.
    2417              : 
    2418              :              Note that it should never be the case that we have both
    2419              :              candidates added here, and for regular member functions
    2420              :              below. */
    2421     15154205 :           if (tsk == tsk_template)
    2422              :             {
    2423         1235 :               if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
    2424         1235 :                                         current_template_parms))
    2425            0 :                 continue;
    2426         1235 :               if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2427              :                                 TREE_TYPE (TREE_TYPE (fn))))
    2428            0 :                 continue;
    2429         1235 :               if (!compparms (fn_arg_types, decl_arg_types))
    2430            3 :                 continue;
    2431              : 
    2432         1232 :               tree freq = get_constraints (fn);
    2433         1232 :               tree dreq = get_constraints (decl);
    2434         1232 :               if (!freq != !dreq)
    2435            0 :                 continue;
    2436         1232 :               if (freq)
    2437              :                 {
    2438              :                   /* C++20 CA104: Substitute directly into the
    2439              :                      constraint-expression.  */
    2440           46 :                   tree fargs = DECL_TI_ARGS (fn);
    2441           46 :                   tsubst_flags_t complain = tf_none;
    2442           46 :                   freq = tsubst_constraint_info (freq, fargs, complain, fn);
    2443           46 :                   if (!cp_tree_equal (freq, dreq))
    2444           29 :                     continue;
    2445              :                 }
    2446              : 
    2447         1203 :               candidates = tree_cons (NULL_TREE, fn, candidates);
    2448         1203 :               continue;
    2449         1203 :             }
    2450              : 
    2451              :           /* See whether this function might be a specialization of this
    2452              :              template.  Suppress access control because we might be trying
    2453              :              to make this specialization a friend, and we have already done
    2454              :              access control for the declaration of the specialization.  */
    2455     15152970 :           push_deferring_access_checks (dk_no_check);
    2456     15152970 :           targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
    2457     15152970 :           pop_deferring_access_checks ();
    2458              : 
    2459     15152970 :           if (!targs)
    2460              :             /* We cannot deduce template arguments that when used to
    2461              :                specialize TMPL will produce DECL.  */
    2462     12845899 :             continue;
    2463              : 
    2464      2307071 :           if (uses_template_parms (targs))
    2465              :             /* We deduced something involving 'auto', which isn't a valid
    2466              :                template argument.  */
    2467            3 :             continue;
    2468              : 
    2469              :           /* Save this template, and the arguments deduced.  */
    2470      2307068 :           templates = tree_cons (targs, fn, templates);
    2471              :         }
    2472       618005 :       else if (need_member_template)
    2473              :         /* FN is an ordinary member function, and we need a
    2474              :            specialization of a member template.  */
    2475              :         ;
    2476       617959 :       else if (TREE_CODE (fn) != FUNCTION_DECL)
    2477              :         /* We can get IDENTIFIER_NODEs here in certain erroneous
    2478              :            cases.  */
    2479              :         ;
    2480       617959 :       else if (!DECL_FUNCTION_MEMBER_P (fn))
    2481              :         /* This is just an ordinary non-member function.  Nothing can
    2482              :            be a specialization of that.  */
    2483              :         ;
    2484       606975 :       else if (DECL_ARTIFICIAL (fn))
    2485              :         /* Cannot specialize functions that are created implicitly.  */
    2486              :         ;
    2487              :       else
    2488              :         {
    2489       606797 :           tree decl_arg_types;
    2490              : 
    2491              :           /* This is an ordinary member function.  However, since
    2492              :              we're here, we can assume its enclosing class is a
    2493              :              template class.  For example,
    2494              : 
    2495              :                template <typename T> struct S { void f(); };
    2496              :                template <> void S<int>::f() {}
    2497              : 
    2498              :              Here, S<int>::f is a non-template, but S<int> is a
    2499              :              template class.  If FN has the same type as DECL, we
    2500              :              might be in business.  */
    2501              : 
    2502       606797 :           if (!DECL_TEMPLATE_INFO (fn))
    2503              :             /* Its enclosing class is an explicit specialization
    2504              :                of a template class.  This is not a candidate.  */
    2505            6 :             continue;
    2506              : 
    2507       606791 :           if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2508              :                             TREE_TYPE (TREE_TYPE (fn))))
    2509              :             /* The return types differ.  */
    2510         1486 :             continue;
    2511              : 
    2512              :           /* Adjust the type of DECL in case FN is a static member.  */
    2513       605305 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2514       605305 :           if (DECL_STATIC_FUNCTION_P (fn)
    2515       605305 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2516          190 :             decl_arg_types = TREE_CHAIN (decl_arg_types);
    2517              : 
    2518       605305 :           if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
    2519              :                          decl_arg_types))
    2520       112586 :             continue;
    2521              : 
    2522       492719 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
    2523       985259 :               && (type_memfn_rqual (TREE_TYPE (decl))
    2524       492540 :                   != type_memfn_rqual (TREE_TYPE (fn))))
    2525            6 :             continue;
    2526              : 
    2527              :           // If the deduced arguments do not satisfy the constraints,
    2528              :           // this is not a candidate.
    2529       492713 :           if (flag_concepts && !constraints_satisfied_p (fn))
    2530           21 :             continue;
    2531              : 
    2532              :           // Add the candidate.
    2533       492692 :           candidates = tree_cons (NULL_TREE, fn, candidates);
    2534              :         }
    2535              :     }
    2536              : 
    2537      4760892 :   if (templates && TREE_CHAIN (templates))
    2538              :     {
    2539              :       /* We have:
    2540              : 
    2541              :            [temp.expl.spec]
    2542              : 
    2543              :            It is possible for a specialization with a given function
    2544              :            signature to be instantiated from more than one function
    2545              :            template.  In such cases, explicit specification of the
    2546              :            template arguments must be used to uniquely identify the
    2547              :            function template specialization being specialized.
    2548              : 
    2549              :          Note that here, there's no suggestion that we're supposed to
    2550              :          determine which of the candidate templates is most
    2551              :          specialized.  However, we, also have:
    2552              : 
    2553              :            [temp.func.order]
    2554              : 
    2555              :            Partial ordering of overloaded function template
    2556              :            declarations is used in the following contexts to select
    2557              :            the function template to which a function template
    2558              :            specialization refers:
    2559              : 
    2560              :            -- when an explicit specialization refers to a function
    2561              :               template.
    2562              : 
    2563              :          So, we do use the partial ordering rules, at least for now.
    2564              :          This extension can only serve to make invalid programs valid,
    2565              :          so it's safe.  And, there is strong anecdotal evidence that
    2566              :          the committee intended the partial ordering rules to apply;
    2567              :          the EDG front end has that behavior, and John Spicer claims
    2568              :          that the committee simply forgot to delete the wording in
    2569              :          [temp.expl.spec].  */
    2570        21543 :       tree tmpl = most_specialized_instantiation (templates);
    2571        21543 :       if (tmpl != error_mark_node)
    2572              :         {
    2573        21540 :           templates = tmpl;
    2574        21540 :           TREE_CHAIN (templates) = NULL_TREE;
    2575              :         }
    2576              :     }
    2577              : 
    2578              :   // Concepts allows multiple declarations of member functions
    2579              :   // with the same signature. Like above, we need to rely on
    2580              :   // on the partial ordering of those candidates to determine which
    2581              :   // is the best.
    2582      4760901 :   if (flag_concepts && candidates && TREE_CHAIN (candidates))
    2583              :     {
    2584           12 :       if (tree cand = most_constrained_function (candidates))
    2585              :         {
    2586           12 :           candidates = cand;
    2587           12 :           TREE_CHAIN (cand) = NULL_TREE;
    2588              :         }
    2589              :     }
    2590              : 
    2591      4760901 :   if (templates == NULL_TREE && candidates == NULL_TREE)
    2592              :     {
    2593          222 :       auto_diagnostic_group d;
    2594          222 :       error ("template-id %qD for %q+D does not match any template "
    2595              :              "declaration", template_id, decl);
    2596          222 :       if (header_mismatch)
    2597            3 :         inform (DECL_SOURCE_LOCATION (decl),
    2598              :                 "saw %d %<template<>%>, need %d for "
    2599              :                 "specializing a member function template",
    2600              :                 header_count, template_count + 1);
    2601          222 :       print_candidates (DECL_SOURCE_LOCATION (decl), orig_fns);
    2602          222 :       return error_mark_node;
    2603          222 :     }
    2604      4266796 :   else if ((templates && TREE_CHAIN (templates))
    2605      4760676 :            || (candidates && TREE_CHAIN (candidates))
    2606      9521355 :            || (templates && candidates))
    2607              :     {
    2608            3 :       auto_diagnostic_group d;
    2609            3 :       error ("ambiguous template specialization %qD for %q+D",
    2610              :              template_id, decl);
    2611            3 :       candidates = chainon (candidates, templates);
    2612            3 :       print_candidates (input_location, candidates);
    2613            3 :       return error_mark_node;
    2614            3 :     }
    2615              : 
    2616              :   /* We have one, and exactly one, match.  */
    2617      4760676 :   if (candidates)
    2618              :     {
    2619       493883 :       tree fn = TREE_VALUE (candidates);
    2620       493883 :       *targs_out = copy_node (DECL_TI_ARGS (fn));
    2621              : 
    2622              :       /* Propagate the candidate's constraints to the declaration.  */
    2623       493883 :       if (tsk != tsk_template)
    2624       492680 :         set_constraints (decl, get_constraints (fn));
    2625              : 
    2626              :       /* DECL is a re-declaration or partial instantiation of a template
    2627              :          function.  */
    2628       493883 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2629              :         return fn;
    2630              :       /* It was a specialization of an ordinary member function in a
    2631              :          template class.  */
    2632       492680 :       return DECL_TI_TEMPLATE (fn);
    2633              :     }
    2634              : 
    2635              :   /* It was a specialization of a template.  */
    2636      4266793 :   tree tmpl = TREE_VALUE (templates);
    2637      4266793 :   *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
    2638              : 
    2639              :   /* Propagate the template's constraints to the declaration.  */
    2640      4266793 :   if (tsk != tsk_template)
    2641      3215425 :     set_constraints (decl, get_constraints (tmpl));
    2642              : 
    2643              :   return tmpl;
    2644              : }
    2645              : 
    2646              : /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
    2647              :    but with the default argument values filled in from those in the
    2648              :    TMPL_TYPES.  */
    2649              : 
    2650              : static tree
    2651       213813 : copy_default_args_to_explicit_spec_1 (tree spec_types,
    2652              :                                       tree tmpl_types)
    2653              : {
    2654       213813 :   tree new_spec_types;
    2655              : 
    2656       213813 :   if (!spec_types)
    2657              :     return NULL_TREE;
    2658              : 
    2659       213813 :   if (spec_types == void_list_node)
    2660              :     return void_list_node;
    2661              : 
    2662              :   /* Substitute into the rest of the list.  */
    2663       127866 :   new_spec_types =
    2664       127866 :     copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
    2665       127866 :                                           TREE_CHAIN (tmpl_types));
    2666              : 
    2667              :   /* Add the default argument for this parameter.  */
    2668       255732 :   return hash_tree_cons (TREE_PURPOSE (tmpl_types),
    2669       127866 :                          TREE_VALUE (spec_types),
    2670       127866 :                          new_spec_types);
    2671              : }
    2672              : 
    2673              : /* DECL is an explicit specialization.  Replicate default arguments
    2674              :    from the template it specializes.  (That way, code like:
    2675              : 
    2676              :      template <class T> void f(T = 3);
    2677              :      template <> void f(double);
    2678              :      void g () { f (); }
    2679              : 
    2680              :    works, as required.)  An alternative approach would be to look up
    2681              :    the correct default arguments at the call-site, but this approach
    2682              :    is consistent with how implicit instantiations are handled.  */
    2683              : 
    2684              : static void
    2685       666956 : copy_default_args_to_explicit_spec (tree decl)
    2686              : {
    2687       666956 :   tree tmpl;
    2688       666956 :   tree spec_types;
    2689       666956 :   tree tmpl_types;
    2690       666956 :   tree new_spec_types;
    2691       666956 :   tree old_type;
    2692       666956 :   tree new_type;
    2693       666956 :   tree t;
    2694       666956 :   tree object_type = NULL_TREE;
    2695       666956 :   tree in_charge = NULL_TREE;
    2696       666956 :   tree vtt = NULL_TREE;
    2697              : 
    2698              :   /* See if there's anything we need to do.  */
    2699       666956 :   tmpl = DECL_TI_TEMPLATE (decl);
    2700       666956 :   tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
    2701      2616284 :   for (t = tmpl_types; t; t = TREE_CHAIN (t))
    2702      2035275 :     if (TREE_PURPOSE (t))
    2703              :       break;
    2704       666956 :   if (!t)
    2705              :     return;
    2706              : 
    2707        85947 :   old_type = TREE_TYPE (decl);
    2708        85947 :   spec_types = TYPE_ARG_TYPES (old_type);
    2709              : 
    2710        85947 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2711              :     {
    2712              :       /* Remove the this pointer, but remember the object's type for
    2713              :          CV quals.  */
    2714        85947 :       object_type = TREE_TYPE (TREE_VALUE (spec_types));
    2715        85947 :       spec_types = TREE_CHAIN (spec_types);
    2716        85947 :       tmpl_types = TREE_CHAIN (tmpl_types);
    2717              : 
    2718        85947 :       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
    2719              :         {
    2720              :           /* DECL may contain more parameters than TMPL due to the extra
    2721              :              in-charge parameter in constructors and destructors.  */
    2722            0 :           in_charge = spec_types;
    2723            0 :           spec_types = TREE_CHAIN (spec_types);
    2724              :         }
    2725        85947 :       if (DECL_HAS_VTT_PARM_P (decl))
    2726              :         {
    2727            0 :           vtt = spec_types;
    2728            0 :           spec_types = TREE_CHAIN (spec_types);
    2729              :         }
    2730              :     }
    2731              : 
    2732              :   /* Compute the merged default arguments.  */
    2733        85947 :   new_spec_types =
    2734        85947 :     copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
    2735              : 
    2736              :   /* Compute the new FUNCTION_TYPE.  */
    2737        85947 :   if (object_type)
    2738              :     {
    2739        85947 :       if (vtt)
    2740            0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
    2741            0 :                                          TREE_VALUE (vtt),
    2742              :                                          new_spec_types);
    2743              : 
    2744        85947 :       if (in_charge)
    2745              :         /* Put the in-charge parameter back.  */
    2746            0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
    2747            0 :                                          TREE_VALUE (in_charge),
    2748              :                                          new_spec_types);
    2749              : 
    2750        85947 :       new_type = build_method_type_directly (object_type,
    2751        85947 :                                              TREE_TYPE (old_type),
    2752              :                                              new_spec_types);
    2753              :     }
    2754              :   else
    2755            0 :     new_type = cp_build_function_type (TREE_TYPE (old_type), new_spec_types);
    2756        85947 :   new_type = cp_build_type_attribute_variant (new_type,
    2757        85947 :                                               TYPE_ATTRIBUTES (old_type));
    2758        85947 :   new_type = cxx_copy_lang_qualifiers (new_type, old_type);
    2759              : 
    2760        85947 :   TREE_TYPE (decl) = new_type;
    2761              : }
    2762              : 
    2763              : /* Return the number of template headers we expect to see for a definition
    2764              :    or specialization of CTYPE or one of its non-template members.  */
    2765              : 
    2766              : int
    2767     36569985 : num_template_headers_for_class (tree ctype)
    2768              : {
    2769     36569985 :   int num_templates = 0;
    2770              : 
    2771     55392956 :   while (ctype && CLASS_TYPE_P (ctype))
    2772              :     {
    2773              :       /* You're supposed to have one `template <...>' for every
    2774              :          template class, but you don't need one for a full
    2775              :          specialization.  For example:
    2776              : 
    2777              :          template <class T> struct S{};
    2778              :          template <> struct S<int> { void f(); };
    2779              :          void S<int>::f () {}
    2780              : 
    2781              :          is correct; there shouldn't be a `template <>' for the
    2782              :          definition of `S<int>::f'.  */
    2783     25698904 :       if (!CLASSTYPE_TEMPLATE_INFO (ctype))
    2784              :         /* If CTYPE does not have template information of any
    2785              :            kind,  then it is not a template, nor is it nested
    2786              :            within a template.  */
    2787              :         break;
    2788     19067429 :       if (explicit_class_specialization_p (ctype))
    2789              :         break;
    2790     18822971 :       if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
    2791     18450068 :         ++num_templates;
    2792              : 
    2793     18822971 :       ctype = TYPE_CONTEXT (ctype);
    2794              :     }
    2795              : 
    2796     36569985 :   return num_templates;
    2797              : }
    2798              : 
    2799              : /* Do a simple sanity check on the template headers that precede the
    2800              :    variable declaration DECL.  */
    2801              : 
    2802              : void
    2803      5173143 : check_template_variable (tree decl)
    2804              : {
    2805      5173143 :   tree ctx = CP_DECL_CONTEXT (decl);
    2806      5173143 :   int wanted = num_template_headers_for_class (ctx);
    2807     10346283 :   if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
    2808     10346274 :       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
    2809              :     {
    2810      4822671 :       if (cxx_dialect < cxx14)
    2811         5280 :         pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
    2812              :                  "variable templates only available with "
    2813              :                  "%<-std=c++14%> or %<-std=gnu++14%>");
    2814              : 
    2815              :       // Namespace-scope variable templates should have a template header.
    2816      4822671 :       ++wanted;
    2817              :     }
    2818      5173143 :   if (template_header_count > wanted)
    2819              :     {
    2820           12 :       auto_diagnostic_group d;
    2821           12 :       bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    2822              :                              "too many template headers for %qD "
    2823              :                              "(should be %d)",
    2824              :                              decl, wanted);
    2825           12 :       if (warned && CLASS_TYPE_P (ctx)
    2826           21 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
    2827            3 :         inform (DECL_SOURCE_LOCATION (decl),
    2828              :                 "members of an explicitly specialized class are defined "
    2829              :                 "without a template header");
    2830           12 :     }
    2831      5173143 : }
    2832              : 
    2833              : /* An explicit specialization whose declarator-id or class-head-name is not
    2834              :    qualified shall be declared in the nearest enclosing namespace of the
    2835              :    template, or, if the namespace is inline (7.3.1), any namespace from its
    2836              :    enclosing namespace set.
    2837              : 
    2838              :    If the name declared in the explicit instantiation is an unqualified name,
    2839              :    the explicit instantiation shall appear in the namespace where its template
    2840              :    is declared or, if that namespace is inline (7.3.1), any namespace from its
    2841              :    enclosing namespace set.  */
    2842              : 
    2843              : void
    2844     16760147 : check_unqualified_spec_or_inst (tree t, location_t loc)
    2845              : {
    2846     16760147 :   tree tmpl = most_general_template (t);
    2847     33520294 :   if (DECL_NAMESPACE_SCOPE_P (tmpl)
    2848     32949431 :       && !is_nested_namespace (current_namespace,
    2849     16189284 :                                CP_DECL_CONTEXT (tmpl), true))
    2850              :     {
    2851           17 :       if (processing_specialization)
    2852            6 :         permerror (loc, "explicit specialization of %qD outside its "
    2853              :                    "namespace must use a nested-name-specifier", tmpl);
    2854           11 :       else if (processing_explicit_instantiation
    2855           11 :                && cxx_dialect >= cxx11)
    2856              :         /* This was allowed in C++98, so only pedwarn.  */
    2857            9 :         pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
    2858              :                  "outside its namespace must use a nested-name-"
    2859              :                  "specifier", tmpl);
    2860              :     }
    2861     16760147 : }
    2862              : 
    2863              : /* Warn for a template specialization SPEC that is missing some of a set
    2864              :    of function or type attributes that the template TEMPL is declared with.
    2865              :    ATTRLIST is a list of additional attributes that SPEC should be taken
    2866              :    to ultimately be declared with.  */
    2867              : 
    2868              : static void
    2869      1618112 : warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
    2870              : {
    2871      1618112 :   if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    2872              :     tmpl = DECL_TEMPLATE_RESULT (tmpl);
    2873              : 
    2874              :   /* Avoid warning if the difference between the primary and
    2875              :      the specialization is not in one of the attributes below.  */
    2876      1618112 :   const char* const blacklist[] = {
    2877              :     "alloc_align", "alloc_size", "assume_aligned", "format",
    2878              :     "format_arg", "malloc", "nonnull", NULL
    2879              :   };
    2880              : 
    2881              :   /* Put together a list of the black listed attributes that the primary
    2882              :      template is declared with that the specialization is not, in case
    2883              :      it's not apparent from the most recent declaration of the primary.  */
    2884      1618112 :   auto_vec<const char *> mismatches;
    2885      1618112 :   unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
    2886              :                                                  blacklist, mismatches);
    2887              : 
    2888      1618112 :   if (!nattrs)
    2889      1618085 :     return;
    2890              : 
    2891           27 :   auto_diagnostic_group d;
    2892           27 :   if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
    2893              :                   "explicit specialization %q#D may be missing attributes",
    2894              :                   spec))
    2895              :     {
    2896           27 :       pp_markup::comma_separated_quoted_strings e (mismatches);
    2897           51 :       inform (DECL_SOURCE_LOCATION (tmpl),
    2898              :               nattrs > 1
    2899              :               ? G_("missing primary template attributes %e")
    2900              :               : G_("missing primary template attribute %e"),
    2901              :               &e);
    2902           27 :     }
    2903      1618112 : }
    2904              : 
    2905              : /* Perform type checking in explicit instantiation of variable templates
    2906              :    as per CWG 1704.  That is, detect
    2907              :      template<typename T> T var = {};
    2908              :      template int *var<int>;
    2909              :    where there's a mismatch 'int' x 'int *'.  INST is the instantiation
    2910              :    of the variable template, DECL is the VAR_DECL from the template-id
    2911              :    used in the explicit instantiation.  */
    2912              : 
    2913              : static bool
    2914          242 : check_explicit_inst_of_var_template (tree inst, tree decl)
    2915              : {
    2916          242 :   if (!inst || inst == error_mark_node)
    2917              :     return true;
    2918              : 
    2919          242 :   tree type1 = TREE_TYPE (inst);
    2920          242 :   tree type2 = TREE_TYPE (decl);
    2921              : 
    2922              :   /* Redeclaration with type auto is OK.  */
    2923          242 :   if (is_auto (type1) || is_auto (type2))
    2924              :     return true;
    2925              : 
    2926              :   /* Absence of major array bound is permitted.  */
    2927          224 :   if (TREE_CODE (type1) == ARRAY_TYPE
    2928           18 :       && TREE_CODE (type2) == ARRAY_TYPE
    2929          242 :       && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
    2930              :     {
    2931            9 :       type1 = TREE_TYPE (type1);
    2932            9 :       type2 = TREE_TYPE (type2);
    2933              :     }
    2934              : 
    2935          224 :   if (same_type_p (type1, type2))
    2936              :     /* All good.  */
    2937              :     return true;
    2938              : 
    2939           40 :   auto_diagnostic_group d;
    2940           40 :   error ("type %qT for explicit instantiation %qD does not match declared "
    2941              :          "type %qT", type2, decl, type1);
    2942           40 :   inform (DECL_SOURCE_LOCATION (inst), "variable template declared here");
    2943           40 :   return false;
    2944           40 : }
    2945              : 
    2946              : /* Check to see if the function just declared, as indicated in
    2947              :    DECLARATOR, and in DECL, is a specialization of a function
    2948              :    template.  We may also discover that the declaration is an explicit
    2949              :    instantiation at this point.
    2950              : 
    2951              :    Returns DECL, or an equivalent declaration that should be used
    2952              :    instead if all goes well.  Issues an error message if something is
    2953              :    amiss.  Returns error_mark_node if the error is not easily
    2954              :    recoverable.
    2955              : 
    2956              :    FLAGS is a bitmask consisting of the following flags:
    2957              : 
    2958              :    2: The function has a definition.
    2959              :    4: The function is a friend.
    2960              : 
    2961              :    The TEMPLATE_COUNT is the number of references to qualifying
    2962              :    template classes that appeared in the name of the function.  For
    2963              :    example, in
    2964              : 
    2965              :      template <class T> struct S { void f(); };
    2966              :      void S<int>::f();
    2967              : 
    2968              :    the TEMPLATE_COUNT would be 1.  However, explicitly specialized
    2969              :    classes are not counted in the TEMPLATE_COUNT, so that in
    2970              : 
    2971              :      template <class T> struct S {};
    2972              :      template <> struct S<int> { void f(); }
    2973              :      template <> void S<int>::f();
    2974              : 
    2975              :    the TEMPLATE_COUNT would be 0.  (Note that this declaration is
    2976              :    invalid; there should be no template <>.)
    2977              : 
    2978              :    If the function is a specialization, it is marked as such via
    2979              :    DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
    2980              :    is set up correctly, and it is added to the list of specializations
    2981              :    for that template.  */
    2982              : 
    2983              : tree
    2984    261993447 : check_explicit_specialization (tree declarator,
    2985              :                                tree decl,
    2986              :                                int template_count,
    2987              :                                int flags,
    2988              :                                tree attrlist)
    2989              : {
    2990    261993447 :   int have_def = flags & 2;
    2991    261993447 :   int is_friend = flags & 4;
    2992    261993447 :   bool is_concept = flags & 8;
    2993    261993447 :   int specialization = 0;
    2994    261993447 :   int explicit_instantiation = 0;
    2995    261993447 :   int member_specialization = 0;
    2996    261993447 :   tree ctype = DECL_CLASS_CONTEXT (decl);
    2997    261993447 :   tree dname = DECL_NAME (decl);
    2998    261993447 :   tmpl_spec_kind tsk;
    2999              : 
    3000    261993447 :   if (is_friend)
    3001              :     {
    3002        85172 :       if (!processing_specialization)
    3003              :         tsk = tsk_none;
    3004              :       else
    3005              :         tsk = tsk_excessive_parms;
    3006              :     }
    3007              :   else
    3008    261908275 :     tsk = current_tmpl_spec_kind (template_count);
    3009              : 
    3010    261908275 :   switch (tsk)
    3011              :     {
    3012    203549095 :     case tsk_none:
    3013    203549095 :       if (processing_specialization && !VAR_P (decl))
    3014              :         {
    3015       470615 :           specialization = 1;
    3016       470615 :           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    3017              :         }
    3018    203078480 :       else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
    3019    203078480 :                || (DECL_LANG_SPECIFIC (decl)
    3020    137070880 :                    && DECL_IMPLICIT_INSTANTIATION (decl)))
    3021              :         {
    3022        85184 :           if (is_friend)
    3023              :             /* This could be something like:
    3024              : 
    3025              :                template <class T> void f(T);
    3026              :                class S { friend void f<>(int); }  */
    3027              :             specialization = 1;
    3028              :           else
    3029              :             {
    3030              :               /* This case handles bogus declarations like template <>
    3031              :                  template <class T> void f<int>(); */
    3032              : 
    3033           12 :               error_at (cp_expr_loc_or_input_loc (declarator),
    3034              :                         "template-id %qE in declaration of primary template",
    3035              :                         declarator);
    3036           12 :               return decl;
    3037              :             }
    3038              :         }
    3039              :       break;
    3040              : 
    3041            0 :     case tsk_invalid_member_spec:
    3042              :       /* The error has already been reported in
    3043              :          check_specialization_scope.  */
    3044            0 :       return error_mark_node;
    3045              : 
    3046            0 :     case tsk_invalid_expl_inst:
    3047            0 :       error ("template parameter list used in explicit instantiation");
    3048              : 
    3049              :       /* Fall through.  */
    3050              : 
    3051      2061053 :     case tsk_expl_inst:
    3052      2061053 :       if (have_def)
    3053            0 :         error ("definition provided for explicit instantiation");
    3054              : 
    3055              :       explicit_instantiation = 1;
    3056              :       break;
    3057              : 
    3058            0 :     case tsk_excessive_parms:
    3059            0 :     case tsk_insufficient_parms:
    3060            0 :       if (tsk == tsk_excessive_parms)
    3061            0 :         error ("too many template parameter lists in declaration of %qD",
    3062              :                decl);
    3063            0 :       else if (template_header_count)
    3064            0 :         error("too few template parameter lists in declaration of %qD", decl);
    3065              :       else
    3066            0 :         error("explicit specialization of %qD must be introduced by "
    3067              :               "%<template <>%>", decl);
    3068              : 
    3069              :       /* Fall through.  */
    3070      1062431 :     case tsk_expl_spec:
    3071      1062431 :       if (is_concept)
    3072            0 :         error ("explicit specialization declared %<concept%>");
    3073              : 
    3074      1062431 :       if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
    3075              :         /* In cases like template<> constexpr bool v = true;
    3076              :            We'll give an error in check_template_variable.  */
    3077              :         break;
    3078              : 
    3079      1062419 :       SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    3080      1062419 :       if (ctype)
    3081              :         member_specialization = 1;
    3082              :       else
    3083              :         specialization = 1;
    3084              :       break;
    3085              : 
    3086     55320868 :     case tsk_template:
    3087     55320868 :       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
    3088              :         {
    3089              :           /* This case handles bogus declarations like template <>
    3090              :              template <class T> void f<int>(); */
    3091              : 
    3092      1051395 :           if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
    3093           15 :             error_at (cp_expr_loc_or_input_loc (declarator),
    3094              :                       "template-id %qE in declaration of primary template",
    3095              :                       declarator);
    3096      1051383 :           else if (variable_template_p (TREE_OPERAND (declarator, 0)))
    3097              :             {
    3098              :               /* Partial specialization of variable template.  */
    3099      1051368 :               SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    3100      1051368 :               specialization = 1;
    3101      1051368 :               goto ok;
    3102              :             }
    3103           15 :           else if (cxx_dialect < cxx14)
    3104            5 :             error_at (cp_expr_loc_or_input_loc (declarator),
    3105              :                       "non-type partial specialization %qE "
    3106              :                       "is not allowed", declarator);
    3107              :           else
    3108           16 :             error_at (cp_expr_loc_or_input_loc (declarator),
    3109              :                       "non-class, non-variable partial specialization %qE "
    3110              :                       "is not allowed", declarator);
    3111              :           return decl;
    3112      1051368 :         ok:;
    3113              :         }
    3114              : 
    3115     55320841 :       if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
    3116              :         /* This is a specialization of a member template, without
    3117              :            specialization the containing class.  Something like:
    3118              : 
    3119              :              template <class T> struct S {
    3120              :                template <class U> void f (U);
    3121              :              };
    3122              :              template <> template <class U> void S<int>::f(U) {}
    3123              : 
    3124              :            That's a specialization -- but of the entire template.  */
    3125              :         specialization = 1;
    3126              :       break;
    3127              : 
    3128            0 :     default:
    3129            0 :       gcc_unreachable ();
    3130              :     }
    3131              : 
    3132       470615 :   if ((specialization || member_specialization)
    3133              :       /* This doesn't apply to variable templates.  */
    3134     56939047 :       && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
    3135              :     {
    3136       668253 :       tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
    3137      2835356 :       for (; t; t = TREE_CHAIN (t))
    3138      2167106 :         if (TREE_PURPOSE (t))
    3139              :           {
    3140            3 :             permerror (input_location,
    3141              :                        "default argument specified in explicit specialization");
    3142            3 :             break;
    3143              :           }
    3144              :     }
    3145              : 
    3146    261993408 :   if (specialization || member_specialization || explicit_instantiation)
    3147              :     {
    3148      4731845 :       tree tmpl = NULL_TREE;
    3149      4731845 :       tree targs = NULL_TREE;
    3150      4731845 :       bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
    3151      4731845 :       bool found_hidden = false;
    3152              : 
    3153              :       /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
    3154      4731845 :       if (!was_template_id)
    3155              :         {
    3156      1754976 :           tree fns;
    3157              : 
    3158      1754976 :           gcc_assert (identifier_p (declarator));
    3159      1754976 :           if (ctype)
    3160              :             fns = dname;
    3161              :           else
    3162              :             {
    3163              :               /* If there is no class context, the explicit instantiation
    3164              :                  must be at namespace scope.  */
    3165       852896 :               gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
    3166              : 
    3167              :               /* Find the namespace binding, using the declaration
    3168              :                  context.  */
    3169       852896 :               fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
    3170              :                                            LOOK_want::NORMAL, true);
    3171       852896 :               if (fns == error_mark_node)
    3172              :                 {
    3173              :                   /* If lookup fails, look for a friend declaration so we can
    3174              :                      give a better diagnostic.  */
    3175           22 :                   fns = (lookup_qualified_name
    3176           22 :                          (CP_DECL_CONTEXT (decl), dname,
    3177              :                           LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
    3178              :                           /*complain*/true));
    3179           22 :                   found_hidden = true;
    3180              :                 }
    3181              : 
    3182       852896 :               if (fns == error_mark_node || !is_overloaded_fn (fns))
    3183              :                 {
    3184           22 :                   error ("%qD is not a template function", dname);
    3185           22 :                   fns = error_mark_node;
    3186              :                 }
    3187              :             }
    3188              : 
    3189      1754976 :           declarator = lookup_template_function (fns, NULL_TREE);
    3190              :         }
    3191              : 
    3192      4731845 :       if (declarator == error_mark_node)
    3193      2062365 :         return error_mark_node;
    3194              : 
    3195      4731823 :       if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
    3196              :         {
    3197            0 :           if (!explicit_instantiation)
    3198              :             /* A specialization in class scope.  This is invalid,
    3199              :                but the error will already have been flagged by
    3200              :                check_specialization_scope.  */
    3201              :             return error_mark_node;
    3202              :           else
    3203              :             {
    3204              :               /* It's not valid to write an explicit instantiation in
    3205              :                  class scope, e.g.:
    3206              : 
    3207              :                    class C { template void f(); }
    3208              : 
    3209              :                    This case is caught by the parser.  However, on
    3210              :                    something like:
    3211              : 
    3212              :                    template class C { void f(); };
    3213              : 
    3214              :                    (which is invalid) we can get here.  The error will be
    3215              :                    issued later.  */
    3216            0 :               ;
    3217              :             }
    3218              : 
    3219            0 :           return decl;
    3220              :         }
    3221      4731823 :       else if (ctype != NULL_TREE
    3222      4731823 :                && (identifier_p (TREE_OPERAND (declarator, 0))))
    3223              :         {
    3224              :           // We'll match variable templates in start_decl.
    3225       902080 :           if (VAR_P (decl))
    3226              :             return decl;
    3227              : 
    3228              :           /* Find the list of functions in ctype that have the same
    3229              :              name as the declared function.  */
    3230       902006 :           tree name = TREE_OPERAND (declarator, 0);
    3231              : 
    3232       902006 :           if (constructor_name_p (name, ctype))
    3233              :             {
    3234            0 :               if (DECL_CONSTRUCTOR_P (decl)
    3235            0 :                   ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
    3236            0 :                   : !CLASSTYPE_DESTRUCTOR (ctype))
    3237              :                 {
    3238              :                   /* From [temp.expl.spec]:
    3239              : 
    3240              :                      If such an explicit specialization for the member
    3241              :                      of a class template names an implicitly-declared
    3242              :                      special member function (clause _special_), the
    3243              :                      program is ill-formed.
    3244              : 
    3245              :                      Similar language is found in [temp.explicit].  */
    3246            0 :                   error ("specialization of implicitly-declared special member function");
    3247            0 :                   return error_mark_node;
    3248              :                 }
    3249              : 
    3250            0 :               name = DECL_NAME (decl);
    3251              :             }
    3252              : 
    3253              :           /* For a type-conversion operator, We might be looking for
    3254              :              `operator int' which will be a specialization of
    3255              :              `operator T'.  Grab all the conversion operators, and
    3256              :              then select from them.  */
    3257       902006 :           tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
    3258              :                                         ? conv_op_identifier : name);
    3259              : 
    3260       902006 :           if (fns == NULL_TREE)
    3261              :             {
    3262            0 :               error ("no member function %qD declared in %qT", name, ctype);
    3263            0 :               return error_mark_node;
    3264              :             }
    3265              :           else
    3266       902006 :             TREE_OPERAND (declarator, 0) = fns;
    3267              :         }
    3268              : 
    3269              :       /* Figure out what exactly is being specialized at this point.
    3270              :          Note that for an explicit instantiation, even one for a
    3271              :          member function, we cannot tell a priori whether the
    3272              :          instantiation is for a member template, or just a member
    3273              :          function of a template class.  Even if a member template is
    3274              :          being instantiated, the member template arguments may be
    3275              :          elided if they can be deduced from the rest of the
    3276              :          declaration.  */
    3277      4731749 :       tmpl = determine_specialization (declarator, decl,
    3278              :                                        &targs,
    3279              :                                        member_specialization,
    3280              :                                        template_count,
    3281              :                                        tsk);
    3282              : 
    3283      4731749 :       if (!tmpl || tmpl == error_mark_node)
    3284              :         /* We couldn't figure out what this declaration was
    3285              :            specializing.  */
    3286          240 :         return error_mark_node;
    3287              :       else
    3288              :         {
    3289      4731509 :           if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
    3290              :             {
    3291            9 :               auto_diagnostic_group d;
    3292            9 :               if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    3293              :                            "friend declaration %qD is not visible to "
    3294              :                            "explicit specialization", tmpl))
    3295            9 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    3296              :                         "friend declaration here");
    3297            9 :             }
    3298              : 
    3299      4731509 :           if (!ctype && !is_friend
    3300      4731509 :               && CP_DECL_CONTEXT (decl) == current_namespace)
    3301      3682492 :             check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
    3302              : 
    3303      4731509 :           tree gen_tmpl = most_general_template (tmpl);
    3304              : 
    3305      4731509 :           if (explicit_instantiation)
    3306              :             {
    3307              :               /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
    3308              :                  is done by do_decl_instantiation later.  */
    3309              : 
    3310      2060826 :               int arg_depth = TMPL_ARGS_DEPTH (targs);
    3311      2060826 :               int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
    3312              : 
    3313      2060826 :               if (arg_depth > parm_depth)
    3314              :                 {
    3315              :                   /* If TMPL is not the most general template (for
    3316              :                      example, if TMPL is a friend template that is
    3317              :                      injected into namespace scope), then there will
    3318              :                      be too many levels of TARGS.  Remove some of them
    3319              :                      here.  */
    3320       408074 :                   int i;
    3321       408074 :                   tree new_targs;
    3322              : 
    3323       408074 :                   new_targs = make_tree_vec (parm_depth);
    3324       816148 :                   for (i = arg_depth - parm_depth; i < arg_depth; ++i)
    3325       816148 :                     TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
    3326       408074 :                       = TREE_VEC_ELT (targs, i);
    3327       408074 :                   targs = new_targs;
    3328              :                 }
    3329              : 
    3330      2060826 :               tree e1;
    3331      2060826 :               tree inst = instantiate_template (tmpl, targs, tf_error);
    3332      2060826 :               if (variable_template_p (tmpl)
    3333      2060826 :                   && !check_explicit_inst_of_var_template (inst, decl))
    3334           40 :                 return error_mark_node;
    3335              :               /* [except.spec] In an explicit instantiation
    3336              :                  a noexcept-specifier may be specified, but is not required.
    3337              :                  If a noexcept-specifier is specified in an explicit
    3338              :                  instantiation, the exception specification shall be the same
    3339              :                  as the exception specification of all other declarations of
    3340              :                  that function.  */
    3341      2060786 :               else if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    3342      2060584 :                        && (e1 = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)))
    3343              :                        /* This could be the implicit noexcept(true) given
    3344              :                           to destructors, but here we are interested only
    3345              :                           in user-written noexcept-specs which would have
    3346              :                           been evaluated by now.  */
    3347       282927 :                        && !UNEVALUATED_NOEXCEPT_SPEC_P (e1)
    3348      2343708 :                        && maybe_instantiate_noexcept (inst))
    3349              :                 {
    3350       282922 :                   tree e2 = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inst));
    3351       282922 :                   if (!comp_except_specs (e1, e2, ce_normal))
    3352              :                     {
    3353           24 :                       auto_diagnostic_group d;
    3354           24 :                       error ("exception specification %qX in explicit "
    3355              :                              "instantiation does not match the instantiated "
    3356              :                              "one %qX", e1, e2 ? e2 : noexcept_false_spec);
    3357           24 :                       inform (DECL_SOURCE_LOCATION (tmpl),
    3358              :                               "template declared here");
    3359           24 :                       return error_mark_node;
    3360           24 :                     }
    3361              :                 }
    3362              : 
    3363              :               return inst;
    3364              :             }
    3365              : 
    3366              :           /* If we thought that the DECL was a member function, but it
    3367              :              turns out to be specializing a static member function,
    3368              :              make DECL a static member function as well.  */
    3369      2670683 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    3370       668159 :               && DECL_STATIC_FUNCTION_P (tmpl)
    3371      2671973 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    3372         1281 :             revert_static_member_fn (decl);
    3373              : 
    3374              :           /* If this is a specialization of a member template of a
    3375              :              template class, we want to return the TEMPLATE_DECL, not
    3376              :              the specialization of it.  */
    3377      2670683 :           if (tsk == tsk_template && !was_template_id)
    3378              :             {
    3379         1203 :               tree result = DECL_TEMPLATE_RESULT (tmpl);
    3380         1203 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    3381         1203 :               DECL_INITIAL (result) = NULL_TREE;
    3382         1203 :               if (have_def)
    3383              :                 {
    3384         1191 :                   tree parm;
    3385         1191 :                   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    3386         2382 :                   DECL_SOURCE_LOCATION (result)
    3387         1191 :                     = DECL_SOURCE_LOCATION (decl);
    3388              :                   /* We want to use the argument list specified in the
    3389              :                      definition, not in the original declaration.  */
    3390         1191 :                   DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
    3391         2459 :                   for (parm = DECL_ARGUMENTS (result); parm;
    3392         1268 :                        parm = DECL_CHAIN (parm))
    3393         1268 :                     DECL_CONTEXT (parm) = result;
    3394              :                 }
    3395         1203 :               decl = register_specialization (tmpl, gen_tmpl, targs,
    3396              :                                               is_friend, 0);
    3397         1203 :               if (flag_contracts)
    3398           38 :                 remove_fn_contract_specifiers (result);
    3399              :               return decl;
    3400              :             }
    3401              : 
    3402              :           /* Set up the DECL_TEMPLATE_INFO for DECL.  */
    3403      2669480 :           DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
    3404              : 
    3405      2669480 :           if (was_template_id)
    3406      2088452 :             TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
    3407              : 
    3408              :           /* Inherit default function arguments from the template
    3409              :              DECL is specializing.  */
    3410      2669480 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    3411       666956 :             copy_default_args_to_explicit_spec (decl);
    3412              : 
    3413              :           /* This specialization has the same protection as the
    3414              :              template it specializes.  */
    3415      2669480 :           TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
    3416      2669480 :           TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
    3417              : 
    3418              :           /* 7.1.1-1 [dcl.stc]
    3419              : 
    3420              :              A storage-class-specifier shall not be specified in an
    3421              :              explicit specialization...
    3422              : 
    3423              :              The parser rejects these, so unless action is taken here,
    3424              :              explicit function specializations will always appear with
    3425              :              global linkage.
    3426              : 
    3427              :              The action recommended by the C++ CWG in response to C++
    3428              :              defect report 605 is to make the storage class and linkage
    3429              :              of the explicit specialization match the templated function:
    3430              : 
    3431              :              http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
    3432              :            */
    3433      2669480 :           if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
    3434              :             {
    3435       111192 :               tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
    3436       111192 :               gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
    3437              : 
    3438              :               /* This specialization has the same linkage and visibility as
    3439              :                  the function template it specializes.  */
    3440       111192 :               TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
    3441       111192 :               if (! TREE_PUBLIC (decl))
    3442              :                 {
    3443           37 :                   DECL_INTERFACE_KNOWN (decl) = 1;
    3444           37 :                   DECL_NOT_REALLY_EXTERN (decl) = 1;
    3445              :                 }
    3446       111192 :               DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
    3447       111192 :               if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
    3448              :                 {
    3449        13103 :                   DECL_VISIBILITY_SPECIFIED (decl) = 1;
    3450        13103 :                   DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
    3451              :                 }
    3452              :             }
    3453              : 
    3454              :           /* If DECL is a friend declaration, declared using an
    3455              :              unqualified name, the namespace associated with DECL may
    3456              :              have been set incorrectly.  For example, in:
    3457              : 
    3458              :                template <typename T> void f(T);
    3459              :                namespace N {
    3460              :                  struct S { friend void f<int>(int); }
    3461              :                }
    3462              : 
    3463              :              we will have set the DECL_CONTEXT for the friend
    3464              :              declaration to N, rather than to the global namespace.  */
    3465      2669480 :           if (DECL_NAMESPACE_SCOPE_P (decl))
    3466      2198560 :             DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
    3467              : 
    3468      2669480 :           if (is_friend && !have_def)
    3469              :             /* This is not really a declaration of a specialization.
    3470              :                It's just the name of an instantiation.  But, it's not
    3471              :                a request for an instantiation, either.  */
    3472        85158 :             SET_DECL_IMPLICIT_INSTANTIATION (decl);
    3473      2584322 :           else if (TREE_CODE (decl) == FUNCTION_DECL)
    3474              :             /* A specialization is not necessarily COMDAT.  */
    3475       581798 :             DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
    3476       581798 :                                   && DECL_DECLARED_INLINE_P (decl));
    3477      2002524 :           else if (VAR_P (decl))
    3478      2002524 :             DECL_COMDAT (decl) = false;
    3479              : 
    3480              :           /* If this is a full specialization, register it so that we can find
    3481              :              it again.  Partial specializations will be registered in
    3482              :              process_partial_specialization.  */
    3483      2669480 :           if (!processing_template_decl)
    3484              :             {
    3485      1618112 :               warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
    3486              : 
    3487      1618112 :               decl = register_specialization (decl, gen_tmpl, targs,
    3488              :                                               is_friend, 0);
    3489              :             }
    3490              : 
    3491      2669480 :           if (flag_contracts
    3492       495199 :               && decl != error_mark_node
    3493      3164671 :               && DECL_TEMPLATE_SPECIALIZATION (decl))
    3494       481467 :             remove_fn_contract_specifiers (decl);
    3495              : 
    3496              :           /* A 'structor should already have clones.  */
    3497      3336415 :           gcc_assert (decl == error_mark_node
    3498              :                       || variable_template_p (tmpl)
    3499              :                       || !(DECL_CONSTRUCTOR_P (decl)
    3500              :                            || DECL_DESTRUCTOR_P (decl))
    3501              :                       || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    3502              :         }
    3503              : 
    3504      2669480 :       if (decl
    3505      2669480 :           && VAR_OR_FUNCTION_DECL_P (decl)
    3506      2669456 :           && DECL_LANG_SPECIFIC (decl)
    3507      5338936 :           && DECL_TEMPLATE_SPECIALIZATION (decl))
    3508      2584298 :         maybe_diagnose_no_specializations (decl);
    3509              :     }
    3510              : 
    3511              :   return decl;
    3512              : }
    3513              : 
    3514              : /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
    3515              :    parameters.  These are represented in the same format used for
    3516              :    DECL_TEMPLATE_PARMS.  */
    3517              : 
    3518              : int
    3519    184099571 : comp_template_parms (const_tree parms1, const_tree parms2)
    3520              : {
    3521    184099571 :   if (parms1 == parms2)
    3522              :     return 1;
    3523              : 
    3524    168150855 :   tree t1 = TREE_VALUE (parms1);
    3525    168150855 :   tree t2 = TREE_VALUE (parms2);
    3526    168150855 :   int i;
    3527              : 
    3528    168150855 :   gcc_assert (TREE_CODE (t1) == TREE_VEC);
    3529    168150855 :   gcc_assert (TREE_CODE (t2) == TREE_VEC);
    3530              : 
    3531    168150855 :   if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
    3532              :     return 0;
    3533              : 
    3534    169468149 :   for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
    3535              :     {
    3536    115404370 :       tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
    3537    115404370 :       tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
    3538              : 
    3539              :       /* If either of the template parameters are invalid, assume
    3540              :          they match for the sake of error recovery. */
    3541    115404370 :       if (error_operand_p (parm1) || error_operand_p (parm2))
    3542              :         return 1;
    3543              : 
    3544    115404346 :       if (TREE_CODE (parm1) != TREE_CODE (parm2))
    3545              :         return 0;
    3546              : 
    3547    209385520 :       if (TREE_CODE (parm1) == TYPE_DECL
    3548    114571632 :           && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1))
    3549    113787209 :               == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2))))
    3550     94813888 :         continue;
    3551     19757744 :       else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
    3552              :         return 0;
    3553              :     }
    3554              : 
    3555              :   return 1;
    3556              : }
    3557              : 
    3558              : /* Returns true if two template parameters are declared with
    3559              :    equivalent constraints.  */
    3560              : 
    3561              : static bool
    3562    852142607 : template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
    3563              : {
    3564    852142607 :   tree req1 = TREE_TYPE (parm1);
    3565    852142607 :   tree req2 = TREE_TYPE (parm2);
    3566    852142607 :   if (!req1 != !req2)
    3567              :     return false;
    3568    850127612 :   if (req1)
    3569      1275071 :     return cp_tree_equal (req1, req2);
    3570              :   return true;
    3571              : }
    3572              : 
    3573              : /* Returns true when two template parameters are equivalent.  */
    3574              : 
    3575              : static bool
    3576    962280622 : template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
    3577              : {
    3578    962280622 :   tree decl1 = TREE_VALUE (parm1);
    3579    962280622 :   tree decl2 = TREE_VALUE (parm2);
    3580              : 
    3581              :   /* If either of the template parameters are invalid, assume
    3582              :      they match for the sake of error recovery. */
    3583    962280622 :   if (error_operand_p (decl1) || error_operand_p (decl2))
    3584              :     return true;
    3585              : 
    3586              :   /* ... they declare parameters of the same kind.  */
    3587    962280619 :   if (TREE_CODE (decl1) != TREE_CODE (decl2))
    3588              :     return false;
    3589              : 
    3590              :   /* ... one parameter was introduced by a parameter declaration, then
    3591              :      both are. This case arises as a result of eagerly rewriting declarations
    3592              :      during parsing.  */
    3593    924843516 :   if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1)
    3594    924843516 :       != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2))
    3595              :     return false;
    3596              : 
    3597              :   /* ... if either declares a pack, they both do.  */
    3598    924740008 :   if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
    3599              :     return false;
    3600              : 
    3601    847517535 :   if (TREE_CODE (decl1) == PARM_DECL)
    3602              :     {
    3603              :       /* ... if they declare non-type parameters, the types are equivalent.  */
    3604      3161186 :       if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
    3605              :         return false;
    3606              :     }
    3607    844356349 :   else if (TREE_CODE (decl2) == TEMPLATE_DECL)
    3608              :     {
    3609              :       /* ... if they declare template template parameters, their template
    3610              :          parameter lists are equivalent.  */
    3611        29452 :       if (!template_heads_equivalent_p (decl1, decl2))
    3612              :         return false;
    3613              :     }
    3614              : 
    3615              :   /* ... if they are declared with a qualified-concept name, they both
    3616              :      are, and those names are equivalent.  */
    3617    847385436 :   return template_parameter_constraints_equivalent_p (parm1, parm2);
    3618              : }
    3619              : 
    3620              : /* Returns true if two template parameters lists are equivalent.
    3621              :    Two template parameter lists are equivalent if they have the
    3622              :    same length and their corresponding parameters are equivalent.
    3623              : 
    3624              :    PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
    3625              :    data structure returned by DECL_TEMPLATE_PARMS.
    3626              : 
    3627              :    This is generally the same implementation as comp_template_parms
    3628              :    except that it also the concept names and arguments used to
    3629              :    introduce parameters.  */
    3630              : 
    3631              : static bool
    3632   1626674191 : template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
    3633              : {
    3634   1626674191 :   if (parms1 == parms2)
    3635              :     return true;
    3636              : 
    3637   1626674191 :   tree list1 = TREE_VALUE (parms1);
    3638   1626674191 :   tree list2 = TREE_VALUE (parms2);
    3639              : 
    3640   1626674191 :   if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
    3641              :     return 0;
    3642              : 
    3643   1454678249 :   for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
    3644              :     {
    3645    962280622 :       tree parm1 = TREE_VEC_ELT (list1, i);
    3646    962280622 :       tree parm2 = TREE_VEC_ELT (list2, i);
    3647    962280622 :       if (!template_parameters_equivalent_p (parm1, parm2))
    3648              :         return false;
    3649              :     }
    3650              : 
    3651              :   return true;
    3652              : }
    3653              : 
    3654              : /* Return true if the requires-clause of the template parameter lists are
    3655              :    equivalent and false otherwise.  */
    3656              : static bool
    3657    561154793 : template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
    3658              : {
    3659    561154793 :   tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
    3660    561154793 :   tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
    3661    561154793 :   if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
    3662              :     return false;
    3663    476138639 :   if (!cp_tree_equal (req1, req2))
    3664              :     return false;
    3665              :   return true;
    3666              : }
    3667              : 
    3668              : /* Returns true if two template heads are equivalent. 17.6.6.1p6:
    3669              :    Two template heads are equivalent if their template parameter
    3670              :    lists are equivalent and their requires clauses are equivalent.
    3671              : 
    3672              :    In pre-C++20, this is equivalent to calling comp_template_parms
    3673              :    for the template parameters of TMPL1 and TMPL2.  */
    3674              : 
    3675              : bool
    3676   1626674191 : template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
    3677              : {
    3678   1626674191 :   tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
    3679   1626674191 :   tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
    3680              : 
    3681              :   /* ... have the same number of template parameters, and their
    3682              :      corresponding parameters are equivalent.  */
    3683   1626674191 :   if (!template_parameter_lists_equivalent_p (parms1, parms2))
    3684              :     return false;
    3685              : 
    3686              :   /* ... if either has a requires-clause, they both do and their
    3687              :      corresponding constraint-expressions are equivalent.  */
    3688    492397627 :   return template_requirements_equivalent_p (parms1, parms2);
    3689              : }
    3690              : 
    3691              : /* Determine whether PARM is a parameter pack.  */
    3692              : 
    3693              : bool
    3694   6316547745 : template_parameter_pack_p (const_tree parm)
    3695              : {
    3696              :   /* Determine if we have a non-type template parameter pack.  */
    3697   6316547745 :   if (TREE_CODE (parm) == PARM_DECL)
    3698    518429112 :     return (DECL_TEMPLATE_PARM_P (parm)
    3699    518429112 :             && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
    3700   5798118633 :   if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
    3701      1313033 :     return TEMPLATE_PARM_PARAMETER_PACK (parm);
    3702              : 
    3703              :   /* If this is a list of template parameters, we could get a
    3704              :      TYPE_DECL or a TEMPLATE_DECL.  */
    3705   5796805600 :   if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
    3706   5561678000 :     parm = TREE_TYPE (parm);
    3707              : 
    3708              :   /* Otherwise it must be a type template parameter.  */
    3709   5796805600 :   return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    3710   5796805600 :            || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    3711   5796805600 :           && TEMPLATE_TYPE_PARAMETER_PACK (parm));
    3712              : }
    3713              : 
    3714              : /* Determine if T is a function parameter pack.  */
    3715              : 
    3716              : bool
    3717      2079314 : function_parameter_pack_p (const_tree t)
    3718              : {
    3719      2079314 :   if (t && TREE_CODE (t) == PARM_DECL)
    3720      2079314 :     return DECL_PACK_P (t);
    3721              :   return false;
    3722              : }
    3723              : 
    3724              : /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
    3725              :    PRIMARY_FUNC_TMPL_INST is a primary function template instantiation.  */
    3726              : 
    3727              : tree
    3728      1887740 : get_function_template_decl (const_tree primary_func_tmpl_inst)
    3729              : {
    3730      1887740 :   if (! primary_func_tmpl_inst
    3731      1887740 :       || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
    3732      3775480 :       || ! primary_template_specialization_p (primary_func_tmpl_inst))
    3733              :     return NULL;
    3734              : 
    3735       530635 :   return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
    3736              : }
    3737              : 
    3738              : /* Return true iff the function parameter PARAM_DECL was expanded
    3739              :    from the function parameter pack PACK.  */
    3740              : 
    3741              : bool
    3742       757276 : function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
    3743              : {
    3744       757276 :   if (DECL_ARTIFICIAL (param_decl)
    3745       757276 :       || !function_parameter_pack_p (pack))
    3746              :     return false;
    3747              : 
    3748              :   /* The parameter pack and its pack arguments have the same
    3749              :      DECL_PARM_INDEX.  */
    3750       757276 :   return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
    3751              : }
    3752              : 
    3753              : /* Determine whether ARGS describes a variadic template args list,
    3754              :    i.e., one that is terminated by a template argument pack.  */
    3755              : 
    3756              : static bool
    3757           20 : template_args_variadic_p (tree args)
    3758              : {
    3759           20 :   int nargs;
    3760           20 :   tree last_parm;
    3761              : 
    3762           20 :   if (args == NULL_TREE)
    3763              :     return false;
    3764              : 
    3765           20 :   args = INNERMOST_TEMPLATE_ARGS (args);
    3766           20 :   nargs = TREE_VEC_LENGTH (args);
    3767              : 
    3768           20 :   if (nargs == 0)
    3769              :     return false;
    3770              : 
    3771           20 :   last_parm = TREE_VEC_ELT (args, nargs - 1);
    3772              : 
    3773           20 :   return ARGUMENT_PACK_P (last_parm);
    3774              : }
    3775              : 
    3776              : /* Generate a new name for the parameter pack name NAME (an
    3777              :    IDENTIFIER_NODE) that incorporates its */
    3778              : 
    3779              : static tree
    3780      1631003 : make_ith_pack_parameter_name (tree name, int i)
    3781              : {
    3782              :   /* Munge the name to include the parameter index.  */
    3783              : #define NUMBUF_LEN 128
    3784      1631003 :   char numbuf[NUMBUF_LEN];
    3785      1631003 :   char* newname;
    3786      1631003 :   int newname_len;
    3787              : 
    3788      1631003 :   if (name == NULL_TREE)
    3789              :     return name;
    3790      1622438 :   snprintf (numbuf, NUMBUF_LEN, "%i", i);
    3791      1622438 :   newname_len = IDENTIFIER_LENGTH (name)
    3792      1622438 :                 + strlen (numbuf) + 2;
    3793      1622438 :   newname = (char*)alloca (newname_len);
    3794      1622438 :   snprintf (newname, newname_len,
    3795      1622438 :             "%s#%i", IDENTIFIER_POINTER (name), i);
    3796      1622438 :   return get_identifier (newname);
    3797              : }
    3798              : 
    3799              : /* Return true if T is a primary function, class or alias template
    3800              :    specialization, not including the template pattern.  */
    3801              : 
    3802              : bool
    3803    170464032 : primary_template_specialization_p (const_tree t)
    3804              : {
    3805    170464032 :   if (!t)
    3806              :     return false;
    3807              : 
    3808    170464032 :   if (VAR_OR_FUNCTION_DECL_P (t))
    3809     86779872 :     return (DECL_LANG_SPECIFIC (t)
    3810     86779744 :             && DECL_USE_TEMPLATE (t)
    3811     80987021 :             && DECL_TEMPLATE_INFO (t)
    3812    167766893 :             && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
    3813     83684160 :   else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    3814     83684030 :     return (CLASSTYPE_TEMPLATE_INFO (t)
    3815     83683408 :             && CLASSTYPE_USE_TEMPLATE (t)
    3816    167367438 :             && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
    3817          130 :   else if (alias_template_specialization_p (t, nt_transparent))
    3818              :     return true;
    3819              :   return false;
    3820              : }
    3821              : 
    3822              : /* Return true if PARM is a template template parameter.  */
    3823              : 
    3824              : bool
    3825     58636288 : template_template_parameter_p (const_tree parm)
    3826              : {
    3827     58636288 :   return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
    3828              : }
    3829              : 
    3830              : /* Return true iff PARM is a DECL representing a type template
    3831              :    parameter.  */
    3832              : 
    3833              : bool
    3834   2382178376 : template_type_parameter_p (const_tree parm)
    3835              : {
    3836   2382178376 :   return (parm
    3837   2382178376 :           && (TREE_CODE (parm) == TYPE_DECL
    3838   1166733391 :               || TREE_CODE (parm) == TEMPLATE_DECL)
    3839   3597834480 :           && DECL_TEMPLATE_PARM_P (parm));
    3840              : }
    3841              : 
    3842              : /* Return the template parameters of T if T is a
    3843              :    primary template instantiation, NULL otherwise.  */
    3844              : 
    3845              : tree
    3846    188183516 : get_primary_template_innermost_parameters (const_tree t)
    3847              : {
    3848    188183516 :   tree parms = NULL, template_info = NULL;
    3849              : 
    3850    188183516 :   if ((template_info = get_template_info (t))
    3851    188183516 :       && primary_template_specialization_p (t))
    3852     83588861 :     parms = INNERMOST_TEMPLATE_PARMS
    3853              :         (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
    3854              : 
    3855    188183516 :   return parms;
    3856              : }
    3857              : 
    3858              : /* Returns the template arguments of T if T is a template instantiation,
    3859              :    NULL otherwise.  */
    3860              : 
    3861              : tree
    3862     40321713 : get_template_innermost_arguments (const_tree t)
    3863              : {
    3864     40321713 :   tree args = NULL, template_info = NULL;
    3865              : 
    3866     40321713 :   if ((template_info = get_template_info (t))
    3867     80643426 :       && TI_ARGS (template_info))
    3868     40321713 :     args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
    3869              : 
    3870     40321713 :   return args;
    3871              : }
    3872              : 
    3873              : /* Return the argument pack elements of T if T is a template argument pack,
    3874              :    NULL otherwise.  */
    3875              : 
    3876              : tree
    3877     62644563 : get_template_argument_pack_elems (const_tree t)
    3878              : {
    3879     62644563 :   if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
    3880     62644563 :       && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
    3881              :     return NULL;
    3882              : 
    3883      5249097 :   return ARGUMENT_PACK_ARGS (t);
    3884              : }
    3885              : 
    3886              : /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
    3887              :    ARGUMENT_PACK_SELECT represents. */
    3888              : 
    3889              : static tree
    3890     12211026 : argument_pack_select_arg (tree t)
    3891              : {
    3892     12211026 :   tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
    3893     12211026 :   tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
    3894              : 
    3895              :   /* If the selected argument is an expansion E, that most likely means we were
    3896              :      called from gen_elem_of_pack_expansion_instantiation during the
    3897              :      substituting of an argument pack (of which the Ith element is a pack
    3898              :      expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
    3899              :      In this case, the Ith element resulting from this substituting is going to
    3900              :      be a pack expansion, which pattern is the pattern of E.  Let's return the
    3901              :      pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
    3902              :      resulting pack expansion from it.  */
    3903     12211026 :   if (PACK_EXPANSION_P (arg))
    3904              :     {
    3905              :       /* Make sure we aren't throwing away arg info.  */
    3906       287552 :       gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
    3907       287552 :       arg = PACK_EXPANSION_PATTERN (arg);
    3908              :     }
    3909              : 
    3910     12211026 :   return arg;
    3911              : }
    3912              : 
    3913              : /* Return a modification of ARGS that's suitable for preserving inside a hash
    3914              :    table.  In particular, this replaces each ARGUMENT_PACK_SELECT with its
    3915              :    underlying argument.  ARGS is copied (upon modification) iff COW_P.  */
    3916              : 
    3917              : static tree
    3918       377067 : preserve_args (tree args, bool cow_p = true)
    3919              : {
    3920       377067 :   if (!args)
    3921              :     return NULL_TREE;
    3922              : 
    3923       983296 :   for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    3924              :     {
    3925       606232 :       tree t = TREE_VEC_ELT (args, i);
    3926       606232 :       tree r;
    3927       606232 :       if (!t)
    3928              :         r = NULL_TREE;
    3929       606027 :       else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
    3930           86 :         r = argument_pack_select_arg (t);
    3931       605941 :       else if (TREE_CODE (t) == TREE_VEC)
    3932       116358 :         r = preserve_args (t, cow_p);
    3933              :       else
    3934              :         r = t;
    3935       606027 :       if (r != t)
    3936              :         {
    3937          101 :           if (cow_p)
    3938              :             {
    3939           95 :               args = copy_template_args (args);
    3940           95 :               cow_p = false;
    3941              :             }
    3942          101 :           TREE_VEC_ELT (args, i) = r;
    3943              :         }
    3944              :     }
    3945              : 
    3946              :   return args;
    3947              : }
    3948              : 
    3949              : /* True iff FN is a function representing a built-in variadic parameter
    3950              :    pack.  */
    3951              : 
    3952              : bool
    3953    471907521 : builtin_pack_fn_p (tree fn)
    3954              : {
    3955    471907521 :   if (!fn
    3956    471797111 :       || TREE_CODE (fn) != FUNCTION_DECL
    3957    646053687 :       || !DECL_IS_UNDECLARED_BUILTIN (fn))
    3958              :     return false;
    3959              : 
    3960      9185435 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3961              :     return true;
    3962              : 
    3963              :   return false;
    3964              : }
    3965              : 
    3966              : /* True iff CALL is a call to a function representing a built-in variadic
    3967              :    parameter pack.  */
    3968              : 
    3969              : static bool
    3970    380662611 : builtin_pack_call_p (tree call)
    3971              : {
    3972    380662611 :   if (TREE_CODE (call) != CALL_EXPR)
    3973              :     return false;
    3974    283920716 :   return builtin_pack_fn_p (CALL_EXPR_FN (call));
    3975              : }
    3976              : 
    3977              : /* Return a TREE_VEC for the expansion of __integer_pack(HI).  */
    3978              : 
    3979              : static tree
    3980       114217 : expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
    3981              :                      tree in_decl)
    3982              : {
    3983       114217 :   tree ohi = CALL_EXPR_ARG (call, 0);
    3984       114217 :   tree hi = tsubst_expr (ohi, args, complain, in_decl);
    3985              : 
    3986       114217 :   if (instantiation_dependent_expression_p (hi))
    3987              :     {
    3988       111351 :       if (hi != ohi)
    3989              :         {
    3990       111351 :           call = copy_node (call);
    3991       111351 :           CALL_EXPR_ARG (call, 0) = hi;
    3992              :         }
    3993       111351 :       tree ex = make_pack_expansion (call, complain);
    3994       111351 :       tree vec = make_tree_vec (1);
    3995       111351 :       TREE_VEC_ELT (vec, 0) = ex;
    3996       111351 :       return vec;
    3997              :     }
    3998              :   else
    3999              :     {
    4000         2866 :       hi = instantiate_non_dependent_expr (hi, complain);
    4001         2866 :       hi = cxx_constant_value (hi, complain);
    4002         2866 :       int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
    4003              : 
    4004              :       /* Calculate the largest value of len that won't make the size of the vec
    4005              :          overflow an int.  The compiler will exceed resource limits long before
    4006              :          this, but it seems a decent place to diagnose.  */
    4007         2866 :       int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
    4008              : 
    4009         2866 :       if (len < 0 || len > max)
    4010              :         {
    4011           12 :           if ((complain & tf_error)
    4012            9 :               && hi != error_mark_node)
    4013            9 :             error ("argument to %<__integer_pack%> must be between 0 and %d",
    4014              :                    max);
    4015           12 :           return error_mark_node;
    4016              :         }
    4017              : 
    4018         2854 :       tree vec = make_tree_vec (len);
    4019              : 
    4020        17112 :       for (int i = 0; i < len; ++i)
    4021        11404 :         TREE_VEC_ELT (vec, i) = size_int (i);
    4022              : 
    4023              :       return vec;
    4024              :     }
    4025              : }
    4026              : 
    4027              : /* Return a TREE_VEC for the expansion of built-in template parameter pack
    4028              :    CALL.  */
    4029              : 
    4030              : static tree
    4031       114217 : expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
    4032              :                           tree in_decl)
    4033              : {
    4034       114217 :   if (!builtin_pack_call_p (call))
    4035              :     return NULL_TREE;
    4036              : 
    4037       114217 :   tree fn = CALL_EXPR_FN (call);
    4038              : 
    4039       114217 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    4040       114217 :     return expand_integer_pack (call, args, complain, in_decl);
    4041              : 
    4042              :   return NULL_TREE;
    4043              : }
    4044              : 
    4045              : /* Return true if the tree T has the extra args mechanism for
    4046              :    avoiding partial instantiation.  */
    4047              : 
    4048              : static bool
    4049   8209702981 : has_extra_args_mechanism_p (const_tree t)
    4050              : {
    4051   8209702981 :   return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS  */
    4052   8172340589 :           || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS  */
    4053   8168773579 :           || (TREE_CODE (t) == IF_STMT
    4054      1052557 :               && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS  */
    4055  16378124475 :           || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS  */
    4056              : }
    4057              : 
    4058              : /* Return *_EXTRA_ARGS of the given supported tree T.  */
    4059              : 
    4060              : static tree&
    4061        34545 : tree_extra_args (tree t)
    4062              : {
    4063        34545 :   gcc_checking_assert (has_extra_args_mechanism_p (t));
    4064              : 
    4065        34545 :   if (PACK_EXPANSION_P (t))
    4066        12088 :     return PACK_EXPANSION_EXTRA_ARGS (t);
    4067        22457 :   else if (TREE_CODE (t) == REQUIRES_EXPR)
    4068         3183 :     return REQUIRES_EXPR_EXTRA_ARGS (t);
    4069        19274 :   else if (TREE_CODE (t) == IF_STMT
    4070        19274 :            && IF_STMT_CONSTEXPR_P (t))
    4071        18279 :     return IF_STMT_EXTRA_ARGS (t);
    4072          995 :   else if (TREE_CODE (t) == LAMBDA_EXPR)
    4073          995 :     return LAMBDA_EXPR_EXTRA_ARGS (t);
    4074              : 
    4075            0 :   gcc_unreachable ();
    4076              : }
    4077              : 
    4078              : /* Structure used to track the progress of find_parameter_packs_r.  */
    4079   2254517683 : struct find_parameter_pack_data
    4080              : {
    4081              :   /* TREE_LIST that will contain all of the parameter packs found by
    4082              :      the traversal.  */
    4083              :   tree* parameter_packs;
    4084              : 
    4085              :   /* Set of AST nodes that have been visited by the traversal.  */
    4086              :   hash_set<tree> *visited;
    4087              : 
    4088              :   /* True iff we found a subtree that has the extra args mechanism.  */
    4089              :   bool found_extra_args_tree_p = false;
    4090              : };
    4091              : 
    4092              : /* Identifies all of the argument packs that occur in a template
    4093              :    argument and appends them to the TREE_LIST inside DATA, which is a
    4094              :    find_parameter_pack_data structure. This is a subroutine of
    4095              :    make_pack_expansion and uses_parameter_packs.  */
    4096              : static tree
    4097   8424705140 : find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
    4098              : {
    4099   8424705140 :   tree t = *tp;
    4100   8424705140 :   struct find_parameter_pack_data* ppd =
    4101              :     (struct find_parameter_pack_data*)data;
    4102   8424705140 :   bool parameter_pack_p = false;
    4103              : 
    4104              : #define WALK_SUBTREE(NODE)                              \
    4105              :   cp_walk_tree (&(NODE), &find_parameter_packs_r,       \
    4106              :                 ppd, ppd->visited)                   \
    4107              : 
    4108              :   /* Don't look through typedefs; we are interested in whether a
    4109              :      parameter pack is actually written in the expression/type we're
    4110              :      looking at, not the target type.  */
    4111   8424705140 :   if (TYPE_P (t) && typedef_variant_p (t))
    4112              :     {
    4113              :       /* But do look at arguments for an alias template.  */
    4114    216047221 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    4115    163541023 :         cp_walk_tree (&TI_ARGS (tinfo),
    4116              :                       &find_parameter_packs_r,
    4117              :                       ppd, ppd->visited);
    4118    216047221 :       *walk_subtrees = 0;
    4119    216047221 :       return NULL_TREE;
    4120              :     }
    4121              : 
    4122              :   /* Identify whether this is a parameter pack or not.  */
    4123   8208657919 :   switch (TREE_CODE (t))
    4124              :     {
    4125     80654227 :     case TEMPLATE_PARM_INDEX:
    4126     80654227 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    4127              :         parameter_pack_p = true;
    4128              :       break;
    4129              : 
    4130   1321643091 :     case TEMPLATE_TYPE_PARM:
    4131   1321643091 :       t = TYPE_MAIN_VARIANT (t);
    4132              :       /* FALLTHRU */
    4133   1322589744 :     case TEMPLATE_TEMPLATE_PARM:
    4134   1322589744 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    4135              :         parameter_pack_p = true;
    4136              :       break;
    4137              : 
    4138    479884603 :     case FIELD_DECL:
    4139    479884603 :     case PARM_DECL:
    4140    479884603 :       if (DECL_PACK_P (t))
    4141              :         {
    4142              :           /* We don't want to walk into the type of a PARM_DECL,
    4143              :              because we don't want to see the type parameter pack.  */
    4144      3798789 :           *walk_subtrees = 0;
    4145      3798789 :           parameter_pack_p = true;
    4146              :         }
    4147              :       break;
    4148              : 
    4149    217810164 :     case VAR_DECL:
    4150    217810164 :       if (DECL_PACK_P (t))
    4151              :         {
    4152              :           /* We don't want to walk into the type of a variadic capture proxy,
    4153              :              because we don't want to see the type parameter pack.  */
    4154         6025 :           *walk_subtrees = 0;
    4155         6025 :           parameter_pack_p = true;
    4156              :         }
    4157    217804139 :       else if (variable_template_specialization_p (t))
    4158              :         {
    4159       214913 :           cp_walk_tree (&DECL_TI_ARGS (t),
    4160              :                         find_parameter_packs_r,
    4161              :                         ppd, ppd->visited);
    4162       214913 :           *walk_subtrees = 0;
    4163              :         }
    4164              :       break;
    4165              : 
    4166    283692279 :     case CALL_EXPR:
    4167    283692279 :       if (builtin_pack_call_p (t))
    4168              :         parameter_pack_p = true;
    4169              :       break;
    4170              : 
    4171              :     case BASES:
    4172              :       parameter_pack_p = true;
    4173              :       break;
    4174              :     default:
    4175              :       /* Not a parameter pack.  */
    4176              :       break;
    4177              :     }
    4178              : 
    4179      4019727 :   if (parameter_pack_p)
    4180              :     {
    4181              :       /* Add this parameter pack to the list.  */
    4182     72462567 :       *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
    4183              :     }
    4184              : 
    4185   8208657919 :   if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
    4186      5775724 :     ppd->found_extra_args_tree_p = true;
    4187              : 
    4188   8208657919 :   if (TYPE_P (t))
    4189   2553636930 :     cp_walk_tree (&TYPE_CONTEXT (t),
    4190              :                   &find_parameter_packs_r, ppd, ppd->visited);
    4191              : 
    4192              :   /* This switch statement will return immediately if we don't find a
    4193              :      parameter pack.  ??? Should some of these be in cp_walk_subtrees?  */
    4194   8208657919 :   switch (TREE_CODE (t))
    4195              :     {
    4196       462991 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    4197              :       /* Check the template itself.  */
    4198       462991 :       cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
    4199              :                     &find_parameter_packs_r, ppd, ppd->visited);
    4200       462991 :       return NULL_TREE;
    4201              : 
    4202     80654227 :     case TEMPLATE_PARM_INDEX:
    4203     80654227 :       if (parameter_pack_p)
    4204      3565182 :         WALK_SUBTREE (TREE_TYPE (t));
    4205              :       return NULL_TREE;
    4206              : 
    4207      8046571 :     case DECL_EXPR:
    4208      8046571 :       {
    4209      8046571 :         tree decl = DECL_EXPR_DECL (t);
    4210              :         /* Ignore the declaration of a capture proxy for a parameter pack.  */
    4211      8046571 :         if (is_capture_proxy (decl))
    4212      2716616 :           *walk_subtrees = 0;
    4213      8046571 :         if (is_typedef_decl (decl))
    4214              :           /* Since we stop at typedefs above, we need to look through them at
    4215              :              the point of the DECL_EXPR.  */
    4216        93961 :           cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
    4217              :                         &find_parameter_packs_r, ppd, ppd->visited);
    4218              :         return NULL_TREE;
    4219              :       }
    4220              : 
    4221    243250612 :     case TEMPLATE_DECL:
    4222    243250612 :       if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
    4223              :         return NULL_TREE;
    4224         5971 :       cp_walk_tree (&TREE_TYPE (t),
    4225              :                     &find_parameter_packs_r, ppd, ppd->visited);
    4226         5971 :       return NULL_TREE;
    4227              : 
    4228     37338216 :     case TYPE_PACK_EXPANSION:
    4229     37338216 :     case EXPR_PACK_EXPANSION:
    4230     37338216 :       *walk_subtrees = 0;
    4231     37338216 :       return NULL_TREE;
    4232              : 
    4233        22619 :     case PACK_INDEX_TYPE:
    4234        22619 :     case PACK_INDEX_EXPR:
    4235              :       /* We can have an expansion of an expansion, such as "Ts...[Is]...",
    4236              :          so do look into the index (but not the pack).  */
    4237        22619 :       cp_walk_tree (&PACK_INDEX_INDEX (t), &find_parameter_packs_r, ppd,
    4238              :                     ppd->visited);
    4239        22619 :       *walk_subtrees = 0;
    4240        22619 :       return NULL_TREE;
    4241              : 
    4242     39384564 :     case INTEGER_TYPE:
    4243     39384564 :       cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
    4244              :                     ppd, ppd->visited);
    4245     39384564 :       *walk_subtrees = 0;
    4246     39384564 :       return NULL_TREE;
    4247              : 
    4248    252449647 :     case IDENTIFIER_NODE:
    4249    252449647 :       cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
    4250              :                     ppd->visited);
    4251    252449647 :       *walk_subtrees = 0;
    4252    252449647 :       return NULL_TREE;
    4253              : 
    4254      1899553 :     case LAMBDA_EXPR:
    4255      1899553 :       {
    4256              :         /* Since we defer implicit capture, look in the parms and body.  */
    4257      1899553 :         tree fn = lambda_function (t);
    4258      1899553 :         cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
    4259              :                       ppd->visited);
    4260      1899553 :         cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
    4261              :                       ppd->visited);
    4262      1899553 :         return NULL_TREE;
    4263              :       }
    4264              : 
    4265     10500113 :     case DECLTYPE_TYPE:
    4266     10500113 :       cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
    4267              :                     ppd, ppd->visited);
    4268     10500113 :       *walk_subtrees = 0;
    4269     10500113 :       return NULL_TREE;
    4270              : 
    4271      1009231 :     case IF_STMT:
    4272      1009231 :       cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
    4273              :                     ppd, ppd->visited);
    4274      1009231 :       cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
    4275              :                     ppd, ppd->visited);
    4276      1009231 :       cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
    4277              :                     ppd, ppd->visited);
    4278              :       /* Don't walk into IF_STMT_EXTRA_ARGS.  */
    4279      1009231 :       *walk_subtrees = 0;
    4280      1009231 :       return NULL_TREE;
    4281              : 
    4282           93 :     case TAG_DEFN:
    4283           93 :       t = TREE_TYPE (t);
    4284           93 :       if (CLASS_TYPE_P (t))
    4285              :         {
    4286              :           /* Local class, need to look through the whole definition.
    4287              :              TYPE_BINFO might be unset for a partial instantiation.  */
    4288           48 :           if (TYPE_BINFO (t))
    4289           54 :             for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
    4290            9 :               cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
    4291              :                             ppd, ppd->visited);
    4292              :         }
    4293              :       else
    4294              :         /* Enum, look at the values.  */
    4295          165 :         for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
    4296          120 :           cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
    4297              :                         &find_parameter_packs_r,
    4298              :                         ppd, ppd->visited);
    4299              :       return NULL_TREE;
    4300              : 
    4301      7079159 :     case FUNCTION_TYPE:
    4302      7079159 :     case METHOD_TYPE:
    4303      7079159 :       WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
    4304      7079159 :       break;
    4305              : 
    4306              :     default:
    4307              :       return NULL_TREE;
    4308              :     }
    4309              : 
    4310              : #undef WALK_SUBTREE
    4311              : 
    4312      7079159 :   return NULL_TREE;
    4313              : }
    4314              : 
    4315              : /* Determines if the expression or type T uses any parameter packs.  */
    4316              : tree
    4317     14272468 : uses_parameter_packs (tree t)
    4318              : {
    4319     14272468 :   tree parameter_packs = NULL_TREE;
    4320     14272468 :   struct find_parameter_pack_data ppd;
    4321     14272468 :   ppd.parameter_packs = &parameter_packs;
    4322     14272468 :   ppd.visited = new hash_set<tree>;
    4323     14272468 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4324     28544936 :   delete ppd.visited;
    4325     14272468 :   return parameter_packs;
    4326              : }
    4327              : 
    4328              : /* Turn ARG, which may be an expression, type, or a TREE_LIST
    4329              :    representation a base-class initializer into a parameter pack
    4330              :    expansion. If all goes well, the resulting node will be an
    4331              :    EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
    4332              :    respectively.  */
    4333              : tree
    4334     66819976 : make_pack_expansion (tree arg, tsubst_flags_t complain)
    4335              : {
    4336     66819976 :   tree result;
    4337     66819976 :   tree parameter_packs = NULL_TREE;
    4338     66819976 :   bool for_types = false;
    4339     66819976 :   struct find_parameter_pack_data ppd;
    4340              : 
    4341     66819976 :   if (!arg || arg == error_mark_node)
    4342              :     return arg;
    4343              : 
    4344     66819961 :   if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
    4345              :     {
    4346              :       /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
    4347              :          class initializer.  In this case, the TREE_PURPOSE will be a
    4348              :          _TYPE node (representing the base class expansion we're
    4349              :          initializing) and the TREE_VALUE will be a TREE_LIST
    4350              :          containing the initialization arguments.
    4351              : 
    4352              :          The resulting expansion looks somewhat different from most
    4353              :          expansions. Rather than returning just one _EXPANSION, we
    4354              :          return a TREE_LIST whose TREE_PURPOSE is a
    4355              :          TYPE_PACK_EXPANSION containing the bases that will be
    4356              :          initialized.  The TREE_VALUE will be identical to the
    4357              :          original TREE_VALUE, which is a list of arguments that will
    4358              :          be passed to each base.  We do not introduce any new pack
    4359              :          expansion nodes into the TREE_VALUE (although it is possible
    4360              :          that some already exist), because the TREE_PURPOSE and
    4361              :          TREE_VALUE all need to be expanded together with the same
    4362              :          _EXPANSION node.  Note that the TYPE_PACK_EXPANSION in the
    4363              :          resulting TREE_PURPOSE will mention the parameter packs in
    4364              :          both the bases and the arguments to the bases.  */
    4365           34 :       tree purpose;
    4366           34 :       tree value;
    4367           34 :       tree parameter_packs = NULL_TREE;
    4368              : 
    4369              :       /* Determine which parameter packs will be used by the base
    4370              :          class expansion.  */
    4371           34 :       ppd.visited = new hash_set<tree>;
    4372           34 :       ppd.parameter_packs = &parameter_packs;
    4373           34 :       gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
    4374           34 :       cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
    4375              :                     &ppd, ppd.visited);
    4376              : 
    4377           34 :       if (parameter_packs == NULL_TREE)
    4378              :         {
    4379            0 :           if (complain & tf_error)
    4380            0 :             error ("base initializer expansion %qT contains no parameter packs",
    4381              :                    arg);
    4382            0 :           delete ppd.visited;
    4383            0 :           return error_mark_node;
    4384              :         }
    4385              : 
    4386           34 :       if (TREE_VALUE (arg) != void_type_node)
    4387              :         {
    4388              :           /* Collect the sets of parameter packs used in each of the
    4389              :              initialization arguments.  */
    4390           65 :           for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
    4391              :             {
    4392              :               /* Determine which parameter packs will be expanded in this
    4393              :                  argument.  */
    4394           37 :               cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
    4395              :                             &ppd, ppd.visited);
    4396              :             }
    4397              :         }
    4398              : 
    4399           68 :       delete ppd.visited;
    4400              : 
    4401              :       /* Create the pack expansion type for the base type.  */
    4402           34 :       purpose = cxx_make_type (TYPE_PACK_EXPANSION);
    4403           34 :       PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
    4404           68 :       PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
    4405           34 :       PACK_EXPANSION_LOCAL_P (purpose) = local_bindings_p ();
    4406              : 
    4407              :       /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4408              :          they will rarely be compared to anything.  */
    4409           34 :       SET_TYPE_STRUCTURAL_EQUALITY (purpose);
    4410              : 
    4411           34 :       return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
    4412              :     }
    4413              : 
    4414     66819927 :   if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
    4415     58599458 :     for_types = true;
    4416              : 
    4417              :   /* Build the PACK_EXPANSION_* node.  */
    4418     66819927 :   result = for_types
    4419     58599458 :      ? cxx_make_type (TYPE_PACK_EXPANSION)
    4420      8220469 :      : make_node (EXPR_PACK_EXPANSION);
    4421     66819927 :   PACK_EXPANSION_PATTERN (result) = arg;
    4422     66819927 :   if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
    4423              :     {
    4424              :       /* Propagate type and const-expression information.  */
    4425      8220469 :       TREE_TYPE (result) = TREE_TYPE (arg);
    4426      8220469 :       TREE_CONSTANT (result) = TREE_CONSTANT (arg);
    4427              :       /* Mark this read now, since the expansion might be length 0.  */
    4428      8220469 :       mark_exp_read (arg);
    4429              :     }
    4430              :   else
    4431              :     /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4432              :        they will rarely be compared to anything.  */
    4433     58599458 :     SET_TYPE_STRUCTURAL_EQUALITY (result);
    4434              : 
    4435              :   /* Determine which parameter packs will be expanded.  */
    4436     66819927 :   ppd.parameter_packs = &parameter_packs;
    4437     66819927 :   ppd.visited = new hash_set<tree>;
    4438     66819927 :   cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
    4439    133639854 :   delete ppd.visited;
    4440              : 
    4441              :   /* Make sure we found some parameter packs.  */
    4442     66819927 :   if (parameter_packs == NULL_TREE)
    4443              :     {
    4444          104 :       if (complain & tf_error)
    4445              :         {
    4446          104 :           if (TYPE_P (arg))
    4447           56 :             error ("expansion pattern %qT contains no parameter packs", arg);
    4448              :           else
    4449           48 :             error ("expansion pattern %qE contains no parameter packs", arg);
    4450              :         }
    4451          104 :       return error_mark_node;
    4452              :     }
    4453    125419225 :   PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
    4454              : 
    4455              :   /* Contract conditions are parsed outside a function body but function
    4456              :      parameter pack expansions in them must use the instantiated parameters
    4457              :      rather than dummy declarations.  */
    4458     66819823 :   PACK_EXPANSION_LOCAL_P (result) = local_bindings_p ();
    4459     66819823 :   if (ppd.found_extra_args_tree_p)
    4460              :     /* If the pattern of this pack expansion contains a subtree that has
    4461              :        the extra args mechanism for avoiding partial instantiation, then
    4462              :        force this pack expansion to also use extra args.  Otherwise
    4463              :        partial instantiation of this pack expansion may not lower the
    4464              :        level of some parameter packs within the pattern, which would
    4465              :        confuse tsubst_pack_expansion later (PR101764).  */
    4466        83172 :     PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
    4467              : 
    4468              :   return result;
    4469              : }
    4470              : 
    4471              : /* Create a PACK_INDEX_* using the pack expansion PACK and index INDEX.  */
    4472              : 
    4473              : tree
    4474        23687 : make_pack_index (tree pack, tree index)
    4475              : {
    4476        23687 :   if (pack == error_mark_node)
    4477              :     return error_mark_node;
    4478              : 
    4479        23661 :   bool for_types;
    4480        23661 :   if (TREE_CODE (pack) == TYPE_PACK_EXPANSION)
    4481              :     for_types = true;
    4482        17083 :   else if (TREE_CODE (pack) == EXPR_PACK_EXPANSION)
    4483              :     for_types = false;
    4484              :   else
    4485              :     {
    4486              :       /* Maybe we've already partially substituted the pack.  */
    4487         3088 :       gcc_checking_assert (TREE_CODE (pack) == TREE_VEC);
    4488         3088 :       for_types = TYPE_P (TREE_VEC_ELT (pack, 0));
    4489              :     }
    4490              : 
    4491         3088 :   tree t = (for_types
    4492         9666 :             ? cxx_make_type (PACK_INDEX_TYPE)
    4493        16570 :             : make_node (PACK_INDEX_EXPR));
    4494        23661 :   PACK_INDEX_PACK (t) = pack;
    4495        23661 :   PACK_INDEX_INDEX (t) = index;
    4496        23661 :   if (TREE_CODE (t) == PACK_INDEX_TYPE)
    4497         7091 :     SET_TYPE_STRUCTURAL_EQUALITY (t);
    4498              :   return t;
    4499              : }
    4500              : 
    4501              : /* Checks T for any "bare" parameter packs, which have not yet been
    4502              :    expanded, and issues an error if any are found. This operation can
    4503              :    only be done on full expressions or types (e.g., an expression
    4504              :    statement, "if" condition, etc.), because we could have expressions like:
    4505              : 
    4506              :      foo(f(g(h(args)))...)
    4507              : 
    4508              :    where "args" is a parameter pack. check_for_bare_parameter_packs
    4509              :    should not be called for the subexpressions args, h(args),
    4510              :    g(h(args)), or f(g(h(args))), because we would produce erroneous
    4511              :    error messages.
    4512              : 
    4513              :    Returns TRUE and emits an error if there were bare parameter packs,
    4514              :    returns FALSE otherwise.  */
    4515              : bool
    4516   2173417196 : check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
    4517              : {
    4518   2173417196 :   tree parameter_packs = NULL_TREE;
    4519   2173417196 :   struct find_parameter_pack_data ppd;
    4520              : 
    4521   2173417196 :   if (!processing_template_decl || !t || t == error_mark_node)
    4522              :     return false;
    4523              : 
    4524   1402189154 :   if (TREE_CODE (t) == TYPE_DECL)
    4525            0 :     t = TREE_TYPE (t);
    4526              : 
    4527   1402189154 :   ppd.parameter_packs = &parameter_packs;
    4528   1402189154 :   ppd.visited = new hash_set<tree>;
    4529   1402189154 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4530   2804378308 :   delete ppd.visited;
    4531              : 
    4532   1402189154 :   if (!parameter_packs)
    4533              :     return false;
    4534              : 
    4535          376 :   if (loc == UNKNOWN_LOCATION)
    4536          370 :     loc = cp_expr_loc_or_input_loc (t);
    4537              : 
    4538              :   /* It's OK for a lambda to have an unexpanded parameter pack from the
    4539              :      containing context, but do complain about unexpanded capture packs.  */
    4540          376 :   tree lam = current_lambda_expr ();
    4541          376 :   if (lam)
    4542          106 :     lam = TREE_TYPE (lam);
    4543              : 
    4544          106 :   if (lam && lam != current_class_type)
    4545              :     {
    4546              :       /* We're in a lambda, but it isn't the innermost class.
    4547              :          This should work, but currently doesn't.  */
    4548            9 :       sorry_at (loc, "unexpanded parameter pack in local class in lambda");
    4549            9 :       return true;
    4550              :     }
    4551              : 
    4552          367 :   if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
    4553          180 :     for (; parameter_packs;
    4554           86 :          parameter_packs = TREE_CHAIN (parameter_packs))
    4555              :       {
    4556           94 :         tree pack = TREE_VALUE (parameter_packs);
    4557           94 :         if (is_capture_proxy (pack)
    4558           94 :             || (TREE_CODE (pack) == PARM_DECL
    4559           24 :                 && DECL_CONTEXT (pack)
    4560           21 :                 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
    4561              :           break;
    4562              :       }
    4563              : 
    4564          367 :   if (parameter_packs)
    4565              :     {
    4566          281 :       auto_diagnostic_group d;
    4567          281 :       error_at (loc, "parameter packs not expanded with %<...%>:");
    4568          843 :       while (parameter_packs)
    4569              :         {
    4570          281 :           tree pack = TREE_VALUE (parameter_packs);
    4571          281 :           tree name = NULL_TREE;
    4572              : 
    4573          281 :           if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
    4574          281 :               || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
    4575          204 :             name = TYPE_NAME (pack);
    4576           77 :           else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
    4577           39 :             name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
    4578           38 :           else if (TREE_CODE (pack) == CALL_EXPR)
    4579            3 :             name = DECL_NAME (CALL_EXPR_FN (pack));
    4580              :           else
    4581           35 :             name = DECL_NAME (pack);
    4582              : 
    4583          281 :           if (name)
    4584          281 :             inform (loc, "        %qD", name);
    4585              :           else
    4586            0 :             inform (loc, "        %s", "<anonymous>");
    4587              : 
    4588          281 :           parameter_packs = TREE_CHAIN (parameter_packs);
    4589              :         }
    4590              : 
    4591          281 :       return true;
    4592          281 :     }
    4593              : 
    4594              :   return false;
    4595              : }
    4596              : 
    4597              : /* Expand any parameter packs that occur in the template arguments in
    4598              :    ARGS.  */
    4599              : tree
    4600    929619631 : expand_template_argument_pack (tree args)
    4601              : {
    4602    929619631 :   if (args == error_mark_node)
    4603              :     return error_mark_node;
    4604              : 
    4605    929619608 :   tree result_args = NULL_TREE;
    4606   1859239216 :   int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
    4607    929619608 :   int num_result_args = -1;
    4608    929619608 :   int non_default_args_count = -1;
    4609              : 
    4610              :   /* First, determine if we need to expand anything, and the number of
    4611              :      slots we'll need.  */
    4612   2306421456 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4613              :     {
    4614   1376801897 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4615   1376801897 :       if (arg == NULL_TREE)
    4616              :         return args;
    4617   1376801848 :       if (ARGUMENT_PACK_P (arg))
    4618              :         {
    4619     83163090 :           int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
    4620     83163090 :           if (num_result_args < 0)
    4621     83163090 :             num_result_args = in_arg + num_packed;
    4622              :           else
    4623            0 :             num_result_args += num_packed;
    4624              :         }
    4625              :       else
    4626              :         {
    4627   1293638758 :           if (num_result_args >= 0)
    4628           27 :             num_result_args++;
    4629              :         }
    4630              :     }
    4631              : 
    4632              :   /* If no expansion is necessary, we're done.  */
    4633    929619559 :   if (num_result_args < 0)
    4634              :     return args;
    4635              : 
    4636              :   /* Expand arguments.  */
    4637     83163090 :   result_args = make_tree_vec (num_result_args);
    4638     83163090 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
    4639     80926472 :     non_default_args_count =
    4640     80926472 :       GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
    4641    185810661 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4642              :     {
    4643    102647571 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4644    102647571 :       if (ARGUMENT_PACK_P (arg))
    4645              :         {
    4646     83163090 :           tree packed = ARGUMENT_PACK_ARGS (arg);
    4647     83163090 :           int i, num_packed = TREE_VEC_LENGTH (packed);
    4648    227539182 :           for (i = 0; i < num_packed; ++i, ++out_arg)
    4649    144376092 :             TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
    4650     83163090 :           if (non_default_args_count > 0)
    4651     80926468 :             non_default_args_count += num_packed - 1;
    4652              :         }
    4653              :       else
    4654              :         {
    4655     19484481 :           TREE_VEC_ELT (result_args, out_arg) = arg;
    4656     19484481 :           ++out_arg;
    4657              :         }
    4658              :     }
    4659     83163090 :   if (non_default_args_count >= 0)
    4660     80926472 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
    4661              :   return result_args;
    4662              : }
    4663              : 
    4664              : /* Checks if DECL shadows a template parameter.
    4665              : 
    4666              :    [temp.local]: A template-parameter shall not be redeclared within its
    4667              :    scope (including nested scopes).
    4668              : 
    4669              :    Emits an error and returns TRUE if the DECL shadows a parameter,
    4670              :    returns FALSE otherwise.  */
    4671              : 
    4672              : bool
    4673   1866011922 : check_template_shadow (tree decl)
    4674              : {
    4675   1866011922 :   tree olddecl;
    4676              : 
    4677              :   /* If we're not in a template, we can't possibly shadow a template
    4678              :      parameter.  */
    4679   1866011922 :   if (!current_template_parms)
    4680              :     return true;
    4681              : 
    4682              :   /* Figure out what we're shadowing.  */
    4683    991866732 :   decl = OVL_FIRST (decl);
    4684    991866732 :   olddecl = innermost_non_namespace_value (DECL_NAME (decl));
    4685              : 
    4686              :   /* If there's no previous binding for this name, we're not shadowing
    4687              :      anything, let alone a template parameter.  */
    4688    991866732 :   if (!olddecl)
    4689              :     return true;
    4690              : 
    4691              :   /* If we're not shadowing a template parameter, we're done.  Note
    4692              :      that OLDDECL might be an OVERLOAD (or perhaps even an
    4693              :      ERROR_MARK), so we can't just blithely assume it to be a _DECL
    4694              :      node.  */
    4695     74299609 :   if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
    4696              :     return true;
    4697              : 
    4698              :   /* We check for decl != olddecl to avoid bogus errors for using a
    4699              :      name inside a class.  We check TPFI to avoid duplicate errors for
    4700              :      inline member templates.  */
    4701          162 :   if (decl == olddecl
    4702          162 :       || (DECL_TEMPLATE_PARM_P (decl)
    4703           69 :           && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
    4704              :     return true;
    4705              : 
    4706              :   /* Don't complain about the injected class name, as we've already
    4707              :      complained about the class itself.  */
    4708          129 :   if (DECL_SELF_REFERENCE_P (decl))
    4709              :     return false;
    4710              : 
    4711          120 :   auto_diagnostic_group d;
    4712          120 :   if (DECL_TEMPLATE_PARM_P (decl))
    4713           36 :     error ("declaration of template parameter %q+D shadows "
    4714              :            "template parameter", decl);
    4715              :   else
    4716           84 :     error ("declaration of %q+#D shadows template parameter", decl);
    4717          120 :   inform (DECL_SOURCE_LOCATION (olddecl),
    4718              :           "template parameter %qD declared here", olddecl);
    4719          120 :   return false;
    4720          120 : }
    4721              : 
    4722              : /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
    4723              :    ORIG_LEVEL, DECL, and TYPE.  */
    4724              : 
    4725              : static tree
    4726    226342659 : build_template_parm_index (int index,
    4727              :                            int level,
    4728              :                            int orig_level,
    4729              :                            tree decl,
    4730              :                            tree type)
    4731              : {
    4732    226342659 :   tree t = make_node (TEMPLATE_PARM_INDEX);
    4733    226342659 :   TEMPLATE_PARM_IDX (t) = index;
    4734    226342659 :   TEMPLATE_PARM_LEVEL (t) = level;
    4735    226342659 :   TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
    4736    226342659 :   TEMPLATE_PARM_DECL (t) = decl;
    4737    226342659 :   TREE_TYPE (t) = type;
    4738    226342659 :   TREE_CONSTANT (t) = TREE_CONSTANT (decl);
    4739    226342659 :   TREE_READONLY (t) = TREE_READONLY (decl);
    4740              : 
    4741    226342659 :   return t;
    4742              : }
    4743              : 
    4744              : struct ctp_hasher : ggc_ptr_hash<tree_node>
    4745              : {
    4746   1300435817 :   static hashval_t hash (tree t)
    4747              :   {
    4748   1300435817 :     ++comparing_specializations;
    4749   1300435817 :     tree_code code = TREE_CODE (t);
    4750   1300435817 :     hashval_t val = iterative_hash_object (code, 0);
    4751   1300435817 :     val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
    4752   1300435817 :     val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
    4753   1300435817 :     if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
    4754              :       {
    4755   1100674835 :         val
    4756   1100674835 :           = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val);
    4757   1100674835 :         if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
    4758    347270329 :           val = iterative_hash_placeholder_constraint (c, val);
    4759              :       }
    4760   1300435817 :     if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
    4761    109950908 :       val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
    4762   1300435817 :     --comparing_specializations;
    4763   1300435817 :     return val;
    4764              :   }
    4765              : 
    4766   1647287407 :   static bool equal (tree t, tree u)
    4767              :   {
    4768   1647287407 :     ++comparing_specializations;
    4769   1647287407 :     bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
    4770   1647287407 :     --comparing_specializations;
    4771   1647287407 :     return eq;
    4772              :   }
    4773              : };
    4774              : 
    4775              : static GTY (()) hash_table<ctp_hasher> *ctp_table;
    4776              : 
    4777              : /* Find the canonical type parameter for the given template type
    4778              :    parameter.  Returns the canonical type parameter, which may be TYPE
    4779              :    if no such parameter existed.  */
    4780              : 
    4781              : tree
    4782    214638154 : canonical_type_parameter (tree type)
    4783              : {
    4784    214638154 :   if (ctp_table == NULL)
    4785       101500 :     ctp_table = hash_table<ctp_hasher>::create_ggc (61);
    4786              : 
    4787    214638154 :   tree& slot = *ctp_table->find_slot (type, INSERT);
    4788    214638154 :   if (slot == NULL_TREE)
    4789      2837904 :     slot = type;
    4790    214638154 :   return slot;
    4791              : }
    4792              : 
    4793              : /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
    4794              :    TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
    4795              :    TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
    4796              :    new one is created.  */
    4797              : 
    4798              : static tree
    4799     21492031 : reduce_template_parm_level (tree index, tree type, int levels, tree args,
    4800              :                             tsubst_flags_t complain)
    4801              : {
    4802     21492031 :   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
    4803      5463492 :       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
    4804      5463492 :           != TEMPLATE_PARM_LEVEL (index) - levels)
    4805     26914748 :       || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
    4806              :     {
    4807     16074855 :       tree orig_decl = TEMPLATE_PARM_DECL (index);
    4808              : 
    4809     16074855 :       tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
    4810     16074855 :                               TREE_CODE (orig_decl), DECL_NAME (orig_decl),
    4811              :                               type);
    4812     16074855 :       TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
    4813     16074855 :       TREE_READONLY (decl) = TREE_READONLY (orig_decl);
    4814     16074855 :       DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
    4815     16074855 :       DECL_ARTIFICIAL (decl) = 1;
    4816     16074855 :       SET_DECL_TEMPLATE_PARM_P (decl);
    4817              : 
    4818     16074855 :       tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
    4819     16074855 :                                             TEMPLATE_PARM_LEVEL (index) - levels,
    4820     16074855 :                                             TEMPLATE_PARM_ORIG_LEVEL (index),
    4821              :                                             decl, type);
    4822     16074855 :       TEMPLATE_PARM_DESCENDANTS (index) = tpi;
    4823     32149710 :       TEMPLATE_PARM_PARAMETER_PACK (tpi)
    4824     16074855 :         = TEMPLATE_PARM_PARAMETER_PACK (index);
    4825              : 
    4826              :       /* Template template parameters need this.  */
    4827     16074855 :       tree inner = decl;
    4828     16074855 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
    4829              :         {
    4830         1896 :           inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl),
    4831         1896 :                                        TYPE_DECL, DECL_NAME (decl), type);
    4832         1896 :           DECL_TEMPLATE_RESULT (decl) = inner;
    4833         1896 :           DECL_ARTIFICIAL (inner) = true;
    4834         1896 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl),
    4835              :                                               args, complain);
    4836         1896 :           DECL_TEMPLATE_PARMS (decl) = parms;
    4837         1896 :           tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl);
    4838         1896 :           DECL_TEMPLATE_INFO (inner)
    4839         3792 :             = build_template_info (DECL_TI_TEMPLATE (orig_inner),
    4840              :                                    template_parms_to_args (parms));
    4841              :         }
    4842              : 
    4843              :       /* Attach the TPI to the decl.  */
    4844     16074855 :       if (TREE_CODE (inner) == TYPE_DECL)
    4845     15281181 :         TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
    4846              :       else
    4847       793674 :         DECL_INITIAL (decl) = tpi;
    4848              :     }
    4849              : 
    4850     21492031 :   return TEMPLATE_PARM_DESCENDANTS (index);
    4851              : }
    4852              : 
    4853              : /* Process information from new template parameter PARM and append it
    4854              :    to the LIST being built.  This new parameter is a non-type
    4855              :    parameter iff IS_NON_TYPE is true. This new parameter is a
    4856              :    parameter pack iff IS_PARAMETER_PACK is true.  The location of PARM
    4857              :    is in PARM_LOC.  */
    4858              : 
    4859              : tree
    4860    177740940 : process_template_parm (tree list, location_t parm_loc, tree parm,
    4861              :                        bool is_non_type, bool is_parameter_pack)
    4862              : {
    4863    177740940 :   gcc_assert (TREE_CODE (parm) == TREE_LIST);
    4864    177740940 :   tree prev = NULL_TREE;
    4865    177740940 :   int idx = 0;
    4866              : 
    4867    177740940 :   if (list)
    4868              :     {
    4869     81695392 :       prev = tree_last (list);
    4870              : 
    4871     81695392 :       tree p = TREE_VALUE (prev);
    4872     81695392 :       if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
    4873     75968342 :         idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
    4874      5727050 :       else if (TREE_CODE (p) == PARM_DECL)
    4875      5727030 :         idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
    4876              : 
    4877     81695392 :       ++idx;
    4878              :     }
    4879              : 
    4880    177740940 :   tree decl = NULL_TREE;
    4881    177740940 :   tree defval = TREE_PURPOSE (parm);
    4882    177740940 :   tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
    4883              : 
    4884    177740940 :   if (is_non_type)
    4885              :     {
    4886     11670184 :       parm = TREE_VALUE (parm);
    4887              : 
    4888     11670184 :       SET_DECL_TEMPLATE_PARM_P (parm);
    4889              : 
    4890     11670184 :       if (TREE_TYPE (parm) != error_mark_node)
    4891              :         {
    4892              :           /* [temp.param]
    4893              : 
    4894              :              The top-level cv-qualifiers on the template-parameter are
    4895              :              ignored when determining its type.  */
    4896     11670092 :           TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
    4897     11670092 :           if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
    4898          100 :             TREE_TYPE (parm) = error_mark_node;
    4899     11669992 :           else if (uses_parameter_packs (TREE_TYPE (parm))
    4900           63 :                    && !is_parameter_pack
    4901              :                    /* If we're in a nested template parameter list, the template
    4902              :                       template parameter could be a parameter pack.  */
    4903     11670010 :                    && processing_template_parmlist == 1)
    4904              :             {
    4905              :               /* This template parameter is not a parameter pack, but it
    4906              :                  should be. Complain about "bare" parameter packs.  */
    4907           12 :               check_for_bare_parameter_packs (TREE_TYPE (parm));
    4908              : 
    4909              :               /* Recover by calling this a parameter pack.  */
    4910           12 :               is_parameter_pack = true;
    4911              :             }
    4912              :         }
    4913              : 
    4914              :       /* A template parameter is not modifiable.  */
    4915     11670184 :       TREE_CONSTANT (parm) = 1;
    4916     11670184 :       TREE_READONLY (parm) = 1;
    4917     11670184 :       decl = build_decl (parm_loc,
    4918     11670184 :                          CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
    4919     11670184 :       TREE_CONSTANT (decl) = 1;
    4920     11670184 :       TREE_READONLY (decl) = 1;
    4921     11670184 :       DECL_INITIAL (parm) = DECL_INITIAL (decl)
    4922     23340368 :         = build_template_parm_index (idx, current_template_depth,
    4923     11670184 :                                      current_template_depth,
    4924     11670184 :                                      decl, TREE_TYPE (parm));
    4925              : 
    4926     11670184 :       TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
    4927     11670184 :         = is_parameter_pack;
    4928              :     }
    4929              :   else
    4930              :     {
    4931    166070756 :       tree t;
    4932    166070756 :       parm = TREE_VALUE (TREE_VALUE (parm));
    4933              : 
    4934    166070756 :       if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
    4935              :         {
    4936       421974 :           t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
    4937              :           /* This is for distinguishing between real templates and template
    4938              :              template parameters */
    4939       421974 :           TREE_TYPE (parm) = t;
    4940              : 
    4941              :           /* any_template_parm_r expects to be able to get the targs of a
    4942              :              DECL_TEMPLATE_RESULT.  */
    4943       421974 :           tree result = DECL_TEMPLATE_RESULT (parm);
    4944       421974 :           TREE_TYPE (result) = t;
    4945       421974 :           tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
    4946       421974 :           tree tinfo = build_template_info (parm, args);
    4947       421974 :           retrofit_lang_decl (result);
    4948       421974 :           DECL_TEMPLATE_INFO (result) = tinfo;
    4949              : 
    4950       421974 :           decl = parm;
    4951       421974 :         }
    4952              :       else
    4953              :         {
    4954    165648782 :           t = cxx_make_type (TEMPLATE_TYPE_PARM);
    4955              :           /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
    4956    165648782 :           decl = build_decl (parm_loc,
    4957              :                              TYPE_DECL, parm, t);
    4958              :         }
    4959              : 
    4960    166070756 :       TYPE_NAME (t) = decl;
    4961    166070756 :       TYPE_STUB_DECL (t) = decl;
    4962    166070756 :       parm = decl;
    4963    166070756 :       TEMPLATE_TYPE_PARM_INDEX (t)
    4964    166070756 :         = build_template_parm_index (idx, current_template_depth,
    4965    166070756 :                                      current_template_depth,
    4966    166070756 :                                      decl, TREE_TYPE (parm));
    4967    166070756 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
    4968    166070756 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
    4969              :     }
    4970    177740940 :   DECL_ARTIFICIAL (decl) = 1;
    4971    177740940 :   SET_DECL_TEMPLATE_PARM_P (decl);
    4972              : 
    4973    177740940 :   if (TREE_CODE (parm) == TEMPLATE_DECL
    4974    177740940 :       && !uses_outer_template_parms (parm))
    4975       421941 :     TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true;
    4976              : 
    4977              :   /* Build requirements for the type/template parameter.
    4978              :      This must be done after SET_DECL_TEMPLATE_PARM_P or
    4979              :      process_template_parm could fail. */
    4980    177740940 :   tree reqs = finish_shorthand_constraint (parm, constr, is_non_type);
    4981              : 
    4982    177740940 :   decl = pushdecl (decl);
    4983    177740940 :   if (!is_non_type)
    4984    166070756 :     parm = decl;
    4985              : 
    4986              :   /* Build the parameter node linking the parameter declaration,
    4987              :      its default argument (if any), and its constraints (if any). */
    4988    177740940 :   parm = build_tree_list (defval, parm);
    4989    177740940 :   TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
    4990              : 
    4991    177740940 :   if (prev)
    4992     81695392 :     TREE_CHAIN (prev) = parm;
    4993              :   else
    4994              :     list = parm;
    4995              : 
    4996    177740940 :   return list;
    4997              : }
    4998              : 
    4999              : /* The end of a template parameter list has been reached.  Process the
    5000              :    tree list into a parameter vector, converting each parameter into a more
    5001              :    useful form.  Type parameters are saved as IDENTIFIER_NODEs, and others
    5002              :    as PARM_DECLs.  */
    5003              : 
    5004              : tree
    5005     95679335 : end_template_parm_list (tree parms)
    5006              : {
    5007     95679335 :   tree saved_parmlist = make_tree_vec (list_length (parms));
    5008              : 
    5009              :   /* Pop the dummy parameter level and add the real one.  We do not
    5010              :      morph the dummy parameter in place, as it might have been
    5011              :      captured by a (nested) template-template-parm.  */
    5012     95679335 :   current_template_parms = TREE_CHAIN (current_template_parms);
    5013              : 
    5014    191358670 :   current_template_parms
    5015     95679335 :     = tree_cons (size_int (current_template_depth + 1),
    5016              :                  saved_parmlist, current_template_parms);
    5017              : 
    5018    272879436 :   for (unsigned ix = 0; parms; ix++)
    5019              :     {
    5020    177200101 :       tree parm = parms;
    5021    177200101 :       parms = TREE_CHAIN (parms);
    5022    177200101 :       TREE_CHAIN (parm) = NULL_TREE;
    5023              : 
    5024    177200101 :       TREE_VEC_ELT (saved_parmlist, ix) = parm;
    5025              :     }
    5026              : 
    5027     95679335 :   --processing_template_parmlist;
    5028              : 
    5029     95679335 :   return saved_parmlist;
    5030              : }
    5031              : 
    5032              : // Explicitly indicate the end of the template parameter list. We assume
    5033              : // that the current template parameters have been constructed and/or
    5034              : // managed explicitly, as when creating new template template parameters
    5035              : // from a shorthand constraint.
    5036              : void
    5037            0 : end_template_parm_list ()
    5038              : {
    5039            0 :   --processing_template_parmlist;
    5040            0 : }
    5041              : 
    5042              : /* end_template_decl is called after a template declaration is seen.  */
    5043              : 
    5044              : void
    5045     96045703 : end_template_decl (void)
    5046              : {
    5047     96045703 :   reset_specialization ();
    5048              : 
    5049     96045703 :   if (! processing_template_decl)
    5050              :     return;
    5051              : 
    5052              :   /* This matches the pushlevel in begin_template_parm_list.  */
    5053     96045700 :   finish_scope ();
    5054              : 
    5055     96045700 :   --processing_template_decl;
    5056     96045700 :   current_template_parms = TREE_CHAIN (current_template_parms);
    5057              : }
    5058              : 
    5059              : /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
    5060              :    thereof, and converts it into an argument suitable to be passed to
    5061              :    the type substitution functions.  Note that if the TREE_LIST contains
    5062              :    an error_mark node, the returned argument is error_mark_node.  */
    5063              : 
    5064              : tree
    5065    881541084 : template_parm_to_arg (tree t)
    5066              : {
    5067    881541084 :   if (!t)
    5068              :     return NULL_TREE;
    5069              : 
    5070    881541008 :   if (TREE_CODE (t) == TREE_LIST)
    5071    859993484 :     t = TREE_VALUE (t);
    5072              : 
    5073    881541008 :   if (error_operand_p (t))
    5074          678 :     return error_mark_node;
    5075              : 
    5076    881540330 :   if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
    5077              :     {
    5078    875206944 :       if (TREE_CODE (t) == TYPE_DECL
    5079     66583319 :           || TREE_CODE (t) == TEMPLATE_DECL)
    5080    809789198 :         t = TREE_TYPE (t);
    5081              :       else
    5082     65417746 :         t = DECL_INITIAL (t);
    5083              :     }
    5084              : 
    5085    881540330 :   gcc_assert (TEMPLATE_PARM_P (t));
    5086              : 
    5087    881540330 :   if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
    5088    881540330 :       || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
    5089              :     {
    5090    815961303 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    5091              :         {
    5092              :           /* Turn this argument into a TYPE_ARGUMENT_PACK
    5093              :              with a single element, which expands T.  */
    5094     26936153 :           tree vec = make_tree_vec (1);
    5095     26936153 :           if (CHECKING_P)
    5096     26936153 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    5097              : 
    5098     26936153 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    5099              : 
    5100     26936153 :           t = cxx_make_type (TYPE_ARGUMENT_PACK);
    5101     26936153 :           ARGUMENT_PACK_ARGS (t) = vec;
    5102              :         }
    5103              :     }
    5104              :   else
    5105              :     {
    5106     65579027 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    5107              :         {
    5108              :           /* Turn this argument into a NONTYPE_ARGUMENT_PACK
    5109              :              with a single element, which expands T.  */
    5110      1679618 :           tree vec = make_tree_vec (1);
    5111      1679618 :           if (CHECKING_P)
    5112      1679618 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    5113              : 
    5114      1679618 :           t = convert_from_reference (t);
    5115      1679618 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    5116              : 
    5117      1679618 :           t  = make_node (NONTYPE_ARGUMENT_PACK);
    5118      1679618 :           ARGUMENT_PACK_ARGS (t) = vec;
    5119              :         }
    5120              :       else
    5121     63899409 :         t = convert_from_reference (t);
    5122              :     }
    5123              :   return t;
    5124              : }
    5125              : 
    5126              : /* If T looks like a generic template argument produced by template_parm_to_arg,
    5127              :    return the corresponding template parameter, otherwise return NULL_TREE.  */
    5128              : 
    5129              : static tree
    5130   1928851491 : template_arg_to_parm (tree t)
    5131              : {
    5132   1928851491 :   if (t == NULL_TREE)
    5133              :     return NULL_TREE;
    5134              : 
    5135   1928851491 :   if (ARGUMENT_PACK_P (t))
    5136              :     {
    5137    220990828 :       tree args = ARGUMENT_PACK_ARGS (t);
    5138    220990828 :       if (TREE_VEC_LENGTH (args) == 1
    5139    220990828 :           && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
    5140     95524149 :         t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
    5141              :     }
    5142              : 
    5143   1928851491 :   if (REFERENCE_REF_P (t))
    5144         2704 :     t = TREE_OPERAND (t, 0);
    5145              : 
    5146   1928851491 :   if (TEMPLATE_PARM_P (t))
    5147              :     return t;
    5148              :   else
    5149    505537470 :     return NULL_TREE;
    5150              : }
    5151              : 
    5152              : /* Given a single level of template parameters (a TREE_VEC), return it
    5153              :    as a set of template arguments.  */
    5154              : 
    5155              : tree
    5156    422355278 : template_parms_level_to_args (tree parms)
    5157              : {
    5158    422355278 :   parms = copy_node (parms);
    5159    422355278 :   TREE_TYPE (parms) = NULL_TREE;
    5160   1275899491 :   for (tree& parm : tree_vec_range (parms))
    5161    853544213 :     parm = template_parm_to_arg (parm);
    5162              : 
    5163    422355278 :   if (CHECKING_P)
    5164    422355278 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
    5165              : 
    5166    422355278 :   return parms;
    5167              : }
    5168              : 
    5169              : /* Given a set of template parameters, return them as a set of template
    5170              :    arguments.  The template parameters are represented as a TREE_VEC, in
    5171              :    the form documented in cp-tree.h for template arguments.  */
    5172              : 
    5173              : tree
    5174    341952639 : template_parms_to_args (tree parms)
    5175              : {
    5176    341952639 :   tree header;
    5177    341952639 :   tree args = NULL_TREE;
    5178    341952639 :   int length = TMPL_PARMS_DEPTH (parms);
    5179    341952639 :   int l = length;
    5180              : 
    5181              :   /* If there is only one level of template parameters, we do not
    5182              :      create a TREE_VEC of TREE_VECs.  Instead, we return a single
    5183              :      TREE_VEC containing the arguments.  */
    5184    341952639 :   if (length > 1)
    5185     58484199 :     args = make_tree_vec (length);
    5186              : 
    5187    743394994 :   for (header = parms; header; header = TREE_CHAIN (header))
    5188              :     {
    5189    401442355 :       tree a = template_parms_level_to_args (TREE_VALUE (header));
    5190              : 
    5191    401442355 :       if (length > 1)
    5192    117973915 :         TREE_VEC_ELT (args, --l) = a;
    5193              :       else
    5194              :         args = a;
    5195              :     }
    5196              : 
    5197    341952639 :   return args;
    5198              : }
    5199              : 
    5200              : /* Within the declaration of a template, return the currently active
    5201              :    template parameters as an argument TREE_VEC.  */
    5202              : 
    5203              : static tree
    5204    340898756 : current_template_args (void)
    5205              : {
    5206            0 :   return template_parms_to_args (current_template_parms);
    5207              : }
    5208              : 
    5209              : /* Return the fully generic arguments for of TMPL, i.e. what
    5210              :    current_template_args would be while parsing it.  */
    5211              : 
    5212              : tree
    5213     53878083 : generic_targs_for (tree tmpl)
    5214              : {
    5215     53878083 :   if (tmpl == NULL_TREE)
    5216              :     return NULL_TREE;
    5217     53878083 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
    5218    107756166 :       || DECL_TEMPLATE_SPECIALIZATION (tmpl))
    5219              :     /* DECL_TEMPLATE_RESULT doesn't have the arguments we want.  For a template
    5220              :        template parameter, it has no TEMPLATE_INFO; for a partial
    5221              :        specialization, it has the arguments for the primary template, and we
    5222              :        want the arguments for the partial specialization.  */;
    5223     53747397 :   else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
    5224     53747397 :     if (tree ti = get_template_info (result))
    5225     53747397 :       return TI_ARGS (ti);
    5226       130686 :   return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
    5227              : }
    5228              : 
    5229              : /* Return the template arguments corresponding to the template parameters of
    5230              :    DECL's enclosing scope.  When DECL is a member of a partial specialization,
    5231              :    this returns the arguments for the partial specialization as opposed to those
    5232              :    for the primary template, which is the main difference between this function
    5233              :    and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT.  */
    5234              : 
    5235              : tree
    5236       205131 : outer_template_args (const_tree decl)
    5237              : {
    5238       205131 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    5239         5915 :     decl = DECL_TEMPLATE_RESULT (decl);
    5240       205131 :   tree ti = get_template_info (decl);
    5241       205131 :   if (!ti)
    5242              :     return NULL_TREE;
    5243       205131 :   tree args = TI_ARGS (ti);
    5244       205131 :   if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)))
    5245              :     return args;
    5246       410262 :   if (TMPL_ARGS_DEPTH (args) == 1)
    5247              :     return NULL_TREE;
    5248       199498 :   return strip_innermost_template_args (args, 1);
    5249              : }
    5250              : 
    5251              : /* Update the declared TYPE by doing any lookups which were thought to be
    5252              :    dependent, but are not now that we know the SCOPE of the declarator.  */
    5253              : 
    5254              : tree
    5255    187287315 : maybe_update_decl_type (tree orig_type, tree scope)
    5256              : {
    5257    187287315 :   tree type = orig_type;
    5258              : 
    5259    187287315 :   if (type == NULL_TREE)
    5260              :     return type;
    5261              : 
    5262    178462420 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5263     84792946 :     type = TREE_TYPE (type);
    5264              : 
    5265      8571252 :   if (scope && TYPE_P (scope) && dependent_type_p (scope)
    5266      7159491 :       && dependent_type_p (type)
    5267              :       /* Don't bother building up the args in this case.  */
    5268    182034791 :       && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
    5269              :     {
    5270              :       /* tsubst in the args corresponding to the template parameters,
    5271              :          including auto if present.  Most things will be unchanged, but
    5272              :          make_typename_type and tsubst_qualified_id will resolve
    5273              :          TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
    5274      2570007 :       tree args = current_template_args ();
    5275      2570007 :       tree auto_node = type_uses_auto (type);
    5276      2570007 :       tree pushed;
    5277      2570007 :       if (auto_node)
    5278              :         {
    5279            0 :           tree auto_vec = make_tree_vec (1);
    5280            0 :           TREE_VEC_ELT (auto_vec, 0) = auto_node;
    5281            0 :           args = add_to_template_args (args, auto_vec);
    5282              :         }
    5283      2570007 :       pushed = push_scope (scope);
    5284      2570007 :       type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
    5285      2570007 :       if (pushed)
    5286      2570004 :         pop_scope (scope);
    5287              :     }
    5288              : 
    5289    178462420 :   if (type == error_mark_node)
    5290              :     return orig_type;
    5291              : 
    5292    178461646 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5293              :     {
    5294     84792913 :       if (same_type_p (type, TREE_TYPE (orig_type)))
    5295              :         type = orig_type;
    5296              :       else
    5297        69238 :         type = TYPE_NAME (type);
    5298              :     }
    5299              :   return type;
    5300              : }
    5301              : 
    5302              : /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
    5303              :    template PARMS and constraints, CONSTR.  If MEMBER_TEMPLATE_P is true,
    5304              :    the new  template is a member template. */
    5305              : 
    5306              : static tree
    5307    193499109 : build_template_decl (tree decl, tree parms, bool member_template_p)
    5308              : {
    5309    193499109 :   gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
    5310              : 
    5311    193499109 :   tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
    5312    193499109 :   SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
    5313    193499109 :   DECL_TEMPLATE_PARMS (tmpl) = parms;
    5314    193499109 :   DECL_TEMPLATE_RESULT (tmpl) = decl;
    5315    193499109 :   DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
    5316    193499109 :   TREE_TYPE (tmpl) = TREE_TYPE (decl);
    5317    193499109 :   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    5318    193499109 :   DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
    5319              : 
    5320              :   /* Propagate module information from the decl.  */
    5321    193499109 :   DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
    5322              : 
    5323    193499109 :   return tmpl;
    5324              : }
    5325              : 
    5326              : struct template_parm_data
    5327              : {
    5328              :   /* The level of the template parameters we are currently
    5329              :      processing.  */
    5330              :   int level;
    5331              : 
    5332              :   /* The index of the specialization argument we are currently
    5333              :      processing.  */
    5334              :   int current_arg;
    5335              : 
    5336              :   /* An array whose size is the number of template parameters.  The
    5337              :      elements are nonzero if the parameter has been used in any one
    5338              :      of the arguments processed so far.  */
    5339              :   int* parms;
    5340              : 
    5341              :   /* An array whose size is the number of template arguments.  The
    5342              :      elements are nonzero if the argument makes use of template
    5343              :      parameters of this level.  */
    5344              :   int* arg_uses_template_parms;
    5345              : };
    5346              : 
    5347              : /* Subroutine of push_template_decl used to see if each template
    5348              :    parameter in a partial specialization is used in the explicit
    5349              :    argument list.  If T is of the LEVEL given in DATA (which is
    5350              :    treated as a template_parm_data*), then DATA->PARMS is marked
    5351              :    appropriately.  */
    5352              : 
    5353              : static int
    5354     16270969 : mark_template_parm (tree t, void* data)
    5355              : {
    5356     16270969 :   int level;
    5357     16270969 :   int idx;
    5358     16270969 :   struct template_parm_data* tpd = (struct template_parm_data*) data;
    5359              : 
    5360     16270969 :   template_parm_level_and_index (t, &level, &idx);
    5361              : 
    5362     16270969 :   if (level == tpd->level)
    5363              :     {
    5364     16244671 :       tpd->parms[idx] = 1;
    5365     16244671 :       tpd->arg_uses_template_parms[tpd->current_arg] = 1;
    5366              :     }
    5367              : 
    5368              :   /* In C++17 the type of a non-type argument is a deduced context.  */
    5369     16270969 :   if (cxx_dialect >= cxx17
    5370     16166723 :       && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5371      1895441 :     for_each_template_parm (TREE_TYPE (t),
    5372              :                             &mark_template_parm,
    5373              :                             data,
    5374              :                             NULL,
    5375              :                             /*include_nondeduced_p=*/false);
    5376              : 
    5377              :   /* Return zero so that for_each_template_parm will continue the
    5378              :      traversal of the tree; we want to mark *every* template parm.  */
    5379     16270969 :   return 0;
    5380              : }
    5381              : 
    5382              : /* Process the partial specialization DECL.  */
    5383              : 
    5384              : static tree
    5385      8535691 : process_partial_specialization (tree decl)
    5386              : {
    5387      8535691 :   tree type = TREE_TYPE (decl);
    5388      8535691 :   tree tinfo = get_template_info (decl);
    5389      8535691 :   tree maintmpl = TI_TEMPLATE (tinfo);
    5390      8535691 :   tree specargs = TI_ARGS (tinfo);
    5391      8535691 :   tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
    5392      8535691 :   tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
    5393      8535691 :   tree inner_parms;
    5394      8535691 :   tree inst;
    5395      8535691 :   int nargs = TREE_VEC_LENGTH (inner_args);
    5396      8535691 :   int ntparms;
    5397      8535691 :   int  i;
    5398      8535691 :   struct template_parm_data tpd;
    5399      8535691 :   struct template_parm_data tpd2;
    5400              : 
    5401      8535691 :   gcc_assert (current_template_parms);
    5402              : 
    5403      8535691 :   inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
    5404      8535691 :   ntparms = TREE_VEC_LENGTH (inner_parms);
    5405              : 
    5406              :   /* We check that each of the template parameters given in the
    5407              :      partial specialization is used in the argument list to the
    5408              :      specialization.  For example:
    5409              : 
    5410              :        template <class T> struct S;
    5411              :        template <class T> struct S<T*>;
    5412              : 
    5413              :      The second declaration is OK because `T*' uses the template
    5414              :      parameter T, whereas
    5415              : 
    5416              :        template <class T> struct S<int>;
    5417              : 
    5418              :      is no good.  Even trickier is:
    5419              : 
    5420              :        template <class T>
    5421              :        struct S1
    5422              :        {
    5423              :           template <class U>
    5424              :           struct S2;
    5425              :           template <class U>
    5426              :           struct S2<T>;
    5427              :        };
    5428              : 
    5429              :      The S2<T> declaration is actually invalid; it is a
    5430              :      full-specialization.  Of course,
    5431              : 
    5432              :           template <class U>
    5433              :           struct S2<T (*)(U)>;
    5434              : 
    5435              :      or some such would have been OK.  */
    5436      8535691 :   tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
    5437      8535691 :   tpd.parms = XALLOCAVEC (int, ntparms);
    5438      8535691 :   memset (tpd.parms, 0, sizeof (int) * ntparms);
    5439              : 
    5440      8535691 :   tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5441      8535691 :   memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
    5442     25173775 :   for (i = 0; i < nargs; ++i)
    5443              :     {
    5444     16638084 :       tpd.current_arg = i;
    5445     16638084 :       for_each_template_parm (TREE_VEC_ELT (inner_args, i),
    5446              :                               &mark_template_parm,
    5447              :                               &tpd,
    5448              :                               NULL,
    5449              :                               /*include_nondeduced_p=*/false);
    5450              :     }
    5451              : 
    5452      8535691 :   {
    5453      8535691 :     auto_diagnostic_group d;
    5454      8535691 :     bool did_error_intro = false;
    5455     32349473 :     for (i = 0; i < ntparms; ++i)
    5456     15278091 :       if (tpd.parms[i] == 0)
    5457              :         {
    5458              :           /* One of the template parms was not used in a deduced context in the
    5459              :              specialization.  */
    5460           55 :           if (!did_error_intro)
    5461              :             {
    5462           55 :               error ("template parameters not deducible in "
    5463              :                      "partial specialization:");
    5464           55 :               did_error_intro = true;
    5465              :             }
    5466              : 
    5467          110 :           inform (input_location, "        %qD",
    5468           55 :                   TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
    5469              :         }
    5470              : 
    5471      8535691 :     if (did_error_intro)
    5472           55 :       return error_mark_node;
    5473      8535691 :   }
    5474              : 
    5475              :   /* [temp.class.spec]
    5476              : 
    5477              :      The argument list of the specialization shall not be identical to
    5478              :      the implicit argument list of the primary template.  */
    5479      8535636 :   tree main_args
    5480      8535636 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
    5481      8535636 :   if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
    5482      8535636 :       && (!flag_concepts
    5483       719038 :           || !strictly_subsumes (current_template_constraints (), maintmpl)))
    5484              :     {
    5485           24 :       auto_diagnostic_group d;
    5486           24 :       if (!flag_concepts)
    5487            4 :         error ("partial specialization %q+D does not specialize "
    5488              :                "any template arguments; to define the primary template, "
    5489              :                "remove the template argument list", decl);
    5490              :       else
    5491           20 :         error ("partial specialization %q+D does not specialize any "
    5492              :                "template arguments and is not more constrained than "
    5493              :                "the primary template; to define the primary template, "
    5494              :                "remove the template argument list", decl);
    5495           24 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5496           24 :     }
    5497              : 
    5498              :   /* A partial specialization that replaces multiple parameters of the
    5499              :      primary template with a pack expansion is less specialized for those
    5500              :      parameters.  */
    5501      8535636 :   if (nargs < DECL_NTPARMS (maintmpl))
    5502              :     {
    5503            3 :       auto_diagnostic_group d;
    5504            3 :       error ("partial specialization is not more specialized than the "
    5505              :              "primary template because it replaces multiple parameters "
    5506              :              "with a pack expansion");
    5507            3 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5508              :       /* Avoid crash in process_partial_specialization.  */
    5509            3 :       return decl;
    5510            3 :     }
    5511              : 
    5512      8535633 :   else if (nargs > DECL_NTPARMS (maintmpl))
    5513              :     {
    5514            9 :       auto_diagnostic_group d;
    5515            9 :       error ("too many arguments for partial specialization %qT", type);
    5516            9 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5517              :       /* Avoid crash below.  */
    5518            9 :       return decl;
    5519            9 :     }
    5520              : 
    5521              :   /* If we aren't in a dependent class, we can actually try deduction.  */
    5522      8535624 :   else if (tpd.level == 1
    5523              :            /* FIXME we should be able to handle a partial specialization of a
    5524              :               partial instantiation, but currently we can't (c++/41727).  */
    5525     16176402 :            && TMPL_ARGS_DEPTH (specargs) == 1
    5526     16623819 :            && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
    5527              :     {
    5528           17 :       auto_diagnostic_group d;
    5529           17 :       if (pedwarn (input_location, 0,
    5530              :                    "partial specialization %qD is not more specialized than",
    5531              :                    decl))
    5532           17 :         inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
    5533              :                 maintmpl);
    5534           17 :     }
    5535              : 
    5536              :   /* [temp.spec.partial]
    5537              : 
    5538              :      The type of a template parameter corresponding to a specialized
    5539              :      non-type argument shall not be dependent on a parameter of the
    5540              :      specialization.
    5541              : 
    5542              :      Also, we verify that pack expansions only occur at the
    5543              :      end of the argument list.  */
    5544      8535624 :   tpd2.parms = 0;
    5545     25173607 :   for (i = 0; i < nargs; ++i)
    5546              :     {
    5547     16637983 :       tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
    5548     16637983 :       tree arg = TREE_VEC_ELT (inner_args, i);
    5549     16637983 :       tree packed_args = NULL_TREE;
    5550     16637983 :       int j, len = 1;
    5551              : 
    5552     16637983 :       if (ARGUMENT_PACK_P (arg))
    5553              :         {
    5554              :           /* Extract the arguments from the argument pack. We'll be
    5555              :              iterating over these in the following loop.  */
    5556       815073 :           packed_args = ARGUMENT_PACK_ARGS (arg);
    5557       815073 :           len = TREE_VEC_LENGTH (packed_args);
    5558              :         }
    5559              : 
    5560     33681237 :       for (j = 0; j < len; j++)
    5561              :         {
    5562     17043254 :           if (packed_args)
    5563              :             /* Get the Jth argument in the parameter pack.  */
    5564      1220344 :             arg = TREE_VEC_ELT (packed_args, j);
    5565              : 
    5566     17043254 :           if (PACK_EXPANSION_P (arg))
    5567              :             {
    5568              :               /* Pack expansions must come at the end of the
    5569              :                  argument list.  */
    5570       473905 :               if ((packed_args && j < len - 1)
    5571            6 :                   || (!packed_args && i < nargs - 1))
    5572              :                 {
    5573           12 :                   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5574            6 :                     error ("parameter pack argument %qE must be at the "
    5575              :                            "end of the template argument list", arg);
    5576              :                   else
    5577            6 :                     error ("parameter pack argument %qT must be at the "
    5578              :                            "end of the template argument list", arg);
    5579              :                 }
    5580              :             }
    5581              : 
    5582     17043254 :           if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5583              :             /* We only care about the pattern.  */
    5584        68935 :             arg = PACK_EXPANSION_PATTERN (arg);
    5585              : 
    5586     17043254 :           if (/* These first two lines are the `non-type' bit.  */
    5587     17043254 :               !TYPE_P (arg)
    5588      4486896 :               && TREE_CODE (arg) != TEMPLATE_DECL
    5589              :               /* This next two lines are the `argument expression is not just a
    5590              :                  simple identifier' condition and also the `specialized
    5591              :                  non-type argument' bit.  */
    5592      4223811 :               && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
    5593     20086089 :               && !((REFERENCE_REF_P (arg)
    5594      3042811 :                     || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
    5595          148 :                    && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
    5596              :             {
    5597              :               /* Look at the corresponding template parameter,
    5598              :                  marking which template parameters its type depends
    5599              :                  upon.  */
    5600      3042690 :               tree type = TREE_TYPE (parm);
    5601              : 
    5602      3042690 :               if (!tpd2.parms)
    5603              :                 {
    5604              :                   /* We haven't yet initialized TPD2.  Do so now.  */
    5605      1876312 :                   tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5606              :                   /* The number of parameters here is the number in the
    5607              :                      main template, which, as checked in the assertion
    5608              :                      above, is NARGS.  */
    5609      1876312 :                   tpd2.parms = XALLOCAVEC (int, nargs);
    5610      1876312 :                   tpd2.level =
    5611      1876312 :                     TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
    5612              :                 }
    5613              : 
    5614              :               /* Mark the template parameters.  But this time, we're
    5615              :                  looking for the template parameters of the main
    5616              :                  template, not in the specialization.  */
    5617      3042690 :               tpd2.current_arg = i;
    5618      3042690 :               tpd2.arg_uses_template_parms[i] = 0;
    5619      3042690 :               memset (tpd2.parms, 0, sizeof (int) * nargs);
    5620      3042690 :               for_each_template_parm (type,
    5621              :                                       &mark_template_parm,
    5622              :                                       &tpd2,
    5623              :                                       NULL,
    5624              :                                       /*include_nondeduced_p=*/false);
    5625              : 
    5626      3042690 :               if (tpd2.arg_uses_template_parms [i])
    5627              :                 {
    5628              :                   /* The type depended on some template parameters.
    5629              :                      If they are fully specialized in the
    5630              :                      specialization, that's OK.  */
    5631              :                   int j;
    5632              :                   int count = 0;
    5633           78 :                   for (j = 0; j < nargs; ++j)
    5634           54 :                     if (tpd2.parms[j] != 0
    5635           24 :                         && tpd.arg_uses_template_parms [j])
    5636           18 :                       ++count;
    5637           24 :                   if (count != 0)
    5638           18 :                     error_n (input_location, count,
    5639              :                              "type %qT of template argument %qE depends "
    5640              :                              "on a template parameter",
    5641              :                              "type %qT of template argument %qE depends "
    5642              :                              "on template parameters",
    5643              :                              type,
    5644              :                              arg);
    5645              :                 }
    5646              :             }
    5647              :         }
    5648              :     }
    5649              : 
    5650              :   /* We should only get here once.  */
    5651      8535624 :   if (TREE_CODE (decl) == TYPE_DECL)
    5652      7484259 :     gcc_assert (!COMPLETE_TYPE_P (type));
    5653              : 
    5654              :   // Build the template decl.
    5655      8535624 :   tree tmpl = build_template_decl (decl, current_template_parms,
    5656      8535624 :                                    DECL_MEMBER_TEMPLATE_P (maintmpl));
    5657      8535624 :   SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    5658      8535624 :   DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
    5659      8535624 :   DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
    5660              : 
    5661              :   /* Give template template parms a DECL_CONTEXT of the template
    5662              :      for which they are a parameter.  */
    5663     23813622 :   for (i = 0; i < ntparms; ++i)
    5664              :     {
    5665     15277998 :       tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
    5666     15277998 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    5667       189002 :         DECL_CONTEXT (parm) = tmpl;
    5668              :     }
    5669              : 
    5670      8535624 :   if (VAR_P (decl))
    5671              :     {
    5672              :       /* We didn't register this in check_explicit_specialization so we could
    5673              :          wait until the constraints were set.  */
    5674      1051365 :       tree reg = register_specialization (decl, maintmpl, specargs, false, 0);
    5675      1051365 :       if (reg != decl)
    5676              :         /* Redeclaration.  */
    5677              :         return reg;
    5678              :     }
    5679              :   else
    5680      7484259 :     associate_classtype_constraints (type);
    5681              : 
    5682     17071230 :   DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
    5683      8535615 :     = tree_cons (specargs, tmpl,
    5684      8535615 :                  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
    5685      8535615 :   TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
    5686              :   /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL.  */
    5687      8535615 :   gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
    5688      8535615 :   TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE);
    5689              : 
    5690      8535615 :   set_defining_module_for_partial_spec (decl);
    5691              : 
    5692     65589939 :   for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
    5693     57054324 :        inst = TREE_CHAIN (inst))
    5694              :     {
    5695     57054324 :       tree instance = TREE_VALUE (inst);
    5696     60522544 :       if (TYPE_P (instance)
    5697     57054324 :           ? (COMPLETE_TYPE_P (instance)
    5698     53586104 :              && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
    5699      3468220 :           : DECL_TEMPLATE_INSTANTIATION (instance))
    5700              :         {
    5701       143863 :           tree partial_ti = most_specialized_partial_spec (instance, tf_none,
    5702              :                                                            /*rechecking=*/true);
    5703       143863 :           tree inst_decl = (DECL_P (instance)
    5704       143863 :                             ? instance : TYPE_NAME (instance));
    5705       143863 :           if (!partial_ti)
    5706              :             /* OK */;
    5707        72621 :           else if (partial_ti == error_mark_node)
    5708            3 :             permerror (input_location,
    5709              :                        "declaration of %qD ambiguates earlier template "
    5710              :                        "instantiation for %qD", decl, inst_decl);
    5711        72618 :           else if (TI_TEMPLATE (partial_ti) == tmpl)
    5712            6 :             permerror (input_location,
    5713              :                        "partial specialization of %qD after instantiation "
    5714              :                        "of %qD", decl, inst_decl);
    5715              :         }
    5716              :     }
    5717              : 
    5718              :   return decl;
    5719              : }
    5720              : 
    5721              : /* PARM is a template parameter of some form; return the corresponding
    5722              :    TEMPLATE_PARM_INDEX.  */
    5723              : 
    5724              : static tree
    5725    391561188 : get_template_parm_index (tree parm)
    5726              : {
    5727    391561188 :   if (TREE_CODE (parm) == PARM_DECL
    5728    391561188 :       || TREE_CODE (parm) == CONST_DECL)
    5729    177845729 :     parm = DECL_INITIAL (parm);
    5730    213715459 :   else if (TREE_CODE (parm) == TYPE_DECL
    5731     11600464 :            || TREE_CODE (parm) == TEMPLATE_DECL)
    5732    202119402 :     parm = TREE_TYPE (parm);
    5733    391561188 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    5734    179194428 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
    5735    179194365 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    5736    212428204 :     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
    5737    391561188 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
    5738    391561188 :   return parm;
    5739              : }
    5740              : 
    5741              : /* Subroutine of fixed_parameter_pack_p below.  Look for any template
    5742              :    parameter packs used by the template parameter PARM.  */
    5743              : 
    5744              : static void
    5745         8076 : fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
    5746              : {
    5747              :   /* A type parm can't refer to another parm.  */
    5748         8076 :   if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
    5749              :     return;
    5750         8061 :   else if (TREE_CODE (parm) == PARM_DECL)
    5751              :     {
    5752         8019 :       cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
    5753              :                     ppd, ppd->visited);
    5754         8019 :       return;
    5755              :     }
    5756              : 
    5757           42 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
    5758              : 
    5759           42 :   tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
    5760           93 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    5761              :     {
    5762           51 :       tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
    5763           51 :       if (template_parameter_pack_p (p))
    5764              :         /* Any packs in the type are expanded by this parameter.  */;
    5765              :       else
    5766           33 :         fixed_parameter_pack_p_1 (p, ppd);
    5767              :     }
    5768              : }
    5769              : 
    5770              : /* PARM is a template parameter pack.  Return any parameter packs used in
    5771              :    its type or the type of any of its template parameters.  If there are
    5772              :    any such packs, it will be instantiated into a fixed template parameter
    5773              :    list by partial instantiation rather than be fully deduced.  */
    5774              : 
    5775              : tree
    5776    207816619 : fixed_parameter_pack_p (tree parm)
    5777              : {
    5778              :   /* This can only be true in a member template.  */
    5779    207816619 :   if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
    5780              :     return NULL_TREE;
    5781              :   /* This can only be true for a parameter pack.  */
    5782      3345438 :   if (!template_parameter_pack_p (parm))
    5783              :     return NULL_TREE;
    5784              :   /* A type parm can't refer to another parm.  */
    5785      3345438 :   if (TREE_CODE (parm) == TYPE_DECL)
    5786              :     return NULL_TREE;
    5787              : 
    5788         8043 :   tree parameter_packs = NULL_TREE;
    5789         8043 :   struct find_parameter_pack_data ppd;
    5790         8043 :   ppd.parameter_packs = &parameter_packs;
    5791         8043 :   ppd.visited = new hash_set<tree>;
    5792              : 
    5793         8043 :   fixed_parameter_pack_p_1 (parm, &ppd);
    5794              : 
    5795        16086 :   delete ppd.visited;
    5796         8043 :   return parameter_packs;
    5797              : }
    5798              : 
    5799              : /* Check that a template declaration's use of default arguments and
    5800              :    parameter packs is not invalid.  Here, PARMS are the template
    5801              :    parameters.  IS_PRIMARY is true if DECL is the thing declared by
    5802              :    a primary template.  IS_PARTIAL is true if DECL is a partial
    5803              :    specialization.
    5804              : 
    5805              :    IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
    5806              :    function template declaration or a friend class template
    5807              :    declaration.  In the function case, 1 indicates a declaration, 2
    5808              :    indicates a redeclaration.  When IS_FRIEND_DECL=2, no errors are
    5809              :    emitted for extraneous default arguments.
    5810              : 
    5811              :    Returns TRUE if there were no errors found, FALSE otherwise. */
    5812              : 
    5813              : bool
    5814    266350347 : check_default_tmpl_args (tree decl, tree parms, bool is_primary,
    5815              :                          bool is_partial, int is_friend_decl)
    5816              : {
    5817    266350347 :   const char *msg;
    5818    266350347 :   int last_level_to_check;
    5819    266350347 :   tree parm_level;
    5820    266350347 :   bool no_errors = true;
    5821              : 
    5822              :   /* [temp.param]
    5823              : 
    5824              :      A default template-argument shall not be specified in a
    5825              :      function template declaration or a function template definition, nor
    5826              :      in the template-parameter-list of the definition of a member of a
    5827              :      class template.  */
    5828              : 
    5829    266350347 :   if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
    5830    266350347 :       || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
    5831              :     /* You can't have a function template declaration in a local
    5832              :        scope, nor you can you define a member of a class template in a
    5833              :        local scope.  */
    5834              :     return true;
    5835              : 
    5836     67174900 :   if ((DECL_IMPLICIT_TYPEDEF_P (decl)
    5837     41224601 :        && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5838    331982381 :       || (TREE_CODE (decl) == FUNCTION_DECL
    5839    202103377 :           && LAMBDA_FUNCTION_P (decl)))
    5840              :     /* A lambda doesn't have an explicit declaration; don't complain
    5841              :        about the parms of the enclosing class.  */
    5842              :     return true;
    5843              : 
    5844    261769264 :   if (current_class_type
    5845    205493918 :       && !TYPE_BEING_DEFINED (current_class_type)
    5846     73842115 :       && DECL_LANG_SPECIFIC (decl)
    5847     73737384 :       && DECL_DECLARES_FUNCTION_P (decl)
    5848              :       /* If this is either a friend defined in the scope of the class
    5849              :          or a member function.  */
    5850    152266240 :       && (DECL_FUNCTION_MEMBER_P (decl)
    5851     73407612 :           ? same_type_p (DECL_CONTEXT (decl), current_class_type)
    5852            0 :           : DECL_FRIEND_CONTEXT (decl)
    5853            0 :           ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
    5854              :           : false)
    5855              :       /* And, if it was a member function, it really was defined in
    5856              :          the scope of the class.  */
    5857    408584488 :       && (!DECL_FUNCTION_MEMBER_P (decl)
    5858     73407612 :           || DECL_INITIALIZED_IN_CLASS_P (decl)))
    5859              :     /* We already checked these parameters when the template was
    5860              :        declared, so there's no need to do it again now.  This function
    5861              :        was defined in class scope, but we're processing its body now
    5862              :        that the class is complete.  */
    5863              :     return true;
    5864              : 
    5865              :   /* Core issue 226 (C++0x only): the following only applies to class
    5866              :      templates.  */
    5867    196120853 :   if (is_primary
    5868     81747458 :       && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
    5869              :     {
    5870              :       /* [temp.param]
    5871              : 
    5872              :          If a template-parameter has a default template-argument, all
    5873              :          subsequent template-parameters shall have a default
    5874              :          template-argument supplied.  */
    5875     66066686 :       for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
    5876              :         {
    5877     34595728 :           tree inner_parms = TREE_VALUE (parm_level);
    5878     34595728 :           int ntparms = TREE_VEC_LENGTH (inner_parms);
    5879     34595728 :           int seen_def_arg_p = 0;
    5880     34595728 :           int i;
    5881              : 
    5882     92247292 :           for (i = 0; i < ntparms; ++i)
    5883              :             {
    5884     57651564 :               tree parm = TREE_VEC_ELT (inner_parms, i);
    5885              : 
    5886     57651564 :               if (parm == error_mark_node)
    5887            0 :                 continue;
    5888              : 
    5889     57651564 :               if (TREE_PURPOSE (parm))
    5890              :                 seen_def_arg_p = 1;
    5891     52888064 :               else if (seen_def_arg_p
    5892     52888064 :                        && !template_parameter_pack_p (TREE_VALUE (parm)))
    5893              :                 {
    5894           21 :                   error ("no default argument for %qD", TREE_VALUE (parm));
    5895              :                   /* For better subsequent error-recovery, we indicate that
    5896              :                      there should have been a default argument.  */
    5897           21 :                   TREE_PURPOSE (parm) = error_mark_node;
    5898           21 :                   no_errors = false;
    5899              :                 }
    5900     52888043 :               else if (!is_partial
    5901     52888043 :                        && !is_friend_decl
    5902              :                        /* Don't complain about an enclosing partial
    5903              :                           specialization.  */
    5904     34908737 :                        && parm_level == parms
    5905     31679549 :                        && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
    5906     27053413 :                        && i < ntparms - 1
    5907     10639166 :                        && template_parameter_pack_p (TREE_VALUE (parm))
    5908              :                        /* A fixed parameter pack will be partially
    5909              :                           instantiated into a fixed length list.  */
    5910     52888082 :                        && !fixed_parameter_pack_p (TREE_VALUE (parm)))
    5911              :                 {
    5912              :                   /* A primary class template, primary variable template
    5913              :                      (DR 2032), or alias template can only have one
    5914              :                      parameter pack, at the end of the template
    5915              :                      parameter list.  */
    5916              : 
    5917           24 :                   error ("parameter pack %q+D must be at the end of the"
    5918           24 :                          " template parameter list", TREE_VALUE (parm));
    5919              : 
    5920           24 :                   TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
    5921           24 :                     = error_mark_node;
    5922           24 :                   no_errors = false;
    5923              :                 }
    5924              :             }
    5925              :         }
    5926              :     }
    5927              : 
    5928    196120853 :   if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
    5929              :       || is_partial
    5930    195633530 :       || !is_primary
    5931     73053303 :       || is_friend_decl)
    5932              :     /* For an ordinary class template, default template arguments are
    5933              :        allowed at the innermost level, e.g.:
    5934              :          template <class T = int>
    5935              :          struct S {};
    5936              :        but, in a partial specialization, they're not allowed even
    5937              :        there, as we have in [temp.class.spec]:
    5938              : 
    5939              :          The template parameter list of a specialization shall not
    5940              :          contain default template argument values.
    5941              : 
    5942              :        So, for a partial specialization, or for a function template
    5943              :        (in C++98/C++03), we look at all of them.  */
    5944              :     ;
    5945              :   else
    5946              :     /* But, for a primary class template that is not a partial
    5947              :        specialization we look at all template parameters except the
    5948              :        innermost ones.  */
    5949     71104498 :     parms = TREE_CHAIN (parms);
    5950              : 
    5951              :   /* Figure out what error message to issue.  */
    5952    196120853 :   if (is_friend_decl == 2)
    5953              :     msg = G_("default template arguments may not be used in function template "
    5954              :              "friend re-declaration");
    5955    195908149 :   else if (is_friend_decl)
    5956              :     msg = G_("default template arguments may not be used in template "
    5957              :              "friend declarations");
    5958    194172048 :   else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
    5959              :     msg = G_("default template arguments may not be used in function templates "
    5960              :              "without %<-std=c++11%> or %<-std=gnu++11%>");
    5961    193699204 :   else if (is_partial)
    5962              :     msg = G_("default template arguments may not be used in "
    5963              :              "partial specializations");
    5964    185163477 :   else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
    5965              :     msg = G_("default argument for template parameter for class enclosing %qD");
    5966              :   else
    5967              :     /* Per [temp.param]/9, "A default template-argument shall not be
    5968              :        specified in the template-parameter-lists of the definition of
    5969              :        a member of a class template that appears outside of the member's
    5970              :        class.", thus if we aren't handling a member of a class template
    5971              :        there is no need to examine the parameters.  */
    5972              :     return true;
    5973              : 
    5974    109302214 :   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
    5975              :     /* If we're inside a class definition, there's no need to
    5976              :        examine the parameters to the class itself.  On the one
    5977              :        hand, they will be checked when the class is defined, and,
    5978              :        on the other, default arguments are valid in things like:
    5979              :          template <class T = double>
    5980              :          struct S { template <class U> void f(U); };
    5981              :        Here the default argument for `S' has no bearing on the
    5982              :        declaration of `f'.  */
    5983     93378179 :     last_level_to_check = template_class_depth (current_class_type) + 1;
    5984              :   else
    5985              :     /* Check everything.  */
    5986              :     last_level_to_check = 0;
    5987              : 
    5988    109302214 :   for (parm_level = parms;
    5989    243260884 :        parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
    5990     20484972 :        parm_level = TREE_CHAIN (parm_level))
    5991              :     {
    5992     20484975 :       tree inner_parms = TREE_VALUE (parm_level);
    5993     20484975 :       int i;
    5994     20484975 :       int ntparms;
    5995              : 
    5996     20484975 :       ntparms = TREE_VEC_LENGTH (inner_parms);
    5997     62750958 :       for (i = 0; i < ntparms; ++i)
    5998              :         {
    5999     42265986 :           if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
    6000            0 :             continue;
    6001              : 
    6002     42265986 :           if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
    6003              :             {
    6004           30 :               if (msg)
    6005              :                 {
    6006           30 :                   no_errors = false;
    6007           30 :                   if (is_friend_decl == 2)
    6008              :                     return no_errors;
    6009              : 
    6010           27 :                   error (msg, decl);
    6011           27 :                   msg = 0;
    6012              :                 }
    6013              : 
    6014              :               /* Clear out the default argument so that we are not
    6015              :                  confused later.  */
    6016           27 :               TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
    6017              :             }
    6018              :         }
    6019              : 
    6020              :       /* At this point, if we're still interested in issuing messages,
    6021              :          they must apply to classes surrounding the object declared.  */
    6022     20484972 :       if (msg)
    6023     20484944 :         msg = G_("default argument for template parameter for class "
    6024              :                  "enclosing %qD");
    6025              :     }
    6026              : 
    6027              :   return no_errors;
    6028              : }
    6029              : 
    6030              : /* Worker for push_template_decl_real, called via
    6031              :    for_each_template_parm.  DATA is really an int, indicating the
    6032              :    level of the parameters we are interested in.  If T is a template
    6033              :    parameter of that level, return nonzero.  */
    6034              : 
    6035              : static int
    6036      2350487 : template_parm_this_level_p (tree t, void* data)
    6037              : {
    6038      2350487 :   int this_level = *(int *)data;
    6039      2350487 :   int level;
    6040              : 
    6041      2350487 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    6042       446789 :     level = TEMPLATE_PARM_LEVEL (t);
    6043              :   else
    6044      1903698 :     level = TEMPLATE_TYPE_LEVEL (t);
    6045      2350487 :   return level == this_level;
    6046              : }
    6047              : 
    6048              : /* Worker for uses_outer_template_parms, called via for_each_template_parm.
    6049              :    DATA is really an int, indicating the innermost outer level of parameters.
    6050              :    If T is a template parameter of that level or further out, return
    6051              :    nonzero.  */
    6052              : 
    6053              : static int
    6054     12190729 : template_parm_outer_level (tree t, void *data)
    6055              : {
    6056     12190729 :   int this_level = *(int *)data;
    6057     12190729 :   int level;
    6058              : 
    6059     12190729 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    6060       293474 :     level = TEMPLATE_PARM_LEVEL (t);
    6061              :   else
    6062     11897255 :     level = TEMPLATE_TYPE_LEVEL (t);
    6063     12190729 :   return level <= this_level;
    6064              : }
    6065              : 
    6066              : /* Creates a TEMPLATE_DECL for the indicated DECL using the template
    6067              :    parameters given by current_template_args, or reuses a
    6068              :    previously existing one, if appropriate.  Returns the DECL, or an
    6069              :    equivalent one, if it is replaced via a call to duplicate_decls.
    6070              : 
    6071              :    If IS_FRIEND is true, DECL is a friend declaration.  */
    6072              : 
    6073              : tree
    6074    345914865 : push_template_decl (tree decl, bool is_friend)
    6075              : {
    6076    345914865 :   if (decl == error_mark_node || !current_template_parms)
    6077              :     return error_mark_node;
    6078              : 
    6079              :   /* See if this is a partial specialization.  */
    6080     80759308 :   bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
    6081     22247001 :                       && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
    6082     21888394 :                       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
    6083    419189785 :                      || (VAR_P (decl)
    6084     66057711 :                          && DECL_LANG_SPECIFIC (decl)
    6085      7632407 :                          && DECL_TEMPLATE_SPECIALIZATION (decl)
    6086      1051380 :                          && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
    6087              : 
    6088              :   /* No surprising friend functions.  */
    6089    345914836 :   gcc_checking_assert (is_friend
    6090              :                        || !(TREE_CODE (decl) == FUNCTION_DECL
    6091              :                             && DECL_UNIQUE_FRIEND_P (decl)));
    6092              : 
    6093    335861619 :   tree ctx;
    6094    335861619 :   if (is_friend)
    6095              :     /* For a friend, we want the context of the friend, not
    6096              :        the type of which it is a friend.  */
    6097     10053217 :     ctx = CP_DECL_CONTEXT (decl);
    6098    335861619 :   else if (CP_DECL_CONTEXT (decl)
    6099    335861619 :            && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
    6100              :     /* In the case of a virtual function, we want the class in which
    6101              :        it is defined.  */
    6102    280151592 :     ctx = CP_DECL_CONTEXT (decl);
    6103              :   else
    6104              :     /* Otherwise, if we're currently defining some class, the DECL
    6105              :        is assumed to be a member of the class.  */
    6106     55710027 :     ctx = current_scope ();
    6107              : 
    6108    345914836 :   if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
    6109     65692165 :     ctx = NULL_TREE;
    6110              : 
    6111    345914836 :   if (!DECL_CONTEXT (decl))
    6112          133 :     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
    6113              : 
    6114              :   /* See if this is a primary template.  */
    6115    345914836 :   bool is_primary = false;
    6116    345914836 :   if (is_friend && ctx
    6117    345914836 :       && uses_template_parms_level (ctx, current_template_depth))
    6118              :     /* A friend template that specifies a class context, i.e.
    6119              :          template <typename T> friend void A<T>::f();
    6120              :        is not primary.  */
    6121              :     ;
    6122    367625771 :   else if (DECL_IMPLICIT_TYPEDEF_P (decl) && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    6123              :     /* Lambdas are not primary.  */
    6124              :     ;
    6125              :   else
    6126    344640961 :     is_primary = template_parm_scope_p ();
    6127              : 
    6128              :   /* True if the template is a member template, in the sense of
    6129              :      [temp.mem].  */
    6130    344640961 :   bool member_template_p = false;
    6131              : 
    6132    344640961 :   if (is_primary)
    6133              :     {
    6134     84268684 :       warning (OPT_Wtemplates, "template %qD declared", decl);
    6135              : 
    6136     84268684 :       if (DECL_CLASS_SCOPE_P (decl))
    6137              :         member_template_p = true;
    6138              : 
    6139     84268684 :       if (TREE_CODE (decl) == TYPE_DECL
    6140     84268684 :           && IDENTIFIER_ANON_P (DECL_NAME (decl)))
    6141              :         {
    6142            6 :           error ("template class without a name");
    6143            6 :           return error_mark_node;
    6144              :         }
    6145     84268678 :       else if (TREE_CODE (decl) == FUNCTION_DECL)
    6146              :         {
    6147     53377747 :           if (member_template_p)
    6148              :             {
    6149     21739141 :               if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
    6150            6 :                 error ("member template %qD may not have virt-specifiers", decl);
    6151              :             }
    6152    106755494 :           if (DECL_DESTRUCTOR_P (decl))
    6153              :             {
    6154              :               /* [temp.mem]
    6155              : 
    6156              :                  A destructor shall not be a member template.  */
    6157            6 :               error_at (DECL_SOURCE_LOCATION (decl),
    6158              :                         "destructor %qD declared as member template", decl);
    6159            6 :               return error_mark_node;
    6160              :             }
    6161    106755482 :           if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
    6162     53381591 :               && (!prototype_p (TREE_TYPE (decl))
    6163         3850 :                   || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
    6164         3850 :                   || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    6165         3850 :                   || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    6166              :                       == void_list_node)))
    6167              :             {
    6168              :               /* [basic.stc.dynamic.allocation]
    6169              : 
    6170              :                  An allocation function can be a function
    6171              :                  template. ... Template allocation functions shall
    6172              :                  have two or more parameters.  */
    6173            9 :               error ("invalid template declaration of %qD", decl);
    6174            9 :               return error_mark_node;
    6175              :             }
    6176              :         }
    6177     24130701 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6178     50160102 :                && CLASS_TYPE_P (TREE_TYPE (decl)))
    6179              :         /* Class template.  */;
    6180     11621766 :       else if (TREE_CODE (decl) == TYPE_DECL
    6181     11621766 :                && TYPE_DECL_ALIAS_P (decl))
    6182              :         /* alias-declaration */
    6183      4861527 :         gcc_assert (!DECL_ARTIFICIAL (decl));
    6184      6760239 :       else if (VAR_P (decl))
    6185              :         /* C++14 variable template. */;
    6186      2888817 :       else if (TREE_CODE (decl) == CONCEPT_DECL)
    6187              :         /* C++20 concept definitions.  */;
    6188              :       else
    6189              :         {
    6190            9 :           error ("template declaration of %q#D", decl);
    6191            9 :           return error_mark_node;
    6192              :         }
    6193              :     }
    6194              : 
    6195     80759293 :   bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
    6196    404427110 :                   && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
    6197    251785793 :                       || (VAR_OR_FUNCTION_DECL_P (decl)
    6198    203019478 :                           && DECL_LOCAL_DECL_P (decl))));
    6199              : 
    6200              :   /* Check to see that the rules regarding the use of default
    6201              :      arguments are not being violated.  We check args for a friend
    6202              :      functions when we know whether it's a definition, introducing
    6203              :      declaration or re-declaration.  */
    6204    274032782 :   if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
    6205    264492654 :     check_default_tmpl_args (decl, current_template_parms,
    6206              :                              is_primary, is_partial, is_friend);
    6207              : 
    6208              :   /* Ensure that there are no parameter packs in the type of this
    6209              :      declaration that have not been expanded.  */
    6210    345914806 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    6211              :     {
    6212              :       /* Check each of the arguments individually to see if there are
    6213              :          any bare parameter packs.  */
    6214    196208994 :       tree type = TREE_TYPE (decl);
    6215    196208994 :       tree arg = DECL_ARGUMENTS (decl);
    6216    196208994 :       tree argtype = TYPE_ARG_TYPES (type);
    6217              : 
    6218    622564464 :       while (arg && argtype)
    6219              :         {
    6220    426355470 :           if (!DECL_PACK_P (arg)
    6221    847672919 :               && check_for_bare_parameter_packs (TREE_TYPE (arg)))
    6222              :             {
    6223              :             /* This is a PARM_DECL that contains unexpanded parameter
    6224              :                packs. We have already complained about this in the
    6225              :                check_for_bare_parameter_packs call, so just replace
    6226              :                these types with ERROR_MARK_NODE.  */
    6227           42 :               TREE_TYPE (arg) = error_mark_node;
    6228           42 :               TREE_VALUE (argtype) = error_mark_node;
    6229              :             }
    6230              : 
    6231    426355470 :           arg = DECL_CHAIN (arg);
    6232    426355470 :           argtype = TREE_CHAIN (argtype);
    6233              :         }
    6234              : 
    6235              :       /* Check for bare parameter packs in the return type and the
    6236              :          exception specifiers.  */
    6237    196208994 :       if (check_for_bare_parameter_packs (TREE_TYPE (type)))
    6238              :         /* Errors were already issued, set return type to int
    6239              :            as the frontend doesn't expect error_mark_node as
    6240              :            the return type.  */
    6241            3 :         TREE_TYPE (type) = integer_type_node;
    6242    196208994 :       if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
    6243            0 :         TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
    6244              :     }
    6245              :   else
    6246              :     {
    6247    299411624 :       if (check_for_bare_parameter_packs (is_typedef_decl (decl)
    6248              :                                           ? DECL_ORIGINAL_TYPE (decl)
    6249     91193587 :                                           : TREE_TYPE (decl)))
    6250              :         {
    6251           64 :           TREE_TYPE (decl) = error_mark_node;
    6252           64 :           return error_mark_node;
    6253              :         }
    6254              : 
    6255      8535694 :       if (is_partial && VAR_P (decl)
    6256    150757116 :           && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
    6257            3 :         return error_mark_node;
    6258              :     }
    6259              : 
    6260    345914739 :   if (is_partial)
    6261      8535691 :     return process_partial_specialization (decl);
    6262              : 
    6263    337379048 :   tree args = current_template_args ();
    6264    337379048 :   tree tmpl = NULL_TREE;
    6265    337379048 :   bool new_template_p = false;
    6266    337379048 :   if (local_p)
    6267              :     {
    6268              :       /* Does not get a template head.  */
    6269     71882015 :       tmpl = NULL_TREE;
    6270     71882015 :       gcc_checking_assert (!is_primary);
    6271              :     }
    6272    265497033 :   else if (!ctx
    6273    207687468 :            || TREE_CODE (ctx) == FUNCTION_DECL
    6274    206315898 :            || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
    6275     73842367 :            || (DECL_IMPLICIT_TYPEDEF_P (decl)
    6276       210408 :                && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    6277    339339184 :            || (is_friend && !(DECL_LANG_SPECIFIC (decl)
    6278           84 :                               && DECL_TEMPLATE_INFO (decl))))
    6279              :     {
    6280    191654969 :       if (DECL_LANG_SPECIFIC (decl)
    6281    173492163 :           && DECL_TEMPLATE_INFO (decl)
    6282    198533604 :           && DECL_TI_TEMPLATE (decl))
    6283      6878635 :         tmpl = DECL_TI_TEMPLATE (decl);
    6284              :       /* If DECL is a TYPE_DECL for a class-template, then there won't
    6285              :          be DECL_LANG_SPECIFIC.  The information equivalent to
    6286              :          DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
    6287     60535134 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6288     14657645 :                && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
    6289    184842088 :                && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
    6290              :         {
    6291              :           /* Since a template declaration already existed for this
    6292              :              class-type, we must be redeclaring it here.  Make sure
    6293              :              that the redeclaration is valid.  */
    6294        32877 :           redeclare_class_template (TREE_TYPE (decl),
    6295              :                                     current_template_parms,
    6296              :                                     current_template_constraints ());
    6297              :           /* We don't need to create a new TEMPLATE_DECL; just use the
    6298              :              one we already had.  */
    6299        65754 :           tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
    6300              :         }
    6301              :       else
    6302              :         {
    6303    184743457 :           tmpl = build_template_decl (decl, current_template_parms,
    6304              :                                       member_template_p);
    6305    184743457 :           new_template_p = true;
    6306              : 
    6307    184743457 :           if (DECL_LANG_SPECIFIC (decl)
    6308    184743457 :               && DECL_TEMPLATE_SPECIALIZATION (decl))
    6309              :             {
    6310              :               /* A specialization of a member template of a template
    6311              :                  class.  */
    6312            0 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    6313            0 :               DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
    6314            0 :               DECL_TEMPLATE_INFO (decl) = NULL_TREE;
    6315              :             }
    6316              :         }
    6317              :     }
    6318              :   else
    6319              :     {
    6320     73842064 :       tree a, t, current, parms;
    6321     73842064 :       int i;
    6322     73842064 :       tree tinfo = get_template_info (decl);
    6323              : 
    6324     73842064 :       if (!tinfo)
    6325              :         {
    6326           15 :           error ("template definition of non-template %q#D", decl);
    6327           15 :           return error_mark_node;
    6328              :         }
    6329              : 
    6330     73842049 :       tmpl = TI_TEMPLATE (tinfo);
    6331              : 
    6332     73842049 :       if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    6333     73407612 :           && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
    6334     73407612 :           && DECL_TEMPLATE_SPECIALIZATION (decl)
    6335     73842067 :           && DECL_MEMBER_TEMPLATE_P (tmpl))
    6336              :         {
    6337              :           /* The declaration is a specialization of a member
    6338              :              template, declared outside the class.  Therefore, the
    6339              :              innermost template arguments will be NULL, so we
    6340              :              replace them with the arguments determined by the
    6341              :              earlier call to check_explicit_specialization.  */
    6342           12 :           args = DECL_TI_ARGS (decl);
    6343              : 
    6344           12 :           tree new_tmpl
    6345           12 :             = build_template_decl (decl, current_template_parms,
    6346              :                                    member_template_p);
    6347           12 :           DECL_TI_TEMPLATE (decl) = new_tmpl;
    6348           12 :           SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
    6349           12 :           DECL_TEMPLATE_INFO (new_tmpl)
    6350           12 :             = build_template_info (tmpl, args);
    6351              : 
    6352           12 :           register_specialization (new_tmpl,
    6353              :                                    most_general_template (tmpl),
    6354              :                                    args,
    6355              :                                    is_friend, 0);
    6356           12 :           return decl;
    6357              :         }
    6358              : 
    6359              :       /* Make sure the template headers we got make sense.  */
    6360              : 
    6361     73842037 :       parms = DECL_TEMPLATE_PARMS (tmpl);
    6362     73842037 :       i = TMPL_PARMS_DEPTH (parms);
    6363    205894317 :       if (TMPL_ARGS_DEPTH (args) != i)
    6364              :         {
    6365           12 :           error ("expected %d levels of template parms for %q#D, got %d",
    6366            6 :                  i, decl, TMPL_ARGS_DEPTH (args));
    6367            6 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6368            6 :           return error_mark_node;
    6369              :         }
    6370              :       else
    6371    163401263 :         for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
    6372              :           {
    6373    179118494 :             a = TMPL_ARGS_LEVEL (args, i);
    6374     89559247 :             t = INNERMOST_TEMPLATE_PARMS (parms);
    6375              : 
    6376     89559247 :             if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
    6377              :               {
    6378           15 :                 if (current == decl)
    6379           15 :                   error ("got %d template parameters for %q#D",
    6380           15 :                          TREE_VEC_LENGTH (a), decl);
    6381              :                 else
    6382            0 :                   error ("got %d template parameters for %q#T",
    6383            0 :                          TREE_VEC_LENGTH (a), current);
    6384           15 :                 error ("  but %d required", TREE_VEC_LENGTH (t));
    6385              :                 /* Avoid crash in import_export_decl.  */
    6386           15 :                 DECL_INTERFACE_KNOWN (decl) = 1;
    6387           15 :                 return error_mark_node;
    6388              :               }
    6389              : 
    6390     89559232 :             if (current == decl)
    6391              :               current = ctx;
    6392     15717216 :             else if (current == NULL_TREE)
    6393              :               /* Can happen in erroneous input.  */
    6394              :               break;
    6395              :             else
    6396     15717216 :               current = get_containing_scope (current);
    6397              :           }
    6398              : 
    6399              :       /* Check that the parms are used in the appropriate qualifying scopes
    6400              :          in the declarator.  */
    6401    147684032 :       if (!comp_template_args
    6402     73842016 :           (TI_ARGS (tinfo),
    6403     73842016 :            TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
    6404              :         {
    6405            3 :           auto_diagnostic_group d;
    6406            3 :           error ("template arguments to %qD do not match original "
    6407            3 :                  "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
    6408            3 :           if (!uses_template_parms (TI_ARGS (tinfo)))
    6409            3 :             inform (input_location, "use %<template<>%> for"
    6410              :                     " an explicit specialization");
    6411              :           /* Avoid crash in import_export_decl.  */
    6412            3 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6413            3 :           return error_mark_node;
    6414            3 :         }
    6415              : 
    6416              :       /* Check that the constraints for each enclosing template scope are
    6417              :          consistent with the original declarations.  */
    6418     73842013 :       if (flag_concepts)
    6419              :         {
    6420     72909554 :           tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
    6421     72909554 :           tree scope_parms = current_template_parms;
    6422     72909554 :           if (PRIMARY_TEMPLATE_P (tmpl))
    6423              :             {
    6424     19730834 :               decl_parms = TREE_CHAIN (decl_parms);
    6425     19730834 :               scope_parms = TREE_CHAIN (scope_parms);
    6426              :             }
    6427    141666702 :           while (decl_parms)
    6428              :             {
    6429     68757166 :               if (!template_requirements_equivalent_p (decl_parms, scope_parms))
    6430              :                 {
    6431           18 :                   error ("redeclaration of %qD with different constraints",
    6432           18 :                          TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
    6433           18 :                   break;
    6434              :                 }
    6435     68757148 :               decl_parms = TREE_CHAIN (decl_parms);
    6436     68757148 :               scope_parms = TREE_CHAIN (scope_parms);
    6437              :             }
    6438              :         }
    6439              :     }
    6440              : 
    6441    530993964 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6442              : 
    6443    337378997 :   if (new_template_p)
    6444              :     {
    6445              :       /* Push template declarations for global functions and types.
    6446              :          Note that we do not try to push a global template friend
    6447              :          declared in a template class; such a thing may well depend on
    6448              :          the template parameters of the class and we'll push it when
    6449              :          instantiating the befriending class.  */
    6450    184743457 :       if (!ctx
    6451    184743457 :           && !(is_friend && template_class_depth (current_class_type) > 0))
    6452              :         {
    6453     48552474 :           tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
    6454     48552474 :           if (pushed == error_mark_node)
    6455              :             return error_mark_node;
    6456              : 
    6457              :           /* pushdecl may have found an existing template.  */
    6458     48552417 :           if (pushed != tmpl)
    6459              :             {
    6460      3195063 :               decl = DECL_TEMPLATE_RESULT (pushed);
    6461      3195063 :               tmpl = NULL_TREE;
    6462              :             }
    6463              :         }
    6464    136190983 :       else if (is_friend)
    6465              :         {
    6466              :           /* Record this decl as belonging to the current class.  It's
    6467              :              not chained onto anything else.  */
    6468      3754156 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
    6469      3754156 :           gcc_checking_assert (!DECL_CHAIN (tmpl));
    6470      3754156 :           DECL_CHAIN (tmpl) = current_scope ();
    6471              :         }
    6472              :     }
    6473    152635540 :   else if (tmpl)
    6474              :     /* The type may have been completed, or (erroneously) changed.  */
    6475     80753525 :     TREE_TYPE (tmpl) = TREE_TYPE (decl);
    6476              : 
    6477    265496925 :   if (tmpl)
    6478              :     {
    6479    262301862 :       if (is_primary)
    6480              :         {
    6481     72537852 :           tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6482              : 
    6483     72537852 :           DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6484              : 
    6485              :           /* Give template template parms a DECL_CONTEXT of the template
    6486              :              for which they are a parameter.  */
    6487     72537852 :           parms = INNERMOST_TEMPLATE_PARMS (parms);
    6488    202838834 :           for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
    6489              :             {
    6490    130300982 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6491    130300982 :               if (TREE_CODE (parm) == TEMPLATE_DECL)
    6492       228439 :                 DECL_CONTEXT (parm) = tmpl;
    6493              :             }
    6494              : 
    6495     72537852 :           if (TREE_CODE (decl) == TYPE_DECL
    6496     72537852 :               && TYPE_DECL_ALIAS_P (decl))
    6497              :             {
    6498      4861497 :               if (tree constr
    6499      4861497 :                   = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
    6500              :                 {
    6501              :                   /* ??? Why don't we do this here for all templates?  */
    6502       293351 :                   constr = build_constraints (constr, NULL_TREE);
    6503       293351 :                   set_constraints (decl, constr);
    6504              :                 }
    6505              :             }
    6506              :         }
    6507              : 
    6508              :       /* The DECL_TI_ARGS of DECL contains full set of arguments
    6509              :          referring wback to its most general template.  If TMPL is a
    6510              :          specialization, ARGS may only have the innermost set of
    6511              :          arguments.  Add the missing argument levels if necessary.  */
    6512    262301862 :       if (DECL_TEMPLATE_INFO (tmpl))
    6513         1194 :         args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
    6514              : 
    6515              :       /* Bug c++/103901.  Let's sorry now rather than ICE later.  */
    6516    262301862 :       if (TREE_VEC_LENGTH (args) == 0
    6517          279 :           && ctx && LAMBDA_FUNCTION_P (ctx)
    6518            3 :           && DECL_IMPLICIT_TYPEDEF_P (decl)
    6519    262301865 :           && CLASS_TYPE_P (TREE_TYPE (decl)))
    6520            3 :         sorry ("local class in lambda in template parameter list");
    6521              : 
    6522    262301862 :       tree info = build_template_info (tmpl, args);
    6523              : 
    6524    262301862 :       if (DECL_IMPLICIT_TYPEDEF_P (decl))
    6525     14762606 :         SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
    6526              :       else
    6527              :         {
    6528    247539256 :           retrofit_lang_decl (decl);
    6529    247539256 :           DECL_TEMPLATE_INFO (decl) = info;
    6530              :         }
    6531              :     }
    6532              : 
    6533    337378940 :   if (is_typedef_decl (decl)
    6534     58512207 :       && (dependent_opaque_alias_p (TREE_TYPE (decl))
    6535     58509878 :           || dependent_alias_template_spec_p (TREE_TYPE (decl), nt_opaque)))
    6536              :     {
    6537              :       /* Manually mark such aliases as dependent so that dependent_type_p_r
    6538              :          doesn't have to handle them.  */
    6539       654503 :       TYPE_DEPENDENT_P_VALID (TREE_TYPE (decl)) = true;
    6540       654503 :       TYPE_DEPENDENT_P (TREE_TYPE (decl)) = true;
    6541              :       /* The identity of such aliases is hairy; see structural_comptypes.  */
    6542       654503 :       SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (decl));
    6543              :     }
    6544              : 
    6545    337378940 :   if (flag_implicit_templates
    6546    336678252 :       && !is_friend
    6547    326640344 :       && TREE_PUBLIC (decl)
    6548    191494900 :       && VAR_OR_FUNCTION_DECL_P (decl))
    6549              :     /* Set DECL_COMDAT on template instantiations; if we force
    6550              :        them to be emitted by explicit instantiation,
    6551              :        mark_needed will tell cgraph to do the right thing.  */
    6552    188469565 :     DECL_COMDAT (decl) = true;
    6553              : 
    6554    599680802 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6555              : 
    6556              :   return decl;
    6557              : }
    6558              : 
    6559              : /* FN is an inheriting constructor that inherits from the constructor
    6560              :    template INHERITED; turn FN into a constructor template with a matching
    6561              :    template header.  */
    6562              : 
    6563              : tree
    6564       130956 : add_inherited_template_parms (tree fn, tree inherited)
    6565              : {
    6566       130956 :   tree inner_parms
    6567       130956 :     = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
    6568       130956 :   inner_parms = copy_node (inner_parms);
    6569       130956 :   tree parms
    6570       130956 :     = tree_cons (size_int (current_template_depth + 1),
    6571              :                  inner_parms, current_template_parms);
    6572       130956 :   tree tmpl = build_template_decl (fn, parms, /*member*/true);
    6573       130956 :   tree args = template_parms_to_args (parms);
    6574       130956 :   DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
    6575       130956 :   DECL_ARTIFICIAL (tmpl) = true;
    6576       130956 :   DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6577       130956 :   return tmpl;
    6578              : }
    6579              : 
    6580              : /* Called when a class template TYPE is redeclared with the indicated
    6581              :    template PARMS, e.g.:
    6582              : 
    6583              :      template <class T> struct S;
    6584              :      template <class T> struct S {};  */
    6585              : 
    6586              : bool
    6587      2870815 : redeclare_class_template (tree type, tree parms, tree cons)
    6588              : {
    6589      2870815 :   tree tmpl;
    6590      2870815 :   tree tmpl_parms;
    6591      2870815 :   int i;
    6592              : 
    6593      2870815 :   if (!TYPE_TEMPLATE_INFO (type))
    6594              :     {
    6595            0 :       error ("%qT is not a template type", type);
    6596            0 :       return false;
    6597              :     }
    6598              : 
    6599      2870815 :   tmpl = TYPE_TI_TEMPLATE (type);
    6600      2870815 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6601              :     /* The type is nested in some template class.  Nothing to worry
    6602              :        about here; there are no new template parameters for the nested
    6603              :        type.  */
    6604              :     return true;
    6605              : 
    6606      2870815 :   if (!parms)
    6607              :     {
    6608            3 :       error ("template specifiers not specified in declaration of %qD",
    6609              :              tmpl);
    6610            3 :       return false;
    6611              :     }
    6612              : 
    6613      2870812 :   parms = INNERMOST_TEMPLATE_PARMS (parms);
    6614      2870812 :   tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
    6615              : 
    6616      2870812 :   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
    6617              :     {
    6618           21 :       auto_diagnostic_group d;
    6619           21 :       error_n (input_location, TREE_VEC_LENGTH (parms),
    6620              :                "redeclared with %d template parameter",
    6621              :                "redeclared with %d template parameters",
    6622           21 :                TREE_VEC_LENGTH (parms));
    6623           42 :       inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
    6624              :                 "previous declaration %qD used %d template parameter",
    6625              :                 "previous declaration %qD used %d template parameters",
    6626           21 :                 tmpl, TREE_VEC_LENGTH (tmpl_parms));
    6627           21 :       return false;
    6628           21 :     }
    6629              : 
    6630      7627956 :   for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
    6631              :     {
    6632      4757213 :       tree tmpl_parm;
    6633      4757213 :       tree parm;
    6634              : 
    6635      4757213 :       if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
    6636      4757213 :           || TREE_VEC_ELT (parms, i) == error_mark_node)
    6637            0 :         continue;
    6638              : 
    6639      4757213 :       tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
    6640      4757213 :       if (error_operand_p (tmpl_parm))
    6641              :         return false;
    6642              : 
    6643      4757198 :       parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6644              : 
    6645              :       /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
    6646              :          TEMPLATE_DECL.  */
    6647      4757198 :       if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
    6648      4757186 :           || (TREE_CODE (tmpl_parm) != TYPE_DECL
    6649       530971 :               && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
    6650      4757177 :           || (TREE_CODE (tmpl_parm) != PARM_DECL
    6651      4226804 :               && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
    6652      4226804 :                   != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
    6653      9514372 :           || (TREE_CODE (tmpl_parm) == PARM_DECL
    6654       530373 :               && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
    6655       530373 :                   != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
    6656              :         {
    6657           27 :           auto_diagnostic_group d;
    6658           27 :           error ("template parameter %q+#D", tmpl_parm);
    6659           27 :           if (DECL_P (parm))
    6660           24 :             inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
    6661              :           else
    6662            3 :             inform (input_location, "redeclared here");
    6663           27 :           return false;
    6664           27 :         }
    6665              : 
    6666              :       /* The parameters can be declared to introduce different
    6667              :          constraints.  */
    6668      4757171 :       tree p1 = TREE_VEC_ELT (tmpl_parms, i);
    6669      4757171 :       tree p2 = TREE_VEC_ELT (parms, i);
    6670      4757171 :       if (!template_parameter_constraints_equivalent_p (p1, p2))
    6671              :         {
    6672            6 :           auto_diagnostic_group d;
    6673            6 :           error ("declaration of template parameter %q+#D with different "
    6674              :                  "constraints", parm);
    6675            6 :           inform (DECL_SOURCE_LOCATION (tmpl_parm),
    6676              :                   "original declaration appeared here");
    6677            6 :           return false;
    6678            6 :         }
    6679              : 
    6680              :       /* Give each template template parm in this redeclaration a
    6681              :          DECL_CONTEXT of the template for which they are a parameter.  */
    6682      4757165 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    6683              :         {
    6684          589 :           gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
    6685              :                                || DECL_CONTEXT (parm) == tmpl);
    6686          589 :           DECL_CONTEXT (parm) = tmpl;
    6687              :         }
    6688              :     }
    6689              : 
    6690      2870743 :   if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
    6691              :     return false;
    6692              : 
    6693      2870740 :   tree ci = get_constraints (tmpl);
    6694      2930164 :   tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
    6695      2930167 :   tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
    6696              : 
    6697              :   /* Two classes with different constraints declare different entities.  */
    6698      2870740 :   if (!cp_tree_equal (req1, req2))
    6699              :     {
    6700           15 :       auto_diagnostic_group d;
    6701           15 :       error_at (input_location, "redeclaration of %q#D with different "
    6702              :                                 "constraints", tmpl);
    6703           15 :       inform (DECL_SOURCE_LOCATION (tmpl),
    6704              :               "original declaration appeared here");
    6705           15 :       return false;
    6706           15 :     }
    6707              : 
    6708              :     return true;
    6709              : }
    6710              : 
    6711              : /* The actual substitution part of instantiate_non_dependent_expr,
    6712              :    to be used when the caller has already checked
    6713              :     !instantiation_dependent_uneval_expression_p (expr)
    6714              :    and cleared processing_template_decl.  */
    6715              : 
    6716              : tree
    6717     78398505 : instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
    6718              : {
    6719     78398505 :   return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE);
    6720              : }
    6721              : 
    6722              : /* Instantiate the non-dependent expression EXPR.  */
    6723              : 
    6724              : tree
    6725    151444300 : instantiate_non_dependent_expr (tree expr,
    6726              :                                 tsubst_flags_t complain /* = tf_error */)
    6727              : {
    6728    151444300 :   if (expr == NULL_TREE)
    6729              :     return NULL_TREE;
    6730              : 
    6731    151444300 :   if (processing_template_decl)
    6732              :     {
    6733              :       /* The caller should have checked this already.  */
    6734     38501009 :       gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
    6735     38501009 :       processing_template_decl_sentinel s;
    6736     38501009 :       expr = instantiate_non_dependent_expr_internal (expr, complain);
    6737     38501009 :     }
    6738              :   return expr;
    6739              : }
    6740              : 
    6741              : /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
    6742              :    expression is dependent or non-constant.  */
    6743              : 
    6744              : tree
    6745    156830201 : instantiate_non_dependent_or_null (tree expr)
    6746              : {
    6747    156830201 :   if (expr == NULL_TREE)
    6748              :     return NULL_TREE;
    6749    145189950 :   if (processing_template_decl)
    6750              :     {
    6751       385404 :       if (!is_nondependent_constant_expression (expr))
    6752              :         expr = NULL_TREE;
    6753              :       else
    6754              :         {
    6755       375561 :           processing_template_decl_sentinel s;
    6756       375561 :           expr = instantiate_non_dependent_expr_internal (expr, tf_error);
    6757       375561 :         }
    6758              :     }
    6759              :   return expr;
    6760              : }
    6761              : 
    6762              : /* True iff T is a specialization of a variable template.  */
    6763              : 
    6764              : bool
    6765    284416200 : variable_template_specialization_p (tree t)
    6766              : {
    6767    284416200 :   if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
    6768              :     return false;
    6769     27166895 :   tree tmpl = DECL_TI_TEMPLATE (t);
    6770     27166895 :   return variable_template_p (tmpl);
    6771              : }
    6772              : 
    6773              : /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
    6774              :    template declaration, or a TYPE_DECL for an alias declaration.  */
    6775              : 
    6776              : bool
    6777    160001968 : alias_type_or_template_p (tree t)
    6778              : {
    6779    160001968 :   if (t == NULL_TREE)
    6780              :     return false;
    6781            0 :   return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
    6782    160001968 :           || (TYPE_P (t)
    6783    160001968 :               && TYPE_NAME (t)
    6784    160001968 :               && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    6785    311546250 :           || DECL_ALIAS_TEMPLATE_P (t));
    6786              : }
    6787              : 
    6788              : /* If T is a specialization of an alias template, return it; otherwise return
    6789              :    NULL_TREE.  If TRANSPARENT_TYPEDEFS is true, look through other aliases.  */
    6790              : 
    6791              : tree
    6792   9576419461 : alias_template_specialization_p (const_tree t,
    6793              :                                  bool transparent_typedefs)
    6794              : {
    6795   9576807331 :   if (!TYPE_P (t))
    6796              :     return NULL_TREE;
    6797              : 
    6798              :   /* It's an alias template specialization if it's an alias and its
    6799              :      TYPE_NAME is a specialization of a primary template.  */
    6800   9576807215 :   if (typedef_variant_p (t))
    6801              :     {
    6802    790395498 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6803    718717112 :         if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    6804              :           return const_cast<tree> (t);
    6805    482167771 :       if (transparent_typedefs && !dependent_opaque_alias_p (t))
    6806       387870 :         return alias_template_specialization_p (DECL_ORIGINAL_TYPE
    6807              :                                                 (TYPE_NAME (t)),
    6808       387870 :                                                 transparent_typedefs);
    6809              :     }
    6810              : 
    6811              :   return NULL_TREE;
    6812              : }
    6813              : 
    6814              : /* A cache of the result of complex_alias_template_p.  */
    6815              : 
    6816              : static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
    6817              : 
    6818              : /* Data structure for complex_alias_template_*.  */
    6819              : 
    6820              : struct uses_all_template_parms_data
    6821              : {
    6822              :   int level;
    6823              :   tree *seen;
    6824              : };
    6825              : 
    6826              : /* walk_tree callback for complex_alias_template_p.  */
    6827              : 
    6828              : static tree
    6829     62152499 : complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
    6830              : {
    6831     62152499 :   tree t = *tp;
    6832     62152499 :   auto &data = *(struct uses_all_template_parms_data*)data_;
    6833              : 
    6834     62152499 :   switch (TREE_CODE (t))
    6835              :     {
    6836     11596057 :     case TEMPLATE_TYPE_PARM:
    6837     11596057 :     case TEMPLATE_PARM_INDEX:
    6838     11596057 :     case TEMPLATE_TEMPLATE_PARM:
    6839     11596057 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    6840     11596057 :       {
    6841     11596057 :         tree idx = get_template_parm_index (t);
    6842     11596057 :         if (TEMPLATE_PARM_LEVEL (idx) == data.level)
    6843      9882673 :           data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
    6844              :       }
    6845              : 
    6846     62152499 :     default:;
    6847              :     }
    6848              : 
    6849     62152499 :   if (!PACK_EXPANSION_P (t))
    6850              :     return 0;
    6851              : 
    6852              :   /* An alias template with a pack expansion that expands a pack from the
    6853              :      enclosing class needs to be considered complex, to avoid confusion with
    6854              :      the same pack being used as an argument to the alias's own template
    6855              :      parameter (91966).  */
    6856      3200246 :   for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
    6857      1049391 :        pack = TREE_CHAIN (pack))
    6858              :     {
    6859      1148017 :       tree parm_pack = TREE_VALUE (pack);
    6860      1148017 :       if (!TEMPLATE_PARM_P (parm_pack))
    6861        42095 :         continue;
    6862      1105922 :       int idx, level;
    6863      1105922 :       template_parm_level_and_index (parm_pack, &level, &idx);
    6864      1105922 :       if (level < data.level)
    6865        98626 :         return t;
    6866              : 
    6867              :       /* Consider the expanded packs to be used outside the expansion...  */
    6868      1007296 :       data.seen[idx] = boolean_true_node;
    6869              :     }
    6870              : 
    6871              :   /* ...but don't walk into the pattern.  Consider PR104008:
    6872              : 
    6873              :      template <typename T, typename... Ts>
    6874              :      using IsOneOf = disjunction<is_same<T, Ts>...>;
    6875              : 
    6876              :      where IsOneOf seemingly uses all of its template parameters in its
    6877              :      expansion (and does not expand a pack from the enclosing class), so the
    6878              :      alias was not marked as complex.  However, if it is used like
    6879              :      "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
    6880              :      expansion.  So only Ts is considered used by the pack expansion.  */
    6881      1036251 :   *walk_subtrees = false;
    6882              : 
    6883      1036251 :   return 0;
    6884              : }
    6885              : 
    6886              : /* An alias template is complex from a SFINAE perspective if a template-id
    6887              :    using that alias can be ill-formed when the expansion is not, as with
    6888              :    the void_t template.
    6889              : 
    6890              :    If this predicate returns true in the ordinary case, the out parameter
    6891              :    SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
    6892              :    the I'th template parameter of the alias template is used in the alias.  */
    6893              : 
    6894              : static bool
    6895    157200259 : complex_alias_template_p (const_tree tmpl, tree *seen_out)
    6896              : {
    6897    157200259 :   tmpl = most_general_template (tmpl);
    6898    157200259 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6899              :     return false;
    6900              : 
    6901              :   /* A renaming alias isn't complex.  */
    6902    113627619 :   if (get_underlying_template (const_cast<tree> (tmpl)) != tmpl)
    6903              :     return false;
    6904              : 
    6905              :   /* Any other constrained alias is complex.  */
    6906    104546377 :   if (get_constraints (tmpl))
    6907              :     return true;
    6908              : 
    6909              :   /* An alias with dependent type attributes is complex.  */
    6910     81138171 :   if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
    6911              :     return true;
    6912              : 
    6913     81132458 :   if (!complex_alias_tmpl_info)
    6914       102367 :     complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
    6915              : 
    6916     81132458 :   if (tree *slot = complex_alias_tmpl_info->get (tmpl))
    6917              :     {
    6918     74035167 :       tree result = *slot;
    6919     74035167 :       if (result == boolean_false_node)
    6920              :         return false;
    6921      9113490 :       if (result == boolean_true_node)
    6922              :         return true;
    6923      8224152 :       gcc_assert (TREE_CODE (result) == TREE_VEC);
    6924      8224152 :       if (seen_out)
    6925      8224152 :         *seen_out = result;
    6926              :       return true;
    6927              :     }
    6928              : 
    6929      7097291 :   struct uses_all_template_parms_data data;
    6930      7097291 :   tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6931      7097291 :   tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6932      7097291 :   data.level = TMPL_PARMS_DEPTH (parms);
    6933      7097291 :   int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
    6934      7097291 :   tree seen = make_tree_vec (len);
    6935      7097291 :   data.seen = TREE_VEC_BEGIN (seen);
    6936     17571921 :   for (int i = 0; i < len; ++i)
    6937     10474630 :     data.seen[i] = boolean_false_node;
    6938              : 
    6939      7097291 :   if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
    6940              :     {
    6941        98626 :       complex_alias_tmpl_info->put (tmpl, boolean_true_node);
    6942        98626 :       return true;
    6943              :     }
    6944              : 
    6945     16476335 :   for (int i = 0; i < len; ++i)
    6946      9986626 :     if (data.seen[i] != boolean_true_node)
    6947              :       {
    6948       508956 :         complex_alias_tmpl_info->put (tmpl, seen);
    6949       508956 :         if (seen_out)
    6950       508956 :           *seen_out = seen;
    6951              :         return true;
    6952              :       }
    6953              : 
    6954      6489709 :   complex_alias_tmpl_info->put (tmpl, boolean_false_node);
    6955      6489709 :   return false;
    6956              : }
    6957              : 
    6958              : /* If T is a specialization of a complex alias template with a dependent
    6959              :    argument for an unused template parameter, return it; otherwise return
    6960              :    NULL_TREE.  If T is a typedef to such a specialization, return the
    6961              :    specialization.  This predicate is usually checked alongside
    6962              :    dependent_opaque_alias_p.  Whereas dependent_opaque_alias_p checks
    6963              :    type equivalence of an alias vs its expansion, this predicate more
    6964              :    broadly checks SFINAE equivalence.  */
    6965              : 
    6966              : tree
    6967    357470811 : dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
    6968              : {
    6969    361943165 :   if (t == error_mark_node)
    6970              :     return NULL_TREE;
    6971    325469884 :   gcc_assert (TYPE_P (t));
    6972              : 
    6973    325469884 :   if (!processing_template_decl || !typedef_variant_p (t))
    6974              :     return NULL_TREE;
    6975              : 
    6976    171711861 :   if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6977              :     {
    6978    157200259 :       tree seen = NULL_TREE;
    6979    157200259 :       if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
    6980              :         {
    6981     33134991 :           tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
    6982     33134991 :           if (!seen)
    6983              :             {
    6984     24401883 :               if (any_dependent_template_arguments_p (args))
    6985     33134991 :                 return const_cast<tree> (t);
    6986              :             }
    6987              :           else
    6988              :             {
    6989      8733108 :               gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
    6990     14888075 :               for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    6991     12754541 :                 if (TREE_VEC_ELT (seen, i) != boolean_true_node
    6992     12754541 :                     && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
    6993              :                   return const_cast<tree> (t);
    6994              :             }
    6995              : 
    6996      2224737 :           return NULL_TREE;
    6997              :         }
    6998              :     }
    6999              : 
    7000    138576870 :   if (transparent_typedefs && !dependent_opaque_alias_p (t))
    7001              :     {
    7002      4472354 :       tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
    7003      4472354 :       return dependent_alias_template_spec_p (utype, transparent_typedefs);
    7004              :     }
    7005              : 
    7006              :   return NULL_TREE;
    7007              : }
    7008              : 
    7009              : /* Return true if substituting into T would yield a different type than
    7010              :    substituting into its expansion.  This predicate is usually checked
    7011              :    alongside dependent_alias_template_spec_p.  */
    7012              : 
    7013              : bool
    7014    550409503 : dependent_opaque_alias_p (const_tree t)
    7015              : {
    7016    550409503 :   return (TYPE_P (t)
    7017    550409503 :           && typedef_variant_p (t)
    7018              :           /* Treat a dependent decltype(lambda) alias as opaque so that we
    7019              :              don't prematurely strip it when used as a template argument.
    7020              :              Otherwise substitution into each occurrence of the (stripped)
    7021              :              alias would incorrectly yield a distinct lambda type.  Also
    7022              :              treat an alias to A<lambda> as opaque so that it doesn't
    7023              :              "leak" into a deeper template context which would cause us to
    7024              :              over substitute into the lambda.  */
    7025   1075582242 :           && (TYPE_DECL_OPAQUE_ALIAS_P (TYPE_NAME (t))
    7026    525160746 :               || any_dependent_type_attributes_p
    7027    525160746 :                   (DECL_ATTRIBUTES (TYPE_NAME (t)))));
    7028              : }
    7029              : 
    7030              : /* Return the number of innermost template parameters in TMPL.  */
    7031              : 
    7032              : static int
    7033     96049856 : num_innermost_template_parms (const_tree tmpl)
    7034              : {
    7035     96049856 :   tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    7036     96049856 :   return TREE_VEC_LENGTH (parms);
    7037              : }
    7038              : 
    7039              : /* Return either TMPL or another template that it is equivalent to under DR
    7040              :    1286: An alias that just changes the name of a template is equivalent to
    7041              :    the other template.  */
    7042              : 
    7043              : static tree
    7044    131074096 : get_underlying_template (tree tmpl)
    7045              : {
    7046    131074096 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
    7047    140157319 :   while (DECL_ALIAS_TEMPLATE_P (tmpl))
    7048              :     {
    7049              :       /* Determine if the alias is equivalent to an underlying template.  */
    7050    121846516 :       tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    7051              :       /* The underlying type may have been ill-formed. Don't proceed.  */
    7052    121846516 :       if (!orig_type)
    7053              :         break;
    7054    121846513 :       tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
    7055     48027528 :       if (!tinfo)
    7056              :         break;
    7057              : 
    7058     48025012 :       tree underlying = TI_TEMPLATE (tinfo);
    7059     48025012 :       if (!PRIMARY_TEMPLATE_P (underlying)
    7060     96049940 :           || (num_innermost_template_parms (tmpl)
    7061     48024928 :               != num_innermost_template_parms (underlying)))
    7062              :         break;
    7063              : 
    7064              :       /* Does the alias add cv-quals?  */
    7065     26621889 :       if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
    7066              :         break;
    7067              : 
    7068     26621864 :       tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
    7069     26621864 :       if (!comp_template_args (TI_ARGS (tinfo), alias_args))
    7070              :         break;
    7071              : 
    7072              :       /* Are any default template arguments equivalent?  */
    7073      9083435 :       tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    7074      9083435 :       tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
    7075      9083435 :       const int nparms = TREE_VEC_LENGTH (aparms);
    7076     19368822 :       for (int i = 0; i < nparms; ++i)
    7077              :         {
    7078     10285528 :           tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
    7079     10285528 :           tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
    7080     10285528 :           if (!template_args_equal (adefarg, udefarg))
    7081          141 :             goto top_break;
    7082              :         }
    7083              : 
    7084              :       /* If TMPL adds or changes any constraints, it isn't equivalent.  I think
    7085              :          it's appropriate to treat a less-constrained alias as equivalent.  */
    7086      9083294 :       if (!at_least_as_constrained (underlying, tmpl))
    7087              :         break;
    7088              : 
    7089              :       /* If TMPL adds dependent type attributes, it isn't equivalent.  */
    7090      9083231 :       if (dependent_opaque_alias_p (TREE_TYPE (tmpl)))
    7091              :         break;
    7092              : 
    7093              :       /* Alias is equivalent.  Strip it and repeat.  */
    7094              :       tmpl = underlying;
    7095              :     }
    7096     17541044 :   top_break:;
    7097              : 
    7098    131074096 :   return tmpl;
    7099              : }
    7100              : 
    7101              : /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
    7102              :    must be a reference-to-function or a pointer-to-function type, as specified
    7103              :    in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
    7104              :    and check that the resulting function has external linkage.  */
    7105              : 
    7106              : static tree
    7107         1827 : convert_nontype_argument_function (tree type, tree expr,
    7108              :                                    tsubst_flags_t complain)
    7109              : {
    7110         1827 :   tree fns = expr;
    7111         1827 :   tree fn, fn_no_ptr;
    7112         1827 :   linkage_kind linkage;
    7113              : 
    7114         1827 :   fn = instantiate_type (type, fns, tf_none);
    7115         1827 :   if (fn == error_mark_node)
    7116              :     return error_mark_node;
    7117              : 
    7118         1822 :   if (value_dependent_expression_p (fn))
    7119           21 :     goto accept;
    7120              : 
    7121         1801 :   fn_no_ptr = fn;
    7122         1801 :   if (REFERENCE_REF_P (fn_no_ptr))
    7123           14 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    7124         1801 :   fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
    7125         1801 :   if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
    7126         1630 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    7127         1801 :   if (BASELINK_P (fn_no_ptr))
    7128            0 :     fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
    7129              : 
    7130              :   /* [temp.arg.nontype]/1
    7131              : 
    7132              :      A template-argument for a non-type, non-template template-parameter
    7133              :      shall be one of:
    7134              :      [...]
    7135              :      -- the address of an object or function with external [C++11: or
    7136              :         internal] linkage.  */
    7137              : 
    7138         1801 :   STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
    7139         1801 :   if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
    7140              :     {
    7141           37 :       if (complain & tf_error)
    7142              :         {
    7143           20 :           auto_diagnostic_group d;
    7144           20 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    7145           20 :           tree c;
    7146           20 :           if (cxx_dialect >= cxx17
    7147           20 :               && (c = cxx_constant_value (fn),
    7148           15 :                   c == error_mark_node))
    7149              :             ;
    7150              :           else
    7151              :             {
    7152            5 :               error_at (loc, "%qE is not a valid template argument for "
    7153              :                         "type %qT", expr, type);
    7154            5 :               if (TYPE_PTR_P (type))
    7155            5 :                 inform (loc, "it must be the address of a function "
    7156              :                         "with external linkage");
    7157              :               else
    7158            0 :                 inform (loc, "it must be the name of a function with "
    7159              :                         "external linkage");
    7160              :             }
    7161           20 :         }
    7162              :       return NULL_TREE;
    7163              :     }
    7164              : 
    7165         1764 :   linkage = decl_linkage (fn_no_ptr);
    7166         1764 :   if ((cxx_dialect < cxx11 && linkage != lk_external)
    7167         1763 :       || (cxx_dialect < cxx17 && linkage == lk_none))
    7168              :     {
    7169            5 :       if (complain & tf_error)
    7170              :         {
    7171            2 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    7172            2 :           if (cxx_dialect >= cxx11)
    7173            2 :             error_at (loc, "%qE is not a valid template argument for type "
    7174              :                       "%qT because %qD has no linkage",
    7175              :                       expr, type, fn_no_ptr);
    7176              :           else
    7177            0 :             error_at (loc, "%qE is not a valid template argument for type "
    7178              :                       "%qT because %qD does not have external linkage",
    7179              :                       expr, type, fn_no_ptr);
    7180              :         }
    7181              :       return NULL_TREE;
    7182              :     }
    7183              : 
    7184         1759 :  accept:
    7185         1780 :   if (TYPE_REF_P (type))
    7186              :     {
    7187          145 :       if (REFERENCE_REF_P (fn))
    7188           11 :         fn = TREE_OPERAND (fn, 0);
    7189              :       else
    7190          134 :         fn = build_address (fn);
    7191              :     }
    7192         1780 :   if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
    7193          134 :     fn = build_nop (type, fn);
    7194              : 
    7195              :   return fn;
    7196              : }
    7197              : 
    7198              : /* Subroutine of convert_nontype_argument.
    7199              :    Check if EXPR of type TYPE is a valid pointer-to-member constant.
    7200              :    Emit an error otherwise.  */
    7201              : 
    7202              : static bool
    7203         2399 : check_valid_ptrmem_cst_expr (tree type, tree expr,
    7204              :                              tsubst_flags_t complain)
    7205              : {
    7206         2399 :   tree orig_expr = expr;
    7207         2399 :   STRIP_NOPS (expr);
    7208         2399 :   if (null_ptr_cst_p (expr))
    7209              :     return true;
    7210         2399 :   if (TREE_CODE (expr) == PTRMEM_CST
    7211         2399 :       && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
    7212              :                       PTRMEM_CST_CLASS (expr)))
    7213              :     return true;
    7214          244 :   if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
    7215              :     return true;
    7216           37 :   if (processing_template_decl
    7217            0 :       && TREE_CODE (expr) == ADDR_EXPR
    7218           37 :       && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
    7219              :     return true;
    7220           37 :   if (complain & tf_error)
    7221              :     {
    7222           19 :       auto_diagnostic_group d;
    7223           19 :       location_t loc = cp_expr_loc_or_input_loc (orig_expr);
    7224           19 :       error_at (loc, "%qE is not a valid template argument for type %qT",
    7225              :                 orig_expr, type);
    7226           19 :       if (TREE_CODE (expr) != PTRMEM_CST)
    7227           10 :         inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
    7228              :       else
    7229            9 :         inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
    7230           19 :     }
    7231              :   return false;
    7232              : }
    7233              : 
    7234              : /* Returns TRUE iff the address of OP is value-dependent.
    7235              : 
    7236              :    14.6.2.4 [temp.dep.temp]:
    7237              :    A non-integral non-type template-argument is dependent if its type is
    7238              :    dependent or it has either of the following forms
    7239              :      qualified-id
    7240              :      & qualified-id
    7241              :    and contains a nested-name-specifier which specifies a class-name that
    7242              :    names a dependent type.
    7243              : 
    7244              :    We generalize this to just say that the address of a member of a
    7245              :    dependent class is value-dependent; the above doesn't cover the
    7246              :    address of a static data member named with an unqualified-id.  */
    7247              : 
    7248              : static bool
    7249      6231655 : has_value_dependent_address (tree op)
    7250              : {
    7251      6231655 :   STRIP_ANY_LOCATION_WRAPPER (op);
    7252              : 
    7253              :   /* We could use get_inner_reference here, but there's no need;
    7254              :      this is only relevant for template non-type arguments, which
    7255              :      can only be expressed as &id-expression.  */
    7256      6231655 :   if (DECL_P (op))
    7257              :     {
    7258       284676 :       tree ctx = CP_DECL_CONTEXT (op);
    7259              : 
    7260       284676 :       if (TYPE_P (ctx) && dependent_type_p (ctx))
    7261              :         return true;
    7262              : 
    7263       265278 :       if (VAR_P (op)
    7264        54921 :           && TREE_STATIC (op)
    7265        25150 :           && TREE_CODE (ctx) == FUNCTION_DECL
    7266          513 :           && DECL_TEMPLATE_INFO (ctx)
    7267       265638 :           && any_dependent_template_arguments_p (DECL_TI_ARGS (ctx)))
    7268              :         return true;
    7269              :     }
    7270              : 
    7271              :   return false;
    7272              : }
    7273              : 
    7274              : /* The next set of functions are used for providing helpful explanatory
    7275              :    diagnostics for failed overload resolution.  Their messages should be
    7276              :    indented by two spaces for consistency with the messages in
    7277              :    call.cc  */
    7278              : 
    7279              : static int
    7280            0 : unify_success (bool /*explain_p*/)
    7281              : {
    7282            0 :   return 0;
    7283              : }
    7284              : 
    7285              : /* Other failure functions should call this one, to provide a single function
    7286              :    for setting a breakpoint on.  */
    7287              : 
    7288              : static int
    7289            0 : unify_invalid (bool /*explain_p*/)
    7290              : {
    7291            0 :   return 1;
    7292              : }
    7293              : 
    7294              : static int
    7295       378900 : unify_parameter_deduction_failure (bool explain_p, tree parm)
    7296              : {
    7297            0 :   if (explain_p)
    7298          272 :     inform (input_location,
    7299              :             "  couldn%'t deduce template parameter %qD", parm);
    7300            0 :   return unify_invalid (explain_p);
    7301              : }
    7302              : 
    7303              : static int
    7304      3962252 : unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
    7305              : {
    7306            0 :   if (explain_p)
    7307           57 :     inform (input_location,
    7308              :             "  types %qT and %qT have incompatible cv-qualifiers",
    7309              :             parm, arg);
    7310            0 :   return unify_invalid (explain_p);
    7311              : }
    7312              : 
    7313              : static int
    7314    240968868 : unify_type_mismatch (bool explain_p, tree parm, tree arg)
    7315              : {
    7316         6747 :   if (explain_p)
    7317          518 :     inform (input_location, "  mismatched types %qT and %qT", parm, arg);
    7318         6747 :   return unify_invalid (explain_p);
    7319              : }
    7320              : 
    7321              : static int
    7322       466802 : unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
    7323              : {
    7324            0 :   if (explain_p)
    7325            0 :     inform (input_location,
    7326              :             "  template parameter %qD is not a parameter pack, but "
    7327              :             "argument %qD is",
    7328              :             parm, arg);
    7329            0 :   return unify_invalid (explain_p);
    7330              : }
    7331              : 
    7332              : static int
    7333            3 : unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
    7334              : {
    7335            0 :   if (explain_p)
    7336            0 :     inform (input_location,
    7337              :             "  template argument %qE does not match "
    7338              :             "pointer-to-member constant %qE",
    7339              :             arg, parm);
    7340            0 :   return unify_invalid (explain_p);
    7341              : }
    7342              : 
    7343              : static int
    7344           14 : unify_expression_unequal (bool explain_p, tree parm, tree arg)
    7345              : {
    7346            3 :   if (explain_p)
    7347            0 :     inform (input_location, "  %qE is not equivalent to %qE", parm, arg);
    7348            3 :   return unify_invalid (explain_p);
    7349              : }
    7350              : 
    7351              : static int
    7352           18 : unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
    7353              : {
    7354            0 :   if (explain_p)
    7355            3 :     inform (input_location,
    7356              :             "  inconsistent parameter pack deduction with %qT and %qT",
    7357              :             old_arg, new_arg);
    7358           18 :   return unify_invalid (explain_p);
    7359              : }
    7360              : 
    7361              : static int
    7362      1045565 : unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
    7363              : {
    7364      1045565 :   if (explain_p)
    7365              :     {
    7366           60 :       if (TYPE_P (parm))
    7367           54 :         inform (input_location,
    7368              :                 "  deduced conflicting types for parameter %qT (%qT and %qT)",
    7369              :                 parm, first, second);
    7370              :       else
    7371            6 :         inform (input_location,
    7372              :                 "  deduced conflicting values for non-type parameter "
    7373              :                 "%qE (%qE and %qE)", parm, first, second);
    7374              :     }
    7375      1045565 :   return unify_invalid (explain_p);
    7376              : }
    7377              : 
    7378              : static int
    7379           20 : unify_vla_arg (bool explain_p, tree arg)
    7380              : {
    7381            0 :   if (explain_p)
    7382            9 :     inform (input_location,
    7383              :             "  variable-sized array type %qT is not "
    7384              :             "a valid template argument",
    7385              :             arg);
    7386            0 :   return unify_invalid (explain_p);
    7387              : }
    7388              : 
    7389              : static int
    7390            6 : unify_method_type_error (bool explain_p, tree arg)
    7391              : {
    7392            0 :   if (explain_p)
    7393            0 :     inform (input_location,
    7394              :             "  member function type %qT is not a valid template argument",
    7395              :             arg);
    7396            0 :   return unify_invalid (explain_p);
    7397              : }
    7398              : 
    7399              : static int
    7400      3955428 : unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
    7401              : {
    7402      3955428 :   if (explain_p)
    7403              :     {
    7404            3 :       if (least_p)
    7405            0 :         inform_n (input_location, wanted,
    7406              :                   "  candidate expects at least %d argument, %d provided",
    7407              :                   "  candidate expects at least %d arguments, %d provided",
    7408              :                   wanted, have);
    7409              :       else
    7410            3 :         inform_n (input_location, wanted,
    7411              :                   "  candidate expects %d argument, %d provided",
    7412              :                   "  candidate expects %d arguments, %d provided",
    7413              :                   wanted, have);
    7414              :     }
    7415      3955428 :   return unify_invalid (explain_p);
    7416              : }
    7417              : 
    7418              : static int
    7419        52655 : unify_too_many_arguments (bool explain_p, int have, int wanted)
    7420              : {
    7421            0 :   return unify_arity (explain_p, have, wanted);
    7422              : }
    7423              : 
    7424              : static int
    7425        26565 : unify_too_few_arguments (bool explain_p, int have, int wanted,
    7426              :                          bool least_p = false)
    7427              : {
    7428            0 :   return unify_arity (explain_p, have, wanted, least_p);
    7429              : }
    7430              : 
    7431              : static int
    7432      3173356 : unify_arg_conversion (bool explain_p, tree to_type,
    7433              :                       tree from_type, tree arg)
    7434              : {
    7435      3173356 :   if (explain_p)
    7436          239 :     inform (cp_expr_loc_or_input_loc (arg),
    7437              :             "  cannot convert %qE (type %qT) to type %qT",
    7438              :             arg, from_type, to_type);
    7439      3173356 :   return unify_invalid (explain_p);
    7440              : }
    7441              : 
    7442              : static int
    7443    153280322 : unify_no_common_base (bool explain_p, enum template_base_result r,
    7444              :                       tree parm, tree arg)
    7445              : {
    7446    153280322 :   if (explain_p)
    7447          852 :     switch (r)
    7448              :       {
    7449            3 :       case tbr_ambiguous_baseclass:
    7450            3 :         inform (input_location, "  %qT is an ambiguous base class of %qT",
    7451              :                 parm, arg);
    7452            3 :         break;
    7453          849 :       default:
    7454          849 :         inform (input_location, "  %qT is not derived from %qT", arg, parm);
    7455          849 :         break;
    7456              :       }
    7457    153280322 :   return unify_invalid (explain_p);
    7458              : }
    7459              : 
    7460              : static int
    7461           24 : unify_inconsistent_template_template_parameters (bool explain_p)
    7462              : {
    7463           24 :   if (explain_p)
    7464           10 :     inform (input_location,
    7465              :             "  template parameters of a template template argument are "
    7466              :             "inconsistent with other deduced template arguments");
    7467           24 :   return unify_invalid (explain_p);
    7468              : }
    7469              : 
    7470              : static int
    7471         1053 : unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
    7472              : {
    7473            0 :   if (explain_p)
    7474            0 :     inform (input_location,
    7475              :             "  cannot deduce a template for %qT from non-template type %qT",
    7476              :             parm, arg);
    7477            0 :   return unify_invalid (explain_p);
    7478              : }
    7479              : 
    7480              : static int
    7481     11879958 : unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
    7482              : {
    7483            0 :   if (explain_p)
    7484            9 :     inform (input_location,
    7485              :             "  template argument %qE does not match %qE", arg, parm);
    7486            0 :   return unify_invalid (explain_p);
    7487              : }
    7488              : 
    7489              : /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
    7490              :    argument for TYPE, points to an unsuitable object.
    7491              : 
    7492              :    Also adjust the type of the index in C++20 array subobject references.  */
    7493              : 
    7494              : static bool
    7495       191490 : invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
    7496              : {
    7497       191544 :   switch (TREE_CODE (expr))
    7498              :     {
    7499           54 :     CASE_CONVERT:
    7500           54 :       return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
    7501           54 :                                        complain);
    7502              : 
    7503            0 :     case TARGET_EXPR:
    7504            0 :       return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
    7505            0 :                                        complain);
    7506              : 
    7507        93112 :     case CONSTRUCTOR:
    7508        93112 :       {
    7509       389608 :         for (auto &e: CONSTRUCTOR_ELTS (expr))
    7510       121332 :           if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
    7511              :             return true;
    7512              :       }
    7513              :       break;
    7514              : 
    7515         3396 :     case ADDR_EXPR:
    7516         3396 :       {
    7517         3396 :         tree decl = TREE_OPERAND (expr, 0);
    7518              : 
    7519         3396 :         if (cxx_dialect >= cxx20)
    7520         3349 :           while (TREE_CODE (decl) == COMPONENT_REF
    7521         3349 :                  || TREE_CODE (decl) == ARRAY_REF)
    7522              :             {
    7523          258 :               tree &op = TREE_OPERAND (decl, 1);
    7524          258 :               if (TREE_CODE (decl) == ARRAY_REF
    7525          148 :                   && TREE_CODE (op) == INTEGER_CST)
    7526              :                 /* Canonicalize array offsets to ptrdiff_t; how they were
    7527              :                    written doesn't matter for subobject identity.  */
    7528          148 :                 op = fold_convert (ptrdiff_type_node, op);
    7529          258 :               decl = TREE_OPERAND (decl, 0);
    7530              :             }
    7531              : 
    7532         3396 :         if (!VAR_OR_FUNCTION_DECL_P (decl))
    7533              :           {
    7534           76 :             if (complain & tf_error)
    7535           45 :               error_at (cp_expr_loc_or_input_loc (expr),
    7536              :                         "%qE is not a valid template argument of type %qT "
    7537              :                         "because %qE is not a variable or function",
    7538              :                         expr, type, decl);
    7539              :             return true;
    7540              :           }
    7541         3320 :         else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
    7542              :           {
    7543            2 :             if (complain & tf_error)
    7544            3 :               error_at (cp_expr_loc_or_input_loc (expr),
    7545              :                         "%qE is not a valid template argument of type %qT "
    7546              :                         "in C++98 because %qD does not have external linkage",
    7547              :                         expr, type, decl);
    7548              :             return true;
    7549              :           }
    7550         3318 :         else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
    7551         3318 :                  && decl_linkage (decl) == lk_none)
    7552              :           {
    7553            7 :             if (complain & tf_error)
    7554           14 :               error_at (cp_expr_loc_or_input_loc (expr),
    7555              :                         "%qE is not a valid template argument of type %qT "
    7556              :                         "because %qD has no linkage", expr, type, decl);
    7557              :             return true;
    7558              :           }
    7559              :         /* For a constant template parameter of reference or pointer type,
    7560              :            or for each non-static data member of reference or pointer type
    7561              :            in a constant template parameter of class type or subobject thereof,
    7562              :            the reference or pointer value shall not refer or point to
    7563              :            (respectively):
    7564              :            -- a temporary object,
    7565              :            -- a string literal object,
    7566              :            -- the result of a typeid expression,
    7567              :            -- a predefined __func__ variable, or
    7568              :            -- a subobject of one of the above.  */
    7569         3302 :         else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)
    7570         3436 :                  && !DECL_NTTP_OBJECT_P (decl))
    7571              :           {
    7572           12 :             gcc_checking_assert (DECL_TINFO_P (decl)
    7573              :                                  || DECL_FNAME_P (decl)
    7574              :                                  || DECL_IGNORED_P (decl));
    7575           12 :             if (complain & tf_error)
    7576            9 :               error ("the address of %qD is not a valid template argument",
    7577              :                      decl);
    7578              :             return true;
    7579              :           }
    7580         3299 :         else if (cxx_dialect < cxx20
    7581         3588 :                  && !(same_type_ignoring_top_level_qualifiers_p
    7582          289 :                       (strip_array_types (TREE_TYPE (type)),
    7583          289 :                        strip_array_types (TREE_TYPE (decl)))))
    7584              :           {
    7585            6 :             if (complain & tf_error)
    7586            4 :               error ("the address of the %qT subobject of %qD is not a "
    7587            4 :                      "valid template argument", TREE_TYPE (type), decl);
    7588              :             return true;
    7589              :           }
    7590         3293 :         else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
    7591              :           {
    7592            1 :             if (complain & tf_error)
    7593            1 :               error ("the address of %qD is not a valid template argument "
    7594              :                      "because it does not have static storage duration",
    7595              :                      decl);
    7596              :             return true;
    7597              :           }
    7598              :       }
    7599              :       break;
    7600              : 
    7601        94982 :     default:
    7602        94982 :       if (!INDIRECT_TYPE_P (type))
    7603              :         /* We're only concerned about pointers and references here.  */;
    7604          190 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7605              :         /* Null pointer values are OK in C++11.  */;
    7606              :       else
    7607              :         {
    7608           17 :           tree c;
    7609           17 :           if (!(complain & tf_error))
    7610              :             ;
    7611           11 :           else if (cxx_dialect >= cxx17
    7612           11 :                    && (c = cxx_constant_value (expr),
    7613            7 :                        c == error_mark_node))
    7614              :             ;
    7615            4 :           else if (VAR_P (expr))
    7616            2 :             error ("%qD is not a valid template argument "
    7617              :                    "because %qD is a variable, not the address of "
    7618              :                    "a variable", expr, expr);
    7619              :           else
    7620            2 :             error ("%qE is not a valid template argument for %qT "
    7621              :                    "because it is not the address of a variable",
    7622              :                    expr, type);
    7623              :           return true;
    7624              :         }
    7625              :     }
    7626              :   return false;
    7627              : 
    7628              : }
    7629              : 
    7630              : /* Return a VAR_DECL for the C++20 template parameter object corresponding to
    7631              :    template argument EXPR.  */
    7632              : 
    7633              : static tree
    7634        66846 : create_template_parm_object (tree expr, tsubst_flags_t complain)
    7635              : {
    7636        66846 :   tree orig = expr;
    7637        66846 :   if (TREE_CODE (expr) == TARGET_EXPR)
    7638        66846 :     expr = TARGET_EXPR_INITIAL (expr);
    7639              : 
    7640        66846 :   if (!TREE_CONSTANT (expr))
    7641              :     {
    7642           41 :       if ((complain & tf_error)
    7643           41 :           && require_rvalue_constant_expression (orig))
    7644           14 :         cxx_constant_value (orig);
    7645           41 :       return error_mark_node;
    7646              :     }
    7647        66805 :   if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
    7648            6 :     return error_mark_node;
    7649              : 
    7650              :   /* This is no longer a compound literal.  */
    7651        66799 :   gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
    7652              : 
    7653        66799 :   return get_template_parm_object (expr, mangle_template_parm_object (expr));
    7654              : }
    7655              : 
    7656              : /* The template arguments corresponding to template parameter objects of types
    7657              :    that contain pointers to members.  */
    7658              : 
    7659              : static GTY(()) hash_map<tree, tree> *tparm_obj_values;
    7660              : 
    7661              : /* Find or build an nttp object for (already-validated) EXPR with name
    7662              :    NAME.  When CHECK_INIT is false we don't need to process the initialiser,
    7663              :    it's already been done.  */
    7664              : 
    7665              : tree
    7666        67794 : get_template_parm_object (tree expr, tree name, bool check_init/*=true*/)
    7667              : {
    7668        67794 :   tree decl = get_global_binding (name);
    7669        67794 :   if (decl)
    7670              :     return decl;
    7671              : 
    7672         2627 :   tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
    7673         2627 :   decl = create_temporary_var (type);
    7674         2627 :   DECL_NTTP_OBJECT_P (decl) = true;
    7675         2627 :   DECL_CONTEXT (decl) = NULL_TREE;
    7676         2627 :   TREE_STATIC (decl) = true;
    7677         2627 :   DECL_DECLARED_CONSTEXPR_P (decl) = true;
    7678         2627 :   TREE_READONLY (decl) = true;
    7679         2627 :   DECL_NAME (decl) = name;
    7680         2627 :   SET_DECL_ASSEMBLER_NAME (decl, name);
    7681         2627 :   comdat_linkage (decl);
    7682         2627 :   if (check_init)
    7683         2619 :     expr = unshare_expr_without_location (expr);
    7684              : 
    7685         2627 :   if (!zero_init_p (type))
    7686              :     {
    7687              :       /* If EXPR contains any PTRMEM_CST, they will get clobbered by
    7688              :          lower_var_init before we're done mangling.  So store the original
    7689              :          value elsewhere.  We only need to unshare EXPR if it's not yet
    7690              :          been processed.  */
    7691           35 :       tree copy = check_init ? unshare_constructor (expr) : expr;
    7692           35 :       hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
    7693              :     }
    7694              : 
    7695         2627 :   if (!check_init)
    7696              :     {
    7697              :       /* The EXPR is the already processed initializer, set it on the NTTP
    7698              :          object now so that cp_finish_decl doesn't do it again later.  */
    7699            8 :       gcc_checking_assert (reduced_constant_expression_p (expr));
    7700            8 :       DECL_INITIAL (decl) = expr;
    7701            8 :       DECL_INITIALIZED_P (decl) = true;
    7702            8 :       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
    7703              :       /* FIXME setting TREE_CONSTANT on refs breaks the back end.  */
    7704            8 :       if (!TYPE_REF_P (type))
    7705            8 :         TREE_CONSTANT (decl) = true;
    7706            8 :       pushdecl_top_level (decl);
    7707              :       /* The caller must call cp_finish_decl to complete its processing.  */
    7708            8 :       return decl;
    7709              :     }
    7710              : 
    7711         2619 :   pushdecl_top_level_and_finish (decl, expr);
    7712              : 
    7713         2619 :   return decl;
    7714              : }
    7715              : 
    7716              : /* Return the actual template argument corresponding to template parameter
    7717              :    object VAR.  */
    7718              : 
    7719              : tree
    7720        51538 : tparm_object_argument (tree var)
    7721              : {
    7722        51538 :   if (zero_init_p (TREE_TYPE (var)))
    7723        51507 :     return DECL_INITIAL (var);
    7724           31 :   return *(tparm_obj_values->get (var));
    7725              : }
    7726              : 
    7727              : /* Attempt to convert the non-type template parameter EXPR to the
    7728              :    indicated TYPE.  If the conversion is successful, return the
    7729              :    converted value.  If the conversion is unsuccessful, return
    7730              :    NULL_TREE if we issued an error message, or error_mark_node if we
    7731              :    did not.  If tf_error is not set in COMPLAIN, whether NULL_TREE
    7732              :    or error_mark_node is returned doesn't matter.  We issue error
    7733              :    messages for out-and-out bad template parameters, but not simply
    7734              :    because the conversion failed, since we might be just trying to
    7735              :    do argument deduction.  Both TYPE and EXPR must be non-dependent.
    7736              : 
    7737              :    The conversion follows the special rules described in
    7738              :    [temp.arg.nontype], and it is much more strict than an implicit
    7739              :    conversion.
    7740              : 
    7741              :    This function is called twice for each template argument (see
    7742              :    lookup_template_class for a more accurate description of this
    7743              :    problem). This means that we need to handle expressions which
    7744              :    are not valid in a C++ source, but can be created from the
    7745              :    first call (for instance, casts to perform conversions). These
    7746              :    hacks can go away after we fix the double coercion problem.  */
    7747              : 
    7748              : static tree
    7749    189267938 : convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
    7750              : {
    7751    189267938 :   tree expr_type;
    7752    189267938 :   location_t loc = cp_expr_loc_or_input_loc (expr);
    7753              : 
    7754              :   /* Detect immediately string literals as invalid non-type argument.
    7755              :      This special-case is not needed for correctness (we would easily
    7756              :      catch this later), but only to provide better diagnostic for this
    7757              :      common user mistake. As suggested by DR 100, we do not mention
    7758              :      linkage issues in the diagnostic as this is not the point.  */
    7759    189267938 :   if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
    7760              :     {
    7761            6 :       if (complain & tf_error)
    7762            6 :         error ("%qE is not a valid template argument for type %qT "
    7763              :                "because string literals can never be used in this context",
    7764              :                expr, type);
    7765              :       return NULL_TREE;
    7766              :     }
    7767              : 
    7768              :   /* Add the ADDR_EXPR now for the benefit of
    7769              :      value_dependent_expression_p.  */
    7770         4722 :   if (TYPE_PTROBV_P (type)
    7771    189270875 :       && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
    7772              :     {
    7773          117 :       expr = decay_conversion (expr, complain);
    7774          117 :       if (expr == error_mark_node)
    7775              :         return error_mark_node;
    7776              :     }
    7777              : 
    7778              :   /* If we are in a template, EXPR may be non-dependent, but still
    7779              :      have a syntactic, rather than semantic, form.  For example, EXPR
    7780              :      might be a SCOPE_REF, rather than the VAR_DECL to which the
    7781              :      SCOPE_REF refers.  Preserving the qualifying scope is necessary
    7782              :      so that access checking can be performed when the template is
    7783              :      instantiated -- but here we need the resolved form so that we can
    7784              :      convert the argument.  */
    7785    189267932 :   bool non_dep = false;
    7786          888 :   if (TYPE_REF_OBJ_P (type)
    7787    189268655 :       && has_value_dependent_address (expr))
    7788              :     /* If we want the address and it's value-dependent, don't fold.  */;
    7789    189267905 :   else if (processing_template_decl
    7790    189267905 :            && !instantiation_dependent_expression_p (expr))
    7791              :     non_dep = true;
    7792    189267932 :   if (error_operand_p (expr))
    7793          779 :     return error_mark_node;
    7794    189267153 :   expr_type = TREE_TYPE (expr);
    7795              : 
    7796              :   /* If the argument is non-dependent, perform any conversions in
    7797              :      non-dependent context as well.  */
    7798    189267153 :   processing_template_decl_sentinel s (non_dep);
    7799     21783467 :   if (non_dep)
    7800     21783467 :     expr = instantiate_non_dependent_expr_internal (expr, complain);
    7801              : 
    7802    189267153 :   bool val_dep_p = value_dependent_expression_p (expr);
    7803    189267153 :   if (val_dep_p)
    7804     26234268 :     expr = canonicalize_expr_argument (expr, complain);
    7805              :   else
    7806    163032885 :     STRIP_ANY_LOCATION_WRAPPER (expr);
    7807              : 
    7808              :   /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
    7809              :      to a non-type argument of "nullptr".  */
    7810    189267153 :   if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
    7811          103 :     expr = fold_simple (convert (type, expr));
    7812              : 
    7813              :   /* In C++11, integral or enumeration non-type template arguments can be
    7814              :      arbitrary constant expressions.  Pointer and pointer to
    7815              :      member arguments can be general constant expressions that evaluate
    7816              :      to a null value, but otherwise still need to be of a specific form.  */
    7817    189267153 :   if (cxx_dialect >= cxx11)
    7818              :     {
    7819    189226301 :       if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
    7820              :         /* A PTRMEM_CST is already constant, and a valid template
    7821              :            argument for a parameter of pointer to member type, we just want
    7822              :            to leave it in that form rather than lower it to a
    7823              :            CONSTRUCTOR.  */;
    7824    189224367 :       else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7825        76364 :                || cxx_dialect >= cxx17)
    7826              :         {
    7827              :           /* C++17: A template-argument for a non-type template-parameter shall
    7828              :              be a converted constant expression (8.20) of the type of the
    7829              :              template-parameter.  */
    7830    189224081 :           expr = build_converted_constant_expr (type, expr, complain);
    7831    189224081 :           if (expr == error_mark_node)
    7832              :             /* Make sure we return NULL_TREE only if we have really issued
    7833              :                an error, as described above.  */
    7834         1416 :             return (complain & tf_error) ? NULL_TREE : error_mark_node;
    7835    189222665 :           else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
    7836              :             {
    7837      3912192 :               IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
    7838      3912192 :               return expr;
    7839              :             }
    7840    185310473 :           expr = maybe_constant_value (expr, NULL_TREE, mce_true);
    7841    185310473 :           expr = convert_from_reference (expr);
    7842              :           /* EXPR may have become value-dependent.  */
    7843    185310473 :           val_dep_p = value_dependent_expression_p (expr);
    7844              :         }
    7845           84 :       else if (TYPE_PTR_OR_PTRMEM_P (type)
    7846          344 :                || NULLPTR_TYPE_P (type))
    7847              :         {
    7848          234 :           tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
    7849          266 :           if ((TYPE_PTR_P (type) || NULLPTR_TYPE_P (type))
    7850          240 :               ? integer_zerop (folded)
    7851           26 :               : null_member_pointer_value_p (folded))
    7852    185353545 :             expr = folded;
    7853              :         }
    7854              :     }
    7855              : 
    7856    185353545 :   if (TYPE_REF_P (type))
    7857          862 :     expr = mark_lvalue_use (expr);
    7858              :   else
    7859    185352683 :     expr = mark_rvalue_use (expr);
    7860              : 
    7861              :   /* HACK: Due to double coercion, we can get a
    7862              :      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
    7863              :      which is the tree that we built on the first call (see
    7864              :      below when coercing to reference to object or to reference to
    7865              :      function). We just strip everything and get to the arg.
    7866              :      See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
    7867              :      for examples.  */
    7868    185353545 :   if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
    7869              :     {
    7870              :       /* Check this before we strip *& to avoid redundancy.  */
    7871          862 :       if (!mark_single_function (expr, complain))
    7872            0 :         return error_mark_node;
    7873              : 
    7874          862 :       tree probe_type, probe = expr;
    7875          862 :       if (REFERENCE_REF_P (probe))
    7876          780 :         probe = TREE_OPERAND (probe, 0);
    7877          862 :       probe_type = TREE_TYPE (probe);
    7878          862 :       if (TREE_CODE (probe) == NOP_EXPR)
    7879              :         {
    7880              :           /* ??? Maybe we could use convert_from_reference here, but we
    7881              :              would need to relax its constraints because the NOP_EXPR
    7882              :              could actually change the type to something more cv-qualified,
    7883              :              and this is not folded by convert_from_reference.  */
    7884          730 :           tree addr = TREE_OPERAND (probe, 0);
    7885          730 :           if (TYPE_REF_P (probe_type)
    7886          730 :               && TREE_CODE (addr) == ADDR_EXPR
    7887          728 :               && TYPE_PTR_P (TREE_TYPE (addr))
    7888         1458 :               && (same_type_ignoring_top_level_qualifiers_p
    7889          728 :                   (TREE_TYPE (probe_type),
    7890          728 :                    TREE_TYPE (TREE_TYPE (addr)))))
    7891              :             {
    7892          718 :               expr = TREE_OPERAND (addr, 0);
    7893          718 :               expr_type = TREE_TYPE (probe_type);
    7894              :             }
    7895              :         }
    7896              :     }
    7897              : 
    7898              :   /* [temp.arg.nontype]/5, bullet 1
    7899              : 
    7900              :      For a non-type template-parameter of integral or enumeration type,
    7901              :      integral promotions (_conv.prom_) and integral conversions
    7902              :      (_conv.integral_) are applied.  */
    7903    185353545 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7904        77401 :       || SCALAR_FLOAT_TYPE_P (type))
    7905              :     {
    7906    185276704 :       if (cxx_dialect < cxx11)
    7907              :         {
    7908        40388 :           tree t = build_converted_constant_expr (type, expr, complain);
    7909        40388 :           t = maybe_constant_value (t);
    7910        40388 :           if (t != error_mark_node)
    7911    185276704 :             expr = t;
    7912              :         }
    7913              : 
    7914    185276704 :       if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
    7915           11 :         return error_mark_node;
    7916              : 
    7917              :       /* Notice that there are constant expressions like '4 % 0' which
    7918              :          do not fold into integer constants.  */
    7919    185276693 :       if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
    7920              :         {
    7921          202 :           if (complain & tf_error)
    7922              :             {
    7923           86 :               int errs = errorcount, warns = warningcount + werrorcount;
    7924           86 :               if (!require_potential_constant_expression (expr))
    7925           19 :                 expr = error_mark_node;
    7926              :               else
    7927           67 :                 expr = cxx_constant_value (expr);
    7928           86 :               if (errorcount > errs || warningcount + werrorcount > warns)
    7929           57 :                 inform (loc, "in template argument for type %qT", type);
    7930           86 :               if (expr == error_mark_node)
    7931              :                 return NULL_TREE;
    7932              :               /* else cxx_constant_value complained but gave us
    7933              :                  a real constant, so go ahead.  */
    7934            3 :               if (!CONSTANT_CLASS_P (expr))
    7935              :                 {
    7936              :                   /* Some assemble time constant expressions like
    7937              :                      (intptr_t)&&lab1 - (intptr_t)&&lab2 or
    7938              :                      4 + (intptr_t)&&var satisfy reduced_constant_expression_p
    7939              :                      as we can emit them into .rodata initializers of
    7940              :                      variables, yet they can't fold into an INTEGER_CST at
    7941              :                      compile time.  Refuse them here.  */
    7942            0 :                   gcc_checking_assert (reduced_constant_expression_p (expr));
    7943            0 :                   error_at (loc, "template argument %qE for type %qT not "
    7944              :                                  "a compile-time constant", expr, type);
    7945            0 :                   return NULL_TREE;
    7946              :                 }
    7947              :             }
    7948              :           else
    7949              :             return NULL_TREE;
    7950              :         }
    7951              : 
    7952              :       /* Avoid typedef problems.  */
    7953    185276494 :       if (TREE_TYPE (expr) != type)
    7954      1300728 :         expr = fold_convert (type, expr);
    7955              :     }
    7956              :   /* [temp.arg.nontype]/5, bullet 2
    7957              : 
    7958              :      For a non-type template-parameter of type pointer to object,
    7959              :      qualification conversions (_conv.qual_) and the array-to-pointer
    7960              :      conversion (_conv.array_) are applied.  */
    7961        76841 :   else if (TYPE_PTROBV_P (type))
    7962              :     {
    7963         2897 :       tree decayed = expr;
    7964              : 
    7965              :       /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
    7966              :          decay_conversion or an explicit cast.  If it's a problematic cast,
    7967              :          we'll complain about it below.  */
    7968         2897 :       if (TREE_CODE (expr) == NOP_EXPR)
    7969              :         {
    7970          264 :           tree probe = expr;
    7971          264 :           STRIP_NOPS (probe);
    7972          264 :           if (TREE_CODE (probe) == ADDR_EXPR
    7973          264 :               && TYPE_PTR_P (TREE_TYPE (probe)))
    7974              :             {
    7975          261 :               expr = probe;
    7976          261 :               expr_type = TREE_TYPE (expr);
    7977              :             }
    7978              :         }
    7979              : 
    7980              :       /* [temp.arg.nontype]/1  (TC1 version, DR 49):
    7981              : 
    7982              :          A template-argument for a non-type, non-template template-parameter
    7983              :          shall be one of: [...]
    7984              : 
    7985              :          -- the name of a non-type template-parameter;
    7986              :          -- the address of an object or function with external linkage, [...]
    7987              :             expressed as "& id-expression" where the & is optional if the name
    7988              :             refers to a function or array, or if the corresponding
    7989              :             template-parameter is a reference.
    7990              : 
    7991              :         Here, we do not care about functions, as they are invalid anyway
    7992              :         for a parameter of type pointer-to-object.  */
    7993              : 
    7994         2897 :       if (val_dep_p)
    7995              :         /* Non-type template parameters are OK.  */
    7996              :         ;
    7997         2826 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7998              :         /* Null pointer values are OK in C++11.  */;
    7999         2726 :       else if (TREE_CODE (expr) != ADDR_EXPR
    8000           42 :                && !INDIRECT_TYPE_P (expr_type))
    8001              :         /* Other values, like integer constants, might be valid
    8002              :            non-type arguments of some other type.  */
    8003           25 :         return error_mark_node;
    8004         2701 :       else if (invalid_tparm_referent_p (type, expr, complain))
    8005              :         return NULL_TREE;
    8006              : 
    8007         2769 :       expr = decayed;
    8008              : 
    8009         2769 :       expr = perform_qualification_conversions (type, expr);
    8010         2769 :       if (expr == error_mark_node)
    8011              :         return error_mark_node;
    8012              :     }
    8013              :   /* [temp.arg.nontype]/5, bullet 3
    8014              : 
    8015              :      For a non-type template-parameter of type reference to object, no
    8016              :      conversions apply. The type referred to by the reference may be more
    8017              :      cv-qualified than the (otherwise identical) type of the
    8018              :      template-argument. The template-parameter is bound directly to the
    8019              :      template-argument, which must be an lvalue.  */
    8020        73944 :   else if (TYPE_REF_OBJ_P (type))
    8021              :     {
    8022          705 :       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
    8023              :                                                       expr_type))
    8024            0 :         return error_mark_node;
    8025              : 
    8026          705 :       if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
    8027              :         {
    8028            2 :           if (complain & tf_error)
    8029            1 :             error ("%qE is not a valid template argument for type %qT "
    8030              :                    "because of conflicts in cv-qualification", expr, type);
    8031              :           return NULL_TREE;
    8032              :         }
    8033              : 
    8034          703 :       if (!lvalue_p (expr))
    8035              :         {
    8036           12 :           if (complain & tf_error)
    8037            8 :             error ("%qE is not a valid template argument for type %qT "
    8038              :                    "because it is not an lvalue", expr, type);
    8039              :           return NULL_TREE;
    8040              :         }
    8041              : 
    8042              :       /* [temp.arg.nontype]/1
    8043              : 
    8044              :          A template-argument for a non-type, non-template template-parameter
    8045              :          shall be one of: [...]
    8046              : 
    8047              :          -- the address of an object or function with external linkage.  */
    8048          691 :       if (INDIRECT_REF_P (expr)
    8049          691 :           && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
    8050              :         {
    8051           39 :           expr = TREE_OPERAND (expr, 0);
    8052           39 :           if (DECL_P (expr))
    8053              :             {
    8054            7 :               if (complain & tf_error)
    8055            3 :                 error ("%q#D is not a valid template argument for type %qT "
    8056              :                        "because a reference variable does not have a constant "
    8057              :                        "address", expr, type);
    8058              :               return NULL_TREE;
    8059              :             }
    8060              :         }
    8061              : 
    8062          684 :       if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
    8063              :         /* OK, dependent reference.  We don't want to ask whether a DECL is
    8064              :            itself value-dependent, since what we want here is its address.  */;
    8065              :       else
    8066              :         {
    8067          652 :           expr = build_address (expr);
    8068              : 
    8069          652 :           if (invalid_tparm_referent_p (type, expr, complain))
    8070              :             return NULL_TREE;
    8071              :         }
    8072              : 
    8073          672 :       if (!same_type_p (type, TREE_TYPE (expr)))
    8074          640 :         expr = build_nop (type, expr);
    8075              :     }
    8076              :   /* [temp.arg.nontype]/5, bullet 4
    8077              : 
    8078              :      For a non-type template-parameter of type pointer to function, only
    8079              :      the function-to-pointer conversion (_conv.func_) is applied. If the
    8080              :      template-argument represents a set of overloaded functions (or a
    8081              :      pointer to such), the matching function is selected from the set
    8082              :      (_over.over_).  */
    8083        73239 :   else if (TYPE_PTRFN_P (type))
    8084              :     {
    8085              :       /* If the argument is a template-id, we might not have enough
    8086              :          context information to decay the pointer.  */
    8087         1761 :       if (!type_unknown_p (expr_type))
    8088              :         {
    8089         1499 :           expr = decay_conversion (expr, complain);
    8090         1499 :           if (expr == error_mark_node)
    8091              :             return error_mark_node;
    8092              :         }
    8093              : 
    8094         1757 :       if (cxx_dialect >= cxx11 && integer_zerop (expr))
    8095              :         /* Null pointer values are OK in C++11.  */
    8096           87 :         return perform_qualification_conversions (type, expr);
    8097              : 
    8098         1670 :       expr = convert_nontype_argument_function (type, expr, complain);
    8099         1670 :       if (!expr || expr == error_mark_node)
    8100              :         return expr;
    8101              :     }
    8102              :   /* [temp.arg.nontype]/5, bullet 5
    8103              : 
    8104              :      For a non-type template-parameter of type reference to function, no
    8105              :      conversions apply. If the template-argument represents a set of
    8106              :      overloaded functions, the matching function is selected from the set
    8107              :      (_over.over_).  */
    8108        71478 :   else if (TYPE_REFFN_P (type))
    8109              :     {
    8110          157 :       if (TREE_CODE (expr) == ADDR_EXPR)
    8111              :         {
    8112            0 :           if (complain & tf_error)
    8113              :             {
    8114            0 :               auto_diagnostic_group d;
    8115            0 :               error ("%qE is not a valid template argument for type %qT "
    8116              :                      "because it is a pointer", expr, type);
    8117            0 :               inform (input_location, "try using %qE instead",
    8118            0 :                       TREE_OPERAND (expr, 0));
    8119            0 :             }
    8120              :           return NULL_TREE;
    8121              :         }
    8122              : 
    8123          157 :       expr = convert_nontype_argument_function (type, expr, complain);
    8124          157 :       if (!expr || expr == error_mark_node)
    8125              :         return expr;
    8126              :     }
    8127              :   /* [temp.arg.nontype]/5, bullet 6
    8128              : 
    8129              :      For a non-type template-parameter of type pointer to member function,
    8130              :      no conversions apply. If the template-argument represents a set of
    8131              :      overloaded member functions, the matching member function is selected
    8132              :      from the set (_over.over_).  */
    8133        71321 :   else if (TYPE_PTRMEMFUNC_P (type))
    8134              :     {
    8135         1473 :       expr = instantiate_type (type, expr, tf_none);
    8136         1473 :       if (expr == error_mark_node)
    8137              :         return error_mark_node;
    8138              : 
    8139              :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    8140              :          expression must be a pointer-to-member constant.  */
    8141         1444 :       if (!val_dep_p
    8142         1444 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    8143              :         return NULL_TREE;
    8144              : 
    8145              :       /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
    8146              :          into a CONSTRUCTOR, so build up a new PTRMEM_CST instead.  */
    8147         1427 :       if (fnptr_conv_p (type, TREE_TYPE (expr)))
    8148            3 :         expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
    8149              :     }
    8150              :   /* [temp.arg.nontype]/5, bullet 7
    8151              : 
    8152              :      For a non-type template-parameter of type pointer to data member,
    8153              :      qualification conversions (_conv.qual_) are applied.  */
    8154        69848 :   else if (TYPE_PTRDATAMEM_P (type))
    8155              :     {
    8156              :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    8157              :          expression must be a pointer-to-member constant.  */
    8158          990 :       if (!val_dep_p
    8159          990 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    8160              :         return NULL_TREE;
    8161              : 
    8162          970 :       expr = perform_qualification_conversions (type, expr);
    8163          970 :       if (expr == error_mark_node)
    8164              :         return expr;
    8165              :     }
    8166        68858 :   else if (NULLPTR_TYPE_P (type))
    8167              :     {
    8168           36 :       if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
    8169              :         {
    8170            0 :           if (complain & tf_error)
    8171            0 :             error ("%qE is not a valid template argument for type %qT "
    8172            0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    8173              :           return NULL_TREE;
    8174              :         }
    8175           36 :       if (!integer_zerop (expr) && !val_dep_p)
    8176              :         {
    8177            4 :           if (complain & tf_error)
    8178              :             {
    8179            2 :               expr = cxx_constant_value (expr);
    8180            2 :               if (expr == error_mark_node)
    8181              :                 return NULL_TREE;
    8182            0 :               gcc_assert (integer_zerop (expr));
    8183              :             }
    8184              :           else
    8185              :             return NULL_TREE;
    8186              :         }
    8187              :       return expr;
    8188              :     }
    8189        68822 :   else if (CLASS_TYPE_P (type))
    8190              :     {
    8191              :       /* Replace the argument with a reference to the corresponding template
    8192              :          parameter object.  */
    8193        67236 :       if (!val_dep_p)
    8194        66846 :         expr = create_template_parm_object (expr, complain);
    8195        67236 :       if (expr == error_mark_node)
    8196              :         return NULL_TREE;
    8197              :     }
    8198         1586 :   else if (REFLECTION_TYPE_P (type))
    8199              :     {
    8200         1586 :       if (!REFLECTION_TYPE_P (TREE_TYPE (expr)))
    8201              :         {
    8202            0 :           if (complain & tf_error)
    8203            0 :             error ("%qE is not a valid template argument for type %qT "
    8204            0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    8205              :           return NULL_TREE;
    8206              :         }
    8207         1586 :       if (!REFLECT_EXPR_P (expr) && !val_dep_p)
    8208              :         {
    8209            8 :           if (complain & tf_error)
    8210              :             {
    8211            4 :               expr = cxx_constant_value (expr);
    8212            4 :               if (expr == error_mark_node)
    8213              :                 return NULL_TREE;
    8214            0 :               gcc_assert (REFLECT_EXPR_P (expr));
    8215              :             }
    8216              :           else
    8217              :             return NULL_TREE;
    8218              :         }
    8219              :       return expr;
    8220              :     }
    8221              :   /* A template non-type parameter must be one of the above.  */
    8222              :   else
    8223            0 :     gcc_unreachable ();
    8224              : 
    8225              :   /* Sanity check: did we actually convert the argument to the
    8226              :      right type?  */
    8227    185351294 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p
    8228              :               (type, TREE_TYPE (expr)));
    8229    185351294 :   return convert_from_reference (expr);
    8230    189267153 : }
    8231              : 
    8232              : /* Subroutine of coerce_template_template_parms, which returns true if
    8233              :    PARM and ARG match using the rule for the template parameters of
    8234              :    template template parameters.  Both PARM and ARG are template parameters;
    8235              :    the rest of the arguments are the same as for
    8236              :    coerce_template_template_parms.  */
    8237              : 
    8238              : static bool
    8239      1494757 : coerce_template_template_parm (tree parm, tree arg, tsubst_flags_t complain,
    8240              :                                tree in_decl, tree outer_args)
    8241              : {
    8242      1494757 :   if (arg == NULL_TREE || error_operand_p (arg)
    8243      2989508 :       || parm == NULL_TREE || error_operand_p (parm))
    8244              :     return false;
    8245              : 
    8246      1494751 :   if (TREE_CODE (arg) != TREE_CODE (parm))
    8247              :     return false;
    8248              : 
    8249      1494404 :   switch (TREE_CODE (parm))
    8250              :     {
    8251           13 :     case TEMPLATE_DECL:
    8252              :       /* We encounter instantiations of templates like
    8253              :          template <template <template <class> class> class TT>
    8254              :          class C;  */
    8255           13 :       {
    8256           13 :         if (!coerce_template_template_parms
    8257           13 :             (parm, arg, complain, in_decl, outer_args))
    8258              :           return false;
    8259              :       }
    8260              :       /* Fall through.  */
    8261              : 
    8262      1488954 :     case TYPE_DECL:
    8263      1488954 :       if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
    8264      1488954 :           && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8265              :         /* Argument is a parameter pack but parameter is not.  */
    8266            7 :         return false;
    8267              :       break;
    8268              : 
    8269         5450 :     case PARM_DECL:
    8270              :       /* The tsubst call is used to handle cases such as
    8271              : 
    8272              :            template <int> class C {};
    8273              :            template <class T, template <T> class TT> class D {};
    8274              :            D<int, C> d;
    8275              : 
    8276              :          i.e. the parameter list of TT depends on earlier parameters.  */
    8277         5450 :       if (!uses_template_parms (TREE_TYPE (arg)))
    8278              :         {
    8279         5421 :           ++processing_template_decl;
    8280         5421 :           tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
    8281         5421 :           --processing_template_decl;
    8282         5421 :           if (!uses_template_parms (t)
    8283         5421 :               && !same_type_p (t, TREE_TYPE (arg)))
    8284              :             return false;
    8285              :         }
    8286              : 
    8287         5426 :       if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
    8288         5426 :           && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8289              :         /* Argument is a parameter pack but parameter is not.  */
    8290            3 :         return false;
    8291              : 
    8292              :       break;
    8293              : 
    8294            0 :     default:
    8295            0 :       gcc_unreachable ();
    8296              :     }
    8297              : 
    8298              :   return true;
    8299              : }
    8300              : 
    8301              : /* Coerce template argument list ARGLIST for use with template
    8302              :    template-parameter TEMPL.  */
    8303              : 
    8304              : static tree
    8305       374515 : coerce_template_args_for_ttp (tree templ, tree arglist,
    8306              :                               tsubst_flags_t complain)
    8307              : {
    8308              :   /* Consider an example where a template template parameter declared as
    8309              : 
    8310              :      template <class T, class U = std::allocator<T> > class TT
    8311              : 
    8312              :      The template parameter level of T and U are one level larger than
    8313              :      of TT.  To proper process the default argument of U, say when an
    8314              :      instantiation `TT<int>' is seen, we need to build the full
    8315              :      arguments containing {int} as the innermost level.  Outer levels,
    8316              :      available when not appearing as default template argument, can be
    8317              :      obtained from the arguments of the enclosing template.
    8318              : 
    8319              :      Suppose that TT is later substituted with std::vector.  The above
    8320              :      instantiation is `TT<int, std::allocator<T> >' with TT at
    8321              :      level 1, and T at level 2, while the template arguments at level 1
    8322              :      becomes {std::vector} and the inner level 2 is {int}.  */
    8323              : 
    8324       374515 :   tree outer = DECL_CONTEXT (templ);
    8325       374515 :   if (outer)
    8326       185168 :     outer = generic_targs_for (outer);
    8327       189347 :   else if (current_template_parms)
    8328              :     {
    8329              :       /* This is an argument of the current template, so we haven't set
    8330              :          DECL_CONTEXT yet.  We can also get here when level-lowering a
    8331              :          bound ttp. */
    8332              :       tree relevant_template_parms;
    8333              : 
    8334              :       /* Parameter levels that are greater than the level of the given
    8335              :          template template parm are irrelevant.  */
    8336              :       relevant_template_parms = current_template_parms;
    8337       183284 :       while (TMPL_PARMS_DEPTH (relevant_template_parms)
    8338       366568 :              != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
    8339            3 :         relevant_template_parms = TREE_CHAIN (relevant_template_parms);
    8340              : 
    8341       183281 :       outer = template_parms_to_args (relevant_template_parms);
    8342              :     }
    8343              : 
    8344       374515 :   if (outer)
    8345       368449 :     arglist = add_to_template_args (outer, arglist);
    8346              : 
    8347       374515 :   tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
    8348       374515 :   return coerce_template_parms (parmlist, arglist, templ, complain);
    8349              : }
    8350              : 
    8351              : /* A cache of template template parameters with match-all default
    8352              :    arguments.  */
    8353              : static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
    8354              : 
    8355              : /* T is a bound template template-parameter.  Copy its arguments into default
    8356              :    arguments of the template template-parameter's template parameters.  */
    8357              : 
    8358              : static tree
    8359           48 : add_defaults_to_ttp (tree otmpl)
    8360              : {
    8361           82 :   if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
    8362           18 :     return *c;
    8363              : 
    8364           30 :   tree ntmpl = copy_node (otmpl);
    8365              : 
    8366           30 :   tree ntype = copy_node (TREE_TYPE (otmpl));
    8367           30 :   TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
    8368           30 :   TYPE_MAIN_VARIANT (ntype) = ntype;
    8369           30 :   TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
    8370           30 :   TYPE_NAME (ntype) = ntmpl;
    8371           30 :   SET_TYPE_STRUCTURAL_EQUALITY (ntype);
    8372              : 
    8373           30 :   tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
    8374           30 :     = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
    8375           30 :   TEMPLATE_PARM_DECL (idx) = ntmpl;
    8376           30 :   TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
    8377              : 
    8378           30 :   tree oparms = DECL_TEMPLATE_PARMS (otmpl);
    8379           30 :   tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
    8380           30 :   TREE_CHAIN (parms) = TREE_CHAIN (oparms);
    8381           30 :   tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
    8382           68 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    8383              :     {
    8384           38 :       tree o = TREE_VEC_ELT (vec, i);
    8385           38 :       if (!template_parameter_pack_p (TREE_VALUE (o)))
    8386              :         {
    8387           34 :           tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
    8388           34 :           TREE_PURPOSE (n) = any_targ_node;
    8389              :         }
    8390              :     }
    8391              : 
    8392           30 :   tree oresult = DECL_TEMPLATE_RESULT (otmpl);
    8393           30 :   tree gen_otmpl = DECL_TI_TEMPLATE (oresult);
    8394           30 :   tree gen_ntmpl;
    8395           30 :   if (gen_otmpl == otmpl)
    8396              :     gen_ntmpl = ntmpl;
    8397              :   else
    8398            0 :     gen_ntmpl = add_defaults_to_ttp (gen_otmpl);
    8399              : 
    8400           30 :   tree nresult = copy_decl (oresult);
    8401           30 :   DECL_TEMPLATE_INFO (nresult)
    8402           30 :     = build_template_info (gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult)));
    8403           30 :   DECL_TEMPLATE_RESULT (ntmpl) = nresult;
    8404              : 
    8405           30 :   hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
    8406           30 :   return ntmpl;
    8407              : }
    8408              : 
    8409              : /* ARG is a bound potential template template-argument, and PARGS is a list
    8410              :    of arguments for the corresponding template template-parameter.  Adjust
    8411              :    PARGS as appropriate for application to ARG's template, and if ARG is a
    8412              :    BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
    8413              :    arguments to the template template parameter.  */
    8414              : 
    8415              : static tree
    8416       177576 : coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
    8417              : {
    8418       177576 :   ++processing_template_decl;
    8419       177576 :   tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
    8420       177576 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8421              :     {
    8422              :       /* When comparing two template template-parameters in partial ordering,
    8423              :          rewrite the one currently being used as an argument to have default
    8424              :          arguments for all parameters.  */
    8425           48 :       arg_tmpl = add_defaults_to_ttp (arg_tmpl);
    8426           48 :       pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
    8427           48 :       if (pargs != error_mark_node)
    8428           44 :         arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
    8429           88 :                                            TYPE_TI_ARGS (arg));
    8430              :     }
    8431              :   else
    8432              :     {
    8433       177528 :       tree aparms
    8434       177528 :         = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
    8435       177528 :       pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
    8436              :     }
    8437       177576 :   --processing_template_decl;
    8438       177576 :   return pargs;
    8439              : }
    8440              : 
    8441              : /* Subroutine of unify for the case when PARM is a
    8442              :    BOUND_TEMPLATE_TEMPLATE_PARM.  */
    8443              : 
    8444              : static int
    8445       177774 : unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
    8446              :                       bool explain_p)
    8447              : {
    8448       177774 :   tree parmvec = TYPE_TI_ARGS (parm);
    8449       355548 :   tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
    8450              : 
    8451              :   /* The template template parm might be variadic and the argument
    8452              :      not, so flatten both argument lists.  */
    8453       177774 :   parmvec = expand_template_argument_pack (parmvec);
    8454       177774 :   argvec = expand_template_argument_pack (argvec);
    8455              : 
    8456       177774 :   if (flag_new_ttp)
    8457              :     {
    8458              :       /* In keeping with P0522R0, adjust P's template arguments
    8459              :          to apply to A's template; then flatten it again.  */
    8460       177576 :       tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
    8461       177576 :       nparmvec = expand_template_argument_pack (nparmvec);
    8462              : 
    8463       177576 :       if (unify (tparms, targs, nparmvec, argvec,
    8464              :                  UNIFY_ALLOW_NONE, explain_p))
    8465              :         return 1;
    8466              : 
    8467              :       /* If the P0522 adjustment eliminated a pack expansion, deduce
    8468              :          empty packs.  */
    8469       177300 :       if (flag_new_ttp
    8470       177300 :           && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
    8471       177300 :           && unify_pack_expansion (tparms, targs, parmvec, argvec,
    8472              :                                    DEDUCE_EXACT, /*sub*/true, explain_p))
    8473            0 :         return 1;
    8474              :     }
    8475              :   else
    8476              :     {
    8477              :       /* Deduce arguments T, i from TT<T> or TT<i>.
    8478              :          We check each element of PARMVEC and ARGVEC individually
    8479              :          rather than the whole TREE_VEC since they can have
    8480              :          different number of elements, which is allowed under N2555.  */
    8481              : 
    8482          198 :       int len = TREE_VEC_LENGTH (parmvec);
    8483              : 
    8484              :       /* Check if the parameters end in a pack, making them
    8485              :          variadic.  */
    8486          198 :       int parm_variadic_p = 0;
    8487          198 :       if (len > 0
    8488          198 :           && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
    8489              :         parm_variadic_p = 1;
    8490              : 
    8491          400 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8492              :         /* If the template argument list of P contains a pack
    8493              :            expansion that is not the last template argument, the
    8494              :            entire template argument list is a non-deduced
    8495              :            context.  */
    8496          204 :         if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
    8497              :           return unify_success (explain_p);
    8498              : 
    8499          196 :       if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
    8500            2 :         return unify_too_few_arguments (explain_p,
    8501            2 :                                         TREE_VEC_LENGTH (argvec), len);
    8502              : 
    8503          377 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8504          198 :         if (unify (tparms, targs,
    8505          198 :                    TREE_VEC_ELT (parmvec, i),
    8506          198 :                    TREE_VEC_ELT (argvec, i),
    8507              :                    UNIFY_ALLOW_NONE, explain_p))
    8508              :           return 1;
    8509              : 
    8510          179 :       if (parm_variadic_p
    8511          179 :           && unify_pack_expansion (tparms, targs,
    8512              :                                    parmvec, argvec,
    8513              :                                    DEDUCE_EXACT,
    8514              :                                    /*subr=*/true, explain_p))
    8515            2 :         return 1;
    8516              :     }
    8517              : 
    8518              :   return 0;
    8519              : }
    8520              : 
    8521              : /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
    8522              :    template template parameters.
    8523              : 
    8524              :    Consider the example:
    8525              :      template <class T> class A;
    8526              :      template<template <class U> class TT> class B;
    8527              : 
    8528              :    For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
    8529              :    and OUTER_ARGS contains A.  */
    8530              : 
    8531              : static int
    8532     10482953 : coerce_template_template_parms (tree parm_tmpl,
    8533              :                                 tree arg_tmpl,
    8534              :                                 tsubst_flags_t complain,
    8535              :                                 tree in_decl,
    8536              :                                 tree outer_args)
    8537              : {
    8538     10482953 :   int nparms, nargs, i;
    8539     10482953 :   tree parm, arg;
    8540     10482953 :   int variadic_p = 0;
    8541              : 
    8542     10482953 :   tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl);
    8543     10482953 :   tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl);
    8544     10482953 :   tree gen_arg_tmpl = most_general_template (arg_tmpl);
    8545     10482953 :   tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl);
    8546              : 
    8547     10482953 :   nparms = TREE_VEC_LENGTH (parm_parms);
    8548     10482953 :   nargs = TREE_VEC_LENGTH (arg_parms);
    8549              : 
    8550     10482953 :   if (flag_new_ttp)
    8551              :     {
    8552              :       /* P0522R0: A template template-parameter P is at least as specialized as
    8553              :          a template template-argument A if, given the following rewrite to two
    8554              :          function templates, the function template corresponding to P is at
    8555              :          least as specialized as the function template corresponding to A
    8556              :          according to the partial ordering rules for function templates
    8557              :          ([temp.func.order]). Given an invented class template X with the
    8558              :          template parameter list of A (including default arguments):
    8559              : 
    8560              :          * Each of the two function templates has the same template parameters,
    8561              :          respectively, as P or A.
    8562              : 
    8563              :          * Each function template has a single function parameter whose type is
    8564              :          a specialization of X with template arguments corresponding to the
    8565              :          template parameters from the respective function template where, for
    8566              :          each template parameter PP in the template parameter list of the
    8567              :          function template, a corresponding template argument AA is formed. If
    8568              :          PP declares a parameter pack, then AA is the pack expansion
    8569              :          PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
    8570              : 
    8571              :          If the rewrite produces an invalid type, then P is not at least as
    8572              :          specialized as A.  */
    8573              : 
    8574              :       /* So coerce P's args to apply to A's parms, and then deduce between A's
    8575              :          args and the converted args.  If that succeeds, A is at least as
    8576              :          specialized as P, so they match.*/
    8577     10456915 :       processing_template_decl_sentinel ptds (/*reset*/false);
    8578     10456915 :       ++processing_template_decl;
    8579              : 
    8580     10456915 :       tree pargs = template_parms_level_to_args (parm_parms);
    8581              : 
    8582              :       /* PARM and ARG might be at different template depths, and we want to
    8583              :          pass the right additional levels of args when coercing PARGS to
    8584              :          ARG_PARMS in case we need to do any substitution into non-type
    8585              :          template parameter types.
    8586              : 
    8587              :          OUTER_ARGS are not the right outer levels in this case, as they are
    8588              :          the args we're building up for PARM, and for the coercion we want the
    8589              :          args for ARG.  If DECL_CONTEXT isn't set for a template template
    8590              :          parameter, we can assume that it's in the current scope.  */
    8591     10456915 :       tree ctx = DECL_CONTEXT (arg_tmpl);
    8592     10456915 :       if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8593       478393 :         ctx = current_scope ();
    8594     10456915 :       tree scope_args = NULL_TREE;
    8595     10456915 :       if (tree tinfo = get_template_info (ctx))
    8596       759216 :         scope_args = TI_ARGS (tinfo);
    8597     10456915 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8598              :         {
    8599       557170 :           int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl));
    8600       644303 :           int scope_depth = TMPL_ARGS_DEPTH (scope_args);
    8601       557170 :           tree full_pargs = make_tree_vec (level + 1);
    8602              : 
    8603              :         /* Only use as many levels from the scope as needed
    8604              :            (excluding the level of ARG).  */
    8605      1135998 :           for (int i = 0; i < level - 1; ++i)
    8606        21658 :             if (i < scope_depth)
    8607        42354 :               TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1);
    8608              :             else
    8609          481 :               TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0);
    8610              : 
    8611              :           /* Add the arguments that appear at the levels of ARG.  */
    8612       557170 :           tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl));
    8613      1114340 :           adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1);
    8614       557170 :           TREE_VEC_ELT (full_pargs, level - 1) = adjacent;
    8615              : 
    8616       557170 :           TREE_VEC_ELT (full_pargs, level) = pargs;
    8617       557170 :           pargs = full_pargs;
    8618              :         }
    8619              :       else
    8620      9899745 :         pargs = add_to_template_args (scope_args, pargs);
    8621              : 
    8622     10456915 :       pargs = coerce_template_parms (gen_arg_parms, pargs,
    8623              :                                      NULL_TREE, tf_none);
    8624     10456915 :       if (pargs != error_mark_node)
    8625              :         {
    8626     10455886 :           tree targs = make_tree_vec (nargs);
    8627     10455886 :           tree aargs = template_parms_level_to_args (arg_parms);
    8628     10455886 :           if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
    8629              :                       /*explain*/false))
    8630      9524406 :             return 1;
    8631              :         }
    8632     10456915 :     }
    8633              : 
    8634              :   /* Determine whether we have a parameter pack at the end of the
    8635              :      template template parameter's template parameter list.  */
    8636       958547 :   if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
    8637              :     {
    8638       958547 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
    8639              : 
    8640       958547 :       if (error_operand_p (parm))
    8641              :         return 0;
    8642              : 
    8643       958541 :       switch (TREE_CODE (parm))
    8644              :         {
    8645       952117 :         case TEMPLATE_DECL:
    8646       952117 :         case TYPE_DECL:
    8647       952117 :           if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8648       938958 :             variadic_p = 1;
    8649              :           break;
    8650              : 
    8651         6424 :         case PARM_DECL:
    8652         6424 :           if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8653       938958 :             variadic_p = 1;
    8654              :           break;
    8655              : 
    8656            0 :         default:
    8657            0 :           gcc_unreachable ();
    8658              :         }
    8659              :     }
    8660              : 
    8661       958541 :   if (nargs != nparms
    8662       466348 :       && !(variadic_p && nargs >= nparms - 1))
    8663              :     return 0;
    8664              : 
    8665              :   /* Check all of the template parameters except the parameter pack at
    8666              :      the end (if any).  */
    8667      1124729 :   for (i = 0; i < nparms - variadic_p; ++i)
    8668              :     {
    8669       167278 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
    8670       167278 :           || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8671            0 :         continue;
    8672              : 
    8673       167278 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8674       167278 :       arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8675              : 
    8676       167278 :       if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8677              :                                           outer_args))
    8678              :         return 0;
    8679              : 
    8680              :     }
    8681              : 
    8682       957451 :   if (variadic_p)
    8683              :     {
    8684              :       /* Check each of the template parameters in the template
    8685              :          argument against the template parameter pack at the end of
    8686              :          the template template parameter.  */
    8687       938954 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
    8688              :         return 0;
    8689              : 
    8690       938954 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8691              : 
    8692      2266409 :       for (; i < nargs; ++i)
    8693              :         {
    8694      1327479 :           if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8695            0 :             continue;
    8696              : 
    8697      1327479 :           arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8698              : 
    8699      1327479 :           if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8700              :                                               outer_args))
    8701              :             return 0;
    8702              :         }
    8703              :     }
    8704              : 
    8705              :   return 1;
    8706              : }
    8707              : 
    8708              : /* Verifies that the deduced template arguments (in TARGS) for the
    8709              :    template template parameters (in TPARMS) represent valid bindings,
    8710              :    by comparing the template parameter list of each template argument
    8711              :    to the template parameter list of its corresponding template
    8712              :    template parameter, in accordance with DR150. This
    8713              :    routine can only be called after all template arguments have been
    8714              :    deduced. It will return TRUE if all of the template template
    8715              :    parameter bindings are okay, FALSE otherwise.  */
    8716              : bool
    8717     86579902 : template_template_parm_bindings_ok_p (tree tparms, tree targs)
    8718              : {
    8719     86579902 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
    8720     86579902 :   bool ret = true;
    8721              : 
    8722              :   /* We're dealing with template parms in this process.  */
    8723     86579902 :   ++processing_template_decl;
    8724              : 
    8725     86579902 :   targs = INNERMOST_TEMPLATE_ARGS (targs);
    8726              : 
    8727    309033009 :   for (i = 0; i < ntparms; ++i)
    8728              :     {
    8729    135873241 :       tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
    8730    135873241 :       tree targ = TREE_VEC_ELT (targs, i);
    8731              : 
    8732    135873241 :       if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
    8733              :         {
    8734       797443 :           tree packed_args = NULL_TREE;
    8735       797443 :           int idx, len = 1;
    8736              : 
    8737       797443 :           if (ARGUMENT_PACK_P (targ))
    8738              :             {
    8739              :               /* Look inside the argument pack.  */
    8740          145 :               packed_args = ARGUMENT_PACK_ARGS (targ);
    8741          145 :               len = TREE_VEC_LENGTH (packed_args);
    8742              :             }
    8743              : 
    8744      1594872 :           for (idx = 0; idx < len; ++idx)
    8745              :             {
    8746       797465 :               if (packed_args)
    8747              :                 /* Extract the next argument from the argument
    8748              :                    pack.  */
    8749          167 :                 targ = TREE_VEC_ELT (packed_args, idx);
    8750              : 
    8751       797465 :               if (PACK_EXPANSION_P (targ))
    8752              :                 /* Look at the pattern of the pack expansion.  */
    8753           40 :                 targ = PACK_EXPANSION_PATTERN (targ);
    8754              : 
    8755              :               /* Extract the template parameters from the template
    8756              :                  argument.  */
    8757       797465 :               if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
    8758       134707 :                 targ = TYPE_NAME (targ);
    8759              : 
    8760              :               /* Verify that we can coerce the template template
    8761              :                  parameters from the template argument to the template
    8762              :                  parameter.  This requires an exact match.  */
    8763       797465 :               if (TREE_CODE (targ) == TEMPLATE_DECL
    8764      1594921 :                   && !coerce_template_template_parms
    8765       797456 :                        (tparm,
    8766              :                         targ,
    8767              :                         tf_none,
    8768              :                         tparm,
    8769              :                         targs))
    8770              :                 {
    8771           36 :                   ret = false;
    8772           36 :                   goto out;
    8773              :                 }
    8774              :             }
    8775              :         }
    8776              :     }
    8777              : 
    8778     86579866 :  out:
    8779              : 
    8780     86579902 :   --processing_template_decl;
    8781     86579902 :   return ret;
    8782              : }
    8783              : 
    8784              : /* Since type attributes aren't mangled, we need to strip them from
    8785              :    template type arguments.  */
    8786              : 
    8787              : tree
    8788   1615882622 : canonicalize_type_argument (tree arg, tsubst_flags_t complain)
    8789              : {
    8790   1615882622 :   if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
    8791              :     return arg;
    8792    714955367 :   bool removed_attributes = false;
    8793    714955367 :   tree canon = strip_typedefs (arg, &removed_attributes);
    8794    714955367 :   if (removed_attributes
    8795          710 :       && (complain & tf_warning))
    8796           15 :     warning (OPT_Wignored_attributes,
    8797              :              "ignoring attributes on template argument %qT", arg);
    8798              :   return canon;
    8799              : }
    8800              : 
    8801              : /* And from inside dependent non-type arguments like sizeof(Type).  */
    8802              : 
    8803              : static tree
    8804     49177210 : canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
    8805              : {
    8806     49177210 :   if (!arg || arg == error_mark_node)
    8807              :     return arg;
    8808     49177210 :   bool removed_attributes = false;
    8809     49177210 :   tree canon = strip_typedefs_expr (arg, &removed_attributes);
    8810     49177210 :   if (removed_attributes
    8811            0 :       && (complain & tf_warning))
    8812            0 :     warning (OPT_Wignored_attributes,
    8813              :              "ignoring attributes in template argument %qE", arg);
    8814              :   return canon;
    8815              : }
    8816              : 
    8817              : /* A template declaration can be substituted for a constrained
    8818              :    template template parameter only when the argument is no more
    8819              :    constrained than the parameter.  */
    8820              : 
    8821              : static bool
    8822      9684400 : is_compatible_template_arg (tree parm, tree arg, tree args)
    8823              : {
    8824      9684400 :   tree parm_cons = get_constraints (parm);
    8825              : 
    8826              :   /* For now, allow constrained template template arguments
    8827              :      and unconstrained template template parameters.  */
    8828      9684400 :   if (parm_cons == NULL_TREE)
    8829              :     return true;
    8830              : 
    8831              :   /* If the template parameter is constrained, we need to rewrite its
    8832              :      constraints in terms of the ARG's template parameters. This ensures
    8833              :      that all of the template parameter types will have the same depth.
    8834              : 
    8835              :      Note that this is only valid when coerce_template_template_parm is
    8836              :      true for the innermost template parameters of PARM and ARG. In other
    8837              :      words, because coercion is successful, this conversion will be valid.  */
    8838           87 :   tree new_args = NULL_TREE;
    8839           87 :   if (parm_cons)
    8840              :     {
    8841           87 :       tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
    8842           87 :       new_args = template_parms_level_to_args (aparms);
    8843           87 :       new_args = add_to_template_args (args, new_args);
    8844           87 :       ++processing_template_decl;
    8845           87 :       parm_cons = tsubst_constraint_info (parm_cons, new_args,
    8846              :                                           tf_none, NULL_TREE);
    8847           87 :       --processing_template_decl;
    8848           87 :       if (parm_cons == error_mark_node)
    8849              :         return false;
    8850              :     }
    8851              : 
    8852           87 :   return ttp_subsumes (parm_cons, arg);
    8853              : }
    8854              : 
    8855              : /* We can't fully resolve ARG given as a non-type template argument to TYPE,
    8856              :    because one of them is dependent.  But we need to represent the
    8857              :    conversion for the benefit of cp_tree_equal.  */
    8858              : 
    8859              : static tree
    8860     22945091 : maybe_build_nontype_implicit_conv (tree type, tree arg, bool force)
    8861              : {
    8862              :   /* Auto parms get no conversion.  */
    8863     22945091 :   if (type_uses_auto (type))
    8864              :     return arg;
    8865              :   /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
    8866              :      That would complicate other things, and it doesn't seem necessary.  */
    8867     22892039 :   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    8868              :     return arg;
    8869              :   /* We don't need or want to add this conversion now if we're going to use the
    8870              :      argument for deduction.  */
    8871     21457827 :   if (!value_dependent_expression_p (arg))
    8872              :     force = false;
    8873     21457362 :   else if (!force)
    8874              :     return arg;
    8875              : 
    8876      7180624 :   type = cv_unqualified (type);
    8877      7180624 :   tree argtype = TREE_TYPE (arg);
    8878      7180624 :   if (argtype && same_type_p (type, argtype))
    8879              :     return arg;
    8880              : 
    8881      7180588 :   arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
    8882      7180588 :   IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
    8883      7180588 :   IMPLICIT_CONV_EXPR_FORCED (arg) = force;
    8884      7180588 :   return arg;
    8885              : }
    8886              : 
    8887              : /* Convert the non-type template parameter ARG to the indicated TYPE.
    8888              :    If one of them is dependent, create an appropriate conversion.
    8889              :    FORCE_CONV is true in a forced context (i.e. alias or concept).  */
    8890              : 
    8891              : static tree
    8892    212206630 : convert_nontype_argument_maybe_dependent (tree type, tree arg,
    8893              :                                           bool force_conv,
    8894              :                                           tsubst_flags_t complain)
    8895              : {
    8896    212206630 :   if (dependent_type_p (type) || type_dependent_expression_p (arg))
    8897              :     {
    8898     22942942 :       tree val = canonicalize_expr_argument (arg, complain);
    8899     22942942 :       return maybe_build_nontype_implicit_conv (type, val, force_conv);
    8900              :     }
    8901              : 
    8902              :   /* We used to call digest_init here.  However, digest_init will report
    8903              :      errors, which we don't want when complain is zero.  More importantly,
    8904              :      digest_init will try too hard to convert things: for example,
    8905              :      `0' should not be converted to pointer type at this point according to
    8906              :      the standard.  Accepting this is not merely an extension, since
    8907              :      deciding whether or not these conversions can occur is part of
    8908              :      determining which function template to call, or whether a given
    8909              :      explicit argument specification is valid.  */
    8910    189263688 :   tree r = convert_nontype_argument (type, convert_from_reference (arg),
    8911              :                                      complain);
    8912              : 
    8913              :   /* When force_conv, we need to express this conversion with
    8914              :      IMPLICIT_CONV_EXPR rather than NOP_EXPR so we go through
    8915              :      convert_nontype_argument again at substitution time for
    8916              :      e.g. narrowing checks.  */
    8917     64853833 :   if (force_conv && r && CONVERT_EXPR_P (r)
    8918       116504 :       && !same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (arg))
    8919    189265837 :       && value_dependent_expression_p (arg))
    8920              :     {
    8921         2149 :       tree op = TREE_OPERAND (r, 0);
    8922         2149 :       gcc_assert (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (op),
    8923              :                                                              TREE_TYPE (arg)));
    8924         2149 :       r = maybe_build_nontype_implicit_conv (type, op, force_conv);
    8925              :     }
    8926              : 
    8927              :   return r;
    8928              : }
    8929              : 
    8930              : /* Convert the indicated template ARG as necessary to match the
    8931              :    indicated template PARM.  Returns the converted ARG, or
    8932              :    error_mark_node if the conversion was unsuccessful.  Error and
    8933              :    warning messages are issued under control of COMPLAIN.  This
    8934              :    conversion is for the Ith parameter in the parameter list.  ARGS is
    8935              :    the full set of template arguments deduced so far.  */
    8936              : 
    8937              : static tree
    8938   1464035269 : convert_template_argument (tree parm,
    8939              :                            tree arg,
    8940              :                            tree args,
    8941              :                            tsubst_flags_t complain,
    8942              :                            int i,
    8943              :                            tree in_decl)
    8944              : {
    8945   1464035269 :   tree orig_arg;
    8946   1464035269 :   tree val;
    8947   1464035269 :   int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
    8948              : 
    8949   1464035269 :   if (parm == error_mark_node || error_operand_p (arg))
    8950              :     return error_mark_node;
    8951              : 
    8952   1445966131 :   if (arg == any_targ_node)
    8953              :     return arg;
    8954              : 
    8955   1445966119 :   if (TREE_CODE (arg) == TREE_LIST
    8956   1445966119 :       && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
    8957              :     {
    8958              :       /* The template argument was the name of some
    8959              :          member function.  That's usually
    8960              :          invalid, but static members are OK.  In any
    8961              :          case, grab the underlying fields/functions
    8962              :          and issue an error later if required.  */
    8963            0 :       TREE_TYPE (arg) = unknown_type_node;
    8964              :     }
    8965              : 
    8966   1445966119 :   orig_arg = arg;
    8967              : 
    8968   1445966119 :   requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
    8969   2891932238 :   requires_type = (TREE_CODE (parm) == TYPE_DECL
    8970   1445966119 :                    || requires_tmpl_type);
    8971              : 
    8972              :   /* When determining whether an argument pack expansion is a template,
    8973              :      look at the pattern.  */
    8974   1445966119 :   if (PACK_EXPANSION_P (arg))
    8975     20461346 :     arg = PACK_EXPANSION_PATTERN (arg);
    8976              : 
    8977              :   /* Deal with an injected-class-name used as a template template arg.  */
    8978   1445966119 :   if (requires_tmpl_type && CLASS_TYPE_P (arg))
    8979              :     {
    8980         1739 :       tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
    8981         1739 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    8982              :         {
    8983           39 :           if (cxx_dialect >= cxx11)
    8984              :             /* OK under DR 1004.  */;
    8985            2 :           else if (complain & tf_warning_or_error)
    8986            2 :             pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
    8987            2 :                      " used as template template argument", TYPE_NAME (arg));
    8988            0 :           else if (flag_pedantic_errors)
    8989   1445966080 :             t = arg;
    8990              : 
    8991              :           arg = t;
    8992              :         }
    8993              :     }
    8994              : 
    8995   2901676328 :   is_tmpl_type =
    8996   1445966119 :     ((TREE_CODE (arg) == TEMPLATE_DECL
    8997      9479286 :       && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
    8998   1436486833 :      || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
    8999   1436486824 :      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    9000   2882246258 :      || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
    9001              : 
    9002              :   if (is_tmpl_type
    9003      9744090 :       && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    9004      9537405 :           || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
    9005       264795 :     arg = TYPE_STUB_DECL (arg);
    9006              : 
    9007   1445966119 :   is_type = TYPE_P (arg) || is_tmpl_type;
    9008              : 
    9009      3366885 :   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
    9010   1445966137 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
    9011              :     {
    9012           12 :       if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
    9013              :         {
    9014            3 :           if (complain & tf_error)
    9015            3 :             error ("invalid use of destructor %qE as a type", orig_arg);
    9016            3 :           return error_mark_node;
    9017              :         }
    9018              : 
    9019            9 :       permerror (input_location,
    9020              :                  "to refer to a type member of a template parameter, "
    9021              :                  "use %<typename %E%>", orig_arg);
    9022              : 
    9023            9 :       orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
    9024            9 :                                      TREE_OPERAND (arg, 1),
    9025              :                                      typename_type,
    9026              :                                      complain);
    9027            9 :       arg = orig_arg;
    9028            9 :       is_type = 1;
    9029              :     }
    9030   1445966116 :   if (is_type != requires_type)
    9031              :     {
    9032      3377419 :       if (in_decl)
    9033              :         {
    9034      3376970 :           if (complain & tf_error)
    9035              :             {
    9036          130 :               auto_diagnostic_group d;
    9037          130 :               error ("type/value mismatch at argument %d in template "
    9038              :                      "parameter list for %qD",
    9039              :                      i + 1, in_decl);
    9040          130 :               if (is_type)
    9041              :                 {
    9042              :                   /* The template argument is a type, but we're expecting
    9043              :                      an expression.  */
    9044           55 :                   inform (input_location,
    9045              :                           "  expected a constant of type %qT, got %qT",
    9046           40 :                           TREE_TYPE (parm),
    9047           40 :                           (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
    9048              :                   /* [temp.arg]/2: "In a template-argument, an ambiguity
    9049              :                      between a type-id and an expression is resolved to a
    9050              :                      type-id, regardless of the form of the corresponding
    9051              :                      template-parameter."  So give the user a clue.  */
    9052           40 :                   if (TREE_CODE (arg) == FUNCTION_TYPE)
    9053            6 :                     inform (input_location, "  ambiguous template argument "
    9054              :                             "for non-type template parameter is treated as "
    9055              :                             "function type");
    9056              :                 }
    9057           90 :               else if (requires_tmpl_type)
    9058            9 :                 inform (input_location,
    9059              :                         "  expected a class template, got %qE", orig_arg);
    9060              :               else
    9061           81 :                 inform (input_location,
    9062              :                         "  expected a type, got %qE", orig_arg);
    9063          130 :             }
    9064              :         }
    9065      3377419 :       return error_mark_node;
    9066              :     }
    9067   1442588697 :   if (is_tmpl_type ^ requires_tmpl_type)
    9068              :     {
    9069         2187 :       if (in_decl && (complain & tf_error))
    9070              :         {
    9071           15 :           auto_diagnostic_group d;
    9072           15 :           error ("type/value mismatch at argument %d in template "
    9073              :                  "parameter list for %qD",
    9074              :                  i + 1, in_decl);
    9075           15 :           if (is_tmpl_type)
    9076            6 :             inform (input_location,
    9077            6 :                     "  expected a type, got %qT", DECL_NAME (arg));
    9078              :           else
    9079            9 :             inform (input_location,
    9080              :                     "  expected a class template, got %qT", orig_arg);
    9081           15 :         }
    9082         2187 :       return error_mark_node;
    9083              :     }
    9084              : 
    9085   1442586510 :   if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
    9086              :     /* We already did the appropriate conversion when packing args.  */
    9087              :     val = orig_arg;
    9088   1442586471 :   else if (is_type)
    9089              :     {
    9090   1270553923 :       if (requires_tmpl_type)
    9091              :         {
    9092      9743588 :           if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
    9093              :             /* The number of argument required is not known yet.
    9094              :                Just accept it for now.  */
    9095              :             val = orig_arg;
    9096              :           else
    9097              :             {
    9098              :               /* Strip alias templates that are equivalent to another
    9099              :                  template.  */
    9100      9685484 :               arg = get_underlying_template (arg);
    9101              : 
    9102      9685484 :               if (coerce_template_template_parms (parm, arg,
    9103              :                                                   complain, in_decl,
    9104              :                                                   args))
    9105              :                 {
    9106      9684400 :                   val = arg;
    9107              : 
    9108              :                   /* TEMPLATE_TEMPLATE_PARM node is preferred over
    9109              :                      TEMPLATE_DECL.  */
    9110      9684400 :                   if (val != error_mark_node)
    9111              :                     {
    9112      9684400 :                       if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
    9113       429308 :                         val = TREE_TYPE (val);
    9114      9684400 :                       if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
    9115          107 :                         val = make_pack_expansion (val, complain);
    9116              :                     }
    9117              :                 }
    9118              :               else
    9119              :                 {
    9120         1084 :                   if (in_decl && (complain & tf_error))
    9121              :                     {
    9122           41 :                       auto_diagnostic_group d;
    9123           41 :                       error ("type/value mismatch at argument %d in "
    9124              :                              "template parameter list for %qD",
    9125              :                              i + 1, in_decl);
    9126           41 :                       inform (input_location,
    9127              :                               "  expected a template of type %qD, got %qT",
    9128              :                               parm, orig_arg);
    9129           41 :                     }
    9130              : 
    9131         1084 :                   val = error_mark_node;
    9132              :                 }
    9133              : 
    9134              :               // Check that the constraints are compatible before allowing the
    9135              :               // substitution.
    9136      9685484 :               if (val != error_mark_node)
    9137      9684400 :                 if (!is_compatible_template_arg (parm, arg, args))
    9138              :                   {
    9139           18 :                     if (in_decl && (complain & tf_error))
    9140              :                       {
    9141           15 :                         auto_diagnostic_group d;
    9142           15 :                         error ("constraint mismatch at argument %d in "
    9143              :                                "template parameter list for %qD",
    9144              :                                i + 1, in_decl);
    9145           15 :                         inform (input_location, "  expected %qD but got %qD",
    9146              :                                 parm, arg);
    9147           15 :                       }
    9148           18 :                     val = error_mark_node;
    9149              :                   }
    9150              :             }
    9151              :         }
    9152              :       else
    9153              :         val = orig_arg;
    9154              :       /* We only form one instance of each template specialization.
    9155              :          Therefore, if we use a non-canonical variant (i.e., a
    9156              :          typedef), any future messages referring to the type will use
    9157              :          the typedef, which is confusing if those future uses do not
    9158              :          themselves also use the typedef.  */
    9159   1270553923 :       if (TYPE_P (val))
    9160   1261297747 :         val = canonicalize_type_argument (val, complain);
    9161              :     }
    9162              :   else
    9163              :     {
    9164    172032548 :       tree t = TREE_TYPE (parm);
    9165              : 
    9166    344065096 :       if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
    9167    344065096 :           > TMPL_ARGS_DEPTH (args))
    9168              :         /* We don't have enough levels of args to do any substitution.  This
    9169              :            can happen in the context of -fnew-ttp-matching.  */;
    9170    172031723 :       else if (tree a = type_uses_auto (t))
    9171              :         {
    9172       187828 :           t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
    9173              :                                  LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
    9174       187828 :           if (t == error_mark_node)
    9175              :             return error_mark_node;
    9176              :         }
    9177              :       else
    9178    171843895 :         t = tsubst (t, args, complain, in_decl);
    9179              : 
    9180              :       /* Perform array-to-pointer and function-to-pointer conversion
    9181              :          as per [temp.param]/10.  */
    9182    172032530 :       t = type_decays_to (t);
    9183              : 
    9184    172032530 :       if (invalid_nontype_parm_type_p (t, complain))
    9185          108 :         return error_mark_node;
    9186              : 
    9187              :       /* Drop top-level cv-qualifiers on the substituted/deduced type of
    9188              :          this non-type template parameter, as per [temp.param]/6.  */
    9189    172032422 :       t = cv_unqualified (t);
    9190              : 
    9191    172032422 :       if (t != TREE_TYPE (parm))
    9192      6088123 :         t = canonicalize_type_argument (t, complain);
    9193              : 
    9194              :       /* We need to handle arguments for alias or concept templates
    9195              :          differently: we need to force building an IMPLICIT_CONV_EXPR, because
    9196              :          these arguments are going to be substituted directly into the
    9197              :          dependent type; they might not get another chance at
    9198              :          convert_nontype_argument.  But if the argument ends up here again for
    9199              :          a template that isn't one of those, remove the conversion for
    9200              :          consistency between naming the same dependent type directly or through
    9201              :          an alias.  */
    9202    172032422 :       bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9203    127244114 :                                     || concept_definition_p (in_decl));
    9204    129162724 :       if (!force_conv
    9205    129162724 :           && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR
    9206      4218420 :           && IMPLICIT_CONV_EXPR_FORCED (orig_arg)
    9207      4218267 :           && same_type_p (TREE_TYPE (orig_arg), t))
    9208      4218261 :         orig_arg = TREE_OPERAND (orig_arg, 0);
    9209              : 
    9210    172032422 :       val = convert_nontype_argument_maybe_dependent (t, orig_arg, force_conv,
    9211              :                                                       complain);
    9212              : 
    9213    172032422 :       if (val == NULL_TREE)
    9214          524 :         val = error_mark_node;
    9215    172031898 :       else if (val == error_mark_node && (complain & tf_error))
    9216           57 :         error_at (cp_expr_loc_or_input_loc (orig_arg),
    9217              :                   "could not convert template argument %qE from %qT to %qT",
    9218           41 :                   orig_arg, TREE_TYPE (orig_arg), t);
    9219              : 
    9220    172032422 :       if (INDIRECT_REF_P (val))
    9221              :         {
    9222              :           /* Reject template arguments that are references to built-in
    9223              :              functions with no library fallbacks.  */
    9224         4515 :           const_tree inner = TREE_OPERAND (val, 0);
    9225         4515 :           const_tree innertype = TREE_TYPE (inner);
    9226         4515 :           if (innertype
    9227         2378 :               && TYPE_REF_P (innertype)
    9228         2369 :               && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
    9229          102 :               && TREE_OPERAND_LENGTH (inner) > 0
    9230         4587 :               && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
    9231            4 :               return error_mark_node;
    9232              :         }
    9233              : 
    9234    172032418 :       if (TREE_CODE (val) == SCOPE_REF)
    9235              :         {
    9236              :           /* Strip typedefs from the SCOPE_REF.  */
    9237      8383839 :           tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
    9238      8383839 :           tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
    9239              :                                                    complain);
    9240     16767678 :           val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
    9241      8383839 :                                       QUALIFIED_NAME_IS_TEMPLATE (val));
    9242              :         }
    9243              :     }
    9244              : 
    9245              :   return val;
    9246              : }
    9247              : 
    9248              : /* Coerces the remaining template arguments in INNER_ARGS (from
    9249              :    ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
    9250              :    Returns the coerced argument pack. PARM_IDX is the position of this
    9251              :    parameter in the template parameter list. ARGS is the original
    9252              :    template argument list.  */
    9253              : static tree
    9254    103908314 : coerce_template_parameter_pack (tree parms,
    9255              :                                 int parm_idx,
    9256              :                                 tree args,
    9257              :                                 tree inner_args,
    9258              :                                 int arg_idx,
    9259              :                                 tree new_args,
    9260              :                                 int* lost,
    9261              :                                 tree in_decl,
    9262              :                                 tsubst_flags_t complain)
    9263              : {
    9264    103908314 :   tree parm = TREE_VEC_ELT (parms, parm_idx);
    9265    207816628 :   int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    9266    103908314 :   tree packed_args;
    9267    103908314 :   tree argument_pack;
    9268    103908314 :   tree packed_parms = NULL_TREE;
    9269              : 
    9270    103908314 :   if (arg_idx > nargs)
    9271              :     arg_idx = nargs;
    9272              : 
    9273    103908314 :   if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
    9274              :     {
    9275              :       /* When the template parameter is a non-type template parameter pack
    9276              :          or template template parameter pack whose type or template
    9277              :          parameters use parameter packs, we know exactly how many arguments
    9278              :          we are looking for.  Build a vector of the instantiated decls for
    9279              :          these template parameters in PACKED_PARMS.  */
    9280              :       /* We can't use make_pack_expansion here because it would interpret a
    9281              :          _DECL as a use rather than a declaration.  */
    9282           87 :       tree decl = TREE_VALUE (parm);
    9283           87 :       tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
    9284           87 :       PACK_EXPANSION_PATTERN (exp) = decl;
    9285          174 :       PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
    9286           87 :       SET_TYPE_STRUCTURAL_EQUALITY (exp);
    9287              : 
    9288           87 :       TREE_VEC_LENGTH (args)--;
    9289           87 :       packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
    9290           84 :       TREE_VEC_LENGTH (args)++;
    9291              : 
    9292           84 :       if (packed_parms == error_mark_node)
    9293              :         return error_mark_node;
    9294              : 
    9295              :       /* If we're doing a partial instantiation of a member template,
    9296              :          verify that all of the types used for the non-type
    9297              :          template parameter pack are, in fact, valid for non-type
    9298              :          template parameters.  */
    9299           84 :       if (arg_idx < nargs
    9300           84 :           && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
    9301              :         {
    9302           42 :           int j, len = TREE_VEC_LENGTH (packed_parms);
    9303           92 :           for (j = 0; j < len; ++j)
    9304              :             {
    9305           52 :               tree t = TREE_VEC_ELT (packed_parms, j);
    9306           52 :               if (TREE_CODE (t) == PARM_DECL
    9307           52 :                   && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
    9308            2 :                 return error_mark_node;
    9309              :             }
    9310              :           /* We don't know how many args we have yet, just
    9311              :              use the unconverted ones for now.  */
    9312              :           return NULL_TREE;
    9313              :         }
    9314              : 
    9315           42 :       packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
    9316              :     }
    9317              :   else
    9318    103908227 :     packed_args = make_tree_vec (nargs - arg_idx);
    9319              : 
    9320              :   /* Convert the remaining arguments, which will be a part of the
    9321              :      parameter pack "parm".  */
    9322    103908269 :   int first_pack_arg = arg_idx;
    9323    282815891 :   for (; arg_idx < nargs; ++arg_idx)
    9324              :     {
    9325    178907634 :       tree arg = TREE_VEC_ELT (inner_args, arg_idx);
    9326    178907634 :       tree actual_parm = TREE_VALUE (parm);
    9327    178907634 :       int pack_idx = arg_idx - first_pack_arg;
    9328              : 
    9329    178907634 :       if (packed_parms)
    9330              :         {
    9331              :           /* Once we've packed as many args as we have types, stop.  */
    9332           72 :           if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
    9333              :             break;
    9334           60 :           else if (PACK_EXPANSION_P (arg))
    9335              :             /* We don't know how many args we have yet, just
    9336              :                use the unconverted ones for now.  */
    9337              :             return NULL_TREE;
    9338              :           else
    9339           60 :             actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
    9340              :         }
    9341              : 
    9342    178907622 :       if (arg == error_mark_node)
    9343              :         {
    9344          749 :           if (complain & tf_error)
    9345          746 :             error ("template argument %d is invalid", arg_idx + 1);
    9346              :         }
    9347              :       else
    9348    178906873 :         arg = convert_template_argument (actual_parm,
    9349              :                                          arg, new_args, complain, parm_idx,
    9350              :                                          in_decl);
    9351    178907622 :       if (arg == error_mark_node)
    9352          922 :         (*lost)++;
    9353    178907622 :       TREE_VEC_ELT (packed_args, pack_idx) = arg;
    9354              :     }
    9355              : 
    9356    103908269 :   if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
    9357    103908269 :       && TREE_VEC_LENGTH (packed_args) > 0)
    9358              :     {
    9359            3 :       if (complain & tf_error)
    9360            3 :         error ("wrong number of template arguments (%d, should be %d)",
    9361            3 :                arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
    9362            3 :       return error_mark_node;
    9363              :     }
    9364              : 
    9365    103908266 :   if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
    9366    103908266 :       || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
    9367    101004734 :     argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
    9368              :   else
    9369              :     {
    9370      2903532 :       argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
    9371      2903532 :       TREE_CONSTANT (argument_pack) = 1;
    9372              :     }
    9373              : 
    9374    103908266 :   ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
    9375    103908266 :   if (CHECKING_P)
    9376    103908266 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
    9377              :                                          TREE_VEC_LENGTH (packed_args));
    9378    103908266 :   return argument_pack;
    9379              : }
    9380              : 
    9381              : /* Returns the number of pack expansions in the template argument vector
    9382              :    ARGS.  */
    9383              : 
    9384              : static int
    9385   2117319966 : pack_expansion_args_count (tree args)
    9386              : {
    9387   2117319966 :   int i;
    9388   2117319966 :   int count = 0;
    9389   2117319966 :   if (args)
    9390   5803582968 :     for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
    9391              :       {
    9392   3686263002 :         tree elt = TREE_VEC_ELT (args, i);
    9393   3686263002 :         if (elt && PACK_EXPANSION_P (elt))
    9394     31669318 :           ++count;
    9395              :       }
    9396   2117319966 :   return count;
    9397              : }
    9398              : 
    9399              : /* Convert all template arguments to their appropriate types, and
    9400              :    return a vector containing the innermost resulting template
    9401              :    arguments.  If any error occurs, return error_mark_node. Error and
    9402              :    warning messages are issued under control of COMPLAIN.
    9403              : 
    9404              :    If PARMS represents all template parameters levels, this function
    9405              :    returns a vector of vectors representing all the resulting argument
    9406              :    levels.  Note that in this case, only the innermost arguments are
    9407              :    coerced because the outermost ones are supposed to have been coerced
    9408              :    already.  Otherwise, if PARMS represents only (the innermost) vector
    9409              :    of parameters, this function returns a vector containing just the
    9410              :    innermost resulting arguments.
    9411              : 
    9412              :    If REQUIRE_ALL_ARGS is false, argument deduction will be performed
    9413              :    for arguments not specified in ARGS.  If REQUIRE_ALL_ARGS is true,
    9414              :    arguments not specified in ARGS must have default arguments which
    9415              :    we'll use to fill in ARGS.  */
    9416              : 
    9417              : tree
    9418    920373970 : coerce_template_parms (tree parms,
    9419              :                        tree args,
    9420              :                        tree in_decl,
    9421              :                        tsubst_flags_t complain,
    9422              :                        bool require_all_args /* = true */)
    9423              : {
    9424    920373970 :   int nparms, nargs, parm_idx, arg_idx, lost = 0;
    9425    920373970 :   tree orig_inner_args;
    9426    920373970 :   tree inner_args;
    9427              : 
    9428              :   /* When used as a boolean value, indicates whether this is a
    9429              :      variadic template parameter list. Since it's an int, we can also
    9430              :      subtract it from nparms to get the number of non-variadic
    9431              :      parameters.  */
    9432    920373970 :   int variadic_p = 0;
    9433    920373970 :   int variadic_args_p = 0;
    9434    920373970 :   int post_variadic_parms = 0;
    9435              : 
    9436              :   /* Adjustment to nparms for fixed parameter packs.  */
    9437    920373970 :   int fixed_pack_adjust = 0;
    9438    920373970 :   int fixed_packs = 0;
    9439    920373970 :   int missing = 0;
    9440              : 
    9441              :   /* Likewise for parameters with default arguments.  */
    9442    920373970 :   int default_p = 0;
    9443              : 
    9444    920373970 :   if (args == error_mark_node)
    9445              :     return error_mark_node;
    9446              : 
    9447    920373970 :   bool return_full_args = false;
    9448    920373970 :   if (TREE_CODE (parms) == TREE_LIST)
    9449              :     {
    9450    727021922 :       if (TMPL_PARMS_DEPTH (parms) > 1)
    9451              :         {
    9452     26863602 :           gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
    9453              :           return_full_args = true;
    9454              :         }
    9455    727021922 :       parms = INNERMOST_TEMPLATE_PARMS (parms);
    9456              :     }
    9457              : 
    9458    920373970 :   nparms = TREE_VEC_LENGTH (parms);
    9459              : 
    9460              :   /* Determine if there are any parameter packs or default arguments.  */
    9461   2298199604 :   for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
    9462              :     {
    9463   1377825634 :       tree parm = TREE_VEC_ELT (parms, parm_idx);
    9464   1377825634 :       if (variadic_p)
    9465        16490 :         ++post_variadic_parms;
    9466   1377825634 :       if (template_parameter_pack_p (TREE_VALUE (parm)))
    9467    107899743 :         ++variadic_p;
    9468   1377825634 :       if (TREE_PURPOSE (parm))
    9469    189609395 :         ++default_p;
    9470              :     }
    9471              : 
    9472    920373970 :   inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
    9473              :   /* If there are no parameters that follow a parameter pack, we need to
    9474              :      expand any argument packs so that we can deduce a parameter pack from
    9475              :      some non-packed args followed by an argument pack, as in variadic85.C.
    9476              :      If there are such parameters, we need to leave argument packs intact
    9477              :      so the arguments are assigned properly.  This can happen when dealing
    9478              :      with a nested class inside a partial specialization of a class
    9479              :      template, as in variadic92.C, or when deducing a template parameter pack
    9480              :      from a sub-declarator, as in variadic114.C.  */
    9481    920373970 :   if (!post_variadic_parms)
    9482    920360605 :     inner_args = expand_template_argument_pack (inner_args);
    9483              : 
    9484              :   /* Count any pack expansion args.  */
    9485    920373970 :   variadic_args_p = pack_expansion_args_count (inner_args);
    9486              : 
    9487   1840747940 :   nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    9488     68279014 :   if ((nargs - variadic_args_p > nparms && !variadic_p)
    9489    985964863 :       || (nargs < nparms - variadic_p
    9490              :           && require_all_args
    9491     23591855 :           && !variadic_args_p
    9492     14521797 :           && (TREE_VEC_ELT (parms, nargs) != error_mark_node
    9493     14521797 :               && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
    9494              :     {
    9495      2688306 :     bad_nargs:
    9496      2688306 :       if (complain & tf_error)
    9497              :         {
    9498           88 :           auto_diagnostic_group d;
    9499           88 :           if (variadic_p || default_p)
    9500              :             {
    9501           12 :               nparms -= variadic_p + default_p;
    9502           12 :               error ("wrong number of template arguments "
    9503              :                      "(%d, should be at least %d)", nargs, nparms);
    9504              :             }
    9505              :           else
    9506           76 :              error ("wrong number of template arguments "
    9507              :                     "(%d, should be %d)", nargs, nparms);
    9508              : 
    9509           88 :           if (in_decl)
    9510           88 :             inform (DECL_SOURCE_LOCATION (in_decl),
    9511              :                     "provided for %qD", in_decl);
    9512           88 :         }
    9513              : 
    9514      2688306 :       return error_mark_node;
    9515              :     }
    9516              :   /* We can't pass a pack expansion to a non-pack parameter of an alias
    9517              :      template (DR 1430).  */
    9518    917685673 :   else if (in_decl
    9519    907226843 :            && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9520    746032376 :                || concept_definition_p (in_decl))
    9521    221745802 :            && variadic_args_p
    9522    922099845 :            && nargs - variadic_args_p < nparms - variadic_p)
    9523              :     {
    9524           24 :       if (complain & tf_error)
    9525              :         {
    9526           27 :           for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
    9527              :             {
    9528           27 :               tree arg = TREE_VEC_ELT (inner_args, i);
    9529           27 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    9530              : 
    9531           27 :               if (PACK_EXPANSION_P (arg)
    9532           27 :                   && !template_parameter_pack_p (parm))
    9533              :                 {
    9534           24 :                   auto_diagnostic_group d;
    9535           24 :                   if (DECL_ALIAS_TEMPLATE_P (in_decl))
    9536           12 :                     error_at (location_of (arg),
    9537              :                               "pack expansion argument for non-pack parameter "
    9538              :                               "%qD of alias template %qD", parm, in_decl);
    9539              :                   else
    9540           12 :                     error_at (location_of (arg),
    9541              :                               "pack expansion argument for non-pack parameter "
    9542              :                               "%qD of concept %qD", parm, in_decl);
    9543           24 :                   inform (DECL_SOURCE_LOCATION (parm), "declared here");
    9544           24 :                   goto found;
    9545           24 :                 }
    9546              :             }
    9547            0 :           gcc_unreachable ();
    9548           24 :         found:;
    9549              :         }
    9550           24 :       return error_mark_node;
    9551              :     }
    9552              : 
    9553              :   /* We need to evaluate the template arguments, even though this
    9554              :      template-id may be nested within a "sizeof".  */
    9555    917685649 :   cp_evaluated ev;
    9556              : 
    9557    917685649 :   tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
    9558    917685649 :   tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
    9559    917685649 :   int pack_adjust = 0;
    9560   2273738176 :   for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
    9561              :     {
    9562   1369043960 :       tree arg;
    9563   1369043960 :       tree parm;
    9564              : 
    9565              :       /* Get the Ith template parameter.  */
    9566   1369043960 :       parm = TREE_VEC_ELT (parms, parm_idx);
    9567              : 
    9568   1369043960 :       if (parm == error_mark_node)
    9569              :         {
    9570            0 :           TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
    9571            0 :           continue;
    9572              :         }
    9573              : 
    9574              :       /* Calculate the next argument.  */
    9575   1369043960 :       if (arg_idx < nargs)
    9576   1337651420 :         arg = TREE_VEC_ELT (inner_args, arg_idx);
    9577              :       else
    9578              :         arg = NULL_TREE;
    9579              : 
    9580   1369043960 :       if (template_parameter_pack_p (TREE_VALUE (parm))
    9581    107193592 :           && (arg || require_all_args || !(complain & tf_partial))
    9582   1472952313 :           && !(arg && ARGUMENT_PACK_P (arg)))
    9583              :         {
    9584              :           /* Some arguments will be placed in the
    9585              :              template parameter pack PARM.  */
    9586    103908314 :           arg = coerce_template_parameter_pack (parms, parm_idx, args,
    9587              :                                                 inner_args, arg_idx,
    9588              :                                                 new_args, &lost,
    9589              :                                                 in_decl, complain);
    9590              : 
    9591    103908311 :           if (arg == NULL_TREE)
    9592              :             {
    9593              :               /* We don't know how many args we have yet, just use the
    9594              :                  unconverted (and still packed) ones for now.  */
    9595           40 :               ggc_free (new_inner_args);
    9596           40 :               new_inner_args = strip_typedefs (orig_inner_args,
    9597              :                                                /*remove_attrs=*/nullptr,
    9598              :                                                STF_KEEP_INJ_CLASS_NAME);
    9599           40 :               arg_idx = nargs;
    9600           40 :               break;
    9601              :             }
    9602              : 
    9603    103908271 :           TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
    9604              : 
    9605              :           /* Store this argument.  */
    9606    103908271 :           if (arg == error_mark_node)
    9607              :             {
    9608            5 :               lost++;
    9609              :               /* We are done with all of the arguments.  */
    9610            5 :               arg_idx = nargs;
    9611            5 :               break;
    9612              :             }
    9613              :           else
    9614              :             {
    9615    103908266 :               pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
    9616    103908266 :               arg_idx += pack_adjust;
    9617    103908266 :               if (fixed_parameter_pack_p (TREE_VALUE (parm)))
    9618              :                 {
    9619           39 :                   ++fixed_packs;
    9620           39 :                   fixed_pack_adjust += pack_adjust;
    9621              :                 }
    9622              :             }
    9623              : 
    9624    103908266 :           continue;
    9625              :         }
    9626   1265135646 :       else if (arg)
    9627              :         {
    9628   1235910810 :           if (PACK_EXPANSION_P (arg))
    9629              :             {
    9630              :               /* "If every valid specialization of a variadic template
    9631              :                  requires an empty template parameter pack, the template is
    9632              :                  ill-formed, no diagnostic required."  So check that the
    9633              :                  pattern works with this parameter.  */
    9634      1090424 :               tree pattern = PACK_EXPANSION_PATTERN (arg);
    9635      1090424 :               tree conv = convert_template_argument (TREE_VALUE (parm),
    9636              :                                                      pattern, new_args,
    9637              :                                                      complain, parm_idx,
    9638              :                                                      in_decl);
    9639      1090424 :               if (conv == error_mark_node)
    9640              :                 {
    9641            9 :                   if (complain & tf_error)
    9642            6 :                     inform (input_location, "so any instantiation with a "
    9643              :                             "non-empty parameter pack would be ill-formed");
    9644            9 :                   ++lost;
    9645              :                 }
    9646      1090415 :               else if (TYPE_P (conv) && !TYPE_P (pattern))
    9647              :                 /* Recover from missing typename.  */
    9648            9 :                 TREE_VEC_ELT (inner_args, arg_idx)
    9649           18 :                   = make_pack_expansion (conv, complain);
    9650              : 
    9651              :               /* We don't know how many args we have yet, just
    9652              :                  use the unconverted (but unpacked) ones for now.  */
    9653      1090424 :               ggc_free (new_inner_args);
    9654      1090424 :               new_inner_args = strip_typedefs (inner_args,
    9655              :                                                /*remove_attrs=*/nullptr,
    9656              :                                                STF_KEEP_INJ_CLASS_NAME);
    9657      1090424 :               arg_idx = nargs;
    9658      1090424 :               break;
    9659              :             }
    9660              :         }
    9661     29224836 :       else if (require_all_args)
    9662              :         {
    9663              :           /* There must be a default arg in this case.  */
    9664     17323875 :           arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
    9665     17323875 :                                      complain | (processing_template_decl
    9666     17323875 :                                                  ? tf_partial : tf_none),
    9667              :                                      in_decl);
    9668              :           /* The position of the first default template argument,
    9669              :              is also the number of non-defaulted arguments in NEW_INNER_ARGS.
    9670              :              Record that.  */
    9671     17323875 :           if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9672     14521676 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9673              :                                                  arg_idx - pack_adjust);
    9674              :         }
    9675              :       else
    9676              :         break;
    9677              : 
    9678   1252144261 :       if (arg == error_mark_node)
    9679              :         {
    9680         1029 :           if (complain & tf_error)
    9681         1001 :             error ("template argument %d is invalid", arg_idx + 1);
    9682              :         }
    9683   1252143232 :       else if (!arg)
    9684              :         {
    9685              :           /* This can occur if there was an error in the template
    9686              :              parameter list itself (which we would already have
    9687              :              reported) that we are trying to recover from, e.g., a class
    9688              :              template with a parameter list such as
    9689              :              template<typename..., typename> (cpp0x/variadic150.C).  */
    9690           39 :           ++lost;
    9691              : 
    9692              :           /* This can also happen with a fixed parameter pack (71834).  */
    9693           39 :           if (arg_idx >= nargs)
    9694            6 :             ++missing;
    9695              :         }
    9696              :       else
    9697   1252143193 :         arg = convert_template_argument (TREE_VALUE (parm),
    9698              :                                          arg, new_args, complain,
    9699              :                                          parm_idx, in_decl);
    9700              : 
    9701   1252144261 :       if (arg == error_mark_node)
    9702      3383582 :         lost++;
    9703              : 
    9704   1252144261 :       TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
    9705              :     }
    9706              : 
    9707    917685646 :   if (missing || arg_idx < nargs - variadic_args_p)
    9708              :     {
    9709              :       /* If we had fixed parameter packs, we didn't know how many arguments we
    9710              :          actually needed earlier; now we do.  */
    9711            9 :       nparms += fixed_pack_adjust;
    9712            9 :       variadic_p -= fixed_packs;
    9713            9 :       goto bad_nargs;
    9714              :     }
    9715              : 
    9716    917685637 :   if (arg_idx < nargs)
    9717              :     {
    9718              :       /* We had some pack expansion arguments that will only work if the packs
    9719              :          are empty, but wait until instantiation time to complain.
    9720              :          See variadic-ttp3.C.  */
    9721              : 
    9722              :       /* Except that we can't provide empty packs to alias templates or
    9723              :          concepts when there are no corresponding parameters. Basically,
    9724              :          we can get here with this:
    9725              : 
    9726              :              template<typename T> concept C = true;
    9727              : 
    9728              :              template<typename... Args>
    9729              :                requires C<Args...>
    9730              :              void f();
    9731              : 
    9732              :          When parsing C<Args...>, we try to form a concept check of
    9733              :          C<?, Args...>. Without the extra check for substituting an empty
    9734              :          pack past the last parameter, we can accept the check as valid.
    9735              : 
    9736              :          FIXME: This may be valid for alias templates (but I doubt it).
    9737              : 
    9738              :          FIXME: The error could be better also.   */
    9739       339681 :       if (in_decl && concept_definition_p (in_decl))
    9740              :         {
    9741            0 :           if (complain & tf_error)
    9742            0 :             error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
    9743              :                       "too many arguments");
    9744    917685637 :           return error_mark_node;
    9745              :         }
    9746              : 
    9747       226201 :       int len = nparms + (nargs - arg_idx);
    9748       226201 :       tree args = make_tree_vec (len);
    9749       226201 :       int i = 0;
    9750       678606 :       for (; i < nparms; ++i)
    9751       226204 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
    9752       452402 :       for (; i < len; ++i, ++arg_idx)
    9753       226201 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
    9754              :                                                arg_idx - pack_adjust);
    9755       226201 :       new_inner_args = args;
    9756              :     }
    9757              : 
    9758    917685637 :   if (lost)
    9759              :     {
    9760      3382984 :       gcc_assert (!(complain & tf_error) || seen_error ());
    9761      3382984 :       return error_mark_node;
    9762              :     }
    9763              : 
    9764    914302653 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9765    898690615 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9766              :                                          TREE_VEC_LENGTH (new_inner_args));
    9767              : 
    9768              :   /* If we expanded packs in inner_args and aren't returning it now, the
    9769              :      expanded vec is garbage.  */
    9770    914302653 :   if (inner_args != new_inner_args
    9771    913233331 :       && inner_args != orig_inner_args)
    9772     82167617 :     ggc_free (inner_args);
    9773              : 
    9774    914302653 :   return return_full_args ? new_args : new_inner_args;
    9775              : }
    9776              : 
    9777              : /* Returns true if T is a wrapper to make a C++20 template parameter
    9778              :    object const.  */
    9779              : 
    9780              : static bool
    9781  10520155428 : class_nttp_const_wrapper_p (tree t)
    9782              : {
    9783  10520155428 :   if (cxx_dialect < cxx20)
    9784              :     return false;
    9785  10455565637 :   return (TREE_CODE (t) == VIEW_CONVERT_EXPR
    9786         1054 :           && CP_TYPE_CONST_P (TREE_TYPE (t))
    9787  10455566691 :           && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
    9788              : }
    9789              : 
    9790              : /* Returns 1 if template args OT and NT are equivalent.  */
    9791              : 
    9792              : int
    9793   4528275452 : template_args_equal (tree ot, tree nt)
    9794              : {
    9795   4528275452 :   if (nt == ot)
    9796              :     return 1;
    9797   2934675065 :   if (nt == NULL_TREE || ot == NULL_TREE)
    9798              :     return false;
    9799   2934070750 :   if (nt == any_targ_node || ot == any_targ_node)
    9800              :     return true;
    9801              : 
    9802   2934070746 :   if (class_nttp_const_wrapper_p (nt))
    9803          385 :     nt = TREE_OPERAND (nt, 0);
    9804   2934070746 :   if (class_nttp_const_wrapper_p (ot))
    9805          335 :     ot = TREE_OPERAND (ot, 0);
    9806              : 
    9807              :   /* DR 1558: Don't treat an alias template specialization with dependent
    9808              :      arguments as equivalent to its underlying type when used as a template
    9809              :      argument; we need them to be distinct so that we substitute into the
    9810              :      specialization arguments at instantiation time.  And aliases can't be
    9811              :      equivalent without being ==, so we don't need to look any deeper.
    9812              : 
    9813              :      During partial ordering, however, we need to treat them normally so we can
    9814              :      order uses of the same alias with different cv-qualification (79960).  */
    9815   2934070746 :   auto cso = make_temp_override (comparing_dependent_aliases);
    9816   2934070746 :   if (!comparing_for_partial_ordering)
    9817   2928723458 :     ++comparing_dependent_aliases;
    9818              : 
    9819   2934070746 :   if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
    9820              :     /* For member templates */
    9821    177384821 :     return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
    9822   2834635899 :   else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
    9823     22977672 :     return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
    9824     21361437 :             && template_args_equal (PACK_EXPANSION_PATTERN (ot),
    9825     21361437 :                                     PACK_EXPANSION_PATTERN (nt))
    9826     38098128 :             && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
    9827     38098128 :                                     PACK_EXPANSION_EXTRA_ARGS (nt)));
    9828   2811041061 :   else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
    9829    634681714 :     return cp_tree_equal (ot, nt);
    9830   2176359347 :   else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
    9831            0 :     gcc_unreachable ();
    9832   2176359347 :   else if (TYPE_P (nt) || TYPE_P (ot))
    9833              :     {
    9834   1898839580 :       if (!(TYPE_P (nt) && TYPE_P (ot)))
    9835              :         return false;
    9836   1877971791 :       return same_type_p (ot, nt);
    9837              :     }
    9838              :   else
    9839              :     {
    9840              :       /* Try to treat a template non-type argument that has been converted
    9841              :          to the parameter type as equivalent to one that hasn't yet.  */
    9842    277519767 :       for (enum tree_code code1 = TREE_CODE (ot);
    9843    279634637 :            CONVERT_EXPR_CODE_P (code1)
    9844    279634637 :              || code1 == NON_LVALUE_EXPR;
    9845      2114870 :            code1 = TREE_CODE (ot))
    9846      2114870 :         ot = TREE_OPERAND (ot, 0);
    9847              : 
    9848    277519767 :       for (enum tree_code code2 = TREE_CODE (nt);
    9849    279681537 :            CONVERT_EXPR_CODE_P (code2)
    9850    279681537 :              || code2 == NON_LVALUE_EXPR;
    9851      2161770 :            code2 = TREE_CODE (nt))
    9852      2161770 :         nt = TREE_OPERAND (nt, 0);
    9853              : 
    9854    277519767 :       return cp_tree_equal (ot, nt);
    9855              :     }
    9856   2934070746 : }
    9857              : 
    9858              : /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
    9859              :    template arguments.  Returns false otherwise, and updates OLDARG_PTR and
    9860              :    NEWARG_PTR with the offending arguments if they are non-NULL.  */
    9861              : 
    9862              : bool
    9863   3386372767 : comp_template_args (tree oldargs, tree newargs,
    9864              :                     tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
    9865              : {
    9866   3386372767 :   if (oldargs == newargs)
    9867              :     return true;
    9868              : 
    9869   2539345762 :   if (!oldargs || !newargs)
    9870              :     return false;
    9871              : 
    9872   2539345761 :   if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    9873              :     return false;
    9874              : 
    9875   3879338825 :   for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
    9876              :     {
    9877   3033874932 :       tree nt = TREE_VEC_ELT (newargs, i);
    9878   3033874932 :       tree ot = TREE_VEC_ELT (oldargs, i);
    9879              : 
    9880   3033874932 :       if (! template_args_equal (ot, nt))
    9881              :         {
    9882   1692591499 :           if (oldarg_ptr != NULL)
    9883           12 :             *oldarg_ptr = ot;
    9884   1692591499 :           if (newarg_ptr != NULL)
    9885           12 :             *newarg_ptr = nt;
    9886              :           return false;
    9887              :         }
    9888              :     }
    9889              :   return true;
    9890              : }
    9891              : 
    9892              : static bool
    9893     23746719 : comp_template_args_porder (tree oargs, tree nargs)
    9894              : {
    9895     23746719 :   ++comparing_for_partial_ordering;
    9896     23746719 :   bool equal = comp_template_args (oargs, nargs);
    9897     23746719 :   --comparing_for_partial_ordering;
    9898     23746719 :   return equal;
    9899              : }
    9900              : 
    9901              : /* Implement a freelist interface for objects of type T.
    9902              : 
    9903              :    Head is a separate object, rather than a regular member, so that we
    9904              :    can define it as a GTY deletable pointer, which is highly
    9905              :    desirable.  A data member could be declared that way, but then the
    9906              :    containing object would implicitly get GTY((user)), which would
    9907              :    prevent us from instantiating freelists as global objects.
    9908              :    Although this way we can create freelist global objects, they're
    9909              :    such thin wrappers that instantiating temporaries at every use
    9910              :    loses nothing and saves permanent storage for the freelist object.
    9911              : 
    9912              :    Member functions next, anew, poison and reinit have default
    9913              :    implementations that work for most of the types we're interested
    9914              :    in, but if they don't work for some type, they should be explicitly
    9915              :    specialized.  See the comments before them for requirements, and
    9916              :    the example specializations for the tree_list_freelist.  */
    9917              : template <typename T>
    9918              : class freelist
    9919              : {
    9920              :   /* Return the next object in a chain.  We could just do type
    9921              :      punning, but if we access the object with its underlying type, we
    9922              :      avoid strict-aliasing trouble.  This needs only work between
    9923              :      poison and reinit.  */
    9924    921284814 :   static T *&next (T *obj) { return obj->next; }
    9925              : 
    9926              :   /* Return a newly allocated, uninitialized or minimally-initialized
    9927              :      object of type T.  Any initialization performed by anew should
    9928              :      either remain across the life of the object and the execution of
    9929              :      poison, or be redone by reinit.  */
    9930     20752464 :   static T *anew () { return ggc_alloc<T> (); }
    9931              : 
    9932              :   /* Optionally scribble all over the bits holding the object, so that
    9933              :      they become (mostly?) uninitialized memory.  This is called while
    9934              :      preparing to make the object part of the free list.  */
    9935    940121383 :   static void poison (T *obj) {
    9936    940121383 :     T *p ATTRIBUTE_UNUSED = obj;
    9937    940121383 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9938              : 
    9939              : #ifdef ENABLE_GC_CHECKING
    9940              :     /* Poison the data, to indicate the data is garbage.  */
    9941              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
    9942    940121383 :     memset (p, 0xa5, sizeof (*p));
    9943              : #endif
    9944              :     /* Let valgrind know the object is free.  */
    9945              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
    9946              : 
    9947              :     /* Let valgrind know the next portion of the object is available,
    9948              :        but uninitialized.  */
    9949              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9950              :   }
    9951              : 
    9952              :   /* Bring an object that underwent at least one lifecycle after anew
    9953              :      and before the most recent free and poison, back to a usable
    9954              :      state, reinitializing whatever is needed for it to be
    9955              :      functionally equivalent to an object just allocated and returned
    9956              :      by anew.  This may poison or clear the next field, used by
    9957              :      freelist housekeeping after poison was called.  */
    9958    921284814 :   static void reinit (T *obj) {
    9959    921284814 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9960              : 
    9961              : #ifdef ENABLE_GC_CHECKING
    9962    921284814 :     memset (q, 0xa5, sizeof (*q));
    9963              : #endif
    9964              :     /* Let valgrind know the entire object is available, but
    9965              :        uninitialized.  */
    9966              :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
    9967              :   }
    9968              : 
    9969              :   /* Reference a GTY-deletable pointer that points to the first object
    9970              :      in the free list proper.  */
    9971              :   T *&head;
    9972              : public:
    9973              :   /* Construct a freelist object chaining objects off of HEAD.  */
    9974   1882161021 :   freelist (T *&head) : head(head) {}
    9975              : 
    9976              :   /* Add OBJ to the free object list.  The former head becomes OBJ's
    9977              :      successor.  */
    9978    940122200 :   void free (T *obj)
    9979              :   {
    9980    940122200 :     poison (obj);
    9981    940122200 :     next (obj) = head;
    9982    940122200 :     head = obj;
    9983    940122200 :   }
    9984              : 
    9985              :   /* Take an object from the free list, if one is available, or
    9986              :      allocate a new one.  Objects taken from the free list should be
    9987              :      regarded as filled with garbage, except for bits that are
    9988              :      configured to be preserved across free and alloc.  */
    9989    942038821 :   T *alloc ()
    9990              :   {
    9991    942038821 :     if (head)
    9992              :       {
    9993    921285536 :         T *obj = head;
    9994    921285536 :         head = next (head);
    9995    921285536 :         reinit (obj);
    9996    921285536 :         return obj;
    9997              :       }
    9998              :     else
    9999     20753285 :       return anew ();
   10000              :   }
   10001              : };
   10002              : 
   10003              : /* Explicitly specialize the interfaces for freelist<tree_node>: we
   10004              :    want to allocate a TREE_LIST using the usual interface, and ensure
   10005              :    TREE_CHAIN remains functional.  Alas, we have to duplicate a bit of
   10006              :    build_tree_list logic in reinit, so this could go out of sync.  */
   10007              : template <>
   10008              : inline tree &
   10009         2356 : freelist<tree_node>::next (tree obj)
   10010              : {
   10011         2356 :   return TREE_CHAIN (obj);
   10012              : }
   10013              : template <>
   10014              : inline tree
   10015          821 : freelist<tree_node>::anew ()
   10016              : {
   10017          821 :   return build_tree_list (NULL, NULL);
   10018              : }
   10019              : template <>
   10020              : inline void
   10021          817 : freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
   10022              : {
   10023          817 :   int size ATTRIBUTE_UNUSED = sizeof (tree_list);
   10024          817 :   tree p ATTRIBUTE_UNUSED = obj;
   10025          817 :   tree_base *b ATTRIBUTE_UNUSED = &obj->base;
   10026          817 :   tree *q ATTRIBUTE_UNUSED = &next (obj);
   10027              : 
   10028              : #ifdef ENABLE_GC_CHECKING
   10029          817 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
   10030              : 
   10031              :   /* Poison the data, to indicate the data is garbage.  */
   10032          817 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
   10033          817 :   memset (p, 0xa5, size);
   10034              : #endif
   10035              :   /* Let valgrind know the object is free.  */
   10036          817 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
   10037              :   /* But we still want to use the TREE_CODE and TREE_CHAIN parts.  */
   10038          817 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
   10039          817 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
   10040              : 
   10041              : #ifdef ENABLE_GC_CHECKING
   10042          817 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
   10043              :   /* Keep TREE_CHAIN functional.  */
   10044          817 :   TREE_SET_CODE (obj, TREE_LIST);
   10045              : #else
   10046              :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
   10047              : #endif
   10048          817 : }
   10049              : template <>
   10050              : inline void
   10051          722 : freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
   10052              : {
   10053          722 :   tree_common *c ATTRIBUTE_UNUSED = &obj->common;
   10054              : 
   10055              : #ifdef ENABLE_GC_CHECKING
   10056          722 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
   10057          722 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
   10058          722 :   memset (obj, 0, sizeof (tree_list));
   10059              : #endif
   10060              : 
   10061              :   /* Let valgrind know the entire object is available, but
   10062              :      uninitialized.  */
   10063          722 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
   10064              : 
   10065              : #ifdef ENABLE_GC_CHECKING
   10066          722 :   TREE_SET_CODE (obj, TREE_LIST);
   10067              : #else
   10068              :   TREE_CHAIN (obj) = NULL_TREE;
   10069              :   TREE_TYPE (obj) = NULL_TREE;
   10070              : #endif
   10071          722 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c)));
   10072          722 : }
   10073              : 
   10074              : /* Point to the first object in the TREE_LIST freelist.  */
   10075              : static GTY((deletable)) tree tree_list_freelist_head;
   10076              : /* Return the/an actual TREE_LIST freelist.  */
   10077              : static inline freelist<tree_node>
   10078         2360 : tree_list_freelist ()
   10079              : {
   10080         2360 :   return tree_list_freelist_head;
   10081              : }
   10082              : 
   10083              : /* Point to the first object in the tinst_level freelist.  */
   10084              : static GTY((deletable)) tinst_level *tinst_level_freelist_head;
   10085              : /* Return the/an actual tinst_level freelist.  */
   10086              : static inline freelist<tinst_level>
   10087   1839573295 : tinst_level_freelist ()
   10088              : {
   10089   1839573295 :   return tinst_level_freelist_head;
   10090              : }
   10091              : 
   10092              : /* Point to the first object in the pending_template freelist.  */
   10093              : static GTY((deletable)) pending_template *pending_template_freelist_head;
   10094              : /* Return the/an actual pending_template freelist.  */
   10095              : static inline freelist<pending_template>
   10096     42585366 : pending_template_freelist ()
   10097              : {
   10098     42585366 :   return pending_template_freelist_head;
   10099              : }
   10100              : 
   10101              : /* Build the TREE_LIST object out of a split list, store it
   10102              :    permanently, and return it.  */
   10103              : tree
   10104         1543 : tinst_level::to_list ()
   10105              : {
   10106         1543 :   gcc_assert (split_list_p ());
   10107         1543 :   tree ret = tree_list_freelist ().alloc ();
   10108         1543 :   TREE_PURPOSE (ret) = tldcl;
   10109         1543 :   TREE_VALUE (ret) = targs;
   10110         1543 :   tldcl = ret;
   10111         1543 :   targs = NULL;
   10112         1543 :   gcc_assert (tree_list_p ());
   10113         1543 :   return ret;
   10114              : }
   10115              : 
   10116              : const unsigned short tinst_level::refcount_infinity;
   10117              : 
   10118              : /* Increment OBJ's refcount unless it is already infinite.  */
   10119              : static tinst_level *
   10120   2821641178 : inc_refcount_use (tinst_level *obj)
   10121              : {
   10122   2143802360 :   if (obj && obj->refcount != tinst_level::refcount_infinity)
   10123   2143802360 :     ++obj->refcount;
   10124   2821641178 :   return obj;
   10125              : }
   10126              : 
   10127              : /* Release storage for OBJ and node, if it's a TREE_LIST.  */
   10128              : void
   10129    919137712 : tinst_level::free (tinst_level *obj)
   10130              : {
   10131    919137712 :   if (obj->tree_list_p ())
   10132          817 :     tree_list_freelist ().free (obj->get_node ());
   10133    919137712 :   tinst_level_freelist ().free (obj);
   10134    919137712 : }
   10135              : 
   10136              : /* Decrement OBJ's refcount if not infinite.  If it reaches zero, release
   10137              :    OBJ's DECL and OBJ, and start over with the tinst_level object that
   10138              :    used to be referenced by OBJ's NEXT.  */
   10139              : static void
   10140   2862003110 : dec_refcount_use (tinst_level *obj)
   10141              : {
   10142   2862003110 :   while (obj
   10143   2142050191 :          && obj->refcount != tinst_level::refcount_infinity
   10144   5923191013 :          && !--obj->refcount)
   10145              :     {
   10146    919137712 :       tinst_level *next = obj->next;
   10147    919137712 :       tinst_level::free (obj);
   10148    919137712 :       obj = next;
   10149              :     }
   10150   2862003110 : }
   10151              : 
   10152              : /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
   10153              :    and of the former PTR.  Omitting the second argument is equivalent
   10154              :    to passing (T*)NULL; this is allowed because passing the
   10155              :    zero-valued integral constant NULL confuses type deduction and/or
   10156              :    overload resolution.  */
   10157              : template <typename T>
   10158              : static void
   10159   2862003110 : set_refcount_ptr (T *& ptr, T *obj = NULL)
   10160              : {
   10161   2821641178 :   T *save = ptr;
   10162   2862003110 :   ptr = inc_refcount_use (obj);
   10163   2821641178 :   dec_refcount_use (save);
   10164   2821641178 : }
   10165              : 
   10166              : static void
   10167     27347721 : add_pending_template (tree d)
   10168              : {
   10169     27347721 :   tree ti = (TYPE_P (d)
   10170     27347721 :              ? CLASSTYPE_TEMPLATE_INFO (d)
   10171     27347721 :              : DECL_TEMPLATE_INFO (d));
   10172     27347721 :   struct pending_template *pt;
   10173     27347721 :   int level;
   10174              : 
   10175     27347721 :   if (TI_PENDING_TEMPLATE_FLAG (ti))
   10176              :     return;
   10177              : 
   10178              :   /* We are called both from instantiate_decl, where we've already had a
   10179              :      tinst_level pushed, and instantiate_template, where we haven't.
   10180              :      Compensate.  */
   10181     21601695 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   10182     21601695 :   level = !current_tinst_level
   10183     43203390 :     || current_tinst_level->maybe_get_node () != d;
   10184              : 
   10185              :   if (level)
   10186            0 :     push_tinst_level (d);
   10187              : 
   10188     21601695 :   pt = pending_template_freelist ().alloc ();
   10189     21601695 :   pt->next = NULL;
   10190     21601695 :   pt->tinst = NULL;
   10191     21601695 :   set_refcount_ptr (pt->tinst, current_tinst_level);
   10192     21601695 :   if (last_pending_template)
   10193     21568578 :     last_pending_template->next = pt;
   10194              :   else
   10195        33117 :     pending_templates = pt;
   10196              : 
   10197     21601695 :   last_pending_template = pt;
   10198              : 
   10199     21601695 :   TI_PENDING_TEMPLATE_FLAG (ti) = 1;
   10200              : 
   10201     21601695 :   if (level)
   10202            0 :     pop_tinst_level ();
   10203              : }
   10204              : 
   10205              : /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
   10206              :    ARGLIST.  Valid choices for FNS are given in the cp-tree.def
   10207              :    documentation for TEMPLATE_ID_EXPR.  */
   10208              : 
   10209              : tree
   10210     70613803 : lookup_template_function (tree fns, tree arglist)
   10211              : {
   10212     70613803 :   if (fns == error_mark_node || arglist == error_mark_node)
   10213              :     return error_mark_node;
   10214              : 
   10215     70613772 :   gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
   10216              : 
   10217     70613772 :   if (!is_overloaded_fn (fns) && !identifier_p (fns))
   10218              :     {
   10219            3 :       error ("%q#D is not a function template", fns);
   10220            3 :       return error_mark_node;
   10221              :     }
   10222              : 
   10223     70613769 :   if (BASELINK_P (fns))
   10224              :     {
   10225       866127 :       fns = copy_node (fns);
   10226      1732254 :       BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
   10227              :                                          unknown_type_node,
   10228       866127 :                                          BASELINK_FUNCTIONS (fns),
   10229              :                                          arglist);
   10230       866127 :       return fns;
   10231              :     }
   10232              : 
   10233     69747642 :   return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
   10234              : }
   10235              : 
   10236              : /* Within the scope of a template class S<T>, the name S gets bound
   10237              :    (in build_self_reference) to a TYPE_DECL for the class, not a
   10238              :    TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
   10239              :    or one of its enclosing classes, and that type is a template,
   10240              :    return the associated TEMPLATE_DECL.  Otherwise, the original
   10241              :    DECL is returned.
   10242              : 
   10243              :    Also handle the case when DECL is a TREE_LIST of ambiguous
   10244              :    injected-class-names from different bases.  */
   10245              : 
   10246              : tree
   10247    758338585 : maybe_get_template_decl_from_type_decl (tree decl)
   10248              : {
   10249    758338585 :   if (decl == NULL_TREE)
   10250              :     return decl;
   10251              : 
   10252              :   /* DR 176: A lookup that finds an injected-class-name (10.2
   10253              :      [class.member.lookup]) can result in an ambiguity in certain cases
   10254              :      (for example, if it is found in more than one base class). If all of
   10255              :      the injected-class-names that are found refer to specializations of
   10256              :      the same class template, and if the name is followed by a
   10257              :      template-argument-list, the reference refers to the class template
   10258              :      itself and not a specialization thereof, and is not ambiguous.  */
   10259    758338585 :   if (TREE_CODE (decl) == TREE_LIST)
   10260              :     {
   10261              :       tree t, tmpl = NULL_TREE;
   10262        38020 :       for (t = decl; t; t = TREE_CHAIN (t))
   10263              :         {
   10264        37918 :           tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
   10265        37918 :           if (!tmpl)
   10266              :             tmpl = elt;
   10267        18959 :           else if (tmpl != elt)
   10268              :             break;
   10269              :         }
   10270        18959 :       if (tmpl && t == NULL_TREE)
   10271              :         return tmpl;
   10272              :       else
   10273        18857 :         return decl;
   10274              :     }
   10275              : 
   10276    758319626 :   return (decl != NULL_TREE
   10277    758319626 :           && DECL_SELF_REFERENCE_P (decl)
   10278      7066832 :           && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
   10279      7066832 :     ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
   10280              : }
   10281              : 
   10282              : /* If TYPE is the generic implicit instantiation A<T>, return the primary
   10283              :    template type A<T> (which is suitable for entering into, e.g. for defining
   10284              :    a member of A<T>), otherwise return TYPE.  */
   10285              : 
   10286              : tree
   10287    527541193 : adjust_type_for_entering_scope (tree type)
   10288              : {
   10289    525194477 :   if (CLASS_TYPE_P (type)
   10290    525194474 :       && dependent_type_p (type)
   10291     51521337 :       && TYPE_TEMPLATE_INFO (type)
   10292              :       /* We detect the generic implicit instantiation A<T> by inspecting
   10293              :          TYPE_CANONICAL, which lookup_template_class sets to the primary
   10294              :          template type A<T>.  */
   10295    579062514 :       && TYPE_CANONICAL (type) == TREE_TYPE (TYPE_TI_TEMPLATE (type)))
   10296     19635528 :     type = TYPE_CANONICAL (type);
   10297              : 
   10298    527541193 :   return type;
   10299              : }
   10300              : 
   10301              : /* Convenience wrapper over tsubst that does adjust_type_for_entering_scope
   10302              :    on the result.  */
   10303              : 
   10304              : static tree
   10305    507626334 : tsubst_entering_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   10306              : {
   10307    507626334 :   t = tsubst (t, args, complain, in_decl);
   10308    507626334 :   return adjust_type_for_entering_scope (t);
   10309              : }
   10310              : 
   10311              : /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
   10312              :    parameters, find the desired type.
   10313              : 
   10314              :    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
   10315              : 
   10316              :    If D1 is an identifier and CONTEXT is non-NULL, then the lookup is
   10317              :    carried out in CONTEXT. Currently, only namespaces are supported for
   10318              :    CONTEXT.
   10319              : 
   10320              :    If D1 is an identifier and CONTEXT is NULL, the lookup is performed
   10321              :    in the innermost non-namespace binding.
   10322              : 
   10323              :    Otherwise CONTEXT is ignored and no lookup is carried out.
   10324              : 
   10325              :    IN_DECL, if non-NULL, is the template declaration we are trying to
   10326              :    instantiate.
   10327              : 
   10328              :    Issue error and warning messages under control of COMPLAIN.
   10329              : 
   10330              :    ??? Note that this function is currently called *twice* for each
   10331              :    template-id: the first time from the parser, while creating the
   10332              :    incomplete type (finish_template_type), and the second type during the
   10333              :    real instantiation (instantiate_template_class). This is surely something
   10334              :    that we want to avoid. It also causes some problems with argument
   10335              :    coercion (see convert_nontype_argument for more information on this).  */
   10336              : 
   10337              : tree
   10338   1167945892 : lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
   10339              :                        tsubst_flags_t complain)
   10340              : {
   10341   1167945892 :   auto_timevar tv (TV_TEMPLATE_INST);
   10342              : 
   10343   1167945892 :   tree templ = NULL_TREE, parmlist;
   10344   1167945892 :   tree t;
   10345   1167945892 :   spec_entry **slot;
   10346   1167945892 :   spec_entry *entry;
   10347              : 
   10348   1167945892 :   if (identifier_p (d1) && context)
   10349              :     {
   10350       222819 :       gcc_checking_assert (TREE_CODE (context) == NAMESPACE_DECL);
   10351       222819 :       push_decl_namespace (context);
   10352       222819 :       templ = lookup_name (d1, LOOK_where::NAMESPACE, LOOK_want::NORMAL);
   10353       222819 :       pop_decl_namespace ();
   10354              :     }
   10355   1167723073 :   else if (identifier_p (d1))
   10356              :     {
   10357            0 :       tree value = innermost_non_namespace_value (d1);
   10358            0 :       if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
   10359              :         templ = value;
   10360              :       else
   10361              :         {
   10362            0 :           templ = lookup_name (d1);
   10363            0 :           templ = maybe_get_template_decl_from_type_decl (templ);
   10364              :         }
   10365              :     }
   10366   1167723073 :   else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
   10367              :     {
   10368            0 :       tree type = TREE_TYPE (d1);
   10369              : 
   10370              :       /* If we are declaring a constructor, say A<T>::A<T>, we will get
   10371              :          an implicit typename for the second A.  Deal with it.  */
   10372            0 :       if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
   10373            0 :         type = TREE_TYPE (type);
   10374              : 
   10375            0 :       if (CLASSTYPE_TEMPLATE_INFO (type))
   10376              :         {
   10377            0 :           templ = CLASSTYPE_TI_TEMPLATE (type);
   10378            0 :           d1 = DECL_NAME (templ);
   10379              :         }
   10380              :     }
   10381   1167723073 :   else if (TREE_CODE (d1) == ENUMERAL_TYPE
   10382   1167723073 :            || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
   10383              :     {
   10384    970503921 :       templ = TYPE_TI_TEMPLATE (d1);
   10385    970503921 :       d1 = DECL_NAME (templ);
   10386              :     }
   10387    197219152 :   else if (DECL_TYPE_TEMPLATE_P (d1))
   10388              :     {
   10389    197219148 :       templ = d1;
   10390    197219148 :       d1 = DECL_NAME (templ);
   10391              :     }
   10392            4 :   else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
   10393              :     {
   10394            0 :       templ = d1;
   10395            0 :       d1 = DECL_NAME (templ);
   10396              :     }
   10397              : 
   10398              :   /* Issue an error message if we didn't find a template.  */
   10399   1167945888 :   if (! templ)
   10400              :     {
   10401         1512 :       if (complain & tf_error)
   10402              :         {
   10403            4 :           if (TYPE_P (d1))
   10404            0 :             error ("%qT is not a template", d1);
   10405              :           else
   10406            4 :             error ("%qE is not a template", d1);
   10407              :         }
   10408         1512 :       return error_mark_node;
   10409              :     }
   10410              : 
   10411   1167944380 :   if (TREE_CODE (templ) != TEMPLATE_DECL
   10412              :          /* Make sure it's a user visible template, if it was named by
   10413              :             the user.  */
   10414   1167944380 :       || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
   10415    196143575 :           && !PRIMARY_TEMPLATE_P (templ)))
   10416              :     {
   10417            0 :       if (complain & tf_error)
   10418              :         {
   10419            0 :           error ("non-template type %qT used as a template", d1);
   10420            0 :           if (in_decl)
   10421            0 :             error ("for template declaration %q+D", in_decl);
   10422              :         }
   10423            0 :       return error_mark_node;
   10424              :     }
   10425              : 
   10426   1167944380 :   complain &= ~tf_user;
   10427              : 
   10428              :   /* An alias that just changes the name of a template is equivalent to the
   10429              :      other template, so if any of the arguments are pack expansions, strip
   10430              :      the alias to avoid problems with a pack expansion passed to a non-pack
   10431              :      alias template parameter (DR 1430).  */
   10432   1167944380 :   if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
   10433      7730255 :     templ = get_underlying_template (templ);
   10434              : 
   10435   1167944380 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
   10436              :     {
   10437       374467 :       tree parm;
   10438       374467 :       tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
   10439       374467 :       if (arglist2 == error_mark_node
   10440       374467 :           || (!uses_template_parms (arglist2)
   10441        13443 :               && check_instantiated_args (templ, arglist2, complain)))
   10442           18 :         return error_mark_node;
   10443              : 
   10444       374449 :       parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
   10445       374449 :       return parm;
   10446              :     }
   10447              :   else
   10448              :     {
   10449   1167569913 :       tree template_type = TREE_TYPE (templ);
   10450   1167569913 :       tree gen_tmpl;
   10451   1167569913 :       tree type_decl;
   10452   1167569913 :       tree found = NULL_TREE;
   10453   1167569913 :       int arg_depth;
   10454   1167569913 :       int parm_depth;
   10455   1167569913 :       int is_dependent_type;
   10456   1167569913 :       int use_partial_inst_tmpl = false;
   10457              : 
   10458   1167569913 :       if (template_type == error_mark_node)
   10459              :         /* An error occurred while building the template TEMPL, and a
   10460              :            diagnostic has most certainly been emitted for that
   10461              :            already.  Let's propagate that error.  */
   10462              :         return error_mark_node;
   10463              : 
   10464   1167569910 :       gen_tmpl = most_general_template (templ);
   10465   1167569910 :       if (modules_p ())
   10466      4568875 :         lazy_load_pendings (gen_tmpl);
   10467              : 
   10468   1167569910 :       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
   10469   1167569910 :       parm_depth = TMPL_PARMS_DEPTH (parmlist);
   10470   2335139820 :       arg_depth = TMPL_ARGS_DEPTH (arglist);
   10471              : 
   10472   1167569910 :       if (arg_depth == 1 && parm_depth > 1)
   10473              :         {
   10474              :           /* We've been given an incomplete set of template arguments.
   10475              :              For example, given:
   10476              : 
   10477              :                template <class T> struct S1 {
   10478              :                  template <class U> struct S2 {};
   10479              :                  template <class U> struct S2<U*> {};
   10480              :                 };
   10481              : 
   10482              :              we will be called with an ARGLIST of `U*', but the
   10483              :              TEMPLATE will be `template <class T> template
   10484              :              <class U> struct S1<T>::S2'.  We must fill in the missing
   10485              :              arguments.  */
   10486      7118213 :           tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
   10487      7118213 :           arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
   10488     14236426 :           arg_depth = TMPL_ARGS_DEPTH (arglist);
   10489              :         }
   10490              : 
   10491              :       /* Now we should have enough arguments.  */
   10492   1167569910 :       gcc_assert (parm_depth == arg_depth);
   10493              : 
   10494   1167569910 :       if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
   10495              :         {
   10496              :           /* The user referred to a specialization of an alias
   10497              :             template represented by GEN_TMPL.
   10498              : 
   10499              :             [temp.alias]/2 says:
   10500              : 
   10501              :                 When a template-id refers to the specialization of an
   10502              :                 alias template, it is equivalent to the associated
   10503              :                 type obtained by substitution of its
   10504              :                 template-arguments for the template-parameters in the
   10505              :                 type-id of the alias template.  */
   10506              : 
   10507     43107850 :           t = instantiate_alias_template (gen_tmpl, arglist, complain);
   10508              :           /* Note that the call above (by indirectly calling
   10509              :              register_specialization in tsubst_decl) registers the
   10510              :              TYPE_DECL representing the specialization of the alias
   10511              :              template.  So next time someone substitutes ARGLIST for
   10512              :              the template parms into the alias template (GEN_TMPL),
   10513              :              she'll get that TYPE_DECL back.  */
   10514              : 
   10515     43107850 :           if (t == error_mark_node)
   10516              :             return error_mark_node;
   10517     43096756 :           return TREE_TYPE (t);
   10518              :         }
   10519              : 
   10520              :       /* From here on, we're only interested in the most general
   10521              :          template.  */
   10522              : 
   10523              :       /* Shortcut looking up the current class scope again.  */
   10524   1124462060 :       for (tree cur = current_nonlambda_class_type ();
   10525   1960080963 :            cur != NULL_TREE;
   10526    835618903 :            cur = get_containing_scope (cur))
   10527              :         {
   10528   1457509668 :           if (!CLASS_TYPE_P (cur))
   10529    572380711 :             continue;
   10530              : 
   10531    885128957 :           tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
   10532   2512385030 :           if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
   10533              :             break;
   10534              : 
   10535    814318012 :           if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
   10536   1385108482 :               && comp_template_args (arglist, TI_ARGS (ti)))
   10537              :             return cur;
   10538              :         }
   10539              : 
   10540              :       /* Calculate the BOUND_ARGS.  These will be the args that are
   10541              :          actually tsubst'd into the definition to create the
   10542              :          instantiation.  */
   10543    573382240 :       if (PRIMARY_TEMPLATE_P (gen_tmpl))
   10544    565827649 :         arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
   10545              : 
   10546    573382237 :       if (arglist == error_mark_node)
   10547              :         /* We were unable to bind the arguments.  */
   10548              :         return error_mark_node;
   10549              : 
   10550              :       /* In the scope of a template class, explicit references to the
   10551              :          template class refer to the type of the template, not any
   10552              :          instantiation of it.  For example, in:
   10553              : 
   10554              :            template <class T> class C { void f(C<T>); }
   10555              : 
   10556              :          the `C<T>' is just the same as `C'.  Outside of the
   10557              :          class, however, such a reference is an instantiation.
   10558              :          One can use adjust_type_for_entering_scope to make
   10559              :          this adjustment as needed.  */
   10560    573380453 :       if (!PRIMARY_TEMPLATE_P (gen_tmpl)
   10561    573380453 :           || currently_open_class (template_type))
   10562              :         {
   10563     11494116 :           tree tinfo = TYPE_TEMPLATE_INFO (template_type);
   10564              : 
   10565     22988232 :           if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
   10566              :             return template_type;
   10567              :         }
   10568              : 
   10569              :       /* If we already have this specialization, return it.  */
   10570    573338139 :       spec_entry elt;
   10571    573338139 :       elt.tmpl = gen_tmpl;
   10572    573338139 :       elt.args = arglist;
   10573    573338139 :       entry = type_specializations->find (&elt);
   10574              : 
   10575    573338139 :       if (entry)
   10576    457536317 :         return entry->spec;
   10577              : 
   10578              :       /* If the template's constraints are not satisfied,
   10579              :          then we cannot form a valid type.
   10580              : 
   10581              :          Note that the check is deferred until after the hash
   10582              :          lookup. This prevents redundant checks on previously
   10583              :          instantiated specializations. */
   10584    115801822 :       if (flag_concepts
   10585    115801822 :           && !constraints_satisfied_p (gen_tmpl, arglist))
   10586              :         {
   10587          914 :           if (complain & tf_error)
   10588              :             {
   10589           87 :               auto_diagnostic_group d;
   10590           87 :               error ("template constraint failure for %qD", gen_tmpl);
   10591           87 :               diagnose_constraints (input_location, gen_tmpl, arglist);
   10592           87 :             }
   10593          914 :           return error_mark_node;
   10594              :         }
   10595              : 
   10596    115800908 :       is_dependent_type = uses_template_parms (arglist);
   10597              : 
   10598              :       /* If the deduced arguments are invalid, then the binding
   10599              :          failed.  */
   10600    115800908 :       if (!is_dependent_type
   10601    115800908 :           && check_instantiated_args (gen_tmpl,
   10602              :                                       INNERMOST_TEMPLATE_ARGS (arglist),
   10603              :                                       complain))
   10604            8 :         return error_mark_node;
   10605              : 
   10606    115800900 :       if (!is_dependent_type
   10607     52633534 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   10608      3803922 :           && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
   10609    117955192 :           && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
   10610              :         /* This occurs when the user has tried to define a tagged type
   10611              :            in a scope that forbids it.  We emitted an error during the
   10612              :            parse.  We didn't complete the bail out then, so here we
   10613              :            are.  */
   10614           12 :         return error_mark_node;
   10615              : 
   10616    115800888 :       context = DECL_CONTEXT (gen_tmpl);
   10617    115800888 :       if (context && TYPE_P (context))
   10618              :         {
   10619      5852786 :           if (!uses_template_parms (DECL_CONTEXT (templ)))
   10620              :             /* If the context of the partially instantiated template is
   10621              :                already non-dependent, then we might as well use it.  */
   10622      1907430 :             context = DECL_CONTEXT (templ);
   10623              :           else
   10624              :             {
   10625      3945356 :               context = tsubst_entering_scope (context, arglist,
   10626              :                                                complain, in_decl);
   10627              :               /* Try completing the enclosing context if it's not already so.  */
   10628      3945356 :               if (context != error_mark_node
   10629      3945356 :                   && !COMPLETE_TYPE_P (context))
   10630              :                 {
   10631      3610238 :                   context = complete_type (context);
   10632      3610238 :                   if (COMPLETE_TYPE_P (context))
   10633              :                     {
   10634              :                       /* Completion could have caused us to register the desired
   10635              :                          specialization already, so check the table again.  */
   10636            3 :                       entry = type_specializations->find (&elt);
   10637            3 :                       if (entry)
   10638            3 :                         return entry->spec;
   10639              :                     }
   10640              :                 }
   10641              :             }
   10642              :         }
   10643              :       else
   10644    109948102 :         context = tsubst (context, arglist, complain, in_decl);
   10645              : 
   10646    115800885 :       if (context == error_mark_node)
   10647              :         return error_mark_node;
   10648              : 
   10649    115800882 :       if (!context)
   10650            0 :         context = global_namespace;
   10651              : 
   10652              :       /* Create the type.  */
   10653    115800882 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE)
   10654              :         {
   10655       395252 :           if (!is_dependent_type)
   10656              :             {
   10657       395249 :               set_current_access_from_decl (TYPE_NAME (template_type));
   10658       790498 :               t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
   10659       395249 :                               tsubst (ENUM_UNDERLYING_TYPE (template_type),
   10660              :                                       arglist, complain, in_decl),
   10661       395249 :                               tsubst_attributes (TYPE_ATTRIBUTES (template_type),
   10662              :                                                  arglist, complain, in_decl),
   10663       395249 :                               SCOPED_ENUM_P (template_type), NULL);
   10664              : 
   10665       395249 :               if (t == error_mark_node)
   10666              :                 return t;
   10667              :             }
   10668              :           else
   10669              :             {
   10670              :               /* We don't want to call start_enum for this type, since
   10671              :                  the values for the enumeration constants may involve
   10672              :                  template parameters.  And, no one should be interested
   10673              :                  in the enumeration constants for such a type.  */
   10674            3 :               t = cxx_make_type (ENUMERAL_TYPE);
   10675            6 :               SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
   10676              :             }
   10677       790459 :           SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
   10678       790492 :           ENUM_FIXED_UNDERLYING_TYPE_P (t)
   10679       395246 :             = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
   10680              :         }
   10681    115405630 :       else if (CLASS_TYPE_P (template_type))
   10682              :         {
   10683              :           /* Lambda closures are regenerated in tsubst_lambda_expr, not
   10684              :              instantiated here.  */
   10685    230627946 :           gcc_assert (!LAMBDA_TYPE_P (template_type));
   10686              : 
   10687    115405630 :           t = make_class_type (TREE_CODE (template_type));
   10688    346216890 :           CLASSTYPE_DECLARED_CLASS (t)
   10689    115405630 :             = CLASSTYPE_DECLARED_CLASS (template_type);
   10690    115405630 :           SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
   10691              : 
   10692              :           /* A local class.  Make sure the decl gets registered properly.  */
   10693    115405630 :           if (context == current_function_decl)
   10694       628258 :             if (pushtag (DECL_NAME (gen_tmpl), t)
   10695       628258 :                 == error_mark_node)
   10696              :               return error_mark_node;
   10697              : 
   10698    115405618 :           if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
   10699              :             /* This instantiation is another name for the primary
   10700              :                template type. Set the TYPE_CANONICAL field
   10701              :                appropriately. */
   10702      6438944 :             TYPE_CANONICAL (t) = template_type;
   10703    108966674 :           else if (any_template_arguments_need_structural_equality_p (arglist))
   10704      1813680 :             SET_TYPE_STRUCTURAL_EQUALITY (t);
   10705              :         }
   10706              :       else
   10707            0 :         gcc_unreachable ();
   10708              : 
   10709              :       /* If we called start_enum or pushtag above, this information
   10710              :          will already be set up.  */
   10711    115800864 :       type_decl = TYPE_NAME (t);
   10712    115800864 :       if (!type_decl)
   10713              :         {
   10714    114777375 :           TYPE_CONTEXT (t) = FROB_CONTEXT (context);
   10715              : 
   10716    114777375 :           type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
   10717    114777375 :           DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
   10718    229554750 :           DECL_SOURCE_LOCATION (type_decl)
   10719    114777375 :             = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
   10720              :         }
   10721              : 
   10722    115800864 :       set_instantiating_module (type_decl);
   10723              :       /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
   10724              :          of export flag.  We want to propagate this because it might
   10725              :          be a friend declaration that pushes a new hidden binding.  */
   10726    115800864 :       DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
   10727              : 
   10728    115800864 :       if (CLASS_TYPE_P (template_type))
   10729              :         {
   10730    115405618 :           TREE_PRIVATE (type_decl)
   10731    115405618 :             = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
   10732    115405618 :           TREE_PROTECTED (type_decl)
   10733    115405618 :             = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
   10734    115405618 :           if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
   10735              :             {
   10736    114317985 :               DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
   10737    114317985 :               DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
   10738              :             }
   10739              :         }
   10740              : 
   10741    115800864 :       if (OVERLOAD_TYPE_P (t))
   10742              :         {
   10743              :           static const char *tags[] = {"abi_tag", "may_alias"};
   10744              : 
   10745    347402592 :           for (unsigned ix = 0; ix != 2; ix++)
   10746              :             {
   10747    231601728 :               tree attributes
   10748    231601728 :                 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
   10749              : 
   10750    231601728 :               if (attributes)
   10751           48 :                 TYPE_ATTRIBUTES (t)
   10752           96 :                   = tree_cons (TREE_PURPOSE (attributes),
   10753           48 :                                TREE_VALUE (attributes),
   10754           48 :                                TYPE_ATTRIBUTES (t));
   10755              :             }
   10756              :         }
   10757              : 
   10758              :       /* Let's consider the explicit specialization of a member
   10759              :          of a class template specialization that is implicitly instantiated,
   10760              :          e.g.:
   10761              :              template<class T>
   10762              :              struct S
   10763              :              {
   10764              :                template<class U> struct M {}; //#0
   10765              :              };
   10766              : 
   10767              :              template<>
   10768              :              template<>
   10769              :              struct S<int>::M<char> //#1
   10770              :              {
   10771              :                int i;
   10772              :              };
   10773              :         [temp.expl.spec]/4 says this is valid.
   10774              : 
   10775              :         In this case, when we write:
   10776              :         S<int>::M<char> m;
   10777              : 
   10778              :         M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
   10779              :         the one of #0.
   10780              : 
   10781              :         When we encounter #1, we want to store the partial instantiation
   10782              :         of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
   10783              : 
   10784              :         For all cases other than this "explicit specialization of member of a
   10785              :         class template", we just want to store the most general template into
   10786              :         the CLASSTYPE_TI_TEMPLATE of M.
   10787              : 
   10788              :         This case of "explicit specialization of member of a class template"
   10789              :         only happens when:
   10790              :         1/ the enclosing class is an instantiation of, and therefore not
   10791              :         the same as, the context of the most general template, and
   10792              :         2/ we aren't looking at the partial instantiation itself, i.e.
   10793              :         the innermost arguments are not the same as the innermost parms of
   10794              :         the most general template.
   10795              : 
   10796              :         So it's only when 1/ and 2/ happens that we want to use the partial
   10797              :         instantiation of the member template in lieu of its most general
   10798              :         template.  */
   10799              : 
   10800    115800864 :       if (PRIMARY_TEMPLATE_P (gen_tmpl)
   10801    227293162 :           && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
   10802              :           /* the enclosing class must be an instantiation...  */
   10803      2596203 :           && CLASS_TYPE_P (context)
   10804    118384440 :           && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
   10805              :         {
   10806      1429315 :           TREE_VEC_LENGTH (arglist)--;
   10807      1429315 :           ++processing_template_decl;
   10808      1429315 :           tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl));
   10809      1429315 :           tree partial_inst_args =
   10810      1429315 :             tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
   10811              :                     arglist, complain, NULL_TREE);
   10812      1429315 :           --processing_template_decl;
   10813      1429315 :           TREE_VEC_LENGTH (arglist)++;
   10814      1429315 :           if (partial_inst_args == error_mark_node)
   10815              :             return error_mark_node;
   10816      2858624 :           use_partial_inst_tmpl =
   10817              :             /*...and we must not be looking at the partial instantiation
   10818              :              itself. */
   10819      1429312 :             !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
   10820              :                                  partial_inst_args);
   10821              :         }
   10822              : 
   10823      1429312 :       if (!use_partial_inst_tmpl)
   10824              :         /* This case is easy; there are no member templates involved.  */
   10825              :         found = gen_tmpl;
   10826              :       else
   10827              :         {
   10828              :           /* This is a full instantiation of a member template.  Find
   10829              :              the partial instantiation of which this is an instance.  */
   10830              : 
   10831              :           /* Temporarily reduce by one the number of levels in the ARGLIST
   10832              :              so as to avoid comparing the last set of arguments.  */
   10833       807465 :           TREE_VEC_LENGTH (arglist)--;
   10834              :           /* We don't use COMPLAIN in the following call because this isn't
   10835              :              the immediate context of deduction.  For instance, tf_partial
   10836              :              could be set here as we might be at the beginning of template
   10837              :              argument deduction when any explicitly specified template
   10838              :              arguments are substituted into the function type.  tf_partial
   10839              :              could lead into trouble because we wouldn't find the partial
   10840              :              instantiation that might have been created outside tf_partial
   10841              :              context, because the levels of template parameters wouldn't
   10842              :              match, because in a tf_partial context, tsubst doesn't reduce
   10843              :              TEMPLATE_PARM_LEVEL.  */
   10844       807465 :           found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
   10845       807465 :           TREE_VEC_LENGTH (arglist)++;
   10846       807465 :           found = (TREE_CODE (found) == TEMPLATE_DECL
   10847       807465 :                    ? found
   10848            0 :                    : CLASSTYPE_TI_TEMPLATE (found));
   10849              : 
   10850       807465 :           if (DECL_CLASS_TEMPLATE_P (found)
   10851      1614930 :               && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
   10852              :             {
   10853              :               /* If this partial instantiation is specialized, we want to
   10854              :                  use it for hash table lookup.  */
   10855            9 :               elt.tmpl = found;
   10856            9 :               elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
   10857            9 :               elt.hash = 0; /* Recalculate after changing tmpl/args.  */
   10858              :             }
   10859              :         }
   10860              : 
   10861              :       /* Build template info for the new specialization.  */
   10862    115800861 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
   10863              : 
   10864    115800861 :       elt.spec = t;
   10865    115800861 :       slot = type_specializations->find_slot (&elt, INSERT);
   10866    115800861 :       gcc_checking_assert (*slot == NULL);
   10867    115800861 :       entry = ggc_alloc<spec_entry> ();
   10868    115800861 :       *entry = elt;
   10869    115800861 :       *slot = entry;
   10870              : 
   10871              :       /* Note this use of the partial instantiation so we can check it
   10872              :          later in maybe_process_partial_specialization.  */
   10873    231601722 :       DECL_TEMPLATE_INSTANTIATIONS (found)
   10874    115800861 :         = tree_cons (arglist, t,
   10875    115800861 :                      DECL_TEMPLATE_INSTANTIATIONS (found));
   10876              : 
   10877    115800861 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE
   10878    115800861 :           && !uses_template_parms (current_nonlambda_scope ()))
   10879              :         /* Now that the type has been registered on the instantiations
   10880              :            list, we set up the enumerators.  Because the enumeration
   10881              :            constants may involve the enumeration type itself, we make
   10882              :            sure to register the type first, and then create the
   10883              :            constants.  That way, doing tsubst_expr for the enumeration
   10884              :            constants won't result in recursive calls here; we'll find
   10885              :            the instantiation and exit above.  */
   10886       395246 :         tsubst_enum (template_type, t, arglist);
   10887              : 
   10888    115800816 :       if (CLASS_TYPE_P (template_type) && is_dependent_type)
   10889              :         /* If the type makes use of template parameters, the
   10890              :            code that generates debugging information will crash.  */
   10891     63167354 :         DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
   10892              : 
   10893              :       /* Possibly limit visibility based on template args.  */
   10894    115800816 :       TREE_PUBLIC (type_decl) = 1;
   10895    115800816 :       determine_visibility (type_decl);
   10896              : 
   10897    115800816 :       inherit_targ_abi_tags (t);
   10898              : 
   10899    115800816 :       return t;
   10900              :     }
   10901   1167945844 : }
   10902              : 
   10903              : /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.  */
   10904              : 
   10905              : tree
   10906     37538418 : lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
   10907              : {
   10908     37538418 :   tree gen_templ = most_general_template (templ);
   10909     37538418 :   tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
   10910     37538418 :   arglist = add_outermost_template_args (templ, arglist);
   10911     37538418 :   arglist = coerce_template_parms (parms, arglist, templ, complain);
   10912     37538418 :   if (arglist == error_mark_node)
   10913              :     return error_mark_node;
   10914              : 
   10915              :   /* The type of the expression is NULL_TREE since the template-id could refer
   10916              :      to an explicit or partial specialization. */
   10917     37538334 :   return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
   10918              : }
   10919              : 
   10920              : /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
   10921              :    not dependent.  */
   10922              : 
   10923              : tree
   10924     35536301 : finish_template_variable (tree var, tsubst_flags_t complain)
   10925              : {
   10926     35536301 :   tree templ = TREE_OPERAND (var, 0);
   10927     35536301 :   tree arglist = TREE_OPERAND (var, 1);
   10928              : 
   10929              :   /* If the template or arguments are dependent, then we
   10930              :      can't resolve the TEMPLATE_ID_EXPR yet.  */
   10931     35536301 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
   10932     35536301 :       || any_dependent_template_arguments_p (arglist))
   10933              :     return var;
   10934              : 
   10935     22149097 :   if (flag_concepts && !constraints_satisfied_p (templ, arglist))
   10936              :     {
   10937           21 :       if (complain & tf_error)
   10938              :         {
   10939           18 :           auto_diagnostic_group d;
   10940           18 :           error ("use of invalid variable template %qE", var);
   10941           18 :           diagnose_constraints (location_of (var), templ, arglist);
   10942           18 :         }
   10943           21 :       return error_mark_node;
   10944              :     }
   10945              : 
   10946     22149076 :   return instantiate_template (templ, arglist, complain);
   10947              : }
   10948              : 
   10949              : /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
   10950              :    TARGS template args, and instantiate it if it's not dependent.  */
   10951              : 
   10952              : tree
   10953     22827435 : lookup_and_finish_template_variable (tree templ, tree targs,
   10954              :                                      tsubst_flags_t complain)
   10955              : {
   10956     22827435 :   tree var = lookup_template_variable (templ, targs, complain);
   10957     22827435 :   if (var == error_mark_node)
   10958              :     return error_mark_node;
   10959     22827419 :   var = finish_template_variable (var, complain);
   10960     22827419 :   mark_used (var, complain);
   10961     22827419 :   return var;
   10962              : }
   10963              : 
   10964              : /* If the set of template parameters PARMS contains a template parameter
   10965              :    at the given LEVEL and INDEX, then return this parameter.  Otherwise
   10966              :    return NULL_TREE.  */
   10967              : 
   10968              : static tree
   10969     83047718 : corresponding_template_parameter_list (tree parms, int level, int index)
   10970              : {
   10971     89054859 :   while (TMPL_PARMS_DEPTH (parms) > level)
   10972      6007141 :     parms = TREE_CHAIN (parms);
   10973              : 
   10974     83047718 :   if (TMPL_PARMS_DEPTH (parms) != level
   10975     83047718 :       || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
   10976              :     return NULL_TREE;
   10977              : 
   10978     80234374 :   return TREE_VEC_ELT (TREE_VALUE (parms), index);
   10979              : }
   10980              : 
   10981              : /* Return the TREE_LIST for the template parameter from PARMS that positionally
   10982              :    corresponds to the template parameter PARM, or else return NULL_TREE.  */
   10983              : 
   10984              : static tree
   10985     83047718 : corresponding_template_parameter_list (tree parms, tree parm)
   10986              : {
   10987     83047718 :   int level, index;
   10988     83047718 :   template_parm_level_and_index (parm, &level, &index);
   10989     83047718 :   return corresponding_template_parameter_list (parms, level, index);
   10990              : }
   10991              : 
   10992              : /* As above, but pull out the actual parameter.  */
   10993              : 
   10994              : static tree
   10995     83046641 : corresponding_template_parameter (tree parms, tree parm)
   10996              : {
   10997     83046641 :   tree list = corresponding_template_parameter_list (parms, parm);
   10998     83046641 :   if (!list)
   10999              :     return NULL_TREE;
   11000              : 
   11001     80233297 :   tree t = TREE_VALUE (list);
   11002              :   /* As in template_parm_to_arg.  */
   11003     80233297 :   if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
   11004     78351878 :     t = TREE_TYPE (t);
   11005              :   else
   11006      1881419 :     t = DECL_INITIAL (t);
   11007              : 
   11008     80233297 :   gcc_assert (TEMPLATE_PARM_P (t));
   11009              :   return t;
   11010              : }
   11011              : 
   11012              : struct pair_fn_data
   11013              : {
   11014              :   tree_fn_t fn;
   11015              :   tree_fn_t any_fn;
   11016              :   void *data;
   11017              :   /* True when we should also visit template parameters that occur in
   11018              :      non-deduced contexts.  */
   11019              :   bool include_nondeduced_p;
   11020              :   hash_set<tree> *visited;
   11021              : };
   11022              : 
   11023              : /* Called from for_each_template_parm via walk_tree.  */
   11024              : 
   11025              : static tree
   11026   1011707601 : for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
   11027              : {
   11028   1011707601 :   tree t = *tp;
   11029   1011707601 :   struct pair_fn_data *pfd = (struct pair_fn_data *) d;
   11030   1011707601 :   tree_fn_t fn = pfd->fn;
   11031   1011707601 :   void *data = pfd->data;
   11032   1011707601 :   tree result = NULL_TREE;
   11033              : 
   11034              : #define WALK_SUBTREE(NODE)                                              \
   11035              :   do                                                                    \
   11036              :     {                                                                   \
   11037              :       result = for_each_template_parm (NODE, fn, data, pfd->visited, \
   11038              :                                        pfd->include_nondeduced_p,    \
   11039              :                                        pfd->any_fn);                 \
   11040              :       if (result) goto out;                                             \
   11041              :     }                                                                   \
   11042              :   while (0)
   11043              : 
   11044   1011707601 :   if (pfd->any_fn && (*pfd->any_fn)(t, data))
   11045              :     return t;
   11046              : 
   11047   1011707601 :   if (TYPE_P (t)
   11048    423845337 :       && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
   11049    423390890 :     WALK_SUBTREE (TYPE_CONTEXT (t));
   11050              : 
   11051   1011541277 :   switch (TREE_CODE (t))
   11052              :     {
   11053     75143341 :     case RECORD_TYPE:
   11054     75143341 :       if (TYPE_PTRMEMFUNC_P (t))
   11055              :         break;
   11056              :       /* Fall through.  */
   11057              : 
   11058     75699712 :     case UNION_TYPE:
   11059     75699712 :     case ENUMERAL_TYPE:
   11060     75699712 :       if (!TYPE_TEMPLATE_INFO (t))
   11061      8062823 :         *walk_subtrees = 0;
   11062              :       else
   11063     67636889 :         WALK_SUBTREE (TYPE_TI_ARGS (t));
   11064              :       break;
   11065              : 
   11066     10858517 :     case METHOD_TYPE:
   11067              :       /* Since we're not going to walk subtrees, we have to do this
   11068              :          explicitly here.  */
   11069     10858517 :       WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
   11070              :       /* Fall through.  */
   11071              : 
   11072      2338049 :     case FUNCTION_TYPE:
   11073              :       /* Check the return type.  */
   11074      2338049 :       WALK_SUBTREE (TREE_TYPE (t));
   11075              : 
   11076              :       /* Check the parameter types.  Since default arguments are not
   11077              :          instantiated until they are needed, the TYPE_ARG_TYPES may
   11078              :          contain expressions that involve template parameters.  But,
   11079              :          no-one should be looking at them yet.  And, once they're
   11080              :          instantiated, they don't contain template parameters, so
   11081              :          there's no point in looking at them then, either.  */
   11082      2116793 :       {
   11083      2116793 :         tree parm;
   11084              : 
   11085      7012011 :         for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
   11086      4941446 :           WALK_SUBTREE (TREE_VALUE (parm));
   11087              : 
   11088              :         /* Since we've already handled the TYPE_ARG_TYPES, we don't
   11089              :            want walk_tree walking into them itself.  */
   11090      2070565 :         *walk_subtrees = 0;
   11091              :       }
   11092              : 
   11093      2070565 :       if (flag_noexcept_type)
   11094              :         {
   11095      2064347 :           tree spec = TYPE_RAISES_EXCEPTIONS (t);
   11096      2064347 :           if (spec)
   11097       562059 :             WALK_SUBTREE (TREE_PURPOSE (spec));
   11098              :         }
   11099              :       break;
   11100              : 
   11101     13212641 :     case TYPEOF_TYPE:
   11102     13212641 :     case DECLTYPE_TYPE:
   11103     13212641 :       if (pfd->include_nondeduced_p
   11104     13212641 :           && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
   11105              :                                      pfd->visited,
   11106              :                                      pfd->include_nondeduced_p,
   11107              :                                      pfd->any_fn))
   11108         1766 :         return error_mark_node;
   11109     13210875 :       *walk_subtrees = false;
   11110     13210875 :       break;
   11111              : 
   11112            6 :     case TRAIT_TYPE:
   11113            6 :       if (pfd->include_nondeduced_p)
   11114              :         {
   11115            0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
   11116            0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
   11117              :         }
   11118            6 :       *walk_subtrees = false;
   11119            6 :       break;
   11120              : 
   11121      1468905 :     case FUNCTION_DECL:
   11122      1468905 :     case VAR_DECL:
   11123      1468905 :       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   11124       223599 :         WALK_SUBTREE (DECL_TI_ARGS (t));
   11125              :       break;
   11126              : 
   11127      6889069 :     case PARM_DECL:
   11128      6889069 :       WALK_SUBTREE (TREE_TYPE (t));
   11129              :       break;
   11130              : 
   11131       463335 :     case CONST_DECL:
   11132       463335 :       if (DECL_TEMPLATE_PARM_P (t))
   11133            0 :         WALK_SUBTREE (DECL_INITIAL (t));
   11134       463335 :       if (DECL_CONTEXT (t)
   11135       463335 :           && pfd->include_nondeduced_p)
   11136        45269 :         WALK_SUBTREE (DECL_CONTEXT (t));
   11137              :       break;
   11138              : 
   11139       296216 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   11140              :       /* Record template parameters such as `T' inside `TT<T>'.  */
   11141       296216 :       WALK_SUBTREE (TYPE_TI_ARGS (t));
   11142              :       /* Fall through.  */
   11143              : 
   11144    151805372 :     case TEMPLATE_TEMPLATE_PARM:
   11145    151805372 :     case TEMPLATE_TYPE_PARM:
   11146    151805372 :     case TEMPLATE_PARM_INDEX:
   11147    151805372 :       if (fn && (*fn)(t, data))
   11148              :         return t;
   11149    140501580 :       else if (!fn)
   11150            0 :         return t;
   11151              :       break;
   11152              : 
   11153     40788446 :     case TEMPLATE_DECL:
   11154              :       /* A template template parameter is encountered.  */
   11155     40788446 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   11156           32 :         WALK_SUBTREE (TREE_TYPE (t));
   11157              : 
   11158              :       /* Already substituted template template parameter */
   11159     40788446 :       *walk_subtrees = 0;
   11160     40788446 :       break;
   11161              : 
   11162     46553337 :     case TYPENAME_TYPE:
   11163              :       /* A template-id in a TYPENAME_TYPE might be a deduced context after
   11164              :          partial instantiation.  */
   11165     46553337 :       WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
   11166     46553337 :       *walk_subtrees = 0;
   11167     46553337 :       break;
   11168              : 
   11169     18342588 :     case INDIRECT_REF:
   11170     18342588 :     case COMPONENT_REF:
   11171              :       /* If there's no type, then this thing must be some expression
   11172              :          involving template parameters.  */
   11173     18342588 :       if (!fn && !TREE_TYPE (t))
   11174            0 :         return error_mark_node;
   11175              :       break;
   11176              : 
   11177      1784759 :     case CONSTRUCTOR:
   11178      1784759 :     case TRAIT_EXPR:
   11179      1784759 :     case PLUS_EXPR:
   11180      1784759 :     case MULT_EXPR:
   11181      1784759 :     case SCOPE_REF:
   11182              :       /* These are non-deduced contexts.  */
   11183      1784759 :       if (!pfd->include_nondeduced_p)
   11184       135658 :         *walk_subtrees = 0;
   11185              :       break;
   11186              : 
   11187      9582882 :     case MODOP_EXPR:
   11188      9582882 :     case CAST_EXPR:
   11189      9582882 :     case IMPLICIT_CONV_EXPR:
   11190      9582882 :     case REINTERPRET_CAST_EXPR:
   11191      9582882 :     case CONST_CAST_EXPR:
   11192      9582882 :     case STATIC_CAST_EXPR:
   11193      9582882 :     case DYNAMIC_CAST_EXPR:
   11194      9582882 :     case ARROW_EXPR:
   11195      9582882 :     case DOTSTAR_EXPR:
   11196      9582882 :     case TYPEID_EXPR:
   11197      9582882 :     case PSEUDO_DTOR_EXPR:
   11198      9582882 :       if (!fn)
   11199            0 :         return error_mark_node;
   11200              :       break;
   11201              : 
   11202           20 :     case SPLICE_SCOPE:
   11203           20 :       WALK_SUBTREE (SPLICE_SCOPE_EXPR (t));
   11204           20 :       *walk_subtrees = 0;
   11205           20 :       break;
   11206              : 
   11207              :     default:
   11208              :       break;
   11209              :     }
   11210              : 
   11211              :   #undef WALK_SUBTREE
   11212              : 
   11213              :   /* We didn't find any template parameters we liked.  */
   11214   1011707601 :  out:
   11215              :   return result;
   11216              : }
   11217              : 
   11218              : /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
   11219              :    BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
   11220              :    call FN with the parameter and the DATA.
   11221              :    If FN returns nonzero, the iteration is terminated, and
   11222              :    for_each_template_parm returns 1.  Otherwise, the iteration
   11223              :    continues.  If FN never returns a nonzero value, the value
   11224              :    returned by for_each_template_parm is 0.  If FN is NULL, it is
   11225              :    considered to be the function which always returns 1.
   11226              : 
   11227              :    If INCLUDE_NONDEDUCED_P, then this routine will also visit template
   11228              :    parameters that occur in non-deduced contexts.  When false, only
   11229              :    visits those template parameters that can be deduced.  */
   11230              : 
   11231              : static tree
   11232    839448792 : for_each_template_parm (tree t, tree_fn_t fn, void* data,
   11233              :                         hash_set<tree> *visited,
   11234              :                         bool include_nondeduced_p,
   11235              :                         tree_fn_t any_fn)
   11236              : {
   11237    839448792 :   struct pair_fn_data pfd;
   11238    839448792 :   tree result;
   11239              : 
   11240              :   /* Set up.  */
   11241    839448792 :   pfd.fn = fn;
   11242    839448792 :   pfd.any_fn = any_fn;
   11243    839448792 :   pfd.data = data;
   11244    839448792 :   pfd.include_nondeduced_p = include_nondeduced_p;
   11245              : 
   11246              :   /* Walk the tree.  (Conceptually, we would like to walk without
   11247              :      duplicates, but for_each_template_parm_r recursively calls
   11248              :      for_each_template_parm, so we would need to reorganize a fair
   11249              :      bit to use walk_tree_without_duplicates, so we keep our own
   11250              :      visited list.)  */
   11251    839448792 :   if (visited)
   11252    803025513 :     pfd.visited = visited;
   11253              :   else
   11254     36423279 :     pfd.visited = new hash_set<tree>;
   11255    839448792 :   result = cp_walk_tree (&t,
   11256              :                          for_each_template_parm_r,
   11257              :                          &pfd,
   11258              :                          pfd.visited);
   11259              : 
   11260              :   /* Clean up.  */
   11261    839448792 :   if (!visited)
   11262              :     {
   11263     72846558 :       delete pfd.visited;
   11264     36423279 :       pfd.visited = 0;
   11265              :     }
   11266              : 
   11267    839448792 :   return result;
   11268              : }
   11269              : 
   11270          580 : struct find_template_parameter_info
   11271              : {
   11272     49484220 :   explicit find_template_parameter_info (tree ctx_parms)
   11273     49484220 :     : ctx_parms (ctx_parms),
   11274     49484220 :       max_depth (TMPL_PARMS_DEPTH (ctx_parms))
   11275     49484220 :   {}
   11276              : 
   11277              :   hash_set<tree> visited;
   11278              :   hash_set<tree> parms;
   11279              :   tree parm_list = NULL_TREE;
   11280              :   tree *parm_list_tail = &parm_list;
   11281              :   tree ctx_parms;
   11282              :   int max_depth;
   11283              : 
   11284              :   tree find_in (tree);
   11285              :   tree find_in_recursive (tree);
   11286              :   bool found (tree);
   11287          580 :   unsigned num_found () { return parms.elements (); }
   11288              : };
   11289              : 
   11290              : /* Appends the declaration of T to the list in DATA.  */
   11291              : 
   11292              : static int
   11293     90147369 : keep_template_parm (tree t, void* data)
   11294              : {
   11295     90147369 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   11296              : 
   11297              :   /* Template parameters declared within the expression are not part of
   11298              :      the parameter mapping. For example, in this concept:
   11299              : 
   11300              :        template<typename T>
   11301              :        concept C = requires { <expr> } -> same_as<int>;
   11302              : 
   11303              :      the return specifier same_as<int> declares a new decltype parameter
   11304              :      that must not be part of the parameter mapping. The same is true
   11305              :      for generic lambda parameters, lambda template parameters, etc.  */
   11306     90147369 :   int level;
   11307     90147369 :   int index;
   11308     90147369 :   template_parm_level_and_index (t, &level, &index);
   11309     90147369 :   if (level == 0 || level > ftpi->max_depth)
   11310              :     return 0;
   11311              : 
   11312     83046641 :   if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
   11313              :     /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
   11314              :        BOUND_TEMPLATE_TEMPLATE_PARM itself.  */
   11315       230794 :     t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
   11316              : 
   11317              :   /* This template parameter might be an argument to a cached dependent
   11318              :      specalization that was formed earlier inside some other template, in
   11319              :      which case the parameter is not among the ones that are in-scope.
   11320              :      Look in CTX_PARMS to find the corresponding in-scope template
   11321              :      parameter, and use it instead.  */
   11322     83046641 :   if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
   11323     80233297 :     t = in_scope;
   11324              : 
   11325              :   /* Arguments like const T yield parameters like const T. This means that
   11326              :      a template-id like X<T, const T> would yield two distinct parameters:
   11327              :      T and const T. Adjust types to their unqualified versions.  */
   11328     83046641 :   if (TYPE_P (t))
   11329     81165222 :     t = TYPE_MAIN_VARIANT (t);
   11330     83046641 :   if (!ftpi->parms.add (t))
   11331              :     {
   11332              :       /* Append T to PARM_LIST.  */
   11333     74038722 :       tree node = build_tree_list (NULL_TREE, t);
   11334     74038722 :       *ftpi->parm_list_tail = node;
   11335     74038722 :       ftpi->parm_list_tail = &TREE_CHAIN (node);
   11336              :     }
   11337              : 
   11338              :   /* Verify the parameter we found has a valid index.  */
   11339     83046641 :   if (flag_checking)
   11340              :     {
   11341     83046641 :       tree parms = ftpi->ctx_parms;
   11342     89053782 :       while (TMPL_PARMS_DEPTH (parms) > level)
   11343      6007141 :         parms = TREE_CHAIN (parms);
   11344     83046641 :       if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
   11345     80233297 :         gcc_assert (index < len);
   11346              :     }
   11347              : 
   11348              :   return 0;
   11349              : }
   11350              : 
   11351              : /* Ensure that we recursively examine certain terms that are not normally
   11352              :    visited in for_each_template_parm_r.  */
   11353              : 
   11354              : static int
   11355    703448532 : any_template_parm_r (tree t, void *data)
   11356              : {
   11357    703448532 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   11358              : 
   11359              : #define WALK_SUBTREE(NODE)                                              \
   11360              :   do                                                                    \
   11361              :     {                                                                   \
   11362              :       for_each_template_parm (NODE, keep_template_parm, data,           \
   11363              :                               &ftpi->visited, true,                      \
   11364              :                               any_template_parm_r);                     \
   11365              :     }                                                                   \
   11366              :   while (0)
   11367              : 
   11368              :   /* A mention of a member alias/typedef is a use of all of its template
   11369              :      arguments, including those from the enclosing class, so we don't use
   11370              :      alias_template_specialization_p here.  */
   11371    703448532 :   if (TYPE_P (t) && typedef_variant_p (t))
   11372     10469777 :     if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
   11373     10469528 :       WALK_SUBTREE (TI_ARGS (tinfo));
   11374              : 
   11375    703448532 :   switch (TREE_CODE (t))
   11376              :     {
   11377     87934993 :     case TEMPLATE_TYPE_PARM:
   11378              :       /* Type constraints of a placeholder type may contain parameters.  */
   11379     87934993 :       if (is_auto (t))
   11380      9913980 :         if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
   11381      9870873 :           WALK_SUBTREE (constr);
   11382              :       break;
   11383              : 
   11384     42967685 :     case TEMPLATE_ID_EXPR:
   11385              :       /* Search through references to variable templates.  */
   11386     42967685 :       WALK_SUBTREE (TREE_OPERAND (t, 0));
   11387     42967685 :       WALK_SUBTREE (TREE_OPERAND (t, 1));
   11388     42967685 :       break;
   11389              : 
   11390              :     case TEMPLATE_PARM_INDEX:
   11391              :       /* No need to consider template parameters within the type of an NTTP:
   11392              :          substitution into an NTTP is done directly with the corresponding
   11393              :          template argument, and its type only comes into play earlier during
   11394              :          coercion.  */
   11395              :       break;
   11396              : 
   11397     40467104 :     case TEMPLATE_DECL:
   11398              :       /* If T is a member template that shares template parameters with
   11399              :          ctx_parms, we need to mark all those parameters for mapping.
   11400              :          To that end, it should suffice to just walk the DECL_CONTEXT of
   11401              :          the template (assuming the template is not overly general).  */
   11402     40467104 :       WALK_SUBTREE (DECL_CONTEXT (t));
   11403     40467104 :       break;
   11404              : 
   11405          345 :     case LAMBDA_EXPR:
   11406          345 :       {
   11407              :         /* TREE_STATIC on LAMBDA_EXPR_EXTRA_ARGS means a full set of
   11408              :            arguments, so we can just look there; they will replace
   11409              :            any template parms in the rest of the LAMBDA_EXPR.  */
   11410          345 :         if (tree args = LAMBDA_EXPR_EXTRA_ARGS (t))
   11411              :           {
   11412          251 :             WALK_SUBTREE (args);
   11413              :             /* Without TREE_STATIC the args are just outer levels, so we'd
   11414              :                still need to look through the lambda for just inner
   11415              :                parameters.  Hopefully that's not necessary.  */
   11416          251 :             gcc_checking_assert (TREE_STATIC (args));
   11417              :             return 0;
   11418              :           }
   11419              :         /* Look in the parms and body.  */
   11420           94 :         tree fn = lambda_function (t);
   11421           94 :         WALK_SUBTREE (TREE_TYPE (fn));
   11422           94 :         WALK_SUBTREE (DECL_SAVED_TREE (fn));
   11423              :       }
   11424           94 :       break;
   11425              : 
   11426     19820093 :     case IDENTIFIER_NODE:
   11427     19820093 :       if (IDENTIFIER_CONV_OP_P (t))
   11428              :         /* The conversion-type-id of a conversion operator may be dependent.  */
   11429         5950 :         WALK_SUBTREE (TREE_TYPE (t));
   11430              :       break;
   11431              : 
   11432           18 :     case CONVERT_EXPR:
   11433           18 :       if (is_dummy_object (t))
   11434           15 :         WALK_SUBTREE (TREE_TYPE (t));
   11435              :       break;
   11436              : 
   11437              :     default:
   11438              :       break;
   11439              :     }
   11440              : 
   11441              :   /* Keep walking.  */
   11442              :   return 0;
   11443              : }
   11444              : 
   11445              : /* Look through T for template parameters.  */
   11446              : 
   11447              : tree
   11448     49485297 : find_template_parameter_info::find_in (tree t)
   11449              : {
   11450     49485297 :   return for_each_template_parm (t, keep_template_parm, this, &visited,
   11451              :                                  /*include_nondeduced*/true,
   11452     49485297 :                                  any_template_parm_r);
   11453              : }
   11454              : 
   11455              : /* As above, but also recursively look into the default arguments of template
   11456              :    parameters we found.  Used for alias CTAD.  */
   11457              : 
   11458              : tree
   11459          580 : find_template_parameter_info::find_in_recursive (tree t)
   11460              : {
   11461          580 :   if (tree r = find_in (t))
   11462              :     return r;
   11463              :   /* Since newly found parms are added to the end of the list, we
   11464              :      can just walk it until we reach the end.  */
   11465         1657 :   for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
   11466              :     {
   11467         1077 :       tree parm = TREE_VALUE (pl);
   11468         1077 :       tree list = corresponding_template_parameter_list (ctx_parms, parm);
   11469         1077 :       if (tree r = find_in (TREE_PURPOSE (list)))
   11470              :         return r;
   11471              :     }
   11472              :   return NULL_TREE;
   11473              : }
   11474              : 
   11475              : /* True if PARM was found by a previous call to find_in.  PARM can be a
   11476              :    TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P.  */
   11477              : 
   11478              : bool
   11479         1158 : find_template_parameter_info::found (tree parm)
   11480              : {
   11481         1158 :   if (TREE_CODE (parm) == TREE_LIST)
   11482         1158 :     parm = TREE_VALUE (parm);
   11483         1158 :   if (TREE_CODE (parm) == TYPE_DECL
   11484         1158 :       || TREE_CODE (parm) == TEMPLATE_DECL)
   11485         1113 :     parm = TREE_TYPE (parm);
   11486              :   else
   11487           45 :     parm = DECL_INITIAL (parm);
   11488         1158 :   gcc_checking_assert (TEMPLATE_PARM_P (parm));
   11489         1158 :   return parms.contains (parm);
   11490              : }
   11491              : 
   11492              : /* Returns a list of unique template parameters found within T, where CTX_PARMS
   11493              :    are the template parameters in scope.  */
   11494              : 
   11495              : tree
   11496     49483734 : find_template_parameters (tree t, tree ctx_parms)
   11497              : {
   11498     49483734 :   if (!ctx_parms)
   11499              :     return NULL_TREE;
   11500              : 
   11501     49483640 :   find_template_parameter_info ftpi (ctx_parms);
   11502     49483640 :   ftpi.find_in (t);
   11503     49483640 :   return ftpi.parm_list;
   11504     49483640 : }
   11505              : 
   11506              : /* Returns true if T depends on any template parameter.  */
   11507              : 
   11508              : bool
   11509   4340338812 : uses_template_parms (tree t)
   11510              : {
   11511   4340338812 :   if (t == NULL_TREE || t == error_mark_node)
   11512              :     return false;
   11513              : 
   11514              :   /* Namespaces can't depend on any template parameters.  */
   11515   3995503466 :   if (TREE_CODE (t) == NAMESPACE_DECL)
   11516              :     return false;
   11517              : 
   11518   3995502273 :   processing_template_decl_sentinel ptds (/*reset*/false);
   11519   3995502273 :   ++processing_template_decl;
   11520              : 
   11521   3995502273 :   if (TYPE_P (t))
   11522   2165573729 :     return dependent_type_p (t);
   11523   1829928544 :   else if (TREE_CODE (t) == TREE_VEC)
   11524   1491757522 :     return any_dependent_template_arguments_p (t);
   11525    338171022 :   else if (TREE_CODE (t) == TREE_LIST)
   11526    150597458 :     return (uses_template_parms (TREE_VALUE (t))
   11527    150597458 :             || uses_template_parms (TREE_CHAIN (t)));
   11528    187573564 :   else if (TREE_CODE (t) == TYPE_DECL)
   11529           93 :     return dependent_type_p (TREE_TYPE (t));
   11530              :   else
   11531    187573471 :     return instantiation_dependent_expression_p (t);
   11532   3995502273 : }
   11533              : 
   11534              : /* Returns true if T depends on any template parameter with level LEVEL.  */
   11535              : 
   11536              : bool
   11537      2370881 : uses_template_parms_level (tree t, int level)
   11538              : {
   11539      2370881 :   return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
   11540      2370881 :                                  /*include_nondeduced_p=*/true);
   11541              : }
   11542              : 
   11543              : /* Returns true if the signature of DECL depends on any template parameter from
   11544              :    its enclosing class.  */
   11545              : 
   11546              : static bool
   11547    319842031 : uses_outer_template_parms (tree decl)
   11548              : {
   11549    319842031 :   int depth;
   11550    319842031 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11551       421977 :     depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
   11552              :   else
   11553    319420054 :     depth = template_class_depth (CP_DECL_CONTEXT (decl));
   11554    319842031 :   if (depth == 0)
   11555              :     return false;
   11556     11704197 :   if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
   11557              :                               &depth, NULL, /*include_nondeduced_p=*/true))
   11558              :     return true;
   11559      1017902 :   if (PRIMARY_TEMPLATE_P (decl)
   11560      1017902 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11561              :     {
   11562      1017902 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
   11563      2418849 :       for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
   11564              :         {
   11565      1400992 :           tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
   11566      1400992 :           tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
   11567      1400992 :           if (TREE_CODE (parm) == PARM_DECL
   11568      1400992 :               && for_each_template_parm (TREE_TYPE (parm),
   11569              :                                          template_parm_outer_level,
   11570              :                                          &depth, NULL, /*nondeduced*/true))
   11571              :             return true;
   11572      1400953 :           if (TREE_CODE (parm) == TEMPLATE_DECL
   11573      1400953 :               && uses_outer_template_parms (parm))
   11574              :             return true;
   11575      1400953 :           if (defarg
   11576      1400953 :               && for_each_template_parm (defarg, template_parm_outer_level,
   11577              :                                          &depth, NULL, /*nondeduced*/true))
   11578              :             return true;
   11579              :         }
   11580              :     }
   11581      1017857 :   if (uses_outer_template_parms_in_constraints (decl))
   11582              :     return true;
   11583              :   return false;
   11584              : }
   11585              : 
   11586              : /* Returns true if the constraints of DECL depend on any template parameters
   11587              :    from its enclosing scope.  */
   11588              : 
   11589              : bool
   11590      5348492 : uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/)
   11591              : {
   11592      5348492 :   tree ci = get_constraints (decl);
   11593      5348492 :   if (ci)
   11594        46653 :     ci = CI_ASSOCIATED_CONSTRAINTS (ci);
   11595        46653 :   if (!ci)
   11596              :     return false;
   11597        46653 :   if (!ctx)
   11598              :     {
   11599        17641 :       if (tree fc = DECL_FRIEND_CONTEXT (decl))
   11600              :         ctx = fc;
   11601              :       else
   11602          108 :         ctx = CP_DECL_CONTEXT (decl);
   11603              :     }
   11604        46653 :   int depth = template_class_depth (ctx);
   11605        46653 :   if (depth == 0)
   11606              :     return false;
   11607        46629 :   return for_each_template_parm (ci, template_parm_outer_level,
   11608        46629 :                                  &depth, NULL, /*nondeduced*/true);
   11609              : }
   11610              : 
   11611              : /* Returns TRUE iff INST is an instantiation we don't need to do in an
   11612              :    ill-formed translation unit, i.e. a variable or function that isn't
   11613              :    usable in a constant expression.  */
   11614              : 
   11615              : static inline bool
   11616      6784806 : neglectable_inst_p (tree d)
   11617              : {
   11618      6594299 :   return (d && DECL_P (d)
   11619      5762171 :           && !undeduced_auto_decl (d)
   11620     12436257 :           && !(TREE_CODE (d) == FUNCTION_DECL
   11621      5651451 :                ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
   11622       787250 :                : decl_maybe_constant_var_p (d)));
   11623              : }
   11624              : 
   11625              : /* Returns TRUE iff we should refuse to instantiate DECL because it's
   11626              :    neglectable and instantiated from within an erroneous instantiation.  */
   11627              : 
   11628              : static bool
   11629    135544985 : limit_bad_template_recursion (tree decl)
   11630              : {
   11631    135544985 :   struct tinst_level *lev = current_tinst_level;
   11632    135544985 :   int errs = errorcount + sorrycount;
   11633    135544985 :   if (errs == 0 || !neglectable_inst_p (decl))
   11634              :     return false;
   11635              : 
   11636              :   /* Avoid instantiating members of an ill-formed class.  */
   11637      2654531 :   bool refuse
   11638      5309015 :     = (DECL_CLASS_SCOPE_P (decl)
   11639      4487867 :        && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
   11640              : 
   11641              :   if (!refuse)
   11642              :     {
   11643      3026126 :       for (; lev; lev = lev->next)
   11644      4929119 :         if (neglectable_inst_p (lev->maybe_get_node ()))
   11645              :           break;
   11646      2653300 :       refuse = (lev && errs > lev->errors);
   11647              :     }
   11648              : 
   11649              :   if (refuse)
   11650              :     {
   11651              :       /* Don't warn about it not being defined.  */
   11652         3919 :       suppress_warning (decl, OPT_Wunused);
   11653         3919 :       tree clone;
   11654         6353 :       FOR_EACH_CLONE (clone, decl)
   11655         2434 :         suppress_warning (clone, OPT_Wunused);
   11656              :     }
   11657              :   return refuse;
   11658              : }
   11659              : 
   11660              : static int tinst_depth;
   11661              : extern int max_tinst_depth;
   11662              : int depth_reached;
   11663              : int tinst_dump_id;
   11664              : 
   11665              : static GTY(()) struct tinst_level *last_error_tinst_level;
   11666              : 
   11667              : /* We're starting to instantiate D; record the template instantiation context
   11668              :    at LOC for diagnostics and to restore it later.  */
   11669              : 
   11670              : bool
   11671    920438502 : push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
   11672              : {
   11673    920438502 :   struct tinst_level *new_level;
   11674              : 
   11675    920438502 :   if (tinst_depth >= max_tinst_depth)
   11676              :     {
   11677              :       /* Tell error.cc not to try to instantiate any templates.  */
   11678           48 :       at_eof = 3;
   11679           48 :       fatal_error (input_location,
   11680              :                    "template instantiation depth exceeds maximum of %d"
   11681              :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   11682              :                    max_tinst_depth);
   11683              :       return false;
   11684              :     }
   11685              : 
   11686              :   /* If the current instantiation caused problems, don't let it instantiate
   11687              :      anything else.  Do allow deduction substitution and decls usable in
   11688              :      constant expressions.  */
   11689    920438454 :   if (!targs && limit_bad_template_recursion (tldcl))
   11690              :     {
   11691              :       /* Avoid no_linkage_errors and unused function (and all other)
   11692              :          warnings for this decl.  */
   11693         2871 :       suppress_warning (tldcl);
   11694         2871 :       return false;
   11695              :     }
   11696              : 
   11697              :   /* When not -quiet, dump template instantiations other than functions, since
   11698              :      announce_function will take care of those.  */
   11699    920435583 :   if (!quiet_flag && !targs
   11700            0 :       && TREE_CODE (tldcl) != TREE_LIST
   11701            0 :       && TREE_CODE (tldcl) != FUNCTION_DECL)
   11702            0 :     fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
   11703              : 
   11704    920435583 :   new_level = tinst_level_freelist ().alloc ();
   11705    920435583 :   new_level->tldcl = tldcl;
   11706    920435583 :   new_level->targs = targs;
   11707    920435583 :   new_level->locus = loc;
   11708    920435583 :   new_level->errors = errorcount + sorrycount;
   11709    920435583 :   new_level->had_errors = false;
   11710    920435583 :   new_level->next = NULL;
   11711    920435583 :   new_level->refcount = 0;
   11712    920435583 :   new_level->path = new_level->visible = nullptr;
   11713    920435583 :   set_refcount_ptr (new_level->next, current_tinst_level);
   11714    920435583 :   set_refcount_ptr (current_tinst_level, new_level);
   11715              : 
   11716    920435583 :   if (cxx_dump_pretty_printer pp {tinst_dump_id})
   11717              :     {
   11718              : #if __GNUC__ >= 10
   11719            0 : #pragma GCC diagnostic push
   11720            0 : #pragma GCC diagnostic ignored "-Wformat-diag"
   11721              : #endif
   11722            0 :       bool list_p = new_level->list_p ();
   11723            0 :       if ((list_p || TREE_CODE (tldcl) == TEMPLATE_FOR_STMT)
   11724            0 :           && !pp.has_flag (TDF_DETAILS))
   11725              :         /* Skip non-instantiations unless -details.  */;
   11726              :       else
   11727              :         {
   11728            0 :           if (tinst_depth == 0)
   11729            0 :             pp_newline (&pp);
   11730            0 :           if (loc && pp.has_flag (TDF_LINENO))
   11731              :             {
   11732            0 :               for (int i = 0; i < tinst_depth; ++i)
   11733            0 :                 pp_space (&pp);
   11734            0 :               const expanded_location el = expand_location (loc);
   11735            0 :               pp_printf (&pp, "%s:%d:%d", el.file, el.line, el.column);
   11736            0 :               pp_newline (&pp);
   11737              :             }
   11738            0 :           for (int i = 0; i < tinst_depth; ++i)
   11739            0 :             pp_space (&pp);
   11740            0 :           if (TREE_CODE (tldcl) == TEMPLATE_FOR_STMT)
   11741            0 :             pp_printf (&pp, "I template for");
   11742            0 :           else if (list_p)
   11743            0 :             pp_printf (&pp, "S %S", new_level->get_node ());
   11744              :           else
   11745            0 :             pp_printf (&pp, "I %D", tldcl);
   11746            0 :           pp_newline (&pp);
   11747              :         }
   11748              : #if __GNUC__ >= 10
   11749              : #pragma GCC diagnostic pop
   11750              : #endif
   11751    920435583 :     }
   11752              : 
   11753    920435583 :   ++tinst_depth;
   11754    920435583 :   if (GATHER_STATISTICS && (tinst_depth > depth_reached))
   11755              :     depth_reached = tinst_depth;
   11756              : 
   11757    920435583 :   return true;
   11758              : }
   11759              : 
   11760              : /* We're starting substitution of TMPL<ARGS>; record the template
   11761              :    substitution context for diagnostics and to restore it later.  */
   11762              : 
   11763              : bool
   11764    804270514 : push_tinst_level (tree tmpl, tree args)
   11765              : {
   11766    804270514 :   return push_tinst_level_loc (tmpl, args, input_location);
   11767              : }
   11768              : 
   11769              : /* We're starting to instantiate D; record INPUT_LOCATION and the
   11770              :    template instantiation context for diagnostics and to restore it
   11771              :    later.  */
   11772              : 
   11773              : bool
   11774    116012952 : push_tinst_level (tree d)
   11775              : {
   11776    116012952 :   return push_tinst_level_loc (d, input_location);
   11777              : }
   11778              : 
   11779              : /* Likewise, but record LOC as the program location.  */
   11780              : 
   11781              : bool
   11782    116166742 : push_tinst_level_loc (tree d, location_t loc)
   11783              : {
   11784    116166742 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   11785    116166742 :   return push_tinst_level_loc (d, NULL, loc);
   11786              : }
   11787              : 
   11788              : /* We're done instantiating this template; return to the instantiation
   11789              :    context.  */
   11790              : 
   11791              : void
   11792    939784066 : pop_tinst_level (void)
   11793              : {
   11794              :   /* Restore the filename and line number stashed away when we started
   11795              :      this instantiation.  */
   11796    939784066 :   input_location = current_tinst_level->locus;
   11797    939784066 :   if (unsigned errs = errorcount + sorrycount)
   11798     21365538 :     if (errs > current_tinst_level->errors)
   11799       299605 :       current_tinst_level->had_errors = true;
   11800    939784066 :   set_refcount_ptr (current_tinst_level, current_tinst_level->next);
   11801    939784066 :   --tinst_depth;
   11802    939784066 : }
   11803              : 
   11804              : /* True if the instantiation represented by LEVEL is complete.  */
   11805              : 
   11806              : static bool
   11807     43933311 : tinst_complete_p (struct tinst_level *level)
   11808              : {
   11809     43933311 :   gcc_assert (!level->list_p ());
   11810     43933311 :   tree node = level->get_node ();
   11811     43933311 :   if (TYPE_P (node))
   11812            0 :     return COMPLETE_TYPE_P (node);
   11813              :   else
   11814     43933311 :     return (DECL_TEMPLATE_INSTANTIATED (node)
   11815     43933311 :             || DECL_TEMPLATE_SPECIALIZATION (node));
   11816              : }
   11817              : 
   11818              : /* We're instantiating a deferred template; restore the template
   11819              :    instantiation context in which the instantiation was requested, which
   11820              :    is one step out from LEVEL.  Return the corresponding DECL or TYPE.  */
   11821              : 
   11822              : static tree
   11823     19378270 : reopen_tinst_level (struct tinst_level *level)
   11824              : {
   11825     19378270 :   struct tinst_level *t;
   11826              : 
   11827     19378270 :   tinst_depth = 0;
   11828     95816732 :   for (t = level; t; t = t->next)
   11829     76438462 :     ++tinst_depth;
   11830              : 
   11831     19378270 :   set_refcount_ptr (current_tinst_level, level);
   11832     19378270 :   pop_tinst_level ();
   11833     19378270 :   if (current_tinst_level && !current_tinst_level->had_errors)
   11834     14568777 :     current_tinst_level->errors = errorcount+sorrycount;
   11835              : 
   11836     19378270 :   if (cxx_dump_pretty_printer pp {tinst_dump_id})
   11837              :     {
   11838              : #if __GNUC__ >= 10
   11839            0 : #pragma GCC diagnostic push
   11840            0 : #pragma GCC diagnostic ignored "-Wformat-diag"
   11841              : #endif
   11842              :       /* Dump the reopened instantiation context.  */
   11843            0 :       t = current_tinst_level;
   11844            0 :       if (!pp.has_flag (TDF_DETAILS))
   11845              :         /* Skip non-instantiations unless -details.  */
   11846            0 :         while (t && t->list_p ())
   11847            0 :           t = t->next;
   11848            0 :       if (t)
   11849              :         {
   11850            0 :           static tree last_ctx = NULL_TREE;
   11851            0 :           tree ctx = t->get_node ();
   11852            0 :           if (ctx != last_ctx)
   11853              :             {
   11854            0 :               last_ctx = ctx;
   11855            0 :               pp_newline (&pp);
   11856            0 :               if (TREE_CODE (t->tldcl) == TEMPLATE_FOR_STMT)
   11857            0 :                 pp_printf (&pp, "RI template for");
   11858            0 :               else if (t->list_p ())
   11859            0 :                 pp_printf (&pp, "RS %S", ctx);
   11860              :               else
   11861            0 :                 pp_printf (&pp, "RI %D", ctx);
   11862            0 :               pp_newline (&pp);
   11863              :             }
   11864              :         }
   11865              : #if __GNUC__ >= 10
   11866              : #pragma GCC diagnostic pop
   11867              : #endif
   11868     19378270 :     }
   11869              : 
   11870     19378270 :   tree decl = level->maybe_get_node ();
   11871     19378270 :   if (decl && modules_p ())
   11872              :     {
   11873              :       /* An instantiation is in module purview only if it had an explicit
   11874              :          instantiation definition in module purview; mark_decl_instantiated uses
   11875              :          set_instantiating_module to set the flag in that case.  */
   11876        64976 :       if (DECL_MODULE_PURVIEW_P (decl))
   11877        21737 :         module_kind |= MK_PURVIEW;
   11878              :       else
   11879        43239 :         module_kind &= ~MK_PURVIEW;
   11880              :     }
   11881     19378270 :   return decl;
   11882              : }
   11883              : 
   11884              : /* Returns the TINST_LEVEL which gives the original instantiation
   11885              :    context.  */
   11886              : 
   11887              : struct tinst_level *
   11888          147 : outermost_tinst_level (void)
   11889              : {
   11890          147 :   struct tinst_level *level = current_tinst_level;
   11891          147 :   if (level)
   11892           18 :     while (level->next)
   11893              :       level = level->next;
   11894          147 :   return level;
   11895              : }
   11896              : 
   11897              : /* True iff T is a friend function declaration that is not itself a template
   11898              :    and is not defined in a class template.  */
   11899              : 
   11900              : bool
   11901    159267811 : non_templated_friend_p (tree t)
   11902              : {
   11903    159267811 :   if (t && TREE_CODE (t) == FUNCTION_DECL
   11904    318185087 :       && DECL_UNIQUE_FRIEND_P (t))
   11905              :     {
   11906      2978394 :       tree ti = DECL_TEMPLATE_INFO (t);
   11907      2978394 :       if (!ti)
   11908              :         return true;
   11909              :       /* DECL_FRIEND_CONTEXT is set for a friend defined in class.  */
   11910      5956788 :       if (DECL_FRIEND_CONTEXT (t))
   11911              :         return false;
   11912              :       /* Non-templated friends in a class template are still represented with a
   11913              :          TEMPLATE_DECL; check that its primary template is the befriending
   11914              :          class.  Note that DECL_PRIMARY_TEMPLATE is null for
   11915              :          template <class T> friend A<T>::f(); */
   11916      1165659 :       tree tmpl = TI_TEMPLATE (ti);
   11917      1165659 :       tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
   11918      1165659 :       return ((primary || DECL_NAMESPACE_SCOPE_P (t)) && primary != tmpl);
   11919              :     }
   11920              :   else
   11921              :     return false;
   11922              : }
   11923              : 
   11924              : /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
   11925              :    vector of template arguments, as for tsubst.
   11926              : 
   11927              :    Returns an appropriate tsubst'd friend declaration.  */
   11928              : 
   11929              : static tree
   11930      2779673 : tsubst_friend_function (tree decl, tree args)
   11931              : {
   11932      2779673 :   tree new_friend;
   11933              : 
   11934      2779673 :   if (decl_specialization_friend_p (decl))
   11935              :     /* This was a friend declared with an explicit template
   11936              :        argument list, e.g.:
   11937              : 
   11938              :        friend void f<>(T);
   11939              : 
   11940              :        to indicate that f was a template instantiation, not a new
   11941              :        function declaration.  Now, we have to figure out what
   11942              :        instantiation of what template.  */
   11943              :     {
   11944        29176 :       tree template_id, arglist, fns;
   11945        29176 :       tree new_args;
   11946        29176 :       tree tmpl;
   11947        29176 :       tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
   11948              : 
   11949              :       /* Friend functions are looked up in the containing namespace scope.
   11950              :          We must enter that scope, to avoid finding member functions of the
   11951              :          current class with same name.  */
   11952        29176 :       push_nested_namespace (ns);
   11953        29176 :       fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
   11954              :                          tf_warning_or_error, NULL_TREE);
   11955        29176 :       pop_nested_namespace (ns);
   11956        29176 :       arglist = tsubst (DECL_TI_ARGS (decl), args,
   11957              :                         tf_warning_or_error, NULL_TREE);
   11958        29176 :       template_id = lookup_template_function (fns, arglist);
   11959              : 
   11960        29176 :       new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11961        29176 :       tmpl = determine_specialization (template_id, new_friend,
   11962              :                                        &new_args,
   11963              :                                        /*need_member_template=*/0,
   11964        29176 :                                        TREE_VEC_LENGTH (args),
   11965              :                                        tsk_none);
   11966        29176 :       return instantiate_template (tmpl, new_args, tf_error);
   11967              :     }
   11968              : 
   11969      2750497 :   new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11970      2750497 :   if (new_friend == error_mark_node)
   11971              :     return error_mark_node;
   11972              : 
   11973              :   /* The NEW_FRIEND will look like an instantiation, to the
   11974              :      compiler, but is not an instantiation from the point of view of
   11975              :      the language.  For example, we might have had:
   11976              : 
   11977              :      template <class T> struct S {
   11978              :        template <class U> friend void f(T, U);
   11979              :      };
   11980              : 
   11981              :      Then, in S<int>, template <class U> void f(int, U) is not an
   11982              :      instantiation of anything.  */
   11983              : 
   11984      2750497 :   DECL_USE_TEMPLATE (new_friend) = 0;
   11985      2750497 :   if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11986              :     {
   11987      1777383 :       DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
   11988      1777383 :       DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
   11989      3554766 :       DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
   11990      1777383 :         = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
   11991              : 
   11992              :       /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
   11993              :          match in decls_match.  */
   11994      1777383 :       tree parms = DECL_TEMPLATE_PARMS (new_friend);
   11995      1777383 :       tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
   11996      1777383 :       treqs = maybe_substitute_reqs_for (treqs, new_friend);
   11997      1777383 :       if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
   11998              :         {
   11999       199202 :           TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
   12000              :           /* As well as each TEMPLATE_PARM_CONSTRAINTS.  */
   12001       199202 :           tsubst_each_template_parm_constraints (parms, args,
   12002              :                                                  tf_warning_or_error);
   12003              :         }
   12004              :     }
   12005              : 
   12006              :   /* The mangled name for the NEW_FRIEND is incorrect.  The function
   12007              :      is not a template instantiation and should not be mangled like
   12008              :      one.  Therefore, we forget the mangling here; we'll recompute it
   12009              :      later if we need it.  */
   12010      2750497 :   if (TREE_CODE (new_friend) != TEMPLATE_DECL)
   12011              :     {
   12012       973114 :       SET_DECL_RTL (new_friend, NULL);
   12013       973114 :       SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
   12014              :     }
   12015              : 
   12016      2750497 :   if (DECL_NAMESPACE_SCOPE_P (new_friend))
   12017              :     {
   12018      2750422 :       tree old_decl;
   12019      2750422 :       tree ns;
   12020              : 
   12021              :       /* We must save some information from NEW_FRIEND before calling
   12022              :          duplicate decls since that function will free NEW_FRIEND if
   12023              :          possible.  */
   12024      2750422 :       tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
   12025      2750422 :       tree new_friend_result_template_info = NULL_TREE;
   12026      2750422 :       bool new_friend_is_defn =
   12027              :         (new_friend_template_info
   12028      2750422 :          && (DECL_INITIAL (DECL_TEMPLATE_RESULT
   12029              :                            (template_for_substitution (new_friend)))
   12030      2750422 :              != NULL_TREE));
   12031      2750422 :       tree not_tmpl = new_friend;
   12032              : 
   12033      2750422 :       if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   12034              :         {
   12035              :           /* This declaration is a `primary' template.  */
   12036      1777335 :           DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
   12037              : 
   12038      1777335 :           not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
   12039      1777335 :           new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
   12040              :         }
   12041              : 
   12042              :       /* We need to propagate module attachment for the new friend from the
   12043              :          owner of this template.  */
   12044      2750422 :       propagate_defining_module (new_friend, decl);
   12045              : 
   12046              :       /* Inside pushdecl_namespace_level, we will push into the
   12047              :          current namespace. However, the friend function should go
   12048              :          into the namespace of the template.  */
   12049      2750422 :       ns = decl_namespace_context (new_friend);
   12050      2750422 :       push_nested_namespace (ns);
   12051      2750422 :       old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
   12052      2750422 :       pop_nested_namespace (ns);
   12053              : 
   12054      2750422 :       if (old_decl == error_mark_node)
   12055              :         return error_mark_node;
   12056              : 
   12057      2750401 :       if (old_decl != new_friend)
   12058              :         {
   12059              :           /* This new friend declaration matched an existing
   12060              :              declaration.  For example, given:
   12061              : 
   12062              :                template <class T> void f(T);
   12063              :                template <class U> class C {
   12064              :                  template <class T> friend void f(T) {}
   12065              :                };
   12066              : 
   12067              :              the friend declaration actually provides the definition
   12068              :              of `f', once C has been instantiated for some type.  So,
   12069              :              old_decl will be the out-of-class template declaration,
   12070              :              while new_friend is the in-class definition.
   12071              : 
   12072              :              But, if `f' was called before this point, the
   12073              :              instantiation of `f' will have DECL_TI_ARGS corresponding
   12074              :              to `T' but not to `U', references to which might appear
   12075              :              in the definition of `f'.  Previously, the most general
   12076              :              template for an instantiation of `f' was the out-of-class
   12077              :              version; now it is the in-class version.  Therefore, we
   12078              :              run through all specialization of `f', adding to their
   12079              :              DECL_TI_ARGS appropriately.  In particular, they need a
   12080              :              new set of outer arguments, corresponding to the
   12081              :              arguments for this class instantiation.
   12082              : 
   12083              :              The same situation can arise with something like this:
   12084              : 
   12085              :                friend void f(int);
   12086              :                template <class T> class C {
   12087              :                  friend void f(T) {}
   12088              :                };
   12089              : 
   12090              :              when `C<int>' is instantiated.  Now, `f(int)' is defined
   12091              :              in the class.  */
   12092              : 
   12093      1015666 :           if (!new_friend_is_defn)
   12094              :             /* On the other hand, if the in-class declaration does
   12095              :                *not* provide a definition, then we don't want to alter
   12096              :                existing definitions.  We can just leave everything
   12097              :                alone.  */
   12098              :             ;
   12099              :           else
   12100              :             {
   12101           66 :               tree old_template = most_general_template (old_decl);
   12102           66 :               tree new_template = TI_TEMPLATE (new_friend_template_info);
   12103           66 :               tree new_args = TI_ARGS (new_friend_template_info);
   12104              : 
   12105              :               /* Overwrite whatever template info was there before, if
   12106              :                  any, with the new template information pertaining to
   12107              :                  the declaration.  */
   12108           66 :               DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
   12109              : 
   12110           66 :               if (TREE_CODE (old_decl) != TEMPLATE_DECL)
   12111              :                 {
   12112              :                   /* We should have called reregister_specialization in
   12113              :                      duplicate_decls.  */
   12114           33 :                   gcc_assert (retrieve_specialization (new_template,
   12115              :                                                        new_args, 0)
   12116              :                               == old_decl);
   12117              : 
   12118              :                   /* Instantiate it if the global has already been used.  */
   12119           33 :                   if (DECL_ODR_USED (old_decl))
   12120            3 :                     instantiate_decl (old_decl, /*defer_ok=*/true,
   12121              :                                       /*expl_inst_class_mem_p=*/false);
   12122              :                 }
   12123              :               else
   12124              :                 {
   12125           33 :                   tree t;
   12126              : 
   12127              :                   /* Indicate that the old function template is a partial
   12128              :                      instantiation.  */
   12129           33 :                   DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
   12130           33 :                     = new_friend_result_template_info;
   12131              : 
   12132           33 :                   gcc_assert (new_template
   12133              :                               == most_general_template (new_template));
   12134           33 :                   gcc_assert (new_template != old_decl);
   12135              : 
   12136              :                   /* Reassign any specializations already in the hash table
   12137              :                      to the new more general template, and add the
   12138              :                      additional template args.  */
   12139           33 :                   for (t = DECL_TEMPLATE_INSTANTIATIONS (old_template);
   12140          252 :                        t != NULL_TREE;
   12141          219 :                        t = TREE_CHAIN (t))
   12142              :                     {
   12143          219 :                       tree spec = TREE_VALUE (t);
   12144          219 :                       spec_entry elt;
   12145              : 
   12146          219 :                       elt.tmpl = old_decl;
   12147          219 :                       elt.args = DECL_TI_ARGS (spec);
   12148          219 :                       elt.spec = NULL_TREE;
   12149              : 
   12150          438 :                       if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (DECL_TI_ARGS (spec))
   12151          429 :                           && !is_specialization_of_friend (spec, new_template))
   12152          192 :                         continue;
   12153              : 
   12154           27 :                       decl_specializations->remove_elt (&elt);
   12155              : 
   12156           27 :                       tree& spec_args = DECL_TI_ARGS (spec);
   12157           54 :                       spec_args = add_outermost_template_args
   12158           27 :                         (new_args, INNERMOST_TEMPLATE_ARGS (spec_args));
   12159              : 
   12160           27 :                       register_specialization
   12161           27 :                         (spec, new_template, spec_args, true, 0);
   12162              : 
   12163              :                     }
   12164           33 :                   DECL_TEMPLATE_INSTANTIATIONS (old_template) = NULL_TREE;
   12165              :                 }
   12166              :             }
   12167              : 
   12168              :           /* The information from NEW_FRIEND has been merged into OLD_DECL
   12169              :              by duplicate_decls.  */
   12170              :           new_friend = old_decl;
   12171              :         }
   12172              : 
   12173              :       /* We've just introduced a namespace-scope function without
   12174              :          necessarily having opened the enclosing namespace, so
   12175              :          make sure the namespace is declared in this TU as well.  */
   12176      2750401 :       if (modules_p ())
   12177         7838 :         for (tree ctx = DECL_CONTEXT (new_friend);
   12178        17186 :              TREE_CODE (ctx) == NAMESPACE_DECL; ctx = DECL_CONTEXT (ctx))
   12179         9348 :           expose_existing_namespace (ctx);
   12180              :     }
   12181              :   else
   12182              :     {
   12183           75 :       tree context = DECL_CONTEXT (new_friend);
   12184           75 :       bool dependent_p;
   12185              : 
   12186              :       /* In the code
   12187              :            template <class T> class C {
   12188              :              template <class U> friend void C1<U>::f (); // case 1
   12189              :              friend void C2<T>::f ();                      // case 2
   12190              :            };
   12191              :          we only need to make sure CONTEXT is a complete type for
   12192              :          case 2.  To distinguish between the two cases, we note that
   12193              :          CONTEXT of case 1 remains dependent type after tsubst while
   12194              :          this isn't true for case 2.  */
   12195           75 :       ++processing_template_decl;
   12196           75 :       dependent_p = dependent_type_p (context);
   12197           75 :       --processing_template_decl;
   12198              : 
   12199           75 :       if (!dependent_p
   12200           75 :           && !complete_type_or_else (context, NULL_TREE))
   12201            0 :         return error_mark_node;
   12202              : 
   12203           75 :       if (COMPLETE_TYPE_P (context))
   12204              :         {
   12205           69 :           tree fn = new_friend;
   12206              :           /* do_friend adds the TEMPLATE_DECL for any member friend
   12207              :              template even if it isn't a member template, i.e.
   12208              :                template <class T> friend A<T>::f();
   12209              :              Look through it in that case.  */
   12210           69 :           if (TREE_CODE (fn) == TEMPLATE_DECL
   12211           69 :               && !PRIMARY_TEMPLATE_P (fn))
   12212           24 :             fn = DECL_TEMPLATE_RESULT (fn);
   12213              :           /* Check to see that the declaration is really present, and,
   12214              :              possibly obtain an improved declaration.  */
   12215           69 :           fn = check_classfn (context, fn, NULL_TREE);
   12216              : 
   12217           69 :           if (fn)
   12218      2779673 :             new_friend = fn;
   12219              :         }
   12220              :     }
   12221              : 
   12222              :   return new_friend;
   12223              : }
   12224              : 
   12225              : /* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
   12226              :    template arguments, as for tsubst.
   12227              : 
   12228              :    Returns an appropriate tsubst'd friend type or error_mark_node on
   12229              :    failure.  */
   12230              : 
   12231              : static tree
   12232       608718 : tsubst_friend_class (tree friend_tmpl, tree args)
   12233              : {
   12234       608718 :   tree tmpl;
   12235              : 
   12236       608718 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
   12237              :     {
   12238            3 :       tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
   12239            3 :       return TREE_TYPE (tmpl);
   12240              :     }
   12241              : 
   12242       608715 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) == 1)
   12243              :     /* The template has already been fully substituted, e.g. for
   12244              : 
   12245              :          template <typename> friend class ::C;
   12246              : 
   12247              :        so we can just return it directly.  */
   12248       134044 :     return TREE_TYPE (friend_tmpl);
   12249              : 
   12250       474671 :   tree context = CP_DECL_CONTEXT (friend_tmpl);
   12251       474671 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   12252       474175 :     push_nested_namespace (context);
   12253              :   else
   12254              :     {
   12255          496 :       context = tsubst (context, args, tf_error, NULL_TREE);
   12256          496 :       push_nested_class (context);
   12257              :     }
   12258              : 
   12259       474671 :   tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
   12260              :                       LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
   12261              : 
   12262       474671 :   if (!tmpl)
   12263              :     /* If we didn't find by name lookup, the type may still exist but as a
   12264              :        'hidden' import; we should check for this too to avoid accidentally
   12265              :        instantiating a duplicate.  */
   12266        12644 :     tmpl = lookup_imported_hidden_friend (friend_tmpl);
   12267              : 
   12268       474671 :   if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
   12269              :     {
   12270              :       /* The friend template has already been declared.  Just
   12271              :          check to see that the declarations match, and install any new
   12272              :          default parameters.  We must tsubst the default parameters,
   12273              :          of course.  We only need the innermost template parameters
   12274              :          because that is all that redeclare_class_template will look
   12275              :          at.  */
   12276              : 
   12277       462039 :       if (modules_p ())
   12278              :         /* Check that the existing declaration's module attachment is
   12279              :            compatible with the attachment of the friend template.  */
   12280         1714 :         module_may_redeclare (tmpl, friend_tmpl);
   12281              : 
   12282       462039 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (friend_tmpl))
   12283              :         {
   12284       462036 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
   12285              :                                               args, tf_warning_or_error);
   12286       462036 :           tsubst_each_template_parm_constraints (parms, args,
   12287              :                                                  tf_warning_or_error);
   12288       462036 :           location_t saved_input_location = input_location;
   12289       462036 :           input_location = DECL_SOURCE_LOCATION (friend_tmpl);
   12290       462036 :           tree cons = get_constraints (friend_tmpl);
   12291       462036 :           ++processing_template_decl;
   12292       462036 :           cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
   12293       462036 :                                          DECL_FRIEND_CONTEXT (friend_tmpl));
   12294       462036 :           --processing_template_decl;
   12295       462036 :           redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
   12296       462036 :           input_location = saved_input_location;
   12297              :         }
   12298              :     }
   12299              :   else
   12300              :     {
   12301              :       /* The friend template has not already been declared.  In this
   12302              :          case, the instantiation of the template class will cause the
   12303              :          injection of this template into the namespace scope.  */
   12304        12632 :       tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
   12305              : 
   12306        12632 :       if (tmpl != error_mark_node)
   12307              :         {
   12308              :           /* The new TMPL is not an instantiation of anything, so we
   12309              :              forget its origins.  It is also not a specialization of
   12310              :              anything.  We don't reset CLASSTYPE_TI_TEMPLATE
   12311              :              for the new type because that is supposed to be the
   12312              :              corresponding template decl, i.e., TMPL.  */
   12313        12627 :           spec_entry elt;
   12314        12627 :           elt.tmpl = friend_tmpl;
   12315        12627 :           elt.args = CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl));
   12316        12627 :           elt.spec = TREE_TYPE (tmpl);
   12317        12627 :           type_specializations->remove_elt (&elt);
   12318              : 
   12319        12627 :           DECL_USE_TEMPLATE (tmpl) = 0;
   12320        12627 :           DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
   12321        12627 :           CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
   12322        12627 :           CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
   12323        12627 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
   12324        12627 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = false;
   12325              : 
   12326              :           /* Substitute into and set the constraints on the new declaration.  */
   12327        12627 :           if (tree ci = get_constraints (friend_tmpl))
   12328              :             {
   12329            6 :               ++processing_template_decl;
   12330            6 :               ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
   12331            6 :                                            DECL_FRIEND_CONTEXT (friend_tmpl));
   12332            6 :               --processing_template_decl;
   12333            6 :               set_constraints (tmpl, ci);
   12334            6 :               tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
   12335              :                                                      args, tf_warning_or_error);
   12336              :             }
   12337              : 
   12338              :           /* We need to propagate the attachment of the original template to the
   12339              :              newly instantiated template type.  */
   12340        12627 :           propagate_defining_module (tmpl, friend_tmpl);
   12341              : 
   12342              :           /* Inject this template into the enclosing namespace scope.  */
   12343        12627 :           tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
   12344              :         }
   12345              :     }
   12346              : 
   12347       474671 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   12348       474175 :     pop_nested_namespace (context);
   12349              :   else
   12350          496 :     pop_nested_class ();
   12351              : 
   12352       474671 :   return TREE_TYPE (tmpl);
   12353              : }
   12354              : 
   12355              : /* Returns zero if TYPE cannot be completed later due to circularity.
   12356              :    Otherwise returns one.  */
   12357              : 
   12358              : static int
   12359      8353384 : can_complete_type_without_circularity (tree type)
   12360              : {
   12361      8353789 :   if (type == NULL_TREE || type == error_mark_node)
   12362              :     return 0;
   12363      8353789 :   else if (COMPLETE_TYPE_P (type))
   12364              :     return 1;
   12365       746693 :   else if (TREE_CODE (type) == ARRAY_TYPE)
   12366          405 :     return can_complete_type_without_circularity (TREE_TYPE (type));
   12367       746283 :   else if (CLASS_TYPE_P (type)
   12368      1492571 :            && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
   12369              :     return 0;
   12370              :   else
   12371       746277 :     return 1;
   12372              : }
   12373              : 
   12374              : static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
   12375              :                                 tsubst_flags_t, tree);
   12376              : 
   12377              : /* Instantiate the contract statement.  */
   12378              : 
   12379              : static tree
   12380          448 : tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
   12381              :                  tree in_decl)
   12382              : {
   12383          448 :   tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
   12384          448 :   bool auto_p  = type_uses_auto (type);
   12385              : 
   12386          448 :   tree r = copy_node (t);
   12387              : 
   12388              :   /* Rebuild the result variable, if present.  */
   12389          448 :   tree oldvar = NULL_TREE;
   12390          448 :   tree newvar = NULL_TREE;
   12391          448 :   if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
   12392              :     {
   12393           64 :       oldvar = POSTCONDITION_IDENTIFIER (t);
   12394           64 :       if (oldvar == error_mark_node)
   12395            0 :         return invalidate_contract (r);
   12396              : 
   12397           64 :       newvar = copy_node (oldvar);
   12398           64 :       TREE_TYPE (newvar) = type;
   12399           64 :       DECL_CONTEXT (newvar) = decl;
   12400           64 :       POSTCONDITION_IDENTIFIER (r) = newvar;
   12401              : 
   12402              :       /* Make sure the postcondition is valid.  */
   12403           64 :       location_t loc = DECL_SOURCE_LOCATION (oldvar);
   12404           64 :       if (!auto_p)
   12405           44 :         if (!check_postcondition_result (decl, type, loc))
   12406            3 :           return invalidate_contract (r);
   12407              :     }
   12408              : 
   12409              :   /* Instantiate the condition.  If the postcondition has a result binding
   12410              :      whose type is undeduced, process the expression as if inside a template to
   12411              :      avoid spurious type errors.  */
   12412          445 :   begin_scope (sk_contract, decl);
   12413          445 :   bool old_pc = processing_postcondition;
   12414          445 :   processing_postcondition = POSTCONDITION_P (t);
   12415          445 :   const bool undeduced_result_type_p = auto_p && newvar;
   12416          445 :   if (undeduced_result_type_p)
   12417           20 :     ++processing_template_decl;
   12418          445 :   if (newvar)
   12419              :     /* Make the variable available for lookup.  */
   12420           61 :     register_local_specialization (newvar, oldvar);
   12421              : 
   12422              :   /* Contract conditions have a wider application of location wrappers than
   12423              :      other trees (which will not work with the generic handling in tsubst_expr),
   12424              :      remove the wrapper here...  */
   12425          445 :   location_t cond_l = EXPR_LOCATION (CONTRACT_CONDITION (t));
   12426          445 :   tree cond_t = tree_strip_any_location_wrapper (CONTRACT_CONDITION (t));
   12427              : 
   12428              :   /* ... and substitute the contained expression.  */
   12429          445 :   cond_t = tsubst_expr (cond_t, args, complain, in_decl);
   12430              : 
   12431              :   /* Convert to bool, if possible, and then re-apply a location wrapper
   12432              :      when required.  */
   12433          445 :   cp_expr new_condition (cond_t, cond_l);
   12434          445 :   CONTRACT_CONDITION (r) = finish_contract_condition (new_condition);
   12435              : 
   12436              :   /* At present, the semantic, kind and comment cannot be dependent.  */
   12437          445 :   gcc_checking_assert
   12438              :     (!type_dependent_expression_p (CONTRACT_EVALUATION_SEMANTIC (r))
   12439              :      && !type_dependent_expression_p (CONTRACT_ASSERTION_KIND (r))
   12440              :      && !type_dependent_expression_p (CONTRACT_COMMENT (r)));
   12441              : 
   12442          445 :   if (undeduced_result_type_p)
   12443           20 :     --processing_template_decl;
   12444          445 :   processing_postcondition = old_pc;
   12445          445 :   gcc_checking_assert (scope_chain && scope_chain->bindings
   12446              :                        && scope_chain->bindings->kind == sk_contract);
   12447          445 :   pop_bindings_and_leave_scope ();
   12448              : 
   12449          445 :   return r;
   12450              : }
   12451              : 
   12452              : /* Instantiate the contract specifier CONTRACT, returning the substituted
   12453              :    contract statement.  */
   12454              : 
   12455              : static tree
   12456          446 : tsubst_contract_specifier (tree decl, tree contract, tree args,
   12457              :                            tsubst_flags_t complain, tree in_decl)
   12458              : {
   12459              :   /* For non-specializations, adjust the current declaration to the most general
   12460              :      version of in_decl. Because we defer the instantiation of contracts as long
   12461              :      as possible, they are still written in terms of the parameters (and return
   12462              :      type) of the most general template.  */
   12463          446 :   tree tmpl = DECL_TI_TEMPLATE (in_decl);
   12464          446 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
   12465          424 :     in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
   12466          446 :   local_specialization_stack specs (lss_copy);
   12467          446 :   register_parameter_specializations (in_decl, decl);
   12468              : 
   12469              :   /* Use the complete set of template arguments for instantiation. The
   12470              :      contract may not have been instantiated and still refer to outer levels
   12471              :      of template parameters.  */
   12472          446 :   args = DECL_TI_ARGS (decl);
   12473              : 
   12474              :   /* For member functions, make this available for semantic analysis.  */
   12475          446 :   tree save_ccp = current_class_ptr;
   12476          446 :   tree save_ccr = current_class_ref;
   12477          446 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
   12478              :     {
   12479          283 :       tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
   12480          283 :       tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
   12481          283 :       inject_this_parameter (this_type, cp_type_quals (this_type));
   12482              :     }
   12483              : 
   12484          446 :   contract = tsubst_contract (decl, contract, args, complain, in_decl);
   12485              : 
   12486          446 :   current_class_ptr = save_ccp;
   12487          446 :   current_class_ref = save_ccr;
   12488              : 
   12489          892 :   return contract;
   12490          446 : }
   12491              : 
   12492              : /* For the unsubstituted contract specifiers SPECIFIERS, instantiate the
   12493              :  contracts for DECL and set them as the contracts for DECL.  Substitution
   12494              :  creates a deep copy of the contract.  */
   12495              : 
   12496              : void
   12497          264 : tsubst_contract_specifiers (tree specifiers, tree decl, tree args,
   12498              :                             tsubst_flags_t complain, tree in_decl)
   12499              : {
   12500          264 :   if (!specifiers)
   12501              :     {
   12502            2 :       if (flag_contracts)
   12503            2 :         set_fn_contract_specifiers (decl, NULL_TREE);
   12504              :       return;
   12505              :     }
   12506              : 
   12507              :   /* SPECIFIERS may be shared with the pattern (see the copy in
   12508              :      tsubst_function_decl), so build a fresh vector rather than substituting
   12509              :      in place.  tsubst_contract () copies each statement it substitutes.  */
   12510          262 :   int len = TREE_VEC_LENGTH (specifiers);
   12511          262 :   tree subst_contracts = make_tree_vec (len);
   12512          970 :   for (int ix = 0; ix < len; ix++)
   12513          446 :     TREE_VEC_ELT (subst_contracts, ix)
   12514          892 :       = tsubst_contract_specifier (decl, TREE_VEC_ELT (specifiers, ix), args,
   12515              :                                    complain, in_decl);
   12516              : 
   12517          262 :   if (flag_contracts)
   12518          262 :     set_fn_contract_specifiers (decl, subst_contracts);
   12519              : }
   12520              : 
   12521              : /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
   12522              :    T or a new TREE_LIST, possibly a chain in the case of a pack expansion.  */
   12523              : 
   12524              : static tree
   12525      1153772 : tsubst_attribute (tree t, tree *decl_p, tree args,
   12526              :                   tsubst_flags_t complain, tree in_decl)
   12527              : {
   12528      1153772 :   gcc_assert (ATTR_IS_DEPENDENT (t));
   12529              : 
   12530      1153772 :   tree val = TREE_VALUE (t);
   12531      1153772 :   if (val == NULL_TREE)
   12532              :     /* Nothing to do.  */;
   12533       242840 :   else if ((flag_openmp || flag_openmp_simd)
   12534       243464 :            && is_attribute_p ("omp declare simd",
   12535          345 :                               get_attribute_name (t)))
   12536              :     {
   12537          105 :       tree clauses = TREE_VALUE (val);
   12538          105 :       clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
   12539              :                                     complain, in_decl);
   12540          105 :       c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   12541          105 :       clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   12542          105 :       tree parms = DECL_ARGUMENTS (*decl_p);
   12543          105 :       clauses
   12544          105 :         = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
   12545          105 :       if (clauses)
   12546          105 :         val = build_tree_list (NULL_TREE, clauses);
   12547              :       else
   12548              :         val = NULL_TREE;
   12549              :     }
   12550       243014 :   else if (flag_openmp
   12551       243254 :            && is_attribute_p ("omp declare variant base",
   12552          240 :                               get_attribute_name (t)))
   12553              :     {
   12554          213 :       ++cp_unevaluated_operand;
   12555          213 :       tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
   12556          213 :       --cp_unevaluated_operand;
   12557          213 :       tree chain = TREE_CHAIN (val);
   12558          213 :       location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
   12559          213 :       tree ctx = copy_list (TREE_VALUE (val));
   12560          213 :       tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain));
   12561          213 :       if (append_args_list
   12562           63 :           && TREE_VALUE (append_args_list)
   12563          276 :           && TREE_CHAIN (TREE_VALUE (append_args_list)))
   12564              :         {
   12565           54 :           append_args_list = TREE_VALUE (append_args_list);
   12566           54 :           append_args_list = TREE_VALUE (TREE_CHAIN (append_args_list));
   12567          153 :           for (; append_args_list;
   12568           99 :                append_args_list = TREE_CHAIN (append_args_list))
   12569              :              {
   12570           99 :               tree pref_list = TREE_VALUE (append_args_list);
   12571           99 :               if (pref_list == NULL_TREE || TREE_CODE (pref_list) != TREE_LIST)
   12572           63 :                 continue;
   12573           36 :               tree fr_list = TREE_VALUE (pref_list);
   12574           36 :               int len = TREE_VEC_LENGTH (fr_list);
   12575          105 :               for (int i = 0; i < len; i++)
   12576              :                 {
   12577           69 :                   tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
   12578              :                   /* Preserve NOP_EXPR to have a location.  */
   12579           69 :                   if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
   12580            0 :                     TREE_OPERAND (*fr_expr, 0)
   12581            0 :                       = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
   12582              :                                      in_decl);
   12583              :                   else
   12584           69 :                     *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
   12585              :                 }
   12586              :             }
   12587              :         }
   12588          423 :       for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
   12589              :         {
   12590          219 :           enum omp_tss_code set = OMP_TSS_CODE (tss);
   12591          219 :           tree selectors = NULL_TREE;
   12592          429 :           for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
   12593          210 :                ts = TREE_CHAIN (ts))
   12594              :             {
   12595          219 :               tree properties = NULL_TREE;
   12596          219 :               tree scoreval = NULL_TREE;
   12597              :               /* FIXME: The body of this loop should really be dispatching
   12598              :                  according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
   12599              :                  of having hard-wired knowledge of specific selectors.  */
   12600          219 :               if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
   12601          219 :                   && set == OMP_TRAIT_SET_CONSTRUCT)
   12602              :                 {
   12603            0 :                   tree clauses = OMP_TS_PROPERTIES (ts);
   12604            0 :                   clauses = tsubst_omp_clauses (clauses,
   12605              :                                                 C_ORT_OMP_DECLARE_SIMD, args,
   12606              :                                                 complain, in_decl);
   12607            0 :                   c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   12608            0 :                   clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   12609            0 :                   properties = clauses;
   12610              :                 }
   12611              :               else
   12612              :                 {
   12613          219 :                   tree v = OMP_TS_SCORE (ts);
   12614           45 :                   if (v)
   12615              :                     {
   12616           45 :                       v = tsubst_expr (v, args, complain, in_decl);
   12617           45 :                       v = fold_non_dependent_expr (v);
   12618           90 :                       if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   12619           87 :                           || TREE_CODE (v) != INTEGER_CST)
   12620              :                         {
   12621            3 :                           location_t loc
   12622            3 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   12623              :                                                   match_loc);
   12624            3 :                           error_at (loc, "score argument must be "
   12625              :                                     "constant integer expression");
   12626            3 :                           return NULL_TREE;
   12627              :                         }
   12628           42 :                       else if (tree_int_cst_sgn (v) < 0)
   12629              :                         {
   12630            3 :                           location_t loc
   12631            3 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   12632              :                                                   match_loc);
   12633            3 :                           error_at (loc, "score argument must be "
   12634              :                                     "non-negative");
   12635            3 :                           return NULL_TREE;
   12636              :                         }
   12637              :                       scoreval = v;
   12638              :                     }
   12639          213 :                   properties = copy_list (OMP_TS_PROPERTIES (ts));
   12640          563 :                   for (tree p = properties; p; p = TREE_CHAIN (p))
   12641          140 :                     if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
   12642           80 :                       continue;
   12643           60 :                     else if (OMP_TP_VALUE (p))
   12644              :                       {
   12645           60 :                         bool allow_string
   12646           60 :                           = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
   12647           60 :                              || set != OMP_TRAIT_SET_USER);
   12648           60 :                         tree v = OMP_TP_VALUE (p);
   12649           60 :                         if (TREE_CODE (v) == STRING_CST && allow_string)
   12650            0 :                           continue;
   12651           60 :                         v = tsubst_expr (v, args, complain, in_decl);
   12652           60 :                         v = fold_non_dependent_expr (v);
   12653          120 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   12654          117 :                             || !tree_fits_shwi_p (v))
   12655              :                           {
   12656            3 :                             location_t loc
   12657            3 :                               = cp_expr_loc_or_loc (OMP_TP_VALUE (p),
   12658              :                                                     match_loc);
   12659            3 :                             if (allow_string)
   12660            0 :                               error_at (loc, "property must be constant "
   12661              :                                              "integer expression or string "
   12662              :                                              "literal");
   12663              :                             else
   12664            3 :                               error_at (loc, "property must be constant "
   12665              :                                              "integer expression");
   12666              :                             return NULL_TREE;
   12667              :                           }
   12668           57 :                         OMP_TP_VALUE (p) = v;
   12669              :                       }
   12670              :                 }
   12671          210 :               selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
   12672              :                                                properties, selectors);
   12673              :             }
   12674          210 :           OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
   12675              :         }
   12676          204 :       if (varid == error_mark_node)
   12677              :         val = error_mark_node;
   12678              :       else
   12679          198 :         val = tree_cons (varid, ctx, chain);
   12680              :     }
   12681              :   /* If the first attribute argument is an identifier, don't
   12682              :      pass it through tsubst.  Attributes like mode, format,
   12683              :      cleanup and several target specific attributes expect it
   12684              :      unmodified.  */
   12685       242801 :   else if (get_attribute_namespace (t) == gnu_identifier
   12686       242801 :            && attribute_takes_identifier_p (get_attribute_name (t)))
   12687              :     {
   12688          109 :       tree chain
   12689          109 :         = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
   12690          109 :       if (chain != TREE_CHAIN (val))
   12691           12 :         val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
   12692              :     }
   12693       242692 :   else if (PACK_EXPANSION_P (val))
   12694              :     {
   12695              :       /* An attribute pack expansion.  */
   12696           25 :       tree purp = TREE_PURPOSE (t);
   12697           25 :       tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
   12698           25 :       if (pack == error_mark_node)
   12699              :         return error_mark_node;
   12700           22 :       int len = TREE_VEC_LENGTH (pack);
   12701           22 :       tree list = NULL_TREE;
   12702           22 :       tree *q = &list;
   12703           54 :       for (int i = 0; i < len; ++i)
   12704              :         {
   12705           32 :           tree elt = TREE_VEC_ELT (pack, i);
   12706           32 :           *q = build_tree_list (purp, elt);
   12707           32 :           q = &TREE_CHAIN (*q);
   12708              :         }
   12709           22 :       return list;
   12710              :     }
   12711              :   else
   12712       242667 :     val = tsubst_expr (val, args, complain, in_decl);
   12713              : 
   12714      1153738 :   if (val == error_mark_node)
   12715              :     return error_mark_node;
   12716      1153717 :   if (val != TREE_VALUE (t))
   12717       242329 :     return build_tree_list (TREE_PURPOSE (t), val);
   12718              :   return t;
   12719              : }
   12720              : 
   12721              : /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
   12722              :    unchanged or a new TREE_LIST chain.  */
   12723              : 
   12724              : static tree
   12725       395257 : tsubst_attributes (tree attributes, tree args,
   12726              :                    tsubst_flags_t complain, tree in_decl)
   12727              : {
   12728       395257 :   tree last_dep = NULL_TREE;
   12729              : 
   12730       395273 :   for (tree t = attributes; t; t = TREE_CHAIN (t))
   12731           36 :     if (ATTR_IS_DEPENDENT (t))
   12732              :       {
   12733           20 :         last_dep = t;
   12734           20 :         attributes = copy_list (attributes);
   12735           20 :         break;
   12736              :       }
   12737              : 
   12738       395257 :   if (last_dep)
   12739           40 :     for (tree *p = &attributes; *p; )
   12740              :       {
   12741           20 :         tree t = *p;
   12742           20 :         if (ATTR_IS_DEPENDENT (t))
   12743              :           {
   12744           20 :             tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
   12745           20 :             if (subst != t)
   12746              :               {
   12747            6 :                 *p = subst;
   12748            9 :                 while (*p)
   12749            3 :                   p = &TREE_CHAIN (*p);
   12750            6 :                 *p = TREE_CHAIN (t);
   12751            6 :                 continue;
   12752              :               }
   12753              :           }
   12754           14 :         p = &TREE_CHAIN (*p);
   12755              :       }
   12756              : 
   12757       395257 :   return attributes;
   12758              : }
   12759              : 
   12760              : /* Apply any attributes which had to be deferred until instantiation
   12761              :    time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
   12762              :    ARGS, COMPLAIN, IN_DECL are as tsubst.  Returns true normally,
   12763              :    false on error.  */
   12764              : 
   12765              : static bool
   12766   1482836953 : apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
   12767              :                                 tree args, tsubst_flags_t complain, tree in_decl)
   12768              : {
   12769   1482836953 :   tree t;
   12770   1482836953 :   tree *p;
   12771              : 
   12772   1482836953 :   if (attributes == NULL_TREE)
   12773              :     return true;
   12774              : 
   12775     35537711 :   if (DECL_P (*decl_p))
   12776              :     {
   12777     35302515 :       if (TREE_TYPE (*decl_p) == error_mark_node)
   12778              :         return false;
   12779     35302512 :       p = &DECL_ATTRIBUTES (*decl_p);
   12780              :       /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
   12781              :          to our attributes parameter.  */
   12782     35302512 :       gcc_assert (*p == attributes);
   12783              :     }
   12784       235196 :   else if (FUNC_OR_METHOD_TYPE_P (*decl_p)
   12785       214334 :            || (attr_flags & ATTR_FLAG_TYPE_IN_PLACE) == 0)
   12786              :     p = NULL;
   12787              :   else
   12788              :     {
   12789       214307 :       p = &TYPE_ATTRIBUTES (*decl_p);
   12790              :       /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
   12791              :          lookup_template_class_1, and should be preserved.  */
   12792       214307 :       gcc_assert (*p != attributes);
   12793       225061 :       while (*p)
   12794        10754 :         p = &TREE_CHAIN (*p);
   12795              :     }
   12796              : 
   12797              :   /* save_template_attributes puts the dependent attributes at the beginning of
   12798              :      the list; find the non-dependent ones.  */
   12799     36691460 :   for (t = attributes; t; t = TREE_CHAIN (t))
   12800     36329386 :     if (!ATTR_IS_DEPENDENT (t))
   12801              :       break;
   12802     35537708 :   tree nondep = t;
   12803              : 
   12804              :   /* Apply any non-dependent attributes.  */
   12805     35537708 :   if (p)
   12806     35516819 :     *p = nondep;
   12807        20889 :   else if (nondep)
   12808        20827 :     *decl_p = cp_build_type_attribute_variant (*decl_p, nondep);
   12809              : 
   12810     35537708 :   if (nondep == attributes)
   12811              :     return true;
   12812              : 
   12813              :   /* And then any dependent ones.  */
   12814      1153655 :   tree late_attrs = NULL_TREE;
   12815      1153655 :   tree *q = &late_attrs;
   12816      2307383 :   for (t = attributes; t != nondep; t = TREE_CHAIN (t))
   12817              :     {
   12818      1153752 :       *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
   12819      1153752 :       if (*q == error_mark_node)
   12820              :         return false;
   12821      1153728 :       if (*q == t)
   12822              :         {
   12823       911374 :           *q = copy_node (t);
   12824       911374 :           TREE_CHAIN (*q) = NULL_TREE;
   12825              :         }
   12826      2307460 :       while (*q)
   12827      1153732 :         q = &TREE_CHAIN (*q);
   12828              :     }
   12829              : 
   12830              :   /* cplus_decl_attributes can add some attributes implicitly.  For templates,
   12831              :      those attributes should have been added already when those templates were
   12832              :      parsed, and shouldn't be added based on from which context they are
   12833              :      first time instantiated.  */
   12834      1153631 :   auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
   12835      1153631 :   auto o2 = make_temp_override (optimization_current_node,
   12836      1153631 :                                 optimization_default_node);
   12837      1153631 :   auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
   12838      1153631 :   auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
   12839      1153631 :                                 NULL);
   12840      1153631 :   auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
   12841      1153631 :   auto o6 = make_temp_override (target_option_current_node,
   12842      1153631 :                                 target_option_default_node);
   12843              : 
   12844      1153631 :   cplus_decl_attributes (decl_p, late_attrs, attr_flags);
   12845              : 
   12846      1153631 :   return true;
   12847      1153631 : }
   12848              : 
   12849              : /* The template TMPL is being instantiated with the template arguments TARGS.
   12850              :    Perform the access checks that we deferred when parsing the template.  */
   12851              : 
   12852              : static void
   12853     65650418 : perform_instantiation_time_access_checks (tree tmpl, tree targs)
   12854              : {
   12855     65650418 :   unsigned i;
   12856     65650418 :   deferred_access_check *chk;
   12857              : 
   12858     65650418 :   if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
   12859     65650418 :     return;
   12860              : 
   12861    131300836 :   if (vec<deferred_access_check, va_gc> *access_checks
   12862     65650418 :       = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
   12863          105 :     FOR_EACH_VEC_ELT (*access_checks, i, chk)
   12864              :       {
   12865           57 :         tree decl = chk->decl;
   12866           57 :         tree diag_decl = chk->diag_decl;
   12867           57 :         tree type_scope = TREE_TYPE (chk->binfo);
   12868              : 
   12869           57 :         if (uses_template_parms (type_scope))
   12870            6 :           type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
   12871              : 
   12872              :         /* Make access check error messages point to the location
   12873              :            of the use of the typedef.  */
   12874           57 :         iloc_sentinel ils (chk->loc);
   12875           57 :         perform_or_defer_access_check (TYPE_BINFO (type_scope),
   12876              :                                        decl, diag_decl, tf_warning_or_error);
   12877           57 :       }
   12878              : }
   12879              : 
   12880              : /* If the template T that we're about to instantiate contained errors at
   12881              :    parse time that we downgraded into warnings or suppressed, diagnose the
   12882              :    error now to render the TU ill-formed (if the TU has not already been
   12883              :    deemed ill-formed by an earlier error).  */
   12884              : 
   12885              : static void
   12886    108599880 : maybe_diagnose_erroneous_template (tree t)
   12887              : {
   12888    108599880 :   if (erroneous_templates && !(seen_error) ())
   12889         1050 :     if (location_t *error_loc = erroneous_templates->get (t))
   12890              :       {
   12891           15 :         auto_diagnostic_group d;
   12892           15 :         location_t decl_loc = location_of (t);
   12893           15 :         error_at (decl_loc, "instantiating erroneous template");
   12894           15 :         inform (*error_loc, "first error appeared here");
   12895           15 :       }
   12896    108599880 : }
   12897              : 
   12898              : tree
   12899    997449799 : instantiate_class_template (tree type)
   12900              : {
   12901    997449799 :   auto_timevar tv (TV_TEMPLATE_INST);
   12902              : 
   12903    997449799 :   tree templ, args, pattern, t, member;
   12904    997449799 :   tree typedecl;
   12905    997449799 :   tree pbinfo;
   12906    997449799 :   tree base_list;
   12907    997449799 :   unsigned int saved_maximum_field_alignment;
   12908              : 
   12909    997449799 :   if (type == error_mark_node)
   12910              :     return error_mark_node;
   12911              : 
   12912   1984178973 :   if (COMPLETE_OR_OPEN_TYPE_P (type)
   12913   1050358042 :       || uses_template_parms (type))
   12914    954235789 :     return type;
   12915              : 
   12916              :   /* Figure out which template is being instantiated.  */
   12917     43214010 :   templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
   12918     43214010 :   gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
   12919              : 
   12920              :   /* Mark the type as in the process of being defined.  */
   12921     43214010 :   TYPE_BEING_DEFINED (type) = 1;
   12922              : 
   12923              :   /* We may be in the middle of deferred access check.  Disable
   12924              :      it now.  */
   12925     43214010 :   deferring_access_check_sentinel acs (dk_no_deferred);
   12926              : 
   12927              :   /* Determine what specialization of the original template to
   12928              :      instantiate.  */
   12929     43214010 :   t = most_specialized_partial_spec (type, tf_warning_or_error);
   12930     43214010 :   if (t == error_mark_node)
   12931              :     return error_mark_node;
   12932     43213995 :   else if (t)
   12933              :     {
   12934              :       /* This TYPE is actually an instantiation of a partial
   12935              :          specialization.  We replace the innermost set of ARGS with
   12936              :          the arguments appropriate for substitution.  For example,
   12937              :          given:
   12938              : 
   12939              :            template <class T> struct S {};
   12940              :            template <class T> struct S<T*> {};
   12941              : 
   12942              :          and supposing that we are instantiating S<int*>, ARGS will
   12943              :          presently be {int*} -- but we need {int}.  */
   12944      9131408 :       pattern = TREE_TYPE (TI_TEMPLATE (t));
   12945      9131408 :       args = TI_ARGS (t);
   12946              :     }
   12947              :   else
   12948              :     {
   12949     34082587 :       pattern = TREE_TYPE (templ);
   12950     34082587 :       args = CLASSTYPE_TI_ARGS (type);
   12951              :     }
   12952              : 
   12953              :   /* If the template we're instantiating is incomplete, then clearly
   12954              :      there's nothing we can do.  */
   12955     43213995 :   if (!COMPLETE_TYPE_P (pattern))
   12956              :     {
   12957              :       /* We can try again later.  */
   12958      1221083 :       TYPE_BEING_DEFINED (type) = 0;
   12959      1221083 :       return type;
   12960              :     }
   12961              : 
   12962              :   /* If we've recursively instantiated too many templates, stop.  */
   12963     41992912 :   if (! push_tinst_level (type))
   12964            0 :     return type;
   12965              : 
   12966     51124241 :   maybe_diagnose_erroneous_template (t ? TI_TEMPLATE (t) : templ);
   12967              : 
   12968     41992891 :   bool push_to_top = maybe_push_to_top_level (TYPE_MAIN_DECL (type));
   12969              : 
   12970     41992891 :   mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
   12971              : 
   12972              :   /* Use #pragma pack from the template context.  */
   12973     41992891 :   saved_maximum_field_alignment = maximum_field_alignment;
   12974     41992891 :   maximum_field_alignment = TYPE_PRECISION (pattern);
   12975              : 
   12976     41992891 :   SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
   12977              : 
   12978              :   /* Set the input location to the most specialized template definition.
   12979              :      This is needed if tsubsting causes an error.  */
   12980     41992891 :   typedecl = TYPE_MAIN_DECL (pattern);
   12981     41992891 :   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
   12982     41992891 :     DECL_SOURCE_LOCATION (typedecl);
   12983              : 
   12984     41992891 :   set_instantiating_module (TYPE_NAME (type));
   12985              : 
   12986     41992891 :   TYPE_PACKED (type) = TYPE_PACKED (pattern);
   12987     41992891 :   SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
   12988     41992891 :   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
   12989     41992891 :   CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
   12990     41992891 :   if (ANON_AGGR_TYPE_P (pattern))
   12991       107879 :     SET_ANON_AGGR_TYPE_P (type);
   12992     41992891 :   if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
   12993              :     {
   12994     41367412 :       CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
   12995     41367412 :       CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
   12996              :       /* Adjust visibility for template arguments.  */
   12997     41367412 :       determine_visibility (TYPE_MAIN_DECL (type));
   12998              :     }
   12999     41992891 :   if (CLASS_TYPE_P (type))
   13000     41992891 :     CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
   13001              : 
   13002     41992891 :   pbinfo = TYPE_BINFO (pattern);
   13003              : 
   13004              :   /* We should never instantiate a nested class before its enclosing
   13005              :      class; we need to look up the nested class by name before we can
   13006              :      instantiate it, and that lookup should instantiate the enclosing
   13007              :      class.  */
   13008     41992891 :   gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
   13009              :               || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
   13010              : 
   13011              :   /* When instantiating nested lambdas, ensure that they get the mangling
   13012              :      scope of the new class type.  */
   13013     41992891 :   start_lambda_scope (TYPE_NAME (type));
   13014              : 
   13015     41992891 :   base_list = NULL_TREE;
   13016              :   /* Defer access checking while we substitute into the types named in
   13017              :      the base-clause.  */
   13018     41992891 :   push_deferring_access_checks (dk_deferred);
   13019              : 
   13020              :   /* Push into the containing scope for name lookup (114804).  */
   13021     41992891 :   tree pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
   13022              : 
   13023     41992891 :   if (BINFO_N_BASE_BINFOS (pbinfo))
   13024              :     {
   13025              :       tree pbase_binfo;
   13026              :       int i;
   13027              : 
   13028              :       /* Substitute into each of the bases to determine the actual
   13029              :          basetypes.  */
   13030     44778674 :       for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
   13031              :         {
   13032     22659748 :           tree base;
   13033     22659748 :           tree access = BINFO_BASE_ACCESS (pbinfo, i);
   13034     22659748 :           tree annotations = NULL_TREE;
   13035     22659748 :           tree expanded_bases = NULL_TREE;
   13036     22659748 :           int idx, len = 1;
   13037              : 
   13038     22659748 :           if (i + BINFO_BASE_BINFOS (pbinfo)->length ()
   13039     22659748 :               < vec_safe_length (BINFO_BASE_ACCESSES (pbinfo)))
   13040           12 :             annotations
   13041           12 :               = BINFO_BASE_ACCESS (pbinfo,
   13042              :                                    i + BINFO_BASE_BINFOS (pbinfo)->length ());
   13043              : 
   13044     22659748 :           if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
   13045              :             {
   13046          551 :               expanded_bases
   13047          551 :                 = tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
   13048              :                                          args, tf_error, NULL_TREE);
   13049          551 :               if (expanded_bases == error_mark_node)
   13050            0 :                 continue;
   13051              : 
   13052          551 :               len = TREE_VEC_LENGTH (expanded_bases);
   13053              :             }
   13054              : 
   13055     22659748 :           if (annotations)
   13056            8 :             annotations = tsubst_attributes (annotations, args,
   13057              :                                              tf_warning_or_error, NULL_TREE);
   13058              : 
   13059     45320169 :           for (idx = 0; idx < len; idx++)
   13060              :             {
   13061     22660421 :               if (expanded_bases)
   13062              :                 /* Extract the already-expanded base class.  */
   13063         1224 :                 base = TREE_VEC_ELT (expanded_bases, idx);
   13064              :               else
   13065              :                 /* Substitute to figure out the base class.  */
   13066     22659197 :                 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
   13067              :                                NULL_TREE);
   13068              : 
   13069     22660421 :               if (base == error_mark_node)
   13070           38 :                 continue;
   13071              : 
   13072     22660383 :               tree acc = access;
   13073     22660383 :               if (annotations)
   13074              :                 /* Make sure each direct base from the pack has its unique
   13075              :                    set of annotations.  */
   13076           14 :                 acc = build_tree_list (access, idx == 0 ? annotations
   13077            4 :                                                : copy_list (annotations));
   13078     22660383 :               base_list = tree_cons (acc, base, base_list);
   13079     22660383 :               if (BINFO_VIRTUAL_P (pbase_binfo))
   13080        43992 :                 TREE_TYPE (base_list) = integer_type_node;
   13081              :             }
   13082              :         }
   13083              : 
   13084              :       /* The list is now in reverse order; correct that.  */
   13085     22118926 :       base_list = nreverse (base_list);
   13086              :     }
   13087              :   /* Now call xref_basetypes to set up all the base-class
   13088              :      information.  */
   13089     41992891 :   xref_basetypes (type, base_list);
   13090              : 
   13091     41992891 :   apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
   13092              :                                   (int) ATTR_FLAG_TYPE_IN_PLACE,
   13093              :                                   args, tf_error, NULL_TREE);
   13094     41992891 :   fixup_attribute_variants (type);
   13095              : 
   13096              :   /* Now that our base classes are set up, enter the scope of the
   13097              :      class, so that name lookups into base classes, etc. will work
   13098              :      correctly.  This is precisely analogous to what we do in
   13099              :      begin_class_definition when defining an ordinary non-template
   13100              :      class.  */
   13101     41992891 :   pushclass (type);
   13102              : 
   13103              :   /* Now check accessibility of the types named in its base-clause,
   13104              :      relative to the scope of the class.  */
   13105     41992891 :   pop_to_parent_deferring_access_checks ();
   13106              : 
   13107              :   /* A vector to hold members marked with attribute used. */
   13108     41992891 :   auto_vec<tree> used;
   13109              : 
   13110              :   /* Now members are processed in the order of declaration.  */
   13111     41992891 :   for (member = CLASSTYPE_DECL_LIST (pattern);
   13112    272766817 :        member; member = TREE_CHAIN (member))
   13113              :     {
   13114    230776677 :       tree t = TREE_VALUE (member);
   13115              : 
   13116    230776677 :       if (TREE_PURPOSE (member))
   13117              :         {
   13118    226809027 :           if (TYPE_P (t))
   13119              :             {
   13120      3803598 :               if (LAMBDA_TYPE_P (t))
   13121              :                 /* A closure type for a lambda in an NSDMI or default argument.
   13122              :                    Ignore it; it will be regenerated when needed.  */
   13123         7576 :                 continue;
   13124              : 
   13125              :               /* If the member is a class template, we've
   13126              :                  already substituted its type.  */
   13127      1752657 :               if (CLASS_TYPE_P (t)
   13128      3900027 :                   && CLASSTYPE_IS_TEMPLATE (t))
   13129       621866 :                 continue;
   13130              : 
   13131      1525504 :               tree newtag = tsubst (t, args, tf_error, NULL_TREE);
   13132      1525504 :               if (newtag == error_mark_node)
   13133            0 :                 continue;
   13134              : 
   13135      1525504 :               if (TREE_CODE (newtag) != ENUMERAL_TYPE)
   13136              :                 {
   13137      1130791 :                   tree name = TYPE_IDENTIFIER (t);
   13138              : 
   13139              :                   /* Now, install the tag.  We don't use pushtag
   13140              :                      because that does too much work -- creating an
   13141              :                      implicit typedef, which we've already done.  */
   13142      1130791 :                   set_identifier_type_value (name, TYPE_NAME (newtag));
   13143      1130791 :                   maybe_add_class_template_decl_list (type, newtag, false);
   13144      1130791 :                   TREE_PUBLIC (TYPE_NAME (newtag)) = true;
   13145      1130791 :                   determine_visibility (TYPE_NAME (newtag));
   13146              :                 }
   13147              :             }
   13148    224654081 :           else if (DECL_DECLARES_FUNCTION_P (t))
   13149              :             {
   13150    114064115 :               tree r;
   13151              : 
   13152    114064115 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13153     22509228 :                 ++processing_template_decl;
   13154    114064115 :               r = tsubst (t, args, tf_error, NULL_TREE);
   13155    114064115 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13156     22509228 :                 --processing_template_decl;
   13157              : 
   13158    114064115 :               set_current_access_from_decl (r);
   13159    114064115 :               finish_member_declaration (r);
   13160              :               /* Instantiate members marked with attribute used.  */
   13161    114064115 :               if (r != error_mark_node && DECL_PRESERVE_P (r))
   13162           24 :                 used.safe_push (r);
   13163    114064115 :               if (TREE_CODE (r) == FUNCTION_DECL
   13164    114064115 :                   && DECL_OMP_DECLARE_REDUCTION_P (r))
   13165          134 :                 cp_check_omp_declare_reduction (r);
   13166              :             }
   13167    110589966 :           else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
   13168    114393611 :                    && LAMBDA_TYPE_P (TREE_TYPE (t)))
   13169              :             /* A closure type for a lambda in an NSDMI or default argument.
   13170              :                Ignore it; it will be regenerated when needed.  */;
   13171              :           else
   13172              :             {
   13173              :               /* Build new TYPE_FIELDS.  */
   13174    110582386 :               if (TREE_CODE (t) == STATIC_ASSERT)
   13175      5477671 :                 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE);
   13176    105104715 :               else if (TREE_CODE (t) != CONST_DECL)
   13177              :                 {
   13178    105104715 :                   tree r;
   13179    105104715 :                   tree vec = NULL_TREE;
   13180    105104715 :                   int len = 1;
   13181              : 
   13182    105104715 :                   gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
   13183              :                   /* The file and line for this declaration, to
   13184              :                      assist in error message reporting.  Since we
   13185              :                      called push_tinst_level above, we don't need to
   13186              :                      restore these.  */
   13187    105104715 :                   input_location = DECL_SOURCE_LOCATION (t);
   13188              : 
   13189    105104715 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   13190      3224120 :                     ++processing_template_decl;
   13191    105104715 :                   r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
   13192    105104664 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   13193      3224120 :                     --processing_template_decl;
   13194              : 
   13195    105104664 :                   if (TREE_CODE (r) == TREE_VEC)
   13196              :                     {
   13197              :                       /* A capture pack became multiple fields.  */
   13198          293 :                       vec = r;
   13199          293 :                       len = TREE_VEC_LENGTH (vec);
   13200              :                     }
   13201              : 
   13202    210207056 :                   for (int i = 0; i < len; ++i)
   13203              :                     {
   13204    105105092 :                       if (vec)
   13205          721 :                         r = TREE_VEC_ELT (vec, i);
   13206    105105092 :                       if (VAR_P (r))
   13207              :                         {
   13208              :                           /* In [temp.inst]:
   13209              : 
   13210              :                              [t]he initialization (and any associated
   13211              :                              side-effects) of a static data member does
   13212              :                              not occur unless the static data member is
   13213              :                              itself used in a way that requires the
   13214              :                              definition of the static data member to
   13215              :                              exist.
   13216              : 
   13217              :                              Therefore, we do not substitute into the
   13218              :                              initialized for the static data member here.  */
   13219      3965121 :                           finish_static_data_member_decl
   13220      3965121 :                             (r,
   13221              :                              /*init=*/NULL_TREE,
   13222              :                              /*init_const_expr_p=*/false,
   13223              :                              /*asmspec_tree=*/NULL_TREE,
   13224              :                              /*flags=*/0);
   13225              :                           /* Instantiate members marked with attribute used. */
   13226      3965121 :                           if (r != error_mark_node && DECL_PRESERVE_P (r))
   13227            6 :                             used.safe_push (r);
   13228              :                         }
   13229    101139971 :                       else if (TREE_CODE (r) == FIELD_DECL)
   13230              :                         {
   13231              :                           /* Determine whether R has a valid type and can be
   13232              :                              completed later.  If R is invalid, then its type
   13233              :                              is replaced by error_mark_node.  */
   13234      8353384 :                           tree rtype = TREE_TYPE (r);
   13235      8353384 :                           if (can_complete_type_without_circularity (rtype))
   13236      8353373 :                             complete_type (rtype);
   13237              : 
   13238      8350684 :                           if (!complete_or_array_type_p (rtype))
   13239              :                             {
   13240              :                               /* If R's type couldn't be completed and
   13241              :                                  it isn't a flexible array member (whose
   13242              :                                  type is incomplete by definition) give
   13243              :                                  an error.  */
   13244           31 :                               cxx_incomplete_type_error (r, rtype);
   13245           31 :                               TREE_TYPE (r) = error_mark_node;
   13246              :                             }
   13247      8350653 :                           else if (TREE_CODE (rtype) == ARRAY_TYPE
   13248       228975 :                                    && TYPE_DOMAIN (rtype) == NULL_TREE
   13249      8350653 :                                    && (TREE_CODE (type) == UNION_TYPE
   13250           48 :                                        || TREE_CODE (type) == QUAL_UNION_TYPE))
   13251              :                             {
   13252            4 :                               error ("flexible array member %qD in union", r);
   13253            4 :                               TREE_TYPE (r) = error_mark_node;
   13254              :                             }
   13255      8350649 :                           else if (!verify_type_context (input_location,
   13256              :                                                          TCTX_FIELD, rtype))
   13257            0 :                             TREE_TYPE (r) = error_mark_node;
   13258              :                         }
   13259              : 
   13260              :                       /* If it is a TYPE_DECL for a class-scoped
   13261              :                          ENUMERAL_TYPE, such a thing will already have
   13262              :                          been added to the field list by tsubst_enum
   13263              :                          in finish_member_declaration case above.  */
   13264    105102392 :                       if (!(TREE_CODE (r) == TYPE_DECL
   13265     87723007 :                             && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
   13266       645886 :                             && DECL_ARTIFICIAL (r)))
   13267              :                         {
   13268    104707679 :                           set_current_access_from_decl (r);
   13269    104707679 :                           finish_member_declaration (r);
   13270              :                         }
   13271              :                     }
   13272              :                 }
   13273              :             }
   13274              :         }
   13275              :       else
   13276              :         {
   13277      3967650 :           if (TREE_CODE (t) == TU_LOCAL_ENTITY)
   13278              :             /* Ignore.  */;
   13279      3388391 :           else if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
   13280      6747284 :                    || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   13281              :             {
   13282              :               /* Build new CLASSTYPE_FRIEND_CLASSES.  */
   13283              : 
   13284      1187935 :               tree friend_type = t;
   13285      1187935 :               if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   13286              :                 {
   13287              :                   /* template <class T> friend class C;  */
   13288       608718 :                   friend_type = tsubst_friend_class (friend_type, args);
   13289              :                 }
   13290       579217 :               else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
   13291              :                 {
   13292              :                   /* template <class T> friend class C::D;  */
   13293           15 :                   friend_type = tsubst (friend_type, args,
   13294              :                                         tf_warning_or_error, NULL_TREE);
   13295           15 :                   if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   13296           12 :                     friend_type = TREE_TYPE (friend_type);
   13297              :                 }
   13298       579202 :               else if (TREE_CODE (friend_type) == TYPENAME_TYPE
   13299       579202 :                        || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
   13300              :                 {
   13301              :                   /* This could be either
   13302              : 
   13303              :                        friend class T::C;
   13304              : 
   13305              :                      when dependent_type_p is false or
   13306              : 
   13307              :                        template <class U> friend class T::C;
   13308              : 
   13309              :                      otherwise.  */
   13310              :                   /* Bump processing_template_decl in case this is something like
   13311              :                      template <class T> friend struct A<T>::B.  */
   13312         2176 :                   ++processing_template_decl;
   13313         2176 :                   friend_type = tsubst (friend_type, args,
   13314              :                                         tf_warning_or_error, NULL_TREE);
   13315         2176 :                   --processing_template_decl;
   13316              :                 }
   13317       577026 :               else if (PACK_EXPANSION_P (friend_type))
   13318              :                 {
   13319           24 :                   friend_type = tsubst_pack_expansion (friend_type, args,
   13320              :                                                        tf_warning_or_error,
   13321              :                                                        NULL_TREE);
   13322           24 :                   if (friend_type != error_mark_node)
   13323              :                     {
   13324           24 :                       unsigned int len = TREE_VEC_LENGTH (friend_type);
   13325           63 :                       for (unsigned int idx = 0; idx < len; ++idx)
   13326           39 :                         if (TREE_VEC_ELT (friend_type, idx) != error_mark_node)
   13327           39 :                           make_friend_class (type,
   13328           39 :                                              TREE_VEC_ELT (friend_type, idx),
   13329              :                                              /*complain=*/false);
   13330              :                     }
   13331           24 :                   friend_type = error_mark_node;
   13332              :                 }
   13333       577002 :               else if (uses_template_parms (friend_type))
   13334              :                 /* friend class C<T>;  */
   13335       545027 :                 friend_type = tsubst (friend_type, args,
   13336              :                                       tf_warning_or_error, NULL_TREE);
   13337              : 
   13338              :               /* Otherwise it's
   13339              : 
   13340              :                    friend class C;
   13341              : 
   13342              :                  where C is already declared or
   13343              : 
   13344              :                    friend class C<int>;
   13345              : 
   13346              :                  We don't have to do anything in these cases.  */
   13347              : 
   13348      1187935 :               if (friend_type != error_mark_node)
   13349      1187891 :                 make_friend_class (type, friend_type, /*complain=*/false);
   13350              :             }
   13351              :           else
   13352              :             {
   13353              :               /* Build new DECL_FRIENDLIST.  */
   13354      2779673 :               tree r;
   13355              : 
   13356              :               /* The file and line for this declaration, to
   13357              :                  assist in error message reporting.  Since we
   13358              :                  called push_tinst_level above, we don't need to
   13359              :                  restore these.  */
   13360      2779673 :               input_location = DECL_SOURCE_LOCATION (t);
   13361              : 
   13362      2779673 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13363              :                 {
   13364      1777383 :                   ++processing_template_decl;
   13365      1777383 :                   push_deferring_access_checks (dk_no_check);
   13366              :                 }
   13367              : 
   13368      2779673 :               r = tsubst_friend_function (t, args);
   13369      2779673 :               add_friend (type, r, /*complain=*/false);
   13370      2779673 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   13371              :                 {
   13372      1777383 :                   pop_deferring_access_checks ();
   13373      1777383 :                   --processing_template_decl;
   13374              :                 }
   13375              :             }
   13376              :         }
   13377              :     }
   13378              : 
   13379              :   /* Set the file and line number information to whatever is given for
   13380              :      the class itself.  This puts error messages involving generated
   13381              :      implicit functions at a predictable point, and the same point
   13382              :      that would be used for non-template classes.  */
   13383     41990140 :   input_location = DECL_SOURCE_LOCATION (typedecl);
   13384              : 
   13385     41990140 :   unreverse_member_declarations (type);
   13386     41990140 :   finish_struct_1 (type);
   13387     41990140 :   TYPE_BEING_DEFINED (type) = 0;
   13388              : 
   13389     41990140 :   finish_lambda_scope ();
   13390              : 
   13391              :   /* Remember if instantiating this class ran into errors, so we can avoid
   13392              :      instantiating member functions in limit_bad_template_recursion.  We set
   13393              :      this flag even if the problem was in another instantiation triggered by
   13394              :      this one, as that will likely also cause trouble for member functions.  */
   13395     41990140 :   if (errorcount + sorrycount > current_tinst_level->errors)
   13396         1488 :     CLASSTYPE_ERRONEOUS (type) = true;
   13397              : 
   13398              :   /* We don't instantiate default arguments for member functions.  14.7.1:
   13399              : 
   13400              :      The implicit instantiation of a class template specialization causes
   13401              :      the implicit instantiation of the declarations, but not of the
   13402              :      definitions or default arguments, of the class member functions,
   13403              :      member classes, static data members and member templates....  */
   13404              : 
   13405     41990140 :   perform_instantiation_time_access_checks (pattern, args);
   13406     41990140 :   perform_deferred_access_checks (tf_warning_or_error);
   13407              : 
   13408              :   /* Now that we've gone through all the members, instantiate those
   13409              :      marked with attribute used.  We must do this in the context of
   13410              :      the class -- not the context we pushed from, as that might be
   13411              :      inside a template and change the behaviour of mark_used.  */
   13412     41990224 :   for (tree x : used)
   13413           30 :     mark_used (x);
   13414              : 
   13415     41990140 :   popclass ();
   13416     41990140 :   pop_scope (pushed_scope);
   13417     41990140 :   maximum_field_alignment = saved_maximum_field_alignment;
   13418     41990140 :   maybe_pop_from_top_level (push_to_top);
   13419     41990140 :   pop_tinst_level ();
   13420              : 
   13421              :   /* The vtable for a template class can be emitted in any translation
   13422              :      unit in which the class is instantiated.  When there is no key
   13423              :      method, however, finish_struct_1 will already have added TYPE to
   13424              :      the keyed_classes.  */
   13425     41990140 :   if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
   13426            0 :     vec_safe_push (keyed_classes, type);
   13427              : 
   13428     41990140 :   return type;
   13429   1039437167 : }
   13430              : 
   13431              : tree
   13432   3445449471 : tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13433              : {
   13434   3445449471 :   tree r;
   13435              : 
   13436   3445449471 :   if (!t)
   13437              :     r = t;
   13438   3408277350 :   else if (TYPE_P (t))
   13439   3107096174 :     r = tsubst (t, args, complain, in_decl);
   13440              :   else
   13441              :     {
   13442    301181176 :       if (!(complain & tf_warning))
   13443    188971466 :         ++c_inhibit_evaluation_warnings;
   13444    301181176 :       r = tsubst_expr (t, args, complain, in_decl);
   13445    301181140 :       if (!(complain & tf_warning))
   13446    188971436 :         --c_inhibit_evaluation_warnings;
   13447              :     }
   13448              : 
   13449   3445449435 :   return r;
   13450              : }
   13451              : 
   13452              : /* Given a function parameter pack TMPL_PARM and some function parameters
   13453              :    instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
   13454              :    and set *SPEC_P to point at the next point in the list.  */
   13455              : 
   13456              : tree
   13457       785847 : extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
   13458              : {
   13459              :   /* Collect all of the extra "packed" parameters into an
   13460              :      argument pack.  */
   13461       785847 :   tree argpack;
   13462       785847 :   tree spec_parm = *spec_p;
   13463       785847 :   int len;
   13464              : 
   13465      1394510 :   for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
   13466       609138 :     if (tmpl_parm
   13467       609138 :         && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
   13468              :       break;
   13469              : 
   13470       785847 :   spec_parm = *spec_p;
   13471       785847 :   if (len == 1 && DECL_PACK_P (spec_parm))
   13472              :     {
   13473              :       /* The instantiation is still a parameter pack; don't wrap it in a
   13474              :          NONTYPE_ARGUMENT_PACK.  */
   13475         3846 :       argpack = spec_parm;
   13476         3846 :       spec_parm = DECL_CHAIN (spec_parm);
   13477              :     }
   13478              :   else
   13479              :     {
   13480              :       /* Fill in PARMVEC with all of the parameters.  */
   13481       782001 :       tree parmvec = make_tree_vec (len);
   13482       782001 :       argpack = make_node (NONTYPE_ARGUMENT_PACK);
   13483      2168819 :       for (int i = 0; i < len; i++)
   13484              :         {
   13485       604817 :           tree elt = spec_parm;
   13486       604817 :           if (DECL_PACK_P (elt))
   13487            0 :             elt = make_pack_expansion (elt);
   13488       604817 :           TREE_VEC_ELT (parmvec, i) = elt;
   13489       604817 :           spec_parm = DECL_CHAIN (spec_parm);
   13490              :         }
   13491              : 
   13492              :       /* Build the argument packs.  */
   13493       782001 :       ARGUMENT_PACK_ARGS (argpack) = parmvec;
   13494              :     }
   13495       785847 :   *spec_p = spec_parm;
   13496              : 
   13497       785847 :   return argpack;
   13498              : }
   13499              : 
   13500              : /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
   13501              :    NONTYPE_ARGUMENT_PACK.  */
   13502              : 
   13503              : static tree
   13504         1245 : make_fnparm_pack (tree spec_parm)
   13505              : {
   13506            0 :   return extract_fnparm_pack (NULL_TREE, &spec_parm);
   13507              : }
   13508              : 
   13509              : /* Return 1 if the Ith element of the argument pack ARG_PACK is a
   13510              :    pack expansion with no extra args, 2 if it has extra args, or 0
   13511              :    if it is not a pack expansion.  */
   13512              : 
   13513              : static int
   13514     24819878 : argument_pack_element_is_expansion_p (tree arg_pack, int i)
   13515              : {
   13516     24819878 :   if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   13517              :     /* We're being called before this happens in tsubst_pack_expansion.  */
   13518            0 :     arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   13519     24819878 :   tree vec = ARGUMENT_PACK_ARGS (arg_pack);
   13520     24819878 :   if (i >= TREE_VEC_LENGTH (vec))
   13521              :     return 0;
   13522     24819878 :   tree elt = TREE_VEC_ELT (vec, i);
   13523     24819878 :   if (DECL_P (elt))
   13524              :     /* A decl pack is itself an expansion.  */
   13525      1365552 :     elt = TREE_TYPE (elt);
   13526     24819878 :   if (!PACK_EXPANSION_P (elt))
   13527              :     return 0;
   13528       475712 :   if (PACK_EXPANSION_EXTRA_ARGS (elt))
   13529            3 :     return 2;
   13530              :   return 1;
   13531              : }
   13532              : 
   13533              : 
   13534              : /* Creates and return an ARGUMENT_PACK_SELECT tree node.  */
   13535              : 
   13536              : static tree
   13537      6938638 : make_argument_pack_select (tree arg_pack, unsigned index)
   13538              : {
   13539      6938638 :   tree aps = make_node (ARGUMENT_PACK_SELECT);
   13540              : 
   13541      6938638 :   ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
   13542      6938638 :   ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   13543              : 
   13544      6938638 :   return aps;
   13545              : }
   13546              : 
   13547              : /*  This is a subroutine of tsubst_pack_expansion.
   13548              : 
   13549              :     It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
   13550              :     mechanism to store the (non complete list of) arguments of the
   13551              :     substitution and return a non substituted pack expansion, in order
   13552              :     to wait for when we have enough arguments to really perform the
   13553              :     substitution.  */
   13554              : 
   13555              : static bool
   13556     24385580 : use_pack_expansion_extra_args_p (tree t,
   13557              :                                  tree parm_packs,
   13558              :                                  int arg_pack_len,
   13559              :                                  bool has_empty_arg)
   13560              : {
   13561     24385580 :   if (has_empty_arg
   13562     24385580 :       && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
   13563              :     return true;
   13564              : 
   13565              :   /* If one pack has an expansion and another pack has a normal
   13566              :      argument or if one pack has an empty argument and an another
   13567              :      one hasn't then tsubst_pack_expansion cannot perform the
   13568              :      substitution and need to fall back on the
   13569              :      PACK_EXPANSION_EXTRA mechanism.  */
   13570     24385563 :   if (parm_packs == NULL_TREE)
   13571              :     return false;
   13572      7583342 :   else if (has_empty_arg)
   13573              :     {
   13574              :       /* If all the actual packs are pack expansions, we can still
   13575              :          substitute directly.  */
   13576          994 :       for (tree p = parm_packs; p; p = TREE_CHAIN (p))
   13577              :         {
   13578          994 :           tree a = TREE_VALUE (p);
   13579          994 :           if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
   13580            0 :             a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
   13581          994 :           a = ARGUMENT_PACK_ARGS (a);
   13582          994 :           if (TREE_VEC_LENGTH (a) == 1)
   13583          207 :             a = TREE_VEC_ELT (a, 0);
   13584          994 :           if (PACK_EXPANSION_P (a))
   13585            0 :             continue;
   13586              :           return true;
   13587              :         }
   13588              :       return false;
   13589              :     }
   13590              : 
   13591     19410328 :   for (int i = 0 ; i < arg_pack_len; ++i)
   13592              :     {
   13593              :       bool has_expansion_arg = false;
   13594              :       bool has_non_expansion_arg = false;
   13595     12811984 :       for (tree parm_pack = parm_packs;
   13596     24639976 :            parm_pack;
   13597     12811984 :            parm_pack = TREE_CHAIN (parm_pack))
   13598              :         {
   13599     12811987 :           tree arg = TREE_VALUE (parm_pack);
   13600              : 
   13601     12811987 :           int exp = argument_pack_element_is_expansion_p (arg, i);
   13602     12811987 :           if (exp == 2)
   13603              :             /* We can't substitute a pack expansion with extra args into
   13604              :                our pattern.  */
   13605              :             return true;
   13606     12811984 :           else if (exp)
   13607              :             has_expansion_arg = true;
   13608              :           else
   13609     12574125 :             has_non_expansion_arg = true;
   13610              :         }
   13611              : 
   13612     11827989 :       if (has_expansion_arg && has_non_expansion_arg)
   13613              :         {
   13614              :           /* We can get here with:
   13615              : 
   13616              :               template <class... Ts> struct X {
   13617              :                 template <class... Us> using Y = Z<void(Ts, Us)...>;
   13618              :               };
   13619              :               template <class A, class... P>
   13620              :               using foo = X<int, int>::Y<A, P...>;
   13621              : 
   13622              :              where we compare int and A and then the second int and P...,
   13623              :              whose expansion-ness doesn't match, but that's OK.  */
   13624              :           return true;
   13625              :         }
   13626              :     }
   13627              :   return false;
   13628              : }
   13629              : 
   13630              : /* [temp.variadic]/6 says that:
   13631              : 
   13632              :        The instantiation of a pack expansion [...]
   13633              :        produces a list E1,E2, ..., En, where N is the number of elements
   13634              :        in the pack expansion parameters.
   13635              : 
   13636              :    This subroutine of tsubst_pack_expansion produces one of these Ei.
   13637              : 
   13638              :    PATTERN is the pattern of the pack expansion.  PARM_PACKS is a
   13639              :    TREE_LIST in which each TREE_PURPOSE is a parameter pack of
   13640              :    PATTERN, and each TREE_VALUE is its corresponding argument pack.
   13641              :    INDEX is the index 'i' of the element Ei to produce.  ARGS,
   13642              :    COMPLAIN, and IN_DECL are the same parameters as for the
   13643              :    tsubst_pack_expansion function.
   13644              : 
   13645              :    The function returns the resulting Ei upon successful completion,
   13646              :    or error_mark_node.
   13647              : 
   13648              :    Note that this function possibly modifies the ARGS parameter, so
   13649              :    it's the responsibility of the caller to restore it.  */
   13650              : 
   13651              : static tree
   13652     11023911 : gen_elem_of_pack_expansion_instantiation (tree pattern,
   13653              :                                           tree parm_packs,
   13654              :                                           unsigned index,
   13655              :                                           tree args /* This parm gets
   13656              :                                                        modified.  */,
   13657              :                                           tsubst_flags_t complain,
   13658              :                                           tree in_decl)
   13659              : {
   13660     11023911 :   tree t;
   13661     11023911 :   bool ith_elem_is_expansion = false;
   13662              : 
   13663              :   /* For each parameter pack, change the substitution of the parameter
   13664              :      pack to the ith argument in its argument pack, then expand the
   13665              :      pattern.  */
   13666     23031802 :   for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
   13667              :     {
   13668     12007891 :       tree parm = TREE_PURPOSE (pack);
   13669     12007891 :       tree arg_pack = TREE_VALUE (pack);
   13670     12007891 :       tree aps;                 /* instance of ARGUMENT_PACK_SELECT.  */
   13671              : 
   13672     24015782 :       ith_elem_is_expansion |=
   13673     12007891 :         argument_pack_element_is_expansion_p (arg_pack, index);
   13674              : 
   13675              :       /* Select the Ith argument from the pack.  */
   13676     12007891 :       if (TREE_CODE (parm) == PARM_DECL
   13677     11360839 :           || VAR_P (parm)
   13678     11325404 :           || TREE_CODE (parm) == FIELD_DECL)
   13679              :         {
   13680       682511 :           if (index == 0)
   13681              :             {
   13682       485565 :               aps = make_argument_pack_select (arg_pack, index);
   13683       485565 :               if (!mark_used (parm, complain) && !(complain & tf_error))
   13684            0 :                 return error_mark_node;
   13685       485565 :               register_local_specialization (aps, parm);
   13686              :             }
   13687              :           else
   13688       196946 :             aps = retrieve_local_specialization (parm);
   13689              :         }
   13690              :       else
   13691              :         {
   13692     11325380 :           int idx, level;
   13693     11325380 :           template_parm_level_and_index (parm, &level, &idx);
   13694              : 
   13695     11325380 :           if (index == 0)
   13696              :             {
   13697      6453073 :               aps = make_argument_pack_select (arg_pack, index);
   13698              :               /* Update the corresponding argument.  */
   13699     12906146 :               TMPL_ARG (args, level, idx) = aps;
   13700              :             }
   13701              :           else
   13702              :             /* Re-use the ARGUMENT_PACK_SELECT.  */
   13703      9744614 :             aps = TMPL_ARG (args, level, idx);
   13704              :         }
   13705     12007891 :       ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   13706              :     }
   13707              : 
   13708              :   /* Substitute into the PATTERN with the (possibly altered)
   13709              :      arguments.  */
   13710     11023911 :   if (pattern == in_decl)
   13711              :     /* Expanding a fixed parameter pack from
   13712              :        coerce_template_parameter_pack.  */
   13713          115 :     t = tsubst_decl (pattern, args, complain);
   13714     11023796 :   else if (pattern == error_mark_node)
   13715              :     t = error_mark_node;
   13716     11023793 :   else if (!TYPE_P (pattern))
   13717      2277512 :     t = tsubst_expr (pattern, args, complain, in_decl);
   13718              :   else
   13719              :     {
   13720      8746281 :       t = tsubst (pattern, args, complain, in_decl);
   13721      8746281 :       if (is_auto (t) && !ith_elem_is_expansion)
   13722              :         /* When expanding the fake auto... pack expansion from add_capture, we
   13723              :            need to mark that the expansion is no longer a pack.  */
   13724          175 :         TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
   13725              :     }
   13726              : 
   13727              :   /*  If the Ith argument pack element is a pack expansion, then
   13728              :       the Ith element resulting from the substituting is going to
   13729              :       be a pack expansion as well.  */
   13730     11023911 :   if (ith_elem_is_expansion)
   13731       211601 :     t = make_pack_expansion (t, complain);
   13732              : 
   13733              :   return t;
   13734              : }
   13735              : 
   13736              : /* When the unexpanded parameter pack in a fold expression expands to an empty
   13737              :    sequence, the value of the expression is as follows; the program is
   13738              :    ill-formed if the operator is not listed in this table.
   13739              : 
   13740              :    &&   true
   13741              :    ||   false
   13742              :    ,    void()  */
   13743              : 
   13744              : tree
   13745        45492 : expand_empty_fold (tree t, tsubst_flags_t complain)
   13746              : {
   13747        45492 :   tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
   13748        45492 :   if (!FOLD_EXPR_MODIFY_P (t))
   13749        45459 :     switch (code)
   13750              :       {
   13751        42353 :       case TRUTH_ANDIF_EXPR:
   13752        42353 :         return boolean_true_node;
   13753           65 :       case TRUTH_ORIF_EXPR:
   13754           65 :         return boolean_false_node;
   13755         2987 :       case COMPOUND_EXPR:
   13756         2987 :         return void_node;
   13757              :       default:
   13758              :         break;
   13759              :       }
   13760              : 
   13761           87 :   if (complain & tf_error)
   13762           87 :     error_at (location_of (t),
   13763              :               "fold of empty expansion over %O", code);
   13764           87 :   return error_mark_node;
   13765              : }
   13766              : 
   13767              : /* Given a fold-expression T and a current LEFT and RIGHT operand,
   13768              :    form an expression that combines the two terms using the
   13769              :    operator of T. */
   13770              : 
   13771              : static tree
   13772       635083 : fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
   13773              : {
   13774       635083 :   tree_code code = FOLD_EXPR_OP (t);
   13775              : 
   13776       635083 :   tree lookups = templated_operator_saved_lookups (t);
   13777              : 
   13778              :   // Handle compound assignment operators.
   13779       635083 :   if (FOLD_EXPR_MODIFY_P (t))
   13780          953 :     return build_x_modify_expr (input_location, left, code, right,
   13781              :                                 lookups, complain);
   13782              : 
   13783       634130 :   warning_sentinel s(warn_parentheses);
   13784       634130 :   switch (code)
   13785              :     {
   13786         5238 :     case COMPOUND_EXPR:
   13787         5238 :       return build_x_compound_expr (input_location, left, right,
   13788         5238 :                                     lookups, complain);
   13789       628892 :     default:
   13790       628892 :       return build_x_binary_op (input_location, code,
   13791       628892 :                                 left, TREE_CODE (left),
   13792       628892 :                                 right, TREE_CODE (right),
   13793              :                                 lookups, /*overload=*/NULL,
   13794              :                                 complain);
   13795              :     }
   13796       634130 : }
   13797              : 
   13798              : /* Substitute ARGS into the pack of a fold expression T. */
   13799              : 
   13800              : static inline tree
   13801       524988 : tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13802              : {
   13803       524988 :   return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
   13804              : }
   13805              : 
   13806              : /* Substitute ARGS into the pack of a fold expression T. */
   13807              : 
   13808              : static inline tree
   13809        12398 : tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13810              : {
   13811        12398 :   return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
   13812              : }
   13813              : 
   13814              : /* Expand a PACK of arguments into a grouped as left fold.
   13815              :    Given a pack containing elements A0, A1, ..., An and an
   13816              :    operator @, this builds the expression:
   13817              : 
   13818              :       ((A0 @ A1) @ A2) ... @ An
   13819              : 
   13820              :    Note that PACK must not be empty.
   13821              : 
   13822              :    The operator is defined by the original fold expression T. */
   13823              : 
   13824              : static tree
   13825        15219 : expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
   13826              : {
   13827        15219 :   tree left = TREE_VEC_ELT (pack, 0);
   13828        34792 :   for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
   13829              :     {
   13830        19573 :       tree right = TREE_VEC_ELT (pack, i);
   13831        19573 :       left = fold_expression (t, left, right, complain);
   13832              :     }
   13833        15219 :   return left;
   13834              : }
   13835              : 
   13836              : /* Substitute into a unary left fold expression. */
   13837              : 
   13838              : static tree
   13839         6068 : tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13840              :                         tree in_decl)
   13841              : {
   13842         6068 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13843         6068 :   if (pack == error_mark_node)
   13844              :     return error_mark_node;
   13845         6068 :   if (PACK_EXPANSION_P (pack))
   13846              :     {
   13847         1094 :       tree r = copy_node (t);
   13848         1094 :       FOLD_EXPR_PACK (r) = pack;
   13849         1094 :       return r;
   13850              :     }
   13851         4974 :   if (TREE_VEC_LENGTH (pack) == 0)
   13852          204 :     return expand_empty_fold (t, complain);
   13853              :   else
   13854         4770 :     return expand_left_fold (t, pack, complain);
   13855              : }
   13856              : 
   13857              : /* Substitute into a binary left fold expression.
   13858              : 
   13859              :    Do this by building a single (non-empty) vector of arguments and
   13860              :    building the expression from those elements. */
   13861              : 
   13862              : static tree
   13863        10653 : tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13864              :                          tree in_decl)
   13865              : {
   13866        10653 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13867        10653 :   if (pack == error_mark_node)
   13868              :     return error_mark_node;
   13869        10653 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13870        10653 :   if (init == error_mark_node)
   13871              :     return error_mark_node;
   13872              : 
   13873        10653 :   if (PACK_EXPANSION_P (pack))
   13874              :     {
   13875          204 :       tree r = copy_node (t);
   13876          204 :       FOLD_EXPR_PACK (r) = pack;
   13877          204 :       FOLD_EXPR_INIT (r) = init;
   13878          204 :       return r;
   13879              :     }
   13880              : 
   13881        10449 :   tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
   13882        10449 :   TREE_VEC_ELT (vec, 0) = init;
   13883        19261 :   for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
   13884         8812 :     TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
   13885              : 
   13886        10449 :   return expand_left_fold (t, vec, complain);
   13887              : }
   13888              : 
   13889              : /* Expand a PACK of arguments into a grouped as right fold.
   13890              :    Given a pack containing elementns A0, A1, ..., and an
   13891              :    operator @, this builds the expression:
   13892              : 
   13893              :       A0@ ... (An-2 @ (An-1 @ An))
   13894              : 
   13895              :    Note that PACK must not be empty.
   13896              : 
   13897              :    The operator is defined by the original fold expression T. */
   13898              : 
   13899              : tree
   13900       449891 : expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
   13901              : {
   13902              :   // Build the expression.
   13903       449891 :   int n = TREE_VEC_LENGTH (pack);
   13904       449891 :   tree right = TREE_VEC_ELT (pack, n - 1);
   13905      1065401 :   for (--n; n != 0; --n)
   13906              :     {
   13907       615510 :       tree left = TREE_VEC_ELT (pack, n - 1);
   13908       615510 :       right = fold_expression (t, left, right, complain);
   13909              :     }
   13910       449891 :   return right;
   13911              : }
   13912              : 
   13913              : /* Substitute into a unary right fold expression. */
   13914              : 
   13915              : static tree
   13916       506522 : tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13917              :                          tree in_decl)
   13918              : {
   13919       506522 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13920       506522 :   if (pack == error_mark_node)
   13921              :     return error_mark_node;
   13922       501678 :   if (PACK_EXPANSION_P (pack))
   13923              :     {
   13924         8154 :       tree r = copy_node (t);
   13925         8154 :       FOLD_EXPR_PACK (r) = pack;
   13926         8154 :       return r;
   13927              :     }
   13928       493524 :   if (TREE_VEC_LENGTH (pack) == 0)
   13929        45288 :     return expand_empty_fold (t, complain);
   13930              :   else
   13931       448236 :     return expand_right_fold (t, pack, complain);
   13932              : }
   13933              : 
   13934              : /* Substitute into a binary right fold expression.
   13935              : 
   13936              :    Do this by building a single (non-empty) vector of arguments and
   13937              :    building the expression from those elements. */
   13938              : 
   13939              : static tree
   13940         1745 : tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13941              :                          tree in_decl)
   13942              : {
   13943         1745 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13944         1745 :   if (pack == error_mark_node)
   13945              :     return error_mark_node;
   13946         1745 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13947         1745 :   if (init == error_mark_node)
   13948              :     return error_mark_node;
   13949              : 
   13950         1745 :   if (PACK_EXPANSION_P (pack))
   13951              :     {
   13952           90 :       tree r = copy_node (t);
   13953           90 :       FOLD_EXPR_PACK (r) = pack;
   13954           90 :       FOLD_EXPR_INIT (r) = init;
   13955           90 :       return r;
   13956              :     }
   13957              : 
   13958         1655 :   int n = TREE_VEC_LENGTH (pack);
   13959         1655 :   tree vec = make_tree_vec (n + 1);
   13960         6038 :   for (int i = 0; i < n; ++i)
   13961         2728 :     TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
   13962         1655 :   TREE_VEC_ELT (vec, n) = init;
   13963              : 
   13964         1655 :   return expand_right_fold (t, vec, complain);
   13965              : }
   13966              : 
   13967              : /* Walk through the pattern of a pack expansion, adding everything in
   13968              :    local_specializations to a list.  */
   13969              : 
   13970              : class el_data
   13971              : {
   13972              : public:
   13973              :   /* Set of variables declared within the pattern.  */
   13974              :   hash_set<tree> internal;
   13975              :   /* Set of AST nodes that have been visited by the traversal.  */
   13976              :   hash_set<tree> visited;
   13977              :   /* List of local_specializations used within the pattern.  */
   13978              :   tree extra;
   13979              :   tsubst_flags_t complain;
   13980              :   /* True iff we don't want to walk into unevaluated contexts.  */
   13981              :   bool skip_unevaluated_operands = false;
   13982              :   /* The unevaluated contexts that we avoided walking.  */
   13983              :   auto_vec<tree> skipped_trees;
   13984              : 
   13985        11495 :   el_data (tsubst_flags_t c)
   13986        11495 :     : extra (NULL_TREE), complain (c) {}
   13987              : };
   13988              : static tree
   13989      1022027 : extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
   13990              : {
   13991      1022027 :   el_data &data = *reinterpret_cast<el_data*>(data_);
   13992      1022027 :   tree *extra = &data.extra;
   13993      1022027 :   tsubst_flags_t complain = data.complain;
   13994              : 
   13995      1022027 :   if (data.skip_unevaluated_operands
   13996      1022027 :       && unevaluated_p (TREE_CODE (*tp)))
   13997              :     {
   13998        11510 :       data.skipped_trees.safe_push (*tp);
   13999        11510 :       *walk_subtrees = 0;
   14000        11510 :       return NULL_TREE;
   14001              :     }
   14002              : 
   14003      1010517 :   if (TYPE_P (*tp) && typedef_variant_p (*tp))
   14004              :     /* Remember local typedefs (85214).  */
   14005         9695 :     tp = &TYPE_NAME (*tp);
   14006              : 
   14007      1010517 :   if (has_extra_args_mechanism_p (*tp))
   14008              :     /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
   14009              :        potentially see a previously captured local in an evaluated context
   14010              :        that's really only used in an unevaluated context (PR114303).  This
   14011              :        means callers of build_extra_args need to clear *_EXTRA_ARGS of the
   14012              :        outermost tree.  Nested *_EXTRA_ARGS should naturally be empty since
   14013              :        the outermost (extra-args) tree will intercept any substitution before
   14014              :        a nested tree can.  */
   14015        34545 :     gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE
   14016              :                         /* Except a lambda nested inside an extra-args tree
   14017              :                            can have extra args if we deferred partial
   14018              :                            substitution into it at template parse time.  But
   14019              :                            we don't walk LAMBDA_EXPR_EXTRA_ARGS anyway.  */
   14020              :                          || TREE_CODE (*tp) == LAMBDA_EXPR);
   14021              : 
   14022      1010517 :   if (TREE_CODE (*tp) == DECL_EXPR)
   14023              :     {
   14024        20155 :       tree decl = DECL_EXPR_DECL (*tp);
   14025        20155 :       data.internal.add (decl);
   14026        18331 :       if (DECL_DECOMPOSITION_P (decl)
   14027        20161 :           && TREE_TYPE (decl) != error_mark_node)
   14028              :         {
   14029            6 :           gcc_assert (DECL_NAME (decl) == NULL_TREE);
   14030            6 :           for (tree decl2 = DECL_CHAIN (decl);
   14031           18 :                decl2
   14032           12 :                && DECL_DECOMPOSITION_P (decl2)
   14033           12 :                && DECL_NAME (decl2)
   14034           30 :                && TREE_TYPE (decl2) != error_mark_node;
   14035           12 :                decl2 = DECL_CHAIN (decl2))
   14036              :             {
   14037           12 :               gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
   14038           12 :               data.internal.add (decl2);
   14039              :             }
   14040              :         }
   14041              :     }
   14042       990362 :   else if (TREE_CODE (*tp) == LAMBDA_EXPR)
   14043              :     {
   14044              :       /* Since we defer implicit capture, look in the parms and body.  */
   14045          995 :       tree fn = lambda_function (*tp);
   14046          995 :       cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
   14047              :                     &data.visited);
   14048          995 :       cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
   14049              :                     &data.visited);
   14050              :     }
   14051       989367 :   else if (tree spec = retrieve_local_specialization (*tp))
   14052              :     {
   14053        22009 :       if (data.internal.contains (*tp))
   14054              :         /* Don't mess with variables declared within the pattern.  */
   14055              :         return NULL_TREE;
   14056        21994 :       if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   14057              :         {
   14058              :           /* Maybe pull out the PARM_DECL for a partial instantiation.  */
   14059          654 :           tree args = ARGUMENT_PACK_ARGS (spec);
   14060          654 :           if (TREE_VEC_LENGTH (args) == 1)
   14061              :             {
   14062           60 :               tree elt = TREE_VEC_ELT (args, 0);
   14063           60 :               if (PACK_EXPANSION_P (elt))
   14064            0 :                 elt = PACK_EXPANSION_PATTERN (elt);
   14065           60 :               if (DECL_PACK_P (elt))
   14066              :                 spec = elt;
   14067              :             }
   14068          654 :           if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   14069              :             {
   14070              :               /* Handle lambda capture here, since we aren't doing any
   14071              :                  substitution now, and so tsubst_copy won't call
   14072              :                  process_outer_var_ref.  */
   14073          654 :               tree args = ARGUMENT_PACK_ARGS (spec);
   14074          654 :               int len = TREE_VEC_LENGTH (args);
   14075         2512 :               for (int i = 0; i < len; ++i)
   14076              :                 {
   14077         1858 :                   tree arg = TREE_VEC_ELT (args, i);
   14078         1858 :                   tree carg = arg;
   14079         1858 :                   if (outer_automatic_var_p (arg))
   14080         1858 :                     carg = process_outer_var_ref (arg, complain);
   14081         1858 :                   if (carg != arg)
   14082              :                     {
   14083              :                       /* Make a new NONTYPE_ARGUMENT_PACK of the capture
   14084              :                          proxies.  */
   14085         1858 :                       if (i == 0)
   14086              :                         {
   14087          648 :                           spec = copy_node (spec);
   14088          648 :                           args = copy_node (args);
   14089          648 :                           ARGUMENT_PACK_ARGS (spec) = args;
   14090          648 :                           register_local_specialization (spec, *tp);
   14091              :                         }
   14092         1858 :                       TREE_VEC_ELT (args, i) = carg;
   14093              :                     }
   14094              :                 }
   14095              :             }
   14096              :         }
   14097        21994 :       if (outer_automatic_var_p (spec))
   14098           27 :         spec = process_outer_var_ref (spec, complain);
   14099        21994 :       *extra = tree_cons (*tp, spec, *extra);
   14100              :     }
   14101              :   return NULL_TREE;
   14102              : }
   14103              : static tree
   14104        11495 : extract_local_specs (tree pattern, tsubst_flags_t complain)
   14105              : {
   14106        11495 :   el_data data (complain);
   14107              :   /* Walk the pattern twice, ignoring unevaluated operands the first time
   14108              :      around, so that if a local specialization appears in both an evaluated
   14109              :      and unevaluated context we prefer to process it in the evaluated context
   14110              :      (since e.g. process_outer_var_ref is a no-op inside an unevaluated
   14111              :      context).  */
   14112        11495 :   data.skip_unevaluated_operands = true;
   14113        11495 :   cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
   14114              :   /* Now walk the unevaluated contexts we skipped the first time around.  */
   14115        11495 :   data.skip_unevaluated_operands = false;
   14116        42977 :   for (tree t : data.skipped_trees)
   14117              :     {
   14118        11510 :       data.visited.remove (t);
   14119        11510 :       cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
   14120              :     }
   14121        22990 :   return data.extra;
   14122        11495 : }
   14123              : 
   14124              : /* Extract any uses of local_specializations from PATTERN and add them to ARGS
   14125              :    for use in PACK_EXPANSION_EXTRA_ARGS.  */
   14126              : 
   14127              : tree
   14128        13733 : build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
   14129              : {
   14130              :   /* Make a copy of the extra arguments so that they won't get changed
   14131              :      out from under us.  */
   14132        13733 :   tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
   14133        13733 :   if ((complain & tf_partial) || TREE_STATIC (args))
   14134              :     /* Remember whether this is a partial substitution.  */
   14135         2696 :     TREE_STATIC (extra) = true;
   14136        13733 :   if (local_specializations)
   14137        11495 :     if (tree locals = extract_local_specs (pattern, complain))
   14138         8138 :       extra = tree_cons (NULL_TREE, extra, locals);
   14139        13733 :   return extra;
   14140              : }
   14141              : 
   14142              : /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
   14143              :    normal template args to ARGS.  */
   14144              : 
   14145              : tree
   14146    114243286 : add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
   14147              : {
   14148    114243286 :   if (!extra)
   14149              :     return args;
   14150              : 
   14151        86160 :   if (TREE_CODE (extra) == TREE_LIST)
   14152              :     {
   14153       334737 :       for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
   14154              :         {
   14155              :           /* The partial instantiation involved local declarations collected in
   14156              :              extract_local_specs; map from the general template to our local
   14157              :              context.  */
   14158       249368 :           tree gen = TREE_PURPOSE (elt);
   14159       249368 :           tree inst = TREE_VALUE (elt);
   14160       249368 :           if (DECL_P (inst))
   14161       248714 :             if (tree local = retrieve_local_specialization (inst))
   14162       249368 :               inst = local;
   14163              :           /* else inst is already a full instantiation of the pack.  */
   14164       249368 :           register_local_specialization (inst, gen);
   14165       249368 :           if (is_normal_capture_proxy (gen))
   14166       105894 :             register_local_specialization (inst, DECL_CAPTURED_VARIABLE (gen));
   14167              :         }
   14168        85369 :       gcc_assert (!TREE_PURPOSE (extra));
   14169        85369 :       extra = TREE_VALUE (extra);
   14170              :     }
   14171        86160 :   if (TREE_STATIC (extra))
   14172              :     {
   14173              :       /* This is a partial substitution into e.g. a requires-expr or lambda-expr
   14174              :          inside a default template argument; we expect 'extra' to be a full set
   14175              :          of template arguments for the template context, so it suffices to just
   14176              :          substitute into them.  */
   14177           84 :       args = tsubst_template_args (extra, args, complain, in_decl);
   14178           84 :       if (processing_template_decl)
   14179              :         /* A templated substitution into a partial substitution is still a
   14180              :            partial substitution.  */
   14181           12 :         TREE_STATIC (args) = true;
   14182              :     }
   14183              :   else
   14184        86076 :     args = add_to_template_args (extra, args);
   14185              :   return args;
   14186              : }
   14187              : 
   14188              : /* We've seen a lambda capture proxy pack PAT in a context outside the body,
   14189              :    such as in a trailing requires-clause.  Here we cannot use
   14190              :    lookup_init_capture_pack, both because checking satisfaction comes after
   14191              :    tsubst_lambda_expr (so the field pack isn't in local_specializations) and
   14192              :    before start_lambda_function (so the proxies aren't there to lookup_name).
   14193              : 
   14194              :    However, because satisfy_declaration_constraints pushed into the context of
   14195              :    the substituted closure type, we can find it in current_class_type,
   14196              :    enumerate the expanded field pack, and then use that to expand the capture
   14197              :    proxy pack.
   14198              : 
   14199              :    This seems like cheating, but there's really no alternative since lambdas
   14200              :    are not instantiated; we have to use the same closure type.  To address this
   14201              :    in the language, probably lambda constraints should be substituted
   14202              :    immediately like lambda noexcept.  */
   14203              : 
   14204              : tree
   14205           15 : reconstruct_lambda_capture_pack (tree pat, tree args, tsubst_flags_t complain,
   14206              :                                  tree in_decl)
   14207              : {
   14208           15 :   gcc_assert (cp_unevaluated_operand && !at_function_scope_p ());
   14209           15 :   tree newc = current_class_type;
   14210              : 
   14211              :   /* Look up the pattern FIELD_DECL.  */
   14212           15 :   tree oldfield = TREE_OPERAND (DECL_VALUE_EXPR (pat), 1);
   14213           15 :   gcc_assert (DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (oldfield)))
   14214              :               == DECL_SOURCE_LOCATION (TYPE_NAME (newc)));
   14215              : 
   14216           15 :   if (!retrieve_local_specialization (oldfield))
   14217              :     {
   14218              :       /* Look through TYPE_FIELDS to find captures with matching
   14219              :          capture#1 names.  */
   14220           15 :       tree pn = DECL_NAME (oldfield);
   14221           15 :       const char *ps = IDENTIFIER_POINTER (pn);
   14222           15 :       int pl = IDENTIFIER_LENGTH (pn);
   14223           15 :       tree firstcap = NULL_TREE;
   14224           15 :       int ncap = 0;
   14225           15 :       for (tree f = next_subobject_field (TYPE_FIELDS (newc));
   14226           45 :            f; f = DECL_CHAIN (f))
   14227              :         {
   14228           42 :           const char *s = IDENTIFIER_POINTER (DECL_NAME (f));
   14229           42 :           if (strncmp (ps, s, pl) == 0 && s[pl] == '#')
   14230              :             {
   14231           24 :               if (!firstcap)
   14232           12 :                 firstcap = f;
   14233           24 :               ++ncap;
   14234              :             }
   14235           18 :           else if (firstcap)
   14236              :             break;
   14237              :         }
   14238           15 :       tree fpack = make_tree_vec (ncap);
   14239           54 :       for (int i = 0; i < ncap; ++i)
   14240              :         {
   14241           24 :           TREE_VEC_ELT (fpack, i) = firstcap;
   14242           24 :           firstcap = DECL_CHAIN (firstcap);
   14243              :         }
   14244           15 :       tree spec = make_node (NONTYPE_ARGUMENT_PACK);
   14245           15 :       ARGUMENT_PACK_ARGS (spec) = fpack;
   14246              :       /* satisfy_declaration_constraints set up local_specializations.  */
   14247           15 :       register_local_specialization (spec, oldfield);
   14248              :     }
   14249              : 
   14250              :   /* Now that the field pack is in local_specializations, we can substitute
   14251              :      into the DECL_VALUE_EXPR of the proxy.  */
   14252           15 :   tree ve = DECL_VALUE_EXPR (pat);
   14253           15 :   ve = make_pack_expansion (ve);
   14254           15 :   ve = tsubst_pack_expansion (ve, args, complain, in_decl);
   14255           15 :   const int len = TREE_VEC_LENGTH (ve);
   14256           15 :   tree ppack = make_tree_vec (len);
   14257           54 :   for (int i = 0; i < len; ++i)
   14258              :     {
   14259           24 :       tree p = copy_decl (pat);
   14260           24 :       tree vei = TREE_VEC_ELT (ve, i);
   14261           24 :       SET_DECL_VALUE_EXPR (p, vei);
   14262           24 :       TREE_TYPE (p) = lambda_proxy_type (vei);
   14263           24 :       TREE_VEC_ELT (ppack, i) = p;
   14264              :     }
   14265           15 :   tree spec = make_node (NONTYPE_ARGUMENT_PACK);
   14266           15 :   ARGUMENT_PACK_ARGS (spec) = ppack;
   14267           15 :   register_local_specialization (spec, pat);
   14268           15 :   return spec;
   14269              : }
   14270              : 
   14271              : /* Substitute ARGS into T, which is a pack expansion
   14272              :    (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION).  Returns a
   14273              :    TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
   14274              :    (if only a partial substitution could be performed) or
   14275              :    ERROR_MARK_NODE if there was an error.  */
   14276              : 
   14277              : tree
   14278     95988618 : tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   14279              :                        tree in_decl)
   14280              : {
   14281     95988618 :   tree pattern;
   14282     95988618 :   tree pack, packs = NULL_TREE;
   14283     95988618 :   bool unsubstituted_packs = false;
   14284     95988618 :   int i, len = -1;
   14285     95988618 :   tree result;
   14286     95988618 :   bool need_local_specializations = false;
   14287     95988618 :   int levels;
   14288              : 
   14289     95988618 :   gcc_assert (PACK_EXPANSION_P (t));
   14290     95988618 :   pattern = PACK_EXPANSION_PATTERN (t);
   14291              : 
   14292              :   /* Add in any args remembered from an earlier partial instantiation.  */
   14293     95988618 :   args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
   14294              : 
   14295    191977233 :   levels = TMPL_ARGS_DEPTH (args);
   14296              : 
   14297              :   /* Determine the argument packs that will instantiate the parameter
   14298              :      packs used in the expansion expression. While we're at it,
   14299              :      compute the number of arguments to be expanded and make sure it
   14300              :      is consistent.  */
   14301    279348824 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
   14302     93949238 :        pack = TREE_CHAIN (pack))
   14303              :     {
   14304     96856142 :       tree parm_pack = TREE_VALUE (pack);
   14305     96856142 :       tree arg_pack = NULL_TREE;
   14306     96856142 :       tree orig_arg = NULL_TREE;
   14307     96856142 :       int level = 0;
   14308              : 
   14309     96856142 :       if (TREE_CODE (parm_pack) == BASES)
   14310              :         {
   14311           27 :           gcc_assert (parm_pack == pattern);
   14312           27 :           tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl);
   14313           27 :           if (BASES_DIRECT (parm_pack))
   14314      2906901 :             return calculate_direct_bases (type, complain);
   14315              :           else
   14316           12 :             return calculate_bases (type, complain);
   14317              :         }
   14318     96856115 :       else if (builtin_pack_call_p (parm_pack))
   14319              :         {
   14320       114220 :           if (parm_pack != pattern)
   14321              :             {
   14322            3 :               if (complain & tf_error)
   14323            3 :                 sorry ("%qE is not the entire pattern of the pack expansion",
   14324              :                        parm_pack);
   14325            3 :               return error_mark_node;
   14326              :             }
   14327       114217 :           return expand_builtin_pack_call (parm_pack, args,
   14328       114217 :                                            complain, in_decl);
   14329              :         }
   14330     96741895 :       else if (TREE_CODE (parm_pack) == PARM_DECL)
   14331              :         {
   14332              :           /* We know we have correct local_specializations if this
   14333              :              expansion is at function scope, or if we're dealing with a
   14334              :              local parameter in a requires expression; for the latter,
   14335              :              tsubst_requires_expr set it up appropriately.  */
   14336       832914 :           if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
   14337       831501 :             arg_pack = retrieve_local_specialization (parm_pack);
   14338              :           else
   14339              :             /* We can't rely on local_specializations for a parameter
   14340              :                name used later in a function declaration (such as in a
   14341              :                late-specified return type).  Even if it exists, it might
   14342              :                have the wrong value for a recursive call.  */
   14343              :             need_local_specializations = true;
   14344              : 
   14345       831501 :           if (!arg_pack)
   14346              :             {
   14347              :               /* This parameter pack was used in an unevaluated context.  Just
   14348              :                  make a dummy decl, since it's only used for its type.  */
   14349         1415 :               ++cp_unevaluated_operand;
   14350         1415 :               arg_pack = tsubst_decl (parm_pack, args, complain);
   14351         1415 :               --cp_unevaluated_operand;
   14352         1415 :               if (arg_pack && DECL_PACK_P (arg_pack))
   14353              :                 /* Partial instantiation of the parm_pack, we can't build
   14354              :                    up an argument pack yet.  */
   14355              :                 arg_pack = NULL_TREE;
   14356              :               else
   14357         1245 :                 arg_pack = make_fnparm_pack (arg_pack);
   14358              :             }
   14359       831499 :           else if (DECL_PACK_P (arg_pack))
   14360              :             /* This argument pack isn't fully instantiated yet.  */
   14361              :             arg_pack = NULL_TREE;
   14362              :         }
   14363     95908981 :       else if (is_capture_proxy (parm_pack))
   14364              :         {
   14365          424 :           arg_pack = retrieve_local_specialization (parm_pack);
   14366          424 :           if (!arg_pack)
   14367           15 :             arg_pack = reconstruct_lambda_capture_pack (parm_pack, args,
   14368              :                                                         complain, in_decl);
   14369          424 :           if (DECL_DECOMPOSITION_P (arg_pack))
   14370              :             {
   14371          128 :               orig_arg = arg_pack;
   14372          128 :               goto expand_sb_pack;
   14373              :             }
   14374          296 :           if (DECL_PACK_P (arg_pack))
   14375              :             arg_pack = NULL_TREE;
   14376              :         }
   14377     95908557 :       else if (TREE_CODE (parm_pack) == FIELD_DECL)
   14378              :         /* For reconstruct_lambda_capture_pack.  */
   14379           15 :         arg_pack = retrieve_local_specialization (parm_pack);
   14380     95908542 :       else if (DECL_DECOMPOSITION_P (parm_pack))
   14381              :         {
   14382         9279 :           orig_arg = retrieve_local_specialization (parm_pack);
   14383         9279 :           if (DECL_DECOMPOSITION_P (orig_arg))
   14384              :             {
   14385         9407 :             expand_sb_pack:
   14386         9407 :               if (TREE_TYPE (orig_arg) == error_mark_node)
   14387              :                 return error_mark_node;
   14388         9407 :               gcc_assert (DECL_HAS_VALUE_EXPR_P (orig_arg));
   14389         9407 :               arg_pack = DECL_VALUE_EXPR (orig_arg);
   14390         9407 :               if (TREE_CODE (arg_pack) != ARRAY_REF)
   14391              :                 {
   14392              :                   /* Structured binding packs when initializer is non-dependent
   14393              :                      should have their DECL_VALUE_EXPR set to a TREE_VEC.  See
   14394              :                      cp_finish_decomp comment above the packv variable for
   14395              :                      details.  */
   14396         9404 :                   tree vec = make_tree_vec (TREE_VEC_LENGTH (arg_pack) - 2);
   14397         9404 :                   if (TREE_VEC_LENGTH (vec))
   14398         8646 :                     memcpy (TREE_VEC_BEGIN (vec), &TREE_VEC_ELT (arg_pack, 2),
   14399         8646 :                             TREE_VEC_LENGTH (vec) * sizeof (tree));
   14400         9404 :                   arg_pack = make_node (NONTYPE_ARGUMENT_PACK);
   14401         9404 :                   ARGUMENT_PACK_ARGS (arg_pack) = vec;
   14402              :                 }
   14403              :               else
   14404              :                 {
   14405              :                   /* If the structured binding pack has type dependent
   14406              :                      base, we can't expand it yet.  */
   14407            3 :                   tree base = TREE_OPERAND (arg_pack, 0);
   14408            3 :                   gcc_assert (VAR_P (base)
   14409              :                               && type_dependent_expression_p (base));
   14410              :                   arg_pack = NULL_TREE;
   14411              :                 }
   14412              :             }
   14413              :           else
   14414              :             {
   14415            0 :               gcc_assert (TREE_CODE (orig_arg) == ARGUMENT_PACK_SELECT);
   14416              :               arg_pack = orig_arg;
   14417              :             }
   14418              :         }
   14419              :       else
   14420              :         {
   14421     95899263 :           int idx;
   14422     95899263 :           template_parm_level_and_index (parm_pack, &level, &idx);
   14423     95899263 :           if (level <= levels)
   14424    166636726 :             arg_pack = TMPL_ARG (args, level, idx);
   14425              : 
   14426     79101894 :           if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
   14427     83318384 :               && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
   14428              :             arg_pack = NULL_TREE;
   14429              :         }
   14430              : 
   14431     95900523 :       if (orig_arg == NULL_TREE)
   14432              :         orig_arg = arg_pack;
   14433     96741895 :       if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   14434         5376 :         arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   14435              : 
   14436     79937052 :       if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
   14437              :         /* This can only happen if we forget to expand an argument
   14438              :            pack somewhere else. Just return an error, silently.  */
   14439              :         {
   14440            9 :           result = make_tree_vec (1);
   14441            9 :           TREE_VEC_ELT (result, 0) = error_mark_node;
   14442            9 :           return result;
   14443              :         }
   14444              : 
   14445     96741886 :       if (arg_pack)
   14446              :         {
   14447     79937043 :           int my_len
   14448     79937043 :             = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
   14449              : 
   14450              :           /* Don't bother trying to do a partial substitution with
   14451              :              incomplete packs; we'll try again after deduction.  */
   14452     79937043 :           if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
   14453              :             return t;
   14454              : 
   14455     77144433 :           if (len < 0)
   14456              :             len = my_len;
   14457       864916 :           else if (len != my_len)
   14458              :             {
   14459           35 :               if (!(complain & tf_error))
   14460              :                 /* Fail quietly.  */;
   14461           13 :               else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
   14462            7 :                 error ("mismatched argument pack lengths while expanding %qT",
   14463              :                        pattern);
   14464              :               else
   14465            6 :                 error ("mismatched argument pack lengths while expanding %qE",
   14466              :                        pattern);
   14467           35 :               return error_mark_node;
   14468              :             }
   14469              : 
   14470              :           /* Keep track of the parameter packs and their corresponding
   14471              :              argument packs.  */
   14472     77144398 :           packs = tree_cons (parm_pack, arg_pack, packs);
   14473     77144398 :           TREE_TYPE (packs) = orig_arg;
   14474              :         }
   14475              :       else
   14476              :         {
   14477              :           /* We can't substitute for this parameter pack.  We use a flag as
   14478              :              well as the missing_level counter because function parameter
   14479              :              packs don't have a level.  */
   14480     16804843 :           gcc_assert (processing_template_decl || is_auto (parm_pack)
   14481              :                       || args == NULL_TREE);
   14482              :           unsubstituted_packs = true;
   14483              :         }
   14484              :     }
   14485              : 
   14486              :   /* If the expansion is just T..., return the matching argument pack, unless
   14487              :      we need to call convert_from_reference on all the elements.  This is an
   14488              :      important optimization; see c++/68422.  */
   14489     93081714 :   if (!unsubstituted_packs
   14490    169360196 :       && TREE_PURPOSE (packs) == pattern)
   14491              :     {
   14492     68722490 :       tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
   14493              : 
   14494              :       /* If the argument pack is a single pack expansion, pull it out.  */
   14495     68722490 :       if (TREE_VEC_LENGTH (args) == 1
   14496     68722490 :           && pack_expansion_args_count (args))
   14497              :         {
   14498      2008350 :           tree arg = TREE_VEC_ELT (args, 0);
   14499      2008350 :           if (PACK_EXPANSION_SIZEOF_P (t)
   14500      2008350 :               && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
   14501              :             /* Except if this isn't a simple sizeof...(T) which gets sZ
   14502              :                mangling, keep the TREE_VEC to get sP mangling.  */;
   14503              :           else
   14504      2008341 :             return TREE_VEC_ELT (args, 0);
   14505              :         }
   14506              : 
   14507              :       /* Types need no adjustment, nor does sizeof..., and if we still have
   14508              :          some pack expansion args we won't do anything yet.  */
   14509     66714149 :       if (TREE_CODE (t) == TYPE_PACK_EXPANSION
   14510      3886627 :           || PACK_EXPANSION_SIZEOF_P (t)
   14511     70592512 :           || pack_expansion_args_count (args))
   14512              :         return args;
   14513              :       /* Also optimize expression pack expansions if we can tell that the
   14514              :          elements won't have reference type.  */
   14515      3790382 :       tree type = TREE_TYPE (pattern);
   14516      3790382 :       if (type && !TYPE_REF_P (type)
   14517              :           && !PACK_EXPANSION_P (type)
   14518              :           && !WILDCARD_TYPE_P (type))
   14519              :         return args;
   14520              :       /* Otherwise use the normal path so we get convert_from_reference.  */
   14521              :     }
   14522              : 
   14523              :   /* We cannot expand this expansion expression, because we don't have
   14524              :      all of the argument packs we need.  */
   14525     24385580 :   if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
   14526              :     {
   14527              :       /* We got some full packs, but we can't substitute them in until we
   14528              :          have values for all the packs.  So remember these until then.  */
   14529              : 
   14530         1023 :       t = make_pack_expansion (pattern, complain);
   14531         1023 :       PACK_EXPANSION_EXTRA_ARGS (t)
   14532         1023 :         = build_extra_args (pattern, args, complain);
   14533         1023 :       return t;
   14534              :     }
   14535              : 
   14536              :   /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
   14537              :      type, so create our own local specializations map; the current map is
   14538              :      either NULL or (in the case of recursive unification) might have
   14539              :      bindings that we don't want to use or alter.  */
   14540     24384557 :   local_specialization_stack lss (need_local_specializations
   14541     73152539 :                                   ? lss_blank : lss_nop);
   14542              : 
   14543     24384557 :   if (unsubstituted_packs)
   14544              :     {
   14545              :       /* There were no real arguments, we're just replacing a parameter
   14546              :          pack with another version of itself. Substitute into the
   14547              :          pattern and return a PACK_EXPANSION_*. The caller will need to
   14548              :          deal with that.  */
   14549     16802221 :       if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
   14550       628449 :         result = tsubst_expr (pattern, args, complain, in_decl);
   14551              :       else
   14552     16173772 :         result = tsubst (pattern, args, complain, in_decl);
   14553     16802221 :       result = make_pack_expansion (result, complain);
   14554     16802221 :       if (result == error_mark_node)
   14555              :         return error_mark_node;
   14556     16802218 :       PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
   14557     16802218 :       PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
   14558     16802218 :       if (PACK_EXPANSION_AUTO_P (t))
   14559              :         {
   14560              :           /* This is a fake auto... pack expansion created in add_capture with
   14561              :              _PACKS that don't appear in the pattern.  Copy one over.  */
   14562            6 :           packs = PACK_EXPANSION_PARAMETER_PACKS (t);
   14563            3 :           pack = retrieve_local_specialization (TREE_VALUE (packs));
   14564            3 :           gcc_checking_assert (DECL_PACK_P (pack));
   14565            6 :           PACK_EXPANSION_PARAMETER_PACKS (result)
   14566            3 :             = build_tree_list (NULL_TREE, pack);
   14567            3 :           PACK_EXPANSION_AUTO_P (result) = true;
   14568              :         }
   14569              :       return result;
   14570              :     }
   14571              : 
   14572      7582336 :   gcc_assert (len >= 0);
   14573              : 
   14574              :   /* For each argument in each argument pack, substitute into the
   14575              :      pattern.  */
   14576      7582336 :   result = make_tree_vec (len);
   14577      7582336 :   tree elem_args = copy_template_args (args);
   14578     25442640 :   for (i = 0; i < len; ++i)
   14579              :     {
   14580     11023911 :       t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
   14581              :                                                     i,
   14582              :                                                     elem_args, complain,
   14583              :                                                     in_decl);
   14584     11023911 :       TREE_VEC_ELT (result, i) = t;
   14585     11023911 :       if (t == error_mark_node)
   14586              :         {
   14587              :           result = error_mark_node;
   14588              :           break;
   14589              :         }
   14590              :     }
   14591              : 
   14592              :   /* Update ARGS to restore the substitution from parameter packs to
   14593              :      their argument packs.  */
   14594     16029495 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   14595              :     {
   14596      8447159 :       tree parm = TREE_PURPOSE (pack);
   14597              : 
   14598      8447159 :       if (TREE_CODE (parm) == PARM_DECL
   14599      7626112 :           || VAR_P (parm)
   14600      7616611 :           || TREE_CODE (parm) == FIELD_DECL)
   14601       830563 :         register_local_specialization (TREE_TYPE (pack), parm);
   14602              :       else
   14603              :         {
   14604      7616596 :           int idx, level;
   14605              : 
   14606      7616596 :           if (TREE_VALUE (pack) == NULL_TREE)
   14607            0 :             continue;
   14608              : 
   14609      7616596 :           template_parm_level_and_index (parm, &level, &idx);
   14610              : 
   14611              :           /* Update the corresponding argument.  */
   14612     15233192 :           if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   14613      1335703 :             TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx)
   14614      2671406 :               = TREE_TYPE (pack);
   14615              :           else
   14616      6280893 :             TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
   14617              :         }
   14618              :     }
   14619              : 
   14620              :   /* If the dependent pack arguments were such that we end up with only a
   14621              :      single pack expansion again, there's no need to keep it in a TREE_VEC.  */
   14622      2154139 :   if (len == 1 && TREE_CODE (result) == TREE_VEC
   14623      9733309 :       && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
   14624       202171 :     return TREE_VEC_ELT (result, 0);
   14625              : 
   14626              :   return result;
   14627              : }
   14628              : 
   14629              : /* Substitute ARGS into T, which is a TREE_VEC.  This function creates a new
   14630              :    TREE_VEC rather than substituting the elements in-place.  */
   14631              : 
   14632              : static tree
   14633         7759 : tsubst_tree_vec (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14634              : {
   14635         7759 :   const int len = TREE_VEC_LENGTH (t);
   14636         7759 :   tree r = make_tree_vec (len);
   14637        42433 :   for (int i = 0; i < len; ++i)
   14638              :     {
   14639        26915 :       tree arg = TREE_VEC_ELT (t, i);
   14640        26915 :       if (TYPE_P (arg))
   14641         4388 :         TREE_VEC_ELT (r, i) = tsubst (arg, args, complain, in_decl);
   14642              :       else
   14643        22527 :         TREE_VEC_ELT (r, i) = tsubst_expr (arg, args, complain, in_decl);
   14644              :     }
   14645         7759 :   return r;
   14646              : }
   14647              : 
   14648              : /* Substitute ARGS into T, which is a pack index (i.e., PACK_INDEX_TYPE or
   14649              :    PACK_INDEX_EXPR).  Returns a single type or expression, a PACK_INDEX_*
   14650              :    node if only a partial substitution could be performed, or ERROR_MARK_NODE
   14651              :    if there was an error.  */
   14652              : 
   14653              : static tree
   14654        28493 : tsubst_pack_index (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14655              : {
   14656        28493 :   tree pack = PACK_INDEX_PACK (t);
   14657        28493 :   if (PACK_EXPANSION_P (pack))
   14658        20734 :     pack = tsubst_pack_expansion (pack, args, complain, in_decl);
   14659              :   else
   14660              :     {
   14661              :       /* PACK can be {*args#0} whose args#0's value-expr refers to
   14662              :          a partially instantiated closure.  Let tsubst find the
   14663              :          fully-instantiated one.  */
   14664         7759 :       gcc_assert (TREE_CODE (pack) == TREE_VEC);
   14665         7759 :       pack = tsubst_tree_vec (pack, args, complain, in_decl);
   14666              :     }
   14667        28493 :   if (TREE_CODE (pack) == TREE_VEC && TREE_VEC_LENGTH (pack) == 0)
   14668              :     {
   14669           12 :       if (complain & tf_error)
   14670           12 :         error ("cannot index an empty pack");
   14671           12 :       return error_mark_node;
   14672              :     }
   14673        28481 :   tree index = tsubst_expr (PACK_INDEX_INDEX (t), args, complain, in_decl);
   14674        28481 :   const bool parenthesized_p = (TREE_CODE (t) == PACK_INDEX_EXPR
   14675        28481 :                                 && PACK_INDEX_PARENTHESIZED_P (t));
   14676        28481 :   tree r;
   14677        28481 :   if (!type_dependent_expression_p (index))
   14678        28197 :     index = build_converted_constant_expr (size_type_node, index, complain);
   14679        28481 :   if (error_operand_p (index))
   14680            2 :     return error_mark_node;
   14681        28479 :   if (!value_dependent_expression_p (index) && TREE_CODE (pack) == TREE_VEC)
   14682        22552 :     r = pack_index_element (index, pack, parenthesized_p, complain);
   14683              :   else
   14684         5927 :     r = make_pack_index (pack, index);
   14685        28479 :   if (TREE_CODE (t) == PACK_INDEX_TYPE)
   14686         6433 :     r = cp_build_qualified_type (r, cp_type_quals (t) | cp_type_quals (r),
   14687              :                                  complain | tf_ignore_bad_quals);
   14688              :   return r;
   14689              : }
   14690              : 
   14691              : /* Make an argument pack out of the TREE_VEC VEC.  */
   14692              : 
   14693              : static tree
   14694           27 : make_argument_pack (tree vec)
   14695              : {
   14696           27 :   tree pack;
   14697              : 
   14698           27 :   if (TYPE_P (TREE_VEC_ELT (vec, 0)))
   14699           15 :     pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   14700              :   else
   14701              :     {
   14702           12 :       pack = make_node (NONTYPE_ARGUMENT_PACK);
   14703           12 :       TREE_CONSTANT (pack) = 1;
   14704              :     }
   14705           27 :   ARGUMENT_PACK_ARGS (pack) = vec;
   14706           27 :   return pack;
   14707              : }
   14708              : 
   14709              : /* Return an exact copy of template args T that can be modified
   14710              :    independently.  */
   14711              : 
   14712              : tree
   14713     25696814 : copy_template_args (tree t)
   14714              : {
   14715     25696814 :   if (t == error_mark_node)
   14716              :     return t;
   14717              : 
   14718     25696814 :   int len = TREE_VEC_LENGTH (t);
   14719     25696814 :   tree new_vec = make_tree_vec (len);
   14720              : 
   14721    110302407 :   for (int i = 0; i < len; ++i)
   14722              :     {
   14723     58908779 :       tree elt = TREE_VEC_ELT (t, i);
   14724     58908779 :       if (elt && TREE_CODE (elt) == TREE_VEC)
   14725      2109542 :         elt = copy_template_args (elt);
   14726     58908779 :       TREE_VEC_ELT (new_vec, i) = elt;
   14727              :     }
   14728              : 
   14729     51393628 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
   14730     25696814 :     = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   14731              : 
   14732     25696814 :   return new_vec;
   14733              : }
   14734              : 
   14735              : /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg.  */
   14736              : 
   14737              : tree
   14738    137474410 : tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
   14739              :                       tree in_decl)
   14740              : {
   14741              :   /* This flag is used only during deduction, and we don't expect to
   14742              :      substitute such ARGUMENT_PACKs.  */
   14743    137474410 :   gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
   14744              : 
   14745              :   /* Substitute into each of the arguments.  */
   14746    137474410 :   tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
   14747              :                                          args, complain, in_decl);
   14748    137474410 :   if (pack_args == error_mark_node)
   14749              :     return error_mark_node;
   14750              : 
   14751    136597743 :   if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
   14752              :     return orig_arg;
   14753              : 
   14754              :   /* If we're substituting into a generic ARGUMENT_PACK for a variadic
   14755              :      template parameter, we might be able to avoid allocating a new
   14756              :      ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
   14757              :      if the substituted result is identical to it.  */
   14758    132266211 :   if (tree parm = template_arg_to_parm (orig_arg))
   14759              :     {
   14760     57667722 :       int level, index;
   14761     57667722 :       template_parm_level_and_index (parm, &level, &index);
   14762    170271799 :       if (TMPL_ARGS_DEPTH (args) >= level)
   14763    101154996 :         if (tree arg = TMPL_ARG (args, level, index))
   14764     47312082 :           if (TREE_CODE (arg) == TREE_CODE (orig_arg)
   14765     47312082 :               && ARGUMENT_PACK_ARGS (arg) == pack_args)
   14766              :             {
   14767     45787756 :               gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
   14768     45787756 :               return arg;
   14769              :             }
   14770              :     }
   14771              : 
   14772     86478455 :   tree new_arg;
   14773     86478455 :   if (TYPE_P (orig_arg))
   14774              :     {
   14775     85652601 :       new_arg = cxx_make_type (TREE_CODE (orig_arg));
   14776     85652601 :       SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
   14777              :     }
   14778              :   else
   14779              :     {
   14780       825854 :       new_arg = make_node (TREE_CODE (orig_arg));
   14781       825854 :       TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
   14782              :     }
   14783     86478455 :   ARGUMENT_PACK_ARGS (new_arg) = pack_args;
   14784              :   return new_arg;
   14785              : }
   14786              : 
   14787              : /* Substitute ARGS into the vector or list of template arguments T.  */
   14788              : 
   14789              : tree
   14790   1824716954 : tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   14791              : {
   14792   1824716954 :   if (t == error_mark_node)
   14793              :     return error_mark_node;
   14794              : 
   14795              :   /* In "sizeof(X<I>)" we need to evaluate "I".  */
   14796   1824716954 :   cp_evaluated ev;
   14797              : 
   14798   1824716954 :   const int len = TREE_VEC_LENGTH (t);
   14799   1824716954 :   tree *elts = XALLOCAVEC (tree, len);
   14800   1824716954 :   int expanded_len_adjust = 0;
   14801              : 
   14802              :   /* True iff the substituted result is identical to T.  */
   14803   1824716954 :   bool const_subst_p = true;
   14804              : 
   14805   5381391516 :   for (int i = 0; i < len; i++)
   14806              :     {
   14807   3558769311 :       tree orig_arg = TREE_VEC_ELT (t, i);
   14808   3558769311 :       tree new_arg;
   14809              : 
   14810   3558769311 :       if (!orig_arg)
   14811              :         new_arg = NULL_TREE;
   14812   3558769239 :       else if (TREE_CODE (orig_arg) == TREE_VEC)
   14813     84898959 :         new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
   14814   3473870280 :       else if (PACK_EXPANSION_P (orig_arg))
   14815              :         {
   14816              :           /* Substitute into an expansion expression.  */
   14817     81574167 :           new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
   14818              : 
   14819     81574167 :           if (TREE_CODE (new_arg) == TREE_VEC)
   14820              :             /* Add to the expanded length adjustment the number of
   14821              :                expanded arguments. We subtract one from this
   14822              :                measurement, because the argument pack expression
   14823              :                itself is already counted as 1 in
   14824              :                LEN. EXPANDED_LEN_ADJUST can actually be negative, if
   14825              :                the argument pack is empty.  */
   14826     64737064 :             expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
   14827              :         }
   14828   3392296113 :       else if (ARGUMENT_PACK_P (orig_arg))
   14829    134138439 :         new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
   14830              :       else
   14831   3258157674 :         new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
   14832              : 
   14833   3558769275 :       if (new_arg == error_mark_node)
   14834              :         return error_mark_node;
   14835              : 
   14836   3556674562 :       elts[i] = new_arg;
   14837   3556674562 :       if (new_arg != orig_arg)
   14838   3434413807 :         const_subst_p = false;
   14839              :     }
   14840              : 
   14841   1822622205 :   if (const_subst_p)
   14842              :     return t;
   14843              : 
   14844   1804054071 :   tree maybe_reuse = NULL_TREE;
   14845              : 
   14846              :   /* If ARGS and T are both multi-level, the substituted result may be
   14847              :      identical to ARGS.  */
   14848   1804054071 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
   14849     84690294 :       && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
   14850      8354535 :       && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
   14851              :     maybe_reuse = args;
   14852              :   /* If T appears to be a vector of generic template arguments, the
   14853              :      substituted result may be identical to the corresponding level
   14854              :      from ARGS.  */
   14855   1796585280 :   else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
   14856              :     {
   14857   1365646299 :       int level, index;
   14858   1365646299 :       template_parm_level_and_index (parm, &level, &index);
   14859   3883529621 :       if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
   14860   2476453264 :         maybe_reuse = TMPL_ARGS_LEVEL (args, level);
   14861              :     }
   14862              : 
   14863              :   /* If the substituted result is identical to MAYBE_REUSE, return
   14864              :      it and avoid allocating a new TREE_VEC, as an optimization.  */
   14865    127419667 :   if (maybe_reuse != NULL_TREE
   14866   1245695423 :       && TREE_VEC_LENGTH (maybe_reuse) == len
   14867   2362220754 :       && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
   14868              :     return maybe_reuse;
   14869              : 
   14870              :   /* If T consists of only a pack expansion for which substitution yielded
   14871              :      a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
   14872              :      of effectively making a copy.  */
   14873    717565246 :   if (len == 1
   14874    425961774 :       && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
   14875    784464686 :       && TREE_CODE (elts[0]) == TREE_VEC)
   14876              :     return elts[0];
   14877              : 
   14878              :   /* Make space for the expanded arguments coming from template
   14879              :      argument packs.  */
   14880    663100373 :   tree r = make_tree_vec (len + expanded_len_adjust);
   14881              :   /* T can contain TREE_VECs. That happens if T contains the
   14882              :      arguments for a member template.
   14883              :      In that case each TREE_VEC in T represents a level of template
   14884              :      arguments, and T won't carry any non defaulted argument count.
   14885              :      It will rather be the nested TREE_VECs that will carry one.
   14886              :      In other words, T carries a non defaulted argument count only
   14887              :      if it doesn't contain any nested TREE_VEC.  */
   14888    663100373 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
   14889              :     {
   14890    627031380 :       int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   14891    627031380 :       count += expanded_len_adjust;
   14892    627031380 :       SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
   14893              :     }
   14894              : 
   14895    663100373 :   int out = 0;
   14896   1693214269 :   for (int i = 0; i < len; i++)
   14897              :     {
   14898   1030113896 :       tree orig_arg = TREE_VEC_ELT (t, i);
   14899   1030113896 :       if (orig_arg
   14900   1030113869 :           && PACK_EXPANSION_P (orig_arg)
   14901     23605695 :           && TREE_CODE (elts[i]) == TREE_VEC)
   14902              :         {
   14903              :           /* Now expand the template argument pack "in place".  */
   14904     25373514 :           for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
   14905     15101323 :             TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
   14906              :         }
   14907              :       else
   14908              :         {
   14909   1019841705 :           TREE_VEC_ELT (r, out) = elts[i];
   14910   1019841705 :           out++;
   14911              :         }
   14912              :     }
   14913    663100373 :   gcc_assert (out == TREE_VEC_LENGTH (r));
   14914              : 
   14915              :   return r;
   14916   1824716918 : }
   14917              : 
   14918              : /* Substitute ARGS into one level PARMS of template parameters.  */
   14919              : 
   14920              : static tree
   14921     28410588 : tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
   14922              : {
   14923     28410588 :   if (parms == error_mark_node)
   14924              :     return error_mark_node;
   14925              : 
   14926     28410588 :   tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
   14927              : 
   14928     99646615 :   for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
   14929              :     {
   14930     42825439 :       tree tuple = TREE_VEC_ELT (parms, i);
   14931              : 
   14932     42825439 :       if (tuple == error_mark_node)
   14933            0 :         continue;
   14934              : 
   14935     42825439 :       TREE_VEC_ELT (new_vec, i) =
   14936     42825439 :         tsubst_template_parm (tuple, args, complain);
   14937              :     }
   14938              : 
   14939              :   return new_vec;
   14940              : }
   14941              : 
   14942              : /* Return the result of substituting ARGS into the template parameters
   14943              :    given by PARMS.  If there are m levels of ARGS and m + n levels of
   14944              :    PARMS, then the result will contain n levels of PARMS.  For
   14945              :    example, if PARMS is `template <class T> template <class U>
   14946              :    template <T*, U, class V>' and ARGS is {{int}, {double}} then the
   14947              :    result will be `template <int*, double, class V>'.  */
   14948              : 
   14949              : static tree
   14950     28529413 : tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
   14951              : {
   14952     28529413 :   tree r = NULL_TREE;
   14953     28529413 :   tree* new_parms;
   14954              : 
   14955              :   /* When substituting into a template, we must set
   14956              :      PROCESSING_TEMPLATE_DECL as the template parameters may be
   14957              :      dependent if they are based on one-another, and the dependency
   14958              :      predicates are short-circuit outside of templates.  */
   14959     28529413 :   ++processing_template_decl;
   14960              : 
   14961     28529413 :   for (new_parms = &r;
   14962    170574723 :        parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
   14963     56821036 :        new_parms = &(TREE_CHAIN (*new_parms)),
   14964     28410518 :          parms = TREE_CHAIN (parms))
   14965              :     {
   14966     28410518 :       tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
   14967              :                                                   args, complain);
   14968     56821036 :       *new_parms =
   14969     56821030 :         tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
   14970              :                              - TMPL_ARGS_DEPTH (args)),
   14971              :                    new_vec, NULL_TREE);
   14972     56821036 :       TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
   14973     56821036 :         = TEMPLATE_PARMS_CONSTRAINTS (parms);
   14974              :     }
   14975              : 
   14976     28529413 :   --processing_template_decl;
   14977              : 
   14978     28529413 :   return r;
   14979              : }
   14980              : 
   14981              : /* Return the result of substituting ARGS into one template parameter
   14982              :    given by T. T Must be a TREE_LIST which TREE_VALUE is the template
   14983              :    parameter and which TREE_PURPOSE is the default argument of the
   14984              :    template parameter.  */
   14985              : 
   14986              : static tree
   14987     42825439 : tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
   14988              : {
   14989     42825439 :   tree default_value, parm_decl;
   14990              : 
   14991     42825439 :   if (args == NULL_TREE
   14992     42825439 :       || t == NULL_TREE
   14993     42825433 :       || t == error_mark_node)
   14994              :     return t;
   14995              : 
   14996     42825433 :   gcc_assert (TREE_CODE (t) == TREE_LIST);
   14997              : 
   14998     42825433 :   default_value = TREE_PURPOSE (t);
   14999     42825433 :   parm_decl = TREE_VALUE (t);
   15000              : 
   15001     42825433 :   parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
   15002     42825433 :   if (TREE_CODE (parm_decl) == PARM_DECL
   15003     42825433 :       && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
   15004           28 :     parm_decl = error_mark_node;
   15005     42825433 :   default_value = tsubst_template_arg (default_value, args,
   15006              :                                        complain, NULL_TREE);
   15007              : 
   15008     42825433 :   tree r = build_tree_list (default_value, parm_decl);
   15009     42825433 :   TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
   15010     42825433 :   return r;
   15011              : }
   15012              : 
   15013              : /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
   15014              :    parameter in PARMS for sake of declaration matching.  */
   15015              : 
   15016              : static void
   15017       661244 : tsubst_each_template_parm_constraints (tree parms, tree args,
   15018              :                                        tsubst_flags_t complain)
   15019              : {
   15020       661244 :   ++processing_template_decl;
   15021      1322488 :   for (; parms; parms = TREE_CHAIN (parms))
   15022              :     {
   15023       661244 :       tree level = TREE_VALUE (parms);
   15024      1988415 :       for (tree parm : tree_vec_range (level))
   15025      2654342 :         TEMPLATE_PARM_CONSTRAINTS (parm)
   15026      2654342 :           = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
   15027              :                                complain, NULL_TREE);
   15028              :     }
   15029       661244 :   --processing_template_decl;
   15030       661244 : }
   15031              : 
   15032              : /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
   15033              :    indexed in reverse order of the parameters.  */
   15034              : 
   15035              : static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
   15036              : 
   15037              : /* Return a reference to the vec* of defarg insts for FN.  */
   15038              : 
   15039              : static vec<tree,va_gc> *&
   15040       816784 : defarg_insts_for (tree fn)
   15041              : {
   15042       816784 :   if (!defarg_inst)
   15043        14221 :     defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
   15044       816784 :   tree_vec_map in = { { fn }, nullptr };
   15045       816784 :   tree_vec_map **slot
   15046       816784 :     = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
   15047       816784 :   if (!*slot)
   15048              :     {
   15049       539129 :       *slot = ggc_alloc<tree_vec_map> ();
   15050       539129 :       **slot = in;
   15051              :     }
   15052       816784 :   return (*slot)->to;
   15053              : }
   15054              : 
   15055              : /* Substitute into the default argument ARG (a default argument for
   15056              :    FN), which has the indicated TYPE.  */
   15057              : 
   15058              : tree
   15059       844441 : tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
   15060              :                          tsubst_flags_t complain)
   15061              : {
   15062       844441 :   int errs = errorcount + sorrycount;
   15063              : 
   15064              :   /* This can happen in invalid code.  */
   15065       844441 :   if (TREE_CODE (arg) == DEFERRED_PARSE)
   15066              :     return arg;
   15067              : 
   15068              :   /* Shortcut {}.  */
   15069        27754 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg)
   15070       872098 :       && CONSTRUCTOR_NELTS (arg) == 0)
   15071              :     return arg;
   15072              : 
   15073       816784 :   tree parm = FUNCTION_FIRST_USER_PARM (fn);
   15074       816784 :   parm = chain_index (parmnum, parm);
   15075       816784 :   tree parmtype = TREE_TYPE (parm);
   15076       816784 :   if (DECL_BY_REFERENCE (parm))
   15077           65 :     parmtype = TREE_TYPE (parmtype);
   15078       816784 :   if (parmtype == error_mark_node)
   15079              :     return error_mark_node;
   15080              : 
   15081       816784 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
   15082              : 
   15083              :   /* Remember the location of the pointer to the vec rather than the location
   15084              :      of the particular element, in case the vec grows in tsubst_expr.  */
   15085       816784 :   vec<tree,va_gc> *&defs = defarg_insts_for (fn);
   15086              :   /* Index in reverse order to avoid allocating space for initial parameters
   15087              :      that don't have default arguments.  */
   15088       816784 :   unsigned ridx = list_length (parm);
   15089      1094439 :   if (vec_safe_length (defs) < ridx)
   15090       539270 :     vec_safe_grow_cleared (defs, ridx);
   15091       277514 :   else if (tree inst = (*defs)[ridx - 1])
   15092              :     return inst;
   15093              : 
   15094              :   /* This default argument came from a template.  Instantiate the
   15095              :      default argument here, not in tsubst.  In the case of
   15096              :      something like:
   15097              : 
   15098              :        template <class T>
   15099              :        struct S {
   15100              :          static T t();
   15101              :          void f(T = t());
   15102              :        };
   15103              : 
   15104              :      we must be careful to do name lookup in the scope of S<T>,
   15105              :      rather than in the current class.  */
   15106       573344 :   push_to_top_level ();
   15107       573344 :   push_access_scope (fn);
   15108       573344 :   push_deferring_access_checks (dk_no_deferred);
   15109              :   /* So in_immediate_context knows this is a default argument.  */
   15110       573344 :   begin_scope (sk_function_parms, fn);
   15111       573344 :   start_lambda_scope (parm);
   15112              : 
   15113              :   /* The default argument expression may cause implicitly defined
   15114              :      member functions to be synthesized, which will result in garbage
   15115              :      collection.  We must treat this situation as if we were within
   15116              :      the body of function so as to avoid collecting live data on the
   15117              :      stack.  */
   15118       573344 :   ++function_depth;
   15119       573344 :   arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
   15120       573344 :   --function_depth;
   15121              : 
   15122       573344 :   finish_lambda_scope ();
   15123              : 
   15124              :   /* Make sure the default argument is reasonable.  */
   15125       573344 :   arg = check_default_argument (type, arg, complain);
   15126              : 
   15127       573344 :   if (errorcount+sorrycount > errs
   15128       573344 :       && (complain & tf_warning_or_error))
   15129           15 :     inform (input_location,
   15130              :             "  when instantiating default argument for call to %qD", fn);
   15131              : 
   15132       573344 :   leave_scope ();
   15133       573344 :   pop_deferring_access_checks ();
   15134       573344 :   pop_access_scope (fn);
   15135       573344 :   pop_from_top_level ();
   15136              : 
   15137       573344 :   if (arg != error_mark_node && !cp_unevaluated_operand)
   15138       570481 :     (*defs)[ridx - 1] = arg;
   15139              : 
   15140              :   return arg;
   15141              : }
   15142              : 
   15143              : /* Substitute into all the default arguments for FN.  */
   15144              : 
   15145              : static void
   15146       949524 : tsubst_default_arguments (tree fn, tsubst_flags_t complain)
   15147              : {
   15148       949524 :   tree arg;
   15149       949524 :   tree tmpl_args;
   15150              : 
   15151       949524 :   tmpl_args = DECL_TI_ARGS (fn);
   15152              : 
   15153              :   /* If this function is not yet instantiated, we certainly don't need
   15154              :      its default arguments.  */
   15155       949524 :   if (uses_template_parms (tmpl_args))
   15156              :     return;
   15157              :   /* Don't do this again for clones.  */
   15158       949524 :   if (DECL_CLONED_FUNCTION_P (fn))
   15159              :     return;
   15160              : 
   15161       949524 :   int i = 0;
   15162       949524 :   for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
   15163      3762786 :        arg;
   15164      2813262 :        arg = TREE_CHAIN (arg), ++i)
   15165      2813262 :     if (TREE_PURPOSE (arg))
   15166           12 :       TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
   15167           12 :                                                     TREE_VALUE (arg),
   15168           12 :                                                     TREE_PURPOSE (arg),
   15169              :                                                     complain);
   15170              : }
   15171              : 
   15172              : /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier.  */
   15173              : static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
   15174              : 
   15175              : /* Store a pair to EXPLICIT_SPECIFIER_MAP.  */
   15176              : 
   15177              : void
   15178      1692493 : store_explicit_specifier (tree v, tree t)
   15179              : {
   15180      1692493 :   if (!explicit_specifier_map)
   15181        13661 :     explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
   15182      1692493 :   DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
   15183      1692493 :   explicit_specifier_map->put (v, t);
   15184      1692493 : }
   15185              : 
   15186              : /* Lookup an element in EXPLICIT_SPECIFIER_MAP.  */
   15187              : 
   15188              : tree
   15189      1910055 : lookup_explicit_specifier (tree v)
   15190              : {
   15191      1910055 :   return *explicit_specifier_map->get (v);
   15192              : }
   15193              : 
   15194              : /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
   15195              :    FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
   15196              :    are ARG_TYPES, and exception specification is RAISES, and otherwise is
   15197              :    identical to T.  */
   15198              : 
   15199              : static tree
   15200    172669901 : rebuild_function_or_method_type (tree t, tree args, tree return_type,
   15201              :                                  tree arg_types, tree raises,
   15202              :                                  tsubst_flags_t complain)
   15203              : {
   15204    172669901 :   gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
   15205              : 
   15206    172669901 :   tree new_type;
   15207    172669901 :   if (TREE_CODE (t) == FUNCTION_TYPE)
   15208              :     {
   15209     65625263 :       new_type = cp_build_function_type (return_type, arg_types);
   15210     65625263 :       new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
   15211              :     }
   15212              :   else
   15213              :     {
   15214    107044638 :       tree r = TREE_TYPE (TREE_VALUE (arg_types));
   15215              :       /* Don't pick up extra function qualifiers from the basetype.  */
   15216    107044638 :       r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
   15217    107044638 :       if (! MAYBE_CLASS_TYPE_P (r))
   15218              :         {
   15219              :           /* [temp.deduct]
   15220              : 
   15221              :              Type deduction may fail for any of the following
   15222              :              reasons:
   15223              : 
   15224              :              -- Attempting to create "pointer to member of T" when T
   15225              :              is not a class type.  */
   15226            0 :           if (complain & tf_error)
   15227            0 :             error ("creating pointer to member function of non-class type %qT",
   15228              :                    r);
   15229            0 :           return error_mark_node;
   15230              :         }
   15231              : 
   15232    107044638 :       new_type = build_method_type_directly (r, return_type,
   15233    107044638 :                                              TREE_CHAIN (arg_types));
   15234              :     }
   15235    172669901 :   if (!apply_late_template_attributes (&new_type, TYPE_ATTRIBUTES (t), 0,
   15236              :                                        args, complain, NULL_TREE))
   15237            0 :     return error_mark_node;
   15238              : 
   15239    172669901 :   cp_ref_qualifier rqual = type_memfn_rqual (t);
   15240    172669901 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   15241    172669901 :   return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
   15242              : }
   15243              : 
   15244              : /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
   15245              :    each of its formal parameters.  If there is a disagreement then rebuild
   15246              :    DECL's function type according to its formal parameter types, as part of a
   15247              :    resolution for Core issues 1001/1322.  */
   15248              : 
   15249              : static void
   15250    148208589 : maybe_rebuild_function_decl_type (tree decl, tree args)
   15251              : {
   15252    148208589 :   bool function_type_needs_rebuilding = false;
   15253    148208589 :   if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
   15254              :     {
   15255    108242739 :       tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
   15256    403685288 :       while (parm_type_list && parm_type_list != void_list_node)
   15257              :         {
   15258    187199916 :           tree parm_type = TREE_VALUE (parm_type_list);
   15259    187199916 :           tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   15260    187199916 :           if (!same_type_p (parm_type, formal_parm_type_unqual))
   15261              :             {
   15262              :               function_type_needs_rebuilding = true;
   15263              :               break;
   15264              :             }
   15265              : 
   15266    187199810 :           parm_list = DECL_CHAIN (parm_list);
   15267    187199810 :           parm_type_list = TREE_CHAIN (parm_type_list);
   15268              :         }
   15269              :     }
   15270              : 
   15271    108242739 :   if (!function_type_needs_rebuilding)
   15272    148208483 :     return;
   15273              : 
   15274          106 :   const tree fntype = TREE_TYPE (decl);
   15275          106 :   tree parm_list = DECL_ARGUMENTS (decl);
   15276          106 :   tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
   15277          106 :   tree new_parm_type_list = NULL_TREE;
   15278          106 :   tree *q = &new_parm_type_list;
   15279          136 :   for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
   15280              :     {
   15281           30 :       *q = copy_node (old_parm_type_list);
   15282           30 :       parm_list = DECL_CHAIN (parm_list);
   15283           30 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   15284           30 :       q = &TREE_CHAIN (*q);
   15285              :     }
   15286          221 :   while (old_parm_type_list && old_parm_type_list != void_list_node)
   15287              :     {
   15288          115 :       *q = copy_node (old_parm_type_list);
   15289          115 :       tree *new_parm_type = &TREE_VALUE (*q);
   15290          115 :       tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   15291          115 :       if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
   15292          106 :         *new_parm_type = formal_parm_type_unqual;
   15293              : 
   15294          115 :       parm_list = DECL_CHAIN (parm_list);
   15295          115 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   15296          115 :       q = &TREE_CHAIN (*q);
   15297              :     }
   15298          106 :   if (old_parm_type_list == void_list_node)
   15299          106 :     *q = void_list_node;
   15300              : 
   15301          106 :   TREE_TYPE (decl)
   15302          212 :     = rebuild_function_or_method_type (fntype, args,
   15303          106 :                                        TREE_TYPE (fntype), new_parm_type_list,
   15304          106 :                                        TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
   15305              : }
   15306              : 
   15307              : /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL.  */
   15308              : 
   15309              : static tree
   15310    151054576 : tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
   15311              :                       tree lambda_fntype, bool use_spec_table = true)
   15312              : {
   15313    151054576 :   tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
   15314    151054576 :   hashval_t hash = 0;
   15315    151054576 :   tree in_decl = t;
   15316              : 
   15317              :   /* Nobody should be tsubst'ing into non-template functions.  */
   15318    151054576 :   gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
   15319              :               || DECL_LOCAL_DECL_P (t));
   15320              : 
   15321    151054576 :   if (DECL_LOCAL_DECL_P (t))
   15322              :     {
   15323          387 :       if (tree spec = retrieve_local_specialization (t))
   15324              :         return spec;
   15325              :     }
   15326    151054189 :   else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
   15327              :     {
   15328              :       /* If T is not dependent, just return it.  */
   15329    151025013 :       if (!uses_template_parms (DECL_TI_ARGS (t))
   15330    151025086 :           && !LAMBDA_FUNCTION_P (t))
   15331              :         return t;
   15332              : 
   15333              :       /* A non-templated friend doesn't get DECL_TEMPLATE_INFO.  */
   15334    150491329 :       if (non_templated_friend_p (t))
   15335        23587 :         goto friend_case;
   15336              : 
   15337              :       /* Calculate the most general template of which R is a
   15338              :          specialization.  */
   15339    150467742 :       gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
   15340              : 
   15341              :       /* We're substituting a lambda function under tsubst_lambda_expr but not
   15342              :          directly from it; find the matching function we're already inside.
   15343              :          But don't do this if T is a generic lambda with a single level of
   15344              :          template parms, as in that case we're doing a normal instantiation. */
   15345    152418071 :       if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
   15346    150609339 :           && (!generic_lambda_fn_p (t)
   15347       141573 :               || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
   15348           57 :         return enclosing_instantiation_of (t);
   15349              : 
   15350              :       /* Calculate the complete set of arguments used to
   15351              :          specialize R.  */
   15352    150467685 :       if (use_spec_table && !lambda_fntype)
   15353              :         {
   15354     93133250 :           argvec = tsubst_template_args (DECL_TI_ARGS
   15355              :                                          (DECL_TEMPLATE_RESULT
   15356              :                                           (DECL_TI_TEMPLATE (t))),
   15357              :                                          args, complain, in_decl);
   15358     93133250 :           if (argvec == error_mark_node)
   15359              :             return error_mark_node;
   15360              : 
   15361              :           /* Check to see if we already have this specialization.  */
   15362     93133250 :           hash = spec_hasher::hash (gen_tmpl, argvec);
   15363     93133250 :           if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
   15364              :             /* The spec for these args might be a partial instantiation of the
   15365              :                template, but here what we want is the FUNCTION_DECL.  */
   15366       628719 :             return STRIP_TEMPLATE (spec);
   15367              :         }
   15368              :       else
   15369              :         argvec = args;
   15370              :     }
   15371              :   else
   15372              :     {
   15373              :       /* This special case arises when we have something like this:
   15374              : 
   15375              :          template <class T> struct S {
   15376              :            friend void f<int>(int, double);
   15377              :          };
   15378              : 
   15379              :          Here, the DECL_TI_TEMPLATE for the friend declaration
   15380              :          will be an IDENTIFIER_NODE.  We are being called from
   15381              :          tsubst_friend_function, and we want only to create a
   15382              :          new decl (R) with appropriate types so that we can call
   15383              :          determine_specialization.  */
   15384        29176 :     friend_case:
   15385              :       gen_tmpl = NULL_TREE;
   15386              :       argvec = NULL_TREE;
   15387              :     }
   15388              : 
   15389              :   /* Make sure tsubst_decl substitutes all the parameters.  */
   15390    299776096 :   cp_evaluated ev;
   15391              : 
   15392    149892116 :   tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
   15393       246958 :                   : NULL_TREE);
   15394    149892116 :   tree ctx = closure ? closure : DECL_CONTEXT (t);
   15395    149892116 :   bool member = ctx && TYPE_P (ctx);
   15396              : 
   15397              :   /* If this is a static or xobj lambda, remove the 'this' pointer added in
   15398              :      tsubst_lambda_expr now that we know the closure type.  */
   15399    149892116 :   if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15400          308 :     lambda_fntype = static_fn_type (lambda_fntype);
   15401              : 
   15402    149892116 :   if (member && !closure)
   15403    120444793 :     ctx = tsubst_entering_scope (ctx, args, complain, t);
   15404              : 
   15405    149892116 :   tree type = (lambda_fntype ? lambda_fntype
   15406    149645158 :                : tsubst (TREE_TYPE (t), args,
   15407              :                          complain | tf_fndecl_type, in_decl));
   15408    149883980 :   if (type == error_mark_node)
   15409              :     return error_mark_node;
   15410              : 
   15411              :   /* If we hit excessive deduction depth, the type is bogus even if
   15412              :      it isn't error_mark_node, so don't build a decl.  */
   15413    148208616 :   if (excessive_deduction_depth)
   15414              :     return error_mark_node;
   15415              : 
   15416              :   /* We do NOT check for matching decls pushed separately at this
   15417              :      point, as they may not represent instantiations of this
   15418              :      template, and in any case are considered separate under the
   15419              :      discrete model.  */
   15420    148208616 :   tree r = copy_decl (t);
   15421    148208616 :   DECL_USE_TEMPLATE (r) = 0;
   15422    148208616 :   TREE_TYPE (r) = type;
   15423              :   /* Clear out the mangled name and RTL for the instantiation.  */
   15424    148208616 :   SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   15425    148208616 :   SET_DECL_RTL (r, NULL);
   15426              :   /* Leave DECL_INITIAL set on deleted instantiations.  */
   15427    148208616 :   if (!DECL_DELETED_FN (r))
   15428    144974162 :     DECL_INITIAL (r) = NULL_TREE;
   15429    148208616 :   DECL_CONTEXT (r) = ctx;
   15430    148208616 :   set_instantiating_module (r);
   15431              : 
   15432              :   /* Handle explicit(dependent-expr).  */
   15433    148208616 :   if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
   15434              :     {
   15435      1901123 :       tree spec = lookup_explicit_specifier (t);
   15436      1901123 :       spec = tsubst_expr (spec, args, complain, in_decl);
   15437      1901123 :       spec = build_explicit_specifier (spec, complain);
   15438      1901123 :       if (spec == error_mark_node)
   15439              :         return error_mark_node;
   15440      1901102 :       if (instantiation_dependent_expression_p (spec))
   15441      1233539 :         store_explicit_specifier (r, spec);
   15442              :       else
   15443              :         {
   15444       667563 :           DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
   15445       667563 :           DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
   15446              :         }
   15447              :     }
   15448              : 
   15449              :   /* OpenMP UDRs have the only argument a reference to the declared
   15450              :      type.  We want to diagnose if the declared type is a reference,
   15451              :      which is invalid, but as references to references are usually
   15452              :      quietly merged, diagnose it here.  */
   15453    148208595 :   if (DECL_OMP_DECLARE_REDUCTION_P (t))
   15454              :     {
   15455          267 :       tree argtype
   15456          267 :         = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
   15457          267 :       argtype = tsubst (argtype, args, complain, in_decl);
   15458          267 :       if (TYPE_REF_P (argtype))
   15459            6 :         error_at (DECL_SOURCE_LOCATION (t),
   15460              :                   "reference type %qT in "
   15461              :                   "%<#pragma omp declare reduction%>", argtype);
   15462          267 :       if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   15463          210 :         DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
   15464              :                                           argtype);
   15465              :     }
   15466              : 
   15467    148208595 :   if (member && DECL_CONV_FN_P (r))
   15468              :     /* Type-conversion operator.  Reconstruct the name, in
   15469              :        case it's the name of one of the template's parameters.  */
   15470      1399282 :     DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
   15471              : 
   15472    148208595 :   tree parms = DECL_ARGUMENTS (t);
   15473    148208595 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15474       246650 :     parms = DECL_CHAIN (parms);
   15475    148208595 :   parms = tsubst (parms, args, complain, t);
   15476    148208595 :   if (parms == error_mark_node)
   15477              :     return error_mark_node;
   15478    442313845 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   15479    294105256 :     DECL_CONTEXT (parm) = r;
   15480    148208589 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   15481              :     {
   15482       246650 :       tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
   15483       246650 :       DECL_NAME (tparm) = closure_identifier;
   15484       246650 :       DECL_CHAIN (tparm) = parms;
   15485       246650 :       parms = tparm;
   15486              :     }
   15487    148208589 :   DECL_ARGUMENTS (r) = parms;
   15488    148208589 :   DECL_RESULT (r) = NULL_TREE;
   15489              : 
   15490    148208589 :   maybe_rebuild_function_decl_type (r, args);
   15491              : 
   15492    148208589 :   TREE_STATIC (r) = 0;
   15493    148208589 :   TREE_PUBLIC (r) = TREE_PUBLIC (t);
   15494    148208589 :   DECL_EXTERNAL (r) = 1;
   15495              :   /* If this is an instantiation of a function with internal
   15496              :      linkage, we already know what object file linkage will be
   15497              :      assigned to the instantiation.  */
   15498    148208589 :   DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
   15499    148208589 :   DECL_DEFER_OUTPUT (r) = 0;
   15500    148208589 :   DECL_CHAIN (r) = NULL_TREE;
   15501    148208589 :   DECL_PENDING_INLINE_INFO (r) = 0;
   15502    148208589 :   DECL_PENDING_INLINE_P (r) = 0;
   15503    148208589 :   DECL_SAVED_TREE (r) = NULL_TREE;
   15504    148208589 :   DECL_STRUCT_FUNCTION (r) = NULL;
   15505    148208589 :   TREE_USED (r) = 0;
   15506              :   /* We'll re-clone as appropriate in instantiate_template.  */
   15507    148208589 :   DECL_CLONED_FUNCTION (r) = NULL_TREE;
   15508              : 
   15509              :   /* If we aren't complaining now, return on error before we register
   15510              :      the specialization so that we'll complain eventually.  */
   15511    148208589 :   if ((complain & tf_error) == 0
   15512     31117077 :       && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   15513    150450965 :       && !grok_op_properties (r, /*complain=*/false))
   15514            3 :     return error_mark_node;
   15515              : 
   15516              :   /* If we are looking at an xobj lambda, we might need to check the type of
   15517              :      its xobj parameter.  */
   15518    150157968 :   if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r))
   15519              :     {
   15520         1083 :       tree closure_obj = DECL_CONTEXT (r);
   15521         1083 :       tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj);
   15522         1083 :       tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r));
   15523              : 
   15524         1083 :       if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
   15525          763 :             || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)))
   15526              :         /* If a lambda has an empty capture clause, an xobj parameter of
   15527              :            unrelated type is not an error.  */;
   15528          871 :       else if (dependent_type_p (obj_param))
   15529              :         /* If we are coming from tsubst_lambda_expr we might not have
   15530              :            substituted into our xobj parameter yet.  We can't error out until
   15531              :            we know what the type really is so do nothing...
   15532              :            ...but if we are instantiating the call op for real and we don't
   15533              :            have a real type then something has gone incredibly wrong.  */
   15534          200 :         gcc_assert (lambda_fntype);
   15535              :       else
   15536              :         {
   15537              :           /* We have a lambda with captures, and know the type of the xobj
   15538              :              parameter, time to check it.  */
   15539          671 :           tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param));
   15540          671 :           if (!same_or_base_type_p (closure_obj, obj_param_type))
   15541              :             {
   15542              :               /* This error does not emit when the lambda's call operator
   15543              :                  template is instantiated by taking its address, such as in
   15544              :                  the following case:
   15545              : 
   15546              :                  auto f = [x = 0](this auto&&){};
   15547              :                  int (*fp)(int&) = &decltype(f)::operator();
   15548              : 
   15549              :                  It only emits when explicitly calling the call operator with
   15550              :                  an explicit template parameter:
   15551              : 
   15552              :                  template<typename T>
   15553              :                  struct S : T {
   15554              :                    using T::operator();
   15555              :                    operator int() const {return {};}
   15556              :                  };
   15557              : 
   15558              :                  auto s = S{[x = 0](this auto&&) {}};
   15559              :                  s.operator()<int>();
   15560              : 
   15561              :                  This is due to resolve_address_of_overloaded_function being
   15562              :                  deficient at reporting candidates when overload resolution
   15563              :                  fails.
   15564              : 
   15565              :                  This diagnostic will be active in the first case if/when
   15566              :                  resolve_address_of_overloaded_function is fixed to properly
   15567              :                  emit candidates upon failure to resolve to an overload.  */
   15568           54 :               if (complain & tf_error)
   15569            3 :                 error ("a lambda with captures may not have an explicit "
   15570              :                        "object parameter of an unrelated type");
   15571           54 :               return error_mark_node;
   15572              :             }
   15573              :         }
   15574              :     }
   15575              : 
   15576              :   /* Associate the constraints directly with the instantiation. We
   15577              :      don't substitute through the constraints; that's only done when
   15578              :      they are checked.  */
   15579    148208532 :   if (tree ci = get_constraints (t))
   15580      7986127 :     set_constraints (r, ci);
   15581              : 
   15582              :   /* copy_decl () does not know about contract specifiers.  NOTE these are not
   15583              :      substituted at this point.  */
   15584    148208532 :   if (tree ctrct = get_fn_contract_specifiers (t))
   15585          369 :     set_fn_contract_specifiers (r, ctrct);
   15586              : 
   15587              :   /* The parms have now been substituted, check for incorrect const cases.  */
   15588    148208532 :   check_postconditions_in_redecl (t, r);
   15589              : 
   15590    292898729 :   if (DECL_FRIEND_CONTEXT (t))
   15591      3614354 :     SET_DECL_FRIEND_CONTEXT (r,
   15592              :                              tsubst (DECL_FRIEND_CONTEXT (t),
   15593              :                                      args, complain, in_decl));
   15594              : 
   15595    148208532 :   if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15596              :                                        args, complain, in_decl))
   15597           15 :     return error_mark_node;
   15598              : 
   15599              :   /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
   15600              :      this in the special friend case mentioned above where
   15601              :      GEN_TMPL is NULL.  */
   15602    148208517 :   if (gen_tmpl && !closure)
   15603              :     {
   15604    147908412 :       DECL_TEMPLATE_INFO (r)
   15605    147908412 :         = build_template_info (gen_tmpl, argvec);
   15606    147908412 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   15607              : 
   15608    147908412 :       if (use_spec_table)
   15609              :         {
   15610     92504358 :           tree new_r
   15611     92504358 :             = register_specialization (r, gen_tmpl, argvec, false, hash);
   15612     92504358 :           if (new_r != r)
   15613              :             /* We instantiated this while substituting into
   15614              :                the type earlier (template/friend54.C).  */
   15615              :             return new_r;
   15616              :         }
   15617              : 
   15618              :       /* We're not supposed to instantiate default arguments
   15619              :          until they are called, for a template.  But, for a
   15620              :          declaration like:
   15621              : 
   15622              :          template <class T> void f ()
   15623              :          { extern void g(int i = T()); }
   15624              : 
   15625              :          we should do the substitution when the template is
   15626              :          instantiated.  We handle the member function case in
   15627              :          instantiate_class_template since the default arguments
   15628              :          might refer to other members of the class.  */
   15629    147908412 :       if (!member
   15630     27660877 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   15631    148857936 :           && !uses_template_parms (argvec))
   15632       949524 :         tsubst_default_arguments (r, complain);
   15633              :     }
   15634       300105 :   else if (DECL_LOCAL_DECL_P (r))
   15635              :     {
   15636          387 :       if (!cp_unevaluated_operand)
   15637          387 :         register_local_specialization (r, t);
   15638              :     }
   15639              :   else
   15640       299718 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15641              : 
   15642              :   /* Copy the list of befriending classes.  */
   15643    148208517 :   for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
   15644    149252595 :        *friends;
   15645      1044078 :        friends = &TREE_CHAIN (*friends))
   15646              :     {
   15647      1044078 :       *friends = copy_node (*friends);
   15648      1044078 :       TREE_VALUE (*friends)
   15649      2088156 :         = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
   15650              :     }
   15651              : 
   15652    296417034 :   if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
   15653              :     {
   15654     28002099 :       maybe_retrofit_in_chrg (r);
   15655     56004198 :       if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
   15656            6 :         return error_mark_node;
   15657              :       /* If this is an instantiation of a member template, clone it.
   15658              :          If it isn't, that'll be handled by
   15659              :          clone_constructors_and_destructors.  */
   15660     56004180 :       if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
   15661     10435342 :         clone_cdtor (r, /*update_methods=*/false);
   15662              :     }
   15663    120206418 :   else if ((complain & tf_error) != 0
   15664     90523578 :            && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   15665    141337532 :            && !grok_op_properties (r, /*complain=*/true))
   15666            3 :     return error_mark_node;
   15667              : 
   15668              :   /* Possibly limit visibility based on template args.  */
   15669    148208508 :   DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   15670    148208508 :   if (DECL_VISIBILITY_SPECIFIED (t))
   15671              :     {
   15672    120060482 :       DECL_VISIBILITY_SPECIFIED (r) = 0;
   15673    120060482 :       DECL_ATTRIBUTES (r)
   15674    240120964 :         = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   15675              :     }
   15676    148208508 :   determine_visibility (r);
   15677    148208508 :   if (DECL_SECTION_NAME (t))
   15678            6 :     set_decl_section_name (r, t);
   15679    152730861 :   if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
   15680        45611 :       && COMPLETE_TYPE_P (DECL_CONTEXT (r))
   15681    148208508 :       && !processing_template_decl)
   15682            0 :     defaulted_late_check (r);
   15683              : 
   15684    148208508 :   if (flag_openmp)
   15685       197620 :     if (tree attr = lookup_attribute ("omp declare variant base",
   15686       197620 :                                       DECL_ATTRIBUTES (r)))
   15687          191 :       omp_declare_variant_finalize (r, attr);
   15688              : 
   15689    148208508 :   return r;
   15690              : }
   15691              : 
   15692              : /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL.  */
   15693              : 
   15694              : static tree
   15695     28771912 : tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
   15696              :                       tree lambda_fntype, tree lambda_tparms)
   15697              : {
   15698              :   /* We can get here when processing a member function template,
   15699              :      member class template, or template template parameter.  */
   15700     28771912 :   tree decl = DECL_TEMPLATE_RESULT (t);
   15701     28771912 :   tree in_decl = t;
   15702     28771912 :   tree spec;
   15703     28771912 :   tree tmpl_args;
   15704     28771912 :   tree full_args = NULL_TREE;
   15705     28771912 :   tree r;
   15706     28771912 :   hashval_t hash = 0;
   15707              : 
   15708     28771912 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   15709              :     {
   15710              :       /* Template template parameter is treated here.  */
   15711        20710 :       tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15712        20710 :       if (new_type == error_mark_node)
   15713              :         r = error_mark_node;
   15714              :       /* If we get a real template back, return it.  This can happen in
   15715              :          the context of most_specialized_partial_spec.  */
   15716        20710 :       else if (TREE_CODE (new_type) == TEMPLATE_DECL)
   15717              :         r = new_type;
   15718              :       else
   15719              :         /* The new TEMPLATE_DECL was built in
   15720              :            reduce_template_parm_level.  */
   15721        41420 :         r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
   15722              :       return r;
   15723              :     }
   15724              : 
   15725     28751202 :   if (!lambda_fntype)
   15726              :     {
   15727              :       /* We might already have an instance of this template.
   15728              :          The ARGS are for the surrounding class type, so the
   15729              :          full args contain the tsubst'd args for the context,
   15730              :          plus the innermost args from the template decl.  */
   15731     28701936 :       tmpl_args = DECL_CLASS_TEMPLATE_P (t)
   15732      1808352 :         ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
   15733     26893584 :         : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
   15734              :       /* Because this is a template, the arguments will still be
   15735              :          dependent, even after substitution.  If
   15736              :          PROCESSING_TEMPLATE_DECL is not set, the dependency
   15737              :          predicates will short-circuit.  */
   15738     28701936 :       ++processing_template_decl;
   15739     28701936 :       full_args = tsubst_template_args (tmpl_args, args,
   15740              :                                         complain, in_decl);
   15741     28701936 :       --processing_template_decl;
   15742     28701936 :       if (full_args == error_mark_node)
   15743              :         return error_mark_node;
   15744              : 
   15745              :       /* If this is a default template template argument,
   15746              :          tsubst might not have changed anything.  */
   15747     28701918 :       if (full_args == tmpl_args)
   15748              :         return t;
   15749              : 
   15750     28701918 :       hash = spec_hasher::hash (t, full_args);
   15751     28701918 :       spec = retrieve_specialization (t, full_args, hash);
   15752     28701918 :       if (spec != NULL_TREE)
   15753              :         {
   15754       808322 :           if (TYPE_P (spec))
   15755              :             /* Type partial instantiations are stored as the type by
   15756              :                lookup_template_class_1, not here as the template.  */
   15757       807437 :             spec = CLASSTYPE_TI_TEMPLATE (spec);
   15758          885 :           else if (TREE_CODE (spec) != TEMPLATE_DECL)
   15759          693 :             spec = DECL_TI_TEMPLATE (spec);
   15760              :           return spec;
   15761              :         }
   15762              :     }
   15763              : 
   15764              :   /* Make a new template decl.  It will be similar to the
   15765              :      original, but will record the current template arguments.
   15766              :      We also create a new function declaration, which is just
   15767              :      like the old one, but points to this new template, rather
   15768              :      than the old one.  */
   15769     27942862 :   r = copy_decl (t);
   15770     27942862 :   gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
   15771     27942862 :   DECL_CHAIN (r) = NULL_TREE;
   15772              : 
   15773              :   // Build new template info linking to the original template decl.
   15774     27942862 :   if (!lambda_fntype)
   15775              :     {
   15776     27893596 :       DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   15777     27893596 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   15778              :     }
   15779              :   else
   15780        49266 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15781              : 
   15782              :   /* The template parameters for this new template are all the
   15783              :      template parameters for the old template, except the
   15784              :      outermost level of parameters.  */
   15785     27942862 :   auto tparm_guard = make_temp_override (current_template_parms);
   15786     55885724 :   DECL_TEMPLATE_PARMS (r)
   15787     55885724 :     = current_template_parms
   15788     27942862 :     = (lambda_tparms
   15789     27942862 :        ? lambda_tparms
   15790     27893596 :        : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
   15791              :                                 complain));
   15792              : 
   15793     27942862 :   bool class_p = false;
   15794     27942862 :   tree inner = decl;
   15795     27942862 :   ++processing_template_decl;
   15796     27942862 :   if (TREE_CODE (inner) == FUNCTION_DECL)
   15797     24336966 :     inner = tsubst_function_decl (inner, args, complain, lambda_fntype,
   15798              :                                   /*use_spec_table=*/false);
   15799              :   else
   15800              :     {
   15801      3605896 :       if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
   15802              :         {
   15803      1000900 :           class_p = true;
   15804      1000900 :           inner = TREE_TYPE (inner);
   15805              :         }
   15806      3605896 :       if (class_p)
   15807      1000900 :         inner = tsubst_entering_scope (inner, args, complain, in_decl);
   15808              :       else
   15809      2604996 :         inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false);
   15810              :     }
   15811     27942862 :   --processing_template_decl;
   15812     27942862 :   if (inner == error_mark_node)
   15813              :     return error_mark_node;
   15814              : 
   15815     27942802 :   if (class_p)
   15816              :     {
   15817              :       /* For a partial specialization, we need to keep pointing to
   15818              :          the primary template.  */
   15819      1000882 :       if (!DECL_TEMPLATE_SPECIALIZATION (t))
   15820              :         {
   15821       634502 :           CLASSTYPE_TI_TEMPLATE (inner) = r;
   15822       634502 :           CLASSTYPE_USE_TEMPLATE (inner) = 0;
   15823              :         }
   15824              : 
   15825      1000882 :       DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
   15826      1000882 :       inner = TYPE_MAIN_DECL (inner);
   15827              :     }
   15828     26941920 :   else if (lambda_fntype)
   15829              :     {
   15830        49266 :       tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
   15831        49266 :       DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
   15832              :     }
   15833              :   else
   15834              :     {
   15835     26892654 :       DECL_TI_TEMPLATE (inner) = r;
   15836              :       /* Set DECL_TI_ARGS to the full set of template arguments,
   15837              :          which tsubst_function_decl / tsubst_decl didn't do due to
   15838              :          use_spec_table=false.  */
   15839     26892654 :       DECL_TI_ARGS (inner) = full_args;
   15840     26892654 :       DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
   15841              :     }
   15842              : 
   15843     27942802 :   DECL_TEMPLATE_RESULT (r) = inner;
   15844     27942802 :   TREE_TYPE (r) = TREE_TYPE (inner);
   15845     27942802 :   DECL_CONTEXT (r) = DECL_CONTEXT (inner);
   15846              : 
   15847     27942802 :   if (modules_p ())
   15848              :     {
   15849              :       /* Propagate module information from the decl.  */
   15850        86127 :       DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
   15851        86127 :       if (DECL_LANG_SPECIFIC (inner))
   15852              :         /* If this is a constrained template, the above tsubst of
   15853              :            inner can find the unconstrained template, which may have
   15854              :            come from an import.  This is ok, because we don't
   15855              :            register this instantiation (see below).  */
   15856        82635 :         gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
   15857              :                              || (TEMPLATE_PARMS_CONSTRAINTS
   15858              :                                  (DECL_TEMPLATE_PARMS (t))));
   15859              :     }
   15860              : 
   15861     27942802 :   DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
   15862     27942802 :   DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
   15863              : 
   15864     27942802 :   if (PRIMARY_TEMPLATE_P (t))
   15865     27573219 :     DECL_PRIMARY_TEMPLATE (r) = r;
   15866              : 
   15867     27942802 :   if (!lambda_fntype && !class_p)
   15868              :     {
   15869              :       /* Record this non-type partial instantiation.  */
   15870              :       /* FIXME we'd like to always register the TEMPLATE_DECL, or always
   15871              :          the DECL_TEMPLATE_RESULT, but it seems the modules code relies
   15872              :          on this current behavior.  */
   15873     26892654 :       if (TREE_CODE (inner) == FUNCTION_DECL)
   15874     24287658 :         register_specialization (r, t, full_args, false, hash);
   15875              :       else
   15876      2604996 :         register_specialization (inner, t, full_args, false, hash);
   15877              :     }
   15878              : 
   15879              :   return r;
   15880     27942862 : }
   15881              : 
   15882              : /* True if FN is the op() for a lambda in an uninstantiated template.  */
   15883              : 
   15884              : bool
   15885   1149468072 : lambda_fn_in_template_p (tree fn)
   15886              : {
   15887   1156316816 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15888              :     return false;
   15889       832828 :   tree closure = DECL_CONTEXT (fn);
   15890       832828 :   return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
   15891              : }
   15892              : 
   15893              : /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
   15894              :    which the above is true.  */
   15895              : 
   15896              : bool
   15897    965448944 : regenerated_lambda_fn_p (tree fn)
   15898              : {
   15899    971322195 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15900              :     return false;
   15901      1336037 :   tree closure = DECL_CONTEXT (fn);
   15902      1336037 :   tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
   15903      1336037 :   return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
   15904              : }
   15905              : 
   15906              : /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
   15907              :    If T is not a regenerated LAMBDA_EXPR, return T.  */
   15908              : 
   15909              : tree
   15910       498850 : most_general_lambda (tree t)
   15911              : {
   15912      1005187 :   while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15913       506337 :     t = TI_TEMPLATE (ti);
   15914       498850 :   return t;
   15915              : }
   15916              : 
   15917              : /* Return the set of template arguments used to regenerate the lambda T
   15918              :    from its most general lambda.  */
   15919              : 
   15920              : tree
   15921       329439 : lambda_regenerating_args (tree t)
   15922              : {
   15923       658878 :   if (LAMBDA_FUNCTION_P (t))
   15924       329439 :     t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
   15925       329439 :   gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
   15926       329439 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15927       329439 :     return TI_ARGS (ti);
   15928              :   else
   15929              :     return NULL_TREE;
   15930              : }
   15931              : 
   15932              : /* We're instantiating a variable from template function TCTX.  Return the
   15933              :    corresponding current enclosing scope.  We can match them up using
   15934              :    DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
   15935              :    the DECL_SOURCE_LOCATION for a function instantiation is updated to match
   15936              :    the template definition in regenerate_decl_from_template.  */
   15937              : 
   15938              : static tree
   15939       106360 : enclosing_instantiation_of (tree tctx)
   15940              : {
   15941       106360 :   tree fn = current_function_decl;
   15942              : 
   15943              :   /* We shouldn't ever need to do this for other artificial functions.  */
   15944       107648 :   gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
   15945              : 
   15946       106363 :   for (; fn; fn = decl_function_context (fn))
   15947       106363 :     if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
   15948       106360 :       return fn;
   15949            0 :   gcc_unreachable ();
   15950              : }
   15951              : 
   15952              : /* Substitute the ARGS into the T, which is a _DECL.  Return the
   15953              :    result of the substitution.  Issue error and warning messages under
   15954              :    control of COMPLAIN.  The flag USE_SPEC_TABLE controls if we look up
   15955              :    and insert into the specializations table or if we can assume it's
   15956              :    the caller's responsibility; this is used by instantiate_template
   15957              :    to avoid doing some redundant work.  */
   15958              : 
   15959              : static tree
   15960    862880553 : tsubst_decl (tree t, tree args, tsubst_flags_t complain,
   15961              :              bool use_spec_table /* = true */)
   15962              : {
   15963              : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   15964    862880553 :   location_t saved_loc;
   15965    862880553 :   tree r = NULL_TREE;
   15966    862880553 :   tree in_decl = t;
   15967    862880553 :   hashval_t hash = 0;
   15968              : 
   15969    862880553 :   if (t == error_mark_node)
   15970              :     return error_mark_node;
   15971              : 
   15972              :   /* Set the filename and linenumber to improve error-reporting.  */
   15973    862880550 :   saved_loc = input_location;
   15974    862880550 :   input_location = DECL_SOURCE_LOCATION (t);
   15975              : 
   15976    862880550 :   switch (TREE_CODE (t))
   15977              :     {
   15978     28722646 :     case TEMPLATE_DECL:
   15979     28722646 :       r = tsubst_template_decl (t, args, complain,
   15980              :                                 /*lambda_fntype=*/NULL_TREE,
   15981              :                                 /*lambda_tparms=*/NULL_TREE);
   15982     28722646 :       break;
   15983              : 
   15984    126519918 :     case FUNCTION_DECL:
   15985    126519918 :       r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE,
   15986              :                                 use_spec_table);
   15987    126511782 :       break;
   15988              : 
   15989    335858716 :     case PARM_DECL:
   15990    335858716 :       {
   15991    335858716 :         tree type = NULL_TREE;
   15992    335858716 :         int i, len = 1;
   15993    335858716 :         tree expanded_types = NULL_TREE;
   15994    335858716 :         tree prev_r = NULL_TREE;
   15995    335858716 :         tree first_r = NULL_TREE;
   15996              : 
   15997    335858716 :         if (DECL_PACK_P (t))
   15998              :           {
   15999              :             /* If there is a local specialization that isn't a
   16000              :                parameter pack, it means that we're doing a "simple"
   16001              :                substitution from inside tsubst_pack_expansion. Just
   16002              :                return the local specialization (which will be a single
   16003              :                parm).  */
   16004      4056421 :             tree spec = retrieve_local_specialization (t);
   16005      4056421 :             if (spec
   16006           42 :                 && TREE_CODE (spec) == PARM_DECL
   16007      4056430 :                 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
   16008            0 :               RETURN (spec);
   16009              : 
   16010              :             /* Expand the TYPE_PACK_EXPANSION that provides the types for
   16011              :                the parameters in this function parameter pack.  */
   16012      4056421 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   16013              :                                                     complain, in_decl);
   16014      4056421 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   16015              :               {
   16016      1840555 :                 len = TREE_VEC_LENGTH (expanded_types);
   16017              : 
   16018              :                 /* Zero-length parameter packs are boring. Just substitute
   16019              :                    into the chain.  */
   16020      1840555 :                 if (len == 0 && !cp_unevaluated_operand)
   16021       653620 :                   RETURN (tsubst (TREE_CHAIN (t), args, complain,
   16022              :                                   TREE_CHAIN (t)));
   16023              :               }
   16024              :             else
   16025              :               {
   16026              :                 /* All we did was update the type. Make a note of that.  */
   16027              :                 type = expanded_types;
   16028              :                 expanded_types = NULL_TREE;
   16029              :               }
   16030              :           }
   16031              : 
   16032              :         /* Loop through all of the parameters we'll build. When T is
   16033              :            a function parameter pack, LEN is the number of expanded
   16034              :            types in EXPANDED_TYPES; otherwise, LEN is 1.  */
   16035    335205096 :         r = NULL_TREE;
   16036    670853232 :         for (i = 0; i < len; ++i)
   16037              :           {
   16038    335648142 :             prev_r = r;
   16039    335648142 :             r = copy_node (t);
   16040    335648142 :             if (DECL_TEMPLATE_PARM_P (t))
   16041      1980064 :               SET_DECL_TEMPLATE_PARM_P (r);
   16042              : 
   16043    335648142 :             if (expanded_types)
   16044              :               /* We're on the Ith parameter of the function parameter
   16045              :                  pack.  */
   16046              :               {
   16047              :                 /* Get the Ith type.  */
   16048      1629981 :                 type = TREE_VEC_ELT (expanded_types, i);
   16049              : 
   16050              :                 /* Rename the parameter to include the index.  */
   16051      1629981 :                 DECL_NAME (r)
   16052      3259962 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   16053              :               }
   16054    334018161 :             else if (!type)
   16055              :               /* We're dealing with a normal parameter.  */
   16056    331802295 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16057              : 
   16058    335648142 :             if (type == error_mark_node && !(complain & tf_error))
   16059            6 :               RETURN (error_mark_node);
   16060              : 
   16061    335648136 :             type = type_decays_to (type);
   16062    335648136 :             TREE_TYPE (r) = type;
   16063    335648136 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   16064              : 
   16065    335648136 :             if (DECL_INITIAL (r))
   16066              :               {
   16067     48108114 :                 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
   16068     46128050 :                   DECL_INITIAL (r) = TREE_TYPE (r);
   16069              :                 else
   16070      1980064 :                   DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
   16071              :                                              complain, in_decl);
   16072              :               }
   16073              : 
   16074    335648136 :             DECL_CONTEXT (r) = NULL_TREE;
   16075              : 
   16076    335648136 :             if (!DECL_TEMPLATE_PARM_P (r))
   16077    333668072 :               DECL_ARG_TYPE (r) = type_passed_as (type);
   16078              : 
   16079    335648136 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   16080              :                                                  args, complain, in_decl))
   16081            0 :               return error_mark_node;
   16082              : 
   16083              :             /* Keep track of the first new parameter we
   16084              :                generate. That's what will be returned to the
   16085              :                caller.  */
   16086    335648136 :             if (!first_r)
   16087    335204958 :               first_r = r;
   16088              : 
   16089              :             /* Build a proper chain of parameters when substituting
   16090              :                into a function parameter pack.  */
   16091    335648136 :             if (prev_r)
   16092       443178 :               DECL_CHAIN (prev_r) = r;
   16093              :           }
   16094              : 
   16095              :         /* If cp_unevaluated_operand is set, we're just looking for a
   16096              :            single dummy parameter, so don't keep going.  */
   16097    335205090 :         if (DECL_CHAIN (t) && !cp_unevaluated_operand)
   16098              :           {
   16099    173482667 :             tree chain = tsubst (DECL_CHAIN (t), args,
   16100    173482667 :                                  complain, DECL_CHAIN (t));
   16101    173482667 :             if (chain == error_mark_node)
   16102            3 :               RETURN (error_mark_node);
   16103    173482664 :             DECL_CHAIN (r) = chain;
   16104              :           }
   16105              : 
   16106              :         /* FIRST_R contains the start of the chain we've built.  */
   16107    335205087 :         r = first_r;
   16108              :       }
   16109    335205087 :       break;
   16110              : 
   16111      8687636 :     case FIELD_DECL:
   16112      8687636 :       {
   16113      8687636 :         tree type = NULL_TREE;
   16114      8687636 :         tree vec = NULL_TREE;
   16115      8687636 :         tree expanded_types = NULL_TREE;
   16116      8687636 :         int len = 1;
   16117              : 
   16118      8687636 :         if (PACK_EXPANSION_P (TREE_TYPE (t)))
   16119              :           {
   16120              :             /* This field is a lambda capture pack.  Return a TREE_VEC of
   16121              :                the expanded fields to instantiate_class_template_1.  */
   16122          386 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   16123              :                                                     complain, in_decl);
   16124          386 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   16125              :               {
   16126          377 :                 len = TREE_VEC_LENGTH (expanded_types);
   16127          377 :                 vec = make_tree_vec (len);
   16128              :               }
   16129              :             else
   16130              :               {
   16131              :                 /* All we did was update the type. Make a note of that.  */
   16132              :                 type = expanded_types;
   16133              :                 expanded_types = NULL_TREE;
   16134              :               }
   16135              :           }
   16136              : 
   16137     17375427 :         for (int i = 0; i < len; ++i)
   16138              :           {
   16139      8687969 :             r = copy_decl (t);
   16140      8687969 :             if (expanded_types)
   16141              :               {
   16142          710 :                 type = TREE_VEC_ELT (expanded_types, i);
   16143          710 :                 DECL_NAME (r)
   16144         1420 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   16145              :               }
   16146      8687259 :             else if (!type)
   16147      8687250 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16148              : 
   16149      8687963 :             if (type == error_mark_node)
   16150          163 :               RETURN (error_mark_node);
   16151      8687800 :             TREE_TYPE (r) = type;
   16152      8687800 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   16153              : 
   16154      8687800 :             if (DECL_C_BIT_FIELD (r))
   16155              :               {
   16156              :                 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
   16157              :                    number of bits.  */
   16158       255502 :                 tree width
   16159       255502 :                   = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
   16160              :                                  complain, in_decl);
   16161       255502 :                 if (width
   16162       255502 :                     && width != error_mark_node
   16163       255502 :                     && !type_dependent_expression_p (width)
   16164       511004 :                     && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
   16165              :                          (TREE_TYPE (width)))
   16166              :                   {
   16167            9 :                     if (complain & tf_error)
   16168            9 :                       error_at (DECL_SOURCE_LOCATION (t),
   16169              :                                 "width of bit-field %qD has non-integral "
   16170            9 :                                 "type %qT", r, TREE_TYPE (width));
   16171            9 :                     RETURN (error_mark_node);
   16172              :                   }
   16173       255493 :                 DECL_BIT_FIELD_REPRESENTATIVE (r) = width;
   16174              :               }
   16175      8687791 :             if (DECL_INITIAL (t))
   16176              :               {
   16177              :                 /* Set up DECL_TEMPLATE_INFO so that we can get at the
   16178              :                    NSDMI in perform_member_init.  Still set DECL_INITIAL
   16179              :                    so that we know there is one.  */
   16180       843172 :                 DECL_INITIAL (r) = void_node;
   16181       843172 :                 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
   16182       843172 :                 retrofit_lang_decl (r);
   16183       843172 :                 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   16184              :               }
   16185              :             /* We don't have to set DECL_CONTEXT here; it is set by
   16186              :                finish_member_declaration.  */
   16187      8687791 :             DECL_CHAIN (r) = NULL_TREE;
   16188              : 
   16189      8687791 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   16190              :                                                  args, complain, in_decl))
   16191            0 :               return error_mark_node;
   16192              : 
   16193      8687791 :             if (vec)
   16194          710 :               TREE_VEC_ELT (vec, i) = r;
   16195              :           }
   16196              : 
   16197      8687458 :         if (vec)
   16198          377 :           r = vec;
   16199              :       }
   16200              :       break;
   16201              : 
   16202      1838854 :     case USING_DECL:
   16203              :       /* We reach here only for member using decls.  We also need to check
   16204              :          uses_template_parms because DECL_DEPENDENT_P is not set for a
   16205              :          using-declaration that designates a member of the current
   16206              :          instantiation (c++/53549).  */
   16207      1838854 :       if (DECL_DEPENDENT_P (t)
   16208      1838854 :           || uses_template_parms (USING_DECL_SCOPE (t)))
   16209              :         {
   16210              :           /* True iff this using-decl was written as a pack expansion
   16211              :              (and a pack appeared in its scope or name).  If a pack
   16212              :              appeared in both, we expand the packs separately and
   16213              :              manually merge them.  */
   16214      1838602 :           bool variadic_p = false;
   16215              : 
   16216      1838602 :           tree scope = USING_DECL_SCOPE (t);
   16217      1838602 :           if (PACK_EXPANSION_P (scope))
   16218              :             {
   16219          287 :               scope = tsubst_pack_expansion (scope, args,
   16220              :                                              complain | tf_qualifying_scope,
   16221              :                                              in_decl);
   16222          287 :               variadic_p = true;
   16223              :             }
   16224              :           else
   16225      1838315 :             scope = tsubst_scope (scope, args, complain, in_decl);
   16226              : 
   16227      1838602 :           tree name = DECL_NAME (t);
   16228      1838602 :           if (IDENTIFIER_CONV_OP_P (name)
   16229      1838602 :               && PACK_EXPANSION_P (TREE_TYPE (name)))
   16230              :             {
   16231           42 :               name = tsubst_pack_expansion (TREE_TYPE (name), args,
   16232              :                                             complain, in_decl);
   16233           42 :               if (name == error_mark_node)
   16234              :                 {
   16235            0 :                   r = error_mark_node;
   16236            0 :                   break;
   16237              :                 }
   16238           42 :               name = copy_node (name);
   16239          105 :               for (tree& elt : tree_vec_range (name))
   16240           63 :                 elt = make_conv_op_name (elt);
   16241           42 :               variadic_p = true;
   16242              :             }
   16243              :           else
   16244      3677120 :             name = tsubst_name (name, args, complain, in_decl);
   16245              : 
   16246      1838602 :           int len;
   16247      1838602 :           if (!variadic_p)
   16248              :             len = 1;
   16249          296 :           else if (TREE_CODE (scope) == TREE_VEC
   16250          287 :                    && TREE_CODE (name) == TREE_VEC)
   16251              :             {
   16252           33 :               if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
   16253              :                 {
   16254            3 :                   error ("mismatched argument pack lengths (%d vs %d)",
   16255            3 :                          TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
   16256            3 :                   r = error_mark_node;
   16257            3 :                   break;
   16258              :                 }
   16259              :               len = TREE_VEC_LENGTH (scope);
   16260              :             }
   16261          263 :           else if (TREE_CODE (scope) == TREE_VEC)
   16262          254 :             len = TREE_VEC_LENGTH (scope);
   16263              :           else /* TREE_CODE (name) == TREE_VEC  */
   16264            9 :             len = TREE_VEC_LENGTH (name);
   16265              : 
   16266      1838599 :           r = make_tree_vec (len);
   16267      3677596 :           for (int i = 0; i < len; ++i)
   16268              :             {
   16269      1839027 :               tree escope = (TREE_CODE (scope) == TREE_VEC
   16270      1839027 :                              ? TREE_VEC_ELT (scope, i)
   16271          709 :                              : scope);
   16272      1839027 :               tree ename = (TREE_CODE (name) == TREE_VEC
   16273      1839027 :                             ? TREE_VEC_ELT (name, i)
   16274           57 :                             : name);
   16275      1839027 :               tree elt = do_class_using_decl (escope, ename);
   16276      1839027 :               if (!elt)
   16277              :                 {
   16278           30 :                   r = error_mark_node;
   16279           30 :                   break;
   16280              :                 }
   16281      1838997 :               TREE_PROTECTED (elt) = TREE_PROTECTED (t);
   16282      1838997 :               TREE_PRIVATE (elt) = TREE_PRIVATE (t);
   16283      1838997 :               TREE_VEC_ELT (r, i) = elt;
   16284              :             }
   16285              : 
   16286      1838599 :           if (!variadic_p && r != error_mark_node)
   16287      1838276 :             r = TREE_VEC_ELT (r, 0);
   16288              :         }
   16289              :       else
   16290              :         {
   16291          252 :           r = copy_node (t);
   16292          252 :           DECL_CHAIN (r) = NULL_TREE;
   16293              :         }
   16294              :       break;
   16295              : 
   16296    252078848 :     case TYPE_DECL:
   16297    252078848 :     case VAR_DECL:
   16298    252078848 :       {
   16299    252078848 :         tree argvec = NULL_TREE;
   16300    252078848 :         tree gen_tmpl = NULL_TREE;
   16301    252078848 :         tree tmpl = NULL_TREE;
   16302    252078848 :         tree type = NULL_TREE;
   16303              : 
   16304    252078848 :         if (TREE_TYPE (t) == error_mark_node)
   16305           11 :           RETURN (error_mark_node);
   16306              : 
   16307    252078837 :         if (TREE_CODE (t) == TYPE_DECL
   16308    252078837 :             && (TREE_CODE (TREE_TYPE (t)) == TU_LOCAL_ENTITY
   16309    204878938 :                 || t == TYPE_MAIN_DECL (TREE_TYPE (t))))
   16310              :           {
   16311              :             /* If this is the canonical decl, we don't have to
   16312              :                mess with instantiations, and often we can't (for
   16313              :                typename, template type parms and such).  Note that
   16314              :                TYPE_NAME is not correct for the above test if
   16315              :                we've copied the type for a typedef.  */
   16316     42991398 :             type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16317     42991353 :             if (type == error_mark_node)
   16318            9 :               RETURN (error_mark_node);
   16319     42991344 :             r = TYPE_NAME (type);
   16320     42991344 :             break;
   16321              :           }
   16322              : 
   16323              :         /* Check to see if we already have the specialization we
   16324              :            need.  */
   16325    209087439 :         tree spec = NULL_TREE;
   16326    209087439 :         bool local_p = false;
   16327    209087439 :         tree ctx = DECL_CONTEXT (t);
   16328     47199896 :         if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
   16329    256287247 :             && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
   16330              :           {
   16331    185604124 :             local_p = false;
   16332    185604124 :             if (DECL_CLASS_SCOPE_P (t))
   16333              :               {
   16334    122367516 :                 ctx = tsubst_entering_scope (ctx, args, complain, in_decl);
   16335    122367516 :                 if (DECL_SELF_REFERENCE_P (t))
   16336              :                   /* The context and type of an injected-class-name are
   16337              :                      the same, so we don't need to substitute both.  */
   16338              :                   type = ctx;
   16339              :                 /* If CTX is unchanged, then T is in fact the
   16340              :                    specialization we want.  That situation occurs when
   16341              :                    referencing a static data member within in its own
   16342              :                    class.  We can use pointer equality, rather than
   16343              :                    same_type_p, because DECL_CONTEXT is always
   16344              :                    canonical...  */
   16345    122367516 :                 if (ctx == DECL_CONTEXT (t)
   16346              :                     /* ... unless T is a member template; in which
   16347              :                        case our caller can be willing to create a
   16348              :                        specialization of that template represented
   16349              :                        by T.  */
   16350    122367516 :                     && !(DECL_TI_TEMPLATE (t)
   16351     24088289 :                          && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
   16352              :                   spec = t;
   16353              :               }
   16354              : 
   16355              :             if (!spec)
   16356              :               {
   16357    180785954 :                 tmpl = DECL_TI_TEMPLATE (t);
   16358    180785954 :                 if (use_spec_table)
   16359              :                   {
   16360     93601297 :                     argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
   16361     93601297 :                     if (argvec == error_mark_node)
   16362            0 :                       RETURN (error_mark_node);
   16363     93601297 :                     gen_tmpl = most_general_template (tmpl);
   16364     93601297 :                     hash = spec_hasher::hash (gen_tmpl, argvec);
   16365     93601297 :                     spec = retrieve_specialization (gen_tmpl, argvec, hash);
   16366              :                   }
   16367              :                 else
   16368              :                   argvec = args;
   16369              :               }
   16370              :           }
   16371              :         else
   16372              :           {
   16373     23483315 :             if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
   16374              :               /* Subsequent calls to pushdecl will fill this in.  */
   16375              :               ctx = NULL_TREE;
   16376              :             /* A local variable.  */
   16377     23483315 :             local_p = true;
   16378              :             /* Unless this is a reference to a static variable from an
   16379              :                enclosing function, in which case we need to fill it in now.  */
   16380     23483315 :             if (TREE_STATIC (t))
   16381              :               {
   16382       105069 :                 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
   16383       105069 :                 if (fn != current_function_decl)
   16384     23483315 :                   ctx = fn;
   16385              :               }
   16386     23483315 :             spec = retrieve_local_specialization (t);
   16387              :           }
   16388              :         /* If we already have the specialization we need, there is
   16389              :            nothing more to do.  */
   16390    204269269 :         if (spec)
   16391              :           {
   16392      8257159 :             r = spec;
   16393      8257159 :             break;
   16394              :           }
   16395              : 
   16396              :         /* Create a new node for the specialization we need.  */
   16397    200830280 :         if (type == NULL_TREE)
   16398              :           {
   16399    158837392 :             if (is_typedef_decl (t))
   16400              :               type = DECL_ORIGINAL_TYPE (t);
   16401              :             else
   16402     38942887 :               type = TREE_TYPE (t);
   16403    158837392 :             if (VAR_P (t)
   16404     38942740 :                 && VAR_HAD_UNKNOWN_BOUND (t)
   16405    158837783 :                 && type != error_mark_node)
   16406          391 :               type = strip_array_domain (type);
   16407    158837392 :             tsubst_flags_t tcomplain = complain;
   16408    158837392 :             if (VAR_P (t))
   16409     38942740 :               tcomplain |= tf_tst_ok;
   16410    158837392 :             if (DECL_DECOMPOSITION_P (t) && DECL_PACK_P (t))
   16411              :               type = NULL_TREE;
   16412    158826859 :             else if (is_capture_proxy (t) && WILDCARD_TYPE_P (type))
   16413              :               /* We'll set type from DECL_VALUE_EXPR.  */
   16414              :               type = NULL_TREE;
   16415              :             else
   16416    158826512 :               type = tsubst (type, args, tcomplain, in_decl);
   16417              :             /* Substituting the type might have recursively instantiated this
   16418              :                same alias (c++/86171).  */
   16419     48169743 :             if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   16420    203042013 :                 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
   16421              :               {
   16422            0 :                 r = spec;
   16423            0 :                 break;
   16424              :               }
   16425              :           }
   16426    200830280 :         if (type == error_mark_node && !(complain & tf_error))
   16427     36470989 :           RETURN (error_mark_node);
   16428    164359291 :         r = copy_decl (t);
   16429    164359291 :         if (VAR_P (r))
   16430              :           {
   16431     38942740 :             DECL_INITIALIZED_P (r) = 0;
   16432     38942740 :             DECL_TEMPLATE_INSTANTIATED (r) = 0;
   16433     38942740 :             if (DECL_DECOMPOSITION_P (t) && DECL_PACK_P (t))
   16434              :               {
   16435        10533 :                 tree dtype = cxx_make_type (DECLTYPE_TYPE);
   16436        10533 :                 DECLTYPE_TYPE_EXPR (dtype) = r;
   16437        10533 :                 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (dtype) = 1;
   16438        10533 :                 SET_TYPE_STRUCTURAL_EQUALITY (dtype);
   16439        10533 :                 type = cxx_make_type (TYPE_PACK_EXPANSION);
   16440        10533 :                 PACK_EXPANSION_PATTERN (type) = dtype;
   16441        10533 :                 SET_TYPE_STRUCTURAL_EQUALITY (type);
   16442        21066 :                 PACK_EXPANSION_PARAMETER_PACKS (type) = r;
   16443              :               }
   16444     38942740 :             if (DECL_HAS_VALUE_EXPR_P (t))
   16445              :               {
   16446       163018 :                 tree ve = DECL_VALUE_EXPR (t);
   16447              :                 /* If the DECL_VALUE_EXPR is converted to the declared type,
   16448              :                    preserve the identity so that gimplify_type_sizes works.  */
   16449       163018 :                 bool nop = (type && TREE_CODE (ve) == NOP_EXPR);
   16450            3 :                 if (nop)
   16451            3 :                   ve = TREE_OPERAND (ve, 0);
   16452       163018 :                 ve = tsubst_expr (ve, args, complain, in_decl);
   16453       163018 :                 gcc_assert (ve != error_mark_node);
   16454       163018 :                 if (REFERENCE_REF_P (ve))
   16455              :                   {
   16456       106984 :                     gcc_assert (!type || TYPE_REF_P (type));
   16457       106984 :                     ve = TREE_OPERAND (ve, 0);
   16458              :                   }
   16459       163018 :                 if (nop)
   16460            3 :                   ve = build_nop (type, ve);
   16461       163015 :                 else if (!type && is_capture_proxy (t))
   16462          347 :                   type = lambda_proxy_type (ve);
   16463       162668 :                 else if (DECL_LANG_SPECIFIC (t)
   16464       161889 :                          && DECL_OMP_PRIVATIZED_MEMBER (t)
   16465          183 :                          && TREE_CODE (ve) == COMPONENT_REF
   16466          183 :                          && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
   16467       162851 :                          && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
   16468            6 :                   type = TREE_TYPE (ve);
   16469              :                 else
   16470       162662 :                   gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
   16471              :                                        == TYPE_MAIN_VARIANT (type));
   16472       163018 :                 SET_DECL_VALUE_EXPR (r, ve);
   16473              :               }
   16474     38942740 :             if (TREE_CODE (type) == FUNCTION_TYPE)
   16475              :               {
   16476              :                 /* It may seem that this case cannot occur, since:
   16477              : 
   16478              :                    typedef void f();
   16479              :                    void g() { f x; }
   16480              : 
   16481              :                    declares a function, not a variable.  However:
   16482              : 
   16483              :                    typedef void f();
   16484              :                    template <typename T> void g() { T t; }
   16485              :                    template void g<f>();
   16486              : 
   16487              :                    is an attempt to declare a variable with function
   16488              :                    type.  */
   16489            3 :                 error ("variable %qD has function type",
   16490              :                        /* R is not yet sufficiently initialized, so we
   16491              :                           just use its name.  */
   16492            3 :                        DECL_NAME (r));
   16493            3 :                 RETURN (error_mark_node);
   16494              :               }
   16495     38942737 :             type = complete_type (type);
   16496              :             /* Wait until cp_finish_decl to set this again, to handle
   16497              :                circular dependency (template/instantiate6.C). */
   16498     38942734 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
   16499     38942734 :             type = check_var_type (DECL_NAME (r), type,
   16500     38942734 :                                    DECL_SOURCE_LOCATION (r));
   16501              :           }
   16502    125416551 :         else if (DECL_SELF_REFERENCE_P (t))
   16503     41992888 :           SET_DECL_SELF_REFERENCE_P (r);
   16504    164359285 :         TREE_TYPE (r) = type;
   16505    164359285 :         cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   16506    164359285 :         DECL_CONTEXT (r) = ctx;
   16507              :         /* Clear out the mangled name and RTL for the instantiation.  */
   16508    164359285 :         SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   16509    164359285 :         if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
   16510    164359285 :           SET_DECL_RTL (r, NULL);
   16511    164359285 :         set_instantiating_module (r);
   16512              : 
   16513              :         /* The initializer must not be expanded until it is required;
   16514              :            see [temp.inst].  */
   16515    164359285 :         DECL_INITIAL (r) = NULL_TREE;
   16516    164359285 :         DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
   16517    164359285 :         if (VAR_P (r))
   16518              :           {
   16519     38942734 :             if (DECL_LANG_SPECIFIC (r))
   16520     18589827 :               SET_DECL_DEPENDENT_INIT_P (r, false);
   16521              : 
   16522     38942734 :             SET_DECL_MODE (r, VOIDmode);
   16523              : 
   16524              :             /* Possibly limit visibility based on template args.  */
   16525     38942734 :             DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   16526     38942734 :             if (DECL_VISIBILITY_SPECIFIED (t))
   16527              :               {
   16528            0 :                 DECL_VISIBILITY_SPECIFIED (r) = 0;
   16529            0 :                 DECL_ATTRIBUTES (r)
   16530            0 :                   = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   16531              :               }
   16532     38942734 :             determine_visibility (r);
   16533     21374802 :             if ((!local_p || TREE_STATIC (t))
   16534     17673001 :                 && !(flag_openmp && DECL_LANG_SPECIFIC (t)
   16535        30159 :                      && DECL_OMP_DECLARE_MAPPER_P (t))
   16536     56615723 :                 && DECL_SECTION_NAME (t))
   16537            9 :               set_decl_section_name (r, t);
   16538              :           }
   16539              : 
   16540    164359285 :         if (!local_p)
   16541              :           {
   16542              :             /* A static data member declaration is always marked
   16543              :                external when it is declared in-class, even if an
   16544              :                initializer is present.  We mimic the non-template
   16545              :                processing here.  */
   16546    140876299 :             DECL_EXTERNAL (r) = 1;
   16547    140876299 :             if (DECL_NAMESPACE_SCOPE_P (t))
   16548     43774254 :               DECL_NOT_REALLY_EXTERN (r) = 1;
   16549              : 
   16550    140876299 :             DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
   16551    140876299 :             SET_DECL_IMPLICIT_INSTANTIATION (r);
   16552    140876299 :             if (use_spec_table)
   16553     90162631 :               register_specialization (r, gen_tmpl, argvec, false, hash);
   16554              :           }
   16555              :         else
   16556              :           {
   16557     23482986 :             if (DECL_LANG_SPECIFIC (r))
   16558      1024743 :               DECL_TEMPLATE_INFO (r) = NULL_TREE;
   16559     23482986 :             if (!cp_unevaluated_operand)
   16560     23482965 :               register_local_specialization (r, t);
   16561              :           }
   16562              : 
   16563    164359285 :         if (VAR_P (r)
   16564     38942734 :             && CP_DECL_THREAD_LOCAL_P (r)
   16565    164359415 :             && !processing_template_decl)
   16566          118 :           set_decl_tls_model (r, decl_default_tls_model (r));
   16567              : 
   16568    164359285 :         DECL_CHAIN (r) = NULL_TREE;
   16569              : 
   16570    164359285 :         if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
   16571              :                                              /*flags=*/0,
   16572              :                                              args, complain, in_decl))
   16573            3 :           return error_mark_node;
   16574              : 
   16575              :         /* Preserve a typedef that names a type.  */
   16576    289775692 :         if (is_typedef_decl (r) && type != error_mark_node)
   16577              :           {
   16578              :             /* Now that we've substituted the type, it may not be opaque
   16579              :                anymore.  */
   16580    125416028 :             TYPE_DECL_OPAQUE_ALIAS_P (r) = any_lambdas_p (type);
   16581    125416028 :             DECL_ORIGINAL_TYPE (r) = NULL_TREE;
   16582    125416028 :             set_underlying_type (r);
   16583              : 
   16584              :             /* common_handle_aligned_attribute doesn't apply the alignment
   16585              :                to DECL_ORIGINAL_TYPE.  */
   16586    125416028 :             if (TYPE_USER_ALIGN (TREE_TYPE (t)))
   16587          337 :               TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
   16588          337 :                                                   TYPE_ALIGN (TREE_TYPE (t)));
   16589              : 
   16590              :             /* Preserve structural-ness of a partially instantiated typedef.  */
   16591    125416028 :             if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t))
   16592    125416028 :                 && dependent_type_p (TREE_TYPE (r)))
   16593     16665287 :               SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (r));
   16594              :           }
   16595              : 
   16596    164359282 :         if (flag_openmp
   16597       300345 :             && VAR_P (t)
   16598        74583 :             && DECL_LANG_SPECIFIC (t)
   16599        32965 :             && DECL_OMP_DECLARE_MAPPER_P (t)
   16600    164359304 :             && strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   16601           19 :           DECL_NAME (r) = omp_mapper_id (DECL_NAME (t), TREE_TYPE (r));
   16602              : 
   16603    164359282 :         layout_decl (r, 0);
   16604              :       }
   16605    164359282 :       break;
   16606              : 
   16607    109173932 :     case NAMESPACE_DECL:
   16608    109173932 :       if (dependent_namespace_p (t))
   16609            2 :         r = tsubst_expr (ORIGINAL_NAMESPACE (t), args, complain, in_decl);
   16610              :       else
   16611    109173930 :         r = t;
   16612              :       break;
   16613              : 
   16614            0 :     default:
   16615            0 :       gcc_unreachable ();
   16616              :     }
   16617              : #undef RETURN
   16618              : 
   16619    862872357 :  out:
   16620              :   /* Restore the file and line information.  */
   16621    862872357 :   input_location = saved_loc;
   16622              : 
   16623    862872357 :   return r;
   16624              : }
   16625              : 
   16626              : /* Substitute into the complete parameter type list PARMS.  */
   16627              : 
   16628              : tree
   16629      5554030 : tsubst_function_parms (tree parms,
   16630              :                        tree args,
   16631              :                        tsubst_flags_t complain,
   16632              :                        tree in_decl)
   16633              : {
   16634      5554030 :   return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
   16635              : }
   16636              : 
   16637              : /* Substitute into the ARG_TYPES of a function type.
   16638              :    If END is a TREE_CHAIN, leave it and any following types
   16639              :    un-substituted.  */
   16640              : 
   16641              : static tree
   16642    505084753 : tsubst_arg_types (tree arg_types,
   16643              :                   tree args,
   16644              :                   tree end,
   16645              :                   tsubst_flags_t complain,
   16646              :                   tree in_decl)
   16647              : {
   16648    505084753 :   tree type = NULL_TREE;
   16649    505084753 :   int len = 1;
   16650    505084753 :   tree expanded_args = NULL_TREE;
   16651              : 
   16652    505084753 :   if (!arg_types || arg_types == void_list_node || arg_types == end)
   16653              :     return arg_types;
   16654              : 
   16655    324064277 :   if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
   16656              :     {
   16657              :       /* For a pack expansion, perform substitution on the
   16658              :          entire expression. Later on, we'll handle the arguments
   16659              :          one-by-one.  */
   16660      6598106 :       expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
   16661              :                                             args, complain, in_decl);
   16662              : 
   16663      6598106 :       if (TREE_CODE (expanded_args) == TREE_VEC)
   16664              :         /* So that we'll spin through the parameters, one by one.  */
   16665      3852613 :         len = TREE_VEC_LENGTH (expanded_args);
   16666              :       else
   16667              :         {
   16668              :           /* We only partially substituted into the parameter
   16669              :              pack. Our type is TYPE_PACK_EXPANSION.  */
   16670              :           type = expanded_args;
   16671              :           expanded_args = NULL_TREE;
   16672              :         }
   16673              :     }
   16674              :   else
   16675    317466171 :     type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
   16676              : 
   16677              :   /* Check if a substituted type is erroneous before substituting into
   16678              :      the rest of the chain.  */
   16679    646624617 :   for (int i = 0; i < len; i++)
   16680              :     {
   16681    322566818 :       if (expanded_args)
   16682      2355154 :         type = TREE_VEC_ELT (expanded_args, i);
   16683              : 
   16684    322566818 :       if (type == error_mark_node)
   16685              :         return error_mark_node;
   16686    322560372 :       if (VOID_TYPE_P (type))
   16687              :         {
   16688           32 :           if (complain & tf_error)
   16689              :             {
   16690            6 :               error ("invalid parameter type %qT", type);
   16691            6 :               if (in_decl)
   16692            3 :                 error ("in declaration %q+D", in_decl);
   16693              :             }
   16694           32 :           return error_mark_node;
   16695              :         }
   16696              :     }
   16697              : 
   16698              :   /* We do not substitute into default arguments here.  The standard
   16699              :      mandates that they be instantiated only when needed, which is
   16700              :      done in build_over_call.  */
   16701    324057799 :   tree default_arg = TREE_PURPOSE (arg_types);
   16702              : 
   16703              :   /* Except that we do substitute default arguments under tsubst_lambda_expr,
   16704              :      since the new op() won't have any associated template arguments for us
   16705              :      to refer to later.  */
   16706    324057799 :   if (lambda_fn_in_template_p (in_decl)
   16707    324057799 :       || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
   16708    296885789 :           && DECL_LOCAL_DECL_P (in_decl)))
   16709       196052 :     default_arg = tsubst_expr (default_arg, args, complain, in_decl);
   16710              : 
   16711    324057799 :   tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
   16712    324057799 :                                                args, end, complain, in_decl);
   16713    324057799 :   if (remaining_arg_types == error_mark_node)
   16714              :     return error_mark_node;
   16715              : 
   16716    646601234 :   for (int i = len-1; i >= 0; i--)
   16717              :     {
   16718    322551889 :       if (expanded_args)
   16719      2355151 :         type = TREE_VEC_ELT (expanded_args, i);
   16720              : 
   16721              :       /* Do array-to-pointer, function-to-pointer conversion, and ignore
   16722              :          top-level qualifiers as required.  */
   16723    322551889 :       type = cv_unqualified (type_decays_to (type));
   16724              : 
   16725    322551889 :       if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
   16726              :         {
   16727              :           /* We've instantiated a template before its default arguments
   16728              :              have been parsed.  This can happen for a nested template
   16729              :              class, and is not an error unless we require the default
   16730              :              argument in a call of this function.  */
   16731            3 :           remaining_arg_types
   16732            3 :             = tree_cons (default_arg, type, remaining_arg_types);
   16733            3 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
   16734              :                          remaining_arg_types);
   16735              :         }
   16736              :       else
   16737    322551886 :         remaining_arg_types
   16738    322551886 :           = hash_tree_cons (default_arg, type, remaining_arg_types);
   16739              :     }
   16740              : 
   16741    324049345 :   return remaining_arg_types;
   16742              : }
   16743              : 
   16744              : /* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
   16745              :    *not* handle the exception-specification for FNTYPE, because the
   16746              :    initial substitution of explicitly provided template parameters
   16747              :    during argument deduction forbids substitution into the
   16748              :    exception-specification:
   16749              : 
   16750              :      [temp.deduct]
   16751              : 
   16752              :      All references in the function type of the function template to  the
   16753              :      corresponding template parameters are replaced by the specified tem-
   16754              :      plate argument values.  If a substitution in a template parameter or
   16755              :      in  the function type of the function template results in an invalid
   16756              :      type, type deduction fails.  [Note: The equivalent  substitution  in
   16757              :      exception specifications is done only when the function is instanti-
   16758              :      ated, at which point a program is  ill-formed  if  the  substitution
   16759              :      results in an invalid type.]  */
   16760              : 
   16761              : static tree
   16762    174375544 : tsubst_function_type (tree t,
   16763              :                       tree args,
   16764              :                       tsubst_flags_t complain,
   16765              :                       tree in_decl)
   16766              : {
   16767    174375544 :   tree return_type;
   16768    174375544 :   tree arg_types = NULL_TREE;
   16769              : 
   16770              :   /* The TYPE_CONTEXT is not used for function/method types.  */
   16771    174375544 :   gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
   16772              : 
   16773              :   /* DR 1227: Mixing immediate and non-immediate contexts in deduction
   16774              :      failure.  */
   16775    174375544 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   16776              : 
   16777    174375544 :   if (late_return_type_p)
   16778              :     {
   16779              :       /* Substitute the argument types.  */
   16780     15613608 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   16781              :                                     complain, in_decl);
   16782     15613608 :       if (arg_types == error_mark_node)
   16783              :         return error_mark_node;
   16784              : 
   16785     15613584 :       tree save_ccp = current_class_ptr;
   16786     15613584 :       tree save_ccr = current_class_ref;
   16787     15613584 :       tree this_type = (TREE_CODE (t) == METHOD_TYPE
   16788     17350698 :                         ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
   16789      1737114 :       bool do_inject = this_type && CLASS_TYPE_P (this_type);
   16790      1737114 :       if (do_inject)
   16791              :         {
   16792              :           /* DR 1207: 'this' is in scope in the trailing return type.  */
   16793      1737114 :           inject_this_parameter (this_type, cp_type_quals (this_type));
   16794              :         }
   16795              : 
   16796              :       /* Substitute the return type.  */
   16797     15613584 :       return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16798              : 
   16799     15610884 :       if (do_inject)
   16800              :         {
   16801      1737114 :           current_class_ptr = save_ccp;
   16802      1737114 :           current_class_ref = save_ccr;
   16803              :         }
   16804              :     }
   16805              :   else
   16806              :     /* Substitute the return type.  */
   16807    158761936 :     return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   16808              : 
   16809    174364687 :   if (return_type == error_mark_node)
   16810              :     return error_mark_node;
   16811              :   /* DR 486 clarifies that creation of a function type with an
   16812              :      invalid return type is a deduction failure.  */
   16813    172676462 :   if (TREE_CODE (return_type) == ARRAY_TYPE
   16814    172676293 :       || TREE_CODE (return_type) == FUNCTION_TYPE)
   16815              :     {
   16816          285 :       if (complain & tf_error)
   16817              :         {
   16818           26 :           if (TREE_CODE (return_type) == ARRAY_TYPE)
   16819           12 :             error ("function returning an array");
   16820              :           else
   16821           14 :             error ("function returning a function");
   16822              :         }
   16823          285 :       return error_mark_node;
   16824              :     }
   16825              : 
   16826    172676177 :   if (!late_return_type_p)
   16827              :     {
   16828              :       /* Substitute the argument types.  */
   16829    158012197 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   16830              :                                     complain, in_decl);
   16831    158012197 :       if (arg_types == error_mark_node)
   16832              :         return error_mark_node;
   16833              :     }
   16834              : 
   16835              :   /* Construct a new type node and return it.  */
   16836    172669795 :   return rebuild_function_or_method_type (t, args, return_type, arg_types,
   16837    172669795 :                                           /*raises=*/NULL_TREE, complain);
   16838              : }
   16839              : 
   16840              : /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
   16841              :    ARGS into that specification, and return the substituted
   16842              :    specification.  If there is no specification, return NULL_TREE.  */
   16843              : 
   16844              : static tree
   16845    172669772 : tsubst_exception_specification (tree fntype,
   16846              :                                 tree args,
   16847              :                                 tsubst_flags_t complain,
   16848              :                                 tree in_decl,
   16849              :                                 bool defer_ok)
   16850              : {
   16851    172669772 :   tree specs;
   16852    172669772 :   tree new_specs;
   16853              : 
   16854    172669772 :   specs = TYPE_RAISES_EXCEPTIONS (fntype);
   16855    172669772 :   new_specs = NULL_TREE;
   16856    234939055 :   if (specs && TREE_PURPOSE (specs))
   16857              :     {
   16858              :       /* A noexcept-specifier.  */
   16859     62230828 :       tree expr = TREE_PURPOSE (specs);
   16860     62230828 :       if (TREE_CODE (expr) == INTEGER_CST)
   16861     54828995 :         new_specs = expr;
   16862      7401833 :       else if (defer_ok)
   16863              :         {
   16864              :           /* Defer instantiation of noexcept-specifiers to avoid
   16865              :              excessive instantiations (c++/49107).  */
   16866      7337695 :           new_specs = make_node (DEFERRED_NOEXCEPT);
   16867      7337695 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   16868              :             {
   16869              :               /* We already partially instantiated this member template,
   16870              :                  so combine the new args with the old.  */
   16871           28 :               DEFERRED_NOEXCEPT_PATTERN (new_specs)
   16872           28 :                 = DEFERRED_NOEXCEPT_PATTERN (expr);
   16873           56 :               DEFERRED_NOEXCEPT_ARGS (new_specs)
   16874           56 :                 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
   16875              :             }
   16876              :           else
   16877              :             {
   16878      7337667 :               DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
   16879      7337667 :               DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
   16880              :             }
   16881              :         }
   16882              :       else
   16883              :         {
   16884        64138 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   16885              :             {
   16886            0 :               args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
   16887              :                                            args);
   16888            0 :               expr = DEFERRED_NOEXCEPT_PATTERN (expr);
   16889              :             }
   16890        64138 :           cp_unevaluated u;
   16891        64138 :           new_specs = tsubst_expr (expr, args, complain, in_decl);
   16892        64138 :         }
   16893     62230828 :       new_specs = build_noexcept_spec (new_specs, complain);
   16894              :       /* We've instantiated a template before a noexcept-specifier
   16895              :          contained therein has been parsed.  This can happen for
   16896              :          a nested template class:
   16897              : 
   16898              :           struct S {
   16899              :             template<typename> struct B { B() noexcept(...); };
   16900              :             struct A : B<int> { ... use B() ... };
   16901              :           };
   16902              : 
   16903              :          where completing B<int> will trigger instantiating the
   16904              :          noexcept, even though we only parse it at the end of S.  */
   16905     62230828 :       if (UNPARSED_NOEXCEPT_SPEC_P (specs))
   16906              :         {
   16907           15 :           gcc_checking_assert (defer_ok);
   16908           15 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
   16909              :         }
   16910              :     }
   16911    110438944 :   else if (specs)
   16912              :     {
   16913        38455 :       if (! TREE_VALUE (specs))
   16914        38394 :         new_specs = specs;
   16915              :       else
   16916          118 :         while (specs)
   16917              :           {
   16918           61 :             tree spec;
   16919           61 :             int i, len = 1;
   16920           61 :             tree expanded_specs = NULL_TREE;
   16921              : 
   16922           61 :             if (PACK_EXPANSION_P (TREE_VALUE (specs)))
   16923              :               {
   16924              :                 /* Expand the pack expansion type.  */
   16925           13 :                 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
   16926              :                                                        args, complain,
   16927              :                                                        in_decl);
   16928              : 
   16929           13 :                 if (expanded_specs == error_mark_node)
   16930              :                   return error_mark_node;
   16931           11 :                 else if (TREE_CODE (expanded_specs) == TREE_VEC)
   16932            4 :                   len = TREE_VEC_LENGTH (expanded_specs);
   16933              :                 else
   16934              :                   {
   16935              :                     /* We're substituting into a member template, so
   16936              :                        we got a TYPE_PACK_EXPANSION back.  Add that
   16937              :                        expansion and move on.  */
   16938            7 :                     gcc_assert (TREE_CODE (expanded_specs)
   16939              :                                 == TYPE_PACK_EXPANSION);
   16940            7 :                     new_specs = add_exception_specifier (new_specs,
   16941              :                                                          expanded_specs,
   16942              :                                                          complain);
   16943            7 :                     specs = TREE_CHAIN (specs);
   16944            7 :                     continue;
   16945              :                   }
   16946              :               }
   16947              : 
   16948          110 :             for (i = 0; i < len; ++i)
   16949              :               {
   16950           60 :                 if (expanded_specs)
   16951           12 :                   spec = TREE_VEC_ELT (expanded_specs, i);
   16952              :                 else
   16953           48 :                   spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
   16954           60 :                 if (spec == error_mark_node)
   16955              :                   return spec;
   16956           58 :                 new_specs = add_exception_specifier (new_specs, spec,
   16957              :                                                      complain);
   16958              :               }
   16959              : 
   16960           50 :             specs = TREE_CHAIN (specs);
   16961              :           }
   16962              :     }
   16963    172669768 :   return new_specs;
   16964              : }
   16965              : 
   16966              : /* Substitute through a TREE_LIST of types or expressions, handling pack
   16967              :    expansions.  */
   16968              : 
   16969              : tree
   16970     34948647 : tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16971              : {
   16972     34948647 :   if (t == void_list_node)
   16973              :     return t;
   16974              : 
   16975     34948629 :   tree purpose = TREE_PURPOSE (t);
   16976     34948629 :   tree purposevec = NULL_TREE;
   16977     34948629 :   if (!purpose)
   16978              :     ;
   16979            2 :   else if (PACK_EXPANSION_P (purpose))
   16980              :     {
   16981            0 :       purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
   16982            0 :       if (TREE_CODE (purpose) == TREE_VEC)
   16983            0 :         purposevec = purpose;
   16984              :     }
   16985            2 :   else if (TYPE_P (purpose))
   16986            0 :     purpose = tsubst (purpose, args, complain, in_decl);
   16987              :   else
   16988            2 :     purpose = tsubst_expr (purpose, args, complain, in_decl);
   16989     34948629 :   if (purpose == error_mark_node || purposevec == error_mark_node)
   16990              :     return error_mark_node;
   16991              : 
   16992     34948629 :   tree value = TREE_VALUE (t);
   16993     34948629 :   tree valuevec = NULL_TREE;
   16994     34948629 :   if (!value)
   16995              :     ;
   16996     34948629 :   else if (PACK_EXPANSION_P (value))
   16997              :     {
   16998       718545 :       value = tsubst_pack_expansion (value, args, complain, in_decl);
   16999       718545 :       if (TREE_CODE (value) == TREE_VEC)
   17000       713789 :         valuevec = value;
   17001              :     }
   17002     34230084 :   else if (TYPE_P (value))
   17003           24 :     value = tsubst (value, args, complain, in_decl);
   17004              :   else
   17005     34230060 :     value = tsubst_expr (value, args, complain, in_decl);
   17006     34948629 :   if (value == error_mark_node || valuevec == error_mark_node)
   17007              :     return error_mark_node;
   17008              : 
   17009     34907181 :   tree chain = TREE_CHAIN (t);
   17010     34907181 :   if (!chain)
   17011              :     ;
   17012       768112 :   else if (TREE_CODE (chain) == TREE_LIST)
   17013       768112 :     chain = tsubst_tree_list (chain, args, complain, in_decl);
   17014            0 :   else if (TYPE_P (chain))
   17015            0 :     chain = tsubst (chain, args, complain, in_decl);
   17016              :   else
   17017            0 :     chain = tsubst_expr (chain, args, complain, in_decl);
   17018     34907181 :   if (chain == error_mark_node)
   17019              :     return error_mark_node;
   17020              : 
   17021     34907181 :   if (purpose == TREE_PURPOSE (t)
   17022     34907179 :       && value == TREE_VALUE (t)
   17023     35574208 :       && chain == TREE_CHAIN (t))
   17024              :     return t;
   17025              : 
   17026     34240374 :   int len;
   17027              :   /* Determine the number of arguments.  */
   17028     34240374 :   if (purposevec)
   17029              :     {
   17030            0 :       len = TREE_VEC_LENGTH (purposevec);
   17031            0 :       gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
   17032              :     }
   17033     34240374 :   else if (valuevec)
   17034       713789 :     len = TREE_VEC_LENGTH (valuevec);
   17035              :   else
   17036              :     len = 1;
   17037              : 
   17038     68313164 :   for (int i = len; i-- > 0; )
   17039              :     {
   17040     34072790 :       if (purposevec)
   17041            0 :         purpose = TREE_VEC_ELT (purposevec, i);
   17042     34072790 :       if (valuevec)
   17043       546205 :         value = TREE_VEC_ELT (valuevec, i);
   17044              : 
   17045     34072790 :       if (value && TYPE_P (value))
   17046           26 :         chain = hash_tree_cons (purpose, value, chain);
   17047              :       else
   17048     34072764 :         chain = tree_cons (purpose, value, chain);
   17049              :     }
   17050              : 
   17051              :   return chain;
   17052              : }
   17053              : 
   17054              : /* Substitute ARGS into T, which is a splice scope.  */
   17055              : 
   17056              : static tree
   17057          504 : tsubst_splice_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17058              : {
   17059          504 :   tree r = tsubst (SPLICE_SCOPE_EXPR (t), args, complain, in_decl);
   17060          504 :   if (r == error_mark_node)
   17061              :     return r;
   17062          474 :   const bool type_p = SPLICE_SCOPE_TYPE_P (t);
   17063          474 :   if (dependent_splice_p (r))
   17064           12 :     r = make_splice_scope (r, type_p);
   17065          462 :   else if (type_p && ctad_template_p (r))
   17066           10 :     r = make_template_placeholder (r);
   17067          474 :   if (type_p
   17068          474 :       ? !valid_splice_type_p (r)
   17069          208 :       : !valid_splice_scope_p (r))
   17070              :     {
   17071           46 :       if (complain & tf_error)
   17072              :         {
   17073           46 :           const location_t loc = EXPR_LOCATION (SPLICE_SCOPE_EXPR (t));
   17074           46 :           auto_diagnostic_group d;
   17075           46 :           if (type_p)
   17076           24 :             error_at (loc, "expected a reflection of a type");
   17077              :           else
   17078           22 :             error_at (loc, "expected a reflection of a class, namespace, or "
   17079              :                       "enumeration");
   17080           46 :           inform_tree_category (r);
   17081           46 :         }
   17082           46 :       return error_mark_node;
   17083              :     }
   17084              : 
   17085          428 :   if (type_p)
   17086          242 :     r = cp_build_qualified_type (r, cp_type_quals (t) | cp_type_quals (r),
   17087              :                                  complain | tf_ignore_bad_quals);
   17088              : 
   17089              :   return r;
   17090              : }
   17091              : 
   17092              : /* Substitute ARGS into T, which is a splice expression.  */
   17093              : 
   17094              : static tree
   17095         1750 : tsubst_splice_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17096              : {
   17097         1750 :   tree template_id = NULL_TREE;
   17098         3468 :   auto apply_template = [&](tree templ)
   17099              :     {
   17100         1718 :       if (!template_id)
   17101              :         return templ;
   17102          294 :       template_id = copy_node (template_id);
   17103          294 :       tree ret = template_id;
   17104              : 
   17105              :       /* follow the example of lookup_template_function, but for all
   17106              :          templates.  */
   17107          294 :       if (BASELINK_P (templ))
   17108              :         {
   17109          228 :           ret = copy_node (templ);
   17110          228 :           BASELINK_FUNCTIONS (ret) = template_id;
   17111          228 :           templ = BASELINK_FUNCTIONS (templ);
   17112              :         }
   17113          294 :       TREE_OPERAND (template_id, 0) = templ;
   17114          294 :       return ret;
   17115         1750 :     };
   17116              : 
   17117         1750 :   if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
   17118              :     {
   17119          294 :       template_id = t;
   17120          294 :       t = TREE_OPERAND (t, 0);
   17121              :     }
   17122              : 
   17123         1750 :   tree op = tsubst_expr (TREE_OPERAND (t, 0), args,
   17124              :                          (complain & ~tf_no_name_lookup), in_decl);
   17125         1750 :   if (op == error_mark_node)
   17126              :     return error_mark_node;
   17127         1722 :   op = splice (op);
   17128         1722 :   if (op == error_mark_node)
   17129              :     return error_mark_node;
   17130         1718 :   if (dependent_splice_p (op))
   17131              :     {
   17132           20 :       if (SPLICE_EXPR_EXPRESSION_P (t))
   17133           16 :         SET_SPLICE_EXPR_EXPRESSION_P (op);
   17134           20 :       if (SPLICE_EXPR_MEMBER_ACCESS_P (t))
   17135           12 :         SET_SPLICE_EXPR_MEMBER_ACCESS_P (op, true);
   17136           20 :       if (SPLICE_EXPR_ADDRESS_P (t))
   17137            0 :         SET_SPLICE_EXPR_ADDRESS_P (op, true);
   17138           20 :       if (SPLICE_EXPR_TEMPLATE_P (t))
   17139            0 :         SET_SPLICE_EXPR_TEMPLATE_P (op, true);
   17140           20 :       if (SPLICE_EXPR_TARGS_P (t))
   17141            0 :         SET_SPLICE_EXPR_TARGS_P (op, true);
   17142           20 :       return apply_template (op);
   17143              :     }
   17144              : 
   17145              :   /* We have to form a template-id for checking too.  */
   17146         1698 :   op = apply_template (op);
   17147              : 
   17148         1698 :   if (SPLICE_EXPR_EXPRESSION_P (t)
   17149         1698 :       && !check_splice_expr (input_location, UNKNOWN_LOCATION, op,
   17150         1218 :                              SPLICE_EXPR_ADDRESS_P (t),
   17151         1218 :                              SPLICE_EXPR_MEMBER_ACCESS_P (t),
   17152         1218 :                              SPLICE_EXPR_TEMPLATE_P (t),
   17153         1218 :                              SPLICE_EXPR_TARGS_P (t),
   17154              :                              (complain & tf_error)))
   17155           76 :     return error_mark_node;
   17156              : 
   17157              :   /* For the template-id case, we have to substitute only after checking, to
   17158              :      reject the case where the template part is a type.  */
   17159         1622 :   if (template_id)
   17160          280 :     op = tsubst_expr (op, args, complain, in_decl);
   17161              : 
   17162         1622 :   if (SPLICE_EXPR_ADDRESS_P (t))
   17163              :     {
   17164           50 :       push_deferring_access_checks (dk_no_check);
   17165           50 :       if (BASELINK_P (op))
   17166           24 :         op = build_offset_ref (BINFO_TYPE (BASELINK_ACCESS_BINFO (op)), op,
   17167              :                                /*address_p=*/true, complain);
   17168           26 :       else if (DECL_NONSTATIC_MEMBER_P (op))
   17169           12 :         op = build_offset_ref (DECL_CONTEXT (op), op,
   17170              :                                /*address_p=*/true, complain);
   17171           50 :       pop_deferring_access_checks ();
   17172              :     }
   17173              : 
   17174         1622 :   if (outer_automatic_var_p (op))
   17175            2 :     op = process_outer_var_ref (op, complain);
   17176              :   /* Like in cp_parser_splice_expression, for foo.[: bar :]
   17177              :      cp_parser_postfix_dot_deref_expression wants to see only
   17178              :      certain kind of entities.  */
   17179         1622 :   if (SPLICE_EXPR_MEMBER_ACCESS_P (t))
   17180          882 :     gcc_assert (valid_splice_for_member_access_p (op, /*decls_only_p=*/false));
   17181          740 :   else if (SPLICE_EXPR_EXPRESSION_P (t))
   17182              :     {
   17183          260 :       op = convert_from_reference (op);
   17184          260 :       if (flag_contracts && processing_contract_condition)
   17185           32 :         op = constify_contract_access (op);
   17186              :     }
   17187              : 
   17188              :   return op;
   17189              : }
   17190              : 
   17191              : /* Return true iff we're in an expansion statement.  */
   17192              : 
   17193              : static bool
   17194     42986495 : in_expansion_stmt_p ()
   17195              : {
   17196     42986495 :   if (in_expansion_stmt)
   17197              :     return true;
   17198              : 
   17199              :   /* In instantiations in_expansion_stmt is false.  */
   17200     42986495 :   for (cp_binding_level *b = current_binding_level;
   17201    121689074 :        b && b->kind != sk_function_parms;
   17202     78702579 :        b = b->level_chain)
   17203     78703339 :     if (b->kind == sk_template_for)
   17204              :       return true;
   17205              :   return false;
   17206              : }
   17207              : 
   17208              : /* Take the tree structure T and replace template parameters used
   17209              :    therein with the argument vector ARGS.  IN_DECL is an associated
   17210              :    decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
   17211              :    Issue error and warning messages under control of COMPLAIN.  Note
   17212              :    that we must be relatively non-tolerant of extensions here, in
   17213              :    order to preserve conformance; if we allow substitutions that
   17214              :    should not be allowed, we may allow argument deductions that should
   17215              :    not succeed, and therefore report ambiguous overload situations
   17216              :    where there are none.  In theory, we could allow the substitution,
   17217              :    but indicate that it should have failed, and allow our caller to
   17218              :    make sure that the right thing happens, but we don't try to do this
   17219              :    yet.
   17220              : 
   17221              :    This function is used for dealing with types, decls and the like;
   17222              :    for expressions, use tsubst_expr or tsubst_copy.  */
   17223              : 
   17224              : tree
   17225   7612464954 : tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17226              : {
   17227   7612464954 :   enum tree_code code;
   17228   7612464954 :   tree type, r = NULL_TREE;
   17229              : 
   17230   7612464954 :   if (t == NULL_TREE || t == error_mark_node
   17231   7562057316 :       || t == integer_type_node
   17232   7472281278 :       || t == void_type_node
   17233   7384151944 :       || t == char_type_node
   17234   7369855945 :       || t == unknown_type_node
   17235   7369855935 :       || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
   17236              :     return t;
   17237              : 
   17238              :   /* Any instantiation of a template containing a TU-local entity is an
   17239              :      exposure, so always issue a diagnostic irrespective of complain.  */
   17240   7369662063 :   if (instantiating_tu_local_entity (t))
   17241           28 :     return error_mark_node;
   17242              : 
   17243   7369662035 :   tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
   17244   7369662035 :   complain &= ~tf_tst_ok;
   17245              : 
   17246   7369662035 :   tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
   17247   7369662035 :   complain &= ~tf_qualifying_scope;
   17248              : 
   17249   7369662035 :   if (DECL_P (t))
   17250    715820417 :     return tsubst_decl (t, args, complain);
   17251              : 
   17252   6653841618 :   if (args == NULL_TREE)
   17253              :     return t;
   17254              : 
   17255   6584118033 :   code = TREE_CODE (t);
   17256              : 
   17257   6584118033 :   gcc_assert (code != IDENTIFIER_NODE);
   17258   6584118033 :   type = TREE_TYPE (t);
   17259              : 
   17260   6584118033 :   gcc_assert (type != unknown_type_node);
   17261              : 
   17262   6584118033 :   if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   17263              :     return d;
   17264              : 
   17265              :   /* Reuse typedefs.  We need to do this to handle dependent attributes,
   17266              :      such as attribute aligned.  */
   17267   6584098461 :   if (TYPE_P (t)
   17268   6584098461 :       && typedef_variant_p (t))
   17269              :     {
   17270    339290375 :       tree decl = TYPE_NAME (t);
   17271              : 
   17272    339290375 :       if (alias_template_specialization_p (t, nt_opaque))
   17273              :         {
   17274              :           /* DECL represents an alias template and we want to
   17275              :              instantiate it.  */
   17276    118952492 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   17277    118952492 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   17278    118952492 :           r = instantiate_alias_template (tmpl, gen_args, complain);
   17279              :         }
   17280    440675766 :       else if (DECL_CLASS_SCOPE_P (decl)
   17281    181103076 :                && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
   17282    395754921 :                && uses_template_parms (DECL_CONTEXT (decl)))
   17283              :         {
   17284    175168830 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   17285    175168830 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   17286    175168830 :           r = retrieve_specialization (tmpl, gen_args, 0);
   17287              :         }
   17288     90338106 :       else if ((DECL_FUNCTION_SCOPE_P (decl)
   17289      2187476 :                 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
   17290      2187470 :                 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
   17291              :                /* The { } of an expansion-statement is considered a template
   17292              :                   definition.  */
   17293     88155548 :                || in_expansion_stmt_p ())
   17294      2183318 :         r = retrieve_local_specialization (decl);
   17295              :       else
   17296              :         /* The typedef is from a non-template context.  */
   17297              :         return t;
   17298              : 
   17299    296304637 :       if (r)
   17300              :         {
   17301    296302366 :           r = TREE_TYPE (r);
   17302    296302366 :           r = cp_build_qualified_type
   17303    296302366 :             (r, cp_type_quals (t) | cp_type_quals (r),
   17304              :              complain | tf_ignore_bad_quals);
   17305    296302366 :           return r;
   17306              :         }
   17307              :       else
   17308              :         {
   17309              :           /* We don't have an instantiation yet, so drop the typedef.  */
   17310         2271 :           int quals = cp_type_quals (t);
   17311         2271 :           t = DECL_ORIGINAL_TYPE (decl);
   17312         2271 :           t = cp_build_qualified_type (t, quals,
   17313              :                                        complain | tf_ignore_bad_quals);
   17314              :         }
   17315              :     }
   17316              : 
   17317   6244810357 :   bool fndecl_type = (complain & tf_fndecl_type);
   17318   6244810357 :   complain &= ~tf_fndecl_type;
   17319              : 
   17320   6244810357 :   if (type
   17321   6244810357 :       && code != TYPENAME_TYPE
   17322   1072690953 :       && code != TEMPLATE_TYPE_PARM
   17323   1072690953 :       && code != TEMPLATE_PARM_INDEX
   17324              :       && code != IDENTIFIER_NODE
   17325    863331560 :       && code != FUNCTION_TYPE
   17326    796175501 :       && code != METHOD_TYPE
   17327    796175501 :       && code != PACK_INDEX_TYPE)
   17328    688949622 :     type = tsubst (type, args, complain, in_decl);
   17329   6244810357 :   if (type == error_mark_node)
   17330              :     return error_mark_node;
   17331              : 
   17332   6244804638 :   switch (code)
   17333              :     {
   17334   1060642656 :     case RECORD_TYPE:
   17335   1060642656 :       if (TYPE_PTRMEMFUNC_P (t))
   17336        32833 :         return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
   17337              :       /* Fall through.  */
   17338   1088345176 :     case UNION_TYPE:
   17339   1088345176 :     case ENUMERAL_TYPE:
   17340   1088345176 :       if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
   17341              :         {
   17342   1938834154 :           if (LAMBDA_TYPE_P (t))
   17343              :             {
   17344              :               /* In reconstruct_lambda_capture_pack we need to be able to
   17345              :                  rebuild the lambda closure parm, which means looking up the
   17346              :                  closure type.  See the comment for that function about using
   17347              :                  current_class_type.  */
   17348           36 :               tree c = current_class_type;
   17349           72 :               if (LAMBDA_TYPE_P (c)
   17350           72 :                   && (DECL_SOURCE_LOCATION (TYPE_NAME (t))
   17351           36 :                       == DECL_SOURCE_LOCATION (TYPE_NAME (c))))
   17352              :                 return c;
   17353            0 :               gcc_unreachable ();
   17354              :             }
   17355              :           /* Figure out what arguments are appropriate for the
   17356              :              type we are trying to find.  For example, given:
   17357              : 
   17358              :                template <class T> struct S;
   17359              :                template <class T, class U> void f(T, U) { S<U> su; }
   17360              : 
   17361              :              and supposing that we are instantiating f<int, double>,
   17362              :              then our ARGS will be {int, double}, but, when looking up
   17363              :              S we only want {double}.  */
   17364   1941259352 :           tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
   17365              :                                               complain, in_decl);
   17366    970629640 :           if (argvec == error_mark_node)
   17367              :             return error_mark_node;
   17368              : 
   17369    970503921 :           tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
   17370              :                                           complain);
   17371    970503876 :           return cp_build_qualified_type (r, cp_type_quals (t), complain);
   17372              :         }
   17373              :       else
   17374              :         /* This is not a template type, so there's nothing to do.  */
   17375              :         return t;
   17376              : 
   17377              :     case ERROR_MARK:
   17378              :     case IDENTIFIER_NODE:
   17379              :     case VOID_TYPE:
   17380              :     case OPAQUE_TYPE:
   17381              :     case REAL_TYPE:
   17382              :     case COMPLEX_TYPE:
   17383              :     case VECTOR_TYPE:
   17384              :     case BOOLEAN_TYPE:
   17385              :     case NULLPTR_TYPE:
   17386              :     case LANG_TYPE:
   17387              :       return t;
   17388              : 
   17389    116470793 :     case INTEGER_TYPE:
   17390    116470793 :       if (t == integer_type_node)
   17391              :         return t;
   17392              : 
   17393    116470781 :       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
   17394    116470781 :           && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
   17395              :         return t;
   17396              : 
   17397       622598 :       {
   17398       622598 :         tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
   17399              : 
   17400       622598 :         max = tsubst_expr (omax, args, complain, in_decl);
   17401              : 
   17402              :         /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
   17403              :            needed.  */
   17404       622598 :         if (TREE_CODE (max) == NOP_EXPR
   17405        41683 :             && TREE_SIDE_EFFECTS (omax)
   17406       622647 :             && !TREE_TYPE (max))
   17407            0 :           TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
   17408              : 
   17409              :         /* If we're in a partial instantiation, preserve the magic NOP_EXPR
   17410              :            with TREE_SIDE_EFFECTS that indicates this is not an integral
   17411              :            constant expression.  */
   17412       622598 :         if (processing_template_decl
   17413       622598 :             && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
   17414              :           {
   17415            0 :             gcc_assert (TREE_CODE (max) == NOP_EXPR);
   17416            0 :             TREE_SIDE_EFFECTS (max) = 1;
   17417              :           }
   17418              : 
   17419       622598 :         return compute_array_index_type (NULL_TREE, max, complain);
   17420              :       }
   17421              : 
   17422   3067392557 :     case TEMPLATE_TYPE_PARM:
   17423   3067392557 :       if (TEMPLATE_TYPE_LEVEL (t) == 0)
   17424              :         {
   17425              :           /* This is either an ordinary level-less auto or a CTAD placeholder
   17426              :              auto.  These get replaced only via do_auto_deduction which, in the
   17427              :              ordinary case, temporarily overrides its level to 1 before calling
   17428              :              tsubst.  CTAD placeholders are replaced via do_class_deduction.  */
   17429       215990 :           gcc_checking_assert (is_auto (t));
   17430       215990 :           tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
   17431       215990 :           if (!tmpl)
   17432              :             /* Ordinary level-less auto has nothing to substitute.  */
   17433              :             return t;
   17434              : 
   17435              :           /* Substitute the template of this CTAD placeholder.  */
   17436        30449 :           tmpl = tsubst_expr (tmpl, args, complain, in_decl);
   17437        30449 :           if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   17438         3261 :             tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
   17439              : 
   17440        30449 :           if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
   17441         3946 :             return make_template_placeholder (tmpl);
   17442              :           else
   17443              :             return t;
   17444              :         }
   17445              :       /* Fall through.  */
   17446   3279771257 :     case TEMPLATE_TEMPLATE_PARM:
   17447   3279771257 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   17448   3279771257 :     case TEMPLATE_PARM_INDEX:
   17449   3279771257 :       {
   17450   3279771257 :         int idx;
   17451   3279771257 :         int level;
   17452   3279771257 :         int levels;
   17453   3279771257 :         tree arg = NULL_TREE;
   17454              : 
   17455   3279771257 :         r = NULL_TREE;
   17456              : 
   17457   3279771257 :         gcc_assert (TREE_VEC_LENGTH (args) > 0);
   17458   3279771257 :         template_parm_level_and_index (t, &level, &idx);
   17459              : 
   17460   3279771257 :         levels = TMPL_ARGS_DEPTH (args);
   17461   3279771257 :         if (level <= levels
   17462   9456902699 :             && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
   17463              :           {
   17464   3088565678 :             arg = TMPL_ARG (args, level, idx);
   17465              : 
   17466              :             /* See through ARGUMENT_PACK_SELECT arguments. */
   17467   3088565678 :             if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   17468     11527709 :               arg = argument_pack_select_arg (arg);
   17469              :           }
   17470              : 
   17471   3279771257 :         if (arg == error_mark_node)
   17472              :           return error_mark_node;
   17473   3279771200 :         else if (arg != NULL_TREE)
   17474              :           {
   17475   3070100955 :             if (ARGUMENT_PACK_P (arg))
   17476              :               /* If ARG is an argument pack, we don't actually want to
   17477              :                  perform a substitution here, because substitutions
   17478              :                  for argument packs are only done
   17479              :                  element-by-element. We can get to this point when
   17480              :                  substituting the type of a non-type template
   17481              :                  parameter pack, when that type actually contains
   17482              :                  template parameter packs from an outer template, e.g.,
   17483              : 
   17484              :                  template<typename... Types> struct A {
   17485              :                    template<Types... Values> struct B { };
   17486              :                  };  */
   17487              :               return t;
   17488              : 
   17489   3070100647 :             if (code == TEMPLATE_TYPE_PARM)
   17490              :               {
   17491   2865433318 :                 int quals;
   17492              : 
   17493   2865433318 :                 gcc_assert (TYPE_P (arg));
   17494              : 
   17495   2865433318 :                 quals = cp_type_quals (arg) | cp_type_quals (t);
   17496              : 
   17497   2865433318 :                 return cp_build_qualified_type
   17498   2865433318 :                   (arg, quals, complain | tf_ignore_bad_quals);
   17499              :               }
   17500    204667329 :             else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
   17501              :               {
   17502              :                 /* We are processing a type constructed from a
   17503              :                    template template parameter.  */
   17504      1396954 :                 tree argvec = tsubst (TYPE_TI_ARGS (t),
   17505              :                                       args, complain, in_decl);
   17506       698477 :                 if (argvec == error_mark_node)
   17507              :                   return error_mark_node;
   17508              : 
   17509       697397 :                 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
   17510              :                             || TREE_CODE (arg) == TEMPLATE_DECL
   17511              :                             || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
   17512              : 
   17513       697397 :                 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
   17514              :                   /* Consider this code:
   17515              : 
   17516              :                         template <template <class> class Template>
   17517              :                         struct Internal {
   17518              :                         template <class Arg> using Bind = Template<Arg>;
   17519              :                         };
   17520              : 
   17521              :                         template <template <class> class Template, class Arg>
   17522              :                         using Instantiate = Template<Arg>; //#0
   17523              : 
   17524              :                         template <template <class> class Template,
   17525              :                                   class Argument>
   17526              :                         using Bind =
   17527              :                           Instantiate<Internal<Template>::template Bind,
   17528              :                                       Argument>; //#1
   17529              : 
   17530              :                      When #1 is parsed, the
   17531              :                      BOUND_TEMPLATE_TEMPLATE_PARM representing the
   17532              :                      parameter `Template' in #0 matches the
   17533              :                      UNBOUND_CLASS_TEMPLATE representing the argument
   17534              :                      `Internal<Template>::template Bind'; We then want
   17535              :                      to assemble the type `Bind<Argument>' that can't
   17536              :                      be fully created right now, because
   17537              :                      `Internal<Template>' not being complete, the Bind
   17538              :                      template cannot be looked up in that context.  So
   17539              :                      we need to "store" `Bind<Argument>' for later
   17540              :                      when the context of Bind becomes complete.  Let's
   17541              :                      store that in a TYPENAME_TYPE.  */
   17542            6 :                   return make_typename_type (TYPE_CONTEXT (arg),
   17543              :                                              build_nt (TEMPLATE_ID_EXPR,
   17544            6 :                                                        TYPE_IDENTIFIER (arg),
   17545              :                                                        argvec),
   17546              :                                              typename_type,
   17547            6 :                                              complain);
   17548              : 
   17549              :                 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
   17550              :                    are resolving nested-types in the signature of a
   17551              :                    member function templates.  Otherwise ARG is a
   17552              :                    TEMPLATE_DECL and is the real template to be
   17553              :                    instantiated.  */
   17554       697391 :                 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   17555          127 :                   arg = TYPE_NAME (arg);
   17556              : 
   17557       697391 :                 r = lookup_template_class (arg,
   17558              :                                            argvec, in_decl,
   17559       697391 :                                            DECL_CONTEXT (arg),
   17560              :                                            complain);
   17561       697391 :                 return cp_build_qualified_type
   17562       697391 :                   (r, cp_type_quals (t) | cp_type_quals (r), complain);
   17563              :               }
   17564    203968852 :             else if (code == TEMPLATE_TEMPLATE_PARM)
   17565              :               return arg;
   17566              :             else
   17567              :               /* TEMPLATE_PARM_INDEX.  */
   17568    201500267 :               return convert_from_reference (unshare_expr (arg));
   17569              :           }
   17570              : 
   17571    209670245 :         if (level == 1)
   17572              :           /* This can happen during the attempted tsubst'ing in
   17573              :              unify.  This means that we don't yet have any information
   17574              :              about the template parameter in question.  */
   17575              :           return t;
   17576              : 
   17577              :         /* Early in template argument deduction substitution, we don't
   17578              :            want to reduce the level of 'auto', or it will be confused
   17579              :            with a normal template parm in subsequent deduction.
   17580              :            Similarly, don't reduce the level of template parameters to
   17581              :            avoid mismatches when deducing their types.  */
   17582    191205539 :         if (complain & tf_partial)
   17583              :           return t;
   17584              : 
   17585              :         /* If we get here, we must have been looking at a parm for a
   17586              :            more deeply nested template.  Make a new version of this
   17587              :            template parameter, but with a lower level.  */
   17588    190611996 :         int quals;
   17589    190611996 :         switch (code)
   17590              :           {
   17591    184388887 :           case TEMPLATE_TYPE_PARM:
   17592    184388887 :           case TEMPLATE_TEMPLATE_PARM:
   17593    184388887 :             quals = cp_type_quals (t);
   17594    184388887 :             if (quals)
   17595              :               {
   17596     12670041 :                 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
   17597     12670041 :                 t = TYPE_MAIN_VARIANT (t);
   17598              :               }
   17599              : 
   17600    184388887 :             if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
   17601    169218497 :               if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
   17602    169218497 :                   && (code == TEMPLATE_TYPE_PARM
   17603        54232 :                       || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
   17604              :                 /* Cache lowering a type parameter or a simple template
   17605              :                    template parameter.  */
   17606    169177579 :                 r = TREE_TYPE (d);
   17607              : 
   17608    184388887 :             if (!r)
   17609              :               {
   17610     15211308 :                 r = copy_type (t);
   17611     15211308 :                 TEMPLATE_TYPE_PARM_INDEX (r)
   17612     15211308 :                   = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
   17613              :                                                 r, levels, args, complain);
   17614     15211308 :                 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
   17615     15211308 :                 TYPE_MAIN_VARIANT (r) = r;
   17616     15211308 :                 TYPE_POINTER_TO (r) = NULL_TREE;
   17617     15211308 :                 TYPE_REFERENCE_TO (r) = NULL_TREE;
   17618              : 
   17619     15211308 :                 if (code == TEMPLATE_TYPE_PARM)
   17620     15209412 :                   if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
   17621              :                     /* Propagate constraints on placeholders since they are
   17622              :                        only instantiated during satisfaction.  */
   17623       529737 :                     PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
   17624              : 
   17625     15211308 :                 if (TYPE_STRUCTURAL_EQUALITY_P (t))
   17626            0 :                   SET_TYPE_STRUCTURAL_EQUALITY (r);
   17627              :                 else
   17628     15211308 :                   TYPE_CANONICAL (r) = canonical_type_parameter (r);
   17629              :               }
   17630              : 
   17631    184388887 :             if (quals)
   17632     12670041 :               r = cp_build_qualified_type (r, quals,
   17633              :                                            complain | tf_ignore_bad_quals);
   17634              :             break;
   17635              : 
   17636        12253 :           case BOUND_TEMPLATE_TEMPLATE_PARM:
   17637        12253 :             {
   17638        12253 :               tree tinfo = TYPE_TEMPLATE_INFO (t);
   17639              :               /* We might need to substitute into the types of non-type
   17640              :                  template parameters.  This also lowers the level of
   17641              :                  the ttp appropriately.  */
   17642        12253 :               tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
   17643              :                                   complain, in_decl);
   17644        12253 :               if (tmpl == error_mark_node)
   17645              :                 return error_mark_node;
   17646        12253 :               tree argvec = tsubst (TI_ARGS (tinfo), args,
   17647              :                                     complain, in_decl);
   17648        12253 :               if (argvec == error_mark_node)
   17649              :                 return error_mark_node;
   17650        12253 :               r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE,
   17651              :                                          complain);
   17652        12253 :               r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   17653        12253 :               break;
   17654              :             }
   17655              : 
   17656      6210856 :           case TEMPLATE_PARM_INDEX:
   17657              :             /* OK, now substitute the type of the non-type parameter.  We
   17658              :                couldn't do it earlier because it might be an auto parameter,
   17659              :                and we wouldn't need to if we had an argument.  */
   17660      6210856 :             type = tsubst (type, args, complain, in_decl);
   17661      6210856 :             if (type == error_mark_node)
   17662              :               return error_mark_node;
   17663      6210850 :             r = reduce_template_parm_level (t, type, levels, args, complain);
   17664      6210850 :             break;
   17665              : 
   17666              :           default:
   17667              :             gcc_unreachable ();
   17668              :           }
   17669              : 
   17670    190611990 :         return r;
   17671              :       }
   17672              : 
   17673           26 :     case TREE_LIST:
   17674           26 :       return tsubst_tree_list (t, args, complain, in_decl);
   17675              : 
   17676            0 :     case TREE_BINFO:
   17677              :       /* We should never be tsubsting a binfo.  */
   17678            0 :       gcc_unreachable ();
   17679              : 
   17680    417621051 :     case TREE_VEC:
   17681              :       /* A vector of template arguments.  */
   17682    417621051 :       gcc_assert (!type);
   17683    417621051 :       return tsubst_template_args (t, args, complain, in_decl);
   17684              : 
   17685    644721036 :     case POINTER_TYPE:
   17686    644721036 :     case REFERENCE_TYPE:
   17687    644721036 :       {
   17688    644721036 :         if (type == TREE_TYPE (t)
   17689     36022379 :             && TREE_CODE (type) != METHOD_TYPE
   17690    680742813 :             && (TYPE_ATTRIBUTES (t) == NULL_TREE
   17691            6 :                 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
   17692              :           return t;
   17693              : 
   17694              :         /* [temp.deduct]
   17695              : 
   17696              :            Type deduction may fail for any of the following
   17697              :            reasons:
   17698              : 
   17699              :            -- Attempting to create a pointer to reference type.
   17700              :            -- Attempting to create a reference to a reference type or
   17701              :               a reference to void.
   17702              : 
   17703              :           Core issue 106 says that creating a reference to a reference
   17704              :           during instantiation is no longer a cause for failure. We
   17705              :           only enforce this check in strict C++98 mode.  */
   17706    608699265 :         if ((TYPE_REF_P (type)
   17707     34175361 :              && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
   17708    608699058 :             || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
   17709              :           {
   17710         1497 :             static location_t last_loc;
   17711              : 
   17712              :             /* We keep track of the last time we issued this error
   17713              :                message to avoid spewing a ton of messages during a
   17714              :                single bad template instantiation.  */
   17715         1497 :             if (complain & tf_error
   17716          105 :                 && last_loc != input_location)
   17717              :               {
   17718          105 :                 if (VOID_TYPE_P (type))
   17719            8 :                   error ("forming reference to void");
   17720           97 :                else if (code == POINTER_TYPE)
   17721           97 :                  error ("forming pointer to reference type %qT", type);
   17722              :                else
   17723            0 :                   error ("forming reference to reference type %qT", type);
   17724          105 :                 last_loc = input_location;
   17725              :               }
   17726              : 
   17727         1497 :             return error_mark_node;
   17728              :           }
   17729    608697768 :         else if (TREE_CODE (type) == FUNCTION_TYPE
   17730    608697768 :                  && (type_memfn_quals (type) != TYPE_UNQUALIFIED
   17731      6966154 :                      || type_memfn_rqual (type) != REF_QUAL_NONE))
   17732              :           {
   17733           88 :             if (complain & tf_error)
   17734              :               {
   17735           11 :                 if (code == POINTER_TYPE)
   17736            6 :                   error ("forming pointer to qualified function type %qT",
   17737              :                          type);
   17738              :                 else
   17739            5 :                   error ("forming reference to qualified function type %qT",
   17740              :                          type);
   17741              :               }
   17742           88 :             return error_mark_node;
   17743              :           }
   17744    608697680 :         else if (code == POINTER_TYPE)
   17745              :           {
   17746    281217676 :             r = build_pointer_type (type);
   17747    281217676 :             if (TREE_CODE (type) == METHOD_TYPE)
   17748        32827 :               r = build_ptrmemfunc_type (r);
   17749              :           }
   17750    327480004 :         else if (TYPE_REF_P (type))
   17751              :           /* In C++0x, during template argument substitution, when there is an
   17752              :              attempt to create a reference to a reference type, reference
   17753              :              collapsing is applied as described in [14.3.1/4 temp.arg.type]:
   17754              : 
   17755              :              "If a template-argument for a template-parameter T names a type
   17756              :              that is a reference to a type A, an attempt to create the type
   17757              :              'lvalue reference to cv T' creates the type 'lvalue reference to
   17758              :              A,' while an attempt to create the type type rvalue reference to
   17759              :              cv T' creates the type T"
   17760              :           */
   17761     68350308 :           r = cp_build_reference_type (TREE_TYPE (type),
   17762     34175154 :                                        TYPE_REF_IS_RVALUE (t)
   17763     22698016 :                                        && TYPE_REF_IS_RVALUE (type));
   17764              :         else
   17765    293304850 :           r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
   17766    608697680 :         r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   17767              : 
   17768    608697680 :         if (r != error_mark_node)
   17769              :           /* Will this ever be needed for TYPE_..._TO values?  */
   17770    608697680 :           layout_type (r);
   17771              : 
   17772    608697680 :         if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
   17773              :                                              /*flags=*/0,
   17774              :                                              args, complain, in_decl))
   17775            0 :           return error_mark_node;
   17776              : 
   17777    608697680 :         return r;
   17778              :       }
   17779       205389 :     case OFFSET_TYPE:
   17780       205389 :       {
   17781       205389 :         r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
   17782       205389 :         if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
   17783              :           {
   17784              :             /* [temp.deduct]
   17785              : 
   17786              :                Type deduction may fail for any of the following
   17787              :                reasons:
   17788              : 
   17789              :                -- Attempting to create "pointer to member of T" when T
   17790              :                   is not a class type.  */
   17791            0 :             if (complain & tf_error)
   17792            0 :               error ("creating pointer to member of non-class type %qT", r);
   17793            0 :             return error_mark_node;
   17794              :           }
   17795       205389 :         if (TYPE_REF_P (type))
   17796              :           {
   17797            6 :             if (complain & tf_error)
   17798            3 :               error ("creating pointer to member reference type %qT", type);
   17799            6 :             return error_mark_node;
   17800              :           }
   17801       205383 :         if (VOID_TYPE_P (type))
   17802              :           {
   17803            3 :             if (complain & tf_error)
   17804            3 :               error ("creating pointer to member of type void");
   17805            3 :             return error_mark_node;
   17806              :           }
   17807       205380 :         gcc_assert (TREE_CODE (type) != METHOD_TYPE);
   17808       205380 :         if (TREE_CODE (type) == FUNCTION_TYPE)
   17809              :           {
   17810              :             /* The type of the implicit object parameter gets its
   17811              :                cv-qualifiers from the FUNCTION_TYPE. */
   17812       154630 :             tree memptr;
   17813       154630 :             tree method_type
   17814       154630 :               = build_memfn_type (type, r, type_memfn_quals (type),
   17815              :                                   type_memfn_rqual (type));
   17816       154630 :             memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
   17817       154630 :             return cp_build_qualified_type (memptr, cp_type_quals (t),
   17818       154630 :                                             complain);
   17819              :           }
   17820              :         else
   17821        50750 :           return cp_build_qualified_type (build_ptrmem_type (r, type),
   17822              :                                           cp_type_quals (t),
   17823        50750 :                                           complain);
   17824              :       }
   17825    174375499 :     case FUNCTION_TYPE:
   17826    174375499 :     case METHOD_TYPE:
   17827    174375499 :       {
   17828    174375499 :         tree fntype;
   17829    174375499 :         tree specs;
   17830    174375499 :         fntype = tsubst_function_type (t, args, complain, in_decl);
   17831    174364666 :         if (fntype == error_mark_node)
   17832              :           return error_mark_node;
   17833              : 
   17834              :         /* Substitute the exception specification.  */
   17835    172669750 :         specs = tsubst_exception_specification (t, args, complain, in_decl,
   17836              :                                                 /*defer_ok*/fndecl_type);
   17837    172669750 :         if (specs == error_mark_node)
   17838              :           return error_mark_node;
   17839    172669737 :         if (specs)
   17840     62269246 :           fntype = build_exception_variant (fntype, specs);
   17841              :         return fntype;
   17842              :       }
   17843     11257554 :     case ARRAY_TYPE:
   17844     11257554 :       {
   17845     11257554 :         tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
   17846     11257554 :         if (domain == error_mark_node)
   17847              :           return error_mark_node;
   17848              : 
   17849              :         /* As an optimization, we avoid regenerating the array type if
   17850              :            it will obviously be the same as T.  */
   17851     11257204 :         if (type == TREE_TYPE (t)
   17852      8763753 :             && domain == TYPE_DOMAIN (t)
   17853     19944560 :             && (TYPE_ATTRIBUTES (t) == NULL_TREE
   17854            3 :                 || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t))))
   17855              :           return t;
   17856              : 
   17857              :         /* These checks should match the ones in create_array_type_for_decl.
   17858              : 
   17859              :            [temp.deduct]
   17860              : 
   17861              :            The deduction may fail for any of the following reasons:
   17862              : 
   17863              :            -- Attempting to create an array with an element type that
   17864              :               is void, a function type, or a reference type, or [DR337]
   17865              :               an abstract class type.  */
   17866      2569851 :         if (VOID_TYPE_P (type)
   17867      2569851 :             || TREE_CODE (type) == FUNCTION_TYPE
   17868      2569841 :             || (TREE_CODE (type) == ARRAY_TYPE
   17869         1827 :                 && TYPE_DOMAIN (type) == NULL_TREE)
   17870      5139684 :             || TYPE_REF_P (type))
   17871              :           {
   17872           18 :             if (complain & tf_error)
   17873            3 :               error ("creating array of %qT", type);
   17874           18 :             return error_mark_node;
   17875              :           }
   17876              : 
   17877      2569833 :         if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
   17878      2569833 :                                   !(complain & tf_error)))
   17879            0 :           return error_mark_node;
   17880              : 
   17881      2569833 :         r = build_cplus_array_type (type, domain);
   17882              : 
   17883      2569833 :         if (!valid_array_size_p (input_location, r, in_decl,
   17884              :                                  (complain & tf_error)))
   17885           15 :           return error_mark_node;
   17886              : 
   17887      2569818 :         if (TYPE_USER_ALIGN (t))
   17888              :           {
   17889            6 :             SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
   17890            6 :             TYPE_USER_ALIGN (r) = 1;
   17891              :           }
   17892              : 
   17893      2569818 :         if (!apply_late_template_attributes (&r, TYPE_ATTRIBUTES (t),
   17894              :                                              /*flags=*/0,
   17895              :                                              args, complain, in_decl))
   17896            0 :           return error_mark_node;
   17897              : 
   17898      2569818 :         return r;
   17899              :       }
   17900              : 
   17901    246064128 :     case TYPENAME_TYPE:
   17902    246064128 :       {
   17903    246064128 :         tree ctx = TYPE_CONTEXT (t);
   17904    246064128 :         if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
   17905              :           {
   17906            6 :             ctx = tsubst_pack_expansion (ctx, args,
   17907              :                                          complain | tf_qualifying_scope,
   17908              :                                          in_decl);
   17909            6 :             if (ctx == error_mark_node)
   17910              :               return error_mark_node;
   17911            6 :             if (TREE_VEC_LENGTH (ctx) > 1)
   17912              :                 {
   17913            0 :                   if (complain & tf_error)
   17914            0 :                     error ("%qD expanded to more than one element",
   17915            0 :                            TYPENAME_TYPE_FULLNAME (t));
   17916            0 :                   return error_mark_node;
   17917              :                 }
   17918            6 :             if (TREE_VEC_LENGTH (ctx) == 0)
   17919              :               {
   17920            3 :                 if (complain & tf_error)
   17921            6 :                   error ("%qD is instantiated for an empty pack",
   17922            3 :                          TYPENAME_TYPE_FULLNAME (t));
   17923            3 :                 return error_mark_node;
   17924              :               }
   17925            3 :             ctx = TREE_VEC_ELT (ctx, 0);
   17926              :           }
   17927              :         else
   17928    246064122 :           ctx = tsubst_entering_scope (ctx, args,
   17929              :                                        complain | tf_qualifying_scope,
   17930              :                                        in_decl);
   17931    246064125 :         if (ctx == error_mark_node)
   17932              :           return error_mark_node;
   17933              : 
   17934    245975916 :         tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args,
   17935              :                               complain, in_decl);
   17936    245975916 :         if (f == error_mark_node)
   17937              :           return error_mark_node;
   17938              : 
   17939              :         /* We had [:X:]:: which was substituted into a NAMESPACE_DECL and not
   17940              :            a type.  */
   17941    245975614 :         if (TREE_CODE (ctx) == NAMESPACE_DECL)
   17942              :           {
   17943          106 :             tree id;
   17944          106 :             tree targs = NULL_TREE;
   17945          106 :             if (TREE_CODE (f) == TEMPLATE_ID_EXPR)
   17946              :               {
   17947           26 :                 id = TREE_OPERAND (f, 0);
   17948           26 :                 targs = TREE_OPERAND (f, 1);
   17949              :               }
   17950              :             else
   17951              :               id = f;
   17952              : 
   17953          106 :             gcc_checking_assert (identifier_p (id));
   17954          106 :             tree decl = lookup_qualified_name (ctx, id);
   17955          106 :             if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
   17956              :               {
   17957            6 :                 if (complain & tf_error)
   17958            6 :                   qualified_name_lookup_error (ctx, id, decl, input_location);
   17959            6 :                 return error_mark_node;
   17960              :               }
   17961          100 :             if (targs)
   17962              :               {
   17963           24 :                 f = lookup_template_class (decl, targs, in_decl, NULL_TREE,
   17964              :                                            complain);
   17965           24 :                 if (f == error_mark_node)
   17966              :                   return error_mark_node;
   17967              :               }
   17968           76 :             else if (TREE_CODE (decl) == NAMESPACE_DECL)
   17969              :               return decl;
   17970           54 :             else if (TREE_CODE (decl) == TYPE_DECL)
   17971           50 :               f = TREE_TYPE (decl);
   17972              :             else
   17973              :               {
   17974            4 :                 if (complain & tf_error)
   17975            4 :                   error ("%qD is not a type", decl);
   17976            4 :                 return error_mark_node;
   17977              :               }
   17978           70 :             return cp_build_qualified_type
   17979           70 :                     (f, cp_type_quals (f) | cp_type_quals (t), complain);
   17980              :           }
   17981              : 
   17982    243891132 :         if (!MAYBE_CLASS_TYPE_P (ctx))
   17983              :           {
   17984        13730 :             if (complain & tf_error)
   17985           67 :               error ("%qT is not a class, struct, or union type", ctx);
   17986        13730 :             return error_mark_node;
   17987              :           }
   17988    245961778 :         else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
   17989              :           {
   17990              :             /* Normally, make_typename_type does not require that the CTX
   17991              :                have complete type in order to allow things like:
   17992              : 
   17993              :                  template <class T> struct S { typename S<T>::X Y; };
   17994              : 
   17995              :                But, such constructs have already been resolved by this
   17996              :                point, so here CTX really should have complete type, unless
   17997              :                it's a partial instantiation.  */
   17998    216216528 :             if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
   17999         3210 :               return error_mark_node;
   18000              :           }
   18001              : 
   18002    245958568 :         enum tag_types tag_type = get_typename_tag (t);
   18003    245958568 :         tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
   18004    245958568 :         tcomplain |= tst_ok_flag | qualifying_scope_flag;
   18005    245958568 :         f = make_typename_type (ctx, f, tag_type, tcomplain);
   18006    245958568 :         if (f == error_mark_node)
   18007              :           return f;
   18008    226133255 :         if (TREE_CODE (f) == TYPE_DECL)
   18009              :           {
   18010    196108565 :             complain |= tf_ignore_bad_quals;
   18011    196108565 :             f = TREE_TYPE (f);
   18012              :           }
   18013              : 
   18014    226133255 :         if (!WILDCARD_TYPE_P (f))
   18015              :           {
   18016    196824739 :             if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
   18017              :               {
   18018            3 :                 if (complain & tf_error)
   18019            3 :                   error ("%qT resolves to %qT, which is not an enumeration type",
   18020              :                          t, f);
   18021              :                 else
   18022              :                   return error_mark_node;
   18023              :               }
   18024    193899169 :             else if (TYPENAME_IS_CLASS_OR_STRUCT_P (t)
   18025    196824796 :                      && !NON_UNION_CLASS_TYPE_P (f))
   18026              :               {
   18027           43 :                 if (complain & tf_error)
   18028            6 :                   error ("%qT resolves to %qT, which is not a non-union "
   18029              :                          "class type", t, f);
   18030              :                 else
   18031              :                   return error_mark_node;
   18032              :               }
   18033    196824693 :             else if (TYPENAME_IS_UNION_P (t) && !UNION_TYPE_P (f))
   18034              :               {
   18035            3 :                 if (complain & tf_error)
   18036            3 :                   error ("%qT resolves to %qT, which is not a union type",
   18037              :                          t, f);
   18038              :                 else
   18039              :                   return error_mark_node;
   18040              :               }
   18041              :           }
   18042              : 
   18043    226133218 :         return cp_build_qualified_type
   18044    226133218 :           (f, cp_type_quals (f) | cp_type_quals (t), complain);
   18045              :       }
   18046              : 
   18047       122544 :     case UNBOUND_CLASS_TEMPLATE:
   18048       122544 :       {
   18049       122544 :         tree name = TYPE_IDENTIFIER (t);
   18050       122544 :         if (name == error_mark_node)
   18051              :           return error_mark_node;
   18052              : 
   18053       122544 :         tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
   18054       122544 :         parm_list = tsubst_template_parms (parm_list, args, complain);
   18055       122544 :         if (parm_list == error_mark_node)
   18056              :           return error_mark_node;
   18057              : 
   18058       122559 :         if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
   18059           15 :           ++processing_template_decl;
   18060       122544 :         tree ctx = tsubst_entering_scope (TYPE_CONTEXT (t), args,
   18061              :                                           complain, in_decl);
   18062       122559 :         if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1)
   18063           15 :           --processing_template_decl;
   18064       122544 :         if (ctx == error_mark_node)
   18065              :           return error_mark_node;
   18066              : 
   18067              :         /* We had [:X:]::template NAME where X was substituted into
   18068              :            a NAMESPACE_DECL and not a type.  */
   18069       122544 :         if (TREE_CODE (ctx) == NAMESPACE_DECL)
   18070              :           {
   18071           20 :             tree decl = lookup_qualified_name (ctx, name);
   18072           20 :             if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
   18073              :               {
   18074            2 :                 if (complain & tf_error)
   18075            2 :                   qualified_name_lookup_error (ctx, name, decl, input_location);
   18076            2 :                 return error_mark_node;
   18077              :               }
   18078              :             /* As in make_unbound_class_template, we want only a class or
   18079              :                alias template.  */
   18080           18 :             if (!DECL_TYPE_TEMPLATE_P (decl))
   18081              :               {
   18082            4 :                 if (complain & tf_error)
   18083            4 :                   error ("%qD is not a class template", decl);
   18084            4 :                 return error_mark_node;
   18085              :               }
   18086              :             return decl;
   18087              :           }
   18088              : 
   18089       122524 :         return make_unbound_class_template (ctx, name, parm_list, complain);
   18090              :       }
   18091              : 
   18092          222 :     case TYPEOF_TYPE:
   18093          222 :       {
   18094          222 :         tree type;
   18095              : 
   18096          222 :         ++cp_unevaluated_operand;
   18097          222 :         ++c_inhibit_evaluation_warnings;
   18098              : 
   18099          222 :         type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
   18100              : 
   18101          222 :         --cp_unevaluated_operand;
   18102          222 :         --c_inhibit_evaluation_warnings;
   18103              : 
   18104          222 :         type = finish_typeof (type);
   18105          222 :         return cp_build_qualified_type (type,
   18106          222 :                                         cp_type_quals (t)
   18107          222 :                                         | cp_type_quals (type),
   18108          222 :                                         complain);
   18109              :       }
   18110              : 
   18111     30584097 :     case DECLTYPE_TYPE:
   18112     30584097 :       {
   18113     30584097 :         tree type;
   18114              : 
   18115     30584097 :         ++cp_unevaluated_operand;
   18116     30584097 :         ++c_inhibit_evaluation_warnings;
   18117              : 
   18118     30584097 :         type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
   18119              :                             complain|tf_decltype, in_decl);
   18120              : 
   18121     30573297 :         --cp_unevaluated_operand;
   18122     30573297 :         --c_inhibit_evaluation_warnings;
   18123              : 
   18124     30573297 :         if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
   18125       304792 :           type = lambda_capture_field_type (type,
   18126              :                                             false /*explicit_init*/,
   18127       152396 :                                             DECLTYPE_FOR_REF_CAPTURE (t));
   18128     30420901 :         else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
   18129           39 :           type = lambda_proxy_type (type);
   18130              :         else
   18131              :           {
   18132     30420862 :             bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
   18133       668657 :             if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
   18134     30420866 :                 && EXPR_P (type))
   18135              :               /* In a template ~id could be either a complement expression
   18136              :                  or an unqualified-id naming a destructor; if instantiating
   18137              :                  it produces an expression, it's not an id-expression or
   18138              :                  member access.  */
   18139              :               id = false;
   18140     30420862 :             type = finish_decltype_type (type, id, complain);
   18141              :           }
   18142     30573297 :         return cp_build_qualified_type (type,
   18143     30573297 :                                         cp_type_quals (t)
   18144     30573297 :                                         | cp_type_quals (type),
   18145     30573297 :                                         complain | tf_ignore_bad_quals);
   18146              :       }
   18147              : 
   18148      7921242 :     case TRAIT_TYPE:
   18149      7921242 :       {
   18150      7921242 :         tree type1 = TRAIT_TYPE_TYPE1 (t);
   18151      7921242 :         if (TYPE_P (type1))
   18152      7807870 :           type1 = tsubst (type1, args, complain, in_decl);
   18153              :         else
   18154       113372 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   18155      7921242 :         tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
   18156      7921242 :         type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
   18157      7921242 :         return cp_build_qualified_type (type,
   18158      7921242 :                                         cp_type_quals (t) | cp_type_quals (type),
   18159      7921242 :                                         complain | tf_ignore_bad_quals);
   18160              :       }
   18161              : 
   18162            0 :     case TYPE_ARGUMENT_PACK:
   18163            0 :     case NONTYPE_ARGUMENT_PACK:
   18164            0 :       return tsubst_argument_pack (t, args, complain, in_decl);
   18165              : 
   18166         6439 :     case PACK_INDEX_TYPE:
   18167         6439 :       return tsubst_pack_index (t, args, complain, in_decl);
   18168              : 
   18169          504 :     case SPLICE_SCOPE:
   18170          504 :       return tsubst_splice_scope (t, args, complain, in_decl);
   18171              : 
   18172          448 :     case SPLICE_EXPR:
   18173              :       /* We are coming from tsubst_splice_scope for [:R:]
   18174              :          where R needs to expand to a type or scope.  */
   18175          448 :       gcc_checking_assert (!SPLICE_EXPR_EXPRESSION_P (t));
   18176          448 :       return tsubst_splice_expr (t, args, complain, in_decl);
   18177              : 
   18178           56 :     case TEMPLATE_ID_EXPR:
   18179           56 :       {
   18180              :         /* We end up here coming from tsubst_splice_scope for
   18181              :            [:R:]<int>.  R needs to expand to a type or scope.  */
   18182           56 :         tree templ = TREE_OPERAND (t, 0);
   18183           56 :         gcc_assert (TREE_CODE (templ) == SPLICE_EXPR);
   18184           56 :         templ = tsubst_splice_expr (templ, args, complain, in_decl);
   18185           56 :         if (templ == error_mark_node)
   18186              :           return error_mark_node;
   18187           44 :         if (!DECL_TYPE_TEMPLATE_P (templ)
   18188           56 :             && !DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
   18189              :           {
   18190           12 :             if (complain & tf_error)
   18191              :               {
   18192            6 :                 auto_diagnostic_group d;
   18193           12 :                 error_at (cp_expr_loc_or_input_loc (TREE_OPERAND (t, 0)),
   18194              :                           "expected a reflection of a type template");
   18195            6 :                 inform_tree_category (templ);
   18196            6 :               }
   18197           12 :             return error_mark_node;
   18198              :           }
   18199           40 :         tree targs = TREE_OPERAND (t, 1);
   18200           40 :         if (targs)
   18201           40 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   18202           40 :         if (targs == error_mark_node)
   18203              :           return error_mark_node;
   18204           40 :         tree r = finish_template_type (templ, targs, /*entering_scope=*/false);
   18205           40 :         if (TREE_CODE (r) == TYPE_DECL)
   18206           36 :           r = TREE_TYPE (r);
   18207              :         return r;
   18208              :       }
   18209              : 
   18210            0 :     case VOID_CST:
   18211            0 :     case INTEGER_CST:
   18212            0 :     case REAL_CST:
   18213            0 :     case STRING_CST:
   18214            0 :     case PLUS_EXPR:
   18215            0 :     case MINUS_EXPR:
   18216            0 :     case NEGATE_EXPR:
   18217            0 :     case NOP_EXPR:
   18218            0 :     case INDIRECT_REF:
   18219            0 :     case ADDR_EXPR:
   18220            0 :     case CALL_EXPR:
   18221            0 :     case ARRAY_REF:
   18222            0 :     case SCOPE_REF:
   18223            0 :     case OMP_ARRAY_SECTION:
   18224              :       /* We should use one of the expression tsubsts for these codes.  */
   18225            0 :       gcc_unreachable ();
   18226              : 
   18227            0 :     default:
   18228            0 :       sorry ("use of %qs in template", get_tree_code_name (code));
   18229            0 :       return error_mark_node;
   18230              :     }
   18231              : }
   18232              : 
   18233              : /* Convenience wrapper over tsubst for substituting into the LHS
   18234              :    of the :: scope resolution operator.  */
   18235              : 
   18236              : static tree
   18237     84443537 : tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   18238              : {
   18239     84443537 :   gcc_checking_assert (TYPE_P (t) || TREE_CODE (t) == NAMESPACE_DECL);
   18240     84443537 :   return tsubst (t, args, complain | tf_qualifying_scope, in_decl);
   18241              : }
   18242              : 
   18243              : /* Convenience wrapper over tsubst for substituting into an id-expression
   18244              :    without resolving its terminal name.  */
   18245              : 
   18246              : static tree
   18247    349631494 : tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   18248              : {
   18249    267027045 :   return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl);
   18250              : }
   18251              : 
   18252              : /* OLDFNS is a lookup set of member functions from some class template, and
   18253              :    NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
   18254              :    of that class template.  Return the subset of NEWFNS which are
   18255              :    specializations of a function from OLDFNS.  */
   18256              : 
   18257              : static tree
   18258      4824157 : filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
   18259              : {
   18260              :   /* Record all member functions from the old lookup set OLDFNS into
   18261              :      VISIBLE_SET.  */
   18262      4824157 :   hash_set<tree> visible_set;
   18263      4824157 :   bool seen_dep_using = false;
   18264     14518935 :   for (tree fn : lkp_range (oldfns))
   18265              :     {
   18266      4870621 :       if (TREE_CODE (fn) == USING_DECL)
   18267              :         {
   18268              :           /* Imprecisely handle dependent using-decl by keeping all members
   18269              :              in the new lookup set that are defined in a base class, i.e.
   18270              :              members that could plausibly have been introduced by this
   18271              :              dependent using-decl.
   18272              :              FIXME: Track which members are introduced by a dependent
   18273              :              using-decl precisely, perhaps by performing another lookup
   18274              :              from the substituted USING_DECL_SCOPE.  */
   18275            0 :           gcc_checking_assert (DECL_DEPENDENT_P (fn));
   18276              :           seen_dep_using = true;
   18277              :         }
   18278              :       else
   18279      4870621 :         visible_set.add (fn);
   18280              :     }
   18281              : 
   18282              :   /* Returns true iff (a less specialized version of) FN appeared in
   18283              :      the old lookup set OLDFNS.  */
   18284      9698027 :   auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
   18285      4873870 :     if (DECL_CONTEXT (fn) != newtype)
   18286              :       /* FN is a member function from a base class, introduced via a
   18287              :          using-decl; if it might have been introduced by a dependent
   18288              :          using-decl then just conservatively keep it, otherwise look
   18289              :          in the old lookup set for FN exactly.  */
   18290           33 :       return seen_dep_using || visible_set.contains (fn);
   18291      4873837 :     else if (TREE_CODE (fn) == TEMPLATE_DECL)
   18292              :       /* FN is a member function template from the current class;
   18293              :          look in the old lookup set for the TEMPLATE_DECL from which
   18294              :          it was specialized.  */
   18295      2572439 :       return visible_set.contains (DECL_TI_TEMPLATE (fn));
   18296              :     else
   18297              :       /* FN is a non-template member function from the current class;
   18298              :          look in the old lookup set for the FUNCTION_DECL from which
   18299              :          it was specialized.  */
   18300      2301398 :       return visible_set.contains (DECL_TEMPLATE_RESULT
   18301      2301398 :                                    (DECL_TI_TEMPLATE (fn)));
   18302      4824157 :   };
   18303              : 
   18304      4824157 :   bool lookup_changed_p = false;
   18305     14518734 :   for (tree fn : lkp_range (newfns))
   18306      4870624 :     if (!visible_p (fn))
   18307              :       {
   18308              :         lookup_changed_p = true;
   18309              :         break;
   18310              :       }
   18311      4824157 :   if (!lookup_changed_p)
   18312              :     return newfns;
   18313              : 
   18314              :   /* Filter out from NEWFNS the member functions that weren't
   18315              :      previously visible according to OLDFNS.  */
   18316          204 :   tree filtered_fns = NULL_TREE;
   18317          204 :   unsigned filtered_size = 0;
   18318         3654 :   for (tree fn : lkp_range (newfns))
   18319         3246 :     if (visible_p (fn))
   18320              :       {
   18321          204 :         filtered_fns = lookup_add (fn, filtered_fns);
   18322          204 :         filtered_size++;
   18323              :       }
   18324          204 :   gcc_checking_assert (seen_dep_using
   18325              :                        ? filtered_size >= visible_set.elements ()
   18326              :                        : filtered_size == visible_set.elements ());
   18327              : 
   18328              :   return filtered_fns;
   18329      4824157 : }
   18330              : 
   18331              : /* tsubst a BASELINK.  OBJECT_TYPE, if non-NULL, is the type of the
   18332              :    expression on the left-hand side of the "." or "->" operator.  We
   18333              :    only do the lookup if we had a dependent BASELINK.  Otherwise we
   18334              :    adjust it onto the instantiated hierarchy.  */
   18335              : 
   18336              : static tree
   18337     21078701 : tsubst_baselink (tree baselink, tree object_type,
   18338              :                  tree args, tsubst_flags_t complain, tree in_decl)
   18339              : {
   18340     21078701 :   bool qualified_p = BASELINK_QUALIFIED_P (baselink);
   18341     21078701 :   tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
   18342     21078701 :   qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
   18343              : 
   18344     21078701 :   tree optype = BASELINK_OPTYPE (baselink);
   18345     21078701 :   optype = tsubst (optype, args, complain, in_decl);
   18346              : 
   18347     21078701 :   tree template_args = NULL_TREE;
   18348     21078701 :   bool template_id_p = false;
   18349     21078701 :   tree fns = BASELINK_FUNCTIONS (baselink);
   18350     21078701 :   if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
   18351              :     {
   18352      3884157 :       template_id_p = true;
   18353      3884157 :       template_args = TREE_OPERAND (fns, 1);
   18354      3884157 :       fns = TREE_OPERAND (fns, 0);
   18355      3884157 :       if (template_args)
   18356      3884157 :         template_args = tsubst_template_args (template_args, args,
   18357              :                                               complain, in_decl);
   18358      3884157 :       if (template_args == error_mark_node)
   18359              :         return error_mark_node;
   18360              :     }
   18361              : 
   18362     21078675 :   tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
   18363     21078675 :   binfo_type = tsubst (binfo_type, args, complain, in_decl);
   18364     21078675 :   bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
   18365     43151213 :                       || optype != BASELINK_OPTYPE (baselink));
   18366              : 
   18367              :   if (dependent_p)
   18368              :     {
   18369     15605724 :       tree name = OVL_NAME (fns);
   18370     15605724 :       if (IDENTIFIER_CONV_OP_P (name))
   18371           12 :         name = make_conv_op_name (optype);
   18372              : 
   18373              :       /* See maybe_dependent_member_ref.  */
   18374     15605724 :       if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
   18375              :         {
   18376       125839 :           if (template_id_p)
   18377       125197 :             name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
   18378              :                            template_args);
   18379       125839 :           return build_qualified_name (NULL_TREE, qualifying_scope, name,
   18380       125839 :                                        /* ::template */false);
   18381              :         }
   18382              : 
   18383     15479885 :       if (name == complete_dtor_identifier)
   18384              :         /* Treat as-if non-dependent below.  */
   18385            0 :         dependent_p = false;
   18386              : 
   18387     15479885 :       bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
   18388     15479885 :       baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
   18389              :                                   complain);
   18390     15479885 :       if (!baselink)
   18391              :         {
   18392            6 :           if (complain & tf_error)
   18393              :             {
   18394            6 :               if (constructor_name_p (name, qualifying_scope))
   18395            0 :                 error ("cannot call constructor %<%T::%D%> directly",
   18396              :                        qualifying_scope, name);
   18397              :               else
   18398              :                 /* Lookup succeeded at parse time, but failed during
   18399              :                    instantiation; must be because we're trying to refer to it
   18400              :                    while forming its declaration (c++/120204).  */
   18401            6 :                 error ("declaration of %<%T::%D%> depends on itself",
   18402              :                        qualifying_scope, name);
   18403              :             }
   18404            6 :           return error_mark_node;
   18405              :         }
   18406              : 
   18407     15479879 :       if (maybe_incomplete)
   18408              :         {
   18409              :           /* Filter out from the new lookup set those functions which didn't
   18410              :              appear in the original lookup set (in a less specialized form).
   18411              :              This is needed to preserve the consistency of member lookup
   18412              :              performed in an incomplete-class context, within which
   18413              :              later-declared members ought to remain invisible.  */
   18414      4824157 :           BASELINK_FUNCTIONS (baselink)
   18415      4824157 :             = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
   18416              :                                    binfo_type);
   18417      4824157 :           BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
   18418              :         }
   18419              : 
   18420     15479879 :       fns = BASELINK_FUNCTIONS (baselink);
   18421              :     }
   18422              :   else
   18423              :     {
   18424              :       /* We're going to overwrite pieces below, make a duplicate.  */
   18425      5472951 :       baselink = copy_node (baselink);
   18426              : 
   18427      5472951 :       if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
   18428              :         {
   18429              :           /* The decl we found was from non-dependent scope, but we still need
   18430              :              to update the binfos for the instantiated qualifying_scope.  */
   18431       173362 :           BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
   18432       173362 :           BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
   18433              :                                                    ba_unique, nullptr, complain);
   18434              :         }
   18435              :     }
   18436              : 
   18437              :   /* If lookup found a single function, mark it as used at this point.
   18438              :      (If lookup found multiple functions the one selected later by
   18439              :      overload resolution will be marked as used at that point.)  */
   18440     20952830 :   if (!template_id_p && !really_overloaded_fn (fns))
   18441              :     {
   18442     13180401 :       tree fn = OVL_FIRST (fns);
   18443     13180401 :       bool ok = mark_used (fn, complain);
   18444     13180401 :       if (!ok && !(complain & tf_error))
   18445            6 :         return error_mark_node;
   18446     13180392 :       if (ok && BASELINK_P (baselink))
   18447              :         /* We might have instantiated an auto function.  */
   18448     13180392 :         TREE_TYPE (baselink) = TREE_TYPE (fn);
   18449              :     }
   18450              : 
   18451     20952824 :   if (BASELINK_P (baselink))
   18452              :     {
   18453              :       /* Add back the template arguments, if present.  */
   18454     20952824 :       if (template_id_p)
   18455      7517868 :         BASELINK_FUNCTIONS (baselink)
   18456      3758934 :           = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
   18457              : 
   18458              :       /* Update the conversion operator type.  */
   18459     20952824 :       BASELINK_OPTYPE (baselink) = optype;
   18460              :     }
   18461              : 
   18462     20952824 :   if (!object_type)
   18463       546882 :     object_type = current_class_type;
   18464              : 
   18465     20952824 :   if (qualified_p || !dependent_p)
   18466              :     {
   18467      5516059 :       baselink = adjust_result_of_qualified_name_lookup (baselink,
   18468              :                                                          qualifying_scope,
   18469              :                                                          object_type);
   18470      5516059 :       if (!qualified_p)
   18471              :         /* We need to call adjust_result_of_qualified_name_lookup in case the
   18472              :            destructor names a base class, but we unset BASELINK_QUALIFIED_P
   18473              :            so that we still get virtual function binding.  */
   18474      5372845 :         BASELINK_QUALIFIED_P (baselink) = false;
   18475              :     }
   18476              : 
   18477              :   return baselink;
   18478              : }
   18479              : 
   18480              : /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
   18481              :    true if the qualified-id will be a postfix-expression in-and-of
   18482              :    itself; false if more of the postfix-expression follows the
   18483              :    QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
   18484              :    of "&".  REFLECTING_P is true if this SCOPE_REF is an operand of ^^.  */
   18485              : 
   18486              : static tree
   18487     83829468 : tsubst_qualified_id (tree qualified_id, tree args,
   18488              :                      tsubst_flags_t complain, tree in_decl,
   18489              :                      bool done, bool address_p, bool reflecting_p = false)
   18490              : {
   18491     83829468 :   tree expr;
   18492     83829468 :   tree scope;
   18493     83829468 :   tree name;
   18494     83829468 :   bool is_template;
   18495     83829468 :   tree template_args;
   18496     83829468 :   location_t loc = EXPR_LOCATION (qualified_id);
   18497              : 
   18498     83829468 :   gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
   18499              : 
   18500              :   /* Figure out what name to look up.  */
   18501     83829468 :   name = TREE_OPERAND (qualified_id, 1);
   18502     83829468 :   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
   18503              :     {
   18504        68427 :       is_template = true;
   18505        68427 :       template_args = TREE_OPERAND (name, 1);
   18506        68427 :       if (template_args)
   18507        68427 :         template_args = tsubst_template_args (template_args, args,
   18508              :                                               complain, in_decl);
   18509        68427 :       if (template_args == error_mark_node)
   18510              :         return error_mark_node;
   18511        68417 :       name = TREE_OPERAND (name, 0);
   18512              :     }
   18513              :   else
   18514              :     {
   18515              :       is_template = false;
   18516              :       template_args = NULL_TREE;
   18517              :     }
   18518              : 
   18519              :   /* Substitute into the qualifying scope.  When there are no ARGS, we
   18520              :      are just trying to simplify a non-dependent expression.  In that
   18521              :      case the qualifying scope may be dependent, and, in any case,
   18522              :      substituting will not help.  */
   18523     83829458 :   scope = TREE_OPERAND (qualified_id, 0);
   18524     83829458 :   if (args)
   18525              :     {
   18526     82604449 :       scope = tsubst_scope (scope, args, complain, in_decl);
   18527     82604449 :       expr = tsubst_name (name, args, complain, in_decl);
   18528              :     }
   18529              :   else
   18530              :     expr = name;
   18531              : 
   18532     83829458 :   if (dependent_scope_p (scope) || dependent_namespace_p (scope))
   18533              :     {
   18534     11575431 :       if (TREE_CODE (expr) == SCOPE_REF)
   18535              :         /* We built one in tsubst_baselink.  */
   18536            6 :         gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
   18537              :       else
   18538              :         {
   18539     11575425 :           if (is_template)
   18540        12148 :             expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
   18541              :                                      template_args);
   18542     11575425 :           expr = build_qualified_name (NULL_TREE, scope, expr,
   18543     11575425 :                                        QUALIFIED_NAME_IS_TEMPLATE
   18544              :                                        (qualified_id));
   18545              :         }
   18546     11575431 :       REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
   18547     11575431 :       return expr;
   18548              :     }
   18549              : 
   18550     72254027 :   if (!BASELINK_P (name) && !DECL_P (expr))
   18551              :     {
   18552     66131781 :       if (TREE_CODE (expr) == BIT_NOT_EXPR)
   18553              :         {
   18554              :           /* A BIT_NOT_EXPR is used to represent a destructor.  */
   18555            3 :           if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
   18556              :             {
   18557            0 :               error ("qualifying type %qT does not match destructor name ~%qT",
   18558            0 :                      scope, TREE_OPERAND (expr, 0));
   18559            0 :               expr = error_mark_node;
   18560              :             }
   18561              :           else
   18562            3 :             expr = lookup_qualified_name (scope, complete_dtor_identifier,
   18563              :                                           LOOK_want::NORMAL, false);
   18564              :         }
   18565              :       else
   18566     66131778 :         expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
   18567     66131727 :       if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
   18568              :                      ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL
   18569              :           /* For ^^T::X, we'll take both types and non-types.  */
   18570     66131727 :           && !reflecting_p)
   18571              :         {
   18572           30 :           if (complain & tf_error)
   18573              :             {
   18574           24 :               auto_diagnostic_group d;
   18575           24 :               error ("dependent-name %qE is parsed as a non-type, but "
   18576              :                      "instantiation yields a type", qualified_id);
   18577           24 :               inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
   18578           24 :             }
   18579           30 :           return error_mark_node;
   18580              :         }
   18581              :     }
   18582              : 
   18583     72253943 :   if (DECL_P (expr))
   18584              :     {
   18585     67121238 :       if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
   18586              :                                                 scope, complain))
   18587           21 :         return error_mark_node;
   18588              :       /* Remember that there was a reference to this entity.  */
   18589     67121217 :       if (!mark_used (expr, complain) && !(complain & tf_error))
   18590            0 :         return error_mark_node;
   18591              :     }
   18592              : 
   18593     72245831 :   if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
   18594              :     {
   18595        61348 :       if (complain & tf_error)
   18596          167 :         qualified_name_lookup_error (scope,
   18597          167 :                                      TREE_OPERAND (qualified_id, 1),
   18598              :                                      expr, input_location);
   18599        61348 :       return error_mark_node;
   18600              :     }
   18601              : 
   18602     72184483 :   if (is_template)
   18603              :     {
   18604        51061 :       if (variable_template_p (expr))
   18605         9871 :         expr = lookup_and_finish_template_variable (expr, template_args,
   18606              :                                                     complain);
   18607              :       else
   18608        41190 :         expr = lookup_template_function (expr, template_args);
   18609              :     }
   18610              : 
   18611     72184483 :   if (expr == error_mark_node && complain & tf_error)
   18612            3 :     qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
   18613              :                                  expr, input_location);
   18614              :   /* For ^^S::mem, we do not want to create the dummy object that
   18615              :      finish_non_static_data_member would give us.  */
   18616     72184480 :   else if (TYPE_P (scope) && !reflecting_p)
   18617              :     {
   18618     72184376 :       expr = (adjust_result_of_qualified_name_lookup
   18619     72184376 :               (expr, scope, current_nonlambda_class_type ()));
   18620     72184376 :       expr = (finish_qualified_id_expr
   18621     72184382 :               (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
   18622     72184376 :                QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
   18623              :                /*template_arg_p=*/false, complain));
   18624              :     }
   18625              : 
   18626              :   /* Expressions do not generally have reference type.  */
   18627     72184483 :   if (TREE_CODE (expr) != SCOPE_REF
   18628              :       /* However, if we're about to form a pointer-to-member, we just
   18629              :          want the referenced member referenced.  */
   18630     68956367 :       && TREE_CODE (expr) != OFFSET_REF)
   18631     68925624 :     expr = convert_from_reference (expr);
   18632              : 
   18633     72184483 :   if (REF_PARENTHESIZED_P (qualified_id))
   18634          241 :     expr = force_paren_expr (expr);
   18635              : 
   18636     72184483 :   expr = maybe_wrap_with_location (expr, loc);
   18637              : 
   18638     72184483 :   return expr;
   18639              : }
   18640              : 
   18641              : /* tsubst the initializer for a VAR_DECL.  INIT is the unsubstituted
   18642              :    initializer, DECL is the substituted VAR_DECL.  Other arguments are as
   18643              :    for tsubst.  */
   18644              : 
   18645              : static tree
   18646     39258398 : tsubst_init (tree init, tree decl, tree args,
   18647              :              tsubst_flags_t complain, tree in_decl)
   18648              : {
   18649     39258398 :   if (!init)
   18650              :     return NULL_TREE;
   18651              : 
   18652     33380668 :   init = tsubst_expr (init, args, complain, in_decl);
   18653              : 
   18654     33380668 :   tree type = TREE_TYPE (decl);
   18655              : 
   18656     33380668 :   if (!init && type != error_mark_node)
   18657              :     {
   18658           74 :       if (tree auto_node = type_uses_auto (type))
   18659              :         {
   18660            8 :           if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   18661              :             {
   18662            6 :               if (complain & tf_error)
   18663            6 :                 error ("initializer for %q#D expands to an empty list "
   18664              :                        "of expressions", decl);
   18665            6 :               return error_mark_node;
   18666              :             }
   18667              :         }
   18668           66 :       else if (!dependent_type_p (type))
   18669              :         {
   18670              :           /* If we had an initializer but it
   18671              :              instantiated to nothing,
   18672              :              value-initialize the object.  This will
   18673              :              only occur when the initializer was a
   18674              :              pack expansion where the parameter packs
   18675              :              used in that expansion were of length
   18676              :              zero.  */
   18677           66 :           init = build_value_init (type, complain);
   18678           66 :           if (TREE_CODE (init) == AGGR_INIT_EXPR)
   18679           45 :             init = get_target_expr (init, complain);
   18680           66 :           if (TREE_CODE (init) == TARGET_EXPR)
   18681           45 :             TARGET_EXPR_DIRECT_INIT_P (init) = true;
   18682              :         }
   18683              :     }
   18684              : 
   18685              :   return init;
   18686              : }
   18687              : 
   18688              : /* If T is a reference to a dependent member of the current instantiation C and
   18689              :    we are trying to refer to that member in a partial instantiation of C,
   18690              :    return a SCOPE_REF; otherwise, return NULL_TREE.
   18691              : 
   18692              :    This can happen when forming a C++17 deduction guide, as in PR96199.  */
   18693              : 
   18694              : static tree
   18695   8154442601 : maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
   18696              :                             tree in_decl)
   18697              : {
   18698   8154442601 :   if (!(complain & tf_dguide))
   18699              :     return NULL_TREE;
   18700              : 
   18701      2363228 :   tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
   18702      2363228 :   if (!decl || !DECL_P (decl))
   18703              :     return NULL_TREE;
   18704              : 
   18705      1299110 :   tree ctx = context_for_name_lookup (decl);
   18706      1299110 :   if (!CLASS_TYPE_P (ctx))
   18707              :     return NULL_TREE;
   18708              : 
   18709        62132 :   ctx = tsubst (ctx, args, complain, in_decl);
   18710        62132 :   if (!dependent_scope_p (ctx))
   18711              :     return NULL_TREE;
   18712              : 
   18713        55304 :   if (TYPE_P (t))
   18714              :     {
   18715        33540 :       bool stripped = false;
   18716        33540 :       if (typedef_variant_p (t))
   18717              :         {
   18718              :           /* Since this transformation may undesirably turn a deduced context
   18719              :              into a non-deduced one, we'd rather strip typedefs than perform
   18720              :              the transformation.  */
   18721        18674 :           tree u = strip_typedefs (t);
   18722        18674 :           if (u != t)
   18723              :             {
   18724        33540 :               stripped = true;
   18725        33540 :               t = u;
   18726              :             }
   18727              :         }
   18728        33540 :       decl = TYPE_NAME (t);
   18729        33540 :       if (decl)
   18730        32852 :         decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
   18731        32852 :       if (!decl)
   18732              :         {
   18733        29630 :           if (stripped)
   18734              :             /* The original type was an alias from the current instantiation
   18735              :                which we stripped to something outside it.  At this point we
   18736              :                need to commit to using the stripped type rather than deferring
   18737              :                to the caller (which would use the original type), to ensure
   18738              :                eligible bits of the stripped type get transformed.  */
   18739        15662 :             return tsubst (t, args, complain, in_decl);
   18740              :           else
   18741              :             /* The original type wasn't a typedef, and we decided it doesn't
   18742              :                need rewriting, so just let the caller (tsubst) substitute it
   18743              :                normally.  */
   18744              :             return NULL_TREE;
   18745              :         }
   18746         3910 :       return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
   18747         3910 :                                       complain);
   18748              :     }
   18749              : 
   18750        21764 :   tree name = DECL_NAME (t);
   18751        21764 :   tree fullname = name;
   18752        21764 :   if (instantiates_primary_template_p (t))
   18753              :     {
   18754         3020 :       tree tinfo = get_template_info (t);
   18755         3020 :       name = DECL_NAME (TI_TEMPLATE (tinfo));
   18756         3020 :       tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
   18757         3020 :       targs = tsubst_template_args (targs, args, complain, in_decl);
   18758         3020 :       fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
   18759              :     }
   18760              : 
   18761        21764 :   if (TREE_CODE (t) == TYPE_DECL)
   18762              :     {
   18763        19477 :       if (!is_typedef_decl (t)
   18764        16465 :           && TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
   18765        15567 :           && TYPE_NAME (TREE_TYPE (t)) == t)
   18766              :         /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
   18767              :            scope, but it doesn't need to be rewritten again.  */
   18768              :         return NULL_TREE;
   18769         3910 :       tree type = build_typename_type (ctx, name, fullname, typename_type);
   18770         3910 :       return TYPE_NAME (type);
   18771              :     }
   18772         2287 :   else if (DECL_TYPE_TEMPLATE_P (t))
   18773           24 :     return make_unbound_class_template (ctx, name,
   18774           24 :                                         NULL_TREE, complain);
   18775              :   else
   18776         2263 :     return build_qualified_name (NULL_TREE, ctx, fullname,
   18777         2263 :                                  TREE_CODE (t) == TEMPLATE_DECL);
   18778              : }
   18779              : 
   18780              : /* Helper function for tsubst_omp_clauses, used for instantiation of
   18781              :    OMP_CLAUSE_DECL of clauses.  */
   18782              : 
   18783              : static tree
   18784         6479 : tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
   18785              :                         tree in_decl, tree *iterator_cache)
   18786              : {
   18787         6479 :   if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
   18788              :     return decl;
   18789              : 
   18790              :   /* Handle OpenMP iterators.  */
   18791         6431 :   if (OMP_ITERATOR_DECL_P (decl))
   18792              :     {
   18793          135 :       tree ret;
   18794          135 :       if (iterator_cache[0] == TREE_PURPOSE (decl))
   18795           20 :         ret = iterator_cache[1];
   18796              :       else
   18797              :         {
   18798          115 :           tree *tp = &ret;
   18799          115 :           begin_scope (sk_omp, NULL);
   18800          254 :           for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
   18801              :             {
   18802          139 :               *tp = copy_node (it);
   18803          139 :               OMP_ITERATOR_VAR (*tp)
   18804          139 :                 = tsubst_decl (OMP_ITERATOR_VAR (it), args, complain);
   18805          139 :               DECL_CONTEXT (OMP_ITERATOR_VAR (*tp)) = current_function_decl;
   18806          139 :               pushdecl (OMP_ITERATOR_VAR (*tp));
   18807          139 :               OMP_ITERATOR_BEGIN (*tp)
   18808          139 :                 = tsubst_stmt (OMP_ITERATOR_BEGIN (it), args, complain, in_decl);
   18809          139 :               OMP_ITERATOR_END (*tp)
   18810          139 :                 = tsubst_stmt (OMP_ITERATOR_END (it), args, complain, in_decl);
   18811          139 :               OMP_ITERATOR_STEP (*tp)
   18812          139 :                 = tsubst_stmt (OMP_ITERATOR_STEP (it), args, complain, in_decl);
   18813          139 :               TREE_CHAIN (*tp) = NULL_TREE;
   18814          139 :               tp = &TREE_CHAIN (*tp);
   18815              :             }
   18816          115 :           TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
   18817          115 :           iterator_cache[0] = TREE_PURPOSE (decl);
   18818          115 :           iterator_cache[1] = ret;
   18819              :         }
   18820          135 :       return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
   18821              :                                                            args, complain,
   18822              :                                                            in_decl, NULL));
   18823              :     }
   18824              : 
   18825              :   /* Handle an OpenMP array section represented as a TREE_LIST (or
   18826              :      OMP_CLAUSE_DOACROSS_KIND).  An OMP_CLAUSE_DOACROSS (with a depend
   18827              :      kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
   18828              :      TREE_LIST.  We can handle it exactly the same as an array section
   18829              :      (purpose, value, and a chain), even though the nomenclature
   18830              :      (low_bound, length, etc) is different.  */
   18831         6296 :   if (TREE_CODE (decl) == TREE_LIST)
   18832              :     {
   18833           27 :       tree low_bound
   18834           27 :         = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl);
   18835           27 :       tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl);
   18836           27 :       tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
   18837              :                                            in_decl, NULL);
   18838           27 :       if (TREE_PURPOSE (decl) == low_bound
   18839           27 :           && TREE_VALUE (decl) == length
   18840           36 :           && TREE_CHAIN (decl) == chain)
   18841              :         return decl;
   18842           18 :       tree ret = tree_cons (low_bound, length, chain);
   18843           18 :       OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
   18844           18 :         = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
   18845           18 :       return ret;
   18846              :     }
   18847         6269 :   else if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
   18848              :     {
   18849         1912 :       tree low_bound
   18850         1912 :         = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl);
   18851         1912 :       tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain,
   18852              :                                  in_decl);
   18853         1912 :       tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args,
   18854              :                                            complain, in_decl, NULL);
   18855         1912 :       if (TREE_OPERAND (decl, 0) == base
   18856          594 :           && TREE_OPERAND (decl, 1) == low_bound
   18857         2394 :           && TREE_OPERAND (decl, 2) == length)
   18858              :         return decl;
   18859         1439 :       return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound,
   18860         1439 :                      length);
   18861              :     }
   18862         4357 :   tree ret = tsubst_stmt (decl, args, complain, in_decl);
   18863              :   /* Undo convert_from_reference tsubst_expr could have called.  */
   18864         4357 :   if (decl
   18865         4357 :       && REFERENCE_REF_P (ret)
   18866          561 :       && !REFERENCE_REF_P (decl))
   18867          551 :     ret = TREE_OPERAND (ret, 0);
   18868              :   return ret;
   18869              : }
   18870              : 
   18871              : /* Like tsubst_copy, but specifically for OpenMP clauses.  */
   18872              : 
   18873              : static tree
   18874         4930 : tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
   18875              :                     tree args, tsubst_flags_t complain, tree in_decl)
   18876              : {
   18877         4930 :   tree new_clauses = NULL_TREE, nc, oc;
   18878         4930 :   tree linear_no_step = NULL_TREE;
   18879         4930 :   tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
   18880              : 
   18881        11087 :   for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
   18882              :     {
   18883         6157 :       nc = copy_node (oc);
   18884         6157 :       OMP_CLAUSE_CHAIN (nc) = new_clauses;
   18885         6157 :       new_clauses = nc;
   18886              : 
   18887         6157 :       switch (OMP_CLAUSE_CODE (nc))
   18888              :         {
   18889          190 :         case OMP_CLAUSE_LASTPRIVATE:
   18890          190 :           if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
   18891              :             {
   18892           38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
   18893           38 :               tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
   18894              :                            complain, in_decl);
   18895           38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc)
   18896           76 :                 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
   18897              :             }
   18898              :           /* FALLTHRU */
   18899         2963 :         case OMP_CLAUSE_PRIVATE:
   18900         2963 :         case OMP_CLAUSE_SHARED:
   18901         2963 :         case OMP_CLAUSE_FIRSTPRIVATE:
   18902         2963 :         case OMP_CLAUSE_COPYIN:
   18903         2963 :         case OMP_CLAUSE_COPYPRIVATE:
   18904         2963 :         case OMP_CLAUSE_UNIFORM:
   18905         2963 :         case OMP_CLAUSE_DEPEND:
   18906         2963 :         case OMP_CLAUSE_DOACROSS:
   18907         2963 :         case OMP_CLAUSE_AFFINITY:
   18908         2963 :         case OMP_CLAUSE_FROM:
   18909         2963 :         case OMP_CLAUSE_TO:
   18910         2963 :         case OMP_CLAUSE_MAP:
   18911         2963 :         case OMP_CLAUSE__CACHE_:
   18912         2963 :         case OMP_CLAUSE_NONTEMPORAL:
   18913         2963 :         case OMP_CLAUSE_USE_DEVICE_PTR:
   18914         2963 :         case OMP_CLAUSE_USE_DEVICE_ADDR:
   18915         2963 :         case OMP_CLAUSE_IS_DEVICE_PTR:
   18916         2963 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
   18917         2963 :         case OMP_CLAUSE_INCLUSIVE:
   18918         2963 :         case OMP_CLAUSE_EXCLUSIVE:
   18919         5926 :           OMP_CLAUSE_DECL (nc)
   18920         2963 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18921              :                                       in_decl, iterator_cache);
   18922         2963 :           break;
   18923          150 :         case OMP_CLAUSE_NUM_TEAMS:
   18924          150 :           if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
   18925           66 :             OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
   18926          132 :               = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
   18927              :                              complain, in_decl);
   18928              :           /* FALLTHRU */
   18929          762 :         case OMP_CLAUSE_TILE:
   18930          762 :         case OMP_CLAUSE_IF:
   18931          762 :         case OMP_CLAUSE_SELF:
   18932          762 :         case OMP_CLAUSE_NUM_THREADS:
   18933          762 :         case OMP_CLAUSE_SCHEDULE:
   18934          762 :         case OMP_CLAUSE_COLLAPSE:
   18935          762 :         case OMP_CLAUSE_FINAL:
   18936          762 :         case OMP_CLAUSE_DEVICE:
   18937          762 :         case OMP_CLAUSE_DIST_SCHEDULE:
   18938          762 :         case OMP_CLAUSE_THREAD_LIMIT:
   18939          762 :         case OMP_CLAUSE_SAFELEN:
   18940          762 :         case OMP_CLAUSE_SIMDLEN:
   18941          762 :         case OMP_CLAUSE_NUM_TASKS:
   18942          762 :         case OMP_CLAUSE_GRAINSIZE:
   18943          762 :         case OMP_CLAUSE_PRIORITY:
   18944          762 :         case OMP_CLAUSE_ORDERED:
   18945          762 :         case OMP_CLAUSE_HINT:
   18946          762 :         case OMP_CLAUSE_FILTER:
   18947          762 :         case OMP_CLAUSE_NUM_GANGS:
   18948          762 :         case OMP_CLAUSE_NUM_WORKERS:
   18949          762 :         case OMP_CLAUSE_VECTOR_LENGTH:
   18950          762 :         case OMP_CLAUSE_WORKER:
   18951          762 :         case OMP_CLAUSE_VECTOR:
   18952          762 :         case OMP_CLAUSE_ASYNC:
   18953          762 :         case OMP_CLAUSE_WAIT:
   18954          762 :         case OMP_CLAUSE_DETACH:
   18955          762 :         case OMP_CLAUSE_DYN_GROUPPRIVATE:
   18956          762 :           OMP_CLAUSE_OPERAND (nc, 0)
   18957          762 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   18958          762 :           break;
   18959           18 :         case OMP_CLAUSE_PARTIAL:
   18960           18 :           OMP_CLAUSE_PARTIAL_EXPR (nc)
   18961           18 :             = tsubst_expr (OMP_CLAUSE_PARTIAL_EXPR (oc), args, complain,
   18962              :                            in_decl);
   18963           18 :           break;
   18964           36 :         case OMP_CLAUSE_SIZES:
   18965           36 :           OMP_CLAUSE_SIZES_LIST (nc)
   18966           36 :             = tsubst_expr (OMP_CLAUSE_SIZES_LIST (oc), args, complain,
   18967              :                            in_decl);
   18968           36 :           break;
   18969            6 :         case OMP_CLAUSE_NOCONTEXT:
   18970            6 :           OMP_CLAUSE_NOCONTEXT_EXPR (nc)
   18971            6 :             = tsubst_expr (OMP_CLAUSE_NOCONTEXT_EXPR (oc), args, complain,
   18972              :                            in_decl);
   18973            6 :           break;
   18974            6 :         case OMP_CLAUSE_NOVARIANTS:
   18975            6 :           OMP_CLAUSE_NOVARIANTS_EXPR (nc)
   18976            6 :             = tsubst_expr (OMP_CLAUSE_NOVARIANTS_EXPR (oc), args, complain,
   18977              :                            in_decl);
   18978            6 :           break;
   18979          956 :         case OMP_CLAUSE_REDUCTION:
   18980          956 :         case OMP_CLAUSE_IN_REDUCTION:
   18981          956 :         case OMP_CLAUSE_TASK_REDUCTION:
   18982          956 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
   18983              :             {
   18984           45 :               tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
   18985           45 :               if (TREE_CODE (placeholder) == SCOPE_REF)
   18986              :                 {
   18987            5 :                   tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
   18988              :                                        complain, in_decl);
   18989            5 :                   OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
   18990           10 :                     = build_qualified_name (NULL_TREE, scope,
   18991            5 :                                             TREE_OPERAND (placeholder, 1),
   18992              :                                             false);
   18993              :                 }
   18994              :               else
   18995           40 :                 gcc_assert (identifier_p (placeholder));
   18996              :             }
   18997         1912 :           OMP_CLAUSE_DECL (nc)
   18998          956 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   18999              :                                       in_decl, NULL);
   19000          956 :           break;
   19001           66 :         case OMP_CLAUSE_GANG:
   19002           66 :         case OMP_CLAUSE_ALIGNED:
   19003          132 :           OMP_CLAUSE_DECL (nc)
   19004           66 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   19005              :                                       in_decl, NULL);
   19006           66 :           OMP_CLAUSE_OPERAND (nc, 1)
   19007           66 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   19008           66 :           break;
   19009          293 :         case OMP_CLAUSE_ALLOCATE:
   19010          586 :           OMP_CLAUSE_DECL (nc)
   19011          293 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   19012              :                                       in_decl, NULL);
   19013          293 :           OMP_CLAUSE_OPERAND (nc, 1)
   19014          293 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   19015          293 :           OMP_CLAUSE_OPERAND (nc, 2)
   19016          293 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   19017          293 :           break;
   19018          127 :         case OMP_CLAUSE_LINEAR:
   19019          254 :           OMP_CLAUSE_DECL (nc)
   19020          127 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   19021              :                                       in_decl, NULL);
   19022          127 :           if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
   19023              :             {
   19024            2 :               gcc_assert (!linear_no_step);
   19025              :               linear_no_step = nc;
   19026              :             }
   19027          125 :           else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
   19028            0 :             OMP_CLAUSE_LINEAR_STEP (nc)
   19029            0 :               = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
   19030              :                                         complain, in_decl, NULL);
   19031              :           else
   19032          125 :             OMP_CLAUSE_LINEAR_STEP (nc)
   19033          250 :               = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args,
   19034              :                              complain, in_decl);
   19035              :           break;
   19036            6 :         case OMP_CLAUSE_USES_ALLOCATORS:
   19037            6 :           OMP_CLAUSE_OPERAND (nc, 0)
   19038            6 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   19039            6 :           OMP_CLAUSE_OPERAND (nc, 1)
   19040            6 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   19041            6 :           OMP_CLAUSE_OPERAND (nc, 2)
   19042            6 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   19043            6 :           break;
   19044          138 :         case OMP_CLAUSE_INIT:
   19045          138 :           if (ort == C_ORT_OMP_INTEROP
   19046          138 :               && OMP_CLAUSE_INIT_PREFER_TYPE (nc)
   19047           66 :               && TREE_CODE (OMP_CLAUSE_INIT_PREFER_TYPE (nc)) == TREE_LIST
   19048          192 :               && (OMP_CLAUSE_CHAIN (nc)  == NULL_TREE
   19049           42 :                   || OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (nc) ) != OMP_CLAUSE_INIT
   19050           36 :                   || (OMP_CLAUSE_INIT_PREFER_TYPE (nc)
   19051           36 :                       != OMP_CLAUSE_INIT_PREFER_TYPE (OMP_CLAUSE_CHAIN (nc) ))))
   19052              :             {
   19053           30 :               tree pref_list = OMP_CLAUSE_INIT_PREFER_TYPE (nc);
   19054           30 :               tree fr_list = TREE_VALUE (pref_list);
   19055           30 :               int len = TREE_VEC_LENGTH (fr_list);
   19056          144 :               for (int i = 0; i < len; i++)
   19057              :                 {
   19058          114 :                   tree *fr_expr = &TREE_VEC_ELT (fr_list, i);
   19059              :                   /* Preserve NOP_EXPR to have a location.  */
   19060          114 :                   if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR)
   19061           42 :                     TREE_OPERAND (*fr_expr, 0)
   19062           84 :                       = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain,
   19063              :                                      in_decl);
   19064              :                   else
   19065           72 :                     *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl);
   19066              :                 }
   19067              :             }
   19068              :           /* FALLTHRU */
   19069          192 :         case OMP_CLAUSE_DESTROY:
   19070          192 :         case OMP_CLAUSE_USE:
   19071          192 :         case OMP_CLAUSE_INTEROP:
   19072          192 :           OMP_CLAUSE_OPERAND (nc, 0)
   19073          192 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   19074          192 :           break;
   19075              :         case OMP_CLAUSE_NOWAIT:
   19076              :         case OMP_CLAUSE_DEFAULT:
   19077              :         case OMP_CLAUSE_DEVICE_TYPE:
   19078              :         case OMP_CLAUSE_UNTIED:
   19079              :         case OMP_CLAUSE_MERGEABLE:
   19080              :         case OMP_CLAUSE_INBRANCH:
   19081              :         case OMP_CLAUSE_NOTINBRANCH:
   19082              :         case OMP_CLAUSE_PROC_BIND:
   19083              :         case OMP_CLAUSE_FOR:
   19084              :         case OMP_CLAUSE_PARALLEL:
   19085              :         case OMP_CLAUSE_SECTIONS:
   19086              :         case OMP_CLAUSE_TASKGROUP:
   19087              :         case OMP_CLAUSE_NOGROUP:
   19088              :         case OMP_CLAUSE_THREADS:
   19089              :         case OMP_CLAUSE_SIMD:
   19090              :         case OMP_CLAUSE_DEFAULTMAP:
   19091              :         case OMP_CLAUSE_ORDER:
   19092              :         case OMP_CLAUSE_BIND:
   19093              :         case OMP_CLAUSE_INDEPENDENT:
   19094              :         case OMP_CLAUSE_AUTO:
   19095              :         case OMP_CLAUSE_SEQ:
   19096              :         case OMP_CLAUSE_IF_PRESENT:
   19097              :         case OMP_CLAUSE_FINALIZE:
   19098              :         case OMP_CLAUSE_NOHOST:
   19099              :         case OMP_CLAUSE_FULL:
   19100              :           break;
   19101            0 :         default:
   19102            0 :           gcc_unreachable ();
   19103              :         }
   19104         6157 :       if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
   19105         5260 :         switch (OMP_CLAUSE_CODE (nc))
   19106              :           {
   19107         2004 :           case OMP_CLAUSE_SHARED:
   19108         2004 :           case OMP_CLAUSE_PRIVATE:
   19109         2004 :           case OMP_CLAUSE_FIRSTPRIVATE:
   19110         2004 :           case OMP_CLAUSE_LASTPRIVATE:
   19111         2004 :           case OMP_CLAUSE_COPYPRIVATE:
   19112         2004 :           case OMP_CLAUSE_LINEAR:
   19113         2004 :           case OMP_CLAUSE_REDUCTION:
   19114         2004 :           case OMP_CLAUSE_IN_REDUCTION:
   19115         2004 :           case OMP_CLAUSE_TASK_REDUCTION:
   19116         2004 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   19117         2004 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   19118         2004 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   19119         2004 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   19120         2004 :           case OMP_CLAUSE_INCLUSIVE:
   19121         2004 :           case OMP_CLAUSE_EXCLUSIVE:
   19122         2004 :           case OMP_CLAUSE_ALLOCATE:
   19123              :             /* tsubst_expr on SCOPE_REF results in returning
   19124              :                finish_non_static_data_member result.  Undo that here.  */
   19125         2004 :             if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
   19126         2004 :                 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
   19127              :                     == IDENTIFIER_NODE))
   19128              :               {
   19129           79 :                 tree t = OMP_CLAUSE_DECL (nc);
   19130           79 :                 tree v = t;
   19131          464 :                 while (v)
   19132          464 :                   switch (TREE_CODE (v))
   19133              :                     {
   19134          385 :                     case COMPONENT_REF:
   19135          385 :                     case MEM_REF:
   19136          385 :                     case INDIRECT_REF:
   19137          385 :                     CASE_CONVERT:
   19138          385 :                     case POINTER_PLUS_EXPR:
   19139          385 :                       v = TREE_OPERAND (v, 0);
   19140          385 :                       continue;
   19141           77 :                     case PARM_DECL:
   19142           77 :                       if (DECL_CONTEXT (v) == current_function_decl
   19143           77 :                           && DECL_ARTIFICIAL (v)
   19144          154 :                           && DECL_NAME (v) == this_identifier)
   19145           77 :                         OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
   19146              :                       /* FALLTHRU */
   19147              :                     default:
   19148              :                       v = NULL_TREE;
   19149              :                       break;
   19150              :                     }
   19151              :               }
   19152         1925 :             else if (VAR_P (OMP_CLAUSE_DECL (oc))
   19153          959 :                      && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
   19154          197 :                      && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
   19155          197 :                      && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
   19156         2122 :                      && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
   19157              :               {
   19158          197 :                 tree decl = OMP_CLAUSE_DECL (nc);
   19159          197 :                 if (VAR_P (decl))
   19160              :                   {
   19161          197 :                     retrofit_lang_decl (decl);
   19162          197 :                     DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
   19163              :                   }
   19164              :               }
   19165              :             break;
   19166              :           default:
   19167              :             break;
   19168              :           }
   19169              :     }
   19170              : 
   19171         4930 :   new_clauses = nreverse (new_clauses);
   19172         4930 :   if (ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_OMP_DECLARE_MAPPER)
   19173              :     {
   19174         4814 :       if (ort & C_ORT_OMP)
   19175         4089 :         new_clauses = c_omp_instantiate_mappers (new_clauses, ort);
   19176         4814 :       new_clauses = finish_omp_clauses (new_clauses, ort);
   19177         4814 :       if (linear_no_step)
   19178            2 :         for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
   19179            2 :           if (nc == linear_no_step)
   19180              :             {
   19181            2 :               OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
   19182            2 :               break;
   19183              :             }
   19184              :     }
   19185         4930 :   return new_clauses;
   19186              : }
   19187              : 
   19188              : /* Like tsubst_copy, but specifically for OpenMP context selectors.  */
   19189              : static tree
   19190           58 : tsubst_omp_context_selector (tree ctx, tree args, tsubst_flags_t complain,
   19191              :                              tree in_decl)
   19192              : {
   19193           58 :   tree new_ctx = NULL_TREE;
   19194          128 :   for (tree set = ctx; set; set = TREE_CHAIN (set))
   19195              :     {
   19196           70 :       tree selectors = NULL_TREE;
   19197          140 :       for (tree sel = OMP_TSS_TRAIT_SELECTORS (set); sel;
   19198           70 :            sel = TREE_CHAIN (sel))
   19199              :         {
   19200           70 :           enum omp_ts_code code = OMP_TS_CODE (sel);
   19201           70 :           tree properties = NULL_TREE;
   19202           70 :           tree score = OMP_TS_SCORE (sel);
   19203            6 :           tree t;
   19204              : 
   19205            6 :           if (score)
   19206              :             {
   19207            6 :               score = tsubst_expr (score, args, complain, in_decl);
   19208            6 :               score = fold_non_dependent_expr (score);
   19209            6 :               if (!value_dependent_expression_p (score)
   19210            6 :                   && !type_dependent_expression_p (score))
   19211              :                 {
   19212           12 :                   if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
   19213           12 :                       || TREE_CODE (score) != INTEGER_CST)
   19214              :                     {
   19215            0 :                       error_at (cp_expr_loc_or_input_loc (score),
   19216              :                                 "%<score%> argument must "
   19217              :                                 "be constant integer expression");
   19218            0 :                       score = NULL_TREE;
   19219              :                     }
   19220            6 :                   else if (tree_int_cst_sgn (score) < 0)
   19221              :                     {
   19222            0 :                       error_at (cp_expr_loc_or_input_loc (score),
   19223              :                                 "%<score%> argument must be non-negative");
   19224            0 :                       score = NULL_TREE;
   19225              :                     }
   19226              :                 }
   19227              :             }
   19228              : 
   19229           70 :           enum omp_tp_type property_kind
   19230           70 :             = omp_ts_map[OMP_TS_CODE (sel)].tp_type;
   19231           70 :           switch (property_kind)
   19232              :               {
   19233           70 :               case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
   19234           70 :               case OMP_TRAIT_PROPERTY_BOOL_EXPR:
   19235          140 :                 t = tsubst_expr (OMP_TP_VALUE (OMP_TS_PROPERTIES (sel)),
   19236              :                                  args, complain, in_decl);
   19237           70 :                 t = fold_non_dependent_expr (t);
   19238           70 :                 if (!value_dependent_expression_p (t)
   19239           70 :                     && !type_dependent_expression_p (t))
   19240              :                   {
   19241           70 :                     if (property_kind == OMP_TRAIT_PROPERTY_BOOL_EXPR)
   19242           58 :                       t = maybe_convert_cond (t);
   19243              :                     else
   19244              :                       {
   19245           12 :                         t = convert_from_reference (t);
   19246           12 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
   19247              :                           {
   19248            0 :                             error_at (cp_expr_loc_or_input_loc (t),
   19249              :                                       "property must be integer expression");
   19250            0 :                             t = error_mark_node;
   19251              :                           }
   19252              :                       }
   19253              :                   }
   19254           70 :                 if (t != error_mark_node
   19255           70 :                     && !processing_template_decl
   19256           70 :                     && TREE_CODE (t) != CLEANUP_POINT_EXPR)
   19257           55 :                   t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
   19258           70 :                 properties = make_trait_property (NULL_TREE, t, NULL_TREE);
   19259           70 :                 break;
   19260            0 :               case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
   19261            0 :                 if (OMP_TS_CODE (sel) == OMP_TRAIT_CONSTRUCT_SIMD)
   19262            0 :                   properties = tsubst_omp_clauses (OMP_TS_PROPERTIES (sel),
   19263              :                                                    C_ORT_OMP_DECLARE_SIMD,
   19264              :                                                    args, complain, in_decl);
   19265              :                 break;
   19266            0 :               default:
   19267              :                 /* Nothing to do here, just copy.  */
   19268            0 :                 for (tree prop = OMP_TS_PROPERTIES (sel);
   19269            0 :                      prop; prop = TREE_CHAIN (prop))
   19270            0 :                   properties = make_trait_property (OMP_TP_NAME (prop),
   19271            0 :                                                     OMP_TP_VALUE (prop),
   19272              :                                                     properties);
   19273              :               }
   19274           70 :           selectors = make_trait_selector (code, score, properties, selectors);
   19275              :         }
   19276           70 :       new_ctx = make_trait_set_selector (OMP_TSS_CODE (set),
   19277              :                                          nreverse (selectors),
   19278              :                                          new_ctx);
   19279              :     }
   19280           58 :   return nreverse (new_ctx);
   19281              : }
   19282              : 
   19283              : 
   19284              : /* Like tsubst_expr, but unshare TREE_LIST nodes.  */
   19285              : 
   19286              : static tree
   19287        14418 : tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
   19288              :                           tree in_decl)
   19289              : {
   19290              : #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
   19291              : 
   19292        14418 :   tree purpose, value, chain;
   19293              : 
   19294        14418 :   if (t == NULL)
   19295              :     return t;
   19296              : 
   19297        11702 :   if (TREE_CODE (t) != TREE_LIST)
   19298         5962 :     return tsubst_expr (t, args, complain, in_decl);
   19299              : 
   19300         5740 :   if (t == void_list_node)
   19301              :     return t;
   19302              : 
   19303         5740 :   purpose = TREE_PURPOSE (t);
   19304         5740 :   if (purpose)
   19305         2946 :     purpose = RECUR (purpose);
   19306         5740 :   value = TREE_VALUE (t);
   19307         5740 :   if (value)
   19308              :     {
   19309         5740 :       if (TREE_CODE (value) != LABEL_DECL)
   19310         5737 :         value = RECUR (value);
   19311              :       else
   19312              :         {
   19313            3 :           value = lookup_label (DECL_NAME (value));
   19314            3 :           gcc_assert (TREE_CODE (value) == LABEL_DECL);
   19315            3 :           TREE_USED (value) = 1;
   19316              :         }
   19317              :     }
   19318         5740 :   chain = TREE_CHAIN (t);
   19319         5740 :   if (chain && chain != void_type_node)
   19320         1579 :     chain = RECUR (chain);
   19321         5740 :   return tree_cons (purpose, value, chain);
   19322              : #undef RECUR
   19323              : }
   19324              : 
   19325              : /* Used to temporarily communicate the list of #pragma omp parallel
   19326              :    clauses to #pragma omp for instantiation if they are combined
   19327              :    together.  */
   19328              : 
   19329              : static tree *omp_parallel_combined_clauses;
   19330              : 
   19331              : static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
   19332              :                                  cp_decomp *);
   19333              : 
   19334              : /* Substitute one OMP_FOR iterator.  */
   19335              : 
   19336              : static bool
   19337         1275 : tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
   19338              :                          tree initv, tree condv, tree incrv, tree *clauses,
   19339              :                          tree args, tsubst_flags_t complain, tree in_decl)
   19340              : {
   19341              : #define RECUR(NODE)                             \
   19342              :   tsubst_stmt ((NODE), args, complain, in_decl)
   19343         1275 :   tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
   19344         1275 :   bool ret = false;
   19345              : 
   19346         1275 :   init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
   19347         1275 :   gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
   19348              : 
   19349         1275 :   decl = TREE_OPERAND (init, 0);
   19350         1275 :   init = TREE_OPERAND (init, 1);
   19351         1275 :   tree decl_expr = NULL_TREE;
   19352         1275 :   bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
   19353         1275 :   if (range_for)
   19354              :     {
   19355          164 :       bool decomp = false;
   19356          164 :       if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
   19357              :         {
   19358           41 :           tree v = DECL_VALUE_EXPR (decl);
   19359           41 :           if ((TREE_CODE (v) == ARRAY_REF
   19360           41 :                && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
   19361           41 :               || (TREE_CODE (v) == TREE_VEC
   19362            0 :                   && DECL_DECOMPOSITION_P (TREE_VEC_ELT (v, 0))))
   19363              :             {
   19364           41 :               v = (TREE_CODE (v) == ARRAY_REF
   19365           41 :                    ? TREE_OPERAND (v, 0) : TREE_VEC_ELT (v, 0));
   19366           41 :               cp_decomp decomp_d = { NULL_TREE, 0 };
   19367           41 :               tree d = tsubst_decl (v, args, complain);
   19368           41 :               maybe_push_decl (d);
   19369           41 :               d = tsubst_decomp_names (d, v, args, complain,
   19370              :                                        in_decl, &decomp_d);
   19371           41 :               decomp = true;
   19372           41 :               if (d == error_mark_node)
   19373            0 :                 decl = error_mark_node;
   19374              :               else
   19375          155 :                 for (unsigned int i = 0; i < decomp_d.count; i++)
   19376              :                   {
   19377          114 :                     if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl))
   19378              :                       {
   19379          114 :                         tree v = build_nt (ARRAY_REF, d,
   19380          114 :                                            size_int (decomp_d.count - i - 1),
   19381              :                                            NULL_TREE, NULL_TREE);
   19382          114 :                         SET_DECL_VALUE_EXPR (decomp_d.decl, v);
   19383          114 :                         DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1;
   19384              :                       }
   19385          114 :                     fit_decomposition_lang_decl (decomp_d.decl, d);
   19386          114 :                     decomp_d.decl = DECL_CHAIN (decomp_d.decl);
   19387              :                   }
   19388              :             }
   19389              :         }
   19390          164 :       decl = tsubst_decl (decl, args, complain);
   19391          164 :       if (!decomp)
   19392          123 :         maybe_push_decl (decl);
   19393              :     }
   19394         1111 :   else if (init && TREE_CODE (init) == DECL_EXPR)
   19395              :     {
   19396              :       /* We need to jump through some hoops to handle declarations in the
   19397              :          init-statement, since we might need to handle auto deduction,
   19398              :          but we need to keep control of initialization.  */
   19399          223 :       decl_expr = init;
   19400          223 :       init = DECL_INITIAL (DECL_EXPR_DECL (init));
   19401          223 :       decl = tsubst_decl (decl, args, complain);
   19402              :     }
   19403              :   else
   19404              :     {
   19405          888 :       if (TREE_CODE (decl) == SCOPE_REF)
   19406              :         {
   19407            8 :           decl = RECUR (decl);
   19408            8 :           if (TREE_CODE (decl) == COMPONENT_REF)
   19409              :             {
   19410              :               tree v = decl;
   19411           24 :               while (v)
   19412           24 :                 switch (TREE_CODE (v))
   19413              :                   {
   19414           20 :                   case COMPONENT_REF:
   19415           20 :                   case MEM_REF:
   19416           20 :                   case INDIRECT_REF:
   19417           20 :                   CASE_CONVERT:
   19418           20 :                   case POINTER_PLUS_EXPR:
   19419           20 :                     v = TREE_OPERAND (v, 0);
   19420           20 :                     continue;
   19421            4 :                   case PARM_DECL:
   19422            4 :                     if (DECL_CONTEXT (v) == current_function_decl
   19423            4 :                         && DECL_ARTIFICIAL (v)
   19424            8 :                         && DECL_NAME (v) == this_identifier)
   19425              :                       {
   19426            4 :                         decl = TREE_OPERAND (decl, 1);
   19427            4 :                         decl = omp_privatize_field (decl, false);
   19428              :                       }
   19429              :                     /* FALLTHRU */
   19430              :                   default:
   19431              :                     v = NULL_TREE;
   19432              :                     break;
   19433              :                   }
   19434              :             }
   19435              :         }
   19436              :       else
   19437          880 :         decl = RECUR (decl);
   19438              :     }
   19439         1275 :   if (init && TREE_CODE (init) == TREE_VEC)
   19440              :     {
   19441           12 :       init = copy_node (init);
   19442           12 :       TREE_VEC_ELT (init, 0)
   19443           12 :         = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
   19444           12 :       TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
   19445           12 :       TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
   19446              :     }
   19447              :   else
   19448         1263 :     init = RECUR (init);
   19449              : 
   19450         1275 :   if (orig_declv && OMP_FOR_ORIG_DECLS (t))
   19451              :     {
   19452          640 :       tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   19453          640 :       if (TREE_CODE (o) == TREE_LIST)
   19454           92 :         TREE_VEC_ELT (orig_declv, i)
   19455          184 :           = tree_cons (RECUR (TREE_PURPOSE (o)),
   19456           92 :                        RECUR (TREE_VALUE (o)),
   19457              :                        NULL_TREE);
   19458              :       else
   19459          548 :         TREE_VEC_ELT (orig_declv, i) = RECUR (o);
   19460              :     }
   19461              : 
   19462         1275 :   if (range_for)
   19463              :     {
   19464          164 :       tree this_pre_body = NULL_TREE;
   19465          164 :       tree orig_init = NULL_TREE;
   19466          164 :       tree orig_decl = NULL_TREE;
   19467          164 :       tree init_sl = NULL_TREE;
   19468          164 :       cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
   19469              :                                 orig_init, cond, incr, true);
   19470          164 :       if (orig_decl)
   19471              :         {
   19472          159 :           if (orig_declv == NULL_TREE)
   19473          143 :             orig_declv = copy_node (declv);
   19474          159 :           TREE_VEC_ELT (orig_declv, i) = orig_decl;
   19475          159 :           ret = true;
   19476              :         }
   19477            5 :       else if (orig_declv)
   19478            0 :         TREE_VEC_ELT (orig_declv, i) = decl;
   19479              :     }
   19480              : 
   19481         1275 :   tree auto_node = type_uses_auto (TREE_TYPE (decl));
   19482         1275 :   if (!range_for && auto_node && init)
   19483            2 :     TREE_TYPE (decl)
   19484            4 :       = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
   19485              : 
   19486         1275 :   gcc_assert (!type_dependent_expression_p (decl));
   19487              : 
   19488         1275 :   if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
   19489              :     {
   19490         1080 :       if (decl_expr)
   19491              :         {
   19492              :           /* Declare the variable, but don't let that initialize it.  */
   19493          172 :           tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
   19494          172 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
   19495          172 :           RECUR (decl_expr);
   19496          172 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
   19497              :         }
   19498              : 
   19499         1080 :       if (!range_for)
   19500              :         {
   19501          916 :           cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   19502          916 :           if (COMPARISON_CLASS_P (cond)
   19503          916 :               && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
   19504              :             {
   19505           12 :               tree lhs = RECUR (TREE_OPERAND (cond, 0));
   19506           12 :               tree rhs = copy_node (TREE_OPERAND (cond, 1));
   19507           12 :               TREE_VEC_ELT (rhs, 0)
   19508           12 :                 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
   19509           12 :               TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
   19510           12 :               TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
   19511           12 :               cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
   19512              :                              lhs, rhs);
   19513              :             }
   19514              :           else
   19515          904 :             cond = RECUR (cond);
   19516          916 :           incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   19517          916 :           if (TREE_CODE (incr) == MODIFY_EXPR)
   19518              :             {
   19519          211 :               tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19520          211 :               tree rhs = RECUR (TREE_OPERAND (incr, 1));
   19521          211 :               incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
   19522              :                                           NOP_EXPR, rhs, NULL_TREE, complain);
   19523              :             }
   19524              :           else
   19525          705 :             incr = RECUR (incr);
   19526          916 :           if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   19527           18 :             TREE_VEC_ELT (orig_declv, i) = decl;
   19528              :         }
   19529         1080 :       TREE_VEC_ELT (declv, i) = decl;
   19530         1080 :       TREE_VEC_ELT (initv, i) = init;
   19531         1080 :       TREE_VEC_ELT (condv, i) = cond;
   19532         1080 :       TREE_VEC_ELT (incrv, i) = incr;
   19533         1080 :       return ret;
   19534              :     }
   19535              : 
   19536          195 :   if (decl_expr)
   19537              :     {
   19538              :       /* Declare and initialize the variable.  */
   19539           51 :       RECUR (decl_expr);
   19540           51 :       init = NULL_TREE;
   19541              :     }
   19542          144 :   else if (init)
   19543              :     {
   19544           61 :       tree *pc;
   19545           61 :       int j;
   19546          109 :       for (j = ((omp_parallel_combined_clauses == NULL
   19547          170 :                 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
   19548              :         {
   19549          128 :           for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
   19550              :             {
   19551           80 :               if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
   19552           80 :                   && OMP_CLAUSE_DECL (*pc) == decl)
   19553              :                 break;
   19554           76 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
   19555           76 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19556              :                 {
   19557           31 :                   if (j)
   19558              :                     break;
   19559              :                   /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
   19560            1 :                   tree c = *pc;
   19561            1 :                   *pc = OMP_CLAUSE_CHAIN (c);
   19562            1 :                   OMP_CLAUSE_CHAIN (c) = *clauses;
   19563            1 :                   *clauses = c;
   19564              :                 }
   19565           45 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
   19566           45 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19567              :                 {
   19568            0 :                   error ("iteration variable %qD should not be firstprivate",
   19569              :                          decl);
   19570            0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   19571              :                 }
   19572           45 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
   19573           45 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   19574              :                 {
   19575            0 :                   error ("iteration variable %qD should not be reduction",
   19576              :                          decl);
   19577            0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   19578              :                 }
   19579              :               else
   19580           45 :                 pc = &OMP_CLAUSE_CHAIN (*pc);
   19581              :             }
   19582           82 :           if (*pc)
   19583              :             break;
   19584              :         }
   19585           61 :       if (*pc == NULL_TREE)
   19586              :         {
   19587           27 :           tree c = build_omp_clause (input_location,
   19588           27 :                                      TREE_CODE (t) == OMP_LOOP
   19589              :                                      ? OMP_CLAUSE_LASTPRIVATE
   19590              :                                      : OMP_CLAUSE_PRIVATE);
   19591           27 :           OMP_CLAUSE_DECL (c) = decl;
   19592           27 :           c = finish_omp_clauses (c, C_ORT_OMP);
   19593           27 :           if (c)
   19594              :             {
   19595           27 :               OMP_CLAUSE_CHAIN (c) = *clauses;
   19596           27 :               *clauses = c;
   19597              :             }
   19598              :         }
   19599              :     }
   19600          195 :   cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   19601          195 :   if (COMPARISON_CLASS_P (cond))
   19602              :     {
   19603          195 :       tree op0 = RECUR (TREE_OPERAND (cond, 0));
   19604          195 :       tree op1 = RECUR (TREE_OPERAND (cond, 1));
   19605          195 :       cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
   19606              :     }
   19607              :   else
   19608            0 :     cond = RECUR (cond);
   19609          195 :   incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   19610          195 :   switch (TREE_CODE (incr))
   19611              :     {
   19612           85 :     case PREINCREMENT_EXPR:
   19613           85 :     case PREDECREMENT_EXPR:
   19614           85 :     case POSTINCREMENT_EXPR:
   19615           85 :     case POSTDECREMENT_EXPR:
   19616           85 :       incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
   19617           85 :                      RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
   19618           85 :       break;
   19619          110 :     case MODIFY_EXPR:
   19620          110 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   19621          110 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   19622              :         {
   19623          110 :           tree rhs = TREE_OPERAND (incr, 1);
   19624          110 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19625          110 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   19626          110 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   19627          110 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19628          110 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   19629              :                                  rhs0, rhs1));
   19630              :         }
   19631              :       else
   19632            0 :         incr = RECUR (incr);
   19633              :       break;
   19634            0 :     case MODOP_EXPR:
   19635            0 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   19636            0 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   19637              :         {
   19638            0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19639            0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19640            0 :                          build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
   19641            0 :                                  TREE_TYPE (decl), lhs,
   19642            0 :                                  RECUR (TREE_OPERAND (incr, 2))));
   19643              :         }
   19644            0 :       else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
   19645            0 :                && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
   19646            0 :                    || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
   19647              :         {
   19648            0 :           tree rhs = TREE_OPERAND (incr, 2);
   19649            0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   19650            0 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   19651            0 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   19652            0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   19653            0 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   19654              :                                  rhs0, rhs1));
   19655              :         }
   19656              :       else
   19657            0 :         incr = RECUR (incr);
   19658              :       break;
   19659            0 :     default:
   19660            0 :       incr = RECUR (incr);
   19661            0 :       break;
   19662              :     }
   19663              : 
   19664          195 :   if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   19665            5 :     TREE_VEC_ELT (orig_declv, i) = decl;
   19666          195 :   TREE_VEC_ELT (declv, i) = decl;
   19667          195 :   TREE_VEC_ELT (initv, i) = init;
   19668          195 :   TREE_VEC_ELT (condv, i) = cond;
   19669          195 :   TREE_VEC_ELT (incrv, i) = incr;
   19670          195 :   return false;
   19671              : #undef RECUR
   19672              : }
   19673              : 
   19674              : /* Helper function of tsubst_expr, find OMP_TEAMS inside
   19675              :    of OMP_TARGET's body.  */
   19676              : 
   19677              : static tree
   19678          164 : tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
   19679              : {
   19680          164 :   *walk_subtrees = 0;
   19681          164 :   switch (TREE_CODE (*tp))
   19682              :     {
   19683              :     case OMP_TEAMS:
   19684              :       return *tp;
   19685           82 :     case BIND_EXPR:
   19686           82 :     case STATEMENT_LIST:
   19687           82 :       *walk_subtrees = 1;
   19688           82 :       break;
   19689              :     default:
   19690              :       break;
   19691              :     }
   19692              :   return NULL_TREE;
   19693              : }
   19694              : 
   19695              : /* Helper function for tsubst_expr.  For decomposition declaration
   19696              :    artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
   19697              :    also the corresponding decls representing the identifiers
   19698              :    of the decomposition declaration.  Return DECL if successful
   19699              :    or error_mark_node otherwise, set *FIRST to the first decl
   19700              :    in the list chained through DECL_CHAIN and *CNT to the number
   19701              :    of such decls.  */
   19702              : 
   19703              : static tree
   19704        59674 : tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
   19705              :                      tsubst_flags_t complain, tree in_decl, cp_decomp *decomp)
   19706              : {
   19707        59674 :   tree decl2, decl3, prev = decl;
   19708        59674 :   decomp->count = 0;
   19709        59674 :   gcc_assert (DECL_NAME (decl) == NULL_TREE);
   19710        59674 :   for (decl2 = DECL_CHAIN (pattern_decl);
   19711              :        decl2
   19712       112530 :        && DECL_DECOMPOSITION_P (decl2)
   19713       279257 :        && DECL_NAME (decl2);
   19714       109261 :        decl2 = DECL_CHAIN (decl2))
   19715              :     {
   19716       109273 :       if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0)
   19717              :         {
   19718           12 :           gcc_assert (errorcount);
   19719              :           return error_mark_node;
   19720              :         }
   19721       109261 :       decomp->count++;
   19722       109261 :       gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
   19723       109261 :       gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
   19724       109261 :       tree v = DECL_VALUE_EXPR (decl2);
   19725       109261 :       DECL_HAS_VALUE_EXPR_P (decl2) = 0;
   19726       109261 :       SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
   19727       109261 :       decl3 = tsubst (decl2, args, complain, in_decl);
   19728       109261 :       SET_DECL_VALUE_EXPR (decl2, v);
   19729       109261 :       DECL_HAS_VALUE_EXPR_P (decl2) = 1;
   19730       109261 :       if (VAR_P (decl3))
   19731       109261 :         DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
   19732              :       else
   19733              :         {
   19734            0 :           gcc_assert (errorcount);
   19735            0 :           decl = error_mark_node;
   19736            0 :           continue;
   19737              :         }
   19738       109261 :       maybe_push_decl (decl3);
   19739       109261 :       if (error_operand_p (decl3))
   19740            0 :         decl = error_mark_node;
   19741       109261 :       else if (decl != error_mark_node
   19742       109261 :                && DECL_CHAIN (decl3) != prev
   19743       109261 :                && decl != prev)
   19744              :         {
   19745            0 :           gcc_assert (errorcount);
   19746              :           decl = error_mark_node;
   19747              :         }
   19748              :       else
   19749              :         prev = decl3;
   19750              :     }
   19751        59662 :   decomp->decl = prev;
   19752        59662 :   return decl;
   19753              : }
   19754              : 
   19755              : /* Return the proper local_specialization for init-capture pack DECL.  */
   19756              : 
   19757              : static tree
   19758          171 : lookup_init_capture_pack (tree decl)
   19759              : {
   19760              :   /* We handle normal pack captures by forwarding to the specialization of the
   19761              :      captured parameter.  We can't do that for pack init-captures; we need them
   19762              :      to have their own local_specialization.  We created the individual
   19763              :      VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
   19764              :      when we process the DECL_EXPR for the pack init-capture in the template.
   19765              :      So, how do we find them?  We don't know the capture proxy pack when
   19766              :      building the individual resulting proxies, and we don't know the
   19767              :      individual proxies when instantiating the pack.  What we have in common is
   19768              :      the FIELD_DECL.
   19769              : 
   19770              :      So...when we instantiate the FIELD_DECL, we stick the result in
   19771              :      local_specializations.  Then at the DECL_EXPR we look up that result, see
   19772              :      how many elements it has, synthesize the names, and look them up.  */
   19773              : 
   19774          171 :   tree cname = DECL_NAME (decl);
   19775          171 :   tree val = DECL_VALUE_EXPR (decl);
   19776          171 :   tree field = TREE_OPERAND (val, 1);
   19777          171 :   gcc_assert (TREE_CODE (field) == FIELD_DECL);
   19778          171 :   tree fpack = retrieve_local_specialization (field);
   19779          171 :   if (fpack == error_mark_node)
   19780              :     return error_mark_node;
   19781              : 
   19782          171 :   int len = 1;
   19783          171 :   tree vec = NULL_TREE;
   19784          171 :   tree r = NULL_TREE;
   19785          171 :   if (TREE_CODE (fpack) == TREE_VEC)
   19786              :     {
   19787          168 :       len = TREE_VEC_LENGTH (fpack);
   19788          168 :       vec = make_tree_vec (len);
   19789          168 :       r = make_node (NONTYPE_ARGUMENT_PACK);
   19790          168 :       ARGUMENT_PACK_ARGS (r) = vec;
   19791              :     }
   19792          486 :   for (int i = 0; i < len; ++i)
   19793              :     {
   19794          315 :       tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
   19795          315 :       tree elt = lookup_name (ename);
   19796          315 :       if (vec)
   19797          312 :         TREE_VEC_ELT (vec, i) = elt;
   19798              :       else
   19799              :         r = elt;
   19800              :     }
   19801              :   return r;
   19802              : }
   19803              : 
   19804              : /* T is an operand of a template tree being substituted.  Return whether
   19805              :    T is dependent such that we should suppress some warnings that would
   19806              :    make sense if the substituted expression were written directly, like
   19807              :      template <int I> bool f() { return I == 2; }
   19808              :    We don't want to warn when instantiating f that comparing two constants
   19809              :    always has the same value.
   19810              : 
   19811              :    This is a more limited concept of dependence than instantiation-dependent;
   19812              :    here we don't care whether substitution could fail.  */
   19813              : 
   19814              : static bool
   19815    118715146 : dependent_operand_p (tree t)
   19816              : {
   19817    120037036 :   while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
   19818      1321890 :     t = TREE_OPERAND (t, 0);
   19819              : 
   19820    118715146 :   ++processing_template_decl;
   19821    118715146 :   bool r = (potential_constant_expression (t)
   19822    118715146 :             ? value_dependent_expression_p (t)
   19823    118715146 :             : type_dependent_expression_p (t));
   19824    118715146 :   --processing_template_decl;
   19825    118715146 :   return r;
   19826              : }
   19827              : 
   19828              : /* A superset of tsubst_expr that also handles statement trees.  */
   19829              : 
   19830              : static tree
   19831    373839741 : tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   19832              : {
   19833              : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   19834              : #define RECUR(NODE)                             \
   19835              :   tsubst_stmt ((NODE), args, complain, in_decl)
   19836              : 
   19837    373839741 :   tree stmt, tmp;
   19838    373839741 :   tree r;
   19839    373839741 :   location_t loc;
   19840              : 
   19841    373839741 :   if (t == NULL_TREE || t == error_mark_node)
   19842              :     return t;
   19843              : 
   19844    371405171 :   loc = input_location;
   19845    371405171 :   if (location_t eloc = cp_expr_location (t))
   19846    320174168 :     input_location = eloc;
   19847    371405171 :   if (STATEMENT_CODE_P (TREE_CODE (t)))
   19848     54097683 :     current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
   19849              : 
   19850    371405171 :   switch (TREE_CODE (t))
   19851              :     {
   19852     47154485 :     case STATEMENT_LIST:
   19853     47154485 :       {
   19854    237634860 :         for (tree stmt : tsi_range (t))
   19855    190480378 :           RECUR (stmt);
   19856              :         break;
   19857              :       }
   19858              : 
   19859      3287405 :     case CTOR_INITIALIZER:
   19860      3287405 :       finish_mem_initializers (tsubst_initializer_list
   19861      3287405 :                                (TREE_OPERAND (t, 0), args));
   19862      3287405 :       break;
   19863              : 
   19864     22116402 :     case RETURN_EXPR:
   19865     22116402 :       finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
   19866     22116399 :       break;
   19867              : 
   19868          176 :     case CO_RETURN_EXPR:
   19869          176 :       finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
   19870          176 :       break;
   19871              : 
   19872     27039318 :     case EXPR_STMT:
   19873     27039318 :       tmp = RECUR (EXPR_STMT_EXPR (t));
   19874     27039315 :       if (EXPR_STMT_STMT_EXPR_RESULT (t))
   19875          160 :         finish_stmt_expr_expr (tmp, cur_stmt_expr);
   19876              :       else
   19877     27039155 :         finish_expr_stmt (tmp);
   19878              :       break;
   19879              : 
   19880        15200 :     case USING_STMT:
   19881        15200 :       finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
   19882        15200 :       break;
   19883              : 
   19884            0 :     case PRECONDITION_STMT:
   19885            0 :     case POSTCONDITION_STMT:
   19886            0 :       gcc_unreachable ();
   19887              : 
   19888            2 :     case ASSERTION_STMT:
   19889            2 :       {
   19890            2 :         r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
   19891            2 :         if (r != error_mark_node)
   19892            2 :           add_stmt (r);
   19893            2 :         RETURN (r);
   19894              :       }
   19895              :       break;
   19896              : 
   19897     24929660 :     case DECL_EXPR:
   19898     24929660 :       {
   19899     24929660 :         tree decl, pattern_decl;
   19900     24929660 :         tree init;
   19901              : 
   19902     24929660 :         pattern_decl = decl = DECL_EXPR_DECL (t);
   19903     24929660 :         if (TREE_CODE (decl) == LABEL_DECL)
   19904           39 :           finish_label_decl (DECL_NAME (decl));
   19905     24929621 :         else if (TREE_CODE (decl) == USING_DECL)
   19906              :           {
   19907       402198 :             tree scope = USING_DECL_SCOPE (decl);
   19908       402198 :             if (DECL_DEPENDENT_P (decl))
   19909              :               {
   19910            9 :                 scope = tsubst (scope, args, complain, in_decl);
   19911            9 :                 if (!MAYBE_CLASS_TYPE_P (scope)
   19912            6 :                     && TREE_CODE (scope) != ENUMERAL_TYPE)
   19913              :                   {
   19914            3 :                     if (complain & tf_error)
   19915            3 :                       error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
   19916              :                                 "class, namespace, or enumeration", scope);
   19917            3 :                     return error_mark_node;
   19918              :                   }
   19919            6 :                 finish_nonmember_using_decl (scope, DECL_NAME (decl));
   19920              :               }
   19921              :             else
   19922              :               {
   19923              :                 /* This is a non-dependent using-decl, and we'll have
   19924              :                    used the names it found during template parsing.  We do
   19925              :                    not want to do the lookup again, because we might not
   19926              :                    find the things we found then.  */
   19927       402189 :                 gcc_checking_assert (scope == tsubst (scope, args,
   19928              :                                                       complain, in_decl));
   19929              :                 /* We still need to push the bindings so that we can look up
   19930              :                    this name later.  */
   19931       804378 :                 push_using_decl_bindings (DECL_NAME (decl),
   19932       402189 :                                           USING_DECL_DECLS (decl));
   19933              :               }
   19934              :           }
   19935     24527423 :         else if (is_capture_proxy (decl)
   19936     24527423 :                  && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
   19937              :           {
   19938              :             /* We're in tsubst_lambda_expr, we've already inserted a new
   19939              :                capture proxy, so look it up and register it.  */
   19940       334083 :             tree inst;
   19941       334083 :             if (!DECL_PACK_P (decl))
   19942              :               {
   19943       333697 :                 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
   19944              :                                     LOOK_want::HIDDEN_LAMBDA);
   19945       333697 :                 gcc_assert (inst != decl && is_capture_proxy (inst));
   19946              :               }
   19947          386 :             else if (is_normal_capture_proxy (decl))
   19948              :               {
   19949          215 :                 inst = (retrieve_local_specialization
   19950          215 :                         (DECL_CAPTURED_VARIABLE (decl)));
   19951          215 :                 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
   19952              :                             || DECL_PACK_P (inst));
   19953              :               }
   19954              :             else
   19955          171 :               inst = lookup_init_capture_pack (decl);
   19956              : 
   19957       334083 :             register_local_specialization (inst, decl);
   19958       334083 :             break;
   19959              :           }
   19960     24193340 :         else if (DECL_PRETTY_FUNCTION_P (decl))
   19961        38307 :           decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
   19962        38307 :                                   DECL_NAME (decl),
   19963              :                                   true/*DECL_PRETTY_FUNCTION_P (decl)*/);
   19964      2972171 :         else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   19965     25882426 :                  && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   19966              :           /* Don't copy the old closure; we'll create a new one in
   19967              :              tsubst_lambda_expr.  */
   19968              :           break;
   19969              :         else
   19970              :           {
   19971     23919807 :             init = DECL_INITIAL (decl);
   19972     23919807 :             decl = tsubst (decl, args, complain, in_decl);
   19973     23919804 :             if (decl != error_mark_node)
   19974              :               {
   19975              :                 /* By marking the declaration as instantiated, we avoid
   19976              :                    trying to instantiate it.  Since instantiate_decl can't
   19977              :                    handle local variables, and since we've already done
   19978              :                    all that needs to be done, that's the right thing to
   19979              :                    do.  */
   19980     23919792 :                 if (VAR_P (decl))
   19981     21182469 :                   DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   19982     21182469 :                 if (VAR_P (decl) && !DECL_NAME (decl)
   19983     23979497 :                     && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   19984              :                   /* Anonymous aggregates are a special case.  */
   19985           33 :                   finish_anon_union (decl);
   19986     23919759 :                 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
   19987              :                   {
   19988       162117 :                     DECL_CONTEXT (decl) = current_function_decl;
   19989       162117 :                     insert_capture_proxy (decl);
   19990              :                   }
   19991     23757642 :                 else if (DECL_IMPLICIT_TYPEDEF_P (t))
   19992              :                   /* We already did a pushtag.  */;
   19993      2737323 :                 else if (VAR_OR_FUNCTION_DECL_P (decl)
   19994     23758029 :                          && DECL_LOCAL_DECL_P (decl))
   19995              :                   {
   19996          475 :                     if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   19997          133 :                       DECL_CONTEXT (decl) = NULL_TREE;
   19998          475 :                     decl = pushdecl (decl);
   19999          475 :                     if (TREE_CODE (decl) == FUNCTION_DECL
   20000          378 :                         && DECL_OMP_DECLARE_REDUCTION_P (decl)
   20001          599 :                         && cp_check_omp_declare_reduction (decl))
   20002           88 :                       instantiate_body (pattern_decl, args, decl, true);
   20003              :                   }
   20004              :                 else
   20005              :                   {
   20006     23757167 :                     bool const_init = false;
   20007     23757167 :                     cp_decomp decomp_d, *decomp = NULL;
   20008     23757167 :                     tree asmspec_tree = NULL_TREE;
   20009     23757167 :                     maybe_push_decl (decl);
   20010              : 
   20011     23757167 :                     if (VAR_P (decl)
   20012     21020231 :                         && DECL_LANG_SPECIFIC (decl)
   20013     24506878 :                         && DECL_OMP_PRIVATIZED_MEMBER (decl))
   20014              :                       break;
   20015              : 
   20016     21020048 :                     if (DECL_DECOMPOSITION_P (decl)
   20017     23816251 :                         && TREE_TYPE (pattern_decl) != error_mark_node)
   20018              :                       {
   20019        59267 :                         decomp = &decomp_d;
   20020        59267 :                         if (tsubst_decomp_names (decl, pattern_decl, args,
   20021              :                                                  complain, in_decl, decomp)
   20022        59267 :                             == error_mark_node)
   20023              :                           {
   20024           12 :                             decomp = NULL;
   20025              :                             /* As in cp_finish_decomp.  */
   20026           12 :                             if (TREE_STATIC (decl))
   20027              :                               {
   20028            3 :                                 tree id = get_identifier ("<decomp>");
   20029            3 :                                 SET_DECL_ASSEMBLER_NAME (decl, id);
   20030              :                               }
   20031              :                           }
   20032              :                       }
   20033              : 
   20034     23756984 :                     init = tsubst_init (init, decl, args, complain, in_decl);
   20035              : 
   20036     23756984 :                     if (VAR_P (decl))
   20037     21020048 :                       const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
   20038              :                                     (pattern_decl));
   20039              : 
   20040              :                     /* In a non-template function, VLA type declarations are
   20041              :                        handled in grokdeclarator; for templates, handle them
   20042              :                        now.  */
   20043     23756984 :                     predeclare_vla (decl);
   20044              : 
   20045     44777032 :                     if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
   20046              :                       {
   20047            6 :                         tree id = DECL_ASSEMBLER_NAME (pattern_decl);
   20048            6 :                         const char *asmspec = IDENTIFIER_POINTER (id);
   20049            6 :                         gcc_assert (asmspec[0] == '*');
   20050            6 :                         asmspec_tree
   20051            6 :                           = build_string (IDENTIFIER_LENGTH (id) - 1,
   20052              :                                           asmspec + 1);
   20053            6 :                         TREE_TYPE (asmspec_tree) = char_array_type_node;
   20054              :                       }
   20055              : 
   20056     23756984 :                     cp_finish_decl (decl, init, const_init, asmspec_tree, 0,
   20057              :                                     decomp);
   20058              :                   }
   20059              :               }
   20060              :           }
   20061              : 
   20062              :         break;
   20063              :       }
   20064              : 
   20065      1292669 :     case FOR_STMT:
   20066      1292669 :       stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
   20067      1292669 :       RECUR (FOR_INIT_STMT (t));
   20068      1292669 :       finish_init_stmt (stmt);
   20069      1292669 :       tmp = RECUR (FOR_COND (t));
   20070      1292669 :       finish_for_cond (tmp, stmt, false, 0, false);
   20071      1292669 :       tmp = RECUR (FOR_EXPR (t));
   20072      1292669 :       finish_for_expr (tmp, stmt);
   20073      1292669 :       {
   20074      1292669 :         bool prev = note_iteration_stmt_body_start ();
   20075      1292669 :         RECUR (FOR_BODY (t));
   20076      1292669 :         note_iteration_stmt_body_end (prev);
   20077              :       }
   20078      1292669 :       finish_for_stmt (stmt);
   20079      1292669 :       break;
   20080              : 
   20081        78688 :     case RANGE_FOR_STMT:
   20082        78688 :       {
   20083              :         /* Construct another range_for, if this is not a final
   20084              :            substitution (for inside a generic lambda of a
   20085              :            template).  Otherwise convert to a regular for.  */
   20086        78688 :         tree decl, expr;
   20087       157376 :         stmt = (processing_template_decl
   20088        78688 :                 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
   20089        78182 :                 : begin_for_stmt (NULL_TREE, NULL_TREE));
   20090        78688 :         RECUR (RANGE_FOR_INIT_STMT (t));
   20091        78688 :         decl = RANGE_FOR_DECL (t);
   20092        78688 :         decl = tsubst (decl, args, complain, in_decl);
   20093        78688 :         maybe_push_decl (decl);
   20094        78688 :         expr = RECUR (RANGE_FOR_EXPR (t));
   20095              : 
   20096        78688 :         cp_decomp decomp_d, *decomp = NULL;
   20097        78688 :         if (DECL_DECOMPOSITION_P (decl))
   20098              :           {
   20099          366 :             decomp = &decomp_d;
   20100          366 :             decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
   20101              :                                         complain, in_decl, decomp);
   20102              :           }
   20103              : 
   20104        78688 :         tree unroll = RECUR (RANGE_FOR_UNROLL (t));
   20105        78688 :         if (unroll)
   20106           33 :           unroll
   20107           33 :             = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)),
   20108              :                                       unroll);
   20109        78688 :         if (processing_template_decl)
   20110              :           {
   20111          506 :             RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
   20112          506 :             RANGE_FOR_UNROLL (stmt) = unroll;
   20113          506 :             RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t);
   20114          506 :             finish_range_for_decl (stmt, decl, expr);
   20115          506 :             if (decomp && decl != error_mark_node)
   20116            3 :               cp_finish_decomp (decl, decomp);
   20117              :           }
   20118              :         else
   20119       156364 :           stmt = cp_convert_range_for (stmt, decl, expr, decomp,
   20120        78182 :                                        RANGE_FOR_IVDEP (t), unroll,
   20121        78182 :                                        RANGE_FOR_NOVECTOR (t));
   20122              : 
   20123        78688 :         bool prev = note_iteration_stmt_body_start ();
   20124        78688 :         RECUR (RANGE_FOR_BODY (t));
   20125        78688 :         note_iteration_stmt_body_end (prev);
   20126        78688 :         finish_for_stmt (stmt);
   20127              :       }
   20128        78688 :       break;
   20129              : 
   20130       439373 :     case WHILE_STMT:
   20131       439373 :       stmt = begin_while_stmt ();
   20132       439373 :       tmp = RECUR (WHILE_COND (t));
   20133       439373 :       finish_while_stmt_cond (tmp, stmt, false, 0, false);
   20134       439373 :       {
   20135       439373 :         bool prev = note_iteration_stmt_body_start ();
   20136       439373 :         RECUR (WHILE_BODY (t));
   20137       439373 :         note_iteration_stmt_body_end (prev);
   20138              :       }
   20139       439373 :       finish_while_stmt (stmt);
   20140       439373 :       break;
   20141              : 
   20142       927415 :     case DO_STMT:
   20143       927415 :       stmt = begin_do_stmt ();
   20144       927415 :       {
   20145       927415 :         bool prev = note_iteration_stmt_body_start ();
   20146       927415 :         RECUR (DO_BODY (t));
   20147       927415 :         note_iteration_stmt_body_end (prev);
   20148              :       }
   20149       927415 :       finish_do_body (stmt);
   20150       927415 :       tmp = RECUR (DO_COND (t));
   20151       927415 :       finish_do_stmt (tmp, stmt, false, 0, false);
   20152       927415 :       break;
   20153              : 
   20154         1275 :     case TEMPLATE_FOR_STMT:
   20155         1275 :       {
   20156         1275 :         tree init;
   20157         1275 :         stmt = build_stmt (EXPR_LOCATION (t), TEMPLATE_FOR_STMT, NULL_TREE,
   20158              :                            NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
   20159         1275 :         TEMPLATE_FOR_SCOPE (stmt) = begin_template_for_scope (&init);
   20160         1275 :         TEMPLATE_FOR_INIT_STMT (stmt) = init;
   20161         1275 :         RECUR (TEMPLATE_FOR_INIT_STMT (t));
   20162         1275 :         TEMPLATE_FOR_EXPR (stmt) = RECUR (TEMPLATE_FOR_EXPR (t));
   20163         1275 :         if (processing_template_decl)
   20164              :           {
   20165           29 :             tree orig_decl = TEMPLATE_FOR_DECL (t);
   20166           29 :             if (TREE_CODE (orig_decl) == TREE_VEC)
   20167            0 :               orig_decl = TREE_VEC_ELT (orig_decl, 0);
   20168           29 :             tree decl = tsubst (orig_decl, args, complain, in_decl);
   20169           29 :             maybe_push_decl (decl);
   20170           29 :             if (VAR_P (decl))
   20171              :               {
   20172           29 :                 retrofit_lang_decl (decl);
   20173           29 :                 SET_DECL_DEPENDENT_INIT_P (decl, 1);
   20174              :               }
   20175              : 
   20176           29 :             cp_decomp decomp_d, *decomp = NULL;
   20177           29 :             if (DECL_DECOMPOSITION_P (decl))
   20178              :               {
   20179            0 :                 decomp = &decomp_d;
   20180            0 :                 decl = tsubst_decomp_names (decl, orig_decl, args,
   20181              :                                             complain, in_decl, decomp);
   20182            0 :                 if (decl != error_mark_node)
   20183              :                   {
   20184            0 :                     tree v = make_tree_vec (decomp->count + 1);
   20185            0 :                     TREE_VEC_ELT (v, 0) = decl;
   20186            0 :                     decl = decomp->decl;
   20187            0 :                     for (unsigned i = 0; i < decomp->count; ++i)
   20188              :                       {
   20189            0 :                         TREE_VEC_ELT (v, decomp->count - i) = decl;
   20190            0 :                         decl = DECL_CHAIN (decl);
   20191              :                       }
   20192              :                     decl = v;
   20193              :                   }
   20194              :               }
   20195           29 :             TEMPLATE_FOR_DECL (stmt) = decl;
   20196           29 :             TEMPLATE_FOR_INIT_STMT (stmt) = pop_stmt_list (init);
   20197           29 :             add_stmt (stmt);
   20198           29 :             TEMPLATE_FOR_BODY (stmt) = do_pushlevel (sk_block);
   20199           29 :             auto save_in_expansion_stmt = in_expansion_stmt;
   20200           29 :             in_expansion_stmt = true;
   20201           29 :             bool prev = note_iteration_stmt_body_start ();
   20202           29 :             RECUR (TEMPLATE_FOR_BODY (t));
   20203           29 :             note_iteration_stmt_body_end (prev);
   20204           29 :             in_expansion_stmt = save_in_expansion_stmt;
   20205           29 :             TEMPLATE_FOR_BODY (stmt)
   20206           58 :               = do_poplevel (TEMPLATE_FOR_BODY (stmt));
   20207              :           }
   20208              :         else
   20209              :           {
   20210         1246 :             TEMPLATE_FOR_DECL (stmt) = TEMPLATE_FOR_DECL (t);
   20211         1246 :             TEMPLATE_FOR_BODY (stmt) = TEMPLATE_FOR_BODY (t);
   20212         1246 :             finish_expansion_stmt (stmt, args, complain, in_decl);
   20213              :           }
   20214         1275 :         add_stmt (do_poplevel (TEMPLATE_FOR_SCOPE (stmt)));
   20215              :       }
   20216         1275 :       break;
   20217              : 
   20218     18873538 :     case IF_STMT:
   20219     18873538 :       stmt = begin_if_stmt ();
   20220     18873538 :       IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
   20221     18873538 :       IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
   20222     18873538 :       if (IF_STMT_CONSTEXPR_P (t))
   20223      5907943 :         args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
   20224     18873538 :       {
   20225     18873538 :         tree cond = IF_COND (t);
   20226     18873538 :         bool was_dep = dependent_operand_p (cond);
   20227     18873538 :         cond = RECUR (cond);
   20228     18873538 :         warning_sentinel s1(warn_address, was_dep);
   20229     18873538 :         tmp = finish_if_stmt_cond (cond, stmt);
   20230     18873538 :       }
   20231     18873538 :       if (IF_STMT_CONSTEXPR_P (t)
   20232     18873538 :           && instantiation_dependent_expression_p (tmp))
   20233              :         {
   20234              :           /* We're partially instantiating a generic lambda, but the condition
   20235              :              of the constexpr if is still dependent.  Don't substitute into the
   20236              :              branches now, just remember the template arguments.  */
   20237         7814 :           do_poplevel (IF_SCOPE (stmt));
   20238         7814 :           IF_SCOPE (stmt) = NULL_TREE;
   20239         7814 :           IF_COND (stmt) = IF_COND (t);
   20240         7814 :           THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
   20241         7814 :           ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
   20242         7814 :           IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
   20243         7814 :           add_stmt (stmt);
   20244         7814 :           break;
   20245              :         }
   20246     18865724 :       if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
   20247              :         /* Don't instantiate the THEN_CLAUSE. */;
   20248     16009169 :       else if (IF_STMT_CONSTEVAL_P (t))
   20249              :         {
   20250        31352 :           bool save_in_consteval_if_p = in_consteval_if_p;
   20251        31352 :           in_consteval_if_p = true;
   20252        31352 :           RECUR (THEN_CLAUSE (t));
   20253        31352 :           in_consteval_if_p = save_in_consteval_if_p;
   20254              :         }
   20255              :       else
   20256              :         {
   20257     15977817 :           tree folded = fold_non_dependent_expr (tmp, complain);
   20258     15977817 :           bool inhibit = integer_zerop (folded);
   20259     15977817 :           if (inhibit)
   20260        55274 :             ++c_inhibit_evaluation_warnings;
   20261     15977817 :           RECUR (THEN_CLAUSE (t));
   20262     15977817 :           if (inhibit)
   20263        55274 :             --c_inhibit_evaluation_warnings;
   20264              :         }
   20265     18865724 :       finish_then_clause (stmt);
   20266              : 
   20267     18865724 :       if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
   20268              :         /* Don't instantiate the ELSE_CLAUSE. */;
   20269     15822167 :       else if (ELSE_CLAUSE (t))
   20270              :         {
   20271      7070422 :           tree folded = fold_non_dependent_expr (tmp, complain);
   20272      7070422 :           bool inhibit = integer_nonzerop (folded);
   20273      7070422 :           begin_else_clause (stmt);
   20274      7070422 :           if (inhibit)
   20275        22933 :             ++c_inhibit_evaluation_warnings;
   20276      7070422 :           RECUR (ELSE_CLAUSE (t));
   20277      7070422 :           if (inhibit)
   20278        22933 :             --c_inhibit_evaluation_warnings;
   20279      7070422 :           finish_else_clause (stmt);
   20280              :         }
   20281              : 
   20282     18865724 :       finish_if_stmt (stmt);
   20283     18865724 :       break;
   20284              : 
   20285     50116108 :     case BIND_EXPR:
   20286     50116108 :       if (BIND_EXPR_BODY_BLOCK (t))
   20287      4338900 :         stmt = begin_function_body ();
   20288              :       else
   20289     91444711 :         stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
   20290              :                                     ? BCS_TRY_BLOCK : 0);
   20291              : 
   20292     50116108 :       RECUR (BIND_EXPR_BODY (t));
   20293              : 
   20294     50116099 :       if (BIND_EXPR_BODY_BLOCK (t))
   20295      4338900 :         finish_function_body (stmt);
   20296              :       else
   20297     45777199 :         finish_compound_stmt (stmt);
   20298              :       break;
   20299              : 
   20300      1020903 :     case BREAK_STMT:
   20301      1020903 :       finish_break_stmt ();
   20302      1020903 :       break;
   20303              : 
   20304         1516 :     case CONTINUE_STMT:
   20305         1516 :       finish_continue_stmt ();
   20306         1516 :       break;
   20307              : 
   20308       272081 :     case SWITCH_STMT:
   20309       272081 :       stmt = begin_switch_stmt ();
   20310       272081 :       tmp = RECUR (SWITCH_STMT_COND (t));
   20311       272081 :       finish_switch_cond (tmp, stmt);
   20312       272081 :       RECUR (SWITCH_STMT_BODY (t));
   20313       272081 :       finish_switch_stmt (stmt);
   20314       272081 :       break;
   20315              : 
   20316      1585597 :     case CASE_LABEL_EXPR:
   20317      1585597 :       {
   20318      1585597 :         tree decl = CASE_LABEL (t);
   20319      1585597 :         tree low = RECUR (CASE_LOW (t));
   20320      1585597 :         tree high = RECUR (CASE_HIGH (t));
   20321      1585597 :         tree l = finish_case_label (EXPR_LOCATION (t), low, high);
   20322      1585597 :         if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
   20323              :           {
   20324      1585588 :             tree label = CASE_LABEL (l);
   20325      1585588 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   20326      1585588 :             if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   20327            6 :               cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   20328              :           }
   20329              :       }
   20330              :       break;
   20331              : 
   20332          275 :     case LABEL_EXPR:
   20333          275 :       {
   20334          275 :         tree decl = LABEL_EXPR_LABEL (t);
   20335          275 :         tree label = finish_label_stmt (DECL_NAME (decl));
   20336          275 :         if (TREE_CODE (label) == LABEL_DECL)
   20337              :           {
   20338          275 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   20339          275 :             copy_warning (label, decl);
   20340              :           }
   20341          275 :         if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   20342           18 :           cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   20343              :       }
   20344          275 :       break;
   20345              : 
   20346          475 :     case GOTO_EXPR:
   20347          475 :       tmp = GOTO_DESTINATION (t);
   20348          475 :       if (TREE_CODE (tmp) != LABEL_DECL)
   20349              :         /* Computed goto's must be tsubst'd into.  On the other hand,
   20350              :            non-computed gotos must not be; the identifier in question
   20351              :            will have no binding.  */
   20352           23 :         tmp = RECUR (tmp);
   20353              :       else
   20354          452 :         tmp = DECL_NAME (tmp);
   20355          475 :       finish_goto_stmt (tmp);
   20356          475 :       break;
   20357              : 
   20358         1039 :     case ASM_EXPR:
   20359         1039 :       {
   20360         1039 :         tree string = RECUR (ASM_STRING (t));
   20361         1039 :         tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
   20362              :                                                  complain, in_decl);
   20363         1039 :         tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
   20364              :                                                 complain, in_decl);
   20365         1039 :         tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
   20366              :                                                   complain, in_decl);
   20367         1039 :         tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
   20368              :                                                 complain, in_decl);
   20369         2078 :         tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
   20370              :                                outputs, inputs, clobbers, labels,
   20371         1039 :                                ASM_INLINE_P (t), false);
   20372         1039 :         tree asm_expr = tmp;
   20373         1039 :         if (asm_expr != error_mark_node)
   20374              :           {
   20375          930 :             if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
   20376          930 :               asm_expr = TREE_OPERAND (asm_expr, 0);
   20377          930 :             ASM_BASIC_P (asm_expr) = ASM_BASIC_P (t);
   20378              :           }
   20379              :       }
   20380              :       break;
   20381              : 
   20382       109705 :     case TRY_BLOCK:
   20383       109705 :       if (CLEANUP_P (t))
   20384              :         {
   20385            0 :           stmt = begin_try_block ();
   20386            0 :           RECUR (TRY_STMTS (t));
   20387            0 :           finish_cleanup_try_block (stmt);
   20388            0 :           finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
   20389              :         }
   20390              :       else
   20391              :         {
   20392       109705 :           tree compound_stmt = NULL_TREE;
   20393              : 
   20394       109705 :           if (FN_TRY_BLOCK_P (t))
   20395           54 :             stmt = begin_function_try_block (&compound_stmt);
   20396              :           else
   20397       109651 :             stmt = begin_try_block ();
   20398              : 
   20399       109705 :           RECUR (TRY_STMTS (t));
   20400              : 
   20401       109705 :           if (FN_TRY_BLOCK_P (t))
   20402           54 :             finish_function_try_block (stmt);
   20403              :           else
   20404       109651 :             finish_try_block (stmt);
   20405              : 
   20406       109705 :           RECUR (TRY_HANDLERS (t));
   20407       109705 :           if (FN_TRY_BLOCK_P (t))
   20408           54 :             finish_function_handler_sequence (stmt, compound_stmt);
   20409              :           else
   20410       109651 :             finish_handler_sequence (stmt);
   20411              :         }
   20412              :       break;
   20413              : 
   20414       111032 :     case HANDLER:
   20415       111032 :       {
   20416       111032 :         tree decl = HANDLER_PARMS (t);
   20417              : 
   20418       111032 :         if (decl)
   20419              :           {
   20420         3742 :             decl = tsubst (decl, args, complain, in_decl);
   20421              :             /* Prevent instantiate_decl from trying to instantiate
   20422              :                this variable.  We've already done all that needs to be
   20423              :                done.  */
   20424         3742 :             if (decl != error_mark_node)
   20425         3739 :               DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   20426              :           }
   20427       111032 :         stmt = begin_handler ();
   20428       111032 :         finish_handler_parms (decl, stmt);
   20429       111032 :         RECUR (HANDLER_BODY (t));
   20430       111032 :         finish_handler (stmt);
   20431              :       }
   20432       111032 :       break;
   20433              : 
   20434       628726 :     case TAG_DEFN:
   20435       628726 :       tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
   20436       628726 :       if (dependent_type_p (tmp))
   20437              :         /* This is a partial instantiation, try again when full.  */
   20438           21 :         add_stmt (build_min (TAG_DEFN, tmp));
   20439       628705 :       else if (CLASS_TYPE_P (tmp))
   20440              :         {
   20441              :           /* Local classes are not independent templates; they are
   20442              :              instantiated along with their containing function.  And this
   20443              :              way we don't have to deal with pushing out of one local class
   20444              :              to instantiate a member of another local class.  */
   20445              :           /* Closures are handled by the LAMBDA_EXPR.  */
   20446      1256350 :           gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
   20447       628214 :           complete_type (tmp);
   20448       628214 :           tree save_ccp = current_class_ptr;
   20449       628214 :           tree save_ccr = current_class_ref;
   20450      4391711 :           for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
   20451      3763497 :             if ((VAR_P (fld)
   20452      3763494 :                  || (TREE_CODE (fld) == FUNCTION_DECL
   20453      2591720 :                      && !DECL_ARTIFICIAL (fld)))
   20454      6355201 :                 && DECL_TEMPLATE_INSTANTIATION (fld))
   20455      2591707 :               instantiate_decl (fld, /*defer_ok=*/false,
   20456              :                                 /*expl_inst_class=*/false);
   20457      1171790 :             else if (TREE_CODE (fld) == FIELD_DECL)
   20458       543328 :               maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
   20459       628214 :           current_class_ptr = save_ccp;
   20460       628214 :           current_class_ref = save_ccr;
   20461              :         }
   20462              :       break;
   20463              : 
   20464      6498197 :     case STATIC_ASSERT:
   20465      6498197 :       {
   20466      6498197 :         tree condition, message;
   20467              : 
   20468      6498197 :         ++c_inhibit_evaluation_warnings;
   20469      6498197 :         condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
   20470              :                                  complain, in_decl);
   20471      6498197 :         message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args,
   20472              :                                complain, in_decl);
   20473      6498197 :         if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST
   20474      6498197 :             && TREE_CODE (message) == STRING_CST)
   20475            0 :           message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t),
   20476            0 :                                 PAREN_EXPR, TREE_TYPE (message), message);
   20477      6498197 :         --c_inhibit_evaluation_warnings;
   20478              : 
   20479      6498197 :         finish_static_assert (condition, message,
   20480      6498197 :                               STATIC_ASSERT_SOURCE_LOCATION (t),
   20481              :                               /*member_p=*/false, /*show_expr_p=*/true,
   20482      6498197 :                               CONSTEVAL_BLOCK_P (t));
   20483              :       }
   20484      6498197 :       break;
   20485              : 
   20486          263 :     case OACC_KERNELS:
   20487          263 :     case OACC_PARALLEL:
   20488          263 :     case OACC_SERIAL:
   20489          263 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC_TARGET, args,
   20490              :                                 complain, in_decl);
   20491          263 :       stmt = begin_omp_parallel ();
   20492          263 :       RECUR (OMP_BODY (t));
   20493          263 :       finish_omp_construct (TREE_CODE (t), stmt, tmp);
   20494          263 :       break;
   20495              : 
   20496          855 :     case OMP_PARALLEL:
   20497          855 :       r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
   20498          855 :       tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
   20499              :                                 complain, in_decl);
   20500          855 :       if (OMP_PARALLEL_COMBINED (t))
   20501          445 :         omp_parallel_combined_clauses = &tmp;
   20502          855 :       stmt = begin_omp_parallel ();
   20503          855 :       RECUR (OMP_PARALLEL_BODY (t));
   20504          855 :       gcc_assert (omp_parallel_combined_clauses == NULL);
   20505         1710 :       OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
   20506          855 :         = OMP_PARALLEL_COMBINED (t);
   20507          855 :       pop_omp_privatization_clauses (r);
   20508          855 :       break;
   20509              : 
   20510          355 :     case OMP_TASK:
   20511          355 :       if (OMP_TASK_BODY (t) == NULL_TREE)
   20512              :         {
   20513            0 :           tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   20514              :                                     complain, in_decl);
   20515            0 :           t = copy_node (t);
   20516            0 :           OMP_TASK_CLAUSES (t) = tmp;
   20517            0 :           add_stmt (t);
   20518            0 :           break;
   20519              :         }
   20520          355 :       r = push_omp_privatization_clauses (false);
   20521          355 :       tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   20522              :                                 complain, in_decl);
   20523          355 :       stmt = begin_omp_task ();
   20524          355 :       RECUR (OMP_TASK_BODY (t));
   20525          355 :       finish_omp_task (tmp, stmt);
   20526          355 :       pop_omp_privatization_clauses (r);
   20527          355 :       break;
   20528              : 
   20529         1263 :     case OMP_FOR:
   20530         1263 :     case OMP_LOOP:
   20531         1263 :     case OMP_SIMD:
   20532         1263 :     case OMP_DISTRIBUTE:
   20533         1263 :     case OMP_TASKLOOP:
   20534         1263 :     case OMP_TILE:
   20535         1263 :     case OMP_UNROLL:
   20536         1263 :     case OACC_LOOP:
   20537         1263 :       {
   20538         1263 :         tree clauses, body, pre_body;
   20539         1263 :         tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
   20540         1263 :         tree orig_declv = NULL_TREE;
   20541         1263 :         tree incrv = NULL_TREE;
   20542         1263 :         enum c_omp_region_type ort = C_ORT_OMP;
   20543         1263 :         bool any_range_for = false;
   20544         1263 :         int i;
   20545              : 
   20546         1263 :         if (TREE_CODE (t) == OACC_LOOP)
   20547           51 :           ort = C_ORT_ACC;
   20548              : 
   20549         1263 :         r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
   20550         1263 :         clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
   20551              :                                       in_decl);
   20552         1263 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20553              :           {
   20554         1081 :             declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20555         1081 :             if (OMP_FOR_ORIG_DECLS (t))
   20556          563 :               orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20557         1081 :             initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20558         1081 :             condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20559         1081 :             incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   20560              :           }
   20561              : 
   20562         1263 :         keep_next_level (true);
   20563         1263 :         stmt = begin_omp_structured_block ();
   20564              : 
   20565         1263 :         pre_body = push_stmt_list ();
   20566         1263 :         RECUR (OMP_FOR_PRE_BODY (t));
   20567         1263 :         pre_body = pop_stmt_list (pre_body);
   20568              : 
   20569         1263 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20570         2395 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20571              :             {
   20572         1314 :               if (TREE_VEC_ELT (OMP_FOR_INIT (t), i))
   20573         1275 :                 any_range_for
   20574         1275 :                   |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
   20575              :                                               condv, incrv, &clauses, args,
   20576              :                                               complain, in_decl);
   20577              :               else
   20578              :                 {
   20579           39 :                   TREE_VEC_ELT (declv, i) = global_namespace;
   20580           39 :                   TREE_VEC_ELT (initv, i) = NULL_TREE;
   20581           39 :                   TREE_VEC_ELT (condv, i) = NULL_TREE;
   20582           39 :                   TREE_VEC_ELT (incrv, i) = NULL_TREE;
   20583           39 :                   if (orig_declv)
   20584           30 :                     TREE_VEC_ELT (orig_declv, i) = NULL_TREE;
   20585              :                 }
   20586              :             }
   20587         1263 :         omp_parallel_combined_clauses = NULL;
   20588              : 
   20589         1263 :         if (any_range_for)
   20590              :           {
   20591          143 :             gcc_assert (orig_declv);
   20592          143 :             body = begin_omp_structured_block ();
   20593          468 :             for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20594          182 :               if (TREE_VEC_ELT (declv, i) != global_namespace
   20595          182 :                   && TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
   20596          159 :                   && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
   20597          341 :                   && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
   20598          159 :                 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
   20599          159 :                                          TREE_VEC_ELT (declv, i));
   20600              :           }
   20601              :         else
   20602         1120 :           body = push_stmt_list ();
   20603         1263 :         RECUR (OMP_FOR_BODY (t));
   20604         1263 :         if (any_range_for)
   20605          143 :           body = finish_omp_structured_block (body);
   20606              :         else
   20607         1120 :           body = pop_stmt_list (body);
   20608              : 
   20609         1263 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   20610         1081 :           t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
   20611              :                               orig_declv, initv, condv, incrv, body, pre_body,
   20612              :                               NULL, clauses);
   20613              :         else
   20614              :           {
   20615          182 :             t = make_node (TREE_CODE (t));
   20616          182 :             TREE_TYPE (t) = void_type_node;
   20617          182 :             OMP_FOR_BODY (t) = body;
   20618          182 :             OMP_FOR_PRE_BODY (t) = pre_body;
   20619          182 :             OMP_FOR_CLAUSES (t) = clauses;
   20620          182 :             SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
   20621          182 :             add_stmt (t);
   20622              :           }
   20623              : 
   20624         1263 :         add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
   20625              :                                         t));
   20626         1263 :         pop_omp_privatization_clauses (r);
   20627              : 
   20628         1263 :         if (any_range_for && !processing_template_decl && t)
   20629          267 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   20630          144 :             if (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i)
   20631          144 :                 && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t),
   20632              :                                             i)) == TREE_LIST)
   20633              :               {
   20634          144 :                 tree v = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   20635          144 :                 if (TREE_CHAIN (v) == NULL_TREE
   20636          144 :                     || TREE_CODE (TREE_CHAIN (v)) != TREE_VEC)
   20637            5 :                   continue;
   20638          139 :                 v = TREE_VEC_ELT (TREE_CHAIN (v), 0);
   20639          139 :                 gcc_assert (VAR_P (v) && DECL_NAME (v) == NULL_TREE);
   20640          139 :                 DECL_NAME (v) = for_range_identifier;
   20641              :               }
   20642              :       }
   20643         1263 :       break;
   20644              : 
   20645           19 :     case OMP_SECTIONS:
   20646           19 :     case OMP_MASKED:
   20647           19 :       omp_parallel_combined_clauses = NULL;
   20648              :       /* FALLTHRU */
   20649          494 :     case OMP_SINGLE:
   20650          494 :     case OMP_SCOPE:
   20651          494 :     case OMP_TEAMS:
   20652          494 :     case OMP_CRITICAL:
   20653          494 :     case OMP_TASKGROUP:
   20654          494 :     case OMP_SCAN:
   20655          988 :       r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
   20656          264 :                                           && OMP_TEAMS_COMBINED (t));
   20657          494 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
   20658              :                                 in_decl);
   20659          494 :       if (TREE_CODE (t) == OMP_TEAMS)
   20660              :         {
   20661          264 :           keep_next_level (true);
   20662          264 :           stmt = begin_omp_structured_block ();
   20663          264 :           RECUR (OMP_BODY (t));
   20664          264 :           stmt = finish_omp_structured_block (stmt);
   20665              :         }
   20666              :       else
   20667              :         {
   20668          230 :           stmt = push_stmt_list ();
   20669          230 :           RECUR (OMP_BODY (t));
   20670          230 :           stmt = pop_stmt_list (stmt);
   20671              :         }
   20672              : 
   20673          494 :       if (TREE_CODE (t) == OMP_CRITICAL
   20674           18 :           && tmp != NULL_TREE
   20675          506 :           && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
   20676              :         {
   20677            6 :           error_at (OMP_CLAUSE_LOCATION (tmp),
   20678              :                     "%<#pragma omp critical%> with %<hint%> clause requires "
   20679              :                     "a name, except when %<omp_sync_hint_none%> is used");
   20680            6 :           RETURN (error_mark_node);
   20681              :         }
   20682          488 :       t = copy_node (t);
   20683          488 :       OMP_BODY (t) = stmt;
   20684          488 :       OMP_CLAUSES (t) = tmp;
   20685          488 :       add_stmt (t);
   20686          488 :       pop_omp_privatization_clauses (r);
   20687          488 :       break;
   20688              : 
   20689          114 :     case OMP_DEPOBJ:
   20690          114 :       r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
   20691          114 :       if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
   20692              :         {
   20693          105 :           enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
   20694          105 :           if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
   20695              :             {
   20696           58 :               tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
   20697              :                                         args, complain, in_decl);
   20698           58 :               if (tmp == NULL_TREE)
   20699            6 :                 tmp = error_mark_node;
   20700              :             }
   20701              :           else
   20702              :             {
   20703           94 :               kind = (enum omp_clause_depend_kind)
   20704           47 :                      tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
   20705           47 :               tmp = NULL_TREE;
   20706              :             }
   20707          105 :           finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
   20708              :         }
   20709              :       else
   20710            9 :         finish_omp_depobj (EXPR_LOCATION (t), r,
   20711              :                            OMP_CLAUSE_DEPEND_INVALID,
   20712            9 :                            OMP_DEPOBJ_CLAUSES (t));
   20713              :       break;
   20714              : 
   20715          842 :     case OACC_DATA:
   20716          842 :     case OMP_TARGET_DATA:
   20717          842 :     case OMP_TARGET:
   20718         1669 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
   20719              :                                 TREE_CODE (t) == OACC_DATA
   20720              :                                 ? C_ORT_ACC
   20721              :                                 : TREE_CODE (t) == OMP_TARGET
   20722          827 :                                 ? C_ORT_OMP_TARGET : C_ORT_OMP,
   20723              :                                 args, complain, in_decl);
   20724          842 :       keep_next_level (true);
   20725          842 :       stmt = begin_omp_structured_block ();
   20726              : 
   20727          842 :       RECUR (OMP_BODY (t));
   20728          842 :       stmt = finish_omp_structured_block (stmt);
   20729              : 
   20730          842 :       t = copy_node (t);
   20731          842 :       OMP_BODY (t) = stmt;
   20732          842 :       OMP_CLAUSES (t) = tmp;
   20733              : 
   20734          842 :       if (TREE_CODE (t) == OMP_TARGET)
   20735          816 :         finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
   20736              :                                    &OMP_CLAUSES (t));
   20737              : 
   20738          842 :       if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
   20739              :         {
   20740           82 :           tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
   20741           82 :           if (teams)
   20742              :             /* For combined target teams, ensure the num_teams and
   20743              :                thread_limit clause expressions are evaluated on the host,
   20744              :                before entering the target construct.  */
   20745          160 :             for (tree c = OMP_TEAMS_CLAUSES (teams);
   20746          160 :                  c; c = OMP_CLAUSE_CHAIN (c))
   20747           78 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
   20748           78 :                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
   20749          141 :                 for (int i = 0;
   20750          219 :                      i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
   20751          141 :                   if (OMP_CLAUSE_OPERAND (c, i)
   20752          141 :                       && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
   20753              :                     {
   20754           42 :                       tree expr = OMP_CLAUSE_OPERAND (c, i);
   20755           42 :                       expr = force_target_expr (TREE_TYPE (expr), expr,
   20756              :                                                 tf_none);
   20757           42 :                       if (expr == error_mark_node)
   20758            0 :                         continue;
   20759           42 :                       tmp = TARGET_EXPR_SLOT (expr);
   20760           42 :                       add_stmt (expr);
   20761           42 :                       OMP_CLAUSE_OPERAND (c, i) = expr;
   20762           42 :                       tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   20763              :                                                   OMP_CLAUSE_FIRSTPRIVATE);
   20764           42 :                       OMP_CLAUSE_DECL (tc) = tmp;
   20765           42 :                       OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
   20766           42 :                       OMP_TARGET_CLAUSES (t) = tc;
   20767              :                     }
   20768              :         }
   20769          842 :       add_stmt (t);
   20770          842 :       break;
   20771              : 
   20772            1 :     case OACC_DECLARE:
   20773            1 :       t = copy_node (t);
   20774            1 :       tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
   20775              :                                 complain, in_decl);
   20776            1 :       OACC_DECLARE_CLAUSES (t) = tmp;
   20777            1 :       add_stmt (t);
   20778            1 :       break;
   20779              : 
   20780          177 :     case OMP_TARGET_UPDATE:
   20781          177 :     case OMP_TARGET_ENTER_DATA:
   20782          177 :     case OMP_TARGET_EXIT_DATA:
   20783          290 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t),
   20784              :                                 (TREE_CODE (t) == OMP_TARGET_EXIT_DATA
   20785              :                                  ? C_ORT_OMP_EXIT_DATA : C_ORT_OMP), args,
   20786              :                                 complain, in_decl);
   20787          177 :       t = copy_node (t);
   20788          177 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   20789          177 :       add_stmt (t);
   20790          177 :       break;
   20791              : 
   20792          395 :     case OACC_CACHE:
   20793          395 :     case OACC_ENTER_DATA:
   20794          395 :     case OACC_EXIT_DATA:
   20795          395 :     case OACC_UPDATE:
   20796          395 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
   20797              :                                 complain, in_decl);
   20798          395 :       t = copy_node (t);
   20799          395 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   20800          395 :       add_stmt (t);
   20801          395 :       break;
   20802              : 
   20803           33 :     case OMP_ORDERED:
   20804           33 :       tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
   20805              :                                 complain, in_decl);
   20806           33 :       if (OMP_BODY (t))
   20807              :         {
   20808            3 :           stmt = push_stmt_list ();
   20809            3 :           RECUR (OMP_BODY (t));
   20810            3 :           stmt = pop_stmt_list (stmt);
   20811              :         }
   20812              :       else
   20813              :         stmt = NULL_TREE;
   20814              : 
   20815           33 :       t = copy_node (t);
   20816           33 :       OMP_BODY (t) = stmt;
   20817           33 :       OMP_ORDERED_CLAUSES (t) = tmp;
   20818           33 :       add_stmt (t);
   20819           33 :       break;
   20820              : 
   20821           73 :     case OMP_MASTER:
   20822           73 :     case OMP_STRUCTURED_BLOCK:
   20823           73 :       omp_parallel_combined_clauses = NULL;
   20824              :       /* FALLTHRU */
   20825           92 :     case OMP_SECTION:
   20826           92 :       stmt = push_stmt_list ();
   20827           92 :       RECUR (OMP_BODY (t));
   20828           92 :       stmt = pop_stmt_list (stmt);
   20829              : 
   20830           92 :       t = copy_node (t);
   20831           92 :       OMP_BODY (t) = stmt;
   20832           92 :       add_stmt (t);
   20833           92 :       break;
   20834              : 
   20835           12 :     case OMP_DISPATCH:
   20836           12 :       tmp = tsubst_omp_clauses (OMP_DISPATCH_CLAUSES (t), C_ORT_OMP, args,
   20837              :                                 complain, in_decl);
   20838           12 :       stmt = RECUR (OMP_DISPATCH_BODY (t));
   20839           12 :       t = copy_node (t);
   20840           12 :       OMP_DISPATCH_BODY (t) = stmt;
   20841           12 :       OMP_DISPATCH_CLAUSES (t) = tmp;
   20842           12 :       add_stmt (t);
   20843           12 :       break;
   20844              : 
   20845          609 :     case OMP_ATOMIC:
   20846          609 :       gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
   20847          609 :       tmp = NULL_TREE;
   20848          609 :       if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
   20849           30 :         tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
   20850              :                                   complain, in_decl);
   20851          609 :       if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
   20852              :         {
   20853          184 :           tree op1 = TREE_OPERAND (t, 1);
   20854          184 :           tree rhs1 = NULL_TREE;
   20855          184 :           tree r = NULL_TREE;
   20856          184 :           tree lhs, rhs;
   20857          184 :           if (TREE_CODE (op1) == COMPOUND_EXPR)
   20858              :             {
   20859           99 :               rhs1 = RECUR (TREE_OPERAND (op1, 0));
   20860           99 :               op1 = TREE_OPERAND (op1, 1);
   20861              :             }
   20862          184 :           if (TREE_CODE (op1) == COND_EXPR)
   20863              :             {
   20864           46 :               gcc_assert (rhs1 == NULL_TREE);
   20865           46 :               tree c = TREE_OPERAND (op1, 0);
   20866           46 :               if (TREE_CODE (c) == MODIFY_EXPR)
   20867              :                 {
   20868           14 :                   r = RECUR (TREE_OPERAND (c, 0));
   20869           14 :                   c = TREE_OPERAND (c, 1);
   20870              :                 }
   20871           46 :               gcc_assert (TREE_CODE (c) == EQ_EXPR);
   20872           46 :               rhs = RECUR (TREE_OPERAND (c, 1));
   20873           46 :               lhs = RECUR (TREE_OPERAND (op1, 2));
   20874           46 :               rhs1 = RECUR (TREE_OPERAND (op1, 1));
   20875              :             }
   20876              :           else
   20877              :             {
   20878          138 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   20879          138 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   20880              :             }
   20881          184 :           finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
   20882              :                              lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
   20883          184 :                              tmp, OMP_ATOMIC_MEMORY_ORDER (t),
   20884          184 :                              OMP_ATOMIC_WEAK (t));
   20885              :         }
   20886              :       else
   20887              :         {
   20888          425 :           tree op1 = TREE_OPERAND (t, 1);
   20889          425 :           tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
   20890          425 :           tree rhs1 = NULL_TREE, r = NULL_TREE;
   20891          425 :           enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
   20892          425 :           enum tree_code opcode = NOP_EXPR;
   20893          425 :           if (code == OMP_ATOMIC_READ)
   20894              :             {
   20895          166 :               v = RECUR (TREE_OPERAND (op1, 0));
   20896          166 :               lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   20897              :             }
   20898          259 :           else if (code == OMP_ATOMIC_CAPTURE_OLD
   20899          259 :                    || code == OMP_ATOMIC_CAPTURE_NEW)
   20900              :             {
   20901          233 :               tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
   20902          233 :               v = RECUR (TREE_OPERAND (op1, 0));
   20903          233 :               lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   20904          233 :               if (TREE_CODE (op11) == COMPOUND_EXPR)
   20905              :                 {
   20906           91 :                   rhs1 = RECUR (TREE_OPERAND (op11, 0));
   20907           91 :                   op11 = TREE_OPERAND (op11, 1);
   20908              :                 }
   20909          233 :               if (TREE_CODE (op11) == COND_EXPR)
   20910              :                 {
   20911           78 :                   gcc_assert (rhs1 == NULL_TREE);
   20912           78 :                   tree c = TREE_OPERAND (op11, 0);
   20913           78 :                   if (TREE_CODE (c) == MODIFY_EXPR)
   20914              :                     {
   20915           36 :                       r = RECUR (TREE_OPERAND (c, 0));
   20916           36 :                       c = TREE_OPERAND (c, 1);
   20917              :                     }
   20918           78 :                   gcc_assert (TREE_CODE (c) == EQ_EXPR);
   20919           78 :                   rhs = RECUR (TREE_OPERAND (c, 1));
   20920           78 :                   lhs = RECUR (TREE_OPERAND (op11, 2));
   20921           78 :                   rhs1 = RECUR (TREE_OPERAND (op11, 1));
   20922              :                 }
   20923              :               else
   20924              :                 {
   20925          155 :                   lhs = RECUR (TREE_OPERAND (op11, 0));
   20926          155 :                   rhs = RECUR (TREE_OPERAND (op11, 1));
   20927              :                 }
   20928          233 :               opcode = TREE_CODE (op11);
   20929          233 :               if (opcode == MODIFY_EXPR)
   20930           11 :                 opcode = NOP_EXPR;
   20931              :             }
   20932              :           else
   20933              :             {
   20934           26 :               code = OMP_ATOMIC;
   20935           26 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   20936           26 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   20937              :             }
   20938          425 :           finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
   20939              :                              lhs1, rhs1, r, tmp,
   20940          425 :                              OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
   20941              :         }
   20942              :       break;
   20943              : 
   20944           54 :     case OMP_METADIRECTIVE:
   20945           54 :       {
   20946           54 :         tree variants = NULL_TREE;
   20947          166 :         for (tree v = OMP_METADIRECTIVE_VARIANTS (t); v; v = TREE_CHAIN (v))
   20948              :           {
   20949          112 :             tree ctx = OMP_METADIRECTIVE_VARIANT_SELECTOR (v);
   20950          112 :             tree directive = OMP_METADIRECTIVE_VARIANT_DIRECTIVE (v);
   20951          112 :             tree body = OMP_METADIRECTIVE_VARIANT_BODY (v);
   20952          112 :             tree s;
   20953              : 
   20954              :             /* CTX is null if this is the default variant.  */
   20955          112 :             if (ctx)
   20956              :               {
   20957           58 :                 ctx = tsubst_omp_context_selector (ctx, args, complain,
   20958              :                                                    in_decl);
   20959              :                 /* Remove the selector from further consideration if it can be
   20960              :                    evaluated as a non-match at this point.  */
   20961           58 :                 if (omp_context_selector_matches (ctx, NULL_TREE, false) == 0)
   20962           16 :                   continue;
   20963              :               }
   20964           96 :             s = push_stmt_list ();
   20965           96 :             RECUR (directive);
   20966           96 :             directive = pop_stmt_list (s);
   20967           96 :             if (body)
   20968              :               {
   20969           36 :                 s = push_stmt_list ();
   20970           36 :                 RECUR (body);
   20971           36 :                 body = pop_stmt_list (s);
   20972              :               }
   20973           96 :             variants
   20974           96 :               = chainon (variants,
   20975              :                          make_omp_metadirective_variant (ctx, directive,
   20976              :                                                          body));
   20977              :           }
   20978           54 :         t = copy_node (t);
   20979           54 :         OMP_METADIRECTIVE_VARIANTS (t) = variants;
   20980              : 
   20981              :         /* Try to resolve the metadirective early.  */
   20982           54 :         vec<struct omp_variant> candidates
   20983           54 :           = omp_early_resolve_metadirective (t);
   20984           54 :         if (!candidates.is_empty ())
   20985           54 :           t = c_omp_expand_variant_construct (candidates);
   20986           54 :         add_stmt (t);
   20987           54 :         break;
   20988              :       }
   20989              : 
   20990            0 :     case OMP_DECLARE_MAPPER:
   20991            0 :       {
   20992            0 :         t = copy_node (t);
   20993              : 
   20994            0 :         tree decl = OMP_DECLARE_MAPPER_DECL (t);
   20995            0 :         decl = tsubst (decl, args, complain, in_decl);
   20996            0 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20997            0 :         tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t);
   20998            0 :         clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_MAPPER, args,
   20999              :                                       complain, in_decl);
   21000            0 :         TREE_TYPE (t) = type;
   21001            0 :         OMP_DECLARE_MAPPER_DECL (t) = decl;
   21002            0 :         OMP_DECLARE_MAPPER_CLAUSES (t) = clauses;
   21003            0 :         cp_check_omp_declare_mapper (t);
   21004            0 :         RETURN (t);
   21005              :       }
   21006              : 
   21007           80 :     case TRANSACTION_EXPR:
   21008           80 :       {
   21009           80 :         int flags = 0;
   21010           80 :         flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
   21011           80 :         flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
   21012              : 
   21013           80 :         if (TRANSACTION_EXPR_IS_STMT (t))
   21014              :           {
   21015           47 :             tree body = TRANSACTION_EXPR_BODY (t);
   21016           47 :             tree noex = NULL_TREE;
   21017           47 :             if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
   21018              :               {
   21019           12 :                 noex = MUST_NOT_THROW_COND (body);
   21020           12 :                 if (noex == NULL_TREE)
   21021            3 :                   noex = boolean_true_node;
   21022           12 :                 body = TREE_OPERAND (body, 0);
   21023              :               }
   21024           47 :             stmt = begin_transaction_stmt (input_location, NULL, flags);
   21025           47 :             RECUR (body);
   21026           47 :             finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
   21027              :           }
   21028              :         else
   21029              :           {
   21030           33 :             stmt = build_transaction_expr (EXPR_LOCATION (t),
   21031           33 :                                            RECUR (TRANSACTION_EXPR_BODY (t)),
   21032              :                                            flags, NULL_TREE);
   21033           33 :             RETURN (stmt);
   21034              :           }
   21035              :       }
   21036           47 :       break;
   21037              : 
   21038           36 :     case OMP_INTEROP:
   21039           36 :       tmp = tsubst_omp_clauses (OMP_INTEROP_CLAUSES (t), C_ORT_OMP_INTEROP,
   21040              :                                 args, complain, in_decl);
   21041           36 :       t = copy_node (t);
   21042           36 :       OMP_INTEROP_CLAUSES (t) = tmp;
   21043           36 :       add_stmt (t);
   21044           36 :       break;
   21045              : 
   21046           21 :     case MUST_NOT_THROW_EXPR:
   21047           21 :       {
   21048           21 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21049           21 :         tree cond = RECUR (MUST_NOT_THROW_COND (t));
   21050           21 :         stmt = build_must_not_throw_expr (op0, cond);
   21051           21 :         if (stmt && TREE_CODE (stmt) == MUST_NOT_THROW_EXPR)
   21052              :           {
   21053           18 :             MUST_NOT_THROW_NOEXCEPT_P (stmt) = MUST_NOT_THROW_NOEXCEPT_P (t);
   21054           18 :             MUST_NOT_THROW_THROW_P (stmt) = MUST_NOT_THROW_THROW_P (t);
   21055           18 :             MUST_NOT_THROW_CATCH_P (stmt) = MUST_NOT_THROW_CATCH_P (t);
   21056              :           }
   21057           21 :         RETURN (stmt);
   21058              :       }
   21059              : 
   21060            0 :     case EXPR_PACK_EXPANSION:
   21061            0 :       error ("invalid use of pack expansion expression");
   21062            0 :       RETURN (error_mark_node);
   21063              : 
   21064            0 :     case NONTYPE_ARGUMENT_PACK:
   21065            0 :       error ("use %<...%> to expand argument pack");
   21066            0 :       RETURN (error_mark_node);
   21067              : 
   21068       221196 :     case COMPOUND_EXPR:
   21069       221196 :       tmp = RECUR (TREE_OPERAND (t, 0));
   21070       221196 :       if (tmp == NULL_TREE)
   21071              :         /* If the first operand was a statement, we're done with it.  */
   21072       144200 :         RETURN (RECUR (TREE_OPERAND (t, 1)));
   21073        76996 :       RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
   21074              :                                     RECUR (TREE_OPERAND (t, 1)),
   21075              :                                     templated_operator_saved_lookups (t),
   21076              :                                     complain));
   21077              : 
   21078       322210 :     case PREDICT_EXPR:
   21079       322210 :       RETURN (add_stmt (copy_node (t)));
   21080              : 
   21081        10422 :     case ANNOTATE_EXPR:
   21082        10422 :       {
   21083              :         /* Although ANNOTATE_EXPR is an expression, it can only appear in
   21084              :            WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
   21085              :            using tsubst_stmt rather than tsubst_expr and can contain
   21086              :            DECL_EXPRs.  */
   21087        10422 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   21088        10422 :         tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   21089        10422 :         tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
   21090        10422 :         if (TREE_CODE (op2) == INTEGER_CST
   21091        10422 :             && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
   21092        10407 :           op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
   21093              :                                         op3);
   21094        10422 :         RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
   21095              :                             TREE_TYPE (op1), op1, op2, op3));
   21096              :       }
   21097              : 
   21098    164344387 :     default:
   21099    164344387 :       gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
   21100              : 
   21101    164344387 :       RETURN (tsubst_expr (t, args, complain, in_decl));
   21102              :     }
   21103              : 
   21104    206506870 :   RETURN (NULL_TREE);
   21105    371405141 :  out:
   21106    371405141 :   input_location = loc;
   21107    371405141 :   return r;
   21108              : #undef RECUR
   21109              : #undef RETURN
   21110              : }
   21111              : 
   21112              : /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
   21113              :    function.  For description of the body see comment above
   21114              :    cp_parser_omp_declare_reduction_exprs.  */
   21115              : 
   21116              : static void
   21117          104 : tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   21118              : {
   21119          104 :   if (t == NULL_TREE || t == error_mark_node)
   21120            0 :     return;
   21121              : 
   21122          104 :   gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
   21123              : 
   21124          104 :   tree_stmt_iterator tsi;
   21125          104 :   int i;
   21126          104 :   tree stmts[7];
   21127          104 :   memset (stmts, 0, sizeof stmts);
   21128          104 :   for (i = 0, tsi = tsi_start (t);
   21129          619 :        i < 7 && !tsi_end_p (tsi);
   21130          515 :        i++, tsi_next (&tsi))
   21131          515 :     stmts[i] = tsi_stmt (tsi);
   21132          104 :   gcc_assert (tsi_end_p (tsi));
   21133              : 
   21134          104 :   if (i >= 3)
   21135              :     {
   21136          104 :       gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
   21137              :                   && TREE_CODE (stmts[1]) == DECL_EXPR);
   21138          104 :       tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
   21139              :                              args, complain, in_decl);
   21140          104 :       tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
   21141              :                             args, complain, in_decl);
   21142              :       /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
   21143              :          expect to be pushing it.  */
   21144          104 :       DECL_CONTEXT (omp_out) = current_function_decl;
   21145          104 :       DECL_CONTEXT (omp_in) = current_function_decl;
   21146          104 :       keep_next_level (true);
   21147          104 :       tree block = begin_omp_structured_block ();
   21148          104 :       tsubst_stmt (stmts[2], args, complain, in_decl);
   21149          104 :       block = finish_omp_structured_block (block);
   21150          104 :       block = maybe_cleanup_point_expr_void (block);
   21151          104 :       add_decl_expr (omp_out);
   21152          104 :       copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
   21153          104 :       add_decl_expr (omp_in);
   21154          104 :       finish_expr_stmt (block);
   21155              :     }
   21156          104 :   if (i >= 6)
   21157              :     {
   21158           57 :       gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
   21159              :                   && TREE_CODE (stmts[4]) == DECL_EXPR);
   21160           57 :       tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
   21161              :                               args, complain, in_decl);
   21162           57 :       tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
   21163              :                               args, complain, in_decl);
   21164           57 :       DECL_CONTEXT (omp_priv) = current_function_decl;
   21165           57 :       DECL_CONTEXT (omp_orig) = current_function_decl;
   21166           57 :       keep_next_level (true);
   21167           57 :       tree block = begin_omp_structured_block ();
   21168           57 :       tsubst_stmt (stmts[5], args, complain, in_decl);
   21169           57 :       block = finish_omp_structured_block (block);
   21170           57 :       block = maybe_cleanup_point_expr_void (block);
   21171           57 :       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
   21172           57 :       add_decl_expr (omp_priv);
   21173           57 :       add_decl_expr (omp_orig);
   21174           57 :       finish_expr_stmt (block);
   21175           57 :       if (i == 7)
   21176           32 :         add_decl_expr (omp_orig);
   21177              :     }
   21178              : }
   21179              : 
   21180              : /* T is a postfix-expression that is not being used in a function
   21181              :    call.  Return the substituted version of T.  */
   21182              : 
   21183              : static tree
   21184     38707633 : tsubst_non_call_postfix_expression (tree t, tree args,
   21185              :                                     tsubst_flags_t complain,
   21186              :                                     tree in_decl)
   21187              : {
   21188     38707633 :   if (TREE_CODE (t) == SCOPE_REF)
   21189        26155 :     t = tsubst_qualified_id (t, args, complain, in_decl,
   21190              :                              /*done=*/false, /*address_p=*/false);
   21191              :   else
   21192     38681478 :     t = tsubst_expr (t, args, complain, in_decl);
   21193              : 
   21194     38707633 :   return t;
   21195              : }
   21196              : 
   21197              : /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
   21198              :    LAMBDA_EXPR_CAPTURE_LIST passed in LIST.  Do deduction for a previously
   21199              :    dependent init-capture.  EXPLICIT_P is true if the original list had
   21200              :    explicit captures.  */
   21201              : 
   21202              : static void
   21203       334407 : prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
   21204              :                      tsubst_flags_t complain)
   21205              : {
   21206       334407 :   if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
   21207              :     {
   21208          568 :       tree type = NULL_TREE;
   21209          568 :       if (!init)
   21210              :         {
   21211            3 :           if (complain & tf_error)
   21212            3 :             error ("empty initializer in lambda init-capture");
   21213            3 :           init = error_mark_node;
   21214              :         }
   21215          565 :       else if (TREE_CODE (init) == TREE_LIST)
   21216            0 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   21217          568 :       if (!type)
   21218          568 :         type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
   21219          568 :       TREE_TYPE (field) = type;
   21220          568 :       cp_apply_type_quals_to_decl (cp_type_quals (type), field);
   21221              :     }
   21222       334407 :   list = tree_cons (field, init, list);
   21223       334407 :   LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
   21224       334407 : }
   21225              : 
   21226              : /* T is a LAMBDA_EXPR.  Generate a new LAMBDA_EXPR for the current
   21227              :    instantiation context.  Instantiating a pack expansion containing a lambda
   21228              :    might result in multiple lambdas all based on the same lambda in the
   21229              :    template.  */
   21230              : 
   21231              : tree
   21232       248694 : tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   21233              : {
   21234       248694 :   tree oldfn = lambda_function (t);
   21235       248694 :   in_decl = oldfn;
   21236              : 
   21237       248694 :   args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
   21238       248694 :   if (processing_template_decl
   21239       248694 :       && (!in_template_context || (complain & tf_partial)
   21240         3390 :           || LAMBDA_EXPR_EXTRA_ARGS (t)))
   21241              :     {
   21242              :       /* Defer templated substitution into a lambda-expr if we lost the
   21243              :          necessary template context.  This may happen for a lambda-expr
   21244              :          used as a default template argument.
   21245              : 
   21246              :          Defer dependent substitution as well so that we don't prematurely
   21247              :          lower the level of a deduced return type or any other auto or
   21248              :          template parameter belonging to the lambda.
   21249              : 
   21250              :          Finally, if a substitution into this lambda was previously
   21251              :          deferred, keep deferring until the final (non-templated)
   21252              :          substitution.  */
   21253         1718 :       t = copy_node (t);
   21254         1718 :       LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
   21255         1718 :       LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain);
   21256         1718 :       return t;
   21257              :     }
   21258              : 
   21259       246976 :   tree r = build_lambda_expr ();
   21260              : 
   21261       246976 :   LAMBDA_EXPR_LOCATION (r) = LAMBDA_EXPR_LOCATION (t);
   21262       246976 :   LAMBDA_EXPR_CONSTEVAL_BLOCK_P (r) = LAMBDA_EXPR_CONSTEVAL_BLOCK_P (t);
   21263       246976 :   LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
   21264       246976 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   21265         5692 :     LAMBDA_EXPR_REGEN_INFO (r)
   21266         2846 :       = build_template_info (t, add_to_template_args (TI_ARGS (ti),
   21267              :                                                       preserve_args (args)));
   21268              :   else
   21269       488260 :     LAMBDA_EXPR_REGEN_INFO (r)
   21270       244130 :       = build_template_info (t, preserve_args (args));
   21271              : 
   21272       246976 :   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
   21273              : 
   21274       246976 :   vec<tree,va_gc>* field_packs = NULL;
   21275       246976 :   unsigned name_independent_cnt = 0;
   21276       581059 :   for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
   21277       334083 :        cap = TREE_CHAIN (cap))
   21278              :     {
   21279       334089 :       tree ofield = TREE_PURPOSE (cap);
   21280       334089 :       tree init = TREE_VALUE (cap);
   21281       334089 :       if (PACK_EXPANSION_P (init))
   21282          386 :         init = tsubst_pack_expansion (init, args, complain, in_decl);
   21283              :       else
   21284       333703 :         init = tsubst_expr (init, args, complain, in_decl);
   21285              : 
   21286       334089 :       if (init == error_mark_node)
   21287            6 :         return error_mark_node;
   21288              : 
   21289       334083 :       if (init && TREE_CODE (init) == TREE_LIST)
   21290           12 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   21291              : 
   21292       334083 :       if (!processing_template_decl
   21293       331828 :           && init && TREE_CODE (init) != TREE_VEC
   21294       665531 :           && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
   21295              :         {
   21296              :           /* For a VLA, simply tsubsting the field type won't work, we need to
   21297              :              go through add_capture again.  XXX do we want to do this for all
   21298              :              captures?  */
   21299            9 :           tree name = (get_identifier
   21300              :                        (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
   21301            9 :           tree ftype = TREE_TYPE (ofield);
   21302            9 :           bool by_ref = (TYPE_REF_P (ftype)
   21303            9 :                          || (TREE_CODE (ftype) == DECLTYPE_TYPE
   21304            0 :                              && DECLTYPE_FOR_REF_CAPTURE (ftype)));
   21305            9 :           add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield),
   21306              :                        &name_independent_cnt);
   21307            9 :           continue;
   21308            9 :         }
   21309              : 
   21310       334074 :       if (PACK_EXPANSION_P (ofield))
   21311          374 :         ofield = PACK_EXPANSION_PATTERN (ofield);
   21312       334074 :       tree field = tsubst_decl (ofield, args, complain);
   21313              : 
   21314       334074 :       if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
   21315              :         {
   21316              :           /* Remember these for when we've pushed local_specializations.  */
   21317          171 :           vec_safe_push (field_packs, ofield);
   21318          171 :           vec_safe_push (field_packs, field);
   21319              :         }
   21320              : 
   21321       334074 :       if (field == error_mark_node)
   21322              :         return error_mark_node;
   21323              : 
   21324       334074 :       if (TREE_CODE (field) == TREE_VEC)
   21325              :         {
   21326          377 :           int len = TREE_VEC_LENGTH (field);
   21327          377 :           gcc_assert (TREE_CODE (init) == TREE_VEC
   21328              :                       && TREE_VEC_LENGTH (init) == len);
   21329         1087 :           for (int i = 0; i < len; ++i)
   21330         1420 :             prepend_one_capture (TREE_VEC_ELT (field, i),
   21331          710 :                                  TREE_VEC_ELT (init, i),
   21332          710 :                                  LAMBDA_EXPR_CAPTURE_LIST (r),
   21333          710 :                                  LAMBDA_CAPTURE_EXPLICIT_P (cap),
   21334              :                                  complain);
   21335              :         }
   21336              :       else
   21337              :         {
   21338       667394 :           prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
   21339       333697 :                                LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
   21340              : 
   21341       333697 :           if (id_equal (DECL_NAME (field), "__this"))
   21342        34448 :             LAMBDA_EXPR_THIS_CAPTURE (r) = field;
   21343              :         }
   21344              :     }
   21345              : 
   21346       246970 :   tree type = begin_lambda_type (r);
   21347       246970 :   if (type == error_mark_node)
   21348              :     {
   21349            0 :       gcc_checking_assert (!(complain & tf_error) || seen_error ());
   21350            0 :       return error_mark_node;
   21351              :     }
   21352              : 
   21353       246970 :   if (LAMBDA_EXPR_EXTRA_SCOPE (t)
   21354              :       /* When evaluating a concept we instantiate any lambda bodies
   21355              :          in the context of the evaluation.  For ABI reasons don't
   21356              :          record a scope for this instantiated lambda so we don't
   21357              :          throw off the scope counter.  */
   21358       246970 :       && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (t)) != CONCEPT_DECL)
   21359       243753 :     record_lambda_scope (r);
   21360       246970 :   if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
   21361              :     /* If we're pushed into another scope (PR105652), fix it.  */
   21362         3854 :     TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
   21363         3854 :       = TYPE_CONTEXT (TREE_TYPE (t));
   21364       246970 :   DECL_SOURCE_LOCATION (TYPE_NAME (type))
   21365       246970 :     = DECL_SOURCE_LOCATION (TYPE_NAME (TREE_TYPE (t)));
   21366       246970 :   record_lambda_scope_discriminator (r);
   21367              : 
   21368              :   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
   21369       246970 :   determine_visibility (TYPE_NAME (type));
   21370              : 
   21371       246970 :   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
   21372              : 
   21373       246970 :   tree oldtmpl = (generic_lambda_fn_p (oldfn)
   21374       246970 :                   ? DECL_TI_TEMPLATE (oldfn)
   21375        49266 :                   : NULL_TREE);
   21376              : 
   21377        49266 :   tree tparms = NULL_TREE;
   21378        49266 :   if (oldtmpl)
   21379        49266 :     tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
   21380              : 
   21381       246970 :   tree fntype = static_fn_type (oldfn);
   21382              : 
   21383       246970 :   begin_scope (sk_lambda, NULL_TREE);
   21384              : 
   21385              :   /* Like in cp_parser_lambda_expression, we need to bring the captures
   21386              :      into the lambda scope.  */
   21387       246970 :   tree ns = decl_namespace_context (type);
   21388       246970 :   push_nested_namespace (ns);
   21389       246970 :   push_nested_class (type);
   21390       246970 :   tree dummy_fco = maybe_add_dummy_lambda_op (r);
   21391       246970 :   pop_nested_class ();
   21392       246970 :   pop_nested_namespace (ns);
   21393       246970 :   push_capture_proxies (r, /*early_p=*/true);
   21394              : 
   21395       246970 :   tree saved_ctp = current_template_parms;
   21396       246970 :   if (oldtmpl)
   21397              :     {
   21398        49266 :       ++processing_template_decl;
   21399        49266 :       current_template_parms = tparms;
   21400              :     }
   21401       246970 :   fntype = tsubst (fntype, args, complain, in_decl);
   21402       246970 :   if (oldtmpl)
   21403              :     {
   21404        49266 :       current_template_parms = saved_ctp;
   21405        49266 :       --processing_template_decl;
   21406              :     }
   21407              : 
   21408              :   /* We are about to create the real operator(), so get rid of the old one.  */
   21409       246970 :   if (dummy_fco)
   21410       156635 :     remove_dummy_lambda_op (dummy_fco, r);
   21411              : 
   21412       246970 :   if (fntype == error_mark_node)
   21413              :     r = error_mark_node;
   21414              :   else
   21415              :     {
   21416              :       /* Fix the type of 'this'.
   21417              :          For static and xobj member functions we use this to transport the
   21418              :          lambda's closure type.  It appears that in the regular case the
   21419              :          object parameter is still pulled off, and then re-added again anyway.
   21420              :          So perhaps we could do something better here?  */
   21421       246958 :       fntype = build_memfn_type (fntype, type,
   21422              :                                  type_memfn_quals (fntype),
   21423              :                                  type_memfn_rqual (fntype));
   21424       246958 :       tree inst = (oldtmpl
   21425       246958 :                    ? tsubst_template_decl (oldtmpl, args, complain,
   21426              :                                            fntype, tparms)
   21427       197692 :                    : tsubst_function_decl (oldfn, args, complain, fntype));
   21428       246958 :       if (inst == error_mark_node)
   21429              :         {
   21430            0 :           r = error_mark_node;
   21431            0 :           goto out;
   21432              :         }
   21433       246958 :       finish_member_declaration (inst);
   21434       246958 :       record_lambda_scope_sig_discriminator (r, inst);
   21435              : 
   21436       296224 :       tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
   21437              : 
   21438              :       /* Let finish_function set this.  */
   21439       246958 :       DECL_DECLARED_CONSTEXPR_P (fn) = false;
   21440              : 
   21441              :       /* The body of a lambda-expression is not a subexpression of the
   21442              :          enclosing expression.  */
   21443       246958 :       cp_evaluated ev;
   21444              : 
   21445              :       /* Now we're done with the parameter-declaration-clause, and should
   21446              :          assume "const" unless "mutable" was present.  */
   21447       246958 :       LAMBDA_EXPR_CONST_QUAL_P (r) = LAMBDA_EXPR_CONST_QUAL_P (t);
   21448              : 
   21449       246958 :       bool nested = cfun;
   21450       246958 :       if (nested)
   21451       236002 :         push_function_context ();
   21452              :       else
   21453              :         /* Still increment function_depth so that we don't GC in the
   21454              :            middle of an expression.  */
   21455        10956 :         ++function_depth;
   21456              : 
   21457       246958 :       local_specialization_stack s (lss_copy);
   21458              : 
   21459       246958 :       bool save_in_consteval_if_p = in_consteval_if_p;
   21460       246958 :       in_consteval_if_p = false;
   21461              : 
   21462       246958 :       tree body = start_lambda_function (fn, r);
   21463              : 
   21464              :       /* Now record them for lookup_init_capture_pack.  */
   21465       246958 :       int fplen = vec_safe_length (field_packs);
   21466       247129 :       for (int i = 0; i < fplen; )
   21467              :         {
   21468          171 :           tree pack = (*field_packs)[i++];
   21469          171 :           tree inst = (*field_packs)[i++];
   21470          171 :           register_local_specialization (inst, pack);
   21471              :         }
   21472       246958 :       release_tree_vector (field_packs);
   21473              : 
   21474       246958 :       register_parameter_specializations (oldfn, fn);
   21475              : 
   21476       246958 :       if (oldtmpl)
   21477              :         {
   21478              :           /* We might not partially instantiate some parts of the function, so
   21479              :              copy these flags from the original template.  */
   21480        49266 :           language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
   21481        49266 :           current_function_returns_value = ol->returns_value;
   21482        49266 :           current_function_returns_null = ol->returns_null;
   21483        49266 :           current_function_returns_abnormally = ol->returns_abnormally;
   21484        49266 :           current_function_infinite_loop = ol->infinite_loop;
   21485              :         }
   21486              : 
   21487              :       /* [temp.deduct] A lambda-expression appearing in a function type or a
   21488              :          template parameter is not considered part of the immediate context for
   21489              :          the purposes of template argument deduction. */
   21490       246958 :       if (!emitting_diagnostic_p ())
   21491       246925 :         complain = tf_warning_or_error;
   21492              : 
   21493       246958 :       tree saved = DECL_SAVED_TREE (oldfn);
   21494       246958 :       if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
   21495              :         /* We already have a body block from start_lambda_function, we don't
   21496              :            need another to confuse NRV (91217).  */
   21497       246958 :         saved = BIND_EXPR_BODY (saved);
   21498              : 
   21499       246958 :       tsubst_stmt (saved, args, complain, r);
   21500              : 
   21501       246958 :       finish_lambda_function (body);
   21502              : 
   21503       246958 :       in_consteval_if_p = save_in_consteval_if_p;
   21504              : 
   21505       246958 :       if (nested)
   21506       236002 :         pop_function_context ();
   21507              :       else
   21508        10956 :         --function_depth;
   21509              : 
   21510              :       /* The capture list was built up in reverse order; fix that now.  */
   21511       246958 :       LAMBDA_EXPR_CAPTURE_LIST (r)
   21512       246958 :         = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
   21513              : 
   21514       246958 :       maybe_add_lambda_conv_op (type);
   21515       246958 :     }
   21516              : 
   21517       246970 : out:
   21518       246970 :   pop_bindings_and_leave_scope ();
   21519       246970 :   finish_struct (type, /*attr*/NULL_TREE);
   21520              : 
   21521       246970 :   insert_pending_capture_proxies ();
   21522              : 
   21523       246970 :   return r;
   21524              : }
   21525              : 
   21526              : /* Subroutine of maybe_fold_fn_template_args.  */
   21527              : 
   21528              : static bool
   21529     37297244 : fold_targs_r (tree targs, tsubst_flags_t complain)
   21530              : {
   21531     37297244 :   int len = TREE_VEC_LENGTH (targs);
   21532     80067633 :   for (int i = 0; i < len; ++i)
   21533              :     {
   21534     42770392 :       tree &elt = TREE_VEC_ELT (targs, i);
   21535     42770392 :       if (!elt || TYPE_P (elt)
   21536       722786 :           || TREE_CODE (elt) == TEMPLATE_DECL)
   21537     42048812 :         continue;
   21538       721580 :       if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
   21539              :         {
   21540            0 :           if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
   21541              :             return false;
   21542              :         }
   21543       721580 :       else if (/* We can only safely preevaluate scalar prvalues.  */
   21544       721580 :                SCALAR_TYPE_P (TREE_TYPE (elt))
   21545       713662 :                && !glvalue_p (elt)
   21546       696856 :                && !TREE_CONSTANT (elt))
   21547              :         {
   21548         3801 :           elt = cxx_constant_value (elt, complain);
   21549         3801 :           if (elt == error_mark_node)
   21550              :             return false;
   21551              :         }
   21552              :     }
   21553              : 
   21554              :   return true;
   21555              : }
   21556              : 
   21557              : /* Try to do constant evaluation of any explicit template arguments in FN
   21558              :    before overload resolution, to get any errors only once.  Return true iff
   21559              :    we didn't have any problems folding.  */
   21560              : 
   21561              : static bool
   21562    119614837 : maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
   21563              : {
   21564    119614837 :   if (processing_template_decl || fn == NULL_TREE)
   21565              :     return true;
   21566    110963990 :   if (fn == error_mark_node)
   21567              :     return false;
   21568    110621582 :   if (TREE_CODE (fn) == OFFSET_REF
   21569    110554262 :       || TREE_CODE (fn) == COMPONENT_REF)
   21570     16957853 :     fn = TREE_OPERAND (fn, 1);
   21571    110621582 :   if (BASELINK_P (fn))
   21572     27819665 :     fn = BASELINK_FUNCTIONS (fn);
   21573    110621582 :   if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
   21574              :     return true;
   21575     37297244 :   tree targs = TREE_OPERAND (fn, 1);
   21576     37297244 :   if (targs == NULL_TREE)
   21577              :     return true;
   21578     37297244 :   if (targs == error_mark_node)
   21579              :     return false;
   21580     37297244 :   return fold_targs_r (targs, complain);
   21581              : }
   21582              : 
   21583              : /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling.  */
   21584              : 
   21585              : static void
   21586    119674455 : tsubst_call_args (tree t, tree args, tsubst_flags_t complain,
   21587              :                   tree in_decl, releasing_vec &call_args)
   21588              : {
   21589    119674455 :   unsigned int nargs = call_expr_nargs (t);
   21590    206086291 :   for (unsigned int i = 0; i < nargs; ++i)
   21591              :     {
   21592     86411839 :       tree arg = CALL_EXPR_ARG (t, i);
   21593              : 
   21594     86411839 :       if (!PACK_EXPANSION_P (arg))
   21595     85764975 :         vec_safe_push (call_args, tsubst_expr (arg, args, complain, in_decl));
   21596              :       else
   21597              :         {
   21598              :           /* Expand the pack expansion and push each entry onto CALL_ARGS.  */
   21599       646864 :           arg = tsubst_pack_expansion (arg, args, complain, in_decl);
   21600       646864 :           if (TREE_CODE (arg) == TREE_VEC)
   21601              :             {
   21602       586083 :               unsigned int len, j;
   21603              : 
   21604       586083 :               len = TREE_VEC_LENGTH (arg);
   21605      1055723 :               for (j = 0; j < len; ++j)
   21606              :                 {
   21607       469640 :                   tree value = TREE_VEC_ELT (arg, j);
   21608       469640 :                   if (value != NULL_TREE)
   21609       469640 :                     value = convert_from_reference (value);
   21610       469640 :                   vec_safe_push (call_args, value);
   21611              :                 }
   21612              :             }
   21613              :           else
   21614              :             /* A partial substitution.  Add one entry.  */
   21615        60781 :             vec_safe_push (call_args, arg);
   21616              :         }
   21617              :     }
   21618    119674452 : }
   21619              : 
   21620              : /* Like tsubst but deals with expressions and performs semantic
   21621              :    analysis.  */
   21622              : 
   21623              : tree
   21624   1929193678 : tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   21625              : {
   21626              : #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
   21627              : #define RECUR(NODE)                                             \
   21628              :   tsubst_expr (NODE, args, complain, in_decl)
   21629              : 
   21630   1929193678 :   tree retval, op1;
   21631   1929193678 :   location_t save_loc;
   21632              : 
   21633   1929193678 :   if (t == NULL_TREE || t == error_mark_node)
   21634              :     return t;
   21635              : 
   21636   1924867717 :   save_loc = input_location;
   21637   1924867717 :   if (location_t eloc = cp_expr_location (t))
   21638    761879796 :     input_location = eloc;
   21639              : 
   21640              :   /* N3276 decltype magic only applies to calls at the top level or on the
   21641              :      right side of a comma.  */
   21642   1924867717 :   tsubst_flags_t decltype_flag = (complain & tf_decltype);
   21643   1924867717 :   complain &= ~tf_decltype;
   21644              : 
   21645              :   /* This flag only applies to id-expressions at the top level, and
   21646              :      controls resolution thereof.  */
   21647   1924867717 :   tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup);
   21648   1924867717 :   complain &= ~tf_no_name_lookup;
   21649              : 
   21650   1924867717 :   if (instantiating_tu_local_entity (t))
   21651           52 :     RETURN (error_mark_node);
   21652              : 
   21653   1924867665 :   if (!no_name_lookup_flag)
   21654   1570291716 :     if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   21655              :       return d;
   21656              : 
   21657   1924865378 :   switch (TREE_CODE (t))
   21658              :     {
   21659       176912 :     case USING_DECL:
   21660       176912 :       t = DECL_NAME (t);
   21661              :       /* Fall through.  */
   21662    337550450 :     case IDENTIFIER_NODE:
   21663    337550450 :       {
   21664    337550450 :         tree decl;
   21665    337550450 :         cp_id_kind idk;
   21666    337550450 :         const char *error_msg;
   21667              : 
   21668    337550450 :         if (IDENTIFIER_CONV_OP_P (t))
   21669              :           {
   21670         2728 :             tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21671         2728 :             t = make_conv_op_name (new_type);
   21672              :           }
   21673              : 
   21674    337550450 :         if (no_name_lookup_flag)
   21675    337284923 :           RETURN (t);
   21676              : 
   21677              :         /* Look up the name.  */
   21678       265527 :         decl = lookup_name (t);
   21679              : 
   21680              :         /* By convention, expressions use ERROR_MARK_NODE to indicate
   21681              :            failure, not NULL_TREE.  */
   21682       265527 :         if (decl == NULL_TREE)
   21683           57 :           decl = error_mark_node;
   21684              : 
   21685       265527 :         decl = finish_id_expression (t, decl, NULL_TREE,
   21686              :                                      &idk,
   21687              :                                      /*i_c_e_p=*/false,
   21688              :                                      /*allow_i_c_e_p=*/true,
   21689              :                                      /*non_i_c_e_p=*/nullptr,
   21690              :                                      /*template_p=*/false,
   21691              :                                      /*done=*/true,
   21692              :                                      /*address_p=*/false,
   21693              :                                      /*template_arg_p=*/false,
   21694              :                                      &error_msg,
   21695              :                                      input_location);
   21696       265527 :         if (error_msg)
   21697            0 :           error (error_msg);
   21698       265527 :         if (identifier_p (decl))
   21699              :           {
   21700           57 :             if (complain & tf_error)
   21701           54 :               unqualified_name_lookup_error (decl);
   21702           57 :             decl = error_mark_node;
   21703              :           }
   21704    337550450 :         RETURN (decl);
   21705              :       }
   21706              : 
   21707     68611323 :     case TEMPLATE_ID_EXPR:
   21708     68611323 :       {
   21709     68611323 :         tree object;
   21710     68611323 :         tree templ = TREE_OPERAND (t, 0);
   21711     68611323 :         tree targs = TREE_OPERAND (t, 1);
   21712     68611323 :         tsubst_flags_t complain_lookup = complain | no_name_lookup_flag;
   21713              : 
   21714     68611323 :         if (TREE_CODE (templ) == SPLICE_EXPR)
   21715          294 :           return tsubst_splice_expr (t, args, complain_lookup, in_decl);
   21716              : 
   21717     68611029 :         templ = tsubst_expr (templ, args, complain_lookup, in_decl);
   21718              : 
   21719     68611029 :         if (targs)
   21720     68611029 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   21721     68611029 :         if (targs == error_mark_node)
   21722         5820 :           RETURN (error_mark_node);
   21723              : 
   21724     68605209 :         if (TREE_CODE (templ) == SCOPE_REF)
   21725              :           {
   21726           47 :             tree name = TREE_OPERAND (templ, 1);
   21727           47 :             tree tid = lookup_template_function (name, targs);
   21728           47 :             TREE_OPERAND (templ, 1) = tid;
   21729           47 :             RETURN (templ);
   21730              :           }
   21731              : 
   21732     94805711 :         if (concept_definition_p (templ))
   21733              :           {
   21734      3383152 :             tree check = build_concept_check (templ, targs, complain);
   21735      3383152 :             if (check == error_mark_node)
   21736              :               RETURN (error_mark_node);
   21737     65222010 :             RETURN (check);
   21738              :           }
   21739              : 
   21740     65222010 :         if (variable_template_p (templ))
   21741              :           {
   21742     22817397 :             if (no_name_lookup_flag)
   21743          127 :               RETURN (lookup_template_variable (templ, targs, complain));
   21744              : 
   21745     22817270 :             tree r = lookup_and_finish_template_variable (templ, targs,
   21746              :                                                           complain);
   21747     22817270 :             r = convert_from_reference (r);
   21748     22817270 :             r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
   21749     22817270 :             RETURN (r);
   21750              :           }
   21751              : 
   21752     42404613 :         if (TREE_CODE (templ) == COMPONENT_REF)
   21753              :           {
   21754            0 :             object = TREE_OPERAND (templ, 0);
   21755            0 :             templ = TREE_OPERAND (templ, 1);
   21756              :           }
   21757              :         else
   21758              :           object = NULL_TREE;
   21759              : 
   21760     42404613 :         tree tid = lookup_template_function (templ, targs);
   21761     42404613 :         protected_set_expr_location (tid, EXPR_LOCATION (t));
   21762              : 
   21763     42404613 :         if (object)
   21764            0 :           RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
   21765              :                          object, tid, NULL_TREE));
   21766     42404613 :         else if (no_name_lookup_flag)
   21767      4943759 :           RETURN (tid);
   21768     37460854 :         else if (identifier_p (templ))
   21769              :           {
   21770              :             /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
   21771              :                name lookup found nothing when parsing the template name.  */
   21772            0 :             gcc_assert (cxx_dialect >= cxx20 || seen_error ());
   21773            0 :             RETURN (tid);
   21774              :           }
   21775              :         else
   21776     37460854 :           RETURN (baselink_for_fns (tid));
   21777              :       }
   21778              : 
   21779     53178522 :     case INDIRECT_REF:
   21780     53178522 :       {
   21781     53178522 :         tree r = RECUR (TREE_OPERAND (t, 0));
   21782              : 
   21783     53178522 :         if (REFERENCE_REF_P (t))
   21784              :           {
   21785              :             /* A type conversion to reference type will be enclosed in
   21786              :                such an indirect ref, but the substitution of the cast
   21787              :                will have also added such an indirect ref.  */
   21788     33485359 :             r = convert_from_reference (r);
   21789              :           }
   21790              :         else
   21791     19693163 :           r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
   21792              :                                     templated_operator_saved_lookups (t),
   21793              :                                     complain|decltype_flag);
   21794              : 
   21795     53178522 :         if (REF_PARENTHESIZED_P (t))
   21796        10606 :           r = force_paren_expr (r);
   21797              : 
   21798     53178522 :         RETURN (r);
   21799              :       }
   21800              : 
   21801            9 :     case MEM_REF:
   21802            9 :       {
   21803            9 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21804            9 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   21805            9 :         tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21806            9 :         RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1));
   21807              :       }
   21808              : 
   21809     18423299 :     case NOP_EXPR:
   21810     18423299 :       {
   21811     18423299 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21812     18423299 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21813     18423296 :         RETURN (build_nop (type, op0));
   21814              :       }
   21815              : 
   21816     30468636 :     case IMPLICIT_CONV_EXPR:
   21817     30468636 :       {
   21818     30468636 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21819     30468636 :         if (type == error_mark_node)
   21820            3 :           RETURN (error_mark_node);
   21821     30468633 :         tree expr = RECUR (TREE_OPERAND (t, 0));
   21822     30468633 :         if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
   21823              :           {
   21824     29469259 :             tree r = (convert_nontype_argument_maybe_dependent
   21825     29469259 :                       (type, expr, IMPLICIT_CONV_EXPR_FORCED (t), complain));
   21826     29469259 :             if (r == NULL_TREE)
   21827           37 :               r = error_mark_node;
   21828     29469259 :             RETURN (r);
   21829              :           }
   21830       999374 :         int flags = LOOKUP_IMPLICIT;
   21831       999374 :         if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
   21832       234150 :           flags = LOOKUP_NORMAL;
   21833       999374 :         if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
   21834        23381 :           flags |= LOOKUP_NO_NARROWING;
   21835       999374 :         RETURN (perform_implicit_conversion_flags (type, expr, complain,
   21836              :                                                   flags));
   21837              :       }
   21838              : 
   21839        22398 :     case CONVERT_EXPR:
   21840        22398 :       {
   21841        22398 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21842        22398 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21843        22398 :         if (op0 == error_mark_node)
   21844           21 :           RETURN (error_mark_node);
   21845        22377 :         RETURN (build1 (CONVERT_EXPR, type, op0));
   21846              :       }
   21847              : 
   21848     37051713 :     case CAST_EXPR:
   21849     37051713 :     case REINTERPRET_CAST_EXPR:
   21850     37051713 :     case CONST_CAST_EXPR:
   21851     37051713 :     case DYNAMIC_CAST_EXPR:
   21852     37051713 :     case STATIC_CAST_EXPR:
   21853     37051713 :       {
   21854     37051713 :         tree type;
   21855     37051713 :         tree op, r = NULL_TREE;
   21856              : 
   21857     37051713 :         tsubst_flags_t tcomplain = complain;
   21858     37051713 :         if (TREE_CODE (t) == CAST_EXPR)
   21859     30224172 :           tcomplain |= tf_tst_ok;
   21860     37051713 :         type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   21861              : 
   21862     37051713 :         op = RECUR (TREE_OPERAND (t, 0));
   21863              : 
   21864     37051713 :         warning_sentinel s(warn_useless_cast);
   21865     37051713 :         warning_sentinel s2(warn_ignored_qualifiers);
   21866     37051713 :         warning_sentinel s3(warn_int_in_bool_context);
   21867     37051713 :         switch (TREE_CODE (t))
   21868              :           {
   21869     30224172 :           case CAST_EXPR:
   21870     30224172 :             r = build_functional_cast (input_location, type, op, complain);
   21871     30224172 :             break;
   21872        90139 :           case REINTERPRET_CAST_EXPR:
   21873        90139 :             r = build_reinterpret_cast (input_location, type, op, complain);
   21874        90139 :             break;
   21875       188741 :           case CONST_CAST_EXPR:
   21876       188741 :             r = build_const_cast (input_location, type, op, complain);
   21877       188741 :             break;
   21878         4941 :           case DYNAMIC_CAST_EXPR:
   21879         4941 :             r = build_dynamic_cast (input_location, type, op, complain);
   21880         4941 :             break;
   21881      6543720 :           case STATIC_CAST_EXPR:
   21882      6543720 :             r = build_static_cast (input_location, type, op, complain);
   21883      6543720 :             if (IMPLICIT_RVALUE_P (t))
   21884            0 :               set_implicit_rvalue_p (r);
   21885              :             break;
   21886            0 :           default:
   21887            0 :             gcc_unreachable ();
   21888              :           }
   21889              : 
   21890     37051710 :         RETURN (r);
   21891     37051710 :       }
   21892              : 
   21893        76872 :     case BIT_CAST_EXPR:
   21894        76872 :       {
   21895        76872 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21896        76872 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21897        76872 :         RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
   21898              :       }
   21899              : 
   21900       982378 :     case POSTDECREMENT_EXPR:
   21901       982378 :     case POSTINCREMENT_EXPR:
   21902       982378 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21903              :                                                 args, complain, in_decl);
   21904       982378 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
   21905              :                                 templated_operator_saved_lookups (t),
   21906              :                                 complain|decltype_flag));
   21907              : 
   21908       583734 :     case BIT_NOT_EXPR:
   21909       583734 :       if (identifier_p (TREE_OPERAND (t, 0)))
   21910              :         {
   21911           23 :           gcc_checking_assert (no_name_lookup_flag);
   21912           23 :           RETURN (t);
   21913              :         }
   21914       583711 :       else if (TYPE_P (TREE_OPERAND (t, 0)))
   21915              :         {
   21916       365546 :           gcc_checking_assert (no_name_lookup_flag);
   21917       365546 :           tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
   21918       365546 :           RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0));
   21919              :         }
   21920              :       /* Fall through.  */
   21921     15503359 :     case PREDECREMENT_EXPR:
   21922     15503359 :     case PREINCREMENT_EXPR:
   21923     15503359 :     case NEGATE_EXPR:
   21924     15503359 :     case ABS_EXPR:
   21925     15503359 :     case TRUTH_NOT_EXPR:
   21926     15503359 :     case UNARY_PLUS_EXPR:  /* Unary + */
   21927     15503359 :     case REALPART_EXPR:
   21928     15503359 :     case IMAGPART_EXPR:
   21929     15503359 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t),
   21930              :                                 RECUR (TREE_OPERAND (t, 0)),
   21931              :                                 templated_operator_saved_lookups (t),
   21932              :                                 complain|decltype_flag));
   21933              : 
   21934         2872 :     case EXCESS_PRECISION_EXPR:
   21935         2872 :       {
   21936         2872 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21937         2872 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21938         2872 :         if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
   21939            0 :           RETURN (op0);
   21940         2872 :         RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
   21941              :                             type, op0));
   21942              :       }
   21943              : 
   21944            0 :     case FIX_TRUNC_EXPR:
   21945              :       /* convert_like should have created an IMPLICIT_CONV_EXPR.  */
   21946            0 :       gcc_unreachable ();
   21947              : 
   21948      1159683 :     case ADDR_EXPR:
   21949      1159683 :       op1 = TREE_OPERAND (t, 0);
   21950      1159683 :       if (TREE_CODE (op1) == LABEL_DECL)
   21951           31 :         RETURN (finish_label_address_expr (DECL_NAME (op1),
   21952              :                                           EXPR_LOCATION (op1)));
   21953      1159652 :       if (TREE_CODE (op1) == SCOPE_REF)
   21954        74968 :         op1 = tsubst_qualified_id (op1, args, complain, in_decl,
   21955              :                                    /*done=*/true, /*address_p=*/true);
   21956              :       else
   21957      1084684 :         op1 = tsubst_non_call_postfix_expression (op1, args, complain,
   21958              :                                                   in_decl);
   21959      1159652 :       RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
   21960              :                                 templated_operator_saved_lookups (t),
   21961              :                                 complain|decltype_flag));
   21962              : 
   21963     84355420 :     case PLUS_EXPR:
   21964     84355420 :     case MINUS_EXPR:
   21965     84355420 :     case MULT_EXPR:
   21966     84355420 :     case TRUNC_DIV_EXPR:
   21967     84355420 :     case CEIL_DIV_EXPR:
   21968     84355420 :     case FLOOR_DIV_EXPR:
   21969     84355420 :     case ROUND_DIV_EXPR:
   21970     84355420 :     case EXACT_DIV_EXPR:
   21971     84355420 :     case BIT_AND_EXPR:
   21972     84355420 :     case BIT_IOR_EXPR:
   21973     84355420 :     case BIT_XOR_EXPR:
   21974     84355420 :     case TRUNC_MOD_EXPR:
   21975     84355420 :     case FLOOR_MOD_EXPR:
   21976     84355420 :     case TRUTH_ANDIF_EXPR:
   21977     84355420 :     case TRUTH_ORIF_EXPR:
   21978     84355420 :     case TRUTH_AND_EXPR:
   21979     84355420 :     case TRUTH_OR_EXPR:
   21980     84355420 :     case RSHIFT_EXPR:
   21981     84355420 :     case LSHIFT_EXPR:
   21982     84355420 :     case EQ_EXPR:
   21983     84355420 :     case NE_EXPR:
   21984     84355420 :     case MAX_EXPR:
   21985     84355420 :     case MIN_EXPR:
   21986     84355420 :     case LE_EXPR:
   21987     84355420 :     case GE_EXPR:
   21988     84355420 :     case LT_EXPR:
   21989     84355420 :     case GT_EXPR:
   21990     84355420 :     case SPACESHIP_EXPR:
   21991     84355420 :     case MEMBER_REF:
   21992     84355420 :     case DOTSTAR_EXPR:
   21993     84355420 :       {
   21994              :         /* If either OP0 or OP1 was value- or type-dependent, suppress
   21995              :            warnings that depend on the range of the types involved.  */
   21996     84355420 :         tree op0 = TREE_OPERAND (t, 0);
   21997     84355420 :         tree op1 = TREE_OPERAND (t, 1);
   21998     84355420 :         const bool was_dep = (dependent_operand_p (op0)
   21999     84355420 :                               || dependent_operand_p (op1));
   22000     84355420 :         op0 = RECUR (op0);
   22001     84355420 :         op1 = RECUR (op1);
   22002              : 
   22003     84355375 :         warning_sentinel s1(warn_type_limits, was_dep);
   22004     84355375 :         warning_sentinel s2(warn_div_by_zero, was_dep);
   22005     84355375 :         warning_sentinel s3(warn_logical_op, was_dep);
   22006     84355375 :         warning_sentinel s4(warn_tautological_compare, was_dep);
   22007     84355375 :         warning_sentinel s5(warn_address, was_dep);
   22008              : 
   22009     84355375 :         tree r = build_x_binary_op
   22010    327424014 :           (input_location, TREE_CODE (t),
   22011              :            op0,
   22012     84355375 :            (warning_suppressed_p (TREE_OPERAND (t, 0))
   22013              :             ? ERROR_MARK
   22014     74357889 :             : TREE_CODE (TREE_OPERAND (t, 0))),
   22015              :            op1,
   22016     84355375 :            (warning_suppressed_p (TREE_OPERAND (t, 1))
   22017              :             ? ERROR_MARK
   22018     72378522 :             : TREE_CODE (TREE_OPERAND (t, 1))),
   22019              :            templated_operator_saved_lookups (t),
   22020              :            /*overload=*/NULL,
   22021              :            complain|decltype_flag);
   22022     84355345 :         if (EXPR_P (r))
   22023     84300631 :           copy_warning (r, t);
   22024              : 
   22025     84355345 :         RETURN (r);
   22026     84355345 :       }
   22027              : 
   22028            6 :     case POINTER_PLUS_EXPR:
   22029            6 :       {
   22030            6 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22031            6 :         if (op0 == error_mark_node)
   22032            0 :           RETURN (error_mark_node);
   22033            6 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   22034            6 :         if (op1 == error_mark_node)
   22035            0 :           RETURN (error_mark_node);
   22036            6 :         RETURN (fold_build_pointer_plus (op0, op1));
   22037              :       }
   22038              : 
   22039     78660059 :     case SCOPE_REF:
   22040     78660059 :       if (no_name_lookup_flag)
   22041              :         {
   22042          773 :           tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
   22043          773 :           tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl);
   22044          773 :           RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1,
   22045              :                                         QUALIFIED_NAME_IS_TEMPLATE (t)));
   22046              :         }
   22047              :       else
   22048     78659286 :         RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
   22049              :                                     /*address_p=*/false));
   22050              : 
   22051      9703280 :     case BASELINK:
   22052      9703280 :       RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
   22053              :                                args, complain, in_decl));
   22054              : 
   22055      2663185 :     case ARRAY_REF:
   22056      2663185 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   22057              :                                                 args, complain, in_decl);
   22058      2663185 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
   22059      2663185 :           && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
   22060         3836 :               == ovl_op_identifier (ARRAY_REF)))
   22061              :         {
   22062          610 :           tree c = TREE_OPERAND (t, 1);
   22063          610 :           releasing_vec index_exp_list;
   22064          610 :           tsubst_call_args (c, args, complain, in_decl, index_exp_list);
   22065              : 
   22066          610 :           tree r;
   22067          610 :           if (vec_safe_length (index_exp_list) == 1
   22068          654 :               && !PACK_EXPANSION_P (index_exp_list[0]))
   22069           44 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   22070           44 :                                  index_exp_list[0], NULL,
   22071              :                                  complain | decltype_flag);
   22072              :           else
   22073          566 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   22074              :                                  NULL_TREE, &index_exp_list,
   22075              :                                  complain | decltype_flag);
   22076          610 :           RETURN (r);
   22077          610 :         }
   22078      2662575 :       RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
   22079              :                                  RECUR (TREE_OPERAND (t, 1)),
   22080              :                                  complain|decltype_flag));
   22081              : 
   22082           30 :     case OMP_ARRAY_SECTION:
   22083           30 :       {
   22084           30 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22085           30 :         tree op1 = NULL_TREE, op2 = NULL_TREE;
   22086           30 :         if (op0 == error_mark_node)
   22087            0 :           RETURN (error_mark_node);
   22088           30 :         if (TREE_OPERAND (t, 1))
   22089              :           {
   22090           27 :             op1 = RECUR (TREE_OPERAND (t, 1));
   22091           27 :             if (op1 == error_mark_node)
   22092            0 :               RETURN (error_mark_node);
   22093              :           }
   22094           30 :         if (TREE_OPERAND (t, 2))
   22095              :           {
   22096           30 :             op2 = RECUR (TREE_OPERAND (t, 2));
   22097           30 :             if (op2 == error_mark_node)
   22098            0 :               RETURN (error_mark_node);
   22099              :           }
   22100           30 :         RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2));
   22101              :       }
   22102              : 
   22103           11 :     case OMP_DECLARE_MAPPER:
   22104           11 :       {
   22105           11 :         t = copy_node (t);
   22106              : 
   22107           11 :         tree decl = OMP_DECLARE_MAPPER_DECL (t);
   22108           11 :         DECL_OMP_DECLARE_MAPPER_P (decl) = 1;
   22109           11 :         decl = tsubst (decl, args, complain, in_decl);
   22110           11 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   22111           11 :         tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t);
   22112           11 :         clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_MAPPER, args,
   22113              :                                       complain, in_decl);
   22114           11 :         TREE_TYPE (t) = type;
   22115           11 :         OMP_DECLARE_MAPPER_DECL (t) = decl;
   22116           11 :         OMP_DECLARE_MAPPER_CLAUSES (t) = clauses;
   22117           11 :         cp_check_omp_declare_mapper (t);
   22118           11 :         RETURN (t);
   22119              :       }
   22120              : 
   22121      7999992 :     case SIZEOF_EXPR:
   22122     14179980 :       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
   22123     14171259 :           || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
   22124              :         {
   22125      1828737 :           tree expanded, op = TREE_OPERAND (t, 0);
   22126      1828737 :           int len = 0;
   22127              : 
   22128      1828737 :           if (SIZEOF_EXPR_TYPE_P (t))
   22129            0 :             op = TREE_TYPE (op);
   22130              : 
   22131      1828737 :           ++cp_unevaluated_operand;
   22132      1828737 :           ++c_inhibit_evaluation_warnings;
   22133              :           /* We only want to compute the number of arguments.  */
   22134      1828737 :           if (PACK_EXPANSION_P (op))
   22135              :             {
   22136      1828725 :               expanded = NULL_TREE;
   22137      1828725 :               if (DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (op)))
   22138              :                 {
   22139          397 :                   tree d = PACK_EXPANSION_PATTERN (op);
   22140          397 :                   if (DECL_HAS_VALUE_EXPR_P (d))
   22141              :                     {
   22142          397 :                       d = DECL_VALUE_EXPR (d);
   22143          397 :                       if (TREE_CODE (d) == TREE_VEC)
   22144              :                         {
   22145          207 :                           tree b = TREE_VEC_ELT (d, 0);
   22146          207 :                           if (!type_dependent_expression_p_push (b))
   22147              :                             {
   22148          207 :                               expanded = void_node;
   22149          207 :                               len = TREE_VEC_LENGTH (d) - 2;
   22150              :                             }
   22151              :                         }
   22152              :                     }
   22153              :                 }
   22154          207 :               if (!expanded)
   22155      1828518 :                 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
   22156              :             }
   22157              :           else
   22158           12 :             expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
   22159              :                                              args, complain, in_decl);
   22160      1828737 :           --cp_unevaluated_operand;
   22161      1828737 :           --c_inhibit_evaluation_warnings;
   22162              : 
   22163      1828737 :           if (TREE_CODE (expanded) == TREE_VEC)
   22164              :             {
   22165      1158668 :               len = TREE_VEC_LENGTH (expanded);
   22166              :               /* Set TREE_USED for the benefit of -Wunused.  */
   22167      3391176 :               for (int i = 0; i < len; i++)
   22168      2232508 :                 if (DECL_P (TREE_VEC_ELT (expanded, i)))
   22169        11148 :                   TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
   22170              :             }
   22171              : 
   22172      1828737 :           if (expanded == error_mark_node)
   22173            0 :             RETURN (error_mark_node);
   22174      1828737 :           else if (PACK_EXPANSION_P (expanded)
   22175      1828737 :                    || (TREE_CODE (expanded) == TREE_VEC
   22176      1158668 :                        && pack_expansion_args_count (expanded)))
   22177              : 
   22178              :             {
   22179       669889 :               if (PACK_EXPANSION_P (expanded))
   22180              :                 /* OK.  */;
   22181              :               else
   22182           27 :                 expanded = make_argument_pack (expanded);
   22183              : 
   22184       669889 :               if (TYPE_P (expanded))
   22185       669627 :                 RETURN (cxx_sizeof_or_alignof_type (input_location,
   22186              :                                                     expanded, SIZEOF_EXPR,
   22187              :                                                     false,
   22188              :                                                     complain & tf_error));
   22189              :               else
   22190          262 :                 RETURN (cxx_sizeof_or_alignof_expr (input_location,
   22191              :                                                     expanded, SIZEOF_EXPR,
   22192              :                                                     false,
   22193              :                                                     complain & tf_error));
   22194              :             }
   22195              :           else
   22196      1158848 :             RETURN (build_int_cst (size_type_node, len));
   22197              :         }
   22198              :       /* Fall through */
   22199              : 
   22200      6981934 :     case ALIGNOF_EXPR:
   22201      6981934 :       {
   22202      6981934 :         tree r;
   22203              : 
   22204      6981934 :         op1 = TREE_OPERAND (t, 0);
   22205      6981934 :         if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
   22206            0 :           op1 = TREE_TYPE (op1);
   22207      6981934 :         bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
   22208      6981934 :                             && ALIGNOF_EXPR_STD_P (t));
   22209      6981934 :         if (!args)
   22210              :           {
   22211              :             /* When there are no ARGS, we are trying to evaluate a
   22212              :                non-dependent expression from the parser.  Trying to do
   22213              :                the substitutions may not work.  */
   22214       216662 :             if (!TYPE_P (op1))
   22215          382 :               op1 = TREE_TYPE (op1);
   22216              :           }
   22217              :         else
   22218              :           {
   22219      6765272 :             ++cp_unevaluated_operand;
   22220      6765272 :             ++c_inhibit_evaluation_warnings;
   22221      6765272 :             if (TYPE_P (op1))
   22222      6588784 :               op1 = tsubst (op1, args, complain, in_decl);
   22223              :             else
   22224       176488 :               op1 = tsubst_expr (op1, args, complain, in_decl);
   22225      6765272 :             --cp_unevaluated_operand;
   22226      6765272 :             --c_inhibit_evaluation_warnings;
   22227              :           }
   22228      6981934 :         if (TYPE_P (op1))
   22229      6805426 :           r = cxx_sizeof_or_alignof_type (input_location,
   22230      6805426 :                                           op1, TREE_CODE (t), std_alignof,
   22231              :                                           complain & tf_error);
   22232              :         else
   22233       176508 :           r = cxx_sizeof_or_alignof_expr (input_location,
   22234       176508 :                                           op1, TREE_CODE (t), std_alignof,
   22235              :                                           complain & tf_error);
   22236      6981934 :         if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
   22237              :           {
   22238      6170438 :             if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
   22239              :               {
   22240      6170330 :                 if (!processing_template_decl && TYPE_P (op1))
   22241              :                   {
   22242      5467799 :                     r = build_min (SIZEOF_EXPR, size_type_node,
   22243              :                                    build1 (NOP_EXPR, op1, error_mark_node));
   22244      5467799 :                     SIZEOF_EXPR_TYPE_P (r) = 1;
   22245              :                   }
   22246              :                 else
   22247       702531 :                   r = build_min (SIZEOF_EXPR, size_type_node, op1);
   22248      6170330 :                 TREE_SIDE_EFFECTS (r) = 0;
   22249      6170330 :                 TREE_READONLY (r) = 1;
   22250              :               }
   22251      6170438 :             SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
   22252      6170438 :             copy_warning (r, t);
   22253              :           }
   22254      6981934 :         RETURN (r);
   22255              :       }
   22256              : 
   22257            0 :     case AT_ENCODE_EXPR:
   22258            0 :       {
   22259            0 :         op1 = TREE_OPERAND (t, 0);
   22260            0 :         ++cp_unevaluated_operand;
   22261            0 :         ++c_inhibit_evaluation_warnings;
   22262            0 :         op1 = tsubst (op1, args, complain, in_decl);
   22263            0 :         --cp_unevaluated_operand;
   22264            0 :         --c_inhibit_evaluation_warnings;
   22265            0 :         RETURN (objc_build_encode_expr (op1));
   22266              :       }
   22267              : 
   22268       898407 :     case NOEXCEPT_EXPR:
   22269       898407 :       op1 = TREE_OPERAND (t, 0);
   22270       898407 :       ++cp_unevaluated_operand;
   22271       898407 :       ++c_inhibit_evaluation_warnings;
   22272       898407 :       ++cp_noexcept_operand;
   22273       898407 :       op1 = tsubst_expr (op1, args, complain, in_decl);
   22274       898407 :       --cp_unevaluated_operand;
   22275       898407 :       --c_inhibit_evaluation_warnings;
   22276       898407 :       --cp_noexcept_operand;
   22277       898407 :       RETURN (finish_noexcept_expr (op1, complain));
   22278              : 
   22279     16834918 :     case MODOP_EXPR:
   22280     16834918 :       {
   22281     16834918 :         warning_sentinel s(warn_div_by_zero);
   22282     16834918 :         tree lhs = RECUR (TREE_OPERAND (t, 0));
   22283     16834918 :         tree rhs = RECUR (TREE_OPERAND (t, 2));
   22284              : 
   22285     16834918 :         tree r = build_x_modify_expr
   22286     16834918 :           (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
   22287              :            templated_operator_saved_lookups (t),
   22288              :            complain|decltype_flag);
   22289              :         /* TREE_NO_WARNING must be set if either the expression was
   22290              :            parenthesized or it uses an operator such as >>= rather
   22291              :            than plain assignment.  In the former case, it was already
   22292              :            set and must be copied.  In the latter case,
   22293              :            build_x_modify_expr sets it and it must not be reset
   22294              :            here.  */
   22295     16834918 :         if (warning_suppressed_p (t, OPT_Wparentheses))
   22296        23194 :           suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
   22297              : 
   22298     16834918 :         RETURN (r);
   22299     16834918 :       }
   22300              : 
   22301      3396381 :     case ARROW_EXPR:
   22302      3396381 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   22303              :                                                 args, complain, in_decl);
   22304              :       /* Remember that there was a reference to this entity.  */
   22305      3396381 :       if (DECL_P (op1)
   22306      3396381 :           && !mark_used (op1, complain) && !(complain & tf_error))
   22307            0 :         RETURN (error_mark_node);
   22308      3396381 :       RETURN (build_x_arrow (input_location, op1, complain));
   22309              : 
   22310       915050 :     case NEW_EXPR:
   22311       915050 :       {
   22312       915050 :         tree placement = RECUR (TREE_OPERAND (t, 0));
   22313       915050 :         tree init = RECUR (TREE_OPERAND (t, 3));
   22314       915050 :         vec<tree, va_gc> *placement_vec;
   22315       915050 :         vec<tree, va_gc> *init_vec;
   22316       915050 :         tree ret;
   22317       915050 :         location_t loc = EXPR_LOCATION (t);
   22318              : 
   22319       915050 :         if (placement == NULL_TREE)
   22320       244111 :           placement_vec = NULL;
   22321       670939 :         else if (placement == error_mark_node)
   22322            3 :           RETURN (error_mark_node);
   22323              :         else
   22324              :           {
   22325       670936 :             placement_vec = make_tree_vector ();
   22326      1341872 :             for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
   22327       670936 :               vec_safe_push (placement_vec, TREE_VALUE (placement));
   22328              :           }
   22329              : 
   22330              :         /* If there was an initializer in the original tree, but it
   22331              :            instantiated to an empty list, then we should pass a
   22332              :            non-NULL empty vector to tell build_new that it was an
   22333              :            empty initializer() rather than no initializer.  This can
   22334              :            only happen when the initializer is a pack expansion whose
   22335              :            parameter packs are of length zero.  */
   22336       915047 :         if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
   22337       234145 :           init_vec = NULL;
   22338       680902 :         else if (init == error_mark_node)
   22339            6 :           RETURN (error_mark_node);
   22340              :         else
   22341              :           {
   22342       680896 :             init_vec = make_tree_vector ();
   22343       680896 :             if (init == void_node)
   22344         9984 :               gcc_assert (init_vec != NULL);
   22345              :             else
   22346              :               {
   22347      1229661 :                 for (; init != NULL_TREE; init = TREE_CHAIN (init))
   22348       558749 :                   vec_safe_push (init_vec, TREE_VALUE (init));
   22349              :               }
   22350              :           }
   22351              : 
   22352              :         /* Avoid passing an enclosing decl to valid_array_size_p.  */
   22353       915041 :         in_decl = NULL_TREE;
   22354              : 
   22355       915041 :         tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
   22356       915041 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   22357       915041 :         ret = build_new (loc, &placement_vec, op1, op2,
   22358       915041 :                          &init_vec, NEW_EXPR_USE_GLOBAL (t),
   22359              :                          complain);
   22360              : 
   22361       915041 :         if (placement_vec != NULL)
   22362       914825 :           release_tree_vector (placement_vec);
   22363       915041 :         if (init_vec != NULL)
   22364       544680 :           release_tree_vector (init_vec);
   22365              : 
   22366       915050 :         RETURN (ret);
   22367              :       }
   22368              : 
   22369        52499 :     case DELETE_EXPR:
   22370        52499 :       {
   22371        52499 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22372        52499 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   22373        52499 :         RETURN (delete_sanity (input_location, op0, op1,
   22374              :                                DELETE_EXPR_USE_VEC (t),
   22375              :                                DELETE_EXPR_USE_GLOBAL (t),
   22376              :                                complain));
   22377              :       }
   22378              : 
   22379       188857 :     case COMPOUND_EXPR:
   22380       188857 :       {
   22381       188857 :         tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args,
   22382              :                                 complain & ~tf_decltype, in_decl);
   22383       188857 :         RETURN (build_x_compound_expr (EXPR_LOCATION (t),
   22384              :                                        op0,
   22385              :                                        RECUR (TREE_OPERAND (t, 1)),
   22386              :                                        templated_operator_saved_lookups (t),
   22387              :                                        complain|decltype_flag));
   22388              :       }
   22389              : 
   22390    119763724 :     case CALL_EXPR:
   22391    119763724 :       {
   22392    119763724 :         tree function;
   22393    119763724 :         unsigned int nargs;
   22394    119763724 :         bool qualified_p;
   22395    119763724 :         bool koenig_p;
   22396    119763724 :         tree ret;
   22397              : 
   22398    119763724 :         function = CALL_EXPR_FN (t);
   22399              :         /* Internal function with no arguments.  */
   22400    119763724 :         if (function == NULL_TREE && call_expr_nargs (t) == 0)
   22401    119407807 :           RETURN (t);
   22402              : 
   22403              :         /* When we parsed the expression, we determined whether or
   22404              :            not Koenig lookup should be performed.  */
   22405    119673944 :         koenig_p = KOENIG_LOOKUP_P (t);
   22406    119673944 :         if (function == NULL_TREE)
   22407              :           {
   22408              :             koenig_p = false;
   22409              :             qualified_p = false;
   22410              :           }
   22411    119672821 :         else if (TREE_CODE (function) == SCOPE_REF)
   22412              :           {
   22413      5068981 :             qualified_p = true;
   22414      5068981 :             function = tsubst_qualified_id (function, args, complain, in_decl,
   22415              :                                             /*done=*/false,
   22416              :                                             /*address_p=*/false);
   22417              :           }
   22418    114603840 :         else if (CALL_EXPR_STATIC_CHAIN (t)
   22419           99 :                  && TREE_CODE (function) == FUNCTION_DECL
   22420    114603939 :                  && fndecl_built_in_p (function, BUILT_IN_CLASSIFY_TYPE))
   22421              :           {
   22422           99 :             tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain,
   22423              :                                 in_decl);
   22424           99 :             if (dependent_type_p (type))
   22425              :               {
   22426            0 :                 ret = build_vl_exp (CALL_EXPR, 4);
   22427            0 :                 CALL_EXPR_FN (ret) = function;
   22428            0 :                 CALL_EXPR_STATIC_CHAIN (ret) = type;
   22429            0 :                 CALL_EXPR_ARG (ret, 0)
   22430            0 :                   = build_min (SIZEOF_EXPR, size_type_node, type);
   22431            0 :                 TREE_TYPE (ret) = integer_type_node;
   22432              :               }
   22433              :             else
   22434           99 :               ret = build_int_cst (integer_type_node, type_to_class (type));
   22435           99 :             RETURN (ret);
   22436              :           }
   22437    114603741 :         else if (koenig_p
   22438    114603741 :                  && (identifier_p (function)
   22439      8328399 :                      || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22440      2727705 :                          && identifier_p (TREE_OPERAND (function, 0)))))
   22441              :           {
   22442              :             /* Do nothing; calling tsubst_expr on an identifier
   22443              :                would incorrectly perform unqualified lookup again.
   22444              : 
   22445              :                Note that we can also have an IDENTIFIER_NODE if the earlier
   22446              :                unqualified lookup found a dependent local extern declaration
   22447              :                (as per finish_call_expr); in that case koenig_p will be false
   22448              :                and we do want to do the lookup again to find the substituted
   22449              :                declaration.  */
   22450       192785 :             qualified_p = false;
   22451              : 
   22452       192785 :             if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
   22453         6236 :               function = tsubst_name (function, args, complain, in_decl);
   22454              :           }
   22455              :         else
   22456              :           {
   22457    114410956 :             if (TREE_CODE (function) == COMPONENT_REF)
   22458              :               {
   22459     18152578 :                 tree op = TREE_OPERAND (function, 1);
   22460              : 
   22461     18152578 :                 qualified_p = (TREE_CODE (op) == SCOPE_REF
   22462     18152578 :                                || (BASELINK_P (op)
   22463     11375359 :                                    && BASELINK_QUALIFIED_P (op)));
   22464              :               }
   22465              :             else
   22466              :               qualified_p = false;
   22467              : 
   22468    114410956 :             if (TREE_CODE (function) == ADDR_EXPR
   22469    114410956 :                 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
   22470              :               /* Avoid error about taking the address of a constructor.  */
   22471       151718 :               function = TREE_OPERAND (function, 0);
   22472              : 
   22473    114410956 :             function = tsubst_expr (function, args, complain, in_decl);
   22474              : 
   22475    114410956 :             if (BASELINK_P (function))
   22476      9578605 :               qualified_p = true;
   22477              :           }
   22478              : 
   22479    119673845 :         nargs = call_expr_nargs (t);
   22480    119673845 :         releasing_vec call_args;
   22481    119673845 :         tsubst_call_args (t, args, complain, in_decl, call_args);
   22482              : 
   22483              :         /* Stripped-down processing for a call in a thunk.  Specifically, in
   22484              :            the thunk template for a generic lambda.  */
   22485    119673842 :         if (call_from_lambda_thunk_p (t))
   22486              :           {
   22487              :             /* Now that we've expanded any packs, the number of call args
   22488              :                might be different.  */
   22489          105 :             unsigned int cargs = call_args->length ();
   22490          105 :             tree thisarg = NULL_TREE;
   22491          105 :             if (TREE_CODE (function) == COMPONENT_REF)
   22492              :               {
   22493          105 :                 thisarg = TREE_OPERAND (function, 0);
   22494          105 :                 if (TREE_CODE (thisarg) == INDIRECT_REF)
   22495          105 :                   thisarg = TREE_OPERAND (thisarg, 0);
   22496          105 :                 function = TREE_OPERAND (function, 1);
   22497          105 :                 if (TREE_CODE (function) == BASELINK)
   22498          105 :                   function = BASELINK_FUNCTIONS (function);
   22499              :               }
   22500              :             /* We aren't going to do normal overload resolution, so force the
   22501              :                template-id to resolve.  */
   22502          105 :             function = resolve_nondeduced_context (function, complain);
   22503          432 :             for (unsigned i = 0; i < cargs; ++i)
   22504              :               {
   22505              :                 /* In a thunk, pass through args directly, without any
   22506              :                    conversions.  */
   22507          222 :                 tree arg = (*call_args)[i];
   22508          222 :                 while (TREE_CODE (arg) != PARM_DECL)
   22509            0 :                   arg = TREE_OPERAND (arg, 0);
   22510          222 :                 (*call_args)[i] = arg;
   22511              :               }
   22512          105 :             if (thisarg)
   22513              :               {
   22514              :                 /* If there are no other args, just push 'this'.  */
   22515          105 :                 if (cargs == 0)
   22516            9 :                   vec_safe_push (call_args, thisarg);
   22517              :                 else
   22518              :                   {
   22519              :                     /* Otherwise, shift the other args over to make room.  */
   22520           96 :                     tree last = (*call_args)[cargs - 1];
   22521           96 :                     vec_safe_push (call_args, last);
   22522          222 :                     for (int i = cargs - 1; i > 0; --i)
   22523          126 :                       (*call_args)[i] = (*call_args)[i - 1];
   22524           96 :                     (*call_args)[0] = thisarg;
   22525              :                   }
   22526              :               }
   22527          105 :             ret = build_call_a (function, call_args->length (),
   22528              :                                 call_args->address ());
   22529              :             /* The thunk location is not interesting.  */
   22530          105 :             SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
   22531          105 :             CALL_FROM_THUNK_P (ret) = true;
   22532          105 :             if (CLASS_TYPE_P (TREE_TYPE (ret)))
   22533            9 :               CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
   22534              : 
   22535          105 :             RETURN (ret);
   22536              :           }
   22537              : 
   22538              :         /* We do not perform argument-dependent lookup if normal
   22539              :            lookup finds a non-function, in accordance with the
   22540              :            resolution of DR 218.  */
   22541    119673737 :         if (koenig_p
   22542      8514945 :             && ((is_overloaded_fn (function)
   22543              :                  /* If lookup found a member function, the Koenig lookup is
   22544              :                     not appropriate, even if an unqualified-name was used
   22545              :                     to denote the function.  */
   22546      8322141 :                  && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
   22547      8521181 :                 || identifier_p (function)
   22548              :                 /* C++20 P0846: Lookup found nothing.  */
   22549         6255 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22550         6236 :                     && identifier_p (TREE_OPERAND (function, 0))))
   22551              :             /* Only do this when substitution turns a dependent call
   22552              :                into a non-dependent call.  */
   22553      8514926 :             && type_dependent_expression_p_push (t)
   22554    127092638 :             && !any_type_dependent_arguments_p (call_args))
   22555      7298347 :           function = perform_koenig_lookup (function, call_args, tf_none);
   22556              : 
   22557    119673737 :         if (function != NULL_TREE
   22558       165663 :             && (identifier_p (function)
   22559    119506961 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   22560     37457480 :                     && identifier_p (TREE_OPERAND (function, 0))
   22561          727 :                     && !any_dependent_template_arguments_p (TREE_OPERAND
   22562              :                                                             (function, 1))))
   22563    119839400 :             && !any_type_dependent_arguments_p (call_args))
   22564              :           {
   22565        58906 :             bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
   22566        58906 :             if (template_id_p)
   22567            6 :               function = TREE_OPERAND (function, 0);
   22568        58906 :             if (koenig_p && (complain & tf_warning_or_error))
   22569              :               {
   22570              :                 /* For backwards compatibility and good diagnostics, try
   22571              :                    the unqualified lookup again if we aren't in SFINAE
   22572              :                    context.  */
   22573           57 :                 tree unq = tsubst_expr (function, args, complain, in_decl);
   22574           57 :                 if (unq == error_mark_node)
   22575           33 :                   RETURN (error_mark_node);
   22576              : 
   22577           24 :                 if (unq != function)
   22578              :                   {
   22579           24 :                     auto_diagnostic_group d;
   22580           24 :                     char const *const msg
   22581              :                       = G_("%qD was not declared in this scope, "
   22582              :                            "and no declarations were found by "
   22583              :                            "argument-dependent lookup at the point "
   22584              :                            "of instantiation");
   22585              : 
   22586           24 :                     bool in_lambda = (current_class_type
   22587           36 :                                       && LAMBDA_TYPE_P (current_class_type));
   22588              :                     /* In a lambda fn, we have to be careful to not
   22589              :                        introduce new this captures.  Legacy code can't
   22590              :                        be using lambdas anyway, so it's ok to be
   22591              :                        stricter.  Be strict with C++20 template-id ADL too.
   22592              :                        And be strict if we're already failing anyway.  */
   22593           24 :                     bool strict = in_lambda || template_id_p || seen_error ();
   22594           24 :                     bool diag = true;
   22595            9 :                     if (strict)
   22596           15 :                       error_at (cp_expr_loc_or_input_loc (t),
   22597              :                                 msg, function);
   22598              :                     else
   22599            9 :                       diag = permerror (cp_expr_loc_or_input_loc (t),
   22600              :                                         msg, function);
   22601           24 :                     if (diag)
   22602              :                       {
   22603           24 :                         tree fn = unq;
   22604              : 
   22605           24 :                         if (INDIRECT_REF_P (fn))
   22606            3 :                           fn = TREE_OPERAND (fn, 0);
   22607           24 :                         if (is_overloaded_fn (fn))
   22608           21 :                           fn = get_first_fn (fn);
   22609              : 
   22610           24 :                         if (!DECL_P (fn))
   22611              :                           /* Can't say anything more.  */;
   22612           21 :                         else if (DECL_CLASS_SCOPE_P (fn))
   22613              :                           {
   22614            9 :                             location_t loc = cp_expr_loc_or_input_loc (t);
   22615            9 :                             inform (loc,
   22616              :                                     "declarations in dependent base %qT are "
   22617              :                                     "not found by unqualified lookup",
   22618            9 :                                     DECL_CLASS_CONTEXT (fn));
   22619            9 :                             if (current_class_ptr)
   22620            6 :                               inform (loc,
   22621              :                                       "use %<this->%D%> instead", function);
   22622              :                             else
   22623            3 :                               inform (loc,
   22624              :                                       "use %<%T::%D%> instead",
   22625              :                                       current_class_name, function);
   22626              :                           }
   22627              :                         else
   22628           12 :                           inform (DECL_SOURCE_LOCATION (fn),
   22629              :                                   "%qD declared here, later in the "
   22630              :                                   "translation unit", fn);
   22631           24 :                         if (strict)
   22632           15 :                           RETURN (error_mark_node);
   22633              :                       }
   22634              : 
   22635            9 :                     function = unq;
   22636           24 :                   }
   22637              :               }
   22638        58858 :             if (identifier_p (function))
   22639              :               {
   22640        58849 :                 if (complain & tf_error)
   22641            0 :                   unqualified_name_lookup_error (function);
   22642        58849 :                 RETURN (error_mark_node);
   22643              :               }
   22644              :           }
   22645              : 
   22646              :         /* Remember that there was a reference to this entity.  */
   22647    119614840 :         if (function != NULL_TREE
   22648    119613717 :             && DECL_P (function)
   22649    130587315 :             && !mark_used (function, complain) && !(complain & tf_error))
   22650            3 :           RETURN (error_mark_node);
   22651              : 
   22652    119614837 :         if (!maybe_fold_fn_template_args (function, complain))
   22653       342411 :           return error_mark_node;
   22654              : 
   22655              :         /* Put back tf_decltype for the actual call.  */
   22656    119272426 :         complain |= decltype_flag;
   22657              : 
   22658    119272426 :         if (function == NULL_TREE)
   22659         1123 :           switch (CALL_EXPR_IFN (t))
   22660              :             {
   22661           45 :             case IFN_LAUNDER:
   22662           45 :               gcc_assert (nargs == 1);
   22663           45 :               if (vec_safe_length (call_args) != 1)
   22664              :                 {
   22665            6 :                   error_at (cp_expr_loc_or_input_loc (t),
   22666              :                             "wrong number of arguments to "
   22667              :                             "%<__builtin_launder%>");
   22668            6 :                   ret = error_mark_node;
   22669              :                 }
   22670              :               else
   22671           39 :                 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
   22672           39 :                                               (*call_args)[0], complain);
   22673              :               break;
   22674              : 
   22675          245 :             case IFN_VEC_CONVERT:
   22676          245 :               gcc_assert (nargs == 1);
   22677          245 :               if (vec_safe_length (call_args) != 1)
   22678              :                 {
   22679            0 :                   error_at (cp_expr_loc_or_input_loc (t),
   22680              :                             "wrong number of arguments to "
   22681              :                             "%<__builtin_convertvector%>");
   22682            0 :                   ret = error_mark_node;
   22683            0 :                   break;
   22684              :                 }
   22685          245 :               ret = cp_build_vec_convert ((*call_args)[0], input_location,
   22686          245 :                                           tsubst (TREE_TYPE (t), args,
   22687              :                                                   complain, in_decl),
   22688              :                                           complain);
   22689          245 :               if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
   22690           35 :                 RETURN (ret);
   22691              :               break;
   22692              : 
   22693          722 :             case IFN_SHUFFLEVECTOR:
   22694          722 :               {
   22695          722 :                 ret = build_x_shufflevector (input_location, call_args,
   22696              :                                              complain);
   22697          722 :                 if (ret != error_mark_node)
   22698          719 :                   RETURN (ret);
   22699              :                 break;
   22700              :               }
   22701              : 
   22702            0 :             case IFN_BSWAP:
   22703            0 :             case IFN_BITREVERSE:
   22704            0 :               {
   22705            0 :                 ret = build_x_bswapg_bitreverseg (input_location,
   22706            0 :                                                   CALL_EXPR_IFN (t), call_args,
   22707              :                                                   complain);
   22708            0 :                 if (ret != error_mark_node)
   22709            0 :                   RETURN (ret);
   22710              :                 break;
   22711              :               }
   22712              : 
   22713           99 :             case IFN_ASSUME:
   22714           99 :               gcc_assert (nargs == 1);
   22715           99 :               if (vec_safe_length (call_args) != 1)
   22716              :                 {
   22717            0 :                   error_at (cp_expr_loc_or_input_loc (t),
   22718              :                             "wrong number of arguments to "
   22719              :                             "%<assume%> attribute");
   22720            0 :                   ret = error_mark_node;
   22721              :                 }
   22722              :               else
   22723              :                 {
   22724           99 :                   tree &arg = (*call_args)[0];
   22725           99 :                   if (!type_dependent_expression_p (arg))
   22726           99 :                     arg = contextual_conv_bool (arg, tf_warning_or_error);
   22727           99 :                   if (error_operand_p (arg))
   22728              :                     {
   22729            6 :                       ret = error_mark_node;
   22730            6 :                       break;
   22731              :                     }
   22732           93 :                   ret = build_assume_call (EXPR_LOCATION (t), arg);
   22733           93 :                   RETURN (ret);
   22734              :                 }
   22735            0 :               break;
   22736              : 
   22737           12 :             case IFN_GOMP_DISPATCH:
   22738           24 :               ret = build_call_expr_internal_loc (EXPR_LOCATION (t),
   22739              :                                                   IFN_GOMP_DISPATCH,
   22740           12 :                                                   TREE_TYPE (call_args[0]), 1,
   22741           12 :                                                   call_args[0]);
   22742           12 :               RETURN (ret);
   22743              :               break;
   22744              : 
   22745            0 :             default:
   22746              :               /* Unsupported internal function with arguments.  */
   22747            0 :               gcc_unreachable ();
   22748              :             }
   22749    119271303 :         else if (TREE_CODE (function) == OFFSET_REF
   22750              :                  || TREE_CODE (function) == DOTSTAR_EXPR
   22751              :                  || TREE_CODE (function) == MEMBER_REF)
   22752        86558 :           ret = build_offset_ref_call_from_tree (function, &call_args,
   22753              :                                                  complain);
   22754    119184745 :         else if (concept_check_p (function))
   22755              :           /* Calls to concepts should have been previously diagnosed.  */
   22756            0 :           gcc_unreachable ();
   22757              :         else
   22758    119184745 :           ret = finish_call_expr (function, &call_args,
   22759              :                                   /*disallow_virtual=*/qualified_p,
   22760              :                                   koenig_p,
   22761              :                                   complain);
   22762              : 
   22763    119258064 :         if (ret != error_mark_node)
   22764              :           {
   22765    118956843 :             bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
   22766    118956843 :             bool ord = CALL_EXPR_ORDERED_ARGS (t);
   22767    118956843 :             bool rev = CALL_EXPR_REVERSE_ARGS (t);
   22768    118956843 :             bool mtc = false;
   22769    118956843 :             if (TREE_CODE (t) == CALL_EXPR)
   22770    118956843 :               mtc = CALL_EXPR_MUST_TAIL_CALL (t);
   22771    118956843 :             if (op || ord || rev || mtc)
   22772       456394 :               if (tree call = extract_call_expr (ret))
   22773              :                 {
   22774       456394 :                   CALL_EXPR_OPERATOR_SYNTAX (call) = op;
   22775       456394 :                   CALL_EXPR_ORDERED_ARGS (call) = ord;
   22776       456394 :                   CALL_EXPR_REVERSE_ARGS (call) = rev;
   22777       456394 :                   if (TREE_CODE (call) == CALL_EXPR)
   22778       455725 :                     CALL_EXPR_MUST_TAIL_CALL (call) = mtc;
   22779          669 :                   else if (TREE_CODE (call) == AGGR_INIT_EXPR)
   22780          669 :                     AGGR_INIT_EXPR_MUST_TAIL (call) = mtc;
   22781              :                 }
   22782    118956843 :             if (CALL_FROM_NEW_OR_DELETE_P (t))
   22783              :               {
   22784       223031 :                 tree call = extract_call_expr (ret);
   22785       223031 :                 tree fn = cp_get_callee_fndecl_nofold (call);
   22786       446062 :                 if (fn ? !DECL_IS_REPLACEABLE_OPERATOR (fn)
   22787            0 :                        : !processing_template_decl)
   22788              :                   {
   22789            9 :                     auto_diagnostic_group d;
   22790            9 :                     location_t loc = cp_expr_loc_or_input_loc (t);
   22791            9 :                     if (!fn || IDENTIFIER_NEW_OP_P (DECL_NAME (fn)))
   22792            6 :                       error_at (loc, "call to %<__builtin_operator_new%> "
   22793              :                                      "does not select replaceable global "
   22794              :                                      "allocation function");
   22795              :                     else
   22796            3 :                       error_at (loc, "call to %<__builtin_operator_delete%> "
   22797              :                                      "does not select replaceable global "
   22798              :                                      "deallocation function");
   22799            9 :                     if (fn)
   22800            9 :                       inform (DECL_SOURCE_LOCATION (fn),
   22801              :                               "selected function declared here");
   22802            9 :                   }
   22803       223022 :                 else if (call && TREE_CODE (call) == CALL_EXPR)
   22804       223022 :                   CALL_FROM_NEW_OR_DELETE_P (call) = 1;
   22805              :               }
   22806    118956843 :             if (warning_suppressed_p (t, OPT_Wpessimizing_move))
   22807              :               /* This also suppresses -Wredundant-move.  */
   22808    108336571 :               suppress_warning (ret, OPT_Wpessimizing_move);
   22809              :           }
   22810              : 
   22811    119317928 :         RETURN (ret);
   22812    119660339 :       }
   22813              : 
   22814      4961169 :     case COND_EXPR:
   22815      4961169 :       {
   22816      4961169 :         tree cond = RECUR (TREE_OPERAND (t, 0));
   22817      4961169 :         cond = mark_rvalue_use (cond);
   22818      4961169 :         tree folded_cond = fold_non_dependent_expr (cond, complain);
   22819      4961169 :         tree exp1, exp2;
   22820              : 
   22821      4961169 :         if (TREE_CODE (folded_cond) == INTEGER_CST)
   22822              :           {
   22823      3830974 :             if (integer_zerop (folded_cond))
   22824              :               {
   22825      3152455 :                 ++c_inhibit_evaluation_warnings;
   22826      3152455 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   22827      3152455 :                 --c_inhibit_evaluation_warnings;
   22828      3152455 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   22829              :               }
   22830              :             else
   22831              :               {
   22832       678519 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   22833       678519 :                 ++c_inhibit_evaluation_warnings;
   22834       678519 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   22835       678519 :                 --c_inhibit_evaluation_warnings;
   22836              :               }
   22837              :             cond = folded_cond;
   22838              :           }
   22839              :         else
   22840              :           {
   22841      1130195 :             exp1 = RECUR (TREE_OPERAND (t, 1));
   22842      1130195 :             exp2 = RECUR (TREE_OPERAND (t, 2));
   22843              :           }
   22844              : 
   22845      4961169 :         warning_sentinel s(warn_duplicated_branches);
   22846      4961169 :         RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
   22847              :                                          cond, exp1, exp2, complain));
   22848      4961169 :       }
   22849              : 
   22850           18 :     case PSEUDO_DTOR_EXPR:
   22851           18 :       {
   22852           18 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   22853           18 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   22854           18 :         tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
   22855           18 :         RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
   22856              :                                                input_location, complain));
   22857              :       }
   22858              : 
   22859     34180509 :     case TREE_LIST:
   22860     34180509 :       RETURN (tsubst_tree_list (t, args, complain, in_decl));
   22861              : 
   22862     30581005 :     case COMPONENT_REF:
   22863     30581005 :       {
   22864     30581005 :         tree object;
   22865     30581005 :         tree object_type;
   22866     30581005 :         tree member;
   22867     30581005 :         tree r;
   22868              : 
   22869     30581005 :         object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   22870              :                                                      args, complain, in_decl);
   22871              :         /* Remember that there was a reference to this entity.  */
   22872     30581005 :         if (DECL_P (object)
   22873     30581005 :             && !mark_used (object, complain) && !(complain & tf_error))
   22874            0 :           RETURN (error_mark_node);
   22875     30581005 :         object_type = TREE_TYPE (object);
   22876              : 
   22877     30581005 :         member = TREE_OPERAND (t, 1);
   22878     30581005 :         const bool splice_p = dependent_splice_p (member);
   22879     30581005 :         if (BASELINK_P (member))
   22880     11375421 :           member = tsubst_baselink (member,
   22881     11375421 :                                     non_reference (TREE_TYPE (object)),
   22882              :                                     args, complain, in_decl);
   22883              :         /* In reconstruct_lambda_capture_pack, handle replacing the FIELD_DECL
   22884              :            pack with an element.  */
   22885     36907920 :         else if (object_type && LAMBDA_TYPE_P (object_type)
   22886       168180 :                  && TREE_CODE (member) == FIELD_DECL
   22887     19367782 :                  && (r = retrieve_local_specialization (member)))
   22888              :           {
   22889           24 :             if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   22890           24 :               r = argument_pack_select_arg (r);
   22891              :             member = r;
   22892              :           }
   22893              :         else
   22894     19205560 :           member = tsubst_name (member, args, complain, in_decl);
   22895     30581005 :         if (member == error_mark_node)
   22896           40 :           RETURN (error_mark_node);
   22897              : 
   22898     30069754 :         if (object_type && TYPE_PTRMEMFUNC_P (object_type)
   22899     30580989 :             && TREE_CODE (member) == FIELD_DECL)
   22900              :           {
   22901            3 :             r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
   22902            3 :             RETURN (r);
   22903              :           }
   22904     30580962 :         else if (TREE_CODE (member) == FIELD_DECL)
   22905              :           {
   22906              :             /* Assume access of this FIELD_DECL has already been checked; we
   22907              :                don't recheck it to avoid bogus access errors when substituting
   22908              :                a reduced constant initializer (97740).  */
   22909      7042708 :             gcc_checking_assert (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
   22910              :                                  || dependent_splice_p (TREE_OPERAND (t, 1)));
   22911      7042708 :             push_deferring_access_checks (dk_deferred);
   22912      7042708 :             r = finish_non_static_data_member (member, object, NULL_TREE,
   22913              :                                                complain);
   22914      7042708 :             pop_deferring_access_checks ();
   22915      7042708 :             if (REF_PARENTHESIZED_P (t))
   22916         4785 :               r = force_paren_expr (r);
   22917      7042708 :             RETURN (r);
   22918              :           }
   22919     23538254 :         else if (type_dependent_expression_p (object))
   22920              :           /* We can't do much here.  */;
   22921     22907748 :         else if (!CLASS_TYPE_P (object_type))
   22922              :           {
   22923       451990 :             if (scalarish_type_p (object_type))
   22924              :               {
   22925       269023 :                 tree s = NULL_TREE;
   22926       269023 :                 tree dtor = member;
   22927              : 
   22928       269023 :                 if (TREE_CODE (dtor) == SCOPE_REF)
   22929              :                   {
   22930           15 :                     s = TREE_OPERAND (dtor, 0);
   22931           15 :                     dtor = TREE_OPERAND (dtor, 1);
   22932              :                   }
   22933       269023 :                 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
   22934              :                   {
   22935       162168 :                     dtor = TREE_OPERAND (dtor, 0);
   22936       162168 :                     if (TYPE_P (dtor))
   22937       162168 :                       RETURN (finish_pseudo_destructor_expr
   22938              :                               (object, s, dtor, input_location, complain));
   22939              :                   }
   22940              :               }
   22941              :           }
   22942     22455758 :         else if (TREE_CODE (member) == SCOPE_REF
   22943          758 :                  && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR
   22944     22455776 :                  && identifier_p (TREE_OPERAND (TREE_OPERAND (member, 1), 0)))
   22945              :           {
   22946              :             /* Lookup the template functions now that we know what the
   22947              :                scope is.  */
   22948           15 :             tree scope = TREE_OPERAND (member, 0);
   22949           15 :             tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
   22950           15 :             tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
   22951           15 :             member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
   22952              :                                             /*complain=*/false);
   22953           15 :             if (BASELINK_P (member))
   22954              :               {
   22955           30 :                 BASELINK_FUNCTIONS (member)
   22956           15 :                   = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
   22957              :                               args);
   22958           15 :                 member = (adjust_result_of_qualified_name_lookup
   22959           15 :                           (member, BINFO_TYPE (BASELINK_BINFO (member)),
   22960              :                            object_type));
   22961              :               }
   22962              :             else
   22963              :               {
   22964            0 :                 qualified_name_lookup_error (scope, tmpl, member,
   22965              :                                              input_location);
   22966           15 :                 RETURN (error_mark_node);
   22967              :               }
   22968              :           }
   22969     22455743 :         else if (TREE_CODE (member) == SCOPE_REF
   22970          743 :                  && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
   22971     22455746 :                  && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
   22972              :           {
   22973            3 :             if (complain & tf_error)
   22974              :               {
   22975            3 :                 if (TYPE_P (TREE_OPERAND (member, 0)))
   22976            3 :                   error ("%qT is not a class or namespace",
   22977            3 :                          TREE_OPERAND (member, 0));
   22978              :                 else
   22979            0 :                   error ("%qD is not a class or namespace",
   22980            0 :                          TREE_OPERAND (member, 0));
   22981              :               }
   22982            3 :             RETURN (error_mark_node);
   22983              :           }
   22984     22455740 :         else if (splice_p && BASELINK_P (member))
   22985              :           /* We need to call adjust_result_of_qualified_name_lookup when
   22986              :              we have obj->[:^^T::fn:], but we don't set BASELINK_QUALIFIED_P
   22987              :              so that we still get virtual function binding.  */
   22988          668 :           member = (adjust_result_of_qualified_name_lookup
   22989          668 :                     (member, NULL_TREE, object_type));
   22990              : 
   22991     23376083 :         r = finish_class_member_access_expr (object, member,
   22992              :                                              /*template_p=*/false,
   22993              :                                              complain,
   22994     23376083 :                                              COMPONENT_REF_SPLICE_P (t));
   22995     23376083 :         if (REF_PARENTHESIZED_P (t))
   22996         1285 :           r = force_paren_expr (r);
   22997     23376083 :         RETURN (r);
   22998              :       }
   22999              : 
   23000        28583 :     case THROW_EXPR:
   23001        28583 :       RETURN (build_throw
   23002              :        (input_location, RECUR (TREE_OPERAND (t, 0)), complain));
   23003              : 
   23004      5752477 :     case CONSTRUCTOR:
   23005      5752477 :       {
   23006      5752477 :         vec<constructor_elt, va_gc> *n;
   23007      5752477 :         constructor_elt *ce;
   23008      5752477 :         unsigned HOST_WIDE_INT idx;
   23009      5752477 :         bool process_index_p;
   23010      5752477 :         int newlen;
   23011      5752477 :         bool need_copy_p = false;
   23012      5752477 :         tree r;
   23013              : 
   23014      5752477 :         tsubst_flags_t tcomplain = complain;
   23015      5752477 :         if (COMPOUND_LITERAL_P (t))
   23016      3233473 :           tcomplain |= tf_tst_ok;
   23017      5752477 :         tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   23018      5752477 :         if (type == error_mark_node)
   23019           22 :           RETURN (error_mark_node);
   23020              : 
   23021              :         /* We do not want to process the index of aggregate
   23022              :            initializers as they are identifier nodes which will be
   23023              :            looked up by digest_init.  */
   23024      5752455 :         process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
   23025              : 
   23026      5752455 :         if (null_member_pointer_value_p (t))
   23027              :           {
   23028           25 :             gcc_assert (same_type_p (type, TREE_TYPE (t)));
   23029           25 :             RETURN (t);
   23030              :           }
   23031              : 
   23032      5752430 :         n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
   23033      5752430 :         newlen = vec_safe_length (n);
   23034      9023904 :         FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
   23035              :           {
   23036      3271474 :             if (ce->index && process_index_p
   23037              :                 /* An identifier index is looked up in the type
   23038              :                    being initialized, not the current scope.  */
   23039          381 :                 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
   23040          340 :               ce->index = RECUR (ce->index);
   23041              : 
   23042      3271474 :             if (PACK_EXPANSION_P (ce->value))
   23043              :               {
   23044              :                 /* Substitute into the pack expansion.  */
   23045        18388 :                 ce->value = tsubst_pack_expansion (ce->value, args, complain,
   23046              :                                                   in_decl);
   23047              : 
   23048        18388 :                 if (ce->value == error_mark_node
   23049        18385 :                     || PACK_EXPANSION_P (ce->value))
   23050              :                   ;
   23051        17148 :                 else if (TREE_VEC_LENGTH (ce->value) == 1)
   23052              :                   /* Just move the argument into place.  */
   23053         4417 :                   ce->value = TREE_VEC_ELT (ce->value, 0);
   23054              :                 else
   23055              :                   {
   23056              :                     /* Update the length of the final CONSTRUCTOR
   23057              :                        arguments vector, and note that we will need to
   23058              :                        copy.*/
   23059        12731 :                     newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
   23060        12731 :                     need_copy_p = true;
   23061              :                   }
   23062              :               }
   23063              :             else
   23064      3253086 :               ce->value = RECUR (ce->value);
   23065              :           }
   23066              : 
   23067      5752430 :         if (need_copy_p)
   23068              :           {
   23069        12709 :             vec<constructor_elt, va_gc> *old_n = n;
   23070              : 
   23071        12709 :             vec_alloc (n, newlen);
   23072        38485 :             FOR_EACH_VEC_ELT (*old_n, idx, ce)
   23073              :               {
   23074        13067 :                 if (TREE_CODE (ce->value) == TREE_VEC)
   23075              :                   {
   23076        12731 :                     int i, len = TREE_VEC_LENGTH (ce->value);
   23077        60425 :                     for (i = 0; i < len; ++i)
   23078        47694 :                       CONSTRUCTOR_APPEND_ELT (n, 0,
   23079              :                                               TREE_VEC_ELT (ce->value, i));
   23080              :                   }
   23081              :                 else
   23082        13403 :                   CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
   23083              :               }
   23084              :           }
   23085              : 
   23086      5752430 :         r = build_constructor (init_list_type_node, n);
   23087      5752430 :         CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
   23088     11504860 :         CONSTRUCTOR_IS_DESIGNATED_INIT (r)
   23089      5752430 :           = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
   23090              : 
   23091      5752430 :         if (TREE_HAS_CONSTRUCTOR (t))
   23092              :           {
   23093      3233451 :             fcl_t cl = fcl_functional;
   23094      3233451 :             if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
   23095          372 :               cl = fcl_c99;
   23096      3233451 :             RETURN (finish_compound_literal (type, r, complain, cl));
   23097              :           }
   23098              : 
   23099      2518979 :         TREE_TYPE (r) = type;
   23100      5752477 :         RETURN (r);
   23101              :       }
   23102              : 
   23103        75186 :     case TYPEID_EXPR:
   23104        75186 :       {
   23105        75186 :         tree operand_0 = TREE_OPERAND (t, 0);
   23106        75186 :         if (TYPE_P (operand_0))
   23107              :           {
   23108        74734 :             operand_0 = tsubst (operand_0, args, complain, in_decl);
   23109        74734 :             RETURN (get_typeid (operand_0, complain, TREE_TYPE (t)));
   23110              :           }
   23111              :         else
   23112              :           {
   23113              :             /* [expr.typeid]/4-5: substitute the operand unevaluated first, then
   23114              :                again evaluated if it is a polymorphic glvalue, so the /4
   23115              :                side-effects occur.  The unevaluated pass instantiates nothing,
   23116              :                so re-substituting has nothing to undo (PR c++/125886).  */
   23117          452 :             tree operand;
   23118          452 :             tree uneval;
   23119          452 :             {
   23120          452 :               cp_unevaluated u (/* typeid_operand= */ true);
   23121          452 :               uneval = RECUR (operand_0);
   23122          452 :             }
   23123              :             /* If we're already within an unevaluated operand, everything
   23124              :                in the subtree stays not potentially evaluated regardless
   23125              :                of [expr.typeid]/4 ([basic.def.odr]/3), so the evaluated
   23126              :                re-parse below can have nothing to do; skip it.  */
   23127          452 :             if (!cp_unevaluated_operand && typeid_evaluated_p (uneval))
   23128          270 :               operand = RECUR (operand_0);
   23129              :             else
   23130              :               operand = uneval;
   23131          452 :             RETURN (build_typeid (operand, complain, TREE_TYPE (t)));
   23132              :           }
   23133              :       }
   23134              : 
   23135    183664081 :     case FUNCTION_DECL:
   23136    183664081 :     case PARM_DECL:
   23137    183664081 :     case VAR_DECL:
   23138    183664081 :       if (!args)
   23139      2594568 :         RETURN (t);
   23140    181069513 :       tree r;
   23141    105972288 :       if (VAR_OR_FUNCTION_DECL_P (t)
   23142    189739880 :           && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   23143      8790661 :         r = tsubst_decl (t, args, complain);
   23144    172278852 :       else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
   23145              :         {
   23146              :           /* Local specialization will usually have been created when
   23147              :              we instantiated the DECL_EXPR_DECL. */
   23148          358 :           r = retrieve_local_specialization (t);
   23149          358 :           if (!r)
   23150              :             {
   23151              :               /* We're in a generic lambda referencing a local extern
   23152              :                  from an outer block-scope of a non-template.  */
   23153           18 :               gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
   23154              :               r = t;
   23155              :             }
   23156              :         }
   23157    172278494 :       else if (local_variable_p (t)
   23158    172278494 :                && ((r = retrieve_local_specialization (t))
   23159      2006011 :                    || TREE_CODE (t) == PARM_DECL
   23160         7401 :                    || uses_template_parms (DECL_CONTEXT (t))))
   23161              :         {
   23162    161325335 :           if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
   23163              :             {
   23164              :               /* We get here for a use of 'this' in an NSDMI.  */
   23165      2357979 :               if (DECL_NAME (t) == this_identifier && current_class_ptr
   23166      2716973 :                   && !LAMBDA_TYPE_P (TREE_TYPE (TREE_TYPE (current_class_ptr))))
   23167       359183 :                 RETURN (current_class_ptr);
   23168              : 
   23169              :               /* Parameters of non-templates map to themselves (e.g. in
   23170              :                  expansion statement body).  */
   23171      1639427 :               if (DECL_CONTEXT (t) && !uses_template_parms (DECL_CONTEXT (t)))
   23172           83 :                 RETURN (t);
   23173              : 
   23174              :               /* This can happen for a parameter name used later in a function
   23175              :                  declaration (such as in a late-specified return type).  Just
   23176              :                  make a dummy decl, since it's only used for its type.  */
   23177      1639344 :               gcc_assert (cp_unevaluated_operand);
   23178      1639344 :               r = tsubst_decl (t, args, complain);
   23179              :               /* Give it the template pattern as its context; its true context
   23180              :                  hasn't been instantiated yet and this is good enough for
   23181              :                  mangling.  */
   23182      1639344 :               DECL_CONTEXT (r) = DECL_CONTEXT (t);
   23183              :             }
   23184         1289 :           else if (r == NULL_TREE)
   23185              :             {
   23186              :               /* First try name lookup to find the instantiation.  */
   23187         1289 :               if (DECL_NAME (t))
   23188         1283 :                 r = lookup_name (DECL_NAME (t));
   23189         1289 :               if (r)
   23190              :                 {
   23191         1265 :                   if (!VAR_P (r))
   23192              :                     {
   23193              :                       /* During error-recovery we may find a non-variable,
   23194              :                          even an OVERLOAD: just bail out and avoid ICEs and
   23195              :                          duplicate diagnostics (c++/62207).  */
   23196            3 :                       gcc_assert (seen_error ());
   23197            3 :                       RETURN (error_mark_node);
   23198              :                     }
   23199         1262 :                   if (!is_capture_proxy (r))
   23200              :                     {
   23201              :                       /* Make sure the one we found is the one we want.  */
   23202         1234 :                       tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
   23203         1234 :                       if (ctx != DECL_CONTEXT (r))
   23204              :                         r = NULL_TREE;
   23205              :                     }
   23206              :                 }
   23207              : 
   23208              :               if (r)
   23209              :                 /* OK */;
   23210              :               else
   23211              :                 {
   23212              :                   /* This can happen for a variable used in a
   23213              :                      late-specified return type of a local lambda, or for a
   23214              :                      local static or constant.  Building a new VAR_DECL
   23215              :                      should be OK in all those cases.  */
   23216           24 :                   r = tsubst_decl (t, args, complain);
   23217           24 :                   if (local_specializations)
   23218              :                     /* Avoid infinite recursion (79640).  */
   23219           12 :                     register_local_specialization (r, t);
   23220           24 :                   if (decl_maybe_constant_var_p (r))
   23221              :                     {
   23222              :                       /* We can't call cp_finish_decl, so handle the
   23223              :                          initializer by hand.  */
   23224            0 :                       tree init = tsubst_init (DECL_INITIAL (t), r, args,
   23225              :                                                complain, in_decl);
   23226            0 :                       if (!processing_template_decl)
   23227            0 :                         init = maybe_constant_init (init);
   23228            0 :                       if (processing_template_decl
   23229            0 :                           ? potential_constant_expression (init)
   23230            0 :                           : reduced_constant_expression_p (init))
   23231            0 :                         DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
   23232            0 :                           = TREE_CONSTANT (r) = true;
   23233            0 :                       DECL_INITIAL (r) = init;
   23234            0 :                       if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
   23235            0 :                         TREE_TYPE (r)
   23236            0 :                           = do_auto_deduction (TREE_TYPE (r), init, auto_node,
   23237              :                                                complain, adc_variable_type);
   23238              :                     }
   23239           24 :                   gcc_assert (cp_unevaluated_operand
   23240              :                               || TREE_STATIC (r)
   23241              :                               || decl_constant_var_p (r)
   23242              :                               || seen_error ());
   23243           24 :                   if (!processing_template_decl
   23244           24 :                       && !TREE_STATIC (r))
   23245           24 :                     r = process_outer_var_ref (r, complain);
   23246              :                 }
   23247              :               /* Remember this for subsequent uses.  */
   23248         1286 :               if (local_specializations)
   23249         1274 :                 register_local_specialization (r, t);
   23250              :             }
   23251    160966066 :           if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   23252       683207 :             r = argument_pack_select_arg (r);
   23253              :         }
   23254              :       else
   23255              :         r = t;
   23256    180710244 :       if (!mark_used (r, complain))
   23257           24 :         RETURN (error_mark_node);
   23258              : 
   23259    180710217 :       if (!no_name_lookup_flag
   23260    175875642 :           && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL))
   23261              :         {
   23262              :           /* ??? We're doing a subset of finish_id_expression here.  */
   23263    167205284 :           if (tree wrap = maybe_get_tls_wrapper_call (r))
   23264              :             /* Replace an evaluated use of the thread_local variable with
   23265              :                a call to its wrapper.  */
   23266              :             r = wrap;
   23267    167205158 :           else if (outer_automatic_var_p (r))
   23268      1552950 :             r = process_outer_var_ref (r, complain);
   23269              : 
   23270    167205284 :           if (!TYPE_REF_P (TREE_TYPE (t)))
   23271              :             /* If the original type was a reference, we'll be wrapped in
   23272              :                the appropriate INDIRECT_REF.  */
   23273    137546438 :             r = convert_from_reference (r);
   23274              :         }
   23275    180710217 :       RETURN (r);
   23276              : 
   23277      3398135 :     case CONST_DECL:
   23278      3398135 :       {
   23279      3398135 :         tree enum_type;
   23280      3398135 :         tree v;
   23281              : 
   23282      3398135 :         if (DECL_TEMPLATE_PARM_P (t))
   23283            9 :           RETURN (RECUR (DECL_INITIAL (t)));
   23284      3398126 :         if (!args || !uses_template_parms (DECL_CONTEXT (t)))
   23285      2949638 :           RETURN (t);
   23286              : 
   23287              :         /* Unfortunately, we cannot just call lookup_name here.
   23288              :            Consider:
   23289              : 
   23290              :              template <int I> int f() {
   23291              :              enum E { a = I };
   23292              :              struct S { void g() { E e = a; } };
   23293              :              };
   23294              : 
   23295              :            When we instantiate f<7>::S::g(), say, lookup_name is not
   23296              :            clever enough to find f<7>::a.  */
   23297       448488 :         enum_type = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
   23298              : 
   23299       448488 :         for (v = TYPE_VALUES (enum_type);
   23300       517205 :              v != NULL_TREE;
   23301        68717 :              v = TREE_CHAIN (v))
   23302       517205 :           if (TREE_PURPOSE (v) == DECL_NAME (t))
   23303       517205 :             RETURN (TREE_VALUE (v));
   23304              : 
   23305              :           /* We didn't find the name.  That should never happen; if
   23306              :              name-lookup found it during preliminary parsing, we
   23307              :              should find it again here during instantiation.  */
   23308            0 :         gcc_unreachable ();
   23309     10175065 :         RETURN (t);
   23310              :       }
   23311              : 
   23312     10175065 :     case FIELD_DECL:
   23313     10175065 :       if (DECL_CONTEXT (t))
   23314              :         {
   23315     10175065 :           tree ctx;
   23316              : 
   23317     10175065 :           ctx = tsubst_entering_scope (DECL_CONTEXT (t), args,
   23318              :                                        complain, in_decl);
   23319     10175065 :           if (ctx != DECL_CONTEXT (t))
   23320              :             {
   23321      9453053 :               tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
   23322      9453053 :               if (!r)
   23323              :                 {
   23324            0 :                   if (complain & tf_error)
   23325            0 :                     error ("using invalid field %qD", t);
   23326            0 :                   RETURN (error_mark_node);
   23327              :                 }
   23328      9453053 :               RETURN (r);
   23329              :             }
   23330              :         }
   23331       722012 :       RETURN (t);
   23332              : 
   23333     68839899 :     case OVERLOAD:
   23334     68839899 :       if (modules_p ())
   23335      1325660 :         for (tree ovl : lkp_range (t))
   23336       601636 :           if (instantiating_tu_local_entity (ovl))
   23337       601636 :             RETURN (error_mark_node);
   23338     68839887 :       RETURN (t);
   23339              : 
   23340     26783096 :     case TEMPLATE_DECL:
   23341     26783096 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   23342         4885 :         RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
   23343              :                         args, complain, in_decl));
   23344     26778211 :       else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
   23345            0 :         RETURN (tsubst (t, args, complain, in_decl));
   23346     53556422 :       else if (DECL_CLASS_SCOPE_P (t)
   23347     27047157 :                && uses_template_parms (DECL_CONTEXT (t)))
   23348              :         {
   23349              :           /* Template template argument like the following example need
   23350              :              special treatment:
   23351              : 
   23352              :                template <template <class> class TT> struct C {};
   23353              :                template <class T> struct D {
   23354              :                  template <class U> struct E {};
   23355              :                  C<E> c;                          // #1
   23356              :                };
   23357              :                D<int> d;                          // #2
   23358              : 
   23359              :              We are processing the template argument `E' in #1 for
   23360              :              the template instantiation #2.  Originally, `E' is a
   23361              :              TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
   23362              :              have to substitute this with one having context `D<int>'.  */
   23363              : 
   23364       196467 :           tree context = tsubst_entering_scope (DECL_CONTEXT (t), args,
   23365              :                                                 complain, in_decl);
   23366       196467 :           RETURN (lookup_field (context, DECL_NAME(t), 0, false));
   23367              :         }
   23368              :       else
   23369              :         /* Ordinary template template argument.  */
   23370     26581744 :         RETURN (t);
   23371              : 
   23372    207379388 :     case TEMPLATE_PARM_INDEX:
   23373    207379388 :     case TYPE_DECL:
   23374    207379388 :       RETURN (tsubst (t, args, complain, in_decl));
   23375              : 
   23376            0 :     case CLEANUP_POINT_EXPR:
   23377              :       /* We shouldn't have built any of these during initial template
   23378              :          generation.  Instead, they should be built during instantiation
   23379              :          in response to the saved STMT_IS_FULL_EXPR_P setting.  */
   23380            0 :       gcc_unreachable ();
   23381              : 
   23382            0 :     case TARGET_EXPR:
   23383              :       /* TARGET_EXPR represents temporary objects and should not appear in
   23384              :          templated trees.  */
   23385            0 :       gcc_unreachable ();
   23386              : 
   23387          138 :     case OFFSET_REF:
   23388          138 :       {
   23389              :         /* We should only get here for an OFFSET_REF like A::m; a .* in a
   23390              :            template is represented as a DOTSTAR_EXPR.  */
   23391          138 :         gcc_checking_assert
   23392              :           (same_type_p (TREE_TYPE (t), TREE_TYPE (TREE_OPERAND (t, 1))));
   23393          138 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23394          138 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   23395          138 :         tree type = TREE_TYPE (op1);
   23396          138 :         r = build2 (OFFSET_REF, type, op0, op1);
   23397          138 :         PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
   23398          138 :         if (!mark_used (op1, complain)
   23399          138 :             && !(complain & tf_error))
   23400            0 :           RETURN (error_mark_node);
   23401          138 :         RETURN (r);
   23402              :       }
   23403              : 
   23404        22054 :     case PACK_INDEX_EXPR:
   23405        22054 :       RETURN (tsubst_pack_index (t, args, complain, in_decl));
   23406              : 
   23407            0 :     case EXPR_PACK_EXPANSION:
   23408            0 :       error ("invalid use of pack expansion expression");
   23409            0 :       RETURN (error_mark_node);
   23410              : 
   23411            0 :     case NONTYPE_ARGUMENT_PACK:
   23412            0 :       error ("use %<...%> to expand argument pack");
   23413            0 :       RETURN (error_mark_node);
   23414              : 
   23415        18364 :     case VOID_CST:
   23416        18364 :       gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
   23417        18364 :       RETURN (t);
   23418              : 
   23419    162879150 :     case INTEGER_CST:
   23420    162879150 :     case REAL_CST:
   23421    162879150 :     case COMPLEX_CST:
   23422    162879150 :     case VECTOR_CST:
   23423    162879150 :       {
   23424              :         /* Instantiate any typedefs in the type.  */
   23425    162879150 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23426    162879150 :         r = fold_convert (type, t);
   23427    162879150 :         gcc_assert (TREE_CODE (r) == TREE_CODE (t));
   23428    162879150 :         RETURN (r);
   23429              :       }
   23430              : 
   23431      8403712 :     case STRING_CST:
   23432      8403712 :       {
   23433      8403712 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23434      8403712 :         r = t;
   23435      8403712 :         if (type != TREE_TYPE (t))
   23436              :           {
   23437            3 :             r = copy_node (t);
   23438            3 :             TREE_TYPE (r) = type;
   23439              :           }
   23440      8403712 :         RETURN (r);
   23441              :       }
   23442              : 
   23443           51 :     case RAW_DATA_CST:
   23444           51 :       {
   23445           51 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23446           51 :         r = copy_node (t);
   23447           51 :         TREE_TYPE (r) = type;
   23448           51 :         RETURN (r);
   23449              :       }
   23450              : 
   23451           31 :     case PTRMEM_CST:
   23452              :       /* These can sometimes show up in a partial instantiation, but never
   23453              :          involve template parms.  */
   23454           31 :       gcc_assert (!uses_template_parms (t));
   23455           31 :       RETURN (t);
   23456              : 
   23457         6068 :     case UNARY_LEFT_FOLD_EXPR:
   23458         6068 :       RETURN (tsubst_unary_left_fold (t, args, complain, in_decl));
   23459       506522 :     case UNARY_RIGHT_FOLD_EXPR:
   23460       506522 :       RETURN (tsubst_unary_right_fold (t, args, complain, in_decl));
   23461        10653 :     case BINARY_LEFT_FOLD_EXPR:
   23462        10653 :       RETURN (tsubst_binary_left_fold (t, args, complain, in_decl));
   23463         1745 :     case BINARY_RIGHT_FOLD_EXPR:
   23464         1745 :       RETURN (tsubst_binary_right_fold (t, args, complain, in_decl));
   23465            0 :     case PREDICT_EXPR:
   23466            0 :       RETURN (t);
   23467              : 
   23468     91084934 :     case DEBUG_BEGIN_STMT:
   23469              :       /* ??? There's no point in copying it for now, but maybe some
   23470              :          day it will contain more information, such as a pointer back
   23471              :          to the containing function, inlined copy or so.  */
   23472     91084934 :       RETURN (t);
   23473              : 
   23474          115 :     case CO_YIELD_EXPR:
   23475          115 :       RETURN (finish_co_yield_expr (input_location,
   23476              :                                     RECUR (TREE_OPERAND (t, 0))));
   23477              : 
   23478          121 :     case CO_AWAIT_EXPR:
   23479          121 :       RETURN (finish_co_await_expr (input_location,
   23480              :                                     RECUR (TREE_OPERAND (t, 0))));
   23481              : 
   23482           42 :     case VA_ARG_EXPR:
   23483           42 :       {
   23484           42 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23485           42 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23486           42 :         RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
   23487              :       }
   23488              : 
   23489           48 :     case OFFSETOF_EXPR:
   23490           48 :       {
   23491           48 :         tree object_ptr
   23492           48 :           = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   23493           48 :         RETURN (finish_offsetof (object_ptr,
   23494              :                                  RECUR (TREE_OPERAND (t, 0)),
   23495              :                                  EXPR_LOCATION (t)));
   23496              :       }
   23497              : 
   23498       458930 :     case ADDRESSOF_EXPR:
   23499       458930 :       RETURN (cp_build_addressof (EXPR_LOCATION (t),
   23500              :                                   RECUR (TREE_OPERAND (t, 0)), complain));
   23501              : 
   23502     19017674 :     case TRAIT_EXPR:
   23503     19017674 :       {
   23504     19017674 :         tree type1 = TRAIT_EXPR_TYPE1 (t);
   23505     19017674 :         if (TYPE_P (type1))
   23506     19017565 :           type1 = tsubst (type1, args, complain, in_decl);
   23507              :         else
   23508          109 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   23509     19017674 :         tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
   23510              :                              complain, in_decl);
   23511     19017674 :         if (TRAIT_EXPR_KIND (t) == CPTK_STRUCTURED_BINDING_SIZE
   23512          129 :             && type1 != error_mark_node
   23513     19017803 :             && !processing_template_decl)
   23514              :           /* __builtin_structured_binding_size handled separately
   23515              :              to make it SFINAE friendly.  */
   23516          129 :           RETURN (finish_structured_binding_size (TRAIT_EXPR_LOCATION (t),
   23517              :                                                   type1, complain));
   23518     19017545 :         RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
   23519              :                                    TRAIT_EXPR_KIND (t), type1, type2));
   23520              :       }
   23521              : 
   23522          172 :     case STMT_EXPR:
   23523          172 :       {
   23524          172 :         tree old_stmt_expr = cur_stmt_expr;
   23525          172 :         tree stmt_expr = begin_stmt_expr ();
   23526              : 
   23527          172 :         cur_stmt_expr = stmt_expr;
   23528          172 :         tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl);
   23529          172 :         stmt_expr = finish_stmt_expr (stmt_expr, false);
   23530          172 :         cur_stmt_expr = old_stmt_expr;
   23531              : 
   23532              :         /* If the resulting list of expression statement is empty,
   23533              :            fold it further into void_node.  */
   23534          172 :         if (empty_expr_stmt_p (stmt_expr))
   23535            9 :           stmt_expr = void_node;
   23536              : 
   23537          172 :         RETURN (stmt_expr);
   23538              :       }
   23539              : 
   23540       248694 :     case LAMBDA_EXPR:
   23541       248694 :       {
   23542       248694 :         tree r = tsubst_lambda_expr (t, args, complain, in_decl);
   23543              : 
   23544       248694 :         RETURN (build_lambda_object (r));
   23545              :       }
   23546              : 
   23547           12 :     case TRANSACTION_EXPR:
   23548           12 :       gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t));
   23549           12 :       RETURN (tsubst_stmt (t, args, complain, in_decl));
   23550              : 
   23551       269299 :     case PAREN_EXPR:
   23552       269299 :       if (REF_PARENTHESIZED_P (t))
   23553       269298 :         RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
   23554              :       else
   23555              :         /* Recreate the PAREN_EXPR from __builtin_assoc_barrier.  */
   23556              :         {
   23557            1 :           tree op0 = RECUR (TREE_OPERAND (t, 0));
   23558            1 :           RETURN (build1_loc (input_location, PAREN_EXPR,
   23559              :                               TREE_TYPE (op0), op0));
   23560              :         }
   23561              : 
   23562           23 :     case VEC_PERM_EXPR:
   23563           23 :       {
   23564           23 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   23565           23 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   23566           23 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   23567           23 :         RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
   23568              :                                        complain));
   23569              :       }
   23570              : 
   23571     12097226 :     case REQUIRES_EXPR:
   23572     12097226 :       {
   23573     12097226 :         complain &= ~tf_warning_or_error;
   23574     12097226 :         tree r = tsubst_requires_expr (t, args, complain, in_decl);
   23575     12094526 :         RETURN (r);
   23576              :       }
   23577              : 
   23578            9 :     case RANGE_EXPR:
   23579              :       /* No need to substitute further, a RANGE_EXPR will always be built
   23580              :          with constant operands.  */
   23581            9 :       RETURN (t);
   23582              : 
   23583    164376715 :     case NON_LVALUE_EXPR:
   23584    164376715 :     case VIEW_CONVERT_EXPR:
   23585    164376715 :       {
   23586    164376715 :         tree op = RECUR (TREE_OPERAND (t, 0));
   23587              : 
   23588    164376715 :         if (location_wrapper_p (t))
   23589              :           /* We need to do this here as well as in tsubst_copy so we get the
   23590              :              other tsubst_copy_and_build semantics for a PARM_DECL operand.  */
   23591    164361057 :           RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
   23592              : 
   23593        15658 :         gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
   23594        15658 :         if (REF_PARENTHESIZED_P (t))
   23595              :           /* force_paren_expr can also create a VIEW_CONVERT_EXPR.  */
   23596            0 :           RETURN (finish_parenthesized_expr (op));
   23597              : 
   23598        15658 :         check_param_in_postcondition (op, EXPR_LOCATION (t));
   23599              : 
   23600        15658 :         if (flag_contracts && processing_contract_condition)
   23601          453 :             op = constify_contract_access (op);
   23602              : 
   23603              :         /* Otherwise, we're dealing with a wrapper to make a C++20 template
   23604              :            parameter object const.  */
   23605        15658 :         if (TREE_TYPE (op) == NULL_TREE
   23606        15658 :             || !CP_TYPE_CONST_P (TREE_TYPE (op)))
   23607              :           {
   23608              :             /* The template argument is not const, presumably because
   23609              :                it is still dependent, and so not the const template parm
   23610              :                object.  */
   23611           89 :             tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   23612           89 :             if (TREE_CODE (op) == CONSTRUCTOR
   23613           86 :                 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
   23614              :               {
   23615              :                 /* Don't add a wrapper to these.  */
   23616           12 :                 op = copy_node (op);
   23617           12 :                 TREE_TYPE (op) = type;
   23618              :               }
   23619              :             else
   23620              :               /* Do add a wrapper otherwise (in particular, if op is
   23621              :                  another TEMPLATE_PARM_INDEX).  */
   23622           77 :               op = build1 (VIEW_CONVERT_EXPR, type, op);
   23623              :           }
   23624        15658 :         RETURN (op);
   23625              :       }
   23626              : 
   23627         3732 :     case REFLECT_EXPR:
   23628         3732 :       {
   23629         3732 :         tree h = REFLECT_EXPR_HANDLE (t);
   23630         3732 :         reflect_kind kind = REFLECT_EXPR_KIND (t);
   23631         3732 :         if (TYPE_P (h) || TREE_CODE (h) == NAMESPACE_DECL)
   23632         3114 :           h = tsubst (h, args, complain, in_decl);
   23633          618 :         else if (kind == REFLECT_ANNOTATION)
   23634              :           /* annotations_of should be called on reflections of already
   23635              :              instantiated entities and so no need to tsubst the annotation
   23636              :              attribute and we rely on pointer equality of that.  */
   23637              :           ;
   23638          616 :         else if (kind == REFLECT_BASE)
   23639              :           /* Don't substitute; class_bases_of is never called for a dependent
   23640              :              type.  */;
   23641          614 :         else if (TREE_CODE (h) == SCOPE_REF)
   23642           78 :           h = tsubst_qualified_id (h, args, complain, in_decl,
   23643              :                                    /*done=*/true, /*address_p=*/false,
   23644              :                                    /*reflecting_p=*/true);
   23645              :         else
   23646              :           {
   23647              :             /* [expr.reflect] The id-expression of a reflect-expression is
   23648              :                an unevaluated operand.  */
   23649          536 :             cp_unevaluated u;
   23650          536 :             h = RECUR (h);
   23651          536 :           }
   23652         3732 :         RETURN (get_reflection (EXPR_LOCATION (t), h, kind));
   23653              :       }
   23654              : 
   23655          952 :     case SPLICE_EXPR:
   23656          952 :       RETURN (tsubst_splice_expr (t, args, complain, in_decl));
   23657              : 
   23658            0 :     default:
   23659              :       /* Handle Objective-C++ constructs, if appropriate.  */
   23660            0 :       if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
   23661            0 :         RETURN (subst);
   23662              : 
   23663              :       /* We shouldn't get here, but keep going if !flag_checking.  */
   23664            0 :       if (flag_checking)
   23665            0 :         gcc_unreachable ();
   23666            0 :       RETURN (t);
   23667              :     }
   23668              : 
   23669              : #undef RECUR
   23670              : #undef RETURN
   23671   1924498290 :  out:
   23672   1924498290 :   input_location = save_loc;
   23673   1924498290 :   return retval;
   23674              : }
   23675              : 
   23676              : /* Verify that the instantiated ARGS are valid. For type arguments,
   23677              :    make sure that the type's linkage is ok. For non-type arguments,
   23678              :    make sure they are constants if they are integral or enumerations.
   23679              :    Emit an error under control of COMPLAIN, and return TRUE on error.  */
   23680              : 
   23681              : static bool
   23682    293150498 : check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
   23683              : {
   23684    293150498 :   if (dependent_template_arg_p (t))
   23685              :     return false;
   23686    264692403 :   if (ARGUMENT_PACK_P (t))
   23687              :     {
   23688     18562996 :       tree vec = ARGUMENT_PACK_ARGS (t);
   23689     18562996 :       int len = TREE_VEC_LENGTH (vec);
   23690     18562996 :       bool result = false;
   23691     18562996 :       int i;
   23692              : 
   23693     51449894 :       for (i = 0; i < len; ++i)
   23694     32886898 :         if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
   23695            0 :           result = true;
   23696              :       return result;
   23697              :     }
   23698    246129407 :   else if (TYPE_P (t))
   23699              :     {
   23700              :       /* [basic.link]: A name with no linkage (notably, the name
   23701              :          of a class or enumeration declared in a local scope)
   23702              :          shall not be used to declare an entity with linkage.
   23703              :          This implies that names with no linkage cannot be used as
   23704              :          template arguments
   23705              : 
   23706              :          DR 757 relaxes this restriction for C++0x.  */
   23707    208159079 :       tree nt = (cxx_dialect > cxx98 ? NULL_TREE
   23708       190658 :                  : no_linkage_check (t, /*relaxed_p=*/false));
   23709              : 
   23710       190658 :       if (nt)
   23711              :         {
   23712              :           /* DR 488 makes use of a type with no linkage cause
   23713              :              type deduction to fail.  */
   23714            8 :           if (complain & tf_error)
   23715              :             {
   23716           10 :               if (TYPE_UNNAMED_P (nt))
   23717            0 :                 error ("%qT is/uses unnamed type", t);
   23718              :               else
   23719            5 :                 error ("template argument for %qD uses local type %qT",
   23720              :                        tmpl, t);
   23721              :             }
   23722              :           return true;
   23723              :         }
   23724              :       /* In order to avoid all sorts of complications, we do not
   23725              :          allow variably-modified types as template arguments.  */
   23726    208159071 :       else if (variably_modified_type_p (t, NULL_TREE))
   23727              :         {
   23728            6 :           if (complain & tf_error)
   23729            3 :             error ("%qT is a variably modified type", t);
   23730              :           return true;
   23731              :         }
   23732              :     }
   23733              :   /* Class template and alias template arguments should be OK.  */
   23734     37970328 :   else if (DECL_TYPE_TEMPLATE_P (t))
   23735              :     ;
   23736              :   /* A non-type argument of integral or enumerated type must be a
   23737              :      constant.  */
   23738     37458628 :   else if (TREE_TYPE (t)
   23739     37458628 :            && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
   23740     37435934 :            && !REFERENCE_REF_P (t)
   23741     74894412 :            && !TREE_CONSTANT (t))
   23742              :     {
   23743            0 :       if (complain & tf_error)
   23744            0 :         error ("integral expression %qE is not constant", t);
   23745              :       return true;
   23746              :     }
   23747              :   return false;
   23748              : }
   23749              : 
   23750              : static bool
   23751    170025852 : check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
   23752              : {
   23753    170025852 :   int ix, len = DECL_NTPARMS (tmpl);
   23754    170025852 :   bool result = false;
   23755              : 
   23756    430289452 :   for (ix = 0; ix != len; ix++)
   23757              :     {
   23758    260263600 :       if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
   23759           14 :         result = true;
   23760              :     }
   23761    170025852 :   if (result && (complain & tf_error))
   23762            8 :     error ("  trying to instantiate %qD", tmpl);
   23763    170025852 :   return result;
   23764              : }
   23765              : 
   23766              : /* Call mark_used on each entity within the non-type template arguments in
   23767              :    ARGS for an instantiation of TMPL, to ensure that each such entity is
   23768              :    considered odr-used (and therefore marked for instantiation) regardless of
   23769              :    whether the specialization was first formed in a template context (which
   23770              :    inhibits mark_used).
   23771              : 
   23772              :    This function assumes push_to_top_level has been called beforehand.  */
   23773              : 
   23774              : static void
   23775     81154743 : mark_template_arguments_used (tree tmpl, tree args)
   23776              : {
   23777              :   /* It suffices to do this only when instantiating a primary template.  */
   23778     81154743 :   if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
   23779              :     return;
   23780              : 
   23781              :   /* We already marked outer arguments when specializing the context.  */
   23782     65101055 :   args = INNERMOST_TEMPLATE_ARGS (args);
   23783              : 
   23784     65101055 :   mark_template_arguments_used_1 (args);
   23785              : }
   23786              : 
   23787              : /* Main recursive part of the above.  */
   23788              : 
   23789              : static void
   23790     65657696 : mark_template_arguments_used_1 (tree args)
   23791              : {
   23792    171014908 :   for (tree arg : tree_vec_range (args))
   23793              :     {
   23794              :       /* A (pointer/reference to) function or variable NTTP argument.  */
   23795    105357212 :       if (TREE_CODE (arg) == ADDR_EXPR
   23796    105354504 :           || TREE_CODE (arg) == INDIRECT_REF)
   23797              :         {
   23798         6416 :           while (TREE_CODE (arg) == ADDR_EXPR
   23799         3458 :                  || REFERENCE_REF_P (arg)
   23800         9624 :                  || CONVERT_EXPR_P (arg))
   23801         3458 :             arg = TREE_OPERAND (arg, 0);
   23802         2958 :           if (VAR_OR_FUNCTION_DECL_P (arg))
   23803              :             {
   23804              :               /* Pass tf_none to avoid duplicate diagnostics: if this call
   23805              :                  fails then an earlier call to mark_used for this argument
   23806              :                  must have also failed and emitted a diagnostic.  */
   23807         2943 :               bool ok = mark_used (arg, tf_none);
   23808         2943 :               gcc_checking_assert (ok || seen_error ());
   23809              :             }
   23810              :         }
   23811              :       /* A member function pointer.  */
   23812    105354254 :       else if (TREE_CODE (arg) == PTRMEM_CST)
   23813              :         {
   23814          646 :           bool ok = mark_used (PTRMEM_CST_MEMBER (arg), tf_none);
   23815          646 :           gcc_checking_assert (ok || seen_error ());
   23816              :         }
   23817              :       /* A class NTTP argument.  */
   23818    105353608 :       else if (VAR_P (arg)
   23819    105353608 :                && DECL_NTTP_OBJECT_P (arg))
   23820              :         {
   23821        55734 :           auto mark_used_r = [](tree *tp, int *, void *) {
   23822        39307 :             if (VAR_OR_FUNCTION_DECL_P (*tp))
   23823              :               {
   23824           21 :                 bool ok = mark_used (*tp, tf_none);
   23825           21 :                 gcc_checking_assert (ok || seen_error ());
   23826              :               }
   23827        39307 :             return NULL_TREE;
   23828              :           };
   23829        16427 :           cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
   23830              :                                            mark_used_r, nullptr);
   23831              :         }
   23832    105337181 :       else if (TREE_CODE (arg) == NONTYPE_ARGUMENT_PACK)
   23833       556641 :         mark_template_arguments_used_1 (ARGUMENT_PACK_ARGS (arg));
   23834              :     }
   23835     65657696 : }
   23836              : 
   23837              : /* We're out of SFINAE context now, so generate diagnostics for the access
   23838              :    errors we saw earlier when instantiating D from TMPL and ARGS.  */
   23839              : 
   23840              : static void
   23841            9 : recheck_decl_substitution (tree d, tree tmpl, tree args)
   23842              : {
   23843            9 :   tree pattern = DECL_TEMPLATE_RESULT (tmpl);
   23844            9 :   tree type = TREE_TYPE (pattern);
   23845            9 :   location_t loc = input_location;
   23846              : 
   23847            9 :   push_access_scope (d);
   23848            9 :   push_deferring_access_checks (dk_no_deferred);
   23849            9 :   input_location = DECL_SOURCE_LOCATION (pattern);
   23850            9 :   tsubst (type, args, tf_warning_or_error, d);
   23851            9 :   input_location = loc;
   23852            9 :   pop_deferring_access_checks ();
   23853            9 :   pop_access_scope (d);
   23854            9 : }
   23855              : 
   23856              : /* Instantiate the indicated variable, function, or alias template TMPL with
   23857              :    the template arguments in TARG_PTR.  */
   23858              : 
   23859              : tree
   23860    285333071 : instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
   23861              : {
   23862    285333071 :   auto_timevar tv (TV_TEMPLATE_INST);
   23863              : 
   23864    285333071 :   tree targ_ptr = orig_args;
   23865    285333071 :   tree fndecl;
   23866    285333071 :   tree gen_tmpl;
   23867    285333071 :   bool access_ok = true;
   23868              : 
   23869    285333071 :   if (tmpl == error_mark_node)
   23870              :     return error_mark_node;
   23871              : 
   23872              :   /* The other flags are not relevant anymore here, especially tf_partial
   23873              :      shouldn't be set.  For instance, we may be called while doing a partial
   23874              :      substitution of a template variable, but the type of the variable
   23875              :      template may be auto, in which case we will call do_auto_deduction
   23876              :      in mark_used (which clears tf_partial) and the auto must be properly
   23877              :      reduced at that time for the deduction to work.
   23878              : 
   23879              :      Note that later below we set tf_partial iff there are dependent arguments;
   23880              :      the above is concerned specifically about the non-dependent case.  */
   23881    285333062 :   complain &= tf_warning_or_error;
   23882              : 
   23883    285333062 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
   23884              : 
   23885    285333062 :   if (modules_p ())
   23886      1306860 :     lazy_load_pendings (tmpl);
   23887              : 
   23888              :   /* If this function is a clone, handle it specially.  */
   23889    285333062 :   if (DECL_CLONED_FUNCTION_P (tmpl))
   23890              :     {
   23891      5583488 :       tree spec;
   23892      5583488 :       tree clone;
   23893              : 
   23894              :       /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
   23895              :          DECL_CLONED_FUNCTION.  */
   23896      5583488 :       spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
   23897              :                                    targ_ptr, complain);
   23898      5583488 :       if (spec == error_mark_node)
   23899              :         return error_mark_node;
   23900              : 
   23901              :       /* Look for the clone.  */
   23902     10927535 :       FOR_EACH_CLONE (clone, spec)
   23903     10927535 :         if (DECL_NAME (clone) == DECL_NAME (tmpl))
   23904              :           return clone;
   23905              :       /* We should always have found the clone by now.  */
   23906            0 :       gcc_unreachable ();
   23907              :       return NULL_TREE;
   23908              :     }
   23909              : 
   23910    279749574 :   if (targ_ptr == error_mark_node)
   23911              :     return error_mark_node;
   23912              : 
   23913              :   /* Check to see if we already have this specialization.  */
   23914    279749574 :   gen_tmpl = most_general_template (tmpl);
   23915    559499148 :   if (TMPL_ARGS_DEPTH (targ_ptr)
   23916    279749574 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
   23917              :     /* targ_ptr only has the innermost template args, so add the outer ones
   23918              :        from tmpl, which could be either a partial instantiation or gen_tmpl (in
   23919              :        the case of a non-dependent call within a template definition).  */
   23920      9658318 :     targ_ptr = (add_outermost_template_args
   23921      9658318 :                 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
   23922              :                  targ_ptr));
   23923              : 
   23924    279749574 :   hashval_t hash = spec_hasher::hash (gen_tmpl, targ_ptr);
   23925    279749574 :   tree spec = retrieve_specialization (gen_tmpl, targ_ptr, hash);
   23926              : 
   23927    279749574 :   gcc_checking_assert (tmpl == gen_tmpl
   23928              :                        || ((fndecl
   23929              :                             = retrieve_specialization (tmpl, orig_args, 0))
   23930              :                            == spec)
   23931              :                        || fndecl == NULL_TREE);
   23932              : 
   23933    279749574 :   if (spec != NULL_TREE)
   23934              :     {
   23935    162370707 :       if (FNDECL_HAS_ACCESS_ERRORS (spec))
   23936              :         {
   23937           15 :           if (complain & tf_error)
   23938            9 :             recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
   23939           15 :           return error_mark_node;
   23940              :         }
   23941              :       return spec;
   23942              :     }
   23943              : 
   23944    117378867 :   if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
   23945              :                                complain))
   23946            6 :     return error_mark_node;
   23947              : 
   23948              :   /* We are building a FUNCTION_DECL, during which the access of its
   23949              :      parameters and return types have to be checked.  However this
   23950              :      FUNCTION_DECL which is the desired context for access checking
   23951              :      is not built yet.  We solve this chicken-and-egg problem by
   23952              :      deferring all checks until we have the FUNCTION_DECL.  */
   23953    117378861 :   deferring_access_check_sentinel dacs (dk_deferred);
   23954              : 
   23955              :   /* Instantiation of the function happens in the context of the function
   23956              :      template, not the context of the overload resolution we're doing.  */
   23957    117378861 :   push_to_top_level ();
   23958              :   /* If there are dependent arguments, e.g. because we're doing partial
   23959              :      ordering, make sure processing_template_decl stays set.  And set
   23960              :      tf_partial mainly for benefit of instantiation of alias templates
   23961              :      that contain extra-args trees.  */
   23962    117378861 :   if (uses_template_parms (targ_ptr))
   23963              :     {
   23964     20729867 :       ++processing_template_decl;
   23965     20729867 :       complain |= tf_partial;
   23966              :     }
   23967    117378861 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   23968              :     {
   23969     27740674 :       tree ctx;
   23970     27740674 :       if (!uses_template_parms (DECL_CONTEXT (tmpl)))
   23971              :         /* If the context of the partially instantiated template is
   23972              :            already non-dependent, then we might as well use it.  */
   23973     24431103 :         ctx = DECL_CONTEXT (tmpl);
   23974              :       else
   23975      3309571 :         ctx = tsubst_entering_scope (DECL_CONTEXT (gen_tmpl), targ_ptr,
   23976              :                                      complain, gen_tmpl);
   23977     27740674 :       push_nested_class (ctx);
   23978              :     }
   23979              : 
   23980    117378861 :   tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
   23981              : 
   23982    117378861 :   tree partial_ti = NULL_TREE;
   23983    117378861 :   fndecl = NULL_TREE;
   23984    117378861 :   if (VAR_P (pattern))
   23985              :     {
   23986              :       /* We need to determine if we're using a partial or explicit
   23987              :          specialization now, because the type of the variable could be
   23988              :          different.  */
   23989     13475756 :       tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
   23990     13475756 :       partial_ti = most_specialized_partial_spec (tid, complain);
   23991     13475756 :       if (partial_ti == error_mark_node)
   23992              :         pattern = error_mark_node;
   23993     13475753 :       else if (partial_ti)
   23994              :         {
   23995       921995 :           tree partial_tmpl = TI_TEMPLATE (partial_ti);
   23996       921995 :           tree partial_args = TI_ARGS (partial_ti);
   23997       921995 :           tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
   23998       921995 :           fndecl = tsubst_decl (partial_pat, partial_args, complain,
   23999              :                                 /*use_spec_table=*/false);
   24000              :         }
   24001              :     }
   24002              : 
   24003              :   /* Substitute template parameters to obtain the specialization.  */
   24004       921995 :   if (fndecl == NULL_TREE)
   24005    116456866 :     fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false);
   24006    117370725 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   24007     27740674 :     pop_nested_class ();
   24008    117370725 :   pop_from_top_level ();
   24009              : 
   24010    117370725 :   if (fndecl == error_mark_node)
   24011              :     return error_mark_node;
   24012              : 
   24013              :   /* Substituting the type might have recursively instantiated this
   24014              :      same alias (c++/117530).  */
   24015     79224488 :   if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   24016    113857407 :       && (spec = retrieve_specialization (gen_tmpl, targ_ptr, hash)))
   24017              :     return spec;
   24018              : 
   24019              :   /* The DECL_TI_TEMPLATE should always be the immediate parent
   24020              :      template, not the most general template.  */
   24021     79224476 :   DECL_TI_TEMPLATE (fndecl) = tmpl;
   24022     79224476 :   DECL_TI_ARGS (fndecl) = targ_ptr;
   24023     79224476 :   if (VAR_P (pattern))
   24024              :     {
   24025              :       /* Now that we we've formed this variable template specialization,
   24026              :          remember the result of most_specialized_partial_spec for it.  */
   24027     13475753 :       TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
   24028              : 
   24029              :       /* And remember if the variable was declared with [].  */
   24030     13475753 :       if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
   24031     13475753 :           && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
   24032           12 :         SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
   24033              :     }
   24034              : 
   24035     79224476 :   fndecl = register_specialization (fndecl, gen_tmpl, targ_ptr, false, hash);
   24036     79224476 :   if (fndecl == error_mark_node)
   24037              :     return error_mark_node;
   24038              : 
   24039     79224476 :   set_instantiating_module (fndecl);
   24040              : 
   24041              :   /* Now we know the specialization, compute access previously
   24042              :      deferred.  Do no access control for inheriting constructors,
   24043              :      as we already checked access for the inherited constructor.  */
   24044     79224476 :   if (!(flag_new_inheriting_ctors
   24045    110339188 :         && DECL_INHERITED_CTOR (fndecl)))
   24046              :     {
   24047     79211180 :       push_access_scope (fndecl);
   24048     79211180 :       if (!perform_deferred_access_checks (complain))
   24049              :         access_ok = false;
   24050     79211180 :       pop_access_scope (fndecl);
   24051              :     }
   24052              : 
   24053              :   /* If we've just instantiated the main entry point for a function,
   24054              :      instantiate all the alternate entry points as well.  We do this
   24055              :      by cloning the instantiation of the main entry point, not by
   24056              :      instantiating the template clones.  */
   24057     79224476 :   if (tree chain = DECL_CHAIN (gen_tmpl))
   24058     78752920 :     if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
   24059       186130 :       clone_cdtor (fndecl, /*update_methods=*/false);
   24060              : 
   24061     79224476 :   if (!access_ok)
   24062              :     {
   24063           84 :       if (!(complain & tf_error))
   24064              :         {
   24065              :           /* Remember to reinstantiate when we're out of SFINAE so the user
   24066              :              can see the errors.  */
   24067           84 :           FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
   24068              :         }
   24069           84 :       return error_mark_node;
   24070              :     }
   24071              : 
   24072              :   return fndecl;
   24073    285324935 : }
   24074              : 
   24075              : /* Instantiate the alias template TMPL with ARGS.  Also push a template
   24076              :    instantiation level, which instantiate_template doesn't do because
   24077              :    functions and variables have sufficient context established by the
   24078              :    callers.  */
   24079              : 
   24080              : static tree
   24081    162060342 : instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
   24082              : {
   24083    162060342 :   if (tmpl == error_mark_node || args == error_mark_node)
   24084              :     return error_mark_node;
   24085              : 
   24086    161194273 :   args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
   24087              :                                 args, tmpl, complain);
   24088    161194273 :   if (args == error_mark_node)
   24089              :     return error_mark_node;
   24090              : 
   24091              :   /* FIXME check for satisfaction in check_instantiated_args.  */
   24092    161193536 :   if (!constraints_satisfied_p (tmpl, args))
   24093              :     {
   24094       236210 :       if (complain & tf_error)
   24095              :         {
   24096           27 :           auto_diagnostic_group d;
   24097           27 :           error ("template constraint failure for %qD", tmpl);
   24098           27 :           diagnose_constraints (input_location, tmpl, args);
   24099           27 :         }
   24100       236210 :       return error_mark_node;
   24101              :     }
   24102              : 
   24103    160957326 :   if (!push_tinst_level (tmpl, args))
   24104            0 :     return error_mark_node;
   24105    160957323 :   tree r = instantiate_template (tmpl, args, complain);
   24106    160957323 :   pop_tinst_level ();
   24107              : 
   24108    160957323 :   if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
   24109              :     {
   24110              :       /* Note this is also done at parse time from push_template_decl.  */
   24111              :       /* An alias template specialization can be dependent
   24112              :          even if its underlying type is not.  */
   24113     12241291 :       TYPE_DEPENDENT_P (d) = true;
   24114     12241291 :       TYPE_DEPENDENT_P_VALID (d) = true;
   24115              :       /* Sometimes a dependent alias spec is equivalent to its expansion,
   24116              :          sometimes not.  So always use structural_comptypes.  */
   24117     12241291 :       SET_TYPE_STRUCTURAL_EQUALITY (d);
   24118              :     }
   24119              : 
   24120              :   return r;
   24121              : }
   24122              : 
   24123              : /* PARM is a template parameter pack for FN.  Returns true iff
   24124              :    PARM is used in a deducible way in the argument list of FN.  */
   24125              : 
   24126              : static bool
   24127      6397731 : pack_deducible_p (tree parm, tree fn)
   24128              : {
   24129      6397731 :   tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
   24130     19176034 :   for (; t; t = TREE_CHAIN (t))
   24131              :     {
   24132      6380901 :       tree type = TREE_VALUE (t);
   24133      6380901 :       tree packs;
   24134      6380901 :       if (!PACK_EXPANSION_P (type))
   24135      6378819 :         continue;
   24136         4164 :       for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
   24137         3838 :            packs; packs = TREE_CHAIN (packs))
   24138         2085 :         if (template_args_equal (TREE_VALUE (packs), parm))
   24139              :           {
   24140              :             /* The template parameter pack is used in a function parameter
   24141              :                pack.  If this is the end of the parameter list, the
   24142              :                template parameter pack is deducible.  */
   24143          329 :             if (TREE_CHAIN (t) == void_list_node)
   24144              :               return true;
   24145              :             else
   24146              :               /* Otherwise, not.  Well, it could be deduced from
   24147              :                  a non-pack parameter, but doing so would end up with
   24148              :                  a deduction mismatch, so don't bother.  */
   24149           21 :               return false;
   24150              :           }
   24151              :     }
   24152              :   /* The template parameter pack isn't used in any function parameter
   24153              :      packs, but it might be used deeper, e.g. tuple<Args...>.  */
   24154              :   return true;
   24155              : }
   24156              : 
   24157              : /* Subroutine of fn_type_unification: check non-dependent parms for
   24158              :    convertibility.  */
   24159              : 
   24160              : static int
   24161    118594739 : check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
   24162              :                                  tree fn, unification_kind_t strict, int flags,
   24163              :                                  struct conversion **convs, bool explain_p,
   24164              :                                  bool noninst_only_p)
   24165              : {
   24166              :   /* Non-constructor methods need to leave a conversion for 'this', which
   24167              :      isn't included in nargs here.  */
   24168    118594739 :   unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   24169    138932000 :                      && !DECL_CONSTRUCTOR_P (fn));
   24170              : 
   24171    118594739 :   for (unsigned ia = 0;
   24172    268002027 :        parms && parms != void_list_node && ia < nargs; )
   24173              :     {
   24174    154082290 :       tree parm = TREE_VALUE (parms);
   24175              : 
   24176    154082290 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   24177    154082290 :           && (!TREE_CHAIN (parms)
   24178      1498430 :               || TREE_CHAIN (parms) == void_list_node))
   24179              :         /* For a function parameter pack that occurs at the end of the
   24180              :            parameter-declaration-list, the type A of each remaining
   24181              :            argument of the call is compared with the type P of the
   24182              :            declarator-id of the function parameter pack.  */
   24183              :         break;
   24184              : 
   24185    152583935 :       parms = TREE_CHAIN (parms);
   24186              : 
   24187    152583935 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   24188              :         /* For a function parameter pack that does not occur at the
   24189              :            end of the parameter-declaration-list, the type of the
   24190              :            parameter pack is a non-deduced context.  */
   24191           81 :         continue;
   24192              : 
   24193    152583854 :       if (!uses_template_parms (parm))
   24194              :         {
   24195     32684842 :           tree arg = args[ia];
   24196     32684842 :           conversion **conv_p = convs ? &convs[ia+offset] : NULL;
   24197     32684842 :           int lflags = conv_flags (ia, nargs, fn, arg, flags);
   24198              : 
   24199              :           /* As in add_function_candidate, don't consider conversion to an
   24200              :              unrelated type when LOOKUP_DEFAULTED.  */
   24201     32684842 :           if ((flags & LOOKUP_DEFAULTED)
   24202         3855 :               && ia == 0
   24203         7142 :               && (DECL_CONSTRUCTOR_P (fn)
   24204          280 :                   || DECL_ASSIGNMENT_OPERATOR_P (fn))
   24205     32688133 :               && !reference_related_p (non_reference (parm),
   24206         3291 :                                        DECL_CONTEXT (fn)))
   24207              :             return 1;
   24208              : 
   24209     32681551 :           if (check_non_deducible_conversion (parm, arg, strict, lflags,
   24210              :                                               conv_p, explain_p, noninst_only_p))
   24211              :             return 1;
   24212              :         }
   24213              : 
   24214    149407207 :       ++ia;
   24215              :     }
   24216              : 
   24217              :   return 0;
   24218              : }
   24219              : 
   24220              : /* The FN is a TEMPLATE_DECL for a function.  ARGS is an array with
   24221              :    NARGS elements of the arguments that are being used when calling
   24222              :    it.  TARGS is a vector into which the deduced template arguments
   24223              :    are placed.
   24224              : 
   24225              :    Returns either a FUNCTION_DECL for the matching specialization of FN or
   24226              :    NULL_TREE if no suitable specialization can be found.  If EXPLAIN_P is
   24227              :    true, diagnostics will be printed to explain why it failed.
   24228              : 
   24229              :    If FN is a conversion operator, or we are trying to produce a specific
   24230              :    specialization, RETURN_TYPE is the return type desired.
   24231              : 
   24232              :    The EXPLICIT_TARGS are explicit template arguments provided via a
   24233              :    template-id.
   24234              : 
   24235              :    The parameter STRICT is one of:
   24236              : 
   24237              :    DEDUCE_CALL:
   24238              :      We are deducing arguments for a function call, as in
   24239              :      [temp.deduct.call].  If RETURN_TYPE is non-null, we are
   24240              :      deducing arguments for a call to the result of a conversion
   24241              :      function template, as in [over.call.object].
   24242              : 
   24243              :    DEDUCE_CONV:
   24244              :      We are deducing arguments for a conversion function, as in
   24245              :      [temp.deduct.conv].
   24246              : 
   24247              :    DEDUCE_EXACT:
   24248              :      We are deducing arguments when doing an explicit instantiation
   24249              :      as in [temp.explicit], when determining an explicit specialization
   24250              :      as in [temp.expl.spec], or when taking the address of a function
   24251              :      template, as in [temp.deduct.funcaddr].  */
   24252              : 
   24253              : tree
   24254    503598931 : fn_type_unification (tree fn,
   24255              :                      tree explicit_targs,
   24256              :                      tree targs,
   24257              :                      const tree *args,
   24258              :                      unsigned int nargs,
   24259              :                      tree return_type,
   24260              :                      unification_kind_t strict,
   24261              :                      int flags,
   24262              :                      struct conversion **convs,
   24263              :                      bool explain_p,
   24264              :                      bool decltype_p)
   24265              : {
   24266    503598931 :   tree parms;
   24267    503598931 :   tree fntype;
   24268    503598931 :   tree decl = NULL_TREE;
   24269    503598931 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   24270    503598931 :   bool ok;
   24271    503598931 :   static int deduction_depth;
   24272              :   /* type_unification_real will pass back any access checks from default
   24273              :      template argument substitution.  */
   24274    503598931 :   vec<deferred_access_check, va_gc> *checks = NULL;
   24275              :   /* We don't have all the template args yet.  */
   24276    503598931 :   bool incomplete = true;
   24277              : 
   24278    503598931 :   tree orig_fn = fn;
   24279    503598931 :   if (flag_new_inheriting_ctors)
   24280    503597133 :     fn = strip_inheriting_ctors (fn);
   24281              : 
   24282    503598931 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
   24283    503598931 :   tree r = error_mark_node;
   24284              : 
   24285    503598931 :   tree full_targs = targs;
   24286   1007197862 :   if (TMPL_ARGS_DEPTH (targs)
   24287    503598931 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
   24288        49038 :     full_targs = (add_outermost_template_args
   24289        49038 :                   (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
   24290              :                    targs));
   24291              : 
   24292    503598931 :   if (decltype_p)
   24293     29886940 :     complain |= tf_decltype;
   24294              : 
   24295              :   /* In C++0x, it's possible to have a function template whose type depends
   24296              :      on itself recursively.  This is most obvious with decltype, but can also
   24297              :      occur with enumeration scope (c++/48969).  So we need to catch infinite
   24298              :      recursion and reject the substitution at deduction time; this function
   24299              :      will return error_mark_node for any repeated substitution.
   24300              : 
   24301              :      This also catches excessive recursion such as when f<N> depends on
   24302              :      f<N-1> across all integers, and returns error_mark_node for all the
   24303              :      substitutions back up to the initial one.
   24304              : 
   24305              :      This is, of course, not reentrant.  */
   24306    503598931 :   if (excessive_deduction_depth)
   24307            0 :     return error_mark_node;
   24308    503598931 :   ++deduction_depth;
   24309              : 
   24310    503598931 :   gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
   24311              : 
   24312    503598931 :   fntype = TREE_TYPE (fn);
   24313    503598931 :   if (explicit_targs)
   24314              :     {
   24315              :       /* [temp.deduct]
   24316              : 
   24317              :          The specified template arguments must match the template
   24318              :          parameters in kind (i.e., type, nontype, template), and there
   24319              :          must not be more arguments than there are parameters;
   24320              :          otherwise type deduction fails.
   24321              : 
   24322              :          Nontype arguments must match the types of the corresponding
   24323              :          nontype template parameters, or must be convertible to the
   24324              :          types of the corresponding nontype parameters as specified in
   24325              :          _temp.arg.nontype_, otherwise type deduction fails.
   24326              : 
   24327              :          All references in the function type of the function template
   24328              :          to the corresponding template parameters are replaced by the
   24329              :          specified template argument values.  If a substitution in a
   24330              :          template parameter or in the function type of the function
   24331              :          template results in an invalid type, type deduction fails.  */
   24332     55813983 :       int i, len = TREE_VEC_LENGTH (tparms);
   24333     55813983 :       location_t loc = input_location;
   24334     55813983 :       incomplete = false;
   24335              : 
   24336     55813983 :       if (explicit_targs == error_mark_node)
   24337            0 :         goto fail;
   24338              : 
   24339    111627966 :       if (TMPL_ARGS_DEPTH (explicit_targs)
   24340    111627966 :           < TMPL_ARGS_DEPTH (full_targs))
   24341        19468 :         explicit_targs = add_outermost_template_args (full_targs,
   24342              :                                                       explicit_targs);
   24343              : 
   24344              :       /* Adjust any explicit template arguments before entering the
   24345              :          substitution context.  */
   24346     55813983 :       explicit_targs
   24347     55813983 :         = (coerce_template_parms (tparms, explicit_targs, fn,
   24348              :                                   complain|tf_partial,
   24349              :                                   /*require_all_args=*/false));
   24350     55813983 :       if (explicit_targs == error_mark_node)
   24351      3380543 :         goto fail;
   24352              : 
   24353              :       /* Substitute the explicit args into the function type.  This is
   24354              :          necessary so that, for instance, explicitly declared function
   24355              :          arguments can match null pointed constants.  If we were given
   24356              :          an incomplete set of explicit args, we must not do semantic
   24357              :          processing during substitution as we could create partial
   24358              :          instantiations.  */
   24359    119445422 :       for (i = 0; i < len; i++)
   24360              :         {
   24361     67011982 :           tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
   24362     67011982 :           bool parameter_pack = false;
   24363     67011982 :           tree targ = TREE_VEC_ELT (explicit_targs, i);
   24364              : 
   24365              :           /* Dig out the actual parm.  */
   24366     67011982 :           if (TREE_CODE (parm) == TYPE_DECL
   24367      7145014 :               || TREE_CODE (parm) == TEMPLATE_DECL)
   24368              :             {
   24369     59872076 :               parm = TREE_TYPE (parm);
   24370     59872076 :               parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
   24371              :             }
   24372      7139906 :           else if (TREE_CODE (parm) == PARM_DECL)
   24373              :             {
   24374      7139906 :               parm = DECL_INITIAL (parm);
   24375      7139906 :               parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
   24376              :             }
   24377              : 
   24378     67011982 :           if (targ == NULL_TREE)
   24379              :             /* No explicit argument for this template parameter.  */
   24380              :             incomplete = true;
   24381     54324750 :           else if (parameter_pack && pack_deducible_p (parm, fn))
   24382              :             {
   24383              :               /* Mark the argument pack as "incomplete". We could
   24384              :                  still deduce more arguments during unification.
   24385              :                  We remove this mark in type_unification_real.  */
   24386      6397710 :               ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
   24387      6397710 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ)
   24388      6397710 :                 = ARGUMENT_PACK_ARGS (targ);
   24389              : 
   24390              :               /* We have some incomplete argument packs.  */
   24391      6397710 :               incomplete = true;
   24392              :             }
   24393              :         }
   24394              : 
   24395     52433440 :       if (incomplete)
   24396              :         {
   24397     14921427 :           if (!push_tinst_level (fn, explicit_targs))
   24398              :             {
   24399            0 :               excessive_deduction_depth = true;
   24400            0 :               goto fail;
   24401              :             }
   24402     14921421 :           ++processing_template_decl;
   24403     14921421 :           input_location = DECL_SOURCE_LOCATION (fn);
   24404              :           /* Ignore any access checks; we'll see them again in
   24405              :              instantiate_template and they might have the wrong
   24406              :              access path at this point.  */
   24407     14921421 :           push_deferring_access_checks (dk_deferred);
   24408     14921421 :           tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
   24409     14921421 :           fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
   24410     14918724 :           pop_deferring_access_checks ();
   24411     14918724 :           input_location = loc;
   24412     14918724 :           --processing_template_decl;
   24413     14918724 :           pop_tinst_level ();
   24414              : 
   24415     14918724 :           if (fntype == error_mark_node)
   24416        19515 :             goto fail;
   24417              :         }
   24418              : 
   24419              :       /* Place the explicitly specified arguments in TARGS.  */
   24420     52411222 :       explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
   24421    119378642 :       for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
   24422     66967420 :         TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
   24423     52411222 :       if (!incomplete && CHECKING_P
   24424     89923235 :           && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   24425     37512013 :         SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
   24426              :           (targs, NUM_TMPL_ARGS (explicit_targs));
   24427              :     }
   24428              : 
   24429    500196170 :   if (return_type && strict != DEDUCE_CALL)
   24430              :     {
   24431     15759202 :       tree *new_args = XALLOCAVEC (tree, nargs + 1);
   24432     15759202 :       new_args[0] = return_type;
   24433     15759202 :       memcpy (new_args + 1, args, nargs * sizeof (tree));
   24434     15759202 :       args = new_args;
   24435     15759202 :       ++nargs;
   24436              :     }
   24437              : 
   24438    500196170 :   if (!incomplete)
   24439     37512013 :     goto deduced;
   24440              : 
   24441              :   /* Never do unification on the 'this' parameter.  */
   24442    462684157 :   parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
   24443              : 
   24444    462684157 :   if (return_type && strict == DEDUCE_CALL)
   24445              :     {
   24446              :       /* We're deducing for a call to the result of a template conversion
   24447              :          function.  The parms we really want are in return_type.  */
   24448           15 :       if (INDIRECT_TYPE_P (return_type))
   24449           15 :         return_type = TREE_TYPE (return_type);
   24450           15 :       parms = TYPE_ARG_TYPES (return_type);
   24451              :     }
   24452    462684142 :   else if (return_type)
   24453              :     {
   24454     14747169 :       parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
   24455              :     }
   24456              : 
   24457              :   /* We allow incomplete unification without an error message here
   24458              :      because the standard doesn't seem to explicitly prohibit it.  Our
   24459              :      callers must be ready to deal with unification failures in any
   24460              :      event.  */
   24461              : 
   24462              :   /* If we aren't explaining yet, push tinst context so we can see where
   24463              :      any errors (e.g. from class instantiations triggered by instantiation
   24464              :      of default template arguments) come from.  If we are explaining, this
   24465              :      context is redundant.  */
   24466    462684157 :   if (!explain_p && !push_tinst_level (fn, targs))
   24467              :     {
   24468            0 :       excessive_deduction_depth = true;
   24469            0 :       goto fail;
   24470              :     }
   24471              : 
   24472    462684151 :   ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   24473              :                                full_targs, parms, args, nargs, /*subr=*/0,
   24474              :                                strict, &checks, explain_p);
   24475    462684151 :   if (!explain_p)
   24476    462681376 :     pop_tinst_level ();
   24477    462684151 :   if (!ok)
   24478    399850820 :     goto fail;
   24479              : 
   24480              :   /* Now that we have bindings for all of the template arguments,
   24481              :      ensure that the arguments deduced for the template template
   24482              :      parameters have compatible template parameter lists.  We cannot
   24483              :      check this property before we have deduced all template
   24484              :      arguments, because the template parameter types of a template
   24485              :      template parameter might depend on prior template parameters
   24486              :      deduced after the template template parameter.  The following
   24487              :      ill-formed example illustrates this issue:
   24488              : 
   24489              :        template<typename T, template<T> class C> void f(C<5>, T);
   24490              : 
   24491              :        template<int N> struct X {};
   24492              : 
   24493              :        void g() {
   24494              :          f(X<5>(), 5l); // error: template argument deduction fails
   24495              :        }
   24496              : 
   24497              :      The template parameter list of 'C' depends on the template type
   24498              :      parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
   24499              :      'long'.  Thus, we can't check that 'C' cannot bind to 'X' at the
   24500              :      time that we deduce 'C'.  */
   24501    125666662 :   if (!template_template_parm_bindings_ok_p
   24502     62833331 :            (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
   24503              :     {
   24504           24 :       unify_inconsistent_template_template_parameters (explain_p);
   24505           24 :       goto fail;
   24506              :     }
   24507              : 
   24508     62833307 :  deduced:
   24509              : 
   24510              :   /* As a refinement of CWG2369, check first and foremost non-dependent
   24511              :      conversions that we know are not going to induce template instantiation
   24512              :      (PR99599).  */
   24513    100345320 :   if (strict == DEDUCE_CALL
   24514     60915015 :       && incomplete && flag_concepts
   24515    161021806 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   24516              :                                           convs, explain_p,
   24517              :                                           /*noninst_only_p=*/true))
   24518      2274581 :     goto fail;
   24519              : 
   24520              :   /* CWG2369: Check satisfaction before non-deducible conversions.  */
   24521     98070739 :   if (!constraints_satisfied_p (fn, targs))
   24522              :     {
   24523      2638310 :       if (explain_p)
   24524          703 :         diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
   24525      2638310 :       goto fail;
   24526              :     }
   24527              : 
   24528              :   /* DR 1391: All parameters have args, now check non-dependent parms for
   24529              :      convertibility.  We don't do this if all args were explicitly specified,
   24530              :      as the standard says that we substitute explicit args immediately.  */
   24531     95429729 :   if (incomplete
   24532     95429729 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   24533              :                                           convs, explain_p,
   24534              :                                           /*noninst_only_p=*/false))
   24535       902066 :     goto fail;
   24536              : 
   24537              :   /* All is well so far.  Now, check:
   24538              : 
   24539              :      [temp.deduct]
   24540              : 
   24541              :      When all template arguments have been deduced, all uses of
   24542              :      template parameters in nondeduced contexts are replaced with
   24543              :      the corresponding deduced argument values.  If the
   24544              :      substitution results in an invalid type, as described above,
   24545              :      type deduction fails.  */
   24546     94527663 :   if (!push_tinst_level (fn, targs))
   24547              :     {
   24548            0 :       excessive_deduction_depth = true;
   24549            0 :       goto fail;
   24550              :     }
   24551              : 
   24552              :   /* Also collect access checks from the instantiation.  */
   24553     94527657 :   reopen_deferring_access_checks (checks);
   24554              : 
   24555     94527657 :   decl = instantiate_template (fn, targs, complain);
   24556              : 
   24557     94519521 :   checks = get_deferred_access_checks ();
   24558     94519521 :   pop_deferring_access_checks ();
   24559              : 
   24560     94519521 :   pop_tinst_level ();
   24561              : 
   24562     94519521 :   if (decl == error_mark_node)
   24563      1675283 :     goto fail;
   24564              : 
   24565              :   /* Now perform any access checks encountered during substitution.  */
   24566     92844238 :   push_access_scope (decl);
   24567     92844238 :   ok = perform_access_checks (checks, complain);
   24568     92844238 :   pop_access_scope (decl);
   24569     92844238 :   if (!ok)
   24570           12 :     goto fail;
   24571              : 
   24572              :   /* If we're looking for an exact match, check that what we got
   24573              :      is indeed an exact match.  It might not be if some template
   24574              :      parameters are used in non-deduced contexts.  But don't check
   24575              :      for an exact match if we have dependent template arguments;
   24576              :      in that case we're doing partial ordering, and we already know
   24577              :      that we have two candidates that will provide the actual type.  */
   24578     92844226 :   if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
   24579              :     {
   24580      2390560 :       tree substed = TREE_TYPE (decl);
   24581      2390560 :       unsigned int i;
   24582              : 
   24583      2390560 :       tree sarg
   24584      2390560 :         = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
   24585      2390560 :       if (return_type)
   24586      2390284 :         sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
   24587     10608717 :       for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
   24588      8224904 :         if (!same_type_p (args[i], TREE_VALUE (sarg)))
   24589              :           {
   24590         6747 :             unify_type_mismatch (explain_p, args[i],
   24591         6747 :                                  TREE_VALUE (sarg));
   24592         6747 :             goto fail;
   24593              :           }
   24594      2383813 :       if ((i < nargs || sarg)
   24595              :           /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
   24596              :              doesn't contain the trailing void, and conv fns are always ().  */
   24597      2383813 :           && !DECL_CONV_FN_P (decl))
   24598              :         {
   24599            6 :           unsigned nsargs = i + list_length (sarg);
   24600            6 :           unify_arity (explain_p, nargs, nsargs);
   24601            6 :           goto fail;
   24602              :         }
   24603              :     }
   24604              : 
   24605              :   /* After doing deduction with the inherited constructor, actually return an
   24606              :      instantiation of the inheriting constructor.  */
   24607     92837473 :   if (orig_fn != fn)
   24608        23044 :     decl = instantiate_template (orig_fn, targs, complain);
   24609              : 
   24610              :   r = decl;
   24611              : 
   24612    503585380 :  fail:
   24613    503585380 :   --deduction_depth;
   24614    503585380 :   if (excessive_deduction_depth)
   24615              :     {
   24616            0 :       if (deduction_depth == 0)
   24617              :         /* Reset once we're all the way out.  */
   24618            0 :         excessive_deduction_depth = false;
   24619              :     }
   24620              : 
   24621              :   return r;
   24622              : }
   24623              : 
   24624              : /* Returns true iff PARM is a forwarding reference in the context of
   24625              :    template argument deduction for TMPL.  */
   24626              : 
   24627              : static bool
   24628    500288126 : forwarding_reference_p (tree parm, tree tmpl)
   24629              : {
   24630              :   /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
   24631              :      cv-unqualified template parameter ..."  */
   24632    500288126 :   if (TYPE_REF_P (parm)
   24633    434776624 :       && TYPE_REF_IS_RVALUE (parm)
   24634     17326446 :       && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
   24635    508737160 :       && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
   24636              :     {
   24637      8236566 :       parm = TREE_TYPE (parm);
   24638              :       /* [temp.deduct.call], "... that does not represent a template parameter
   24639              :          of a class template (during class template argument deduction)."  */
   24640      8236566 :       if (tmpl
   24641      8098218 :           && deduction_guide_p (tmpl)
   24642      8261790 :           && DECL_ARTIFICIAL (tmpl))
   24643              :         {
   24644              :           /* Since the template parameters of a synthesized guide consist of
   24645              :              the template parameters of the class template followed by those of
   24646              :              the constructor (if any), we can tell if PARM represents a template
   24647              :              parameter of the class template by comparing its index with the
   24648              :              arity of the class template.  */
   24649        19028 :           tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
   24650        19028 :           if (TEMPLATE_TYPE_IDX (parm)
   24651        19028 :               < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
   24652              :             return false;
   24653              :         }
   24654      8236021 :       return true;
   24655              :     }
   24656              :   return false;
   24657              : }
   24658              : 
   24659              : /* Adjust types before performing type deduction, as described in
   24660              :    [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
   24661              :    sections are symmetric.  PARM is the type of a function parameter
   24662              :    or the return type of the conversion function.  ARG is the type of
   24663              :    the argument passed to the call, or the type of the value
   24664              :    initialized with the result of the conversion function.
   24665              :    ARG_EXPR is the original argument expression, which may be null.  */
   24666              : 
   24667              : static int
   24668    500288126 : maybe_adjust_types_for_deduction (tree tparms,
   24669              :                                   unification_kind_t strict,
   24670              :                                   tree* parm,
   24671              :                                   tree* arg,
   24672              :                                   tree arg_expr)
   24673              : {
   24674    500288126 :   int result = 0;
   24675              : 
   24676    500288126 :   switch (strict)
   24677              :     {
   24678              :     case DEDUCE_CALL:
   24679              :       break;
   24680              : 
   24681       394779 :     case DEDUCE_CONV:
   24682              :       /* [temp.deduct.conv] First remove a reference type on parm.
   24683              :          DRs 322 & 976 affected this.  */
   24684       394779 :       if (TYPE_REF_P (*parm))
   24685           89 :         *parm = TREE_TYPE (*parm);
   24686              : 
   24687              :       /* Swap PARM and ARG throughout the remainder of this
   24688              :          function; the handling is precisely symmetric since PARM
   24689              :          will initialize ARG rather than vice versa.  */
   24690              :       std::swap (parm, arg);
   24691              : 
   24692              :       break;
   24693              : 
   24694     35785063 :     case DEDUCE_EXACT:
   24695              :       /* Core issue #873: Do the DR606 thing (see below) for these cases,
   24696              :          too, but here handle it by stripping the reference from PARM
   24697              :          rather than by adding it to ARG.  */
   24698     35785063 :       if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   24699      1186163 :           && TYPE_REF_P (*arg)
   24700     36857603 :           && !TYPE_REF_IS_RVALUE (*arg))
   24701      1072487 :         *parm = TREE_TYPE (*parm);
   24702              :       /* Nothing else to do in this case.  */
   24703              :       return 0;
   24704              : 
   24705            0 :     default:
   24706            0 :       gcc_unreachable ();
   24707              :     }
   24708              : 
   24709    464503063 :   if (!TYPE_REF_P (*parm))
   24710              :     {
   24711              :       /* [temp.deduct.call]
   24712              : 
   24713              :          If P is not a reference type:
   24714              : 
   24715              :          --If A is an array type, the pointer type produced by the
   24716              :          array-to-pointer standard conversion (_conv.array_) is
   24717              :          used in place of A for type deduction; otherwise,
   24718              : 
   24719              :          --If A is a function type, the pointer type produced by
   24720              :          the function-to-pointer standard conversion
   24721              :          (_conv.func_) is used in place of A for type deduction;
   24722              :          otherwise,
   24723              : 
   24724              :          --If A is a cv-qualified type, the top level
   24725              :          cv-qualifiers of A's type are ignored for type
   24726              :          deduction.  */
   24727     57671686 :       if (TREE_CODE (*arg) == ARRAY_TYPE)
   24728       660425 :         *arg = build_pointer_type (TREE_TYPE (*arg));
   24729     57011261 :       else if (TREE_CODE (*arg) == FUNCTION_TYPE)
   24730         4160 :         *arg = build_pointer_type (*arg);
   24731              :       else
   24732     57007101 :         *arg = TYPE_MAIN_VARIANT (*arg);
   24733              :     }
   24734              : 
   24735              :   /* [temp.deduct.call], "If P is a forwarding reference and the argument is
   24736              :      an lvalue, the type 'lvalue reference to A' is used in place of A for
   24737              :      type deduction."  */
   24738    464503063 :   if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   24739    464503063 :       && (arg_expr ? lvalue_p (arg_expr)
   24740              :           /* try_one_overload doesn't provide an arg_expr, but
   24741              :              functions are always lvalues.  */
   24742           47 :           : TREE_CODE (*arg) == FUNCTION_TYPE))
   24743      4168718 :     *arg = build_reference_type (*arg);
   24744              : 
   24745              :   /* [temp.deduct.call]
   24746              : 
   24747              :      If P is a cv-qualified type, the top level cv-qualifiers
   24748              :      of P's type are ignored for type deduction.  If P is a
   24749              :      reference type, the type referred to by P is used for
   24750              :      type deduction.  */
   24751    464503063 :   *parm = TYPE_MAIN_VARIANT (*parm);
   24752    464503063 :   if (TYPE_REF_P (*parm))
   24753              :     {
   24754    406831377 :       *parm = TREE_TYPE (*parm);
   24755    406831377 :       result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   24756              :     }
   24757              : 
   24758              :   return result;
   24759              : }
   24760              : 
   24761              : /* Return true if computing a conversion from FROM to TO might consider
   24762              :    user-defined conversions, which could lead to arbitrary template
   24763              :    instantiations (e.g. g++.dg/cpp2a/concepts-nondep1.C).  If this predicate
   24764              :    returns false then computing the conversion definitely won't try UDCs.
   24765              : 
   24766              :    Note that this restriction parallels LOOKUP_DEFAULTED for CWG1092, but in
   24767              :    this case we want the early filter to pass instead of fail.  */
   24768              : 
   24769              : static bool
   24770      7763514 : conversion_may_instantiate_p (tree to, tree from)
   24771              : {
   24772      7763514 :   to = non_reference (to);
   24773      7763514 :   from = non_reference (from);
   24774              : 
   24775              :   /* Converting between reference-related types is a standard conversion.  */
   24776      7763514 :   if (reference_related_p (to, from))
   24777              :     return false;
   24778              : 
   24779              :   /* Converting to a non-aggregate class type will consider its
   24780              :      user-declared constructors, which might induce instantiation.  */
   24781      9453178 :   if (CLASS_TYPE_P (complete_type (to))
   24782      6501570 :       && type_has_converting_constructor (to))
   24783              :     return true;
   24784              : 
   24785              :   /* Similarly, converting from a class type will consider its conversion
   24786              :      functions.  */
   24787      7452806 :   if (CLASS_TYPE_P (complete_type (from))
   24788      5151733 :       && TYPE_HAS_CONVERSION (from))
   24789       581181 :     return true;
   24790              : 
   24791              :   /* Otherwise, computing this conversion won't risk arbitrary
   24792              :      template instantiation.  */
   24793              :   return false;
   24794              : }
   24795              : 
   24796              : /* Subroutine of fn_type_unification.  PARM is a function parameter of a
   24797              :    template which doesn't contain any deducible template parameters; check if
   24798              :    ARG is a suitable match for it.  STRICT, FLAGS and EXPLAIN_P are as in
   24799              :    unify_one_argument.  */
   24800              : 
   24801              : static int
   24802     32681551 : check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
   24803              :                                 int flags, struct conversion **conv_p,
   24804              :                                 bool explain_p, bool noninst_only_p)
   24805              : {
   24806     32681551 :   tree type;
   24807              : 
   24808     32681551 :   if (!TYPE_P (arg))
   24809     31616108 :     type = TREE_TYPE (arg);
   24810              :   else
   24811              :     type = arg;
   24812              : 
   24813     32681551 :   if (same_type_p (parm, type))
   24814              :     return unify_success (explain_p);
   24815              : 
   24816     12414445 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   24817     12414445 :   if (strict == DEDUCE_CONV)
   24818              :     {
   24819           27 :       if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
   24820              :         return unify_success (explain_p);
   24821              :     }
   24822     12414418 :   else if (strict == DEDUCE_CALL)
   24823              :     {
   24824     12414418 :       if (conv_p && *conv_p)
   24825              :         {
   24826              :           /* This conversion was already computed earlier (when
   24827              :              computing only non-instantiating conversions).  */
   24828      3478426 :           gcc_checking_assert (!noninst_only_p);
   24829              :           return unify_success (explain_p);
   24830              :         }
   24831              : 
   24832      8935992 :       if (noninst_only_p
   24833      8935992 :           && conversion_may_instantiate_p (parm, type))
   24834              :         return unify_success (explain_p);
   24835              : 
   24836      7354625 :       bool ok = false;
   24837      7354625 :       tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
   24838      7354625 :       if (conv_p)
   24839              :         /* Avoid recalculating this in add_function_candidate.  */
   24840     14708472 :         ok = (*conv_p
   24841      7354236 :               = good_conversion (parm, type, conv_arg, flags, complain));
   24842              :       else
   24843          389 :         ok = can_convert_arg (parm, type, conv_arg, flags, complain);
   24844      7354625 :       if (ok)
   24845              :         return unify_success (explain_p);
   24846              :     }
   24847              : 
   24848      3173356 :   if (strict == DEDUCE_EXACT)
   24849            0 :     return unify_type_mismatch (explain_p, parm, arg);
   24850              :   else
   24851      3173356 :     return unify_arg_conversion (explain_p, parm, type, arg);
   24852              : }
   24853              : 
   24854              : static bool uses_deducible_template_parms (tree type);
   24855              : 
   24856              : /* Returns true iff the expression EXPR is one from which a template
   24857              :    argument can be deduced.  In other words, if it's an undecorated
   24858              :    use of a template non-type parameter.  */
   24859              : 
   24860              : static bool
   24861    932351371 : deducible_expression (tree expr)
   24862              : {
   24863              :   /* Strip implicit conversions and implicit INDIRECT_REFs.  */
   24864   1864708233 :   while (CONVERT_EXPR_P (expr)
   24865    932356823 :          || TREE_CODE (expr) == VIEW_CONVERT_EXPR
   24866    932351682 :          || TREE_CODE (expr) == IMPLICIT_CONV_EXPR
   24867   1864710801 :          || REFERENCE_REF_P (expr))
   24868         7971 :     expr = TREE_OPERAND (expr, 0);
   24869    932351371 :   return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
   24870              : }
   24871              : 
   24872              : /* Returns true iff the array domain DOMAIN uses a template parameter in a
   24873              :    deducible way; that is, if it has a max value of <PARM> - 1.  */
   24874              : 
   24875              : static bool
   24876       155289 : deducible_array_bound (tree domain)
   24877              : {
   24878       155289 :   if (domain == NULL_TREE)
   24879              :     return false;
   24880              : 
   24881       155220 :   tree max = TYPE_MAX_VALUE (domain);
   24882       155220 :   if (TREE_CODE (max) != MINUS_EXPR)
   24883              :     return false;
   24884              : 
   24885       153176 :   return deducible_expression (TREE_OPERAND (max, 0));
   24886              : }
   24887              : 
   24888              : /* Returns true iff the template arguments ARGS use a template parameter
   24889              :    in a deducible way.  */
   24890              : 
   24891              : static bool
   24892    412082631 : deducible_template_args (tree args)
   24893              : {
   24894    431250216 :   for (tree elt : tree_vec_range (args))
   24895              :     {
   24896    419725569 :       bool deducible;
   24897    419725569 :       if (ARGUMENT_PACK_P (elt))
   24898     20330106 :         deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
   24899              :       else
   24900              :         {
   24901    399395463 :           if (PACK_EXPANSION_P (elt))
   24902     19915543 :             elt = PACK_EXPANSION_PATTERN (elt);
   24903    399395463 :           if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
   24904              :             deducible = true;
   24905    399395409 :           else if (TYPE_P (elt))
   24906    397221112 :             deducible = uses_deducible_template_parms (elt);
   24907              :           else
   24908      2174297 :             deducible = deducible_expression (elt);
   24909              :         }
   24910    419725515 :       if (deducible)
   24911    400557984 :         return true;
   24912              :     }
   24913     11524647 :   return false;
   24914              : }
   24915              : 
   24916              : /* Returns true iff TYPE contains any deducible references to template
   24917              :    parameters, as per 14.8.2.5.  */
   24918              : 
   24919              : static bool
   24920    909567413 : uses_deducible_template_parms (tree type)
   24921              : {
   24922   1346462308 :   if (PACK_EXPANSION_P (type))
   24923           90 :     type = PACK_EXPANSION_PATTERN (type);
   24924              : 
   24925              :   /* T
   24926              :      cv-list T
   24927              :      TT<T>
   24928              :      TT<i>
   24929              :      TT<> */
   24930   1346462308 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   24931    882038021 :       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24932              :     return true;
   24933              : 
   24934              :   /* T*
   24935              :      T&
   24936              :      T&&  */
   24937    882037207 :   if (INDIRECT_TYPE_P (type))
   24938    436894895 :     return uses_deducible_template_parms (TREE_TYPE (type));
   24939              : 
   24940              :   /* T[integer-constant ]
   24941              :      type [i]  */
   24942    445142312 :   if (TREE_CODE (type) == ARRAY_TYPE)
   24943      1634121 :     return (uses_deducible_template_parms (TREE_TYPE (type))
   24944      1788899 :             || deducible_array_bound (TYPE_DOMAIN (type)));
   24945              : 
   24946              :   /* T type ::*
   24947              :      type T::*
   24948              :      T T::*
   24949              :      T (type ::*)()
   24950              :      type (T::*)()
   24951              :      type (type ::*)(T)
   24952              :      type (T::*)(T)
   24953              :      T (type ::*)(T)
   24954              :      T (T::*)()
   24955              :      T (T::*)(T) */
   24956    443508191 :   if (TYPE_PTRMEM_P (type))
   24957        23563 :     return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
   24958        23761 :             || (uses_deducible_template_parms
   24959          198 :                 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
   24960              : 
   24961              :   /* template-name <T> (where template-name refers to a class template)
   24962              :      template-name <i> (where template-name refers to a class template) */
   24963    396448227 :   if (CLASS_TYPE_P (type)
   24964    396448215 :       && CLASSTYPE_TEMPLATE_INFO (type)
   24965    835596391 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
   24966    391752525 :     return deducible_template_args (INNERMOST_TEMPLATE_ARGS
   24967    391752525 :                                     (CLASSTYPE_TI_ARGS (type)));
   24968              : 
   24969              :   /* type (T)
   24970              :      T()
   24971              :      T(T)  */
   24972     51732103 :   if (FUNC_OR_METHOD_TYPE_P (type))
   24973              :     {
   24974      8064189 :       if (uses_deducible_template_parms (TREE_TYPE (type)))
   24975              :         return true;
   24976        63721 :       tree parm = TYPE_ARG_TYPES (type);
   24977        63721 :       if (TREE_CODE (type) == METHOD_TYPE)
   24978          139 :         parm = TREE_CHAIN (parm);
   24979        67641 :       for (; parm; parm = TREE_CHAIN (parm))
   24980        65061 :         if (uses_deducible_template_parms (TREE_VALUE (parm)))
   24981              :           return true;
   24982         2580 :       if (flag_noexcept_type
   24983         2565 :           && TYPE_RAISES_EXCEPTIONS (type)
   24984           18 :           && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
   24985         2598 :           && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
   24986              :         return true;
   24987              :     }
   24988              : 
   24989              :   return false;
   24990              : }
   24991              : 
   24992              : /* Subroutine of type_unification_real and unify_pack_expansion to
   24993              :    handle unification of a single P/A pair.  Parameters are as
   24994              :    for those functions.  */
   24995              : 
   24996              : static int
   24997    547598751 : unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
   24998              :                     int subr, unification_kind_t strict,
   24999              :                     bool explain_p)
   25000              : {
   25001    547598751 :   tree arg_expr = NULL_TREE;
   25002    547598751 :   int arg_strict;
   25003              : 
   25004    547598751 :   if (arg == error_mark_node || parm == error_mark_node)
   25005              :     return unify_invalid (explain_p);
   25006    547598739 :   if (arg == unknown_type_node)
   25007              :     /* We can't deduce anything from this, but we might get all the
   25008              :        template args from other function args.  */
   25009              :     return unify_success (explain_p);
   25010              : 
   25011              :   /* Implicit conversions (Clause 4) will be performed on a function
   25012              :      argument to convert it to the type of the corresponding function
   25013              :      parameter if the parameter type contains no template-parameters that
   25014              :      participate in template argument deduction.  */
   25015    547598739 :   if (strict != DEDUCE_EXACT
   25016    547598739 :       && TYPE_P (parm) && !uses_deducible_template_parms (parm))
   25017              :     /* For function parameters with no deducible template parameters,
   25018              :        just return.  We'll check non-dependent conversions later.  */
   25019              :     return unify_success (explain_p);
   25020              : 
   25021    509868553 :   switch (strict)
   25022              :     {
   25023              :     case DEDUCE_CALL:
   25024              :       arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
   25025              :                     | UNIFY_ALLOW_MORE_CV_QUAL
   25026              :                     | UNIFY_ALLOW_DERIVED);
   25027              :       break;
   25028              : 
   25029              :     case DEDUCE_CONV:
   25030              :       arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
   25031              :       break;
   25032              : 
   25033              :     case DEDUCE_EXACT:
   25034              :       arg_strict = UNIFY_ALLOW_NONE;
   25035              :       break;
   25036              : 
   25037            0 :     default:
   25038            0 :       gcc_unreachable ();
   25039              :     }
   25040              : 
   25041              :   /* We only do these transformations if this is the top-level
   25042              :      parameter_type_list in a call or declaration matching; in other
   25043              :      situations (nested function declarators, template argument lists) we
   25044              :      won't be comparing a type to an expression, and we don't do any type
   25045              :      adjustments.  */
   25046    509868553 :   if (!subr)
   25047              :     {
   25048    500611061 :       if (!TYPE_P (arg))
   25049              :         {
   25050    464431219 :           gcc_assert (TREE_TYPE (arg) != NULL_TREE);
   25051    464431219 :           if (type_unknown_p (arg))
   25052              :             {
   25053              :               /* [temp.deduct.type] A template-argument can be
   25054              :                  deduced from a pointer to function or pointer
   25055              :                  to member function argument if the set of
   25056              :                  overloaded functions does not contain function
   25057              :                  templates and at most one of a set of
   25058              :                  overloaded functions provides a unique
   25059              :                  match.  */
   25060       331089 :               resolve_overloaded_unification (tparms, targs, parm,
   25061              :                                               arg, strict,
   25062              :                                               arg_strict, explain_p);
   25063              :               /* If a unique match was not found, this is a
   25064              :                  non-deduced context, so we still succeed. */
   25065       331089 :               return unify_success (explain_p);
   25066              :             }
   25067              : 
   25068    464100130 :           arg_expr = arg;
   25069    464100130 :           arg = unlowered_expr_type (arg);
   25070    464100130 :           if (arg == error_mark_node)
   25071              :             return unify_invalid (explain_p);
   25072              :         }
   25073              : 
   25074    500279972 :       arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   25075              :                                                       &parm, &arg, arg_expr);
   25076              :     }
   25077              :   else
   25078     18514984 :     if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
   25079      9257492 :         != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
   25080           69 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   25081              : 
   25082              :   /* For deduction from an init-list we need the actual list.  */
   25083    500279972 :   if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
   25084        10789 :     arg = arg_expr;
   25085    509537395 :   return unify (tparms, targs, parm, arg, arg_strict, explain_p);
   25086              : }
   25087              : 
   25088              : /* for_each_template_parm callback that always returns 0.  */
   25089              : 
   25090              : static int
   25091     30845818 : zero_r (tree, void *)
   25092              : {
   25093     30845818 :   return 0;
   25094              : }
   25095              : 
   25096              : /* for_each_template_parm any_fn callback to handle deduction of a template
   25097              :    type argument from the type of an array bound.  */
   25098              : 
   25099              : static int
   25100    182321945 : array_deduction_r (tree t, void *data)
   25101              : {
   25102    182321945 :   tree_pair_p d = (tree_pair_p)data;
   25103    182321945 :   tree &tparms = d->purpose;
   25104    182321945 :   tree &targs = d->value;
   25105              : 
   25106    182321945 :   if (TREE_CODE (t) == ARRAY_TYPE)
   25107          292 :     if (tree dom = TYPE_DOMAIN (t))
   25108          180 :       if (tree max = TYPE_MAX_VALUE (dom))
   25109              :         {
   25110          180 :           if (TREE_CODE (max) == MINUS_EXPR)
   25111           14 :             max = TREE_OPERAND (max, 0);
   25112          180 :           if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
   25113           14 :             unify (tparms, targs, TREE_TYPE (max), size_type_node,
   25114              :                    UNIFY_ALLOW_NONE, /*explain*/false);
   25115              :         }
   25116              : 
   25117              :   /* Keep walking.  */
   25118    182321945 :   return 0;
   25119              : }
   25120              : 
   25121              : /* Try to deduce any not-yet-deduced template type arguments from the type of
   25122              :    an array bound.  This is handled separately from unify because 14.8.2.5 says
   25123              :    "The type of a type parameter is only deduced from an array bound if it is
   25124              :    not otherwise deduced."  */
   25125              : 
   25126              : static void
   25127     29948433 : try_array_deduction (tree tparms, tree targs, tree parm)
   25128              : {
   25129     29948433 :   tree_pair_s data = { tparms, targs };
   25130     29948433 :   hash_set<tree> visited;
   25131     29948433 :   for_each_template_parm (parm, zero_r, &data, &visited,
   25132              :                           /*nondeduced*/false, array_deduction_r);
   25133     29948433 : }
   25134              : 
   25135              : /* Most parms like fn_type_unification.
   25136              : 
   25137              :    If SUBR is 1, we're being called recursively (to unify the
   25138              :    arguments of a function or method parameter of a function
   25139              :    template).
   25140              : 
   25141              :    CHECKS is a pointer to a vector of access checks encountered while
   25142              :    substituting default template arguments.  */
   25143              : 
   25144              : static int
   25145    468641939 : type_unification_real (tree tparms,
   25146              :                        tree full_targs,
   25147              :                        tree xparms,
   25148              :                        const tree *xargs,
   25149              :                        unsigned int xnargs,
   25150              :                        int subr,
   25151              :                        unification_kind_t strict,
   25152              :                        vec<deferred_access_check, va_gc> **checks,
   25153              :                        bool explain_p)
   25154              : {
   25155    468641939 :   tree parm, arg;
   25156    468641939 :   int i;
   25157    468641939 :   int ntparms = TREE_VEC_LENGTH (tparms);
   25158    468641939 :   int saw_undeduced = 0;
   25159    468641939 :   tree parms;
   25160    468641939 :   const tree *args;
   25161    468641939 :   unsigned int nargs;
   25162    468641939 :   unsigned int ia;
   25163              : 
   25164    468641939 :   gcc_assert (TREE_CODE (tparms) == TREE_VEC);
   25165    468641939 :   gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
   25166    468641939 :   gcc_assert (ntparms > 0);
   25167              : 
   25168    468641939 :   tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
   25169              : 
   25170              :   /* Reset the number of non-defaulted template arguments contained
   25171              :      in TARGS.  */
   25172    468641939 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
   25173              : 
   25174    468656459 :  again:
   25175    468656459 :   parms = xparms;
   25176    468656459 :   args = xargs;
   25177    468656459 :   nargs = xnargs;
   25178              : 
   25179              :   /* Only fn_type_unification cares about terminal void.  */
   25180    468656459 :   if (nargs && args[nargs-1] == void_type_node)
   25181     14352564 :     --nargs;
   25182              : 
   25183    468656459 :   ia = 0;
   25184   1085225743 :   while (parms && parms != void_list_node
   25185   1167260807 :          && ia < nargs)
   25186              :     {
   25187    538757214 :       parm = TREE_VALUE (parms);
   25188              : 
   25189    538757214 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   25190    538757214 :           && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
   25191              :         /* For a function parameter pack that occurs at the end of the
   25192              :            parameter-declaration-list, the type A of each remaining
   25193              :            argument of the call is compared with the type P of the
   25194              :            declarator-id of the function parameter pack.  */
   25195              :         break;
   25196              : 
   25197    537767023 :       parms = TREE_CHAIN (parms);
   25198              : 
   25199    537767023 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   25200              :         /* For a function parameter pack that does not occur at the
   25201              :            end of the parameter-declaration-list, the type of the
   25202              :            parameter pack is a non-deduced context.  */
   25203           54 :         continue;
   25204              : 
   25205              :       /* [temp.deduct.conv] only applies to the deduction of the return
   25206              :          type, which is always the first argument here.  Other arguments
   25207              :          (notably, explicit object parameters) should undergo normal
   25208              :          call-like unification.  */
   25209    537766969 :       unification_kind_t kind = strict;
   25210    537766969 :       if (strict == DEDUCE_CONV && ia > 0)
   25211           36 :         kind = DEDUCE_CALL;
   25212              : 
   25213    537766969 :       arg = args[ia];
   25214    537766969 :       ++ia;
   25215              : 
   25216    537766969 :       if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind,
   25217              :                               explain_p))
   25218              :         return 1;
   25219              :     }
   25220              : 
   25221     87347628 :   if (parms
   25222     78802261 :       && parms != void_list_node
   25223     91726761 :       && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
   25224              :     {
   25225      1611599 :       gcc_assert (strict != DEDUCE_CONV);
   25226              : 
   25227              :       /* Unify the remaining arguments with the pack expansion type.  */
   25228      1611599 :       tree argvec;
   25229      1611599 :       tree parmvec = make_tree_vec (1);
   25230              : 
   25231              :       /* Allocate a TREE_VEC and copy in all of the arguments */
   25232      1611599 :       argvec = make_tree_vec (nargs - ia);
   25233      4505242 :       for (i = 0; ia < nargs; ++ia, ++i)
   25234      1282044 :         TREE_VEC_ELT (argvec, i) = args[ia];
   25235              : 
   25236              :       /* Copy the parameter into parmvec.  */
   25237      1611599 :       TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
   25238      1611599 :       if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
   25239              :                                 /*subr=*/subr, explain_p))
   25240              :         return 1;
   25241              : 
   25242              :       /* Advance to the end of the list of parameters.  */
   25243      1611450 :       parms = TREE_CHAIN (parms);
   25244              :     }
   25245              : 
   25246              :   /* Fail if we've reached the end of the parm list, and more args
   25247              :      are present, and the parm list isn't variadic.  */
   25248     87347479 :   if (ia < nargs && parms == void_list_node)
   25249        52655 :     return unify_too_many_arguments (explain_p, nargs, ia);
   25250              :   /* Fail if parms are left and they don't have default values and
   25251              :      they aren't all deduced as empty packs (c++/57397).  This is
   25252              :      consistent with sufficient_parms_p.  */
   25253     78749339 :   if (parms && parms != void_list_node
   25254     90062367 :       && TREE_PURPOSE (parms) == NULL_TREE)
   25255              :     {
   25256              :       unsigned int count = nargs;
   25257              :       tree p = parms;
   25258        26762 :       bool type_pack_p;
   25259        26762 :       do
   25260              :         {
   25261        26762 :           type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
   25262        26762 :           if (!type_pack_p)
   25263        26752 :             count++;
   25264        26762 :           p = TREE_CHAIN (p);
   25265              :         }
   25266        26762 :       while (p && p != void_list_node);
   25267        26569 :       if (count != nargs)
   25268        26563 :         return unify_too_few_arguments (explain_p, ia, count,
   25269        26563 :                                         type_pack_p);
   25270              :     }
   25271              : 
   25272     87268261 :   if (!subr)
   25273              :     {
   25274     86976554 :       tsubst_flags_t complain = (explain_p
   25275     86977879 :                                  ? tf_warning_or_error
   25276              :                                  : tf_none);
   25277     86977879 :       bool tried_array_deduction = (cxx_dialect < cxx17);
   25278              : 
   25279    220602751 :       for (i = 0; i < ntparms; i++)
   25280              :         {
   25281    134003772 :           tree targ = TREE_VEC_ELT (targs, i);
   25282    134003772 :           tree tparm = TREE_VEC_ELT (tparms, i);
   25283              : 
   25284              :           /* Clear the "incomplete" flags on all argument packs now so that
   25285              :              substituting them into later default arguments works.  */
   25286    134003772 :           if (targ && ARGUMENT_PACK_P (targ))
   25287              :             {
   25288      9016064 :               ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
   25289      9016064 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
   25290              :             }
   25291              : 
   25292     41793337 :           if (targ || tparm == error_mark_node)
   25293    101226499 :             continue;
   25294     32777273 :           tparm = TREE_VALUE (tparm);
   25295              : 
   25296     32777273 :           if (TREE_CODE (tparm) == TYPE_DECL
   25297     30410406 :               && !tried_array_deduction)
   25298              :             {
   25299     29948374 :               try_array_deduction (tparms, targs, xparms);
   25300     29948374 :               tried_array_deduction = true;
   25301     29948374 :               if (TREE_VEC_ELT (targs, i))
   25302            3 :                 continue;
   25303              :             }
   25304              : 
   25305              :           /* If this is an undeduced nontype parameter that depends on
   25306              :              a type parameter, try another pass; its type may have been
   25307              :              deduced from a later argument than the one from which
   25308              :              this parameter can be deduced.  */
   25309     32817139 :           if (TREE_CODE (tparm) == PARM_DECL
   25310      2366831 :               && !is_auto (TREE_TYPE (tparm))
   25311      2366561 :               && uses_template_parms (TREE_TYPE (tparm))
   25312     32841980 :               && saw_undeduced < 2)
   25313              :             {
   25314        39869 :               saw_undeduced = 1;
   25315        39869 :               continue;
   25316              :             }
   25317              : 
   25318              :           /* Core issue #226 (C++0x) [temp.deduct]:
   25319              : 
   25320              :              If a template argument has not been deduced, its
   25321              :              default template argument, if any, is used.
   25322              : 
   25323              :              When we are in C++98 mode, TREE_PURPOSE will either
   25324              :              be NULL_TREE or ERROR_MARK_NODE, so we do not need
   25325              :              to explicitly check cxx_dialect here.  */
   25326     32737401 :           if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
   25327              :             /* OK, there is a default argument.  Wait until after the
   25328              :                conversion check to do substitution.  */
   25329     32211292 :             continue;
   25330              : 
   25331              :           /* If the type parameter is a parameter pack, then it will
   25332              :              be deduced to an empty parameter pack.  */
   25333       526109 :           if (template_parameter_pack_p (tparm))
   25334              :             {
   25335       147209 :               tree arg;
   25336              : 
   25337       147209 :               if (TREE_CODE (tparm) == PARM_DECL)
   25338              :                 {
   25339         2755 :                   arg = make_node (NONTYPE_ARGUMENT_PACK);
   25340         2755 :                   TREE_CONSTANT (arg) = 1;
   25341              :                 }
   25342              :               else
   25343       144454 :                 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   25344              : 
   25345       147209 :               ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   25346              : 
   25347       147209 :               TREE_VEC_ELT (targs, i) = arg;
   25348       147209 :               continue;
   25349       147209 :             }
   25350              : 
   25351       378900 :           return unify_parameter_deduction_failure (explain_p, tparm);
   25352              :         }
   25353              : 
   25354              :       /* During partial ordering, we deduce dependent template args.  */
   25355              :       bool any_dependent_targs = false;
   25356              : 
   25357              :       /* Now substitute into the default template arguments.  */
   25358    201640604 :       for (i = 0; i < ntparms; i++)
   25359              :         {
   25360    133125676 :           tree targ = TREE_VEC_ELT (targs, i);
   25361    133125676 :           tree tparm = TREE_VEC_ELT (tparms, i);
   25362              : 
   25363    133125676 :           if (targ)
   25364              :             {
   25365    101191087 :               if (!any_dependent_targs && dependent_template_arg_p (targ))
   25366              :                 any_dependent_targs = true;
   25367    101191087 :               continue;
   25368              :             }
   25369     31934589 :           if (tparm == error_mark_node)
   25370            0 :             continue;
   25371              : 
   25372     31934589 :           tree parm = TREE_VALUE (tparm);
   25373     31934589 :           tree arg = TREE_PURPOSE (tparm);
   25374     31934589 :           reopen_deferring_access_checks (*checks);
   25375     31934589 :           location_t save_loc = input_location;
   25376     31934589 :           if (DECL_P (parm))
   25377     31934585 :             input_location = DECL_SOURCE_LOCATION (parm);
   25378              : 
   25379     31934589 :           if (saw_undeduced == 1
   25380        44964 :               && TREE_CODE (parm) == PARM_DECL
   25381        40475 :               && !is_auto (TREE_TYPE (parm))
   25382     31975064 :               && uses_template_parms (TREE_TYPE (parm)))
   25383              :             {
   25384              :               /* The type of this non-type parameter depends on undeduced
   25385              :                  parameters.  Don't try to use its default argument yet,
   25386              :                  since we might deduce an argument for it on the next pass,
   25387              :                  but do check whether the arguments we already have cause
   25388              :                  substitution failure, so that that happens before we try
   25389              :                  later default arguments (78489).  */
   25390        39804 :               ++processing_template_decl;
   25391        39804 :               tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
   25392              :                                   NULL_TREE);
   25393        39804 :               --processing_template_decl;
   25394        39804 :               if (type == error_mark_node)
   25395        14937 :                 arg = error_mark_node;
   25396              :               else
   25397              :                 arg = NULL_TREE;
   25398              :             }
   25399              :           else
   25400              :             {
   25401              :               /* Even if the call is happening in template context, getting
   25402              :                  here means it's non-dependent, and a default argument is
   25403              :                  considered a separate definition under [temp.decls], so we can
   25404              :                  do this substitution without processing_template_decl.  This
   25405              :                  is important if the default argument contains something that
   25406              :                  might be instantiation-dependent like access (87480).  */
   25407     31894785 :               processing_template_decl_sentinel s (!any_dependent_targs);
   25408              : 
   25409     31894785 :               tree used_tparms = NULL_TREE;
   25410     31894785 :               if (saw_undeduced == 1)
   25411              :                 {
   25412         5160 :                   tree tparms_list = build_tree_list (size_int (1), tparms);
   25413         5160 :                   used_tparms = find_template_parameters (arg, tparms_list);
   25414        13168 :                   for (; used_tparms; used_tparms = TREE_CHAIN (used_tparms))
   25415              :                     {
   25416         2854 :                       int level, index;
   25417         2854 :                       template_parm_level_and_index (TREE_VALUE (used_tparms),
   25418              :                                                      &level, &index);
   25419         2854 :                       if (TREE_VEC_ELT (targs, index) == NULL_TREE)
   25420              :                         break;
   25421              :                     }
   25422              :                 }
   25423              : 
   25424         5160 :               if (!used_tparms)
   25425              :                 {
   25426              :                   /* All template parameters within this default argument are
   25427              :                      deduced, so we can use it.  */
   25428     31894779 :                   arg = tsubst_template_arg (arg, full_targs, complain,
   25429              :                                              NULL_TREE);
   25430     31894779 :                   arg = convert_template_argument (parm, arg, full_targs,
   25431              :                                                    complain, i, NULL_TREE);
   25432              :                 }
   25433              :               else if (saw_undeduced == 1)
   25434              :                 arg = NULL_TREE;
   25435              :               else if (!any_dependent_targs)
   25436              :                 arg = error_mark_node;
   25437     31894785 :             }
   25438              : 
   25439     31934589 :           input_location = save_loc;
   25440     31934589 :           *checks = get_deferred_access_checks ();
   25441     31934589 :           pop_deferring_access_checks ();
   25442              : 
   25443     31934589 :           if (arg == error_mark_node)
   25444              :             return 1;
   25445     13850538 :           else if (arg)
   25446              :             {
   25447     13825665 :               TREE_VEC_ELT (targs, i) = arg;
   25448              :               /* The position of the first default template argument,
   25449              :                  is also the number of non-defaulted arguments in TARGS.
   25450              :                  Record that.  */
   25451     13825665 :               if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   25452     11393368 :                 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
   25453              :             }
   25454              :         }
   25455              : 
   25456     68514928 :       if (saw_undeduced++ == 1)
   25457        14520 :         goto again;
   25458              :     }
   25459              : 
   25460     68790790 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   25461     57157292 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
   25462              : 
   25463              :   return unify_success (explain_p);
   25464              : }
   25465              : 
   25466              : /* Subroutine of type_unification_real.  Args are like the variables
   25467              :    at the call site.  ARG is an overloaded function (or template-id);
   25468              :    we try deducing template args from each of the overloads, and if
   25469              :    only one succeeds, we go with that.  Modifies TARGS and returns
   25470              :    true on success.  */
   25471              : 
   25472              : static bool
   25473       331089 : resolve_overloaded_unification (tree tparms,
   25474              :                                 tree targs,
   25475              :                                 tree parm,
   25476              :                                 tree arg,
   25477              :                                 unification_kind_t strict,
   25478              :                                 int sub_strict,
   25479              :                                 bool explain_p)
   25480              : {
   25481       331089 :   tree tempargs = copy_node (targs);
   25482       331089 :   int good = 0;
   25483       331089 :   tree goodfn = NULL_TREE;
   25484       331089 :   bool addr_p;
   25485              : 
   25486       331089 :   if (TREE_CODE (arg) == ADDR_EXPR)
   25487              :     {
   25488          710 :       arg = TREE_OPERAND (arg, 0);
   25489          710 :       addr_p = true;
   25490              :     }
   25491              :   else
   25492              :     addr_p = false;
   25493              : 
   25494       331089 :   if (TREE_CODE (arg) == COMPONENT_REF)
   25495              :     /* Handle `&x' where `x' is some static or non-static member
   25496              :        function name.  */
   25497          159 :     arg = TREE_OPERAND (arg, 1);
   25498              : 
   25499       331089 :   if (TREE_CODE (arg) == OFFSET_REF)
   25500          478 :     arg = TREE_OPERAND (arg, 1);
   25501              : 
   25502              :   /* Strip baselink information.  */
   25503       331089 :   if (BASELINK_P (arg))
   25504          643 :     arg = BASELINK_FUNCTIONS (arg);
   25505              : 
   25506       331089 :   if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
   25507              :     {
   25508              :       /* If we got some explicit template args, we need to plug them into
   25509              :          the affected templates before we try to unify, in case the
   25510              :          explicit args will completely resolve the templates in question.  */
   25511              : 
   25512          486 :       int ok = 0;
   25513          486 :       tree expl_subargs = TREE_OPERAND (arg, 1);
   25514          486 :       arg = TREE_OPERAND (arg, 0);
   25515              : 
   25516         1029 :       for (lkp_iterator iter (arg); iter; ++iter)
   25517              :         {
   25518          543 :           tree fn = *iter;
   25519          543 :           tree subargs, elem;
   25520              : 
   25521          543 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   25522            0 :             continue;
   25523              : 
   25524          543 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   25525              :                                            expl_subargs, NULL_TREE, tf_none);
   25526          543 :           if (subargs != error_mark_node
   25527          543 :               && !any_dependent_template_arguments_p (subargs))
   25528              :             {
   25529          519 :               fn = instantiate_template (fn, subargs, tf_none);
   25530          519 :               if (!constraints_satisfied_p (fn))
   25531           41 :                 continue;
   25532          478 :               if (undeduced_auto_decl (fn))
   25533              :                 {
   25534              :                   /* Instantiate the function to deduce its return type.  */
   25535           12 :                   ++function_depth;
   25536           12 :                   instantiate_decl (fn, /*defer*/false, /*class*/false);
   25537           12 :                   --function_depth;
   25538              :                 }
   25539              : 
   25540          478 :               if (flag_noexcept_type)
   25541          434 :                 maybe_instantiate_noexcept (fn, tf_none);
   25542              : 
   25543          478 :               elem = TREE_TYPE (fn);
   25544          478 :               if (try_one_overload (tparms, targs, tempargs, parm,
   25545              :                                     elem, strict, sub_strict, addr_p, explain_p)
   25546          478 :                   && (!goodfn || !same_type_p (goodfn, elem)))
   25547              :                 {
   25548          451 :                   goodfn = elem;
   25549          451 :                   ++good;
   25550              :                 }
   25551              :             }
   25552           24 :           else if (subargs)
   25553           24 :             ++ok;
   25554              :         }
   25555              :       /* If no templates (or more than one) are fully resolved by the
   25556              :          explicit arguments, this template-id is a non-deduced context; it
   25557              :          could still be OK if we deduce all template arguments for the
   25558              :          enclosing call through other arguments.  */
   25559          486 :       if (good != 1)
   25560              :         good = ok;
   25561              :     }
   25562       330603 :   else if (!OVL_P (arg))
   25563              :     /* If ARG is, for example, "(0, &f)" then its type will be unknown
   25564              :        -- but the deduction does not succeed because the expression is
   25565              :        not just the function on its own.  */
   25566              :     return false;
   25567              :   else
   25568       661628 :     for (lkp_iterator iter (arg); iter; ++iter)
   25569              :       {
   25570       331025 :         tree fn = *iter;
   25571       331025 :         if (flag_noexcept_type)
   25572       330122 :           maybe_instantiate_noexcept (fn, tf_none);
   25573       331025 :         if (TREE_CODE (fn) == FUNCTION_DECL && !constraints_satisfied_p (fn))
   25574            6 :           continue;
   25575       331019 :         tree elem = TREE_TYPE (fn);
   25576       331019 :         if (try_one_overload (tparms, targs, tempargs, parm, elem,
   25577              :                               strict, sub_strict, addr_p, explain_p)
   25578       331019 :             && (!goodfn || !same_type_p (goodfn, elem)))
   25579              :           {
   25580       330775 :             goodfn = elem;
   25581       330775 :             ++good;
   25582              :           }
   25583              :       }
   25584              : 
   25585              :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   25586              :      to function or pointer to member function argument if the set of
   25587              :      overloaded functions does not contain function templates and at most
   25588              :      one of a set of overloaded functions provides a unique match.
   25589              : 
   25590              :      CWG2918 allows multiple functions to match if they all have the same type,
   25591              :      so that we can choose the most constrained later.
   25592              : 
   25593              :      So if we found multiple possibilities, we return success but don't
   25594              :      deduce anything.  */
   25595              : 
   25596       330674 :   if (good == 1)
   25597              :     {
   25598       330822 :       int i = TREE_VEC_LENGTH (targs);
   25599      1357525 :       for (; i--; )
   25600      1026703 :         if (TREE_VEC_ELT (tempargs, i))
   25601              :           {
   25602       559299 :             tree old = TREE_VEC_ELT (targs, i);
   25603       559299 :             tree new_ = TREE_VEC_ELT (tempargs, i);
   25604       558590 :             if (new_ && old && ARGUMENT_PACK_P (old)
   25605       559308 :                 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
   25606              :               /* Don't forget explicit template arguments in a pack.  */
   25607            6 :               ARGUMENT_PACK_EXPLICIT_ARGS (new_)
   25608           12 :                 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
   25609       559299 :             TREE_VEC_ELT (targs, i) = new_;
   25610              :           }
   25611              :     }
   25612       331089 :   if (good)
   25613       331012 :     return true;
   25614              : 
   25615              :   return false;
   25616              : }
   25617              : 
   25618              : /* Core DR 115: In contexts where deduction is done and fails, or in
   25619              :    contexts where deduction is not done, if a template argument list is
   25620              :    specified and it, along with any default template arguments, identifies
   25621              :    a single function template specialization, then the template-id is an
   25622              :    lvalue for the function template specialization.  */
   25623              : 
   25624              : tree
   25625   1721745334 : resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
   25626              : {
   25627   1721745334 :   tree expr, offset, baselink;
   25628   1721745334 :   bool addr;
   25629              : 
   25630   1721745334 :   if (!type_unknown_p (orig_expr))
   25631              :     return orig_expr;
   25632              : 
   25633        15185 :   expr = orig_expr;
   25634        15185 :   addr = false;
   25635        15185 :   offset = NULL_TREE;
   25636        15185 :   baselink = NULL_TREE;
   25637              : 
   25638        15185 :   if (TREE_CODE (expr) == ADDR_EXPR)
   25639              :     {
   25640          475 :       expr = TREE_OPERAND (expr, 0);
   25641          475 :       addr = true;
   25642              :     }
   25643        15185 :   if (TREE_CODE (expr) == OFFSET_REF)
   25644              :     {
   25645          108 :       offset = expr;
   25646          108 :       expr = TREE_OPERAND (expr, 1);
   25647              :     }
   25648        15185 :   if (BASELINK_P (expr))
   25649              :     {
   25650         1135 :       baselink = expr;
   25651         1135 :       expr = BASELINK_FUNCTIONS (expr);
   25652              :     }
   25653              : 
   25654        15185 :   if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
   25655              :     {
   25656         1962 :       int good = 0;
   25657         1962 :       tree goodfn = NULL_TREE;
   25658              : 
   25659              :       /* If we got some explicit template args, we need to plug them into
   25660              :          the affected templates before we try to unify, in case the
   25661              :          explicit args will completely resolve the templates in question.  */
   25662              : 
   25663         1962 :       tree expl_subargs = TREE_OPERAND (expr, 1);
   25664         1962 :       tree arg = TREE_OPERAND (expr, 0);
   25665         1962 :       tree badfn = NULL_TREE;
   25666         1962 :       tree badargs = NULL_TREE;
   25667              : 
   25668         4089 :       for (lkp_iterator iter (arg); iter; ++iter)
   25669              :         {
   25670         2127 :           tree fn = *iter;
   25671         2127 :           tree subargs, elem;
   25672              : 
   25673         2127 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   25674            8 :             continue;
   25675              : 
   25676         2119 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   25677              :                                            expl_subargs, NULL_TREE, tf_none);
   25678         2119 :           if (subargs != error_mark_node
   25679         2119 :               && !any_dependent_template_arguments_p (subargs))
   25680              :             {
   25681         1959 :               elem = instantiate_template (fn, subargs, tf_none);
   25682         1959 :               if (elem == error_mark_node)
   25683              :                 {
   25684              :                   badfn = fn;
   25685              :                   badargs = subargs;
   25686              :                 }
   25687         1902 :               else if (elem && (!goodfn || !decls_match (goodfn, elem))
   25688         3804 :                        && constraints_satisfied_p (elem))
   25689              :                 {
   25690         1854 :                   goodfn = elem;
   25691         1854 :                   ++good;
   25692              :                 }
   25693              :             }
   25694              :         }
   25695         1962 :       if (good == 1)
   25696              :         {
   25697         1764 :           if (!mark_used (goodfn, complain) && !(complain & tf_error))
   25698           15 :             return error_mark_node;
   25699         1749 :           expr = goodfn;
   25700         1749 :           if (baselink)
   25701          674 :             expr = build_baselink (BASELINK_BINFO (baselink),
   25702          674 :                                    BASELINK_ACCESS_BINFO (baselink),
   25703          674 :                                    expr, BASELINK_OPTYPE (baselink));
   25704         1749 :           if (offset)
   25705              :             {
   25706           57 :               tree base
   25707           57 :                 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
   25708           57 :               expr = build_offset_ref (base, expr, addr, complain);
   25709              :             }
   25710         1749 :           if (addr)
   25711          343 :             expr = cp_build_addr_expr (expr, complain);
   25712              :           return expr;
   25713              :         }
   25714          198 :       else if (good == 0 && badargs && (complain & tf_error))
   25715              :         /* There were no good options and at least one bad one, so let the
   25716              :            user know what the problem is.  */
   25717            3 :         instantiate_template (badfn, badargs, complain);
   25718              :     }
   25719              :   return orig_expr;
   25720              : }
   25721              : 
   25722              : /* As above, but error out if the expression remains overloaded.  */
   25723              : 
   25724              : tree
   25725   1241785660 : resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
   25726              : {
   25727   1241785660 :   exp = resolve_nondeduced_context (exp, complain);
   25728   1241785660 :   if (type_unknown_p (exp))
   25729              :     {
   25730          126 :       if (complain & tf_error)
   25731           96 :         cxx_incomplete_type_error (exp, TREE_TYPE (exp));
   25732          126 :       return error_mark_node;
   25733              :     }
   25734              :   return exp;
   25735              : }
   25736              : 
   25737              : /* Subroutine of resolve_overloaded_unification; does deduction for a single
   25738              :    overload.  Fills TARGS with any deduced arguments, or error_mark_node if
   25739              :    different overloads deduce different arguments for a given parm.
   25740              :    ADDR_P is true if the expression for which deduction is being
   25741              :    performed was of the form "& fn" rather than simply "fn".
   25742              : 
   25743              :    Returns 1 on success.  */
   25744              : 
   25745              : static int
   25746       331497 : try_one_overload (tree tparms,
   25747              :                   tree orig_targs,
   25748              :                   tree targs,
   25749              :                   tree parm,
   25750              :                   tree arg,
   25751              :                   unification_kind_t strict,
   25752              :                   int sub_strict,
   25753              :                   bool addr_p,
   25754              :                   bool explain_p)
   25755              : {
   25756       331497 :   int nargs;
   25757       331497 :   tree tempargs;
   25758       331497 :   int i;
   25759              : 
   25760       331497 :   if (arg == error_mark_node)
   25761              :     return 0;
   25762              : 
   25763              :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   25764              :      to function or pointer to member function argument if the set of
   25765              :      overloaded functions does not contain function templates and at most
   25766              :      one of a set of overloaded functions provides a unique match.
   25767              : 
   25768              :      So if this is a template, just return success.  */
   25769              : 
   25770       331496 :   if (uses_template_parms (arg))
   25771              :     return 1;
   25772              : 
   25773         1278 :   if (TREE_CODE (arg) == METHOD_TYPE)
   25774          384 :     arg = build_ptrmemfunc_type (build_pointer_type (arg));
   25775          894 :   else if (addr_p)
   25776          663 :     arg = build_pointer_type (arg);
   25777              : 
   25778         1278 :   sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   25779              :                                                   &parm, &arg, NULL_TREE);
   25780              : 
   25781              :   /* We don't copy orig_targs for this because if we have already deduced
   25782              :      some template args from previous args, unify would complain when we
   25783              :      try to deduce a template parameter for the same argument, even though
   25784              :      there isn't really a conflict.  */
   25785         1278 :   nargs = TREE_VEC_LENGTH (targs);
   25786         1278 :   tempargs = make_tree_vec (nargs);
   25787              : 
   25788         1278 :   if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
   25789              :     return 0;
   25790              : 
   25791              :   /* First make sure we didn't deduce anything that conflicts with
   25792              :      explicitly specified args.  */
   25793         2886 :   for (i = nargs; i--; )
   25794              :     {
   25795         1860 :       tree elt = TREE_VEC_ELT (tempargs, i);
   25796         1860 :       tree oldelt = TREE_VEC_ELT (orig_targs, i);
   25797              : 
   25798         1860 :       if (!elt)
   25799              :         /*NOP*/;
   25800         1190 :       else if (uses_template_parms (elt))
   25801              :         /* Since we're unifying against ourselves, we will fill in
   25802              :            template args used in the function parm list with our own
   25803              :            template parms.  Discard them.  */
   25804            0 :         TREE_VEC_ELT (tempargs, i) = NULL_TREE;
   25805         1190 :       else if (oldelt && ARGUMENT_PACK_P (oldelt))
   25806              :         {
   25807              :           /* Check that the argument at each index of the deduced argument pack
   25808              :              is equivalent to the corresponding explicitly specified argument.
   25809              :              We may have deduced more arguments than were explicitly specified,
   25810              :              and that's OK.  */
   25811              : 
   25812              :           /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
   25813              :              that's wrong if we deduce the same argument pack from multiple
   25814              :              function arguments: it's only incomplete the first time.  */
   25815              : 
   25816           24 :           tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
   25817           24 :           tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
   25818              : 
   25819           24 :           if (TREE_VEC_LENGTH (deduced_pack)
   25820           24 :               < TREE_VEC_LENGTH (explicit_pack))
   25821              :             return 0;
   25822              : 
   25823           30 :           for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
   25824           21 :             if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
   25825           21 :                                       TREE_VEC_ELT (deduced_pack, j)))
   25826              :               return 0;
   25827              :         }
   25828           23 :       else if (oldelt && !template_args_equal (oldelt, elt))
   25829              :         return 0;
   25830              :     }
   25831              : 
   25832         2833 :   for (i = nargs; i--; )
   25833              :     {
   25834         1807 :       tree elt = TREE_VEC_ELT (tempargs, i);
   25835              : 
   25836         1807 :       if (elt)
   25837         1155 :         TREE_VEC_ELT (targs, i) = elt;
   25838              :     }
   25839              : 
   25840              :   return 1;
   25841              : }
   25842              : 
   25843              : /* PARM is a template class (perhaps with unbound template
   25844              :    parameters).  ARG is a fully instantiated type.  If ARG can be
   25845              :    bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
   25846              :    TARGS are as for unify.  */
   25847              : 
   25848              : static tree
   25849    203692988 : try_class_unification (tree tparms, tree targs, tree parm, tree arg,
   25850              :                        bool explain_p)
   25851              : {
   25852    203692988 :   if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   25853              :     return NULL_TREE;
   25854    142311989 :   else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25855              :     /* Matches anything.  */;
   25856    142311221 :   else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
   25857              :     return NULL_TREE;
   25858              : 
   25859              :   /* We need to make a new template argument vector for the call to
   25860              :      unify.  If we used TARGS, we'd clutter it up with the result of
   25861              :      the attempted unification, even if this class didn't work out.
   25862              :      We also don't want to commit ourselves to all the unifications
   25863              :      we've already done, since unification is supposed to be done on
   25864              :      an argument-by-argument basis.  In other words, consider the
   25865              :      following pathological case:
   25866              : 
   25867              :        template <int I, int J, int K>
   25868              :        struct S {};
   25869              : 
   25870              :        template <int I, int J>
   25871              :        struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
   25872              : 
   25873              :        template <int I, int J, int K>
   25874              :        void f(S<I, J, K>, S<I, I, I>);
   25875              : 
   25876              :        void g() {
   25877              :          S<0, 0, 0> s0;
   25878              :          S<0, 1, 2> s2;
   25879              : 
   25880              :          f(s0, s2);
   25881              :        }
   25882              : 
   25883              :      Now, by the time we consider the unification involving `s2', we
   25884              :      already know that we must have `f<0, 0, 0>'.  But, even though
   25885              :      `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
   25886              :      because there are two ways to unify base classes of S<0, 1, 2>
   25887              :      with S<I, I, I>.  If we kept the already deduced knowledge, we
   25888              :      would reject the possibility I=1.  */
   25889     15991004 :   targs = copy_template_args (targs);
   25890     58759715 :   for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
   25891     42768711 :     targ = NULL_TREE;
   25892              : 
   25893     15991004 :   int err;
   25894     15991004 :   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   25895          768 :     err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
   25896              :   else
   25897     15990236 :     err = unify (tparms, targs,
   25898     15990236 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   25899     15990236 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)),
   25900              :                  UNIFY_ALLOW_NONE, explain_p);
   25901              : 
   25902     15991004 :   return err ? NULL_TREE : arg;
   25903              : }
   25904              : 
   25905              : /* Given a template type PARM and a class type ARG, find the unique
   25906              :    base type in ARG that is an instance of PARM.  We do not examine
   25907              :    ARG itself; only its base-classes.  If there is not exactly one
   25908              :    appropriate base class, return NULL_TREE.  PARM may be the type of
   25909              :    a partial specialization, as well as a plain template type.  Used
   25910              :    by unify.  */
   25911              : 
   25912              : static enum template_base_result
   25913    154049046 : get_template_base (tree tparms, tree targs, tree parm, tree arg,
   25914              :                    bool explain_p, tree *result)
   25915              : {
   25916    154049046 :   tree rval = NULL_TREE;
   25917    154049046 :   tree binfo;
   25918              : 
   25919    154049046 :   gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
   25920              : 
   25921    154049046 :   binfo = TYPE_BINFO (complete_type (arg));
   25922    154049046 :   if (!binfo)
   25923              :     {
   25924              :       /* The type could not be completed.  */
   25925          288 :       *result = NULL_TREE;
   25926          288 :       return tbr_incomplete_type;
   25927              :     }
   25928              : 
   25929              :   /* Walk in inheritance graph order.  The search order is not
   25930              :      important, and this avoids multiple walks of virtual bases.  */
   25931    189124411 :   for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
   25932              :     {
   25933     70151332 :       tree r = try_class_unification (tparms, targs, parm,
   25934     35075666 :                                       BINFO_TYPE (binfo), explain_p);
   25935              : 
   25936     35075666 :       if (r)
   25937              :         {
   25938              :           /* If there is more than one satisfactory baseclass, then:
   25939              : 
   25940              :                [temp.deduct.call]
   25941              : 
   25942              :               If they yield more than one possible deduced A, the type
   25943              :               deduction fails.
   25944              : 
   25945              :              applies.  */
   25946       768780 :           if (rval && !same_type_p (r, rval))
   25947              :             {
   25948              :               /* [temp.deduct.call]/4.3: If there is a class C that is a
   25949              :                  (direct or indirect) base class of D and derived (directly or
   25950              :                  indirectly) from a class B and that would be a valid deduced
   25951              :                  A, the deduced A cannot be B or pointer to B, respectively. */
   25952           43 :               if (DERIVED_FROM_P (r, rval))
   25953              :                 /* Ignore r.  */
   25954           27 :                 continue;
   25955           16 :               else if (DERIVED_FROM_P (rval, r))
   25956              :                 /* Ignore rval.  */;
   25957              :               else
   25958              :                 {
   25959           13 :                   *result = NULL_TREE;
   25960           13 :                   return tbr_ambiguous_baseclass;
   25961              :                 }
   25962              :             }
   25963              : 
   25964              :           rval = r;
   25965              :         }
   25966              :     }
   25967              : 
   25968    154048745 :   *result = rval;
   25969    154048745 :   return tbr_success;
   25970              : }
   25971              : 
   25972              : /* Returns the level of DECL, which declares a template parameter.  */
   25973              : 
   25974              : static int
   25975    265428684 : template_decl_level (tree decl)
   25976              : {
   25977    265428684 :   switch (TREE_CODE (decl))
   25978              :     {
   25979    248022404 :     case TYPE_DECL:
   25980    248022404 :     case TEMPLATE_DECL:
   25981    248022404 :       return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
   25982              : 
   25983     17406280 :     case PARM_DECL:
   25984     17406280 :       return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
   25985              : 
   25986            0 :     default:
   25987            0 :       gcc_unreachable ();
   25988              :     }
   25989              :   return 0;
   25990              : }
   25991              : 
   25992              : /* Decide whether ARG can be unified with PARM, considering only the
   25993              :    cv-qualifiers of each type, given STRICT as documented for unify.
   25994              :    Returns nonzero iff the unification is OK on that basis.  */
   25995              : 
   25996              : static int
   25997    514889025 : check_cv_quals_for_unify (int strict, tree arg, tree parm)
   25998              : {
   25999    514889025 :   int arg_quals = cp_type_quals (arg);
   26000    514889025 :   int parm_quals = cp_type_quals (parm);
   26001              : 
   26002    514889025 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   26003    253845804 :       && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   26004              :     {
   26005              :       /*  Although a CVR qualifier is ignored when being applied to a
   26006              :           substituted template parameter ([8.3.2]/1 for example), that
   26007              :           does not allow us to unify "const T" with "int&" because both
   26008              :           types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
   26009              :           It is ok when we're allowing additional CV qualifiers
   26010              :           at the outer level [14.8.2.1]/3,1st bullet.  */
   26011    213943597 :       if ((TYPE_REF_P (arg)
   26012    196004252 :            || FUNC_OR_METHOD_TYPE_P (arg))
   26013     18082237 :           && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
   26014              :         return 0;
   26015              : 
   26016    213943235 :       if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
   26017    144611591 :           && (parm_quals & TYPE_QUAL_RESTRICT))
   26018              :         return 0;
   26019              :     }
   26020              : 
   26021    474986456 :   if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   26022    261208681 :       && (arg_quals & parm_quals) != parm_quals)
   26023              :     return 0;
   26024              : 
   26025    512966902 :   if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
   26026    260803100 :       && (parm_quals & arg_quals) != arg_quals)
   26027      2039001 :     return 0;
   26028              : 
   26029              :   return 1;
   26030              : }
   26031              : 
   26032              : /* Determines the LEVEL and INDEX for the template parameter PARM.  */
   26033              : void
   26034   8625379264 : template_parm_level_and_index (tree parm, int* level, int* index)
   26035              : {
   26036   8625379264 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   26037   8625379264 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   26038    327623620 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   26039              :     {
   26040   8298762593 :       *index = TEMPLATE_TYPE_IDX (parm);
   26041   8298762593 :       *level = TEMPLATE_TYPE_LEVEL (parm);
   26042              :     }
   26043              :   else
   26044              :     {
   26045    326616671 :       *index = TEMPLATE_PARM_IDX (parm);
   26046    326616671 :       *level = TEMPLATE_PARM_LEVEL (parm);
   26047              :     }
   26048   8625379264 : }
   26049              : 
   26050              : #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP)                    \
   26051              :   do {                                                                  \
   26052              :     if (unify (TP, TA, P, A, S, EP))                                    \
   26053              :       return 1;                                                         \
   26054              :   } while (0)
   26055              : 
   26056              : /* Unifies the remaining arguments in PACKED_ARGS with the pack
   26057              :    expansion at the end of PACKED_PARMS. Returns 0 if the type
   26058              :    deduction succeeds, 1 otherwise. STRICT is the same as in
   26059              :    fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
   26060              :    function call argument list. We'll need to adjust the arguments to make them
   26061              :    types. SUBR tells us if this is from a recursive call to
   26062              :    type_unification_real, or for comparing two template argument
   26063              :    lists. */
   26064              : 
   26065              : static int
   26066      8725879 : unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
   26067              :                       tree packed_args, unification_kind_t strict,
   26068              :                       bool subr, bool explain_p)
   26069              : {
   26070      8725879 :   tree parm
   26071      8725879 :     = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
   26072      8725879 :   tree pattern = PACK_EXPANSION_PATTERN (parm);
   26073      8725879 :   tree pack, packs = NULL_TREE;
   26074      8725879 :   int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
   26075              : 
   26076              :   /* Add in any args remembered from an earlier partial instantiation.  */
   26077      8725879 :   targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
   26078     17451758 :   int levels = TMPL_ARGS_DEPTH (targs);
   26079              : 
   26080      8725879 :   packed_args = expand_template_argument_pack (packed_args);
   26081              : 
   26082      8725879 :   int len = TREE_VEC_LENGTH (packed_args);
   26083              : 
   26084              :   /* Determine the parameter packs we will be deducing from the
   26085              :      pattern, and record their current deductions.  */
   26086     17217306 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
   26087     17451796 :        pack; pack = TREE_CHAIN (pack))
   26088              :     {
   26089      8725917 :       tree parm_pack = TREE_VALUE (pack);
   26090      8725917 :       int idx, level;
   26091              : 
   26092              :       /* Only template parameter packs can be deduced, not e.g. function
   26093              :          parameter packs or __bases or __integer_pack.  */
   26094      8725917 :       if (!TEMPLATE_PARM_P (parm_pack))
   26095       320266 :         continue;
   26096              : 
   26097              :       /* Determine the index and level of this parameter pack.  */
   26098      8725911 :       template_parm_level_and_index (parm_pack, &level, &idx);
   26099      8725911 :       if (level > levels)
   26100       320260 :         continue;
   26101              : 
   26102              :       /* Keep track of the parameter packs and their corresponding
   26103              :          argument packs.  */
   26104     16811302 :       packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
   26105      8405651 :       TREE_TYPE (packs) = make_tree_vec (len - start);
   26106              :     }
   26107              : 
   26108              :   /* Loop through all of the arguments that have not yet been
   26109              :      unified and unify each with the pattern.  */
   26110     18553146 :   for (i = start; i < len; i++)
   26111              :     {
   26112      9831782 :       tree parm;
   26113      9831782 :       bool any_explicit = false;
   26114      9831782 :       tree arg = TREE_VEC_ELT (packed_args, i);
   26115              : 
   26116              :       /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
   26117              :          or the element of its argument pack at the current index if
   26118              :          this argument was explicitly specified.  */
   26119     19343366 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   26120              :         {
   26121      9511584 :           int idx, level;
   26122      9511584 :           tree arg, pargs;
   26123      9511584 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   26124              : 
   26125      9511584 :           arg = NULL_TREE;
   26126      9511584 :           if (TREE_VALUE (pack)
   26127       134488 :               && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
   26128      9512345 :               && (i - start < TREE_VEC_LENGTH (pargs)))
   26129              :             {
   26130          698 :               any_explicit = true;
   26131          698 :               arg = TREE_VEC_ELT (pargs, i - start);
   26132              :             }
   26133     19023168 :           TMPL_ARG (targs, level, idx) = arg;
   26134              :         }
   26135              : 
   26136              :       /* If we had explicit template arguments, substitute them into the
   26137              :          pattern before deduction.  */
   26138      9831782 :       if (any_explicit)
   26139              :         {
   26140              :           /* Some arguments might still be unspecified or dependent.  */
   26141          698 :           bool dependent;
   26142          698 :           ++processing_template_decl;
   26143          698 :           dependent = any_dependent_template_arguments_p (targs);
   26144          698 :           if (!dependent)
   26145          683 :             --processing_template_decl;
   26146         1396 :           parm = tsubst (pattern, targs,
   26147              :                          explain_p ? tf_warning_or_error : tf_none,
   26148              :                          NULL_TREE);
   26149          698 :           if (dependent)
   26150           15 :             --processing_template_decl;
   26151          698 :           if (parm == error_mark_node)
   26152              :             return 1;
   26153              :         }
   26154              :       else
   26155              :         parm = pattern;
   26156              : 
   26157              :       /* Unify the pattern with the current argument.  */
   26158      9831782 :       if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
   26159              :                               explain_p))
   26160              :         return 1;
   26161              : 
   26162              :       /* For each parameter pack, collect the deduced value.  */
   26163     19334330 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   26164              :         {
   26165      9507063 :           int idx, level;
   26166      9507063 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   26167              : 
   26168      9507063 :           TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
   26169     19014126 :             TMPL_ARG (targs, level, idx);
   26170              :         }
   26171              :     }
   26172              : 
   26173              :   /* Verify that the results of unification with the parameter packs
   26174              :      produce results consistent with what we've seen before, and make
   26175              :      the deduced argument packs available.  */
   26176     17122476 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   26177              :     {
   26178      8401130 :       tree old_pack = TREE_VALUE (pack);
   26179      8401130 :       tree new_args = TREE_TYPE (pack);
   26180      8401130 :       int i, len = TREE_VEC_LENGTH (new_args);
   26181      8401130 :       int idx, level;
   26182      8401130 :       bool nondeduced_p = false;
   26183              : 
   26184              :       /* By default keep the original deduced argument pack.
   26185              :          If necessary, more specific code is going to update the
   26186              :          resulting deduced argument later down in this function.  */
   26187      8401130 :       template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   26188     16802260 :       TMPL_ARG (targs, level, idx) = old_pack;
   26189              : 
   26190              :       /* If NEW_ARGS contains any NULL_TREE entries, we didn't
   26191              :          actually deduce anything.  */
   26192     17903657 :       for (i = 0; i < len && !nondeduced_p; ++i)
   26193      9502527 :         if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
   26194         2659 :           nondeduced_p = true;
   26195      8401130 :       if (nondeduced_p)
   26196         2659 :         continue;
   26197              : 
   26198      8398471 :       if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
   26199              :         {
   26200              :           /* If we had fewer function args than explicit template args,
   26201              :              just use the explicits.  */
   26202          302 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   26203          302 :           int explicit_len = TREE_VEC_LENGTH (explicit_args);
   26204          302 :           if (len < explicit_len)
   26205      8398471 :             new_args = explicit_args;
   26206              :         }
   26207              : 
   26208      8398471 :       if (!old_pack)
   26209              :         {
   26210      8182430 :           tree result;
   26211              :           /* Build the deduced *_ARGUMENT_PACK.  */
   26212      8182430 :           if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
   26213              :             {
   26214       234431 :               result = make_node (NONTYPE_ARGUMENT_PACK);
   26215       234431 :               TREE_CONSTANT (result) = 1;
   26216              :             }
   26217              :           else
   26218      7947999 :             result = cxx_make_type (TYPE_ARGUMENT_PACK);
   26219              : 
   26220      8182430 :           ARGUMENT_PACK_ARGS (result) = new_args;
   26221              : 
   26222              :           /* Note the deduced argument packs for this parameter
   26223              :              pack.  */
   26224     16364860 :           TMPL_ARG (targs, level, idx) = result;
   26225              :         }
   26226       216041 :       else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
   26227       216041 :                && (ARGUMENT_PACK_ARGS (old_pack)
   26228          302 :                    == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
   26229              :         {
   26230              :           /* We only had the explicitly-provided arguments before, but
   26231              :              now we have a complete set of arguments.  */
   26232          302 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   26233              : 
   26234          302 :           ARGUMENT_PACK_ARGS (old_pack) = new_args;
   26235          302 :           ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
   26236          302 :           ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
   26237              :         }
   26238              :       else
   26239              :         {
   26240       215739 :           tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
   26241       215739 :           tree old_args = ARGUMENT_PACK_ARGS (old_pack);
   26242       215739 :           temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
   26243              :           /* During template argument deduction for the aggregate deduction
   26244              :              candidate, the number of elements in a trailing parameter pack
   26245              :              is only deduced from the number of remaining function
   26246              :              arguments if it is not otherwise deduced.  */
   26247       215739 :           if (cxx_dialect >= cxx20
   26248       211236 :               && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
   26249              :               /* FIXME This isn't set properly for partial instantiations.  */
   26250           14 :               && TPARMS_PRIMARY_TEMPLATE (tparms)
   26251       215751 :               && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
   26252           12 :             TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
   26253       215739 :           if (!comp_template_args (old_args, new_args,
   26254              :                                    &bad_old_arg, &bad_new_arg))
   26255              :             /* Inconsistent unification of this parameter pack.  */
   26256           21 :             return unify_parameter_pack_inconsistent (explain_p,
   26257              :                                                       bad_old_arg,
   26258              :                                                       bad_new_arg);
   26259       215739 :         }
   26260              :     }
   26261              : 
   26262              :   return unify_success (explain_p);
   26263              : }
   26264              : 
   26265              : /* Handle unification of the domain of an array.  PARM_DOM and ARG_DOM are
   26266              :    INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs.  The other
   26267              :    parameters and return value are as for unify.  */
   26268              : 
   26269              : static int
   26270       187737 : unify_array_domain (tree tparms, tree targs,
   26271              :                     tree parm_dom, tree arg_dom,
   26272              :                     bool explain_p)
   26273              : {
   26274       187737 :   tree parm_max;
   26275       187737 :   tree arg_max;
   26276       187737 :   bool parm_cst;
   26277       187737 :   bool arg_cst;
   26278              : 
   26279              :   /* Our representation of array types uses "N - 1" as the
   26280              :      TYPE_MAX_VALUE for an array with "N" elements, if "N" is
   26281              :      not an integer constant.  We cannot unify arbitrarily
   26282              :      complex expressions, so we eliminate the MINUS_EXPRs
   26283              :      here.  */
   26284       187737 :   parm_max = TYPE_MAX_VALUE (parm_dom);
   26285       187737 :   parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
   26286       187737 :   if (!parm_cst)
   26287              :     {
   26288       187636 :       gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
   26289       187636 :       parm_max = TREE_OPERAND (parm_max, 0);
   26290              :     }
   26291       187737 :   arg_max = TYPE_MAX_VALUE (arg_dom);
   26292       187737 :   arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
   26293       187737 :   if (!arg_cst)
   26294              :     {
   26295              :       /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
   26296              :          trying to unify the type of a variable with the type
   26297              :          of a template parameter.  For example:
   26298              : 
   26299              :            template <unsigned int N>
   26300              :            void f (char (&) [N]);
   26301              :            int g();
   26302              :            void h(int i) {
   26303              :              char a[g(i)];
   26304              :              f(a);
   26305              :            }
   26306              : 
   26307              :          Here, the type of the ARG will be "int [g(i)]", and
   26308              :          may be a SAVE_EXPR, etc.  */
   26309         3628 :       if (TREE_CODE (arg_max) != MINUS_EXPR)
   26310            6 :         return unify_vla_arg (explain_p, arg_dom);
   26311         3622 :       arg_max = TREE_OPERAND (arg_max, 0);
   26312              :     }
   26313              : 
   26314              :   /* If only one of the bounds used a MINUS_EXPR, compensate
   26315              :      by adding one to the other bound.  */
   26316       187731 :   if (parm_cst && !arg_cst)
   26317            0 :     parm_max = fold_build2_loc (input_location, PLUS_EXPR,
   26318            0 :                                 TREE_TYPE (parm_max),
   26319              :                                 parm_max,
   26320            0 :                                 build_int_cst (TREE_TYPE (parm_max), 1));
   26321       187731 :   else if (arg_cst && !parm_cst)
   26322       368016 :     arg_max = fold_build2_loc (input_location, PLUS_EXPR,
   26323       184008 :                                TREE_TYPE (arg_max),
   26324              :                                arg_max,
   26325       184008 :                                build_int_cst (TREE_TYPE (arg_max), 1));
   26326              : 
   26327       187731 :   return unify (tparms, targs, parm_max, arg_max,
   26328       187731 :                 UNIFY_ALLOW_INTEGER, explain_p);
   26329              : }
   26330              : 
   26331              : /* Returns whether T, a P or A in unify, is a type, template or expression.  */
   26332              : 
   26333              : enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
   26334              : 
   26335              : static pa_kind_t
   26336   1814910638 : pa_kind (tree t)
   26337              : {
   26338   1814910638 :   if (PACK_EXPANSION_P (t))
   26339      1276689 :     t = PACK_EXPANSION_PATTERN (t);
   26340   1814910638 :   if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
   26341   1814082750 :       || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
   26342   3628993367 :       || DECL_TYPE_TEMPLATE_P (t))
   26343              :     return pa_tmpl;
   26344   1813519807 :   else if (TYPE_P (t))
   26345              :     return pa_type;
   26346              :   else
   26347    325994683 :     return pa_expr;
   26348              : }
   26349              : 
   26350              : /* Deduce the value of template parameters.  TPARMS is the (innermost)
   26351              :    set of template parameters to a template.  TARGS is the bindings
   26352              :    for those template parameters, as determined thus far; TARGS may
   26353              :    include template arguments for outer levels of template parameters
   26354              :    as well.  PARM is a parameter to a template function, or a
   26355              :    subcomponent of that parameter; ARG is the corresponding argument.
   26356              :    This function attempts to match PARM with ARG in a manner
   26357              :    consistent with the existing assignments in TARGS.  If more values
   26358              :    are deduced, then TARGS is updated.
   26359              : 
   26360              :    Returns 0 if the type deduction succeeds, 1 otherwise.  The
   26361              :    parameter STRICT is a bitwise or of the following flags:
   26362              : 
   26363              :      UNIFY_ALLOW_NONE:
   26364              :        Require an exact match between PARM and ARG.
   26365              :      UNIFY_ALLOW_MORE_CV_QUAL:
   26366              :        Allow the deduced ARG to be more cv-qualified (by qualification
   26367              :        conversion) than ARG.
   26368              :      UNIFY_ALLOW_LESS_CV_QUAL:
   26369              :        Allow the deduced ARG to be less cv-qualified than ARG.
   26370              :      UNIFY_ALLOW_DERIVED:
   26371              :        Allow the deduced ARG to be a template base class of ARG,
   26372              :        or a pointer to a template base class of the type pointed to by
   26373              :        ARG.
   26374              :      UNIFY_ALLOW_INTEGER:
   26375              :        Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
   26376              :        case for more information.
   26377              :      UNIFY_ALLOW_OUTER_LEVEL:
   26378              :        This is the outermost level of a deduction. Used to determine validity
   26379              :        of qualification conversions. A valid qualification conversion must
   26380              :        have const qualified pointers leading up to the inner type which
   26381              :        requires additional CV quals, except at the outer level, where const
   26382              :        is not required [conv.qual]. It would be normal to set this flag in
   26383              :        addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
   26384              :      UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
   26385              :        This is the outermost level of a deduction, and PARM can be more CV
   26386              :        qualified at this point.
   26387              :      UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
   26388              :        This is the outermost level of a deduction, and PARM can be less CV
   26389              :        qualified at this point.  */
   26390              : 
   26391              : static int
   26392    930023888 : unify (tree tparms, tree targs, tree parm, tree arg, int strict,
   26393              :        bool explain_p)
   26394              : {
   26395    930023888 :   int idx;
   26396    930023888 :   tree targ;
   26397    930023888 :   tree tparm;
   26398    930023888 :   int strict_in = strict;
   26399    930017055 :   tsubst_flags_t complain = (explain_p
   26400    930023888 :                              ? tf_warning_or_error
   26401              :                              : tf_none);
   26402              : 
   26403    930023888 :   if (arg == error_mark_node)
   26404              :     return unify_invalid (explain_p);
   26405    930023888 :   if (arg == unknown_type_node
   26406    930023888 :       || arg == init_list_type_node)
   26407              :     /* We can't deduce anything from this, but we might get all the
   26408              :        template args from other function args.  */
   26409              :     return unify_success (explain_p);
   26410              : 
   26411    930023888 :   if (parm == any_targ_node || arg == any_targ_node)
   26412              :     return unify_success (explain_p);
   26413              : 
   26414              :   /* Strip conversions that will interfere with NTTP deduction.
   26415              :      I don't think this will do the right thing with respect to types.
   26416              :      But the only case I've seen it in so far has been array bounds, where
   26417              :      signedness is the only information lost, and I think that will be
   26418              :      okay.  VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
   26419              :      finish_id_expression_1, and are also OK.  */
   26420    930023880 :   if (deducible_expression (parm))
   26421     10718445 :     while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR
   26422     21433916 :            || TREE_CODE (parm) == IMPLICIT_CONV_EXPR)
   26423         7320 :       parm = TREE_OPERAND (parm, 0);
   26424              : 
   26425              :   /* If PARM uses template parameters, then we can't bail out here,
   26426              :      even if ARG == PARM, since we won't record unifications for the
   26427              :      template parameters.  We might need them if we're trying to
   26428              :      figure out which of two things is more specialized.  */
   26429    930023880 :   if (arg == parm
   26430    930023880 :       && (DECL_P (parm) || !uses_template_parms (parm)))
   26431              :     return unify_success (explain_p);
   26432              : 
   26433              :   /* Handle init lists early, so the rest of the function can assume
   26434              :      we're dealing with a type. */
   26435    907467103 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg))
   26436              :     {
   26437        11784 :       tree elttype;
   26438        11784 :       tree orig_parm = parm;
   26439              : 
   26440        11784 :       if (!is_std_init_list (parm)
   26441        11784 :           && TREE_CODE (parm) != ARRAY_TYPE)
   26442              :         /* We can only deduce from an initializer list argument if the
   26443              :            parameter is std::initializer_list or an array; otherwise this
   26444              :            is a non-deduced context. */
   26445              :         return unify_success (explain_p);
   26446              : 
   26447         2988 :       if (TREE_CODE (parm) == ARRAY_TYPE)
   26448          532 :         elttype = TREE_TYPE (parm);
   26449              :       else
   26450              :         {
   26451         2456 :           elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
   26452              :           /* Deduction is defined in terms of a single type, so just punt
   26453              :              on the (bizarre) std::initializer_list<T...>.  */
   26454         2456 :           if (PACK_EXPANSION_P (elttype))
   26455              :             return unify_success (explain_p);
   26456              :         }
   26457              : 
   26458         2985 :       if (strict != DEDUCE_EXACT
   26459         2985 :           && TYPE_P (elttype)
   26460         5970 :           && !uses_deducible_template_parms (elttype))
   26461              :         /* If ELTTYPE has no deducible template parms, skip deduction from
   26462              :            the list elements.  */;
   26463              :       else
   26464        16183 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
   26465              :           {
   26466         7877 :             tree elt = e.value;
   26467         7877 :             int elt_strict = strict;
   26468              : 
   26469         7877 :             if (elt == error_mark_node)
   26470              :               return unify_invalid (explain_p);
   26471              : 
   26472         7877 :             if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
   26473              :               {
   26474         6882 :                 tree type = TREE_TYPE (elt);
   26475         6882 :                 if (type == error_mark_node)
   26476            6 :                   return unify_invalid (explain_p);
   26477              :                 /* It should only be possible to get here for a call.  */
   26478         6876 :                 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
   26479        13752 :                 elt_strict |= maybe_adjust_types_for_deduction
   26480         6876 :                   (tparms, DEDUCE_CALL, &elttype, &type, elt);
   26481         6876 :                 elt = type;
   26482              :               }
   26483              : 
   26484         7871 :           RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
   26485              :                                    explain_p);
   26486              :         }
   26487              : 
   26488         2518 :       if (TREE_CODE (parm) == ARRAY_TYPE
   26489         2518 :           && deducible_array_bound (TYPE_DOMAIN (parm)))
   26490              :         {
   26491              :           /* Also deduce from the length of the initializer list.  */
   26492           76 :           tree max = size_int (count_ctor_elements (arg));
   26493           76 :           tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
   26494           76 :           if (idx == error_mark_node)
   26495              :             return unify_invalid (explain_p);
   26496           76 :           return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   26497           76 :                                      idx, explain_p);
   26498              :         }
   26499              : 
   26500              :       /* If the std::initializer_list<T> deduction worked, replace the
   26501              :          deduced A with std::initializer_list<A>.  */
   26502              :       if (orig_parm != parm)
   26503              :         {
   26504              :           idx = TEMPLATE_TYPE_IDX (orig_parm);
   26505              :           targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26506              :           targ = listify (targ);
   26507              :           TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
   26508              :         }
   26509              :       return unify_success (explain_p);
   26510              :     }
   26511              : 
   26512              :   /* If parm and arg aren't the same kind of thing (template, type, or
   26513              :      expression), fail early.  */
   26514    907455319 :   if (pa_kind (parm) != pa_kind (arg))
   26515              :     return unify_invalid (explain_p);
   26516              : 
   26517              :   /* Immediately reject some pairs that won't unify because of
   26518              :      cv-qualification mismatches.  */
   26519    907455159 :   if (TREE_CODE (arg) == TREE_CODE (parm)
   26520    452438234 :       && TYPE_P (arg)
   26521              :       /* It is the elements of the array which hold the cv quals of an array
   26522              :          type, and the elements might be template type parms. We'll check
   26523              :          when we recurse.  */
   26524    298842810 :       && TREE_CODE (arg) != ARRAY_TYPE
   26525              :       /* We check the cv-qualifiers when unifying with template type
   26526              :          parameters below.  We want to allow ARG `const T' to unify with
   26527              :          PARM `T' for example, when computing which of two templates
   26528              :          is more specialized, for example.  */
   26529    298612813 :       && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
   26530   1168478605 :       && !check_cv_quals_for_unify (strict_in, arg, parm))
   26531      2220329 :     return unify_cv_qual_mismatch (explain_p, parm, arg);
   26532              : 
   26533    905234830 :   if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
   26534    443163247 :       && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
   26535   1181742847 :       && !FUNC_OR_METHOD_TYPE_P (parm))
   26536    276211989 :     strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
   26537              :   /* PMFs recurse at the same level, so don't strip this yet.  */
   26538    905234830 :   if (!TYPE_PTRMEMFUNC_P (parm))
   26539    905232084 :     strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
   26540    905234830 :   strict &= ~UNIFY_ALLOW_DERIVED;
   26541    905234830 :   strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   26542    905234830 :   strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
   26543              : 
   26544    905234830 :   switch (TREE_CODE (parm))
   26545              :     {
   26546              :     case TYPENAME_TYPE:
   26547              :     case SCOPE_REF:
   26548              :     case UNBOUND_CLASS_TEMPLATE:
   26549              :       /* In a type which contains a nested-name-specifier, template
   26550              :          argument values cannot be deduced for template parameters used
   26551              :          within the nested-name-specifier.  */
   26552              :       return unify_success (explain_p);
   26553              : 
   26554    254715466 :     case TEMPLATE_TYPE_PARM:
   26555    254715466 :     case TEMPLATE_TEMPLATE_PARM:
   26556    254715466 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   26557    254715466 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   26558    254715466 :       if (error_operand_p (tparm))
   26559              :         return unify_invalid (explain_p);
   26560              : 
   26561    509430924 :       if (TEMPLATE_TYPE_LEVEL (parm)
   26562    254715462 :           != template_decl_level (tparm))
   26563              :         /* The PARM is not one we're trying to unify.  Just check
   26564              :            to see if it matches ARG.  */
   26565              :         {
   26566          462 :           if (TREE_CODE (arg) == TREE_CODE (parm)
   26567          471 :               && (is_auto (parm) ? is_auto (arg)
   26568            9 :                   : same_type_p (parm, arg)))
   26569              :             return unify_success (explain_p);
   26570              :           else
   26571          423 :             return unify_type_mismatch (explain_p, parm, arg);
   26572              :         }
   26573    254715000 :       idx = TEMPLATE_TYPE_IDX (parm);
   26574    254715000 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26575    254715000 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
   26576    254715000 :       if (error_operand_p (tparm))
   26577              :         return unify_invalid (explain_p);
   26578              : 
   26579              :       /* Check for mixed types and values.  */
   26580    254715000 :       if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   26581    253843870 :            && TREE_CODE (tparm) != TYPE_DECL)
   26582    254715000 :           || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   26583       693037 :               && TREE_CODE (tparm) != TEMPLATE_DECL))
   26584            0 :         gcc_unreachable ();
   26585              : 
   26586    254715000 :       if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   26587              :         {
   26588       178093 :           if ((strict_in & UNIFY_ALLOW_DERIVED)
   26589       178093 :               && CLASS_TYPE_P (arg))
   26590              :             {
   26591              :               /* First try to match ARG directly.  */
   26592          768 :               tree t = try_class_unification (tparms, targs, parm, arg,
   26593          768 :                                               explain_p);
   26594          768 :               if (!t)
   26595              :                 {
   26596              :                   /* Otherwise, look for a suitable base of ARG, as below.  */
   26597           40 :                   enum template_base_result r;
   26598           40 :                   r = get_template_base (tparms, targs, parm, arg,
   26599              :                                          explain_p, &t);
   26600           40 :                   if (!t)
   26601           34 :                     return unify_no_common_base (explain_p, r, parm, arg);
   26602            6 :                   arg = t;
   26603              :                 }
   26604              :             }
   26605              :           /* ARG must be constructed from a template class or a template
   26606              :              template parameter.  */
   26607       177325 :           else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
   26608       177325 :                    && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   26609         1053 :             return unify_template_deduction_failure (explain_p, parm, arg);
   26610              : 
   26611              :           /* Deduce arguments T, i from TT<T> or TT<i>.  */
   26612       177006 :           if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
   26613              :             return 1;
   26614              : 
   26615       176745 :           arg = TYPE_TI_TEMPLATE (arg);
   26616       176745 :           if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
   26617              :             /* If the template is a template template parameter, use the
   26618              :                TEMPLATE_TEMPLATE_PARM for matching.  */
   26619           41 :             arg = TREE_TYPE (arg);
   26620              : 
   26621              :           /* Fall through to deduce template name.  */
   26622              :         }
   26623              : 
   26624    254713652 :       if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   26625    254020615 :           || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   26626              :         {
   26627              :           /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
   26628              : 
   26629              :           /* Simple cases: Value already set, does match or doesn't.  */
   26630       869782 :           if (targ != NULL_TREE && template_args_equal (targ, arg))
   26631              :             return unify_success (explain_p);
   26632       850176 :           else if (targ)
   26633        20158 :             return unify_inconsistency (explain_p, parm, targ, arg);
   26634              :         }
   26635              :       else
   26636              :         {
   26637              :           /* If PARM is `const T' and ARG is only `int', we don't have
   26638              :              a match unless we are allowing additional qualification.
   26639              :              If ARG is `const int' and PARM is just `T' that's OK;
   26640              :              that binds `const int' to `T'.  */
   26641    253843870 :           if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
   26642              :                                          arg, parm))
   26643      1739543 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   26644              : 
   26645              :           /* Consider the case where ARG is `const volatile int' and
   26646              :              PARM is `const T'.  Then, T should be `volatile int'.  */
   26647    504208654 :           arg = cp_build_qualified_type
   26648    252104327 :             (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
   26649    252104327 :           if (arg == error_mark_node)
   26650              :             return unify_invalid (explain_p);
   26651              : 
   26652              :           /* Simple cases: Value already set, does match or doesn't.  */
   26653    252104327 :           if (targ != NULL_TREE && same_type_p (targ, arg))
   26654              :             return unify_success (explain_p);
   26655    225421447 :           else if (targ)
   26656      1024830 :             return unify_inconsistency (explain_p, parm, targ, arg);
   26657              : 
   26658              :           /* Make sure that ARG is not a variable-sized array.  (Note
   26659              :              that were talking about variable-sized arrays (like
   26660              :              `int[n]'), rather than arrays of unknown size (like
   26661              :              `int[]').)  We'll get very confused by such a type since
   26662              :              the bound of the array is not constant, and therefore
   26663              :              not mangleable.  Besides, such types are not allowed in
   26664              :              ISO C++, so we can do as we please here.  We do allow
   26665              :              them for 'auto' deduction, since that isn't ABI-exposed.  */
   26666    224396617 :           if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
   26667           14 :             return unify_vla_arg (explain_p, arg);
   26668              : 
   26669              :           /* Strip typedefs as in convert_template_argument.  */
   26670    224396603 :           arg = canonicalize_type_argument (arg, tf_none);
   26671              :         }
   26672              : 
   26673              :       /* If ARG is a parameter pack or an expansion, we cannot unify
   26674              :          against it unless PARM is also a parameter pack.  */
   26675    450452260 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   26676    226429739 :           && !template_parameter_pack_p (parm))
   26677       466795 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   26678              : 
   26679              :       /* If the argument deduction results is a METHOD_TYPE,
   26680              :          then there is a problem.
   26681              :          METHOD_TYPE doesn't map to any real C++ type the result of
   26682              :          the deduction cannot be of that type.  */
   26683    224759826 :       if (TREE_CODE (arg) == METHOD_TYPE)
   26684            6 :         return unify_method_type_error (explain_p, arg);
   26685              : 
   26686    224759820 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   26687    224759820 :       return unify_success (explain_p);
   26688              : 
   26689     10713222 :     case TEMPLATE_PARM_INDEX:
   26690     10713222 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   26691     10713222 :       if (error_operand_p (tparm))
   26692              :         return unify_invalid (explain_p);
   26693              : 
   26694     10713222 :       if (TEMPLATE_PARM_LEVEL (parm)
   26695     10713222 :           != template_decl_level (tparm))
   26696              :         {
   26697              :           /* The PARM is not one we're trying to unify.  Just check
   26698              :              to see if it matches ARG.  */
   26699            3 :           int result = !(TREE_CODE (arg) == TREE_CODE (parm)
   26700            0 :                          && cp_tree_equal (parm, arg));
   26701              :           if (result)
   26702            3 :             unify_expression_unequal (explain_p, parm, arg);
   26703              :           return result;
   26704              :         }
   26705              : 
   26706     10713219 :       idx = TEMPLATE_PARM_IDX (parm);
   26707     10713219 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   26708              : 
   26709     10713219 :       if (targ)
   26710              :         {
   26711         8010 :           if ((strict & UNIFY_ALLOW_INTEGER)
   26712         1602 :               && TREE_TYPE (targ) && TREE_TYPE (arg)
   26713         9612 :               && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
   26714              :             /* We're deducing from an array bound, the type doesn't matter.
   26715              :                This conversion should match the one below.  */
   26716         1602 :             arg = fold (build_nop (TREE_TYPE (targ), arg));
   26717         8010 :           int x = !cp_tree_equal (targ, arg);
   26718         8010 :           if (x)
   26719          577 :             unify_inconsistency (explain_p, parm, targ, arg);
   26720              :           return x;
   26721              :         }
   26722              : 
   26723              :       /* [temp.deduct.type] If, in the declaration of a function template
   26724              :          with a non-type template-parameter, the non-type
   26725              :          template-parameter is used in an expression in the function
   26726              :          parameter-list and, if the corresponding template-argument is
   26727              :          deduced, the template-argument type shall match the type of the
   26728              :          template-parameter exactly, except that a template-argument
   26729              :          deduced from an array bound may be of any integral type.
   26730              :          The non-type parameter might use already deduced type parameters.  */
   26731     10705209 :       tparm = TREE_TYPE (parm);
   26732     32115573 :       if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
   26733              :         /* We don't have enough levels of args to do any substitution.  This
   26734              :            can happen in the context of -fnew-ttp-matching.  */;
   26735              :       else
   26736              :         {
   26737     10699562 :           ++processing_template_decl;
   26738     10699562 :           tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
   26739     10699562 :           --processing_template_decl;
   26740              : 
   26741     10699562 :           if (tree a = type_uses_auto (tparm))
   26742              :             {
   26743         9820 :               tparm = do_auto_deduction (tparm, arg, a,
   26744              :                                          complain, adc_unify, targs,
   26745              :                                          LOOKUP_NORMAL,
   26746         4910 :                                          TPARMS_PRIMARY_TEMPLATE (tparms));
   26747         4910 :               if (tparm == error_mark_node)
   26748              :                 return 1;
   26749              :             }
   26750              :         }
   26751              : 
   26752     10705206 :       if (!TREE_TYPE (arg)
   26753     10705206 :           || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
   26754              :         /* Template-parameter dependent expression.  Just accept it for now.
   26755              :            It will later be processed in convert_template_argument.  */
   26756              :         ;
   26757     10705190 :       else if (same_type_ignoring_top_level_qualifiers_p
   26758     10705190 :                (non_reference (TREE_TYPE (arg)),
   26759              :                 non_reference (tparm)))
   26760              :         /* OK.  Ignore top-level quals here because a class-type template
   26761              :            parameter object is const.  */;
   26762       183308 :       else if ((strict & UNIFY_ALLOW_INTEGER)
   26763       183024 :                && CP_INTEGRAL_TYPE_P (tparm))
   26764              :         /* Convert the ARG to the type of PARM; the deduced non-type
   26765              :            template argument must exactly match the types of the
   26766              :            corresponding parameter.  This conversion should match the
   26767              :            one above.  */
   26768       183009 :         arg = fold (build_nop (tparm, arg));
   26769          299 :       else if (uses_template_parms (tparm))
   26770              :         {
   26771              :           /* We haven't deduced the type of this parameter yet.  */
   26772          200 :           if (cxx_dialect >= cxx17
   26773              :               /* We deduce from array bounds in try_array_deduction.  */
   26774          172 :               && !(strict & UNIFY_ALLOW_INTEGER)
   26775          671 :               && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
   26776              :             {
   26777              :               /* Deduce it from the non-type argument.  As above, ignore
   26778              :                  top-level quals here too.  */
   26779          120 :               tree atype = cv_unqualified (TREE_TYPE (arg));
   26780          120 :               RECUR_AND_CHECK_FAILURE (tparms, targs,
   26781              :                                        tparm, atype,
   26782              :                                        UNIFY_ALLOW_NONE, explain_p);
   26783              :               /* Now check whether the type of this parameter is still
   26784              :                  dependent, and give up if so.  */
   26785          111 :               ++processing_template_decl;
   26786          111 :               tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
   26787          111 :               --processing_template_decl;
   26788          111 :               if (uses_template_parms (tparm))
   26789              :                 return unify_success (explain_p);
   26790              :             }
   26791              :           else
   26792              :             /* Try again later.  */
   26793              :             return unify_success (explain_p);
   26794              :         }
   26795              :       else
   26796           99 :         return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
   26797              : 
   26798              :       /* If ARG is a parameter pack or an expansion, we cannot unify
   26799              :          against it unless PARM is also a parameter pack.  */
   26800     21409912 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   26801     10777401 :           && !TEMPLATE_PARM_PARAMETER_PACK (parm))
   26802            7 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   26803              : 
   26804     10704949 :       arg = convert_nontype_argument_maybe_dependent (tparm, arg,
   26805              :                                                       /*forced=*/false,
   26806              :                                                       complain);
   26807     10704949 :       if (!arg || arg == error_mark_node)
   26808              :         return unify_invalid (explain_p);
   26809              : 
   26810     10704937 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   26811     10704937 :       return unify_success (explain_p);
   26812              : 
   26813           21 :     case PTRMEM_CST:
   26814           21 :      {
   26815              :         /* A pointer-to-member constant can be unified only with
   26816              :          another constant.  */
   26817           21 :       if (TREE_CODE (arg) != PTRMEM_CST)
   26818            3 :         return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
   26819              : 
   26820              :       /* Just unify the class member. It would be useless (and possibly
   26821              :          wrong, depending on the strict flags) to unify also
   26822              :          PTRMEM_CST_CLASS, because we want to be sure that both parm and
   26823              :          arg refer to the same variable, even if through different
   26824              :          classes. For instance:
   26825              : 
   26826              :          struct A { int x; };
   26827              :          struct B : A { };
   26828              : 
   26829              :          Unification of &A::x and &B::x must succeed.  */
   26830           18 :       return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
   26831           18 :                     PTRMEM_CST_MEMBER (arg), strict, explain_p);
   26832              :      }
   26833              : 
   26834      9017286 :     case POINTER_TYPE:
   26835      9017286 :       {
   26836      9017286 :         if (!TYPE_PTR_P (arg))
   26837      4635141 :           return unify_type_mismatch (explain_p, parm, arg);
   26838              : 
   26839              :         /* [temp.deduct.call]
   26840              : 
   26841              :            A can be another pointer or pointer to member type that can
   26842              :            be converted to the deduced A via a qualification
   26843              :            conversion (_conv.qual_).
   26844              : 
   26845              :            We pass down STRICT here rather than UNIFY_ALLOW_NONE.
   26846              :            This will allow for additional cv-qualification of the
   26847              :            pointed-to types if appropriate.  */
   26848              : 
   26849      4382145 :         if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
   26850              :           /* The derived-to-base conversion only persists through one
   26851              :              level of pointers.  */
   26852       655789 :           strict |= (strict_in & UNIFY_ALLOW_DERIVED);
   26853              : 
   26854      4382145 :         return unify (tparms, targs, TREE_TYPE (parm),
   26855      8764290 :                       TREE_TYPE (arg), strict, explain_p);
   26856              :       }
   26857              : 
   26858     34475567 :     case REFERENCE_TYPE:
   26859     34475567 :       if (!TYPE_REF_P (arg)
   26860     34475567 :           || TYPE_REF_IS_RVALUE (parm) != TYPE_REF_IS_RVALUE (arg))
   26861      6419908 :         return unify_type_mismatch (explain_p, parm, arg);
   26862     28055659 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26863     28055659 :                     strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   26864              : 
   26865      3021812 :     case ARRAY_TYPE:
   26866      3021812 :       if (TREE_CODE (arg) != ARRAY_TYPE)
   26867      2791815 :         return unify_type_mismatch (explain_p, parm, arg);
   26868       229997 :       if ((TYPE_DOMAIN (parm) == NULL_TREE)
   26869       229997 :           != (TYPE_DOMAIN (arg) == NULL_TREE))
   26870        13067 :         return unify_type_mismatch (explain_p, parm, arg);
   26871       216930 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   26872              :                                strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   26873       216744 :       if (TYPE_DOMAIN (parm) != NULL_TREE)
   26874       375322 :         return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   26875       187661 :                                    TYPE_DOMAIN (arg), explain_p);
   26876              :       return unify_success (explain_p);
   26877              : 
   26878      7476567 :     case REAL_TYPE:
   26879      7476567 :     case COMPLEX_TYPE:
   26880      7476567 :     case VECTOR_TYPE:
   26881      7476567 :     case INTEGER_TYPE:
   26882      7476567 :     case BOOLEAN_TYPE:
   26883      7476567 :     case ENUMERAL_TYPE:
   26884      7476567 :     case VOID_TYPE:
   26885      7476567 :     case OPAQUE_TYPE:
   26886      7476567 :     case NULLPTR_TYPE:
   26887      7476567 :     case LANG_TYPE:
   26888      7476567 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26889      2341854 :         return unify_type_mismatch (explain_p, parm, arg);
   26890              : 
   26891              :       /* We have already checked cv-qualification at the top of the
   26892              :          function.  */
   26893      5134713 :       if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
   26894      4054736 :         return unify_type_mismatch (explain_p, parm, arg);
   26895              : 
   26896              :       /* As far as unification is concerned, this wins.  Later checks
   26897              :          will invalidate it if necessary.  */
   26898              :       return unify_success (explain_p);
   26899              : 
   26900              :       /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
   26901              :       /* Type INTEGER_CST can come from ordinary constant template args.  */
   26902     11877590 :     case INTEGER_CST:
   26903     11877590 :     case REAL_CST:
   26904     11877590 :       if (TREE_TYPE (arg) == NULL_TREE
   26905     11877590 :           || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
   26906           12 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   26907     11877578 :       while (CONVERT_EXPR_P (arg))
   26908            0 :         arg = TREE_OPERAND (arg, 0);
   26909              : 
   26910     11877578 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26911         3484 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   26912     11874094 :       return (simple_cst_equal (parm, arg)
   26913     11874094 :               ? unify_success (explain_p)
   26914     11874088 :               : unify_template_argument_mismatch (explain_p, parm, arg));
   26915              : 
   26916    139658584 :     case TREE_VEC:
   26917    139658584 :       {
   26918    139658584 :         int i, len, argslen;
   26919    139658584 :         int parm_variadic_p = 0;
   26920              : 
   26921    139658584 :         if (TREE_CODE (arg) != TREE_VEC)
   26922            0 :           return unify_template_argument_mismatch (explain_p, parm, arg);
   26923              : 
   26924    139658584 :         len = TREE_VEC_LENGTH (parm);
   26925    139658584 :         argslen = TREE_VEC_LENGTH (arg);
   26926              : 
   26927              :         /* Check for pack expansions in the parameters.  */
   26928    406484114 :         for (i = 0; i < len; ++i)
   26929              :           {
   26930    266825543 :             if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
   26931              :               {
   26932      7394617 :                 if (i == len - 1)
   26933              :                   /* We can unify against something with a trailing
   26934              :                      parameter pack.  */
   26935              :                   parm_variadic_p = 1;
   26936              :                 else
   26937              :                   /* [temp.deduct.type]/9: If the template argument list of
   26938              :                      P contains a pack expansion that is not the last
   26939              :                      template argument, the entire template argument list
   26940              :                      is a non-deduced context.  */
   26941              :                   return unify_success (explain_p);
   26942              :               }
   26943              :           }
   26944              : 
   26945              :         /* If we don't have enough arguments to satisfy the parameters
   26946              :            (not counting the pack expression at the end), or we have
   26947              :            too many arguments for a parameter list that doesn't end in
   26948              :            a pack expression, we can't unify.  */
   26949    139658571 :         if (parm_variadic_p
   26950    139658571 :             ? argslen < len - parm_variadic_p
   26951              :             : argslen != len)
   26952      3876202 :           return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
   26953              : 
   26954              :         /* Unify all of the parameters that precede the (optional)
   26955              :            pack expression.  */
   26956    333866486 :         for (i = 0; i < len - parm_variadic_p; ++i)
   26957              :           {
   26958    234410184 :             RECUR_AND_CHECK_FAILURE (tparms, targs,
   26959              :                                      TREE_VEC_ELT (parm, i),
   26960              :                                      TREE_VEC_ELT (arg, i),
   26961              :                                      UNIFY_ALLOW_NONE, explain_p);
   26962              :           }
   26963     99456302 :         if (parm_variadic_p)
   26964      7109931 :           return unify_pack_expansion (tparms, targs, parm, arg,
   26965              :                                        DEDUCE_EXACT,
   26966      7109931 :                                        /*subr=*/true, explain_p);
   26967              :         return unify_success (explain_p);
   26968              :       }
   26969              : 
   26970    419786578 :     case RECORD_TYPE:
   26971    419786578 :     case UNION_TYPE:
   26972    419786578 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   26973    215052613 :         return unify_type_mismatch (explain_p, parm, arg);
   26974              : 
   26975    204733965 :       if (TYPE_PTRMEMFUNC_P (parm))
   26976              :         {
   26977         2314 :           if (!TYPE_PTRMEMFUNC_P (arg))
   26978          264 :             return unify_type_mismatch (explain_p, parm, arg);
   26979              : 
   26980         6150 :           return unify (tparms, targs,
   26981         2050 :                         TYPE_PTRMEMFUNC_FN_TYPE (parm),
   26982         2050 :                         TYPE_PTRMEMFUNC_FN_TYPE (arg),
   26983         2050 :                         strict, explain_p);
   26984              :         }
   26985    204731651 :       else if (TYPE_PTRMEMFUNC_P (arg))
   26986          353 :         return unify_type_mismatch (explain_p, parm, arg);
   26987              : 
   26988    204731298 :       if (CLASSTYPE_TEMPLATE_INFO (parm))
   26989              :         {
   26990    202903050 :           tree t = NULL_TREE;
   26991              : 
   26992    202903050 :           if (strict_in & UNIFY_ALLOW_DERIVED)
   26993              :             {
   26994              :               /* First, we try to unify the PARM and ARG directly.  */
   26995    168616554 :               t = try_class_unification (tparms, targs,
   26996              :                                          parm, arg, explain_p);
   26997              : 
   26998    168616554 :               if (!t)
   26999              :                 {
   27000              :                   /* Fallback to the special case allowed in
   27001              :                      [temp.deduct.call]:
   27002              : 
   27003              :                        If P is a class, and P has the form
   27004              :                        template-id, then A can be a derived class of
   27005              :                        the deduced A.  Likewise, if P is a pointer to
   27006              :                        a class of the form template-id, A can be a
   27007              :                        pointer to a derived class pointed to by the
   27008              :                        deduced A.  */
   27009    154049006 :                   enum template_base_result r;
   27010    154049006 :                   r = get_template_base (tparms, targs, parm, arg,
   27011              :                                          explain_p, &t);
   27012              : 
   27013    154049006 :                   if (!t)
   27014              :                     {
   27015              :                       /* Don't give the derived diagnostic if we're
   27016              :                          already dealing with the same template.  */
   27017    153280288 :                       bool same_template
   27018    153280288 :                         = (CLASSTYPE_TEMPLATE_INFO (arg)
   27019    153280288 :                            && (CLASSTYPE_TI_TEMPLATE (parm)
   27020    101232420 :                                == CLASSTYPE_TI_TEMPLATE (arg)));
   27021    153280288 :                       return unify_no_common_base (explain_p && !same_template,
   27022    153280288 :                                                    r, parm, arg);
   27023              :                     }
   27024              :                 }
   27025              :             }
   27026     34286496 :           else if (CLASSTYPE_TEMPLATE_INFO (arg)
   27027     34286496 :                    && (CLASSTYPE_TI_TEMPLATE (parm)
   27028     33044313 :                        == CLASSTYPE_TI_TEMPLATE (arg)))
   27029              :             /* Perhaps PARM is something like S<U> and ARG is S<int>.
   27030              :                Then, we should unify `int' and `U'.  */
   27031     30486317 :             t = arg;
   27032              :           else
   27033              :             /* There's no chance of unification succeeding.  */
   27034    202903047 :             return unify_type_mismatch (explain_p, parm, arg);
   27035              : 
   27036     45822583 :           if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
   27037     45822580 :             return unify (tparms, targs,
   27038     45822580 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   27039     45822580 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)),
   27040     45822580 :                           UNIFY_ALLOW_NONE, explain_p);
   27041            3 :           gcc_checking_assert (t == arg);
   27042              :         }
   27043              : 
   27044      1828251 :       if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
   27045      1827066 :         return unify_type_mismatch (explain_p, parm, arg);
   27046              :       return unify_success (explain_p);
   27047              : 
   27048       296031 :     case METHOD_TYPE:
   27049       296031 :     case FUNCTION_TYPE:
   27050       296031 :       {
   27051       296031 :         unsigned int nargs;
   27052       296031 :         tree *args;
   27053       296031 :         tree a;
   27054       296031 :         unsigned int i;
   27055              : 
   27056       296031 :         if (TREE_CODE (arg) != TREE_CODE (parm))
   27057         1559 :           return unify_type_mismatch (explain_p, parm, arg);
   27058              : 
   27059              :         /* CV qualifications for methods can never be deduced, they must
   27060              :            match exactly.  We need to check them explicitly here,
   27061              :            because type_unification_real treats them as any other
   27062              :            cv-qualified parameter.  */
   27063       294472 :         if (TREE_CODE (parm) == METHOD_TYPE
   27064       296522 :             && (!check_cv_quals_for_unify
   27065         2050 :                 (UNIFY_ALLOW_NONE,
   27066              :                  class_of_this_parm (arg),
   27067              :                  class_of_this_parm (parm))))
   27068         1228 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   27069       293244 :         if (TREE_CODE (arg) == FUNCTION_TYPE
   27070       293244 :             && type_memfn_quals (parm) != type_memfn_quals (arg))
   27071         1128 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   27072       292116 :         if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
   27073          740 :           return unify_type_mismatch (explain_p, parm, arg);
   27074              : 
   27075       291376 :         RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
   27076              :                                  TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
   27077              : 
   27078       290575 :         nargs = list_length (TYPE_ARG_TYPES (arg));
   27079       290575 :         args = XALLOCAVEC (tree, nargs);
   27080       290575 :         for (a = TYPE_ARG_TYPES (arg), i = 0;
   27081       998297 :              a != NULL_TREE && a != void_list_node;
   27082       707722 :              a = TREE_CHAIN (a), ++i)
   27083       707722 :           args[i] = TREE_VALUE (a);
   27084       290575 :         nargs = i;
   27085              : 
   27086       290575 :         if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
   27087              :                                    args, nargs, 1, DEDUCE_EXACT,
   27088              :                                    NULL, explain_p))
   27089              :           return 1;
   27090              : 
   27091       290382 :         if (flag_noexcept_type)
   27092              :           {
   27093       286850 :             tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
   27094       286850 :             tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
   27095       286850 :             if (pspec == NULL_TREE) pspec = noexcept_false_spec;
   27096       286850 :             if (aspec == NULL_TREE) aspec = noexcept_false_spec;
   27097       573700 :             if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
   27098       573700 :                 && uses_template_parms (TREE_PURPOSE (pspec)))
   27099          576 :               RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
   27100              :                                        TREE_PURPOSE (aspec),
   27101              :                                        UNIFY_ALLOW_NONE, explain_p);
   27102              :             else
   27103              :               {
   27104       286274 :                 bool pn = nothrow_spec_p (pspec);
   27105       286274 :                 bool an = nothrow_spec_p (aspec);
   27106              :                 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
   27107              :                    /more/ noexcept, since function pointer conversions are the
   27108              :                    reverse of qualification conversions.  */
   27109       286274 :                 if (an == pn
   27110       238510 :                     || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   27111       238504 :                     || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   27112              :                   /* OK.  */;
   27113              :                 else
   27114           50 :                   return unify_type_mismatch (explain_p, parm, arg);
   27115              :               }
   27116              :           }
   27117       290332 :         if (flag_tm)
   27118              :           {
   27119              :             /* As for noexcept.  */
   27120           62 :             bool pn = tx_safe_fn_type_p (parm);
   27121           62 :             bool an = tx_safe_fn_type_p (arg);
   27122           62 :             if (an == pn
   27123            4 :                 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   27124            4 :                 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   27125              :               /* OK.  */;
   27126              :             else
   27127            1 :               return unify_type_mismatch (explain_p, parm, arg);
   27128              :           }
   27129              : 
   27130              :         return 0;
   27131              :       }
   27132              : 
   27133        43494 :     case OFFSET_TYPE:
   27134              :       /* Unify a pointer to member with a pointer to member function, which
   27135              :          deduces the type of the member as a function type. */
   27136        43494 :       if (TYPE_PTRMEMFUNC_P (arg))
   27137              :         {
   27138              :           /* Check top-level cv qualifiers */
   27139        19659 :           if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
   27140           24 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   27141              : 
   27142        19635 :           RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   27143              :                                    TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
   27144              :                                    UNIFY_ALLOW_NONE, explain_p);
   27145              : 
   27146              :           /* Determine the type of the function we are unifying against. */
   27147        19635 :           tree fntype = static_fn_type (arg);
   27148              : 
   27149        19635 :           return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
   27150              :         }
   27151              : 
   27152        23835 :       if (TREE_CODE (arg) != OFFSET_TYPE)
   27153        22253 :         return unify_type_mismatch (explain_p, parm, arg);
   27154         1582 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   27155              :                                TYPE_OFFSET_BASETYPE (arg),
   27156              :                                UNIFY_ALLOW_NONE, explain_p);
   27157         1582 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   27158         1582 :                     strict, explain_p);
   27159              : 
   27160            0 :     case CONST_DECL:
   27161              :       /* CONST_DECL should already have been folded to its DECL_INITIAL.  */
   27162            0 :       gcc_unreachable ();
   27163              : 
   27164         2305 :     case FIELD_DECL:
   27165         2305 :     case FUNCTION_DECL:
   27166         2305 :     case TEMPLATE_DECL:
   27167              :       /* Matched cases are handled by the ARG == PARM test above.  */
   27168         2305 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   27169              : 
   27170            0 :     case VAR_DECL:
   27171              :       /* We might get a variable as a non-type template argument in parm if the
   27172              :          corresponding parameter is type-dependent.  Make any necessary
   27173              :          adjustments based on whether arg is a reference.  */
   27174            0 :       if (CONSTANT_CLASS_P (arg))
   27175            0 :         parm = fold_non_dependent_expr (parm, complain);
   27176            0 :       else if (REFERENCE_REF_P (arg))
   27177              :         {
   27178            0 :           tree sub = TREE_OPERAND (arg, 0);
   27179            0 :           STRIP_NOPS (sub);
   27180            0 :           if (TREE_CODE (sub) == ADDR_EXPR)
   27181            0 :             arg = TREE_OPERAND (sub, 0);
   27182              :         }
   27183              :       /* Now use the normal expression code to check whether they match.  */
   27184            0 :       goto expr;
   27185              : 
   27186     13296415 :     case TYPE_ARGUMENT_PACK:
   27187     13296415 :     case NONTYPE_ARGUMENT_PACK:
   27188     13296415 :       return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
   27189     26592830 :                     ARGUMENT_PACK_ARGS (arg), strict, explain_p);
   27190              : 
   27191              :     case TYPEOF_TYPE:
   27192              :     case DECLTYPE_TYPE:
   27193              :     case TRAIT_TYPE:
   27194              :     case PACK_INDEX_TYPE:
   27195              :       /* These are non-deduced contexts.  */
   27196              :       return unify_success (explain_p);
   27197              : 
   27198              :     case ERROR_MARK:
   27199              :       /* Unification fails if we hit an error node.  */
   27200              :       return unify_invalid (explain_p);
   27201              : 
   27202           70 :     case INDIRECT_REF:
   27203           70 :       if (REFERENCE_REF_P (parm))
   27204              :         {
   27205           70 :           bool pexp = PACK_EXPANSION_P (arg);
   27206           70 :           if (pexp)
   27207            3 :             arg = PACK_EXPANSION_PATTERN (arg);
   27208           70 :           if (REFERENCE_REF_P (arg))
   27209           58 :             arg = TREE_OPERAND (arg, 0);
   27210           70 :           if (pexp)
   27211            3 :             arg = make_pack_expansion (arg, complain);
   27212           70 :           return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
   27213           70 :                         strict, explain_p);
   27214              :         }
   27215              :       /* FALLTHRU */
   27216              : 
   27217         1469 :     default:
   27218              :       /* An unresolved overload is a nondeduced context.  */
   27219         1469 :       if (is_overloaded_fn (parm) || type_unknown_p (parm))
   27220              :         return unify_success (explain_p);
   27221         1469 :       gcc_assert (EXPR_P (parm)
   27222              :                   || TREE_CODE (parm) == CONSTRUCTOR
   27223              :                   || TREE_CODE (parm) == LAMBDA_EXPR
   27224              :                   || TREE_CODE (parm) == TRAIT_EXPR);
   27225         1469 :     expr:
   27226              :       /* We must be looking at an expression.  This can happen with
   27227              :          something like:
   27228              : 
   27229              :            template <int I>
   27230              :            void foo(S<I>, S<I + 2>);
   27231              : 
   27232              :          or
   27233              : 
   27234              :            template<typename T>
   27235              :            void foo(A<T, T{}>);
   27236              : 
   27237              :          This is a "non-deduced context":
   27238              : 
   27239              :            [deduct.type]
   27240              : 
   27241              :            The non-deduced contexts are:
   27242              : 
   27243              :            --A non-type template argument or an array bound in which
   27244              :              a subexpression references a template parameter.
   27245              : 
   27246              :          In these cases, we assume deduction succeeded, but don't
   27247              :          actually infer any unifications.  */
   27248              : 
   27249         1469 :       if (!uses_template_parms (parm)
   27250         1469 :           && !template_args_equal (parm, arg))
   27251           11 :         return unify_expression_unequal (explain_p, parm, arg);
   27252              :       else
   27253              :         return unify_success (explain_p);
   27254              :     }
   27255              : }
   27256              : #undef RECUR_AND_CHECK_FAILURE
   27257              : 
   27258              : /* Note that DECL can be defined in this translation unit, if
   27259              :    required.  */
   27260              : 
   27261              : static void
   27262     23395136 : mark_definable (tree decl)
   27263              : {
   27264     23395136 :   tree clone;
   27265     23395136 :   DECL_NOT_REALLY_EXTERN (decl) = 1;
   27266     30085269 :   FOR_EACH_CLONE (clone, decl)
   27267      6690133 :     DECL_NOT_REALLY_EXTERN (clone) = 1;
   27268     23395136 : }
   27269              : 
   27270              : /* DECL is an explicit instantiation definition, ensure that it will
   27271              :    be written out here and that it won't clash with other instantiations
   27272              :    in other translation units.  */
   27273              : 
   27274              : void
   27275        31667 : setup_explicit_instantiation_definition_linkage (tree decl)
   27276              : {
   27277        31667 :   mark_definable (decl);
   27278        31667 :   mark_needed (decl);
   27279              :   /* Always make artificials weak.  */
   27280        31667 :   if (DECL_ARTIFICIAL (decl) && flag_weak)
   27281            0 :     comdat_linkage (decl);
   27282              :   /* We also want to put explicit instantiations in linkonce sections.  */
   27283        31667 :   else if (TREE_PUBLIC (decl))
   27284        31646 :     maybe_make_one_only (decl);
   27285        31667 : }
   27286              : 
   27287              : /* Called if RESULT is explicitly instantiated, or is a member of an
   27288              :    explicitly instantiated class.  */
   27289              : 
   27290              : void
   27291     24493544 : mark_decl_instantiated (tree result, int extern_p)
   27292              : {
   27293     24493544 :   SET_DECL_EXPLICIT_INSTANTIATION (result);
   27294              : 
   27295              :   /* consteval functions are never emitted.  */
   27296     24493544 :   if (TREE_CODE (result) == FUNCTION_DECL
   27297     48621559 :       && DECL_IMMEDIATE_FUNCTION_P (result))
   27298              :     return;
   27299              : 
   27300              :   /* For anonymous namespace we don't need to do anything.  */
   27301     24493538 :   if (decl_internal_context_p (result))
   27302              :     {
   27303          993 :       gcc_assert (!TREE_PUBLIC (result));
   27304              :       return;
   27305              :     }
   27306              : 
   27307     24492545 :   if (TREE_CODE (result) != FUNCTION_DECL)
   27308              :     /* The TREE_PUBLIC flag for function declarations will have been
   27309              :        set correctly by tsubst.  */
   27310       365521 :     TREE_PUBLIC (result) = 1;
   27311              : 
   27312     24492545 :   if (extern_p)
   27313              :     {
   27314     24460914 :       DECL_EXTERNAL (result) = 1;
   27315     24460914 :       DECL_NOT_REALLY_EXTERN (result) = 0;
   27316              :     }
   27317              :   else
   27318              :     {
   27319        31631 :       set_instantiating_module (result);
   27320        31631 :       setup_explicit_instantiation_definition_linkage (result);
   27321        31631 :       if (TREE_CODE (result) == FUNCTION_DECL
   27322        31631 :           && DECL_TEMPLATE_INSTANTIATED (result))
   27323              :         /* If the function has already been instantiated, clear DECL_EXTERNAL,
   27324              :            since start_preparsed_function wouldn't have if we had an earlier
   27325              :            extern explicit instantiation.  */
   27326          147 :         DECL_EXTERNAL (result) = 0;
   27327              :     }
   27328              : 
   27329              :   /* If EXTERN_P, then this function will not be emitted -- unless
   27330              :      followed by an explicit instantiation, at which point its linkage
   27331              :      will be adjusted.  If !EXTERN_P, then this function will be
   27332              :      emitted here.  In neither circumstance do we want
   27333              :      import_export_decl to adjust the linkage.  */
   27334     24492545 :   DECL_INTERFACE_KNOWN (result) = 1;
   27335              : }
   27336              : 
   27337              : /* Subroutine of more_specialized_fn: check whether TARGS is missing any
   27338              :    important template arguments.  If any are missing, we check whether
   27339              :    they're important by using error_mark_node for substituting into any
   27340              :    args that were used for partial ordering (the ones between ARGS and END)
   27341              :    and seeing if it bubbles up.  */
   27342              : 
   27343              : static bool
   27344      6209674 : check_undeduced_parms (tree targs, tree args, tree end)
   27345              : {
   27346      6209674 :   bool found = false;
   27347     19366937 :   for (tree& targ : tree_vec_range (targs))
   27348     13157263 :     if (targ == NULL_TREE)
   27349              :       {
   27350      1769416 :         found = true;
   27351      1769416 :         targ = error_mark_node;
   27352              :       }
   27353      6209674 :   if (found)
   27354              :     {
   27355      1768154 :       tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
   27356      1768154 :       if (substed == error_mark_node)
   27357           27 :         return true;
   27358              :     }
   27359              :   return false;
   27360              : }
   27361              : 
   27362              : /* Given two function templates PAT1 and PAT2, return:
   27363              : 
   27364              :    1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   27365              :    -1 if PAT2 is more specialized than PAT1.
   27366              :    0 if neither is more specialized.
   27367              : 
   27368              :    LEN indicates the number of parameters we should consider
   27369              :    (defaulted parameters should not be considered).
   27370              : 
   27371              :    The 1998 std underspecified function template partial ordering, and
   27372              :    DR214 addresses the issue.  We take pairs of arguments, one from
   27373              :    each of the templates, and deduce them against each other.  One of
   27374              :    the templates will be more specialized if all the *other*
   27375              :    template's arguments deduce against its arguments and at least one
   27376              :    of its arguments *does* *not* deduce against the other template's
   27377              :    corresponding argument.  Deduction is done as for class templates.
   27378              :    The arguments used in deduction have reference and top level cv
   27379              :    qualifiers removed.  Iff both arguments were originally reference
   27380              :    types *and* deduction succeeds in both directions, an lvalue reference
   27381              :    wins against an rvalue reference and otherwise the template
   27382              :    with the more cv-qualified argument wins for that pairing (if
   27383              :    neither is more cv-qualified, they both are equal).  Unlike regular
   27384              :    deduction, after all the arguments have been deduced in this way,
   27385              :    we do *not* verify the deduced template argument values can be
   27386              :    substituted into non-deduced contexts.
   27387              : 
   27388              :    The logic can be a bit confusing here, because we look at deduce1 and
   27389              :    targs1 to see if pat2 is at least as specialized, and vice versa; if we
   27390              :    can find template arguments for pat1 to make arg1 look like arg2, that
   27391              :    means that arg2 is at least as specialized as arg1.  */
   27392              : 
   27393              : int
   27394      4156959 : more_specialized_fn (tree pat1, tree pat2, int len)
   27395              : {
   27396      4156959 :   tree decl1 = DECL_TEMPLATE_RESULT (pat1);
   27397      4156959 :   tree decl2 = DECL_TEMPLATE_RESULT (pat2);
   27398      4156959 :   tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
   27399      4156959 :   tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
   27400      4156959 :   tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
   27401      4156959 :   tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
   27402      4156959 :   tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
   27403      4156959 :   tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
   27404      4156959 :   tree origs1, origs2;
   27405      4156959 :   bool lose1 = false;
   27406      4156959 :   bool lose2 = false;
   27407              : 
   27408              :   /* C++17 [temp.func.order]/3 (CWG532)
   27409              : 
   27410              :      If only one of the function templates M is a non-static member of some
   27411              :      class A, M is considered to have a new first parameter inserted in its
   27412              :      function parameter list. Given cv as the cv-qualifiers of M (if any), the
   27413              :      new parameter is of type "rvalue reference to cv A" if the optional
   27414              :      ref-qualifier of M is && or if M has no ref-qualifier and the first
   27415              :      parameter of the other template has rvalue reference type. Otherwise, the
   27416              :      new parameter is of type "lvalue reference to cv A".  */
   27417              : 
   27418      4156959 :   if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2))
   27419              :     {
   27420              :       /* Note C++20 DR2445 extended the above to static member functions, but
   27421              :          I think the old G++ behavior of just skipping the object
   27422              :          parameter when comparing to a static member function was better, so
   27423              :          let's stick with that for now.  This is CWG2834.  --jason 2023-12 */
   27424          567 :       if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1))
   27425              :         {
   27426           42 :           len--; /* LEN is the number of significant arguments for DECL1 */
   27427           42 :           args1 = TREE_CHAIN (args1);
   27428              :         }
   27429          525 :       else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2))
   27430           30 :         args2 = TREE_CHAIN (args2);
   27431              :     }
   27432      4156392 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   27433      4156392 :            && DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   27434              :     {
   27435              :       /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
   27436              :          would break e.g.  cpp1y/lambda-generic-variadic5.C.  */
   27437        18994 :       len--;
   27438        18994 :       args1 = TREE_CHAIN (args1);
   27439        18994 :       args2 = TREE_CHAIN (args2);
   27440              :     }
   27441      4137398 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   27442      4137398 :            || DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   27443              :     {
   27444              :       /* The other is a non-member or explicit object member function;
   27445              :          rewrite the implicit object parameter to a reference.  */
   27446          151 :       tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1;
   27447          151 :       tree &nsargs = ns == decl2 ? args2 : args1;
   27448          151 :       tree obtype = TREE_TYPE (TREE_VALUE (nsargs));
   27449              : 
   27450          151 :       nsargs = TREE_CHAIN (nsargs);
   27451              : 
   27452          151 :       cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns));
   27453          151 :       if (rqual == REF_QUAL_NONE)
   27454              :         {
   27455          103 :           tree otherfirst = ns == decl1 ? args2 : args1;
   27456          103 :           otherfirst = TREE_VALUE (otherfirst);
   27457          103 :           if (TREE_CODE (otherfirst) == REFERENCE_TYPE
   27458          103 :               && TYPE_REF_IS_RVALUE (otherfirst))
   27459              :             rqual = REF_QUAL_RVALUE;
   27460              :         }
   27461          151 :       obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE);
   27462          151 :       nsargs = tree_cons (NULL_TREE, obtype, nsargs);
   27463              :     }
   27464              : 
   27465              :   /* If only one is a conversion operator, they are unordered.  */
   27466      4156959 :   if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
   27467              :     return 0;
   27468              : 
   27469              :   /* Consider the return type for a conversion function */
   27470      4156945 :   if (DECL_CONV_FN_P (decl1))
   27471              :     {
   27472            9 :       args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
   27473            9 :       args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
   27474            9 :       len++;
   27475              :     }
   27476              : 
   27477      4156945 :   processing_template_decl++;
   27478              : 
   27479      4156945 :   origs1 = args1;
   27480      4156945 :   origs2 = args2;
   27481              : 
   27482      4156945 :   while (len--
   27483              :          /* Stop when an ellipsis is seen.  */
   27484     10538732 :          && args1 != NULL_TREE && args2 != NULL_TREE)
   27485              :     {
   27486      6412307 :       tree arg1 = TREE_VALUE (args1);
   27487      6412307 :       tree arg2 = TREE_VALUE (args2);
   27488      6412307 :       int deduce1, deduce2;
   27489      6412307 :       int quals1 = -1;
   27490      6412307 :       int quals2 = -1;
   27491      6412307 :       int ref1 = 0;
   27492      6412307 :       int ref2 = 0;
   27493              : 
   27494      6412307 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   27495         3341 :           && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27496              :         {
   27497              :           /* When both arguments are pack expansions, we need only
   27498              :              unify the patterns themselves.  */
   27499          494 :           arg1 = PACK_EXPANSION_PATTERN (arg1);
   27500          494 :           arg2 = PACK_EXPANSION_PATTERN (arg2);
   27501              : 
   27502              :           /* This is the last comparison we need to do.  */
   27503              :           len = 0;
   27504              :         }
   27505              : 
   27506      6412307 :       if (TYPE_REF_P (arg1))
   27507              :         {
   27508      4274840 :           ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
   27509      4274840 :           arg1 = TREE_TYPE (arg1);
   27510      4274840 :           quals1 = cp_type_quals (arg1);
   27511              :         }
   27512              : 
   27513      6412307 :       if (TYPE_REF_P (arg2))
   27514              :         {
   27515      4413304 :           ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
   27516      4413304 :           arg2 = TREE_TYPE (arg2);
   27517      4413304 :           quals2 = cp_type_quals (arg2);
   27518              :         }
   27519              : 
   27520      6412307 :       arg1 = TYPE_MAIN_VARIANT (arg1);
   27521      6412307 :       arg2 = TYPE_MAIN_VARIANT (arg2);
   27522              : 
   27523      6412307 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
   27524              :         {
   27525         2847 :           int i, len2 = remaining_arguments (args2);
   27526         2847 :           tree parmvec = make_tree_vec (1);
   27527         2847 :           tree argvec = make_tree_vec (len2);
   27528         2847 :           tree ta = args2;
   27529              : 
   27530              :           /* Setup the parameter vector, which contains only ARG1.  */
   27531         2847 :           TREE_VEC_ELT (parmvec, 0) = arg1;
   27532              : 
   27533              :           /* Setup the argument vector, which contains the remaining
   27534              :              arguments.  */
   27535         6676 :           for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
   27536         3829 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   27537              : 
   27538         2847 :           deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
   27539              :                                            argvec, DEDUCE_EXACT,
   27540              :                                            /*subr=*/true, /*explain_p=*/false)
   27541              :                      == 0);
   27542              : 
   27543              :           /* We cannot deduce in the other direction, because ARG1 is
   27544              :              a pack expansion but ARG2 is not.  */
   27545         2847 :           deduce2 = 0;
   27546              :         }
   27547      6409460 :       else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27548              :         {
   27549         1466 :           int i, len1 = remaining_arguments (args1);
   27550         1466 :           tree parmvec = make_tree_vec (1);
   27551         1466 :           tree argvec = make_tree_vec (len1);
   27552         1466 :           tree ta = args1;
   27553              : 
   27554              :           /* Setup the parameter vector, which contains only ARG1.  */
   27555         1466 :           TREE_VEC_ELT (parmvec, 0) = arg2;
   27556              : 
   27557              :           /* Setup the argument vector, which contains the remaining
   27558              :              arguments.  */
   27559         4253 :           for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
   27560         2787 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   27561              : 
   27562         1466 :           deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
   27563              :                                            argvec, DEDUCE_EXACT,
   27564              :                                            /*subr=*/true, /*explain_p=*/false)
   27565              :                      == 0);
   27566              : 
   27567              :           /* We cannot deduce in the other direction, because ARG2 is
   27568              :              a pack expansion but ARG1 is not.*/
   27569         1466 :           deduce1 = 0;
   27570              :         }
   27571              : 
   27572              :       else
   27573              :         {
   27574              :           /* The normal case, where neither argument is a pack
   27575              :              expansion.  */
   27576      6407994 :           deduce1 = (unify (tparms1, targs1, arg1, arg2,
   27577              :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   27578      6407994 :                      == 0);
   27579      6407994 :           deduce2 = (unify (tparms2, targs2, arg2, arg1,
   27580              :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   27581      6407994 :                      == 0);
   27582              :         }
   27583              : 
   27584              :       /* If we couldn't deduce arguments for tparms1 to make arg1 match
   27585              :          arg2, then arg2 is not as specialized as arg1.  */
   27586      6412307 :       if (!deduce1)
   27587              :         lose2 = true;
   27588      6412307 :       if (!deduce2)
   27589       111954 :         lose1 = true;
   27590              : 
   27591              :       /* "If, for a given type, deduction succeeds in both directions
   27592              :          (i.e., the types are identical after the transformations above)
   27593              :          and both P and A were reference types (before being replaced with
   27594              :          the type referred to above):
   27595              :          - if the type from the argument template was an lvalue reference and
   27596              :          the type from the parameter template was not, the argument type is
   27597              :          considered to be more specialized than the other; otherwise,
   27598              :          - if the type from the argument template is more cv-qualified
   27599              :          than the type from the parameter template (as described above),
   27600              :          the argument type is considered to be more specialized than the other;
   27601              :          otherwise,
   27602              :          - neither type is more specialized than the other."  */
   27603              : 
   27604      6412307 :       if (deduce1 && deduce2)
   27605              :         {
   27606      4302735 :           if (ref1 && ref2 && ref1 != ref2)
   27607              :             {
   27608            3 :               if (ref1 > ref2)
   27609              :                 lose1 = true;
   27610              :               else
   27611            3 :                 lose2 = true;
   27612              :             }
   27613      4302732 :           else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
   27614              :             {
   27615         1055 :               if ((quals1 & quals2) == quals2)
   27616           21 :                 lose2 = true;
   27617         1055 :               if ((quals1 & quals2) == quals1)
   27618         1034 :                 lose1 = true;
   27619              :             }
   27620              :         }
   27621              : 
   27622      6412307 :       if (lose1 && lose2)
   27623              :         /* We've failed to deduce something in either direction.
   27624              :            These must be unordered.  */
   27625              :         break;
   27626              : 
   27627      6381787 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   27628      6381748 :           || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   27629              :         /* We have already processed all of the arguments in our
   27630              :            handing of the pack expansion type.  */
   27631          187 :         len = 0;
   27632              : 
   27633      6381787 :       args1 = TREE_CHAIN (args1);
   27634      6381787 :       args2 = TREE_CHAIN (args2);
   27635              :     }
   27636              : 
   27637              :   /* "In most cases, all template parameters must have values in order for
   27638              :      deduction to succeed, but for partial ordering purposes a template
   27639              :      parameter may remain without a value provided it is not used in the
   27640              :      types being used for partial ordering."
   27641              : 
   27642              :      Thus, if we are missing any of the targs1 we need to substitute into
   27643              :      origs1, then pat2 is not as specialized as pat1.  This can happen when
   27644              :      there is a nondeduced context.  */
   27645      4156945 :   if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
   27646              :     lose2 = true;
   27647      4156945 :   if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
   27648              :     lose1 = true;
   27649              : 
   27650      4156945 :   processing_template_decl--;
   27651              : 
   27652              :   /* If both deductions succeed, the partial ordering selects the more
   27653              :      constrained template.  */
   27654              :   /* P2113: If the corresponding template-parameters of the
   27655              :      template-parameter-lists are not equivalent ([temp.over.link]) or if
   27656              :      the function parameters that positionally correspond between the two
   27657              :      templates are not of the same type, neither template is more
   27658              :      specialized than the other.  */
   27659      4156945 :   if (!lose1 && !lose2
   27660      2083234 :       && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
   27661      2083234 :                               DECL_TEMPLATE_PARMS (pat2))
   27662      6240057 :       && compparms (origs1, origs2))
   27663              :     {
   27664      2078106 :       int winner = more_constrained (decl1, decl2);
   27665      2078106 :       if (winner > 0)
   27666              :         lose2 = true;
   27667      1405974 :       else if (winner < 0)
   27668       307266 :         lose1 = true;
   27669              :     }
   27670              : 
   27671              :   /* All things being equal, if the next argument is a pack expansion
   27672              :      for one function but not for the other, prefer the
   27673              :      non-variadic function.  FIXME this is bogus; see c++/41958.  */
   27674      4156945 :   if (lose1 == lose2
   27675      1134368 :       && args1 && TREE_VALUE (args1)
   27676      5291307 :       && args2 && TREE_VALUE (args2))
   27677              :     {
   27678      1134353 :       lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
   27679      1134353 :       lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
   27680              :     }
   27681              : 
   27682      4156945 :   if (lose1 == lose2)
   27683              :     return 0;
   27684      3027859 :   else if (!lose1)
   27685              :     return 1;
   27686              :   else
   27687       383478 :     return -1;
   27688              : }
   27689              : 
   27690              : /* Determine which of two partial specializations of TMPL is more
   27691              :    specialized.
   27692              : 
   27693              :    PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
   27694              :    to the first partial specialization.  The TREE_PURPOSE is the
   27695              :    innermost set of template parameters for the partial
   27696              :    specialization.  PAT2 is similar, but for the second template.
   27697              : 
   27698              :    Return 1 if the first partial specialization is more specialized;
   27699              :    -1 if the second is more specialized; 0 if neither is more
   27700              :    specialized.
   27701              : 
   27702              :    See [temp.class.order] for information about determining which of
   27703              :    two templates is more specialized.  */
   27704              : 
   27705              : static int
   27706      1697250 : more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
   27707              : {
   27708      1697250 :   tree targs;
   27709      1697250 :   int winner = 0;
   27710      1697250 :   bool any_deductions = false;
   27711              : 
   27712      1697250 :   tree tmpl1 = TREE_VALUE (pat1);
   27713      1697250 :   tree tmpl2 = TREE_VALUE (pat2);
   27714      1697250 :   tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
   27715      1697250 :   tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
   27716              : 
   27717              :   /* Just like what happens for functions, if we are ordering between
   27718              :      different template specializations, we may encounter dependent
   27719              :      types in the arguments, and we need our dependency check functions
   27720              :      to behave correctly.  */
   27721      1697250 :   ++processing_template_decl;
   27722      1697250 :   targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
   27723      1697250 :   if (targs)
   27724              :     {
   27725       239403 :       --winner;
   27726       239403 :       any_deductions = true;
   27727              :     }
   27728              : 
   27729      1697250 :   targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
   27730      1697250 :   if (targs)
   27731              :     {
   27732      1465778 :       ++winner;
   27733      1465778 :       any_deductions = true;
   27734              :     }
   27735      1697250 :   --processing_template_decl;
   27736              : 
   27737              :   /* If both deductions succeed, the partial ordering selects the more
   27738              :      constrained template.  */
   27739      1697250 :   if (!winner && any_deductions)
   27740         8012 :     winner = more_constrained (tmpl1, tmpl2);
   27741              : 
   27742              :   /* In the case of a tie where at least one of the templates
   27743              :      has a parameter pack at the end, the template with the most
   27744              :      non-packed parameters wins.  */
   27745      1697250 :   if (winner == 0
   27746      1697250 :       && any_deductions
   27747      1697260 :       && (template_args_variadic_p (TREE_PURPOSE (pat1))
   27748           10 :           || template_args_variadic_p (TREE_PURPOSE (pat2))))
   27749              :     {
   27750            0 :       tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
   27751            0 :       tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
   27752            0 :       int len1 = TREE_VEC_LENGTH (args1);
   27753            0 :       int len2 = TREE_VEC_LENGTH (args2);
   27754              : 
   27755              :       /* We don't count the pack expansion at the end.  */
   27756            0 :       if (template_args_variadic_p (TREE_PURPOSE (pat1)))
   27757            0 :         --len1;
   27758            0 :       if (template_args_variadic_p (TREE_PURPOSE (pat2)))
   27759            0 :         --len2;
   27760              : 
   27761            0 :       if (len1 > len2)
   27762              :         return 1;
   27763            0 :       else if (len1 < len2)
   27764            0 :         return -1;
   27765              :     }
   27766              : 
   27767              :   return winner;
   27768              : }
   27769              : 
   27770              : /* Return the template arguments that will produce the function signature
   27771              :    DECL from the function template FN, with the explicit template
   27772              :    arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
   27773              :    also match.  Return NULL_TREE if no satisfactory arguments could be
   27774              :    found.  */
   27775              : 
   27776              : static tree
   27777     15282510 : get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
   27778              : {
   27779     15282510 :   int ntparms = DECL_NTPARMS (fn);
   27780     15282510 :   tree targs = make_tree_vec (ntparms);
   27781     15282510 :   tree decl_type = TREE_TYPE (decl);
   27782     15282510 :   tree decl_arg_types;
   27783     15282510 :   tree *args;
   27784     15282510 :   unsigned int nargs, ix;
   27785     15282510 :   tree arg;
   27786              : 
   27787     15282510 :   gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
   27788              : 
   27789              :   /* Never do unification on the 'this' parameter.  */
   27790     15282510 :   decl_arg_types = skip_artificial_parms_for (decl,
   27791     15282510 :                                               TYPE_ARG_TYPES (decl_type));
   27792              : 
   27793     15282510 :   nargs = list_length (decl_arg_types);
   27794     15282510 :   args = XALLOCAVEC (tree, nargs);
   27795     15282510 :   for (arg = decl_arg_types, ix = 0;
   27796     60038428 :        arg != NULL_TREE;
   27797     44755918 :        arg = TREE_CHAIN (arg), ++ix)
   27798     44755918 :     args[ix] = TREE_VALUE (arg);
   27799              : 
   27800     30565020 :   if (fn_type_unification (fn, explicit_args, targs,
   27801              :                            args, ix,
   27802            0 :                            (check_rettype || DECL_CONV_FN_P (fn)
   27803     15282510 :                             ? TREE_TYPE (decl_type) : NULL_TREE),
   27804              :                            DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
   27805              :                            /*explain_p=*/false,
   27806              :                            /*decltype*/false)
   27807     15282510 :       == error_mark_node)
   27808     12932062 :     return NULL_TREE;
   27809              : 
   27810              :   return targs;
   27811              : }
   27812              : 
   27813              : /* Return the innermost template arguments that, when applied to a partial
   27814              :    specialization SPEC_TMPL of TMPL, yield the ARGS.
   27815              : 
   27816              :    For example, suppose we have:
   27817              : 
   27818              :      template <class T, class U> struct S {};
   27819              :      template <class T> struct S<T*, int> {};
   27820              : 
   27821              :    Then, suppose we want to get `S<double*, int>'.  SPEC_TMPL will be the
   27822              :    partial specialization and the ARGS will be {double*, int}.  The resulting
   27823              :    vector will be {double}, indicating that `T' is bound to `double'.  */
   27824              : 
   27825              : static tree
   27826     54326209 : get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
   27827              : {
   27828     54326209 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
   27829     54326209 :   tree spec_args
   27830     54326209 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
   27831     54326209 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
   27832     54326209 :   tree deduced_args;
   27833     54326209 :   tree innermost_deduced_args;
   27834              : 
   27835     54326209 :   innermost_deduced_args = make_tree_vec (ntparms);
   27836    108652418 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   27837              :     {
   27838          122 :       deduced_args = copy_node (args);
   27839          244 :       SET_TMPL_ARGS_LEVEL (deduced_args,
   27840              :                            TMPL_ARGS_DEPTH (deduced_args),
   27841              :                            innermost_deduced_args);
   27842              :     }
   27843              :   else
   27844              :     deduced_args = innermost_deduced_args;
   27845              : 
   27846     54326209 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   27847     54326212 :  again:
   27848     54326212 :   if (unify (tparms, deduced_args,
   27849              :              INNERMOST_TEMPLATE_ARGS (spec_args),
   27850              :              INNERMOST_TEMPLATE_ARGS (args),
   27851              :              UNIFY_ALLOW_NONE, /*explain_p=*/false))
   27852              :     return NULL_TREE;
   27853              : 
   27854     69000432 :   for (i =  0; i < ntparms; ++i)
   27855     45117437 :     if (! TREE_VEC_ELT (innermost_deduced_args, i))
   27856              :       {
   27857           26 :         if (!tried_array_deduction)
   27858              :           {
   27859           19 :             try_array_deduction (tparms, innermost_deduced_args,
   27860              :                                  INNERMOST_TEMPLATE_ARGS (spec_args));
   27861           19 :             tried_array_deduction = true;
   27862           19 :             if (TREE_VEC_ELT (innermost_deduced_args, i))
   27863            3 :               goto again;
   27864              :           }
   27865              :         return NULL_TREE;
   27866              :       }
   27867              : 
   27868     23882995 :   if (!push_tinst_level (spec_tmpl, deduced_args))
   27869              :     {
   27870            0 :       excessive_deduction_depth = true;
   27871            0 :       return NULL_TREE;
   27872              :     }
   27873              : 
   27874              :   /* Verify that nondeduced template arguments agree with the type
   27875              :      obtained from argument deduction.
   27876              : 
   27877              :      For example:
   27878              : 
   27879              :        struct A { typedef int X; };
   27880              :        template <class T, class U> struct C {};
   27881              :        template <class T> struct C<T, typename T::X> {};
   27882              : 
   27883              :      Then with the instantiation `C<A, int>', we can deduce that
   27884              :      `T' is `A' but unify () does not check whether `typename T::X'
   27885              :      is `int'.  */
   27886     23882995 :   spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
   27887              : 
   27888     23882995 :   if (spec_args != error_mark_node)
   27889     23746719 :     spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
   27890              :                                        INNERMOST_TEMPLATE_ARGS (spec_args),
   27891              :                                        tmpl, tf_none, false);
   27892              : 
   27893     23882995 :   pop_tinst_level ();
   27894              : 
   27895     23882995 :   if (spec_args == error_mark_node
   27896              :       /* We only need to check the innermost arguments; the other
   27897              :          arguments will always agree.  */
   27898     23882995 :       || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
   27899              :                                      INNERMOST_TEMPLATE_ARGS (args)))
   27900              :     return NULL_TREE;
   27901              : 
   27902              :   /* Now that we have bindings for all of the template arguments,
   27903              :      ensure that the arguments deduced for the template template
   27904              :      parameters have compatible template parameter lists.  See the use
   27905              :      of template_template_parm_bindings_ok_p in fn_type_unification
   27906              :      for more information.  */
   27907     23746571 :   if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
   27908           12 :     return NULL_TREE;
   27909              : 
   27910              :   return deduced_args;
   27911              : }
   27912              : 
   27913              : // Compare two function templates T1 and T2 by deducing bindings
   27914              : // from one against the other. If both deductions succeed, compare
   27915              : // constraints to see which is more constrained.
   27916              : static int
   27917        64770 : more_specialized_inst (tree t1, tree t2)
   27918              : {
   27919        64770 :   int fate = 0;
   27920        64770 :   int count = 0;
   27921              : 
   27922        64770 :   if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
   27923              :     {
   27924          195 :       --fate;
   27925          195 :       ++count;
   27926              :     }
   27927              : 
   27928        64770 :   if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
   27929              :     {
   27930        43182 :       ++fate;
   27931        43182 :       ++count;
   27932              :     }
   27933              : 
   27934              :   // If both deductions succeed, then one may be more constrained.
   27935        64770 :   if (count == 2 && fate == 0)
   27936          126 :     fate = more_constrained (t1, t2);
   27937              : 
   27938        64770 :   return fate;
   27939              : }
   27940              : 
   27941              : /* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
   27942              :    Return the TREE_LIST node with the most specialized template, if
   27943              :    any.  If there is no most specialized template, the error_mark_node
   27944              :    is returned.
   27945              : 
   27946              :    Note that this function does not look at, or modify, the
   27947              :    TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
   27948              :    returned is one of the elements of INSTANTIATIONS, callers may
   27949              :    store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
   27950              :    and retrieve it from the value returned.  */
   27951              : 
   27952              : tree
   27953        98062 : most_specialized_instantiation (tree templates)
   27954              : {
   27955        98062 :   tree fn, champ;
   27956              : 
   27957        98062 :   ++processing_template_decl;
   27958              : 
   27959        98062 :   champ = templates;
   27960       119701 :   for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
   27961              :     {
   27962        21678 :       gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
   27963        21678 :       int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
   27964        21678 :       if (fate == -1)
   27965              :         champ = fn;
   27966        21546 :       else if (!fate)
   27967              :         {
   27968              :           /* Equally specialized, move to next function.  If there
   27969              :              is no next function, nothing's most specialized.  */
   27970        21519 :           fn = TREE_CHAIN (fn);
   27971        21519 :           champ = fn;
   27972        21519 :           if (!fn)
   27973              :             break;
   27974              :         }
   27975              :     }
   27976              : 
   27977        98062 :   if (champ)
   27978              :     /* Now verify that champ is better than everything earlier in the
   27979              :        instantiation list.  */
   27980       141115 :     for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
   27981        43092 :       if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
   27982              :       {
   27983              :         champ = NULL_TREE;
   27984              :         break;
   27985              :       }
   27986              :     }
   27987              : 
   27988        98062 :   processing_template_decl--;
   27989              : 
   27990        98062 :   if (!champ)
   27991           39 :     return error_mark_node;
   27992              : 
   27993              :   return champ;
   27994              : }
   27995              : 
   27996              : /* If DECL is a specialization of some template, return the most
   27997              :    general such template.  Otherwise, returns NULL_TREE.
   27998              : 
   27999              :    For example, given:
   28000              : 
   28001              :      template <class T> struct S { template <class U> void f(U); };
   28002              : 
   28003              :    if TMPL is `template <class U> void S<int>::f(U)' this will return
   28004              :    the full template.  This function will not trace past partial
   28005              :    specializations, however.  For example, given in addition:
   28006              : 
   28007              :      template <class T> struct S<T*> { template <class U> void f(U); };
   28008              : 
   28009              :    if TMPL is `template <class U> void S<int*>::f(U)' this will return
   28010              :    `template <class T> template <class U> S<T*>::f(U)'.  */
   28011              : 
   28012              : tree
   28013   4629858061 : most_general_template (const_tree decl)
   28014              : {
   28015   4629858061 :   if (TREE_CODE (decl) != TEMPLATE_DECL)
   28016              :     {
   28017    939615825 :       if (tree tinfo = get_template_info (decl))
   28018    594660489 :         decl = TI_TEMPLATE (tinfo);
   28019              :       /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
   28020              :          template friend, or a FIELD_DECL for a capture pack.  */
   28021    939615825 :       if (TREE_CODE (decl) != TEMPLATE_DECL)
   28022              :         return NULL_TREE;
   28023              :     }
   28024              : 
   28025   4284896589 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   28026       564073 :     return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl));
   28027              : 
   28028              :   /* Look for more and more general templates.  */
   28029   4331123205 :   while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
   28030              :     {
   28031              :       /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
   28032              :          (See cp-tree.h for details.)  */
   28033     46791001 :       if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
   28034              :         break;
   28035              : 
   28036     93582002 :       if (CLASS_TYPE_P (TREE_TYPE (decl))
   28037      8784318 :           && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
   28038     54544785 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   28039              :         break;
   28040              : 
   28041              :       /* Stop if we run into an explicitly specialized class template.  */
   28042     93581378 :       if (!DECL_NAMESPACE_SCOPE_P (decl)
   28043     43995333 :           && DECL_CONTEXT (decl)
   28044     90786022 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
   28045              :         break;
   28046              : 
   28047     46790689 :       decl = DECL_TI_TEMPLATE (decl);
   28048              :     }
   28049              : 
   28050              :   return const_cast<tree> (decl);
   28051              : }
   28052              : 
   28053              : /* Return the most specialized of the template partial specializations
   28054              :    which can produce TARGET, a specialization of some class or variable
   28055              :    template.  The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
   28056              :    TEMPLATE_DECL node corresponding to the partial specialization, while
   28057              :    the TI_ARGS is the set of template arguments that must be substituted
   28058              :    into the template pattern in order to generate TARGET.  The result is
   28059              :    cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
   28060              :    RECHECKING is true.
   28061              : 
   28062              :    If the choice of partial specialization is ambiguous, a diagnostic
   28063              :    is issued, and the error_mark_node is returned.  If there are no
   28064              :    partial specializations matching TARGET, then NULL_TREE is
   28065              :    returned, indicating that the primary template should be used.  */
   28066              : 
   28067              : tree
   28068     91405869 : most_specialized_partial_spec (tree target, tsubst_flags_t complain,
   28069              :                                bool rechecking /* = false */)
   28070              : {
   28071     91405869 :   tree tinfo = NULL_TREE;
   28072     91405869 :   tree tmpl, args, decl;
   28073     91405869 :   if (TYPE_P (target))
   28074              :     {
   28075     43355327 :       tinfo = CLASSTYPE_TEMPLATE_INFO (target);
   28076     43355327 :       tmpl = TI_TEMPLATE (tinfo);
   28077     43355327 :       args = TI_ARGS (tinfo);
   28078     43355327 :       decl = TYPE_NAME (target);
   28079              :     }
   28080     48050542 :   else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
   28081              :     {
   28082     13475756 :       tmpl = TREE_OPERAND (target, 0);
   28083     13475756 :       args = TREE_OPERAND (target, 1);
   28084     13475756 :       decl = DECL_TEMPLATE_RESULT (tmpl);
   28085              :     }
   28086     34574786 :   else if (VAR_P (target))
   28087              :     {
   28088     34574786 :       tinfo = DECL_TEMPLATE_INFO (target);
   28089     34574786 :       tmpl = TI_TEMPLATE (tinfo);
   28090     34574786 :       args = TI_ARGS (tinfo);
   28091     34574786 :       decl = target;
   28092              :     }
   28093              :   else
   28094            0 :     gcc_unreachable ();
   28095              : 
   28096     91405869 :   if (!PRIMARY_TEMPLATE_P (tmpl))
   28097              :     return NULL_TREE;
   28098              : 
   28099     81386572 :   if (!rechecking
   28100     81386572 :       && tinfo
   28101     81386572 :       && (VAR_P (target) || COMPLETE_TYPE_P (target)))
   28102     26943261 :     return TI_PARTIAL_INFO (tinfo);
   28103              : 
   28104     54443311 :   tree main_tmpl = most_general_template (tmpl);
   28105     54443311 :   tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
   28106     54443311 :   if (!specs)
   28107              :     /* There are no partial specializations of this template.  */
   28108              :     return NULL_TREE;
   28109              : 
   28110     14973089 :   push_access_scope_guard pas (decl);
   28111     14973089 :   deferring_access_check_sentinel acs (dk_no_deferred);
   28112              : 
   28113              :   /* For determining which partial specialization to use, only the
   28114              :      innermost args are interesting.  */
   28115     14973089 :   tree outer_args = NULL_TREE;
   28116     29946178 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   28117              :     {
   28118       309193 :       outer_args = strip_innermost_template_args (args, 1);
   28119       309193 :       args = INNERMOST_TEMPLATE_ARGS (args);
   28120              :     }
   28121              : 
   28122              :   /* The caller hasn't called push_to_top_level yet, but we need
   28123              :      get_partial_spec_bindings to be done in non-template context so that we'll
   28124              :      fully resolve everything.  */
   28125     14973089 :   processing_template_decl_sentinel ptds;
   28126              : 
   28127     14973089 :   tree list = NULL_TREE;
   28128     57816603 :   for (tree t = specs; t; t = TREE_CHAIN (t))
   28129              :     {
   28130     42843520 :       const tree ospec_tmpl = TREE_VALUE (t);
   28131              : 
   28132     42843520 :       tree spec_tmpl;
   28133     42843520 :       if (outer_args)
   28134              :         {
   28135              :           /* Substitute in the template args from the enclosing class.  */
   28136       369821 :           ++processing_template_decl;
   28137       369821 :           spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
   28138       369821 :           --processing_template_decl;
   28139       369821 :           if (spec_tmpl == error_mark_node)
   28140              :             return error_mark_node;
   28141              :         }
   28142              :       else
   28143              :         spec_tmpl = ospec_tmpl;
   28144              : 
   28145     42843514 :       tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
   28146     42843514 :       if (spec_args)
   28147              :         {
   28148     13953200 :           if (outer_args)
   28149       263107 :             spec_args = add_to_template_args (outer_args, spec_args);
   28150              : 
   28151              :           /* Keep the candidate only if its constraints are satisfied.  */
   28152     13953200 :           if (constraints_satisfied_p (ospec_tmpl, spec_args))
   28153     11476938 :             list = tree_cons (spec_args, ospec_tmpl, list);
   28154              :         }
   28155              :     }
   28156              : 
   28157     14973083 :   if (! list)
   28158              :     return NULL_TREE;
   28159              : 
   28160     10126084 :   tree champ = list;
   28161     10126084 :   bool ambiguous_p = false;
   28162     11476847 :   for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
   28163              :     {
   28164      1350775 :       int fate = more_specialized_partial_spec (tmpl, champ, t);
   28165      1350775 :       if (fate == 1)
   28166              :         ;
   28167              :       else
   28168              :         {
   28169       231652 :           if (fate == 0)
   28170              :             {
   28171           91 :               t = TREE_CHAIN (t);
   28172           91 :               if (! t)
   28173              :                 {
   28174              :                   ambiguous_p = true;
   28175              :                   break;
   28176              :                 }
   28177              :             }
   28178              :           champ = t;
   28179              :         }
   28180              :     }
   28181              : 
   28182     10126084 :   if (!ambiguous_p)
   28183     10472544 :     for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
   28184              :       {
   28185       346475 :         int fate = more_specialized_partial_spec (tmpl, champ, t);
   28186       346475 :         if (fate != 1)
   28187              :           {
   28188              :             ambiguous_p = true;
   28189              :             break;
   28190              :           }
   28191              :       }
   28192              : 
   28193     10126072 :   if (ambiguous_p)
   28194              :     {
   28195           15 :       const char *str;
   28196           15 :       char *spaces = NULL;
   28197           15 :       if (!(complain & tf_error))
   28198            3 :         return error_mark_node;
   28199           12 :       auto_diagnostic_group d;
   28200           12 :       if (TYPE_P (target))
   28201            9 :         error ("ambiguous template instantiation for %q#T", target);
   28202              :       else
   28203            3 :         error ("ambiguous template instantiation for %q#D", target);
   28204           12 :       str = ngettext ("candidate is:", "candidates are:", list_length (list));
   28205           57 :       for (tree t = list; t; t = TREE_CHAIN (t))
   28206              :         {
   28207           33 :           tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
   28208           66 :           inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
   28209              :                   "%s %#qS", spaces ? spaces : str, subst);
   28210           33 :           spaces = spaces ? spaces : get_spaces (str);
   28211              :         }
   28212           12 :       free (spaces);
   28213           12 :       return error_mark_node;
   28214           12 :     }
   28215              : 
   28216     10126069 :   tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
   28217     10126069 :   if (!rechecking && tinfo)
   28218      9131456 :     TI_PARTIAL_INFO (tinfo) = result;
   28219              :   return result;
   28220     29946178 : }
   28221              : 
   28222              : /* Explicitly instantiate DECL.  */
   28223              : 
   28224              : void
   28225      2061059 : do_decl_instantiation (tree decl, tree storage)
   28226              : {
   28227      2061059 :   tree result = NULL_TREE;
   28228      2061059 :   int extern_p = 0;
   28229              : 
   28230      2061059 :   if (!decl || decl == error_mark_node)
   28231              :     /* An error occurred, for which grokdeclarator has already issued
   28232              :        an appropriate message.  */
   28233              :     return;
   28234      2060830 :   else if (! DECL_LANG_SPECIFIC (decl))
   28235              :     {
   28236            0 :       error ("explicit instantiation of non-template %q#D", decl);
   28237            0 :       return;
   28238              :     }
   28239              : 
   28240      2060830 :   bool var_templ = (DECL_TEMPLATE_INFO (decl)
   28241      2060830 :                     && variable_template_p (DECL_TI_TEMPLATE (decl)));
   28242              : 
   28243      2060830 :   if (VAR_P (decl) && !var_templ)
   28244              :     {
   28245              :       /* There is an asymmetry here in the way VAR_DECLs and
   28246              :          FUNCTION_DECLs are handled by grokdeclarator.  In the case of
   28247              :          the latter, the DECL we get back will be marked as a
   28248              :          template instantiation, and the appropriate
   28249              :          DECL_TEMPLATE_INFO will be set up.  This does not happen for
   28250              :          VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
   28251              :          should handle VAR_DECLs as it currently handles
   28252              :          FUNCTION_DECLs.  */
   28253           68 :       if (!DECL_CLASS_SCOPE_P (decl))
   28254              :         {
   28255            0 :           error ("%qD is not a static data member of a class template", decl);
   28256            0 :           return;
   28257              :         }
   28258           68 :       result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
   28259           68 :       if (!result || !VAR_P (result))
   28260              :         {
   28261            0 :           error ("no matching template for %qD found", decl);
   28262            0 :           return;
   28263              :         }
   28264           68 :       if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
   28265              :         {
   28266            9 :           error ("type %qT for explicit instantiation %qD does not match "
   28267            3 :                  "declared type %qT", TREE_TYPE (result), decl,
   28268            3 :                  TREE_TYPE (decl));
   28269            3 :           return;
   28270              :         }
   28271              :     }
   28272      2060762 :   else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
   28273              :     {
   28274            0 :       error ("explicit instantiation of %q#D", decl);
   28275            0 :       return;
   28276              :     }
   28277              :   else
   28278              :     result = decl;
   28279              : 
   28280              :   /* Check for various error cases.  Note that if the explicit
   28281              :      instantiation is valid the RESULT will currently be marked as an
   28282              :      *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
   28283              :      until we get here.  */
   28284              : 
   28285      2060827 :   if (DECL_TEMPLATE_SPECIALIZATION (result))
   28286              :     {
   28287              :       /* DR 259 [temp.spec].
   28288              : 
   28289              :          Both an explicit instantiation and a declaration of an explicit
   28290              :          specialization shall not appear in a program unless the explicit
   28291              :          instantiation follows a declaration of the explicit specialization.
   28292              : 
   28293              :          For a given set of template parameters, if an explicit
   28294              :          instantiation of a template appears after a declaration of an
   28295              :          explicit specialization for that template, the explicit
   28296              :          instantiation has no effect.  */
   28297              :       return;
   28298              :     }
   28299      2021494 :   else if (DECL_EXPLICIT_INSTANTIATION (result))
   28300              :     {
   28301              :       /* [temp.spec]
   28302              : 
   28303              :          No program shall explicitly instantiate any template more
   28304              :          than once.
   28305              : 
   28306              :          We check DECL_NOT_REALLY_EXTERN so as not to complain when
   28307              :          the first instantiation was `extern' and the second is not,
   28308              :          and EXTERN_P for the opposite case.  */
   28309          864 :       if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
   28310            6 :         permerror (input_location, "duplicate explicit instantiation of %q#D", result);
   28311              :       /* If an "extern" explicit instantiation follows an ordinary
   28312              :          explicit instantiation, the template is instantiated.  */
   28313              :       if (extern_p)
   28314              :         return;
   28315              :     }
   28316      2020630 :   else if (!DECL_IMPLICIT_INSTANTIATION (result))
   28317              :     {
   28318            0 :       error ("no matching template for %qD found", result);
   28319            0 :       return;
   28320              :     }
   28321      2020630 :   else if (!DECL_TEMPLATE_INFO (result))
   28322              :     {
   28323            0 :       permerror (input_location, "explicit instantiation of non-template %q#D", result);
   28324            0 :       return;
   28325              :     }
   28326              : 
   28327      2021494 :   if (storage == NULL_TREE)
   28328              :     ;
   28329      2017758 :   else if (storage == ridpointers[(int) RID_EXTERN])
   28330              :     {
   28331      2017758 :       if (cxx_dialect == cxx98 && pedantic)
   28332         7392 :         pedwarn (input_location, OPT_Wc__11_extensions,
   28333              :                  "ISO C++ 1998 forbids the use of %<extern%> on explicit "
   28334              :                  "instantiations");
   28335              :       extern_p = 1;
   28336              :     }
   28337              :   else
   28338            0 :     error ("storage class %qD applied to template instantiation", storage);
   28339              : 
   28340      2021494 :   check_explicit_instantiation_namespace (result);
   28341      2021494 :   mark_decl_instantiated (result, extern_p);
   28342      2021494 :   if (! extern_p)
   28343         3736 :     instantiate_decl (result, /*defer_ok=*/true,
   28344              :                       /*expl_inst_class_mem_p=*/false);
   28345              : }
   28346              : 
   28347              : static void
   28348       943974 : mark_class_instantiated (tree t, int extern_p)
   28349              : {
   28350       943974 :   SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
   28351       943974 :   SET_CLASSTYPE_INTERFACE_KNOWN (t);
   28352       943974 :   CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
   28353       943974 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
   28354       943974 :   if (! extern_p)
   28355              :     {
   28356         2766 :       CLASSTYPE_DEBUG_REQUESTED (t) = 1;
   28357         2766 :       rest_of_type_compilation (t, 1);
   28358              :     }
   28359       943974 : }
   28360              : 
   28361              : /* Perform an explicit instantiation of template class T.  STORAGE, if
   28362              :    non-null, is the RID for extern, inline or static.  COMPLAIN is
   28363              :    nonzero if this is called from the parser, zero if called recursively,
   28364              :    since the standard is unclear (as detailed below).  */
   28365              : 
   28366              : void
   28367      1008997 : do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
   28368              : {
   28369      1008997 :   if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
   28370              :     {
   28371            3 :       if (tree ti = TYPE_TEMPLATE_INFO (t))
   28372            0 :         error ("explicit instantiation of non-class template %qD",
   28373            0 :                TI_TEMPLATE (ti));
   28374              :       else
   28375            3 :         error ("explicit instantiation of non-template type %qT", t);
   28376              :       return;
   28377              :     }
   28378              : 
   28379      1008994 :   complete_type (t);
   28380              : 
   28381      1008994 :   if (!COMPLETE_TYPE_P (t))
   28382              :     {
   28383          320 :       if (complain & tf_error)
   28384            0 :         error ("explicit instantiation of %q#T before definition of template",
   28385              :                t);
   28386              :       return;
   28387              :     }
   28388              : 
   28389              :   /* At most one of these will be true.  */
   28390      1008674 :   bool extern_p = false;
   28391      1008674 :   bool nomem_p = false;
   28392      1008674 :   bool static_p = false;
   28393              : 
   28394      1008674 :   if (storage != NULL_TREE)
   28395              :     {
   28396      1005869 :       if (storage == ridpointers[(int) RID_EXTERN])
   28397              :         {
   28398      1005844 :           if (cxx_dialect == cxx98 && pedantic)
   28399         2563 :             pedwarn (input_location, OPT_Wc__11_extensions,
   28400              :                      "ISO C++ 1998 forbids the use of %<extern%> on "
   28401              :                      "explicit instantiations");
   28402              :         }
   28403              :       else
   28404           25 :         pedwarn (input_location, OPT_Wpedantic,
   28405              :                  "ISO C++ forbids the use of %qE"
   28406              :                  " on explicit instantiations", storage);
   28407              : 
   28408      1005869 :       if (storage == ridpointers[(int) RID_INLINE])
   28409              :         nomem_p = true;
   28410      1005847 :       else if (storage == ridpointers[(int) RID_EXTERN])
   28411              :         extern_p = true;
   28412            3 :       else if (storage == ridpointers[(int) RID_STATIC])
   28413              :         static_p = true;
   28414              :       else
   28415            0 :         error ("storage class %qD applied to template instantiation",
   28416              :                storage);
   28417              :     }
   28418              : 
   28419      1008674 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
   28420              :     /* DR 259 [temp.spec].
   28421              : 
   28422              :        Both an explicit instantiation and a declaration of an explicit
   28423              :        specialization shall not appear in a program unless the
   28424              :        explicit instantiation follows a declaration of the explicit
   28425              :        specialization.
   28426              : 
   28427              :        For a given set of template parameters, if an explicit
   28428              :        instantiation of a template appears after a declaration of an
   28429              :        explicit specialization for that template, the explicit
   28430              :        instantiation has no effect.  */
   28431              :     return;
   28432              : 
   28433       943977 :   if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
   28434              :     {
   28435              :       /* We've already instantiated the template.  */
   28436              : 
   28437              :       /* [temp.spec]
   28438              : 
   28439              :          No program shall explicitly instantiate any template more
   28440              :          than once.
   28441              : 
   28442              :          If EXTERN_P then this is ok.  */
   28443            3 :       if (!extern_p && (complain & tf_error))
   28444            3 :         permerror (input_location,
   28445              :                    "duplicate explicit instantiation of %q#T", t);
   28446              : 
   28447              :       return;
   28448              :     }
   28449              : 
   28450       943974 :   check_explicit_instantiation_namespace (TYPE_NAME (t));
   28451       943974 :   mark_class_instantiated (t, extern_p);
   28452              : 
   28453       943974 :   if (nomem_p)
   28454              :     return;
   28455              : 
   28456              :   /* In contrast to implicit instantiation, where only the
   28457              :      declarations, and not the definitions, of members are
   28458              :      instantiated, we have here:
   28459              : 
   28460              :          [temp.explicit]
   28461              : 
   28462              :          An explicit instantiation that names a class template
   28463              :          specialization is also an explicit instantiation of the same
   28464              :          kind (declaration or definition) of each of its members (not
   28465              :          including members inherited from base classes and members
   28466              :          that are templates) that has not been previously explicitly
   28467              :          specialized in the translation unit containing the explicit
   28468              :          instantiation, provided that the associated constraints, if
   28469              :          any, of that member are satisfied by the template arguments
   28470              :          of the explicit instantiation.  */
   28471     37163319 :   for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
   28472     36219370 :     if ((VAR_P (fld)
   28473     35854104 :          || (TREE_CODE (fld) == FUNCTION_DECL
   28474     24110546 :              && !static_p
   28475     24110546 :              && user_provided_p (fld)))
   28476     22818914 :         && DECL_TEMPLATE_INSTANTIATION (fld)
   28477     58692395 :         && constraints_satisfied_p (fld))
   28478              :       {
   28479     22472050 :         mark_decl_instantiated (fld, extern_p);
   28480     22472050 :         if (! extern_p)
   28481        28894 :           instantiate_decl (fld, /*defer_ok=*/true,
   28482              :                             /*expl_inst_class_mem_p=*/true);
   28483              :       }
   28484     13747320 :     else if (DECL_IMPLICIT_TYPEDEF_P (fld))
   28485              :       {
   28486       187605 :         tree type = TREE_TYPE (fld);
   28487              : 
   28488       166940 :         if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   28489       354528 :             && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
   28490       166923 :           do_type_instantiation (type, storage, 0);
   28491              :       }
   28492              : }
   28493              : 
   28494              : /* Return true if T contains any LAMBDA_EXPRs.  */
   28495              : 
   28496              : bool
   28497    198632455 : any_lambdas_p (tree t)
   28498              : {
   28499    198632455 :   walk_tree_fn find_lambda = [](tree *tp, int *, void *)
   28500              :     {
   28501              :       if (TREE_CODE (*tp) == LAMBDA_EXPR)
   28502              :         return *tp;
   28503              :       return NULL_TREE;
   28504              :     };
   28505              : 
   28506    198632455 :   return !!cp_walk_tree_without_duplicates (&t, find_lambda, nullptr);
   28507              : }
   28508              : 
   28509              : /* Given a function DECL, which is a specialization of TMPL, modify
   28510              :    DECL to be a re-instantiation of TMPL with the same template
   28511              :    arguments.  TMPL should be the template into which tsubst'ing
   28512              :    should occur for DECL, not the most general template.
   28513              : 
   28514              :    One reason for doing this is a scenario like this:
   28515              : 
   28516              :      template <class T>
   28517              :      void f(const T&, int i);
   28518              : 
   28519              :      void g() { f(3, 7); }
   28520              : 
   28521              :      template <class T>
   28522              :      void f(const T& t, const int i) { }
   28523              : 
   28524              :    Note that when the template is first instantiated, with
   28525              :    instantiate_template, the resulting DECL will have no name for the
   28526              :    first parameter, and the wrong type for the second.  So, when we go
   28527              :    to instantiate the DECL, we regenerate it.  */
   28528              : 
   28529              : static void
   28530     39161764 : regenerate_decl_from_template (tree decl, tree tmpl, tree args)
   28531              : {
   28532              :   /* The arguments used to instantiate DECL, from the most general
   28533              :      template.  */
   28534     39161764 :   tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
   28535              : 
   28536              :   /* Make sure that we can see identifiers, and compute access correctly.  */
   28537     39161764 :   push_access_scope (decl);
   28538              : 
   28539     39161764 :   if (TREE_CODE (decl) == FUNCTION_DECL)
   28540              :     {
   28541     23660350 :       tree specs;
   28542     23660350 :       int args_depth;
   28543     23660350 :       int parms_depth;
   28544              : 
   28545              :       /* Don't bother with this for unique friends that can't be redeclared and
   28546              :          might change type if regenerated (PR69836).  */
   28547     23660350 :       if (DECL_UNIQUE_FRIEND_P (decl))
   28548        70626 :         goto done;
   28549              : 
   28550              :       /* A template with a lambda in the signature also changes type if
   28551              :          regenerated (PR119401).  */
   28552     23589724 :       if (any_lambdas_p (TREE_TYPE (tmpl)))
   28553           42 :         goto done;
   28554              : 
   28555              :       /* Use the source location of the definition.  */
   28556     23589682 :       DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
   28557              : 
   28558     47179364 :       args_depth = TMPL_ARGS_DEPTH (args);
   28559     23589682 :       parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   28560     23589682 :       if (args_depth > parms_depth)
   28561            0 :         args = get_innermost_template_args (args, parms_depth);
   28562              : 
   28563              :       /* Instantiate a dynamic exception-specification.  noexcept will be
   28564              :          handled below.  */
   28565     23589682 :       if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
   28566      9839881 :         if (TREE_VALUE (raises))
   28567              :           {
   28568           22 :             specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
   28569              :                                                     args, tf_error, NULL_TREE,
   28570              :                                                     /*defer_ok*/false);
   28571           22 :             if (specs && specs != error_mark_node)
   28572           21 :               TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
   28573              :                                                           specs);
   28574              :           }
   28575              : 
   28576              :       /* Merge parameter declarations.  */
   28577     23589682 :       if (tree pattern_parm
   28578     23589682 :           = skip_artificial_parms_for (code_pattern,
   28579     23589682 :                                        DECL_ARGUMENTS (code_pattern)))
   28580              :         {
   28581     16309475 :           tree *p = &DECL_ARGUMENTS (decl);
   28582     22647125 :           for (int skip = num_artificial_parms_for (decl); skip; --skip)
   28583      6337650 :             p = &DECL_CHAIN (*p);
   28584     16309475 :           tree oldarg = *p;
   28585     16309475 :           *p = tsubst_decl (pattern_parm, args, tf_error);
   28586     45345396 :           for (tree t = *p; t; t = DECL_CHAIN (t))
   28587     29035921 :             DECL_CONTEXT (t) = decl;
   28588              :           /* Mark the old PARM_DECLs in case std::meta::parameters_of has
   28589              :              been called on the old declaration and reflections of those
   28590              :              arguments are held across this point and used later.
   28591              :              Such PARM_DECLs are no longer present in
   28592              :              DECL_ARGUMENTS (DECL_CONTEXT (oldarg)) chain.  */
   28593     16309475 :           if (*p != oldarg)
   28594     45315736 :             for (tree t = oldarg; t; t = DECL_CHAIN (t))
   28595     29035921 :               OLD_PARM_DECL_P (t) = 1;
   28596              :         }
   28597              : 
   28598              :       /* The contracts on DECL may predate a later redeclaration of the
   28599              :          template, or have been copied from a more general template.  We should
   28600              :          use the contracts from the current pattern.  */
   28601     23589682 :       tree decl_contracts = get_fn_contract_specifiers (decl);
   28602     23589682 :       tree pattern_contracts = get_fn_contract_specifiers (code_pattern);
   28603              :       /* There are four cases:
   28604              :          neither has contracts, so there is nothing to do;
   28605              :          only the pattern has contracts, so add them to DECL;
   28606              :          only DECL has contracts, so remove them;
   28607              :          or both have contracts, so rebuild DECL's from the pattern.  */
   28608     23589682 :       if (decl_contracts || pattern_contracts)
   28609          264 :         tsubst_contract_specifiers (pattern_contracts, decl, args,
   28610              :                                     tf_warning_or_error, code_pattern);
   28611              : 
   28612              :       /* Merge additional specifiers from the CODE_PATTERN.  */
   28613     23589682 :       if (DECL_DECLARED_INLINE_P (code_pattern)
   28614     45487223 :           && !DECL_DECLARED_INLINE_P (decl))
   28615        65747 :         DECL_DECLARED_INLINE_P (decl) = 1;
   28616              : 
   28617     23589682 :       maybe_instantiate_noexcept (decl, tf_error);
   28618              :     }
   28619     15501414 :   else if (VAR_P (decl))
   28620              :     {
   28621     15501414 :       start_lambda_scope (decl);
   28622     15501414 :       DECL_INITIAL (decl) =
   28623     15501414 :         tsubst_init (DECL_INITIAL (code_pattern), decl, args,
   28624     15501414 :                      tf_error, DECL_TI_TEMPLATE (decl));
   28625     15501414 :       finish_lambda_scope ();
   28626     15501414 :       if (VAR_HAD_UNKNOWN_BOUND (decl))
   28627          218 :         TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
   28628          218 :                                    tf_error, DECL_TI_TEMPLATE (decl));
   28629              :     }
   28630              :   else
   28631            0 :     gcc_unreachable ();
   28632              : 
   28633     39161764 :  done:
   28634     39161764 :   pop_access_scope (decl);
   28635     39161764 : }
   28636              : 
   28637              : /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
   28638              :    substituted to get DECL.  */
   28639              : 
   28640              : tree
   28641    107685578 : template_for_substitution (tree decl)
   28642              : {
   28643    107685578 :   tree tmpl = DECL_TI_TEMPLATE (decl);
   28644    107685578 :   if (VAR_P (decl))
   28645     21104947 :     if (tree partial = most_specialized_partial_spec (decl, tf_none))
   28646       924301 :       if (partial != error_mark_node)
   28647       924301 :         tmpl = TI_TEMPLATE (partial);
   28648              : 
   28649              :   /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
   28650              :      for the instantiation.  This is not always the most general
   28651              :      template.  Consider, for example:
   28652              : 
   28653              :         template <class T>
   28654              :         struct S { template <class U> void f();
   28655              :                    template <> void f<int>(); };
   28656              : 
   28657              :      and an instantiation of S<double>::f<int>.  We want TD to be the
   28658              :      specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
   28659              :   while (/* An instantiation cannot have a definition, so we need a
   28660              :             more general template.  */
   28661    235672060 :          DECL_TEMPLATE_INSTANTIATION (tmpl)
   28662              :            /* We must also deal with friend templates.  Given:
   28663              : 
   28664              :                 template <class T> struct S {
   28665              :                   template <class U> friend void f() {};
   28666              :                 };
   28667              : 
   28668              :               S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
   28669              :               so far as the language is concerned, but that's still
   28670              :               where we get the pattern for the instantiation from.  On
   28671              :               other hand, if the definition comes outside the class, say:
   28672              : 
   28673              :                 template <class T> struct S {
   28674              :                   template <class U> friend void f();
   28675              :                 };
   28676              :                 template <class U> friend void f() {}
   28677              : 
   28678              :               we don't need to look any further.  That's what the check for
   28679              :               DECL_INITIAL is for.  */
   28680    117836030 :           || (TREE_CODE (decl) == FUNCTION_DECL
   28681     85970741 :               && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
   28682      1170180 :               && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
   28683              :     {
   28684              :       /* The present template, TD, should not be a definition.  If it
   28685              :          were a definition, we should be using it!  Note that we
   28686              :          cannot restructure the loop to just keep going until we find
   28687              :          a template with a definition, since that might go too far if
   28688              :          a specialization was declared, but not defined.  */
   28689              : 
   28690              :       /* Fetch the more general template.  */
   28691     10150452 :       tmpl = DECL_TI_TEMPLATE (tmpl);
   28692              :     }
   28693              : 
   28694    107685578 :   return tmpl;
   28695              : }
   28696              : 
   28697              : /* Returns true if we need to instantiate this template instance even if we
   28698              :    know we aren't going to emit it.  */
   28699              : 
   28700              : bool
   28701      3691785 : always_instantiate_p (tree decl)
   28702              : {
   28703              :   /* We always instantiate inline functions so that we can inline them.  An
   28704              :      explicit instantiation declaration prohibits implicit instantiation of
   28705              :      non-inline functions.  With high levels of optimization, we would
   28706              :      normally inline non-inline functions -- but we're not allowed to do
   28707              :      that for "extern template" functions.  Therefore, we check
   28708              :      DECL_DECLARED_INLINE_P, rather than possibly_inlined_p.  */
   28709      3691785 :   return ((TREE_CODE (decl) == FUNCTION_DECL
   28710      3592753 :            && (DECL_DECLARED_INLINE_P (decl)
   28711        72203 :                || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
   28712              :           /* And we need to instantiate static data members so that
   28713              :              their initializers are available in integral constant
   28714              :              expressions.  */
   28715      3763988 :           || (VAR_P (decl)
   28716        99032 :               && decl_maybe_constant_var_p (decl)));
   28717              : }
   28718              : 
   28719              : /* If FN has a noexcept-specifier that hasn't been instantiated yet,
   28720              :    instantiate it now, modifying TREE_TYPE (fn).  Returns false on
   28721              :    error, true otherwise.  */
   28722              : 
   28723              : bool
   28724    464090306 : maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
   28725              : {
   28726    464090306 :   if (fn == error_mark_node)
   28727              :     return false;
   28728              : 
   28729              :   /* Don't instantiate a noexcept-specification from template context.  */
   28730    464090306 :   if (processing_template_decl
   28731    464090306 :       && (!flag_noexcept_type || type_dependent_expression_p (fn)))
   28732              :     return true;
   28733              : 
   28734    463894260 :   tree fntype = TREE_TYPE (fn);
   28735    463894260 :   tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
   28736              : 
   28737    252352216 :   if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
   28738    472371558 :       && DECL_MAYBE_DELETED (fn))
   28739              :     {
   28740        42676 :       if (fn == current_function_decl)
   28741              :         /* We're in start_preparsed_function, keep going.  */
   28742              :         return true;
   28743              : 
   28744           31 :       ++function_depth;
   28745           31 :       maybe_synthesize_method (fn);
   28746           31 :       --function_depth;
   28747           31 :       return !DECL_DELETED_FN (fn);
   28748              :     }
   28749              : 
   28750    716203800 :   if (!spec || !TREE_PURPOSE (spec))
   28751              :     return true;
   28752              : 
   28753    251152802 :   tree noex = TREE_PURPOSE (spec);
   28754    251152802 :   if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   28755    251152802 :       && TREE_CODE (noex) != DEFERRED_PARSE)
   28756              :     return true;
   28757              : 
   28758     10817103 :   tree orig_fn = NULL_TREE;
   28759              :   /* For a member friend template we can get a TEMPLATE_DECL.  Let's use
   28760              :      its FUNCTION_DECL for the rest of this function -- push_access_scope
   28761              :      doesn't accept TEMPLATE_DECLs.  */
   28762     10817103 :   if (DECL_FUNCTION_TEMPLATE_P (fn))
   28763              :     {
   28764           46 :       orig_fn = fn;
   28765           46 :       fn = DECL_TEMPLATE_RESULT (fn);
   28766              :     }
   28767              : 
   28768     10817103 :   if (DECL_CLONED_FUNCTION_P (fn))
   28769              :     {
   28770      4915990 :       tree prime = DECL_CLONED_FUNCTION (fn);
   28771      4915990 :       if (!maybe_instantiate_noexcept (prime, complain))
   28772              :         return false;
   28773      4915898 :       spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
   28774              :     }
   28775      5901113 :   else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
   28776              :     {
   28777      5901113 :       static hash_set<tree>* fns = new hash_set<tree>;
   28778      5901113 :       bool added = false;
   28779      5901113 :       tree pattern = DEFERRED_NOEXCEPT_PATTERN (noex);
   28780      5901113 :       if (pattern == NULL_TREE)
   28781              :         {
   28782      4198813 :           spec = get_defaulted_eh_spec (fn, complain);
   28783      4198813 :           if (spec == error_mark_node)
   28784              :             /* This might have failed because of an unparsed DMI, so
   28785              :                let's try again later.  */
   28786              :             return false;
   28787              :         }
   28788      1702300 :       else if (!(added = !fns->add (fn)))
   28789              :         {
   28790              :           /* If hash_set::add returns true, the element was already there.  */
   28791            3 :           location_t loc = cp_expr_loc_or_loc (pattern,
   28792            3 :                                                DECL_SOURCE_LOCATION (fn));
   28793            3 :           error_at (loc,
   28794              :                     "exception specification of %qD depends on itself",
   28795              :                     fn);
   28796            3 :           spec = noexcept_false_spec;
   28797              :         }
   28798      1702297 :       else if (TREE_CODE (pattern) == DEFERRED_PARSE)
   28799              :         {
   28800            6 :           error ("exception specification of %qD is not available "
   28801              :                  "until end of class definition", fn);
   28802            6 :           spec = noexcept_false_spec;
   28803              :         }
   28804      1702291 :       else if (push_tinst_level (fn))
   28805              :         {
   28806      1702204 :           const bool push_to_top = maybe_push_to_top_level (fn);
   28807      1702204 :           push_access_scope (fn);
   28808      1702204 :           push_deferring_access_checks (dk_no_deferred);
   28809      1702204 :           input_location = DECL_SOURCE_LOCATION (fn);
   28810              : 
   28811      1702204 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   28812      1702204 :               && !DECL_LOCAL_DECL_P (fn))
   28813              :             {
   28814              :               /* If needed, set current_class_ptr for the benefit of
   28815              :                  tsubst_copy/PARM_DECL.  */
   28816       889659 :               tree this_parm = DECL_ARGUMENTS (fn);
   28817       889659 :               current_class_ptr = NULL_TREE;
   28818       889659 :               current_class_ref = cp_build_fold_indirect_ref (this_parm);
   28819       889659 :               current_class_ptr = this_parm;
   28820              :             }
   28821              : 
   28822              :           /* If this function is represented by a TEMPLATE_DECL, then
   28823              :              the deferred noexcept-specification might still contain
   28824              :              dependent types, even after substitution.  And we need the
   28825              :              dependency check functions to work in build_noexcept_spec.  */
   28826      1702204 :           if (orig_fn)
   28827           46 :             ++processing_template_decl;
   28828              : 
   28829              :           /* Do deferred instantiation of the noexcept-specifier.  */
   28830      1702204 :           noex = tsubst_expr (pattern, DEFERRED_NOEXCEPT_ARGS (noex),
   28831              :                               tf_warning_or_error, fn);
   28832              :           /* Build up the noexcept-specification.  */
   28833      1702204 :           spec = build_noexcept_spec (noex, tf_warning_or_error);
   28834              : 
   28835      1702204 :           if (orig_fn)
   28836           46 :             --processing_template_decl;
   28837              : 
   28838      1702204 :           pop_deferring_access_checks ();
   28839      1702204 :           pop_access_scope (fn);
   28840      1702204 :           pop_tinst_level ();
   28841      1702204 :           maybe_pop_from_top_level (push_to_top);
   28842              :         }
   28843              :       else
   28844           87 :         spec = noexcept_false_spec;
   28845              : 
   28846      1702300 :       if (added)
   28847      1702297 :         fns->remove (fn);
   28848              :     }
   28849              : 
   28850     10816919 :   if (spec == error_mark_node)
   28851              :     {
   28852              :       /* This failed with a hard error, so let's go with false.  */
   28853           25 :       gcc_assert (seen_error ());
   28854           25 :       spec = noexcept_false_spec;
   28855              :     }
   28856              : 
   28857     10816919 :   TREE_TYPE (fn) = build_exception_variant (fntype, spec);
   28858     10816919 :   if (orig_fn)
   28859           46 :     TREE_TYPE (orig_fn) = TREE_TYPE (fn);
   28860              : 
   28861              :   /* [basic.stc.dynamic.deallocation]/3 - A deallocation function shall not
   28862              :      have a potentially throwing exception specification.  */
   28863     10816919 :   maybe_diagnose_deallocation_noexcept_false (fn);
   28864              : 
   28865     10816919 :   return true;
   28866              : }
   28867              : 
   28868              : /* We're starting to process the function INST, an instantiation of PATTERN;
   28869              :    add their parameters to local_specializations.  */
   28870              : 
   28871              : void
   28872     23907770 : register_parameter_specializations (tree pattern, tree inst)
   28873              : {
   28874     23907770 :   tree tmpl_parm = DECL_ARGUMENTS (pattern);
   28875     23907770 :   tree spec_parm = DECL_ARGUMENTS (inst);
   28876     23907770 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (inst))
   28877              :     {
   28878     11599260 :       register_local_specialization (spec_parm, tmpl_parm);
   28879     11599260 :       spec_parm = skip_artificial_parms_for (inst, spec_parm);
   28880     11599260 :       tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
   28881              :     }
   28882     53462339 :   for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
   28883              :     {
   28884     29554569 :       if (!DECL_PACK_P (tmpl_parm))
   28885              :         {
   28886     28773780 :           register_local_specialization (spec_parm, tmpl_parm);
   28887     28773780 :           spec_parm = DECL_CHAIN (spec_parm);
   28888              :         }
   28889              :       else
   28890              :         {
   28891              :           /* Register the (value) argument pack as a specialization of
   28892              :              TMPL_PARM, then move on.  */
   28893       780789 :           tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
   28894       780789 :           register_local_specialization (argpack, tmpl_parm);
   28895              :         }
   28896              :     }
   28897     23907770 :   gcc_assert (!spec_parm);
   28898     23907770 : }
   28899              : 
   28900              : /* Instantiate the body of D using PATTERN with ARGS.  We have
   28901              :    already determined PATTERN is the correct template to use.
   28902              :    NESTED_P is true if this is a nested function, in which case
   28903              :    PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL.  */
   28904              : 
   28905              : static void
   28906     39161852 : instantiate_body (tree pattern, tree args, tree d, bool nested_p)
   28907              : {
   28908     39161852 :   tree td = NULL_TREE;
   28909     39161852 :   tree code_pattern = pattern;
   28910              : 
   28911     39161852 :   if (!nested_p)
   28912              :     {
   28913     39161764 :       td = pattern;
   28914     39161764 :       code_pattern = DECL_TEMPLATE_RESULT (td);
   28915              :     }
   28916              :   else
   28917              :     /* Only OMP reductions are nested.  */
   28918           88 :     gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
   28919              : 
   28920     39161852 :   vec<tree> omp_privatization_save;
   28921     39161852 :   if (current_function_decl)
   28922     15049823 :     save_omp_privatization_clauses (omp_privatization_save);
   28923              : 
   28924     39161852 :   bool push_to_top = maybe_push_to_top_level (d);
   28925              : 
   28926     39161852 :   mark_template_arguments_used (pattern, args);
   28927              : 
   28928     39161852 :   if (VAR_P (d))
   28929              :     {
   28930              :       /* The variable might be a lambda's extra scope, and that
   28931              :          lambda's visibility depends on D's.  */
   28932     15501414 :       maybe_commonize_var (d);
   28933     15501414 :       determine_visibility (d);
   28934              :     }
   28935              : 
   28936              :   /* Mark D as instantiated so that recursive calls to
   28937              :      instantiate_decl do not try to instantiate it again.  */
   28938     39161852 :   DECL_TEMPLATE_INSTANTIATED (d) = 1;
   28939              : 
   28940     39161852 :   if (td)
   28941              :     /* Regenerate the declaration in case the template has been modified
   28942              :        by a subsequent redeclaration.  */
   28943     39161764 :     regenerate_decl_from_template (d, td, args);
   28944              : 
   28945              :   /* We already set the file and line above.  Reset them now in case
   28946              :      they changed as a result of calling regenerate_decl_from_template.  */
   28947     39161852 :   input_location = DECL_SOURCE_LOCATION (d);
   28948              : 
   28949     39161852 :   if (VAR_P (d))
   28950              :     {
   28951              :       /* Clear out DECL_RTL; whatever was there before may not be right
   28952              :          since we've reset the type of the declaration.  */
   28953     15501414 :       SET_DECL_RTL (d, NULL);
   28954     15501414 :       DECL_IN_AGGR_P (d) = 0;
   28955              : 
   28956              :       /* The initializer is placed in DECL_INITIAL by
   28957              :          regenerate_decl_from_template so we don't need to
   28958              :          push/pop_access_scope again here.  Pull it out so that
   28959              :          cp_finish_decl can process it.  */
   28960     15501414 :       bool const_init = false;
   28961     15501414 :       tree init = DECL_INITIAL (d);
   28962     15501414 :       DECL_INITIAL (d) = NULL_TREE;
   28963     15501414 :       DECL_INITIALIZED_P (d) = 0;
   28964              : 
   28965              :       /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
   28966              :          initializer.  That function will defer actual emission until
   28967              :          we have a chance to determine linkage.  */
   28968     15501414 :       DECL_EXTERNAL (d) = 0;
   28969              : 
   28970              :       /* Enter the scope of D so that access-checking works correctly.  */
   28971     15501414 :       bool enter_context = DECL_CLASS_SCOPE_P (d);
   28972      2119921 :       if (enter_context)
   28973      2119921 :         push_nested_class (DECL_CONTEXT (d));
   28974              : 
   28975     15501414 :       const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   28976     15501414 :       cp_finish_decl (d, init, const_init, NULL_TREE, 0);
   28977              : 
   28978     15501414 :       if (enter_context)
   28979      2119921 :         pop_nested_class ();
   28980              :     }
   28981     23660438 :   else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
   28982           72 :     synthesize_method (d);
   28983     23660366 :   else if (TREE_CODE (d) == FUNCTION_DECL)
   28984              :     {
   28985              :       /* Set up the list of local specializations.  */
   28986     24797218 :       local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
   28987     23660366 :       tree block = NULL_TREE;
   28988              : 
   28989              :       /* Set up context.  */
   28990     23660366 :       if (nested_p)
   28991           88 :         block = push_stmt_list ();
   28992              :       else
   28993              :         {
   28994     23660278 :           start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
   28995              : 
   28996     23660278 :           perform_instantiation_time_access_checks (code_pattern, args);
   28997              :         }
   28998              : 
   28999              :       /* Create substitution entries for the parameters.  */
   29000     23660366 :       register_parameter_specializations (code_pattern, d);
   29001              : 
   29002              :       /* Substitute into the body of the function.  */
   29003     23660366 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   29004          104 :         tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
   29005              :                         tf_warning_or_error, d);
   29006              :       else
   29007              :         {
   29008     23660262 :           tsubst_stmt (DECL_SAVED_TREE (code_pattern), args,
   29009     23660262 :                        tf_warning_or_error, DECL_TI_TEMPLATE (d));
   29010              : 
   29011              :           /* Set the current input_location to the end of the function
   29012              :              so that finish_function knows where we are.  */
   29013     23660253 :           input_location
   29014     23660253 :             = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
   29015              : 
   29016              :           /* Remember if we saw an infinite loop in the template.  */
   29017     23660253 :           current_function_infinite_loop
   29018     23660253 :             = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
   29019              :         }
   29020              : 
   29021              :       /* Finish the function.  */
   29022     23660357 :       if (nested_p)
   29023           88 :         DECL_SAVED_TREE (d) = pop_stmt_list (block);
   29024              :       else
   29025              :         {
   29026     23660269 :           d = finish_function (/*inline_p=*/false);
   29027     23660269 :           expand_or_defer_fn (d);
   29028              :         }
   29029              : 
   29030     23660357 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   29031          104 :         cp_check_omp_declare_reduction (d);
   29032              : 
   29033     23660357 :       if (int errs = errorcount + sorrycount)
   29034       998921 :         if (errs > current_tinst_level->errors)
   29035         2749 :           if (function *f = DECL_STRUCT_FUNCTION (d))
   29036         2731 :             f->language->erroneous = true;
   29037     23660357 :     }
   29038              : 
   29039              :   /* We're not deferring instantiation any more.  */
   29040     39161843 :   if (!nested_p)
   29041     39161755 :     TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
   29042              : 
   29043     39161843 :   maybe_pop_from_top_level (push_to_top);
   29044              : 
   29045     39161843 :   if (current_function_decl)
   29046     15049823 :     restore_omp_privatization_clauses (omp_privatization_save);
   29047     39161843 : }
   29048              : 
   29049              : /* Produce the definition of D, a _DECL generated from a template.  If
   29050              :    DEFER_OK is true, then we don't have to actually do the
   29051              :    instantiation now; we just have to do it sometime.  Normally it is
   29052              :    an error if this is an explicit instantiation but D is undefined.
   29053              :    EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
   29054              :    instantiated class template.  */
   29055              : 
   29056              : tree
   29057    161234275 : instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
   29058              : {
   29059    161234275 :   tree tmpl = DECL_TI_TEMPLATE (d);
   29060    161234275 :   tree gen_args;
   29061    161234275 :   tree args;
   29062    161234275 :   tree td;
   29063    161234275 :   tree code_pattern;
   29064    161234275 :   tree spec;
   29065    161234275 :   tree gen_tmpl;
   29066    161234275 :   bool pattern_defined;
   29067    161234275 :   location_t saved_loc = input_location;
   29068    161234275 :   bool external_p;
   29069    161234275 :   bool deleted_p;
   29070              : 
   29071              :   /* This function should only be used to instantiate templates for
   29072              :      functions and static member variables.  */
   29073    161234275 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
   29074              : 
   29075    161234275 :   gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
   29076              : 
   29077    161234275 :   if (modules_p ())
   29078              :     /* We may have a pending instantiation of D itself.  */
   29079       614575 :     lazy_load_pendings (d);
   29080              : 
   29081              :   /* Variables are never deferred; if instantiation is required, they
   29082              :      are instantiated right away.  That allows for better code in the
   29083              :      case that an expression refers to the value of the variable --
   29084              :      if the variable has a constant value the referring expression can
   29085              :      take advantage of that fact.  */
   29086    161234275 :   if (VAR_P (d))
   29087    112518532 :     defer_ok = false;
   29088              : 
   29089              :   /* Don't instantiate cloned functions.  Instead, instantiate the
   29090              :      functions they cloned.  */
   29091    161234275 :   if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
   29092      5925065 :     d = DECL_CLONED_FUNCTION (d);
   29093              : 
   29094    161234275 :   if (DECL_TEMPLATE_INSTANTIATED (d)
   29095     66841868 :       || TREE_TYPE (d) == error_mark_node
   29096     66841863 :       || (TREE_CODE (d) == FUNCTION_DECL
   29097     45613589 :           && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
   29098    228055756 :       || DECL_TEMPLATE_SPECIALIZATION (d))
   29099              :     /* D has already been instantiated or explicitly specialized, so
   29100              :        there's nothing for us to do here.
   29101              : 
   29102              :        It might seem reasonable to check whether or not D is an explicit
   29103              :        instantiation, and, if so, stop here.  But when an explicit
   29104              :        instantiation is deferred until the end of the compilation,
   29105              :        DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
   29106              :        the instantiation.  */
   29107              :     return d;
   29108              : 
   29109              :   /* Check to see whether we know that this template will be
   29110              :      instantiated in some other file, as with "extern template"
   29111              :      extension.  */
   29112     66624355 :   external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
   29113              : 
   29114              :   /* In general, we do not instantiate such templates.  */
   29115      2473784 :   if (external_p && !always_instantiate_p (d))
   29116              :     return d;
   29117              : 
   29118     66608539 :   gen_tmpl = most_general_template (tmpl);
   29119     66608539 :   gen_args = DECL_TI_ARGS (d);
   29120              : 
   29121              :   /* We should already have the extra args.  */
   29122     76019529 :   gcc_checking_assert (tmpl == gen_tmpl
   29123              :                        || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
   29124              :                            == TMPL_ARGS_DEPTH (gen_args)));
   29125              :   /* And what's in the hash table should match D.  */
   29126     66608539 :   gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
   29127              :                        == d
   29128              :                        || spec == NULL_TREE);
   29129              : 
   29130              :   /* This needs to happen before any tsubsting.  */
   29131     66608539 :   if (! push_tinst_level (d))
   29132              :     return d;
   29133              : 
   29134     66606989 :   auto_timevar tv (TV_TEMPLATE_INST);
   29135              : 
   29136              :   /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
   29137              :      for the instantiation.  */
   29138     66606989 :   td = template_for_substitution (d);
   29139     66606989 :   args = gen_args;
   29140              : 
   29141     66606989 :   if (variable_template_specialization_p (d))
   29142              :     {
   29143              :       /* Look up an explicit specialization, if any.  */
   29144     13467194 :       tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error);
   29145     13467194 :       if (partial_ti && partial_ti != error_mark_node)
   29146              :         {
   29147       923454 :           td = TI_TEMPLATE (partial_ti);
   29148       923454 :           args = TI_ARGS (partial_ti);
   29149              :         }
   29150              :     }
   29151              : 
   29152     66606989 :   maybe_diagnose_erroneous_template (td);
   29153              : 
   29154     66606989 :   code_pattern = DECL_TEMPLATE_RESULT (td);
   29155              : 
   29156              :   /* We should never be trying to instantiate a member of a class
   29157              :      template or partial specialization.  */
   29158     66606989 :   gcc_assert (d != code_pattern);
   29159              : 
   29160    133213978 :   if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
   29161    105008022 :       || DECL_TEMPLATE_SPECIALIZATION (td))
   29162              :     /* In the case of a friend template whose definition is provided
   29163              :        outside the class, we may have too many arguments.  Drop the
   29164              :        ones we don't need.  The same is true for specializations.  */
   29165     28208569 :     args = get_innermost_template_args
   29166     28208569 :       (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
   29167              : 
   29168     66606989 :   if (TREE_CODE (d) == FUNCTION_DECL)
   29169              :     {
   29170     45543061 :       deleted_p = DECL_DELETED_FN (code_pattern);
   29171     45543061 :       pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
   29172     44569473 :                           && DECL_INITIAL (code_pattern) != error_mark_node)
   29173       973662 :                          || DECL_DEFAULTED_FN (code_pattern)
   29174     46516567 :                          || deleted_p);
   29175              :     }
   29176              :   else
   29177              :     {
   29178     21063928 :       deleted_p = false;
   29179     21063928 :       if (DECL_CLASS_SCOPE_P (code_pattern))
   29180      7680794 :         pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
   29181              :       else
   29182     13383134 :         pattern_defined = ! DECL_EXTERNAL (code_pattern);
   29183              :     }
   29184              : 
   29185              :   /* We may be in the middle of deferred access check.  Disable it now.  */
   29186     66606989 :   push_deferring_access_checks (dk_no_deferred);
   29187              : 
   29188              :   /* Unless an explicit instantiation directive has already determined
   29189              :      the linkage of D, remember that a definition is available for
   29190              :      this entity.  */
   29191     66606989 :   if (pattern_defined
   29192     60157481 :       && !DECL_INTERFACE_KNOWN (d)
   29193    122892228 :       && !DECL_NOT_REALLY_EXTERN (d))
   29194     23363469 :     mark_definable (d);
   29195              : 
   29196     66606989 :   DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
   29197     66606989 :   DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
   29198     66606989 :   input_location = DECL_SOURCE_LOCATION (d);
   29199              : 
   29200              :   /* If D is a member of an explicitly instantiated class template,
   29201              :      and no definition is available, treat it like an implicit
   29202              :      instantiation.  */
   29203     66606989 :   if (!pattern_defined && expl_inst_class_mem_p
   29204     66606989 :       && DECL_EXPLICIT_INSTANTIATION (d))
   29205              :     {
   29206              :       /* Leave linkage flags alone on instantiations with anonymous
   29207              :          visibility.  */
   29208          359 :       if (TREE_PUBLIC (d))
   29209              :         {
   29210          350 :           DECL_NOT_REALLY_EXTERN (d) = 0;
   29211          350 :           DECL_INTERFACE_KNOWN (d) = 0;
   29212              :         }
   29213          359 :       SET_DECL_IMPLICIT_INSTANTIATION (d);
   29214              :     }
   29215              : 
   29216              :   /* Defer all other templates, unless we have been explicitly
   29217              :      forbidden from doing so.  */
   29218     66606989 :   if (/* If there is no definition, we cannot instantiate the
   29219              :          template.  */
   29220              :       ! pattern_defined
   29221              :       /* If it's OK to postpone instantiation, do so.  */
   29222     66606989 :       || defer_ok
   29223              :       /* If this is a static data member that will be defined
   29224              :          elsewhere, we don't want to instantiate the entire data
   29225              :          member, but we do want to instantiate the initializer so that
   29226              :          we can substitute that elsewhere.  */
   29227     39248267 :       || (external_p && VAR_P (d))
   29228              :       /* Handle here a deleted function too, avoid generating
   29229              :          its body (c++/61080).  */
   29230     39162060 :       || deleted_p
   29231              :       /* We need the initializer for an OpenMP declare mapper.  */
   29232    105768761 :       || (VAR_P (d) && DECL_LANG_SPECIFIC (d) && DECL_OMP_DECLARE_MAPPER_P (d)))
   29233              :     {
   29234              :       /* The definition of the static data member is now required so
   29235              :          we must substitute the initializer.  */
   29236     27445225 :       if (VAR_P (d)
   29237      5562514 :           && !DECL_INITIAL (d)
   29238     27946043 :           && DECL_INITIAL (code_pattern))
   29239              :         {
   29240       500273 :           tree ns;
   29241       500273 :           tree init;
   29242       500273 :           bool const_init = false;
   29243       500273 :           bool enter_context = DECL_CLASS_SCOPE_P (d);
   29244              : 
   29245       500273 :           ns = decl_namespace_context (d);
   29246       500273 :           push_nested_namespace (ns);
   29247       500273 :           if (enter_context)
   29248       500031 :             push_nested_class (DECL_CONTEXT (d));
   29249       500273 :           init = tsubst_expr (DECL_INITIAL (code_pattern),
   29250              :                               args,
   29251              :                               tf_warning_or_error, NULL_TREE);
   29252              :           /* If instantiating the initializer involved instantiating this
   29253              :              again, don't call cp_finish_decl twice.  */
   29254       492176 :           if (!DECL_INITIAL (d))
   29255              :             {
   29256              :               /* Make sure the initializer is still constant, in case of
   29257              :                  circular dependency (template/instantiate6.C). */
   29258       492173 :               const_init
   29259       492173 :                 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   29260       492173 :               cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
   29261              :                               /*asmspec_tree=*/NULL_TREE, 0);
   29262              :             }
   29263       489479 :           if (enter_context)
   29264       489237 :             pop_nested_class ();
   29265       489479 :           pop_nested_namespace (ns);
   29266              :         }
   29267              : 
   29268              :       /* We restore the source position here because it's used by
   29269              :          add_pending_template.  */
   29270     27434431 :       input_location = saved_loc;
   29271              : 
   29272     18722175 :       if (at_eof && !pattern_defined
   29273      5415697 :           && DECL_EXPLICIT_INSTANTIATION (d)
   29274     27434446 :           && DECL_NOT_REALLY_EXTERN (d))
   29275              :         /* [temp.explicit]
   29276              : 
   29277              :            The definition of a non-exported function template, a
   29278              :            non-exported member function template, or a non-exported
   29279              :            member function or static data member of a class template
   29280              :            shall be present in every translation unit in which it is
   29281              :            explicitly instantiated.  */
   29282           15 :         permerror (input_location,  "explicit instantiation of %qD "
   29283              :                    "but no definition available", d);
   29284              : 
   29285              :       /* If we're in unevaluated context, we just wanted to get the
   29286              :          constant value; this isn't an odr use, so don't queue
   29287              :          a full instantiation.  */
   29288     27434431 :       if (!cp_unevaluated_operand
   29289              :           /* ??? Historically, we have instantiated inline functions, even
   29290              :              when marked as "extern template".  */
   29291     27433928 :           && !(external_p && VAR_P (d)))
   29292     27347721 :         add_pending_template (d);
   29293              :     }
   29294              :   else
   29295              :     {
   29296     39161764 :       set_instantiating_module (d);
   29297     39161764 :       if (variable_template_p (gen_tmpl))
   29298     13465186 :         note_vague_linkage_variable (d);
   29299     39161764 :       instantiate_body (td, args, d, false);
   29300              :     }
   29301              : 
   29302     66596186 :   pop_deferring_access_checks ();
   29303     66596186 :   pop_tinst_level ();
   29304     66596186 :   input_location = saved_loc;
   29305              : 
   29306     66596186 :   return d;
   29307     66596186 : }
   29308              : 
   29309              : /* Run through the list of templates that we wish we could
   29310              :    instantiate, and instantiate any we can.  RETRIES is the
   29311              :    number of times we retry pending template instantiation.  */
   29312              : 
   29313              : void
   29314       148436 : instantiate_pending_templates (int retries)
   29315              : {
   29316       148436 :   int reconsider;
   29317       148436 :   location_t saved_loc = input_location;
   29318       148436 :   unsigned saved_module_kind = module_kind;
   29319              : 
   29320              :   /* Instantiating templates may trigger vtable generation.  This in turn
   29321              :      may require further template instantiations.  We place a limit here
   29322              :      to avoid infinite loop.  */
   29323       148436 :   if (pending_templates && retries >= max_tinst_depth)
   29324              :     {
   29325            3 :       tree decl = pending_templates->tinst->maybe_get_node ();
   29326              : 
   29327            3 :       fatal_error (input_location,
   29328              :                    "template instantiation depth exceeds maximum of %d"
   29329              :                    " instantiating %q+D, possibly from virtual table generation"
   29330              :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   29331              :                    max_tinst_depth, decl);
   29332              :       if (TREE_CODE (decl) == FUNCTION_DECL)
   29333              :         /* Pretend that we defined it.  */
   29334              :         DECL_INITIAL (decl) = error_mark_node;
   29335              :       return;
   29336              :     }
   29337              : 
   29338       188457 :   do
   29339              :     {
   29340       188457 :       struct pending_template **t = &pending_templates;
   29341       188457 :       struct pending_template *last = NULL;
   29342       188457 :       reconsider = 0;
   29343     24743498 :       while (*t)
   29344              :         {
   29345     24555050 :           struct tinst_level *tinst = (*t)->tinst;
   29346     24555050 :           bool complete = tinst_complete_p (tinst);
   29347              : 
   29348     24555050 :           if (!complete)
   29349              :             {
   29350     19378270 :               tree instantiation = reopen_tinst_level (tinst);
   29351              : 
   29352     19378270 :               if (limit_bad_template_recursion (instantiation))
   29353              :                 /* Do nothing.  */;
   29354     19377222 :               else if (TYPE_P (instantiation))
   29355              :                 {
   29356            0 :                   instantiate_class_template (instantiation);
   29357            0 :                   if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
   29358            0 :                     for (tree fld = TYPE_FIELDS (instantiation);
   29359            0 :                          fld; fld = TREE_CHAIN (fld))
   29360            0 :                       if ((VAR_P (fld)
   29361            0 :                            || (TREE_CODE (fld) == FUNCTION_DECL
   29362            0 :                                && !DECL_ARTIFICIAL (fld)))
   29363            0 :                           && DECL_TEMPLATE_INSTANTIATION (fld))
   29364            0 :                         instantiate_decl (fld,
   29365              :                                           /*defer_ok=*/false,
   29366              :                                           /*expl_inst_class_mem_p=*/false);
   29367              : 
   29368            0 :                   if (COMPLETE_TYPE_P (instantiation))
   29369     15806891 :                     reconsider = 1;
   29370              :                 }
   29371              :               else
   29372              :                 {
   29373     19377222 :                   instantiation
   29374     19377222 :                     = instantiate_decl (instantiation,
   29375              :                                         /*defer_ok=*/false,
   29376              :                                         /*expl_inst_class_mem_p=*/false);
   29377     19377213 :                   if (DECL_TEMPLATE_INSTANTIATED (instantiation))
   29378     15806891 :                     reconsider = 1;
   29379              :                 }
   29380              : 
   29381     19378261 :               complete = tinst_complete_p (tinst);
   29382              : 
   29383     19378261 :               tinst_depth = 0;
   29384     19378261 :               set_refcount_ptr (current_tinst_level);
   29385              :             }
   29386              : 
   29387     19378261 :           if (complete)
   29388              :             {
   29389              :               /* If INSTANTIATION has been instantiated, then we don't
   29390              :                  need to consider it again in the future.  */
   29391     20983671 :               struct pending_template *drop = *t;
   29392     20983671 :               *t = (*t)->next;
   29393     20983671 :               set_refcount_ptr (drop->tinst);
   29394     20983671 :               pending_template_freelist ().free (drop);
   29395              :             }
   29396              :           else
   29397              :             {
   29398      3571370 :               last = *t;
   29399      3571370 :               t = &(*t)->next;
   29400              :             }
   29401              :         }
   29402       188448 :       last_pending_template = last;
   29403              :     }
   29404       188448 :   while (reconsider);
   29405              : 
   29406       148424 :   input_location = saved_loc;
   29407       148424 :   module_kind = saved_module_kind;
   29408              : }
   29409              : 
   29410              : /* Substitute ARGVEC into T, which is a list of initializers for
   29411              :    either base class or a non-static data member.  The TREE_PURPOSEs
   29412              :    are DECLs, and the TREE_VALUEs are the initializer values.  Used by
   29413              :    instantiate_decl.  */
   29414              : 
   29415              : static tree
   29416      3287405 : tsubst_initializer_list (tree t, tree argvec)
   29417              : {
   29418      3287405 :   tree inits = NULL_TREE;
   29419      3287405 :   tree target_ctor = error_mark_node;
   29420              : 
   29421      7079955 :   for (; t; t = TREE_CHAIN (t))
   29422              :     {
   29423      3792553 :       tree decl;
   29424      3792553 :       tree init;
   29425      3792553 :       tree expanded_bases = NULL_TREE;
   29426      3792553 :       tree expanded_arguments = NULL_TREE;
   29427      3792553 :       int i, len = 1;
   29428              : 
   29429      3792553 :       if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
   29430              :         {
   29431           31 :           tree expr;
   29432           31 :           tree arg;
   29433              : 
   29434              :           /* Expand the base class expansion type into separate base
   29435              :              classes.  */
   29436           31 :           expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
   29437              :                                                  tf_warning_or_error,
   29438              :                                                  NULL_TREE);
   29439           31 :           if (expanded_bases == error_mark_node)
   29440            0 :             continue;
   29441              : 
   29442              :           /* We'll be building separate TREE_LISTs of arguments for
   29443              :              each base.  */
   29444           31 :           len = TREE_VEC_LENGTH (expanded_bases);
   29445           31 :           expanded_arguments = make_tree_vec (len);
   29446          108 :           for (i = 0; i < len; i++)
   29447           46 :             TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
   29448              : 
   29449              :           /* Build a dummy EXPR_PACK_EXPANSION that will be used to
   29450              :              expand each argument in the TREE_VALUE of t.  */
   29451           31 :           expr = make_node (EXPR_PACK_EXPANSION);
   29452           31 :           PACK_EXPANSION_LOCAL_P (expr) = true;
   29453           62 :           PACK_EXPANSION_PARAMETER_PACKS (expr) =
   29454           31 :             PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
   29455              : 
   29456           31 :           if (TREE_VALUE (t) == void_type_node)
   29457              :             /* VOID_TYPE_NODE is used to indicate
   29458              :                value-initialization.  */
   29459              :             {
   29460            9 :               for (i = 0; i < len; i++)
   29461            3 :                 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
   29462              :             }
   29463              :           else
   29464              :             {
   29465              :               /* Substitute parameter packs into each argument in the
   29466              :                  TREE_LIST.  */
   29467           25 :               in_base_initializer = 1;
   29468           59 :               for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
   29469              :                 {
   29470           34 :                   tree expanded_exprs;
   29471              : 
   29472              :                   /* Expand the argument.  */
   29473           34 :                   tree value;
   29474           34 :                   if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   29475              :                     value = TREE_VALUE (arg);
   29476              :                   else
   29477              :                     {
   29478           31 :                       value = expr;
   29479           31 :                       PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
   29480              :                     }
   29481           34 :                   expanded_exprs
   29482           34 :                     = tsubst_pack_expansion (value, argvec,
   29483              :                                              tf_warning_or_error,
   29484              :                                              NULL_TREE);
   29485           34 :                   if (expanded_exprs == error_mark_node)
   29486            3 :                     continue;
   29487              : 
   29488              :                   /* Prepend each of the expanded expressions to the
   29489              :                      corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
   29490           95 :                   for (i = 0; i < len; i++)
   29491           64 :                     if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   29492           36 :                       for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
   29493           27 :                         TREE_VEC_ELT (expanded_arguments, i)
   29494           54 :                           = tree_cons (NULL_TREE,
   29495           27 :                                        TREE_VEC_ELT (expanded_exprs, j),
   29496           27 :                                        TREE_VEC_ELT (expanded_arguments, i));
   29497              :                     else
   29498           55 :                       TREE_VEC_ELT (expanded_arguments, i)
   29499          110 :                         = tree_cons (NULL_TREE,
   29500           55 :                                      TREE_VEC_ELT (expanded_exprs, i),
   29501           55 :                                      TREE_VEC_ELT (expanded_arguments, i));
   29502              :                 }
   29503           25 :               in_base_initializer = 0;
   29504              : 
   29505              :               /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
   29506              :                  since we built them backwards.  */
   29507           68 :               for (i = 0; i < len; i++)
   29508              :                 {
   29509           43 :                   TREE_VEC_ELT (expanded_arguments, i) =
   29510           43 :                     nreverse (TREE_VEC_ELT (expanded_arguments, i));
   29511              :                 }
   29512              :             }
   29513              :         }
   29514              : 
   29515      7585118 :       for (i = 0; i < len; ++i)
   29516              :         {
   29517      3792568 :           if (expanded_bases)
   29518              :             {
   29519           46 :               decl = TREE_VEC_ELT (expanded_bases, i);
   29520           46 :               decl = expand_member_init (decl);
   29521           46 :               init = TREE_VEC_ELT (expanded_arguments, i);
   29522              :             }
   29523              :           else
   29524              :             {
   29525      3792522 :               tree tmp;
   29526      3792522 :               if (TYPE_P (TREE_PURPOSE (t)))
   29527       660377 :                 decl = tsubst (TREE_PURPOSE (t), argvec,
   29528              :                                tf_warning_or_error, NULL_TREE);
   29529              :               else
   29530      3132145 :                 decl = tsubst_expr (TREE_PURPOSE (t), argvec,
   29531              :                                     tf_warning_or_error, NULL_TREE);
   29532              : 
   29533      3792522 :               decl = expand_member_init (decl);
   29534      3792522 :               if (decl && !DECL_P (decl))
   29535       660371 :                 in_base_initializer = 1;
   29536              : 
   29537      3792522 :               init = TREE_VALUE (t);
   29538      3792522 :               tmp = init;
   29539      3792522 :               if (init != void_type_node)
   29540      3273718 :                 init = tsubst_expr (init, argvec,
   29541              :                                     tf_warning_or_error, NULL_TREE);
   29542      3792522 :               if (init == NULL_TREE && tmp != NULL_TREE)
   29543              :                 /* If we had an initializer but it instantiated to nothing,
   29544              :                    value-initialize the object.  This will only occur when
   29545              :                    the initializer was a pack expansion where the parameter
   29546              :                    packs used in that expansion were of length zero.  */
   29547          697 :                 init = void_type_node;
   29548      3792522 :               in_base_initializer = 0;
   29549              :             }
   29550              : 
   29551      3792568 :           if (target_ctor != error_mark_node
   29552            3 :               && init != error_mark_node)
   29553              :             {
   29554            3 :               error ("mem-initializer for %qD follows constructor delegation",
   29555              :                      decl);
   29556            3 :               return inits;
   29557              :             }
   29558              :           /* Look for a target constructor. */
   29559      3792565 :           if (init != error_mark_node
   29560      3792553 :               && decl && CLASS_TYPE_P (decl)
   29561      3803146 :               && same_type_p (decl, current_class_type))
   29562              :             {
   29563        10581 :               maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
   29564        10581 :               if (inits)
   29565              :                 {
   29566            0 :                   error ("constructor delegation follows mem-initializer for %qD",
   29567            0 :                          TREE_PURPOSE (inits));
   29568            0 :                   continue;
   29569              :                 }
   29570              :               target_ctor = init;
   29571              :             }
   29572              : 
   29573      3792565 :           if (decl)
   29574              :             {
   29575      3792559 :               init = build_tree_list (decl, init);
   29576              :               /* Carry over the dummy TREE_TYPE node containing the source
   29577              :                  location.  */
   29578      3792559 :               TREE_TYPE (init) = TREE_TYPE (t);
   29579      3792559 :               TREE_CHAIN (init) = inits;
   29580      3792559 :               inits = init;
   29581              :             }
   29582              :         }
   29583              :     }
   29584              :   return inits;
   29585              : }
   29586              : 
   29587              : /* Instantiate an enumerated type.  TAG is the template type, NEWTAG
   29588              :    is the instantiation (which should have been created with
   29589              :    start_enum) and ARGS are the template arguments to use.  */
   29590              : 
   29591              : static void
   29592       395246 : tsubst_enum (tree tag, tree newtag, tree args)
   29593              : {
   29594       395246 :   tree e;
   29595              : 
   29596       395246 :   if (SCOPED_ENUM_P (newtag))
   29597          327 :     begin_scope (sk_scoped_enum, newtag);
   29598              : 
   29599       395246 :   ENUM_BEING_DEFINED_P (newtag) = 1;
   29600              : 
   29601       841530 :   for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
   29602              :     {
   29603       446329 :       tree value;
   29604       446329 :       tree decl = TREE_VALUE (e);
   29605              : 
   29606              :       /* Note that in a template enum, the TREE_VALUE is the
   29607              :          CONST_DECL, not the corresponding INTEGER_CST.  */
   29608       446329 :       value = tsubst_expr (DECL_INITIAL (decl),
   29609              :                            args, tf_warning_or_error, NULL_TREE);
   29610              : 
   29611              :       /* Give this enumeration constant the correct access.  */
   29612       446284 :       set_current_access_from_decl (decl);
   29613              : 
   29614              :       /* Actually build the enumerator itself.  Here we're assuming that
   29615              :          enumerators can't have dependent attributes.  */
   29616       446284 :       tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
   29617       446284 :                                        DECL_ATTRIBUTES (decl),
   29618       446284 :                                        DECL_SOURCE_LOCATION (decl));
   29619              :       /* Attribute deprecated without an argument isn't sticky: it'll
   29620              :          melt into a tree flag, so we need to propagate the flag here,
   29621              :          since we just created a new enumerator.  */
   29622       446284 :       TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
   29623       446284 :       TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
   29624              :     }
   29625              : 
   29626       395201 :   if (SCOPED_ENUM_P (newtag))
   29627          327 :     finish_scope ();
   29628              : 
   29629       395201 :   finish_enum_value_list (newtag);
   29630       395201 :   finish_enum (newtag);
   29631              : 
   29632       395201 :   DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
   29633       395201 :     = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
   29634       395201 :   TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
   29635       395201 :   TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
   29636       395201 : }
   29637              : 
   29638              : /* DECL is a FUNCTION_DECL that is a template specialization.  Return
   29639              :    its type -- but without substituting the innermost set of template
   29640              :    arguments.  So, innermost set of template parameters will appear in
   29641              :    the type.  */
   29642              : 
   29643              : tree
   29644     17430723 : get_mostly_instantiated_function_type (tree decl)
   29645              : {
   29646              :   /* For a function, DECL_TI_TEMPLATE is partially instantiated.  */
   29647     17430723 :   return TREE_TYPE (DECL_TI_TEMPLATE (decl));
   29648              : }
   29649              : 
   29650              : /* Return truthvalue if we're processing a template different from
   29651              :    the last one involved in diagnostics.  */
   29652              : bool
   29653       259381 : problematic_instantiation_changed (void)
   29654              : {
   29655       259381 :   return current_tinst_level != last_error_tinst_level;
   29656              : }
   29657              : 
   29658              : /* Remember current template involved in diagnostics.  */
   29659              : void
   29660         5981 : record_last_problematic_instantiation (void)
   29661              : {
   29662         5981 :   set_refcount_ptr (last_error_tinst_level, current_tinst_level);
   29663         5981 : }
   29664              : 
   29665              : struct tinst_level *
   29666    260381316 : current_instantiation (void)
   29667              : {
   29668    260381316 :   return current_tinst_level;
   29669              : }
   29670              : 
   29671              : /* Return TRUE if current_function_decl is being instantiated, false
   29672              :    otherwise.  */
   29673              : 
   29674              : bool
   29675    256182179 : instantiating_current_function_p (void)
   29676              : {
   29677    256182179 :   return (current_instantiation ()
   29678    256182179 :           && (current_instantiation ()->maybe_get_node ()
   29679      2893738 :               == current_function_decl));
   29680              : }
   29681              : 
   29682              : /* [temp.param] Check that template non-type parm TYPE is of an allowable
   29683              :    type.  Return false for ok, true for disallowed.  Issue error and
   29684              :    inform messages under control of COMPLAIN.  */
   29685              : 
   29686              : static bool
   29687    185686885 : invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
   29688              : {
   29689    185686885 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
   29690              :     return false;
   29691      2739840 :   else if (TYPE_PTR_P (type))
   29692              :     return false;
   29693      2734810 :   else if (TYPE_REF_P (type)
   29694      2734810 :            && !TYPE_REF_IS_RVALUE (type))
   29695              :     return false;
   29696      2733504 :   else if (TYPE_PTRMEM_P (type))
   29697              :     return false;
   29698      2729392 :   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
   29699              :     {
   29700      2429389 :       if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
   29701              :         {
   29702            1 :           if (complain & tf_error)
   29703            1 :             error ("non-type template parameters of deduced class type only "
   29704              :                    "available with %<-std=c++20%> or %<-std=gnu++20%>");
   29705              :           return true;
   29706              :         }
   29707              :       return false;
   29708              :     }
   29709       300003 :   else if (TREE_CODE (type) == NULLPTR_TYPE)
   29710              :     return false;
   29711       299944 :   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
   29712           24 :            && cxx_dialect < cxx11)
   29713              :     /* Fall through; before C++11 alias templates, a bound ttp
   29714              :        always instantiates into a class type.  */;
   29715       299944 :   else if (WILDCARD_TYPE_P (type))
   29716              :     /* Any other wildcard type not already handled above is allowed.  */
   29717              :     return false;
   29718              :   else if (TREE_CODE (type) == COMPLEX_TYPE)
   29719              :     /* Fall through.  */;
   29720              :   else if (VOID_TYPE_P (type))
   29721              :     /* Fall through.  */;
   29722        80674 :   else if (cxx_dialect >= cxx20)
   29723              :     {
   29724        80621 :       if (dependent_type_p (type))
   29725              :         return false;
   29726        80535 :       if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
   29727              :         return true;
   29728        80502 :       if (structural_type_p (type))
   29729              :         return false;
   29730           52 :       if (complain & tf_error)
   29731              :         {
   29732           11 :           auto_diagnostic_group d;
   29733           11 :           error ("%qT is not a valid type for a template non-type "
   29734              :                  "parameter because it is not structural", type);
   29735           11 :           structural_type_p (type, true);
   29736           11 :         }
   29737              :       return true;
   29738              :     }
   29739           53 :   else if (CLASS_TYPE_P (type))
   29740              :     {
   29741            9 :       if (complain & tf_error)
   29742            7 :         error ("non-type template parameters of class type only available "
   29743              :                "with %<-std=c++20%> or %<-std=gnu++20%>");
   29744              :       return true;
   29745              :     }
   29746              : 
   29747          143 :   if (complain & tf_error)
   29748              :     {
   29749          128 :       if (type == error_mark_node)
   29750           11 :         inform (input_location, "invalid template non-type parameter");
   29751              :       else
   29752          117 :         error ("%q#T is not a valid type for a template non-type parameter",
   29753              :                type);
   29754              :     }
   29755              :   return true;
   29756              : }
   29757              : 
   29758              : /* Returns true iff the noexcept-specifier for TYPE is value-dependent.  */
   29759              : 
   29760              : static bool
   29761     18375303 : value_dependent_noexcept_spec_p (tree type)
   29762              : {
   29763     18375303 :   if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
   29764      8405307 :     if (tree noex = TREE_PURPOSE (spec))
   29765              :       /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
   29766              :          affect overload resolution and treating it as dependent breaks
   29767              :          things.  Same for an unparsed noexcept expression.  */
   29768      8404962 :       if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   29769      8404962 :           && TREE_CODE (noex) != DEFERRED_PARSE
   29770      8404962 :           && value_dependent_expression_p (noex))
   29771              :         return true;
   29772              : 
   29773              :   return false;
   29774              : }
   29775              : 
   29776              : /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
   29777              :    Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
   29778              : 
   29779              : static bool
   29780    621866218 : dependent_type_p_r (tree type)
   29781              : {
   29782    621866218 :   tree scope;
   29783              : 
   29784              :   /* [temp.dep.type]
   29785              : 
   29786              :      A type is dependent if it is:
   29787              : 
   29788              :      -- a template parameter. Template template parameters are types
   29789              :         for us (since TYPE_P holds true for them) so we handle
   29790              :         them here.  */
   29791    621866218 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   29792    621866218 :       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
   29793              :     return true;
   29794              :   /* -- a qualified-id with a nested-name-specifier which contains a
   29795              :         class-name that names a dependent type or whose unqualified-id
   29796              :         names a dependent type.  */
   29797    491018368 :   if (TREE_CODE (type) == TYPENAME_TYPE)
   29798              :     return true;
   29799              : 
   29800              :   /* -- a cv-qualified type where the cv-unqualified type is
   29801              :         dependent.
   29802              :      No code is necessary for this bullet; the code below handles
   29803              :      cv-qualified types, and we don't want to strip aliases with
   29804              :      TYPE_MAIN_VARIANT because of DR 1558.  */
   29805              :   /* -- a compound type constructed from any dependent type.  */
   29806    459471011 :   if (TYPE_PTRMEM_P (type))
   29807       984873 :     return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
   29808       984873 :             || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
   29809              :                                            (type)));
   29810    458486138 :   else if (INDIRECT_TYPE_P (type))
   29811     91516581 :     return dependent_type_p (TREE_TYPE (type));
   29812    366969557 :   else if (FUNC_OR_METHOD_TYPE_P (type))
   29813              :     {
   29814     96348405 :       tree arg_type;
   29815              : 
   29816     96348405 :       if (dependent_type_p (TREE_TYPE (type)))
   29817              :         return true;
   29818     56492747 :       for (arg_type = TYPE_ARG_TYPES (type);
   29819    110536796 :            arg_type;
   29820     54044049 :            arg_type = TREE_CHAIN (arg_type))
   29821     92094766 :         if (dependent_type_p (TREE_VALUE (arg_type)))
   29822              :           return true;
   29823     18442030 :       if (cxx_dialect >= cxx17
   29824     18442030 :           && value_dependent_noexcept_spec_p (type))
   29825              :         /* A value-dependent noexcept-specifier makes the type dependent.  */
   29826              :         return true;
   29827              :       return false;
   29828              :     }
   29829              :   /* -- an array type constructed from any dependent type or whose
   29830              :         size is specified by a constant expression that is
   29831              :         value-dependent.
   29832              : 
   29833              :         We checked for type- and value-dependence of the bounds in
   29834              :         compute_array_index_type, so TYPE_DEPENDENT_P is already set.  */
   29835    270621152 :   if (TREE_CODE (type) == ARRAY_TYPE)
   29836              :     {
   29837        13104 :       if (TYPE_DOMAIN (type)
   29838        13104 :           && dependent_type_p (TYPE_DOMAIN (type)))
   29839              :         return true;
   29840        13104 :       return dependent_type_p (TREE_TYPE (type));
   29841              :     }
   29842              : 
   29843              :   /* -- a template-id in which either the template name is a template
   29844              :      parameter ...  */
   29845    270608048 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   29846              :     return true;
   29847              :   /* ... or any of the template arguments is a dependent type or
   29848              :         an expression that is type-dependent or value-dependent.  */
   29849    134987468 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   29850    398010123 :            && (any_dependent_template_arguments_p
   29851    127670643 :                (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
   29852              :     return true;
   29853              : 
   29854              :   /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
   29855              :      dependent; if the argument of the `typeof' expression is not
   29856              :      type-dependent, then it should already been have resolved.  */
   29857    191820182 :   if (TREE_CODE (type) == TYPEOF_TYPE
   29858    169675103 :       || TREE_CODE (type) == DECLTYPE_TYPE
   29859    162153541 :       || TREE_CODE (type) == TRAIT_TYPE)
   29860              :     return true;
   29861              : 
   29862              :   /* A template argument pack is dependent if any of its packed
   29863              :      arguments are.  */
   29864    161868399 :   if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
   29865              :     {
   29866          241 :       tree args = ARGUMENT_PACK_ARGS (type);
   29867          289 :       for (tree arg : tree_vec_range (args))
   29868          232 :         if (dependent_template_arg_p (arg))
   29869          184 :           return true;
   29870              :     }
   29871              : 
   29872              :   /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
   29873              :      be template parameters.  This includes pack-index-specifiers.  */
   29874    161868215 :   if (TREE_CODE (type) == TYPE_PACK_EXPANSION
   29875    148346718 :       || TREE_CODE (type) == PACK_INDEX_TYPE)
   29876              :     return true;
   29877              : 
   29878    148346519 :   if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
   29879              :     return true;
   29880              : 
   29881              :   /* A splice-scope-specifier is dependent if its splice-specifier
   29882              :      or splice-specialization-specifier is dependent.  */
   29883     60414442 :   if (TREE_CODE (type) == SPLICE_SCOPE)
   29884              :     return true;
   29885              : 
   29886     60414072 :   if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
   29887              :     return true;
   29888              : 
   29889              :   /* The standard does not specifically mention types that are local
   29890              :      to template functions or local classes, but they should be
   29891              :      considered dependent too.  For example:
   29892              : 
   29893              :        template <int I> void f() {
   29894              :          enum E { a = I };
   29895              :          S<sizeof (E)> s;
   29896              :        }
   29897              : 
   29898              :      The size of `E' cannot be known until the value of `I' has been
   29899              :      determined.  Therefore, `E' must be considered dependent.  */
   29900     60414072 :   scope = TYPE_CONTEXT (type);
   29901     60414072 :   if (scope && TYPE_P (scope))
   29902      3227276 :     return dependent_type_p (scope);
   29903              :   /* Don't use type_dependent_expression_p here, as it can lead
   29904              :      to infinite recursion trying to determine whether a lambda
   29905              :      nested in a lambda is dependent (c++/47687).  */
   29906     54258555 :   else if (scope && TREE_CODE (scope) == FUNCTION_DECL
   29907      1407430 :            && DECL_LANG_SPECIFIC (scope)
   29908      1407430 :            && DECL_TEMPLATE_INFO (scope)
   29909      1099721 :            && (any_dependent_template_arguments_p
   29910      1099721 :                (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
   29911              :     return true;
   29912              : 
   29913              :   /* Other types are non-dependent.  */
   29914              :   return false;
   29915              : }
   29916              : 
   29917              : /* Returns TRUE if TYPE is dependent, in the sense of
   29918              :    [temp.dep.type].  Note that a NULL type is considered dependent.  */
   29919              : 
   29920              : bool
   29921  21199525227 : dependent_type_p (tree type)
   29922              : {
   29923              :   /* If there are no template parameters in scope, then there can't be
   29924              :      any dependent types.  */
   29925  21199525227 :   if (!processing_template_decl)
   29926              :     {
   29927              :       /* If we are not processing a template, then nobody should be
   29928              :          providing us with a dependent type.  */
   29929   3968267098 :       gcc_assert (type);
   29930   3968267098 :       gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type)
   29931              :                   || seen_error ());
   29932              :       return false;
   29933              :     }
   29934              : 
   29935              :   /* If the type is NULL, we have not computed a type for the entity
   29936              :      in question; in that case, the type is dependent.  */
   29937  17231258129 :   if (!type)
   29938              :     return true;
   29939              : 
   29940              :   /* Erroneous types can be considered non-dependent.  */
   29941  16932083234 :   if (type == error_mark_node)
   29942              :     return false;
   29943              : 
   29944              :   /* If we have not already computed the appropriate value for TYPE,
   29945              :      do so now.  */
   29946  16932083103 :   if (!TYPE_DEPENDENT_P_VALID (type))
   29947              :     {
   29948    621866218 :       TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
   29949    621866218 :       TYPE_DEPENDENT_P_VALID (type) = 1;
   29950              :     }
   29951              : 
   29952  16932083103 :   return TYPE_DEPENDENT_P (type);
   29953              : }
   29954              : 
   29955              : /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
   29956              :    lookup.  In other words, a dependent type that is not the current
   29957              :    instantiation.  */
   29958              : 
   29959              : bool
   29960   1570626559 : dependent_scope_p (tree scope)
   29961              : {
   29962   1570626559 :   return (scope && TYPE_P (scope) && dependent_type_p (scope)
   29963   2511993833 :           && !currently_open_class (scope));
   29964              : }
   29965              : 
   29966              : /* True if we might find more declarations in SCOPE during instantiation than
   29967              :    we can when parsing the template.  */
   29968              : 
   29969              : bool
   29970    466992897 : dependentish_scope_p (tree scope)
   29971              : {
   29972    466992897 :   return dependent_scope_p (scope) || any_dependent_bases_p (scope);
   29973              : }
   29974              : 
   29975              : /* Returns TRUE if NS is a dependent namespace, in which we can't do any
   29976              :    lookup.  */
   29977              : 
   29978              : bool
   29979    666188702 : dependent_namespace_p (tree ns)
   29980              : {
   29981    666188702 :   if (TREE_CODE (ns) == NAMESPACE_DECL)
   29982    526246671 :     ns = ORIGINAL_NAMESPACE (ns);
   29983    666188702 :   return TREE_CODE (ns) == SPLICE_EXPR;
   29984              : }
   29985              : 
   29986              : /* T is a SCOPE_REF.  Return whether it represents a non-static member of
   29987              :    an unknown base of 'this' (and is therefore instantiation-dependent).  */
   29988              : 
   29989              : static bool
   29990      4395306 : unknown_base_ref_p (tree t)
   29991              : {
   29992      4395306 :   if (!current_class_ptr)
   29993              :     return false;
   29994              : 
   29995      1899596 :   tree mem = TREE_OPERAND (t, 1);
   29996      1899596 :   if (shared_member_p (mem))
   29997              :     return false;
   29998              : 
   29999           13 :   tree cur = current_nonlambda_class_type ();
   30000           13 :   if (!any_dependent_bases_p (cur))
   30001              :     return false;
   30002              : 
   30003            0 :   tree ctx = TREE_OPERAND (t, 0);
   30004            0 :   if (DERIVED_FROM_P (ctx, cur))
   30005            0 :     return false;
   30006              : 
   30007              :   return true;
   30008              : }
   30009              : 
   30010              : /* T is a SCOPE_REF; return whether we need to consider it
   30011              :     instantiation-dependent so that we can check access at instantiation
   30012              :     time even though we know which member it resolves to.  */
   30013              : 
   30014              : static bool
   30015      9713280 : instantiation_dependent_scope_ref_p (tree t)
   30016              : {
   30017      9713280 :   if (DECL_P (TREE_OPERAND (t, 1))
   30018      4395318 :       && CLASS_TYPE_P (TREE_OPERAND (t, 0))
   30019      4395318 :       && !dependent_scope_p (TREE_OPERAND (t, 0))
   30020      4395306 :       && !unknown_base_ref_p (t)
   30021     14108586 :       && accessible_in_template_p (TREE_OPERAND (t, 0),
   30022      4395306 :                                    TREE_OPERAND (t, 1)))
   30023              :     return false;
   30024              :   else
   30025      5318196 :     return true;
   30026              : }
   30027              : 
   30028              : /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
   30029              :    [temp.dep.constexpr].  EXPRESSION is already known to be a constant
   30030              :    expression.  */
   30031              : 
   30032              : /* Note that this predicate is not appropriate for general expressions;
   30033              :    only constant expressions (that satisfy potential_constant_expression)
   30034              :    can be tested for value dependence.  */
   30035              : 
   30036              : bool
   30037   1510211200 : value_dependent_expression_p (tree expression)
   30038              : {
   30039   1541833099 :   if (!processing_template_decl || expression == NULL_TREE)
   30040              :     return false;
   30041              : 
   30042              :   /* A type-dependent expression is also value-dependent.  */
   30043   1212080130 :   if (type_dependent_expression_p (expression))
   30044              :     return true;
   30045              : 
   30046   1044825502 :   switch (TREE_CODE (expression))
   30047              :     {
   30048      1141282 :     case BASELINK:
   30049              :       /* A dependent member function of the current instantiation.  */
   30050      1141282 :       return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
   30051              : 
   30052     31821290 :     case FUNCTION_DECL:
   30053              :       /* A dependent member function of the current instantiation.  */
   30054     63641798 :       if (DECL_CLASS_SCOPE_P (expression)
   30055     52856074 :           && dependent_type_p (DECL_CONTEXT (expression)))
   30056              :         return true;
   30057              :       break;
   30058              : 
   30059              :     case IDENTIFIER_NODE:
   30060              :       /* A name that has not been looked up -- must be dependent.  */
   30061              :       return true;
   30062              : 
   30063              :     case TEMPLATE_PARM_INDEX:
   30064              :       /* A non-type template parm.  */
   30065              :       return true;
   30066              : 
   30067       774987 :     case CONST_DECL:
   30068              :       /* A non-type template parm.  */
   30069       774987 :       if (DECL_TEMPLATE_PARM_P (expression))
   30070              :         return true;
   30071       774987 :       return value_dependent_expression_p (DECL_INITIAL (expression));
   30072              : 
   30073     22691184 :     case VAR_DECL:
   30074              :        /* A constant with literal type and is initialized
   30075              :           with an expression that is value-dependent.  */
   30076     22691184 :       if (DECL_DEPENDENT_INIT_P (expression))
   30077              :         return true;
   30078     12566514 :       if (DECL_HAS_VALUE_EXPR_P (expression))
   30079              :         {
   30080       132080 :           tree value_expr = DECL_VALUE_EXPR (expression);
   30081       132080 :           if (value_dependent_expression_p (value_expr)
   30082              :               /* __PRETTY_FUNCTION__ inside a template function is dependent
   30083              :                  on the name of the function.  */
   30084       132080 :               || (DECL_PRETTY_FUNCTION_P (expression)
   30085              :                   /* It might be used in a template, but not a template
   30086              :                      function, in which case its DECL_VALUE_EXPR will be
   30087              :                      "top level".  */
   30088            5 :                   && value_expr == error_mark_node))
   30089              :             return true;
   30090              :         }
   30091     12434434 :       else if (TYPE_REF_P (TREE_TYPE (expression)))
   30092              :         /* FIXME cp_finish_decl doesn't fold reference initializers.  */
   30093              :         return true;
   30094              :       /* We have a constexpr variable and we're processing a template.  When
   30095              :          there's lifetime extension involved (for which finish_compound_literal
   30096              :          used to create a temporary), we'll not be able to evaluate the
   30097              :          variable until instantiating, so pretend it's value-dependent.  */
   30098     12421707 :       else if (DECL_DECLARED_CONSTEXPR_P (expression)
   30099     12421707 :                && !TREE_CONSTANT (expression))
   30100              :         return true;
   30101              :       return false;
   30102              : 
   30103     34844386 :     case DYNAMIC_CAST_EXPR:
   30104     34844386 :     case STATIC_CAST_EXPR:
   30105     34844386 :     case CONST_CAST_EXPR:
   30106     34844386 :     case REINTERPRET_CAST_EXPR:
   30107     34844386 :     case CAST_EXPR:
   30108     34844386 :     case IMPLICIT_CONV_EXPR:
   30109              :       /* These expressions are value-dependent if the type to which
   30110              :          the cast occurs is dependent or the expression being casted
   30111              :          is value-dependent.  */
   30112     34844386 :       {
   30113     34844386 :         tree type = TREE_TYPE (expression);
   30114              : 
   30115     34844386 :         if (dependent_type_p (type))
   30116              :           return true;
   30117              : 
   30118              :         /* A functional cast has a list of operands.  */
   30119     34844386 :         expression = TREE_OPERAND (expression, 0);
   30120     34844386 :         if (!expression)
   30121              :           {
   30122              :             /* If there are no operands, it must be an expression such
   30123              :                as "int()". This should not happen for aggregate types
   30124              :                because it would form non-constant expressions.  */
   30125          156 :             gcc_assert (cxx_dialect >= cxx11
   30126              :                         || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
   30127              : 
   30128              :             return false;
   30129              :           }
   30130              : 
   30131     34844230 :         if (TREE_CODE (expression) == TREE_LIST)
   30132      3997292 :           return any_value_dependent_elements_p (expression);
   30133              : 
   30134     30846938 :         if (TREE_CODE (type) == REFERENCE_TYPE
   30135     30846938 :             && has_value_dependent_address (expression))
   30136              :           return true;
   30137              : 
   30138              :         return value_dependent_expression_p (expression);
   30139              :       }
   30140              : 
   30141     10560119 :     case SIZEOF_EXPR:
   30142     10560119 :       if (SIZEOF_EXPR_TYPE_P (expression))
   30143            0 :         return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
   30144     10560119 :       if (tree p = TREE_OPERAND (expression, 0))
   30145     10560119 :         if (PACK_EXPANSION_P (p)
   30146     10560119 :             && DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (p)))
   30147              :           {
   30148          822 :             tree d = PACK_EXPANSION_PATTERN (p);
   30149          822 :             if (DECL_HAS_VALUE_EXPR_P (d))
   30150              :               {
   30151          822 :                 d = DECL_VALUE_EXPR (d);
   30152              :                 /* [temp.dep.constexpr]/4:
   30153              :                    Expressions of the following form are value-dependent:
   30154              :                    sizeof ... ( identifier )
   30155              :                    unless the identifier is a structured binding pack whose
   30156              :                    initializer is not dependent.  */
   30157          822 :                 if (TREE_CODE (d) == TREE_VEC
   30158          822 :                     && !type_dependent_expression_p (TREE_VEC_ELT (d, 0)))
   30159              :                   return false;
   30160              :               }
   30161              :           }
   30162              :       /* FALLTHRU */
   30163     11387107 :     case ALIGNOF_EXPR:
   30164     11387107 :     case TYPEID_EXPR:
   30165              :       /* A `sizeof' expression is value-dependent if the operand is
   30166              :          type-dependent or is a pack expansion.  */
   30167     11387107 :       expression = TREE_OPERAND (expression, 0);
   30168     11387107 :       if (PACK_EXPANSION_P (expression))
   30169              :         return true;
   30170      6191558 :       else if (TYPE_P (expression))
   30171      6091751 :         return dependent_type_p (expression);
   30172        99807 :       return instantiation_dependent_uneval_expression_p (expression);
   30173              : 
   30174            0 :     case AT_ENCODE_EXPR:
   30175              :       /* An 'encode' expression is value-dependent if the operand is
   30176              :          type-dependent.  */
   30177            0 :       expression = TREE_OPERAND (expression, 0);
   30178            0 :       return dependent_type_p (expression);
   30179              : 
   30180       639738 :     case NOEXCEPT_EXPR:
   30181       639738 :       expression = TREE_OPERAND (expression, 0);
   30182       639738 :       return instantiation_dependent_uneval_expression_p (expression);
   30183              : 
   30184      3030167 :     case SCOPE_REF:
   30185              :       /* All instantiation-dependent expressions should also be considered
   30186              :          value-dependent.  */
   30187      3030167 :       return instantiation_dependent_scope_ref_p (expression);
   30188              : 
   30189      1523619 :     case COMPONENT_REF:
   30190      1523619 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   30191      1523619 :               || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
   30192              : 
   30193            0 :     case NONTYPE_ARGUMENT_PACK:
   30194              :       /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
   30195              :          is value-dependent.  */
   30196            0 :       for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
   30197            0 :         if (value_dependent_expression_p (arg))
   30198            0 :           return true;
   30199            0 :       return false;
   30200              : 
   30201     21004100 :     case TRAIT_EXPR:
   30202     21004100 :       {
   30203     21004100 :         if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
   30204              :           return true;
   30205              : 
   30206        49933 :         tree type2 = TRAIT_EXPR_TYPE2 (expression);
   30207        49933 :         if (!type2)
   30208              :           return false;
   30209              : 
   30210        49929 :         if (TREE_CODE (type2) != TREE_VEC)
   30211        49885 :           return dependent_type_p (type2);
   30212              : 
   30213           56 :         for (tree arg : tree_vec_range (type2))
   30214           41 :           if (dependent_type_p (arg))
   30215           29 :             return true;
   30216              : 
   30217           15 :         return false;
   30218              :       }
   30219              : 
   30220           30 :     case MODOP_EXPR:
   30221           30 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   30222           30 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
   30223              : 
   30224      1018171 :     case ARRAY_REF:
   30225      1018171 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   30226      1018171 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
   30227              : 
   30228       257959 :     case ADDR_EXPR:
   30229       257959 :       {
   30230       257959 :         tree op = TREE_OPERAND (expression, 0);
   30231       257959 :         return (value_dependent_expression_p (op)
   30232       257959 :                 || has_value_dependent_address (op));
   30233              :       }
   30234              : 
   30235              :     case REQUIRES_EXPR:
   30236              :       /* Treat all requires-expressions as value-dependent so
   30237              :          we don't try to fold them.  */
   30238              :       return true;
   30239              : 
   30240            0 :     case TYPE_REQ:
   30241            0 :       return dependent_type_p (TREE_OPERAND (expression, 0));
   30242              : 
   30243      5244304 :     case CALL_EXPR:
   30244      5244304 :       {
   30245      5244304 :         if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
   30246              :           return true;
   30247      4675764 :         tree fn = get_callee_fndecl (expression);
   30248      4675764 :         int i, nargs;
   30249      4675764 :         nargs = call_expr_nargs (expression);
   30250      5999091 :         for (i = 0; i < nargs; ++i)
   30251              :           {
   30252      2289360 :             tree op = CALL_EXPR_ARG (expression, i);
   30253              :             /* In a call to a constexpr member function, look through the
   30254              :                implicit ADDR_EXPR on the object argument so that it doesn't
   30255              :                cause the call to be considered value-dependent.  We also
   30256              :                look through it in potential_constant_expression.  */
   30257        14620 :             if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
   30258        14620 :                 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   30259      2289364 :                 && TREE_CODE (op) == ADDR_EXPR)
   30260            2 :               op = TREE_OPERAND (op, 0);
   30261      2289360 :             if (value_dependent_expression_p (op))
   30262              :               return true;
   30263              :           }
   30264      3781169 :         if (flag_reflection && !fn && CALL_EXPR_FN (expression))
   30265              :           {
   30266        71438 :             fn = MAYBE_BASELINK_FUNCTIONS (CALL_EXPR_FN (expression));
   30267        71438 :             if (fn && TREE_CODE (fn) != FUNCTION_DECL)
   30268         7530 :               fn = NULL_TREE;
   30269              :           }
   30270              :         /* [meta.reflection.access.context]/8: An invocation of current that
   30271              :            appears at a program point P is value-dependent if eval-point(P)
   30272              :            is enclosed by a scope corresponding to a templated entity.  */
   30273      3709731 :         if (flag_reflection
   30274        72654 :             && fn
   30275        65124 :             && metafunction_p (fn))
   30276              :           {
   30277         2602 :             if (id_equal (DECL_NAME (fn), "current")
   30278           32 :                 && DECL_CLASS_SCOPE_P (fn)
   30279         2634 :                 && id_equal (TYPE_IDENTIFIER (DECL_CONTEXT (fn)),
   30280              :                              "access_context"))
   30281              :               return true;
   30282              :             /* Similarly for these 3 metafns.  */
   30283         2570 :             if (id_equal (DECL_NAME (fn), "current_function")
   30284         2570 :                 || id_equal (DECL_NAME (fn), "current_class")
   30285         5140 :                 || id_equal (DECL_NAME (fn), "current_namespace"))
   30286              :               return true;
   30287              :           }
   30288              : 
   30289              :         return false;
   30290              :       }
   30291              : 
   30292      9973934 :     case TEMPLATE_ID_EXPR:
   30293      9973934 :       return concept_definition_p (TREE_OPERAND (expression, 0))
   30294      9973934 :         && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
   30295              : 
   30296      3927908 :     case CONSTRUCTOR:
   30297      3927908 :       {
   30298      3927908 :         unsigned ix;
   30299      3927908 :         tree val;
   30300      3927908 :         if (dependent_type_p (TREE_TYPE (expression)))
   30301              :           return true;
   30302      4637825 :         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
   30303       710917 :           if (value_dependent_expression_p (val))
   30304              :             return true;
   30305              :         return false;
   30306              :       }
   30307              : 
   30308              :     case STMT_EXPR:
   30309              :       /* Treat a GNU statement expression as dependent to avoid crashing
   30310              :          under instantiate_non_dependent_expr; it can't be constant.  */
   30311              :       return true;
   30312              : 
   30313            8 :     case NEW_EXPR:
   30314            8 :     case VEC_NEW_EXPR:
   30315              :       /* The second operand is a type, which type_dependent_expression_p
   30316              :          (and therefore value_dependent_expression_p) doesn't want to see.  */
   30317            8 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   30318            8 :               || value_dependent_expression_p (TREE_OPERAND (expression, 2))
   30319           16 :               || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
   30320              : 
   30321        16502 :     case REFLECT_EXPR:
   30322              :       /* [temp.dep.constexpr] A reflect-expression is value-dependent
   30323              :          if it contains a dependent nested-name-specifier, type-id,
   30324              :          namespace-name, or template-name, or if it contains
   30325              :          a value-dependent or type-dependent id-expression.  */
   30326        16502 :       if (REFLECT_EXPR_KIND (expression) == REFLECT_BASE)
   30327              :         /* Direct base relationship isn't value-dependent and calling
   30328              :            uses_template_parms on TREE_BINFO leads to ICEs.  */
   30329              :         return false;
   30330        16274 :       if (REFLECT_EXPR_KIND (expression) == REFLECT_DATA_MEMBER_SPEC)
   30331              :         {
   30332              :           /* Data member description is value dependent if the type is
   30333              :              dependent, other optional fields shouldn't be ever dependent.  */
   30334          272 :           tree h = REFLECT_EXPR_HANDLE (expression);
   30335          272 :           return dependent_type_p (TREE_VEC_ELT (h, 0));
   30336              :         }
   30337        16002 :       return uses_template_parms (REFLECT_EXPR_HANDLE (expression));
   30338              : 
   30339    767309026 :     default:
   30340              :       /* A constant expression is value-dependent if any subexpression is
   30341              :          value-dependent.  */
   30342    767309026 :       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
   30343              :         {
   30344     80788030 :         case tcc_reference:
   30345     80788030 :         case tcc_unary:
   30346     80788030 :         case tcc_comparison:
   30347     80788030 :         case tcc_binary:
   30348     80788030 :         case tcc_expression:
   30349     80788030 :         case tcc_vl_exp:
   30350     80788030 :           {
   30351     80788030 :             int i, len = cp_tree_operand_length (expression);
   30352              : 
   30353    218913842 :             for (i = 0; i < len; i++)
   30354              :               {
   30355     92114282 :                 tree t = TREE_OPERAND (expression, i);
   30356              : 
   30357              :                 /* In some cases, some of the operands may be missing.
   30358              :                    (For example, in the case of PREDECREMENT_EXPR, the
   30359              :                    amount to increment by may be missing.)  That doesn't
   30360              :                    make the expression dependent.  */
   30361     92114282 :                 if (t && value_dependent_expression_p (t))
   30362              :                   return true;
   30363              :               }
   30364              :           }
   30365              :           break;
   30366              :         default:
   30367              :           break;
   30368              :         }
   30369              :       break;
   30370              :     }
   30371              : 
   30372              :   /* The expression is not value-dependent.  */
   30373              :   return false;
   30374              : }
   30375              : 
   30376              : /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
   30377              :    [temp.dep.expr].  Note that an expression with no type is
   30378              :    considered dependent.  Other parts of the compiler arrange for an
   30379              :    expression with type-dependent subexpressions to have no type, so
   30380              :    this function doesn't have to be fully recursive.  */
   30381              : 
   30382              : bool
   30383   4782694366 : type_dependent_expression_p (tree expression)
   30384              : {
   30385   4782700118 :   if (!processing_template_decl)
   30386              :     return false;
   30387              : 
   30388   4013263996 :   if (expression == NULL_TREE || expression == error_mark_node)
   30389              :     return false;
   30390              : 
   30391   4012096146 :   gcc_checking_assert (!TYPE_P (expression));
   30392              : 
   30393   4012096146 :   STRIP_ANY_LOCATION_WRAPPER (expression);
   30394              : 
   30395              :   /* Assume a TU-local entity is not dependent, we'll error later when
   30396              :      instantiating anyway.  */
   30397   4012096146 :   if (TREE_CODE (expression) == TU_LOCAL_ENTITY)
   30398              :     return false;
   30399              : 
   30400              :   /* An unresolved name is always dependent.  */
   30401              :   if (identifier_p (expression)
   30402              :       || TREE_CODE (expression) == USING_DECL)
   30403              :     return true;
   30404              : 
   30405              :   /* A lambda-expression in template context is dependent.  dependent_type_p is
   30406              :      true for a lambda in the scope of a class or function template, but that
   30407              :      doesn't cover all template contexts, like a default template argument.  */
   30408              :   if (TREE_CODE (expression) == LAMBDA_EXPR)
   30409              :     return true;
   30410              : 
   30411              :   /* A fold expression is type-dependent. */
   30412              :   if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
   30413              :       || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
   30414              :       || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
   30415              :       || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
   30416              :     return true;
   30417              : 
   30418              :   /* Some expression forms are never type-dependent.  */
   30419              :   if (TREE_CODE (expression) == SIZEOF_EXPR
   30420              :       || TREE_CODE (expression) == ALIGNOF_EXPR
   30421              :       || TREE_CODE (expression) == AT_ENCODE_EXPR
   30422              :       || TREE_CODE (expression) == NOEXCEPT_EXPR
   30423              :       || TREE_CODE (expression) == TRAIT_EXPR
   30424              :       || TREE_CODE (expression) == TYPEID_EXPR
   30425              :       || TREE_CODE (expression) == DELETE_EXPR
   30426              :       || TREE_CODE (expression) == VEC_DELETE_EXPR
   30427              :       || TREE_CODE (expression) == THROW_EXPR
   30428              :       || TREE_CODE (expression) == REQUIRES_EXPR
   30429              :       || REFLECT_EXPR_P (expression))
   30430              :     return false;
   30431              : 
   30432              :   /* The types of these expressions depends only on the type to which
   30433              :      the cast occurs.  */
   30434              :   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
   30435              :       || TREE_CODE (expression) == STATIC_CAST_EXPR
   30436              :       || TREE_CODE (expression) == CONST_CAST_EXPR
   30437              :       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
   30438              :       || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
   30439              :       || TREE_CODE (expression) == CAST_EXPR)
   30440     71382658 :     return dependent_type_p (TREE_TYPE (expression));
   30441              : 
   30442              :   /* The types of these expressions depends only on the type created
   30443              :      by the expression.  */
   30444              :   if (TREE_CODE (expression) == NEW_EXPR
   30445              :       || TREE_CODE (expression) == VEC_NEW_EXPR)
   30446              :     {
   30447              :       /* For NEW_EXPR tree nodes created inside a template, either
   30448              :          the object type itself or a TREE_LIST may appear as the
   30449              :          operand 1.  */
   30450       797519 :       tree type = TREE_OPERAND (expression, 1);
   30451       797519 :       if (TREE_CODE (type) == TREE_LIST)
   30452              :         /* This is an array type.  We need to check array dimensions
   30453              :            as well.  */
   30454            0 :         return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
   30455            0 :                || value_dependent_expression_p
   30456            0 :                     (TREE_OPERAND (TREE_VALUE (type), 1));
   30457              :       /* Array type whose dimension has to be deduced.  */
   30458       797519 :       else if (TREE_CODE (type) == ARRAY_TYPE
   30459       797519 :                && TREE_OPERAND (expression, 2) == NULL_TREE)
   30460              :         return true;
   30461              :       else
   30462       797507 :         return dependent_type_p (type);
   30463              :     }
   30464              : 
   30465              :   if (TREE_CODE (expression) == SCOPE_REF)
   30466              :     {
   30467    116512694 :       tree scope = TREE_OPERAND (expression, 0);
   30468    116512694 :       tree name = TREE_OPERAND (expression, 1);
   30469              : 
   30470              :       /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
   30471              :          contains an identifier associated by name lookup with one or more
   30472              :          declarations declared with a dependent type, or...a
   30473              :          nested-name-specifier or qualified-id that names a member of an
   30474              :          unknown specialization.  */
   30475    116512694 :       return (type_dependent_expression_p (name)
   30476    116512694 :               || dependent_scope_p (scope));
   30477              :     }
   30478              : 
   30479              :   if (TREE_CODE (expression) == TEMPLATE_DECL
   30480    319420060 :       && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
   30481    319420054 :     return uses_outer_template_parms (expression);
   30482              : 
   30483   3348086445 :   if (TREE_CODE (expression) == STMT_EXPR)
   30484          191 :     expression = stmt_expr_value_expr (expression);
   30485              : 
   30486   3348086445 :   if (BRACE_ENCLOSED_INITIALIZER_P (expression))
   30487              :     {
   30488     13640526 :       for (auto &elt : CONSTRUCTOR_ELTS (expression))
   30489      2024834 :         if (type_dependent_expression_p (elt.value))
   30490              :           return true;
   30491              :       return false;
   30492              :     }
   30493              : 
   30494              :   /* A static data member of the current instantiation with incomplete
   30495              :      array type is type-dependent, as the definition and specializations
   30496              :      can have different bounds.  */
   30497   3338581927 :   if (VAR_P (expression)
   30498    457070407 :       && DECL_CLASS_SCOPE_P (expression)
   30499     54795368 :       && dependent_type_p (DECL_CONTEXT (expression))
   30500   3362317543 :       && VAR_HAD_UNKNOWN_BOUND (expression))
   30501              :     return true;
   30502              : 
   30503              :   /* An array of unknown bound depending on a variadic parameter, eg:
   30504              : 
   30505              :      template<typename... Args>
   30506              :        void foo (Args... args)
   30507              :        {
   30508              :          int arr[] = { args... };
   30509              :        }
   30510              : 
   30511              :      template<int... vals>
   30512              :        void bar ()
   30513              :        {
   30514              :          int arr[] = { vals... };
   30515              :        }
   30516              : 
   30517              :      If the array has no length and has an initializer, it must be that
   30518              :      we couldn't determine its length in cp_complete_array_type because
   30519              :      it is dependent.  */
   30520    457013777 :   if (((VAR_P (expression) && DECL_INITIAL (expression))
   30521   2927543937 :        || COMPOUND_LITERAL_P (expression))
   30522    413696625 :       && TREE_TYPE (expression) != NULL_TREE
   30523    413696625 :       && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
   30524   3339485681 :       && !TYPE_DOMAIN (TREE_TYPE (expression)))
   30525              :    return true;
   30526              : 
   30527              :   /* Pull a FUNCTION_DECL out of a BASELINK if we can.  */
   30528   3338319860 :   if (BASELINK_P (expression))
   30529              :     {
   30530     15949962 :       if (BASELINK_OPTYPE (expression)
   30531     15949962 :           && dependent_type_p (BASELINK_OPTYPE (expression)))
   30532              :         return true;
   30533     15949862 :       expression = BASELINK_FUNCTIONS (expression);
   30534              :     }
   30535              : 
   30536              :   /* A function or variable template-id is type-dependent if it has any
   30537              :      dependent template arguments.  */
   30538   2881511414 :   if (VAR_OR_FUNCTION_DECL_P (expression)
   30539    727050853 :       && DECL_LANG_SPECIFIC (expression)
   30540   3727440899 :       && DECL_TEMPLATE_INFO (expression))
   30541              :     {
   30542              :       /* Consider the innermost template arguments, since those are the ones
   30543              :          that come from the template-id; the template arguments for the
   30544              :          enclosing class do not make it type-dependent unless they are used in
   30545              :          the type of the decl.  */
   30546    167361850 :       if (instantiates_primary_template_p (expression)
   30547    196229826 :           && (any_dependent_template_arguments_p
   30548     28867976 :               (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
   30549              :         return true;
   30550              :     }
   30551              : 
   30552              :   /* Otherwise, if the function decl isn't from a dependent scope, it can't be
   30553              :      type-dependent.  Checking this is important for functions with auto return
   30554              :      type, which looks like a dependent type.  */
   30555   3334517675 :   if (TREE_CODE (expression) == FUNCTION_DECL
   30556    396378858 :       && !(DECL_CLASS_SCOPE_P (expression)
   30557    129938430 :            && dependent_type_p (DECL_CONTEXT (expression)))
   30558    194060763 :       && !(DECL_LANG_SPECIFIC (expression)
   30559    194060763 :            && DECL_UNIQUE_FRIEND_P (expression)
   30560      3969430 :            && (!DECL_FRIEND_CONTEXT (expression)
   30561      3884532 :                || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
   30562   3528343360 :       && !DECL_LOCAL_DECL_P (expression))
   30563              :     {
   30564    193711418 :       gcc_assert (!dependent_type_p (TREE_TYPE (expression))
   30565              :                   || undeduced_auto_decl (expression));
   30566              :       return false;
   30567              :     }
   30568              : 
   30569              :   /* Otherwise, its constraints could still depend on outer template parameters
   30570              :      from its (dependent) scope.  */
   30571   3140806257 :   if (TREE_CODE (expression) == FUNCTION_DECL
   30572              :       /* As an optimization, check this cheaper sufficient condition first.
   30573              :          (At this point we've established that we're looking at a member of
   30574              :          a dependent class, so it makes sense to start treating say undeduced
   30575              :          auto as dependent.)  */
   30576     72729010 :       && !dependent_type_p (TREE_TYPE (expression))
   30577   3144421858 :       && uses_outer_template_parms_in_constraints (expression))
   30578              :     return true;
   30579              : 
   30580              :   /* Always dependent, on the number of arguments if nothing else.  */
   30581   3140806251 :   if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
   30582              :     return true;
   30583              : 
   30584              :   /* [temp.dep.expr]: "A pack-index-expression is type-dependent if its
   30585              :      id-expression is type-dependent."  */
   30586   3135900774 :   if (TREE_CODE (expression) == PACK_INDEX_EXPR)
   30587         5599 :     return type_dependent_expression_p (PACK_INDEX_PACK (expression));
   30588              : 
   30589   3135895175 :   if (TREE_TYPE (expression) == unknown_type_node)
   30590              :     {
   30591    226839883 :       if (TREE_CODE (expression) == ADDR_EXPR)
   30592          153 :         return type_dependent_expression_p (TREE_OPERAND (expression, 0));
   30593    226839730 :       if (TREE_CODE (expression) == COMPONENT_REF
   30594    179675148 :           || TREE_CODE (expression) == OFFSET_REF)
   30595              :         {
   30596     47164753 :           if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0)))
   30597              :             return true;
   30598     46773216 :           expression = TREE_OPERAND (expression, 1);
   30599     46773216 :           if (identifier_p (expression))
   30600              :             return false;
   30601              :         }
   30602              :       /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
   30603    226448193 :       if (TREE_CODE (expression) == SCOPE_REF)
   30604              :         return false;
   30605              : 
   30606    226448193 :       if (BASELINK_P (expression))
   30607              :         {
   30608     46773216 :           if (BASELINK_OPTYPE (expression)
   30609     46773216 :               && dependent_type_p (BASELINK_OPTYPE (expression)))
   30610              :             return true;
   30611     46773210 :           expression = BASELINK_FUNCTIONS (expression);
   30612              :         }
   30613              : 
   30614    226448187 :       if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
   30615              :         {
   30616     55319079 :           tree args = TREE_OPERAND (expression, 1);
   30617     55319079 :           if (any_dependent_template_arguments_p (args))
   30618              :             return true;
   30619              :           /* Arguments of a function template-id aren't necessarily coerced
   30620              :              yet so we must conservatively assume that the address (and not
   30621              :              just value) of the argument matters as per [temp.dep.temp]/3.  */
   30622     10648420 :           for (tree arg : tree_vec_range (args))
   30623      5973501 :             if (has_value_dependent_address (arg))
   30624        19293 :               return true;
   30625      4674919 :           expression = TREE_OPERAND (expression, 0);
   30626    317077975 :           if (identifier_p (expression))
   30627              :             return true;
   30628              :         }
   30629              : 
   30630    175804021 :       gcc_assert (OVL_P (expression));
   30631              : 
   30632    548489480 :       for (lkp_iterator iter (expression); iter; ++iter)
   30633    428939731 :         if (type_dependent_expression_p (*iter))
   30634     56254272 :           return true;
   30635              : 
   30636    119549749 :       return false;
   30637              :     }
   30638              : 
   30639              :   /* The type of a non-type template parm declared with a placeholder type
   30640              :      depends on the corresponding template argument, even though
   30641              :      placeholders are not normally considered dependent.  */
   30642   2909055292 :   if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
   30643   2909055292 :       && is_auto (TREE_TYPE (expression)))
   30644              :     return true;
   30645              : 
   30646   2908791766 :   gcc_assert (TREE_CODE (expression) != TYPE_DECL);
   30647              : 
   30648              :   /* Dependent type attributes might not have made it from the decl to
   30649              :      the type yet.  */
   30650   2908791766 :   if (DECL_P (expression)
   30651   2908791766 :       && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
   30652              :     return true;
   30653              : 
   30654   2908791721 :   return (dependent_type_p (TREE_TYPE (expression)));
   30655              : }
   30656              : 
   30657              : /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
   30658              :    type-dependent if the expression refers to a member of the current
   30659              :    instantiation and the type of the referenced member is dependent, or the
   30660              :    class member access expression refers to a member of an unknown
   30661              :    specialization.
   30662              : 
   30663              :    This function returns true if the OBJECT in such a class member access
   30664              :    expression is of an unknown specialization.  */
   30665              : 
   30666              : bool
   30667    361139262 : type_dependent_object_expression_p (tree object)
   30668              : {
   30669              :   /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
   30670              :      dependent.  */
   30671    361139262 :   if (TREE_CODE (object) == IDENTIFIER_NODE)
   30672              :     return true;
   30673    361139262 :   tree scope = TREE_TYPE (object);
   30674    361139262 :   return (!scope || dependent_scope_p (scope));
   30675              : }
   30676              : 
   30677              : /* walk_tree callback function for instantiation_dependent_expression_p,
   30678              :    below.  Returns non-zero if a dependent subexpression is found.  */
   30679              : 
   30680              : static tree
   30681    483514032 : instantiation_dependent_r (tree *tp, int *walk_subtrees,
   30682              :                            void * /*data*/)
   30683              : {
   30684    483514032 :   if (TYPE_P (*tp))
   30685              :     {
   30686              :       /* We don't have to worry about decltype currently because decltype
   30687              :          of an instantiation-dependent expr is a dependent type.  This
   30688              :          might change depending on the resolution of DR 1172.  */
   30689     24327463 :       *walk_subtrees = false;
   30690     24327463 :       return NULL_TREE;
   30691              :     }
   30692    459186569 :   enum tree_code code = TREE_CODE (*tp);
   30693    459186569 :   switch (code)
   30694              :     {
   30695              :       /* Don't treat an argument list as dependent just because it has no
   30696              :          TREE_TYPE.  */
   30697              :     case TREE_LIST:
   30698              :     case TREE_VEC:
   30699              :     case NONTYPE_ARGUMENT_PACK:
   30700              :       return NULL_TREE;
   30701              : 
   30702     36817511 :     case TEMPLATE_PARM_INDEX:
   30703     36817511 :       if (dependent_type_p (TREE_TYPE (*tp)))
   30704        14367 :         return *tp;
   30705     36803144 :       if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
   30706       401625 :         return *tp;
   30707              :       /* We'll check value-dependence separately.  */
   30708              :       return NULL_TREE;
   30709              : 
   30710              :       /* Handle expressions with type operands.  */
   30711      4838658 :     case SIZEOF_EXPR:
   30712      4838658 :     case ALIGNOF_EXPR:
   30713      4838658 :     case TYPEID_EXPR:
   30714      4838658 :     case AT_ENCODE_EXPR:
   30715      4838658 :       {
   30716      4838658 :         tree op = TREE_OPERAND (*tp, 0);
   30717      4838658 :         if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
   30718            0 :           op = TREE_TYPE (op);
   30719      4838658 :         else if (code == SIZEOF_EXPR
   30720      4724059 :                  && PACK_EXPANSION_P (op)
   30721      7438536 :                  && DECL_DECOMPOSITION_P (PACK_EXPANSION_PATTERN (op)))
   30722              :           {
   30723          604 :             tree d = PACK_EXPANSION_PATTERN (op);
   30724          604 :             if (DECL_HAS_VALUE_EXPR_P (d))
   30725              :               {
   30726          604 :                 d = DECL_VALUE_EXPR (d);
   30727          604 :                 if (TREE_CODE (d) == TREE_VEC
   30728          604 :                     && !type_dependent_expression_p (TREE_VEC_ELT (d, 0)))
   30729              :                   {
   30730          392 :                     *walk_subtrees = 0;
   30731          392 :                     return NULL_TREE;
   30732              :                   }
   30733              :               }
   30734              :           }
   30735      4838266 :         if (TYPE_P (op))
   30736              :           {
   30737      4760182 :             if (dependent_type_p (op))
   30738      4113873 :               return *tp;
   30739              :             else
   30740              :               {
   30741       646309 :                 *walk_subtrees = false;
   30742       646309 :                 return NULL_TREE;
   30743              :               }
   30744              :           }
   30745              :         break;
   30746              :       }
   30747              : 
   30748       552309 :     case COMPONENT_REF:
   30749       884974 :       if (identifier_p (TREE_OPERAND (*tp, 1)))
   30750              :         /* In a template, finish_class_member_access_expr creates a
   30751              :            COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
   30752              :            type-dependent, so that we can check access control at
   30753              :            instantiation time (PR 42277).  See also Core issue 1273.  */
   30754              :         return *tp;
   30755              :       break;
   30756              : 
   30757      6683113 :     case SCOPE_REF:
   30758      6683113 :       if (instantiation_dependent_scope_ref_p (*tp))
   30759      5318174 :         return *tp;
   30760              :       else
   30761              :         break;
   30762              : 
   30763              :       /* Treat statement-expressions as dependent.  */
   30764              :     case BIND_EXPR:
   30765              :       return *tp;
   30766              : 
   30767              :       /* Treat requires-expressions as dependent. */
   30768              :     case REQUIRES_EXPR:
   30769              :       return *tp;
   30770              : 
   30771      5544638 :     case CONSTRUCTOR:
   30772      5544638 :       if (CONSTRUCTOR_IS_DEPENDENT (*tp))
   30773              :         return *tp;
   30774              :       break;
   30775              : 
   30776     35322546 :     case TEMPLATE_DECL:
   30777     35322546 :     case FUNCTION_DECL:
   30778              :       /* Before C++17, a noexcept-specifier isn't part of the function type
   30779              :          so it doesn't affect type dependence, but we still want to consider it
   30780              :          for instantiation dependence.  */
   30781     35322546 :       if (cxx_dialect < cxx17
   30782        11391 :           && DECL_DECLARES_FUNCTION_P (*tp)
   30783     35333936 :           && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
   30784          463 :         return *tp;
   30785              :       break;
   30786              : 
   30787              :     default:
   30788              :       break;
   30789              :     }
   30790              : 
   30791    404492532 :   if (type_dependent_expression_p (*tp))
   30792     30765715 :     return *tp;
   30793              :   else
   30794              :     return NULL_TREE;
   30795              : }
   30796              : 
   30797              : /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
   30798              :    sense defined by the ABI:
   30799              : 
   30800              :    "An expression is instantiation-dependent if it is type-dependent
   30801              :    or value-dependent, or it has a subexpression that is type-dependent
   30802              :    or value-dependent."
   30803              : 
   30804              :    Except don't actually check value-dependence for unevaluated expressions,
   30805              :    because in sizeof(i) we don't care about the value of i.  Checking
   30806              :    type-dependence will in turn check value-dependence of array bounds/template
   30807              :    arguments as needed.  */
   30808              : 
   30809              : bool
   30810   1558175828 : instantiation_dependent_uneval_expression_p (tree expression)
   30811              : {
   30812   1558175828 :   tree result;
   30813              : 
   30814   1558175828 :   if (!processing_template_decl)
   30815              :     return false;
   30816              : 
   30817    397735001 :   if (expression == error_mark_node)
   30818              :     return false;
   30819              : 
   30820    397734987 :   result = cp_walk_tree_without_duplicates (&expression,
   30821              :                                             instantiation_dependent_r, NULL);
   30822    397734987 :   return result != NULL_TREE;
   30823              : }
   30824              : 
   30825              : /* As above, but also check value-dependence of the expression as a whole.  */
   30826              : 
   30827              : bool
   30828   1473698538 : instantiation_dependent_expression_p (tree expression)
   30829              : {
   30830   1473698538 :   return (instantiation_dependent_uneval_expression_p (expression)
   30831   1473698538 :           || (processing_template_decl
   30832    318025156 :               && potential_constant_expression (expression)
   30833    318025007 :               && value_dependent_expression_p (expression)));
   30834              : }
   30835              : 
   30836              : /* Like type_dependent_expression_p, but it also works while not processing
   30837              :    a template definition, i.e. during substitution or mangling.  */
   30838              : 
   30839              : bool
   30840      9119572 : type_dependent_expression_p_push (tree expr)
   30841              : {
   30842      9119572 :   bool b;
   30843      9119572 :   ++processing_template_decl;
   30844      9119572 :   b = type_dependent_expression_p (expr);
   30845      9119572 :   --processing_template_decl;
   30846      9119572 :   return b;
   30847              : }
   30848              : 
   30849              : /* Returns TRUE if ARGS contains a type-dependent expression.  */
   30850              : 
   30851              : bool
   30852    139008532 : any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
   30853              : {
   30854    139008532 :   if (!processing_template_decl || !args)
   30855              :     return false;
   30856              : 
   30857    135138505 :   for (tree arg : *args)
   30858    104947103 :     if (type_dependent_expression_p (arg))
   30859              :       return true;
   30860              : 
   30861              :   return false;
   30862              : }
   30863              : 
   30864              : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   30865              :    expressions) contains any type-dependent expressions.  */
   30866              : 
   30867              : bool
   30868            0 : any_type_dependent_elements_p (const_tree list)
   30869              : {
   30870            0 :   for (; list; list = TREE_CHAIN (list))
   30871            0 :     if (type_dependent_expression_p (TREE_VALUE (list)))
   30872              :       return true;
   30873              : 
   30874              :   return false;
   30875              : }
   30876              : 
   30877              : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   30878              :    expressions) contains any value-dependent expressions.  */
   30879              : 
   30880              : bool
   30881      4019001 : any_value_dependent_elements_p (const_tree list)
   30882              : {
   30883      6211343 :   for (; list; list = TREE_CHAIN (list))
   30884      4019021 :     if (value_dependent_expression_p (TREE_VALUE (list)))
   30885              :       return true;
   30886              : 
   30887              :   return false;
   30888              : }
   30889              : 
   30890              : /* Returns TRUE if the ARG (a template argument) is dependent.  */
   30891              : 
   30892              : bool
   30893   5369272448 : dependent_template_arg_p (tree arg)
   30894              : {
   30895   5369272448 :   if (!processing_template_decl)
   30896              :     return false;
   30897              : 
   30898              :   /* Assume a template argument that was wrongly written by the user
   30899              :      is dependent. This is consistent with what
   30900              :      any_dependent_template_arguments_p [that calls this function]
   30901              :      does.  */
   30902   4950978093 :   if (!arg || arg == error_mark_node)
   30903              :     return true;
   30904              : 
   30905   4950977491 :   if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   30906            0 :     arg = argument_pack_select_arg (arg);
   30907              : 
   30908   4950977491 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   30909              :     return true;
   30910   4950616325 :   if (TREE_CODE (arg) == TEMPLATE_DECL)
   30911              :     {
   30912     15340353 :       if (DECL_TEMPLATE_PARM_P (arg))
   30913              :         return true;
   30914              :       /* A member template of a dependent class is not necessarily
   30915              :          type-dependent, but it is a dependent template argument because it
   30916              :          will be a member of an unknown specialization to that template.  */
   30917     15335579 :       tree scope = CP_DECL_CONTEXT (arg);
   30918     15335579 :       return TYPE_P (scope) && dependent_type_p (scope);
   30919              :     }
   30920   4935275972 :   else if (ARGUMENT_PACK_P (arg))
   30921              :     {
   30922    207586865 :       tree args = ARGUMENT_PACK_ARGS (arg);
   30923    440962823 :       for (tree arg : tree_vec_range (args))
   30924    300498947 :         if (dependent_template_arg_p (arg))
   30925     67122989 :           return true;
   30926    140463876 :       return false;
   30927              :     }
   30928   4727689107 :   else if (TYPE_P (arg))
   30929   4179189986 :     return dependent_type_p (arg);
   30930              :   else
   30931    548499121 :     return value_dependent_expression_p (arg);
   30932              : }
   30933              : 
   30934              : /* Identify any expressions that use function parms.  */
   30935              : 
   30936              : static tree
   30937    525751876 : find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
   30938              : {
   30939    525751876 :   tree t = *tp;
   30940    525751876 :   if (TREE_CODE (t) == PARM_DECL)
   30941              :     {
   30942       367939 :       *walk_subtrees = 0;
   30943       367939 :       return t;
   30944              :     }
   30945              :   return NULL_TREE;
   30946              : }
   30947              : 
   30948              : /* Returns true if a type specialization formed using the template
   30949              :    arguments ARGS needs to use structural equality.  */
   30950              : 
   30951              : bool
   30952    109341167 : any_template_arguments_need_structural_equality_p (tree args)
   30953              : {
   30954    109341167 :   int i;
   30955    109341167 :   int j;
   30956              : 
   30957    109341167 :   if (!args)
   30958              :     return false;
   30959    109341167 :   if (args == error_mark_node)
   30960              :     return true;
   30961              : 
   30962    431228557 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   30963              :     {
   30964    112002086 :       tree level = TMPL_ARGS_LEVEL (args, i + 1);
   30965    306588563 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   30966              :         {
   30967    196400170 :           tree arg = TREE_VEC_ELT (level, j);
   30968    196400170 :           tree packed_args = NULL_TREE;
   30969    196400170 :           int k, len = 1;
   30970              : 
   30971    196400170 :           if (ARGUMENT_PACK_P (arg))
   30972              :             {
   30973              :               /* Look inside the argument pack.  */
   30974     15904102 :               packed_args = ARGUMENT_PACK_ARGS (arg);
   30975     15904102 :               len = TREE_VEC_LENGTH (packed_args);
   30976              :             }
   30977              : 
   30978    403283233 :           for (k = 0; k < len; ++k)
   30979              :             {
   30980    208696756 :               if (packed_args)
   30981     28200688 :                 arg = TREE_VEC_ELT (packed_args, k);
   30982              : 
   30983    208696756 :               if (error_operand_p (arg))
   30984      1813693 :                 return true;
   30985    208696756 :               else if (TREE_CODE (arg) == TEMPLATE_DECL)
   30986      4155657 :                 continue;
   30987    204541099 :               else if (arg == any_targ_node)
   30988              :                 /* An any_targ_node argument (added by add_defaults_to_ttp)
   30989              :                    makes the corresponding specialization not canonicalizable,
   30990              :                    since template_args_equal always return true for it.  We
   30991              :                    may see this when called from bind_template_template_parm.  */
   30992              :                 return true;
   30993              :               /* Checking current_function_decl because this structural
   30994              :                  comparison is only necessary for redeclaration.  */
   30995    204541095 :               else if (!current_function_decl
   30996    181783450 :                        && dependent_template_arg_p (arg)
   30997    287215010 :                        && (cp_walk_tree_without_duplicates
   30998              :                            (&arg, find_parm_usage_r, NULL)))
   30999              :                 /* The identity of a class template specialization that uses
   31000              :                    a function parameter depends on the identity of the function.
   31001              :                    And if this specialization appeared in the trailing return
   31002              :                    type thereof, we don't know the identity of the function
   31003              :                    (e.g. if it's a redeclaration or a new function) until we
   31004              :                    form its signature and go through duplicate_decls.  Thus
   31005              :                    it's unsafe to decide on a canonical type now (which depends
   31006              :                    on the DECL_CONTEXT of the function parameter, which can get
   31007              :                    mutated after the fact by duplicate_decls), so just require
   31008              :                    structural equality in this case (PR52830).  */
   31009              :                 return true;
   31010    204173156 :               else if (TYPE_P (arg)
   31011    167824428 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg)
   31012    226241775 :                        && (dependent_alias_template_spec_p (arg, nt_opaque)
   31013     20625145 :                            || dependent_opaque_alias_p (arg)))
   31014              :                 /* Require structural equality for specializations written
   31015              :                    in terms of a dependent alias template specialization.  */
   31016              :                 return true;
   31017     55658094 :               else if (CLASS_TYPE_P (arg)
   31018     54940508 :                        && TYPE_TEMPLATE_INFO (arg)
   31019    245667082 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg))
   31020              :                 /* Require structural equality for specializations written
   31021              :                    in terms of a class template specialization that itself
   31022              :                    needs structural equality.  */
   31023              :                 return true;
   31024              :             }
   31025              :         }
   31026              :     }
   31027              : 
   31028              :   return false;
   31029              : }
   31030              : 
   31031              : /* Returns true if ARGS (a collection of template arguments) contains
   31032              :    any dependent arguments.  */
   31033              : 
   31034              : bool
   31035   3184947662 : any_dependent_template_arguments_p (const_tree args)
   31036              : {
   31037   3184947662 :   if (args == error_mark_node)
   31038              :     return true;
   31039   3184947662 :   if (!processing_template_decl || !args)
   31040              :     return false;
   31041              : 
   31042   7442459350 :   for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
   31043              :     {
   31044   2976882480 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   31045   6348278411 :       for (tree arg : tree_vec_range (const_cast<tree> (level)))
   31046   4483857709 :         if (dependent_template_arg_p (arg))
   31047   1112461778 :           return true;
   31048              :     }
   31049              : 
   31050              :   return false;
   31051              : }
   31052              : 
   31053              : /* Returns true if ARGS contains any errors.  */
   31054              : 
   31055              : bool
   31056     27587884 : any_erroneous_template_args_p (const_tree args)
   31057              : {
   31058     27587884 :   int i;
   31059     27587884 :   int j;
   31060              : 
   31061     27587884 :   if (args == error_mark_node)
   31062              :     return true;
   31063              : 
   31064     27587854 :   if (args && TREE_CODE (args) != TREE_VEC)
   31065              :     {
   31066     27587854 :       if (tree ti = get_template_info (args))
   31067     21434327 :         args = TI_ARGS (ti);
   31068              :       else
   31069              :         args = NULL_TREE;
   31070              :     }
   31071              : 
   31072     21434327 :   if (!args)
   31073              :     return false;
   31074              : 
   31075     85608881 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   31076              :     {
   31077     21562470 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   31078     58205877 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   31079     36643555 :         if (error_operand_p (TREE_VEC_ELT (level, j)))
   31080              :           return true;
   31081              :     }
   31082              : 
   31083              :   return false;
   31084              : }
   31085              : 
   31086              : /* Returns TRUE if the template TMPL is type-dependent.  */
   31087              : 
   31088              : bool
   31089      2075743 : dependent_template_p (tree tmpl)
   31090              : {
   31091      2075743 :   if (TREE_CODE (tmpl) == OVERLOAD)
   31092              :     {
   31093      2065932 :       for (lkp_iterator iter (tmpl); iter; ++iter)
   31094      1373350 :         if (dependent_template_p (*iter))
   31095            0 :           return true;
   31096       692582 :       return false;
   31097              :     }
   31098              : 
   31099              :   /* Template template parameters are dependent.  */
   31100      1127436 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
   31101      2510597 :       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   31102              :     return true;
   31103              :   /* So are names that have not been looked up.  */
   31104      1383161 :   if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
   31105         9622 :     return true;
   31106              :   return false;
   31107              : }
   31108              : 
   31109              : /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
   31110              : 
   31111              : bool
   31112       702393 : dependent_template_id_p (tree tmpl, tree args)
   31113              : {
   31114       702393 :   return (dependent_template_p (tmpl)
   31115       702393 :           || any_dependent_template_arguments_p (args));
   31116              : }
   31117              : 
   31118              : /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
   31119              :    are dependent.  BODY is the body to use for loop transforming
   31120              :    constructs.  */
   31121              : 
   31122              : bool
   31123        21153 : dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv, tree body)
   31124              : {
   31125        21153 :   int i, k;
   31126              : 
   31127        21153 :   if (!processing_template_decl)
   31128              :     return false;
   31129              : 
   31130         1865 :   for (i = 0, k = 0; i < TREE_VEC_LENGTH (declv); i++)
   31131              :     {
   31132         1231 :       tree decl = TREE_VEC_ELT (declv, i);
   31133         1231 :       tree init = TREE_VEC_ELT (initv, i);
   31134         1231 :       tree cond = TREE_VEC_ELT (condv, i);
   31135         1231 :       tree incr = TREE_VEC_ELT (incrv, i);
   31136              : 
   31137         1231 :       if (decl == NULL_TREE)
   31138              :         {
   31139           10 :           tree stmt = body;
   31140           10 :           int j = c_omp_find_generated_loop (stmt, k++, cp_walk_subtrees);
   31141           10 :           init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   31142           10 :           decl = TREE_OPERAND (init, 0);
   31143           10 :           cond = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   31144           10 :           incr = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j);
   31145              :         }
   31146              : 
   31147         1231 :       if (type_dependent_expression_p (decl)
   31148         1231 :           || TREE_CODE (decl) == SCOPE_REF)
   31149              :         return true;
   31150              : 
   31151          897 :       if (init && type_dependent_expression_p (init))
   31152              :         return true;
   31153              : 
   31154          897 :       if (cond == global_namespace)
   31155              :         return true;
   31156              : 
   31157          824 :       if (type_dependent_expression_p (cond))
   31158              :         return true;
   31159              : 
   31160          800 :       if (COMPARISON_CLASS_P (cond)
   31161          800 :           && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
   31162          796 :               || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
   31163              :         return true;
   31164              : 
   31165          789 :       if (TREE_CODE (incr) == MODOP_EXPR)
   31166              :         {
   31167            1 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
   31168            1 :               || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
   31169              :             return true;
   31170              :         }
   31171          788 :       else if (type_dependent_expression_p (incr))
   31172              :         return true;
   31173          788 :       else if (TREE_CODE (incr) == MODIFY_EXPR)
   31174              :         {
   31175          233 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
   31176              :             return true;
   31177          233 :           else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
   31178              :             {
   31179          232 :               tree t = TREE_OPERAND (incr, 1);
   31180          232 :               if (type_dependent_expression_p (TREE_OPERAND (t, 0))
   31181          232 :                   || type_dependent_expression_p (TREE_OPERAND (t, 1)))
   31182              :                 return true;
   31183              : 
   31184              :               /* If this loop has a class iterator with != comparison
   31185              :                  with increment other than i++/++i/i--/--i, make sure the
   31186              :                  increment is constant.  */
   31187          454 :               if (CLASS_TYPE_P (TREE_TYPE (decl))
   31188          288 :                   && TREE_CODE (cond) == NE_EXPR)
   31189              :                 {
   31190            8 :                   if (TREE_OPERAND (t, 0) == decl)
   31191            8 :                     t = TREE_OPERAND (t, 1);
   31192              :                   else
   31193            0 :                     t = TREE_OPERAND (t, 0);
   31194            8 :                   if (TREE_CODE (t) != INTEGER_CST)
   31195              :                     return true;
   31196              :                 }
   31197              :             }
   31198              :         }
   31199              :     }
   31200              : 
   31201              :   return false;
   31202              : }
   31203              : 
   31204              : /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
   31205              :    TYPENAME_TYPE corresponds.  Returns the original TYPENAME_TYPE if
   31206              :    no such TYPE can be found.  Note that this function peers inside
   31207              :    uninstantiated templates and therefore should be used only in
   31208              :    extremely limited situations.  ONLY_CURRENT_P restricts this
   31209              :    peering to the currently open classes hierarchy (which is required
   31210              :    when comparing types).  */
   31211              : 
   31212              : tree
   31213    255228061 : resolve_typename_type (tree type, bool only_current_p)
   31214              : {
   31215    255228061 :   tree scope;
   31216    255228061 :   tree name;
   31217    255228061 :   tree decl;
   31218    255228061 :   int quals;
   31219    255228061 :   tree pushed_scope;
   31220    255228061 :   tree result;
   31221              : 
   31222    255228061 :   gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
   31223              : 
   31224    255228061 :   scope = TYPE_CONTEXT (type);
   31225              :   /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope.  */
   31226    255228061 :   gcc_checking_assert (uses_template_parms (scope));
   31227              : 
   31228              :   /* Usually the non-qualified identifier of a TYPENAME_TYPE is
   31229              :      TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
   31230              :      TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
   31231              :      representing the typedef. In that case TYPE_IDENTIFIER (type) is
   31232              :      not the non-qualified identifier of the TYPENAME_TYPE anymore.
   31233              :      So by getting the TYPE_IDENTIFIER of the _main declaration_ of
   31234              :      the TYPENAME_TYPE instead, we avoid messing up with a possible
   31235              :      typedef variant case.  */
   31236    255228061 :   name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
   31237              : 
   31238              :   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
   31239              :      it first before we can figure out what NAME refers to.  */
   31240    255228061 :   if (TREE_CODE (scope) == TYPENAME_TYPE)
   31241              :     {
   31242      6614244 :       if (TYPENAME_IS_RESOLVING_P (scope))
   31243              :         /* Given a class template A with a dependent base with nested type C,
   31244              :            typedef typename A::C::C C will land us here, as trying to resolve
   31245              :            the initial A::C leads to the local C typedef, which leads back to
   31246              :            A::C::C.  So we break the recursion now.  */
   31247              :         return type;
   31248              :       else
   31249      6614244 :         scope = resolve_typename_type (scope, only_current_p);
   31250              :     }
   31251              :   /* If we don't know what SCOPE refers to, then we cannot resolve the
   31252              :      TYPENAME_TYPE.  */
   31253    255228061 :   if (!CLASS_TYPE_P (scope))
   31254              :     return type;
   31255              :   /* If this is a typedef, we don't want to look inside (c++/11987).  */
   31256    173453279 :   if (typedef_variant_p (type))
   31257              :     return type;
   31258              :   /* If SCOPE isn't the template itself, it will not have a valid
   31259              :      TYPE_FIELDS list.  */
   31260    121838396 :   if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
   31261              :     /* scope is either the template itself or a compatible instantiation
   31262              :        like X<T>, so look up the name in the original template.  */
   31263     82128074 :     scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
   31264              :   /* If scope has no fields, it can't be a current instantiation.  Check this
   31265              :      before currently_open_class to avoid infinite recursion (71515).  */
   31266    121838396 :   if (!TYPE_FIELDS (scope))
   31267              :     return type;
   31268              :   /* If the SCOPE is not the current instantiation, there's no reason
   31269              :      to look inside it.  */
   31270     82011386 :   if (only_current_p && !currently_open_class (scope))
   31271              :     return type;
   31272              :   /* Enter the SCOPE so that name lookup will be resolved as if we
   31273              :      were in the class definition.  In particular, SCOPE will no
   31274              :      longer be considered a dependent type.  */
   31275        77024 :   pushed_scope = push_scope (scope);
   31276              :   /* Look up the declaration.  */
   31277        77024 :   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
   31278              :                         tf_warning_or_error);
   31279              : 
   31280        77024 :   result = NULL_TREE;
   31281              : 
   31282              :   /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
   31283              :      find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
   31284        77024 :   tree fullname = TYPENAME_TYPE_FULLNAME (type);
   31285        77024 :   if (!decl)
   31286              :     /*nop*/;
   31287        76998 :   else if (identifier_p (fullname)
   31288        76992 :            && TREE_CODE (decl) == TYPE_DECL)
   31289              :     {
   31290        76992 :       result = TREE_TYPE (decl);
   31291        76992 :       if (result == error_mark_node)
   31292           29 :         result = NULL_TREE;
   31293              :     }
   31294            6 :   else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
   31295            6 :            && DECL_CLASS_TEMPLATE_P (decl))
   31296              :     {
   31297              :       /* Obtain the template and the arguments.  */
   31298            3 :       tree tmpl = TREE_OPERAND (fullname, 0);
   31299            3 :       if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
   31300              :         {
   31301              :           /* We get here with a plain identifier because a previous tentative
   31302              :              parse of the nested-name-specifier as part of a ptr-operator saw
   31303              :              ::template X<A>.  The use of ::template is necessary in a
   31304              :              ptr-operator, but wrong in a declarator-id.
   31305              : 
   31306              :              [temp.names]: In a qualified-id of a declarator-id, the keyword
   31307              :              template shall not appear at the top level.  */
   31308            3 :           pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
   31309              :                    "keyword %<template%> not allowed in declarator-id");
   31310            3 :           tmpl = decl;
   31311              :         }
   31312            3 :       tree args = TREE_OPERAND (fullname, 1);
   31313              :       /* Instantiate the template.  */
   31314            3 :       result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
   31315              :                                       tf_error | tf_user);
   31316            3 :       result = adjust_type_for_entering_scope (result);
   31317            3 :       if (result == error_mark_node)
   31318           29 :         result = NULL_TREE;
   31319              :     }
   31320              : 
   31321              :   /* Leave the SCOPE.  */
   31322        77024 :   if (pushed_scope)
   31323        77006 :     pop_scope (pushed_scope);
   31324              : 
   31325              :   /* If we failed to resolve it, return the original typename.  */
   31326        77024 :   if (!result)
   31327              :     return type;
   31328              : 
   31329              :   /* If lookup found a typename type, resolve that too.  */
   31330        76995 :   if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
   31331              :     {
   31332              :       /* Ill-formed programs can cause infinite recursion here, so we
   31333              :          must catch that.  */
   31334            3 :       TYPENAME_IS_RESOLVING_P (result) = 1;
   31335            3 :       result = resolve_typename_type (result, only_current_p);
   31336            3 :       TYPENAME_IS_RESOLVING_P (result) = 0;
   31337              :     }
   31338              : 
   31339              :   /* Qualify the resulting type.  */
   31340        76995 :   quals = cp_type_quals (type);
   31341        76995 :   if (quals)
   31342            0 :     result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
   31343              : 
   31344              :   return result;
   31345              : }
   31346              : 
   31347              : /* Returns a type which represents 'auto' or 'decltype(auto)'.  We use a
   31348              :    TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
   31349              :    by default.  If set_canonical is true, we set TYPE_CANONICAL on it.  */
   31350              : 
   31351              : static tree
   31352     32410900 : make_auto_1 (tree name, bool set_canonical, int level = -1)
   31353              : {
   31354     32410900 :   if (level == -1)
   31355     22642370 :     level = current_template_depth + 1;
   31356     32410900 :   tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
   31357     32410900 :   TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
   31358     32410900 :   TYPE_STUB_DECL (au) = TYPE_NAME (au);
   31359     32410900 :   TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
   31360     32410900 :     (0, level, level, TYPE_NAME (au), NULL_TREE);
   31361     32410900 :   if (set_canonical)
   31362     21133820 :     TYPE_CANONICAL (au) = canonical_type_parameter (au);
   31363     32410900 :   DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
   31364     32410900 :   SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
   31365     32410900 :   if (name == decltype_auto_identifier)
   31366      2179229 :     AUTO_IS_DECLTYPE (au) = true;
   31367              : 
   31368     32410900 :   return au;
   31369              : }
   31370              : 
   31371              : tree
   31372       639758 : make_decltype_auto (void)
   31373              : {
   31374       639758 :   return make_auto_1 (decltype_auto_identifier, true);
   31375              : }
   31376              : 
   31377              : tree
   31378     20436395 : make_auto (void)
   31379              : {
   31380     20436395 :   return make_auto_1 (auto_identifier, true);
   31381              : }
   31382              : 
   31383              : /* Return a C++17 deduction placeholder for class template TMPL.
   31384              :    There are represented as an 'auto' with the special level 0 and
   31385              :    CLASS_PLACEHOLDER_TEMPLATE set.  */
   31386              : 
   31387              : tree
   31388      9710863 : make_template_placeholder (tree tmpl)
   31389              : {
   31390      9710863 :   tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
   31391      9710863 :   CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
   31392              :   /* Our canonical type depends on the placeholder.  */
   31393      9710863 :   TYPE_CANONICAL (t) = canonical_type_parameter (t);
   31394      9710863 :   return t;
   31395              : }
   31396              : 
   31397              : /* True iff T is a C++17 class template deduction placeholder.  */
   31398              : 
   31399              : bool
   31400   1978185932 : template_placeholder_p (tree t)
   31401              : {
   31402   2025609410 :   return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
   31403              : }
   31404              : 
   31405              : /* Return an auto for an explicit cast expression auto(x).
   31406              :    Like CTAD placeholders, these have level 0 so that they're
   31407              :    not accidentally replaced via tsubst and are always directly
   31408              :    resolved via do_auto_deduction.  */
   31409              : 
   31410              : tree
   31411        57667 : make_cast_auto ()
   31412              : {
   31413        57667 :   return make_auto_1 (auto_identifier, true, /*level=*/0);
   31414              : }
   31415              : 
   31416              : /* Make a "constrained auto" type-specifier. This is an auto or
   31417              :   decltype(auto) type with constraints that must be associated after
   31418              :   deduction.  The constraint is formed from the given concept CON
   31419              :   and its optional sequence of template arguments ARGS.
   31420              : 
   31421              :   TYPE must be the result of make_auto_type or make_decltype_auto_type. */
   31422              : 
   31423              : static tree
   31424      1565893 : make_constrained_placeholder_type (tree type, tree con, tree args)
   31425              : {
   31426              :   /* Build the constraint. */
   31427      1565893 :   tree tmpl = DECL_TI_TEMPLATE (con);
   31428      1565893 :   ++processing_template_decl;
   31429      1565893 :   tree expr = build_concept_check (tmpl, type, args, tf_warning_or_error);
   31430      1565893 :   --processing_template_decl;
   31431              : 
   31432      1565893 :   PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
   31433      1565893 :     = build_tree_list (current_template_parms, expr);
   31434              : 
   31435              :   /* Our canonical type depends on the constraint.  */
   31436      1565893 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   31437              : 
   31438      1565893 :   return type;
   31439              : }
   31440              : 
   31441              : /* Make a "constrained auto" type-specifier.  */
   31442              : 
   31443              : tree
   31444        26431 : make_constrained_auto (tree con, tree args)
   31445              : {
   31446        26431 :   tree type = make_auto_1 (auto_identifier, false);
   31447        26431 :   return make_constrained_placeholder_type (type, con, args);
   31448              : }
   31449              : 
   31450              : /* Make a "constrained decltype(auto)" type-specifier.  */
   31451              : 
   31452              : tree
   31453      1539462 : make_constrained_decltype_auto (tree con, tree args)
   31454              : {
   31455      1539462 :   tree type = make_auto_1 (decltype_auto_identifier, false);
   31456      1539462 :   return make_constrained_placeholder_type (type, con, args);
   31457              : }
   31458              : 
   31459              : /* Create an "auto..." type-specifier.  */
   31460              : 
   31461              : tree
   31462          183 : make_auto_pack ()
   31463              : {
   31464          183 :   tree type = make_auto_1 (auto_identifier, false);
   31465          183 :   TEMPLATE_TYPE_PARAMETER_PACK (type) = true;
   31466              :   /* Our canonical type depends on being a pack.  */
   31467          183 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   31468          183 :   return type;
   31469              : }
   31470              : 
   31471              : /* Returns true if the placeholder type constraint T has any dependent
   31472              :    (explicit) template arguments.  */
   31473              : 
   31474              : static bool
   31475           59 : placeholder_type_constraint_dependent_p (tree t)
   31476              : {
   31477           59 :   gcc_assert (concept_check_p (t));
   31478           59 :   tree args = TREE_OPERAND (t, 1);
   31479           59 :   tree first = TREE_VEC_ELT (args, 0);
   31480           59 :   if (ARGUMENT_PACK_P (first))
   31481              :     {
   31482           18 :       args = expand_template_argument_pack (args);
   31483           18 :       first = TREE_VEC_ELT (args, 0);
   31484              :     }
   31485           59 :   gcc_checking_assert (is_auto (first));
   31486           77 :   for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
   31487           42 :     if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
   31488              :       return true;
   31489              :   return false;
   31490              : }
   31491              : 
   31492              : /* Prepare and return a concept definition.  */
   31493              : 
   31494              : tree
   31495      2888817 : start_concept_definition (cp_expr id)
   31496              : {
   31497      2888817 :   gcc_assert (identifier_p (id));
   31498      2888817 :   gcc_assert (processing_template_decl);
   31499              : 
   31500      2888817 :   location_t loc = id.get_location();
   31501              : 
   31502              :   /* A concept-definition shall not have associated constraints.  */
   31503      2888817 :   if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
   31504              :     {
   31505            6 :       error_at (loc, "a concept cannot be constrained");
   31506            6 :       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
   31507              :     }
   31508              : 
   31509              :   /* A concept-definition shall appear in namespace scope.  Templates
   31510              :      aren't allowed in block scope, so we only need to check for class
   31511              :      scope.  */
   31512      2888817 :   if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
   31513              :     {
   31514            6 :       error_at (loc, "concept %qE not in namespace scope", *id);
   31515            6 :       return error_mark_node;
   31516              :     }
   31517              : 
   31518      2888811 :   if (current_template_depth > 1)
   31519              :     {
   31520            3 :       error_at (loc, "concept %qE has multiple template parameter lists", *id);
   31521            3 :       return error_mark_node;
   31522              :     }
   31523              : 
   31524              :   /* Initially build the concept declaration; its type is bool.  */
   31525      2888808 :   tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
   31526      2888808 :   DECL_CONTEXT (decl) = current_scope ();
   31527      2888808 :   TREE_PUBLIC (decl) = true;
   31528              : 
   31529      2888808 :   return decl;
   31530              : }
   31531              : 
   31532              : /* Finish building a concept definition. Like other templates, the
   31533              :    CONCEPT_DECL node is wrapped by a TEMPLATE_DECL.  This returns the
   31534              :    the TEMPLATE_DECL. */
   31535              : 
   31536              : tree
   31537      2888808 : finish_concept_definition (tree decl, tree init, tree attrs)
   31538              : {
   31539      2888808 :   DECL_INITIAL (decl) = init;
   31540              : 
   31541      2888808 :   if (attrs)
   31542           30 :     cplus_decl_attributes (&decl, attrs, 0);
   31543              : 
   31544      2888808 :   set_originating_module (decl, false);
   31545      2888808 :   check_module_decl_linkage (decl);
   31546              : 
   31547              :   /* Push the enclosing template.  */
   31548      2888808 :   return push_template_decl (decl);
   31549              : }
   31550              : 
   31551              : /* Given type ARG, return std::initializer_list<ARG>.  */
   31552              : 
   31553              : static tree
   31554          422 : listify (tree arg)
   31555              : {
   31556          422 :   tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
   31557              : 
   31558          422 :   if (std_init_list == error_mark_node
   31559          422 :       || !DECL_CLASS_TEMPLATE_P (std_init_list))
   31560              :     {
   31561           21 :       gcc_rich_location richloc (input_location);
   31562           21 :       maybe_add_include_fixit (&richloc, "<initializer_list>", false);
   31563           21 :       error_at (&richloc,
   31564              :                 "deducing from brace-enclosed initializer list"
   31565              :                 " requires %<#include <initializer_list>%>");
   31566              : 
   31567           21 :       return error_mark_node;
   31568           21 :     }
   31569          401 :   tree argvec = make_tree_vec (1);
   31570          401 :   TREE_VEC_ELT (argvec, 0) = arg;
   31571              : 
   31572          401 :   return lookup_template_class (std_init_list, argvec, NULL_TREE,
   31573          401 :                                 NULL_TREE, tf_warning_or_error);
   31574              : }
   31575              : 
   31576              : /* Replace auto in TYPE with std::initializer_list<auto>.  */
   31577              : 
   31578              : static tree
   31579          422 : listify_autos (tree type, tree auto_node)
   31580              : {
   31581          422 :   tree init_auto = listify (strip_top_quals (auto_node));
   31582          422 :   tree argvec = make_tree_vec (1);
   31583          422 :   TREE_VEC_ELT (argvec, 0) = init_auto;
   31584          422 :   if (processing_template_decl)
   31585            5 :     argvec = add_to_template_args (current_template_args (), argvec);
   31586          422 :   return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
   31587              : }
   31588              : 
   31589              : /* Hash traits for hashing possibly constrained 'auto'
   31590              :    TEMPLATE_TYPE_PARMs for use by do_auto_deduction.  */
   31591              : 
   31592              : struct auto_hash : default_hash_traits<tree>
   31593              : {
   31594              :   static inline hashval_t hash (tree);
   31595              :   static inline bool equal (tree, tree);
   31596              : };
   31597              : 
   31598              : /* Hash the 'auto' T.  */
   31599              : 
   31600              : inline hashval_t
   31601              : auto_hash::hash (tree t)
   31602              : {
   31603              :   if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
   31604              :     /* Matching constrained-type-specifiers denote the same template
   31605              :        parameter, so hash the constraint.  */
   31606              :     return iterative_hash_placeholder_constraint (c, 0);
   31607              :   else
   31608              :     /* But unconstrained autos are all separate, so just hash the pointer.  */
   31609              :     return iterative_hash_object (t, 0);
   31610              : }
   31611              : 
   31612              : /* Compare two 'auto's.  */
   31613              : 
   31614              : inline bool
   31615              : auto_hash::equal (tree t1, tree t2)
   31616              : {
   31617              :   if (t1 == t2)
   31618              :     return true;
   31619              : 
   31620              :   tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
   31621              :   tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
   31622              : 
   31623              :   /* Two unconstrained autos are distinct.  */
   31624              :   if (!c1 || !c2)
   31625              :     return false;
   31626              : 
   31627              :   return equivalent_placeholder_constraints (c1, c2);
   31628              : }
   31629              : 
   31630              : /* The stem for deduction guide names.  */
   31631              : const char *const dguide_base = "__dguide_";
   31632              : 
   31633              : /* Return the name for a deduction guide for class template TMPL.  */
   31634              : 
   31635              : tree
   31636      1437762 : dguide_name (tree tmpl)
   31637              : {
   31638      1437762 :   tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
   31639      1437762 :   tree tname = TYPE_IDENTIFIER (type);
   31640      1437762 :   char *buf = (char *) alloca (1 + strlen (dguide_base)
   31641              :                                + IDENTIFIER_LENGTH (tname));
   31642      1437762 :   memcpy (buf, dguide_base, strlen (dguide_base));
   31643      1437762 :   memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
   31644      1437762 :           IDENTIFIER_LENGTH (tname) + 1);
   31645      1437762 :   tree dname = get_identifier (buf);
   31646      1437762 :   TREE_TYPE (dname) = type;
   31647      1437762 :   return dname;
   31648              : }
   31649              : 
   31650              : /* True if NAME is the name of a deduction guide.  */
   31651              : 
   31652              : bool
   31653   1298026308 : dguide_name_p (tree name)
   31654              : {
   31655   1298026308 :   return (TREE_CODE (name) == IDENTIFIER_NODE
   31656   1298026306 :           && TREE_TYPE (name)
   31657   1363083841 :           && startswith (IDENTIFIER_POINTER (name), dguide_base));
   31658              : }
   31659              : 
   31660              : /* True if FN is a deduction guide.  */
   31661              : 
   31662              : bool
   31663    884478116 : deduction_guide_p (const_tree fn)
   31664              : {
   31665    884478116 :   if (DECL_P (fn))
   31666    884435223 :     if (tree name = DECL_NAME (fn))
   31667    884435111 :       return dguide_name_p (name);
   31668              :   return false;
   31669              : }
   31670              : 
   31671              : /* True if FN is the copy deduction guide, i.e. A(A)->A.  */
   31672              : 
   31673              : bool
   31674         2668 : copy_guide_p (const_tree fn)
   31675              : {
   31676         2668 :   gcc_assert (deduction_guide_p (fn));
   31677         2668 :   if (!DECL_ARTIFICIAL (fn))
   31678              :     return false;
   31679         2668 :   tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
   31680         2668 :   return (TREE_CHAIN (parms) == void_list_node
   31681         2668 :           && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
   31682              : }
   31683              : 
   31684              : /* True if FN is a guide generated from a constructor template.  */
   31685              : 
   31686              : bool
   31687           56 : template_guide_p (const_tree fn)
   31688              : {
   31689           56 :   gcc_assert (deduction_guide_p (fn));
   31690           56 :   if (!DECL_ARTIFICIAL (fn))
   31691              :     return false;
   31692           56 :   tree tmpl = DECL_TI_TEMPLATE (fn);
   31693           56 :   if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
   31694           40 :     return PRIMARY_TEMPLATE_P (org);
   31695              :   return false;
   31696              : }
   31697              : 
   31698              : /* True if FN is an aggregate initialization guide or the copy deduction
   31699              :    guide.  */
   31700              : 
   31701              : bool
   31702           12 : builtin_guide_p (const_tree fn)
   31703              : {
   31704           12 :   if (!deduction_guide_p (fn))
   31705              :     return false;
   31706           12 :   if (!DECL_ARTIFICIAL (fn))
   31707              :     /* Explicitly declared.  */
   31708              :     return false;
   31709           12 :   if (DECL_ABSTRACT_ORIGIN (fn))
   31710              :     /* Derived from a constructor.  */
   31711            0 :     return false;
   31712              :   return true;
   31713              : }
   31714              : 
   31715              : /* True if FN is a C++23 inherited guide.  */
   31716              : 
   31717              : bool
   31718         8098 : inherited_guide_p (const_tree fn)
   31719              : {
   31720         8098 :   gcc_assert (deduction_guide_p (fn));
   31721         8098 :   return LANG_DECL_FN_CHECK (fn)->context != NULL_TREE;
   31722              : }
   31723              : 
   31724              : /* Set the base class BASE from which the transformed guide FN
   31725              :    was inherited as part of C++23 inherited CTAD.  */
   31726              : 
   31727              : static void
   31728          253 : set_inherited_guide_context (const_tree fn, tree base)
   31729              : {
   31730          253 :   gcc_assert (deduction_guide_p (fn));
   31731          253 :   LANG_DECL_FN_CHECK (fn)->context = base;
   31732          253 : }
   31733              : 
   31734              : /* OLDDECL is a _DECL for a template parameter.  Return a similar parameter at
   31735              :    LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
   31736              :    template parameter types.  Note that the handling of template template
   31737              :    parameters relies on current_template_parms being set appropriately for the
   31738              :    new template.  */
   31739              : 
   31740              : static tree
   31741       115967 : rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
   31742              :                        tree tsubst_args, tsubst_flags_t complain)
   31743              : {
   31744       115967 :   if (olddecl == error_mark_node)
   31745              :     return error_mark_node;
   31746              : 
   31747       115964 :   tree oldidx = get_template_parm_index (olddecl);
   31748              : 
   31749       115964 :   tree newtype;
   31750       115964 :   if (TREE_CODE (olddecl) == TYPE_DECL
   31751         6095 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   31752              :     {
   31753       109904 :       tree oldtype = TREE_TYPE (olddecl);
   31754       109904 :       newtype = cxx_make_type (TREE_CODE (oldtype));
   31755       109904 :       TYPE_MAIN_VARIANT (newtype) = newtype;
   31756       109904 :     }
   31757              :   else
   31758              :     {
   31759         6060 :       newtype = TREE_TYPE (olddecl);
   31760         6060 :       if (type_uses_auto (newtype))
   31761              :         {
   31762              :           // Substitute once to fix references to other template parameters.
   31763            6 :           newtype = tsubst (newtype, tsubst_args,
   31764              :                             complain|tf_partial, NULL_TREE);
   31765              :           // Now substitute again to reduce the level of the auto.
   31766            6 :           newtype = tsubst (newtype, current_template_args (),
   31767              :                             complain, NULL_TREE);
   31768              :         }
   31769              :       else
   31770         6054 :         newtype = tsubst (newtype, tsubst_args,
   31771              :                           complain, NULL_TREE);
   31772              :     }
   31773              : 
   31774       115964 :   tree newdecl
   31775       115964 :     = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
   31776       115964 :                   DECL_NAME (olddecl), newtype);
   31777       115964 :   SET_DECL_TEMPLATE_PARM_P (newdecl);
   31778              : 
   31779       115964 :   tree newidx;
   31780       115964 :   if (TREE_CODE (olddecl) == TYPE_DECL
   31781         6095 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   31782              :     {
   31783       109904 :       newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
   31784       109904 :         = build_template_parm_index (index, level, level,
   31785              :                                      newdecl, newtype);
   31786       219808 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   31787       109904 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   31788       109904 :       TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
   31789              : 
   31790       109904 :       if (TREE_CODE (olddecl) == TEMPLATE_DECL)
   31791              :         {
   31792           35 :           tree newresult
   31793           35 :             = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
   31794           35 :                                    DECL_NAME (olddecl), newtype);
   31795           35 :           DECL_ARTIFICIAL (newresult) = true;
   31796           35 :           DECL_TEMPLATE_RESULT (newdecl) = newresult;
   31797              :           // First create a copy (ttargs) of tsubst_args with an
   31798              :           // additional level for the template template parameter's own
   31799              :           // template parameters (ttparms).
   31800           35 :           tree ttparms = (INNERMOST_TEMPLATE_PARMS
   31801           35 :                           (DECL_TEMPLATE_PARMS (olddecl)));
   31802           70 :           const int depth = TMPL_ARGS_DEPTH (tsubst_args);
   31803           35 :           tree ttargs = make_tree_vec (depth + 1);
   31804          126 :           for (int i = 0; i < depth; ++i)
   31805          112 :             TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
   31806           35 :           TREE_VEC_ELT (ttargs, depth)
   31807           35 :             = template_parms_level_to_args (ttparms);
   31808              :           // Substitute ttargs into ttparms to fix references to
   31809              :           // other template parameters.
   31810           35 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   31811              :                                                  complain|tf_partial);
   31812              :           // Now substitute again with args based on tparms, to reduce
   31813              :           // the level of the ttparms.
   31814           35 :           ttargs = current_template_args ();
   31815           35 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   31816              :                                                  complain);
   31817              :           // Finally, tack the adjusted parms onto tparms.
   31818           35 :           ttparms = tree_cons (size_int (level + 1), ttparms,
   31819              :                                copy_node (current_template_parms));
   31820              :           // As with all template template parms, the parameter list captured
   31821              :           // by this template template parm that corresponds to its own level
   31822              :           // should be empty.  This avoids infinite recursion when structurally
   31823              :           // comparing two such rewritten template template parms (PR102479).
   31824           35 :           gcc_assert (!TREE_VEC_LENGTH
   31825              :                       (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
   31826           35 :           gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
   31827           35 :           TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
   31828              :           // All done.
   31829           35 :           DECL_TEMPLATE_PARMS (newdecl) = ttparms;
   31830           35 :           DECL_TEMPLATE_INFO (newresult)
   31831           70 :             = build_template_info (newdecl, template_parms_to_args (ttparms));
   31832              :         }
   31833              : 
   31834       109904 :       if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
   31835            0 :         SET_TYPE_STRUCTURAL_EQUALITY (newtype);
   31836              :       else
   31837       109904 :         TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
   31838              :     }
   31839              :   else
   31840              :     {
   31841         6060 :       tree oldconst = TEMPLATE_PARM_DECL (oldidx);
   31842         6060 :       tree newconst
   31843         6060 :         = build_decl (DECL_SOURCE_LOCATION (oldconst),
   31844         6060 :                       TREE_CODE (oldconst),
   31845         6060 :                       DECL_NAME (oldconst), newtype);
   31846        24240 :       TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
   31847         6060 :         = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
   31848         6060 :       SET_DECL_TEMPLATE_PARM_P (newconst);
   31849         6060 :       newidx = build_template_parm_index (index, level, level,
   31850              :                                           newconst, newtype);
   31851        12120 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   31852         6060 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   31853         6060 :       DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
   31854              :     }
   31855              : 
   31856              :   return newdecl;
   31857              : }
   31858              : 
   31859              : /* As rewrite_template_parm, but for the whole TREE_LIST representing a
   31860              :    template parameter.  */
   31861              : 
   31862              : static tree
   31863       115967 : rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
   31864              :                     tree targs, unsigned targs_index, tsubst_flags_t complain)
   31865              : {
   31866       115967 :   tree olddecl = TREE_VALUE (oldelt);
   31867       115967 :   tree newdecl = rewrite_template_parm (olddecl, index, level,
   31868              :                                         targs, complain);
   31869       115967 :   if (newdecl == error_mark_node)
   31870              :     return error_mark_node;
   31871       115964 :   tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
   31872              :                                      targs, complain, NULL_TREE);
   31873       115964 :   tree list = build_tree_list (newdef, newdecl);
   31874       231928 :   TEMPLATE_PARM_CONSTRAINTS (list)
   31875       115964 :     = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
   31876              :                               targs, complain, NULL_TREE);
   31877       231928 :   int depth = TMPL_ARGS_DEPTH (targs);
   31878       231928 :   TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
   31879       115964 :   return list;
   31880              : }
   31881              : 
   31882              : /* Returns a C++17 class deduction guide template based on the constructor
   31883              :    CTOR.  As a special case, CTOR can be a RECORD_TYPE for an implicit default
   31884              :    guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
   31885              :    aggregate initialization guide.  OUTER_ARGS are the template arguments
   31886              :    for the enclosing scope of the class.  */
   31887              : 
   31888              : static tree
   31889        88523 : build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
   31890              : {
   31891        88523 :   tree tparms, targs, fparms, fargs, ci;
   31892        88523 :   bool memtmpl = false;
   31893        88523 :   bool explicit_p;
   31894        88523 :   location_t loc;
   31895        88523 :   tree fn_tmpl = NULL_TREE;
   31896              : 
   31897        88523 :   if (outer_args)
   31898              :     {
   31899         1549 :       ++processing_template_decl;
   31900         1549 :       type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
   31901         1549 :       --processing_template_decl;
   31902              :     }
   31903              : 
   31904        88523 :   if (!DECL_DECLARES_FUNCTION_P (ctor))
   31905              :     {
   31906         9558 :       if (TYPE_P (ctor))
   31907              :         {
   31908         6523 :           bool copy_p = TYPE_REF_P (ctor);
   31909         6523 :           if (copy_p)
   31910         5746 :             fparms = tree_cons (NULL_TREE, type, void_list_node);
   31911              :           else
   31912          777 :             fparms = void_list_node;
   31913              :         }
   31914         3035 :       else if (TREE_CODE (ctor) == TREE_LIST)
   31915              :         fparms = ctor;
   31916              :       else
   31917            0 :         gcc_unreachable ();
   31918              : 
   31919         9558 :       tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
   31920         9558 :       tparms = DECL_TEMPLATE_PARMS (ctmpl);
   31921         9558 :       targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   31922         9558 :       ci = NULL_TREE;
   31923         9558 :       fargs = NULL_TREE;
   31924         9558 :       loc = DECL_SOURCE_LOCATION (ctmpl);
   31925         9558 :       explicit_p = false;
   31926              :     }
   31927              :   else
   31928              :     {
   31929        78965 :       ++processing_template_decl;
   31930        78965 :       bool ok = true;
   31931              : 
   31932        78965 :       complain |= tf_dguide;
   31933              : 
   31934        78965 :       fn_tmpl
   31935        78965 :         = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
   31936        12993 :            : DECL_TI_TEMPLATE (ctor));
   31937        78965 :       if (outer_args)
   31938         1284 :         fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
   31939        78965 :       ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
   31940              : 
   31941        78965 :       tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
   31942              :       /* If type is a member class template, DECL_TI_ARGS (ctor) will have
   31943              :          fully specialized args for the enclosing class.  Strip those off, as
   31944              :          the deduction guide won't have those template parameters.  */
   31945       157930 :       targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
   31946        78965 :                                                 TMPL_PARMS_DEPTH (tparms));
   31947              :       /* Discard the 'this' parameter.  */
   31948        78965 :       fparms = FUNCTION_ARG_CHAIN (ctor);
   31949        78965 :       fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
   31950        78965 :       ci = get_constraints (ctor);
   31951        78965 :       loc = DECL_SOURCE_LOCATION (ctor);
   31952        78965 :       explicit_p = DECL_NONCONVERTING_P (ctor);
   31953              : 
   31954        78965 :       if (PRIMARY_TEMPLATE_P (fn_tmpl))
   31955              :         {
   31956        65972 :           memtmpl = true;
   31957              : 
   31958              :           /* For a member template constructor, we need to flatten the two
   31959              :              template parameter lists into one, and then adjust the function
   31960              :              signature accordingly.  This gets...complicated.  */
   31961        65972 :           tree save_parms = current_template_parms;
   31962              : 
   31963              :           /* For a member template we should have two levels of parms/args, one
   31964              :              for the class and one for the constructor.  We stripped
   31965              :              specialized args for further enclosing classes above.  */
   31966        65972 :           const int depth = 2;
   31967       131944 :           gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
   31968              : 
   31969              :           /* Template args for translating references to the two-level template
   31970              :              parameters into references to the one-level template parameters we
   31971              :              are creating.  */
   31972        65972 :           tree tsubst_args = copy_node (targs);
   31973        65972 :           TMPL_ARGS_LEVEL (tsubst_args, depth)
   31974       131944 :             = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
   31975              : 
   31976              :           /* Template parms for the constructor template.  */
   31977        65972 :           tree ftparms = TREE_VALUE (tparms);
   31978        65972 :           unsigned flen = TREE_VEC_LENGTH (ftparms);
   31979              :           /* Template parms for the class template.  */
   31980        65972 :           tparms = TREE_CHAIN (tparms);
   31981        65972 :           tree ctparms = TREE_VALUE (tparms);
   31982        65972 :           unsigned clen = TREE_VEC_LENGTH (ctparms);
   31983              :           /* Template parms for the deduction guide start as a copy of the
   31984              :              template parms for the class.  We set current_template_parms for
   31985              :              lookup_template_class_1.  */
   31986        65972 :           current_template_parms = tparms = copy_node (tparms);
   31987        65972 :           tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
   31988       145524 :           for (unsigned i = 0; i < clen; ++i)
   31989        79552 :             TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
   31990              : 
   31991              :           /* Now we need to rewrite the constructor parms to append them to the
   31992              :              class parms.  */
   31993       180666 :           for (unsigned i = 0; i < flen; ++i)
   31994              :             {
   31995       114694 :               unsigned index = i + clen;
   31996       114694 :               unsigned level = 1;
   31997       114694 :               tree oldelt = TREE_VEC_ELT (ftparms, i);
   31998       114694 :               tree newelt
   31999       114694 :                 = rewrite_tparm_list (oldelt, index, level,
   32000              :                                       tsubst_args, i, complain);
   32001       114694 :               if (newelt == error_mark_node)
   32002            3 :                 ok = false;
   32003       114694 :               TREE_VEC_ELT (new_vec, index) = newelt;
   32004              :             }
   32005              : 
   32006              :           /* Now we have a final set of template parms to substitute into the
   32007              :              function signature.  */
   32008        65972 :           targs = template_parms_to_args (tparms);
   32009        65972 :           fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
   32010              :                                      complain, ctor);
   32011        65972 :           if (fparms == error_mark_node)
   32012            0 :             ok = false;
   32013        65972 :           if (ci)
   32014              :             {
   32015        64015 :               if (outer_args)
   32016              :                 /* FIXME: We'd like to avoid substituting outer template
   32017              :                    arguments into the constraint ahead of time, but the
   32018              :                    construction of tsubst_args assumes that outer arguments
   32019              :                    are already substituted in.  */
   32020          766 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   32021        64015 :               ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
   32022              :             }
   32023              : 
   32024              :           /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
   32025              :              cp_unevaluated_operand.  */
   32026        65972 :           cp_evaluated ev;
   32027        65972 :           fargs = tsubst (fargs, tsubst_args, complain, ctor);
   32028        65972 :           current_template_parms = save_parms;
   32029        65972 :         }
   32030              :       else
   32031              :         {
   32032              :           /* Substitute in the same arguments to rewrite class members into
   32033              :              references to members of an unknown specialization.  */
   32034        12993 :           cp_evaluated ev;
   32035        12993 :           fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
   32036        12993 :          if (fparms == error_mark_node)
   32037            3 :            ok = false;
   32038        12993 :           fargs = tsubst (fargs, targs, complain, ctor);
   32039        12993 :           if (ci)
   32040              :             {
   32041         6386 :               if (outer_args)
   32042              :                 /* FIXME: As above.  */
   32043            6 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   32044         6386 :               ci = tsubst_constraint_info (ci, targs, complain, ctor);
   32045              :             }
   32046        12993 :         }
   32047              : 
   32048        78965 :       --processing_template_decl;
   32049        78965 :       if (!ok)
   32050            6 :         return error_mark_node;
   32051              :     }
   32052              : 
   32053        88517 :   if (!memtmpl)
   32054              :     {
   32055              :       /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE.  */
   32056        22548 :       tparms = copy_node (tparms);
   32057        22548 :       INNERMOST_TEMPLATE_PARMS (tparms)
   32058        22548 :         = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
   32059              :     }
   32060              : 
   32061        88517 :   tree fntype = cp_build_function_type (type, fparms);
   32062        88517 :   tree ded_fn = build_lang_decl_loc (loc,
   32063              :                                      FUNCTION_DECL,
   32064              :                                      dguide_name (type), fntype);
   32065        88517 :   DECL_ARGUMENTS (ded_fn) = fargs;
   32066        88517 :   DECL_ARTIFICIAL (ded_fn) = true;
   32067        88517 :   DECL_NONCONVERTING_P (ded_fn) = explicit_p;
   32068        88517 :   tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
   32069        88517 :   DECL_ARTIFICIAL (ded_tmpl) = true;
   32070        88517 :   DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
   32071        88517 :   DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
   32072        88517 :   if (DECL_P (ctor))
   32073        78959 :     DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
   32074        88517 :   if (ci)
   32075        70401 :     set_constraints (ded_tmpl, ci);
   32076              : 
   32077              :   return ded_tmpl;
   32078              : }
   32079              : 
   32080              : /* Add to LIST the member types for the reshaped initializer CTOR.  */
   32081              : 
   32082              : static tree
   32083         3697 : collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
   32084              : {
   32085         3697 :   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
   32086         3697 :   tree idx, val; unsigned i;
   32087        13872 :   FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
   32088              :     {
   32089         6221 :       tree ftype = elt ? elt : TREE_TYPE (idx);
   32090          881 :       if (BRACE_ENCLOSED_INITIALIZER_P (val)
   32091        11056 :           && CONSTRUCTOR_BRACES_ELIDED_P (val))
   32092              :         {
   32093          831 :           tree subelt = NULL_TREE;
   32094          831 :           if (TREE_CODE (ftype) == ARRAY_TYPE)
   32095          825 :             subelt = TREE_TYPE (ftype);
   32096          831 :           list = collect_ctor_idx_types (val, list, subelt);
   32097          831 :           continue;
   32098          831 :         }
   32099         9344 :       tree arg = NULL_TREE;
   32100         9344 :       if (i == v->length() - 1
   32101         9344 :           && PACK_EXPANSION_P (ftype))
   32102              :         /* Give the trailing pack expansion parameter a default argument to
   32103              :            match aggregate initialization behavior, even if we deduce the
   32104              :            length of the pack separately to more than we have initializers. */
   32105           17 :         arg = build_constructor (init_list_type_node, NULL);
   32106              :       /* if ei is of array type and xi is a braced-init-list or string literal,
   32107              :          Ti is an rvalue reference to the declared type of ei */
   32108         9344 :       STRIP_ANY_LOCATION_WRAPPER (val);
   32109         9344 :       if (TREE_CODE (ftype) == ARRAY_TYPE
   32110         9344 :           && (BRACE_ENCLOSED_INITIALIZER_P (val)
   32111            6 :               || TREE_CODE (val) == STRING_CST))
   32112              :         {
   32113           33 :           if (TREE_CODE (val) == STRING_CST)
   32114            6 :             ftype = cp_build_qualified_type
   32115            6 :               (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
   32116           33 :           ftype = (cp_build_reference_type
   32117           39 :                    (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
   32118              :         }
   32119         9344 :       list = tree_cons (arg, ftype, list);
   32120              :     }
   32121              : 
   32122         3697 :   return list;
   32123              : }
   32124              : 
   32125              : /* Return whether ETYPE is, or is derived from, a specialization of TMPL.  */
   32126              : 
   32127              : static bool
   32128         5998 : is_spec_or_derived (tree etype, tree tmpl)
   32129              : {
   32130         5998 :   if (!etype || !CLASS_TYPE_P (etype))
   32131              :     return false;
   32132              : 
   32133         2000 :   etype = cv_unqualified (etype);
   32134         2000 :   tree type = TREE_TYPE (tmpl);
   32135         2000 :   tree tparms = (INNERMOST_TEMPLATE_PARMS
   32136         2000 :                  (DECL_TEMPLATE_PARMS (tmpl)));
   32137         2000 :   tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   32138         2000 :   int err = unify (tparms, targs, type, etype,
   32139              :                    UNIFY_ALLOW_DERIVED, /*explain*/false);
   32140         2000 :   ggc_free (targs);
   32141         2000 :   return !err;
   32142              : }
   32143              : 
   32144              : /* Return a C++20 aggregate deduction candidate for TYPE initialized from
   32145              :    INIT.  */
   32146              : 
   32147              : static tree
   32148         9714 : maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
   32149              : {
   32150         9714 :   if (cxx_dialect < cxx20)
   32151              :     return NULL_TREE;
   32152              : 
   32153         9542 :   if (init == NULL_TREE)
   32154              :     return NULL_TREE;
   32155              : 
   32156         9471 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   32157              :     {
   32158          147 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32159          147 :       tree tinfo = get_template_info (under);
   32160          147 :       if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
   32161            3 :         return alias_ctad_tweaks (tmpl, guide);
   32162              :       return NULL_TREE;
   32163              :     }
   32164              : 
   32165              :   /* We might be creating a guide for a class member template, e.g.,
   32166              : 
   32167              :        template<typename U> struct A {
   32168              :          template<typename T> struct B { T t; };
   32169              :        };
   32170              : 
   32171              :      At this point, A will have been instantiated.  Below, we need to
   32172              :      use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types.  */
   32173         9324 :   const bool member_template_p
   32174         9324 :     = (DECL_TEMPLATE_INFO (tmpl)
   32175         9324 :        && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
   32176         9324 :   tree type = TREE_TYPE (tmpl);
   32177         9324 :   tree template_type = (member_template_p
   32178         9324 :                         ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
   32179         1649 :                         : type);
   32180         9324 :   if (!CP_AGGREGATE_TYPE_P (template_type))
   32181              :     return NULL_TREE;
   32182              : 
   32183              :   /* No aggregate candidate for copy-initialization.  */
   32184         3257 :   if (args->length() == 1)
   32185              :     {
   32186         3003 :       tree val = (*args)[0];
   32187         3003 :       if (is_spec_or_derived (TREE_TYPE (val), tmpl))
   32188              :         return NULL_TREE;
   32189              :     }
   32190              : 
   32191              :   /* If we encounter a problem, we just won't add the candidate.  */
   32192         3149 :   tsubst_flags_t complain = tf_none;
   32193              : 
   32194         3149 :   tree parms = NULL_TREE;
   32195         3149 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   32196              :     {
   32197         2877 :       init = reshape_init (template_type, init, complain);
   32198         2877 :       if (init == error_mark_node)
   32199              :         return NULL_TREE;
   32200         2866 :       parms = collect_ctor_idx_types (init, parms);
   32201              :     }
   32202          272 :   else if (TREE_CODE (init) == TREE_LIST)
   32203              :     {
   32204          270 :       int len = list_length (init);
   32205          291 :       for (tree binfo : BINFO_BASE_BINFOS (TYPE_BINFO (template_type)))
   32206              :         {
   32207           21 :           if (!len)
   32208              :             break;
   32209           21 :           parms = tree_cons (NULL_TREE, BINFO_TYPE (binfo), parms);
   32210           21 :           --len;
   32211              :         }
   32212          270 :       for (tree field = TYPE_FIELDS (template_type);
   32213          920 :            len;
   32214          650 :            --len, field = DECL_CHAIN (field))
   32215              :         {
   32216          655 :           field = next_aggregate_field (field);
   32217          655 :           if (!field)
   32218              :             return NULL_TREE;
   32219          650 :           tree ftype = finish_decltype_type (field, true, complain);
   32220          650 :           parms = tree_cons (NULL_TREE, ftype, parms);
   32221              :         }
   32222              :     }
   32223              :   else
   32224              :     /* Aggregate initialization doesn't apply to an initializer expression.  */
   32225              :     return NULL_TREE;
   32226              : 
   32227              :   /* If we're creating a deduction guide for a member class template,
   32228              :      we've used the original template pattern type for the reshape_init
   32229              :      above; this is done because we want PARMS to be a template parameter
   32230              :      type, something that can be deduced when used as a function template
   32231              :      parameter.  At this point the outer class template has already been
   32232              :      partially instantiated (we deferred the deduction until the enclosing
   32233              :      scope is non-dependent).  Therefore we have to partially instantiate
   32234              :      PARMS, so that its template level is properly reduced and we don't get
   32235              :      mismatches when deducing types using the guide with PARMS.  */
   32236         3131 :   if (member_template_p)
   32237              :     {
   32238           26 :       ++processing_template_decl;
   32239           26 :       parms = tsubst (parms, outer_template_args (tmpl), complain, init);
   32240           26 :       --processing_template_decl;
   32241              :     }
   32242              : 
   32243         3131 :   if (parms)
   32244              :     {
   32245         3035 :       tree last = parms;
   32246         3035 :       parms = nreverse (parms);
   32247         3035 :       TREE_CHAIN (last) = void_list_node;
   32248         3035 :       tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
   32249         3035 :       return guide;
   32250              :     }
   32251              : 
   32252              :   return NULL_TREE;
   32253              : }
   32254              : 
   32255              : /* UGUIDES are the deduction guides for the underlying template of alias
   32256              :    template TMPL; adjust them to be deduction guides for TMPL.
   32257              : 
   32258              :    This routine also handles C++23 inherited CTAD, in which case TMPL is a
   32259              :    TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
   32260              :    the template parameter list of the alias template (equivalently, of the
   32261              :    derived class) and TREE_VALUE the defining-type-id (equivalently, the
   32262              :    base whose guides we're inheriting).  UGUIDES are the base's guides.  */
   32263              : 
   32264              : static tree
   32265          184 : alias_ctad_tweaks (tree tmpl, tree uguides)
   32266              : {
   32267              :   /* [over.match.class.deduct]: When resolving a placeholder for a deduced
   32268              :      class type (9.2.8.2) where the template-name names an alias template A,
   32269              :      the defining-type-id of A must be of the form
   32270              : 
   32271              :      typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   32272              : 
   32273              :      as specified in 9.2.8.2. The guides of A are the set of functions or
   32274              :      function templates formed as follows. For each function or function
   32275              :      template f in the guides of the template named by the simple-template-id
   32276              :      of the defining-type-id, the template arguments of the return type of f
   32277              :      are deduced from the defining-type-id of A according to the process in
   32278              :      13.10.2.5 with the exception that deduction does not fail if not all
   32279              :      template arguments are deduced. Let g denote the result of substituting
   32280              :      these deductions into f. If substitution succeeds, form a function or
   32281              :      function template f' with the following properties and add it to the set
   32282              :      of guides of A:
   32283              : 
   32284              :      * The function type of f' is the function type of g.
   32285              : 
   32286              :      * If f is a function template, f' is a function template whose template
   32287              :      parameter list consists of all the template parameters of A (including
   32288              :      their default template arguments) that appear in the above deductions or
   32289              :      (recursively) in their default template arguments, followed by the
   32290              :      template parameters of f that were not deduced (including their default
   32291              :      template arguments), otherwise f' is not a function template.
   32292              : 
   32293              :      * The associated constraints (13.5.2) are the conjunction of the
   32294              :      associated constraints of g and a constraint that is satisfied if and only
   32295              :      if the arguments of A are deducible (see below) from the return type.
   32296              : 
   32297              :      * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
   32298              :      be so as well.
   32299              : 
   32300              :      * If f was generated from a deduction-guide (12.4.1.8), then f' is
   32301              :      considered to be so as well.
   32302              : 
   32303              :      * The explicit-specifier of f' is the explicit-specifier of g (if
   32304              :      any).  */
   32305              : 
   32306          184 :   enum { alias, inherited } ctad_kind;
   32307          184 :   tree atype, fullatparms, utype, name;
   32308          184 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   32309              :     {
   32310          125 :       ctad_kind = alias;
   32311          125 :       atype = TREE_TYPE (tmpl);
   32312          125 :       fullatparms = DECL_TEMPLATE_PARMS (tmpl);
   32313          125 :       utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32314          125 :       name = dguide_name (tmpl);
   32315              :     }
   32316              :   else
   32317              :     {
   32318           59 :       ctad_kind = inherited;
   32319           59 :       atype = NULL_TREE;
   32320           59 :       fullatparms = TREE_PURPOSE (tmpl);
   32321           59 :       utype = TREE_VALUE (tmpl);
   32322           59 :       name = dguide_name (TPARMS_PRIMARY_TEMPLATE
   32323              :                           (INNERMOST_TEMPLATE_PARMS (fullatparms)));
   32324              :     }
   32325              : 
   32326          184 :   tsubst_flags_t complain = tf_partial;
   32327          184 :   tree aguides = NULL_TREE;
   32328          184 :   tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
   32329          184 :   unsigned natparms = TREE_VEC_LENGTH (atparms);
   32330          960 :   for (tree f : lkp_range (uguides))
   32331              :     {
   32332          592 :       tree in_decl = f;
   32333          592 :       location_t loc = DECL_SOURCE_LOCATION (f);
   32334          592 :       tree ret = TREE_TYPE (TREE_TYPE (f));
   32335          592 :       tree fprime = f;
   32336          592 :       if (TREE_CODE (f) == TEMPLATE_DECL)
   32337              :         {
   32338          580 :           processing_template_decl_sentinel ptds (/*reset*/false);
   32339          580 :           ++processing_template_decl;
   32340              : 
   32341              :           /* Deduce template arguments for f from the type-id of A.  */
   32342          580 :           tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
   32343          580 :           unsigned len = TREE_VEC_LENGTH (ftparms);
   32344          580 :           tree targs = make_tree_vec (len);
   32345          580 :           int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
   32346          580 :           if (err)
   32347              :             /* CWG2664: Discard any deductions, still build the guide.  */
   32348           32 :             for (unsigned i = 0; i < len; ++i)
   32349           16 :               TREE_VEC_ELT (targs, i) = NULL_TREE;
   32350              : 
   32351              :           /* The number of parms for f' is the number of parms of A used in
   32352              :              the deduced arguments plus non-deduced parms of f.  */
   32353          580 :           unsigned ndlen = 0;
   32354          580 :           unsigned j;
   32355         2216 :           for (unsigned i = 0; i < len; ++i)
   32356         1636 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   32357          196 :               ++ndlen;
   32358          580 :           find_template_parameter_info ftpi (fullatparms);
   32359          580 :           ftpi.find_in_recursive (targs);
   32360          580 :           unsigned nusedatparms = ftpi.num_found ();
   32361          580 :           unsigned nfparms = nusedatparms + ndlen;
   32362          580 :           tree gtparms = make_tree_vec (nfparms);
   32363              : 
   32364              :           /* Set current_template_parms as in build_deduction_guide.  */
   32365          580 :           auto ctp = make_temp_override (current_template_parms);
   32366          580 :           current_template_parms = copy_node (fullatparms);
   32367          580 :           TREE_VALUE (current_template_parms) = gtparms;
   32368              : 
   32369          580 :           j = 0;
   32370          580 :           unsigned level = 1;
   32371              : 
   32372              :           /* First copy over the used parms of A.  */
   32373          580 :           tree atargs = make_tree_vec (natparms);
   32374         2318 :           for (unsigned i = 0; i < natparms; ++i)
   32375              :             {
   32376         1158 :               tree elt = TREE_VEC_ELT (atparms, i);
   32377         1158 :               if (ftpi.found (elt))
   32378              :                 {
   32379         1077 :                   unsigned index = j++;
   32380         1077 :                   tree nelt = rewrite_tparm_list (elt, index, level,
   32381              :                                                   atargs, i, complain);
   32382         1077 :                   TREE_VEC_ELT (gtparms, index) = nelt;
   32383              :                 }
   32384              :             }
   32385          580 :           gcc_checking_assert (j == nusedatparms);
   32386              : 
   32387              :           /* Adjust the deduced template args for f to refer to the A parms
   32388              :              with their new indexes.  */
   32389          580 :           if (nusedatparms && nusedatparms != natparms)
   32390           18 :             targs = tsubst_template_args (targs, atargs, complain, in_decl);
   32391              : 
   32392              :           /* Now rewrite the non-deduced parms of f.  */
   32393         1377 :           for (unsigned i = 0; ndlen && i < len; ++i)
   32394          797 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   32395              :               {
   32396          196 :                 --ndlen;
   32397          196 :                 unsigned index = j++;
   32398          196 :                 tree oldlist = TREE_VEC_ELT (ftparms, i);
   32399          196 :                 tree list = rewrite_tparm_list (oldlist, index, level,
   32400              :                                                 targs, i, complain);
   32401          196 :                 TREE_VEC_ELT (gtparms, index) = list;
   32402              :               }
   32403          580 :           gtparms = build_tree_list (size_one_node, gtparms);
   32404              : 
   32405              :           /* Substitute the deduced arguments plus the rewritten template
   32406              :              parameters into f to get g.  This covers the type, copyness,
   32407              :              guideness, and explicit-specifier.  */
   32408          580 :           tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain,
   32409              :                                /*use_spec_table=*/false);
   32410          580 :           if (g == error_mark_node)
   32411            3 :             continue;
   32412          577 :           DECL_NAME (g) = name;
   32413          577 :           if (nfparms == 0)
   32414              :             {
   32415              :               /* The targs are all non-dependent, so g isn't a template.  */
   32416           34 :               fprime = g;
   32417           34 :               ret = TREE_TYPE (TREE_TYPE (fprime));
   32418           34 :               goto non_template;
   32419              :             }
   32420          543 :           DECL_USE_TEMPLATE (g) = 0;
   32421          543 :           fprime = build_template_decl (g, gtparms, false);
   32422          543 :           DECL_TEMPLATE_RESULT (fprime) = g;
   32423          543 :           TREE_TYPE (fprime) = TREE_TYPE (g);
   32424          543 :           tree gtargs = template_parms_to_args (gtparms);
   32425          543 :           DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
   32426          543 :           DECL_PRIMARY_TEMPLATE (fprime) = fprime;
   32427              : 
   32428              :           /* Substitute the associated constraints.  */
   32429          543 :           tree ci = get_constraints (f);
   32430          543 :           if (ci)
   32431              :             {
   32432          119 :               if (tree outer_targs = outer_template_args (f))
   32433            6 :                 ci = tsubst_constraint_info (ci, outer_targs,
   32434              :                                              complain & ~tf_partial, in_decl);
   32435          119 :               ci = tsubst_constraint_info (ci, targs, complain, in_decl);
   32436              :             }
   32437          543 :           if (ci == error_mark_node)
   32438            0 :             continue;
   32439              : 
   32440              :           /* Add a constraint that the return type matches the instantiation of
   32441              :              A with the same template arguments.  */
   32442          543 :           ret = TREE_TYPE (TREE_TYPE (fprime));
   32443          543 :           if (ctad_kind == alias
   32444              :               /* Use template_args_equal instead of same_type_p to get the
   32445              :                  comparing_dependent_aliases behavior.  */
   32446          543 :               && !template_args_equal (atype, ret))
   32447              :             {
   32448           79 :               tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
   32449           79 :               ci = append_constraint (ci, same);
   32450              :             }
   32451              : 
   32452          543 :           if (ci)
   32453              :             {
   32454          186 :               remove_constraints (fprime);
   32455          186 :               set_constraints (fprime, ci);
   32456              :             }
   32457         1160 :         }
   32458              :       else
   32459              :         {
   32460              :           /* For a non-template deduction guide, if the arguments of A aren't
   32461              :              deducible from the return type, don't add the candidate.  */
   32462           12 :         non_template:
   32463           49 :           if (ctad_kind == alias
   32464           46 :               && !type_targs_deducible_from (tmpl, ret))
   32465            3 :             continue;
   32466              :         }
   32467              : 
   32468              :       /* Rewrite the return type of the inherited guide in terms of the
   32469              :          derived class.  This is specified as replacing the return type R
   32470              :          with typename CC<R>::type where the partially specialized CC maps a
   32471              :          base class specialization to a specialization of the derived class
   32472              :          having such a base (inducing substitution failure if no such derived
   32473              :          class exists).
   32474              : 
   32475              :          As specified this mapping would be done at instantiation time using
   32476              :          non-dependent template arguments, but we do it ahead of time using
   32477              :          the generic arguments.  This seems to be good enough since generic
   32478              :          deduction should succeed only if concrete deduction would.  */
   32479          586 :       if (ctad_kind == inherited)
   32480              :         {
   32481          268 :           processing_template_decl_sentinel ptds (/*reset*/false);
   32482          268 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   32483          235 :             ++processing_template_decl;
   32484              : 
   32485          268 :           tree targs = type_targs_deducible_from (tmpl, ret);
   32486          268 :           if (!targs)
   32487           15 :             continue;
   32488              : 
   32489          253 :           if (TREE_CODE (f) != TEMPLATE_DECL)
   32490            6 :             fprime = copy_decl (fprime);
   32491          253 :           tree fntype = TREE_TYPE (fprime);
   32492          253 :           ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), targs,
   32493              :                                        in_decl, NULL_TREE, complain);
   32494          253 :           fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype),
   32495          253 :                                         TYPE_NO_NAMED_ARGS_STDARG_P (fntype));
   32496          253 :           TREE_TYPE (fprime) = fntype;
   32497          253 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   32498          235 :             TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype;
   32499          253 :           set_inherited_guide_context (fprime, utype);
   32500          268 :         }
   32501              : 
   32502          571 :       aguides = lookup_add (fprime, aguides);
   32503              :     }
   32504              : 
   32505          184 :   return aguides;
   32506              : }
   32507              : 
   32508              : /* CTOR is a using-decl inheriting the constructors of some base of the class
   32509              :    template TMPL; adjust the base's guides be deduction guides for TMPL.  */
   32510              : 
   32511              : static tree
   32512           66 : inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain)
   32513              : {
   32514              :   /* [over.match.class.deduct]: In addition, if C is defined and inherits
   32515              :      constructors ([namespace.udecl]) from a direct base class denoted in the
   32516              :      base-specifier-list by a class-or-decltype B, let A be an alias template
   32517              :      whose template parameter list is that of C and whose defining-type-id is
   32518              :      B.  If A is a deducible template ([dcl.type.simple]), the set contains the
   32519              :      guides of A with the return type R of each guide replaced with typename
   32520              :      CC::type given a class template
   32521              : 
   32522              :      template <typename> class CC;
   32523              : 
   32524              :      whose primary template is not defined and with a single partial
   32525              :      specialization whose template parameter list is that of A and whose
   32526              :      template argument list is a specialization of A with the template argument
   32527              :      list of A ([temp.dep.type]) having a member typedef type designating a
   32528              :      template specialization with the template argument list of A but with C as
   32529              :      the template.  */
   32530              : 
   32531           66 :   tree scope = USING_DECL_SCOPE (ctor);
   32532           66 :   if (TREE_CODE (scope) == TYPENAME_TYPE
   32533           66 :       && (TYPE_IDENTIFIER (TYPE_CONTEXT (scope))
   32534            7 :           == TYPENAME_TYPE_FULLNAME (scope)))
   32535              :     /* Recognize using B<T>::B::B as an inherited constructor.  */
   32536              :     /* FIXME: Also recognize using C::B::B?  We might have to call
   32537              :        resolve_typename_type for that.  */
   32538            4 :     scope = TYPE_CONTEXT (scope);
   32539           63 :   if (!CLASS_TYPE_P (scope)
   32540           63 :       || !CLASSTYPE_TEMPLATE_INFO (scope)
   32541          125 :       || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
   32542              :     return NULL_TREE;
   32543              : 
   32544           59 :   tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope);
   32545           59 :   bool any_dguides_p;
   32546           59 :   tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope),
   32547              :                                        any_dguides_p, complain);
   32548           59 :   return alias_ctad_tweaks (t, uguides);
   32549              : }
   32550              : 
   32551              : /* If template arguments for TMPL can be deduced from TYPE, return
   32552              :    the deduced arguments, otherwise return NULL_TREE.
   32553              :    Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
   32554              :    [over.match.class.deduct].
   32555              : 
   32556              :    This check is specified in terms of partial specialization, so the behavior
   32557              :    should be parallel to that of get_partial_spec_bindings.  */
   32558              : 
   32559              : tree
   32560          366 : type_targs_deducible_from (tree tmpl, tree type)
   32561              : {
   32562          366 :   tree tparms, ttype;
   32563          366 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   32564              :     {
   32565              :       /* If tmpl is a class template, this is trivial: it's deducible if
   32566              :          TYPE is a specialization of TMPL.  */
   32567           98 :       if (DECL_CLASS_TEMPLATE_P (tmpl))
   32568              :         {
   32569            0 :           if (CLASS_TYPE_P (type)
   32570            0 :               && CLASSTYPE_TEMPLATE_INFO (type)
   32571            0 :               && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
   32572            0 :             return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   32573              :           else
   32574              :             return NULL_TREE;
   32575              :         }
   32576              : 
   32577              :       /* Otherwise it's an alias template.  */
   32578           98 :       tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
   32579           98 :       ttype = TREE_TYPE (tmpl);
   32580              :     }
   32581              :   else
   32582              :     {
   32583              :       /* TMPL is a synthetic alias template represented as a TREE_LIST as
   32584              :          per alias_ctad_tweaks.  */
   32585          268 :       tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
   32586          268 :       ttype = TREE_VALUE (tmpl);
   32587          268 :       tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype);
   32588          256 :       if (!ti)
   32589              :         /* TTYPE is a typedef to a template-id.  */
   32590           12 :         ti = TYPE_TEMPLATE_INFO (ttype);
   32591          268 :       tmpl = TI_TEMPLATE (ti);
   32592              :     }
   32593              : 
   32594          366 :   int len = TREE_VEC_LENGTH (tparms);
   32595          366 :   tree targs = make_tree_vec (len);
   32596          366 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   32597              : 
   32598          366 :  again:
   32599          366 :   if (unify (tparms, targs, ttype, type,
   32600              :              UNIFY_ALLOW_NONE, false))
   32601              :     return NULL_TREE;
   32602              : 
   32603              :   /* We don't fail on an undeduced targ the second time through (like
   32604              :      get_partial_spec_bindings) because we're going to try defaults.  */
   32605         1236 :   for (int i =  0; i < len; ++i)
   32606          896 :     if (! TREE_VEC_ELT (targs, i))
   32607              :       {
   32608           64 :         tree tparm = TREE_VEC_ELT (tparms, i);
   32609           64 :         tparm = TREE_VALUE (tparm);
   32610              : 
   32611           64 :         if (!tried_array_deduction
   32612           52 :             && TREE_CODE (tparm) == TYPE_DECL)
   32613              :           {
   32614           40 :             try_array_deduction (tparms, targs, ttype);
   32615           40 :             tried_array_deduction = true;
   32616           40 :             if (TREE_VEC_ELT (targs, i))
   32617            0 :               goto again;
   32618              :           }
   32619              :         /* If the type parameter is a parameter pack, then it will be deduced
   32620              :            to an empty parameter pack.  This is another case that doesn't model
   32621              :            well as partial specialization.  */
   32622           64 :         if (template_parameter_pack_p (tparm))
   32623              :           {
   32624            6 :             tree arg;
   32625            6 :             if (TREE_CODE (tparm) == PARM_DECL)
   32626              :               {
   32627            6 :                 arg = make_node (NONTYPE_ARGUMENT_PACK);
   32628            6 :                 TREE_CONSTANT (arg) = 1;
   32629              :               }
   32630              :             else
   32631            0 :               arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   32632            6 :             ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   32633            6 :             TREE_VEC_ELT (targs, i) = arg;
   32634              :           }
   32635              :       }
   32636              : 
   32637              :   /* Maybe add in default template args.  This seems like a flaw in the
   32638              :      specification in terms of partial specialization, since it says the
   32639              :      partial specialization has the template parameter list of A, but a
   32640              :      partial specialization can't have default targs.  */
   32641          340 :   targs = coerce_template_parms (tparms, targs, tmpl, tf_none);
   32642          340 :   if (targs == error_mark_node)
   32643              :     return NULL_TREE;
   32644              : 
   32645              :   /* I believe we don't need the template_template_parm_bindings_ok_p call
   32646              :      because coerce_template_parms did coerce_template_template_parms.  */
   32647              : 
   32648          310 :   if (!constraints_satisfied_p (tmpl, targs))
   32649           12 :     return NULL_TREE;
   32650              : 
   32651              :   return targs;
   32652              : }
   32653              : 
   32654              : /* Return artificial deduction guides built from the constructors of class
   32655              :    template TMPL.  */
   32656              : 
   32657              : static tree
   32658         5746 : ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
   32659              : {
   32660         5746 :   tree outer_args = outer_template_args (tmpl);
   32661         5746 :   tree type = TREE_TYPE (most_general_template (tmpl));
   32662              : 
   32663         5746 :   tree cands = NULL_TREE;
   32664              : 
   32665       159147 :   for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
   32666              :     {
   32667              :       /* We handle C++23 inherited CTAD below.  */
   32668        79004 :       if (iter.using_p ())
   32669           39 :         continue;
   32670              : 
   32671        78965 :       tree guide = build_deduction_guide (type, *iter, outer_args, complain);
   32672        78965 :       cands = lookup_add (guide, cands);
   32673              :     }
   32674              : 
   32675         5746 :   if (cxx_dialect >= cxx23)
   32676              :     /* FIXME: CLASSTYPE_CONSTRUCTORS doesn't contain inherited constructors if
   32677              :        e.g. the class also has a user-defined constructor.  So instead iterate
   32678              :        over TYPE_FIELDS manually to robustly find all relevant using-decls.  */
   32679       221636 :     for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
   32680       216940 :       if (TREE_CODE (field) == USING_DECL
   32681       216940 :           && DECL_NAME (field) == ctor_identifier)
   32682              :         {
   32683           66 :           tree uguides = inherited_ctad_tweaks (tmpl, field, complain);
   32684           66 :           if (uguides)
   32685           53 :             cands = lookup_add (uguides, cands);
   32686              :         }
   32687              : 
   32688              :   /* Add implicit default constructor deduction guide.  */
   32689         5746 :   if (!TYPE_HAS_USER_CONSTRUCTOR (type))
   32690              :     {
   32691          777 :       tree guide = build_deduction_guide (type, type, outer_args,
   32692              :                                           complain);
   32693          777 :       cands = lookup_add (guide, cands);
   32694              :     }
   32695              : 
   32696              :   /* Add copy guide.  */
   32697         5746 :   {
   32698         5746 :     tree gtype = build_reference_type (type);
   32699         5746 :     tree guide = build_deduction_guide (type, gtype, outer_args,
   32700              :                                         complain);
   32701         5746 :     cands = lookup_add (guide, cands);
   32702              :   }
   32703              : 
   32704         5746 :   return cands;
   32705              : }
   32706              : 
   32707              : static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
   32708              : 
   32709              : /* Return the non-aggregate deduction guides for deducible template TMPL.  The
   32710              :    aggregate candidate is added separately because it depends on the
   32711              :    initializer.  Set ANY_DGUIDES_P if we find a non-implicit deduction
   32712              :    guide.  */
   32713              : 
   32714              : static tree
   32715        31005 : deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
   32716              : {
   32717        31005 :   tree guides = NULL_TREE;
   32718        31005 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   32719              :     {
   32720          222 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32721          222 :       tree tinfo = get_template_info (under);
   32722          222 :       guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
   32723              :                                      complain);
   32724              :     }
   32725              :   else
   32726              :     {
   32727        30783 :       guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
   32728              :                                       dguide_name (tmpl),
   32729              :                                       LOOK_want::ANY_REACHABLE,
   32730              :                                       /*complain=*/false);
   32731        30783 :       if (guides == error_mark_node)
   32732              :         guides = NULL_TREE;
   32733              :       else
   32734        21162 :         any_dguides_p = true;
   32735              :     }
   32736              : 
   32737              :   /* Cache the deduction guides for a template.  We also remember the result of
   32738              :      lookup, and rebuild everything if it changes; should be very rare.  */
   32739              :   /* FIXME: Also rebuild if this is a class template that inherits guides from a
   32740              :      base class, and lookup for the latter changed.  */
   32741        31005 :   tree_pair_p cache = NULL;
   32742        62010 :   if (tree_pair_p &r
   32743        31005 :       = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
   32744              :     {
   32745        25163 :       cache = r;
   32746        25163 :       if (cache->purpose == guides)
   32747        25137 :         return cache->value;
   32748              :     }
   32749              :   else
   32750              :     {
   32751         5842 :       r = cache = ggc_cleared_alloc<tree_pair_s> ();
   32752         5842 :       cache->purpose = guides;
   32753              :     }
   32754              : 
   32755         5868 :   tree cands = NULL_TREE;
   32756         5868 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   32757          122 :     cands = alias_ctad_tweaks (tmpl, guides);
   32758              :   else
   32759              :     {
   32760         5746 :       cands = ctor_deduction_guides_for (tmpl, complain);
   32761        21353 :       for (lkp_iterator it (guides); it; ++it)
   32762        15607 :         cands = lookup_add (*it, cands);
   32763              :     }
   32764              : 
   32765         5868 :   cache->value = cands;
   32766         5868 :   return cands;
   32767              : }
   32768              : 
   32769              : /* Return whether TMPL is a (class template argument-) deducible template.  */
   32770              : 
   32771              : bool
   32772    945300582 : ctad_template_p (tree tmpl)
   32773              : {
   32774              :   /* A deducible template is either a class template or is an alias template
   32775              :      whose defining-type-id is of the form
   32776              : 
   32777              :       typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   32778              : 
   32779              :      where the nested-name-specifier (if any) is non-dependent and the
   32780              :      template-name of the simple-template-id names a deducible template.  */
   32781              : 
   32782     14899684 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   32783    953723083 :       && IDENTIFIER_TRAIT_P (DECL_NAME (tmpl)))
   32784              :     /* Don't consider CTAD for templates with the same name as a trait; that
   32785              :        is ambiguous with e.g. __is_invocable(_Fn,_Args...).  */
   32786              :     return false;
   32787     14743664 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   32788    951676859 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   32789              :     return true;
   32790    936705841 :   if (!DECL_ALIAS_TEMPLATE_P (tmpl))
   32791              :     return false;
   32792       145352 :   tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   32793       145352 :   if (tree tinfo = get_template_info (orig))
   32794        27557 :     return ctad_template_p (TI_TEMPLATE (tinfo));
   32795              :   return false;
   32796              : }
   32797              : 
   32798              : /* Deduce template arguments for the class template placeholder PTYPE for
   32799              :    template TMPL based on the initializer INIT, and return the resulting
   32800              :    type.  */
   32801              : 
   32802              : static tree
   32803        42378 : do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
   32804              :                     int flags, tsubst_flags_t complain)
   32805              : {
   32806              :   /* We should have handled this in the caller.  */
   32807        42378 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   32808              :     return ptype;
   32809              : 
   32810              :   /* If the class was erroneous, don't try to deduce, because that
   32811              :      can generate a lot of diagnostic.  */
   32812        42263 :   if (TREE_TYPE (tmpl)
   32813        42263 :       && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
   32814        84523 :       && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
   32815              :     return ptype;
   32816              : 
   32817              :   /* Wait until the enclosing scope is non-dependent.  */
   32818        84518 :   if (DECL_CLASS_SCOPE_P (tmpl)
   32819        45168 :       && dependent_type_p (DECL_CONTEXT (tmpl)))
   32820              :     return ptype;
   32821              : 
   32822              :   /* Initializing one placeholder from another.  */
   32823        42238 :   if (init
   32824        42100 :       && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
   32825        34153 :           || (TREE_CODE (init) == EXPR_PACK_EXPANSION
   32826            3 :               && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
   32827              :                   == TEMPLATE_PARM_INDEX)))
   32828         7950 :       && is_auto (TREE_TYPE (init))
   32829        50188 :       && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
   32830         7890 :     return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
   32831              : 
   32832        34348 :   if (!ctad_template_p (tmpl))
   32833              :     {
   32834            3 :       if (complain & tf_error)
   32835            3 :         error ("non-deducible template %qT used without template arguments", tmpl);
   32836            3 :       return error_mark_node;
   32837              :     }
   32838        34345 :   else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
   32839              :     {
   32840            7 :       if (complain & tf_error)
   32841              :         {
   32842              :           /* Be permissive with equivalent alias templates.  */
   32843            7 :           tree u = get_underlying_template (tmpl);
   32844            7 :           auto_diagnostic_group d;
   32845            1 :           const enum diagnostics::kind dk = ((u == tmpl)
   32846            7 :                                              ? diagnostics::kind::error
   32847              :                                              : diagnostics::kind::pedwarn);
   32848            7 :           bool complained
   32849            7 :             = emit_diagnostic (dk, input_location, 0,
   32850              :                                "alias template deduction only available "
   32851              :                                "with %<-std=c++20%> or %<-std=gnu++20%>");
   32852            7 :           if (u == tmpl)
   32853            6 :             return error_mark_node;
   32854            1 :           else if (complained)
   32855              :             {
   32856            1 :               inform (input_location, "use %qD directly instead", u);
   32857            1 :               tmpl = u;
   32858              :             }
   32859            7 :         }
   32860              :       else
   32861            0 :         return error_mark_node;
   32862              :     }
   32863              : 
   32864              :   /* Wait until the initializer is non-dependent.  */
   32865        34339 :   if (type_dependent_expression_p (init))
   32866              :     return ptype;
   32867              : 
   32868        30731 :   if (outer_targs)
   32869              :     {
   32870         6671 :       int args_depth = TMPL_ARGS_DEPTH (outer_targs);
   32871         6671 :       int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   32872         6671 :       if (parms_depth > 1)
   32873              :         {
   32874              :           /* Substitute outer arguments into this CTAD template from the
   32875              :              current instantiation.  */
   32876            3 :           int want = std::min (args_depth, parms_depth - 1);
   32877            3 :           outer_targs = strip_innermost_template_args (outer_targs,
   32878              :                                                        args_depth - want);
   32879            3 :           tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
   32880            3 :           if (tmpl == error_mark_node)
   32881              :             return error_mark_node;
   32882              :         }
   32883              :     }
   32884              : 
   32885              :   /* Don't bother with the alias rules for an equivalent template.  */
   32886        30731 :   tmpl = get_underlying_template (tmpl);
   32887              : 
   32888        30731 :   tree type = TREE_TYPE (tmpl);
   32889              : 
   32890        30731 :   bool try_list_cand = false;
   32891        30731 :   bool list_init_p = false;
   32892              : 
   32893        30731 :   releasing_vec rv_args = NULL;
   32894        30731 :   vec<tree,va_gc> *&args = *&rv_args;
   32895        30731 :   if (init == NULL_TREE)
   32896          137 :     args = make_tree_vector ();
   32897        30594 :   else if (BRACE_ENCLOSED_INITIALIZER_P (init))
   32898              :     {
   32899        10289 :       list_init_p = true;
   32900        10289 :       try_list_cand = true;
   32901        10289 :       if (CONSTRUCTOR_NELTS (init) == 1
   32902         9065 :           && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
   32903              :         {
   32904              :           /* As an exception, the first phase in 16.3.1.7 (considering the
   32905              :              initializer list as a single argument) is omitted if the
   32906              :              initializer list consists of a single expression of type cv U,
   32907              :              where U is a specialization of C or a class derived from a
   32908              :              specialization of C.  */
   32909         2995 :           tree elt = CONSTRUCTOR_ELT (init, 0)->value;
   32910         2995 :           if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
   32911           36 :             try_list_cand = false;
   32912              :         }
   32913           36 :       if (try_list_cand || is_std_init_list (type))
   32914        10253 :         args = make_tree_vector_single (init);
   32915              :       else
   32916           36 :         args = make_tree_vector_from_ctor (init);
   32917              :     }
   32918        20305 :   else if (TREE_CODE (init) == TREE_LIST)
   32919        10845 :     args = make_tree_vector_from_list (init);
   32920              :   else
   32921         9460 :     args = make_tree_vector_single (init);
   32922              : 
   32923              :   /* Do this now to avoid problems with erroneous args later on.  */
   32924        30731 :   args = resolve_args (args, complain);
   32925        30731 :   if (args == NULL)
   32926            7 :     return error_mark_node;
   32927              : 
   32928        30724 :   bool any_dguides_p = false;
   32929        30724 :   tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
   32930        30724 :   if (cands == error_mark_node)
   32931              :     return error_mark_node;
   32932              : 
   32933              :   /* Prune explicit deduction guides in copy-initialization context (but
   32934              :      not copy-list-initialization).  */
   32935        30718 :   bool elided = false;
   32936        30718 :   if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
   32937              :     {
   32938        64197 :       for (lkp_iterator iter (cands); !elided && iter; ++iter)
   32939        56170 :         if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   32940         2519 :           elided = true;
   32941              : 
   32942         8027 :       if (elided)
   32943              :         {
   32944              :           /* Found a nonconverting guide, prune the candidates.  */
   32945         2519 :           tree pruned = NULL_TREE;
   32946        42237 :           for (lkp_iterator iter (cands); iter; ++iter)
   32947        39718 :             if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   32948        37119 :               pruned = lookup_add (*iter, pruned);
   32949              : 
   32950         2519 :           cands = pruned;
   32951              :         }
   32952              :     }
   32953              : 
   32954        30718 :   if (!any_dguides_p)
   32955         9567 :     if (tree guide = maybe_aggr_guide (tmpl, init, args))
   32956         3035 :       cands = lookup_add (guide, cands);
   32957              : 
   32958        30718 :   tree fndecl = error_mark_node;
   32959              : 
   32960              :   /* If this is list-initialization and the class has a list guide, first
   32961              :      try deducing from the list as a single argument, as [over.match.list].  */
   32962        30718 :   if (try_list_cand)
   32963              :     {
   32964        10253 :       tree list_cands = NULL_TREE;
   32965       184453 :       for (tree dg : lkp_range (cands))
   32966       163947 :         if (is_list_ctor (dg))
   32967         1487 :           list_cands = lookup_add (dg, list_cands);
   32968        10253 :       if (list_cands)
   32969         1130 :         fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
   32970        10253 :       if (fndecl == error_mark_node)
   32971              :         {
   32972              :           /* That didn't work, now try treating the list as a sequence of
   32973              :              arguments.  */
   32974         9459 :           release_tree_vector (args);
   32975         9459 :           args = make_tree_vector_from_ctor (init);
   32976         9459 :           args = resolve_args (args, complain);
   32977         9459 :           if (args == NULL)
   32978            0 :             return error_mark_node;
   32979              :         }
   32980              :     }
   32981              : 
   32982        30718 :   if (elided && !cands)
   32983              :     {
   32984            0 :       error ("cannot deduce template arguments for copy-initialization"
   32985              :              " of %qT, as it has no non-explicit deduction guides or "
   32986              :              "user-declared constructors", type);
   32987            0 :       return error_mark_node;
   32988              :     }
   32989        30718 :   else if (!cands && fndecl == error_mark_node)
   32990              :     {
   32991            0 :       error ("cannot deduce template arguments of %qT, as it has no viable "
   32992              :              "deduction guides", type);
   32993            0 :       return error_mark_node;
   32994              :     }
   32995              : 
   32996        30718 :   if (fndecl == error_mark_node)
   32997        29924 :     fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
   32998              : 
   32999        30718 :   if (fndecl == error_mark_node)
   33000              :     {
   33001          437 :       if (complain & tf_warning_or_error)
   33002              :         {
   33003          198 :           auto_diagnostic_group d;
   33004          198 :           error ("class template argument deduction failed:");
   33005          198 :           perform_dguide_overload_resolution (cands, args, complain);
   33006          198 :           if (elided)
   33007           27 :             inform (input_location, "explicit deduction guides not considered "
   33008              :                     "for copy-initialization");
   33009          198 :         }
   33010          437 :       return error_mark_node;
   33011              :     }
   33012              :   /* [over.match.list]/1: In copy-list-initialization, if an explicit
   33013              :      constructor is chosen, the initialization is ill-formed.  */
   33014        30281 :   else if (flags & LOOKUP_ONLYCONVERTING)
   33015              :     {
   33016         8149 :       if (DECL_NONCONVERTING_P (fndecl))
   33017              :         {
   33018           35 :           if (complain & tf_warning_or_error)
   33019              :             {
   33020              :               // TODO: Pass down location from cp_finish_decl.
   33021           35 :               auto_diagnostic_group d;
   33022           35 :               error ("class template argument deduction for %qT failed: "
   33023              :                      "explicit deduction guide selected in "
   33024              :                      "copy-list-initialization", type);
   33025           35 :               inform (DECL_SOURCE_LOCATION (fndecl),
   33026              :                       "explicit deduction guide declared here");
   33027              : 
   33028           35 :             }
   33029           35 :           return error_mark_node;
   33030              :         }
   33031              :     }
   33032              : 
   33033              :   /* If CTAD succeeded but the type doesn't have any explicit deduction
   33034              :      guides, this deduction might not be what the user intended.  */
   33035        30246 :   if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
   33036              :     {
   33037         8480 :       auto_diagnostic_group d;
   33038         8480 :       if ((!DECL_IN_SYSTEM_HEADER (fndecl)
   33039            6 :            || global_dc->m_warn_system_headers)
   33040         8486 :           && warning (OPT_Wctad_maybe_unsupported,
   33041              :                       "%qT may not intend to support class template argument "
   33042              :                       "deduction", type))
   33043           24 :         inform (input_location, "add a deduction guide to suppress this "
   33044              :                 "warning");
   33045         8480 :     }
   33046              : 
   33047        30246 :   return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
   33048        30246 :                                   cp_type_quals (ptype));
   33049        30731 : }
   33050              : 
   33051              : /* Return true if INIT is an unparenthesized id-expression or an
   33052              :    unparenthesized class member access.  Used for the argument of
   33053              :    decltype(auto).  */
   33054              : 
   33055              : bool
   33056      9070920 : unparenthesized_id_or_class_member_access_p (tree init)
   33057              : {
   33058      9070920 :   STRIP_ANY_LOCATION_WRAPPER (init);
   33059              : 
   33060              :   /* We need to be able to tell '(r)' and 'r' apart (when it's of
   33061              :      reference type).  Only the latter is an id-expression.  */
   33062      1090428 :   if (REFERENCE_REF_P (init)
   33063      9619154 :       && !REF_PARENTHESIZED_P (init))
   33064         4238 :     init = TREE_OPERAND (init, 0);
   33065      9070920 :   return (DECL_P (init)
   33066      9070920 :           || ((TREE_CODE (init) == COMPONENT_REF
   33067      9070419 :                || TREE_CODE (init) == SCOPE_REF)
   33068          247 :               && !REF_PARENTHESIZED_P (init)));
   33069              : }
   33070              : 
   33071              : /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
   33072              :    from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
   33073              :    The CONTEXT determines the context in which auto deduction is performed
   33074              :    and is used to control error diagnostics.  FLAGS are the LOOKUP_* flags.
   33075              : 
   33076              :    OUTER_TARGS is used during template argument deduction (context == adc_unify)
   33077              :    to properly substitute the result.  It's also used in the adc_unify and
   33078              :    adc_requirement contexts to communicate the necessary template arguments
   33079              :    to satisfaction.  OUTER_TARGS is ignored in other contexts.
   33080              : 
   33081              :    Additionally for adc_unify contexts TMPL is the template for which TYPE
   33082              :    is a template parameter type.
   33083              : 
   33084              :    For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
   33085              :    scope may be appended to the list of deduced template arguments prior to
   33086              :    determining constraint satisfaction as appropriate.  */
   33087              : 
   33088              : tree
   33089     25517084 : do_auto_deduction (tree type, tree init, tree auto_node,
   33090              :                    tsubst_flags_t complain /* = tf_warning_or_error */,
   33091              :                    auto_deduction_context context /* = adc_unspecified */,
   33092              :                    tree outer_targs /* = NULL_TREE */,
   33093              :                    int flags /* = LOOKUP_NORMAL */,
   33094              :                    tree tmpl /* = NULL_TREE */)
   33095              : {
   33096     25517084 :   if (type == error_mark_node || init == error_mark_node)
   33097              :     return error_mark_node;
   33098              : 
   33099     25515841 :   if (init && type_dependent_expression_p (init)
   33100     36505865 :       && context != adc_unify)
   33101              :     /* Defining a subset of type-dependent expressions that we can deduce
   33102              :        from ahead of time isn't worth the trouble.  */
   33103              :     return type;
   33104              : 
   33105              :   /* Similarly, we can't deduce from another undeduced decl.  */
   33106     14672414 :   if (init && undeduced_auto_decl (init))
   33107              :     return type;
   33108              : 
   33109              :   /* We may be doing a partial substitution, but we still want to replace
   33110              :      auto_node.  */
   33111     14672401 :   complain &= ~tf_partial;
   33112              : 
   33113     14672401 :   if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
   33114              :     {
   33115              :       /* We don't recurse here because we can't deduce from a nested
   33116              :          initializer_list.  */
   33117        11060 :       if (CONSTRUCTOR_ELTS (init))
   33118        30946 :         for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
   33119              :           {
   33120        21151 :             elt.value = resolve_nondeduced_context (elt.value, complain);
   33121        21151 :             if (!mark_single_function (elt.value, complain))
   33122           15 :               return error_mark_node;
   33123              :           }
   33124              :     }
   33125     14661341 :   else if (init)
   33126              :     {
   33127     14661169 :       init = resolve_nondeduced_context (init, complain);
   33128     14661169 :       if (!mark_single_function (init, complain))
   33129            0 :         return error_mark_node;
   33130              :     }
   33131              : 
   33132              :   /* In C++23, we must deduce the type to int&& for code like
   33133              :        decltype(auto) f(int&& x) { return (x); }
   33134              :      or
   33135              :        auto&& f(int x) { return x; }
   33136              :      so we use treat_lvalue_as_rvalue_p.  But don't do it for
   33137              :        decltype(auto) f(int x) { return x; }
   33138              :      where we should deduce 'int' rather than 'int&&'; transmogrifying
   33139              :      INIT to an rvalue would break that.  */
   33140     14672386 :   tree r;
   33141     14672386 :   if (cxx_dialect >= cxx23
   33142      4384685 :       && context == adc_return_type
   33143       656346 :       && (!AUTO_IS_DECLTYPE (auto_node)
   33144       143589 :           || !unparenthesized_id_or_class_member_access_p (init))
   33145     15328682 :       && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
   33146              :                                         /*return*/true)))
   33147        33489 :     init = r;
   33148              : 
   33149     14672386 :   if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   33150              :     /* C++17 class template argument deduction.  */
   33151        42378 :     return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
   33152              : 
   33153     14630008 :   if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
   33154              :     /* Nothing we can do with this, even in deduction context.  */
   33155              :     return type;
   33156              : 
   33157     14605002 :   location_t loc = cp_expr_loc_or_input_loc (init);
   33158              : 
   33159              :   /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
   33160              :      with either a new invented type template parameter U or, if the
   33161              :      initializer is a braced-init-list (8.5.4), with
   33162              :      std::initializer_list<U>.  */
   33163     14605002 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   33164              :     {
   33165          662 :       if (!DIRECT_LIST_INIT_P (init))
   33166          416 :         type = listify_autos (type, auto_node);
   33167          246 :       else if (CONSTRUCTOR_NELTS (init) == 1)
   33168          240 :         init = CONSTRUCTOR_ELT (init, 0)->value;
   33169              :       else
   33170              :         {
   33171            6 :           if (complain & tf_warning_or_error)
   33172              :             {
   33173            6 :               auto_diagnostic_group d;
   33174            6 :               if (permerror (loc, "direct-list-initialization of "
   33175              :                              "%<auto%> requires exactly one element"))
   33176            6 :                 inform (loc,
   33177              :                         "for deduction to %<std::initializer_list%>, use copy-"
   33178              :                         "list-initialization (i.e. add %<=%> before the %<{%>)");
   33179            6 :             }
   33180            6 :           type = listify_autos (type, auto_node);
   33181              :         }
   33182              :     }
   33183              : 
   33184     14605002 :   if (type == error_mark_node || init == error_mark_node)
   33185              :     return error_mark_node;
   33186              : 
   33187     14604981 :   tree targs;
   33188     14604981 :   if (context == adc_decomp_type
   33189     14604981 :       && auto_node == type
   33190     14604981 :       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
   33191              :     {
   33192              :       /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
   33193              :          and initializer has array type, deduce cv-qualified array type.  */
   33194        10487 :       targs = make_tree_vec (1);
   33195        10487 :       TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
   33196              :     }
   33197     14594494 :   else if (AUTO_IS_DECLTYPE (auto_node))
   33198              :     {
   33199      8927331 :       const bool id = unparenthesized_id_or_class_member_access_p (init);
   33200      8927331 :       tree deduced = finish_decltype_type (init, id, complain);
   33201      8927331 :       deduced = canonicalize_type_argument (deduced, complain);
   33202      8927331 :       if (deduced == error_mark_node)
   33203              :         return error_mark_node;
   33204      8927328 :       targs = make_tree_vec (1);
   33205      8927328 :       TREE_VEC_ELT (targs, 0) = deduced;
   33206              :     }
   33207              :   else
   33208              :     {
   33209      5667163 :       if (error_operand_p (init))
   33210              :         return error_mark_node;
   33211              : 
   33212      5667151 :       tree parms = build_tree_list (NULL_TREE, type);
   33213      5667151 :       tree tparms = make_tree_vec (1);
   33214      5667151 :       TREE_VEC_ELT (tparms, 0)
   33215      5667151 :         = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
   33216              : 
   33217      5667151 :       targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   33218      5667151 :       int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
   33219              :                                        DEDUCE_CALL,
   33220              :                                        NULL, /*explain_p=*/false);
   33221      5667151 :       if (val > 0)
   33222              :         {
   33223           74 :           if (processing_template_decl)
   33224              :             /* Try again at instantiation time.  */
   33225              :             return type;
   33226           65 :           if (type && type != error_mark_node
   33227           65 :               && (complain & tf_error))
   33228              :             /* If type is error_mark_node a diagnostic must have been
   33229              :                emitted by now.  Also, having a mention to '<type error>'
   33230              :                in the diagnostic is not really useful to the user.  */
   33231              :             {
   33232           62 :               if (cfun
   33233           32 :                   && FNDECL_USED_AUTO (current_function_decl)
   33234           12 :                   && (auto_node
   33235           12 :                       == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
   33236           74 :                   && LAMBDA_FUNCTION_P (current_function_decl))
   33237            6 :                 error_at (loc, "unable to deduce lambda return type from %qE",
   33238              :                           init);
   33239              :               else
   33240           56 :                 error_at (loc, "unable to deduce %qT from %qE", type, init);
   33241           62 :               type_unification_real (tparms, targs, parms, &init, 1, 0,
   33242              :                                      DEDUCE_CALL,
   33243              :                                      NULL, /*explain_p=*/true);
   33244              :             }
   33245           65 :           return error_mark_node;
   33246              :         }
   33247              :     }
   33248              : 
   33249              :   /* Check any placeholder constraints against the deduced type. */
   33250     14604892 :   if (processing_template_decl && context == adc_unify)
   33251              :     /* Constraints will be checked after deduction.  */;
   33252     14491029 :   else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
   33253              :     {
   33254      8777874 :       if (context == adc_unify)
   33255              :         /* Simple constrained auto NTTPs should have gotten their constraint
   33256              :            moved to the template's associated constraints.  */
   33257            0 :         gcc_checking_assert (type != auto_node);
   33258              : 
   33259      8777874 :       if (processing_template_decl)
   33260              :         {
   33261           59 :           gcc_checking_assert (context == adc_variable_type
   33262              :                                || context == adc_return_type
   33263              :                                || context == adc_decomp_type);
   33264           59 :           gcc_checking_assert (!type_dependent_expression_p (init));
   33265              :           /* If the constraint is dependent, we need to wait until
   33266              :              instantiation time to resolve the placeholder.  */
   33267           59 :           if (placeholder_type_constraint_dependent_p (constr))
   33268              :             return type;
   33269              :         }
   33270              : 
   33271      8777850 :       if (context == adc_return_type
   33272      8777850 :           || context == adc_variable_type
   33273      8777850 :           || context == adc_decomp_type)
   33274          350 :         if (tree fn = current_function_decl)
   33275          246 :           if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
   33276              :             {
   33277          110 :               outer_targs = DECL_TEMPLATE_INFO (fn)
   33278          110 :                 ? DECL_TI_ARGS (fn) : NULL_TREE;
   33279          122 :               if (LAMBDA_FUNCTION_P (fn))
   33280              :                 {
   33281              :                   /* As in satisfy_declaration_constraints.  */
   33282           12 :                   tree regen_args = lambda_regenerating_args (fn);
   33283           12 :                   if (outer_targs)
   33284            0 :                     outer_targs = add_to_template_args (regen_args, outer_targs);
   33285              :                   else
   33286              :                     outer_targs = regen_args;
   33287              :                 }
   33288              :             }
   33289              : 
   33290      8777850 :       tree full_targs = outer_targs;
   33291      8777850 :       if (context == adc_unify && tmpl)
   33292            0 :         full_targs = add_outermost_template_args (tmpl, full_targs);
   33293      8777850 :       full_targs = add_to_template_args (full_targs, targs);
   33294              : 
   33295              :       /* HACK: Compensate for callers not always communicating all levels of
   33296              :          outer template arguments by filling in the outermost missing levels
   33297              :          with dummy levels before checking satisfaction.  We'll still crash
   33298              :          if the constraint depends on a template argument belonging to one of
   33299              :          these missing levels, but this hack otherwise allows us to handle a
   33300              :          large subset of possible constraints (including all non-dependent
   33301              :          constraints).  */
   33302      8777850 :       if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
   33303     17555937 :                                 - TMPL_ARGS_DEPTH (full_targs)))
   33304              :         {
   33305            0 :           tree dummy_levels = make_tree_vec (missing_levels);
   33306            0 :           for (int i = 0; i < missing_levels; ++i)
   33307            0 :             TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
   33308            0 :           full_targs = add_to_template_args (dummy_levels, full_targs);
   33309              :         }
   33310              : 
   33311      8777850 :       if (!constraints_satisfied_p (auto_node, full_targs))
   33312              :         {
   33313        23371 :           if (complain & tf_warning_or_error)
   33314              :             {
   33315          114 :               auto_diagnostic_group d;
   33316          114 :               switch (context)
   33317              :                 {
   33318            0 :                 case adc_unspecified:
   33319            0 :                 case adc_unify:
   33320            0 :                   error_at (loc, "placeholder constraints not satisfied");
   33321            0 :                   break;
   33322           74 :                 case adc_variable_type:
   33323           74 :                 case adc_decomp_type:
   33324           74 :                   error_at (loc, "deduced initializer does not satisfy "
   33325              :                             "placeholder constraints");
   33326           74 :                   break;
   33327           28 :                 case adc_return_type:
   33328           28 :                   error_at (loc, "deduced return type does not satisfy "
   33329              :                             "placeholder constraints");
   33330           28 :                   break;
   33331           12 :                 case adc_requirement:
   33332           12 :                   error_at (loc, "deduced expression type does not satisfy "
   33333              :                             "placeholder constraints");
   33334           12 :                   break;
   33335              :                 }
   33336          114 :               diagnose_constraints (loc, auto_node, full_targs);
   33337          114 :             }
   33338        23371 :           return error_mark_node;
   33339              :         }
   33340              :     }
   33341              : 
   33342     14581497 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 0)
   33343              :     {
   33344              :       /* Substitute this level-less auto via tsubst by temporarily
   33345              :          overriding its level to 1.  */
   33346       146293 :       TEMPLATE_TYPE_LEVEL (auto_node) = 1;
   33347       146293 :       type = tsubst (type, targs, complain, NULL_TREE);
   33348       146293 :       TEMPLATE_TYPE_LEVEL (auto_node) = 0;
   33349       146293 :       return type;
   33350              :     }
   33351              : 
   33352     14435204 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
   33353              :     /* The outer template arguments are already substituted into type
   33354              :        (but we still may have used them for constraint checking above).  */;
   33355      1096548 :   else if (context == adc_unify)
   33356       147031 :     targs = add_to_template_args (outer_targs, targs);
   33357       949517 :   else if (processing_template_decl)
   33358       949514 :     targs = add_to_template_args (current_template_args (), targs);
   33359     14435204 :   return tsubst (type, targs, complain, NULL_TREE);
   33360              : }
   33361              : 
   33362              : /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
   33363              :    result.  */
   33364              : 
   33365              : tree
   33366    199997618 : splice_late_return_type (tree type, tree late_return_type)
   33367              : {
   33368    199997618 :   if (late_return_type)
   33369              :     {
   33370      3770559 :       gcc_assert (is_auto (type) || seen_error ());
   33371              :       return late_return_type;
   33372              :     }
   33373              : 
   33374    196227059 :   if (tree auto_node = find_type_usage (type, is_auto))
   33375      5296386 :     if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
   33376              :       {
   33377              :         /* In an abbreviated function template we didn't know we were dealing
   33378              :            with a function template when we saw the auto return type, so rebuild
   33379              :            the return type using an auto with the correct level.  */
   33380          141 :         tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
   33381          141 :         tree auto_vec = make_tree_vec (1);
   33382          141 :         TREE_VEC_ELT (auto_vec, 0) = new_auto;
   33383          141 :         tree targs = add_outermost_template_args (current_template_args (),
   33384              :                                                   auto_vec);
   33385              :         /* Also rebuild the constraint info in terms of the new auto.  */
   33386          141 :         if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
   33387            8 :           PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
   33388            8 :             = build_tree_list (current_template_parms,
   33389            4 :                                tsubst_constraint (TREE_VALUE (ci), targs,
   33390              :                                                   tf_none, NULL_TREE));
   33391          141 :         TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
   33392          141 :         return tsubst (type, targs, tf_none, NULL_TREE);
   33393              :       }
   33394              :   return type;
   33395              : }
   33396              : 
   33397              : /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
   33398              :    'decltype(auto)' or a deduced class template.  */
   33399              : 
   33400              : bool
   33401  10530888151 : is_auto (const_tree type)
   33402              : {
   33403  10530888151 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   33404  10530888151 :       && (TYPE_IDENTIFIER (type) == auto_identifier
   33405   4643894268 :           || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
   33406    519503951 :     return true;
   33407              :   else
   33408              :     return false;
   33409              : }
   33410              : 
   33411              : /* for_each_template_parm callback for type_uses_auto.  */
   33412              : 
   33413              : int
   33414            0 : is_auto_r (tree tp, void */*data*/)
   33415              : {
   33416            0 :   return is_auto (tp);
   33417              : }
   33418              : 
   33419              : /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
   33420              :    a use of `auto'.  Returns NULL_TREE otherwise.  */
   33421              : 
   33422              : tree
   33423   3876833539 : type_uses_auto (tree type)
   33424              : {
   33425   3876833539 :   if (type == NULL_TREE)
   33426              :     return NULL_TREE;
   33427              : 
   33428              :   /* For parameter packs, check the contents of the pack.  */
   33429   3871152787 :   if (PACK_EXPANSION_P (type))
   33430      6508662 :     type = PACK_EXPANSION_PATTERN (type);
   33431              : 
   33432   3871152787 :   return find_type_usage (type, is_auto);
   33433              : }
   33434              : 
   33435              : /* Recursively walk over && expressions searching for EXPR. Return a reference
   33436              :    to that expression.  */
   33437              : 
   33438           67 : static tree *find_template_requirement (tree *t, tree key)
   33439              : {
   33440           79 :   if (*t == key)
   33441              :     return t;
   33442           24 :   if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
   33443              :     {
   33444           12 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
   33445              :         return p;
   33446           12 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
   33447              :         return p;
   33448              :     }
   33449              :   return 0;
   33450              : }
   33451              : 
   33452              : /* Convert the generic type parameters in PARM that match the types given in the
   33453              :    range [START_IDX, END_IDX) from the current_template_parms into generic type
   33454              :    packs.  */
   33455              : 
   33456              : tree
   33457        69861 : convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
   33458              : {
   33459        69861 :   tree current = current_template_parms;
   33460        69861 :   int depth = TMPL_PARMS_DEPTH (current);
   33461        69861 :   current = INNERMOST_TEMPLATE_PARMS (current);
   33462        69861 :   tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
   33463              : 
   33464       141678 :   for (int i = 0; i < start_idx; ++i)
   33465         3912 :     TREE_VEC_ELT (replacement, i)
   33466         1956 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   33467              : 
   33468       139734 :   for (int i = start_idx; i < end_idx; ++i)
   33469              :     {
   33470              :       /* Create a distinct parameter pack type from the current parm and add it
   33471              :          to the replacement args to tsubst below into the generic function
   33472              :          parameter.  */
   33473        69873 :       tree node = TREE_VEC_ELT (current, i);
   33474        69873 :       tree o = TREE_TYPE (TREE_VALUE (node));
   33475        69873 :       tree t = copy_type (o);
   33476        69873 :       TEMPLATE_TYPE_PARM_INDEX (t)
   33477        69873 :         = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
   33478              :                                       t, 0, 0, tf_none);
   33479        69873 :       TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
   33480        69873 :       TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
   33481        69873 :       TYPE_MAIN_VARIANT (t) = t;
   33482        69873 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
   33483        69873 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
   33484        69873 :       TREE_VEC_ELT (replacement, i) = t;
   33485              : 
   33486              :       /* Replace the current template parameter with new pack.  */
   33487        69873 :       TREE_VALUE (node) = TREE_CHAIN (t);
   33488              : 
   33489              :       /* Surgically adjust the associated constraint of adjusted parameter
   33490              :          and it's corresponding contribution to the current template
   33491              :          requirements.  */
   33492        69873 :       if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
   33493              :         {
   33494           55 :           gcc_assert (concept_check_p (constr));
   33495           55 :           TREE_VEC_ELT (TREE_OPERAND (constr, 1), 0) = t;
   33496              :           /* Use UNKNOWN_LOCATION so write_template_args can tell the
   33497              :              difference between this and a fold the user wrote.  */
   33498           55 :           location_t loc = UNKNOWN_LOCATION;
   33499           55 :           tree fold = finish_left_unary_fold_expr (loc, constr,
   33500              :                                                    TRUTH_ANDIF_EXPR);
   33501           55 :           TEMPLATE_PARM_CONSTRAINTS (node) = fold;
   33502              : 
   33503              :           /* If there was a constraint, we also need to replace that in
   33504              :              the template requirements, which we've already built.  */
   33505           55 :           tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
   33506           55 :           reqs = find_template_requirement (reqs, constr);
   33507           55 :           *reqs = fold;
   33508              :         }
   33509              :     }
   33510              : 
   33511        69861 :   for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
   33512            0 :     TREE_VEC_ELT (replacement, i)
   33513            0 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   33514              : 
   33515              :   /* If there are more levels then build up the replacement with the outer
   33516              :      template parms.  */
   33517        69861 :   if (depth > 1)
   33518        69265 :     replacement = add_to_template_args (template_parms_to_args
   33519        69265 :                                         (TREE_CHAIN (current_template_parms)),
   33520              :                                         replacement);
   33521              : 
   33522        69861 :   return tsubst (parm, replacement, tf_none, NULL_TREE);
   33523              : }
   33524              : 
   33525              : /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
   33526              :    0..N-1.  */
   33527              : 
   33528              : void
   33529        87188 : declare_integer_pack (void)
   33530              : {
   33531        87188 :   tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
   33532              :                                build_function_type_list (integer_type_node,
   33533              :                                                          integer_type_node,
   33534              :                                                          NULL_TREE),
   33535              :                                NULL_TREE, ECF_CONST);
   33536        87188 :   DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
   33537        87188 :   set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
   33538              :                               CP_BUILT_IN_INTEGER_PACK);
   33539        87188 : }
   33540              : 
   33541              : /* Walk the decl or type specialization table calling FN on each
   33542              :    entry.  */
   33543              : 
   33544              : void
   33545         5646 : walk_specializations (bool decls_p,
   33546              :                       void (*fn) (bool decls_p, spec_entry *entry, void *data),
   33547              :                       void *data)
   33548              : {
   33549         5646 :   spec_hash_table *table = decls_p ? decl_specializations
   33550              :     : type_specializations;
   33551         5646 :   spec_hash_table::iterator end (table->end ());
   33552      1367589 :   for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
   33553      1361943 :     fn (decls_p, *iter, data);
   33554         5646 : }
   33555              : 
   33556              : /* Lookup the specialization of *ELT, in the decl or type
   33557              :    specialization table.  Return the SPEC that's already there, or
   33558              :    NULL if nothing.  */
   33559              : 
   33560              : tree
   33561      2315159 : match_mergeable_specialization (bool decl_p, spec_entry *elt)
   33562              : {
   33563      2315159 :   hash_table<spec_hasher> *specializations
   33564              :     = decl_p ? decl_specializations : type_specializations;
   33565      2315159 :   auto *slot = specializations->find_slot (elt, NO_INSERT);
   33566              : 
   33567      2315159 :   if (slot)
   33568      1754415 :     return (*slot)->spec;
   33569              : 
   33570              :   return NULL_TREE;
   33571              : }
   33572              : 
   33573              : /* Return flags encoding whether SPEC is on the instantiation and/or
   33574              :    specialization lists of TMPL.  */
   33575              : 
   33576              : unsigned
   33577       533475 : get_mergeable_specialization_flags (bool decl_p, tree tmpl, tree decl)
   33578              : {
   33579       533475 :   unsigned flags = 0;
   33580              : 
   33581       533475 :   tree spec = decl_p ? decl : TREE_TYPE (decl);
   33582       533475 :   for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
   33583     10365222 :        inst; inst = TREE_CHAIN (inst))
   33584     10360704 :     if (TREE_VALUE (inst) == spec)
   33585              :       {
   33586              :         flags |= 1;
   33587              :         break;
   33588              :       }
   33589              : 
   33590      1066950 :   if (CLASS_TYPE_P (TREE_TYPE (decl))
   33591       223358 :       && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
   33592       751188 :       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   33593              :     /* Only need to search if DECL is a partial specialization.  */
   33594        15051 :     for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   33595        42358 :          part; part = TREE_CHAIN (part))
   33596        27307 :       if (TREE_VALUE (part) == decl)
   33597              :         {
   33598            0 :           flags |= 2;
   33599            0 :           break;
   33600              :         }
   33601              : 
   33602       533475 :   return flags;
   33603              : }
   33604              : 
   33605              : /* Add a new specialization described by SPEC.  DECL is the
   33606              :    maybe-template decl and FLAGS is as returned from
   33607              :    get_mergeable_specialization_flags.  */
   33608              : 
   33609              : void
   33610       235615 : add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
   33611              :                               unsigned flags)
   33612              : {
   33613       235615 :   if (decl_p)
   33614              :     {
   33615       168698 :       auto *slot = decl_specializations->find_slot (elt, INSERT);
   33616              : 
   33617       168698 :       gcc_checking_assert (!*slot);
   33618       168698 :       auto entry = ggc_alloc<spec_entry> ();
   33619       168698 :       *entry = *elt;
   33620       168698 :       *slot = entry;
   33621              :     }
   33622              :   else
   33623              :     {
   33624        66917 :       auto *slot = type_specializations->find_slot (elt, INSERT);
   33625              : 
   33626              :       /* We don't distinguish different constrained partial type
   33627              :          specializations, so there could be duplicates.  In that case we
   33628              :          must propagate TYPE_CANONICAL so that they are treated as the
   33629              :          same type.  Everything else must be new.   */
   33630        66917 :       if (*slot)
   33631              :         {
   33632          188 :           gcc_checking_assert (flags & 2);
   33633          188 :           TYPE_CANONICAL (elt->spec) = TYPE_CANONICAL ((*slot)->spec);
   33634              :         }
   33635              :       else
   33636              :         {
   33637        66729 :           auto entry = ggc_alloc<spec_entry> ();
   33638        66729 :           *entry = *elt;
   33639        66729 :           *slot = entry;
   33640              :         }
   33641              :     }
   33642              : 
   33643       235615 :   if (flags & 1)
   33644       228806 :     DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
   33645       457612 :       = tree_cons (elt->args, elt->spec,
   33646       228806 :                    DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
   33647              : 
   33648       235615 :   if (flags & 2)
   33649              :     {
   33650              :       /* A partial specialization.  */
   33651         4813 :       tree cons = tree_cons (elt->args, decl,
   33652         4813 :                              DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
   33653         4813 :       TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
   33654         4813 :       DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
   33655         4813 :       set_defining_module_for_partial_spec (STRIP_TEMPLATE (decl));
   33656              :     }
   33657       235615 : }
   33658              : 
   33659              : struct expansion_stmt_bc
   33660              : {
   33661              :   tree break_label;
   33662              :   tree continue_label;
   33663              :   hash_set<tree> *pset;
   33664              :   location_t loc;
   33665              :   bool in_switch;
   33666              : };
   33667              : 
   33668              : /* Helper function for finish_expansion_stmt.  Find BREAK_STMT (not
   33669              :    nested inside of other WHILE_STMT, FOR_STMT, DO_STMT, TEMPLATE_FOR_STMT
   33670              :    or SWITCH_STMT) or CONTINUE_STMT (not nested inside those except
   33671              :    perhaps SWITCH_STMT) and replace them with GOTO_EXPR to lazily created
   33672              :    label.  */
   33673              : 
   33674              : static tree
   33675       107723 : expansion_stmt_find_bc_r (tree *tp, int *walk_subtrees, void *data)
   33676              : {
   33677       107723 :   tree t = *tp;
   33678       107723 :   expansion_stmt_bc *bc_data = (expansion_stmt_bc *) data;
   33679       107723 :   switch (TREE_CODE (t))
   33680              :     {
   33681            0 :     case WHILE_STMT:
   33682            0 :       *walk_subtrees = 0;
   33683            0 :       for (int i = 0; i < TREE_CODE_LENGTH (WHILE_STMT); ++i)
   33684            0 :         if (&TREE_OPERAND (t, i) != &WHILE_BODY (t))
   33685            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33686              :                         data, bc_data->pset);
   33687              :       break;
   33688            0 :     case FOR_STMT:
   33689            0 :       *walk_subtrees = 0;
   33690            0 :       for (int i = 0; i < TREE_CODE_LENGTH (FOR_STMT); ++i)
   33691            0 :         if (&TREE_OPERAND (t, i) != &FOR_BODY (t))
   33692            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33693              :                         data, bc_data->pset);
   33694              :       break;
   33695            0 :     case DO_STMT:
   33696            0 :       *walk_subtrees = 0;
   33697            0 :       for (int i = 0; i < TREE_CODE_LENGTH (DO_STMT); ++i)
   33698            0 :         if (&TREE_OPERAND (t, i) != &DO_BODY (t))
   33699            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33700              :                         data, bc_data->pset);
   33701              :       break;
   33702            0 :     case TEMPLATE_FOR_STMT:
   33703            0 :       *walk_subtrees = 0;
   33704            0 :       for (int i = 0; i < TREE_CODE_LENGTH (TEMPLATE_FOR_STMT); ++i)
   33705            0 :         if (&TREE_OPERAND (t, i) != &TEMPLATE_FOR_BODY (t))
   33706            0 :           cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33707              :                         data, bc_data->pset);
   33708              :       break;
   33709           12 :     case SWITCH_STMT:
   33710           12 :       if (!bc_data->in_switch)
   33711              :         {
   33712           12 :           *walk_subtrees = 0;
   33713           72 :           for (int i = 0; i < TREE_CODE_LENGTH (SWITCH_STMT); ++i)
   33714           60 :             if (&TREE_OPERAND (t, i) != &SWITCH_STMT_BODY (t))
   33715           48 :               cp_walk_tree (&TREE_OPERAND (t, i), expansion_stmt_find_bc_r,
   33716              :                             data, bc_data->pset);
   33717           12 :           bc_data->in_switch = true;
   33718           12 :           cp_walk_tree (&SWITCH_STMT_BODY (t), expansion_stmt_find_bc_r,
   33719              :                         data, bc_data->pset);
   33720           12 :           bc_data->in_switch = false;
   33721              :         }
   33722              :       break;
   33723          249 :     case BREAK_STMT:
   33724          249 :       if (!bc_data->in_switch)
   33725              :         {
   33726          201 :           if (!bc_data->break_label)
   33727              :             {
   33728           45 :               bc_data->break_label = create_artificial_label (bc_data->loc);
   33729           45 :               TREE_USED (bc_data->break_label) = 1;
   33730           45 :               LABEL_DECL_BREAK (bc_data->break_label) = true;
   33731              :             }
   33732          201 :           *tp = build1_loc (EXPR_LOCATION (t), GOTO_EXPR, void_type_node,
   33733              :                             bc_data->break_label);
   33734              :         }
   33735              :       break;
   33736          157 :     case CONTINUE_STMT:
   33737          157 :       if (!bc_data->continue_label)
   33738              :         {
   33739          157 :           bc_data->continue_label = create_artificial_label (bc_data->loc);
   33740          157 :           TREE_USED (bc_data->continue_label) = 1;
   33741          157 :           LABEL_DECL_CONTINUE (bc_data->continue_label) = true;
   33742              :         }
   33743          157 :       *tp = build1_loc (EXPR_LOCATION (t), GOTO_EXPR, void_type_node,
   33744              :                         bc_data->continue_label);
   33745          157 :       break;
   33746       107305 :     default:
   33747       107305 :       if (TYPE_P (t))
   33748          506 :         *walk_subtrees = 0;
   33749              :       break;
   33750              :     }
   33751       107723 :   return NULL_TREE;
   33752              : }
   33753              : 
   33754              : /* Finish an expansion-statement.  */
   33755              : 
   33756              : void
   33757         1777 : finish_expansion_stmt (tree expansion_stmt, tree args,
   33758              :                        tsubst_flags_t complain, tree in_decl)
   33759              : {
   33760         1777 :   tree expansion_init = TEMPLATE_FOR_EXPR (expansion_stmt);
   33761         1777 :   if (error_operand_p (expansion_init))
   33762           48 :     return;
   33763              : 
   33764         1777 :   enum expansion_stmt_kind {
   33765              :     esk_none,
   33766              :     esk_iterating,
   33767              :     esk_destructuring,
   33768              :     esk_enumerating
   33769         1777 :   } kind = esk_none;
   33770              : 
   33771         1777 :   unsigned HOST_WIDE_INT n = 0;
   33772         1777 :   tree range_decl = TEMPLATE_FOR_DECL (expansion_stmt);
   33773         1777 :   bool is_decomp = false;
   33774         1777 :   if (TREE_CODE (range_decl) == TREE_VEC)
   33775              :     {
   33776          296 :       is_decomp = true;
   33777          296 :       range_decl = TREE_VEC_ELT (range_decl, 0);
   33778              :     }
   33779         1777 :   if (error_operand_p (range_decl))
   33780              :     return;
   33781              : 
   33782         1741 :   location_t loc = DECL_SOURCE_LOCATION (range_decl);
   33783         1741 :   tree begin = NULL_TREE, begin_minus_begin_type = NULL_TREE;
   33784         1741 :   auto_vec<tree, 8> destruct_decls;
   33785         1741 :   bool is_lvalue = false;
   33786         1741 :   if (BRACE_ENCLOSED_INITIALIZER_P (expansion_init))
   33787              :     {
   33788              :       /* Enumerating expansion statements.  */
   33789          813 :       kind = esk_enumerating;
   33790          813 :       n = CONSTRUCTOR_NELTS (expansion_init);
   33791              :     }
   33792         1856 :   else if (TYPE_REF_P (TREE_TYPE (expansion_init))
   33793          928 :            ? TREE_CODE (TREE_TYPE (TREE_TYPE (expansion_init))) != ARRAY_TYPE
   33794          928 :            : TREE_CODE (TREE_TYPE (expansion_init)) != ARRAY_TYPE)
   33795              :     {
   33796          237 :       tree range_temp, begin_expr, end_expr, iter_type;
   33797          237 :       range_temp = convert_from_reference (build_range_temp (expansion_init));
   33798          237 :       iter_type = cp_perform_range_for_lookup (range_temp, &begin_expr,
   33799              :                                                &end_expr, tf_none);
   33800          237 :       if (iter_type != error_mark_node
   33801           95 :           || (begin_expr != error_mark_node && end_expr != error_mark_node))
   33802          142 :         kind = esk_iterating;
   33803              :     }
   33804          908 :   if (kind == esk_iterating)
   33805              :     {
   33806              :       /* Iterating expansion statements.  */
   33807          142 :       tree exprs[2];
   33808          142 :       begin = cp_build_range_for_decls (loc, expansion_init, exprs, range_decl);
   33809          142 :       if (!error_operand_p (begin)
   33810          138 :           && !error_operand_p (exprs[0])
   33811          280 :           && !error_operand_p (exprs[1]))
   33812              :         {
   33813              :           /* In the standard this is all evaluated inside of a consteval
   33814              :              lambda.  So, force in_immediate_context () around this.  */
   33815          138 :           in_consteval_if_p_temp_override icip;
   33816          138 :           in_consteval_if_p = true;
   33817          138 :           tree b = exprs[0], e = exprs[1];
   33818              :           /* The begin-expr and end-expr expressions will be usually wrapped
   33819              :              in TARGET_EXPR if they return a class iterator.  The b
   33820              :              and e artificial variables need to have cv-unqualified type
   33821              :              so that e.g. b can be incremented, so unwrap the TARGET_EXPRs
   33822              :              and force TARGET_EXPR with the cv-unqualified type which is
   33823              :              a hack replacement for a VAR_DECL in a lambda.  */
   33824          138 :           tree btype = cv_unqualified (TREE_TYPE (b));
   33825          138 :           tree etype = cv_unqualified (TREE_TYPE (e));
   33826          138 :           if (TREE_CODE (b) == TARGET_EXPR)
   33827           74 :             b = TARGET_EXPR_INITIAL (b);
   33828          138 :           if (TREE_CODE (e) == TARGET_EXPR)
   33829           74 :             e = TARGET_EXPR_INITIAL (e);
   33830          138 :           b = force_target_expr (btype, b, tf_warning_or_error);
   33831          138 :           e = force_target_expr (etype, e, tf_warning_or_error);
   33832          138 :           tree w = build_stmt (loc, WHILE_STMT, NULL_TREE, NULL_TREE,
   33833              :                                NULL_TREE, NULL_TREE, NULL_TREE);
   33834          138 :           tree r = get_target_expr (build_zero_cst (ptrdiff_type_node));
   33835          138 :           tree binc = build_x_unary_op (loc, PREINCREMENT_EXPR,
   33836          138 :                                         TARGET_EXPR_SLOT (b), NULL_TREE,
   33837              :                                         tf_warning_or_error);
   33838          276 :           tree rinc = build2 (PREINCREMENT_EXPR, ptrdiff_type_node,
   33839          138 :                               TARGET_EXPR_SLOT (r),
   33840              :                               build_int_cst (ptrdiff_type_node, 1));
   33841          138 :           WHILE_BODY (w) = build_compound_expr (loc, binc, rinc);
   33842          138 :           WHILE_COND (w) = build_x_binary_op (loc, NE_EXPR, b, ERROR_MARK,
   33843              :                                               e, ERROR_MARK, NULL_TREE, NULL,
   33844              :                                               tf_warning_or_error);
   33845          138 :           {
   33846          138 :             warning_sentinel wur (warn_unused_result);
   33847          138 :             e = build_compound_expr (loc, b, e);
   33848          138 :             e = build_compound_expr (loc, r, e);
   33849          138 :             e = build_compound_expr (loc, e, w);
   33850          138 :             e = build_compound_expr (loc, e, TARGET_EXPR_SLOT (r));
   33851          138 :           }
   33852          138 :           e = fold_build_cleanup_point_expr (TREE_TYPE (e), e);
   33853          138 :           e = cxx_constant_value (e);
   33854          138 :           if (tree_fits_uhwi_p (e))
   33855          113 :             n = tree_to_uhwi (e);
   33856          138 :         }
   33857              :     }
   33858         1599 :   else if (kind == esk_none)
   33859              :     {
   33860          786 :       kind = esk_destructuring;
   33861          786 :       HOST_WIDE_INT sz = cp_decomp_size (loc, TREE_TYPE (expansion_init),
   33862              :                                          tf_warning_or_error);
   33863          786 :       if (sz < 0)
   33864           12 :         return;
   33865          774 :       n = sz;
   33866          774 :       is_lvalue = lvalue_p (expansion_init);
   33867          774 :       tree auto_node = make_auto ();
   33868          774 :       tree decomp_type = cp_build_reference_type (auto_node, true);
   33869          774 :       decomp_type = do_auto_deduction (decomp_type, expansion_init, auto_node);
   33870          774 :       tree decl = build_decl (loc, VAR_DECL, NULL_TREE, decomp_type);
   33871          774 :       TREE_USED (decl) = 1;
   33872          774 :       DECL_ARTIFICIAL (decl) = 1;
   33873          774 :       DECL_DECLARED_CONSTEXPR_P (decl)
   33874          774 :         = DECL_DECLARED_CONSTEXPR_P (range_decl);
   33875          774 :       if (DECL_DECLARED_CONSTEXPR_P (decl))
   33876          482 :         TREE_READONLY (decl) = 1;
   33877          774 :       if (n)
   33878          530 :         fit_decomposition_lang_decl (decl, NULL_TREE);
   33879          774 :       pushdecl (decl);
   33880          774 :       cp_decomp this_decomp;
   33881          774 :       this_decomp.count = n;
   33882          774 :       destruct_decls.safe_grow (n, true);
   33883         2993 :       for (unsigned HOST_WIDE_INT i = 0; i < n; ++i)
   33884              :         {
   33885         1445 :           tree this_type = make_auto ();
   33886         1445 :           if (DECL_DECLARED_CONSTEXPR_P (decl))
   33887          560 :             this_type = cp_build_qualified_type (this_type, TYPE_QUAL_CONST);
   33888         1445 :           tree this_decl = build_decl (loc, VAR_DECL, NULL_TREE, this_type);
   33889         1445 :           TREE_USED (this_decl) = 1;
   33890         1445 :           DECL_ARTIFICIAL (this_decl) = 1;
   33891         1445 :           DECL_DECLARED_CONSTEXPR_P (this_decl)
   33892         1445 :             = DECL_DECLARED_CONSTEXPR_P (decl);
   33893         1445 :           if (DECL_DECLARED_CONSTEXPR_P (decl))
   33894          560 :             TREE_READONLY (this_decl) = 1;
   33895         1445 :           pushdecl (this_decl);
   33896         1445 :           this_decomp.decl = this_decl;
   33897         1445 :           destruct_decls[i] = this_decl;
   33898              :         }
   33899          774 :       DECL_NAME (decl) = for_range__identifier;
   33900         1018 :       cp_finish_decl (decl, expansion_init,
   33901              :                       /*is_constant_init*/false, NULL_TREE,
   33902              :                       LOOKUP_ONLYCONVERTING, n ? &this_decomp : NULL);
   33903          774 :       DECL_NAME (decl) = NULL_TREE;
   33904              :     }
   33905              : 
   33906         1729 :   tree stmt_list = push_stmt_list ();
   33907         1729 :   expansion_stmt_bc bc_data = { NULL_TREE, NULL_TREE, NULL, loc, false };
   33908              : 
   33909         5054 :   for (unsigned HOST_WIDE_INT i = 0; i < n; ++i)
   33910              :     {
   33911         3325 :       tree scope = do_pushlevel (sk_block);
   33912         9975 :       bool revert_outer
   33913         3325 :         = (current_binding_level->level_chain
   33914         3325 :            && current_binding_level->level_chain->kind == sk_template_for);
   33915              :       /* Don't diagnose redeclaration of for-range-declaration decls.
   33916              :          The sk_template_for block is reused for the originally parsed
   33917              :          source as well as the lowered one.  In the original one
   33918              :          redeclaration of the for-range-declaration decls in the substatement
   33919              :          should be diagnosed (i.e. declarations of the same name in sk_block
   33920              :          of the body vs. declarations in sk_template_for block).  In the
   33921              :          lowered case, the sk_block added by do_pushlevel (sk_block) above
   33922              :          will be block in the lowering of each Si.  Those blocks do redeclare
   33923              :          for-range-declaration, so temporarily change sk_template_for
   33924              :          kind to sk_block to avoid it being diagnosed as invalid.  */
   33925         3325 :       if (revert_outer)
   33926         3325 :         current_binding_level->level_chain->kind = sk_block;
   33927         3325 :       tree type = TREE_TYPE (range_decl);
   33928         3325 :       if (args)
   33929         2079 :         type = tsubst (type, args, complain | tf_tst_ok, in_decl);
   33930         3325 :       if (DECL_DECLARED_CONSTEXPR_P (range_decl) && !TYPE_REF_P (type))
   33931         1257 :         type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
   33932         3325 :       tree decl = build_decl (loc, VAR_DECL, DECL_NAME (range_decl), type);
   33933         3325 :       DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (range_decl);
   33934         3325 :       TREE_USED (decl) |= TREE_USED (range_decl);
   33935         3325 :       DECL_READ_P (decl) |= DECL_READ_P (range_decl);
   33936         3325 :       if (args)
   33937         2079 :         apply_late_template_attributes (&decl, DECL_ATTRIBUTES (decl),
   33938              :                                         /*flags=*/0, args, complain,
   33939              :                                         in_decl);
   33940              : 
   33941         3325 :       DECL_DECLARED_CONSTEXPR_P (decl)
   33942         3325 :         = DECL_DECLARED_CONSTEXPR_P (range_decl);
   33943         3325 :       if (DECL_DECLARED_CONSTEXPR_P (decl))
   33944         1257 :         TREE_READONLY (decl) = 1;
   33945         3325 :       pushdecl (decl);
   33946         3325 :       tree init = NULL_TREE;
   33947         3325 :       switch (kind)
   33948              :         {
   33949         1413 :         case esk_enumerating:
   33950         1413 :           init = CONSTRUCTOR_ELT (expansion_init, i)->value;
   33951         1413 :           break;
   33952          467 :         case esk_iterating:
   33953          467 :           tree iter_init, auto_node, iter_type, iter, it;
   33954          467 :           it = build_int_cst (ptrdiff_type_node, i);
   33955          467 :           if (begin_minus_begin_type == NULL_TREE)
   33956              :             {
   33957          105 :               ++cp_unevaluated_operand;
   33958          105 :               ++c_inhibit_evaluation_warnings;
   33959          105 :               tree begin_minus_begin
   33960          210 :                 = build_x_binary_op (loc, MINUS_EXPR, begin, TREE_CODE (begin),
   33961          105 :                                      begin, TREE_CODE (begin), NULL_TREE, NULL,
   33962              :                                      tf_warning_or_error);
   33963          105 :               --cp_unevaluated_operand;
   33964          105 :               --c_inhibit_evaluation_warnings;
   33965          105 :               begin_minus_begin_type
   33966          105 :                 = finish_decltype_type (begin_minus_begin, false,
   33967              :                                         tf_warning_or_error);
   33968              :             }
   33969          467 :           it = build_constructor_single (init_list_type_node, NULL_TREE, it);
   33970          467 :           CONSTRUCTOR_IS_DIRECT_INIT (it) = true;
   33971          467 :           it = finish_compound_literal (begin_minus_begin_type, it,
   33972              :                                         tf_warning_or_error, fcl_functional);
   33973          467 :           iter_init
   33974          467 :             = build_x_binary_op (loc, PLUS_EXPR, begin, ERROR_MARK, it,
   33975              :                                  ERROR_MARK, NULL_TREE, NULL,
   33976              :                                  tf_warning_or_error);
   33977          467 :           auto_node = make_auto ();
   33978          467 :           iter_type = do_auto_deduction (auto_node, iter_init, auto_node);
   33979          467 :           if (DECL_DECLARED_CONSTEXPR_P (range_decl)
   33980          467 :               && !TYPE_REF_P (iter_type))
   33981          212 :             iter_type = cp_build_qualified_type (iter_type, TYPE_QUAL_CONST);
   33982          467 :           iter = build_decl (loc, VAR_DECL, get_identifier ("__for_iter "),
   33983              :                              iter_type);
   33984          467 :           TREE_USED (iter) = 1;
   33985          467 :           DECL_ARTIFICIAL (iter) = 1;
   33986          467 :           if (DECL_DECLARED_CONSTEXPR_P (range_decl))
   33987              :             {
   33988          212 :               TREE_STATIC (iter) = 1;
   33989          212 :               DECL_DECLARED_CONSTEXPR_P (iter) = 1;
   33990          212 :               TREE_READONLY (iter) = 1;
   33991          212 :               DECL_IGNORED_P (iter) = 1;
   33992              :             }
   33993          467 :           pushdecl (iter);
   33994          467 :           cp_finish_decl (iter, iter_init, /*is_constant_init*/false,
   33995              :                           NULL_TREE, LOOKUP_ONLYCONVERTING);
   33996          467 :           init = build_x_indirect_ref (loc, iter, RO_UNARY_STAR, NULL_TREE,
   33997              :                                        tf_warning_or_error);
   33998          467 :           break;
   33999         1445 :         case esk_destructuring:
   34000         1445 :           init = convert_from_reference (destruct_decls[i]);
   34001         1445 :           if (!is_lvalue)
   34002              :             {
   34003           85 :               tree ctype;
   34004           85 :               if (DECL_HAS_VALUE_EXPR_P (destruct_decls[i]))
   34005           42 :                 ctype = unlowered_expr_type (destruct_decls[i]);
   34006              :               else
   34007           43 :                 ctype = lookup_decomp_type (destruct_decls[i]);
   34008           85 :               ctype = cp_build_reference_type (ctype, /*rval=*/true);
   34009           85 :               init = build_static_cast (loc, ctype, init,
   34010              :                                         tf_warning_or_error);
   34011           85 :               init = convert_from_reference (init);
   34012              :             }
   34013              :           break;
   34014              :         default:
   34015              :           gcc_unreachable ();
   34016              :         }
   34017         3325 :       cp_decomp this_decomp = {};
   34018         3325 :       if (is_decomp)
   34019              :         {
   34020          608 :           fit_decomposition_lang_decl (decl, NULL_TREE);
   34021          608 :           tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   34022          608 :           this_decomp.count = TREE_VEC_LENGTH (v) - 1;
   34023         1663 :           for (unsigned i = 0; i < this_decomp.count; ++i)
   34024              :             {
   34025         1055 :               tree this_type = make_auto ();
   34026         1055 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   34027            0 :                 this_type = cp_build_qualified_type (this_type,
   34028              :                                                      TYPE_QUAL_CONST);
   34029         1055 :               tree this_decl
   34030         1055 :                 = build_decl (loc, VAR_DECL,
   34031         1055 :                               DECL_NAME (TREE_VEC_ELT (v, i + 1)),
   34032         1055 :                               this_type);
   34033         1055 :               TREE_USED (this_decl) = 1;
   34034         1055 :               DECL_ARTIFICIAL (this_decl) = 1;
   34035         2110 :               DECL_ATTRIBUTES (this_decl)
   34036         1055 :                 = DECL_ATTRIBUTES (TREE_VEC_ELT (v, i + 1));
   34037         1055 :               TREE_USED (this_decl) |= TREE_USED (TREE_VEC_ELT (v, i + 1));
   34038         1055 :               DECL_READ_P (this_decl) |= DECL_READ_P (TREE_VEC_ELT (v, i + 1));
   34039         1055 :               if (DECL_PACK_P (TREE_VEC_ELT (v, i + 1)))
   34040              :                 {
   34041           41 :                   tree dtype = cxx_make_type (DECLTYPE_TYPE);
   34042           41 :                   DECLTYPE_TYPE_EXPR (dtype) = this_decl;
   34043           41 :                   DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (dtype) = 1;
   34044           41 :                   SET_TYPE_STRUCTURAL_EQUALITY (dtype);
   34045           41 :                   tree type = cxx_make_type (TYPE_PACK_EXPANSION);
   34046           41 :                   PACK_EXPANSION_PATTERN (type) = dtype;
   34047           41 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
   34048           82 :                   PACK_EXPANSION_PARAMETER_PACKS (type) = this_decl;
   34049           41 :                   TREE_TYPE (this_decl) = type;
   34050              :                 }
   34051         1055 :               if (args)
   34052          840 :                 apply_late_template_attributes (&this_decl,
   34053          840 :                                                 DECL_ATTRIBUTES (this_decl),
   34054              :                                                 /*flags=*/0, args,
   34055              :                                                 complain, in_decl);
   34056         1055 :               DECL_DECLARED_CONSTEXPR_P (this_decl)
   34057         1055 :                 = DECL_DECLARED_CONSTEXPR_P (decl);
   34058         1055 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   34059            0 :                 TREE_READONLY (this_decl) = 1;
   34060         1055 :               pushdecl (this_decl);
   34061         1055 :               this_decomp.decl = this_decl;
   34062              :             }
   34063              :         }
   34064         3325 :       cp_finish_decl (decl, init, false, NULL_TREE,
   34065              :                       LOOKUP_ONLYCONVERTING, is_decomp ? &this_decomp : NULL);
   34066         3325 :       if (revert_outer)
   34067         3325 :         current_binding_level->level_chain->kind = sk_template_for;
   34068         3325 :       tree targs = args;
   34069         3325 :       if (args == NULL_TREE)
   34070              :         {
   34071         1246 :           targs = make_tree_vec (1);
   34072         1246 :           TREE_VEC_ELT (targs, 0) = build_int_cst (ptrdiff_type_node, i + 1);
   34073              :         }
   34074         1246 :       if (args != NULL_TREE
   34075         1246 :           || push_tinst_level_loc (expansion_stmt, targs, loc))
   34076              :         {
   34077         3325 :           local_specialization_stack lss (lss_copy);
   34078         3325 :           register_local_specialization (decl, range_decl);
   34079         3325 :           if (is_decomp)
   34080              :             {
   34081          608 :               tree d = this_decomp.decl;
   34082          608 :               unsigned int cnt = this_decomp.count;
   34083          608 :               tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   34084         1663 :               for (unsigned int i = 0; i < cnt; ++i, d = DECL_CHAIN (d))
   34085         1055 :                 register_local_specialization (d, TREE_VEC_ELT (v, cnt - i));
   34086              :             }
   34087         6650 :           tsubst_stmt (TEMPLATE_FOR_BODY (expansion_stmt),
   34088              :                        targs, complain, in_decl ? in_decl : range_decl);
   34089         3325 :           if (args == NULL_TREE)
   34090         1246 :             pop_tinst_level ();
   34091         3325 :         }
   34092         3325 :       tree stmt = do_poplevel (scope);
   34093         3325 :       if (stmt)
   34094              :         {
   34095         3325 :           add_stmt (stmt);
   34096         3325 :           hash_set<tree> pset;
   34097         3325 :           bc_data.continue_label = NULL_TREE;
   34098         3325 :           bc_data.pset = &pset;
   34099         3325 :           cp_walk_tree (&stmt, expansion_stmt_find_bc_r, &bc_data, &pset);
   34100         3325 :           if (bc_data.continue_label)
   34101          157 :             add_stmt (build1 (LABEL_EXPR, void_type_node,
   34102              :                               bc_data.continue_label));
   34103         3325 :         }
   34104              :     }
   34105         1729 :   if (bc_data.break_label)
   34106              :     {
   34107              :       /* If break; is seen, wrap all the expansion stmt bodies in
   34108              :          a single artificial do ... while (0); statement, so that
   34109              :          constant evaluation handles break; correctly.  */
   34110           45 :       tree do_stmt
   34111           45 :         = build_stmt (loc, DO_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
   34112           45 :       DO_COND (do_stmt) = boolean_false_node;
   34113           45 :       DO_BODY (do_stmt) = pop_stmt_list (stmt_list);
   34114           45 :       add_stmt (do_stmt);
   34115           45 :       add_stmt (build1 (LABEL_EXPR, void_type_node, bc_data.break_label));
   34116              :     }
   34117              :   else
   34118         1684 :     add_stmt (pop_stmt_list (stmt_list));
   34119         1729 :   if (args == NULL_TREE)
   34120              :     {
   34121          483 :       TREE_TYPE (range_decl) = error_mark_node;
   34122          483 :       if (DECL_HAS_VALUE_EXPR_P (range_decl))
   34123              :         {
   34124            0 :           SET_DECL_VALUE_EXPR (range_decl, NULL_TREE);
   34125            0 :           DECL_HAS_VALUE_EXPR_P (range_decl) = 0;
   34126              :         }
   34127          483 :       if (is_decomp)
   34128              :         {
   34129           17 :           tree v = TEMPLATE_FOR_DECL (expansion_stmt);
   34130           61 :           for (int i = 1; i < TREE_VEC_LENGTH (v); ++i)
   34131              :             {
   34132           44 :               tree d = TREE_VEC_ELT (v, i);
   34133           44 :               TREE_TYPE (d) = error_mark_node;
   34134           44 :               if (DECL_HAS_VALUE_EXPR_P (d))
   34135              :                 {
   34136           44 :                   SET_DECL_VALUE_EXPR (d, NULL_TREE);
   34137           44 :                   DECL_HAS_VALUE_EXPR_P (d) = 0;
   34138              :                 }
   34139              :             }
   34140              :         }
   34141              :     }
   34142         1741 : }
   34143              : 
   34144              : /* Perform the appropriate conversion of the argument of
   34145              :    std::meta::reflect_constant.   EXPR is the argument, TYPE is its type.
   34146              :    Mainly, the point is to check that the type is valid in this context
   34147              :    and maybe replace the argument with a reference to the corresponding
   34148              :    template parameter object.  */
   34149              : 
   34150              : tree
   34151         4250 : convert_reflect_constant_arg (tree type, tree expr)
   34152              : {
   34153         4250 :   if (invalid_nontype_parm_type_p (type, tf_none))
   34154            0 :     return error_mark_node;
   34155              : 
   34156         4250 :   expr = convert_nontype_argument (type, expr, tf_none);
   34157         4250 :   if (!expr)
   34158           42 :     return error_mark_node;
   34159              :   return expr;
   34160              : }
   34161              : 
   34162              : /* Set up the hash tables for template instantiations.  */
   34163              : 
   34164              : void
   34165       101500 : init_template_processing (void)
   34166              : {
   34167       101500 :   decl_specializations = hash_table<spec_hasher>::create_ggc (37);
   34168       101500 :   type_specializations = hash_table<spec_hasher>::create_ggc (37);
   34169              : 
   34170       101500 :   if (cxx_dialect >= cxx11)
   34171        87188 :     declare_integer_pack ();
   34172       101500 : }
   34173              : 
   34174              : /* Print stats about the template hash tables for -fstats.  */
   34175              : 
   34176              : void
   34177            0 : print_template_statistics (void)
   34178              : {
   34179            0 :   fprintf (stderr, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   34180              :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   34181            0 :            (fmt_size_t) decl_specializations->size (),
   34182            0 :            (fmt_size_t) decl_specializations->elements (),
   34183              :            decl_specializations->collisions ());
   34184            0 :   fprintf (stderr, "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   34185              :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   34186            0 :            (fmt_size_t) type_specializations->size (),
   34187            0 :            (fmt_size_t) type_specializations->elements (),
   34188              :            type_specializations->collisions ());
   34189            0 : }
   34190              : 
   34191              : #if CHECKING_P
   34192              : 
   34193              : namespace selftest {
   34194              : 
   34195              : /* Verify that type_dependent_expression_p () works correctly, even
   34196              :    in the presence of location wrapper nodes.  */
   34197              : 
   34198              : static void
   34199            1 : test_type_dependent_expression_p ()
   34200              : {
   34201            1 :   location_t loc = BUILTINS_LOCATION;
   34202              : 
   34203            1 :   tree name = get_identifier ("foo");
   34204              : 
   34205              :   /* If no templates are involved, nothing is type-dependent.  */
   34206            1 :   gcc_assert (!processing_template_decl);
   34207            1 :   ASSERT_FALSE (type_dependent_expression_p (name));
   34208              : 
   34209            1 :   ++processing_template_decl;
   34210              : 
   34211              :   /* Within a template, an unresolved name is always type-dependent.  */
   34212            1 :   ASSERT_TRUE (type_dependent_expression_p (name));
   34213              : 
   34214              :   /* Ensure it copes with NULL_TREE and errors.  */
   34215            1 :   ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
   34216            1 :   ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
   34217              : 
   34218              :   /* A USING_DECL in a template should be type-dependent, even if wrapped
   34219              :      with a location wrapper (PR c++/83799).  */
   34220            1 :   tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
   34221            1 :   TREE_TYPE (using_decl) = integer_type_node;
   34222            1 :   ASSERT_TRUE (type_dependent_expression_p (using_decl));
   34223            1 :   tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
   34224            1 :   ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
   34225            1 :   ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
   34226              : 
   34227            1 :   --processing_template_decl;
   34228            1 : }
   34229              : 
   34230              : /* Run all of the selftests within this file.  */
   34231              : 
   34232              : void
   34233            1 : cp_pt_cc_tests ()
   34234              : {
   34235            1 :   test_type_dependent_expression_p ();
   34236            1 : }
   34237              : 
   34238              : } // namespace selftest
   34239              : 
   34240              : #endif /* #if CHECKING_P */
   34241              : 
   34242              : #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.