LCOV - code coverage report
Current view: top level - gcc/cp - pt.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.6 % 13949 13339
Test Date: 2024-04-13 14:00:49 Functions: 95.7 % 461 441
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Handle parameterized types (templates) for GNU -*- C++ -*-.
       2                 :             :    Copyright (C) 1992-2024 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                 :             : 
      52                 :             : /* The type of functions taking a tree, and some additional data, and
      53                 :             :    returning an int.  */
      54                 :             : typedef int (*tree_fn_t) (tree, void*);
      55                 :             : 
      56                 :             : /* The PENDING_TEMPLATES is a list of templates whose instantiations
      57                 :             :    have been deferred, either because their definitions were not yet
      58                 :             :    available, or because we were putting off doing the work.  */
      59                 :             : struct GTY ((chain_next ("%h.next"))) pending_template
      60                 :             : {
      61                 :             :   struct pending_template *next;
      62                 :             :   struct tinst_level *tinst;
      63                 :             : };
      64                 :             : 
      65                 :             : static GTY(()) struct pending_template *pending_templates;
      66                 :             : static GTY(()) struct pending_template *last_pending_template;
      67                 :             : 
      68                 :             : int processing_template_parmlist;
      69                 :             : static int template_header_count;
      70                 :             : 
      71                 :             : static vec<int> inline_parm_levels;
      72                 :             : 
      73                 :             : static GTY(()) struct tinst_level *current_tinst_level;
      74                 :             : 
      75                 :             : static GTY(()) vec<tree, va_gc> *saved_access_scope;
      76                 :             : 
      77                 :             : /* Live only within one (recursive) call to tsubst_expr.  We use
      78                 :             :    this to pass the statement expression node from the STMT_EXPR
      79                 :             :    to the EXPR_STMT that is its result.  */
      80                 :             : static tree cur_stmt_expr;
      81                 :             : 
      82                 :             : // -------------------------------------------------------------------------- //
      83                 :             : // Local Specialization Stack
      84                 :             : //
      85                 :             : // Implementation of the RAII helper for creating new local
      86                 :             : // specializations.
      87                 :    35581596 : local_specialization_stack::local_specialization_stack (lss_policy policy)
      88                 :    35581596 :   : saved (local_specializations)
      89                 :             : {
      90                 :    35581596 :   if (policy == lss_nop)
      91                 :             :     ;
      92                 :    19081711 :   else if (policy == lss_blank || !saved)
      93                 :    17639720 :     local_specializations = new hash_map<tree, tree>;
      94                 :             :   else
      95                 :     1441991 :     local_specializations = new hash_map<tree, tree>(*saved);
      96                 :    35581596 : }
      97                 :             : 
      98                 :    35580685 : local_specialization_stack::~local_specialization_stack ()
      99                 :             : {
     100                 :    35580685 :   if (local_specializations != saved)
     101                 :             :     {
     102                 :    38161600 :       delete local_specializations;
     103                 :    19080800 :       local_specializations = saved;
     104                 :             :     }
     105                 :    35580685 : }
     106                 :             : 
     107                 :             : /* True if we've recursed into fn_type_unification too many times.  */
     108                 :             : static bool excessive_deduction_depth;
     109                 :             : 
     110                 :             : struct spec_hasher : ggc_ptr_hash<spec_entry>
     111                 :             : {
     112                 :             :   static hashval_t hash (tree, tree);
     113                 :             :   static hashval_t hash (spec_entry *);
     114                 :             :   static bool equal (spec_entry *, spec_entry *);
     115                 :             : };
     116                 :             : 
     117                 :             : /* The general template is not in these tables.  */
     118                 :             : typedef hash_table<spec_hasher> spec_hash_table;
     119                 :             : static GTY (()) spec_hash_table *decl_specializations;
     120                 :             : static GTY (()) spec_hash_table *type_specializations;
     121                 :             : 
     122                 :             : /* Contains canonical template parameter types. The vector is indexed by
     123                 :             :    the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
     124                 :             :    TREE_LIST, whose TREE_VALUEs contain the canonical template
     125                 :             :    parameters of various types and levels.  */
     126                 :             : static GTY(()) vec<tree, va_gc> *canonical_template_parms;
     127                 :             : 
     128                 :             : #define UNIFY_ALLOW_NONE 0
     129                 :             : #define UNIFY_ALLOW_MORE_CV_QUAL 1
     130                 :             : #define UNIFY_ALLOW_LESS_CV_QUAL 2
     131                 :             : #define UNIFY_ALLOW_DERIVED 4
     132                 :             : #define UNIFY_ALLOW_INTEGER 8
     133                 :             : #define UNIFY_ALLOW_OUTER_LEVEL 16
     134                 :             : #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
     135                 :             : #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
     136                 :             : 
     137                 :             : enum template_base_result {
     138                 :             :   tbr_incomplete_type,
     139                 :             :   tbr_ambiguous_baseclass,
     140                 :             :   tbr_success
     141                 :             : };
     142                 :             : 
     143                 :             : static bool resolve_overloaded_unification (tree, tree, tree, tree,
     144                 :             :                                             unification_kind_t, int,
     145                 :             :                                             bool);
     146                 :             : static int try_one_overload (tree, tree, tree, tree, tree,
     147                 :             :                              unification_kind_t, int, bool, bool);
     148                 :             : static int unify (tree, tree, tree, tree, int, bool);
     149                 :             : static void add_pending_template (tree);
     150                 :             : static tree reopen_tinst_level (struct tinst_level *);
     151                 :             : static tree tsubst_initializer_list (tree, tree);
     152                 :             : static tree get_partial_spec_bindings (tree, tree, tree);
     153                 :             : static void tsubst_enum (tree, tree, tree);
     154                 :             : static bool check_instantiated_args (tree, tree, tsubst_flags_t);
     155                 :             : static int check_non_deducible_conversion (tree, tree, unification_kind_t, int,
     156                 :             :                                            struct conversion **, bool, bool);
     157                 :             : static int maybe_adjust_types_for_deduction (tree, unification_kind_t,
     158                 :             :                                              tree*, tree*, tree);
     159                 :             : static int type_unification_real (tree, tree, tree, const tree *,
     160                 :             :                                   unsigned int, int, unification_kind_t,
     161                 :             :                                   vec<deferred_access_check, va_gc> **,
     162                 :             :                                   bool);
     163                 :             : static void note_template_header (int);
     164                 :             : static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
     165                 :             : static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
     166                 :             : static tree convert_template_argument (tree, tree, tree,
     167                 :             :                                        tsubst_flags_t, int, tree);
     168                 :             : static tree for_each_template_parm (tree, tree_fn_t, void*,
     169                 :             :                                     hash_set<tree> *, bool, tree_fn_t = NULL);
     170                 :             : static tree expand_template_argument_pack (tree);
     171                 :             : static tree build_template_parm_index (int, int, int, tree, tree);
     172                 :             : static bool inline_needs_template_parms (tree, bool);
     173                 :             : static void push_inline_template_parms_recursive (tree, int);
     174                 :             : static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
     175                 :             : static int mark_template_parm (tree, void *);
     176                 :             : static int template_parm_this_level_p (tree, void *);
     177                 :             : static tree tsubst_friend_function (tree, tree);
     178                 :             : static tree tsubst_friend_class (tree, tree);
     179                 :             : static int can_complete_type_without_circularity (tree);
     180                 :             : static tree get_bindings (tree, tree, tree, bool);
     181                 :             : static int template_decl_level (tree);
     182                 :             : static int check_cv_quals_for_unify (int, tree, tree);
     183                 :             : static int unify_pack_expansion (tree, tree, tree,
     184                 :             :                                  tree, unification_kind_t, bool, bool);
     185                 :             : static tree copy_template_args (tree);
     186                 :             : static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
     187                 :             : static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t);
     188                 :             : static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
     189                 :             : static tree tsubst_aggr_type_1 (tree, tree, tsubst_flags_t, tree, int);
     190                 :             : static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
     191                 :             : static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
     192                 :             : static bool check_specialization_scope (void);
     193                 :             : static tree process_partial_specialization (tree);
     194                 :             : static enum template_base_result get_template_base (tree, tree, tree, tree,
     195                 :             :                                                     bool , tree *);
     196                 :             : static tree try_class_unification (tree, tree, tree, tree, bool);
     197                 :             : static bool class_nttp_const_wrapper_p (tree t);
     198                 :             : static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
     199                 :             :                                            tree, tree);
     200                 :             : static bool template_template_parm_bindings_ok_p (tree, tree);
     201                 :             : static void tsubst_default_arguments (tree, tsubst_flags_t);
     202                 :             : static tree for_each_template_parm_r (tree *, int *, void *);
     203                 :             : static tree copy_default_args_to_explicit_spec_1 (tree, tree);
     204                 :             : static void copy_default_args_to_explicit_spec (tree);
     205                 :             : static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
     206                 :             : static bool dependent_template_arg_p (tree);
     207                 :             : static bool dependent_type_p_r (tree);
     208                 :             : static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree);
     209                 :             : static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true);
     210                 :             : static tree tsubst_scope (tree, tree, tsubst_flags_t, tree);
     211                 :             : static tree tsubst_name (tree, tree, tsubst_flags_t, tree);
     212                 :             : static void perform_instantiation_time_access_checks (tree, tree);
     213                 :             : static tree listify (tree);
     214                 :             : static tree listify_autos (tree, tree);
     215                 :             : static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
     216                 :             : static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
     217                 :             : static tree get_underlying_template (tree);
     218                 :             : static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
     219                 :             : static tree canonicalize_expr_argument (tree, tsubst_flags_t);
     220                 :             : static tree make_argument_pack (tree);
     221                 :             : static tree enclosing_instantiation_of (tree tctx);
     222                 :             : static void instantiate_body (tree pattern, tree args, tree d, bool nested);
     223                 :             : static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree);
     224                 :             : static void mark_template_arguments_used (tree, tree);
     225                 :             : static bool uses_outer_template_parms (tree);
     226                 :             : static tree alias_ctad_tweaks (tree, tree);
     227                 :             : static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t);
     228                 :             : static tree deduction_guides_for (tree, bool&, tsubst_flags_t);
     229                 :             : 
     230                 :             : /* Make the current scope suitable for access checking when we are
     231                 :             :    processing T.  T can be FUNCTION_DECL for instantiated function
     232                 :             :    template, VAR_DECL for static member variable, or TYPE_DECL for
     233                 :             :    for a class or alias template (needed by instantiate_decl).  */
     234                 :             : 
     235                 :             : void
     236                 :   140859869 : push_access_scope (tree t)
     237                 :             : {
     238                 :   140859869 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
     239                 :             :               || TREE_CODE (t) == TYPE_DECL);
     240                 :             : 
     241                 :   237547509 :   if (DECL_FRIEND_CONTEXT (t))
     242                 :      407291 :     push_nested_class (DECL_FRIEND_CONTEXT (t));
     243                 :    36579008 :   else if (DECL_IMPLICIT_TYPEDEF_P (t)
     244                 :   150068194 :            && CLASS_TYPE_P (TREE_TYPE (t)))
     245                 :     9615616 :     push_nested_class (TREE_TYPE (t));
     246                 :   130836962 :   else if (DECL_CLASS_SCOPE_P (t))
     247                 :    34759497 :     push_nested_class (DECL_CONTEXT (t));
     248                 :    96077465 :   else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
     249                 :             :     /* An artificial deduction guide should have the same access as
     250                 :             :        the constructor.  */
     251                 :       33847 :     push_nested_class (TREE_TYPE (TREE_TYPE (t)));
     252                 :             :   else
     253                 :    96043618 :     push_to_top_level ();
     254                 :             : 
     255                 :   140859869 :   if (TREE_CODE (t) == FUNCTION_DECL)
     256                 :             :     {
     257                 :    96923608 :       vec_safe_push (saved_access_scope, current_function_decl);
     258                 :    96923608 :       current_function_decl = t;
     259                 :             :     }
     260                 :   140859869 : }
     261                 :             : 
     262                 :             : /* Restore the scope set up by push_access_scope.  T is the node we
     263                 :             :    are processing.  */
     264                 :             : 
     265                 :             : void
     266                 :   140858969 : pop_access_scope (tree t)
     267                 :             : {
     268                 :   140858969 :   if (TREE_CODE (t) == FUNCTION_DECL)
     269                 :    96922708 :     current_function_decl = saved_access_scope->pop();
     270                 :             : 
     271                 :   237545709 :   if (DECL_FRIEND_CONTEXT (t)
     272                 :   140451678 :       || (DECL_IMPLICIT_TYPEDEF_P (t)
     273                 :     9615616 :           && CLASS_TYPE_P (TREE_TYPE (t)))
     274                 :   130836062 :       || DECL_CLASS_SCOPE_P (t)
     275                 :   185091597 :       || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
     276                 :    44816251 :     pop_nested_class ();
     277                 :             :   else
     278                 :    96042718 :     pop_from_top_level ();
     279                 :   140858969 : }
     280                 :             : 
     281                 :             : /* Do any processing required when DECL (a member template
     282                 :             :    declaration) is finished.  Returns the TEMPLATE_DECL corresponding
     283                 :             :    to DECL, unless it is a specialization, in which case the DECL
     284                 :             :    itself is returned.  */
     285                 :             : 
     286                 :             : tree
     287                 :    16766137 : finish_member_template_decl (tree decl)
     288                 :             : {
     289                 :    16766137 :   if (decl == error_mark_node)
     290                 :             :     return error_mark_node;
     291                 :             : 
     292                 :    16766044 :   gcc_assert (DECL_P (decl));
     293                 :             : 
     294                 :    16766044 :   if (TREE_CODE (decl) == TYPE_DECL)
     295                 :             :     {
     296                 :     1133701 :       tree type;
     297                 :             : 
     298                 :     1133701 :       type = TREE_TYPE (decl);
     299                 :     1133701 :       if (type == error_mark_node)
     300                 :             :         return error_mark_node;
     301                 :     1133701 :       if (MAYBE_CLASS_TYPE_P (type)
     302                 :     1133701 :           && CLASSTYPE_TEMPLATE_INFO (type)
     303                 :     1133701 :           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     304                 :             :         {
     305                 :      644467 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     306                 :      644467 :           check_member_template (tmpl);
     307                 :      644467 :           return tmpl;
     308                 :             :         }
     309                 :             :       return NULL_TREE;
     310                 :             :     }
     311                 :    15632343 :   else if (TREE_CODE (decl) == FIELD_DECL)
     312                 :           4 :     error_at (DECL_SOURCE_LOCATION (decl),
     313                 :             :               "data member %qD cannot be a member template", decl);
     314                 :    15632339 :   else if (DECL_TEMPLATE_INFO (decl))
     315                 :             :     {
     316                 :    15632339 :       if (!DECL_TEMPLATE_SPECIALIZATION (decl))
     317                 :             :         {
     318                 :    15632324 :           check_member_template (DECL_TI_TEMPLATE (decl));
     319                 :    15632324 :           return DECL_TI_TEMPLATE (decl);
     320                 :             :         }
     321                 :             :       else
     322                 :             :         return NULL_TREE;
     323                 :             :     }
     324                 :             :   else
     325                 :           0 :     error_at (DECL_SOURCE_LOCATION (decl),
     326                 :             :               "invalid member template declaration %qD", decl);
     327                 :             : 
     328                 :           4 :   return error_mark_node;
     329                 :             : }
     330                 :             : 
     331                 :             : /* Create a template info node.  */
     332                 :             : 
     333                 :             : tree
     334                 :   528196170 : build_template_info (tree template_decl, tree template_args)
     335                 :             : {
     336                 :   528196170 :   tree result = make_node (TEMPLATE_INFO);
     337                 :   528196170 :   TI_TEMPLATE (result) = template_decl;
     338                 :   528196170 :   TI_ARGS (result) = template_args;
     339                 :   528196170 :   return result;
     340                 :             : }
     341                 :             : 
     342                 :             : /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE.  */
     343                 :             : 
     344                 :             : static tree
     345                 :  1753876640 : decl_template_info (const_tree decl)
     346                 :             : {
     347                 :             :   /* This needs to match template_info_decl_check.  */
     348                 :  1753876640 :   if (DECL_LANG_SPECIFIC (decl))
     349                 :  1379334552 :     switch (TREE_CODE (decl))
     350                 :             :       {
     351                 :  1247470360 :       case FUNCTION_DECL:
     352                 :  1247470360 :         if (DECL_THUNK_P (decl))
     353                 :             :           break;
     354                 :  1379320556 :         gcc_fallthrough ();
     355                 :  1379320556 :       case VAR_DECL:
     356                 :  1379320556 :       case FIELD_DECL:
     357                 :  1379320556 :       case TYPE_DECL:
     358                 :  1379320556 :       case CONCEPT_DECL:
     359                 :  1379320556 :       case TEMPLATE_DECL:
     360                 :  1379320556 :         return DECL_TEMPLATE_INFO (decl);
     361                 :             : 
     362                 :             :       default:
     363                 :             :         break;
     364                 :             :       }
     365                 :             :   return NULL_TREE;
     366                 :             : }
     367                 :             : 
     368                 :             : /* Return the template info node corresponding to T, whatever T is.  */
     369                 :             : 
     370                 :             : tree
     371                 :  2385348379 : get_template_info (const_tree t)
     372                 :             : {
     373                 :  2385348379 :   tree tinfo = NULL_TREE;
     374                 :             : 
     375                 :  2385348379 :   if (!t || t == error_mark_node)
     376                 :             :     return NULL;
     377                 :             : 
     378                 :  2385348377 :   if (TREE_CODE (t) == NAMESPACE_DECL
     379                 :  2313560309 :       || TREE_CODE (t) == PARM_DECL)
     380                 :             :     return NULL;
     381                 :             : 
     382                 :  2305544089 :   if (DECL_P (t))
     383                 :  1753876640 :     tinfo = decl_template_info (t);
     384                 :             : 
     385                 :  2305544089 :   if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
     386                 :   372697060 :     t = TREE_TYPE (t);
     387                 :             : 
     388                 :  2305544089 :   if (OVERLOAD_TYPE_P (t))
     389                 :   908799752 :     tinfo = TYPE_TEMPLATE_INFO (t);
     390                 :  1396744337 :   else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
     391                 :          13 :     tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
     392                 :             : 
     393                 :             :   return tinfo;
     394                 :             : }
     395                 :             : 
     396                 :             : /* Returns the template nesting level of the indicated class TYPE.
     397                 :             : 
     398                 :             :    For example, in:
     399                 :             :      template <class T>
     400                 :             :      struct A
     401                 :             :      {
     402                 :             :        template <class U>
     403                 :             :        struct B {};
     404                 :             :      };
     405                 :             : 
     406                 :             :    A<T>::B<U> has depth two, while A<T> has depth one.
     407                 :             :    Both A<T>::B<int> and A<int>::B<U> have depth one, if
     408                 :             :    they are instantiations, not specializations.
     409                 :             : 
     410                 :             :    This function is guaranteed to return 0 if passed NULL_TREE so
     411                 :             :    that, for example, `template_class_depth (current_class_type)' is
     412                 :             :    always safe.  */
     413                 :             : 
     414                 :             : int
     415                 :   491504540 : template_class_depth (tree type)
     416                 :             : {
     417                 :   491504540 :   int depth;
     418                 :             : 
     419                 :   678737498 :   for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
     420                 :             :     {
     421                 :   187232958 :       tree tinfo = get_template_info (type);
     422                 :             : 
     423                 :   187232958 :       if (tinfo
     424                 :   169237518 :           && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL
     425                 :   169237518 :           && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
     426                 :   353745414 :           && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
     427                 :   138132830 :         ++depth;
     428                 :             : 
     429                 :   187232958 :       if (DECL_P (type))
     430                 :             :         {
     431                 :      776685 :           if (tree fctx = DECL_FRIEND_CONTEXT (type))
     432                 :             :             type = fctx;
     433                 :             :           else
     434                 :      378480 :             type = CP_DECL_CONTEXT (type);
     435                 :             :         }
     436                 :   373164439 :       else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
     437                 :      280814 :         type = LAMBDA_TYPE_EXTRA_SCOPE (type);
     438                 :             :       else
     439                 :   186561690 :         type = CP_TYPE_CONTEXT (type);
     440                 :             :     }
     441                 :             : 
     442                 :   491504540 :   return depth;
     443                 :             : }
     444                 :             : 
     445                 :             : /* Return TRUE if NODE instantiates a template that has arguments of
     446                 :             :    its own, be it directly a primary template or indirectly through a
     447                 :             :    partial specializations.  */
     448                 :             : static bool
     449                 :    95886345 : instantiates_primary_template_p (tree node)
     450                 :             : {
     451                 :    95886345 :   tree tinfo = get_template_info (node);
     452                 :    95886345 :   if (!tinfo)
     453                 :             :     return false;
     454                 :             : 
     455                 :    95876314 :   tree tmpl = TI_TEMPLATE (tinfo);
     456                 :    95876314 :   if (PRIMARY_TEMPLATE_P (tmpl))
     457                 :             :     return true;
     458                 :             : 
     459                 :    88748063 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
     460                 :             :     return false;
     461                 :             : 
     462                 :             :   /* So now we know we have a specialization, but it could be a full
     463                 :             :      or a partial specialization.  To tell which, compare the depth of
     464                 :             :      its template arguments with those of its context.  */
     465                 :             : 
     466                 :           0 :   tree ctxt = DECL_CONTEXT (tmpl);
     467                 :           0 :   tree ctinfo = get_template_info (ctxt);
     468                 :           0 :   if (!ctinfo)
     469                 :             :     return true;
     470                 :             : 
     471                 :           0 :   return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
     472                 :           0 :           > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
     473                 :             : }
     474                 :             : 
     475                 :             : /* Subroutine of maybe_begin_member_template_processing.
     476                 :             :    Returns true if processing DECL needs us to push template parms.  */
     477                 :             : 
     478                 :             : static bool
     479                 :   148397892 : inline_needs_template_parms (tree decl, bool nsdmi)
     480                 :             : {
     481                 :   148397892 :   if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
     482                 :             :     return false;
     483                 :             : 
     484                 :   125142919 :   return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
     485                 :   125142919 :           > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl)));
     486                 :             : }
     487                 :             : 
     488                 :             : /* Subroutine of maybe_begin_member_template_processing.
     489                 :             :    Push the template parms in PARMS, starting from LEVELS steps into the
     490                 :             :    chain, and ending at the beginning, since template parms are listed
     491                 :             :    innermost first.  */
     492                 :             : 
     493                 :             : static void
     494                 :    33279419 : push_inline_template_parms_recursive (tree parmlist, int levels)
     495                 :             : {
     496                 :    33279419 :   tree parms = TREE_VALUE (parmlist);
     497                 :    33279419 :   int i;
     498                 :             : 
     499                 :    33279419 :   if (levels > 1)
     500                 :      435849 :     push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
     501                 :             : 
     502                 :    33279419 :   ++processing_template_decl;
     503                 :    66558838 :   current_template_parms
     504                 :    33279419 :     = tree_cons (size_int (current_template_depth + 1),
     505                 :             :                  parms, current_template_parms);
     506                 :    66558838 :   TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)
     507                 :    33279419 :     = TEMPLATE_PARMS_CONSTRAINTS (parmlist);
     508                 :    33279419 :   TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
     509                 :             : 
     510                 :    33279419 :   begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
     511                 :             :                NULL);
     512                 :    89302086 :   for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
     513                 :             :     {
     514                 :    56022667 :       tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
     515                 :             : 
     516                 :    56022667 :       if (error_operand_p (parm))
     517                 :          35 :         continue;
     518                 :             : 
     519                 :    56022632 :       gcc_assert (DECL_P (parm));
     520                 :             : 
     521                 :    56022632 :       switch (TREE_CODE (parm))
     522                 :             :         {
     523                 :    51759044 :         case TYPE_DECL:
     524                 :    51759044 :         case TEMPLATE_DECL:
     525                 :    51759044 :           pushdecl (parm);
     526                 :    51759044 :           break;
     527                 :             : 
     528                 :     4263588 :         case PARM_DECL:
     529                 :             :           /* Push the CONST_DECL.  */
     530                 :     4263588 :           pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
     531                 :     4263588 :           break;
     532                 :             : 
     533                 :           0 :         default:
     534                 :           0 :           gcc_unreachable ();
     535                 :             :         }
     536                 :             :     }
     537                 :    33279419 : }
     538                 :             : 
     539                 :             : /* Restore the template parameter context for a member template, a
     540                 :             :    friend template defined in a class definition, or a non-template
     541                 :             :    member of template class.  */
     542                 :             : 
     543                 :             : void
     544                 :   148397892 : maybe_begin_member_template_processing (tree decl)
     545                 :             : {
     546                 :   148397892 :   tree parms;
     547                 :   148397892 :   int levels = 0;
     548                 :   148397892 :   bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
     549                 :             : 
     550                 :   148397892 :   if (nsdmi)
     551                 :             :     {
     552                 :      856478 :       tree ctx = DECL_CONTEXT (decl);
     553                 :      856478 :       decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
     554                 :             :               /* Disregard full specializations (c++/60999).  */
     555                 :      518449 :               && uses_template_parms (ctx)
     556                 :     1372786 :               ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
     557                 :             :     }
     558                 :             : 
     559                 :   148397892 :   if (inline_needs_template_parms (decl, nsdmi))
     560                 :             :     {
     561                 :    32843570 :       parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
     562                 :    32843570 :       levels = TMPL_PARMS_DEPTH (parms) - current_template_depth;
     563                 :             : 
     564                 :    32843570 :       if (DECL_TEMPLATE_SPECIALIZATION (decl))
     565                 :             :         {
     566                 :           0 :           --levels;
     567                 :           0 :           parms = TREE_CHAIN (parms);
     568                 :             :         }
     569                 :             : 
     570                 :    32843570 :       push_inline_template_parms_recursive (parms, levels);
     571                 :             :     }
     572                 :             : 
     573                 :             :   /* Remember how many levels of template parameters we pushed so that
     574                 :             :      we can pop them later.  */
     575                 :   148397892 :   inline_parm_levels.safe_push (levels);
     576                 :   148397892 : }
     577                 :             : 
     578                 :             : /* Undo the effects of maybe_begin_member_template_processing.  */
     579                 :             : 
     580                 :             : void
     581                 :   148397892 : maybe_end_member_template_processing (void)
     582                 :             : {
     583                 :   148397892 :   int i;
     584                 :   148397892 :   int last;
     585                 :             : 
     586                 :   148397892 :   if (inline_parm_levels.length () == 0)
     587                 :             :     return;
     588                 :             : 
     589                 :   148397892 :   last = inline_parm_levels.pop ();
     590                 :   181677311 :   for (i = 0; i < last; ++i)
     591                 :             :     {
     592                 :    33279419 :       --processing_template_decl;
     593                 :    33279419 :       current_template_parms = TREE_CHAIN (current_template_parms);
     594                 :    33279419 :       poplevel (0, 0, 0);
     595                 :             :     }
     596                 :             : }
     597                 :             : 
     598                 :             : /* Return a new template argument vector which contains all of ARGS,
     599                 :             :    but has as its innermost set of arguments the EXTRA_ARGS.  */
     600                 :             : 
     601                 :             : tree
     602                 :   119177469 : add_to_template_args (tree args, tree extra_args)
     603                 :             : {
     604                 :   119177469 :   tree new_args;
     605                 :   119177469 :   int extra_depth;
     606                 :   119177469 :   int i;
     607                 :   119177469 :   int j;
     608                 :             : 
     609                 :   119177469 :   if (args == NULL_TREE || extra_args == error_mark_node)
     610                 :             :     return extra_args;
     611                 :             : 
     612                 :    74378822 :   extra_depth = TMPL_ARGS_DEPTH (extra_args);
     613                 :    37189411 :   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
     614                 :             : 
     615                 :   118132145 :   for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
     616                 :    37354025 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
     617                 :             : 
     618                 :    74378826 :   for (j = 1; j <= extra_depth; ++j, ++i)
     619                 :    74378830 :     SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
     620                 :             : 
     621                 :             :   return new_args;
     622                 :             : }
     623                 :             : 
     624                 :             : /* Like add_to_template_args, but only the outermost ARGS are added to
     625                 :             :    the EXTRA_ARGS.  In particular, all but TMPL_ARGS_DEPTH
     626                 :             :    (EXTRA_ARGS) levels are added.  This function is used to combine
     627                 :             :    the template arguments from a partial instantiation with the
     628                 :             :    template arguments used to attain the full instantiation from the
     629                 :             :    partial instantiation.
     630                 :             : 
     631                 :             :    If ARGS is a TEMPLATE_DECL, use its parameters as args.  */
     632                 :             : 
     633                 :             : tree
     634                 :   697763547 : add_outermost_template_args (tree args, tree extra_args)
     635                 :             : {
     636                 :   697763547 :   tree new_args;
     637                 :             : 
     638                 :   697763547 :   if (!args)
     639                 :             :     return extra_args;
     640                 :   697763547 :   if (TREE_CODE (args) == TEMPLATE_DECL)
     641                 :             :     {
     642                 :   113387591 :       tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
     643                 :   113387591 :       args = TI_ARGS (ti);
     644                 :             :     }
     645                 :             : 
     646                 :             :   /* If there are more levels of EXTRA_ARGS than there are ARGS,
     647                 :             :      something very fishy is going on.  */
     648                 :  2864453704 :   gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
     649                 :             : 
     650                 :             :   /* If *all* the new arguments will be the EXTRA_ARGS, just return
     651                 :             :      them.  */
     652                 :  3448829660 :   if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
     653                 :             :     return extra_args;
     654                 :             : 
     655                 :             :   /* For the moment, we make ARGS look like it contains fewer levels.  */
     656                 :   101283093 :   TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
     657                 :             : 
     658                 :    33890462 :   new_args = add_to_template_args (args, extra_args);
     659                 :             : 
     660                 :             :   /* Now, we restore ARGS to its full dimensions.  */
     661                 :   101283093 :   TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
     662                 :             : 
     663                 :    33890462 :   return new_args;
     664                 :             : }
     665                 :             : 
     666                 :             : /* Return the N levels of innermost template arguments from the ARGS.  */
     667                 :             : 
     668                 :             : tree
     669                 :  3861501667 : get_innermost_template_args (tree args, int n)
     670                 :             : {
     671                 :  3861501667 :   tree new_args;
     672                 :  3861501667 :   int extra_levels;
     673                 :  3861501667 :   int i;
     674                 :             : 
     675                 :  3861501667 :   gcc_assert (n >= 0);
     676                 :             : 
     677                 :             :   /* If N is 1, just return the innermost set of template arguments.  */
     678                 :  3861501667 :   if (n == 1)
     679                 :  7722875668 :     return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
     680                 :             : 
     681                 :             :   /* If we're not removing anything, just return the arguments we were
     682                 :             :      given.  */
     683                 :      127666 :   extra_levels = TMPL_ARGS_DEPTH (args) - n;
     684                 :       63833 :   gcc_assert (extra_levels >= 0);
     685                 :       63833 :   if (extra_levels == 0)
     686                 :             :     return args;
     687                 :             : 
     688                 :             :   /* Make a new set of arguments, not containing the outer arguments.  */
     689                 :           8 :   new_args = make_tree_vec (n);
     690                 :          32 :   for (i = 1; i <= n; ++i)
     691                 :          32 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     692                 :             :                          TMPL_ARGS_LEVEL (args, i + extra_levels));
     693                 :             : 
     694                 :             :   return new_args;
     695                 :             : }
     696                 :             : 
     697                 :             : /* The inverse of get_innermost_template_args: Return all but the innermost
     698                 :             :    EXTRA_LEVELS levels of template arguments from the ARGS.  */
     699                 :             : 
     700                 :             : static tree
     701                 :      170739 : strip_innermost_template_args (tree args, int extra_levels)
     702                 :             : {
     703                 :      170739 :   tree new_args;
     704                 :      341478 :   int n = TMPL_ARGS_DEPTH (args) - extra_levels;
     705                 :      170739 :   int i;
     706                 :             : 
     707                 :      170739 :   gcc_assert (n >= 0);
     708                 :             : 
     709                 :             :   /* If N is 1, just return the outermost set of template arguments.  */
     710                 :      170739 :   if (n == 1)
     711                 :      340572 :     return TMPL_ARGS_LEVEL (args, 1);
     712                 :             : 
     713                 :             :   /* If we're not removing anything, just return the arguments we were
     714                 :             :      given.  */
     715                 :         453 :   gcc_assert (extra_levels >= 0);
     716                 :         453 :   if (extra_levels == 0)
     717                 :             :     return args;
     718                 :             : 
     719                 :             :   /* Make a new set of arguments, not containing the inner arguments.  */
     720                 :         453 :   new_args = make_tree_vec (n);
     721                 :        1812 :   for (i = 1; i <= n; ++i)
     722                 :        1812 :     SET_TMPL_ARGS_LEVEL (new_args, i,
     723                 :             :                          TMPL_ARGS_LEVEL (args, i));
     724                 :             : 
     725                 :             :   return new_args;
     726                 :             : }
     727                 :             : 
     728                 :             : /* We've got a template header coming up; push to a new level for storing
     729                 :             :    the parms.  */
     730                 :             : 
     731                 :             : void
     732                 :    75019339 : begin_template_parm_list (void)
     733                 :             : {
     734                 :             :   /* We use a non-tag-transparent scope here, which causes pushtag to
     735                 :             :      put tags in this scope, rather than in the enclosing class or
     736                 :             :      namespace scope.  This is the right thing, since we want
     737                 :             :      TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
     738                 :             :      global template class, push_template_decl handles putting the
     739                 :             :      TEMPLATE_DECL into top-level scope.  For a nested template class,
     740                 :             :      e.g.:
     741                 :             : 
     742                 :             :        template <class T> struct S1 {
     743                 :             :          template <class T> struct S2 {};
     744                 :             :        };
     745                 :             : 
     746                 :             :      pushtag contains special code to insert the TEMPLATE_DECL for S2
     747                 :             :      at the right scope.  */
     748                 :    75019339 :   begin_scope (sk_template_parms, NULL);
     749                 :    75019339 :   ++processing_template_decl;
     750                 :    75019339 :   ++processing_template_parmlist;
     751                 :    75019339 :   note_template_header (0);
     752                 :             : 
     753                 :             :   /* Add a dummy parameter level while we process the parameter list.  */
     754                 :   150038678 :   current_template_parms
     755                 :    75019339 :     = tree_cons (size_int (current_template_depth + 1),
     756                 :             :                  make_tree_vec (0),
     757                 :             :                  current_template_parms);
     758                 :    75019339 : }
     759                 :             : 
     760                 :             : /* This routine is called when a specialization is declared.  If it is
     761                 :             :    invalid to declare a specialization here, an error is reported and
     762                 :             :    false is returned, otherwise this routine will return true.  */
     763                 :             : 
     764                 :             : static bool
     765                 :     4740382 : check_specialization_scope (void)
     766                 :             : {
     767                 :     4740382 :   tree scope = current_scope ();
     768                 :             : 
     769                 :             :   /* [temp.expl.spec]
     770                 :             : 
     771                 :             :      An explicit specialization shall be declared in the namespace of
     772                 :             :      which the template is a member, or, for member templates, in the
     773                 :             :      namespace of which the enclosing class or enclosing class
     774                 :             :      template is a member.  An explicit specialization of a member
     775                 :             :      function, member class or static data member of a class template
     776                 :             :      shall be declared in the namespace of which the class template
     777                 :             :      is a member.  */
     778                 :     4740382 :   if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
     779                 :             :     {
     780                 :          32 :       error ("explicit specialization in non-namespace scope %qD", scope);
     781                 :          32 :       return false;
     782                 :             :     }
     783                 :             : 
     784                 :             :   /* [temp.expl.spec]
     785                 :             : 
     786                 :             :      In an explicit specialization declaration for a member of a class
     787                 :             :      template or a member template that appears in namespace scope,
     788                 :             :      the member template and some of its enclosing class templates may
     789                 :             :      remain unspecialized, except that the declaration shall not
     790                 :             :      explicitly specialize a class member template if its enclosing
     791                 :             :      class templates are not explicitly specialized as well.  */
     792                 :     4740350 :   if (current_template_parms)
     793                 :             :     {
     794                 :           8 :       error ("enclosing class templates are not explicitly specialized");
     795                 :           8 :       return false;
     796                 :             :     }
     797                 :             : 
     798                 :             :   return true;
     799                 :             : }
     800                 :             : 
     801                 :             : /* We've just seen template <>.  */
     802                 :             : 
     803                 :             : bool
     804                 :     4740382 : begin_specialization (void)
     805                 :             : {
     806                 :     4740382 :   begin_scope (sk_template_spec, NULL);
     807                 :     4740382 :   note_template_header (1);
     808                 :     9480764 :   return check_specialization_scope ();
     809                 :             : }
     810                 :             : 
     811                 :             : /* Called at then end of processing a declaration preceded by
     812                 :             :    template<>.  */
     813                 :             : 
     814                 :             : void
     815                 :     4740382 : end_specialization (void)
     816                 :             : {
     817                 :     4740382 :   finish_scope ();
     818                 :     4740382 :   reset_specialization ();
     819                 :     4740382 : }
     820                 :             : 
     821                 :             : /* Any template <>'s that we have seen thus far are not referring to a
     822                 :             :    function specialization.  */
     823                 :             : 
     824                 :             : void
     825                 :   140473035 : reset_specialization (void)
     826                 :             : {
     827                 :   140473035 :   processing_specialization = 0;
     828                 :   140473035 :   template_header_count = 0;
     829                 :   140473035 : }
     830                 :             : 
     831                 :             : /* We've just seen a template header.  If SPECIALIZATION is nonzero,
     832                 :             :    it was of the form template <>.  */
     833                 :             : 
     834                 :             : static void
     835                 :    79759721 : note_template_header (int specialization)
     836                 :             : {
     837                 :    79759721 :   processing_specialization = specialization;
     838                 :    79759721 :   template_header_count++;
     839                 :           0 : }
     840                 :             : 
     841                 :             : /* We're beginning an explicit instantiation.  */
     842                 :             : 
     843                 :             : void
     844                 :     2684356 : begin_explicit_instantiation (void)
     845                 :             : {
     846                 :     2684356 :   gcc_assert (!processing_explicit_instantiation);
     847                 :     2684356 :   processing_explicit_instantiation = true;
     848                 :     2684356 : }
     849                 :             : 
     850                 :             : 
     851                 :             : void
     852                 :     2684352 : end_explicit_instantiation (void)
     853                 :             : {
     854                 :     2684352 :   gcc_assert (processing_explicit_instantiation);
     855                 :     2684352 :   processing_explicit_instantiation = false;
     856                 :     2684352 : }
     857                 :             : 
     858                 :             : /* An explicit specialization or partial specialization of TMPL is being
     859                 :             :    declared.  Check that the namespace in which the specialization is
     860                 :             :    occurring is permissible.  Returns false iff it is invalid to
     861                 :             :    specialize TMPL in the current namespace.  */
     862                 :             : 
     863                 :             : static bool
     864                 :    11526512 : check_specialization_namespace (tree tmpl)
     865                 :             : {
     866                 :    11526512 :   tree tpl_ns = decl_namespace_context (tmpl);
     867                 :             : 
     868                 :             :   /* [tmpl.expl.spec]
     869                 :             : 
     870                 :             :      An explicit specialization shall be declared in a namespace enclosing the
     871                 :             :      specialized template. An explicit specialization whose declarator-id is
     872                 :             :      not qualified shall be declared in the nearest enclosing namespace of the
     873                 :             :      template, or, if the namespace is inline (7.3.1), any namespace from its
     874                 :             :      enclosing namespace set.  */
     875                 :    11526512 :   if (current_scope() != DECL_CONTEXT (tmpl)
     876                 :    11526512 :       && !at_namespace_scope_p ())
     877                 :             :     {
     878                 :          16 :       error ("specialization of %qD must appear at namespace scope", tmpl);
     879                 :          16 :       return false;
     880                 :             :     }
     881                 :             : 
     882                 :    11526496 :   if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
     883                 :             :     /* Same or enclosing namespace.  */
     884                 :             :     return true;
     885                 :             :   else
     886                 :             :     {
     887                 :          16 :       auto_diagnostic_group d;
     888                 :          16 :       if (permerror (input_location,
     889                 :             :                      "specialization of %qD in different namespace", tmpl))
     890                 :          12 :         inform (DECL_SOURCE_LOCATION (tmpl),
     891                 :             :                 "  from definition of %q#D", tmpl);
     892                 :          16 :       return false;
     893                 :          16 :     }
     894                 :             : }
     895                 :             : 
     896                 :             : /* SPEC is an explicit instantiation.  Check that it is valid to
     897                 :             :    perform this explicit instantiation in the current namespace.  */
     898                 :             : 
     899                 :             : static void
     900                 :     2718102 : check_explicit_instantiation_namespace (tree spec)
     901                 :             : {
     902                 :     2718102 :   tree ns;
     903                 :             : 
     904                 :             :   /* DR 275: An explicit instantiation shall appear in an enclosing
     905                 :             :      namespace of its template.  */
     906                 :     2718102 :   ns = decl_namespace_context (spec);
     907                 :     2718102 :   if (!is_nested_namespace (current_namespace, ns))
     908                 :           8 :     permerror (input_location, "explicit instantiation of %qD in namespace %qD "
     909                 :             :                "(which does not enclose namespace %qD)",
     910                 :             :                spec, current_namespace, ns);
     911                 :     2718102 : }
     912                 :             : 
     913                 :             : /* Returns true if TYPE is a new partial specialization that needs to be
     914                 :             :    set up.  This may also modify TYPE to point to the correct (new or
     915                 :             :    existing) constrained partial specialization.  */
     916                 :             : 
     917                 :             : static bool
     918                 :    30349123 : maybe_new_partial_specialization (tree& type)
     919                 :             : {
     920                 :             :   /* An implicit instantiation of an incomplete type implies
     921                 :             :      the definition of a new class template.
     922                 :             : 
     923                 :             :         template<typename T>
     924                 :             :           struct S;
     925                 :             : 
     926                 :             :         template<typename T>
     927                 :             :           struct S<T*>;
     928                 :             : 
     929                 :             :      Here, S<T*> is an implicit instantiation of S whose type
     930                 :             :      is incomplete.  */
     931                 :    30349123 :   if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
     932                 :             :     return true;
     933                 :             : 
     934                 :             :   /* It can also be the case that TYPE is a completed specialization.
     935                 :             :      Continuing the previous example, suppose we also declare:
     936                 :             : 
     937                 :             :         template<typename T>
     938                 :             :           requires Integral<T>
     939                 :             :             struct S<T*>;
     940                 :             : 
     941                 :             :      Here, S<T*> refers to the specialization S<T*> defined
     942                 :             :      above. However, we need to differentiate definitions because
     943                 :             :      we intend to define a new partial specialization. In this case,
     944                 :             :      we rely on the fact that the constraints are different for
     945                 :             :      this declaration than that above.
     946                 :             : 
     947                 :             :      Note that we also get here for injected class names and
     948                 :             :      late-parsed template definitions. We must ensure that we
     949                 :             :      do not create new type declarations for those cases.  */
     950                 :    20216876 :   if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
     951                 :             :     {
     952                 :     5033601 :       tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
     953                 :     5033601 :       tree args = CLASSTYPE_TI_ARGS (type);
     954                 :             : 
     955                 :             :       /* If there are no template parameters, this cannot be a new
     956                 :             :          partial template specialization?  */
     957                 :     5033601 :       if (!current_template_parms)
     958                 :             :         return false;
     959                 :             : 
     960                 :             :       /* The injected-class-name is not a new partial specialization.  */
     961                 :     3279880 :       if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
     962                 :             :         return false;
     963                 :             : 
     964                 :             :       /* If the constraints are not the same as those of the primary
     965                 :             :          then, we can probably create a new specialization.  */
     966                 :     3279880 :       tree type_constr = current_template_constraints ();
     967                 :             : 
     968                 :     3279880 :       if (type == TREE_TYPE (tmpl))
     969                 :             :         {
     970                 :          18 :           tree main_constr = get_constraints (tmpl);
     971                 :          18 :           if (equivalent_constraints (type_constr, main_constr))
     972                 :             :             return false;
     973                 :             :         }
     974                 :             : 
     975                 :             :       /* Also, if there's a pre-existing specialization with matching
     976                 :             :          constraints, then this also isn't new.  */
     977                 :     3279862 :       tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
     978                 :     3445152 :       while (specs)
     979                 :             :         {
     980                 :     3400741 :           tree spec_tmpl = TREE_VALUE (specs);
     981                 :     3400741 :           tree spec_args = TREE_PURPOSE (specs);
     982                 :     3400741 :           tree spec_constr = get_constraints (spec_tmpl);
     983                 :     3400741 :           if (comp_template_args (args, spec_args)
     984                 :     3400741 :               && equivalent_constraints (type_constr, spec_constr))
     985                 :             :             {
     986                 :     3235451 :               type = TREE_TYPE (spec_tmpl);
     987                 :     3235451 :               return false;
     988                 :             :             }
     989                 :      165290 :           specs = TREE_CHAIN (specs);
     990                 :             :         }
     991                 :             : 
     992                 :             :       /* Create a new type node (and corresponding type decl)
     993                 :             :          for the newly declared specialization.  */
     994                 :       44411 :       tree t = make_class_type (TREE_CODE (type));
     995                 :       44411 :       CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
     996                 :       44411 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
     997                 :             : 
     998                 :             :       /* We only need a separate type node for storing the definition of this
     999                 :             :          partial specialization; uses of S<T*> are unconstrained, so all are
    1000                 :             :          equivalent.  So keep TYPE_CANONICAL the same.  */
    1001                 :       44411 :       TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
    1002                 :             : 
    1003                 :             :       /* Build the corresponding type decl.  */
    1004                 :       44411 :       tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
    1005                 :       44411 :       DECL_CONTEXT (d) = TYPE_CONTEXT (t);
    1006                 :       44411 :       DECL_SOURCE_LOCATION (d) = input_location;
    1007                 :       44411 :       TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl));
    1008                 :             : 
    1009                 :       44411 :       set_instantiating_module (d);
    1010                 :       44411 :       DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
    1011                 :             : 
    1012                 :       44411 :       type = t;
    1013                 :       44411 :       return true;
    1014                 :             :     }
    1015                 :             : 
    1016                 :             :   return false;
    1017                 :             : }
    1018                 :             : 
    1019                 :             : /* The TYPE is being declared.  If it is a template type, that means it
    1020                 :             :    is a partial specialization.  Do appropriate error-checking.  */
    1021                 :             : 
    1022                 :             : tree
    1023                 :    60854112 : maybe_process_partial_specialization (tree type)
    1024                 :             : {
    1025                 :    60854112 :   tree context;
    1026                 :             : 
    1027                 :    60854112 :   if (type == error_mark_node)
    1028                 :             :     return error_mark_node;
    1029                 :             : 
    1030                 :             :   /* A lambda that appears in specialization context is not itself a
    1031                 :             :      specialization.  */
    1032                 :    60854050 :   if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
    1033                 :             :     return type;
    1034                 :             : 
    1035                 :             :   /* An injected-class-name is not a specialization.  */
    1036                 :    60186912 :   if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
    1037                 :             :     return type;
    1038                 :             : 
    1039                 :    60186904 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
    1040                 :             :     {
    1041                 :           4 :       error ("name of class shadows template template parameter %qD",
    1042                 :           4 :              TYPE_NAME (type));
    1043                 :           4 :       return error_mark_node;
    1044                 :             :     }
    1045                 :             : 
    1046                 :    60186900 :   context = TYPE_CONTEXT (type);
    1047                 :             : 
    1048                 :    60186900 :   if (TYPE_ALIAS_P (type))
    1049                 :             :     {
    1050                 :           6 :       tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
    1051                 :             : 
    1052                 :          12 :       if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    1053                 :           6 :         error ("specialization of alias template %qD",
    1054                 :           6 :                TI_TEMPLATE (tinfo));
    1055                 :             :       else
    1056                 :           0 :         error ("explicit specialization of non-template %qT", type);
    1057                 :           6 :       return error_mark_node;
    1058                 :             :     }
    1059                 :    60186894 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
    1060                 :             :     {
    1061                 :             :       /* This is for ordinary explicit specialization and partial
    1062                 :             :          specialization of a template class such as:
    1063                 :             : 
    1064                 :             :            template <> class C<int>;
    1065                 :             : 
    1066                 :             :          or:
    1067                 :             : 
    1068                 :             :            template <class T> class C<T*>;
    1069                 :             : 
    1070                 :             :          Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
    1071                 :             : 
    1072                 :    30349123 :       if (maybe_new_partial_specialization (type))
    1073                 :             :         {
    1074                 :    10176658 :           if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type))
    1075                 :    10176658 :               && !at_namespace_scope_p ())
    1076                 :          16 :             return error_mark_node;
    1077                 :    10176642 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1078                 :    10176642 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1079                 :    10176642 :           if (processing_template_decl)
    1080                 :             :             {
    1081                 :     6415209 :               tree decl = push_template_decl (TYPE_MAIN_DECL (type));
    1082                 :     6415209 :               if (decl == error_mark_node)
    1083                 :             :                 return error_mark_node;
    1084                 :     6415119 :               return TREE_TYPE (decl);
    1085                 :             :             }
    1086                 :             :         }
    1087                 :    20172465 :       else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
    1088                 :           0 :         error ("specialization of %qT after instantiation", type);
    1089                 :        6700 :       else if (errorcount && !processing_specialization
    1090                 :        5190 :                 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
    1091                 :    20177655 :                && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
    1092                 :             :         /* Trying to define a specialization either without a template<> header
    1093                 :             :            or in an inappropriate place.  We've already given an error, so just
    1094                 :             :            bail now so we don't actually define the specialization.  */
    1095                 :        1489 :         return error_mark_node;
    1096                 :             :     }
    1097                 :    29126621 :   else if (CLASS_TYPE_P (type)
    1098                 :    29126621 :            && !CLASSTYPE_USE_TEMPLATE (type)
    1099                 :    29126621 :            && CLASSTYPE_TEMPLATE_INFO (type)
    1100                 :    20599620 :            && context && CLASS_TYPE_P (context)
    1101                 :    31927903 :            && CLASSTYPE_TEMPLATE_INFO (context))
    1102                 :             :     {
    1103                 :             :       /* This is for an explicit specialization of member class
    1104                 :             :          template according to [temp.expl.spec/18]:
    1105                 :             : 
    1106                 :             :            template <> template <class U> class C<int>::D;
    1107                 :             : 
    1108                 :             :          The context `C<int>' must be an implicit instantiation.
    1109                 :             :          Otherwise this is just a member class template declared
    1110                 :             :          earlier like:
    1111                 :             : 
    1112                 :             :            template <> class C<int> { template <class U> class D; };
    1113                 :             :            template <> template <class U> class C<int>::D;
    1114                 :             : 
    1115                 :             :          In the first case, `C<int>::D' is a specialization of `C<T>::D'
    1116                 :             :          while in the second case, `C<int>::D' is a primary template
    1117                 :             :          and `C<T>::D' may not exist.  */
    1118                 :             : 
    1119                 :     1615926 :       if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
    1120                 :     1615926 :           && !COMPLETE_TYPE_P (type))
    1121                 :             :         {
    1122                 :          34 :           tree t;
    1123                 :          34 :           tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
    1124                 :             : 
    1125                 :          34 :           if (current_namespace
    1126                 :          34 :               != decl_namespace_context (tmpl))
    1127                 :             :             {
    1128                 :           0 :               if (permerror (input_location,
    1129                 :             :                              "specialization of %qD in different namespace",
    1130                 :             :                              type))
    1131                 :           0 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    1132                 :             :                         "from definition of %q#D", tmpl);
    1133                 :             :             }
    1134                 :             : 
    1135                 :             :           /* Check for invalid specialization after instantiation:
    1136                 :             : 
    1137                 :             :                template <> template <> class C<int>::D<int>;
    1138                 :             :                template <> template <class U> class C<int>::D;  */
    1139                 :             : 
    1140                 :          34 :           for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
    1141                 :          49 :                t; t = TREE_CHAIN (t))
    1142                 :             :             {
    1143                 :          15 :               tree inst = TREE_VALUE (t);
    1144                 :          15 :               if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
    1145                 :          15 :                   || !COMPLETE_OR_OPEN_TYPE_P (inst))
    1146                 :             :                 {
    1147                 :             :                   /* We already have a full specialization of this partial
    1148                 :             :                      instantiation, or a full specialization has been
    1149                 :             :                      looked up but not instantiated.  Reassign it to the
    1150                 :             :                      new member specialization template.  */
    1151                 :          11 :                   spec_entry elt;
    1152                 :          11 :                   spec_entry *entry;
    1153                 :             : 
    1154                 :          11 :                   elt.tmpl = most_general_template (tmpl);
    1155                 :          11 :                   elt.args = CLASSTYPE_TI_ARGS (inst);
    1156                 :          11 :                   elt.spec = inst;
    1157                 :             : 
    1158                 :          11 :                   type_specializations->remove_elt (&elt);
    1159                 :             : 
    1160                 :          11 :                   elt.tmpl = tmpl;
    1161                 :          11 :                   CLASSTYPE_TI_ARGS (inst)
    1162                 :          11 :                     = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
    1163                 :             : 
    1164                 :          11 :                   spec_entry **slot
    1165                 :          11 :                     = type_specializations->find_slot (&elt, INSERT);
    1166                 :          11 :                   entry = ggc_alloc<spec_entry> ();
    1167                 :          11 :                   *entry = elt;
    1168                 :          11 :                   *slot = entry;
    1169                 :             :                 }
    1170                 :             :               else
    1171                 :             :                 /* But if we've had an implicit instantiation, that's a
    1172                 :             :                    problem ([temp.expl.spec]/6).  */
    1173                 :           4 :                 error ("specialization %qT after instantiation %qT",
    1174                 :             :                        type, inst);
    1175                 :             :             }
    1176                 :             : 
    1177                 :             :           /* Mark TYPE as a specialization.  And as a result, we only
    1178                 :             :              have one level of template argument for the innermost
    1179                 :             :              class template.  */
    1180                 :          34 :           SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
    1181                 :          34 :           DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
    1182                 :          34 :           CLASSTYPE_TI_ARGS (type)
    1183                 :          68 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
    1184                 :             :         }
    1185                 :             :     }
    1186                 :    28221845 :   else if (processing_specialization)
    1187                 :             :     {
    1188                 :             :        /* Someday C++0x may allow for enum template specialization.  */
    1189                 :          21 :       if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
    1190                 :          42 :           && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
    1191                 :           9 :         pedwarn (input_location, OPT_Wpedantic, "template specialization "
    1192                 :             :                  "of %qD not allowed by ISO C++", type);
    1193                 :             :       else
    1194                 :             :         {
    1195                 :          15 :           error ("explicit specialization of non-template %qT", type);
    1196                 :          15 :           return error_mark_node;
    1197                 :             :         }
    1198                 :             :     }
    1199                 :             : 
    1200                 :    53770165 :   return type;
    1201                 :             : }
    1202                 :             : 
    1203                 :             : /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
    1204                 :             :    gone through coerce_template_parms by now.  */
    1205                 :             : 
    1206                 :             : static void
    1207                 :   467239764 : verify_unstripped_args_1 (tree inner)
    1208                 :             : {
    1209                 :  1515973369 :   for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
    1210                 :             :     {
    1211                 :  1048733605 :       tree arg = TREE_VEC_ELT (inner, i);
    1212                 :  1048733605 :       if (TREE_CODE (arg) == TEMPLATE_DECL)
    1213                 :             :         /* OK */;
    1214                 :  1047993753 :       else if (TYPE_P (arg))
    1215                 :   973975756 :         gcc_assert (strip_typedefs (arg, NULL) == arg);
    1216                 :    74017997 :       else if (ARGUMENT_PACK_P (arg))
    1217                 :     1681259 :         verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
    1218                 :    72336738 :       else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
    1219                 :             :         /* Allow typedefs on the type of a non-type argument, since a
    1220                 :             :            parameter can have them.  */;
    1221                 :             :       else
    1222                 :    72336566 :         gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
    1223                 :             :     }
    1224                 :   467239764 : }
    1225                 :             : 
    1226                 :             : static void
    1227                 :   522087303 : verify_unstripped_args (tree args)
    1228                 :             : {
    1229                 :   522087303 :   ++processing_template_decl;
    1230                 :   522087303 :   if (!any_dependent_template_arguments_p (args))
    1231                 :   465558505 :     verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
    1232                 :   522087303 :   --processing_template_decl;
    1233                 :   522087303 : }
    1234                 :             : 
    1235                 :             : /* Retrieve the specialization (in the sense of [temp.spec] - a
    1236                 :             :    specialization is either an instantiation or an explicit
    1237                 :             :    specialization) of TMPL for the given template ARGS.  If there is
    1238                 :             :    no such specialization, return NULL_TREE.  The ARGS are a vector of
    1239                 :             :    arguments, or a vector of vectors of arguments, in the case of
    1240                 :             :    templates with more than one level of parameters.
    1241                 :             : 
    1242                 :             :    If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
    1243                 :             :    then we search for a partial specialization matching ARGS.  This
    1244                 :             :    parameter is ignored if TMPL is not a class template.
    1245                 :             : 
    1246                 :             :    We can also look up a FIELD_DECL, if it is a lambda capture pack; the
    1247                 :             :    result is a NONTYPE_ARGUMENT_PACK.  */
    1248                 :             : 
    1249                 :             : static tree
    1250                 :   522087303 : retrieve_specialization (tree tmpl, tree args, hashval_t hash)
    1251                 :             : {
    1252                 :   522087303 :   if (tmpl == NULL_TREE)
    1253                 :             :     return NULL_TREE;
    1254                 :             : 
    1255                 :   522087303 :   if (args == error_mark_node)
    1256                 :             :     return NULL_TREE;
    1257                 :             : 
    1258                 :   522087303 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
    1259                 :             :               || TREE_CODE (tmpl) == FIELD_DECL);
    1260                 :             : 
    1261                 :             :   /* There should be as many levels of arguments as there are
    1262                 :             :      levels of parameters.  */
    1263                 :  1044174606 :   gcc_assert (TMPL_ARGS_DEPTH (args)
    1264                 :             :               == (TREE_CODE (tmpl) == TEMPLATE_DECL
    1265                 :             :                   ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
    1266                 :             :                   : template_class_depth (DECL_CONTEXT (tmpl))));
    1267                 :             : 
    1268                 :   522087303 :   if (flag_checking)
    1269                 :   522087303 :     verify_unstripped_args (args);
    1270                 :             : 
    1271                 :             :   /* Lambda functions in templates aren't instantiated normally, but through
    1272                 :             :      tsubst_lambda_expr.  */
    1273                 :   522087303 :   if (lambda_fn_in_template_p (tmpl))
    1274                 :             :     return NULL_TREE;
    1275                 :             : 
    1276                 :   522087303 :   spec_entry elt;
    1277                 :   522087303 :   elt.tmpl = tmpl;
    1278                 :   522087303 :   elt.args = args;
    1279                 :   522087303 :   elt.spec = NULL_TREE;
    1280                 :             : 
    1281                 :   522087303 :   spec_hash_table *specializations;
    1282                 :   522087303 :   if (DECL_CLASS_TEMPLATE_P (tmpl))
    1283                 :     1474024 :     specializations = type_specializations;
    1284                 :             :   else
    1285                 :   520613279 :     specializations = decl_specializations;
    1286                 :             : 
    1287                 :   522087303 :   if (hash == 0)
    1288                 :   183501346 :     hash = spec_hasher::hash (&elt);
    1289                 :   522087303 :   if (spec_entry *found = specializations->find_with_hash (&elt, hash))
    1290                 :   255829382 :     return found->spec;
    1291                 :             : 
    1292                 :             :   return NULL_TREE;
    1293                 :             : }
    1294                 :             : 
    1295                 :             : /* Like retrieve_specialization, but for local declarations.  */
    1296                 :             : 
    1297                 :             : tree
    1298                 :   143834622 : retrieve_local_specialization (tree tmpl)
    1299                 :             : {
    1300                 :   143834622 :   if (local_specializations == NULL)
    1301                 :             :     return NULL_TREE;
    1302                 :             : 
    1303                 :   140403305 :   tree *slot = local_specializations->get (tmpl);
    1304                 :   140403305 :   return slot ? *slot : NULL_TREE;
    1305                 :             : }
    1306                 :             : 
    1307                 :             : /* Returns nonzero iff DECL is a specialization of TMPL.  */
    1308                 :             : 
    1309                 :             : int
    1310                 :     1097462 : is_specialization_of (tree decl, tree tmpl)
    1311                 :             : {
    1312                 :     1097462 :   tree t;
    1313                 :             : 
    1314                 :     1097462 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    1315                 :             :     {
    1316                 :      412690 :       for (t = decl;
    1317                 :      809403 :            t != NULL_TREE;
    1318                 :      412690 :            t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
    1319                 :      543218 :         if (t == tmpl)
    1320                 :             :           return 1;
    1321                 :             :     }
    1322                 :             :   else
    1323                 :             :     {
    1324                 :      700749 :       gcc_assert (TREE_CODE (decl) == TYPE_DECL);
    1325                 :             : 
    1326                 :      700749 :       for (t = TREE_TYPE (decl);
    1327                 :     1597116 :            t != NULL_TREE;
    1328                 :      896367 :            t = CLASSTYPE_USE_TEMPLATE (t)
    1329                 :      896367 :              ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
    1330                 :     1348751 :         if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
    1331                 :             :           return 1;
    1332                 :             :     }
    1333                 :             : 
    1334                 :             :   return 0;
    1335                 :             : }
    1336                 :             : 
    1337                 :             : /* Returns nonzero iff DECL is a specialization of friend declaration
    1338                 :             :    FRIEND_DECL according to [temp.friend].  */
    1339                 :             : 
    1340                 :             : bool
    1341                 :     1210669 : is_specialization_of_friend (tree decl, tree friend_decl)
    1342                 :             : {
    1343                 :     1210669 :   bool need_template = true;
    1344                 :     1210669 :   int template_depth;
    1345                 :             : 
    1346                 :     1210669 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
    1347                 :             :               || TREE_CODE (decl) == TYPE_DECL);
    1348                 :             : 
    1349                 :             :   /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
    1350                 :             :      of a template class, we want to check if DECL is a specialization
    1351                 :             :      if this.  */
    1352                 :     1210669 :   if (TREE_CODE (friend_decl) == FUNCTION_DECL
    1353                 :      113467 :       && DECL_CLASS_SCOPE_P (friend_decl)
    1354                 :          74 :       && DECL_TEMPLATE_INFO (friend_decl)
    1355                 :     1210741 :       && !DECL_USE_TEMPLATE (friend_decl))
    1356                 :             :     {
    1357                 :             :       /* We want a TEMPLATE_DECL for `is_specialization_of'.  */
    1358                 :          72 :       friend_decl = DECL_TI_TEMPLATE (friend_decl);
    1359                 :          72 :       need_template = false;
    1360                 :             :     }
    1361                 :     1210597 :   else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
    1362                 :     1210597 :            && !PRIMARY_TEMPLATE_P (friend_decl))
    1363                 :             :     need_template = false;
    1364                 :             : 
    1365                 :             :   /* There is nothing to do if this is not a template friend.  */
    1366                 :     1210669 :   if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
    1367                 :             :     return false;
    1368                 :             : 
    1369                 :     1097274 :   if (is_specialization_of (decl, friend_decl))
    1370                 :             :     return true;
    1371                 :             : 
    1372                 :             :   /* [temp.friend/6]
    1373                 :             :      A member of a class template may be declared to be a friend of a
    1374                 :             :      non-template class.  In this case, the corresponding member of
    1375                 :             :      every specialization of the class template is a friend of the
    1376                 :             :      class granting friendship.
    1377                 :             : 
    1378                 :             :      For example, given a template friend declaration
    1379                 :             : 
    1380                 :             :        template <class T> friend void A<T>::f();
    1381                 :             : 
    1382                 :             :      the member function below is considered a friend
    1383                 :             : 
    1384                 :             :        template <> struct A<int> {
    1385                 :             :          void f();
    1386                 :             :        };
    1387                 :             : 
    1388                 :             :      For this type of template friend, TEMPLATE_DEPTH below will be
    1389                 :             :      nonzero.  To determine if DECL is a friend of FRIEND, we first
    1390                 :             :      check if the enclosing class is a specialization of another.  */
    1391                 :             : 
    1392                 :      514546 :   template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
    1393                 :      514546 :   if (template_depth
    1394                 :         196 :       && DECL_CLASS_SCOPE_P (decl)
    1395                 :      514734 :       && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
    1396                 :         188 :                                CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
    1397                 :             :     {
    1398                 :             :       /* Next, we check the members themselves.  In order to handle
    1399                 :             :          a few tricky cases, such as when FRIEND_DECL's are
    1400                 :             : 
    1401                 :             :            template <class T> friend void A<T>::g(T t);
    1402                 :             :            template <class T> template <T t> friend void A<T>::h();
    1403                 :             : 
    1404                 :             :          and DECL's are
    1405                 :             : 
    1406                 :             :            void A<int>::g(int);
    1407                 :             :            template <int> void A<int>::h();
    1408                 :             : 
    1409                 :             :          we need to figure out ARGS, the template arguments from
    1410                 :             :          the context of DECL.  This is required for template substitution
    1411                 :             :          of `T' in the function parameter of `g' and template parameter
    1412                 :             :          of `h' in the above examples.  Here ARGS corresponds to `int'.  */
    1413                 :             : 
    1414                 :         184 :       tree context = DECL_CONTEXT (decl);
    1415                 :         184 :       tree args = NULL_TREE;
    1416                 :         184 :       int current_depth = 0;
    1417                 :             : 
    1418                 :         368 :       while (current_depth < template_depth)
    1419                 :             :         {
    1420                 :         184 :           if (CLASSTYPE_TEMPLATE_INFO (context))
    1421                 :             :             {
    1422                 :         184 :               if (current_depth == 0)
    1423                 :         368 :                 args = TYPE_TI_ARGS (context);
    1424                 :             :               else
    1425                 :           0 :                 args = add_to_template_args (TYPE_TI_ARGS (context), args);
    1426                 :         184 :               current_depth++;
    1427                 :             :             }
    1428                 :         184 :           context = TYPE_CONTEXT (context);
    1429                 :             :         }
    1430                 :             : 
    1431                 :         184 :       if (TREE_CODE (decl) == FUNCTION_DECL)
    1432                 :             :         {
    1433                 :          96 :           bool is_template;
    1434                 :          96 :           tree friend_type;
    1435                 :          96 :           tree decl_type;
    1436                 :          96 :           tree friend_args_type;
    1437                 :          96 :           tree decl_args_type;
    1438                 :             : 
    1439                 :             :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1440                 :             :              non-templates.  */
    1441                 :          96 :           is_template = DECL_TEMPLATE_INFO (decl)
    1442                 :          96 :                         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
    1443                 :          96 :           if (need_template ^ is_template)
    1444                 :             :             return false;
    1445                 :          72 :           else if (is_template)
    1446                 :             :             {
    1447                 :             :               /* If both are templates, check template parameter list.  */
    1448                 :          36 :               tree friend_parms
    1449                 :          36 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1450                 :             :                                          args, tf_none);
    1451                 :          36 :               if (!comp_template_parms
    1452                 :          36 :                      (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
    1453                 :             :                       friend_parms))
    1454                 :             :                 return false;
    1455                 :             : 
    1456                 :          24 :               decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
    1457                 :             :             }
    1458                 :             :           else
    1459                 :          36 :             decl_type = TREE_TYPE (decl);
    1460                 :             : 
    1461                 :          60 :           friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
    1462                 :             :                                               tf_none, NULL_TREE);
    1463                 :          60 :           if (friend_type == error_mark_node)
    1464                 :             :             return false;
    1465                 :             : 
    1466                 :             :           /* Check if return types match.  */
    1467                 :          60 :           if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
    1468                 :             :             return false;
    1469                 :             : 
    1470                 :             :           /* Check if function parameter types match, ignoring the
    1471                 :             :              `this' parameter.  */
    1472                 :          52 :           friend_args_type = TYPE_ARG_TYPES (friend_type);
    1473                 :          52 :           decl_args_type = TYPE_ARG_TYPES (decl_type);
    1474                 :          52 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl))
    1475                 :          52 :             friend_args_type = TREE_CHAIN (friend_args_type);
    1476                 :          52 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    1477                 :          52 :             decl_args_type = TREE_CHAIN (decl_args_type);
    1478                 :             : 
    1479                 :          52 :           return compparms (decl_args_type, friend_args_type);
    1480                 :             :         }
    1481                 :             :       else
    1482                 :             :         {
    1483                 :             :           /* DECL is a TYPE_DECL */
    1484                 :          88 :           bool is_template;
    1485                 :          88 :           tree decl_type = TREE_TYPE (decl);
    1486                 :             : 
    1487                 :             :           /* Make sure that both DECL and FRIEND_DECL are templates or
    1488                 :             :              non-templates.  */
    1489                 :          88 :           is_template
    1490                 :          88 :             = CLASSTYPE_TEMPLATE_INFO (decl_type)
    1491                 :          88 :               && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
    1492                 :             : 
    1493                 :          88 :           if (need_template ^ is_template)
    1494                 :             :             return false;
    1495                 :          88 :           else if (is_template)
    1496                 :             :             {
    1497                 :          68 :               tree friend_parms;
    1498                 :             :               /* If both are templates, check the name of the two
    1499                 :             :                  TEMPLATE_DECL's first because is_friend didn't.  */
    1500                 :          68 :               if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
    1501                 :          68 :                   != DECL_NAME (friend_decl))
    1502                 :             :                 return false;
    1503                 :             : 
    1504                 :             :               /* Now check template parameter list.  */
    1505                 :          64 :               friend_parms
    1506                 :          64 :                 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
    1507                 :             :                                          args, tf_none);
    1508                 :          64 :               return comp_template_parms
    1509                 :          64 :                 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
    1510                 :          64 :                  friend_parms);
    1511                 :             :             }
    1512                 :             :           else
    1513                 :          20 :             return (DECL_NAME (decl)
    1514                 :          20 :                     == DECL_NAME (friend_decl));
    1515                 :             :         }
    1516                 :             :     }
    1517                 :             :   return false;
    1518                 :             : }
    1519                 :             : 
    1520                 :             : /* Register the specialization SPEC as a specialization of TMPL with
    1521                 :             :    the indicated ARGS.  IS_FRIEND indicates whether the specialization
    1522                 :             :    is actually just a friend declaration.  ATTRLIST is the list of
    1523                 :             :    attributes that the specialization is declared with or NULL when
    1524                 :             :    it isn't.  Returns SPEC, or an equivalent prior declaration, if
    1525                 :             :    available.
    1526                 :             : 
    1527                 :             :    We also store instantiations of field packs in the hash table, even
    1528                 :             :    though they are not themselves templates, to make lookup easier.  */
    1529                 :             : 
    1530                 :             : static tree
    1531                 :   197446011 : register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
    1532                 :             :                          hashval_t hash)
    1533                 :             : {
    1534                 :   197446011 :   tree fn;
    1535                 :             : 
    1536                 :   197446011 :   gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
    1537                 :             :               || (TREE_CODE (tmpl) == FIELD_DECL
    1538                 :             :                   && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
    1539                 :             : 
    1540                 :   197446011 :   spec_entry elt;
    1541                 :   197446011 :   elt.tmpl = tmpl;
    1542                 :   197446011 :   elt.args = args;
    1543                 :   197446011 :   elt.spec = spec;
    1544                 :             : 
    1545                 :   197446011 :   if (hash == 0)
    1546                 :     1351141 :     hash = spec_hasher::hash (&elt);
    1547                 :             : 
    1548                 :   197446011 :   spec_entry **slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
    1549                 :   197446011 :   if (*slot)
    1550                 :      543895 :     fn = (*slot)->spec;
    1551                 :             :   else
    1552                 :             :     fn = NULL_TREE;
    1553                 :             : 
    1554                 :             :   /* We can sometimes try to re-register a specialization that we've
    1555                 :             :      already got.  In particular, regenerate_decl_from_template calls
    1556                 :             :      duplicate_decls which will update the specialization list.  But,
    1557                 :             :      we'll still get called again here anyhow.  It's more convenient
    1558                 :             :      to simply allow this than to try to prevent it.  */
    1559                 :   197446011 :   if (fn == spec)
    1560                 :             :     return spec;
    1561                 :   197445074 :   else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
    1562                 :             :     {
    1563                 :      542878 :       if (DECL_TEMPLATE_INSTANTIATION (fn))
    1564                 :             :         {
    1565                 :      542621 :           if (DECL_ODR_USED (fn)
    1566                 :      542621 :               || DECL_EXPLICIT_INSTANTIATION (fn))
    1567                 :             :             {
    1568                 :          10 :               error ("specialization of %qD after instantiation",
    1569                 :             :                      fn);
    1570                 :          10 :               return error_mark_node;
    1571                 :             :             }
    1572                 :             :           else
    1573                 :             :             {
    1574                 :      542611 :               tree clone;
    1575                 :             :               /* This situation should occur only if the first
    1576                 :             :                  specialization is an implicit instantiation, the
    1577                 :             :                  second is an explicit specialization, and the
    1578                 :             :                  implicit instantiation has not yet been used.  That
    1579                 :             :                  situation can occur if we have implicitly
    1580                 :             :                  instantiated a member function and then specialized
    1581                 :             :                  it later.
    1582                 :             : 
    1583                 :             :                  We can also wind up here if a friend declaration that
    1584                 :             :                  looked like an instantiation turns out to be a
    1585                 :             :                  specialization:
    1586                 :             : 
    1587                 :             :                    template <class T> void foo(T);
    1588                 :             :                    class S { friend void foo<>(int) };
    1589                 :             :                    template <> void foo(int);
    1590                 :             : 
    1591                 :             :                  We transform the existing DECL in place so that any
    1592                 :             :                  pointers to it become pointers to the updated
    1593                 :             :                  declaration.
    1594                 :             : 
    1595                 :             :                  If there was a definition for the template, but not
    1596                 :             :                  for the specialization, we want this to look as if
    1597                 :             :                  there were no definition, and vice versa.  */
    1598                 :      542611 :               DECL_INITIAL (fn) = NULL_TREE;
    1599                 :      542611 :               duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1600                 :             : 
    1601                 :             :               /* The call to duplicate_decls will have applied
    1602                 :             :                  [temp.expl.spec]:
    1603                 :             : 
    1604                 :             :                    An explicit specialization of a function template
    1605                 :             :                    is inline only if it is explicitly declared to be,
    1606                 :             :                    and independently of whether its function template
    1607                 :             :                    is.
    1608                 :             : 
    1609                 :             :                 to the primary function; now copy the inline bits to
    1610                 :             :                 the various clones.  */
    1611                 :      725214 :               FOR_EACH_CLONE (clone, fn)
    1612                 :             :                 {
    1613                 :      182603 :                   DECL_DECLARED_INLINE_P (clone)
    1614                 :      182603 :                     = DECL_DECLARED_INLINE_P (fn);
    1615                 :      365206 :                   DECL_SOURCE_LOCATION (clone)
    1616                 :      182603 :                     = DECL_SOURCE_LOCATION (fn);
    1617                 :      182603 :                   DECL_DELETED_FN (clone)
    1618                 :      365206 :                     = DECL_DELETED_FN (fn);
    1619                 :             :                 }
    1620                 :      542611 :               check_specialization_namespace (tmpl);
    1621                 :             : 
    1622                 :      542611 :               return fn;
    1623                 :             :             }
    1624                 :             :         }
    1625                 :         257 :       else if (DECL_TEMPLATE_SPECIALIZATION (fn))
    1626                 :             :         {
    1627                 :         257 :           tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1628                 :         257 :           if (dd == error_mark_node)
    1629                 :             :             /* We've already complained in duplicate_decls.  */
    1630                 :             :             return error_mark_node;
    1631                 :             : 
    1632                 :         235 :           if (dd == NULL_TREE && DECL_INITIAL (spec))
    1633                 :             :             /* Dup decl failed, but this is a new definition. Set the
    1634                 :             :                line number so any errors match this new
    1635                 :             :                definition.  */
    1636                 :           0 :             DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
    1637                 :             : 
    1638                 :         235 :           return fn;
    1639                 :             :         }
    1640                 :             :     }
    1641                 :   196902196 :   else if (fn)
    1642                 :          80 :     return duplicate_decls (spec, fn, /*hiding=*/is_friend);
    1643                 :             : 
    1644                 :             :   /* A specialization must be declared in the same namespace as the
    1645                 :             :      template it is specializing.  */
    1646                 :   196902116 :   if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
    1647                 :   197709359 :       && !check_specialization_namespace (tmpl))
    1648                 :           0 :     DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
    1649                 :             : 
    1650                 :   196902116 :   spec_entry *entry = ggc_alloc<spec_entry> ();
    1651                 :   196902116 :   gcc_assert (tmpl && args && spec);
    1652                 :   196902116 :   *entry = elt;
    1653                 :   196902116 :   *slot = entry;
    1654                 :    89667252 :   if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
    1655                 :    16364523 :        && PRIMARY_TEMPLATE_P (tmpl)
    1656                 :    15734136 :        && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
    1657                 :   280309487 :       || variable_template_p (tmpl))
    1658                 :             :     /* If TMPL is a forward declaration of a template function, keep a list
    1659                 :             :        of all specializations in case we need to reassign them to a friend
    1660                 :             :        template later in tsubst_friend_function.
    1661                 :             : 
    1662                 :             :        Also keep a list of all variable template instantiations so that
    1663                 :             :        process_partial_specialization can check whether a later partial
    1664                 :             :        specialization would have used it.  */
    1665                 :    20096508 :     DECL_TEMPLATE_INSTANTIATIONS (tmpl)
    1666                 :    20096508 :       = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
    1667                 :             : 
    1668                 :             :   return spec;
    1669                 :             : }
    1670                 :             : 
    1671                 :             : /* Restricts tree and type comparisons.  */
    1672                 :             : int comparing_specializations;
    1673                 :             : int comparing_dependent_aliases;
    1674                 :             : 
    1675                 :             : /* Whether we are comparing template arguments during partial ordering
    1676                 :             :    (and therefore want the comparison to look through dependent alias
    1677                 :             :    template specializations).  */
    1678                 :             : 
    1679                 :             : static int comparing_for_partial_ordering;
    1680                 :             : 
    1681                 :             : /* Returns true iff two spec_entry nodes are equivalent.  */
    1682                 :             : 
    1683                 :             : bool
    1684                 :  8563778124 : spec_hasher::equal (spec_entry *e1, spec_entry *e2)
    1685                 :             : {
    1686                 :  8563778124 :   int equal;
    1687                 :             : 
    1688                 :  8563778124 :   ++comparing_specializations;
    1689                 :  8563778124 :   ++comparing_dependent_aliases;
    1690                 :  8563778124 :   ++processing_template_decl;
    1691                 :  9120223611 :   equal = (e1->tmpl == e2->tmpl
    1692                 :  8563778124 :            && comp_template_args (e1->args, e2->args));
    1693                 :   556445487 :   if (equal && flag_concepts
    1694                 :             :       /* tmpl could be a FIELD_DECL for a capture pack.  */
    1695                 :   209026181 :       && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
    1696                 :   209026181 :       && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
    1697                 :     6628358 :       && uses_template_parms (e1->args))
    1698                 :             :     {
    1699                 :             :       /* Partial specializations of a variable template can be distinguished by
    1700                 :             :          constraints.  */
    1701                 :         315 :       tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
    1702                 :         315 :       tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
    1703                 :         315 :       equal = equivalent_constraints (c1, c2);
    1704                 :             :     }
    1705                 :  8563778124 :   --processing_template_decl;
    1706                 :  8563778124 :   --comparing_dependent_aliases;
    1707                 :  8563778124 :   --comparing_specializations;
    1708                 :             : 
    1709                 :  8563778124 :   return equal;
    1710                 :             : }
    1711                 :             : 
    1712                 :             : /* Returns a hash for a template TMPL and template arguments ARGS.  */
    1713                 :             : 
    1714                 :             : static hashval_t
    1715                 :  7716008122 : hash_tmpl_and_args (tree tmpl, tree args)
    1716                 :             : {
    1717                 :  7716008122 :   hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
    1718                 :  7716008122 :   return iterative_hash_template_arg (args, val);
    1719                 :             : }
    1720                 :             : 
    1721                 :             : hashval_t
    1722                 :  7678578356 : spec_hasher::hash (tree tmpl, tree args)
    1723                 :             : {
    1724                 :  7678578356 :   ++comparing_specializations;
    1725                 :  7678578356 :   hashval_t val = hash_tmpl_and_args (tmpl, args);
    1726                 :  7678578356 :   --comparing_specializations;
    1727                 :  7678578356 :   return val;
    1728                 :             : }
    1729                 :             : 
    1730                 :             : /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
    1731                 :             :    ignoring SPEC.  */
    1732                 :             : 
    1733                 :             : hashval_t
    1734                 :  7372349002 : spec_hasher::hash (spec_entry *e)
    1735                 :             : {
    1736                 :  7372349002 :   return spec_hasher::hash (e->tmpl, e->args);
    1737                 :             : }
    1738                 :             : 
    1739                 :             : /* Recursively calculate a hash value for a template argument ARG, for use
    1740                 :             :    in the hash tables of template specializations.   We must be
    1741                 :             :    careful to (at least) skip the same entities template_args_equal
    1742                 :             :    does.  */
    1743                 :             : 
    1744                 :             : hashval_t
    1745                 : 38329686249 : iterative_hash_template_arg (tree arg, hashval_t val)
    1746                 :             : {
    1747                 : 38329686249 :   if (arg == NULL_TREE)
    1748                 :   925768288 :     return iterative_hash_object (arg, val);
    1749                 :             : 
    1750                 : 37403917961 :   if (!TYPE_P (arg))
    1751                 :             :     /* Strip nop-like things, but not the same as STRIP_NOPS.  */
    1752                 : 16076354207 :     while (CONVERT_EXPR_P (arg)
    1753                 :             :            || TREE_CODE (arg) == NON_LVALUE_EXPR
    1754                 : 16076354207 :            || class_nttp_const_wrapper_p (arg))
    1755                 :     2661500 :       arg = TREE_OPERAND (arg, 0);
    1756                 :             : 
    1757                 : 37403917961 :   enum tree_code code = TREE_CODE (arg);
    1758                 :             : 
    1759                 : 37403917961 :   val = iterative_hash_object (code, val);
    1760                 :             : 
    1761                 : 37403917961 :   switch (code)
    1762                 :             :     {
    1763                 :           0 :     case ARGUMENT_PACK_SELECT:
    1764                 :             :       /* Getting here with an ARGUMENT_PACK_SELECT means we're probably
    1765                 :             :          preserving it in a hash table, which is bad because it will change
    1766                 :             :          meaning when gen_elem_of_pack_expansion_instantiation changes the
    1767                 :             :          ARGUMENT_PACK_SELECT_INDEX.  */
    1768                 :           0 :       gcc_unreachable ();
    1769                 :             : 
    1770                 :             :     case ERROR_MARK:
    1771                 :             :       return val;
    1772                 :             : 
    1773                 :  1379462012 :     case IDENTIFIER_NODE:
    1774                 :  1379462012 :       return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
    1775                 :             : 
    1776                 : 10866808166 :     case TREE_VEC:
    1777                 : 31910852157 :       for (tree elt : tree_vec_range (arg))
    1778                 : 21044043991 :         val = iterative_hash_template_arg (elt, val);
    1779                 : 10866808166 :       return val;
    1780                 :             : 
    1781                 :   385071452 :     case TYPE_PACK_EXPANSION:
    1782                 :   385071452 :     case EXPR_PACK_EXPANSION:
    1783                 :   385071452 :       val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
    1784                 :   385071452 :       return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
    1785                 :             : 
    1786                 :   996404469 :     case TYPE_ARGUMENT_PACK:
    1787                 :   996404469 :     case NONTYPE_ARGUMENT_PACK:
    1788                 :   996404469 :       return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
    1789                 :             : 
    1790                 :             :     case TREE_LIST:
    1791                 :    42967308 :       for (; arg; arg = TREE_CHAIN (arg))
    1792                 :    21483663 :         val = iterative_hash_template_arg (TREE_VALUE (arg), val);
    1793                 :             :       return val;
    1794                 :             : 
    1795                 :         387 :     case OVERLOAD:
    1796                 :         786 :       for (lkp_iterator iter (arg); iter; ++iter)
    1797                 :         399 :         val = iterative_hash_template_arg (*iter, val);
    1798                 :         387 :       return val;
    1799                 :             : 
    1800                 :     2687930 :     case CONSTRUCTOR:
    1801                 :     2687930 :       {
    1802                 :     2687930 :         iterative_hash_template_arg (TREE_TYPE (arg), val);
    1803                 :     3382310 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
    1804                 :             :           {
    1805                 :      269218 :             val = iterative_hash_template_arg (e.index, val);
    1806                 :      269218 :             val = iterative_hash_template_arg (e.value, val);
    1807                 :             :           }
    1808                 :             :         return val;
    1809                 :             :       }
    1810                 :             : 
    1811                 :     1686764 :     case PARM_DECL:
    1812                 :     1686764 :       if (!DECL_ARTIFICIAL (arg))
    1813                 :             :         {
    1814                 :     1504497 :           val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
    1815                 :     1504497 :           val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
    1816                 :             :         }
    1817                 :     1686764 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1818                 :             : 
    1819                 :   372285723 :     case TEMPLATE_DECL:
    1820                 :   372285723 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
    1821                 :     8435512 :         return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1822                 :             :       break;
    1823                 :             : 
    1824                 :           0 :     case TARGET_EXPR:
    1825                 :           0 :       return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
    1826                 :             : 
    1827                 :       24174 :     case PTRMEM_CST:
    1828                 :       24174 :       val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
    1829                 :       24174 :       return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
    1830                 :             : 
    1831                 :   300611743 :     case TEMPLATE_PARM_INDEX:
    1832                 :   300611743 :       val = iterative_hash_template_arg
    1833                 :   300611743 :         (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
    1834                 :   300611743 :       val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
    1835                 :   300611743 :       return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
    1836                 :             : 
    1837                 :    45738052 :     case TRAIT_EXPR:
    1838                 :    45738052 :       val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
    1839                 :    45738052 :       val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
    1840                 :    45738052 :       return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
    1841                 :             : 
    1842                 :    34230508 :     case BASELINK:
    1843                 :    34230508 :       val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
    1844                 :             :                                          val);
    1845                 :    34230508 :       return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
    1846                 :    34230508 :                                           val);
    1847                 :             : 
    1848                 :         921 :     case MODOP_EXPR:
    1849                 :         921 :       val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
    1850                 :         921 :       code = TREE_CODE (TREE_OPERAND (arg, 1));
    1851                 :         921 :       val = iterative_hash_object (code, val);
    1852                 :         921 :       return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
    1853                 :             : 
    1854                 :          11 :     case LAMBDA_EXPR:
    1855                 :             :       /* [temp.over.link] Two lambda-expressions are never considered
    1856                 :             :          equivalent.
    1857                 :             : 
    1858                 :             :          So just hash the closure type.  */
    1859                 :          11 :       return iterative_hash_template_arg (TREE_TYPE (arg), val);
    1860                 :             : 
    1861                 :   181468895 :     case CAST_EXPR:
    1862                 :   181468895 :     case IMPLICIT_CONV_EXPR:
    1863                 :   181468895 :     case STATIC_CAST_EXPR:
    1864                 :   181468895 :     case REINTERPRET_CAST_EXPR:
    1865                 :   181468895 :     case CONST_CAST_EXPR:
    1866                 :   181468895 :     case DYNAMIC_CAST_EXPR:
    1867                 :   181468895 :     case NEW_EXPR:
    1868                 :   181468895 :       val = iterative_hash_template_arg (TREE_TYPE (arg), val);
    1869                 :             :       /* Now hash operands as usual.  */
    1870                 :   181468895 :       break;
    1871                 :             : 
    1872                 :   470929382 :     case CALL_EXPR:
    1873                 :   470929382 :       {
    1874                 :   470929382 :         tree fn = CALL_EXPR_FN (arg);
    1875                 :   470929382 :         if (tree name = call_expr_dependent_name (arg))
    1876                 :             :           {
    1877                 :   345515295 :             if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
    1878                 :   296421608 :               val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
    1879                 :             :             fn = name;
    1880                 :             :           }
    1881                 :   470929382 :         val = iterative_hash_template_arg (fn, val);
    1882                 :   470929382 :         call_expr_arg_iterator ai;
    1883                 :   581542143 :         for (tree x = first_call_expr_arg (arg, &ai); x;
    1884                 :   110612761 :              x = next_call_expr_arg (&ai))
    1885                 :   110612761 :           val = iterative_hash_template_arg (x, val);
    1886                 :   470929382 :         return val;
    1887                 :             :       }
    1888                 :             : 
    1889                 :             :     default:
    1890                 :             :       break;
    1891                 :             :     }
    1892                 :             : 
    1893                 : 22890342811 :   char tclass = TREE_CODE_CLASS (code);
    1894                 : 22890342811 :   switch (tclass)
    1895                 :             :     {
    1896                 : 19996442240 :     case tcc_type:
    1897                 : 19996442240 :       if (tree ats = alias_template_specialization_p (arg, nt_transparent))
    1898                 :             :         {
    1899                 :             :           // We want an alias specialization that survived strip_typedefs
    1900                 :             :           // to hash differently from its TYPE_CANONICAL, to avoid hash
    1901                 :             :           // collisions that compare as different in template_args_equal.
    1902                 :             :           // These could be dependent specializations that strip_typedefs
    1903                 :             :           // left alone, or untouched specializations because
    1904                 :             :           // coerce_template_parms returns the unconverted template
    1905                 :             :           // arguments if it sees incomplete argument packs.
    1906                 :    37429766 :           tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
    1907                 :    37429766 :           return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
    1908                 :             :         }
    1909                 :             : 
    1910                 : 19959012474 :       switch (code)
    1911                 :             :         {
    1912                 :   172519489 :         case  DECLTYPE_TYPE:
    1913                 :   172519489 :           val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
    1914                 :   172519489 :           break;
    1915                 :             : 
    1916                 :   603362396 :         case TYPENAME_TYPE:
    1917                 :   603362396 :           if (comparing_specializations)
    1918                 :             :             {
    1919                 :             :               /* Hash the components that are relevant to TYPENAME_TYPE
    1920                 :             :                  equivalence as determined by structural_comptypes.  We
    1921                 :             :                  can only coherently do this when comparing_specializations
    1922                 :             :                  is set, because otherwise structural_comptypes tries
    1923                 :             :                  resolving TYPENAME_TYPE via the current instantiation.  */
    1924                 :   590746212 :               tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg));
    1925                 :   590746212 :               tree fullname = TYPENAME_TYPE_FULLNAME (arg);
    1926                 :   590746212 :               val = iterative_hash_template_arg (context, val);
    1927                 :   590746212 :               val = iterative_hash_template_arg (fullname, val);
    1928                 :             :             }
    1929                 :             :           break;
    1930                 :             : 
    1931                 : 19183130589 :         default:
    1932                 : 19183130589 :           if (tree canonical = TYPE_CANONICAL (arg))
    1933                 : 18984536721 :             val = iterative_hash_object (TYPE_HASH (canonical), val);
    1934                 :   198593868 :           else if (tree ti = TYPE_TEMPLATE_INFO (arg))
    1935                 :             :             {
    1936                 :     2966316 :               val = iterative_hash_template_arg (TI_TEMPLATE (ti), val);
    1937                 :     2966316 :               val = iterative_hash_template_arg (TI_ARGS (ti), val);
    1938                 :             :             }
    1939                 :             :           break;
    1940                 :             :         }
    1941                 :             : 
    1942                 :             :       return val;
    1943                 :             : 
    1944                 :  1588421372 :     case tcc_declaration:
    1945                 :  1588421372 :     case tcc_constant:
    1946                 :  1588421372 :       return iterative_hash_expr (arg, val);
    1947                 :             : 
    1948                 :  1305479199 :     default:
    1949                 :  1305479199 :       gcc_assert (IS_EXPR_CODE_CLASS (tclass));
    1950                 :  3496824790 :       for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
    1951                 :  2191345591 :         val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
    1952                 :             :       return val;
    1953                 :             :     }
    1954                 :             : }
    1955                 :             : 
    1956                 :             : /* Unregister the specialization SPEC as a specialization of TMPL.
    1957                 :             :    Replace it with NEW_SPEC, if NEW_SPEC is non-NULL.  Returns true
    1958                 :             :    if the SPEC was listed as a specialization of TMPL.
    1959                 :             : 
    1960                 :             :    Note that SPEC has been ggc_freed, so we can't look inside it.  */
    1961                 :             : 
    1962                 :             : bool
    1963                 :      553789 : reregister_specialization (tree spec, tree tinfo, tree new_spec)
    1964                 :             : {
    1965                 :      553789 :   spec_entry *entry;
    1966                 :      553789 :   spec_entry elt;
    1967                 :             : 
    1968                 :      553789 :   elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
    1969                 :      553789 :   elt.args = TI_ARGS (tinfo);
    1970                 :      553789 :   elt.spec = NULL_TREE;
    1971                 :             : 
    1972                 :      553789 :   entry = decl_specializations->find (&elt);
    1973                 :      553789 :   if (entry != NULL)
    1974                 :             :     {
    1975                 :      553789 :       gcc_assert (entry->spec == spec || entry->spec == new_spec);
    1976                 :      553789 :       gcc_assert (new_spec != NULL_TREE);
    1977                 :      553789 :       entry->spec = new_spec;
    1978                 :      553789 :       return 1;
    1979                 :             :     }
    1980                 :             : 
    1981                 :             :   return 0;
    1982                 :             : }
    1983                 :             : 
    1984                 :             : /* Like register_specialization, but for local declarations.  We are
    1985                 :             :    registering SPEC, an instantiation of TMPL.  */
    1986                 :             : 
    1987                 :             : void
    1988                 :    52613561 : register_local_specialization (tree spec, tree tmpl)
    1989                 :             : {
    1990                 :    52613561 :   gcc_assert (tmpl != spec);
    1991                 :    52613561 :   local_specializations->put (tmpl, spec);
    1992                 :    52613561 : }
    1993                 :             : 
    1994                 :             : /* Registers T as a specialization of itself.  This is used to preserve
    1995                 :             :    the references to already-parsed parameters when instantiating
    1996                 :             :    postconditions.  */
    1997                 :             : 
    1998                 :             : void
    1999                 :          51 : register_local_identity (tree t)
    2000                 :             : {
    2001                 :          51 :   local_specializations->put (t, t);
    2002                 :          51 : }
    2003                 :             : 
    2004                 :             : /* TYPE is a class type.  Returns true if TYPE is an explicitly
    2005                 :             :    specialized class.  */
    2006                 :             : 
    2007                 :             : bool
    2008                 :    17414026 : explicit_class_specialization_p (tree type)
    2009                 :             : {
    2010                 :    17414026 :   if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
    2011                 :             :     return false;
    2012                 :      473066 :   return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
    2013                 :             : }
    2014                 :             : 
    2015                 :             : /* Print the list of functions at FNS, going through all the overloads
    2016                 :             :    for each element of the list.  Alternatively, FNS cannot be a
    2017                 :             :    TREE_LIST, in which case it will be printed together with all the
    2018                 :             :    overloads.
    2019                 :             : 
    2020                 :             :    MORE and *STR should respectively be FALSE and NULL when the function
    2021                 :             :    is called from the outside.  They are used internally on recursive
    2022                 :             :    calls.  print_candidates manages the two parameters and leaves NULL
    2023                 :             :    in *STR when it ends.  */
    2024                 :             : 
    2025                 :             : static void
    2026                 :        1230 : print_candidates_1 (tree fns, char **str, bool more = false)
    2027                 :             : {
    2028                 :        1230 :   if (TREE_CODE (fns) == TREE_LIST)
    2029                 :         824 :     for (; fns; fns = TREE_CHAIN (fns))
    2030                 :         844 :       print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
    2031                 :             :   else
    2032                 :        2291 :     for (lkp_iterator iter (fns); iter;)
    2033                 :             :       {
    2034                 :        1329 :         tree cand = *iter;
    2035                 :        1329 :         ++iter;
    2036                 :             : 
    2037                 :        1329 :         const char *pfx = *str;
    2038                 :        1329 :         if (!pfx)
    2039                 :             :           {
    2040                 :         674 :             if (more || iter)
    2041                 :         411 :               pfx = _("candidates are:");
    2042                 :             :             else
    2043                 :         263 :               pfx = _("candidate is:");
    2044                 :         674 :             *str = get_spaces (pfx);
    2045                 :             :           }
    2046                 :        1329 :         inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
    2047                 :             :       }
    2048                 :        1230 : }
    2049                 :             : 
    2050                 :             : /* Print the list of candidate FNS in an error message.  FNS can also
    2051                 :             :    be a TREE_LIST of non-functions in the case of an ambiguous lookup.  */
    2052                 :             : 
    2053                 :             : void
    2054                 :         674 : print_candidates (tree fns)
    2055                 :             : {
    2056                 :         674 :   char *str = NULL;
    2057                 :         674 :   print_candidates_1 (fns, &str);
    2058                 :         674 :   free (str);
    2059                 :         674 : }
    2060                 :             : 
    2061                 :             : /* Get a (possibly) constrained template declaration for the
    2062                 :             :    purpose of ordering candidates.  */
    2063                 :             : static tree
    2064                 :          16 : get_template_for_ordering (tree list)
    2065                 :             : {
    2066                 :          16 :   gcc_assert (TREE_CODE (list) == TREE_LIST);
    2067                 :          16 :   tree f = TREE_VALUE (list);
    2068                 :          16 :   if (tree ti = DECL_TEMPLATE_INFO (f))
    2069                 :          16 :     return TI_TEMPLATE (ti);
    2070                 :             :   return f;
    2071                 :             : }
    2072                 :             : 
    2073                 :             : /* Among candidates having the same signature, return the
    2074                 :             :    most constrained or NULL_TREE if there is no best candidate.
    2075                 :             :    If the signatures of candidates vary (e.g., template
    2076                 :             :    specialization vs. member function), then there can be no
    2077                 :             :    most constrained.
    2078                 :             : 
    2079                 :             :    Note that we don't compare constraints on the functions
    2080                 :             :    themselves, but rather those of their templates. */
    2081                 :             : static tree
    2082                 :           4 : most_constrained_function (tree candidates)
    2083                 :             : {
    2084                 :             :   // Try to find the best candidate in a first pass.
    2085                 :           4 :   tree champ = candidates;
    2086                 :           8 :   for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
    2087                 :             :     {
    2088                 :           4 :       int winner = more_constrained (get_template_for_ordering (champ),
    2089                 :             :                                      get_template_for_ordering (c));
    2090                 :           4 :       if (winner == -1)
    2091                 :             :         champ = c; // The candidate is more constrained
    2092                 :           0 :       else if (winner == 0)
    2093                 :             :         return NULL_TREE; // Neither is more constrained
    2094                 :             :     }
    2095                 :             : 
    2096                 :             :   // Verify that the champ is better than previous candidates.
    2097                 :           8 :   for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
    2098                 :           4 :     if (!more_constrained (get_template_for_ordering (champ),
    2099                 :             :                            get_template_for_ordering (c)))
    2100                 :             :       return NULL_TREE;
    2101                 :             :   }
    2102                 :             : 
    2103                 :             :   return champ;
    2104                 :             : }
    2105                 :             : 
    2106                 :             : 
    2107                 :             : /* Returns the template (one of the functions given by TEMPLATE_ID)
    2108                 :             :    which can be specialized to match the indicated DECL with the
    2109                 :             :    explicit template args given in TEMPLATE_ID.  The DECL may be
    2110                 :             :    NULL_TREE if none is available.  In that case, the functions in
    2111                 :             :    TEMPLATE_ID are non-members.
    2112                 :             : 
    2113                 :             :    If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
    2114                 :             :    specialization of a member template.
    2115                 :             : 
    2116                 :             :    The TEMPLATE_COUNT is the number of references to qualifying
    2117                 :             :    template classes that appeared in the name of the function. See
    2118                 :             :    check_explicit_specialization for a more accurate description.
    2119                 :             : 
    2120                 :             :    TSK indicates what kind of template declaration (if any) is being
    2121                 :             :    declared.  TSK_TEMPLATE indicates that the declaration given by
    2122                 :             :    DECL, though a FUNCTION_DECL, has template parameters, and is
    2123                 :             :    therefore a template function.
    2124                 :             : 
    2125                 :             :    The template args (those explicitly specified and those deduced)
    2126                 :             :    are output in a newly created vector *TARGS_OUT.
    2127                 :             : 
    2128                 :             :    If it is impossible to determine the result, an error message is
    2129                 :             :    issued.  The error_mark_node is returned to indicate failure.  */
    2130                 :             : 
    2131                 :             : static tree
    2132                 :     3283343 : determine_specialization (tree template_id,
    2133                 :             :                           tree decl,
    2134                 :             :                           tree* targs_out,
    2135                 :             :                           int need_member_template,
    2136                 :             :                           int template_count,
    2137                 :             :                           tmpl_spec_kind tsk)
    2138                 :             : {
    2139                 :     3283343 :   tree fns;
    2140                 :     3283343 :   tree targs;
    2141                 :     3283343 :   tree explicit_targs;
    2142                 :     3283343 :   tree candidates = NULL_TREE;
    2143                 :             : 
    2144                 :             :   /* A TREE_LIST of templates of which DECL may be a specialization.
    2145                 :             :      The TREE_VALUE of each node is a TEMPLATE_DECL.  The
    2146                 :             :      corresponding TREE_PURPOSE is the set of template arguments that,
    2147                 :             :      when used to instantiate the template, would produce a function
    2148                 :             :      with the signature of DECL.  */
    2149                 :     3283343 :   tree templates = NULL_TREE;
    2150                 :     3283343 :   int header_count;
    2151                 :     3283343 :   cp_binding_level *b;
    2152                 :             : 
    2153                 :     3283343 :   *targs_out = NULL_TREE;
    2154                 :             : 
    2155                 :     3283343 :   if (template_id == error_mark_node || decl == error_mark_node)
    2156                 :             :     return error_mark_node;
    2157                 :             : 
    2158                 :             :   /* We shouldn't be specializing a member template of an
    2159                 :             :      unspecialized class template; we already gave an error in
    2160                 :             :      check_specialization_scope, now avoid crashing.  */
    2161                 :     3283339 :   if (!VAR_P (decl)
    2162                 :     2476038 :       && template_count && DECL_CLASS_SCOPE_P (decl)
    2163                 :     4132085 :       && template_class_depth (DECL_CONTEXT (decl)) > 0)
    2164                 :             :     {
    2165                 :           4 :       gcc_assert (errorcount);
    2166                 :           4 :       return error_mark_node;
    2167                 :             :     }
    2168                 :             : 
    2169                 :     3283335 :   fns = TREE_OPERAND (template_id, 0);
    2170                 :     3283335 :   explicit_targs = TREE_OPERAND (template_id, 1);
    2171                 :             : 
    2172                 :     3283335 :   if (fns == error_mark_node)
    2173                 :             :     return error_mark_node;
    2174                 :             : 
    2175                 :             :   /* Check for baselinks.  */
    2176                 :     3283335 :   if (BASELINK_P (fns))
    2177                 :           0 :     fns = BASELINK_FUNCTIONS (fns);
    2178                 :             : 
    2179                 :     3283335 :   if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
    2180                 :             :     {
    2181                 :           8 :       error_at (DECL_SOURCE_LOCATION (decl),
    2182                 :             :                 "%qD is not a function template", fns);
    2183                 :           8 :       return error_mark_node;
    2184                 :             :     }
    2185                 :     3283327 :   else if (VAR_P (decl) && !variable_template_p (fns))
    2186                 :             :     {
    2187                 :           4 :       error ("%qD is not a variable template", fns);
    2188                 :           4 :       return error_mark_node;
    2189                 :             :     }
    2190                 :             : 
    2191                 :             :   /* Count the number of template headers specified for this
    2192                 :             :      specialization.  */
    2193                 :     3283323 :   header_count = 0;
    2194                 :     3283323 :   for (b = current_binding_level;
    2195                 :     4635546 :        b->kind == sk_template_parms;
    2196                 :     1352223 :        b = b->level_chain)
    2197                 :     1352223 :     ++header_count;
    2198                 :             : 
    2199                 :     3283323 :   tree orig_fns = fns;
    2200                 :     3283323 :   bool header_mismatch = false;
    2201                 :             : 
    2202                 :     3283323 :   if (variable_template_p (fns))
    2203                 :             :     {
    2204                 :      807297 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
    2205                 :      807297 :       targs = coerce_template_parms (parms, explicit_targs, fns,
    2206                 :             :                                      tf_warning_or_error);
    2207                 :      807297 :       if (targs != error_mark_node
    2208                 :      807297 :           && constraints_satisfied_p (fns, targs))
    2209                 :      807294 :         templates = tree_cons (targs, fns, templates);
    2210                 :             :     }
    2211                 :    14336391 :   else for (lkp_iterator iter (fns); iter; ++iter)
    2212                 :             :     {
    2213                 :    11860365 :       tree fn = *iter;
    2214                 :             : 
    2215                 :    11860365 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2216                 :             :         {
    2217                 :    11286089 :           tree decl_arg_types;
    2218                 :    11286089 :           tree fn_arg_types;
    2219                 :             : 
    2220                 :             :           /* In case of explicit specialization, we need to check if
    2221                 :             :              the number of template headers appearing in the specialization
    2222                 :             :              is correct. This is usually done in check_explicit_specialization,
    2223                 :             :              but the check done there cannot be exhaustive when specializing
    2224                 :             :              member functions. Consider the following code:
    2225                 :             : 
    2226                 :             :              template <> void A<int>::f(int);
    2227                 :             :              template <> template <> void A<int>::f(int);
    2228                 :             : 
    2229                 :             :              Assuming that A<int> is not itself an explicit specialization
    2230                 :             :              already, the first line specializes "f" which is a non-template
    2231                 :             :              member function, whilst the second line specializes "f" which
    2232                 :             :              is a template member function. So both lines are syntactically
    2233                 :             :              correct, and check_explicit_specialization does not reject
    2234                 :             :              them.
    2235                 :             : 
    2236                 :             :              Here, we can do better, as we are matching the specialization
    2237                 :             :              against the declarations. We count the number of template
    2238                 :             :              headers, and we check if they match TEMPLATE_COUNT + 1
    2239                 :             :              (TEMPLATE_COUNT is the number of qualifying template classes,
    2240                 :             :              plus there must be another header for the member template
    2241                 :             :              itself).
    2242                 :             : 
    2243                 :             :              Notice that if header_count is zero, this is not a
    2244                 :             :              specialization but rather a template instantiation, so there
    2245                 :             :              is no check we can perform here.  */
    2246                 :    11286089 :           if (header_count && header_count != template_count + 1)
    2247                 :             :             {
    2248                 :           8 :               header_mismatch = true;
    2249                 :           8 :               continue;
    2250                 :             :             }
    2251                 :             : 
    2252                 :             :           /* Check that the number of template arguments at the
    2253                 :             :              innermost level for DECL is the same as for FN.  */
    2254                 :    11286081 :           if (current_binding_level->kind == sk_template_parms
    2255                 :    11286081 :               && !current_binding_level->explicit_spec_p
    2256                 :    11286081 :               && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
    2257                 :         955 :                   != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
    2258                 :             :                                       (current_template_parms))))
    2259                 :           4 :             continue;
    2260                 :             : 
    2261                 :             :           /* DECL might be a specialization of FN.  */
    2262                 :    11286077 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2263                 :    11286077 :           fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
    2264                 :             : 
    2265                 :             :           /* For a non-static member function, we need to make sure
    2266                 :             :              that the const qualification is the same.  Since
    2267                 :             :              get_bindings does not try to merge the "this" parameter,
    2268                 :             :              we must do the comparison explicitly.  */
    2269                 :    11286077 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
    2270                 :             :             {
    2271                 :      389059 :               if (!same_type_p (TREE_VALUE (fn_arg_types),
    2272                 :             :                                 TREE_VALUE (decl_arg_types)))
    2273                 :          56 :                 continue;
    2274                 :             : 
    2275                 :             :               /* And the ref-qualification.  */
    2276                 :      778006 :               if (type_memfn_rqual (TREE_TYPE (decl))
    2277                 :      389003 :                   != type_memfn_rqual (TREE_TYPE (fn)))
    2278                 :          38 :                 continue;
    2279                 :             :             }
    2280                 :             : 
    2281                 :             :           /* Skip the "this" parameter and, for constructors of
    2282                 :             :              classes with virtual bases, the VTT parameter.  A
    2283                 :             :              full specialization of a constructor will have a VTT
    2284                 :             :              parameter, but a template never will.  */
    2285                 :    11285983 :           decl_arg_types
    2286                 :    11285983 :             = skip_artificial_parms_for (decl, decl_arg_types);
    2287                 :    11285983 :           fn_arg_types
    2288                 :    11285983 :             = skip_artificial_parms_for (fn, fn_arg_types);
    2289                 :             : 
    2290                 :             :           /* Function templates cannot be specializations; there are
    2291                 :             :              no partial specializations of functions.  Therefore, if
    2292                 :             :              the type of DECL does not match FN, there is no
    2293                 :             :              match.
    2294                 :             : 
    2295                 :             :              Note that it should never be the case that we have both
    2296                 :             :              candidates added here, and for regular member functions
    2297                 :             :              below. */
    2298                 :    11285983 :           if (tsk == tsk_template)
    2299                 :             :             {
    2300                 :         951 :               if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
    2301                 :         951 :                                         current_template_parms))
    2302                 :           0 :                 continue;
    2303                 :         951 :               if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2304                 :             :                                 TREE_TYPE (TREE_TYPE (fn))))
    2305                 :           0 :                 continue;
    2306                 :         951 :               if (!compparms (fn_arg_types, decl_arg_types))
    2307                 :           4 :                 continue;
    2308                 :             : 
    2309                 :         947 :               tree freq = get_constraints (fn);
    2310                 :         947 :               tree dreq = get_constraints (decl);
    2311                 :         947 :               if (!freq != !dreq)
    2312                 :           0 :                 continue;
    2313                 :         947 :               if (freq)
    2314                 :             :                 {
    2315                 :             :                   /* C++20 CA104: Substitute directly into the
    2316                 :             :                      constraint-expression.  */
    2317                 :          14 :                   tree fargs = DECL_TI_ARGS (fn);
    2318                 :          14 :                   tsubst_flags_t complain = tf_none;
    2319                 :          14 :                   freq = tsubst_constraint_info (freq, fargs, complain, fn);
    2320                 :          14 :                   if (!cp_tree_equal (freq, dreq))
    2321                 :          10 :                     continue;
    2322                 :             :                 }
    2323                 :             : 
    2324                 :         937 :               candidates = tree_cons (NULL_TREE, fn, candidates);
    2325                 :         937 :               continue;
    2326                 :         937 :             }
    2327                 :             : 
    2328                 :             :           /* See whether this function might be a specialization of this
    2329                 :             :              template.  Suppress access control because we might be trying
    2330                 :             :              to make this specialization a friend, and we have already done
    2331                 :             :              access control for the declaration of the specialization.  */
    2332                 :    11285032 :           push_deferring_access_checks (dk_no_check);
    2333                 :    11285032 :           targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
    2334                 :    11285032 :           pop_deferring_access_checks ();
    2335                 :             : 
    2336                 :    11285032 :           if (!targs)
    2337                 :             :             /* We cannot deduce template arguments that when used to
    2338                 :             :                specialize TMPL will produce DECL.  */
    2339                 :     9228487 :             continue;
    2340                 :             : 
    2341                 :     2056545 :           if (uses_template_parms (targs))
    2342                 :             :             /* We deduced something involving 'auto', which isn't a valid
    2343                 :             :                template argument.  */
    2344                 :           3 :             continue;
    2345                 :             : 
    2346                 :             :           /* Save this template, and the arguments deduced.  */
    2347                 :     2056542 :           templates = tree_cons (targs, fn, templates);
    2348                 :             :         }
    2349                 :      574276 :       else if (need_member_template)
    2350                 :             :         /* FN is an ordinary member function, and we need a
    2351                 :             :            specialization of a member template.  */
    2352                 :             :         ;
    2353                 :      574216 :       else if (TREE_CODE (fn) != FUNCTION_DECL)
    2354                 :             :         /* We can get IDENTIFIER_NODEs here in certain erroneous
    2355                 :             :            cases.  */
    2356                 :             :         ;
    2357                 :      574216 :       else if (!DECL_FUNCTION_MEMBER_P (fn))
    2358                 :             :         /* This is just an ordinary non-member function.  Nothing can
    2359                 :             :            be a specialization of that.  */
    2360                 :             :         ;
    2361                 :      563771 :       else if (DECL_ARTIFICIAL (fn))
    2362                 :             :         /* Cannot specialize functions that are created implicitly.  */
    2363                 :             :         ;
    2364                 :             :       else
    2365                 :             :         {
    2366                 :      563598 :           tree decl_arg_types;
    2367                 :             : 
    2368                 :             :           /* This is an ordinary member function.  However, since
    2369                 :             :              we're here, we can assume its enclosing class is a
    2370                 :             :              template class.  For example,
    2371                 :             : 
    2372                 :             :                template <typename T> struct S { void f(); };
    2373                 :             :                template <> void S<int>::f() {}
    2374                 :             : 
    2375                 :             :              Here, S<int>::f is a non-template, but S<int> is a
    2376                 :             :              template class.  If FN has the same type as DECL, we
    2377                 :             :              might be in business.  */
    2378                 :             : 
    2379                 :      563598 :           if (!DECL_TEMPLATE_INFO (fn))
    2380                 :             :             /* Its enclosing class is an explicit specialization
    2381                 :             :                of a template class.  This is not a candidate.  */
    2382                 :           8 :             continue;
    2383                 :             : 
    2384                 :      563590 :           if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
    2385                 :             :                             TREE_TYPE (TREE_TYPE (fn))))
    2386                 :             :             /* The return types differ.  */
    2387                 :         826 :             continue;
    2388                 :             : 
    2389                 :             :           /* Adjust the type of DECL in case FN is a static member.  */
    2390                 :      562764 :           decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2391                 :      562764 :           if (DECL_STATIC_FUNCTION_P (fn)
    2392                 :      562764 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2393                 :         187 :             decl_arg_types = TREE_CHAIN (decl_arg_types);
    2394                 :             : 
    2395                 :      562764 :           if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
    2396                 :             :                          decl_arg_types))
    2397                 :      103365 :             continue;
    2398                 :             : 
    2399                 :      459399 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
    2400                 :      918619 :               && (type_memfn_rqual (TREE_TYPE (decl))
    2401                 :      459220 :                   != type_memfn_rqual (TREE_TYPE (fn))))
    2402                 :           6 :             continue;
    2403                 :             : 
    2404                 :             :           // If the deduced arguments do not satisfy the constraints,
    2405                 :             :           // this is not a candidate.
    2406                 :      459393 :           if (flag_concepts && !constraints_satisfied_p (fn))
    2407                 :           7 :             continue;
    2408                 :             : 
    2409                 :             :           // Add the candidate.
    2410                 :      459386 :           candidates = tree_cons (NULL_TREE, fn, candidates);
    2411                 :             :         }
    2412                 :             :     }
    2413                 :             : 
    2414                 :     3283323 :   if (templates && TREE_CHAIN (templates))
    2415                 :             :     {
    2416                 :             :       /* We have:
    2417                 :             : 
    2418                 :             :            [temp.expl.spec]
    2419                 :             : 
    2420                 :             :            It is possible for a specialization with a given function
    2421                 :             :            signature to be instantiated from more than one function
    2422                 :             :            template.  In such cases, explicit specification of the
    2423                 :             :            template arguments must be used to uniquely identify the
    2424                 :             :            function template specialization being specialized.
    2425                 :             : 
    2426                 :             :          Note that here, there's no suggestion that we're supposed to
    2427                 :             :          determine which of the candidate templates is most
    2428                 :             :          specialized.  However, we, also have:
    2429                 :             : 
    2430                 :             :            [temp.func.order]
    2431                 :             : 
    2432                 :             :            Partial ordering of overloaded function template
    2433                 :             :            declarations is used in the following contexts to select
    2434                 :             :            the function template to which a function template
    2435                 :             :            specialization refers:
    2436                 :             : 
    2437                 :             :            -- when an explicit specialization refers to a function
    2438                 :             :               template.
    2439                 :             : 
    2440                 :             :          So, we do use the partial ordering rules, at least for now.
    2441                 :             :          This extension can only serve to make invalid programs valid,
    2442                 :             :          so it's safe.  And, there is strong anecdotal evidence that
    2443                 :             :          the committee intended the partial ordering rules to apply;
    2444                 :             :          the EDG front end has that behavior, and John Spicer claims
    2445                 :             :          that the committee simply forgot to delete the wording in
    2446                 :             :          [temp.expl.spec].  */
    2447                 :       20563 :       tree tmpl = most_specialized_instantiation (templates);
    2448                 :       20563 :       if (tmpl != error_mark_node)
    2449                 :             :         {
    2450                 :       20559 :           templates = tmpl;
    2451                 :       20559 :           TREE_CHAIN (templates) = NULL_TREE;
    2452                 :             :         }
    2453                 :             :     }
    2454                 :             : 
    2455                 :             :   // Concepts allows multiple declarations of member functions
    2456                 :             :   // with the same signature. Like above, we need to rely on
    2457                 :             :   // on the partial ordering of those candidates to determine which
    2458                 :             :   // is the best.
    2459                 :     3283323 :   if (flag_concepts && candidates && TREE_CHAIN (candidates))
    2460                 :             :     {
    2461                 :           4 :       if (tree cand = most_constrained_function (candidates))
    2462                 :             :         {
    2463                 :           4 :           candidates = cand;
    2464                 :           4 :           TREE_CHAIN (cand) = NULL_TREE;
    2465                 :             :         }
    2466                 :             :     }
    2467                 :             : 
    2468                 :     3283323 :   if (templates == NULL_TREE && candidates == NULL_TREE)
    2469                 :             :     {
    2470                 :         239 :       error ("template-id %qD for %q+D does not match any template "
    2471                 :             :              "declaration", template_id, decl);
    2472                 :         239 :       if (header_mismatch)
    2473                 :           4 :         inform (DECL_SOURCE_LOCATION (decl),
    2474                 :             :                 "saw %d %<template<>%>, need %d for "
    2475                 :             :                 "specializing a member function template",
    2476                 :             :                 header_count, template_count + 1);
    2477                 :         239 :       print_candidates (orig_fns);
    2478                 :         239 :       return error_mark_node;
    2479                 :             :     }
    2480                 :     2822765 :   else if ((templates && TREE_CHAIN (templates))
    2481                 :     3283080 :            || (candidates && TREE_CHAIN (candidates))
    2482                 :     6566164 :            || (templates && candidates))
    2483                 :             :     {
    2484                 :           4 :       error ("ambiguous template specialization %qD for %q+D",
    2485                 :             :              template_id, decl);
    2486                 :           4 :       candidates = chainon (candidates, templates);
    2487                 :           4 :       print_candidates (candidates);
    2488                 :           4 :       return error_mark_node;
    2489                 :             :     }
    2490                 :             : 
    2491                 :             :   /* We have one, and exactly one, match.  */
    2492                 :     3283080 :   if (candidates)
    2493                 :             :     {
    2494                 :      460319 :       tree fn = TREE_VALUE (candidates);
    2495                 :      460319 :       *targs_out = copy_node (DECL_TI_ARGS (fn));
    2496                 :             : 
    2497                 :             :       /* Propagate the candidate's constraints to the declaration.  */
    2498                 :      460319 :       if (tsk != tsk_template)
    2499                 :      459382 :         set_constraints (decl, get_constraints (fn));
    2500                 :             : 
    2501                 :             :       /* DECL is a re-declaration or partial instantiation of a template
    2502                 :             :          function.  */
    2503                 :      460319 :       if (TREE_CODE (fn) == TEMPLATE_DECL)
    2504                 :             :         return fn;
    2505                 :             :       /* It was a specialization of an ordinary member function in a
    2506                 :             :          template class.  */
    2507                 :      459382 :       return DECL_TI_TEMPLATE (fn);
    2508                 :             :     }
    2509                 :             : 
    2510                 :             :   /* It was a specialization of a template.  */
    2511                 :     2822761 :   tree tmpl = TREE_VALUE (templates);
    2512                 :     2822761 :   *targs_out = add_outermost_template_args (tmpl, TREE_PURPOSE (templates));
    2513                 :             : 
    2514                 :             :   /* Propagate the template's constraints to the declaration.  */
    2515                 :     2822761 :   if (tsk != tsk_template)
    2516                 :     2279050 :     set_constraints (decl, get_constraints (tmpl));
    2517                 :             : 
    2518                 :             :   return tmpl;
    2519                 :             : }
    2520                 :             : 
    2521                 :             : /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
    2522                 :             :    but with the default argument values filled in from those in the
    2523                 :             :    TMPL_TYPES.  */
    2524                 :             : 
    2525                 :             : static tree
    2526                 :      200314 : copy_default_args_to_explicit_spec_1 (tree spec_types,
    2527                 :             :                                       tree tmpl_types)
    2528                 :             : {
    2529                 :      200314 :   tree new_spec_types;
    2530                 :             : 
    2531                 :      200314 :   if (!spec_types)
    2532                 :             :     return NULL_TREE;
    2533                 :             : 
    2534                 :      200314 :   if (spec_types == void_list_node)
    2535                 :             :     return void_list_node;
    2536                 :             : 
    2537                 :             :   /* Substitute into the rest of the list.  */
    2538                 :      119841 :   new_spec_types =
    2539                 :      119841 :     copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
    2540                 :      119841 :                                           TREE_CHAIN (tmpl_types));
    2541                 :             : 
    2542                 :             :   /* Add the default argument for this parameter.  */
    2543                 :      239682 :   return hash_tree_cons (TREE_PURPOSE (tmpl_types),
    2544                 :      119841 :                          TREE_VALUE (spec_types),
    2545                 :      119841 :                          new_spec_types);
    2546                 :             : }
    2547                 :             : 
    2548                 :             : /* DECL is an explicit specialization.  Replicate default arguments
    2549                 :             :    from the template it specializes.  (That way, code like:
    2550                 :             : 
    2551                 :             :      template <class T> void f(T = 3);
    2552                 :             :      template <> void f(double);
    2553                 :             :      void g () { f (); }
    2554                 :             : 
    2555                 :             :    works, as required.)  An alternative approach would be to look up
    2556                 :             :    the correct default arguments at the call-site, but this approach
    2557                 :             :    is consistent with how implicit instantiations are handled.  */
    2558                 :             : 
    2559                 :             : static void
    2560                 :      542921 : copy_default_args_to_explicit_spec (tree decl)
    2561                 :             : {
    2562                 :      542921 :   tree tmpl;
    2563                 :      542921 :   tree spec_types;
    2564                 :      542921 :   tree tmpl_types;
    2565                 :      542921 :   tree new_spec_types;
    2566                 :      542921 :   tree old_type;
    2567                 :      542921 :   tree new_type;
    2568                 :      542921 :   tree t;
    2569                 :      542921 :   tree object_type = NULL_TREE;
    2570                 :      542921 :   tree in_charge = NULL_TREE;
    2571                 :      542921 :   tree vtt = NULL_TREE;
    2572                 :             : 
    2573                 :             :   /* See if there's anything we need to do.  */
    2574                 :      542921 :   tmpl = DECL_TI_TEMPLATE (decl);
    2575                 :      542921 :   tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
    2576                 :     2199040 :   for (t = tmpl_types; t; t = TREE_CHAIN (t))
    2577                 :     1736592 :     if (TREE_PURPOSE (t))
    2578                 :             :       break;
    2579                 :      542921 :   if (!t)
    2580                 :             :     return;
    2581                 :             : 
    2582                 :       80473 :   old_type = TREE_TYPE (decl);
    2583                 :       80473 :   spec_types = TYPE_ARG_TYPES (old_type);
    2584                 :             : 
    2585                 :       80473 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    2586                 :             :     {
    2587                 :             :       /* Remove the this pointer, but remember the object's type for
    2588                 :             :          CV quals.  */
    2589                 :       80473 :       object_type = TREE_TYPE (TREE_VALUE (spec_types));
    2590                 :       80473 :       spec_types = TREE_CHAIN (spec_types);
    2591                 :       80473 :       tmpl_types = TREE_CHAIN (tmpl_types);
    2592                 :             : 
    2593                 :       80473 :       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
    2594                 :             :         {
    2595                 :             :           /* DECL may contain more parameters than TMPL due to the extra
    2596                 :             :              in-charge parameter in constructors and destructors.  */
    2597                 :           0 :           in_charge = spec_types;
    2598                 :           0 :           spec_types = TREE_CHAIN (spec_types);
    2599                 :             :         }
    2600                 :       80473 :       if (DECL_HAS_VTT_PARM_P (decl))
    2601                 :             :         {
    2602                 :           0 :           vtt = spec_types;
    2603                 :           0 :           spec_types = TREE_CHAIN (spec_types);
    2604                 :             :         }
    2605                 :             :     }
    2606                 :             : 
    2607                 :             :   /* Compute the merged default arguments.  */
    2608                 :       80473 :   new_spec_types =
    2609                 :       80473 :     copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
    2610                 :             : 
    2611                 :             :   /* Compute the new FUNCTION_TYPE.  */
    2612                 :       80473 :   if (object_type)
    2613                 :             :     {
    2614                 :       80473 :       if (vtt)
    2615                 :           0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
    2616                 :           0 :                                          TREE_VALUE (vtt),
    2617                 :             :                                          new_spec_types);
    2618                 :             : 
    2619                 :       80473 :       if (in_charge)
    2620                 :             :         /* Put the in-charge parameter back.  */
    2621                 :           0 :         new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
    2622                 :           0 :                                          TREE_VALUE (in_charge),
    2623                 :             :                                          new_spec_types);
    2624                 :             : 
    2625                 :       80473 :       new_type = build_method_type_directly (object_type,
    2626                 :       80473 :                                              TREE_TYPE (old_type),
    2627                 :             :                                              new_spec_types);
    2628                 :             :     }
    2629                 :             :   else
    2630                 :           0 :     new_type = build_function_type (TREE_TYPE (old_type),
    2631                 :             :                                     new_spec_types);
    2632                 :       80473 :   new_type = cp_build_type_attribute_variant (new_type,
    2633                 :       80473 :                                               TYPE_ATTRIBUTES (old_type));
    2634                 :       80473 :   new_type = cxx_copy_lang_qualifiers (new_type, old_type);
    2635                 :             : 
    2636                 :       80473 :   TREE_TYPE (decl) = new_type;
    2637                 :             : }
    2638                 :             : 
    2639                 :             : /* Return the number of template headers we expect to see for a definition
    2640                 :             :    or specialization of CTYPE or one of its non-template members.  */
    2641                 :             : 
    2642                 :             : int
    2643                 :    28532056 : num_template_headers_for_class (tree ctype)
    2644                 :             : {
    2645                 :    28532056 :   int num_templates = 0;
    2646                 :             : 
    2647                 :    45785717 :   while (ctype && CLASS_TYPE_P (ctype))
    2648                 :             :     {
    2649                 :             :       /* You're supposed to have one `template <...>' for every
    2650                 :             :          template class, but you don't need one for a full
    2651                 :             :          specialization.  For example:
    2652                 :             : 
    2653                 :             :          template <class T> struct S{};
    2654                 :             :          template <> struct S<int> { void f(); };
    2655                 :             :          void S<int>::f () {}
    2656                 :             : 
    2657                 :             :          is correct; there shouldn't be a `template <>' for the
    2658                 :             :          definition of `S<int>::f'.  */
    2659                 :    23275429 :       if (!CLASSTYPE_TEMPLATE_INFO (ctype))
    2660                 :             :         /* If CTYPE does not have template information of any
    2661                 :             :            kind,  then it is not a template, nor is it nested
    2662                 :             :            within a template.  */
    2663                 :             :         break;
    2664                 :    17414026 :       if (explicit_class_specialization_p (ctype))
    2665                 :             :         break;
    2666                 :    17253661 :       if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
    2667                 :    16938464 :         ++num_templates;
    2668                 :             : 
    2669                 :    17253661 :       ctype = TYPE_CONTEXT (ctype);
    2670                 :             :     }
    2671                 :             : 
    2672                 :    28532056 :   return num_templates;
    2673                 :             : }
    2674                 :             : 
    2675                 :             : /* Do a simple sanity check on the template headers that precede the
    2676                 :             :    variable declaration DECL.  */
    2677                 :             : 
    2678                 :             : void
    2679                 :     2769542 : check_template_variable (tree decl)
    2680                 :             : {
    2681                 :     2769542 :   tree ctx = CP_DECL_CONTEXT (decl);
    2682                 :     2769542 :   int wanted = num_template_headers_for_class (ctx);
    2683                 :     5539080 :   if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
    2684                 :     5539068 :       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
    2685                 :             :     {
    2686                 :     2448743 :       if (cxx_dialect < cxx14)
    2687                 :           9 :         pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
    2688                 :             :                  "variable templates only available with "
    2689                 :             :                  "%<-std=c++14%> or %<-std=gnu++14%>");
    2690                 :             : 
    2691                 :             :       // Namespace-scope variable templates should have a template header.
    2692                 :     2448743 :       ++wanted;
    2693                 :             :     }
    2694                 :     2769542 :   if (template_header_count > wanted)
    2695                 :             :     {
    2696                 :          16 :       auto_diagnostic_group d;
    2697                 :          16 :       bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    2698                 :             :                              "too many template headers for %qD "
    2699                 :             :                              "(should be %d)",
    2700                 :             :                              decl, wanted);
    2701                 :          16 :       if (warned && CLASS_TYPE_P (ctx)
    2702                 :          28 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
    2703                 :           4 :         inform (DECL_SOURCE_LOCATION (decl),
    2704                 :             :                 "members of an explicitly specialized class are defined "
    2705                 :             :                 "without a template header");
    2706                 :          16 :     }
    2707                 :     2769542 : }
    2708                 :             : 
    2709                 :             : /* An explicit specialization whose declarator-id or class-head-name is not
    2710                 :             :    qualified shall be declared in the nearest enclosing namespace of the
    2711                 :             :    template, or, if the namespace is inline (7.3.1), any namespace from its
    2712                 :             :    enclosing namespace set.
    2713                 :             : 
    2714                 :             :    If the name declared in the explicit instantiation is an unqualified name,
    2715                 :             :    the explicit instantiation shall appear in the namespace where its template
    2716                 :             :    is declared or, if that namespace is inline (7.3.1), any namespace from its
    2717                 :             :    enclosing namespace set.  */
    2718                 :             : 
    2719                 :             : void
    2720                 :    13395336 : check_unqualified_spec_or_inst (tree t, location_t loc)
    2721                 :             : {
    2722                 :    13395336 :   tree tmpl = most_general_template (t);
    2723                 :    26790672 :   if (DECL_NAMESPACE_SCOPE_P (tmpl)
    2724                 :    26301408 :       && !is_nested_namespace (current_namespace,
    2725                 :    12906072 :                                CP_DECL_CONTEXT (tmpl), true))
    2726                 :             :     {
    2727                 :          21 :       if (processing_specialization)
    2728                 :           8 :         permerror (loc, "explicit specialization of %qD outside its "
    2729                 :             :                    "namespace must use a nested-name-specifier", tmpl);
    2730                 :          13 :       else if (processing_explicit_instantiation
    2731                 :          13 :                && cxx_dialect >= cxx11)
    2732                 :             :         /* This was allowed in C++98, so only pedwarn.  */
    2733                 :          11 :         pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
    2734                 :             :                  "outside its namespace must use a nested-name-"
    2735                 :             :                  "specifier", tmpl);
    2736                 :             :     }
    2737                 :    13395336 : }
    2738                 :             : 
    2739                 :             : /* Warn for a template specialization SPEC that is missing some of a set
    2740                 :             :    of function or type attributes that the template TEMPL is declared with.
    2741                 :             :    ATTRLIST is a list of additional attributes that SPEC should be taken
    2742                 :             :    to ultimately be declared with.  */
    2743                 :             : 
    2744                 :             : static void
    2745                 :      806472 : warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
    2746                 :             : {
    2747                 :      806472 :   if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    2748                 :             :     tmpl = DECL_TEMPLATE_RESULT (tmpl);
    2749                 :             : 
    2750                 :             :   /* Avoid warning if the difference between the primary and
    2751                 :             :      the specialization is not in one of the attributes below.  */
    2752                 :      806472 :   const char* const blacklist[] = {
    2753                 :             :     "alloc_align", "alloc_size", "assume_aligned", "format",
    2754                 :             :     "format_arg", "malloc", "nonnull", NULL
    2755                 :             :   };
    2756                 :             : 
    2757                 :             :   /* Put together a list of the black listed attributes that the primary
    2758                 :             :      template is declared with that the specialization is not, in case
    2759                 :             :      it's not apparent from the most recent declaration of the primary.  */
    2760                 :      806472 :   pretty_printer str;
    2761                 :      806472 :   unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
    2762                 :             :                                                  blacklist, &str);
    2763                 :             : 
    2764                 :      806472 :   if (!nattrs)
    2765                 :      806436 :     return;
    2766                 :             : 
    2767                 :          36 :   auto_diagnostic_group d;
    2768                 :          36 :   if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
    2769                 :             :                   "explicit specialization %q#D may be missing attributes",
    2770                 :             :                   spec))
    2771                 :          68 :     inform (DECL_SOURCE_LOCATION (tmpl),
    2772                 :             :             nattrs > 1
    2773                 :             :             ? G_("missing primary template attributes %s")
    2774                 :             :             : G_("missing primary template attribute %s"),
    2775                 :             :             pp_formatted_text (&str));
    2776                 :      806472 : }
    2777                 :             : 
    2778                 :             : /* Check to see if the function just declared, as indicated in
    2779                 :             :    DECLARATOR, and in DECL, is a specialization of a function
    2780                 :             :    template.  We may also discover that the declaration is an explicit
    2781                 :             :    instantiation at this point.
    2782                 :             : 
    2783                 :             :    Returns DECL, or an equivalent declaration that should be used
    2784                 :             :    instead if all goes well.  Issues an error message if something is
    2785                 :             :    amiss.  Returns error_mark_node if the error is not easily
    2786                 :             :    recoverable.
    2787                 :             : 
    2788                 :             :    FLAGS is a bitmask consisting of the following flags:
    2789                 :             : 
    2790                 :             :    2: The function has a definition.
    2791                 :             :    4: The function is a friend.
    2792                 :             : 
    2793                 :             :    The TEMPLATE_COUNT is the number of references to qualifying
    2794                 :             :    template classes that appeared in the name of the function.  For
    2795                 :             :    example, in
    2796                 :             : 
    2797                 :             :      template <class T> struct S { void f(); };
    2798                 :             :      void S<int>::f();
    2799                 :             : 
    2800                 :             :    the TEMPLATE_COUNT would be 1.  However, explicitly specialized
    2801                 :             :    classes are not counted in the TEMPLATE_COUNT, so that in
    2802                 :             : 
    2803                 :             :      template <class T> struct S {};
    2804                 :             :      template <> struct S<int> { void f(); }
    2805                 :             :      template <> void S<int>::f();
    2806                 :             : 
    2807                 :             :    the TEMPLATE_COUNT would be 0.  (Note that this declaration is
    2808                 :             :    invalid; there should be no template <>.)
    2809                 :             : 
    2810                 :             :    If the function is a specialization, it is marked as such via
    2811                 :             :    DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
    2812                 :             :    is set up correctly, and it is added to the list of specializations
    2813                 :             :    for that template.  */
    2814                 :             : 
    2815                 :             : tree
    2816                 :   205051536 : check_explicit_specialization (tree declarator,
    2817                 :             :                                tree decl,
    2818                 :             :                                int template_count,
    2819                 :             :                                int flags,
    2820                 :             :                                tree attrlist)
    2821                 :             : {
    2822                 :   205051536 :   int have_def = flags & 2;
    2823                 :   205051536 :   int is_friend = flags & 4;
    2824                 :   205051536 :   bool is_concept = flags & 8;
    2825                 :   205051536 :   int specialization = 0;
    2826                 :   205051536 :   int explicit_instantiation = 0;
    2827                 :   205051536 :   int member_specialization = 0;
    2828                 :   205051536 :   tree ctype = DECL_CLASS_CONTEXT (decl);
    2829                 :   205051536 :   tree dname = DECL_NAME (decl);
    2830                 :   205051536 :   tmpl_spec_kind tsk;
    2831                 :             : 
    2832                 :   205051536 :   if (is_friend)
    2833                 :             :     {
    2834                 :          83 :       if (!processing_specialization)
    2835                 :             :         tsk = tsk_none;
    2836                 :             :       else
    2837                 :             :         tsk = tsk_excessive_parms;
    2838                 :             :     }
    2839                 :             :   else
    2840                 :   205051453 :     tsk = current_tmpl_spec_kind (template_count);
    2841                 :             : 
    2842                 :   205051453 :   switch (tsk)
    2843                 :             :     {
    2844                 :   158297527 :     case tsk_none:
    2845                 :   158297527 :       if (processing_specialization && !VAR_P (decl))
    2846                 :             :         {
    2847                 :      454423 :           specialization = 1;
    2848                 :      454423 :           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2849                 :             :         }
    2850                 :   157843104 :       else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
    2851                 :   157843104 :                || (DECL_LANG_SPECIFIC (decl)
    2852                 :   110243271 :                    && DECL_IMPLICIT_INSTANTIATION (decl)))
    2853                 :             :         {
    2854                 :          99 :           if (is_friend)
    2855                 :             :             /* This could be something like:
    2856                 :             : 
    2857                 :             :                template <class T> void f(T);
    2858                 :             :                class S { friend void f<>(int); }  */
    2859                 :             :             specialization = 1;
    2860                 :             :           else
    2861                 :             :             {
    2862                 :             :               /* This case handles bogus declarations like template <>
    2863                 :             :                  template <class T> void f<int>(); */
    2864                 :             : 
    2865                 :          16 :               error_at (cp_expr_loc_or_input_loc (declarator),
    2866                 :             :                         "template-id %qE in declaration of primary template",
    2867                 :             :                         declarator);
    2868                 :          16 :               return decl;
    2869                 :             :             }
    2870                 :             :         }
    2871                 :             :       break;
    2872                 :             : 
    2873                 :           0 :     case tsk_invalid_member_spec:
    2874                 :             :       /* The error has already been reported in
    2875                 :             :          check_specialization_scope.  */
    2876                 :           0 :       return error_mark_node;
    2877                 :             : 
    2878                 :           0 :     case tsk_invalid_expl_inst:
    2879                 :           0 :       error ("template parameter list used in explicit instantiation");
    2880                 :             : 
    2881                 :             :       /* Fall through.  */
    2882                 :             : 
    2883                 :     1908973 :     case tsk_expl_inst:
    2884                 :     1908973 :       if (have_def)
    2885                 :           0 :         error ("definition provided for explicit instantiation");
    2886                 :             : 
    2887                 :             :       explicit_instantiation = 1;
    2888                 :             :       break;
    2889                 :             : 
    2890                 :           0 :     case tsk_excessive_parms:
    2891                 :           0 :     case tsk_insufficient_parms:
    2892                 :           0 :       if (tsk == tsk_excessive_parms)
    2893                 :           0 :         error ("too many template parameter lists in declaration of %qD",
    2894                 :             :                decl);
    2895                 :           0 :       else if (template_header_count)
    2896                 :           0 :         error("too few template parameter lists in declaration of %qD", decl);
    2897                 :             :       else
    2898                 :           0 :         error("explicit specialization of %qD must be introduced by "
    2899                 :             :               "%<template <>%>", decl);
    2900                 :             : 
    2901                 :             :       /* Fall through.  */
    2902                 :      352082 :     case tsk_expl_spec:
    2903                 :      352082 :       if (is_concept)
    2904                 :           0 :         error ("explicit specialization declared %<concept%>");
    2905                 :             : 
    2906                 :      352082 :       if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
    2907                 :             :         /* In cases like template<> constexpr bool v = true;
    2908                 :             :            We'll give an error in check_template_variable.  */
    2909                 :             :         break;
    2910                 :             : 
    2911                 :      352066 :       SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2912                 :      352066 :       if (ctype)
    2913                 :             :         member_specialization = 1;
    2914                 :             :       else
    2915                 :      351819 :         specialization = 1;
    2916                 :             :       break;
    2917                 :             : 
    2918                 :    44492954 :     case tsk_template:
    2919                 :    44492954 :       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
    2920                 :             :         {
    2921                 :             :           /* This case handles bogus declarations like template <>
    2922                 :             :              template <class T> void f<int>(); */
    2923                 :             : 
    2924                 :      543737 :           if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
    2925                 :          10 :             error_at (cp_expr_loc_or_input_loc (declarator),
    2926                 :             :                       "template-id %qE in declaration of primary template",
    2927                 :             :                       declarator);
    2928                 :      543728 :           else if (variable_template_p (TREE_OPERAND (declarator, 0)))
    2929                 :             :             {
    2930                 :             :               /* Partial specialization of variable template.  */
    2931                 :      543711 :               SET_DECL_TEMPLATE_SPECIALIZATION (decl);
    2932                 :      543711 :               specialization = 1;
    2933                 :      543711 :               goto ok;
    2934                 :             :             }
    2935                 :          17 :           else if (cxx_dialect < cxx14)
    2936                 :           5 :             error_at (cp_expr_loc_or_input_loc (declarator),
    2937                 :             :                       "non-type partial specialization %qE "
    2938                 :             :                       "is not allowed", declarator);
    2939                 :             :           else
    2940                 :          17 :             error_at (cp_expr_loc_or_input_loc (declarator),
    2941                 :             :                       "non-class, non-variable partial specialization %qE "
    2942                 :             :                       "is not allowed", declarator);
    2943                 :          26 :           return decl;
    2944                 :      543711 :         ok:;
    2945                 :             :         }
    2946                 :             : 
    2947                 :    44492928 :       if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
    2948                 :             :         /* This is a specialization of a member template, without
    2949                 :             :            specialization the containing class.  Something like:
    2950                 :             : 
    2951                 :             :              template <class T> struct S {
    2952                 :             :                template <class U> void f (U);
    2953                 :             :              };
    2954                 :             :              template <> template <class U> void S<int>::f(U) {}
    2955                 :             : 
    2956                 :             :            That's a specialization -- but of the entire template.  */
    2957                 :             :         specialization = 1;
    2958                 :             :       break;
    2959                 :             : 
    2960                 :           0 :     default:
    2961                 :           0 :       gcc_unreachable ();
    2962                 :             :     }
    2963                 :             : 
    2964                 :   205051494 :   if ((specialization || member_specialization)
    2965                 :             :       /* This doesn't apply to variable templates.  */
    2966                 :   205051494 :       && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
    2967                 :             :     {
    2968                 :      543964 :       tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
    2969                 :     2464451 :       for (; t; t = TREE_CHAIN (t))
    2970                 :     1920491 :         if (TREE_PURPOSE (t))
    2971                 :             :           {
    2972                 :           4 :             permerror (input_location,
    2973                 :             :                        "default argument specified in explicit specialization");
    2974                 :           4 :             break;
    2975                 :             :           }
    2976                 :             :     }
    2977                 :             : 
    2978                 :   205051494 :   if (specialization || member_specialization || explicit_instantiation)
    2979                 :             :     {
    2980                 :     3260208 :       tree tmpl = NULL_TREE;
    2981                 :     3260208 :       tree targs = NULL_TREE;
    2982                 :     3260208 :       bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
    2983                 :     3260208 :       bool found_hidden = false;
    2984                 :             : 
    2985                 :             :       /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
    2986                 :     3260208 :       if (!was_template_id)
    2987                 :             :         {
    2988                 :     1637544 :           tree fns;
    2989                 :             : 
    2990                 :     1637544 :           gcc_assert (identifier_p (declarator));
    2991                 :     1637544 :           if (ctype)
    2992                 :             :             fns = dname;
    2993                 :             :           else
    2994                 :             :             {
    2995                 :             :               /* If there is no class context, the explicit instantiation
    2996                 :             :                  must be at namespace scope.  */
    2997                 :      788771 :               gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
    2998                 :             : 
    2999                 :             :               /* Find the namespace binding, using the declaration
    3000                 :             :                  context.  */
    3001                 :      788771 :               fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
    3002                 :             :                                            LOOK_want::NORMAL, true);
    3003                 :      788771 :               if (fns == error_mark_node)
    3004                 :             :                 {
    3005                 :             :                   /* If lookup fails, look for a friend declaration so we can
    3006                 :             :                      give a better diagnostic.  */
    3007                 :          23 :                   fns = (lookup_qualified_name
    3008                 :          23 :                          (CP_DECL_CONTEXT (decl), dname,
    3009                 :             :                           LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
    3010                 :             :                           /*complain*/true));
    3011                 :          23 :                   found_hidden = true;
    3012                 :             :                 }
    3013                 :             : 
    3014                 :      788771 :               if (fns == error_mark_node || !is_overloaded_fn (fns))
    3015                 :             :                 {
    3016                 :          23 :                   error ("%qD is not a template function", dname);
    3017                 :          23 :                   fns = error_mark_node;
    3018                 :             :                 }
    3019                 :             :             }
    3020                 :             : 
    3021                 :     1637544 :           declarator = lookup_template_function (fns, NULL_TREE);
    3022                 :             :         }
    3023                 :             : 
    3024                 :     3260208 :       if (declarator == error_mark_node)
    3025                 :     1910025 :         return error_mark_node;
    3026                 :             : 
    3027                 :     3260185 :       if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
    3028                 :             :         {
    3029                 :           0 :           if (!explicit_instantiation)
    3030                 :             :             /* A specialization in class scope.  This is invalid,
    3031                 :             :                but the error will already have been flagged by
    3032                 :             :                check_specialization_scope.  */
    3033                 :             :             return error_mark_node;
    3034                 :             :           else
    3035                 :             :             {
    3036                 :             :               /* It's not valid to write an explicit instantiation in
    3037                 :             :                  class scope, e.g.:
    3038                 :             : 
    3039                 :             :                    class C { template void f(); }
    3040                 :             : 
    3041                 :             :                    This case is caught by the parser.  However, on
    3042                 :             :                    something like:
    3043                 :             : 
    3044                 :             :                    template class C { void f(); };
    3045                 :             : 
    3046                 :             :                    (which is invalid) we can get here.  The error will be
    3047                 :             :                    issued later.  */
    3048                 :           0 :               ;
    3049                 :             :             }
    3050                 :             : 
    3051                 :           0 :           return decl;
    3052                 :             :         }
    3053                 :     3260185 :       else if (ctype != NULL_TREE
    3054                 :     3260185 :                && (identifier_p (TREE_OPERAND (declarator, 0))))
    3055                 :             :         {
    3056                 :             :           // We'll match variable templates in start_decl.
    3057                 :      848773 :           if (VAR_P (decl))
    3058                 :             :             return decl;
    3059                 :             : 
    3060                 :             :           /* Find the list of functions in ctype that have the same
    3061                 :             :              name as the declared function.  */
    3062                 :      848720 :           tree name = TREE_OPERAND (declarator, 0);
    3063                 :             : 
    3064                 :      848720 :           if (constructor_name_p (name, ctype))
    3065                 :             :             {
    3066                 :           0 :               if (DECL_CONSTRUCTOR_P (decl)
    3067                 :           0 :                   ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
    3068                 :           0 :                   : !CLASSTYPE_DESTRUCTOR (ctype))
    3069                 :             :                 {
    3070                 :             :                   /* From [temp.expl.spec]:
    3071                 :             : 
    3072                 :             :                      If such an explicit specialization for the member
    3073                 :             :                      of a class template names an implicitly-declared
    3074                 :             :                      special member function (clause _special_), the
    3075                 :             :                      program is ill-formed.
    3076                 :             : 
    3077                 :             :                      Similar language is found in [temp.explicit].  */
    3078                 :           0 :                   error ("specialization of implicitly-declared special member function");
    3079                 :           0 :                   return error_mark_node;
    3080                 :             :                 }
    3081                 :             : 
    3082                 :           0 :               name = DECL_NAME (decl);
    3083                 :             :             }
    3084                 :             : 
    3085                 :             :           /* For a type-conversion operator, We might be looking for
    3086                 :             :              `operator int' which will be a specialization of
    3087                 :             :              `operator T'.  Grab all the conversion operators, and
    3088                 :             :              then select from them.  */
    3089                 :     1693318 :           tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
    3090                 :             :                                         ? conv_op_identifier : name);
    3091                 :             : 
    3092                 :      848720 :           if (fns == NULL_TREE)
    3093                 :             :             {
    3094                 :           0 :               error ("no member function %qD declared in %qT", name, ctype);
    3095                 :           0 :               return error_mark_node;
    3096                 :             :             }
    3097                 :             :           else
    3098                 :      848720 :             TREE_OPERAND (declarator, 0) = fns;
    3099                 :             :         }
    3100                 :             : 
    3101                 :             :       /* Figure out what exactly is being specialized at this point.
    3102                 :             :          Note that for an explicit instantiation, even one for a
    3103                 :             :          member function, we cannot tell a priori whether the
    3104                 :             :          instantiation is for a member template, or just a member
    3105                 :             :          function of a template class.  Even if a member template is
    3106                 :             :          being instantiated, the member template arguments may be
    3107                 :             :          elided if they can be deduced from the rest of the
    3108                 :             :          declaration.  */
    3109                 :     3260132 :       tmpl = determine_specialization (declarator, decl,
    3110                 :             :                                        &targs,
    3111                 :             :                                        member_specialization,
    3112                 :             :                                        template_count,
    3113                 :             :                                        tsk);
    3114                 :             : 
    3115                 :     3260132 :       if (!tmpl || tmpl == error_mark_node)
    3116                 :             :         /* We couldn't figure out what this declaration was
    3117                 :             :            specializing.  */
    3118                 :         254 :         return error_mark_node;
    3119                 :             :       else
    3120                 :             :         {
    3121                 :     3259878 :           if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
    3122                 :             :             {
    3123                 :          12 :               auto_diagnostic_group d;
    3124                 :          12 :               if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
    3125                 :             :                            "friend declaration %qD is not visible to "
    3126                 :             :                            "explicit specialization", tmpl))
    3127                 :          12 :                 inform (DECL_SOURCE_LOCATION (tmpl),
    3128                 :             :                         "friend declaration here");
    3129                 :          12 :             }
    3130                 :             : 
    3131                 :     3259878 :           if (!ctype && !is_friend
    3132                 :     3259878 :               && CP_DECL_CONTEXT (decl) == current_namespace)
    3133                 :     2408315 :             check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
    3134                 :             : 
    3135                 :     3259878 :           tree gen_tmpl = most_general_template (tmpl);
    3136                 :             : 
    3137                 :     3259878 :           if (explicit_instantiation)
    3138                 :             :             {
    3139                 :             :               /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
    3140                 :             :                  is done by do_decl_instantiation later.  */
    3141                 :             : 
    3142                 :     1908758 :               int arg_depth = TMPL_ARGS_DEPTH (targs);
    3143                 :     1908758 :               int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
    3144                 :             : 
    3145                 :     1908758 :               if (arg_depth > parm_depth)
    3146                 :             :                 {
    3147                 :             :                   /* If TMPL is not the most general template (for
    3148                 :             :                      example, if TMPL is a friend template that is
    3149                 :             :                      injected into namespace scope), then there will
    3150                 :             :                      be too many levels of TARGS.  Remove some of them
    3151                 :             :                      here.  */
    3152                 :      388293 :                   int i;
    3153                 :      388293 :                   tree new_targs;
    3154                 :             : 
    3155                 :      388293 :                   new_targs = make_tree_vec (parm_depth);
    3156                 :      776586 :                   for (i = arg_depth - parm_depth; i < arg_depth; ++i)
    3157                 :      776586 :                     TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
    3158                 :      388293 :                       = TREE_VEC_ELT (targs, i);
    3159                 :      388293 :                   targs = new_targs;
    3160                 :             :                 }
    3161                 :             : 
    3162                 :     1908758 :               return instantiate_template (tmpl, targs, tf_error);
    3163                 :             :             }
    3164                 :             : 
    3165                 :             :           /* If we thought that the DECL was a member function, but it
    3166                 :             :              turns out to be specializing a static member function,
    3167                 :             :              make DECL a static member function as well.  */
    3168                 :     1351120 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    3169                 :      543858 :               && DECL_STATIC_FUNCTION_P (tmpl)
    3170                 :     1352147 :               && DECL_IOBJ_MEMBER_FUNCTION_P (decl))
    3171                 :        1015 :             revert_static_member_fn (decl);
    3172                 :             : 
    3173                 :             :           /* If this is a specialization of a member template of a
    3174                 :             :              template class, we want to return the TEMPLATE_DECL, not
    3175                 :             :              the specialization of it.  */
    3176                 :     1351120 :           if (tsk == tsk_template && !was_template_id)
    3177                 :             :             {
    3178                 :         937 :               tree result = DECL_TEMPLATE_RESULT (tmpl);
    3179                 :         937 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    3180                 :         937 :               DECL_INITIAL (result) = NULL_TREE;
    3181                 :         937 :               if (have_def)
    3182                 :             :                 {
    3183                 :         924 :                   tree parm;
    3184                 :         924 :                   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    3185                 :        1848 :                   DECL_SOURCE_LOCATION (result)
    3186                 :         924 :                     = DECL_SOURCE_LOCATION (decl);
    3187                 :             :                   /* We want to use the argument list specified in the
    3188                 :             :                      definition, not in the original declaration.  */
    3189                 :         924 :                   DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
    3190                 :        1935 :                   for (parm = DECL_ARGUMENTS (result); parm;
    3191                 :        1011 :                        parm = DECL_CHAIN (parm))
    3192                 :        1011 :                     DECL_CONTEXT (parm) = result;
    3193                 :             :                 }
    3194                 :         937 :               decl = register_specialization (tmpl, gen_tmpl, targs,
    3195                 :             :                                               is_friend, 0);
    3196                 :         937 :               remove_contract_attributes (result);
    3197                 :         937 :               return decl;
    3198                 :             :             }
    3199                 :             : 
    3200                 :             :           /* Set up the DECL_TEMPLATE_INFO for DECL.  */
    3201                 :     1350183 :           DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
    3202                 :             : 
    3203                 :     1350183 :           if (was_template_id)
    3204                 :      808137 :             TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
    3205                 :             : 
    3206                 :             :           /* Inherit default function arguments from the template
    3207                 :             :              DECL is specializing.  */
    3208                 :     1350183 :           if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    3209                 :      542921 :             copy_default_args_to_explicit_spec (decl);
    3210                 :             : 
    3211                 :             :           /* This specialization has the same protection as the
    3212                 :             :              template it specializes.  */
    3213                 :     1350183 :           TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
    3214                 :     1350183 :           TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
    3215                 :             : 
    3216                 :             :           /* 7.1.1-1 [dcl.stc]
    3217                 :             : 
    3218                 :             :              A storage-class-specifier shall not be specified in an
    3219                 :             :              explicit specialization...
    3220                 :             : 
    3221                 :             :              The parser rejects these, so unless action is taken here,
    3222                 :             :              explicit function specializations will always appear with
    3223                 :             :              global linkage.
    3224                 :             : 
    3225                 :             :              The action recommended by the C++ CWG in response to C++
    3226                 :             :              defect report 605 is to make the storage class and linkage
    3227                 :             :              of the explicit specialization match the templated function:
    3228                 :             : 
    3229                 :             :              http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
    3230                 :             :            */
    3231                 :     1350183 :           if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
    3232                 :             :             {
    3233                 :       88436 :               tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
    3234                 :       88436 :               gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
    3235                 :             : 
    3236                 :             :               /* A concept cannot be specialized.  */
    3237                 :       88436 :               if (DECL_DECLARED_CONCEPT_P (tmpl_func))
    3238                 :             :                 {
    3239                 :           0 :                   error ("explicit specialization of function concept %qD",
    3240                 :             :                          gen_tmpl);
    3241                 :           0 :                   return error_mark_node;
    3242                 :             :                 }
    3243                 :             : 
    3244                 :             :               /* This specialization has the same linkage and visibility as
    3245                 :             :                  the function template it specializes.  */
    3246                 :       88436 :               TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
    3247                 :       88436 :               if (! TREE_PUBLIC (decl))
    3248                 :             :                 {
    3249                 :          40 :                   DECL_INTERFACE_KNOWN (decl) = 1;
    3250                 :          40 :                   DECL_NOT_REALLY_EXTERN (decl) = 1;
    3251                 :             :                 }
    3252                 :       88436 :               DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
    3253                 :       88436 :               if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
    3254                 :             :                 {
    3255                 :          45 :                   DECL_VISIBILITY_SPECIFIED (decl) = 1;
    3256                 :          45 :                   DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
    3257                 :             :                 }
    3258                 :             :             }
    3259                 :             : 
    3260                 :             :           /* If DECL is a friend declaration, declared using an
    3261                 :             :              unqualified name, the namespace associated with DECL may
    3262                 :             :              have been set incorrectly.  For example, in:
    3263                 :             : 
    3264                 :             :                template <typename T> void f(T);
    3265                 :             :                namespace N {
    3266                 :             :                  struct S { friend void f<int>(int); }
    3267                 :             :                }
    3268                 :             : 
    3269                 :             :              we will have set the DECL_CONTEXT for the friend
    3270                 :             :              declaration to N, rather than to the global namespace.  */
    3271                 :     1350183 :           if (DECL_NAMESPACE_SCOPE_P (decl))
    3272                 :      895483 :             DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
    3273                 :             : 
    3274                 :     1350183 :           if (is_friend && !have_def)
    3275                 :             :             /* This is not really a declaration of a specialization.
    3276                 :             :                It's just the name of an instantiation.  But, it's not
    3277                 :             :                a request for an instantiation, either.  */
    3278                 :          71 :             SET_DECL_IMPLICIT_INSTANTIATION (decl);
    3279                 :     1350112 :           else if (TREE_CODE (decl) == FUNCTION_DECL)
    3280                 :             :             /* A specialization is not necessarily COMDAT.  */
    3281                 :      542850 :             DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
    3282                 :      542850 :                                   && DECL_DECLARED_INLINE_P (decl));
    3283                 :      807262 :           else if (VAR_P (decl))
    3284                 :      807262 :             DECL_COMDAT (decl) = false;
    3285                 :             : 
    3286                 :             :           /* If this is a full specialization, register it so that we can find
    3287                 :             :              it again.  Partial specializations will be registered in
    3288                 :             :              process_partial_specialization.  */
    3289                 :     1350183 :           if (!processing_template_decl)
    3290                 :             :             {
    3291                 :      806472 :               warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
    3292                 :             : 
    3293                 :      806472 :               decl = register_specialization (decl, gen_tmpl, targs,
    3294                 :             :                                               is_friend, 0);
    3295                 :             :             }
    3296                 :             : 
    3297                 :             :           /* If this is a specialization, splice any contracts that may have
    3298                 :             :              been inherited from the template, removing them.  */
    3299                 :     1350183 :           if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl))
    3300                 :     1350083 :             remove_contract_attributes (decl);
    3301                 :             : 
    3302                 :             :           /* A 'structor should already have clones.  */
    3303                 :     1893078 :           gcc_assert (decl == error_mark_node
    3304                 :             :                       || variable_template_p (tmpl)
    3305                 :             :                       || !(DECL_CONSTRUCTOR_P (decl)
    3306                 :             :                            || DECL_DESTRUCTOR_P (decl))
    3307                 :             :                       || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    3308                 :             :         }
    3309                 :             :     }
    3310                 :             : 
    3311                 :             :   return decl;
    3312                 :             : }
    3313                 :             : 
    3314                 :             : /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
    3315                 :             :    parameters.  These are represented in the same format used for
    3316                 :             :    DECL_TEMPLATE_PARMS.  */
    3317                 :             : 
    3318                 :             : int
    3319                 :   303069136 : comp_template_parms (const_tree parms1, const_tree parms2)
    3320                 :             : {
    3321                 :   303069136 :   if (parms1 == parms2)
    3322                 :             :     return 1;
    3323                 :             : 
    3324                 :   302581082 :   tree t1 = TREE_VALUE (parms1);
    3325                 :   302581082 :   tree t2 = TREE_VALUE (parms2);
    3326                 :   302581082 :   int i;
    3327                 :             : 
    3328                 :   302581082 :   gcc_assert (TREE_CODE (t1) == TREE_VEC);
    3329                 :   302581082 :   gcc_assert (TREE_CODE (t2) == TREE_VEC);
    3330                 :             : 
    3331                 :   302581082 :   if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
    3332                 :             :     return 0;
    3333                 :             : 
    3334                 :   303613361 :   for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
    3335                 :             :     {
    3336                 :   222117560 :       tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
    3337                 :   222117560 :       tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
    3338                 :             : 
    3339                 :             :       /* If either of the template parameters are invalid, assume
    3340                 :             :          they match for the sake of error recovery. */
    3341                 :   222117560 :       if (error_operand_p (parm1) || error_operand_p (parm2))
    3342                 :             :         return 1;
    3343                 :             : 
    3344                 :   222117525 :       if (TREE_CODE (parm1) != TREE_CODE (parm2))
    3345                 :             :         return 0;
    3346                 :             : 
    3347                 :   392169380 :       if (TREE_CODE (parm1) == TYPE_DECL
    3348                 :   212622154 :           && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1))
    3349                 :   191166463 :               == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2))))
    3350                 :   179547226 :         continue;
    3351                 :    33074928 :       else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
    3352                 :             :         return 0;
    3353                 :             :     }
    3354                 :             : 
    3355                 :             :   return 1;
    3356                 :             : }
    3357                 :             : 
    3358                 :             : /* Returns true if two template parameters are declared with
    3359                 :             :    equivalent constraints.  */
    3360                 :             : 
    3361                 :             : static bool
    3362                 :   118450025 : template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
    3363                 :             : {
    3364                 :   118450025 :   tree req1 = TREE_TYPE (parm1);
    3365                 :   118450025 :   tree req2 = TREE_TYPE (parm2);
    3366                 :   118450025 :   if (!req1 != !req2)
    3367                 :             :     return false;
    3368                 :   117975719 :   if (req1)
    3369                 :      221828 :     return cp_tree_equal (req1, req2);
    3370                 :             :   return true;
    3371                 :             : }
    3372                 :             : 
    3373                 :             : /* Returns true when two template parameters are equivalent.  */
    3374                 :             : 
    3375                 :             : static bool
    3376                 :   130771671 : template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
    3377                 :             : {
    3378                 :   130771671 :   tree decl1 = TREE_VALUE (parm1);
    3379                 :   130771671 :   tree decl2 = TREE_VALUE (parm2);
    3380                 :             : 
    3381                 :             :   /* If either of the template parameters are invalid, assume
    3382                 :             :      they match for the sake of error recovery. */
    3383                 :   130771671 :   if (error_operand_p (decl1) || error_operand_p (decl2))
    3384                 :             :     return true;
    3385                 :             : 
    3386                 :             :   /* ... they declare parameters of the same kind.  */
    3387                 :   130771670 :   if (TREE_CODE (decl1) != TREE_CODE (decl2))
    3388                 :             :     return false;
    3389                 :             : 
    3390                 :             :   /* ... one parameter was introduced by a parameter declaration, then
    3391                 :             :      both are. This case arises as a result of eagerly rewriting declarations
    3392                 :             :      during parsing.  */
    3393                 :   126391552 :   if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1)
    3394                 :   126391552 :       != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2))
    3395                 :             :     return false;
    3396                 :             : 
    3397                 :             :   /* ... if either declares a pack, they both do.  */
    3398                 :   126391356 :   if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
    3399                 :             :     return false;
    3400                 :             : 
    3401                 :   115077476 :   if (TREE_CODE (decl1) == PARM_DECL)
    3402                 :             :     {
    3403                 :             :       /* ... if they declare non-type parameters, the types are equivalent.  */
    3404                 :      569783 :       if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
    3405                 :             :         return false;
    3406                 :             :     }
    3407                 :   114507693 :   else if (TREE_CODE (decl2) == TEMPLATE_DECL)
    3408                 :             :     {
    3409                 :             :       /* ... if they declare template template parameters, their template
    3410                 :             :          parameter lists are equivalent.  */
    3411                 :        1782 :       if (!template_heads_equivalent_p (decl1, decl2))
    3412                 :             :         return false;
    3413                 :             :     }
    3414                 :             : 
    3415                 :             :   /* ... if they are declared with a qualified-concept name, they both
    3416                 :             :      are, and those names are equivalent.  */
    3417                 :   115057478 :   return template_parameter_constraints_equivalent_p (parm1, parm2);
    3418                 :             : }
    3419                 :             : 
    3420                 :             : /* Returns true if two template parameters lists are equivalent.
    3421                 :             :    Two template parameter lists are equivalent if they have the
    3422                 :             :    same length and their corresponding parameters are equivalent.
    3423                 :             : 
    3424                 :             :    PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
    3425                 :             :    data structure returned by DECL_TEMPLATE_PARMS.
    3426                 :             : 
    3427                 :             :    This is generally the same implementation as comp_template_parms
    3428                 :             :    except that it also the concept names and arguments used to
    3429                 :             :    introduce parameters.  */
    3430                 :             : 
    3431                 :             : static bool
    3432                 :   198532218 : template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
    3433                 :             : {
    3434                 :   198532218 :   if (parms1 == parms2)
    3435                 :             :     return true;
    3436                 :             : 
    3437                 :   198532218 :   tree list1 = TREE_VALUE (parms1);
    3438                 :   198532218 :   tree list2 = TREE_VALUE (parms2);
    3439                 :             : 
    3440                 :   198532218 :   if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
    3441                 :             :     return 0;
    3442                 :             : 
    3443                 :   194564128 :   for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
    3444                 :             :     {
    3445                 :   130771671 :       tree parm1 = TREE_VEC_ELT (list1, i);
    3446                 :   130771671 :       tree parm2 = TREE_VEC_ELT (list2, i);
    3447                 :   130771671 :       if (!template_parameters_equivalent_p (parm1, parm2))
    3448                 :             :         return false;
    3449                 :             :     }
    3450                 :             : 
    3451                 :             :   return true;
    3452                 :             : }
    3453                 :             : 
    3454                 :             : /* Return true if the requires-clause of the template parameter lists are
    3455                 :             :    equivalent and false otherwise.  */
    3456                 :             : static bool
    3457                 :    77852308 : template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
    3458                 :             : {
    3459                 :    77852308 :   tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
    3460                 :    77852308 :   tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
    3461                 :    77852308 :   if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
    3462                 :             :     return false;
    3463                 :    77777714 :   if (!cp_tree_equal (req1, req2))
    3464                 :             :     return false;
    3465                 :             :   return true;
    3466                 :             : }
    3467                 :             : 
    3468                 :             : /* Returns true if two template heads are equivalent. 17.6.6.1p6:
    3469                 :             :    Two template heads are equivalent if their template parameter
    3470                 :             :    lists are equivalent and their requires clauses are equivalent.
    3471                 :             : 
    3472                 :             :    In pre-C++20, this is equivalent to calling comp_template_parms
    3473                 :             :    for the template parameters of TMPL1 and TMPL2.  */
    3474                 :             : 
    3475                 :             : bool
    3476                 :   356632954 : template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
    3477                 :             : {
    3478                 :   356632954 :   tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
    3479                 :   356632954 :   tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
    3480                 :             : 
    3481                 :             :   /* Don't change the matching rules for pre-C++20.  */
    3482                 :   356632954 :   if (cxx_dialect < cxx20)
    3483                 :   158100736 :     return comp_template_parms (parms1, parms2);
    3484                 :             : 
    3485                 :             :   /* ... have the same number of template parameters, and their
    3486                 :             :      corresponding parameters are equivalent.  */
    3487                 :   198532218 :   if (!template_parameter_lists_equivalent_p (parms1, parms2))
    3488                 :             :     return false;
    3489                 :             : 
    3490                 :             :   /* ... if either has a requires-clause, they both do and their
    3491                 :             :      corresponding constraint-expressions are equivalent.  */
    3492                 :    63792457 :   return template_requirements_equivalent_p (parms1, parms2);
    3493                 :             : }
    3494                 :             : 
    3495                 :             : /* Determine whether PARM is a parameter pack.  */
    3496                 :             : 
    3497                 :             : bool
    3498                 :  3252122609 : template_parameter_pack_p (const_tree parm)
    3499                 :             : {
    3500                 :             :   /* Determine if we have a non-type template parameter pack.  */
    3501                 :  3252122609 :   if (TREE_CODE (parm) == PARM_DECL)
    3502                 :   402541122 :     return (DECL_TEMPLATE_PARM_P (parm)
    3503                 :   402541122 :             && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
    3504                 :  2849581487 :   if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
    3505                 :     1303699 :     return TEMPLATE_PARM_PARAMETER_PACK (parm);
    3506                 :             : 
    3507                 :             :   /* If this is a list of template parameters, we could get a
    3508                 :             :      TYPE_DECL or a TEMPLATE_DECL.  */
    3509                 :  2848277788 :   if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
    3510                 :  2697597572 :     parm = TREE_TYPE (parm);
    3511                 :             : 
    3512                 :             :   /* Otherwise it must be a type template parameter.  */
    3513                 :  2848277788 :   return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    3514                 :  2848277788 :            || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    3515                 :  2848277788 :           && TEMPLATE_TYPE_PARAMETER_PACK (parm));
    3516                 :             : }
    3517                 :             : 
    3518                 :             : /* Determine if T is a function parameter pack.  */
    3519                 :             : 
    3520                 :             : bool
    3521                 :     1369554 : function_parameter_pack_p (const_tree t)
    3522                 :             : {
    3523                 :     1369554 :   if (t && TREE_CODE (t) == PARM_DECL)
    3524                 :     1369554 :     return DECL_PACK_P (t);
    3525                 :             :   return false;
    3526                 :             : }
    3527                 :             : 
    3528                 :             : /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
    3529                 :             :    PRIMARY_FUNC_TMPL_INST is a primary function template instantiation.  */
    3530                 :             : 
    3531                 :             : tree
    3532                 :     1516685 : get_function_template_decl (const_tree primary_func_tmpl_inst)
    3533                 :             : {
    3534                 :     1516685 :   if (! primary_func_tmpl_inst
    3535                 :     1516685 :       || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
    3536                 :     3033370 :       || ! primary_template_specialization_p (primary_func_tmpl_inst))
    3537                 :     1127789 :     return NULL;
    3538                 :             : 
    3539                 :      388896 :   return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
    3540                 :             : }
    3541                 :             : 
    3542                 :             : /* Return true iff the function parameter PARAM_DECL was expanded
    3543                 :             :    from the function parameter pack PACK.  */
    3544                 :             : 
    3545                 :             : bool
    3546                 :      385081 : function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
    3547                 :             : {
    3548                 :      385081 :   if (DECL_ARTIFICIAL (param_decl)
    3549                 :      385081 :       || !function_parameter_pack_p (pack))
    3550                 :           0 :     return false;
    3551                 :             : 
    3552                 :             :   /* The parameter pack and its pack arguments have the same
    3553                 :             :      DECL_PARM_INDEX.  */
    3554                 :      385081 :   return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
    3555                 :             : }
    3556                 :             : 
    3557                 :             : /* Determine whether ARGS describes a variadic template args list,
    3558                 :             :    i.e., one that is terminated by a template argument pack.  */
    3559                 :             : 
    3560                 :             : static bool
    3561                 :           8 : template_args_variadic_p (tree args)
    3562                 :             : {
    3563                 :           8 :   int nargs;
    3564                 :           8 :   tree last_parm;
    3565                 :             : 
    3566                 :           8 :   if (args == NULL_TREE)
    3567                 :             :     return false;
    3568                 :             : 
    3569                 :           8 :   args = INNERMOST_TEMPLATE_ARGS (args);
    3570                 :           8 :   nargs = TREE_VEC_LENGTH (args);
    3571                 :             : 
    3572                 :           8 :   if (nargs == 0)
    3573                 :             :     return false;
    3574                 :             : 
    3575                 :           8 :   last_parm = TREE_VEC_ELT (args, nargs - 1);
    3576                 :             : 
    3577                 :           8 :   return ARGUMENT_PACK_P (last_parm);
    3578                 :             : }
    3579                 :             : 
    3580                 :             : /* Generate a new name for the parameter pack name NAME (an
    3581                 :             :    IDENTIFIER_NODE) that incorporates its */
    3582                 :             : 
    3583                 :             : static tree
    3584                 :      786687 : make_ith_pack_parameter_name (tree name, int i)
    3585                 :             : {
    3586                 :             :   /* Munge the name to include the parameter index.  */
    3587                 :             : #define NUMBUF_LEN 128
    3588                 :      786687 :   char numbuf[NUMBUF_LEN];
    3589                 :      786687 :   char* newname;
    3590                 :      786687 :   int newname_len;
    3591                 :             : 
    3592                 :      786687 :   if (name == NULL_TREE)
    3593                 :             :     return name;
    3594                 :      783701 :   snprintf (numbuf, NUMBUF_LEN, "%i", i);
    3595                 :      783701 :   newname_len = IDENTIFIER_LENGTH (name)
    3596                 :      783701 :                 + strlen (numbuf) + 2;
    3597                 :      783701 :   newname = (char*)alloca (newname_len);
    3598                 :      783701 :   snprintf (newname, newname_len,
    3599                 :      783701 :             "%s#%i", IDENTIFIER_POINTER (name), i);
    3600                 :      783701 :   return get_identifier (newname);
    3601                 :             : }
    3602                 :             : 
    3603                 :             : /* Return true if T is a primary function, class or alias template
    3604                 :             :    specialization, not including the template pattern.  */
    3605                 :             : 
    3606                 :             : bool
    3607                 :   122940020 : primary_template_specialization_p (const_tree t)
    3608                 :             : {
    3609                 :   122940020 :   if (!t)
    3610                 :             :     return false;
    3611                 :             : 
    3612                 :   122940020 :   if (VAR_OR_FUNCTION_DECL_P (t))
    3613                 :    64516885 :     return (DECL_LANG_SPECIFIC (t)
    3614                 :    64516786 :             && DECL_USE_TEMPLATE (t)
    3615                 :    60879929 :             && DECL_TEMPLATE_INFO (t)
    3616                 :   125396814 :             && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
    3617                 :    58423135 :   else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    3618                 :    58423135 :     return (CLASSTYPE_TEMPLATE_INFO (t)
    3619                 :    58423135 :             && CLASSTYPE_USE_TEMPLATE (t)
    3620                 :   116846270 :             && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
    3621                 :           0 :   else if (alias_template_specialization_p (t, nt_transparent))
    3622                 :             :     return true;
    3623                 :             :   return false;
    3624                 :             : }
    3625                 :             : 
    3626                 :             : /* Return true if PARM is a template template parameter.  */
    3627                 :             : 
    3628                 :             : bool
    3629                 :    39474064 : template_template_parameter_p (const_tree parm)
    3630                 :             : {
    3631                 :    39474064 :   return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
    3632                 :             : }
    3633                 :             : 
    3634                 :             : /* Return true iff PARM is a DECL representing a type template
    3635                 :             :    parameter.  */
    3636                 :             : 
    3637                 :             : bool
    3638                 :  1264242008 : template_type_parameter_p (const_tree parm)
    3639                 :             : {
    3640                 :  1264242008 :   return (parm
    3641                 :  1264242008 :           && (TREE_CODE (parm) == TYPE_DECL
    3642                 :   611369354 :               || TREE_CODE (parm) == TEMPLATE_DECL)
    3643                 :  1917261090 :           && DECL_TEMPLATE_PARM_P (parm));
    3644                 :             : }
    3645                 :             : 
    3646                 :             : /* Return the template parameters of T if T is a
    3647                 :             :    primary template instantiation, NULL otherwise.  */
    3648                 :             : 
    3649                 :             : tree
    3650                 :   136619858 : get_primary_template_innermost_parameters (const_tree t)
    3651                 :             : {
    3652                 :   136619858 :   tree parms = NULL, template_info = NULL;
    3653                 :             : 
    3654                 :   136619858 :   if ((template_info = get_template_info (t))
    3655                 :   136619858 :       && primary_template_specialization_p (t))
    3656                 :    58466994 :     parms = INNERMOST_TEMPLATE_PARMS
    3657                 :             :         (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
    3658                 :             : 
    3659                 :   136619858 :   return parms;
    3660                 :             : }
    3661                 :             : 
    3662                 :             : /* Returns the template arguments of T if T is a template instantiation,
    3663                 :             :    NULL otherwise.  */
    3664                 :             : 
    3665                 :             : tree
    3666                 :    27954016 : get_template_innermost_arguments (const_tree t)
    3667                 :             : {
    3668                 :    27954016 :   tree args = NULL, template_info = NULL;
    3669                 :             : 
    3670                 :    27954016 :   if ((template_info = get_template_info (t))
    3671                 :    55908032 :       && TI_ARGS (template_info))
    3672                 :    27954016 :     args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
    3673                 :             : 
    3674                 :    27954016 :   return args;
    3675                 :             : }
    3676                 :             : 
    3677                 :             : /* Return the argument pack elements of T if T is a template argument pack,
    3678                 :             :    NULL otherwise.  */
    3679                 :             : 
    3680                 :             : tree
    3681                 :    43216735 : get_template_argument_pack_elems (const_tree t)
    3682                 :             : {
    3683                 :    43216735 :   if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
    3684                 :    43216735 :       && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
    3685                 :             :     return NULL;
    3686                 :             : 
    3687                 :     4145750 :   return ARGUMENT_PACK_ARGS (t);
    3688                 :             : }
    3689                 :             : 
    3690                 :             : /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
    3691                 :             :    ARGUMENT_PACK_SELECT represents. */
    3692                 :             : 
    3693                 :             : static tree
    3694                 :    10016326 : argument_pack_select_arg (tree t)
    3695                 :             : {
    3696                 :    10016326 :   tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
    3697                 :    10016326 :   tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
    3698                 :             : 
    3699                 :             :   /* If the selected argument is an expansion E, that most likely means we were
    3700                 :             :      called from gen_elem_of_pack_expansion_instantiation during the
    3701                 :             :      substituting of an argument pack (of which the Ith element is a pack
    3702                 :             :      expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
    3703                 :             :      In this case, the Ith element resulting from this substituting is going to
    3704                 :             :      be a pack expansion, which pattern is the pattern of E.  Let's return the
    3705                 :             :      pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
    3706                 :             :      resulting pack expansion from it.  */
    3707                 :    10016326 :   if (PACK_EXPANSION_P (arg))
    3708                 :             :     {
    3709                 :             :       /* Make sure we aren't throwing away arg info.  */
    3710                 :      397990 :       gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
    3711                 :      397990 :       arg = PACK_EXPANSION_PATTERN (arg);
    3712                 :             :     }
    3713                 :             : 
    3714                 :    10016326 :   return arg;
    3715                 :             : }
    3716                 :             : 
    3717                 :             : /* Return a modification of ARGS that's suitable for preserving inside a hash
    3718                 :             :    table.  In particular, this replaces each ARGUMENT_PACK_SELECT with its
    3719                 :             :    underlying argument.  ARGS is copied (upon modification) iff COW_P.  */
    3720                 :             : 
    3721                 :             : static tree
    3722                 :      291337 : preserve_args (tree args, bool cow_p = true)
    3723                 :             : {
    3724                 :      291337 :   if (!args)
    3725                 :             :     return NULL_TREE;
    3726                 :             : 
    3727                 :      774743 :   for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    3728                 :             :     {
    3729                 :      483408 :       tree t = TREE_VEC_ELT (args, i);
    3730                 :      483408 :       tree r;
    3731                 :      483408 :       if (!t)
    3732                 :             :         r = NULL_TREE;
    3733                 :      483393 :       else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT)
    3734                 :          27 :         r = argument_pack_select_arg (t);
    3735                 :      483366 :       else if (TREE_CODE (t) == TREE_VEC)
    3736                 :       84192 :         r = preserve_args (t, cow_p);
    3737                 :             :       else
    3738                 :             :         r = t;
    3739                 :      483393 :       if (r != t)
    3740                 :             :         {
    3741                 :          27 :           if (cow_p)
    3742                 :             :             {
    3743                 :          25 :               args = copy_template_args (args);
    3744                 :          25 :               cow_p = false;
    3745                 :             :             }
    3746                 :          27 :           TREE_VEC_ELT (args, i) = r;
    3747                 :             :         }
    3748                 :             :     }
    3749                 :             : 
    3750                 :             :   return args;
    3751                 :             : }
    3752                 :             : 
    3753                 :             : /* True iff FN is a function representing a built-in variadic parameter
    3754                 :             :    pack.  */
    3755                 :             : 
    3756                 :             : bool
    3757                 :   275443627 : builtin_pack_fn_p (tree fn)
    3758                 :             : {
    3759                 :   275443627 :   if (!fn
    3760                 :   275419848 :       || TREE_CODE (fn) != FUNCTION_DECL
    3761                 :   362266003 :       || !DECL_IS_UNDECLARED_BUILTIN (fn))
    3762                 :             :     return false;
    3763                 :             : 
    3764                 :     8968612 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3765                 :             :     return true;
    3766                 :             : 
    3767                 :             :   return false;
    3768                 :             : }
    3769                 :             : 
    3770                 :             : /* True iff CALL is a call to a function representing a built-in variadic
    3771                 :             :    parameter pack.  */
    3772                 :             : 
    3773                 :             : static bool
    3774                 :   261548356 : builtin_pack_call_p (tree call)
    3775                 :             : {
    3776                 :   261548356 :   if (TREE_CODE (call) != CALL_EXPR)
    3777                 :             :     return false;
    3778                 :   197064024 :   return builtin_pack_fn_p (CALL_EXPR_FN (call));
    3779                 :             : }
    3780                 :             : 
    3781                 :             : /* Return a TREE_VEC for the expansion of __integer_pack(HI).  */
    3782                 :             : 
    3783                 :             : static tree
    3784                 :      101621 : expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
    3785                 :             :                      tree in_decl)
    3786                 :             : {
    3787                 :      101621 :   tree ohi = CALL_EXPR_ARG (call, 0);
    3788                 :      101621 :   tree hi = tsubst_expr (ohi, args, complain, in_decl);
    3789                 :             : 
    3790                 :      101621 :   if (instantiation_dependent_expression_p (hi))
    3791                 :             :     {
    3792                 :      100399 :       if (hi != ohi)
    3793                 :             :         {
    3794                 :      100399 :           call = copy_node (call);
    3795                 :      100399 :           CALL_EXPR_ARG (call, 0) = hi;
    3796                 :             :         }
    3797                 :      100399 :       tree ex = make_pack_expansion (call, complain);
    3798                 :      100399 :       tree vec = make_tree_vec (1);
    3799                 :      100399 :       TREE_VEC_ELT (vec, 0) = ex;
    3800                 :      100399 :       return vec;
    3801                 :             :     }
    3802                 :             :   else
    3803                 :             :     {
    3804                 :        1222 :       hi = instantiate_non_dependent_expr (hi, complain);
    3805                 :        1222 :       hi = cxx_constant_value (hi, complain);
    3806                 :        1222 :       int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
    3807                 :             : 
    3808                 :             :       /* Calculate the largest value of len that won't make the size of the vec
    3809                 :             :          overflow an int.  The compiler will exceed resource limits long before
    3810                 :             :          this, but it seems a decent place to diagnose.  */
    3811                 :        1222 :       int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
    3812                 :             : 
    3813                 :        1222 :       if (len < 0 || len > max)
    3814                 :             :         {
    3815                 :          12 :           if ((complain & tf_error)
    3816                 :           9 :               && hi != error_mark_node)
    3817                 :           9 :             error ("argument to %<__integer_pack%> must be between 0 and %d",
    3818                 :             :                    max);
    3819                 :          12 :           return error_mark_node;
    3820                 :             :         }
    3821                 :             : 
    3822                 :        1210 :       tree vec = make_tree_vec (len);
    3823                 :             : 
    3824                 :        5577 :       for (int i = 0; i < len; ++i)
    3825                 :        4367 :         TREE_VEC_ELT (vec, i) = size_int (i);
    3826                 :             : 
    3827                 :             :       return vec;
    3828                 :             :     }
    3829                 :             : }
    3830                 :             : 
    3831                 :             : /* Return a TREE_VEC for the expansion of built-in template parameter pack
    3832                 :             :    CALL.  */
    3833                 :             : 
    3834                 :             : static tree
    3835                 :      101621 : expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
    3836                 :             :                           tree in_decl)
    3837                 :             : {
    3838                 :      101621 :   if (!builtin_pack_call_p (call))
    3839                 :             :     return NULL_TREE;
    3840                 :             : 
    3841                 :      101621 :   tree fn = CALL_EXPR_FN (call);
    3842                 :             : 
    3843                 :      101621 :   if (id_equal (DECL_NAME (fn), "__integer_pack"))
    3844                 :      101621 :     return expand_integer_pack (call, args, complain, in_decl);
    3845                 :             : 
    3846                 :             :   return NULL_TREE;
    3847                 :             : }
    3848                 :             : 
    3849                 :             : /* Return true if the tree T has the extra args mechanism for
    3850                 :             :    avoiding partial instantiation.  */
    3851                 :             : 
    3852                 :             : static bool
    3853                 :  6076510721 : has_extra_args_mechanism_p (const_tree t)
    3854                 :             : {
    3855                 :  6076510721 :   return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS  */
    3856                 :  6044853333 :           || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS  */
    3857                 :  6044338223 :           || (TREE_CODE (t) == IF_STMT
    3858                 :      564836 :               && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS  */
    3859                 : 12120618936 :           || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS  */
    3860                 :             : }
    3861                 :             : 
    3862                 :             : /* Return *_EXTRA_ARGS of the given supported tree T.  */
    3863                 :             : 
    3864                 :             : static tree&
    3865                 :       22429 : tree_extra_args (tree t)
    3866                 :             : {
    3867                 :       22429 :   gcc_checking_assert (has_extra_args_mechanism_p (t));
    3868                 :             : 
    3869                 :       22429 :   if (PACK_EXPANSION_P (t))
    3870                 :         538 :     return PACK_EXPANSION_EXTRA_ARGS (t);
    3871                 :       21891 :   else if (TREE_CODE (t) == REQUIRES_EXPR)
    3872                 :        1086 :     return REQUIRES_EXPR_EXTRA_ARGS (t);
    3873                 :       20805 :   else if (TREE_CODE (t) == IF_STMT
    3874                 :       20805 :            && IF_STMT_CONSTEXPR_P (t))
    3875                 :       20629 :     return IF_STMT_EXTRA_ARGS (t);
    3876                 :         176 :   else if (TREE_CODE (t) == LAMBDA_EXPR)
    3877                 :         176 :     return LAMBDA_EXPR_EXTRA_ARGS (t);
    3878                 :             : 
    3879                 :           0 :   gcc_unreachable ();
    3880                 :             : }
    3881                 :             : 
    3882                 :             : /* Structure used to track the progress of find_parameter_packs_r.  */
    3883                 :  1691810799 : struct find_parameter_pack_data
    3884                 :             : {
    3885                 :             :   /* TREE_LIST that will contain all of the parameter packs found by
    3886                 :             :      the traversal.  */
    3887                 :             :   tree* parameter_packs;
    3888                 :             : 
    3889                 :             :   /* Set of AST nodes that have been visited by the traversal.  */
    3890                 :             :   hash_set<tree> *visited;
    3891                 :             : 
    3892                 :             :   /* True iff we're making a type pack expansion.  */
    3893                 :             :   bool type_pack_expansion_p;
    3894                 :             : 
    3895                 :             :   /* True iff we found a subtree that has the extra args mechanism.  */
    3896                 :             :   bool found_extra_args_tree_p = false;
    3897                 :             : };
    3898                 :             : 
    3899                 :             : /* Identifies all of the argument packs that occur in a template
    3900                 :             :    argument and appends them to the TREE_LIST inside DATA, which is a
    3901                 :             :    find_parameter_pack_data structure. This is a subroutine of
    3902                 :             :    make_pack_expansion and uses_parameter_packs.  */
    3903                 :             : static tree
    3904                 :  6244301254 : find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
    3905                 :             : {
    3906                 :  6244301254 :   tree t = *tp;
    3907                 :  6244301254 :   struct find_parameter_pack_data* ppd =
    3908                 :             :     (struct find_parameter_pack_data*)data;
    3909                 :  6244301254 :   bool parameter_pack_p = false;
    3910                 :             : 
    3911                 :             : #define WALK_SUBTREE(NODE)                              \
    3912                 :             :   cp_walk_tree (&(NODE), &find_parameter_packs_r,       \
    3913                 :             :                 ppd, ppd->visited)                   \
    3914                 :             : 
    3915                 :             :   /* Don't look through typedefs; we are interested in whether a
    3916                 :             :      parameter pack is actually written in the expression/type we're
    3917                 :             :      looking at, not the target type.  */
    3918                 :  6244301254 :   if (TYPE_P (t) && typedef_variant_p (t))
    3919                 :             :     {
    3920                 :             :       /* But do look at arguments for an alias template.  */
    3921                 :   168927882 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    3922                 :   127182954 :         cp_walk_tree (&TI_ARGS (tinfo),
    3923                 :             :                       &find_parameter_packs_r,
    3924                 :             :                       ppd, ppd->visited);
    3925                 :   168927882 :       *walk_subtrees = 0;
    3926                 :   168927882 :       return NULL_TREE;
    3927                 :             :     }
    3928                 :             : 
    3929                 :             :   /* Identify whether this is a parameter pack or not.  */
    3930                 :  6075373372 :   switch (TREE_CODE (t))
    3931                 :             :     {
    3932                 :    61416400 :     case TEMPLATE_PARM_INDEX:
    3933                 :    61416400 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    3934                 :             :         parameter_pack_p = true;
    3935                 :             :       break;
    3936                 :             : 
    3937                 :  1042763198 :     case TEMPLATE_TYPE_PARM:
    3938                 :  1042763198 :       t = TYPE_MAIN_VARIANT (t);
    3939                 :             :       /* FALLTHRU */
    3940                 :  1043444598 :     case TEMPLATE_TEMPLATE_PARM:
    3941                 :             :       /* If the placeholder appears in the decl-specifier-seq of a function
    3942                 :             :          parameter pack (14.6.3), or the type-specifier-seq of a type-id that
    3943                 :             :          is a pack expansion, the invented template parameter is a template
    3944                 :             :          parameter pack.  */
    3945                 :    47254936 :       if (ppd->type_pack_expansion_p && is_auto (t)
    3946                 :  1043444623 :           && TEMPLATE_TYPE_LEVEL (t) != 0)
    3947                 :          23 :         TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
    3948                 :  1043444598 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    3949                 :             :         parameter_pack_p = true;
    3950                 :             :       break;
    3951                 :             : 
    3952                 :   352605084 :     case FIELD_DECL:
    3953                 :   352605084 :     case PARM_DECL:
    3954                 :   352605084 :       if (DECL_PACK_P (t))
    3955                 :             :         {
    3956                 :             :           /* We don't want to walk into the type of a PARM_DECL,
    3957                 :             :              because we don't want to see the type parameter pack.  */
    3958                 :     2685503 :           *walk_subtrees = 0;
    3959                 :     2685503 :           parameter_pack_p = true;
    3960                 :             :         }
    3961                 :             :       break;
    3962                 :             : 
    3963                 :   149275613 :     case VAR_DECL:
    3964                 :   149275613 :       if (DECL_PACK_P (t))
    3965                 :             :         {
    3966                 :             :           /* We don't want to walk into the type of a variadic capture proxy,
    3967                 :             :              because we don't want to see the type parameter pack.  */
    3968                 :          40 :           *walk_subtrees = 0;
    3969                 :          40 :           parameter_pack_p = true;
    3970                 :             :         }
    3971                 :   149275573 :       else if (variable_template_specialization_p (t))
    3972                 :             :         {
    3973                 :       56568 :           cp_walk_tree (&DECL_TI_ARGS (t),
    3974                 :             :                         find_parameter_packs_r,
    3975                 :             :                         ppd, ppd->visited);
    3976                 :       56568 :           *walk_subtrees = 0;
    3977                 :             :         }
    3978                 :             :       break;
    3979                 :             : 
    3980                 :   196860780 :     case CALL_EXPR:
    3981                 :   196860780 :       if (builtin_pack_call_p (t))
    3982                 :             :         parameter_pack_p = true;
    3983                 :             :       break;
    3984                 :             : 
    3985                 :             :     case BASES:
    3986                 :             :       parameter_pack_p = true;
    3987                 :             :       break;
    3988                 :             :     default:
    3989                 :             :       /* Not a parameter pack.  */
    3990                 :             :       break;
    3991                 :             :     }
    3992                 :             : 
    3993                 :     2742111 :   if (parameter_pack_p)
    3994                 :             :     {
    3995                 :             :       /* Add this parameter pack to the list.  */
    3996                 :    57839751 :       *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
    3997                 :             :     }
    3998                 :             : 
    3999                 :  6075373372 :   if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t))
    4000                 :     1321658 :     ppd->found_extra_args_tree_p = true;
    4001                 :             : 
    4002                 :  6075373372 :   if (TYPE_P (t))
    4003                 :  1997349623 :     cp_walk_tree (&TYPE_CONTEXT (t),
    4004                 :             :                   &find_parameter_packs_r, ppd, ppd->visited);
    4005                 :             : 
    4006                 :             :   /* This switch statement will return immediately if we don't find a
    4007                 :             :      parameter pack.  ??? Should some of these be in cp_walk_subtrees?  */
    4008                 :  6075373372 :   switch (TREE_CODE (t))
    4009                 :             :     {
    4010                 :      319672 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    4011                 :             :       /* Check the template itself.  */
    4012                 :      319672 :       cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
    4013                 :             :                     &find_parameter_packs_r, ppd, ppd->visited);
    4014                 :      319672 :       return NULL_TREE;
    4015                 :             : 
    4016                 :     1198883 :     case DECL_EXPR:
    4017                 :     1198883 :       {
    4018                 :     1198883 :         tree decl = DECL_EXPR_DECL (t);
    4019                 :             :         /* Ignore the declaration of a capture proxy for a parameter pack.  */
    4020                 :     1198883 :         if (is_capture_proxy (decl))
    4021                 :      873478 :           *walk_subtrees = 0;
    4022                 :     1198883 :         if (is_typedef_decl (decl))
    4023                 :             :           /* Since we stop at typedefs above, we need to look through them at
    4024                 :             :              the point of the DECL_EXPR.  */
    4025                 :       64303 :           cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
    4026                 :             :                         &find_parameter_packs_r, ppd, ppd->visited);
    4027                 :             :         return NULL_TREE;
    4028                 :             :       }
    4029                 :             : 
    4030                 :   170648848 :     case TEMPLATE_DECL:
    4031                 :   170648848 :       if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
    4032                 :             :         return NULL_TREE;
    4033                 :        5585 :       cp_walk_tree (&TREE_TYPE (t),
    4034                 :             :                     &find_parameter_packs_r, ppd, ppd->visited);
    4035                 :        5585 :       return NULL_TREE;
    4036                 :             : 
    4037                 :    31656312 :     case TYPE_PACK_EXPANSION:
    4038                 :    31656312 :     case EXPR_PACK_EXPANSION:
    4039                 :    31656312 :       *walk_subtrees = 0;
    4040                 :    31656312 :       return NULL_TREE;
    4041                 :             : 
    4042                 :    26942581 :     case INTEGER_TYPE:
    4043                 :    26942581 :       cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
    4044                 :             :                     ppd, ppd->visited);
    4045                 :    26942581 :       *walk_subtrees = 0;
    4046                 :    26942581 :       return NULL_TREE;
    4047                 :             : 
    4048                 :   183951848 :     case IDENTIFIER_NODE:
    4049                 :   183951848 :       cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
    4050                 :             :                     ppd->visited);
    4051                 :   183951848 :       *walk_subtrees = 0;
    4052                 :   183951848 :       return NULL_TREE;
    4053                 :             : 
    4054                 :      619970 :     case LAMBDA_EXPR:
    4055                 :      619970 :       {
    4056                 :             :         /* Since we defer implicit capture, look in the parms and body.  */
    4057                 :      619970 :         tree fn = lambda_function (t);
    4058                 :      619970 :         cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
    4059                 :             :                       ppd->visited);
    4060                 :      619970 :         cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
    4061                 :             :                       ppd->visited);
    4062                 :      619970 :         return NULL_TREE;
    4063                 :             :       }
    4064                 :             : 
    4065                 :     4896943 :     case DECLTYPE_TYPE:
    4066                 :     4896943 :       {
    4067                 :             :         /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
    4068                 :             :            type_pack_expansion_p to false so that any placeholders
    4069                 :             :            within the expression don't get marked as parameter packs.  */
    4070                 :     4896943 :         bool type_pack_expansion_p = ppd->type_pack_expansion_p;
    4071                 :     4896943 :         ppd->type_pack_expansion_p = false;
    4072                 :     4896943 :         cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
    4073                 :             :                       ppd, ppd->visited);
    4074                 :     4896943 :         ppd->type_pack_expansion_p = type_pack_expansion_p;
    4075                 :     4896943 :         *walk_subtrees = 0;
    4076                 :     4896943 :         return NULL_TREE;
    4077                 :             :       }
    4078                 :             : 
    4079                 :      513453 :     case IF_STMT:
    4080                 :      513453 :       cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
    4081                 :             :                     ppd, ppd->visited);
    4082                 :      513453 :       cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
    4083                 :             :                     ppd, ppd->visited);
    4084                 :      513453 :       cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
    4085                 :             :                     ppd, ppd->visited);
    4086                 :             :       /* Don't walk into IF_STMT_EXTRA_ARGS.  */
    4087                 :      513453 :       *walk_subtrees = 0;
    4088                 :      513453 :       return NULL_TREE;
    4089                 :             : 
    4090                 :          92 :     case TAG_DEFN:
    4091                 :          92 :       t = TREE_TYPE (t);
    4092                 :          92 :       if (CLASS_TYPE_P (t))
    4093                 :             :         {
    4094                 :             :           /* Local class, need to look through the whole definition.
    4095                 :             :              TYPE_BINFO might be unset for a partial instantiation.  */
    4096                 :          47 :           if (TYPE_BINFO (t))
    4097                 :          53 :             for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
    4098                 :           9 :               cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
    4099                 :             :                             ppd, ppd->visited);
    4100                 :             :         }
    4101                 :             :       else
    4102                 :             :         /* Enum, look at the values.  */
    4103                 :         165 :         for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
    4104                 :         120 :           cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
    4105                 :             :                         &find_parameter_packs_r,
    4106                 :             :                         ppd, ppd->visited);
    4107                 :             :       return NULL_TREE;
    4108                 :             : 
    4109                 :     5669039 :     case FUNCTION_TYPE:
    4110                 :     5669039 :     case METHOD_TYPE:
    4111                 :     5669039 :       WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
    4112                 :     5669039 :       break;
    4113                 :             : 
    4114                 :             :     default:
    4115                 :             :       return NULL_TREE;
    4116                 :             :     }
    4117                 :             : 
    4118                 :             : #undef WALK_SUBTREE
    4119                 :             : 
    4120                 :     5669039 :   return NULL_TREE;
    4121                 :             : }
    4122                 :             : 
    4123                 :             : /* Determines if the expression or type T uses any parameter packs.  */
    4124                 :             : tree
    4125                 :    13252111 : uses_parameter_packs (tree t)
    4126                 :             : {
    4127                 :    13252111 :   tree parameter_packs = NULL_TREE;
    4128                 :    13252111 :   struct find_parameter_pack_data ppd;
    4129                 :    13252111 :   ppd.parameter_packs = &parameter_packs;
    4130                 :    13252111 :   ppd.visited = new hash_set<tree>;
    4131                 :    13252111 :   ppd.type_pack_expansion_p = false;
    4132                 :    13252111 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4133                 :    26504222 :   delete ppd.visited;
    4134                 :    13252111 :   return parameter_packs;
    4135                 :             : }
    4136                 :             : 
    4137                 :             : /* Turn ARG, which may be an expression, type, or a TREE_LIST
    4138                 :             :    representation a base-class initializer into a parameter pack
    4139                 :             :    expansion. If all goes well, the resulting node will be an
    4140                 :             :    EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
    4141                 :             :    respectively.  */
    4142                 :             : tree
    4143                 :    53274878 : make_pack_expansion (tree arg, tsubst_flags_t complain)
    4144                 :             : {
    4145                 :    53274878 :   tree result;
    4146                 :    53274878 :   tree parameter_packs = NULL_TREE;
    4147                 :    53274878 :   bool for_types = false;
    4148                 :    53274878 :   struct find_parameter_pack_data ppd;
    4149                 :             : 
    4150                 :    53274878 :   if (!arg || arg == error_mark_node)
    4151                 :             :     return arg;
    4152                 :             : 
    4153                 :    53274866 :   if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
    4154                 :             :     {
    4155                 :             :       /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
    4156                 :             :          class initializer.  In this case, the TREE_PURPOSE will be a
    4157                 :             :          _TYPE node (representing the base class expansion we're
    4158                 :             :          initializing) and the TREE_VALUE will be a TREE_LIST
    4159                 :             :          containing the initialization arguments. 
    4160                 :             : 
    4161                 :             :          The resulting expansion looks somewhat different from most
    4162                 :             :          expansions. Rather than returning just one _EXPANSION, we
    4163                 :             :          return a TREE_LIST whose TREE_PURPOSE is a
    4164                 :             :          TYPE_PACK_EXPANSION containing the bases that will be
    4165                 :             :          initialized.  The TREE_VALUE will be identical to the
    4166                 :             :          original TREE_VALUE, which is a list of arguments that will
    4167                 :             :          be passed to each base.  We do not introduce any new pack
    4168                 :             :          expansion nodes into the TREE_VALUE (although it is possible
    4169                 :             :          that some already exist), because the TREE_PURPOSE and
    4170                 :             :          TREE_VALUE all need to be expanded together with the same
    4171                 :             :          _EXPANSION node.  Note that the TYPE_PACK_EXPANSION in the
    4172                 :             :          resulting TREE_PURPOSE will mention the parameter packs in
    4173                 :             :          both the bases and the arguments to the bases.  */
    4174                 :          30 :       tree purpose;
    4175                 :          30 :       tree value;
    4176                 :          30 :       tree parameter_packs = NULL_TREE;
    4177                 :             : 
    4178                 :             :       /* Determine which parameter packs will be used by the base
    4179                 :             :          class expansion.  */
    4180                 :          30 :       ppd.visited = new hash_set<tree>;
    4181                 :          30 :       ppd.parameter_packs = &parameter_packs;
    4182                 :          30 :       ppd.type_pack_expansion_p = false;
    4183                 :          30 :       gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
    4184                 :          30 :       cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
    4185                 :             :                     &ppd, ppd.visited);
    4186                 :             : 
    4187                 :          30 :       if (parameter_packs == NULL_TREE)
    4188                 :             :         {
    4189                 :           0 :           if (complain & tf_error)
    4190                 :           0 :             error ("base initializer expansion %qT contains no parameter packs",
    4191                 :             :                    arg);
    4192                 :           0 :           delete ppd.visited;
    4193                 :           0 :           return error_mark_node;
    4194                 :             :         }
    4195                 :             : 
    4196                 :          30 :       if (TREE_VALUE (arg) != void_type_node)
    4197                 :             :         {
    4198                 :             :           /* Collect the sets of parameter packs used in each of the
    4199                 :             :              initialization arguments.  */
    4200                 :          57 :           for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
    4201                 :             :             {
    4202                 :             :               /* Determine which parameter packs will be expanded in this
    4203                 :             :                  argument.  */
    4204                 :          33 :               cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, 
    4205                 :             :                             &ppd, ppd.visited);
    4206                 :             :             }
    4207                 :             :         }
    4208                 :             : 
    4209                 :          60 :       delete ppd.visited;
    4210                 :             : 
    4211                 :             :       /* Create the pack expansion type for the base type.  */
    4212                 :          30 :       purpose = cxx_make_type (TYPE_PACK_EXPANSION);
    4213                 :          30 :       PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg);
    4214                 :          60 :       PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
    4215                 :          30 :       PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
    4216                 :             : 
    4217                 :             :       /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4218                 :             :          they will rarely be compared to anything.  */
    4219                 :          30 :       SET_TYPE_STRUCTURAL_EQUALITY (purpose);
    4220                 :             : 
    4221                 :          30 :       return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
    4222                 :             :     }
    4223                 :             : 
    4224                 :    53274836 :   if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
    4225                 :    47043739 :     for_types = true;
    4226                 :             : 
    4227                 :             :   /* Build the PACK_EXPANSION_* node.  */
    4228                 :   106549672 :   result = for_types
    4229                 :    47043739 :      ? cxx_make_type (TYPE_PACK_EXPANSION)
    4230                 :     6231097 :      : make_node (EXPR_PACK_EXPANSION);
    4231                 :    53274836 :   PACK_EXPANSION_PATTERN (result) = arg;
    4232                 :    53274836 :   if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
    4233                 :             :     {
    4234                 :             :       /* Propagate type and const-expression information.  */
    4235                 :     6231097 :       TREE_TYPE (result) = TREE_TYPE (arg);
    4236                 :     6231097 :       TREE_CONSTANT (result) = TREE_CONSTANT (arg);
    4237                 :             :       /* Mark this read now, since the expansion might be length 0.  */
    4238                 :     6231097 :       mark_exp_read (arg);
    4239                 :             :     }
    4240                 :             :   else
    4241                 :             :     /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
    4242                 :             :        they will rarely be compared to anything.  */
    4243                 :    47043739 :     SET_TYPE_STRUCTURAL_EQUALITY (result);
    4244                 :             : 
    4245                 :             :   /* Determine which parameter packs will be expanded.  */
    4246                 :    53274836 :   ppd.parameter_packs = &parameter_packs;
    4247                 :    53274836 :   ppd.visited = new hash_set<tree>;
    4248                 :    53274836 :   ppd.type_pack_expansion_p = TYPE_P (arg);
    4249                 :    53274836 :   cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
    4250                 :   106549672 :   delete ppd.visited;
    4251                 :             : 
    4252                 :             :   /* Make sure we found some parameter packs.  */
    4253                 :    53274836 :   if (parameter_packs == NULL_TREE)
    4254                 :             :     {
    4255                 :          60 :       if (complain & tf_error)
    4256                 :             :         {
    4257                 :          60 :           if (TYPE_P (arg))
    4258                 :          23 :             error ("expansion pattern %qT contains no parameter packs", arg);
    4259                 :             :           else
    4260                 :          37 :             error ("expansion pattern %qE contains no parameter packs", arg);
    4261                 :             :         }
    4262                 :          60 :       return error_mark_node;
    4263                 :             :     }
    4264                 :   100318492 :   PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
    4265                 :             : 
    4266                 :    53274776 :   PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
    4267                 :    53274776 :   if (ppd.found_extra_args_tree_p)
    4268                 :             :     /* If the pattern of this pack expansion contains a subtree that has
    4269                 :             :        the extra args mechanism for avoiding partial instantiation, then
    4270                 :             :        force this pack expansion to also use extra args.  Otherwise
    4271                 :             :        partial instantiation of this pack expansion may not lower the
    4272                 :             :        level of some parameter packs within the pattern, which would
    4273                 :             :        confuse tsubst_pack_expansion later (PR101764).  */
    4274                 :          54 :     PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true;
    4275                 :             : 
    4276                 :             :   return result;
    4277                 :             : }
    4278                 :             : 
    4279                 :             : /* Checks T for any "bare" parameter packs, which have not yet been
    4280                 :             :    expanded, and issues an error if any are found. This operation can
    4281                 :             :    only be done on full expressions or types (e.g., an expression
    4282                 :             :    statement, "if" condition, etc.), because we could have expressions like:
    4283                 :             : 
    4284                 :             :      foo(f(g(h(args)))...)
    4285                 :             : 
    4286                 :             :    where "args" is a parameter pack. check_for_bare_parameter_packs
    4287                 :             :    should not be called for the subexpressions args, h(args),
    4288                 :             :    g(h(args)), or f(g(h(args))), because we would produce erroneous
    4289                 :             :    error messages.
    4290                 :             : 
    4291                 :             :    Returns TRUE and emits an error if there were bare parameter packs,
    4292                 :             :    returns FALSE otherwise.  */
    4293                 :             : bool
    4294                 :  1625283022 : check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
    4295                 :             : {
    4296                 :  1625283022 :   tree parameter_packs = NULL_TREE;
    4297                 :  1625283022 :   struct find_parameter_pack_data ppd;
    4298                 :             : 
    4299                 :  1625283022 :   if (!processing_template_decl || !t || t == error_mark_node)
    4300                 :             :     return false;
    4301                 :             : 
    4302                 :  1035545437 :   if (TREE_CODE (t) == TYPE_DECL)
    4303                 :           0 :     t = TREE_TYPE (t);
    4304                 :             : 
    4305                 :  1035545437 :   ppd.parameter_packs = &parameter_packs;
    4306                 :  1035545437 :   ppd.visited = new hash_set<tree>;
    4307                 :  1035545437 :   ppd.type_pack_expansion_p = false;
    4308                 :  1035545437 :   cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
    4309                 :  2071090874 :   delete ppd.visited;
    4310                 :             : 
    4311                 :  1035545437 :   if (!parameter_packs)
    4312                 :             :     return false;
    4313                 :             : 
    4314                 :         313 :   if (loc == UNKNOWN_LOCATION)
    4315                 :         307 :     loc = cp_expr_loc_or_input_loc (t);
    4316                 :             : 
    4317                 :             :   /* It's OK for a lambda to have an unexpanded parameter pack from the
    4318                 :             :      containing context, but do complain about unexpanded capture packs.  */
    4319                 :         313 :   tree lam = current_lambda_expr ();
    4320                 :         313 :   if (lam)
    4321                 :          61 :     lam = TREE_TYPE (lam);
    4322                 :             : 
    4323                 :          61 :   if (lam && lam != current_class_type)
    4324                 :             :     {
    4325                 :             :       /* We're in a lambda, but it isn't the innermost class.
    4326                 :             :          This should work, but currently doesn't.  */
    4327                 :           9 :       sorry_at (loc, "unexpanded parameter pack in local class in lambda");
    4328                 :           9 :       return true;
    4329                 :             :     }
    4330                 :             : 
    4331                 :         304 :   if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
    4332                 :          91 :     for (; parameter_packs;
    4333                 :          42 :          parameter_packs = TREE_CHAIN (parameter_packs))
    4334                 :             :       {
    4335                 :          49 :         tree pack = TREE_VALUE (parameter_packs);
    4336                 :          49 :         if (is_capture_proxy (pack)
    4337                 :          49 :             || (TREE_CODE (pack) == PARM_DECL
    4338                 :          16 :                 && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
    4339                 :             :           break;
    4340                 :             :       }
    4341                 :             : 
    4342                 :         304 :   if (parameter_packs)
    4343                 :             :     {
    4344                 :         262 :       error_at (loc, "parameter packs not expanded with %<...%>:");
    4345                 :         786 :       while (parameter_packs)
    4346                 :             :         {
    4347                 :         262 :           tree pack = TREE_VALUE (parameter_packs);
    4348                 :         262 :           tree name = NULL_TREE;
    4349                 :             : 
    4350                 :         262 :           if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
    4351                 :         262 :               || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
    4352                 :         198 :             name = TYPE_NAME (pack);
    4353                 :          64 :           else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
    4354                 :          33 :             name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
    4355                 :          31 :           else if (TREE_CODE (pack) == CALL_EXPR)
    4356                 :           3 :             name = DECL_NAME (CALL_EXPR_FN (pack));
    4357                 :             :           else
    4358                 :          28 :             name = DECL_NAME (pack);
    4359                 :             : 
    4360                 :         262 :           if (name)
    4361                 :         262 :             inform (loc, "        %qD", name);
    4362                 :             :           else
    4363                 :           0 :             inform (loc, "        %s", "<anonymous>");
    4364                 :             : 
    4365                 :         262 :           parameter_packs = TREE_CHAIN (parameter_packs);
    4366                 :             :         }
    4367                 :             : 
    4368                 :             :       return true;
    4369                 :             :     }
    4370                 :             : 
    4371                 :             :   return false;
    4372                 :             : }
    4373                 :             : 
    4374                 :             : /* Expand any parameter packs that occur in the template arguments in
    4375                 :             :    ARGS.  */
    4376                 :             : tree
    4377                 :   572635336 : expand_template_argument_pack (tree args)
    4378                 :             : {
    4379                 :   572635336 :   if (args == error_mark_node)
    4380                 :             :     return error_mark_node;
    4381                 :             : 
    4382                 :   572635317 :   tree result_args = NULL_TREE;
    4383                 :  1145270634 :   int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
    4384                 :   572635317 :   int num_result_args = -1;
    4385                 :   572635317 :   int non_default_args_count = -1;
    4386                 :             : 
    4387                 :             :   /* First, determine if we need to expand anything, and the number of
    4388                 :             :      slots we'll need.  */
    4389                 :  1501137555 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4390                 :             :     {
    4391                 :   928503320 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4392                 :   928503320 :       if (arg == NULL_TREE)
    4393                 :             :         return args;
    4394                 :   928502238 :       if (ARGUMENT_PACK_P (arg))
    4395                 :             :         {
    4396                 :    55623123 :           int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
    4397                 :    55623123 :           if (num_result_args < 0)
    4398                 :    55623123 :             num_result_args = in_arg + num_packed;
    4399                 :             :           else
    4400                 :           0 :             num_result_args += num_packed;
    4401                 :             :         }
    4402                 :             :       else
    4403                 :             :         {
    4404                 :   872879115 :           if (num_result_args >= 0)
    4405                 :           0 :             num_result_args++;
    4406                 :             :         }
    4407                 :             :     }
    4408                 :             : 
    4409                 :             :   /* If no expansion is necessary, we're done.  */
    4410                 :   572634235 :   if (num_result_args < 0)
    4411                 :             :     return args;
    4412                 :             : 
    4413                 :             :   /* Expand arguments.  */
    4414                 :    55623123 :   result_args = make_tree_vec (num_result_args);
    4415                 :    55623123 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
    4416                 :    54657623 :     non_default_args_count =
    4417                 :    54657623 :       GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
    4418                 :   132525170 :   for (in_arg = 0; in_arg < nargs; ++in_arg)
    4419                 :             :     {
    4420                 :    76902047 :       tree arg = TREE_VEC_ELT (args, in_arg);
    4421                 :    76902047 :       if (ARGUMENT_PACK_P (arg))
    4422                 :             :         {
    4423                 :    55623123 :           tree packed = ARGUMENT_PACK_ARGS (arg);
    4424                 :    55623123 :           int i, num_packed = TREE_VEC_LENGTH (packed);
    4425                 :   154840205 :           for (i = 0; i < num_packed; ++i, ++out_arg)
    4426                 :    99217082 :             TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
    4427                 :    55623123 :           if (non_default_args_count > 0)
    4428                 :    54657619 :             non_default_args_count += num_packed - 1;
    4429                 :             :         }
    4430                 :             :       else
    4431                 :             :         {
    4432                 :    21278924 :           TREE_VEC_ELT (result_args, out_arg) = arg;
    4433                 :    21278924 :           ++out_arg;
    4434                 :             :         }
    4435                 :             :     }
    4436                 :    55623123 :   if (non_default_args_count >= 0)
    4437                 :    54657623 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
    4438                 :             :   return result_args;
    4439                 :             : }
    4440                 :             : 
    4441                 :             : /* Checks if DECL shadows a template parameter.
    4442                 :             : 
    4443                 :             :    [temp.local]: A template-parameter shall not be redeclared within its
    4444                 :             :    scope (including nested scopes).
    4445                 :             : 
    4446                 :             :    Emits an error and returns TRUE if the DECL shadows a parameter,
    4447                 :             :    returns FALSE otherwise.  */
    4448                 :             : 
    4449                 :             : bool
    4450                 :  1530855414 : check_template_shadow (tree decl)
    4451                 :             : {
    4452                 :  1530855414 :   tree olddecl;
    4453                 :             : 
    4454                 :             :   /* If we're not in a template, we can't possibly shadow a template
    4455                 :             :      parameter.  */
    4456                 :  1530855414 :   if (!current_template_parms)
    4457                 :             :     return true;
    4458                 :             : 
    4459                 :             :   /* Figure out what we're shadowing.  */
    4460                 :   757004506 :   decl = OVL_FIRST (decl);
    4461                 :   757004506 :   olddecl = innermost_non_namespace_value (DECL_NAME (decl));
    4462                 :             : 
    4463                 :             :   /* If there's no previous binding for this name, we're not shadowing
    4464                 :             :      anything, let alone a template parameter.  */
    4465                 :   757004506 :   if (!olddecl)
    4466                 :             :     return true;
    4467                 :             : 
    4468                 :             :   /* If we're not shadowing a template parameter, we're done.  Note
    4469                 :             :      that OLDDECL might be an OVERLOAD (or perhaps even an
    4470                 :             :      ERROR_MARK), so we can't just blithely assume it to be a _DECL
    4471                 :             :      node.  */
    4472                 :    57172268 :   if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
    4473                 :             :     return true;
    4474                 :             : 
    4475                 :             :   /* We check for decl != olddecl to avoid bogus errors for using a
    4476                 :             :      name inside a class.  We check TPFI to avoid duplicate errors for
    4477                 :             :      inline member templates.  */
    4478                 :         199 :   if (decl == olddecl
    4479                 :         199 :       || (DECL_TEMPLATE_PARM_P (decl)
    4480                 :          92 :           && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
    4481                 :             :     return true;
    4482                 :             : 
    4483                 :             :   /* Don't complain about the injected class name, as we've already
    4484                 :             :      complained about the class itself.  */
    4485                 :         155 :   if (DECL_SELF_REFERENCE_P (decl))
    4486                 :             :     return false;
    4487                 :             : 
    4488                 :         147 :   if (DECL_TEMPLATE_PARM_P (decl))
    4489                 :          48 :     error ("declaration of template parameter %q+D shadows "
    4490                 :             :            "template parameter", decl);
    4491                 :             :   else
    4492                 :          99 :     error ("declaration of %q+#D shadows template parameter", decl);
    4493                 :         147 :   inform (DECL_SOURCE_LOCATION (olddecl),
    4494                 :             :           "template parameter %qD declared here", olddecl);
    4495                 :         147 :   return false;
    4496                 :             : }
    4497                 :             : 
    4498                 :             : /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
    4499                 :             :    ORIG_LEVEL, DECL, and TYPE.  */
    4500                 :             : 
    4501                 :             : static tree
    4502                 :   175203672 : build_template_parm_index (int index,
    4503                 :             :                            int level,
    4504                 :             :                            int orig_level,
    4505                 :             :                            tree decl,
    4506                 :             :                            tree type)
    4507                 :             : {
    4508                 :   175203672 :   tree t = make_node (TEMPLATE_PARM_INDEX);
    4509                 :   175203672 :   TEMPLATE_PARM_IDX (t) = index;
    4510                 :   175203672 :   TEMPLATE_PARM_LEVEL (t) = level;
    4511                 :   175203672 :   TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
    4512                 :   175203672 :   TEMPLATE_PARM_DECL (t) = decl;
    4513                 :   175203672 :   TREE_TYPE (t) = type;
    4514                 :   175203672 :   TREE_CONSTANT (t) = TREE_CONSTANT (decl);
    4515                 :   175203672 :   TREE_READONLY (t) = TREE_READONLY (decl);
    4516                 :             : 
    4517                 :   175203672 :   return t;
    4518                 :             : }
    4519                 :             : 
    4520                 :             : struct ctp_hasher : ggc_ptr_hash<tree_node>
    4521                 :             : {
    4522                 :   919859143 :   static hashval_t hash (tree t)
    4523                 :             :   {
    4524                 :   919859143 :     ++comparing_specializations;
    4525                 :   919859143 :     tree_code code = TREE_CODE (t);
    4526                 :   919859143 :     hashval_t val = iterative_hash_object (code, 0);
    4527                 :   919859143 :     val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val);
    4528                 :   919859143 :     val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val);
    4529                 :   919859143 :     if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
    4530                 :   679931671 :       val = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val);
    4531                 :   919859143 :     if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
    4532                 :   123840421 :       val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val);
    4533                 :   919859143 :     --comparing_specializations;
    4534                 :   919859143 :     return val;
    4535                 :             :   }
    4536                 :             : 
    4537                 :  1198323235 :   static bool equal (tree t, tree u)
    4538                 :             :   {
    4539                 :  1198323235 :     ++comparing_specializations;
    4540                 :  1198323235 :     bool eq = comptypes (t, u, COMPARE_STRUCTURAL);
    4541                 :  1198323235 :     --comparing_specializations;
    4542                 :  1198323235 :     return eq;
    4543                 :             :   }
    4544                 :             : };
    4545                 :             : 
    4546                 :             : static GTY (()) hash_table<ctp_hasher> *ctp_table;
    4547                 :             : 
    4548                 :             : /* Find the canonical type parameter for the given template type
    4549                 :             :    parameter.  Returns the canonical type parameter, which may be TYPE
    4550                 :             :    if no such parameter existed.  */
    4551                 :             : 
    4552                 :             : tree
    4553                 :   162393431 : canonical_type_parameter (tree type)
    4554                 :             : {
    4555                 :   162393431 :   if (ctp_table == NULL)
    4556                 :      101073 :     ctp_table = hash_table<ctp_hasher>::create_ggc (61);
    4557                 :             : 
    4558                 :   162393431 :   tree& slot = *ctp_table->find_slot (type, INSERT);
    4559                 :   162393431 :   if (slot == NULL_TREE)
    4560                 :     1533071 :     slot = type;
    4561                 :   162393431 :   return slot;
    4562                 :             : }
    4563                 :             : 
    4564                 :             : /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
    4565                 :             :    TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
    4566                 :             :    TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
    4567                 :             :    new one is created.  */
    4568                 :             : 
    4569                 :             : static tree
    4570                 :    20211662 : reduce_template_parm_level (tree index, tree type, int levels, tree args,
    4571                 :             :                             tsubst_flags_t complain)
    4572                 :             : {
    4573                 :    20211662 :   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
    4574                 :     7639640 :       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
    4575                 :     7639640 :           != TEMPLATE_PARM_LEVEL (index) - levels)
    4576                 :    27828582 :       || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
    4577                 :             :     {
    4578                 :    13710123 :       tree orig_decl = TEMPLATE_PARM_DECL (index);
    4579                 :             : 
    4580                 :    13710123 :       tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
    4581                 :    13710123 :                               TREE_CODE (orig_decl), DECL_NAME (orig_decl),
    4582                 :             :                               type);
    4583                 :    13710123 :       TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
    4584                 :    13710123 :       TREE_READONLY (decl) = TREE_READONLY (orig_decl);
    4585                 :    13710123 :       DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
    4586                 :    13710123 :       DECL_ARTIFICIAL (decl) = 1;
    4587                 :    13710123 :       SET_DECL_TEMPLATE_PARM_P (decl);
    4588                 :             : 
    4589                 :    13710123 :       tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
    4590                 :    13710123 :                                             TEMPLATE_PARM_LEVEL (index) - levels,
    4591                 :    13710123 :                                             TEMPLATE_PARM_ORIG_LEVEL (index),
    4592                 :             :                                             decl, type);
    4593                 :    13710123 :       TEMPLATE_PARM_DESCENDANTS (index) = tpi;
    4594                 :    27420246 :       TEMPLATE_PARM_PARAMETER_PACK (tpi)
    4595                 :    13710123 :         = TEMPLATE_PARM_PARAMETER_PACK (index);
    4596                 :             : 
    4597                 :             :       /* Template template parameters need this.  */
    4598                 :    13710123 :       tree inner = decl;
    4599                 :    13710123 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
    4600                 :             :         {
    4601                 :        1693 :           inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl),
    4602                 :        1693 :                                        TYPE_DECL, DECL_NAME (decl), type);
    4603                 :        1693 :           DECL_TEMPLATE_RESULT (decl) = inner;
    4604                 :        1693 :           DECL_ARTIFICIAL (inner) = true;
    4605                 :        1693 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl),
    4606                 :             :                                               args, complain);
    4607                 :        1693 :           DECL_TEMPLATE_PARMS (decl) = parms;
    4608                 :        1693 :           tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl);
    4609                 :        1693 :           DECL_TEMPLATE_INFO (inner)
    4610                 :        3386 :             = build_template_info (DECL_TI_TEMPLATE (orig_inner),
    4611                 :             :                                    template_parms_to_args (parms));
    4612                 :             :         }
    4613                 :             : 
    4614                 :             :       /* Attach the TPI to the decl.  */
    4615                 :    13710123 :       if (TREE_CODE (inner) == TYPE_DECL)
    4616                 :    11547006 :         TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
    4617                 :             :       else
    4618                 :     2163117 :         DECL_INITIAL (decl) = tpi;
    4619                 :             :     }
    4620                 :             : 
    4621                 :    20211662 :   return TEMPLATE_PARM_DESCENDANTS (index);
    4622                 :             : }
    4623                 :             : 
    4624                 :             : /* Process information from new template parameter PARM and append it
    4625                 :             :    to the LIST being built.  This new parameter is a non-type
    4626                 :             :    parameter iff IS_NON_TYPE is true. This new parameter is a
    4627                 :             :    parameter pack iff IS_PARAMETER_PACK is true.  The location of PARM
    4628                 :             :    is in PARM_LOC.  */
    4629                 :             : 
    4630                 :             : tree
    4631                 :   143331159 : process_template_parm (tree list, location_t parm_loc, tree parm,
    4632                 :             :                        bool is_non_type, bool is_parameter_pack)
    4633                 :             : {
    4634                 :   143331159 :   gcc_assert (TREE_CODE (parm) == TREE_LIST);
    4635                 :   143331159 :   tree prev = NULL_TREE;
    4636                 :   143331159 :   int idx = 0;
    4637                 :             : 
    4638                 :   143331159 :   if (list)
    4639                 :             :     {
    4640                 :    68124234 :       prev = tree_last (list);
    4641                 :             : 
    4642                 :    68124234 :       tree p = TREE_VALUE (prev);
    4643                 :    68124234 :       if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
    4644                 :    62573348 :         idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
    4645                 :     5550886 :       else if (TREE_CODE (p) == PARM_DECL)
    4646                 :     5550861 :         idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
    4647                 :             : 
    4648                 :    68124234 :       ++idx;
    4649                 :             :     }
    4650                 :             : 
    4651                 :   143331159 :   tree decl = NULL_TREE;
    4652                 :   143331159 :   tree defval = TREE_PURPOSE (parm);
    4653                 :   143331159 :   tree constr = TREE_TYPE (parm);
    4654                 :             : 
    4655                 :   143331159 :   if (is_non_type)
    4656                 :             :     {
    4657                 :    11408861 :       parm = TREE_VALUE (parm);
    4658                 :             : 
    4659                 :    11408861 :       SET_DECL_TEMPLATE_PARM_P (parm);
    4660                 :             : 
    4661                 :    11408861 :       if (TREE_TYPE (parm) != error_mark_node)
    4662                 :             :         {
    4663                 :             :           /* [temp.param]
    4664                 :             : 
    4665                 :             :              The top-level cv-qualifiers on the template-parameter are
    4666                 :             :              ignored when determining its type.  */
    4667                 :    11408787 :           TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
    4668                 :    11408787 :           if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
    4669                 :         127 :             TREE_TYPE (parm) = error_mark_node;
    4670                 :    11408660 :           else if (uses_parameter_packs (TREE_TYPE (parm))
    4671                 :          51 :                    && !is_parameter_pack
    4672                 :             :                    /* If we're in a nested template parameter list, the template
    4673                 :             :                       template parameter could be a parameter pack.  */
    4674                 :    11408678 :                    && processing_template_parmlist == 1)
    4675                 :             :             {
    4676                 :             :               /* This template parameter is not a parameter pack, but it
    4677                 :             :                  should be. Complain about "bare" parameter packs.  */
    4678                 :          12 :               check_for_bare_parameter_packs (TREE_TYPE (parm));
    4679                 :             : 
    4680                 :             :               /* Recover by calling this a parameter pack.  */
    4681                 :          12 :               is_parameter_pack = true;
    4682                 :             :             }
    4683                 :             :         }
    4684                 :             : 
    4685                 :             :       /* A template parameter is not modifiable.  */
    4686                 :    11408861 :       TREE_CONSTANT (parm) = 1;
    4687                 :    11408861 :       TREE_READONLY (parm) = 1;
    4688                 :    11408861 :       decl = build_decl (parm_loc,
    4689                 :    11408861 :                          CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
    4690                 :    11408861 :       TREE_CONSTANT (decl) = 1;
    4691                 :    11408861 :       TREE_READONLY (decl) = 1;
    4692                 :    11408861 :       DECL_INITIAL (parm) = DECL_INITIAL (decl)
    4693                 :    22817722 :         = build_template_parm_index (idx, current_template_depth,
    4694                 :    11408861 :                                      current_template_depth,
    4695                 :    11408861 :                                      decl, TREE_TYPE (parm));
    4696                 :             : 
    4697                 :    11408861 :       TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
    4698                 :    11408861 :         = is_parameter_pack;
    4699                 :             :     }
    4700                 :             :   else
    4701                 :             :     {
    4702                 :   131922298 :       tree t;
    4703                 :   131922298 :       parm = TREE_VALUE (TREE_VALUE (parm));
    4704                 :             : 
    4705                 :   131922298 :       if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
    4706                 :             :         {
    4707                 :      296587 :           t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
    4708                 :             :           /* This is for distinguishing between real templates and template
    4709                 :             :              template parameters */
    4710                 :      296587 :           TREE_TYPE (parm) = t;
    4711                 :             : 
    4712                 :             :           /* any_template_parm_r expects to be able to get the targs of a
    4713                 :             :              DECL_TEMPLATE_RESULT.  */
    4714                 :      296587 :           tree result = DECL_TEMPLATE_RESULT (parm);
    4715                 :      296587 :           TREE_TYPE (result) = t;
    4716                 :      296587 :           tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
    4717                 :      296587 :           tree tinfo = build_template_info (parm, args);
    4718                 :      296587 :           retrofit_lang_decl (result);
    4719                 :      296587 :           DECL_TEMPLATE_INFO (result) = tinfo;
    4720                 :             : 
    4721                 :      296587 :           decl = parm;
    4722                 :      296587 :         }
    4723                 :             :       else
    4724                 :             :         {
    4725                 :   131625711 :           t = cxx_make_type (TEMPLATE_TYPE_PARM);
    4726                 :             :           /* parm is either IDENTIFIER_NODE or NULL_TREE.  */
    4727                 :   131625711 :           decl = build_decl (parm_loc,
    4728                 :             :                              TYPE_DECL, parm, t);
    4729                 :             :         }
    4730                 :             : 
    4731                 :   131922298 :       TYPE_NAME (t) = decl;
    4732                 :   131922298 :       TYPE_STUB_DECL (t) = decl;
    4733                 :   131922298 :       parm = decl;
    4734                 :   131922298 :       TEMPLATE_TYPE_PARM_INDEX (t)
    4735                 :   131922298 :         = build_template_parm_index (idx, current_template_depth,
    4736                 :   131922298 :                                      current_template_depth,
    4737                 :   131922298 :                                      decl, TREE_TYPE (parm));
    4738                 :   131922298 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
    4739                 :   131922298 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
    4740                 :             :     }
    4741                 :   143331159 :   DECL_ARTIFICIAL (decl) = 1;
    4742                 :   143331159 :   SET_DECL_TEMPLATE_PARM_P (decl);
    4743                 :             : 
    4744                 :   143331159 :   if (TREE_CODE (parm) == TEMPLATE_DECL
    4745                 :   143331159 :       && !uses_outer_template_parms (parm))
    4746                 :      296550 :     TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true;
    4747                 :             : 
    4748                 :             :   /* Build requirements for the type/template parameter.
    4749                 :             :      This must be done after SET_DECL_TEMPLATE_PARM_P or
    4750                 :             :      process_template_parm could fail. */
    4751                 :   143331159 :   tree reqs = finish_shorthand_constraint (parm, constr);
    4752                 :             : 
    4753                 :   143331159 :   decl = pushdecl (decl);
    4754                 :   143331159 :   if (!is_non_type)
    4755                 :   131922298 :     parm = decl;
    4756                 :             : 
    4757                 :             :   /* Build the parameter node linking the parameter declaration,
    4758                 :             :      its default argument (if any), and its constraints (if any). */
    4759                 :   143331159 :   parm = build_tree_list (defval, parm);
    4760                 :   143331159 :   TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
    4761                 :             : 
    4762                 :   143331159 :   if (prev)
    4763                 :    68124234 :     TREE_CHAIN (prev) = parm;
    4764                 :             :   else
    4765                 :             :     list = parm;
    4766                 :             : 
    4767                 :   143331159 :   return list;
    4768                 :             : }
    4769                 :             : 
    4770                 :             : /* The end of a template parameter list has been reached.  Process the
    4771                 :             :    tree list into a parameter vector, converting each parameter into a more
    4772                 :             :    useful form.  Type parameters are saved as IDENTIFIER_NODEs, and others
    4773                 :             :    as PARM_DECLs.  */
    4774                 :             : 
    4775                 :             : tree
    4776                 :    75019332 : end_template_parm_list (tree parms)
    4777                 :             : {
    4778                 :    75019332 :   tree saved_parmlist = make_tree_vec (list_length (parms));
    4779                 :             : 
    4780                 :             :   /* Pop the dummy parameter level and add the real one.  We do not
    4781                 :             :      morph the dummy parameter in place, as it might have been
    4782                 :             :      captured by a (nested) template-template-parm.  */
    4783                 :    75019332 :   current_template_parms = TREE_CHAIN (current_template_parms);
    4784                 :             : 
    4785                 :   150038664 :   current_template_parms
    4786                 :    75019332 :     = tree_cons (size_int (current_template_depth + 1),
    4787                 :             :                  saved_parmlist, current_template_parms);
    4788                 :             : 
    4789                 :   218044397 :   for (unsigned ix = 0; parms; ix++)
    4790                 :             :     {
    4791                 :   143025065 :       tree parm = parms;
    4792                 :   143025065 :       parms = TREE_CHAIN (parms);
    4793                 :   143025065 :       TREE_CHAIN (parm) = NULL_TREE;
    4794                 :             : 
    4795                 :   143025065 :       TREE_VEC_ELT (saved_parmlist, ix) = parm;
    4796                 :             :     }
    4797                 :             : 
    4798                 :    75019332 :   --processing_template_parmlist;
    4799                 :             : 
    4800                 :    75019332 :   return saved_parmlist;
    4801                 :             : }
    4802                 :             : 
    4803                 :             : // Explicitly indicate the end of the template parameter list. We assume
    4804                 :             : // that the current template parameters have been constructed and/or
    4805                 :             : // managed explicitly, as when creating new template template parameters
    4806                 :             : // from a shorthand constraint.
    4807                 :             : void
    4808                 :           7 : end_template_parm_list ()
    4809                 :             : {
    4810                 :           7 :   --processing_template_parmlist;
    4811                 :           7 : }
    4812                 :             : 
    4813                 :             : /* end_template_decl is called after a template declaration is seen.  */
    4814                 :             : 
    4815                 :             : void
    4816                 :    75207054 : end_template_decl (void)
    4817                 :             : {
    4818                 :    75207054 :   reset_specialization ();
    4819                 :             : 
    4820                 :    75207054 :   if (! processing_template_decl)
    4821                 :             :     return;
    4822                 :             : 
    4823                 :             :   /* This matches the pushlevel in begin_template_parm_list.  */
    4824                 :    75207054 :   finish_scope ();
    4825                 :             : 
    4826                 :    75207054 :   --processing_template_decl;
    4827                 :    75207054 :   current_template_parms = TREE_CHAIN (current_template_parms);
    4828                 :             : }
    4829                 :             : 
    4830                 :             : /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
    4831                 :             :    thereof, and converts it into an argument suitable to be passed to
    4832                 :             :    the type substitution functions.  Note that if the TREE_LIST contains
    4833                 :             :    an error_mark node, the returned argument is error_mark_node.  */
    4834                 :             : 
    4835                 :             : tree
    4836                 :   672838271 : template_parm_to_arg (tree t)
    4837                 :             : {
    4838                 :   672838271 :   if (!t)
    4839                 :             :     return NULL_TREE;
    4840                 :             : 
    4841                 :   672838226 :   if (TREE_CODE (t) == TREE_LIST)
    4842                 :   671145603 :     t = TREE_VALUE (t);
    4843                 :             : 
    4844                 :   672838226 :   if (error_operand_p (t))
    4845                 :         610 :     return error_mark_node;
    4846                 :             : 
    4847                 :   672837616 :   if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
    4848                 :             :     {
    4849                 :   671637596 :       if (TREE_CODE (t) == TYPE_DECL
    4850                 :    54324802 :           || TREE_CODE (t) == TEMPLATE_DECL)
    4851                 :   618025317 :         t = TREE_TYPE (t);
    4852                 :             :       else
    4853                 :    53612279 :         t = DECL_INITIAL (t);
    4854                 :             :     }
    4855                 :             : 
    4856                 :   672837616 :   gcc_assert (TEMPLATE_PARM_P (t));
    4857                 :             : 
    4858                 :   672837616 :   if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
    4859                 :   672837616 :       || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
    4860                 :             :     {
    4861                 :   619185430 :       if (TEMPLATE_TYPE_PARAMETER_PACK (t))
    4862                 :             :         {
    4863                 :             :           /* Turn this argument into a TYPE_ARGUMENT_PACK
    4864                 :             :              with a single element, which expands T.  */
    4865                 :    23041669 :           tree vec = make_tree_vec (1);
    4866                 :    23041669 :           if (CHECKING_P)
    4867                 :    23041669 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4868                 :             : 
    4869                 :    23041669 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4870                 :             : 
    4871                 :    23041669 :           t = cxx_make_type (TYPE_ARGUMENT_PACK);
    4872                 :    23041669 :           ARGUMENT_PACK_ARGS (t) = vec;
    4873                 :             :         }
    4874                 :             :     }
    4875                 :             :   else
    4876                 :             :     {
    4877                 :    53652186 :       if (TEMPLATE_PARM_PARAMETER_PACK (t))
    4878                 :             :         {
    4879                 :             :           /* Turn this argument into a NONTYPE_ARGUMENT_PACK
    4880                 :             :              with a single element, which expands T.  */
    4881                 :     1332362 :           tree vec = make_tree_vec (1);
    4882                 :     1332362 :           if (CHECKING_P)
    4883                 :     1332362 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
    4884                 :             : 
    4885                 :     1332362 :           t = convert_from_reference (t);
    4886                 :     1332362 :           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
    4887                 :             : 
    4888                 :     1332362 :           t  = make_node (NONTYPE_ARGUMENT_PACK);
    4889                 :     1332362 :           ARGUMENT_PACK_ARGS (t) = vec;
    4890                 :             :         }
    4891                 :             :       else
    4892                 :    52319824 :         t = convert_from_reference (t);
    4893                 :             :     }
    4894                 :             :   return t;
    4895                 :             : }
    4896                 :             : 
    4897                 :             : /* If T looks like a generic template argument produced by template_parm_to_arg,
    4898                 :             :    return the corresponding template parameter, otherwise return NULL_TREE.  */
    4899                 :             : 
    4900                 :             : static tree
    4901                 :  1311669124 : template_arg_to_parm (tree t)
    4902                 :             : {
    4903                 :  1311669124 :   if (t == NULL_TREE)
    4904                 :             :     return NULL_TREE;
    4905                 :             : 
    4906                 :  1311669124 :   if (ARGUMENT_PACK_P (t))
    4907                 :             :     {
    4908                 :   141569803 :       tree args = ARGUMENT_PACK_ARGS (t);
    4909                 :   141569803 :       if (TREE_VEC_LENGTH (args) == 1
    4910                 :   141569803 :           && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0)))
    4911                 :    57625792 :         t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0));
    4912                 :             :     }
    4913                 :             : 
    4914                 :  1311669124 :   if (REFERENCE_REF_P (t))
    4915                 :         414 :     t = TREE_OPERAND (t, 0);
    4916                 :             : 
    4917                 :  1311669124 :   if (TEMPLATE_PARM_P (t))
    4918                 :             :     return t;
    4919                 :             :   else
    4920                 :   321909277 :     return NULL_TREE;
    4921                 :             : }
    4922                 :             : 
    4923                 :             : /* Given a single level of template parameters (a TREE_VEC), return it
    4924                 :             :    as a set of template arguments.  */
    4925                 :             : 
    4926                 :             : tree
    4927                 :   318443811 : template_parms_level_to_args (tree parms)
    4928                 :             : {
    4929                 :   318443811 :   parms = copy_node (parms);
    4930                 :   318443811 :   TREE_TYPE (parms) = NULL_TREE;
    4931                 :   988277363 :   for (tree& parm : tree_vec_range (parms))
    4932                 :   669833552 :     parm = template_parm_to_arg (parm);
    4933                 :             : 
    4934                 :   318443811 :   if (CHECKING_P)
    4935                 :   318443811 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms));
    4936                 :             : 
    4937                 :   318443811 :   return parms;
    4938                 :             : }
    4939                 :             : 
    4940                 :             : /* Given a set of template parameters, return them as a set of template
    4941                 :             :    arguments.  The template parameters are represented as a TREE_VEC, in
    4942                 :             :    the form documented in cp-tree.h for template arguments.  */
    4943                 :             : 
    4944                 :             : tree
    4945                 :   259475972 : template_parms_to_args (tree parms)
    4946                 :             : {
    4947                 :   259475972 :   tree header;
    4948                 :   259475972 :   tree args = NULL_TREE;
    4949                 :   259475972 :   int length = TMPL_PARMS_DEPTH (parms);
    4950                 :   259475972 :   int l = length;
    4951                 :             : 
    4952                 :             :   /* If there is only one level of template parameters, we do not
    4953                 :             :      create a TREE_VEC of TREE_VECs.  Instead, we return a single
    4954                 :             :      TREE_VEC containing the arguments.  */
    4955                 :   259475972 :   if (length > 1)
    4956                 :    41037324 :     args = make_tree_vec (length);
    4957                 :             : 
    4958                 :   560435841 :   for (header = parms; header; header = TREE_CHAIN (header))
    4959                 :             :     {
    4960                 :   300959869 :       tree a = template_parms_level_to_args (TREE_VALUE (header));
    4961                 :             : 
    4962                 :   300959869 :       if (length > 1)
    4963                 :    82521221 :         TREE_VEC_ELT (args, --l) = a;
    4964                 :             :       else
    4965                 :             :         args = a;
    4966                 :             :     }
    4967                 :             : 
    4968                 :   259475972 :   return args;
    4969                 :             : }
    4970                 :             : 
    4971                 :             : /* Within the declaration of a template, return the currently active
    4972                 :             :    template parameters as an argument TREE_VEC.  */
    4973                 :             : 
    4974                 :             : static tree
    4975                 :   258836996 : current_template_args (void)
    4976                 :             : {
    4977                 :           0 :   return template_parms_to_args (current_template_parms);
    4978                 :             : }
    4979                 :             : 
    4980                 :             : /* Return the fully generic arguments for of TMPL, i.e. what
    4981                 :             :    current_template_args would be while parsing it.  */
    4982                 :             : 
    4983                 :             : tree
    4984                 :    17019458 : generic_targs_for (tree tmpl)
    4985                 :             : {
    4986                 :    17019458 :   if (tmpl == NULL_TREE)
    4987                 :             :     return NULL_TREE;
    4988                 :    17019458 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
    4989                 :    34038916 :       || DECL_TEMPLATE_SPECIALIZATION (tmpl))
    4990                 :             :     /* DECL_TEMPLATE_RESULT doesn't have the arguments we want.  For a template
    4991                 :             :        template parameter, it has no TEMPLATE_INFO; for a partial
    4992                 :             :        specialization, it has the arguments for the primary template, and we
    4993                 :             :        want the arguments for the partial specialization.  */;
    4994                 :    16929841 :   else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
    4995                 :    16929841 :     if (tree ti = get_template_info (result))
    4996                 :    16929841 :       return TI_ARGS (ti);
    4997                 :       89617 :   return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
    4998                 :             : }
    4999                 :             : 
    5000                 :             : /* Return the template arguments corresponding to the template parameters of
    5001                 :             :    DECL's enclosing scope.  When DECL is a member of a partial specialization,
    5002                 :             :    this returns the arguments for the partial specialization as opposed to those
    5003                 :             :    for the primary template, which is the main difference between this function
    5004                 :             :    and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT.  */
    5005                 :             : 
    5006                 :             : tree
    5007                 :       27432 : outer_template_args (const_tree decl)
    5008                 :             : {
    5009                 :       27432 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    5010                 :        4692 :     decl = DECL_TEMPLATE_RESULT (decl);
    5011                 :       27432 :   tree ti = get_template_info (decl);
    5012                 :       27432 :   if (!ti)
    5013                 :             :     return NULL_TREE;
    5014                 :       27432 :   tree args = TI_ARGS (ti);
    5015                 :       27432 :   if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti)))
    5016                 :             :     return args;
    5017                 :       54864 :   if (TMPL_ARGS_DEPTH (args) == 1)
    5018                 :             :     return NULL_TREE;
    5019                 :       22777 :   return strip_innermost_template_args (args, 1);
    5020                 :             : }
    5021                 :             : 
    5022                 :             : /* Update the declared TYPE by doing any lookups which were thought to be
    5023                 :             :    dependent, but are not now that we know the SCOPE of the declarator.  */
    5024                 :             : 
    5025                 :             : tree
    5026                 :   149923104 : maybe_update_decl_type (tree orig_type, tree scope)
    5027                 :             : {
    5028                 :   149923104 :   tree type = orig_type;
    5029                 :             : 
    5030                 :   149923104 :   if (type == NULL_TREE)
    5031                 :             :     return type;
    5032                 :             : 
    5033                 :   142875258 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5034                 :    67540513 :     type = TREE_TYPE (type);
    5035                 :             : 
    5036                 :     7700603 :   if (scope && TYPE_P (scope) && dependent_type_p (scope)
    5037                 :     6617204 :       && dependent_type_p (type)
    5038                 :             :       /* Don't bother building up the args in this case.  */
    5039                 :   146154207 :       && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
    5040                 :             :     {
    5041                 :             :       /* tsubst in the args corresponding to the template parameters,
    5042                 :             :          including auto if present.  Most things will be unchanged, but
    5043                 :             :          make_typename_type and tsubst_qualified_id will resolve
    5044                 :             :          TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
    5045                 :     2384547 :       tree args = current_template_args ();
    5046                 :     2384547 :       tree auto_node = type_uses_auto (type);
    5047                 :     2384547 :       tree pushed;
    5048                 :     2384547 :       if (auto_node)
    5049                 :             :         {
    5050                 :           0 :           tree auto_vec = make_tree_vec (1);
    5051                 :           0 :           TREE_VEC_ELT (auto_vec, 0) = auto_node;
    5052                 :           0 :           args = add_to_template_args (args, auto_vec);
    5053                 :             :         }
    5054                 :     2384547 :       pushed = push_scope (scope);
    5055                 :     2384547 :       type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
    5056                 :     2384547 :       if (pushed)
    5057                 :     2384543 :         pop_scope (scope);
    5058                 :             :     }
    5059                 :             : 
    5060                 :   142875258 :   if (type == error_mark_node)
    5061                 :             :     return orig_type;
    5062                 :             : 
    5063                 :   142874630 :   if (TREE_CODE (orig_type) == TYPE_DECL)
    5064                 :             :     {
    5065                 :    67540472 :       if (same_type_p (type, TREE_TYPE (orig_type)))
    5066                 :             :         type = orig_type;
    5067                 :             :       else
    5068                 :       67355 :         type = TYPE_NAME (type);
    5069                 :             :     }
    5070                 :             :   return type;
    5071                 :             : }
    5072                 :             : 
    5073                 :             : /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
    5074                 :             :    template PARMS and constraints, CONSTR.  If MEMBER_TEMPLATE_P is true,
    5075                 :             :    the new  template is a member template. */
    5076                 :             : 
    5077                 :             : static tree
    5078                 :   148814681 : build_template_decl (tree decl, tree parms, bool member_template_p)
    5079                 :             : {
    5080                 :   148814681 :   gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL);
    5081                 :             : 
    5082                 :   148814681 :   tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
    5083                 :   148814681 :   SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
    5084                 :   148814681 :   DECL_TEMPLATE_PARMS (tmpl) = parms;
    5085                 :   148814681 :   DECL_TEMPLATE_RESULT (tmpl) = decl;
    5086                 :   148814681 :   DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
    5087                 :   148814681 :   TREE_TYPE (tmpl) = TREE_TYPE (decl);
    5088                 :   148814681 :   DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
    5089                 :   148814681 :   DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
    5090                 :             : 
    5091                 :             :   /* Propagate module information from the decl.  */
    5092                 :   148814681 :   DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
    5093                 :             : 
    5094                 :   148814681 :   return tmpl;
    5095                 :             : }
    5096                 :             : 
    5097                 :             : struct template_parm_data
    5098                 :             : {
    5099                 :             :   /* The level of the template parameters we are currently
    5100                 :             :      processing.  */
    5101                 :             :   int level;
    5102                 :             : 
    5103                 :             :   /* The index of the specialization argument we are currently
    5104                 :             :      processing.  */
    5105                 :             :   int current_arg;
    5106                 :             : 
    5107                 :             :   /* An array whose size is the number of template parameters.  The
    5108                 :             :      elements are nonzero if the parameter has been used in any one
    5109                 :             :      of the arguments processed so far.  */
    5110                 :             :   int* parms;
    5111                 :             : 
    5112                 :             :   /* An array whose size is the number of template arguments.  The
    5113                 :             :      elements are nonzero if the argument makes use of template
    5114                 :             :      parameters of this level.  */
    5115                 :             :   int* arg_uses_template_parms;
    5116                 :             : };
    5117                 :             : 
    5118                 :             : /* Subroutine of push_template_decl used to see if each template
    5119                 :             :    parameter in a partial specialization is used in the explicit
    5120                 :             :    argument list.  If T is of the LEVEL given in DATA (which is
    5121                 :             :    treated as a template_parm_data*), then DATA->PARMS is marked
    5122                 :             :    appropriately.  */
    5123                 :             : 
    5124                 :             : static int
    5125                 :    14232681 : mark_template_parm (tree t, void* data)
    5126                 :             : {
    5127                 :    14232681 :   int level;
    5128                 :    14232681 :   int idx;
    5129                 :    14232681 :   struct template_parm_data* tpd = (struct template_parm_data*) data;
    5130                 :             : 
    5131                 :    14232681 :   template_parm_level_and_index (t, &level, &idx);
    5132                 :             : 
    5133                 :    14232681 :   if (level == tpd->level)
    5134                 :             :     {
    5135                 :    14221076 :       tpd->parms[idx] = 1;
    5136                 :    14221076 :       tpd->arg_uses_template_parms[tpd->current_arg] = 1;
    5137                 :             :     }
    5138                 :             : 
    5139                 :             :   /* In C++17 the type of a non-type argument is a deduced context.  */
    5140                 :    14232681 :   if (cxx_dialect >= cxx17
    5141                 :    14033491 :       && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5142                 :     1819756 :     for_each_template_parm (TREE_TYPE (t),
    5143                 :             :                             &mark_template_parm,
    5144                 :             :                             data,
    5145                 :             :                             NULL,
    5146                 :             :                             /*include_nondeduced_p=*/false);
    5147                 :             : 
    5148                 :             :   /* Return zero so that for_each_template_parm will continue the
    5149                 :             :      traversal of the tree; we want to mark *every* template parm.  */
    5150                 :    14232681 :   return 0;
    5151                 :             : }
    5152                 :             : 
    5153                 :             : /* Process the partial specialization DECL.  */
    5154                 :             : 
    5155                 :             : static tree
    5156                 :     6958884 : process_partial_specialization (tree decl)
    5157                 :             : {
    5158                 :     6958884 :   tree type = TREE_TYPE (decl);
    5159                 :     6958884 :   tree tinfo = get_template_info (decl);
    5160                 :     6958884 :   tree maintmpl = TI_TEMPLATE (tinfo);
    5161                 :     6958884 :   tree specargs = TI_ARGS (tinfo);
    5162                 :     6958884 :   tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
    5163                 :     6958884 :   tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
    5164                 :     6958884 :   tree inner_parms;
    5165                 :     6958884 :   tree inst;
    5166                 :     6958884 :   int nargs = TREE_VEC_LENGTH (inner_args);
    5167                 :     6958884 :   int ntparms;
    5168                 :     6958884 :   int  i;
    5169                 :     6958884 :   bool did_error_intro = false;
    5170                 :     6958884 :   struct template_parm_data tpd;
    5171                 :     6958884 :   struct template_parm_data tpd2;
    5172                 :             : 
    5173                 :     6958884 :   gcc_assert (current_template_parms);
    5174                 :             : 
    5175                 :             :   /* A concept cannot be specialized.  */
    5176                 :     6958884 :   if (flag_concepts && variable_concept_p (maintmpl))
    5177                 :             :     {
    5178                 :           1 :       error ("specialization of variable concept %q#D", maintmpl);
    5179                 :           1 :       return error_mark_node;
    5180                 :             :     }
    5181                 :             : 
    5182                 :     6958883 :   inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
    5183                 :     6958883 :   ntparms = TREE_VEC_LENGTH (inner_parms);
    5184                 :             : 
    5185                 :             :   /* We check that each of the template parameters given in the
    5186                 :             :      partial specialization is used in the argument list to the
    5187                 :             :      specialization.  For example:
    5188                 :             : 
    5189                 :             :        template <class T> struct S;
    5190                 :             :        template <class T> struct S<T*>;
    5191                 :             : 
    5192                 :             :      The second declaration is OK because `T*' uses the template
    5193                 :             :      parameter T, whereas
    5194                 :             : 
    5195                 :             :        template <class T> struct S<int>;
    5196                 :             : 
    5197                 :             :      is no good.  Even trickier is:
    5198                 :             : 
    5199                 :             :        template <class T>
    5200                 :             :        struct S1
    5201                 :             :        {
    5202                 :             :           template <class U>
    5203                 :             :           struct S2;
    5204                 :             :           template <class U>
    5205                 :             :           struct S2<T>;
    5206                 :             :        };
    5207                 :             : 
    5208                 :             :      The S2<T> declaration is actually invalid; it is a
    5209                 :             :      full-specialization.  Of course,
    5210                 :             : 
    5211                 :             :           template <class U>
    5212                 :             :           struct S2<T (*)(U)>;
    5213                 :             : 
    5214                 :             :      or some such would have been OK.  */
    5215                 :     6958883 :   tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
    5216                 :     6958883 :   tpd.parms = XALLOCAVEC (int, ntparms);
    5217                 :     6958883 :   memset (tpd.parms, 0, sizeof (int) * ntparms);
    5218                 :             : 
    5219                 :     6958883 :   tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5220                 :     6958883 :   memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
    5221                 :    21463577 :   for (i = 0; i < nargs; ++i)
    5222                 :             :     {
    5223                 :    14504694 :       tpd.current_arg = i;
    5224                 :    14504694 :       for_each_template_parm (TREE_VEC_ELT (inner_args, i),
    5225                 :             :                               &mark_template_parm,
    5226                 :             :                               &tpd,
    5227                 :             :                               NULL,
    5228                 :             :                               /*include_nondeduced_p=*/false);
    5229                 :             :     }
    5230                 :    20231721 :   for (i = 0; i < ntparms; ++i)
    5231                 :    13272838 :     if (tpd.parms[i] == 0)
    5232                 :             :       {
    5233                 :             :         /* One of the template parms was not used in a deduced context in the
    5234                 :             :            specialization.  */
    5235                 :          57 :         if (!did_error_intro)
    5236                 :             :           {
    5237                 :          57 :             error ("template parameters not deducible in "
    5238                 :             :                    "partial specialization:");
    5239                 :          57 :             did_error_intro = true;
    5240                 :             :           }
    5241                 :             : 
    5242                 :         114 :         inform (input_location, "        %qD",
    5243                 :          57 :                 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
    5244                 :             :       }
    5245                 :             : 
    5246                 :     6958883 :   if (did_error_intro)
    5247                 :          57 :     return error_mark_node;
    5248                 :             : 
    5249                 :             :   /* [temp.class.spec]
    5250                 :             : 
    5251                 :             :      The argument list of the specialization shall not be identical to
    5252                 :             :      the implicit argument list of the primary template.  */
    5253                 :     6958826 :   tree main_args
    5254                 :     6958826 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
    5255                 :     6958826 :   if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
    5256                 :     6958826 :       && (!flag_concepts
    5257                 :      137123 :           || !strictly_subsumes (current_template_constraints (), maintmpl)))
    5258                 :             :     {
    5259                 :          16 :       if (!flag_concepts)
    5260                 :           9 :         error ("partial specialization %q+D does not specialize "
    5261                 :             :                "any template arguments; to define the primary template, "
    5262                 :             :                "remove the template argument list", decl);
    5263                 :             :       else
    5264                 :           7 :         error ("partial specialization %q+D does not specialize any "
    5265                 :             :                "template arguments and is not more constrained than "
    5266                 :             :                "the primary template; to define the primary template, "
    5267                 :             :                "remove the template argument list", decl);
    5268                 :          16 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5269                 :             :     }
    5270                 :             : 
    5271                 :             :   /* A partial specialization that replaces multiple parameters of the
    5272                 :             :      primary template with a pack expansion is less specialized for those
    5273                 :             :      parameters.  */
    5274                 :     6958826 :   if (nargs < DECL_NTPARMS (maintmpl))
    5275                 :             :     {
    5276                 :           3 :       error ("partial specialization is not more specialized than the "
    5277                 :             :              "primary template because it replaces multiple parameters "
    5278                 :             :              "with a pack expansion");
    5279                 :           3 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5280                 :             :       /* Avoid crash in process_partial_specialization.  */
    5281                 :           3 :       return decl;
    5282                 :             :     }
    5283                 :             : 
    5284                 :     6958823 :   else if (nargs > DECL_NTPARMS (maintmpl))
    5285                 :             :     {
    5286                 :           5 :       error ("too many arguments for partial specialization %qT", type);
    5287                 :           5 :       inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
    5288                 :             :       /* Avoid crash below.  */
    5289                 :           5 :       return decl;
    5290                 :             :     }
    5291                 :             : 
    5292                 :             :   /* If we aren't in a dependent class, we can actually try deduction.  */
    5293                 :     6958818 :   else if (tpd.level == 1
    5294                 :             :            /* FIXME we should be able to handle a partial specialization of a
    5295                 :             :               partial instantiation, but currently we can't (c++/41727).  */
    5296                 :    13398926 :            && TMPL_ARGS_DEPTH (specargs) == 1
    5297                 :    13658274 :            && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
    5298                 :             :     {
    5299                 :          18 :       auto_diagnostic_group d;
    5300                 :          18 :       if (pedwarn (input_location, 0,
    5301                 :             :                    "partial specialization %qD is not more specialized than",
    5302                 :             :                    decl))
    5303                 :          18 :         inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
    5304                 :             :                 maintmpl);
    5305                 :          18 :     }
    5306                 :             : 
    5307                 :             :   /* [temp.spec.partial]
    5308                 :             : 
    5309                 :             :      The type of a template parameter corresponding to a specialized
    5310                 :             :      non-type argument shall not be dependent on a parameter of the
    5311                 :             :      specialization.
    5312                 :             : 
    5313                 :             :      Also, we verify that pack expansions only occur at the
    5314                 :             :      end of the argument list.  */
    5315                 :     6958818 :   tpd2.parms = 0;
    5316                 :    21463419 :   for (i = 0; i < nargs; ++i)
    5317                 :             :     {
    5318                 :    14504601 :       tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
    5319                 :    14504601 :       tree arg = TREE_VEC_ELT (inner_args, i);
    5320                 :    14504601 :       tree packed_args = NULL_TREE;
    5321                 :    14504601 :       int j, len = 1;
    5322                 :             : 
    5323                 :    14504601 :       if (ARGUMENT_PACK_P (arg))
    5324                 :             :         {
    5325                 :             :           /* Extract the arguments from the argument pack. We'll be
    5326                 :             :              iterating over these in the following loop.  */
    5327                 :      673347 :           packed_args = ARGUMENT_PACK_ARGS (arg);
    5328                 :      673347 :           len = TREE_VEC_LENGTH (packed_args);
    5329                 :             :         }
    5330                 :             : 
    5331                 :    29316190 :       for (j = 0; j < len; j++)
    5332                 :             :         {
    5333                 :    14811589 :           if (packed_args)
    5334                 :             :             /* Get the Jth argument in the parameter pack.  */
    5335                 :      980335 :             arg = TREE_VEC_ELT (packed_args, j);
    5336                 :             : 
    5337                 :    14811589 :           if (PACK_EXPANSION_P (arg))
    5338                 :             :             {
    5339                 :             :               /* Pack expansions must come at the end of the
    5340                 :             :                  argument list.  */
    5341                 :      402059 :               if ((packed_args && j < len - 1)
    5342                 :           6 :                   || (!packed_args && i < nargs - 1))
    5343                 :             :                 {
    5344                 :          12 :                   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5345                 :           6 :                     error ("parameter pack argument %qE must be at the "
    5346                 :             :                            "end of the template argument list", arg);
    5347                 :             :                   else
    5348                 :           6 :                     error ("parameter pack argument %qT must be at the "
    5349                 :             :                            "end of the template argument list", arg);
    5350                 :             :                 }
    5351                 :             :             }
    5352                 :             : 
    5353                 :    14811589 :           if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    5354                 :             :             /* We only care about the pattern.  */
    5355                 :       66133 :             arg = PACK_EXPANSION_PATTERN (arg);
    5356                 :             : 
    5357                 :    14811589 :           if (/* These first two lines are the `non-type' bit.  */
    5358                 :    14811589 :               !TYPE_P (arg)
    5359                 :     4388427 :               && TREE_CODE (arg) != TEMPLATE_DECL
    5360                 :             :               /* This next two lines are the `argument expression is not just a
    5361                 :             :                  simple identifier' condition and also the `specialized
    5362                 :             :                  non-type argument' bit.  */
    5363                 :     4132816 :               && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
    5364                 :    17721385 :               && !((REFERENCE_REF_P (arg)
    5365                 :     2909769 :                     || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
    5366                 :          30 :                    && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
    5367                 :             :             {
    5368                 :             :               /* Look at the corresponding template parameter,
    5369                 :             :                  marking which template parameters its type depends
    5370                 :             :                  upon.  */
    5371                 :     2909769 :               tree type = TREE_TYPE (parm);
    5372                 :             : 
    5373                 :     2909769 :               if (!tpd2.parms)
    5374                 :             :                 {
    5375                 :             :                   /* We haven't yet initialized TPD2.  Do so now.  */
    5376                 :     1871609 :                   tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
    5377                 :             :                   /* The number of parameters here is the number in the
    5378                 :             :                      main template, which, as checked in the assertion
    5379                 :             :                      above, is NARGS.  */
    5380                 :     1871609 :                   tpd2.parms = XALLOCAVEC (int, nargs);
    5381                 :     1871609 :                   tpd2.level =
    5382                 :     1871609 :                     TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
    5383                 :             :                 }
    5384                 :             : 
    5385                 :             :               /* Mark the template parameters.  But this time, we're
    5386                 :             :                  looking for the template parameters of the main
    5387                 :             :                  template, not in the specialization.  */
    5388                 :     2909769 :               tpd2.current_arg = i;
    5389                 :     2909769 :               tpd2.arg_uses_template_parms[i] = 0;
    5390                 :     2909769 :               memset (tpd2.parms, 0, sizeof (int) * nargs);
    5391                 :     2909769 :               for_each_template_parm (type,
    5392                 :             :                                       &mark_template_parm,
    5393                 :             :                                       &tpd2,
    5394                 :             :                                       NULL,
    5395                 :             :                                       /*include_nondeduced_p=*/false);
    5396                 :             : 
    5397                 :     2909769 :               if (tpd2.arg_uses_template_parms [i])
    5398                 :             :                 {
    5399                 :             :                   /* The type depended on some template parameters.
    5400                 :             :                      If they are fully specialized in the
    5401                 :             :                      specialization, that's OK.  */
    5402                 :             :                   int j;
    5403                 :             :                   int count = 0;
    5404                 :         100 :                   for (j = 0; j < nargs; ++j)
    5405                 :          69 :                     if (tpd2.parms[j] != 0
    5406                 :          31 :                         && tpd.arg_uses_template_parms [j])
    5407                 :          24 :                       ++count;
    5408                 :          31 :                   if (count != 0)
    5409                 :          24 :                     error_n (input_location, count,
    5410                 :             :                              "type %qT of template argument %qE depends "
    5411                 :             :                              "on a template parameter",
    5412                 :             :                              "type %qT of template argument %qE depends "
    5413                 :             :                              "on template parameters",
    5414                 :             :                              type,
    5415                 :             :                              arg);
    5416                 :             :                 }
    5417                 :             :             }
    5418                 :             :         }
    5419                 :             :     }
    5420                 :             : 
    5421                 :             :   /* We should only get here once.  */
    5422                 :     6958818 :   if (TREE_CODE (decl) == TYPE_DECL)
    5423                 :     6415111 :     gcc_assert (!COMPLETE_TYPE_P (type));
    5424                 :             : 
    5425                 :             :   // Build the template decl.
    5426                 :     6958818 :   tree tmpl = build_template_decl (decl, current_template_parms,
    5427                 :     6958818 :                                    DECL_MEMBER_TEMPLATE_P (maintmpl));
    5428                 :     6958818 :   SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    5429                 :     6958818 :   DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
    5430                 :     6958818 :   DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
    5431                 :             : 
    5432                 :             :   /* Give template template parms a DECL_CONTEXT of the template
    5433                 :             :      for which they are a parameter.  */
    5434                 :    20231577 :   for (i = 0; i < ntparms; ++i)
    5435                 :             :     {
    5436                 :    13272759 :       tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
    5437                 :    13272759 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    5438                 :      105471 :         DECL_CONTEXT (parm) = tmpl;
    5439                 :             :     }
    5440                 :             : 
    5441                 :     6958818 :   if (VAR_P (decl))
    5442                 :             :     {
    5443                 :             :       /* We didn't register this in check_explicit_specialization so we could
    5444                 :             :          wait until the constraints were set.  */
    5445                 :      543707 :       tree reg = register_specialization (decl, maintmpl, specargs, false, 0);
    5446                 :      543707 :       if (reg != decl)
    5447                 :             :         /* Redeclaration.  */
    5448                 :             :         return reg;
    5449                 :             :     }
    5450                 :             :   else
    5451                 :     6415111 :     associate_classtype_constraints (type);
    5452                 :             : 
    5453                 :    13917618 :   DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
    5454                 :     6958809 :     = tree_cons (specargs, tmpl,
    5455                 :     6958809 :                  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
    5456                 :     6958809 :   TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
    5457                 :             :   /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL.  */
    5458                 :     6958809 :   gcc_checking_assert (!TI_PARTIAL_INFO (tinfo));
    5459                 :     6958809 :   TI_PARTIAL_INFO (tinfo) = build_template_info (tmpl, NULL_TREE);
    5460                 :             : 
    5461                 :    47627454 :   for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
    5462                 :    40668645 :        inst = TREE_CHAIN (inst))
    5463                 :             :     {
    5464                 :    40668645 :       tree instance = TREE_VALUE (inst);
    5465                 :    42086005 :       if (TYPE_P (instance)
    5466                 :    40668645 :           ? (COMPLETE_TYPE_P (instance)
    5467                 :    39251285 :              && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
    5468                 :     1417360 :           : DECL_TEMPLATE_INSTANTIATION (instance))
    5469                 :             :         {
    5470                 :       91974 :           tree partial_ti = most_specialized_partial_spec (instance, tf_none,
    5471                 :             :                                                            /*rechecking=*/true);
    5472                 :       91974 :           tree inst_decl = (DECL_P (instance)
    5473                 :       91974 :                             ? instance : TYPE_NAME (instance));
    5474                 :       91974 :           if (!partial_ti)
    5475                 :             :             /* OK */;
    5476                 :       45879 :           else if (partial_ti == error_mark_node)
    5477                 :           4 :             permerror (input_location,
    5478                 :             :                        "declaration of %qD ambiguates earlier template "
    5479                 :             :                        "instantiation for %qD", decl, inst_decl);
    5480                 :       45875 :           else if (TI_TEMPLATE (partial_ti) == tmpl)
    5481                 :           7 :             permerror (input_location,
    5482                 :             :                        "partial specialization of %qD after instantiation "
    5483                 :             :                        "of %qD", decl, inst_decl);
    5484                 :             :         }
    5485                 :             :     }
    5486                 :             : 
    5487                 :             :   return decl;
    5488                 :             : }
    5489                 :             : 
    5490                 :             : /* PARM is a template parameter of some form; return the corresponding
    5491                 :             :    TEMPLATE_PARM_INDEX.  */
    5492                 :             : 
    5493                 :             : static tree
    5494                 :   292501652 : get_template_parm_index (tree parm)
    5495                 :             : {
    5496                 :   292501652 :   if (TREE_CODE (parm) == PARM_DECL
    5497                 :   292501652 :       || TREE_CODE (parm) == CONST_DECL)
    5498                 :   139187438 :     parm = DECL_INITIAL (parm);
    5499                 :   153314214 :   else if (TREE_CODE (parm) == TYPE_DECL
    5500                 :     8486109 :            || TREE_CODE (parm) == TEMPLATE_DECL)
    5501                 :   144832111 :     parm = TREE_TYPE (parm);
    5502                 :   292501652 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
    5503                 :   140255200 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
    5504                 :   140255143 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
    5505                 :   152314845 :     parm = TEMPLATE_TYPE_PARM_INDEX (parm);
    5506                 :   292501652 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
    5507                 :   292501652 :   return parm;
    5508                 :             : }
    5509                 :             : 
    5510                 :             : /* Subroutine of fixed_parameter_pack_p below.  Look for any template
    5511                 :             :    parameter packs used by the template parameter PARM.  */
    5512                 :             : 
    5513                 :             : static void
    5514                 :         821 : fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
    5515                 :             : {
    5516                 :             :   /* A type parm can't refer to another parm.  */
    5517                 :         821 :   if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
    5518                 :             :     return;
    5519                 :         806 :   else if (TREE_CODE (parm) == PARM_DECL)
    5520                 :             :     {
    5521                 :         764 :       cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
    5522                 :             :                     ppd, ppd->visited);
    5523                 :         764 :       return;
    5524                 :             :     }
    5525                 :             : 
    5526                 :          42 :   gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
    5527                 :             : 
    5528                 :          42 :   tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
    5529                 :          93 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    5530                 :             :     {
    5531                 :          51 :       tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
    5532                 :          51 :       if (template_parameter_pack_p (p))
    5533                 :             :         /* Any packs in the type are expanded by this parameter.  */;
    5534                 :             :       else
    5535                 :          33 :         fixed_parameter_pack_p_1 (p, ppd);
    5536                 :             :     }
    5537                 :             : }
    5538                 :             : 
    5539                 :             : /* PARM is a template parameter pack.  Return any parameter packs used in
    5540                 :             :    its type or the type of any of its template parameters.  If there are
    5541                 :             :    any such packs, it will be instantiated into a fixed template parameter
    5542                 :             :    list by partial instantiation rather than be fully deduced.  */
    5543                 :             : 
    5544                 :             : tree
    5545                 :   148781257 : fixed_parameter_pack_p (tree parm)
    5546                 :             : {
    5547                 :             :   /* This can only be true in a member template.  */
    5548                 :   148781257 :   if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
    5549                 :             :     return NULL_TREE;
    5550                 :             :   /* This can only be true for a parameter pack.  */
    5551                 :     3058169 :   if (!template_parameter_pack_p (parm))
    5552                 :             :     return NULL_TREE;
    5553                 :             :   /* A type parm can't refer to another parm.  */
    5554                 :     3058169 :   if (TREE_CODE (parm) == TYPE_DECL)
    5555                 :             :     return NULL_TREE;
    5556                 :             : 
    5557                 :         788 :   tree parameter_packs = NULL_TREE;
    5558                 :         788 :   struct find_parameter_pack_data ppd;
    5559                 :         788 :   ppd.parameter_packs = &parameter_packs;
    5560                 :         788 :   ppd.visited = new hash_set<tree>;
    5561                 :         788 :   ppd.type_pack_expansion_p = false;
    5562                 :             : 
    5563                 :         788 :   fixed_parameter_pack_p_1 (parm, &ppd);
    5564                 :             : 
    5565                 :        1576 :   delete ppd.visited;
    5566                 :         788 :   return parameter_packs;
    5567                 :             : }
    5568                 :             : 
    5569                 :             : /* Check that a template declaration's use of default arguments and
    5570                 :             :    parameter packs is not invalid.  Here, PARMS are the template
    5571                 :             :    parameters.  IS_PRIMARY is true if DECL is the thing declared by
    5572                 :             :    a primary template.  IS_PARTIAL is true if DECL is a partial
    5573                 :             :    specialization.
    5574                 :             : 
    5575                 :             :    IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
    5576                 :             :    function template declaration or a friend class template
    5577                 :             :    declaration.  In the function case, 1 indicates a declaration, 2
    5578                 :             :    indicates a redeclaration.  When IS_FRIEND_DECL=2, no errors are
    5579                 :             :    emitted for extraneous default arguments.
    5580                 :             : 
    5581                 :             :    Returns TRUE if there were no errors found, FALSE otherwise. */
    5582                 :             : 
    5583                 :             : bool
    5584                 :   204854112 : check_default_tmpl_args (tree decl, tree parms, bool is_primary,
    5585                 :             :                          bool is_partial, int is_friend_decl)
    5586                 :             : {
    5587                 :   204854112 :   const char *msg;
    5588                 :   204854112 :   int last_level_to_check;
    5589                 :   204854112 :   tree parm_level;
    5590                 :   204854112 :   bool no_errors = true;
    5591                 :             : 
    5592                 :             :   /* [temp.param]
    5593                 :             : 
    5594                 :             :      A default template-argument shall not be specified in a
    5595                 :             :      function template declaration or a function template definition, nor
    5596                 :             :      in the template-parameter-list of the definition of a member of a
    5597                 :             :      class template.  */
    5598                 :             : 
    5599                 :   204854112 :   if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
    5600                 :   204854112 :       || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
    5601                 :             :     /* You can't have a function template declaration in a local
    5602                 :             :        scope, nor you can you define a member of a class template in a
    5603                 :             :        local scope.  */
    5604                 :             :     return true;
    5605                 :             : 
    5606                 :   204241569 :   if ((TREE_CODE (decl) == TYPE_DECL
    5607                 :    53336764 :        && TREE_TYPE (decl)
    5608                 :    92050410 :        && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5609                 :   257146935 :       || (TREE_CODE (decl) == FUNCTION_DECL
    5610                 :   153162590 :           && LAMBDA_FUNCTION_P (decl)))
    5611                 :             :     /* A lambda doesn't have an explicit declaration; don't complain
    5612                 :             :        about the parms of the enclosing class.  */
    5613                 :             :     return true;
    5614                 :             : 
    5615                 :   202929767 :   if (current_class_type
    5616                 :   158483342 :       && !TYPE_BEING_DEFINED (current_class_type)
    5617                 :    56689205 :       && DECL_LANG_SPECIFIC (decl)
    5618                 :    56603263 :       && DECL_DECLARES_FUNCTION_P (decl)
    5619                 :             :       /* If this is either a friend defined in the scope of the class
    5620                 :             :          or a member function.  */
    5621                 :   116730572 :       && (DECL_FUNCTION_MEMBER_P (decl)
    5622                 :    56302020 :           ? same_type_p (DECL_CONTEXT (decl), current_class_type)
    5623                 :           0 :           : DECL_FRIEND_CONTEXT (decl)
    5624                 :           0 :           ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
    5625                 :             :           : false)
    5626                 :             :       /* And, if it was a member function, it really was defined in
    5627                 :             :          the scope of the class.  */
    5628                 :   315533807 :       && (!DECL_FUNCTION_MEMBER_P (decl)
    5629                 :    56302020 :           || DECL_INITIALIZED_IN_CLASS_P (decl)))
    5630                 :             :     /* We already checked these parameters when the template was
    5631                 :             :        declared, so there's no need to do it again now.  This function
    5632                 :             :        was defined in class scope, but we're processing its body now
    5633                 :             :        that the class is complete.  */
    5634                 :             :     return true;
    5635                 :             : 
    5636                 :             :   /* Core issue 226 (C++0x only): the following only applies to class
    5637                 :             :      templates.  */
    5638                 :   153799048 :   if (is_primary
    5639                 :    63592796 :       && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
    5640                 :             :     {
    5641                 :             :       /* [temp.param]
    5642                 :             : 
    5643                 :             :          If a template-parameter has a default template-argument, all
    5644                 :             :          subsequent template-parameters shall have a default
    5645                 :             :          template-argument supplied.  */
    5646                 :    47061533 :       for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
    5647                 :             :         {
    5648                 :    24673626 :           tree inner_parms = TREE_VALUE (parm_level);
    5649                 :    24673626 :           int ntparms = TREE_VEC_LENGTH (inner_parms);
    5650                 :    24673626 :           int seen_def_arg_p = 0;
    5651                 :    24673626 :           int i;
    5652                 :             : 
    5653                 :    68616388 :           for (i = 0; i < ntparms; ++i)
    5654                 :             :             {
    5655                 :    43942762 :               tree parm = TREE_VEC_ELT (inner_parms, i);
    5656                 :             : 
    5657                 :    43942762 :               if (parm == error_mark_node)
    5658                 :           0 :                 continue;
    5659                 :             : 
    5660                 :    43942762 :               if (TREE_PURPOSE (parm))
    5661                 :             :                 seen_def_arg_p = 1;
    5662                 :    39974523 :               else if (seen_def_arg_p
    5663                 :    39974523 :                        && !template_parameter_pack_p (TREE_VALUE (parm)))
    5664                 :             :                 {
    5665                 :          27 :                   error ("no default argument for %qD", TREE_VALUE (parm));
    5666                 :             :                   /* For better subsequent error-recovery, we indicate that
    5667                 :             :                      there should have been a default argument.  */
    5668                 :          27 :                   TREE_PURPOSE (parm) = error_mark_node;
    5669                 :          27 :                   no_errors = false;
    5670                 :             :                 }
    5671                 :    39974496 :               else if (!is_partial
    5672                 :    39974496 :                        && !is_friend_decl
    5673                 :             :                        /* Don't complain about an enclosing partial
    5674                 :             :                           specialization.  */
    5675                 :    24881083 :                        && parm_level == parms
    5676                 :    22001000 :                        && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
    5677                 :    20905219 :                        && i < ntparms - 1
    5678                 :     9142447 :                        && template_parameter_pack_p (TREE_VALUE (parm))
    5679                 :             :                        /* A fixed parameter pack will be partially
    5680                 :             :                           instantiated into a fixed length list.  */
    5681                 :    39974532 :                        && !fixed_parameter_pack_p (TREE_VALUE (parm)))
    5682                 :             :                 {
    5683                 :             :                   /* A primary class template, primary variable template
    5684                 :             :                      (DR 2032), or alias template can only have one
    5685                 :             :                      parameter pack, at the end of the template
    5686                 :             :                      parameter list.  */
    5687                 :             : 
    5688                 :          21 :                   error ("parameter pack %q+D must be at the end of the"
    5689                 :          21 :                          " template parameter list", TREE_VALUE (parm));
    5690                 :             : 
    5691                 :          21 :                   TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
    5692                 :          21 :                     = error_mark_node;
    5693                 :          21 :                   no_errors = false;
    5694                 :             :                 }
    5695                 :             :             }
    5696                 :             :         }
    5697                 :             :     }
    5698                 :             : 
    5699                 :   153799048 :   if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
    5700                 :             :       || is_partial
    5701                 :   153412099 :       || !is_primary
    5702                 :    56498488 :       || is_friend_decl)
    5703                 :             :     /* For an ordinary class template, default template arguments are
    5704                 :             :        allowed at the innermost level, e.g.:
    5705                 :             :          template <class T = int>
    5706                 :             :          struct S {};
    5707                 :             :        but, in a partial specialization, they're not allowed even
    5708                 :             :        there, as we have in [temp.class.spec]:
    5709                 :             : 
    5710                 :             :          The template parameter list of a specialization shall not
    5711                 :             :          contain default template argument values.
    5712                 :             : 
    5713                 :             :        So, for a partial specialization, or for a function template
    5714                 :             :        (in C++98/C++03), we look at all of them.  */
    5715                 :             :     ;
    5716                 :             :   else
    5717                 :             :     /* But, for a primary class template that is not a partial
    5718                 :             :        specialization we look at all template parameters except the
    5719                 :             :        innermost ones.  */
    5720                 :    54999946 :     parms = TREE_CHAIN (parms);
    5721                 :             : 
    5722                 :             :   /* Figure out what error message to issue.  */
    5723                 :   153799048 :   if (is_friend_decl == 2)
    5724                 :             :     msg = G_("default template arguments may not be used in function template "
    5725                 :             :              "friend re-declaration");
    5726                 :   153596313 :   else if (is_friend_decl)
    5727                 :             :     msg = G_("default template arguments may not be used in template "
    5728                 :             :              "friend declarations");
    5729                 :   152300506 :   else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
    5730                 :             :     msg = G_("default template arguments may not be used in function templates "
    5731                 :             :              "without %<-std=c++11%> or %<-std=gnu++11%>");
    5732                 :   151924979 :   else if (is_partial)
    5733                 :             :     msg = G_("default template arguments may not be used in "
    5734                 :             :              "partial specializations");
    5735                 :   144966059 :   else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
    5736                 :             :     msg = G_("default argument for template parameter for class enclosing %qD");
    5737                 :             :   else
    5738                 :             :     /* Per [temp.param]/9, "A default template-argument shall not be
    5739                 :             :        specified in the template-parameter-lists of the definition of
    5740                 :             :        a member of a class template that appears outside of the member's
    5741                 :             :        class.", thus if we aren't handling a member of a class template
    5742                 :             :        there is no need to examine the parameters.  */
    5743                 :             :     return true;
    5744                 :             : 
    5745                 :    87051272 :   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
    5746                 :             :     /* If we're inside a class definition, there's no need to
    5747                 :             :        examine the parameters to the class itself.  On the one
    5748                 :             :        hand, they will be checked when the class is defined, and,
    5749                 :             :        on the other, default arguments are valid in things like:
    5750                 :             :          template <class T = double>
    5751                 :             :          struct S { template <class U> void f(U); };
    5752                 :             :        Here the default argument for `S' has no bearing on the
    5753                 :             :        declaration of `f'.  */
    5754                 :    73185943 :     last_level_to_check = template_class_depth (current_class_type) + 1;
    5755                 :             :   else
    5756                 :             :     /* Check everything.  */
    5757                 :             :     last_level_to_check = 0;
    5758                 :             : 
    5759                 :    87051272 :   for (parm_level = parms;
    5760                 :   194935909 :        parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
    5761                 :    17562590 :        parm_level = TREE_CHAIN (parm_level))
    5762                 :             :     {
    5763                 :    17562593 :       tree inner_parms = TREE_VALUE (parm_level);
    5764                 :    17562593 :       int i;
    5765                 :    17562593 :       int ntparms;
    5766                 :             : 
    5767                 :    17562593 :       ntparms = TREE_VEC_LENGTH (inner_parms);
    5768                 :    55107582 :       for (i = 0; i < ntparms; ++i)
    5769                 :             :         {
    5770                 :    37544992 :           if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
    5771                 :           0 :             continue;
    5772                 :             : 
    5773                 :    37544992 :           if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
    5774                 :             :             {
    5775                 :          36 :               if (msg)
    5776                 :             :                 {
    5777                 :          36 :                   no_errors = false;
    5778                 :          36 :                   if (is_friend_decl == 2)
    5779                 :             :                     return no_errors;
    5780                 :             : 
    5781                 :          33 :                   error (msg, decl);
    5782                 :          33 :                   msg = 0;
    5783                 :             :                 }
    5784                 :             : 
    5785                 :             :               /* Clear out the default argument so that we are not
    5786                 :             :                  confused later.  */
    5787                 :          33 :               TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
    5788                 :             :             }
    5789                 :             :         }
    5790                 :             : 
    5791                 :             :       /* At this point, if we're still interested in issuing messages,
    5792                 :             :          they must apply to classes surrounding the object declared.  */
    5793                 :    17562590 :       if (msg)
    5794                 :    17562556 :         msg = G_("default argument for template parameter for class "
    5795                 :             :                  "enclosing %qD");
    5796                 :             :     }
    5797                 :             : 
    5798                 :             :   return no_errors;
    5799                 :             : }
    5800                 :             : 
    5801                 :             : /* Worker for push_template_decl_real, called via
    5802                 :             :    for_each_template_parm.  DATA is really an int, indicating the
    5803                 :             :    level of the parameters we are interested in.  If T is a template
    5804                 :             :    parameter of that level, return nonzero.  */
    5805                 :             : 
    5806                 :             : static int
    5807                 :     3134791 : template_parm_this_level_p (tree t, void* data)
    5808                 :             : {
    5809                 :     3134791 :   int this_level = *(int *)data;
    5810                 :     3134791 :   int level;
    5811                 :             : 
    5812                 :     3134791 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5813                 :      379083 :     level = TEMPLATE_PARM_LEVEL (t);
    5814                 :             :   else
    5815                 :     2755708 :     level = TEMPLATE_TYPE_LEVEL (t);
    5816                 :     3134791 :   return level == this_level;
    5817                 :             : }
    5818                 :             : 
    5819                 :             : /* Worker for uses_outer_template_parms, called via for_each_template_parm.
    5820                 :             :    DATA is really an int, indicating the innermost outer level of parameters.
    5821                 :             :    If T is a template parameter of that level or further out, return
    5822                 :             :    nonzero.  */
    5823                 :             : 
    5824                 :             : static int
    5825                 :     8727355 : template_parm_outer_level (tree t, void *data)
    5826                 :             : {
    5827                 :     8727355 :   int this_level = *(int *)data;
    5828                 :     8727355 :   int level;
    5829                 :             : 
    5830                 :     8727355 :   if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    5831                 :      242828 :     level = TEMPLATE_PARM_LEVEL (t);
    5832                 :             :   else
    5833                 :     8484527 :     level = TEMPLATE_TYPE_LEVEL (t);
    5834                 :     8727355 :   return level <= this_level;
    5835                 :             : }
    5836                 :             : 
    5837                 :             : /* Creates a TEMPLATE_DECL for the indicated DECL using the template
    5838                 :             :    parameters given by current_template_args, or reuses a
    5839                 :             :    previously existing one, if appropriate.  Returns the DECL, or an
    5840                 :             :    equivalent one, if it is replaced via a call to duplicate_decls.
    5841                 :             : 
    5842                 :             :    If IS_FRIEND is true, DECL is a friend declaration.  */
    5843                 :             : 
    5844                 :             : tree
    5845                 :   262897675 : push_template_decl (tree decl, bool is_friend)
    5846                 :             : {
    5847                 :   262897675 :   if (decl == error_mark_node || !current_template_parms)
    5848                 :             :     return error_mark_node;
    5849                 :             : 
    5850                 :             :   /* See if this is a partial specialization.  */
    5851                 :    63911000 :   bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
    5852                 :    17501819 :                       && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
    5853                 :    17210917 :                       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
    5854                 :   320393444 :                      || (VAR_P (decl)
    5855                 :    48260919 :                          && DECL_LANG_SPECIFIC (decl)
    5856                 :     4688730 :                          && DECL_TEMPLATE_SPECIALIZATION (decl)
    5857                 :      543725 :                          && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
    5858                 :             : 
    5859                 :             :   /* No surprising friend functions.  */
    5860                 :   262897653 :   gcc_checking_assert (is_friend
    5861                 :             :                        || !(TREE_CODE (decl) == FUNCTION_DECL
    5862                 :             :                             && DECL_UNIQUE_FRIEND_P (decl)));
    5863                 :             : 
    5864                 :   262897653 :   tree ctx;
    5865                 :   262897653 :   if (is_friend)
    5866                 :             :     /* For a friend, we want the context of the friend, not
    5867                 :             :        the type of which it is a friend.  */
    5868                 :     5748963 :     ctx = CP_DECL_CONTEXT (decl);
    5869                 :   257148690 :   else if (CP_DECL_CONTEXT (decl)
    5870                 :   257148690 :            && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
    5871                 :             :     /* In the case of a virtual function, we want the class in which
    5872                 :             :        it is defined.  */
    5873                 :   213157879 :     ctx = CP_DECL_CONTEXT (decl);
    5874                 :             :   else
    5875                 :             :     /* Otherwise, if we're currently defining some class, the DECL
    5876                 :             :        is assumed to be a member of the class.  */
    5877                 :    43990811 :     ctx = current_scope ();
    5878                 :             : 
    5879                 :   262897653 :   if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
    5880                 :    49706800 :     ctx = NULL_TREE;
    5881                 :             : 
    5882                 :   262897653 :   if (!DECL_CONTEXT (decl))
    5883                 :       44582 :     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
    5884                 :             : 
    5885                 :             :   /* See if this is a primary template.  */
    5886                 :   262897653 :   bool is_primary = false;
    5887                 :   262897653 :   if (is_friend && ctx
    5888                 :   262897653 :       && uses_template_parms_level (ctx, current_template_depth))
    5889                 :             :     /* A friend template that specifies a class context, i.e.
    5890                 :             :          template <typename T> friend void A<T>::f();
    5891                 :             :        is not primary.  */
    5892                 :             :     ;
    5893                 :   306545144 :   else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    5894                 :             :     /* Lambdas are not primary.  */
    5895                 :             :     ;
    5896                 :             :   else
    5897                 :   262035305 :     is_primary = template_parm_scope_p ();
    5898                 :             : 
    5899                 :             :   /* True if the template is a member template, in the sense of
    5900                 :             :      [temp.mem].  */
    5901                 :   262035305 :   bool member_template_p = false;
    5902                 :             : 
    5903                 :   262035305 :   if (is_primary)
    5904                 :             :     {
    5905                 :    64166835 :       warning (OPT_Wtemplates, "template %qD declared", decl);
    5906                 :             : 
    5907                 :    64166835 :       if (DECL_CLASS_SCOPE_P (decl))
    5908                 :             :         member_template_p = true;
    5909                 :             : 
    5910                 :    64166835 :       if (TREE_CODE (decl) == TYPE_DECL
    5911                 :    64166835 :           && IDENTIFIER_ANON_P (DECL_NAME (decl)))
    5912                 :             :         {
    5913                 :           8 :           error ("template class without a name");
    5914                 :           8 :           return error_mark_node;
    5915                 :             :         }
    5916                 :    64166827 :       else if (TREE_CODE (decl) == FUNCTION_DECL)
    5917                 :             :         {
    5918                 :    42210921 :           if (member_template_p)
    5919                 :             :             {
    5920                 :    15797062 :               if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
    5921                 :           6 :                 error ("member template %qD may not have virt-specifiers", decl);
    5922                 :             :             }
    5923                 :    84421842 :           if (DECL_DESTRUCTOR_P (decl))
    5924                 :             :             {
    5925                 :             :               /* [temp.mem]
    5926                 :             : 
    5927                 :             :                  A destructor shall not be a member template.  */
    5928                 :           8 :               error_at (DECL_SOURCE_LOCATION (decl),
    5929                 :             :                         "destructor %qD declared as member template", decl);
    5930                 :           8 :               return error_mark_node;
    5931                 :             :             }
    5932                 :    53432690 :           if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
    5933                 :    42215115 :               && (!prototype_p (TREE_TYPE (decl))
    5934                 :        4202 :                   || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
    5935                 :        4202 :                   || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    5936                 :        4202 :                   || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
    5937                 :             :                       == void_list_node)))
    5938                 :             :             {
    5939                 :             :               /* [basic.stc.dynamic.allocation]
    5940                 :             : 
    5941                 :             :                  An allocation function can be a function
    5942                 :             :                  template. ... Template allocation functions shall
    5943                 :             :                  have two or more parameters.  */
    5944                 :          12 :               error ("invalid template declaration of %qD", decl);
    5945                 :          12 :               return error_mark_node;
    5946                 :             :             }
    5947                 :             :         }
    5948                 :    19228428 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5949                 :    37768613 :                && CLASS_TYPE_P (TREE_TYPE (decl)))
    5950                 :             :         /* Class template.  */;
    5951                 :     6143206 :       else if (TREE_CODE (decl) == TYPE_DECL
    5952                 :     6143206 :                && TYPE_DECL_ALIAS_P (decl))
    5953                 :             :         /* alias-declaration */
    5954                 :     3415718 :         gcc_assert (!DECL_ARTIFICIAL (decl));
    5955                 :     2727488 :       else if (VAR_P (decl))
    5956                 :             :         /* C++14 variable template. */;
    5957                 :      542351 :       else if (TREE_CODE (decl) == CONCEPT_DECL)
    5958                 :             :         /* C++20 concept definitions.  */;
    5959                 :             :       else
    5960                 :             :         {
    5961                 :          10 :           error ("template declaration of %q#D", decl);
    5962                 :          10 :           return error_mark_node;
    5963                 :             :         }
    5964                 :             :     }
    5965                 :             : 
    5966                 :    63910982 :   bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
    5967                 :   309306793 :                   && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
    5968                 :   191306138 :                       || (VAR_OR_FUNCTION_DECL_P (decl)
    5969                 :   154612881 :                           && DECL_LOCAL_DECL_P (decl))));
    5970                 :             : 
    5971                 :             :   /* Check to see that the rules regarding the use of default
    5972                 :             :      arguments are not being violated.  We check args for a friend
    5973                 :             :      functions when we know whether it's a definition, introducing
    5974                 :             :      declaration or re-declaration.  */
    5975                 :   208807942 :   if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
    5976                 :   203393300 :     check_default_tmpl_args (decl, current_template_parms,
    5977                 :             :                              is_primary, is_partial, is_friend);
    5978                 :             : 
    5979                 :             :   /* Ensure that there are no parameter packs in the type of this
    5980                 :             :      declaration that have not been expanded.  */
    5981                 :   262897615 :   if (TREE_CODE (decl) == FUNCTION_DECL)
    5982                 :             :     {
    5983                 :             :       /* Check each of the arguments individually to see if there are
    5984                 :             :          any bare parameter packs.  */
    5985                 :   150183373 :       tree type = TREE_TYPE (decl);
    5986                 :   150183373 :       tree arg = DECL_ARGUMENTS (decl);
    5987                 :   150183373 :       tree argtype = TYPE_ARG_TYPES (type);
    5988                 :             : 
    5989                 :   481509149 :       while (arg && argtype)
    5990                 :             :         {
    5991                 :   331325776 :           if (!DECL_PACK_P (arg)
    5992                 :   658306436 :               && check_for_bare_parameter_packs (TREE_TYPE (arg)))
    5993                 :             :             {
    5994                 :             :             /* This is a PARM_DECL that contains unexpanded parameter
    5995                 :             :                packs. We have already complained about this in the
    5996                 :             :                check_for_bare_parameter_packs call, so just replace
    5997                 :             :                these types with ERROR_MARK_NODE.  */
    5998                 :          42 :               TREE_TYPE (arg) = error_mark_node;
    5999                 :          42 :               TREE_VALUE (argtype) = error_mark_node;
    6000                 :             :             }
    6001                 :             : 
    6002                 :   331325776 :           arg = DECL_CHAIN (arg);
    6003                 :   331325776 :           argtype = TREE_CHAIN (argtype);
    6004                 :             :         }
    6005                 :             : 
    6006                 :             :       /* Check for bare parameter packs in the return type and the
    6007                 :             :          exception specifiers.  */
    6008                 :   150183373 :       if (check_for_bare_parameter_packs (TREE_TYPE (type)))
    6009                 :             :         /* Errors were already issued, set return type to int
    6010                 :             :            as the frontend doesn't expect error_mark_node as
    6011                 :             :            the return type.  */
    6012                 :           3 :         TREE_TYPE (type) = integer_type_node;
    6013                 :   150183373 :       if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
    6014                 :           0 :         TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
    6015                 :             :     }
    6016                 :             :   else
    6017                 :             :     {
    6018                 :   225428484 :       if (check_for_bare_parameter_packs (is_typedef_decl (decl)
    6019                 :             :                                           ? DECL_ORIGINAL_TYPE (decl)
    6020                 :    66305142 :                                           : TREE_TYPE (decl)))
    6021                 :             :         {
    6022                 :          64 :           TREE_TYPE (decl) = error_mark_node;
    6023                 :          64 :           return error_mark_node;
    6024                 :             :         }
    6025                 :             : 
    6026                 :     6958887 :       if (is_partial && VAR_P (decl)
    6027                 :   113257889 :           && check_for_bare_parameter_packs (DECL_TI_ARGS (decl)))
    6028                 :           3 :         return error_mark_node;
    6029                 :             :     }
    6030                 :             : 
    6031                 :   262897548 :   if (is_partial)
    6032                 :     6958884 :     return process_partial_specialization (decl);
    6033                 :             : 
    6034                 :   255938664 :   tree args = current_template_args ();
    6035                 :   255938664 :   tree tmpl = NULL_TREE;
    6036                 :   255938664 :   bool new_template_p = false;
    6037                 :   255938664 :   if (local_p)
    6038                 :             :     {
    6039                 :             :       /* Does not get a template head.  */
    6040                 :    54089664 :       tmpl = NULL_TREE;
    6041                 :    54089664 :       gcc_checking_assert (!is_primary);
    6042                 :             :     }
    6043                 :   201849000 :   else if (!ctx
    6044                 :   158575315 :            || TREE_CODE (ctx) == FUNCTION_DECL
    6045                 :   157962772 :            || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
    6046                 :    56775549 :            || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
    6047                 :   258538257 :            || (is_friend && !(DECL_LANG_SPECIFIC (decl)
    6048                 :          93 :                               && DECL_TEMPLATE_INFO (decl))))
    6049                 :             :     {
    6050                 :   145159840 :       if (DECL_LANG_SPECIFIC (decl)
    6051                 :   132050530 :           && DECL_TEMPLATE_INFO (decl)
    6052                 :   148578588 :           && DECL_TI_TEMPLATE (decl))
    6053                 :     3418748 :         tmpl = DECL_TI_TEMPLATE (decl);
    6054                 :             :       /* If DECL is a TYPE_DECL for a class-template, then there won't
    6055                 :             :          be DECL_LANG_SPECIFIC.  The information equivalent to
    6056                 :             :          DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead.  */
    6057                 :    47151385 :       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
    6058                 :    11000487 :                && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
    6059                 :   141787948 :                && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
    6060                 :             :         {
    6061                 :             :           /* Since a template declaration already existed for this
    6062                 :             :              class-type, we must be redeclaring it here.  Make sure
    6063                 :             :              that the redeclaration is valid.  */
    6064                 :       23428 :           redeclare_class_template (TREE_TYPE (decl),
    6065                 :             :                                     current_template_parms,
    6066                 :             :                                     current_template_constraints ());
    6067                 :             :           /* We don't need to create a new TEMPLATE_DECL; just use the
    6068                 :             :              one we already had.  */
    6069                 :       46856 :           tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
    6070                 :             :         }
    6071                 :             :       else
    6072                 :             :         {
    6073                 :   141717664 :           tmpl = build_template_decl (decl, current_template_parms,
    6074                 :             :                                       member_template_p);
    6075                 :   141717664 :           new_template_p = true;
    6076                 :             : 
    6077                 :   141717664 :           if (DECL_LANG_SPECIFIC (decl)
    6078                 :   141717664 :               && DECL_TEMPLATE_SPECIALIZATION (decl))
    6079                 :             :             {
    6080                 :             :               /* A specialization of a member template of a template
    6081                 :             :                  class.  */
    6082                 :           0 :               SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
    6083                 :           0 :               DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
    6084                 :           0 :               DECL_TEMPLATE_INFO (decl) = NULL_TREE;
    6085                 :             :             }
    6086                 :             :         }
    6087                 :             :     }
    6088                 :             :   else
    6089                 :             :     {
    6090                 :    56689160 :       tree a, t, current, parms;
    6091                 :    56689160 :       int i;
    6092                 :    56689160 :       tree tinfo = get_template_info (decl);
    6093                 :             : 
    6094                 :    56689160 :       if (!tinfo)
    6095                 :             :         {
    6096                 :          19 :           error ("template definition of non-template %q#D", decl);
    6097                 :          19 :           return error_mark_node;
    6098                 :             :         }
    6099                 :             : 
    6100                 :    56689141 :       tmpl = TI_TEMPLATE (tinfo);
    6101                 :             : 
    6102                 :    56689141 :       if (DECL_FUNCTION_TEMPLATE_P (tmpl)
    6103                 :    56302020 :           && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
    6104                 :    56302020 :           && DECL_TEMPLATE_SPECIALIZATION (decl)
    6105                 :    56689162 :           && DECL_MEMBER_TEMPLATE_P (tmpl))
    6106                 :             :         {
    6107                 :             :           /* The declaration is a specialization of a member
    6108                 :             :              template, declared outside the class.  Therefore, the
    6109                 :             :              innermost template arguments will be NULL, so we
    6110                 :             :              replace them with the arguments determined by the
    6111                 :             :              earlier call to check_explicit_specialization.  */
    6112                 :          13 :           args = DECL_TI_ARGS (decl);
    6113                 :             : 
    6114                 :          13 :           tree new_tmpl
    6115                 :          13 :             = build_template_decl (decl, current_template_parms,
    6116                 :             :                                    member_template_p);
    6117                 :          13 :           DECL_TI_TEMPLATE (decl) = new_tmpl;
    6118                 :          13 :           SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
    6119                 :          13 :           DECL_TEMPLATE_INFO (new_tmpl)
    6120                 :          13 :             = build_template_info (tmpl, args);
    6121                 :             : 
    6122                 :          13 :           register_specialization (new_tmpl,
    6123                 :             :                                    most_general_template (tmpl),
    6124                 :             :                                    args,
    6125                 :             :                                    is_friend, 0);
    6126                 :          13 :           return decl;
    6127                 :             :         }
    6128                 :             : 
    6129                 :             :       /* Make sure the template headers we got make sense.  */
    6130                 :             : 
    6131                 :    56689128 :       parms = DECL_TEMPLATE_PARMS (tmpl);
    6132                 :    56689128 :       i = TMPL_PARMS_DEPTH (parms);
    6133                 :   157741190 :       if (TMPL_ARGS_DEPTH (args) != i)
    6134                 :             :         {
    6135                 :          16 :           error ("expected %d levels of template parms for %q#D, got %d",
    6136                 :           8 :                  i, decl, TMPL_ARGS_DEPTH (args));
    6137                 :           8 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6138                 :           8 :           return error_mark_node;
    6139                 :             :         }
    6140                 :             :       else
    6141                 :   125766893 :         for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
    6142                 :             :           {
    6143                 :   138155586 :             a = TMPL_ARGS_LEVEL (args, i);
    6144                 :    69077793 :             t = INNERMOST_TEMPLATE_PARMS (parms);
    6145                 :             : 
    6146                 :    69077793 :             if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
    6147                 :             :               {
    6148                 :          20 :                 if (current == decl)
    6149                 :          20 :                   error ("got %d template parameters for %q#D",
    6150                 :          20 :                          TREE_VEC_LENGTH (a), decl);
    6151                 :             :                 else
    6152                 :           0 :                   error ("got %d template parameters for %q#T",
    6153                 :           0 :                          TREE_VEC_LENGTH (a), current);
    6154                 :          20 :                 error ("  but %d required", TREE_VEC_LENGTH (t));
    6155                 :             :                 /* Avoid crash in import_export_decl.  */
    6156                 :          20 :                 DECL_INTERFACE_KNOWN (decl) = 1;
    6157                 :          20 :                 return error_mark_node;
    6158                 :             :               }
    6159                 :             : 
    6160                 :    69077773 :             if (current == decl)
    6161                 :             :               current = ctx;
    6162                 :    12388673 :             else if (current == NULL_TREE)
    6163                 :             :               /* Can happen in erroneous input.  */
    6164                 :             :               break;
    6165                 :             :             else
    6166                 :    12388673 :               current = get_containing_scope (current);
    6167                 :             :           }
    6168                 :             : 
    6169                 :             :       /* Check that the parms are used in the appropriate qualifying scopes
    6170                 :             :          in the declarator.  */
    6171                 :   113378200 :       if (!comp_template_args
    6172                 :    56689100 :           (TI_ARGS (tinfo),
    6173                 :    56689100 :            TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
    6174                 :             :         {
    6175                 :           4 :           error ("template arguments to %qD do not match original "
    6176                 :           4 :                  "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
    6177                 :           4 :           if (!uses_template_parms (TI_ARGS (tinfo)))
    6178                 :           4 :             inform (input_location, "use %<template<>%> for"
    6179                 :             :                     " an explicit specialization");
    6180                 :             :           /* Avoid crash in import_export_decl.  */
    6181                 :           4 :           DECL_INTERFACE_KNOWN (decl) = 1;
    6182                 :           4 :           return error_mark_node;
    6183                 :             :         }
    6184                 :             : 
    6185                 :             :       /* Check that the constraints for each enclosing template scope are
    6186                 :             :          consistent with the original declarations.  */
    6187                 :    56689096 :       if (flag_concepts)
    6188                 :             :         {
    6189                 :    14829089 :           tree decl_parms = DECL_TEMPLATE_PARMS (tmpl);
    6190                 :    14829089 :           tree scope_parms = current_template_parms;
    6191                 :    14829089 :           if (PRIMARY_TEMPLATE_P (tmpl))
    6192                 :             :             {
    6193                 :     4224727 :               decl_parms = TREE_CHAIN (decl_parms);
    6194                 :     4224727 :               scope_parms = TREE_CHAIN (scope_parms);
    6195                 :             :             }
    6196                 :    28888934 :           while (decl_parms)
    6197                 :             :             {
    6198                 :    14059851 :               if (!template_requirements_equivalent_p (decl_parms, scope_parms))
    6199                 :             :                 {
    6200                 :           6 :                   error ("redeclaration of %qD with different constraints",
    6201                 :           6 :                          TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)));
    6202                 :           6 :                   break;
    6203                 :             :                 }
    6204                 :    14059845 :               decl_parms = TREE_CHAIN (decl_parms);
    6205                 :    14059845 :               scope_parms = TREE_CHAIN (scope_parms);
    6206                 :             :             }
    6207                 :             :         }
    6208                 :             :     }
    6209                 :             : 
    6210                 :   403697872 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6211                 :             : 
    6212                 :   255938600 :   if (new_template_p)
    6213                 :             :     {
    6214                 :             :       /* Push template declarations for global functions and types.
    6215                 :             :          Note that we do not try to push a global template friend
    6216                 :             :          declared in a template class; such a thing may well depend on
    6217                 :             :          the template parameters of the class and we'll push it when
    6218                 :             :          instantiating the befriending class.  */
    6219                 :   141717664 :       if (!ctx
    6220                 :   141717664 :           && !(is_friend && template_class_depth (current_class_type) > 0))
    6221                 :             :         {
    6222                 :    37807796 :           tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
    6223                 :    37807796 :           if (pushed == error_mark_node)
    6224                 :             :             return error_mark_node;
    6225                 :             : 
    6226                 :             :           /* pushdecl may have found an existing template.  */
    6227                 :    37807745 :           if (pushed != tmpl)
    6228                 :             :             {
    6229                 :     2831080 :               decl = DECL_TEMPLATE_RESULT (pushed);
    6230                 :     2831080 :               tmpl = NULL_TREE;
    6231                 :             :             }
    6232                 :             :         }
    6233                 :   103909868 :       else if (is_friend)
    6234                 :             :         {
    6235                 :             :           /* Record this decl as belonging to the current class.  It's
    6236                 :             :              not chained onto anything else.  */
    6237                 :     2495370 :           DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
    6238                 :     2495370 :           gcc_checking_assert (!DECL_CHAIN (tmpl));
    6239                 :     2495370 :           DECL_CHAIN (tmpl) = current_scope ();
    6240                 :             :         }
    6241                 :             :     }
    6242                 :   114220936 :   else if (tmpl)
    6243                 :             :     /* The type may have been completed, or (erroneously) changed.  */
    6244                 :    60131272 :     TREE_TYPE (tmpl) = TREE_TYPE (decl);
    6245                 :             : 
    6246                 :   201848885 :   if (tmpl)
    6247                 :             :     {
    6248                 :   199017805 :       if (is_primary)
    6249                 :             :         {
    6250                 :    54376785 :           tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6251                 :             : 
    6252                 :    54376785 :           DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6253                 :             : 
    6254                 :             :           /* Give template template parms a DECL_CONTEXT of the template
    6255                 :             :              for which they are a parameter.  */
    6256                 :    54376785 :           parms = INNERMOST_TEMPLATE_PARMS (parms);
    6257                 :   154637817 :           for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
    6258                 :             :             {
    6259                 :   100261032 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6260                 :   100261032 :               if (TREE_CODE (parm) == TEMPLATE_DECL)
    6261                 :      185699 :                 DECL_CONTEXT (parm) = tmpl;
    6262                 :             :             }
    6263                 :             : 
    6264                 :    54376785 :           if (TREE_CODE (decl) == TYPE_DECL
    6265                 :    54376785 :               && TYPE_DECL_ALIAS_P (decl))
    6266                 :             :             {
    6267                 :     3415688 :               if (tree constr
    6268                 :     3415688 :                   = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
    6269                 :             :                 {
    6270                 :             :                   /* ??? Why don't we do this here for all templates?  */
    6271                 :       63008 :                   constr = build_constraints (constr, NULL_TREE);
    6272                 :       63008 :                   set_constraints (decl, constr);
    6273                 :             :                 }
    6274                 :             :             }
    6275                 :             :         }
    6276                 :             : 
    6277                 :             :       /* The DECL_TI_ARGS of DECL contains full set of arguments
    6278                 :             :          referring wback to its most general template.  If TMPL is a
    6279                 :             :          specialization, ARGS may only have the innermost set of
    6280                 :             :          arguments.  Add the missing argument levels if necessary.  */
    6281                 :   199017805 :       if (DECL_TEMPLATE_INFO (tmpl))
    6282                 :         926 :         args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
    6283                 :             : 
    6284                 :   199017805 :       tree info = build_template_info (tmpl, args);
    6285                 :             : 
    6286                 :   199017805 :       if (DECL_IMPLICIT_TYPEDEF_P (decl))
    6287                 :    11086563 :         SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
    6288                 :             :       else
    6289                 :             :         {
    6290                 :   187931242 :           retrofit_lang_decl (decl);
    6291                 :   187931242 :           DECL_TEMPLATE_INFO (decl) = info;
    6292                 :             :         }
    6293                 :             :     }
    6294                 :             : 
    6295                 :   255938549 :   if (flag_implicit_templates
    6296                 :   255224782 :       && !is_friend
    6297                 :   249482609 :       && TREE_PUBLIC (decl)
    6298                 :   146678896 :       && VAR_OR_FUNCTION_DECL_P (decl))
    6299                 :             :     /* Set DECL_COMDAT on template instantiations; if we force
    6300                 :             :        them to be emitted by explicit instantiation,
    6301                 :             :        mark_needed will tell cgraph to do the right thing.  */
    6302                 :   146569652 :     DECL_COMDAT (decl) = true;
    6303                 :             : 
    6304                 :   454956354 :   gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
    6305                 :             : 
    6306                 :             :   return decl;
    6307                 :             : }
    6308                 :             : 
    6309                 :             : /* FN is an inheriting constructor that inherits from the constructor
    6310                 :             :    template INHERITED; turn FN into a constructor template with a matching
    6311                 :             :    template header.  */
    6312                 :             : 
    6313                 :             : tree
    6314                 :       58269 : add_inherited_template_parms (tree fn, tree inherited)
    6315                 :             : {
    6316                 :       58269 :   tree inner_parms
    6317                 :       58269 :     = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
    6318                 :       58269 :   inner_parms = copy_node (inner_parms);
    6319                 :       58269 :   tree parms
    6320                 :       58269 :     = tree_cons (size_int (current_template_depth + 1),
    6321                 :             :                  inner_parms, current_template_parms);
    6322                 :       58269 :   tree tmpl = build_template_decl (fn, parms, /*member*/true);
    6323                 :       58269 :   tree args = template_parms_to_args (parms);
    6324                 :       58269 :   DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
    6325                 :       58269 :   DECL_ARTIFICIAL (tmpl) = true;
    6326                 :       58269 :   DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
    6327                 :       58269 :   return tmpl;
    6328                 :             : }
    6329                 :             : 
    6330                 :             : /* Called when a class template TYPE is redeclared with the indicated
    6331                 :             :    template PARMS, e.g.:
    6332                 :             : 
    6333                 :             :      template <class T> struct S;
    6334                 :             :      template <class T> struct S {};  */
    6335                 :             : 
    6336                 :             : bool
    6337                 :     2014657 : redeclare_class_template (tree type, tree parms, tree cons)
    6338                 :             : {
    6339                 :     2014657 :   tree tmpl;
    6340                 :     2014657 :   tree tmpl_parms;
    6341                 :     2014657 :   int i;
    6342                 :             : 
    6343                 :     2014657 :   if (!TYPE_TEMPLATE_INFO (type))
    6344                 :             :     {
    6345                 :           0 :       error ("%qT is not a template type", type);
    6346                 :           0 :       return false;
    6347                 :             :     }
    6348                 :             : 
    6349                 :     2014657 :   tmpl = TYPE_TI_TEMPLATE (type);
    6350                 :     2014657 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6351                 :             :     /* The type is nested in some template class.  Nothing to worry
    6352                 :             :        about here; there are no new template parameters for the nested
    6353                 :             :        type.  */
    6354                 :             :     return true;
    6355                 :             : 
    6356                 :     2014657 :   if (!parms)
    6357                 :             :     {
    6358                 :           4 :       error ("template specifiers not specified in declaration of %qD",
    6359                 :             :              tmpl);
    6360                 :           4 :       return false;
    6361                 :             :     }
    6362                 :             : 
    6363                 :     2014653 :   parms = INNERMOST_TEMPLATE_PARMS (parms);
    6364                 :     2014653 :   tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
    6365                 :             : 
    6366                 :     2014653 :   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
    6367                 :             :     {
    6368                 :          24 :       error_n (input_location, TREE_VEC_LENGTH (parms),
    6369                 :             :                "redeclared with %d template parameter",
    6370                 :             :                "redeclared with %d template parameters",
    6371                 :          24 :                TREE_VEC_LENGTH (parms));
    6372                 :          24 :       inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
    6373                 :             :                 "previous declaration %qD used %d template parameter",
    6374                 :             :                 "previous declaration %qD used %d template parameters",
    6375                 :          24 :                 tmpl, TREE_VEC_LENGTH (tmpl_parms));
    6376                 :          24 :       return false;
    6377                 :             :     }
    6378                 :             : 
    6379                 :     5407174 :   for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
    6380                 :             :     {
    6381                 :     3392589 :       tree tmpl_parm;
    6382                 :     3392589 :       tree parm;
    6383                 :             : 
    6384                 :     3392589 :       if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
    6385                 :     3392589 :           || TREE_VEC_ELT (parms, i) == error_mark_node)
    6386                 :           0 :         continue;
    6387                 :             : 
    6388                 :     3392589 :       tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
    6389                 :     3392589 :       if (error_operand_p (tmpl_parm))
    6390                 :             :         return false;
    6391                 :             : 
    6392                 :     3392580 :       parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    6393                 :             : 
    6394                 :             :       /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
    6395                 :             :          TEMPLATE_DECL.  */
    6396                 :     3392580 :       if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
    6397                 :     3392564 :           || (TREE_CODE (tmpl_parm) != TYPE_DECL
    6398                 :      337102 :               && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
    6399                 :     3392553 :           || (TREE_CODE (tmpl_parm) != PARM_DECL
    6400                 :     3056117 :               && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
    6401                 :     3056117 :                   != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
    6402                 :     6785130 :           || (TREE_CODE (tmpl_parm) == PARM_DECL
    6403                 :      336436 :               && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
    6404                 :      336436 :                   != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
    6405                 :             :         {
    6406                 :          33 :           auto_diagnostic_group d;
    6407                 :          33 :           error ("template parameter %q+#D", tmpl_parm);
    6408                 :          33 :           if (DECL_P (parm))
    6409                 :          29 :             inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm);
    6410                 :             :           else
    6411                 :           4 :             inform (input_location, "redeclared here");
    6412                 :          33 :           return false;
    6413                 :          33 :         }
    6414                 :             : 
    6415                 :             :       /* The parameters can be declared to introduce different
    6416                 :             :          constraints.  */
    6417                 :     3392547 :       tree p1 = TREE_VEC_ELT (tmpl_parms, i);
    6418                 :     3392547 :       tree p2 = TREE_VEC_ELT (parms, i);
    6419                 :     3392547 :       if (!template_parameter_constraints_equivalent_p (p1, p2))
    6420                 :             :         {
    6421                 :           2 :           auto_diagnostic_group d;
    6422                 :           2 :           error ("declaration of template parameter %q+#D with different "
    6423                 :             :                  "constraints", parm);
    6424                 :           2 :           inform (DECL_SOURCE_LOCATION (tmpl_parm),
    6425                 :             :                   "original declaration appeared here");
    6426                 :           2 :           return false;
    6427                 :           2 :         }
    6428                 :             : 
    6429                 :             :       /* Give each template template parm in this redeclaration a
    6430                 :             :          DECL_CONTEXT of the template for which they are a parameter.  */
    6431                 :     3392545 :       if (TREE_CODE (parm) == TEMPLATE_DECL)
    6432                 :             :         {
    6433                 :         655 :           gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE
    6434                 :             :                                || DECL_CONTEXT (parm) == tmpl);
    6435                 :         655 :           DECL_CONTEXT (parm) = tmpl;
    6436                 :             :         }
    6437                 :             :     }
    6438                 :             : 
    6439                 :     2014585 :   if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true))
    6440                 :             :     return false;
    6441                 :             : 
    6442                 :     2014581 :   tree ci = get_constraints (tmpl);
    6443                 :     2022533 :   tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
    6444                 :     2022533 :   tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
    6445                 :             : 
    6446                 :             :   /* Two classes with different constraints declare different entities.  */
    6447                 :     2014581 :   if (!cp_tree_equal (req1, req2))
    6448                 :             :     {
    6449                 :           4 :       auto_diagnostic_group d;
    6450                 :           4 :       error_at (input_location, "redeclaration of %q#D with different "
    6451                 :             :                                 "constraints", tmpl);
    6452                 :           4 :       inform (DECL_SOURCE_LOCATION (tmpl),
    6453                 :             :               "original declaration appeared here");
    6454                 :           4 :       return false;
    6455                 :           4 :     }
    6456                 :             : 
    6457                 :             :     return true;
    6458                 :             : }
    6459                 :             : 
    6460                 :             : /* The actual substitution part of instantiate_non_dependent_expr,
    6461                 :             :    to be used when the caller has already checked
    6462                 :             :     !instantiation_dependent_uneval_expression_p (expr)
    6463                 :             :    and cleared processing_template_decl.  */
    6464                 :             : 
    6465                 :             : tree
    6466                 :    59822466 : instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
    6467                 :             : {
    6468                 :    59822466 :   return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE);
    6469                 :             : }
    6470                 :             : 
    6471                 :             : /* Instantiate the non-dependent expression EXPR.  */
    6472                 :             : 
    6473                 :             : tree
    6474                 :   103139398 : instantiate_non_dependent_expr (tree expr,
    6475                 :             :                                 tsubst_flags_t complain /* = tf_error */)
    6476                 :             : {
    6477                 :   103139398 :   if (expr == NULL_TREE)
    6478                 :             :     return NULL_TREE;
    6479                 :             : 
    6480                 :   103139398 :   if (processing_template_decl)
    6481                 :             :     {
    6482                 :             :       /* The caller should have checked this already.  */
    6483                 :    28688511 :       gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr));
    6484                 :    28688511 :       processing_template_decl_sentinel s;
    6485                 :    28688511 :       expr = instantiate_non_dependent_expr_internal (expr, complain);
    6486                 :    28688511 :     }
    6487                 :             :   return expr;
    6488                 :             : }
    6489                 :             : 
    6490                 :             : /* Like instantiate_non_dependent_expr, but return NULL_TREE if the
    6491                 :             :    expression is dependent or non-constant.  */
    6492                 :             : 
    6493                 :             : tree
    6494                 :   103007698 : instantiate_non_dependent_or_null (tree expr)
    6495                 :             : {
    6496                 :   103007698 :   if (expr == NULL_TREE)
    6497                 :             :     return NULL_TREE;
    6498                 :    93257652 :   if (processing_template_decl)
    6499                 :             :     {
    6500                 :       58502 :       if (!is_nondependent_constant_expression (expr))
    6501                 :             :         expr = NULL_TREE;
    6502                 :             :       else
    6503                 :             :         {
    6504                 :       57708 :           processing_template_decl_sentinel s;
    6505                 :       57708 :           expr = instantiate_non_dependent_expr_internal (expr, tf_error);
    6506                 :       57708 :         }
    6507                 :             :     }
    6508                 :             :   return expr;
    6509                 :             : }
    6510                 :             : 
    6511                 :             : /* True iff T is a specialization of a variable template.  */
    6512                 :             : 
    6513                 :             : bool
    6514                 :   188006586 : variable_template_specialization_p (tree t)
    6515                 :             : {
    6516                 :   188006586 :   if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
    6517                 :             :     return false;
    6518                 :    11571504 :   tree tmpl = DECL_TI_TEMPLATE (t);
    6519                 :    11571504 :   return variable_template_p (tmpl);
    6520                 :             : }
    6521                 :             : 
    6522                 :             : /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
    6523                 :             :    template declaration, or a TYPE_DECL for an alias declaration.  */
    6524                 :             : 
    6525                 :             : bool
    6526                 :   135698609 : alias_type_or_template_p (tree t)
    6527                 :             : {
    6528                 :   135698609 :   if (t == NULL_TREE)
    6529                 :             :     return false;
    6530                 :           0 :   return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
    6531                 :   135698609 :           || (TYPE_P (t)
    6532                 :   135698609 :               && TYPE_NAME (t)
    6533                 :   135698609 :               && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
    6534                 :   266404317 :           || DECL_ALIAS_TEMPLATE_P (t));
    6535                 :             : }
    6536                 :             : 
    6537                 :             : /* If T is a specialization of an alias template, return it; otherwise return
    6538                 :             :    NULL_TREE.  If TRANSPARENT_TYPEDEFS is true, look through other aliases.  */
    6539                 :             : 
    6540                 :             : tree
    6541                 : 20332255782 : alias_template_specialization_p (const_tree t,
    6542                 :             :                                  bool transparent_typedefs)
    6543                 :             : {
    6544                 : 20332255782 :   if (!TYPE_P (t))
    6545                 :             :     return NULL_TREE;
    6546                 :             : 
    6547                 :             :   /* It's an alias template specialization if it's an alias and its
    6548                 :             :      TYPE_NAME is a specialization of a primary template.  */
    6549                 : 20332255782 :   if (typedef_variant_p (t))
    6550                 :             :     {
    6551                 :   372948281 :       if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6552                 :   319964864 :         if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
    6553                 :             :           return CONST_CAST_TREE (t);
    6554                 :   249118670 :       if (transparent_typedefs)
    6555                 :      100209 :         return alias_template_specialization_p (DECL_ORIGINAL_TYPE
    6556                 :             :                                                 (TYPE_NAME (t)),
    6557                 :      100209 :                                                 transparent_typedefs);
    6558                 :             :     }
    6559                 :             : 
    6560                 :             :   return NULL_TREE;
    6561                 :             : }
    6562                 :             : 
    6563                 :             : /* A cache of the result of complex_alias_template_p.  */
    6564                 :             : 
    6565                 :             : static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info;
    6566                 :             : 
    6567                 :             : /* Data structure for complex_alias_template_*.  */
    6568                 :             : 
    6569                 :             : struct uses_all_template_parms_data
    6570                 :             : {
    6571                 :             :   int level;
    6572                 :             :   tree *seen;
    6573                 :             : };
    6574                 :             : 
    6575                 :             : /* walk_tree callback for complex_alias_template_p.  */
    6576                 :             : 
    6577                 :             : static tree
    6578                 :    44548550 : complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_)
    6579                 :             : {
    6580                 :    44548550 :   tree t = *tp;
    6581                 :    44548550 :   auto &data = *(struct uses_all_template_parms_data*)data_;
    6582                 :             : 
    6583                 :    44548550 :   switch (TREE_CODE (t))
    6584                 :             :     {
    6585                 :     8482103 :     case TEMPLATE_TYPE_PARM:
    6586                 :     8482103 :     case TEMPLATE_PARM_INDEX:
    6587                 :     8482103 :     case TEMPLATE_TEMPLATE_PARM:
    6588                 :     8482103 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
    6589                 :     8482103 :       {
    6590                 :     8482103 :         tree idx = get_template_parm_index (t);
    6591                 :     8482103 :         if (TEMPLATE_PARM_LEVEL (idx) == data.level)
    6592                 :     6632976 :           data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node;
    6593                 :             :       }
    6594                 :             : 
    6595                 :    44548550 :     default:;
    6596                 :             :     }
    6597                 :             : 
    6598                 :    44548550 :   if (!PACK_EXPANSION_P (t))
    6599                 :             :     return 0;
    6600                 :             : 
    6601                 :             :   /* An alias template with a pack expansion that expands a pack from the
    6602                 :             :      enclosing class needs to be considered complex, to avoid confusion with
    6603                 :             :      the same pack being used as an argument to the alias's own template
    6604                 :             :      parameter (91966).  */
    6605                 :     2535080 :   for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
    6606                 :      802348 :        pack = TREE_CHAIN (pack))
    6607                 :             :     {
    6608                 :      935080 :       tree parm_pack = TREE_VALUE (pack);
    6609                 :      935080 :       if (!TEMPLATE_PARM_P (parm_pack))
    6610                 :       38583 :         continue;
    6611                 :      896497 :       int idx, level;
    6612                 :      896497 :       template_parm_level_and_index (parm_pack, &level, &idx);
    6613                 :      896497 :       if (level < data.level)
    6614                 :      132732 :         return t;
    6615                 :             : 
    6616                 :             :       /* Consider the expanded packs to be used outside the expansion...  */
    6617                 :      763765 :       data.seen[idx] = boolean_true_node;
    6618                 :             :     }
    6619                 :             : 
    6620                 :             :   /* ...but don't walk into the pattern.  Consider PR104008:
    6621                 :             : 
    6622                 :             :      template <typename T, typename... Ts>
    6623                 :             :      using IsOneOf = disjunction<is_same<T, Ts>...>;
    6624                 :             : 
    6625                 :             :      where IsOneOf seemingly uses all of its template parameters in its
    6626                 :             :      expansion (and does not expand a pack from the enclosing class), so the
    6627                 :             :      alias was not marked as complex.  However, if it is used like
    6628                 :             :      "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the
    6629                 :             :      expansion.  So only Ts is considered used by the pack expansion.  */
    6630                 :      791091 :   *walk_subtrees = false;
    6631                 :             : 
    6632                 :      791091 :   return 0;
    6633                 :             : }
    6634                 :             : 
    6635                 :             : /* An alias template is complex from a SFINAE perspective if a template-id
    6636                 :             :    using that alias can be ill-formed when the expansion is not, as with
    6637                 :             :    the void_t template.
    6638                 :             : 
    6639                 :             :    If this predicate returns true in the ordinary case, the out parameter
    6640                 :             :    SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if
    6641                 :             :    the I'th template parameter of the alias template is used in the alias.  */
    6642                 :             : 
    6643                 :             : static bool
    6644                 :    75686435 : complex_alias_template_p (const_tree tmpl, tree *seen_out)
    6645                 :             : {
    6646                 :    75686435 :   tmpl = most_general_template (tmpl);
    6647                 :    75686435 :   if (!PRIMARY_TEMPLATE_P (tmpl))
    6648                 :             :     return false;
    6649                 :             : 
    6650                 :             :   /* A renaming alias isn't complex.  */
    6651                 :    56699430 :   if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
    6652                 :             :     return false;
    6653                 :             : 
    6654                 :             :   /* Any other constrained alias is complex.  */
    6655                 :    53942174 :   if (get_constraints (tmpl))
    6656                 :             :     return true;
    6657                 :             : 
    6658                 :    48251566 :   if (!complex_alias_tmpl_info)
    6659                 :       90260 :     complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (13);
    6660                 :             : 
    6661                 :    48251566 :   if (tree *slot = complex_alias_tmpl_info->get (tmpl))
    6662                 :             :     {
    6663                 :    43678067 :       tree result = *slot;
    6664                 :    43678067 :       if (result == boolean_false_node)
    6665                 :             :         return false;
    6666                 :    11371714 :       if (result == boolean_true_node)
    6667                 :             :         return true;
    6668                 :     9856723 :       gcc_assert (TREE_CODE (result) == TREE_VEC);
    6669                 :     9856723 :       if (seen_out)
    6670                 :     9856723 :         *seen_out = result;
    6671                 :     9856723 :       return true;
    6672                 :             :     }
    6673                 :             : 
    6674                 :     4573499 :   struct uses_all_template_parms_data data;
    6675                 :     4573499 :   tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6676                 :     4573499 :   tree parms = DECL_TEMPLATE_PARMS (tmpl);
    6677                 :     4573499 :   data.level = TMPL_PARMS_DEPTH (parms);
    6678                 :     4573499 :   int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
    6679                 :     4573499 :   tree seen = make_tree_vec (len);
    6680                 :     4573499 :   data.seen = TREE_VEC_BEGIN (seen);
    6681                 :    11819172 :   for (int i = 0; i < len; ++i)
    6682                 :     7245673 :     data.seen[i] = boolean_false_node;
    6683                 :             : 
    6684                 :     4573499 :   if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data))
    6685                 :             :     {
    6686                 :      132732 :       complex_alias_tmpl_info->put (tmpl, boolean_true_node);
    6687                 :      132732 :       return true;
    6688                 :             :     }
    6689                 :             : 
    6690                 :    10711297 :   for (int i = 0; i < len; ++i)
    6691                 :     6727055 :     if (data.seen[i] != boolean_true_node)
    6692                 :             :       {
    6693                 :      456525 :         complex_alias_tmpl_info->put (tmpl, seen);
    6694                 :      456525 :         if (seen_out)
    6695                 :      456525 :           *seen_out = seen;
    6696                 :      456525 :         return true;
    6697                 :             :       }
    6698                 :             : 
    6699                 :     3984242 :   complex_alias_tmpl_info->put (tmpl, boolean_false_node);
    6700                 :     3984242 :   return false;
    6701                 :             : }
    6702                 :             : 
    6703                 :             : /* If T is a specialization of a complex alias template with a dependent
    6704                 :             :    argument for an unused template parameter, return it; otherwise return
    6705                 :             :    NULL_TREE.  If T is a typedef to such a specialization, return the
    6706                 :             :    specialization.  */
    6707                 :             : 
    6708                 :             : tree
    6709                 :  1510658027 : dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
    6710                 :             : {
    6711                 :  1510658027 :   if (t == error_mark_node)
    6712                 :             :     return NULL_TREE;
    6713                 :  1485912327 :   gcc_assert (TYPE_P (t));
    6714                 :             : 
    6715                 :  1485912327 :   if (!processing_template_decl || !typedef_variant_p (t))
    6716                 :             :     return NULL_TREE;
    6717                 :             : 
    6718                 :    88686517 :   if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
    6719                 :             :     {
    6720                 :    75686435 :       tree seen = NULL_TREE;
    6721                 :    75686435 :       if (complex_alias_template_p (TI_TEMPLATE (tinfo), &seen))
    6722                 :             :         {
    6723                 :    17651579 :           tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
    6724                 :    17651579 :           if (!seen)
    6725                 :             :             {
    6726                 :     7338331 :               if (any_dependent_template_arguments_p (args))
    6727                 :    17651579 :                 return CONST_CAST_TREE (t);
    6728                 :             :             }
    6729                 :             :           else
    6730                 :             :             {
    6731                 :    10313248 :               gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen));
    6732                 :    17478885 :               for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i)
    6733                 :    14027181 :                 if (TREE_VEC_ELT (seen, i) != boolean_true_node
    6734                 :    14027181 :                     && dependent_template_arg_p (TREE_VEC_ELT (args, i)))
    6735                 :             :                   return CONST_CAST_TREE (t);
    6736                 :             :             }
    6737                 :             : 
    6738                 :     3489174 :           return NULL_TREE;
    6739                 :             :         }
    6740                 :             :     }
    6741                 :             : 
    6742                 :    71034938 :   if (transparent_typedefs)
    6743                 :             :     {
    6744                 :    10911068 :       tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
    6745                 :    10911068 :       return dependent_alias_template_spec_p (utype, transparent_typedefs);
    6746                 :             :     }
    6747                 :             : 
    6748                 :             :   return NULL_TREE;
    6749                 :             : }
    6750                 :             : 
    6751                 :             : /* Return the number of innermost template parameters in TMPL.  */
    6752                 :             : 
    6753                 :             : static int
    6754                 :    49191506 : num_innermost_template_parms (const_tree tmpl)
    6755                 :             : {
    6756                 :    49191506 :   tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6757                 :    49191506 :   return TREE_VEC_LENGTH (parms);
    6758                 :             : }
    6759                 :             : 
    6760                 :             : /* Return either TMPL or another template that it is equivalent to under DR
    6761                 :             :    1286: An alias that just changes the name of a template is equivalent to
    6762                 :             :    the other template.  */
    6763                 :             : 
    6764                 :             : static tree
    6765                 :    72513216 : get_underlying_template (tree tmpl)
    6766                 :             : {
    6767                 :    72513216 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
    6768                 :    75272448 :   while (DECL_ALIAS_TEMPLATE_P (tmpl))
    6769                 :             :     {
    6770                 :             :       /* Determine if the alias is equivalent to an underlying template.  */
    6771                 :    60124385 :       tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
    6772                 :             :       /* The underlying type may have been ill-formed. Don't proceed.  */
    6773                 :    60124385 :       if (!orig_type)
    6774                 :             :         break;
    6775                 :    60124384 :       tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
    6776                 :    24596155 :       if (!tinfo)
    6777                 :             :         break;
    6778                 :             : 
    6779                 :    24595835 :       tree underlying = TI_TEMPLATE (tinfo);
    6780                 :    24595835 :       if (!PRIMARY_TEMPLATE_P (underlying)
    6781                 :    49191588 :           || (num_innermost_template_parms (tmpl)
    6782                 :    24595753 :               != num_innermost_template_parms (underlying)))
    6783                 :             :         break;
    6784                 :             : 
    6785                 :             :       /* Does the alias add cv-quals?  */
    6786                 :    10111912 :       if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
    6787                 :             :         break;
    6788                 :             : 
    6789                 :    10111894 :       tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
    6790                 :    10111894 :       if (!comp_template_args (TI_ARGS (tinfo), alias_args))
    6791                 :             :         break;
    6792                 :             : 
    6793                 :             :       /* Are any default template arguments equivalent?  */
    6794                 :     2759302 :       tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
    6795                 :     2759302 :       tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying));
    6796                 :     2759302 :       const int nparms = TREE_VEC_LENGTH (aparms);
    6797                 :     5753356 :       for (int i = 0; i < nparms; ++i)
    6798                 :             :         {
    6799                 :     2994096 :           tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i));
    6800                 :     2994096 :           tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i));
    6801                 :     2994096 :           if (!template_args_equal (adefarg, udefarg))
    6802                 :          42 :             goto top_break;
    6803                 :             :         }
    6804                 :             : 
    6805                 :             :       /* If TMPL adds or changes any constraints, it isn't equivalent.  I think
    6806                 :             :          it's appropriate to treat a less-constrained alias as equivalent.  */
    6807                 :     2759260 :       if (!at_least_as_constrained (underlying, tmpl))
    6808                 :             :         break;
    6809                 :             : 
    6810                 :             :       /* Alias is equivalent.  Strip it and repeat.  */
    6811                 :             :       tmpl = underlying;
    6812                 :             :     }
    6813                 :     7352959 :   top_break:;
    6814                 :             : 
    6815                 :    72513216 :   return tmpl;
    6816                 :             : }
    6817                 :             : 
    6818                 :             : /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
    6819                 :             :    must be a reference-to-function or a pointer-to-function type, as specified
    6820                 :             :    in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
    6821                 :             :    and check that the resulting function has external linkage.  */
    6822                 :             : 
    6823                 :             : static tree
    6824                 :         858 : convert_nontype_argument_function (tree type, tree expr,
    6825                 :             :                                    tsubst_flags_t complain)
    6826                 :             : {
    6827                 :         858 :   tree fns = expr;
    6828                 :         858 :   tree fn, fn_no_ptr;
    6829                 :         858 :   linkage_kind linkage;
    6830                 :             : 
    6831                 :         858 :   fn = instantiate_type (type, fns, tf_none);
    6832                 :         858 :   if (fn == error_mark_node)
    6833                 :             :     return error_mark_node;
    6834                 :             : 
    6835                 :         848 :   if (value_dependent_expression_p (fn))
    6836                 :          20 :     goto accept;
    6837                 :             : 
    6838                 :         828 :   fn_no_ptr = fn;
    6839                 :         828 :   if (REFERENCE_REF_P (fn_no_ptr))
    6840                 :          14 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    6841                 :         828 :   fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
    6842                 :         828 :   if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
    6843                 :         717 :     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
    6844                 :         828 :   if (BASELINK_P (fn_no_ptr))
    6845                 :           0 :     fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
    6846                 :             : 
    6847                 :             :   /* [temp.arg.nontype]/1
    6848                 :             : 
    6849                 :             :      A template-argument for a non-type, non-template template-parameter
    6850                 :             :      shall be one of:
    6851                 :             :      [...]
    6852                 :             :      -- the address of an object or function with external [C++11: or
    6853                 :             :         internal] linkage.  */
    6854                 :             : 
    6855                 :         828 :   STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
    6856                 :         828 :   if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
    6857                 :             :     {
    6858                 :          36 :       if (complain & tf_error)
    6859                 :             :         {
    6860                 :          21 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    6861                 :          21 :           error_at (loc, "%qE is not a valid template argument for type %qT",
    6862                 :             :                     expr, type);
    6863                 :          21 :           if (TYPE_PTR_P (type))
    6864                 :          15 :             inform (loc, "it must be the address of a function "
    6865                 :             :                     "with external linkage");
    6866                 :             :           else
    6867                 :           6 :             inform (loc, "it must be the name of a function with "
    6868                 :             :                     "external linkage");
    6869                 :             :         }
    6870                 :          36 :       return NULL_TREE;
    6871                 :             :     }
    6872                 :             : 
    6873                 :         792 :   linkage = decl_linkage (fn_no_ptr);
    6874                 :         792 :   if ((cxx_dialect < cxx11 && linkage != lk_external)
    6875                 :         787 :       || (cxx_dialect < cxx17 && linkage == lk_none))
    6876                 :             :     {
    6877                 :           7 :       if (complain & tf_error)
    6878                 :             :         {
    6879                 :           3 :           location_t loc = cp_expr_loc_or_input_loc (expr);
    6880                 :           3 :           if (cxx_dialect >= cxx11)
    6881                 :           1 :             error_at (loc, "%qE is not a valid template argument for type "
    6882                 :             :                       "%qT because %qD has no linkage",
    6883                 :             :                       expr, type, fn_no_ptr);
    6884                 :             :           else
    6885                 :           2 :             error_at (loc, "%qE is not a valid template argument for type "
    6886                 :             :                       "%qT because %qD does not have external linkage",
    6887                 :             :                       expr, type, fn_no_ptr);
    6888                 :             :         }
    6889                 :           7 :       return NULL_TREE;
    6890                 :             :     }
    6891                 :             : 
    6892                 :         785 :  accept:
    6893                 :         805 :   if (TYPE_REF_P (type))
    6894                 :             :     {
    6895                 :          85 :       if (REFERENCE_REF_P (fn))
    6896                 :          10 :         fn = TREE_OPERAND (fn, 0);
    6897                 :             :       else
    6898                 :          75 :         fn = build_address (fn);
    6899                 :             :     }
    6900                 :         805 :   if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
    6901                 :          75 :     fn = build_nop (type, fn);
    6902                 :             : 
    6903                 :             :   return fn;
    6904                 :             : }
    6905                 :             : 
    6906                 :             : /* Subroutine of convert_nontype_argument.
    6907                 :             :    Check if EXPR of type TYPE is a valid pointer-to-member constant.
    6908                 :             :    Emit an error otherwise.  */
    6909                 :             : 
    6910                 :             : static bool
    6911                 :         989 : check_valid_ptrmem_cst_expr (tree type, tree expr,
    6912                 :             :                              tsubst_flags_t complain)
    6913                 :             : {
    6914                 :         989 :   tree orig_expr = expr;
    6915                 :         989 :   STRIP_NOPS (expr);
    6916                 :         989 :   if (null_ptr_cst_p (expr))
    6917                 :             :     return true;
    6918                 :         989 :   if (TREE_CODE (expr) == PTRMEM_CST
    6919                 :         989 :       && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
    6920                 :             :                       PTRMEM_CST_CLASS (expr)))
    6921                 :             :     return true;
    6922                 :         100 :   if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
    6923                 :             :     return true;
    6924                 :          55 :   if (processing_template_decl
    6925                 :           0 :       && TREE_CODE (expr) == ADDR_EXPR
    6926                 :          55 :       && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
    6927                 :             :     return true;
    6928                 :          55 :   if (complain & tf_error)
    6929                 :             :     {
    6930                 :          28 :       location_t loc = cp_expr_loc_or_input_loc (orig_expr);
    6931                 :          28 :       error_at (loc, "%qE is not a valid template argument for type %qT",
    6932                 :             :                 orig_expr, type);
    6933                 :          28 :       if (TREE_CODE (expr) != PTRMEM_CST)
    6934                 :          14 :         inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
    6935                 :             :       else
    6936                 :          14 :         inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
    6937                 :             :     }
    6938                 :             :   return false;
    6939                 :             : }
    6940                 :             : 
    6941                 :             : /* Returns TRUE iff the address of OP is value-dependent.
    6942                 :             : 
    6943                 :             :    14.6.2.4 [temp.dep.temp]:
    6944                 :             :    A non-integral non-type template-argument is dependent if its type is
    6945                 :             :    dependent or it has either of the following forms
    6946                 :             :      qualified-id
    6947                 :             :      & qualified-id
    6948                 :             :    and contains a nested-name-specifier which specifies a class-name that
    6949                 :             :    names a dependent type.
    6950                 :             : 
    6951                 :             :    We generalize this to just say that the address of a member of a
    6952                 :             :    dependent class is value-dependent; the above doesn't cover the
    6953                 :             :    address of a static data member named with an unqualified-id.  */
    6954                 :             : 
    6955                 :             : static bool
    6956                 :       79658 : has_value_dependent_address (tree op)
    6957                 :             : {
    6958                 :       79658 :   STRIP_ANY_LOCATION_WRAPPER (op);
    6959                 :             : 
    6960                 :             :   /* We could use get_inner_reference here, but there's no need;
    6961                 :             :      this is only relevant for template non-type arguments, which
    6962                 :             :      can only be expressed as &id-expression.  */
    6963                 :       79658 :   if (DECL_P (op))
    6964                 :             :     {
    6965                 :       58795 :       tree ctx = CP_DECL_CONTEXT (op);
    6966                 :             : 
    6967                 :       58795 :       if (TYPE_P (ctx) && dependent_type_p (ctx))
    6968                 :             :         return true;
    6969                 :             : 
    6970                 :       58671 :       if (VAR_P (op)
    6971                 :       58671 :           && TREE_STATIC (op)
    6972                 :        2889 :           && TREE_CODE (ctx) == FUNCTION_DECL
    6973                 :       58859 :           && type_dependent_expression_p (ctx))
    6974                 :             :         return true;
    6975                 :             :     }
    6976                 :             : 
    6977                 :             :   return false;
    6978                 :             : }
    6979                 :             : 
    6980                 :             : /* The next set of functions are used for providing helpful explanatory
    6981                 :             :    diagnostics for failed overload resolution.  Their messages should be
    6982                 :             :    indented by two spaces for consistency with the messages in
    6983                 :             :    call.cc  */
    6984                 :             : 
    6985                 :             : static int
    6986                 :           0 : unify_success (bool /*explain_p*/)
    6987                 :             : {
    6988                 :           0 :   return 0;
    6989                 :             : }
    6990                 :             : 
    6991                 :             : /* Other failure functions should call this one, to provide a single function
    6992                 :             :    for setting a breakpoint on.  */
    6993                 :             : 
    6994                 :             : static int
    6995                 :           0 : unify_invalid (bool /*explain_p*/)
    6996                 :             : {
    6997                 :           0 :   return 1;
    6998                 :             : }
    6999                 :             : 
    7000                 :             : static int
    7001                 :      164558 : unify_parameter_deduction_failure (bool explain_p, tree parm)
    7002                 :             : {
    7003                 :           0 :   if (explain_p)
    7004                 :         238 :     inform (input_location,
    7005                 :             :             "  couldn%'t deduce template parameter %qD", parm);
    7006                 :           0 :   return unify_invalid (explain_p);
    7007                 :             : }
    7008                 :             : 
    7009                 :             : static int
    7010                 :     2254683 : unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
    7011                 :             : {
    7012                 :           0 :   if (explain_p)
    7013                 :          72 :     inform (input_location,
    7014                 :             :             "  types %qT and %qT have incompatible cv-qualifiers",
    7015                 :             :             parm, arg);
    7016                 :           0 :   return unify_invalid (explain_p);
    7017                 :             : }
    7018                 :             : 
    7019                 :             : static int
    7020                 :   158120912 : unify_type_mismatch (bool explain_p, tree parm, tree arg)
    7021                 :             : {
    7022                 :       70729 :   if (explain_p)
    7023                 :         745 :     inform (input_location, "  mismatched types %qT and %qT", parm, arg);
    7024                 :       70729 :   return unify_invalid (explain_p);
    7025                 :             : }
    7026                 :             : 
    7027                 :             : static int
    7028                 :      439001 : unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
    7029                 :             : {
    7030                 :           0 :   if (explain_p)
    7031                 :           0 :     inform (input_location,
    7032                 :             :             "  template parameter %qD is not a parameter pack, but "
    7033                 :             :             "argument %qD is",
    7034                 :             :             parm, arg);
    7035                 :           0 :   return unify_invalid (explain_p);
    7036                 :             : }
    7037                 :             : 
    7038                 :             : static int
    7039                 :           3 : unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
    7040                 :             : {
    7041                 :           0 :   if (explain_p)
    7042                 :           0 :     inform (input_location,
    7043                 :             :             "  template argument %qE does not match "
    7044                 :             :             "pointer-to-member constant %qE",
    7045                 :             :             arg, parm);
    7046                 :           0 :   return unify_invalid (explain_p);
    7047                 :             : }
    7048                 :             : 
    7049                 :             : static int
    7050                 :          13 : unify_expression_unequal (bool explain_p, tree parm, tree arg)
    7051                 :             : {
    7052                 :           0 :   if (explain_p)
    7053                 :           0 :     inform (input_location, "  %qE is not equivalent to %qE", parm, arg);
    7054                 :           0 :   return unify_invalid (explain_p);
    7055                 :             : }
    7056                 :             : 
    7057                 :             : static int
    7058                 :          12 : unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
    7059                 :             : {
    7060                 :           0 :   if (explain_p)
    7061                 :           1 :     inform (input_location,
    7062                 :             :             "  inconsistent parameter pack deduction with %qT and %qT",
    7063                 :             :             old_arg, new_arg);
    7064                 :          12 :   return unify_invalid (explain_p);
    7065                 :             : }
    7066                 :             : 
    7067                 :             : static int
    7068                 :     1224346 : unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
    7069                 :             : {
    7070                 :     1224346 :   if (explain_p)
    7071                 :             :     {
    7072                 :          80 :       if (TYPE_P (parm))
    7073                 :          72 :         inform (input_location,
    7074                 :             :                 "  deduced conflicting types for parameter %qT (%qT and %qT)",
    7075                 :             :                 parm, first, second);
    7076                 :             :       else
    7077                 :           8 :         inform (input_location,
    7078                 :             :                 "  deduced conflicting values for non-type parameter "
    7079                 :             :                 "%qE (%qE and %qE)", parm, first, second);
    7080                 :             :     }
    7081                 :     1224346 :   return unify_invalid (explain_p);
    7082                 :             : }
    7083                 :             : 
    7084                 :             : static int
    7085                 :          26 : unify_vla_arg (bool explain_p, tree arg)
    7086                 :             : {
    7087                 :           0 :   if (explain_p)
    7088                 :          12 :     inform (input_location,
    7089                 :             :             "  variable-sized array type %qT is not "
    7090                 :             :             "a valid template argument",
    7091                 :             :             arg);
    7092                 :           0 :   return unify_invalid (explain_p);
    7093                 :             : }
    7094                 :             : 
    7095                 :             : static int
    7096                 :           8 : unify_method_type_error (bool explain_p, tree arg)
    7097                 :             : {
    7098                 :           0 :   if (explain_p)
    7099                 :           0 :     inform (input_location,
    7100                 :             :             "  member function type %qT is not a valid template argument",
    7101                 :             :             arg);
    7102                 :           0 :   return unify_invalid (explain_p);
    7103                 :             : }
    7104                 :             : 
    7105                 :             : static int
    7106                 :     1385626 : unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
    7107                 :             : {
    7108                 :     1385626 :   if (explain_p)
    7109                 :             :     {
    7110                 :           4 :       if (least_p)
    7111                 :           0 :         inform_n (input_location, wanted,
    7112                 :             :                   "  candidate expects at least %d argument, %d provided",
    7113                 :             :                   "  candidate expects at least %d arguments, %d provided",
    7114                 :             :                   wanted, have);
    7115                 :             :       else
    7116                 :           4 :         inform_n (input_location, wanted,
    7117                 :             :                   "  candidate expects %d argument, %d provided",
    7118                 :             :                   "  candidate expects %d arguments, %d provided",
    7119                 :             :                   wanted, have);
    7120                 :             :     }
    7121                 :     1385626 :   return unify_invalid (explain_p);
    7122                 :             : }
    7123                 :             : 
    7124                 :             : static int
    7125                 :       89108 : unify_too_many_arguments (bool explain_p, int have, int wanted)
    7126                 :             : {
    7127                 :           0 :   return unify_arity (explain_p, have, wanted);
    7128                 :             : }
    7129                 :             : 
    7130                 :             : static int
    7131                 :       44665 : unify_too_few_arguments (bool explain_p, int have, int wanted,
    7132                 :             :                          bool least_p = false)
    7133                 :             : {
    7134                 :           0 :   return unify_arity (explain_p, have, wanted, least_p);
    7135                 :             : }
    7136                 :             : 
    7137                 :             : static int
    7138                 :     1920972 : unify_arg_conversion (bool explain_p, tree to_type,
    7139                 :             :                       tree from_type, tree arg)
    7140                 :             : {
    7141                 :     1920972 :   if (explain_p)
    7142                 :         231 :     inform (cp_expr_loc_or_input_loc (arg),
    7143                 :             :             "  cannot convert %qE (type %qT) to type %qT",
    7144                 :             :             arg, from_type, to_type);
    7145                 :     1920972 :   return unify_invalid (explain_p);
    7146                 :             : }
    7147                 :             : 
    7148                 :             : static int
    7149                 :    67843437 : unify_no_common_base (bool explain_p, enum template_base_result r,
    7150                 :             :                       tree parm, tree arg)
    7151                 :             : {
    7152                 :    67843437 :   if (explain_p)
    7153                 :         839 :     switch (r)
    7154                 :             :       {
    7155                 :           4 :       case tbr_ambiguous_baseclass:
    7156                 :           4 :         inform (input_location, "  %qT is an ambiguous base class of %qT",
    7157                 :             :                 parm, arg);
    7158                 :           4 :         break;
    7159                 :         835 :       default:
    7160                 :         835 :         inform (input_location, "  %qT is not derived from %qT", arg, parm);
    7161                 :         835 :         break;
    7162                 :             :       }
    7163                 :    67843437 :   return unify_invalid (explain_p);
    7164                 :             : }
    7165                 :             : 
    7166                 :             : static int
    7167                 :          32 : unify_inconsistent_template_template_parameters (bool explain_p)
    7168                 :             : {
    7169                 :          32 :   if (explain_p)
    7170                 :          13 :     inform (input_location,
    7171                 :             :             "  template parameters of a template template argument are "
    7172                 :             :             "inconsistent with other deduced template arguments");
    7173                 :          32 :   return unify_invalid (explain_p);
    7174                 :             : }
    7175                 :             : 
    7176                 :             : static int
    7177                 :        1087 : unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
    7178                 :             : {
    7179                 :           0 :   if (explain_p)
    7180                 :           0 :     inform (input_location,
    7181                 :             :             "  cannot deduce a template for %qT from non-template type %qT",
    7182                 :             :             parm, arg);
    7183                 :           0 :   return unify_invalid (explain_p);
    7184                 :             : }
    7185                 :             : 
    7186                 :             : static int
    7187                 :     4065434 : unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
    7188                 :             : {
    7189                 :           0 :   if (explain_p)
    7190                 :           6 :     inform (input_location,
    7191                 :             :             "  template argument %qE does not match %qE", arg, parm);
    7192                 :           0 :   return unify_invalid (explain_p);
    7193                 :             : }
    7194                 :             : 
    7195                 :             : /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
    7196                 :             :    argument for TYPE, points to an unsuitable object.
    7197                 :             : 
    7198                 :             :    Also adjust the type of the index in C++20 array subobject references.  */
    7199                 :             : 
    7200                 :             : static bool
    7201                 :        3787 : invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
    7202                 :             : {
    7203                 :        3794 :   switch (TREE_CODE (expr))
    7204                 :             :     {
    7205                 :           7 :     CASE_CONVERT:
    7206                 :           7 :       return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
    7207                 :           7 :                                        complain);
    7208                 :             : 
    7209                 :           0 :     case TARGET_EXPR:
    7210                 :           0 :       return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
    7211                 :           0 :                                        complain);
    7212                 :             : 
    7213                 :        1394 :     case CONSTRUCTOR:
    7214                 :        1394 :       {
    7215                 :        6085 :         for (auto &e: CONSTRUCTOR_ELTS (expr))
    7216                 :        2377 :           if (invalid_tparm_referent_p (TREE_TYPE (e.value), e.value, complain))
    7217                 :             :             return true;
    7218                 :             :       }
    7219                 :             :       break;
    7220                 :             : 
    7221                 :         771 :     case ADDR_EXPR:
    7222                 :         771 :       {
    7223                 :         771 :         tree decl = TREE_OPERAND (expr, 0);
    7224                 :             : 
    7225                 :         771 :         if (cxx_dialect >= cxx20)
    7226                 :         289 :           while (TREE_CODE (decl) == COMPONENT_REF
    7227                 :         289 :                  || TREE_CODE (decl) == ARRAY_REF)
    7228                 :             :             {
    7229                 :          31 :               tree &op = TREE_OPERAND (decl, 1);
    7230                 :          31 :               if (TREE_CODE (decl) == ARRAY_REF
    7231                 :           8 :                   && TREE_CODE (op) == INTEGER_CST)
    7232                 :             :                 /* Canonicalize array offsets to ptrdiff_t; how they were
    7233                 :             :                    written doesn't matter for subobject identity.  */
    7234                 :           8 :                 op = fold_convert (ptrdiff_type_node, op);
    7235                 :          31 :               decl = TREE_OPERAND (decl, 0);
    7236                 :             :             }
    7237                 :             : 
    7238                 :         771 :         if (!VAR_OR_FUNCTION_DECL_P (decl))
    7239                 :             :           {
    7240                 :          17 :             if (complain & tf_error)
    7241                 :          19 :               error_at (cp_expr_loc_or_input_loc (expr),
    7242                 :             :                         "%qE is not a valid template argument of type %qT "
    7243                 :             :                         "because %qE is not a variable or function",
    7244                 :             :                         expr, type, decl);
    7245                 :          17 :             return true;
    7246                 :             :           }
    7247                 :         754 :         else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
    7248                 :             :           {
    7249                 :           2 :             if (complain & tf_error)
    7250                 :           3 :               error_at (cp_expr_loc_or_input_loc (expr),
    7251                 :             :                         "%qE is not a valid template argument of type %qT "
    7252                 :             :                         "in C++98 because %qD does not have external linkage",
    7253                 :             :                         expr, type, decl);
    7254                 :           2 :             return true;
    7255                 :             :           }
    7256                 :         752 :         else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
    7257                 :         752 :                  && decl_linkage (decl) == lk_none)
    7258                 :             :           {
    7259                 :           3 :             if (complain & tf_error)
    7260                 :           6 :               error_at (cp_expr_loc_or_input_loc (expr),
    7261                 :             :                         "%qE is not a valid template argument of type %qT "
    7262                 :             :                         "because %qD has no linkage", expr, type, decl);
    7263                 :           3 :             return true;
    7264                 :             :           }
    7265                 :             :         /* C++17: For a non-type template-parameter of reference or pointer
    7266                 :             :            type, the value of the constant expression shall not refer to (or
    7267                 :             :            for a pointer type, shall not be the address of):
    7268                 :             :            * a subobject (4.5),
    7269                 :             :            * a temporary object (15.2),
    7270                 :             :            * a string literal (5.13.5),
    7271                 :             :            * the result of a typeid expression (8.2.8), or
    7272                 :             :            * a predefined __func__ variable (11.4.1).  */
    7273                 :         746 :         else if (VAR_P (decl) && DECL_ARTIFICIAL (decl)
    7274                 :         756 :                  && !DECL_NTTP_OBJECT_P (decl))
    7275                 :             :           {
    7276                 :           3 :             gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl));
    7277                 :           3 :             if (complain & tf_error)
    7278                 :           3 :               error ("the address of %qD is not a valid template argument",
    7279                 :             :                      decl);
    7280                 :           3 :             return true;
    7281                 :             :           }
    7282                 :         746 :         else if (cxx_dialect < cxx20
    7283                 :        1241 :                  && !(same_type_ignoring_top_level_qualifiers_p
    7284                 :         495 :                       (strip_array_types (TREE_TYPE (type)),
    7285                 :         495 :                        strip_array_types (TREE_TYPE (decl)))))
    7286                 :             :           {
    7287                 :           6 :             if (complain & tf_error)
    7288                 :           4 :               error ("the address of the %qT subobject of %qD is not a "
    7289                 :           4 :                      "valid template argument", TREE_TYPE (type), decl);
    7290                 :           6 :             return true;
    7291                 :             :           }
    7292                 :         740 :         else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
    7293                 :             :           {
    7294                 :           2 :             if (complain & tf_error)
    7295                 :           2 :               error ("the address of %qD is not a valid template argument "
    7296                 :             :                      "because it does not have static storage duration",
    7297                 :             :                      decl);
    7298                 :           2 :             return true;
    7299                 :             :           }
    7300                 :             :       }
    7301                 :             :       break;
    7302                 :             : 
    7303                 :        1622 :     default:
    7304                 :        1622 :       if (!INDIRECT_TYPE_P (type))
    7305                 :             :         /* We're only concerned about pointers and references here.  */;
    7306                 :          73 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7307                 :             :         /* Null pointer values are OK in C++11.  */;
    7308                 :             :       else
    7309                 :             :         {
    7310                 :          12 :           if (VAR_P (expr))
    7311                 :             :             {
    7312                 :           8 :               if (complain & tf_error)
    7313                 :           8 :                 error ("%qD is not a valid template argument "
    7314                 :             :                        "because %qD is a variable, not the address of "
    7315                 :             :                        "a variable", expr, expr);
    7316                 :           8 :               return true;
    7317                 :             :             }
    7318                 :             :           else
    7319                 :             :             {
    7320                 :           4 :               if (complain & tf_error)
    7321                 :           1 :                 error ("%qE is not a valid template argument for %qT "
    7322                 :             :                        "because it is not the address of a variable",
    7323                 :             :                        expr, type);
    7324                 :           4 :               return true;
    7325                 :             :             }
    7326                 :             :         }
    7327                 :             :     }
    7328                 :             :   return false;
    7329                 :             : 
    7330                 :             : }
    7331                 :             : 
    7332                 :             : /* Return a VAR_DECL for the C++20 template parameter object corresponding to
    7333                 :             :    template argument EXPR.  */
    7334                 :             : 
    7335                 :             : static tree
    7336                 :         635 : create_template_parm_object (tree expr, tsubst_flags_t complain)
    7337                 :             : {
    7338                 :         635 :   tree orig = expr;
    7339                 :         635 :   if (TREE_CODE (expr) == TARGET_EXPR)
    7340                 :         635 :     expr = TARGET_EXPR_INITIAL (expr);
    7341                 :             : 
    7342                 :         635 :   if (!TREE_CONSTANT (expr))
    7343                 :             :     {
    7344                 :           3 :       if ((complain & tf_error)
    7345                 :           3 :           && require_rvalue_constant_expression (orig))
    7346                 :           3 :         cxx_constant_value (orig);
    7347                 :           3 :       return error_mark_node;
    7348                 :             :     }
    7349                 :         632 :   if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
    7350                 :           0 :     return error_mark_node;
    7351                 :             : 
    7352                 :             :   /* This is no longer a compound literal.  */
    7353                 :         632 :   gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
    7354                 :             : 
    7355                 :         632 :   return get_template_parm_object (expr, mangle_template_parm_object (expr));
    7356                 :             : }
    7357                 :             : 
    7358                 :             : /* The template arguments corresponding to template parameter objects of types
    7359                 :             :    that contain pointers to members.  */
    7360                 :             : 
    7361                 :             : static GTY(()) hash_map<tree, tree> *tparm_obj_values;
    7362                 :             : 
    7363                 :             : /* Find or build an nttp object for (already-validated) EXPR with name
    7364                 :             :    NAME.  */
    7365                 :             : 
    7366                 :             : tree
    7367                 :         634 : get_template_parm_object (tree expr, tree name)
    7368                 :             : {
    7369                 :         634 :   tree decl = get_global_binding (name);
    7370                 :         634 :   if (decl)
    7371                 :             :     return decl;
    7372                 :             : 
    7373                 :         203 :   tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
    7374                 :         203 :   decl = create_temporary_var (type);
    7375                 :         203 :   DECL_NTTP_OBJECT_P (decl) = true;
    7376                 :         203 :   DECL_CONTEXT (decl) = NULL_TREE;
    7377                 :         203 :   TREE_STATIC (decl) = true;
    7378                 :         203 :   DECL_DECLARED_CONSTEXPR_P (decl) = true;
    7379                 :         203 :   TREE_READONLY (decl) = true;
    7380                 :         203 :   DECL_NAME (decl) = name;
    7381                 :         203 :   SET_DECL_ASSEMBLER_NAME (decl, name);
    7382                 :         203 :   comdat_linkage (decl);
    7383                 :             : 
    7384                 :         203 :   if (!zero_init_p (type))
    7385                 :             :     {
    7386                 :             :       /* If EXPR contains any PTRMEM_CST, they will get clobbered by
    7387                 :             :          lower_var_init before we're done mangling.  So store the original
    7388                 :             :          value elsewhere.  */
    7389                 :          14 :       tree copy = unshare_constructor (expr);
    7390                 :          14 :       hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
    7391                 :             :     }
    7392                 :             : 
    7393                 :         203 :   pushdecl_top_level_and_finish (decl, expr);
    7394                 :             : 
    7395                 :         203 :   return decl;
    7396                 :             : }
    7397                 :             : 
    7398                 :             : /* Return the actual template argument corresponding to template parameter
    7399                 :             :    object VAR.  */
    7400                 :             : 
    7401                 :             : tree
    7402                 :        4817 : tparm_object_argument (tree var)
    7403                 :             : {
    7404                 :        4817 :   if (zero_init_p (TREE_TYPE (var)))
    7405                 :        4802 :     return DECL_INITIAL (var);
    7406                 :          15 :   return *(tparm_obj_values->get (var));
    7407                 :             : }
    7408                 :             : 
    7409                 :             : /* Attempt to convert the non-type template parameter EXPR to the
    7410                 :             :    indicated TYPE.  If the conversion is successful, return the
    7411                 :             :    converted value.  If the conversion is unsuccessful, return
    7412                 :             :    NULL_TREE if we issued an error message, or error_mark_node if we
    7413                 :             :    did not.  We issue error messages for out-and-out bad template
    7414                 :             :    parameters, but not simply because the conversion failed, since we
    7415                 :             :    might be just trying to do argument deduction.  Both TYPE and EXPR
    7416                 :             :    must be non-dependent.
    7417                 :             : 
    7418                 :             :    The conversion follows the special rules described in
    7419                 :             :    [temp.arg.nontype], and it is much more strict than an implicit
    7420                 :             :    conversion.
    7421                 :             : 
    7422                 :             :    This function is called twice for each template argument (see
    7423                 :             :    lookup_template_class for a more accurate description of this
    7424                 :             :    problem). This means that we need to handle expressions which
    7425                 :             :    are not valid in a C++ source, but can be created from the
    7426                 :             :    first call (for instance, casts to perform conversions). These
    7427                 :             :    hacks can go away after we fix the double coercion problem.  */
    7428                 :             : 
    7429                 :             : static tree
    7430                 :   126704777 : convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
    7431                 :             : {
    7432                 :   126704777 :   tree expr_type;
    7433                 :   126704777 :   location_t loc = cp_expr_loc_or_input_loc (expr);
    7434                 :             : 
    7435                 :             :   /* Detect immediately string literals as invalid non-type argument.
    7436                 :             :      This special-case is not needed for correctness (we would easily
    7437                 :             :      catch this later), but only to provide better diagnostic for this
    7438                 :             :      common user mistake. As suggested by DR 100, we do not mention
    7439                 :             :      linkage issues in the diagnostic as this is not the point.  */
    7440                 :   126704777 :   if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
    7441                 :             :     {
    7442                 :           6 :       if (complain & tf_error)
    7443                 :           6 :         error ("%qE is not a valid template argument for type %qT "
    7444                 :             :                "because string literals can never be used in this context",
    7445                 :             :                expr, type);
    7446                 :           6 :       return NULL_TREE;
    7447                 :             :     }
    7448                 :             : 
    7449                 :             :   /* Add the ADDR_EXPR now for the benefit of
    7450                 :             :      value_dependent_expression_p.  */
    7451                 :        1502 :   if (TYPE_PTROBV_P (type)
    7452                 :   126705476 :       && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
    7453                 :             :     {
    7454                 :         112 :       expr = decay_conversion (expr, complain);
    7455                 :         112 :       if (expr == error_mark_node)
    7456                 :             :         return error_mark_node;
    7457                 :             :     }
    7458                 :             : 
    7459                 :             :   /* If we are in a template, EXPR may be non-dependent, but still
    7460                 :             :      have a syntactic, rather than semantic, form.  For example, EXPR
    7461                 :             :      might be a SCOPE_REF, rather than the VAR_DECL to which the
    7462                 :             :      SCOPE_REF refers.  Preserving the qualifying scope is necessary
    7463                 :             :      so that access checking can be performed when the template is
    7464                 :             :      instantiated -- but here we need the resolved form so that we can
    7465                 :             :      convert the argument.  */
    7466                 :   126704771 :   bool non_dep = false;
    7467                 :         442 :   if (TYPE_REF_OBJ_P (type)
    7468                 :   126705108 :       && has_value_dependent_address (expr))
    7469                 :             :     /* If we want the address and it's value-dependent, don't fold.  */;
    7470                 :   126704750 :   else if (processing_template_decl
    7471                 :   126704750 :            && !instantiation_dependent_expression_p (expr))
    7472                 :             :     non_dep = true;
    7473                 :   126704771 :   if (error_operand_p (expr))
    7474                 :         645 :     return error_mark_node;
    7475                 :   126704126 :   expr_type = TREE_TYPE (expr);
    7476                 :             : 
    7477                 :             :   /* If the argument is non-dependent, perform any conversions in
    7478                 :             :      non-dependent context as well.  */
    7479                 :   126704126 :   processing_template_decl_sentinel s (non_dep);
    7480                 :    19578084 :   if (non_dep)
    7481                 :    19578084 :     expr = instantiate_non_dependent_expr_internal (expr, complain);
    7482                 :             : 
    7483                 :   126704126 :   bool val_dep_p = value_dependent_expression_p (expr);
    7484                 :   126704126 :   if (val_dep_p)
    7485                 :    21379455 :     expr = canonicalize_expr_argument (expr, complain);
    7486                 :             :   else
    7487                 :   105324671 :     STRIP_ANY_LOCATION_WRAPPER (expr);
    7488                 :             : 
    7489                 :             :   /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
    7490                 :             :      to a non-type argument of "nullptr".  */
    7491                 :   126704126 :   if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
    7492                 :          71 :     expr = fold_simple (convert (type, expr));
    7493                 :             : 
    7494                 :             :   /* In C++11, integral or enumeration non-type template arguments can be
    7495                 :             :      arbitrary constant expressions.  Pointer and pointer to
    7496                 :             :      member arguments can be general constant expressions that evaluate
    7497                 :             :      to a null value, but otherwise still need to be of a specific form.  */
    7498                 :   126704126 :   if (cxx_dialect >= cxx11)
    7499                 :             :     {
    7500                 :   126665820 :       if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
    7501                 :             :         /* A PTRMEM_CST is already constant, and a valid template
    7502                 :             :            argument for a parameter of pointer to member type, we just want
    7503                 :             :            to leave it in that form rather than lower it to a
    7504                 :             :            CONSTRUCTOR.  */;
    7505                 :   126665151 :       else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7506                 :        2639 :                || cxx_dialect >= cxx17)
    7507                 :             :         {
    7508                 :             :           /* C++17: A template-argument for a non-type template-parameter shall
    7509                 :             :              be a converted constant expression (8.20) of the type of the
    7510                 :             :              template-parameter.  */
    7511                 :   126664525 :           expr = build_converted_constant_expr (type, expr, complain);
    7512                 :   126664525 :           if (expr == error_mark_node)
    7513                 :             :             /* Make sure we return NULL_TREE only if we have really issued
    7514                 :             :                an error, as described above.  */
    7515                 :         235 :             return (complain & tf_error) ? NULL_TREE : error_mark_node;
    7516                 :   126664345 :           else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
    7517                 :             :             {
    7518                 :     3674520 :               IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
    7519                 :     3674520 :               return expr;
    7520                 :             :             }
    7521                 :   122989825 :           expr = maybe_constant_value (expr, NULL_TREE, mce_true);
    7522                 :   122989825 :           expr = convert_from_reference (expr);
    7523                 :             :           /* EXPR may have become value-dependent.  */
    7524                 :   122989825 :           val_dep_p = value_dependent_expression_p (expr);
    7525                 :             :         }
    7526                 :         626 :       else if (TYPE_PTR_OR_PTRMEM_P (type))
    7527                 :             :         {
    7528                 :         518 :           tree folded = maybe_constant_value (expr, NULL_TREE, mce_true);
    7529                 :         599 :           if (TYPE_PTR_P (type) ? integer_zerop (folded)
    7530                 :          81 :               : null_member_pointer_value_p (folded))
    7531                 :   123029426 :             expr = folded;
    7532                 :             :         }
    7533                 :             :     }
    7534                 :             : 
    7535                 :   123029426 :   if (TYPE_REF_P (type))
    7536                 :         424 :     expr = mark_lvalue_use (expr);
    7537                 :             :   else
    7538                 :   123029002 :     expr = mark_rvalue_use (expr);
    7539                 :             : 
    7540                 :             :   /* HACK: Due to double coercion, we can get a
    7541                 :             :      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
    7542                 :             :      which is the tree that we built on the first call (see
    7543                 :             :      below when coercing to reference to object or to reference to
    7544                 :             :      function). We just strip everything and get to the arg.
    7545                 :             :      See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
    7546                 :             :      for examples.  */
    7547                 :   123029426 :   if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
    7548                 :             :     {
    7549                 :             :       /* Check this before we strip *& to avoid redundancy.  */
    7550                 :         424 :       if (!mark_single_function (expr, complain))
    7551                 :           0 :         return error_mark_node;
    7552                 :             : 
    7553                 :         424 :       tree probe_type, probe = expr;
    7554                 :         424 :       if (REFERENCE_REF_P (probe))
    7555                 :         315 :         probe = TREE_OPERAND (probe, 0);
    7556                 :         424 :       probe_type = TREE_TYPE (probe);
    7557                 :         424 :       if (TREE_CODE (probe) == NOP_EXPR)
    7558                 :             :         {
    7559                 :             :           /* ??? Maybe we could use convert_from_reference here, but we
    7560                 :             :              would need to relax its constraints because the NOP_EXPR
    7561                 :             :              could actually change the type to something more cv-qualified,
    7562                 :             :              and this is not folded by convert_from_reference.  */
    7563                 :         273 :           tree addr = TREE_OPERAND (probe, 0);
    7564                 :         273 :           if (TYPE_REF_P (probe_type)
    7565                 :         273 :               && TREE_CODE (addr) == ADDR_EXPR
    7566                 :         271 :               && TYPE_PTR_P (TREE_TYPE (addr))
    7567                 :         544 :               && (same_type_ignoring_top_level_qualifiers_p
    7568                 :         271 :                   (TREE_TYPE (probe_type),
    7569                 :         271 :                    TREE_TYPE (TREE_TYPE (addr)))))
    7570                 :             :             {
    7571                 :         257 :               expr = TREE_OPERAND (addr, 0);
    7572                 :         257 :               expr_type = TREE_TYPE (probe_type);
    7573                 :             :             }
    7574                 :             :         }
    7575                 :             :     }
    7576                 :             : 
    7577                 :             :   /* [temp.arg.nontype]/5, bullet 1
    7578                 :             : 
    7579                 :             :      For a non-type template-parameter of integral or enumeration type,
    7580                 :             :      integral promotions (_conv.prom_) and integral conversions
    7581                 :             :      (_conv.integral_) are applied.  */
    7582                 :   123029426 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
    7583                 :        3645 :       || SCALAR_FLOAT_TYPE_P (type))
    7584                 :             :     {
    7585                 :   123025820 :       if (cxx_dialect < cxx11)
    7586                 :             :         {
    7587                 :       37850 :           tree t = build_converted_constant_expr (type, expr, complain);
    7588                 :       37850 :           t = maybe_constant_value (t);
    7589                 :       37850 :           if (t != error_mark_node)
    7590                 :   123025820 :             expr = t;
    7591                 :             :         }
    7592                 :             : 
    7593                 :   123025820 :       if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
    7594                 :          13 :         return error_mark_node;
    7595                 :             : 
    7596                 :             :       /* Notice that there are constant expressions like '4 % 0' which
    7597                 :             :          do not fold into integer constants.  */
    7598                 :   123025807 :       if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
    7599                 :             :         {
    7600                 :         135 :           if (complain & tf_error)
    7601                 :             :             {
    7602                 :          78 :               int errs = errorcount, warns = warningcount + werrorcount;
    7603                 :          78 :               if (!require_potential_constant_expression (expr))
    7604                 :          23 :                 expr = error_mark_node;
    7605                 :             :               else
    7606                 :          55 :                 expr = cxx_constant_value (expr);
    7607                 :          78 :               if (errorcount > errs || warningcount + werrorcount > warns)
    7608                 :          63 :                 inform (loc, "in template argument for type %qT", type);
    7609                 :          78 :               if (expr == error_mark_node)
    7610                 :             :                 return NULL_TREE;
    7611                 :             :               /* else cxx_constant_value complained but gave us
    7612                 :             :                  a real constant, so go ahead.  */
    7613                 :           4 :               if (!CONSTANT_CLASS_P (expr))
    7614                 :             :                 {
    7615                 :             :                   /* Some assemble time constant expressions like
    7616                 :             :                      (intptr_t)&&lab1 - (intptr_t)&&lab2 or
    7617                 :             :                      4 + (intptr_t)&&var satisfy reduced_constant_expression_p
    7618                 :             :                      as we can emit them into .rodata initializers of
    7619                 :             :                      variables, yet they can't fold into an INTEGER_CST at
    7620                 :             :                      compile time.  Refuse them here.  */
    7621                 :           0 :                   gcc_checking_assert (reduced_constant_expression_p (expr));
    7622                 :           0 :                   error_at (loc, "template argument %qE for type %qT not "
    7623                 :             :                                  "a compile-time constant", expr, type);
    7624                 :           0 :                   return NULL_TREE;
    7625                 :             :                 }
    7626                 :             :             }
    7627                 :             :           else
    7628                 :             :             return NULL_TREE;
    7629                 :             :         }
    7630                 :             : 
    7631                 :             :       /* Avoid typedef problems.  */
    7632                 :   123025676 :       if (TREE_TYPE (expr) != type)
    7633                 :      557497 :         expr = fold_convert (type, expr);
    7634                 :             :     }
    7635                 :             :   /* [temp.arg.nontype]/5, bullet 2
    7636                 :             : 
    7637                 :             :      For a non-type template-parameter of type pointer to object,
    7638                 :             :      qualification conversions (_conv.qual_) and the array-to-pointer
    7639                 :             :      conversion (_conv.array_) are applied.  */
    7640                 :        3606 :   else if (TYPE_PTROBV_P (type))
    7641                 :             :     {
    7642                 :         659 :       tree decayed = expr;
    7643                 :             : 
    7644                 :             :       /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
    7645                 :             :          decay_conversion or an explicit cast.  If it's a problematic cast,
    7646                 :             :          we'll complain about it below.  */
    7647                 :         659 :       if (TREE_CODE (expr) == NOP_EXPR)
    7648                 :             :         {
    7649                 :         184 :           tree probe = expr;
    7650                 :         184 :           STRIP_NOPS (probe);
    7651                 :         184 :           if (TREE_CODE (probe) == ADDR_EXPR
    7652                 :         184 :               && TYPE_PTR_P (TREE_TYPE (probe)))
    7653                 :             :             {
    7654                 :         181 :               expr = probe;
    7655                 :         181 :               expr_type = TREE_TYPE (expr);
    7656                 :             :             }
    7657                 :             :         }
    7658                 :             : 
    7659                 :             :       /* [temp.arg.nontype]/1  (TC1 version, DR 49):
    7660                 :             : 
    7661                 :             :          A template-argument for a non-type, non-template template-parameter
    7662                 :             :          shall be one of: [...]
    7663                 :             : 
    7664                 :             :          -- the name of a non-type template-parameter;
    7665                 :             :          -- the address of an object or function with external linkage, [...]
    7666                 :             :             expressed as "& id-expression" where the & is optional if the name
    7667                 :             :             refers to a function or array, or if the corresponding
    7668                 :             :             template-parameter is a reference.
    7669                 :             : 
    7670                 :             :         Here, we do not care about functions, as they are invalid anyway
    7671                 :             :         for a parameter of type pointer-to-object.  */
    7672                 :             : 
    7673                 :         659 :       if (val_dep_p)
    7674                 :             :         /* Non-type template parameters are OK.  */
    7675                 :             :         ;
    7676                 :         589 :       else if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7677                 :             :         /* Null pointer values are OK in C++11.  */;
    7678                 :         542 :       else if (TREE_CODE (expr) != ADDR_EXPR
    7679                 :          55 :                && !INDIRECT_TYPE_P (expr_type))
    7680                 :             :         /* Other values, like integer constants, might be valid
    7681                 :             :            non-type arguments of some other type.  */
    7682                 :          43 :         return error_mark_node;
    7683                 :         499 :       else if (invalid_tparm_referent_p (type, expr, complain))
    7684                 :             :         return NULL_TREE;
    7685                 :             : 
    7686                 :         572 :       expr = decayed;
    7687                 :             : 
    7688                 :         572 :       expr = perform_qualification_conversions (type, expr);
    7689                 :         572 :       if (expr == error_mark_node)
    7690                 :             :         return error_mark_node;
    7691                 :             :     }
    7692                 :             :   /* [temp.arg.nontype]/5, bullet 3
    7693                 :             : 
    7694                 :             :      For a non-type template-parameter of type reference to object, no
    7695                 :             :      conversions apply. The type referred to by the reference may be more
    7696                 :             :      cv-qualified than the (otherwise identical) type of the
    7697                 :             :      template-argument. The template-parameter is bound directly to the
    7698                 :             :      template-argument, which must be an lvalue.  */
    7699                 :        2947 :   else if (TYPE_REF_OBJ_P (type))
    7700                 :             :     {
    7701                 :         327 :       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
    7702                 :             :                                                       expr_type))
    7703                 :           0 :         return error_mark_node;
    7704                 :             : 
    7705                 :         327 :       if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
    7706                 :             :         {
    7707                 :           4 :           if (complain & tf_error)
    7708                 :           2 :             error ("%qE is not a valid template argument for type %qT "
    7709                 :             :                    "because of conflicts in cv-qualification", expr, type);
    7710                 :           4 :           return NULL_TREE;
    7711                 :             :         }
    7712                 :             : 
    7713                 :         323 :       if (!lvalue_p (expr))
    7714                 :             :         {
    7715                 :           8 :           if (complain & tf_error)
    7716                 :           8 :             error ("%qE is not a valid template argument for type %qT "
    7717                 :             :                    "because it is not an lvalue", expr, type);
    7718                 :           8 :           return NULL_TREE;
    7719                 :             :         }
    7720                 :             : 
    7721                 :             :       /* [temp.arg.nontype]/1
    7722                 :             : 
    7723                 :             :          A template-argument for a non-type, non-template template-parameter
    7724                 :             :          shall be one of: [...]
    7725                 :             : 
    7726                 :             :          -- the address of an object or function with external linkage.  */
    7727                 :         315 :       if (INDIRECT_REF_P (expr)
    7728                 :         315 :           && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
    7729                 :             :         {
    7730                 :          36 :           expr = TREE_OPERAND (expr, 0);
    7731                 :          36 :           if (DECL_P (expr))
    7732                 :             :             {
    7733                 :           4 :               if (complain & tf_error)
    7734                 :           4 :                 error ("%q#D is not a valid template argument for type %qT "
    7735                 :             :                        "because a reference variable does not have a constant "
    7736                 :             :                        "address", expr, type);
    7737                 :           4 :               return NULL_TREE;
    7738                 :             :             }
    7739                 :             :         }
    7740                 :             : 
    7741                 :         311 :       if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
    7742                 :             :         /* OK, dependent reference.  We don't want to ask whether a DECL is
    7743                 :             :            itself value-dependent, since what we want here is its address.  */;
    7744                 :             :       else
    7745                 :             :         {
    7746                 :         279 :           expr = build_address (expr);
    7747                 :             : 
    7748                 :         279 :           if (invalid_tparm_referent_p (type, expr, complain))
    7749                 :             :             return NULL_TREE;
    7750                 :             :         }
    7751                 :             : 
    7752                 :         310 :       if (!same_type_p (type, TREE_TYPE (expr)))
    7753                 :         278 :         expr = build_nop (type, expr);
    7754                 :             :     }
    7755                 :             :   /* [temp.arg.nontype]/5, bullet 4
    7756                 :             : 
    7757                 :             :      For a non-type template-parameter of type pointer to function, only
    7758                 :             :      the function-to-pointer conversion (_conv.func_) is applied. If the
    7759                 :             :      template-argument represents a set of overloaded functions (or a
    7760                 :             :      pointer to such), the matching function is selected from the set
    7761                 :             :      (_over.over_).  */
    7762                 :        2620 :   else if (TYPE_PTRFN_P (type))
    7763                 :             :     {
    7764                 :             :       /* If the argument is a template-id, we might not have enough
    7765                 :             :          context information to decay the pointer.  */
    7766                 :         779 :       if (!type_unknown_p (expr_type))
    7767                 :             :         {
    7768                 :         484 :           expr = decay_conversion (expr, complain);
    7769                 :         484 :           if (expr == error_mark_node)
    7770                 :             :             return error_mark_node;
    7771                 :             :         }
    7772                 :             : 
    7773                 :         771 :       if (cxx_dialect >= cxx11 && integer_zerop (expr))
    7774                 :             :         /* Null pointer values are OK in C++11.  */
    7775                 :          10 :         return perform_qualification_conversions (type, expr);
    7776                 :             : 
    7777                 :         761 :       expr = convert_nontype_argument_function (type, expr, complain);
    7778                 :         761 :       if (!expr || expr == error_mark_node)
    7779                 :             :         return expr;
    7780                 :             :     }
    7781                 :             :   /* [temp.arg.nontype]/5, bullet 5
    7782                 :             : 
    7783                 :             :      For a non-type template-parameter of type reference to function, no
    7784                 :             :      conversions apply. If the template-argument represents a set of
    7785                 :             :      overloaded functions, the matching function is selected from the set
    7786                 :             :      (_over.over_).  */
    7787                 :        1841 :   else if (TYPE_REFFN_P (type))
    7788                 :             :     {
    7789                 :          97 :       if (TREE_CODE (expr) == ADDR_EXPR)
    7790                 :             :         {
    7791                 :           0 :           if (complain & tf_error)
    7792                 :             :             {
    7793                 :           0 :               error ("%qE is not a valid template argument for type %qT "
    7794                 :             :                      "because it is a pointer", expr, type);
    7795                 :           0 :               inform (input_location, "try using %qE instead",
    7796                 :           0 :                       TREE_OPERAND (expr, 0));
    7797                 :             :             }
    7798                 :           0 :           return NULL_TREE;
    7799                 :             :         }
    7800                 :             : 
    7801                 :          97 :       expr = convert_nontype_argument_function (type, expr, complain);
    7802                 :          97 :       if (!expr || expr == error_mark_node)
    7803                 :             :         return expr;
    7804                 :             :     }
    7805                 :             :   /* [temp.arg.nontype]/5, bullet 6
    7806                 :             : 
    7807                 :             :      For a non-type template-parameter of type pointer to member function,
    7808                 :             :      no conversions apply. If the template-argument represents a set of
    7809                 :             :      overloaded member functions, the matching member function is selected
    7810                 :             :      from the set (_over.over_).  */
    7811                 :        1744 :   else if (TYPE_PTRMEMFUNC_P (type))
    7812                 :             :     {
    7813                 :         649 :       expr = instantiate_type (type, expr, tf_none);
    7814                 :         649 :       if (expr == error_mark_node)
    7815                 :             :         return error_mark_node;
    7816                 :             : 
    7817                 :             :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    7818                 :             :          expression must be a pointer-to-member constant.  */
    7819                 :         605 :       if (!val_dep_p
    7820                 :         605 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    7821                 :             :         return NULL_TREE;
    7822                 :             : 
    7823                 :             :       /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
    7824                 :             :          into a CONSTRUCTOR, so build up a new PTRMEM_CST instead.  */
    7825                 :         578 :       if (fnptr_conv_p (type, TREE_TYPE (expr)))
    7826                 :           2 :         expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
    7827                 :             :     }
    7828                 :             :   /* [temp.arg.nontype]/5, bullet 7
    7829                 :             : 
    7830                 :             :      For a non-type template-parameter of type pointer to data member,
    7831                 :             :      qualification conversions (_conv.qual_) are applied.  */
    7832                 :        1095 :   else if (TYPE_PTRDATAMEM_P (type))
    7833                 :             :     {
    7834                 :             :       /* [temp.arg.nontype] bullet 1 says the pointer to member
    7835                 :             :          expression must be a pointer-to-member constant.  */
    7836                 :         429 :       if (!val_dep_p
    7837                 :         429 :           && !check_valid_ptrmem_cst_expr (type, expr, complain))
    7838                 :             :         return NULL_TREE;
    7839                 :             : 
    7840                 :         401 :       expr = perform_qualification_conversions (type, expr);
    7841                 :         401 :       if (expr == error_mark_node)
    7842                 :             :         return expr;
    7843                 :             :     }
    7844                 :         666 :   else if (NULLPTR_TYPE_P (type))
    7845                 :             :     {
    7846                 :          15 :       if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
    7847                 :             :         {
    7848                 :           0 :           if (complain & tf_error)
    7849                 :           0 :             error ("%qE is not a valid template argument for type %qT "
    7850                 :           0 :                    "because it is of type %qT", expr, type, TREE_TYPE (expr));
    7851                 :           0 :           return NULL_TREE;
    7852                 :             :         }
    7853                 :             :       return expr;
    7854                 :             :     }
    7855                 :         651 :   else if (CLASS_TYPE_P (type))
    7856                 :             :     {
    7857                 :             :       /* Replace the argument with a reference to the corresponding template
    7858                 :             :          parameter object.  */
    7859                 :         651 :       if (!val_dep_p)
    7860                 :         635 :         expr = create_template_parm_object (expr, complain);
    7861                 :         651 :       if (expr == error_mark_node)
    7862                 :             :         return NULL_TREE;
    7863                 :             :     }
    7864                 :             :   /* A template non-type parameter must be one of the above.  */
    7865                 :             :   else
    7866                 :           0 :     gcc_unreachable ();
    7867                 :             : 
    7868                 :             :   /* Sanity check: did we actually convert the argument to the
    7869                 :             :      right type?  */
    7870                 :   123028977 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p
    7871                 :             :               (type, TREE_TYPE (expr)));
    7872                 :   123028977 :   return convert_from_reference (expr);
    7873                 :   126704126 : }
    7874                 :             : 
    7875                 :             : /* Subroutine of coerce_template_template_parms, which returns 1 if
    7876                 :             :    PARM_PARM and ARG_PARM match using the rule for the template
    7877                 :             :    parameters of template template parameters. Both PARM and ARG are
    7878                 :             :    template parameters; the rest of the arguments are the same as for
    7879                 :             :    coerce_template_template_parms.
    7880                 :             :  */
    7881                 :             : static int
    7882                 :      851980 : coerce_template_template_parm (tree parm,
    7883                 :             :                               tree arg,
    7884                 :             :                               tsubst_flags_t complain,
    7885                 :             :                               tree in_decl,
    7886                 :             :                               tree outer_args)
    7887                 :             : {
    7888                 :      851980 :   if (arg == NULL_TREE || error_operand_p (arg)
    7889                 :     1703952 :       || parm == NULL_TREE || error_operand_p (parm))
    7890                 :             :     return 0;
    7891                 :             : 
    7892                 :      851972 :   if (TREE_CODE (arg) != TREE_CODE (parm))
    7893                 :             :     return 0;
    7894                 :             : 
    7895                 :      851629 :   switch (TREE_CODE (parm))
    7896                 :             :     {
    7897                 :          26 :     case TEMPLATE_DECL:
    7898                 :             :       /* We encounter instantiations of templates like
    7899                 :             :          template <template <template <class> class> class TT>
    7900                 :             :          class C;  */
    7901                 :          26 :       {
    7902                 :          26 :         if (!coerce_template_template_parms
    7903                 :          26 :             (parm, arg, complain, in_decl, outer_args))
    7904                 :             :           return 0;
    7905                 :             :       }
    7906                 :             :       /* Fall through.  */
    7907                 :             : 
    7908                 :      848445 :     case TYPE_DECL:
    7909                 :      848445 :       if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
    7910                 :      848445 :           && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    7911                 :             :         /* Argument is a parameter pack but parameter is not.  */
    7912                 :             :         return 0;
    7913                 :             :       break;
    7914                 :             : 
    7915                 :        3184 :     case PARM_DECL:
    7916                 :             :       /* The tsubst call is used to handle cases such as
    7917                 :             : 
    7918                 :             :            template <int> class C {};
    7919                 :             :            template <class T, template <T> class TT> class D {};
    7920                 :             :            D<int, C> d;
    7921                 :             : 
    7922                 :             :          i.e. the parameter list of TT depends on earlier parameters.  */
    7923                 :        3184 :       if (!uses_template_parms (TREE_TYPE (arg)))
    7924                 :             :         {
    7925                 :        3145 :           tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
    7926                 :        3145 :           if (!uses_template_parms (t)
    7927                 :        3145 :               && !same_type_p (t, TREE_TYPE (arg)))
    7928                 :             :             return 0;
    7929                 :             :         }
    7930                 :             : 
    7931                 :        3152 :       if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
    7932                 :        3152 :           && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    7933                 :             :         /* Argument is a parameter pack but parameter is not.  */
    7934                 :             :         return 0;
    7935                 :             : 
    7936                 :             :       break;
    7937                 :             : 
    7938                 :           0 :     default:
    7939                 :           0 :       gcc_unreachable ();
    7940                 :             :     }
    7941                 :             : 
    7942                 :             :   return 1;
    7943                 :             : }
    7944                 :             : 
    7945                 :             : /* Coerce template argument list ARGLIST for use with template
    7946                 :             :    template-parameter TEMPL.  */
    7947                 :             : 
    7948                 :             : static tree
    7949                 :      222210 : coerce_template_args_for_ttp (tree templ, tree arglist,
    7950                 :             :                               tsubst_flags_t complain)
    7951                 :             : {
    7952                 :             :   /* Consider an example where a template template parameter declared as
    7953                 :             : 
    7954                 :             :      template <class T, class U = std::allocator<T> > class TT
    7955                 :             : 
    7956                 :             :      The template parameter level of T and U are one level larger than
    7957                 :             :      of TT.  To proper process the default argument of U, say when an
    7958                 :             :      instantiation `TT<int>' is seen, we need to build the full
    7959                 :             :      arguments containing {int} as the innermost level.  Outer levels,
    7960                 :             :      available when not appearing as default template argument, can be
    7961                 :             :      obtained from the arguments of the enclosing template.
    7962                 :             : 
    7963                 :             :      Suppose that TT is later substituted with std::vector.  The above
    7964                 :             :      instantiation is `TT<int, std::allocator<T> >' with TT at
    7965                 :             :      level 1, and T at level 2, while the template arguments at level 1
    7966                 :             :      becomes {std::vector} and the inner level 2 is {int}.  */
    7967                 :             : 
    7968                 :      222210 :   tree outer = DECL_CONTEXT (templ);
    7969                 :      222210 :   if (outer)
    7970                 :      135065 :     outer = generic_targs_for (outer);
    7971                 :       87145 :   else if (current_template_parms)
    7972                 :             :     {
    7973                 :             :       /* This is an argument of the current template, so we haven't set
    7974                 :             :          DECL_CONTEXT yet.  We can also get here when level-lowering a
    7975                 :             :          bound ttp. */
    7976                 :             :       tree relevant_template_parms;
    7977                 :             : 
    7978                 :             :       /* Parameter levels that are greater than the level of the given
    7979                 :             :          template template parm are irrelevant.  */
    7980                 :             :       relevant_template_parms = current_template_parms;
    7981                 :       83202 :       while (TMPL_PARMS_DEPTH (relevant_template_parms)
    7982                 :      166404 :              != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
    7983                 :           0 :         relevant_template_parms = TREE_CHAIN (relevant_template_parms);
    7984                 :             : 
    7985                 :       83202 :       outer = template_parms_to_args (relevant_template_parms);
    7986                 :             :     }
    7987                 :             : 
    7988                 :      222210 :   if (outer)
    7989                 :      218267 :     arglist = add_to_template_args (outer, arglist);
    7990                 :             : 
    7991                 :      222210 :   tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
    7992                 :      222210 :   return coerce_template_parms (parmlist, arglist, templ, complain);
    7993                 :             : }
    7994                 :             : 
    7995                 :             : /* A cache of template template parameters with match-all default
    7996                 :             :    arguments.  */
    7997                 :             : static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
    7998                 :             : 
    7999                 :             : /* T is a bound template template-parameter.  Copy its arguments into default
    8000                 :             :    arguments of the template template-parameter's template parameters.  */
    8001                 :             : 
    8002                 :             : static tree
    8003                 :          48 : add_defaults_to_ttp (tree otmpl)
    8004                 :             : {
    8005                 :          82 :   if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
    8006                 :          18 :     return *c;
    8007                 :             : 
    8008                 :          30 :   tree ntmpl = copy_node (otmpl);
    8009                 :             : 
    8010                 :          30 :   tree ntype = copy_node (TREE_TYPE (otmpl));
    8011                 :          30 :   TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
    8012                 :          30 :   TYPE_MAIN_VARIANT (ntype) = ntype;
    8013                 :          30 :   TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
    8014                 :          30 :   TYPE_NAME (ntype) = ntmpl;
    8015                 :          30 :   SET_TYPE_STRUCTURAL_EQUALITY (ntype);
    8016                 :             : 
    8017                 :          30 :   tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
    8018                 :          30 :     = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
    8019                 :          30 :   TEMPLATE_PARM_DECL (idx) = ntmpl;
    8020                 :          30 :   TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
    8021                 :             : 
    8022                 :          30 :   tree oparms = DECL_TEMPLATE_PARMS (otmpl);
    8023                 :          30 :   tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
    8024                 :          30 :   TREE_CHAIN (parms) = TREE_CHAIN (oparms);
    8025                 :          30 :   tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
    8026                 :          68 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
    8027                 :             :     {
    8028                 :          38 :       tree o = TREE_VEC_ELT (vec, i);
    8029                 :          38 :       if (!template_parameter_pack_p (TREE_VALUE (o)))
    8030                 :             :         {
    8031                 :          34 :           tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
    8032                 :          34 :           TREE_PURPOSE (n) = any_targ_node;
    8033                 :             :         }
    8034                 :             :     }
    8035                 :             : 
    8036                 :          30 :   tree oresult = DECL_TEMPLATE_RESULT (otmpl);
    8037                 :          30 :   tree gen_otmpl = DECL_TI_TEMPLATE (oresult);
    8038                 :          30 :   tree gen_ntmpl;
    8039                 :          30 :   if (gen_otmpl == otmpl)
    8040                 :             :     gen_ntmpl = ntmpl;
    8041                 :             :   else
    8042                 :           0 :     gen_ntmpl = add_defaults_to_ttp (gen_otmpl);
    8043                 :             : 
    8044                 :          30 :   tree nresult = copy_decl (oresult);
    8045                 :          30 :   DECL_TEMPLATE_INFO (nresult)
    8046                 :          30 :     = build_template_info (gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult)));
    8047                 :          30 :   DECL_TEMPLATE_RESULT (ntmpl) = nresult;
    8048                 :             : 
    8049                 :          30 :   hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
    8050                 :          30 :   return ntmpl;
    8051                 :             : }
    8052                 :             : 
    8053                 :             : /* ARG is a bound potential template template-argument, and PARGS is a list
    8054                 :             :    of arguments for the corresponding template template-parameter.  Adjust
    8055                 :             :    PARGS as appropriate for application to ARG's template, and if ARG is a
    8056                 :             :    BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
    8057                 :             :    arguments to the template template parameter.  */
    8058                 :             : 
    8059                 :             : static tree
    8060                 :       41588 : coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
    8061                 :             : {
    8062                 :       41588 :   ++processing_template_decl;
    8063                 :       41588 :   tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
    8064                 :       41588 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8065                 :             :     {
    8066                 :             :       /* When comparing two template template-parameters in partial ordering,
    8067                 :             :          rewrite the one currently being used as an argument to have default
    8068                 :             :          arguments for all parameters.  */
    8069                 :          48 :       arg_tmpl = add_defaults_to_ttp (arg_tmpl);
    8070                 :          48 :       pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
    8071                 :          48 :       if (pargs != error_mark_node)
    8072                 :          44 :         arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
    8073                 :          88 :                                            TYPE_TI_ARGS (arg));
    8074                 :             :     }
    8075                 :             :   else
    8076                 :             :     {
    8077                 :       41540 :       tree aparms
    8078                 :       41540 :         = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
    8079                 :       41540 :       pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain);
    8080                 :             :     }
    8081                 :       41588 :   --processing_template_decl;
    8082                 :       41588 :   return pargs;
    8083                 :             : }
    8084                 :             : 
    8085                 :             : /* Subroutine of unify for the case when PARM is a
    8086                 :             :    BOUND_TEMPLATE_TEMPLATE_PARM.  */
    8087                 :             : 
    8088                 :             : static int
    8089                 :       41877 : unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
    8090                 :             :                       bool explain_p)
    8091                 :             : {
    8092                 :       41877 :   tree parmvec = TYPE_TI_ARGS (parm);
    8093                 :       83754 :   tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
    8094                 :             : 
    8095                 :             :   /* The template template parm might be variadic and the argument
    8096                 :             :      not, so flatten both argument lists.  */
    8097                 :       41877 :   parmvec = expand_template_argument_pack (parmvec);
    8098                 :       41877 :   argvec = expand_template_argument_pack (argvec);
    8099                 :             : 
    8100                 :       41877 :   if (flag_new_ttp)
    8101                 :             :     {
    8102                 :             :       /* In keeping with P0522R0, adjust P's template arguments
    8103                 :             :          to apply to A's template; then flatten it again.  */
    8104                 :       41588 :       tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
    8105                 :       41588 :       nparmvec = expand_template_argument_pack (nparmvec);
    8106                 :             : 
    8107                 :       41588 :       if (unify (tparms, targs, nparmvec, argvec,
    8108                 :             :                  UNIFY_ALLOW_NONE, explain_p))
    8109                 :             :         return 1;
    8110                 :             : 
    8111                 :             :       /* If the P0522 adjustment eliminated a pack expansion, deduce
    8112                 :             :          empty packs.  */
    8113                 :       41532 :       if (flag_new_ttp
    8114                 :       41532 :           && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
    8115                 :       41532 :           && unify_pack_expansion (tparms, targs, parmvec, argvec,
    8116                 :             :                                    DEDUCE_EXACT, /*sub*/true, explain_p))
    8117                 :             :         return 1;
    8118                 :             :     }
    8119                 :             :   else
    8120                 :             :     {
    8121                 :             :       /* Deduce arguments T, i from TT<T> or TT<i>.
    8122                 :             :          We check each element of PARMVEC and ARGVEC individually
    8123                 :             :          rather than the whole TREE_VEC since they can have
    8124                 :             :          different number of elements, which is allowed under N2555.  */
    8125                 :             : 
    8126                 :         289 :       int len = TREE_VEC_LENGTH (parmvec);
    8127                 :             : 
    8128                 :             :       /* Check if the parameters end in a pack, making them
    8129                 :             :          variadic.  */
    8130                 :         289 :       int parm_variadic_p = 0;
    8131                 :         289 :       if (len > 0
    8132                 :         289 :           && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
    8133                 :             :         parm_variadic_p = 1;
    8134                 :             : 
    8135                 :         597 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8136                 :             :         /* If the template argument list of P contains a pack
    8137                 :             :            expansion that is not the last template argument, the
    8138                 :             :            entire template argument list is a non-deduced
    8139                 :             :            context.  */
    8140                 :         310 :         if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
    8141                 :       41795 :           return unify_success (explain_p);
    8142                 :             : 
    8143                 :         287 :       if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
    8144                 :           4 :         return unify_too_few_arguments (explain_p,
    8145                 :           4 :                                         TREE_VEC_LENGTH (argvec), len);
    8146                 :             : 
    8147                 :         563 :       for (int i = 0; i < len - parm_variadic_p; ++i)
    8148                 :         300 :         if (unify (tparms, targs,
    8149                 :         300 :                    TREE_VEC_ELT (parmvec, i),
    8150                 :         300 :                    TREE_VEC_ELT (argvec, i),
    8151                 :             :                    UNIFY_ALLOW_NONE, explain_p))
    8152                 :             :           return 1;
    8153                 :             : 
    8154                 :         263 :       if (parm_variadic_p
    8155                 :         263 :           && unify_pack_expansion (tparms, targs,
    8156                 :             :                                    parmvec, argvec,
    8157                 :             :                                    DEDUCE_EXACT,
    8158                 :             :                                    /*subr=*/true, explain_p))
    8159                 :             :         return 1;
    8160                 :             :     }
    8161                 :             : 
    8162                 :             :   return 0;
    8163                 :             : }
    8164                 :             : 
    8165                 :             : /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for
    8166                 :             :    template template parameters.
    8167                 :             : 
    8168                 :             :    Consider the example:
    8169                 :             :      template <class T> class A;
    8170                 :             :      template<template <class U> class TT> class B;
    8171                 :             : 
    8172                 :             :    For B<A>, PARM_TMPL is TT, while ARG_TMPL is A,
    8173                 :             :    and OUTER_ARGS contains A.  */
    8174                 :             : 
    8175                 :             : static int
    8176                 :     8784707 : coerce_template_template_parms (tree parm_tmpl,
    8177                 :             :                                 tree arg_tmpl,
    8178                 :             :                                 tsubst_flags_t complain,
    8179                 :             :                                 tree in_decl,
    8180                 :             :                                 tree outer_args)
    8181                 :             : {
    8182                 :     8784707 :   int nparms, nargs, i;
    8183                 :     8784707 :   tree parm, arg;
    8184                 :     8784707 :   int variadic_p = 0;
    8185                 :             : 
    8186                 :     8784707 :   tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl);
    8187                 :     8784707 :   tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl);
    8188                 :     8784707 :   tree gen_arg_tmpl = most_general_template (arg_tmpl);
    8189                 :     8784707 :   tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl);
    8190                 :             : 
    8191                 :     8784707 :   nparms = TREE_VEC_LENGTH (parm_parms);
    8192                 :     8784707 :   nargs = TREE_VEC_LENGTH (arg_parms);
    8193                 :             : 
    8194                 :     8784707 :   if (flag_new_ttp)
    8195                 :             :     {
    8196                 :             :       /* P0522R0: A template template-parameter P is at least as specialized as
    8197                 :             :          a template template-argument A if, given the following rewrite to two
    8198                 :             :          function templates, the function template corresponding to P is at
    8199                 :             :          least as specialized as the function template corresponding to A
    8200                 :             :          according to the partial ordering rules for function templates
    8201                 :             :          ([temp.func.order]). Given an invented class template X with the
    8202                 :             :          template parameter list of A (including default arguments):
    8203                 :             : 
    8204                 :             :          * Each of the two function templates has the same template parameters,
    8205                 :             :          respectively, as P or A.
    8206                 :             : 
    8207                 :             :          * Each function template has a single function parameter whose type is
    8208                 :             :          a specialization of X with template arguments corresponding to the
    8209                 :             :          template parameters from the respective function template where, for
    8210                 :             :          each template parameter PP in the template parameter list of the
    8211                 :             :          function template, a corresponding template argument AA is formed. If
    8212                 :             :          PP declares a parameter pack, then AA is the pack expansion
    8213                 :             :          PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
    8214                 :             : 
    8215                 :             :          If the rewrite produces an invalid type, then P is not at least as
    8216                 :             :          specialized as A.  */
    8217                 :             : 
    8218                 :             :       /* So coerce P's args to apply to A's parms, and then deduce between A's
    8219                 :             :          args and the converted args.  If that succeeds, A is at least as
    8220                 :             :          specialized as P, so they match.*/
    8221                 :     8742447 :       processing_template_decl_sentinel ptds (/*reset*/false);
    8222                 :     8742447 :       ++processing_template_decl;
    8223                 :             : 
    8224                 :     8742447 :       tree pargs = template_parms_level_to_args (parm_parms);
    8225                 :             : 
    8226                 :             :       /* PARM and ARG might be at different template depths, and we want to
    8227                 :             :          pass the right additional levels of args when coercing PARGS to
    8228                 :             :          ARG_PARMS in case we need to do any substitution into non-type
    8229                 :             :          template parameter types.
    8230                 :             : 
    8231                 :             :          OUTER_ARGS are not the right outer levels in this case, as they are
    8232                 :             :          the args we're building up for PARM, and for the coercion we want the
    8233                 :             :          args for ARG.  If DECL_CONTEXT isn't set for a template template
    8234                 :             :          parameter, we can assume that it's in the current scope.  */
    8235                 :     8742447 :       tree ctx = DECL_CONTEXT (arg_tmpl);
    8236                 :     8742447 :       if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8237                 :      322734 :         ctx = current_scope ();
    8238                 :     8742447 :       tree scope_args = NULL_TREE;
    8239                 :     8742447 :       if (tree tinfo = get_template_info (ctx))
    8240                 :      187984 :         scope_args = TI_ARGS (tinfo);
    8241                 :     8742447 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
    8242                 :             :         {
    8243                 :      339752 :           int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl));
    8244                 :      362440 :           int scope_depth = TMPL_ARGS_DEPTH (scope_args);
    8245                 :      339752 :           tree full_pargs = make_tree_vec (level + 1);
    8246                 :             : 
    8247                 :             :         /* Only use as many levels from the scope as needed
    8248                 :             :            (excluding the level of ARG).  */
    8249                 :      356676 :           for (int i = 0; i < level - 1; ++i)
    8250                 :       16924 :             if (i < scope_depth)
    8251                 :       32822 :               TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1);
    8252                 :             :             else
    8253                 :         513 :               TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0);
    8254                 :             : 
    8255                 :             :           /* Add the arguments that appear at the levels of ARG.  */
    8256                 :      339752 :           tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl));
    8257                 :      679504 :           adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1);
    8258                 :      339752 :           TREE_VEC_ELT (full_pargs, level - 1) = adjacent;
    8259                 :             : 
    8260                 :      339752 :           TREE_VEC_ELT (full_pargs, level) = pargs;
    8261                 :      339752 :           pargs = full_pargs;
    8262                 :             :         }
    8263                 :             :       else
    8264                 :     8402695 :         pargs = add_to_template_args (scope_args, pargs);
    8265                 :             : 
    8266                 :     8742447 :       pargs = coerce_template_parms (gen_arg_parms, pargs,
    8267                 :             :                                      NULL_TREE, tf_none);
    8268                 :     8742447 :       if (pargs != error_mark_node)
    8269                 :             :         {
    8270                 :     8741450 :           tree targs = make_tree_vec (nargs);
    8271                 :     8741450 :           tree aargs = template_parms_level_to_args (arg_parms);
    8272                 :     8741450 :           if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
    8273                 :             :                       /*explain*/false))
    8274                 :     8140037 :             return 1;
    8275                 :             :         }
    8276                 :     8742447 :     }
    8277                 :             : 
    8278                 :             :   /* Determine whether we have a parameter pack at the end of the
    8279                 :             :      template template parameter's template parameter list.  */
    8280                 :      644670 :   if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
    8281                 :             :     {
    8282                 :      644670 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
    8283                 :             : 
    8284                 :      644670 :       if (error_operand_p (parm))
    8285                 :             :         return 0;
    8286                 :             : 
    8287                 :      644663 :       switch (TREE_CODE (parm))
    8288                 :             :         {
    8289                 :      640538 :         case TEMPLATE_DECL:
    8290                 :      640538 :         case TYPE_DECL:
    8291                 :      640538 :           if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
    8292                 :      613078 :             variadic_p = 1;
    8293                 :             :           break;
    8294                 :             : 
    8295                 :        4125 :         case PARM_DECL:
    8296                 :        4125 :           if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
    8297                 :      613078 :             variadic_p = 1;
    8298                 :             :           break;
    8299                 :             : 
    8300                 :           0 :         default:
    8301                 :           0 :           gcc_unreachable ();
    8302                 :             :         }
    8303                 :             :     }
    8304                 :             : 
    8305                 :      644663 :   if (nargs != nparms
    8306                 :      165292 :       && !(variadic_p && nargs >= nparms - 1))
    8307                 :             :     return 0;
    8308                 :             : 
    8309                 :             :   /* Check all of the template parameters except the parameter pack at
    8310                 :             :      the end (if any).  */
    8311                 :      733706 :   for (i = 0; i < nparms - variadic_p; ++i)
    8312                 :             :     {
    8313                 :       90124 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
    8314                 :       90124 :           || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8315                 :           0 :         continue;
    8316                 :             : 
    8317                 :       90124 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8318                 :       90124 :       arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8319                 :             : 
    8320                 :       90124 :       if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8321                 :             :                                           outer_args))
    8322                 :             :         return 0;
    8323                 :             : 
    8324                 :             :     }
    8325                 :             : 
    8326                 :      643582 :   if (variadic_p)
    8327                 :             :     {
    8328                 :             :       /* Check each of the template parameters in the template
    8329                 :             :          argument against the template parameter pack at the end of
    8330                 :             :          the template template parameter.  */
    8331                 :      613074 :       if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
    8332                 :             :         return 0;
    8333                 :             : 
    8334                 :      613074 :       parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
    8335                 :             : 
    8336                 :     1374914 :       for (; i < nargs; ++i)
    8337                 :             :         {
    8338                 :      761856 :           if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
    8339                 :           0 :             continue;
    8340                 :             : 
    8341                 :      761856 :           arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
    8342                 :             : 
    8343                 :      761856 :           if (!coerce_template_template_parm (parm, arg, complain, in_decl,
    8344                 :             :                                               outer_args))
    8345                 :             :             return 0;
    8346                 :             :         }
    8347                 :             :     }
    8348                 :             : 
    8349                 :             :   return 1;
    8350                 :             : }
    8351                 :             : 
    8352                 :             : /* Verifies that the deduced template arguments (in TARGS) for the
    8353                 :             :    template template parameters (in TPARMS) represent valid bindings,
    8354                 :             :    by comparing the template parameter list of each template argument
    8355                 :             :    to the template parameter list of its corresponding template
    8356                 :             :    template parameter, in accordance with DR150. This
    8357                 :             :    routine can only be called after all template arguments have been
    8358                 :             :    deduced. It will return TRUE if all of the template template
    8359                 :             :    parameter bindings are okay, FALSE otherwise.  */
    8360                 :             : bool
    8361                 :    49923675 : template_template_parm_bindings_ok_p (tree tparms, tree targs)
    8362                 :             : {
    8363                 :    49923675 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
    8364                 :    49923675 :   bool ret = true;
    8365                 :             : 
    8366                 :             :   /* We're dealing with template parms in this process.  */
    8367                 :    49923675 :   ++processing_template_decl;
    8368                 :             : 
    8369                 :    49923675 :   targs = INNERMOST_TEMPLATE_ARGS (targs);
    8370                 :             : 
    8371                 :   130409624 :   for (i = 0; i < ntparms; ++i)
    8372                 :             :     {
    8373                 :    80485994 :       tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
    8374                 :    80485994 :       tree targ = TREE_VEC_ELT (targs, i);
    8375                 :             : 
    8376                 :    80485994 :       if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
    8377                 :             :         {
    8378                 :      404702 :           tree packed_args = NULL_TREE;
    8379                 :      404702 :           int idx, len = 1;
    8380                 :             : 
    8381                 :      404702 :           if (ARGUMENT_PACK_P (targ))
    8382                 :             :             {
    8383                 :             :               /* Look inside the argument pack.  */
    8384                 :         109 :               packed_args = ARGUMENT_PACK_ARGS (targ);
    8385                 :         109 :               len = TREE_VEC_LENGTH (packed_args);
    8386                 :             :             }
    8387                 :             : 
    8388                 :      809372 :           for (idx = 0; idx < len; ++idx)
    8389                 :             :             {
    8390                 :      404715 :               if (packed_args)
    8391                 :             :                 /* Extract the next argument from the argument
    8392                 :             :                    pack.  */
    8393                 :         122 :                 targ = TREE_VEC_ELT (packed_args, idx);
    8394                 :             : 
    8395                 :      404715 :               if (PACK_EXPANSION_P (targ))
    8396                 :             :                 /* Look at the pattern of the pack expansion.  */
    8397                 :          28 :                 targ = PACK_EXPANSION_PATTERN (targ);
    8398                 :             : 
    8399                 :             :               /* Extract the template parameters from the template
    8400                 :             :                  argument.  */
    8401                 :      404715 :               if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
    8402                 :       70012 :                 targ = TYPE_NAME (targ);
    8403                 :             : 
    8404                 :             :               /* Verify that we can coerce the template template
    8405                 :             :                  parameters from the template argument to the template
    8406                 :             :                  parameter.  This requires an exact match.  */
    8407                 :      404715 :               if (TREE_CODE (targ) == TEMPLATE_DECL
    8408                 :      809418 :                   && !coerce_template_template_parms
    8409                 :      404703 :                        (tparm,
    8410                 :             :                         targ,
    8411                 :             :                         tf_none,
    8412                 :             :                         tparm,
    8413                 :             :                         targs))
    8414                 :             :                 {
    8415                 :          45 :                   ret = false;
    8416                 :          45 :                   goto out;
    8417                 :             :                 }
    8418                 :             :             }
    8419                 :             :         }
    8420                 :             :     }
    8421                 :             : 
    8422                 :    49923630 :  out:
    8423                 :             : 
    8424                 :    49923675 :   --processing_template_decl;
    8425                 :    49923675 :   return ret;
    8426                 :             : }
    8427                 :             : 
    8428                 :             : /* Since type attributes aren't mangled, we need to strip them from
    8429                 :             :    template type arguments.  */
    8430                 :             : 
    8431                 :             : tree
    8432                 :  1029807093 : canonicalize_type_argument (tree arg, tsubst_flags_t complain)
    8433                 :             : {
    8434                 :  1029807093 :   if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
    8435                 :             :     return arg;
    8436                 :   442967086 :   bool removed_attributes = false;
    8437                 :   442967086 :   tree canon = strip_typedefs (arg, &removed_attributes);
    8438                 :   442967086 :   if (removed_attributes
    8439                 :         446 :       && (complain & tf_warning))
    8440                 :          20 :     warning (OPT_Wignored_attributes,
    8441                 :             :              "ignoring attributes on template argument %qT", arg);
    8442                 :             :   return canon;
    8443                 :             : }
    8444                 :             : 
    8445                 :             : /* And from inside dependent non-type arguments like sizeof(Type).  */
    8446                 :             : 
    8447                 :             : static tree
    8448                 :    44220762 : canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
    8449                 :             : {
    8450                 :    44220762 :   if (!arg || arg == error_mark_node)
    8451                 :             :     return arg;
    8452                 :    44220762 :   bool removed_attributes = false;
    8453                 :    44220762 :   tree canon = strip_typedefs_expr (arg, &removed_attributes);
    8454                 :    44220762 :   if (removed_attributes
    8455                 :           0 :       && (complain & tf_warning))
    8456                 :           0 :     warning (OPT_Wignored_attributes,
    8457                 :             :              "ignoring attributes in template argument %qE", arg);
    8458                 :             :   return canon;
    8459                 :             : }
    8460                 :             : 
    8461                 :             : /* A template declaration can be substituted for a constrained
    8462                 :             :    template template parameter only when the argument is no more
    8463                 :             :    constrained than the parameter.  */
    8464                 :             : 
    8465                 :             : static bool
    8466                 :     8378919 : is_compatible_template_arg (tree parm, tree arg, tree args)
    8467                 :             : {
    8468                 :     8378919 :   tree parm_cons = get_constraints (parm);
    8469                 :             : 
    8470                 :             :   /* For now, allow constrained template template arguments
    8471                 :             :      and unconstrained template template parameters.  */
    8472                 :     8378919 :   if (parm_cons == NULL_TREE)
    8473                 :             :     return true;
    8474                 :             : 
    8475                 :             :   /* If the template parameter is constrained, we need to rewrite its
    8476                 :             :      constraints in terms of the ARG's template parameters. This ensures
    8477                 :             :      that all of the template parameter types will have the same depth.
    8478                 :             : 
    8479                 :             :      Note that this is only valid when coerce_template_template_parm is
    8480                 :             :      true for the innermost template parameters of PARM and ARG. In other
    8481                 :             :      words, because coercion is successful, this conversion will be valid.  */
    8482                 :          25 :   tree new_args = NULL_TREE;
    8483                 :          25 :   if (parm_cons)
    8484                 :             :     {
    8485                 :          25 :       tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
    8486                 :          25 :       new_args = template_parms_level_to_args (aparms);
    8487                 :          25 :       new_args = add_to_template_args (args, new_args);
    8488                 :          25 :       ++processing_template_decl;
    8489                 :          25 :       parm_cons = tsubst_constraint_info (parm_cons, new_args,
    8490                 :             :                                           tf_none, NULL_TREE);
    8491                 :          25 :       --processing_template_decl;
    8492                 :          25 :       if (parm_cons == error_mark_node)
    8493                 :             :         return false;
    8494                 :             :     }
    8495                 :             : 
    8496                 :          25 :   return weakly_subsumes (parm_cons, arg);
    8497                 :             : }
    8498                 :             : 
    8499                 :             : // Convert a placeholder argument into a binding to the original
    8500                 :             : // parameter. The original parameter is saved as the TREE_TYPE of
    8501                 :             : // ARG.
    8502                 :             : static inline tree
    8503                 :     3961111 : convert_wildcard_argument (tree parm, tree arg)
    8504                 :             : {
    8505                 :     3961111 :   TREE_TYPE (arg) = parm;
    8506                 :     3961111 :   return arg;
    8507                 :             : }
    8508                 :             : 
    8509                 :             : /* We can't fully resolve ARG given as a non-type template argument to TYPE,
    8510                 :             :    because one of them is dependent.  But we need to represent the
    8511                 :             :    conversion for the benefit of cp_tree_equal.  */
    8512                 :             : 
    8513                 :             : static tree
    8514                 :    22841307 : maybe_convert_nontype_argument (tree type, tree arg, bool force)
    8515                 :             : {
    8516                 :             :   /* Auto parms get no conversion.  */
    8517                 :    22841307 :   if (type_uses_auto (type))
    8518                 :             :     return arg;
    8519                 :             :   /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion?
    8520                 :             :      That would complicate other things, and it doesn't seem necessary.  */
    8521                 :    22839197 :   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
    8522                 :             :     return arg;
    8523                 :             :   /* We don't need or want to add this conversion now if we're going to use the
    8524                 :             :      argument for deduction.  */
    8525                 :    21702127 :   if (!value_dependent_expression_p (arg))
    8526                 :             :     force = false;
    8527                 :    21701982 :   else if (!force)
    8528                 :             :     return arg;
    8529                 :             : 
    8530                 :     1832587 :   type = cv_unqualified (type);
    8531                 :     1832587 :   tree argtype = TREE_TYPE (arg);
    8532                 :     1832587 :   if (argtype && same_type_p (type, argtype))
    8533                 :             :     return arg;
    8534                 :             : 
    8535                 :     1832566 :   arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
    8536                 :     1832566 :   IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
    8537                 :     1832566 :   IMPLICIT_CONV_EXPR_FORCED (arg) = force;
    8538                 :     1832566 :   return arg;
    8539                 :             : }
    8540                 :             : 
    8541                 :             : /* Convert the indicated template ARG as necessary to match the
    8542                 :             :    indicated template PARM.  Returns the converted ARG, or
    8543                 :             :    error_mark_node if the conversion was unsuccessful.  Error and
    8544                 :             :    warning messages are issued under control of COMPLAIN.  This
    8545                 :             :    conversion is for the Ith parameter in the parameter list.  ARGS is
    8546                 :             :    the full set of template arguments deduced so far.  */
    8547                 :             : 
    8548                 :             : static tree
    8549                 :   996411268 : convert_template_argument (tree parm,
    8550                 :             :                            tree arg,
    8551                 :             :                            tree args,
    8552                 :             :                            tsubst_flags_t complain,
    8553                 :             :                            int i,
    8554                 :             :                            tree in_decl)
    8555                 :             : {
    8556                 :   996411268 :   tree orig_arg;
    8557                 :   996411268 :   tree val;
    8558                 :   996411268 :   int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
    8559                 :             : 
    8560                 :   996411268 :   if (parm == error_mark_node || error_operand_p (arg))
    8561                 :             :     return error_mark_node;
    8562                 :             : 
    8563                 :             :   /* Trivially convert placeholders. */
    8564                 :   984257019 :   if (TREE_CODE (arg) == WILDCARD_DECL)
    8565                 :     3961111 :     return convert_wildcard_argument (parm, arg);
    8566                 :             : 
    8567                 :   980295908 :   if (arg == any_targ_node)
    8568                 :             :     return arg;
    8569                 :             : 
    8570                 :   980295896 :   if (TREE_CODE (arg) == TREE_LIST
    8571                 :   980295896 :       && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
    8572                 :             :     {
    8573                 :             :       /* The template argument was the name of some
    8574                 :             :          member function.  That's usually
    8575                 :             :          invalid, but static members are OK.  In any
    8576                 :             :          case, grab the underlying fields/functions
    8577                 :             :          and issue an error later if required.  */
    8578                 :           0 :       TREE_TYPE (arg) = unknown_type_node;
    8579                 :             :     }
    8580                 :             : 
    8581                 :   980295896 :   orig_arg = arg;
    8582                 :             : 
    8583                 :   980295896 :   requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
    8584                 :  1960591792 :   requires_type = (TREE_CODE (parm) == TYPE_DECL
    8585                 :   980295896 :                    || requires_tmpl_type);
    8586                 :             : 
    8587                 :             :   /* When determining whether an argument pack expansion is a template,
    8588                 :             :      look at the pattern.  */
    8589                 :   980295896 :   if (PACK_EXPANSION_P (arg))
    8590                 :    16892764 :     arg = PACK_EXPANSION_PATTERN (arg);
    8591                 :             : 
    8592                 :             :   /* Deal with an injected-class-name used as a template template arg.  */
    8593                 :   980295896 :   if (requires_tmpl_type && CLASS_TYPE_P (arg))
    8594                 :             :     {
    8595                 :         240 :       tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
    8596                 :         240 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    8597                 :             :         {
    8598                 :          38 :           if (cxx_dialect >= cxx11)
    8599                 :             :             /* OK under DR 1004.  */;
    8600                 :           2 :           else if (complain & tf_warning_or_error)
    8601                 :           2 :             pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
    8602                 :           2 :                      " used as template template argument", TYPE_NAME (arg));
    8603                 :           0 :           else if (flag_pedantic_errors)
    8604                 :   980295858 :             t = arg;
    8605                 :             : 
    8606                 :             :           arg = t;
    8607                 :             :         }
    8608                 :             :     }
    8609                 :             : 
    8610                 :  1968983738 :   is_tmpl_type =
    8611                 :   980295896 :     ((TREE_CODE (arg) == TEMPLATE_DECL
    8612                 :     8247148 :       && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
    8613                 :   972048748 :      || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
    8614                 :   972048742 :      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8615                 :  1952211745 :      || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
    8616                 :             : 
    8617                 :     8391946 :   if (is_tmpl_type
    8618                 :     8391946 :       && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
    8619                 :     8259053 :           || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
    8620                 :      144792 :     arg = TYPE_STUB_DECL (arg);
    8621                 :             : 
    8622                 :   980295896 :   is_type = TYPE_P (arg) || is_tmpl_type;
    8623                 :             : 
    8624                 :     2030214 :   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
    8625                 :   980295919 :       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
    8626                 :             :     {
    8627                 :          15 :       if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
    8628                 :             :         {
    8629                 :           4 :           if (complain & tf_error)
    8630                 :           4 :             error ("invalid use of destructor %qE as a type", orig_arg);
    8631                 :           4 :           return error_mark_node;
    8632                 :             :         }
    8633                 :             : 
    8634                 :          11 :       permerror (input_location,
    8635                 :             :                  "to refer to a type member of a template parameter, "
    8636                 :             :                  "use %<typename %E%>", orig_arg);
    8637                 :             : 
    8638                 :          11 :       orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
    8639                 :          11 :                                      TREE_OPERAND (arg, 1),
    8640                 :             :                                      typename_type,
    8641                 :             :                                      complain);
    8642                 :          11 :       arg = orig_arg;
    8643                 :          11 :       is_type = 1;
    8644                 :             :     }
    8645                 :   980295892 :   if (is_type != requires_type)
    8646                 :             :     {
    8647                 :     2032303 :       if (in_decl)
    8648                 :             :         {
    8649                 :     2031894 :           if (complain & tf_error)
    8650                 :             :             {
    8651                 :         143 :               error ("type/value mismatch at argument %d in template "
    8652                 :             :                      "parameter list for %qD",
    8653                 :             :                      i + 1, in_decl);
    8654                 :         143 :               if (is_type)
    8655                 :             :                 {
    8656                 :             :                   /* The template argument is a type, but we're expecting
    8657                 :             :                      an expression.  */
    8658                 :          45 :                   inform (input_location,
    8659                 :             :                           "  expected a constant of type %qT, got %qT",
    8660                 :          34 :                           TREE_TYPE (parm),
    8661                 :          34 :                           (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
    8662                 :             :                   /* [temp.arg]/2: "In a template-argument, an ambiguity
    8663                 :             :                      between a type-id and an expression is resolved to a
    8664                 :             :                      type-id, regardless of the form of the corresponding
    8665                 :             :                      template-parameter."  So give the user a clue.  */
    8666                 :          34 :                   if (TREE_CODE (arg) == FUNCTION_TYPE)
    8667                 :           2 :                     inform (input_location, "  ambiguous template argument "
    8668                 :             :                             "for non-type template parameter is treated as "
    8669                 :             :                             "function type");
    8670                 :             :                 }
    8671                 :         109 :               else if (requires_tmpl_type)
    8672                 :          12 :                 inform (input_location,
    8673                 :             :                         "  expected a class template, got %qE", orig_arg);
    8674                 :             :               else
    8675                 :          97 :                 inform (input_location,
    8676                 :             :                         "  expected a type, got %qE", orig_arg);
    8677                 :             :             }
    8678                 :             :         }
    8679                 :     2032303 :       return error_mark_node;
    8680                 :             :     }
    8681                 :   978263589 :   if (is_tmpl_type ^ requires_tmpl_type)
    8682                 :             :     {
    8683                 :         268 :       if (in_decl && (complain & tf_error))
    8684                 :             :         {
    8685                 :          21 :           error ("type/value mismatch at argument %d in template "
    8686                 :             :                  "parameter list for %qD",
    8687                 :             :                  i + 1, in_decl);
    8688                 :          21 :           if (is_tmpl_type)
    8689                 :           8 :             inform (input_location,
    8690                 :           8 :                     "  expected a type, got %qT", DECL_NAME (arg));
    8691                 :             :           else
    8692                 :          13 :             inform (input_location,
    8693                 :             :                     "  expected a class template, got %qT", orig_arg);
    8694                 :             :         }
    8695                 :         268 :       return error_mark_node;
    8696                 :             :     }
    8697                 :             : 
    8698                 :   978263321 :   if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
    8699                 :             :     /* We already did the appropriate conversion when packing args.  */
    8700                 :             :     val = orig_arg;
    8701                 :   978263300 :   else if (is_type)
    8702                 :             :     {
    8703                 :   843137036 :       if (requires_tmpl_type)
    8704                 :             :         {
    8705                 :     8391869 :           if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
    8706                 :             :             /* The number of argument required is not known yet.
    8707                 :             :                Just accept it for now.  */
    8708                 :             :             val = orig_arg;
    8709                 :             :           else
    8710                 :             :             {
    8711                 :             :               /* Strip alias templates that are equivalent to another
    8712                 :             :                  template.  */
    8713                 :     8379978 :               arg = get_underlying_template (arg);
    8714                 :             : 
    8715                 :     8379978 :               if (coerce_template_template_parms (parm, arg,
    8716                 :             :                                                   complain, in_decl,
    8717                 :             :                                                   args))
    8718                 :             :                 {
    8719                 :     8378919 :                   val = arg;
    8720                 :             : 
    8721                 :             :                   /* TEMPLATE_TEMPLATE_PARM node is preferred over
    8722                 :             :                      TEMPLATE_DECL.  */
    8723                 :     8378919 :                   if (val != error_mark_node)
    8724                 :             :                     {
    8725                 :     8378919 :                       if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
    8726                 :      278111 :                         val = TREE_TYPE (val);
    8727                 :     8378919 :                       if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
    8728                 :          68 :                         val = make_pack_expansion (val, complain);
    8729                 :             :                     }
    8730                 :             :                 }
    8731                 :             :               else
    8732                 :             :                 {
    8733                 :        1059 :                   if (in_decl && (complain & tf_error))
    8734                 :             :                     {
    8735                 :          48 :                       error ("type/value mismatch at argument %d in "
    8736                 :             :                              "template parameter list for %qD",
    8737                 :             :                              i + 1, in_decl);
    8738                 :          48 :                       inform (input_location,
    8739                 :             :                               "  expected a template of type %qD, got %qT",
    8740                 :             :                               parm, orig_arg);
    8741                 :             :                     }
    8742                 :             : 
    8743                 :        1059 :                   val = error_mark_node;
    8744                 :             :                 }
    8745                 :             : 
    8746                 :             :               // Check that the constraints are compatible before allowing the
    8747                 :             :               // substitution.
    8748                 :     8379978 :               if (val != error_mark_node)
    8749                 :     8378919 :                 if (!is_compatible_template_arg (parm, arg, args))
    8750                 :             :                   {
    8751                 :           6 :                     if (in_decl && (complain & tf_error))
    8752                 :             :                       {
    8753                 :           5 :                         error ("constraint mismatch at argument %d in "
    8754                 :             :                                "template parameter list for %qD",
    8755                 :             :                                i + 1, in_decl);
    8756                 :           5 :                         inform (input_location, "  expected %qD but got %qD",
    8757                 :             :                                 parm, arg);
    8758                 :             :                       }
    8759                 :           6 :                     val = error_mark_node;
    8760                 :             :                   }
    8761                 :             :             }
    8762                 :             :         }
    8763                 :             :       else
    8764                 :             :         val = orig_arg;
    8765                 :             :       /* We only form one instance of each template specialization.
    8766                 :             :          Therefore, if we use a non-canonical variant (i.e., a
    8767                 :             :          typedef), any future messages referring to the type will use
    8768                 :             :          the typedef, which is confusing if those future uses do not
    8769                 :             :          themselves also use the typedef.  */
    8770                 :   843137036 :       if (TYPE_P (val))
    8771                 :   835035169 :         val = canonicalize_type_argument (val, complain);
    8772                 :             :     }
    8773                 :             :   else
    8774                 :             :     {
    8775                 :   135126264 :       tree t = TREE_TYPE (parm);
    8776                 :             : 
    8777                 :   270252528 :       if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
    8778                 :   270252528 :           > TMPL_ARGS_DEPTH (args))
    8779                 :             :         /* We don't have enough levels of args to do any substitution.  This
    8780                 :             :            can happen in the context of -fnew-ttp-matching.  */;
    8781                 :   135126248 :       else if (tree a = type_uses_auto (t))
    8782                 :             :         {
    8783                 :        3650 :           t = do_auto_deduction (t, arg, a, complain, adc_unify, args,
    8784                 :             :                                  LOOKUP_IMPLICIT, /*tmpl=*/in_decl);
    8785                 :        3650 :           if (t == error_mark_node)
    8786                 :             :             return error_mark_node;
    8787                 :             :         }
    8788                 :             :       else
    8789                 :   135122598 :         t = tsubst (t, args, complain, in_decl);
    8790                 :             : 
    8791                 :             :       /* Perform array-to-pointer and function-to-pointer conversion
    8792                 :             :          as per [temp.param]/10.  */
    8793                 :   135126247 :       t = type_decays_to (t);
    8794                 :             : 
    8795                 :   135126247 :       if (invalid_nontype_parm_type_p (t, complain))
    8796                 :          55 :         return error_mark_node;
    8797                 :             : 
    8798                 :             :       /* Drop top-level cv-qualifiers on the substituted/deduced type of
    8799                 :             :          this non-type template parameter, as per [temp.param]/6.  */
    8800                 :   135126192 :       t = cv_unqualified (t);
    8801                 :             : 
    8802                 :   135126192 :       if (t != TREE_TYPE (parm))
    8803                 :     4801636 :         t = canonicalize_type_argument (t, complain);
    8804                 :             : 
    8805                 :             :       /* We need to handle arguments for alias or concept templates
    8806                 :             :          differently: we need to force building an IMPLICIT_CONV_EXPR, because
    8807                 :             :          these arguments are going to be substituted directly into the
    8808                 :             :          dependent type; they might not get another chance at
    8809                 :             :          convert_nontype_argument.  But if the argument ends up here again for
    8810                 :             :          a template that isn't one of those, remove the conversion for
    8811                 :             :          consistency between naming the same dependent type directly or through
    8812                 :             :          an alias.  */
    8813                 :   135126192 :       bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl)
    8814                 :   103280669 :                                     || concept_definition_p (in_decl));
    8815                 :   105820799 :       if (!force_conv
    8816                 :   105820799 :           && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR
    8817                 :     4202286 :           && IMPLICIT_CONV_EXPR_FORCED (orig_arg)
    8818                 :     4202156 :           && same_type_p (TREE_TYPE (orig_arg), t))
    8819                 :     4202156 :         orig_arg = TREE_OPERAND (orig_arg, 0);
    8820                 :             : 
    8821                 :   135126192 :       if (!type_dependent_expression_p (orig_arg)
    8822                 :   135126192 :           && !uses_template_parms (t))
    8823                 :             :         /* We used to call digest_init here.  However, digest_init
    8824                 :             :            will report errors, which we don't want when complain
    8825                 :             :            is zero.  More importantly, digest_init will try too
    8826                 :             :            hard to convert things: for example, `0' should not be
    8827                 :             :            converted to pointer type at this point according to
    8828                 :             :            the standard.  Accepting this is not merely an
    8829                 :             :            extension, since deciding whether or not these
    8830                 :             :            conversions can occur is part of determining which
    8831                 :             :            function template to call, or whether a given explicit
    8832                 :             :            argument specification is valid.  */
    8833                 :   112284885 :         val = convert_nontype_argument (t, orig_arg, complain);
    8834                 :             :       else
    8835                 :             :         {
    8836                 :    22841307 :           val = canonicalize_expr_argument (orig_arg, complain);
    8837                 :    22841307 :           val = maybe_convert_nontype_argument (t, val, force_conv);
    8838                 :             :         }
    8839                 :             : 
    8840                 :   135126192 :       if (val == NULL_TREE)
    8841                 :         397 :         val = error_mark_node;
    8842                 :   135125795 :       else if (val == error_mark_node && (complain & tf_error))
    8843                 :          98 :         error_at (cp_expr_loc_or_input_loc (orig_arg),
    8844                 :             :                   "could not convert template argument %qE from %qT to %qT",
    8845                 :          71 :                   orig_arg, TREE_TYPE (orig_arg), t);
    8846                 :             : 
    8847                 :   135126192 :       if (INDIRECT_REF_P (val))
    8848                 :             :         {
    8849                 :             :           /* Reject template arguments that are references to built-in
    8850                 :             :              functions with no library fallbacks.  */
    8851                 :         433 :           const_tree inner = TREE_OPERAND (val, 0);
    8852                 :         433 :           const_tree innertype = TREE_TYPE (inner);
    8853                 :         433 :           if (innertype
    8854                 :         433 :               && TYPE_REF_P (innertype)
    8855                 :         424 :               && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
    8856                 :         113 :               && TREE_OPERAND_LENGTH (inner) > 0
    8857                 :         510 :               && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
    8858                 :           8 :               return error_mark_node;
    8859                 :             :         }
    8860                 :             : 
    8861                 :   135126184 :       if (TREE_CODE (val) == SCOPE_REF)
    8862                 :             :         {
    8863                 :             :           /* Strip typedefs from the SCOPE_REF.  */
    8864                 :     9190199 :           tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
    8865                 :     9190199 :           tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
    8866                 :             :                                                    complain);
    8867                 :    18380398 :           val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
    8868                 :     9190199 :                                       QUALIFIED_NAME_IS_TEMPLATE (val));
    8869                 :             :         }
    8870                 :             :     }
    8871                 :             : 
    8872                 :             :   return val;
    8873                 :             : }
    8874                 :             : 
    8875                 :             : /* Coerces the remaining template arguments in INNER_ARGS (from
    8876                 :             :    ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
    8877                 :             :    Returns the coerced argument pack. PARM_IDX is the position of this
    8878                 :             :    parameter in the template parameter list. ARGS is the original
    8879                 :             :    template argument list.  */
    8880                 :             : static tree
    8881                 :    74390622 : coerce_template_parameter_pack (tree parms,
    8882                 :             :                                 int parm_idx,
    8883                 :             :                                 tree args,
    8884                 :             :                                 tree inner_args,
    8885                 :             :                                 int arg_idx,
    8886                 :             :                                 tree new_args,
    8887                 :             :                                 int* lost,
    8888                 :             :                                 tree in_decl,
    8889                 :             :                                 tsubst_flags_t complain)
    8890                 :             : {
    8891                 :    74390622 :   tree parm = TREE_VEC_ELT (parms, parm_idx);
    8892                 :   148781244 :   int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    8893                 :    74390622 :   tree packed_args;
    8894                 :    74390622 :   tree argument_pack;
    8895                 :    74390622 :   tree packed_parms = NULL_TREE;
    8896                 :             : 
    8897                 :    74390622 :   if (arg_idx > nargs)
    8898                 :             :     arg_idx = nargs;
    8899                 :             : 
    8900                 :    74390622 :   if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
    8901                 :             :     {
    8902                 :             :       /* When the template parameter is a non-type template parameter pack
    8903                 :             :          or template template parameter pack whose type or template
    8904                 :             :          parameters use parameter packs, we know exactly how many arguments
    8905                 :             :          we are looking for.  Build a vector of the instantiated decls for
    8906                 :             :          these template parameters in PACKED_PARMS.  */
    8907                 :             :       /* We can't use make_pack_expansion here because it would interpret a
    8908                 :             :          _DECL as a use rather than a declaration.  */
    8909                 :          59 :       tree decl = TREE_VALUE (parm);
    8910                 :          59 :       tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
    8911                 :          59 :       PACK_EXPANSION_PATTERN (exp) = decl;
    8912                 :         118 :       PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
    8913                 :          59 :       SET_TYPE_STRUCTURAL_EQUALITY (exp);
    8914                 :             : 
    8915                 :          59 :       TREE_VEC_LENGTH (args)--;
    8916                 :          59 :       packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
    8917                 :          56 :       TREE_VEC_LENGTH (args)++;
    8918                 :             : 
    8919                 :          56 :       if (packed_parms == error_mark_node)
    8920                 :             :         return error_mark_node;
    8921                 :             : 
    8922                 :             :       /* If we're doing a partial instantiation of a member template,
    8923                 :             :          verify that all of the types used for the non-type
    8924                 :             :          template parameter pack are, in fact, valid for non-type
    8925                 :             :          template parameters.  */
    8926                 :          56 :       if (arg_idx < nargs
    8927                 :          56 :           && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
    8928                 :             :         {
    8929                 :          17 :           int j, len = TREE_VEC_LENGTH (packed_parms);
    8930                 :          38 :           for (j = 0; j < len; ++j)
    8931                 :             :             {
    8932                 :          23 :               tree t = TREE_VEC_ELT (packed_parms, j);
    8933                 :          23 :               if (TREE_CODE (t) == PARM_DECL
    8934                 :          23 :                   && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
    8935                 :           2 :                 return error_mark_node;
    8936                 :             :             }
    8937                 :             :           /* We don't know how many args we have yet, just
    8938                 :             :              use the unconverted ones for now.  */
    8939                 :             :           return NULL_TREE;
    8940                 :             :         }
    8941                 :             : 
    8942                 :          39 :       packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
    8943                 :             :     }
    8944                 :             :   /* Check if we have a placeholder pack, which indicates we're
    8945                 :             :      in the context of a introduction list.  In that case we want
    8946                 :             :      to match this pack to the single placeholder.  */
    8947                 :    74390563 :   else if (arg_idx < nargs
    8948                 :    72873044 :            && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
    8949                 :    74394494 :            && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
    8950                 :             :     {
    8951                 :          16 :       nargs = arg_idx + 1;
    8952                 :          16 :       packed_args = make_tree_vec (1);
    8953                 :             :     }
    8954                 :             :   else
    8955                 :    74390547 :     packed_args = make_tree_vec (nargs - arg_idx);
    8956                 :             : 
    8957                 :             :   /* Convert the remaining arguments, which will be a part of the
    8958                 :             :      parameter pack "parm".  */
    8959                 :    74390602 :   int first_pack_arg = arg_idx;
    8960                 :   208101123 :   for (; arg_idx < nargs; ++arg_idx)
    8961                 :             :     {
    8962                 :   133710533 :       tree arg = TREE_VEC_ELT (inner_args, arg_idx);
    8963                 :   133710533 :       tree actual_parm = TREE_VALUE (parm);
    8964                 :   133710533 :       int pack_idx = arg_idx - first_pack_arg;
    8965                 :             : 
    8966                 :   133710533 :       if (packed_parms)
    8967                 :             :         {
    8968                 :             :           /* Once we've packed as many args as we have types, stop.  */
    8969                 :          69 :           if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
    8970                 :             :             break;
    8971                 :          57 :           else if (PACK_EXPANSION_P (arg))
    8972                 :             :             /* We don't know how many args we have yet, just
    8973                 :             :                use the unconverted ones for now.  */
    8974                 :             :             return NULL_TREE;
    8975                 :             :           else
    8976                 :          57 :             actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
    8977                 :             :         }
    8978                 :             : 
    8979                 :   133710521 :       if (arg == error_mark_node)
    8980                 :             :         {
    8981                 :          18 :           if (complain & tf_error)
    8982                 :          17 :             error ("template argument %d is invalid", arg_idx + 1);
    8983                 :             :         }
    8984                 :             :       else
    8985                 :   133710503 :         arg = convert_template_argument (actual_parm,
    8986                 :             :                                          arg, new_args, complain, parm_idx,
    8987                 :             :                                          in_decl);
    8988                 :   133710521 :       if (arg == error_mark_node)
    8989                 :        4469 :         (*lost)++;
    8990                 :   133710521 :       TREE_VEC_ELT (packed_args, pack_idx) = arg;
    8991                 :             :     }
    8992                 :             : 
    8993                 :    74390602 :   if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
    8994                 :    74390602 :       && TREE_VEC_LENGTH (packed_args) > 0)
    8995                 :             :     {
    8996                 :           3 :       if (complain & tf_error)
    8997                 :           3 :         error ("wrong number of template arguments (%d, should be %d)",
    8998                 :           3 :                arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
    8999                 :           3 :       return error_mark_node;
    9000                 :             :     }
    9001                 :             : 
    9002                 :    74390599 :   if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
    9003                 :    74390599 :       || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
    9004                 :    72364234 :     argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
    9005                 :             :   else
    9006                 :             :     {
    9007                 :     2026365 :       argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
    9008                 :     2026365 :       TREE_CONSTANT (argument_pack) = 1;
    9009                 :             :     }
    9010                 :             : 
    9011                 :    74390599 :   ARGUMENT_PACK_ARGS (argument_pack) = packed_args;
    9012                 :    74390599 :   if (CHECKING_P)
    9013                 :    74390599 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
    9014                 :             :                                          TREE_VEC_LENGTH (packed_args));
    9015                 :    74390599 :   return argument_pack;
    9016                 :             : }
    9017                 :             : 
    9018                 :             : /* Returns the number of pack expansions in the template argument vector
    9019                 :             :    ARGS.  */
    9020                 :             : 
    9021                 :             : static int
    9022                 :  1414682143 : pack_expansion_args_count (tree args)
    9023                 :             : {
    9024                 :  1414682143 :   int i;
    9025                 :  1414682143 :   int count = 0;
    9026                 :  1414682143 :   if (args)
    9027                 :  4088495631 :     for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
    9028                 :             :       {
    9029                 :  2673813488 :         tree elt = TREE_VEC_ELT (args, i);
    9030                 :  2673813488 :         if (elt && PACK_EXPANSION_P (elt))
    9031                 :    28043157 :           ++count;
    9032                 :             :       }
    9033                 :  1414682143 :   return count;
    9034                 :             : }
    9035                 :             : 
    9036                 :             : /* Convert all template arguments to their appropriate types, and
    9037                 :             :    return a vector containing the innermost resulting template
    9038                 :             :    arguments.  If any error occurs, return error_mark_node. Error and
    9039                 :             :    warning messages are issued under control of COMPLAIN.
    9040                 :             : 
    9041                 :             :    If PARMS represents all template parameters levels, this function
    9042                 :             :    returns a vector of vectors representing all the resulting argument
    9043                 :             :    levels.  Note that in this case, only the innermost arguments are
    9044                 :             :    coerced because the outermost ones are supposed to have been coerced
    9045                 :             :    already.  Otherwise, if PARMS represents only (the innermost) vector
    9046                 :             :    of parameters, this function returns a vector containing just the
    9047                 :             :    innermost resulting arguments.
    9048                 :             : 
    9049                 :             :    If REQUIRE_ALL_ARGS is false, argument deduction will be performed
    9050                 :             :    for arguments not specified in ARGS.  If REQUIRE_ALL_ARGS is true,
    9051                 :             :    arguments not specified in ARGS must have default arguments which
    9052                 :             :    we'll use to fill in ARGS.  */
    9053                 :             : 
    9054                 :             : tree
    9055                 :   567413707 : coerce_template_parms (tree parms,
    9056                 :             :                        tree args,
    9057                 :             :                        tree in_decl,
    9058                 :             :                        tsubst_flags_t complain,
    9059                 :             :                        bool require_all_args /* = true */)
    9060                 :             : {
    9061                 :   567413707 :   int nparms, nargs, parm_idx, arg_idx, lost = 0;
    9062                 :   567413707 :   tree orig_inner_args;
    9063                 :   567413707 :   tree inner_args;
    9064                 :             : 
    9065                 :             :   /* When used as a boolean value, indicates whether this is a
    9066                 :             :      variadic template parameter list. Since it's an int, we can also
    9067                 :             :      subtract it from nparms to get the number of non-variadic
    9068                 :             :      parameters.  */
    9069                 :   567413707 :   int variadic_p = 0;
    9070                 :   567413707 :   int variadic_args_p = 0;
    9071                 :   567413707 :   int post_variadic_parms = 0;
    9072                 :             : 
    9073                 :             :   /* Adjustment to nparms for fixed parameter packs.  */
    9074                 :   567413707 :   int fixed_pack_adjust = 0;
    9075                 :   567413707 :   int fixed_packs = 0;
    9076                 :   567413707 :   int missing = 0;
    9077                 :             : 
    9078                 :             :   /* Likewise for parameters with default arguments.  */
    9079                 :   567413707 :   int default_p = 0;
    9080                 :             : 
    9081                 :   567413707 :   if (args == error_mark_node)
    9082                 :             :     return error_mark_node;
    9083                 :             : 
    9084                 :   567413707 :   bool return_full_args = false;
    9085                 :   567413707 :   if (TREE_CODE (parms) == TREE_LIST)
    9086                 :             :     {
    9087                 :   483845422 :       if (TMPL_PARMS_DEPTH (parms) > 1)
    9088                 :             :         {
    9089                 :    24055272 :           gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args));
    9090                 :             :           return_full_args = true;
    9091                 :             :         }
    9092                 :   483845422 :       parms = INNERMOST_TEMPLATE_PARMS (parms);
    9093                 :             :     }
    9094                 :             : 
    9095                 :   567413707 :   nparms = TREE_VEC_LENGTH (parms);
    9096                 :             : 
    9097                 :             :   /* Determine if there are any parameter packs or default arguments.  */
    9098                 :  1492947582 :   for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
    9099                 :             :     {
    9100                 :   925533875 :       tree parm = TREE_VEC_ELT (parms, parm_idx);
    9101                 :   925533875 :       if (variadic_p)
    9102                 :        3934 :         ++post_variadic_parms;
    9103                 :   925533875 :       if (template_parameter_pack_p (TREE_VALUE (parm)))
    9104                 :    76945773 :         ++variadic_p;
    9105                 :   925533875 :       if (TREE_PURPOSE (parm))
    9106                 :   147376990 :         ++default_p;
    9107                 :             :     }
    9108                 :             : 
    9109                 :   567413707 :   inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
    9110                 :             :   /* If there are no parameters that follow a parameter pack, we need to
    9111                 :             :      expand any argument packs so that we can deduce a parameter pack from
    9112                 :             :      some non-packed args followed by an argument pack, as in variadic85.C.
    9113                 :             :      If there are such parameters, we need to leave argument packs intact
    9114                 :             :      so the arguments are assigned properly.  This can happen when dealing
    9115                 :             :      with a nested class inside a partial specialization of a class
    9116                 :             :      template, as in variadic92.C, or when deducing a template parameter pack
    9117                 :             :      from a sub-declarator, as in variadic114.C.  */
    9118                 :   567413707 :   if (!post_variadic_parms)
    9119                 :   567410286 :     inner_args = expand_template_argument_pack (inner_args);
    9120                 :             : 
    9121                 :             :   /* Count any pack expansion args.  */
    9122                 :   567413707 :   variadic_args_p = pack_expansion_args_count (inner_args);
    9123                 :             : 
    9124                 :  1134827414 :   nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
    9125                 :    39797516 :   if ((nargs - variadic_args_p > nparms && !variadic_p)
    9126                 :   606696065 :       || (nargs < nparms - variadic_p
    9127                 :             :           && require_all_args
    9128                 :    15032647 :           && !variadic_args_p
    9129                 :    10470250 :           && (TREE_VEC_ELT (parms, nargs) != error_mark_node
    9130                 :    10470250 :               && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))
    9131                 :             :     {
    9132                 :      515268 :     bad_nargs:
    9133                 :      515268 :       if (complain & tf_error)
    9134                 :             :         {
    9135                 :          77 :           if (variadic_p || default_p)
    9136                 :             :             {
    9137                 :          10 :               nparms -= variadic_p + default_p;
    9138                 :          10 :               error ("wrong number of template arguments "
    9139                 :             :                      "(%d, should be at least %d)", nargs, nparms);
    9140                 :             :             }
    9141                 :             :           else
    9142                 :          67 :              error ("wrong number of template arguments "
    9143                 :             :                     "(%d, should be %d)", nargs, nparms);
    9144                 :             : 
    9145                 :          77 :           if (in_decl)
    9146                 :          77 :             inform (DECL_SOURCE_LOCATION (in_decl),
    9147                 :             :                     "provided for %qD", in_decl);
    9148                 :             :         }
    9149                 :             : 
    9150                 :      515268 :       return error_mark_node;
    9151                 :             :     }
    9152                 :             :   /* We can't pass a pack expansion to a non-pack parameter of an alias
    9153                 :             :      template (DR 1430).  */
    9154                 :   566898450 :   else if (in_decl
    9155                 :   558152932 :            && (DECL_ALIAS_TEMPLATE_P (in_decl)
    9156                 :   467963829 :                || concept_definition_p (in_decl))
    9157                 :   105496428 :            && variadic_args_p
    9158                 :   570973195 :            && nargs - variadic_args_p < nparms - variadic_p)
    9159                 :             :     {
    9160                 :          16 :       if (complain & tf_error)
    9161                 :             :         {
    9162                 :          18 :           for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
    9163                 :             :             {
    9164                 :          18 :               tree arg = TREE_VEC_ELT (inner_args, i);
    9165                 :          18 :               tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
    9166                 :             : 
    9167                 :          18 :               if (PACK_EXPANSION_P (arg)
    9168                 :          18 :                   && !template_parameter_pack_p (parm))
    9169                 :             :                 {
    9170                 :          15 :                   if (DECL_ALIAS_TEMPLATE_P (in_decl))
    9171                 :          12 :                     error_at (location_of (arg),
    9172                 :             :                               "pack expansion argument for non-pack parameter "
    9173                 :             :                               "%qD of alias template %qD", parm, in_decl);
    9174                 :             :                   else
    9175                 :           3 :                     error_at (location_of (arg),
    9176                 :             :                               "pack expansion argument for non-pack parameter "
    9177                 :             :                               "%qD of concept %qD", parm, in_decl);
    9178                 :          15 :                   inform (DECL_SOURCE_LOCATION (parm), "declared here");
    9179                 :          15 :                   goto found;
    9180                 :             :                 }
    9181                 :             :             }
    9182                 :           0 :           gcc_unreachable ();
    9183                 :          15 :         found:;
    9184                 :             :         }
    9185                 :          16 :       return error_mark_node;
    9186                 :             :     }
    9187                 :             : 
    9188                 :             :   /* We need to evaluate the template arguments, even though this
    9189                 :             :      template-id may be nested within a "sizeof".  */
    9190                 :   566898434 :   cp_evaluated ev;
    9191                 :             : 
    9192                 :   566898434 :   tree new_args = add_outermost_template_args (args, make_tree_vec (nparms));
    9193                 :   566898434 :   tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args));
    9194                 :   566898434 :   int pack_adjust = 0;
    9195                 :  1481681373 :   for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
    9196                 :             :     {
    9197                 :   921926028 :       tree arg;
    9198                 :   921926028 :       tree parm;
    9199                 :             : 
    9200                 :             :       /* Get the Ith template parameter.  */
    9201                 :   921926028 :       parm = TREE_VEC_ELT (parms, parm_idx);
    9202                 :             : 
    9203                 :   921926028 :       if (parm == error_mark_node)
    9204                 :             :         {
    9205                 :           0 :           TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
    9206                 :           0 :           continue;
    9207                 :             :         }
    9208                 :             : 
    9209                 :             :       /* Calculate the next argument.  */
    9210                 :   921926028 :       if (arg_idx < nargs)
    9211                 :   901254175 :         arg = TREE_VEC_ELT (inner_args, arg_idx);
    9212                 :             :       else
    9213                 :             :         arg = NULL_TREE;
    9214                 :             : 
    9215                 :   921926028 :       if (template_parameter_pack_p (TREE_VALUE (parm))
    9216                 :    76400501 :           && (arg || require_all_args || !(complain & tf_partial))
    9217                 :   996316671 :           && !(arg && ARGUMENT_PACK_P (arg)))
    9218                 :             :         {
    9219                 :             :           /* Some arguments will be placed in the
    9220                 :             :              template parameter pack PARM.  */
    9221                 :    74390622 :           arg = coerce_template_parameter_pack (parms, parm_idx, args,
    9222                 :             :                                                 inner_args, arg_idx,
    9223                 :             :                                                 new_args, &lost,
    9224                 :             :                                                 in_decl, complain);
    9225                 :             : 
    9226                 :    74390619 :           if (arg == NULL_TREE)
    9227                 :             :             {
    9228                 :             :               /* We don't know how many args we have yet, just use the
    9229                 :             :                  unconverted (and still packed) ones for now.  */
    9230                 :          15 :               new_inner_args = orig_inner_args;
    9231                 :          15 :               arg_idx = nargs;
    9232                 :          15 :               break;
    9233                 :             :             }
    9234                 :             : 
    9235                 :    74390604 :           TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
    9236                 :             : 
    9237                 :             :           /* Store this argument.  */
    9238                 :    74390604 :           if (arg == error_mark_node)
    9239                 :             :             {
    9240                 :           5 :               lost++;
    9241                 :             :               /* We are done with all of the arguments.  */
    9242                 :           5 :               arg_idx = nargs;
    9243                 :           5 :               break;
    9244                 :             :             }
    9245                 :             :           else
    9246                 :             :             {
    9247                 :    74390599 :               pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
    9248                 :    74390599 :               arg_idx += pack_adjust;
    9249                 :    74390599 :               if (fixed_parameter_pack_p (TREE_VALUE (parm)))
    9250                 :             :                 {
    9251                 :          36 :                   ++fixed_packs;
    9252                 :          36 :                   fixed_pack_adjust += pack_adjust;
    9253                 :             :                 }
    9254                 :             :             }
    9255                 :             : 
    9256                 :    74390599 :           continue;
    9257                 :             :         }
    9258                 :   847535406 :       else if (arg)
    9259                 :             :         {
    9260                 :   828379996 :           if (PACK_EXPANSION_P (arg))
    9261                 :             :             {
    9262                 :             :               /* "If every valid specialization of a variadic template
    9263                 :             :                  requires an empty template parameter pack, the template is
    9264                 :             :                  ill-formed, no diagnostic required."  So check that the
    9265                 :             :                  pattern works with this parameter.  */
    9266                 :      757073 :               tree pattern = PACK_EXPANSION_PATTERN (arg);
    9267                 :      757073 :               tree conv = convert_template_argument (TREE_VALUE (parm),
    9268                 :             :                                                      pattern, new_args,
    9269                 :             :                                                      complain, parm_idx,
    9270                 :             :                                                      in_decl);
    9271                 :      757073 :               if (conv == error_mark_node)
    9272                 :             :                 {
    9273                 :          10 :                   if (complain & tf_error)
    9274                 :           6 :                     inform (input_location, "so any instantiation with a "
    9275                 :             :                             "non-empty parameter pack would be ill-formed");
    9276                 :          10 :                   ++lost;
    9277                 :             :                 }
    9278                 :      757063 :               else if (TYPE_P (conv) && !TYPE_P (pattern))
    9279                 :             :                 /* Recover from missing typename.  */
    9280                 :           9 :                 TREE_VEC_ELT (inner_args, arg_idx)
    9281                 :          18 :                   = make_pack_expansion (conv, complain);
    9282                 :             : 
    9283                 :             :               /* We don't know how many args we have yet, just
    9284                 :             :                  use the unconverted ones for now.  */
    9285                 :      757073 :               new_inner_args = inner_args;
    9286                 :      757073 :               arg_idx = nargs;
    9287                 :      757073 :               break;
    9288                 :             :             }
    9289                 :             :         }
    9290                 :    19155410 :       else if (require_all_args)
    9291                 :             :         {
    9292                 :             :           /* There must be a default arg in this case.  */
    9293                 :    12769417 :           arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
    9294                 :             :                                      complain, in_decl);
    9295                 :             :           /* The position of the first default template argument,
    9296                 :             :              is also the number of non-defaulted arguments in NEW_INNER_ARGS.
    9297                 :             :              Record that.  */
    9298                 :    12769417 :           if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9299                 :    10471239 :             SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9300                 :             :                                                  arg_idx - pack_adjust);
    9301                 :             :         }
    9302                 :             :       else
    9303                 :             :         break;
    9304                 :             : 
    9305                 :   840392340 :       if (arg == error_mark_node)
    9306                 :             :         {
    9307                 :         280 :           if (complain & tf_error)
    9308                 :         251 :             error ("template argument %d is invalid", arg_idx + 1);
    9309                 :             :         }
    9310                 :   840392060 :       else if (!arg)
    9311                 :             :         {
    9312                 :             :           /* This can occur if there was an error in the template
    9313                 :             :              parameter list itself (which we would already have
    9314                 :             :              reported) that we are trying to recover from, e.g., a class
    9315                 :             :              template with a parameter list such as
    9316                 :             :              template<typename..., typename> (cpp0x/variadic150.C).  */
    9317                 :          12 :           ++lost;
    9318                 :             : 
    9319                 :             :           /* This can also happen with a fixed parameter pack (71834).  */
    9320                 :          12 :           if (arg_idx >= nargs)
    9321                 :           6 :             ++missing;
    9322                 :             :         }
    9323                 :             :       else
    9324                 :   840392048 :         arg = convert_template_argument (TREE_VALUE (parm),
    9325                 :             :                                          arg, new_args, complain,
    9326                 :             :                                          parm_idx, in_decl);
    9327                 :             : 
    9328                 :   840392340 :       if (arg == error_mark_node)
    9329                 :     2030164 :         lost++;
    9330                 :             : 
    9331                 :   840392340 :       TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
    9332                 :             :     }
    9333                 :             : 
    9334                 :   566898431 :   if (missing || arg_idx < nargs - variadic_args_p)
    9335                 :             :     {
    9336                 :             :       /* If we had fixed parameter packs, we didn't know how many arguments we
    9337                 :             :          actually needed earlier; now we do.  */
    9338                 :          11 :       nparms += fixed_pack_adjust;
    9339                 :          11 :       variadic_p -= fixed_packs;
    9340                 :          11 :       goto bad_nargs;
    9341                 :             :     }
    9342                 :             : 
    9343                 :   566898420 :   if (arg_idx < nargs)
    9344                 :             :     {
    9345                 :             :       /* We had some pack expansion arguments that will only work if the packs
    9346                 :             :          are empty, but wait until instantiation time to complain.
    9347                 :             :          See variadic-ttp3.C.  */
    9348                 :             : 
    9349                 :             :       /* Except that we can't provide empty packs to alias templates or
    9350                 :             :          concepts when there are no corresponding parameters. Basically,
    9351                 :             :          we can get here with this:
    9352                 :             : 
    9353                 :             :              template<typename T> concept C = true;
    9354                 :             : 
    9355                 :             :              template<typename... Args>
    9356                 :             :                requires C<Args...>
    9357                 :             :              void f();
    9358                 :             : 
    9359                 :             :          When parsing C<Args...>, we try to form a concept check of
    9360                 :             :          C<?, Args...>. Without the extra check for substituting an empty
    9361                 :             :          pack past the last parameter, we can accept the check as valid.
    9362                 :             : 
    9363                 :             :          FIXME: This may be valid for alias templates (but I doubt it).
    9364                 :             : 
    9365                 :             :          FIXME: The error could be better also.   */
    9366                 :       45195 :       if (in_decl && concept_definition_p (in_decl))
    9367                 :             :         {
    9368                 :           0 :           if (complain & tf_error)
    9369                 :           0 :             error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
    9370                 :             :                       "too many arguments");
    9371                 :   566898420 :           return error_mark_node;
    9372                 :             :         }
    9373                 :             : 
    9374                 :       45195 :       int len = nparms + (nargs - arg_idx);
    9375                 :       45195 :       tree args = make_tree_vec (len);
    9376                 :       45195 :       int i = 0;
    9377                 :      135588 :       for (; i < nparms; ++i)
    9378                 :       45198 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
    9379                 :       90390 :       for (; i < len; ++i, ++arg_idx)
    9380                 :       45195 :         TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
    9381                 :             :                                                arg_idx - pack_adjust);
    9382                 :       45195 :       new_inner_args = args;
    9383                 :             :     }
    9384                 :             : 
    9385                 :   566898420 :   if (lost)
    9386                 :             :     {
    9387                 :     2034325 :       gcc_assert (!(complain & tf_error) || seen_error ());
    9388                 :     2034325 :       return error_mark_node;
    9389                 :             :     }
    9390                 :             : 
    9391                 :   564864095 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
    9392                 :   553635838 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
    9393                 :             :                                          TREE_VEC_LENGTH (new_inner_args));
    9394                 :             : 
    9395                 :   564864095 :   return return_full_args ? new_args : new_inner_args;
    9396                 :             : }
    9397                 :             : 
    9398                 :             : /* Returns true if T is a wrapper to make a C++20 template parameter
    9399                 :             :    object const.  */
    9400                 :             : 
    9401                 :             : static bool
    9402                 : 19052655454 : class_nttp_const_wrapper_p (tree t)
    9403                 :             : {
    9404                 : 19052655454 :   if (cxx_dialect < cxx20)
    9405                 :             :     return false;
    9406                 :  6391535491 :   return (TREE_CODE (t) == VIEW_CONVERT_EXPR
    9407                 :          50 :           && CP_TYPE_CONST_P (TREE_TYPE (t))
    9408                 :  6391535541 :           && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
    9409                 :             : }
    9410                 :             : 
    9411                 :             : /* Returns 1 if template args OT and NT are equivalent.  */
    9412                 :             : 
    9413                 :             : int
    9414                 :  2440947660 : template_args_equal (tree ot, tree nt)
    9415                 :             : {
    9416                 :  2440947660 :   if (nt == ot)
    9417                 :             :     return 1;
    9418                 :  1489578665 :   if (nt == NULL_TREE || ot == NULL_TREE)
    9419                 :             :     return false;
    9420                 :  1489481366 :   if (nt == any_targ_node || ot == any_targ_node)
    9421                 :             :     return true;
    9422                 :             : 
    9423                 :  1489481362 :   if (class_nttp_const_wrapper_p (nt))
    9424                 :          14 :     nt = TREE_OPERAND (nt, 0);
    9425                 :  1489481362 :   if (class_nttp_const_wrapper_p (ot))
    9426                 :          11 :     ot = TREE_OPERAND (ot, 0);
    9427                 :             : 
    9428                 :             :   /* DR 1558: Don't treat an alias template specialization with dependent
    9429                 :             :      arguments as equivalent to its underlying type when used as a template
    9430                 :             :      argument; we need them to be distinct so that we substitute into the
    9431                 :             :      specialization arguments at instantiation time.  And aliases can't be
    9432                 :             :      equivalent without being ==, so we don't need to look any deeper.
    9433                 :             : 
    9434                 :             :      During partial ordering, however, we need to treat them normally so we can
    9435                 :             :      order uses of the same alias with different cv-qualification (79960).  */
    9436                 :  1489481362 :   auto cso = make_temp_override (comparing_dependent_aliases);
    9437                 :  1489481362 :   if (!comparing_for_partial_ordering)
    9438                 :  1485933996 :     ++comparing_dependent_aliases;
    9439                 :             : 
    9440                 :  1489481362 :   if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
    9441                 :             :     /* For member templates */
    9442                 :    65854696 :     return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
    9443                 :  1447953944 :   else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
    9444                 :    15557284 :     return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
    9445                 :    13893102 :             && template_args_equal (PACK_EXPANSION_PATTERN (ot),
    9446                 :    13893102 :                                     PACK_EXPANSION_PATTERN (nt))
    9447                 :    26565697 :             && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
    9448                 :    26565697 :                                     PACK_EXPANSION_EXTRA_ARGS (nt)));
    9449                 :  1432011388 :   else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
    9450                 :   241723287 :     return cp_tree_equal (ot, nt);
    9451                 :  1190288101 :   else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
    9452                 :           0 :     gcc_unreachable ();
    9453                 :  1190288101 :   else if (TYPE_P (nt) || TYPE_P (ot))
    9454                 :             :     {
    9455                 :   982929024 :       if (!(TYPE_P (nt) && TYPE_P (ot)))
    9456                 :             :         return false;
    9457                 :   974003118 :       return same_type_p (ot, nt);
    9458                 :             :     }
    9459                 :             :   else
    9460                 :             :     {
    9461                 :             :       /* Try to treat a template non-type argument that has been converted
    9462                 :             :          to the parameter type as equivalent to one that hasn't yet.  */
    9463                 :   207359077 :       for (enum tree_code code1 = TREE_CODE (ot);
    9464                 :   207560489 :            CONVERT_EXPR_CODE_P (code1)
    9465                 :   207560489 :              || code1 == NON_LVALUE_EXPR;
    9466                 :      201412 :            code1 = TREE_CODE (ot))
    9467                 :      201412 :         ot = TREE_OPERAND (ot, 0);
    9468                 :             : 
    9469                 :   207359077 :       for (enum tree_code code2 = TREE_CODE (nt);
    9470                 :   207635000 :            CONVERT_EXPR_CODE_P (code2)
    9471                 :   207635000 :              || code2 == NON_LVALUE_EXPR;
    9472                 :      275923 :            code2 = TREE_CODE (nt))
    9473                 :      275923 :         nt = TREE_OPERAND (nt, 0);
    9474                 :             : 
    9475                 :   207359077 :       return cp_tree_equal (ot, nt);
    9476                 :             :     }
    9477                 :  1489481362 : }
    9478                 :             : 
    9479                 :             : /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of
    9480                 :             :    template arguments.  Returns false otherwise, and updates OLDARG_PTR and
    9481                 :             :    NEWARG_PTR with the offending arguments if they are non-NULL.  */
    9482                 :             : 
    9483                 :             : bool
    9484                 :  2032375584 : comp_template_args (tree oldargs, tree newargs,
    9485                 :             :                     tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */)
    9486                 :             : {
    9487                 :  2032375584 :   if (oldargs == newargs)
    9488                 :             :     return true;
    9489                 :             : 
    9490                 :  1423924207 :   if (!oldargs || !newargs)
    9491                 :             :     return false;
    9492                 :             : 
    9493                 :  1423924206 :   if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    9494                 :             :     return false;
    9495                 :             : 
    9496                 :  2375457446 :   for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
    9497                 :             :     {
    9498                 :  1856388080 :       tree nt = TREE_VEC_ELT (newargs, i);
    9499                 :  1856388080 :       tree ot = TREE_VEC_ELT (oldargs, i);
    9500                 :             : 
    9501                 :  1856388080 :       if (! template_args_equal (ot, nt))
    9502                 :             :         {
    9503                 :   903894386 :           if (oldarg_ptr != NULL)
    9504                 :           6 :             *oldarg_ptr = ot;
    9505                 :   903894386 :           if (newarg_ptr != NULL)
    9506                 :           6 :             *newarg_ptr = nt;
    9507                 :   903894386 :           return false;
    9508                 :             :         }
    9509                 :             :     }
    9510                 :             :   return true;
    9511                 :             : }
    9512                 :             : 
    9513                 :             : static bool
    9514                 :    15074323 : comp_template_args_porder (tree oargs, tree nargs)
    9515                 :             : {
    9516                 :    15074323 :   ++comparing_for_partial_ordering;
    9517                 :    15074323 :   bool equal = comp_template_args (oargs, nargs);
    9518                 :    15074323 :   --comparing_for_partial_ordering;
    9519                 :    15074323 :   return equal;
    9520                 :             : }
    9521                 :             : 
    9522                 :             : /* Implement a freelist interface for objects of type T.
    9523                 :             : 
    9524                 :             :    Head is a separate object, rather than a regular member, so that we
    9525                 :             :    can define it as a GTY deletable pointer, which is highly
    9526                 :             :    desirable.  A data member could be declared that way, but then the
    9527                 :             :    containing object would implicitly get GTY((user)), which would
    9528                 :             :    prevent us from instantiating freelists as global objects.
    9529                 :             :    Although this way we can create freelist global objects, they're
    9530                 :             :    such thin wrappers that instantiating temporaries at every use
    9531                 :             :    loses nothing and saves permanent storage for the freelist object.
    9532                 :             : 
    9533                 :             :    Member functions next, anew, poison and reinit have default
    9534                 :             :    implementations that work for most of the types we're interested
    9535                 :             :    in, but if they don't work for some type, they should be explicitly
    9536                 :             :    specialized.  See the comments before them for requirements, and
    9537                 :             :    the example specializations for the tree_list_freelist.  */
    9538                 :             : template <typename T>
    9539                 :             : class freelist
    9540                 :             : {
    9541                 :             :   /* Return the next object in a chain.  We could just do type
    9542                 :             :      punning, but if we access the object with its underlying type, we
    9543                 :             :      avoid strict-aliasing trouble.  This needs only work between
    9544                 :             :      poison and reinit.  */
    9545                 :  1206531901 :   static T *&next (T *obj) { return obj->next; }
    9546                 :             : 
    9547                 :             :   /* Return a newly allocated, uninitialized or minimally-initialized
    9548                 :             :      object of type T.  Any initialization performed by anew should
    9549                 :             :      either remain across the life of the object and the execution of
    9550                 :             :      poison, or be redone by reinit.  */
    9551                 :    13248673 :   static T *anew () { return ggc_alloc<T> (); }
    9552                 :             : 
    9553                 :             :   /* Optionally scribble all over the bits holding the object, so that
    9554                 :             :      they become (mostly?) uninitialized memory.  This is called while
    9555                 :             :      preparing to make the object part of the free list.  */
    9556                 :   609193056 :   static void poison (T *obj) {
    9557                 :   609193056 :     T *p ATTRIBUTE_UNUSED = obj;
    9558                 :   609193056 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9559                 :             : 
    9560                 :             : #ifdef ENABLE_GC_CHECKING
    9561                 :             :     /* Poison the data, to indicate the data is garbage.  */
    9562                 :             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
    9563                 :   609193056 :     memset (p, 0xa5, sizeof (*p));
    9564                 :             : #endif
    9565                 :             :     /* Let valgrind know the object is free.  */
    9566                 :             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
    9567                 :             : 
    9568                 :             :     /* Let valgrind know the next portion of the object is available,
    9569                 :             :        but uninitialized.  */
    9570                 :             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9571                 :             :   }
    9572                 :             : 
    9573                 :             :   /* Bring an object that underwent at least one lifecycle after anew
    9574                 :             :      and before the most recent free and poison, back to a usable
    9575                 :             :      state, reinitializing whatever is needed for it to be
    9576                 :             :      functionally equivalent to an object just allocated and returned
    9577                 :             :      by anew.  This may poison or clear the next field, used by
    9578                 :             :      freelist housekeeping after poison was called.  */
    9579                 :   597338845 :   static void reinit (T *obj) {
    9580                 :   597338845 :     T **q ATTRIBUTE_UNUSED = &next (obj);
    9581                 :             : 
    9582                 :             : #ifdef ENABLE_GC_CHECKING
    9583                 :   597338845 :     memset (q, 0xa5, sizeof (*q));
    9584                 :             : #endif
    9585                 :             :     /* Let valgrind know the entire object is available, but
    9586                 :             :        uninitialized.  */
    9587                 :             :     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
    9588                 :             :   }
    9589                 :             : 
    9590                 :             :   /* Reference a GTY-deletable pointer that points to the first object
    9591                 :             :      in the free list proper.  */
    9592                 :             :   T *&head;
    9593                 :             : public:
    9594                 :             :   /* Construct a freelist object chaining objects off of HEAD.  */
    9595                 :  1219781967 :   freelist (T *&head) : head(head) {}
    9596                 :             : 
    9597                 :             :   /* Add OBJ to the free object list.  The former head becomes OBJ's
    9598                 :             :      successor.  */
    9599                 :   609193522 :   void free (T *obj)
    9600                 :             :   {
    9601                 :   609193522 :     poison (obj);
    9602                 :   609193522 :     next (obj) = head;
    9603                 :   609193522 :     head = obj;
    9604                 :   609193522 :   }
    9605                 :             : 
    9606                 :             :   /* Take an object from the free list, if one is available, or
    9607                 :             :      allocate a new one.  Objects taken from the free list should be
    9608                 :             :      regarded as filled with garbage, except for bits that are
    9609                 :             :      configured to be preserved across free and alloc.  */
    9610                 :   610588445 :   T *alloc ()
    9611                 :             :   {
    9612                 :   610588445 :     if (head)
    9613                 :             :       {
    9614                 :   597339254 :         T *obj = head;
    9615                 :   597339254 :         head = next (head);
    9616                 :   597339254 :         reinit (obj);
    9617                 :   597339254 :         return obj;
    9618                 :             :       }
    9619                 :             :     else
    9620                 :    13249191 :       return anew ();
    9621                 :             :   }
    9622                 :             : };
    9623                 :             : 
    9624                 :             : /* Explicitly specialize the interfaces for freelist<tree_node>: we
    9625                 :             :    want to allocate a TREE_LIST using the usual interface, and ensure
    9626                 :             :    TREE_CHAIN remains functional.  Alas, we have to duplicate a bit of
    9627                 :             :    build_tree_list logic in reinit, so this could go out of sync.  */
    9628                 :             : template <>
    9629                 :             : inline tree &
    9630                 :        1341 : freelist<tree_node>::next (tree obj)
    9631                 :             : {
    9632                 :        1341 :   return TREE_CHAIN (obj);
    9633                 :             : }
    9634                 :             : template <>
    9635                 :             : inline tree
    9636                 :         518 : freelist<tree_node>::anew ()
    9637                 :             : {
    9638                 :         518 :   return build_tree_list (NULL, NULL);
    9639                 :             : }
    9640                 :             : template <>
    9641                 :             : inline void
    9642                 :         466 : freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
    9643                 :             : {
    9644                 :         466 :   int size ATTRIBUTE_UNUSED = sizeof (tree_list);
    9645                 :         466 :   tree p ATTRIBUTE_UNUSED = obj;
    9646                 :         466 :   tree_base *b ATTRIBUTE_UNUSED = &obj->base;
    9647                 :         466 :   tree *q ATTRIBUTE_UNUSED = &next (obj);
    9648                 :             : 
    9649                 :             : #ifdef ENABLE_GC_CHECKING
    9650                 :         466 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9651                 :             : 
    9652                 :             :   /* Poison the data, to indicate the data is garbage.  */
    9653                 :         466 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
    9654                 :         466 :   memset (p, 0xa5, size);
    9655                 :             : #endif
    9656                 :             :   /* Let valgrind know the object is free.  */
    9657                 :         466 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
    9658                 :             :   /* But we still want to use the TREE_CODE and TREE_CHAIN parts.  */
    9659                 :         466 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9660                 :         466 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
    9661                 :             : 
    9662                 :             : #ifdef ENABLE_GC_CHECKING
    9663                 :         466 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
    9664                 :             :   /* Keep TREE_CHAIN functional.  */
    9665                 :         466 :   TREE_SET_CODE (obj, TREE_LIST);
    9666                 :             : #else
    9667                 :             :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
    9668                 :             : #endif
    9669                 :         466 : }
    9670                 :             : template <>
    9671                 :             : inline void
    9672                 :         409 : freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
    9673                 :             : {
    9674                 :         409 :   tree_common *c ATTRIBUTE_UNUSED = &obj->common;
    9675                 :             : 
    9676                 :             : #ifdef ENABLE_GC_CHECKING
    9677                 :         409 :   gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
    9678                 :         409 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9679                 :         409 :   memset (obj, 0, sizeof (tree_list));
    9680                 :             : #endif
    9681                 :             : 
    9682                 :             :   /* Let valgrind know the entire object is available, but
    9683                 :             :      uninitialized.  */
    9684                 :         409 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
    9685                 :             : 
    9686                 :             : #ifdef ENABLE_GC_CHECKING
    9687                 :         409 :   TREE_SET_CODE (obj, TREE_LIST);
    9688                 :             : #else
    9689                 :             :   TREE_CHAIN (obj) = NULL_TREE;
    9690                 :             :   TREE_TYPE (obj) = NULL_TREE;
    9691                 :             : #endif
    9692                 :         409 :   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c)));
    9693                 :         409 : }
    9694                 :             : 
    9695                 :             : /* Point to the first object in the TREE_LIST freelist.  */
    9696                 :             : static GTY((deletable)) tree tree_list_freelist_head;
    9697                 :             : /* Return the/an actual TREE_LIST freelist.  */
    9698                 :             : static inline freelist<tree_node>
    9699                 :        1393 : tree_list_freelist ()
    9700                 :             : {
    9701                 :        1393 :   return tree_list_freelist_head;
    9702                 :             : }
    9703                 :             : 
    9704                 :             : /* Point to the first object in the tinst_level freelist.  */
    9705                 :             : static GTY((deletable)) tinst_level *tinst_level_freelist_head;
    9706                 :             : /* Return the/an actual tinst_level freelist.  */
    9707                 :             : static inline freelist<tinst_level>
    9708                 :  1190316464 : tinst_level_freelist ()
    9709                 :             : {
    9710                 :  1190316464 :   return tinst_level_freelist_head;
    9711                 :             : }
    9712                 :             : 
    9713                 :             : /* Point to the first object in the pending_template freelist.  */
    9714                 :             : static GTY((deletable)) pending_template *pending_template_freelist_head;
    9715                 :             : /* Return the/an actual pending_template freelist.  */
    9716                 :             : static inline freelist<pending_template>
    9717                 :    29464110 : pending_template_freelist ()
    9718                 :             : {
    9719                 :    29464110 :   return pending_template_freelist_head;
    9720                 :             : }
    9721                 :             : 
    9722                 :             : /* Build the TREE_LIST object out of a split list, store it
    9723                 :             :    permanently, and return it.  */
    9724                 :             : tree
    9725                 :         927 : tinst_level::to_list ()
    9726                 :             : {
    9727                 :         927 :   gcc_assert (split_list_p ());
    9728                 :         927 :   tree ret = tree_list_freelist ().alloc ();
    9729                 :         927 :   TREE_PURPOSE (ret) = tldcl;
    9730                 :         927 :   TREE_VALUE (ret) = targs;
    9731                 :         927 :   tldcl = ret;
    9732                 :         927 :   targs = NULL;
    9733                 :         927 :   gcc_assert (tree_list_p ());
    9734                 :         927 :   return ret;
    9735                 :             : }
    9736                 :             : 
    9737                 :             : const unsigned short tinst_level::refcount_infinity;
    9738                 :             : 
    9739                 :             : /* Increment OBJ's refcount unless it is already infinite.  */
    9740                 :             : static tinst_level *
    9741                 :  1835705488 : inc_refcount_use (tinst_level *obj)
    9742                 :             : {
    9743                 :  1327188966 :   if (obj && obj->refcount != tinst_level::refcount_infinity)
    9744                 :  1327188966 :     ++obj->refcount;
    9745                 :  1835705488 :   return obj;
    9746                 :             : }
    9747                 :             : 
    9748                 :             : /* Release storage for OBJ and node, if it's a TREE_LIST.  */
    9749                 :             : void
    9750                 :   594674043 : tinst_level::free (tinst_level *obj)
    9751                 :             : {
    9752                 :   594674043 :   if (obj->tree_list_p ())
    9753                 :         466 :     tree_list_freelist ().free (obj->get_node ());
    9754                 :   594674043 :   tinst_level_freelist ().free (obj);
    9755                 :   594674043 : }
    9756                 :             : 
    9757                 :             : /* Decrement OBJ's refcount if not infinite.  If it reaches zero, release
    9758                 :             :    OBJ's DECL and OBJ, and start over with the tinst_level object that
    9759                 :             :    used to be referenced by OBJ's NEXT.  */
    9760                 :             : static void
    9761                 :  1867155549 : dec_refcount_use (tinst_level *obj)
    9762                 :             : {
    9763                 :  1867155549 :   while (obj
    9764                 :  1325921075 :          && obj->refcount != tinst_level::refcount_infinity
    9765                 :  3787750667 :          && !--obj->refcount)
    9766                 :             :     {
    9767                 :   594674043 :       tinst_level *next = obj->next;
    9768                 :   594674043 :       tinst_level::free (obj);
    9769                 :   594674043 :       obj = next;
    9770                 :             :     }
    9771                 :  1867155549 : }
    9772                 :             : 
    9773                 :             : /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
    9774                 :             :    and of the former PTR.  Omitting the second argument is equivalent
    9775                 :             :    to passing (T*)NULL; this is allowed because passing the
    9776                 :             :    zero-valued integral constant NULL confuses type deduction and/or
    9777                 :             :    overload resolution.  */
    9778                 :             : template <typename T>
    9779                 :             : static void
    9780                 :  1867155549 : set_refcount_ptr (T *& ptr, T *obj = NULL)
    9781                 :             : {
    9782                 :  1835705488 :   T *save = ptr;
    9783                 :  1867155549 :   ptr = inc_refcount_use (obj);
    9784                 :  1835705488 :   dec_refcount_use (save);
    9785                 :    16931048 : }
    9786                 :             : 
    9787                 :             : static void
    9788                 :    18776578 : add_pending_template (tree d)
    9789                 :             : {
    9790                 :    18776578 :   tree ti = (TYPE_P (d)
    9791                 :    18776578 :              ? CLASSTYPE_TEMPLATE_INFO (d)
    9792                 :    18776578 :              : DECL_TEMPLATE_INFO (d));
    9793                 :    18776578 :   struct pending_template *pt;
    9794                 :    18776578 :   int level;
    9795                 :             : 
    9796                 :    18776578 :   if (TI_PENDING_TEMPLATE_FLAG (ti))
    9797                 :             :     return;
    9798                 :             : 
    9799                 :             :   /* We are called both from instantiate_decl, where we've already had a
    9800                 :             :      tinst_level pushed, and instantiate_template, where we haven't.
    9801                 :             :      Compensate.  */
    9802                 :    14945097 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
    9803                 :    29890194 :   level = !current_tinst_level
    9804                 :    29890194 :     || current_tinst_level->maybe_get_node () != d;
    9805                 :             : 
    9806                 :           0 :   if (level)
    9807                 :           0 :     push_tinst_level (d);
    9808                 :             : 
    9809                 :    14945097 :   pt = pending_template_freelist ().alloc ();
    9810                 :    14945097 :   pt->next = NULL;
    9811                 :    14945097 :   pt->tinst = NULL;
    9812                 :    14945097 :   set_refcount_ptr (pt->tinst, current_tinst_level);
    9813                 :    14945097 :   if (last_pending_template)
    9814                 :    14915527 :     last_pending_template->next = pt;
    9815                 :             :   else
    9816                 :       29570 :     pending_templates = pt;
    9817                 :             : 
    9818                 :    14945097 :   last_pending_template = pt;
    9819                 :             : 
    9820                 :    14945097 :   TI_PENDING_TEMPLATE_FLAG (ti) = 1;
    9821                 :             : 
    9822                 :    14945097 :   if (level)
    9823                 :           0 :     pop_tinst_level ();
    9824                 :             : }
    9825                 :             : 
    9826                 :             : 
    9827                 :             : /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
    9828                 :             :    ARGLIST.  Valid choices for FNS are given in the cp-tree.def
    9829                 :             :    documentation for TEMPLATE_ID_EXPR.  */
    9830                 :             : 
    9831                 :             : tree
    9832                 :    41617698 : lookup_template_function (tree fns, tree arglist)
    9833                 :             : {
    9834                 :    41617698 :   if (fns == error_mark_node || arglist == error_mark_node)
    9835                 :             :     return error_mark_node;
    9836                 :             : 
    9837                 :    41617675 :   gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
    9838                 :             : 
    9839                 :    41617675 :   if (!is_overloaded_fn (fns) && !identifier_p (fns))
    9840                 :             :     {
    9841                 :           4 :       error ("%q#D is not a function template", fns);
    9842                 :           4 :       return error_mark_node;
    9843                 :             :     }
    9844                 :             : 
    9845                 :    41617671 :   if (BASELINK_P (fns))
    9846                 :             :     {
    9847                 :     3592237 :       fns = copy_node (fns);
    9848                 :     7184474 :       BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
    9849                 :             :                                          unknown_type_node,
    9850                 :     3592237 :                                          BASELINK_FUNCTIONS (fns),
    9851                 :             :                                          arglist);
    9852                 :     3592237 :       return fns;
    9853                 :             :     }
    9854                 :             : 
    9855                 :    38025434 :   return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
    9856                 :             : }
    9857                 :             : 
    9858                 :             : /* Within the scope of a template class S<T>, the name S gets bound
    9859                 :             :    (in build_self_reference) to a TYPE_DECL for the class, not a
    9860                 :             :    TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
    9861                 :             :    or one of its enclosing classes, and that type is a template,
    9862                 :             :    return the associated TEMPLATE_DECL.  Otherwise, the original
    9863                 :             :    DECL is returned.
    9864                 :             : 
    9865                 :             :    Also handle the case when DECL is a TREE_LIST of ambiguous
    9866                 :             :    injected-class-names from different bases.  */
    9867                 :             : 
    9868                 :             : tree
    9869                 :   550960289 : maybe_get_template_decl_from_type_decl (tree decl)
    9870                 :             : {
    9871                 :   550960289 :   if (decl == NULL_TREE)
    9872                 :             :     return decl;
    9873                 :             : 
    9874                 :             :   /* DR 176: A lookup that finds an injected-class-name (10.2
    9875                 :             :      [class.member.lookup]) can result in an ambiguity in certain cases
    9876                 :             :      (for example, if it is found in more than one base class). If all of
    9877                 :             :      the injected-class-names that are found refer to specializations of
    9878                 :             :      the same class template, and if the name is followed by a
    9879                 :             :      template-argument-list, the reference refers to the class template
    9880                 :             :      itself and not a specialization thereof, and is not ambiguous.  */
    9881                 :   550959916 :   if (TREE_CODE (decl) == TREE_LIST)
    9882                 :             :     {
    9883                 :             :       tree t, tmpl = NULL_TREE;
    9884                 :       36914 :       for (t = decl; t; t = TREE_CHAIN (t))
    9885                 :             :         {
    9886                 :       36830 :           tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
    9887                 :       36830 :           if (!tmpl)
    9888                 :             :             tmpl = elt;
    9889                 :       18415 :           else if (tmpl != elt)
    9890                 :             :             break;
    9891                 :             :         }
    9892                 :       18415 :       if (tmpl && t == NULL_TREE)
    9893                 :             :         return tmpl;
    9894                 :             :       else
    9895                 :             :         return decl;
    9896                 :             :     }
    9897                 :             : 
    9898                 :   550941501 :   return (decl != NULL_TREE
    9899                 :   550941501 :           && DECL_SELF_REFERENCE_P (decl)
    9900                 :     5690048 :           && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
    9901                 :     5690048 :     ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
    9902                 :             : }
    9903                 :             : 
    9904                 :             : /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
    9905                 :             :    parameters, find the desired type.
    9906                 :             : 
    9907                 :             :    D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
    9908                 :             : 
    9909                 :             :    IN_DECL, if non-NULL, is the template declaration we are trying to
    9910                 :             :    instantiate.
    9911                 :             : 
    9912                 :             :    If ENTERING_SCOPE is nonzero, we are about to enter the scope of
    9913                 :             :    the class we are looking up.
    9914                 :             : 
    9915                 :             :    Issue error and warning messages under control of COMPLAIN.
    9916                 :             : 
    9917                 :             :    If the template class is really a local class in a template
    9918                 :             :    function, then the FUNCTION_CONTEXT is the function in which it is
    9919                 :             :    being instantiated.
    9920                 :             : 
    9921                 :             :    ??? Note that this function is currently called *twice* for each
    9922                 :             :    template-id: the first time from the parser, while creating the
    9923                 :             :    incomplete type (finish_template_type), and the second type during the
    9924                 :             :    real instantiation (instantiate_template_class). This is surely something
    9925                 :             :    that we want to avoid. It also causes some problems with argument
    9926                 :             :    coercion (see convert_nontype_argument for more information on this).  */
    9927                 :             : 
    9928                 :             : tree
    9929                 :   825628873 : lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
    9930                 :             :                        int entering_scope, tsubst_flags_t complain)
    9931                 :             : {
    9932                 :   825628873 :   auto_timevar tv (TV_TEMPLATE_INST);
    9933                 :             : 
    9934                 :   825628873 :   tree templ = NULL_TREE, parmlist;
    9935                 :   825628873 :   tree t;
    9936                 :   825628873 :   spec_entry **slot;
    9937                 :   825628873 :   spec_entry *entry;
    9938                 :   825628873 :   spec_entry elt;
    9939                 :   825628873 :   hashval_t hash;
    9940                 :             : 
    9941                 :   825628873 :   if (identifier_p (d1))
    9942                 :             :     {
    9943                 :       67040 :       tree value = innermost_non_namespace_value (d1);
    9944                 :       67040 :       if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
    9945                 :             :         templ = value;
    9946                 :             :       else
    9947                 :             :         {
    9948                 :       67040 :           if (context)
    9949                 :       67040 :             push_decl_namespace (context);
    9950                 :       67040 :           templ = lookup_name (d1);
    9951                 :       67040 :           templ = maybe_get_template_decl_from_type_decl (templ);
    9952                 :       67040 :           if (context)
    9953                 :       67040 :             pop_decl_namespace ();
    9954                 :             :         }
    9955                 :             :     }
    9956                 :   825561833 :   else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
    9957                 :             :     {
    9958                 :           0 :       tree type = TREE_TYPE (d1);
    9959                 :             : 
    9960                 :             :       /* If we are declaring a constructor, say A<T>::A<T>, we will get
    9961                 :             :          an implicit typename for the second A.  Deal with it.  */
    9962                 :           0 :       if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
    9963                 :           0 :         type = TREE_TYPE (type);
    9964                 :             : 
    9965                 :           0 :       if (CLASSTYPE_TEMPLATE_INFO (type))
    9966                 :             :         {
    9967                 :           0 :           templ = CLASSTYPE_TI_TEMPLATE (type);
    9968                 :           0 :           d1 = DECL_NAME (templ);
    9969                 :             :         }
    9970                 :             :     }
    9971                 :   825561833 :   else if (TREE_CODE (d1) == ENUMERAL_TYPE
    9972                 :   825561833 :            || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
    9973                 :             :     {
    9974                 :   669613479 :       templ = TYPE_TI_TEMPLATE (d1);
    9975                 :   669613479 :       d1 = DECL_NAME (templ);
    9976                 :             :     }
    9977                 :   155948354 :   else if (DECL_TYPE_TEMPLATE_P (d1))
    9978                 :             :     {
    9979                 :   155948354 :       templ = d1;
    9980                 :   155948354 :       d1 = DECL_NAME (templ);
    9981                 :             :     }
    9982                 :           0 :   else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
    9983                 :             :     {
    9984                 :           0 :       templ = d1;
    9985                 :           0 :       d1 = DECL_NAME (templ);
    9986                 :             :     }
    9987                 :             : 
    9988                 :             :   /* Issue an error message if we didn't find a template.  */
    9989                 :   825628873 :   if (! templ)
    9990                 :             :     {
    9991                 :         373 :       if (complain & tf_error)
    9992                 :           0 :         error ("%qT is not a template", d1);
    9993                 :         373 :       return error_mark_node;
    9994                 :             :     }
    9995                 :             : 
    9996                 :   825628500 :   if (TREE_CODE (templ) != TEMPLATE_DECL
    9997                 :             :          /* Make sure it's a user visible template, if it was named by
    9998                 :             :             the user.  */
    9999                 :   825628500 :       || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
   10000                 :   155413809 :           && !PRIMARY_TEMPLATE_P (templ)))
   10001                 :             :     {
   10002                 :           0 :       if (complain & tf_error)
   10003                 :             :         {
   10004                 :           0 :           error ("non-template type %qT used as a template", d1);
   10005                 :           0 :           if (in_decl)
   10006                 :           0 :             error ("for template declaration %q+D", in_decl);
   10007                 :             :         }
   10008                 :           0 :       return error_mark_node;
   10009                 :             :     }
   10010                 :             : 
   10011                 :   825628500 :   complain &= ~tf_user;
   10012                 :             : 
   10013                 :             :   /* An alias that just changes the name of a template is equivalent to the
   10014                 :             :      other template, so if any of the arguments are pack expansions, strip
   10015                 :             :      the alias to avoid problems with a pack expansion passed to a non-pack
   10016                 :             :      alias template parameter (DR 1430).  */
   10017                 :   825628500 :   if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
   10018                 :     7423418 :     templ = get_underlying_template (templ);
   10019                 :             : 
   10020                 :   825628500 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
   10021                 :             :     {
   10022                 :      222162 :       tree parm;
   10023                 :      222162 :       tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
   10024                 :      222162 :       if (arglist2 == error_mark_node
   10025                 :      222162 :           || (!uses_template_parms (arglist2)
   10026                 :        8639 :               && check_instantiated_args (templ, arglist2, complain)))
   10027                 :          23 :         return error_mark_node;
   10028                 :             : 
   10029                 :      222139 :       parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
   10030                 :      222139 :       return parm;
   10031                 :             :     }
   10032                 :             :   else
   10033                 :             :     {
   10034                 :   825406338 :       tree template_type = TREE_TYPE (templ);
   10035                 :   825406338 :       tree gen_tmpl;
   10036                 :   825406338 :       tree type_decl;
   10037                 :   825406338 :       tree found = NULL_TREE;
   10038                 :   825406338 :       int arg_depth;
   10039                 :   825406338 :       int parm_depth;
   10040                 :   825406338 :       int is_dependent_type;
   10041                 :   825406338 :       int use_partial_inst_tmpl = false;
   10042                 :             : 
   10043                 :   825406338 :       if (template_type == error_mark_node)
   10044                 :             :         /* An error occurred while building the template TEMPL, and a
   10045                 :             :            diagnostic has most certainly been emitted for that
   10046                 :             :            already.  Let's propagate that error.  */
   10047                 :             :         return error_mark_node;
   10048                 :             : 
   10049                 :   825406337 :       gen_tmpl = most_general_template (templ);
   10050                 :   825406337 :       if (modules_p ())
   10051                 :     3842141 :         lazy_load_pendings (gen_tmpl);
   10052                 :             : 
   10053                 :   825406337 :       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
   10054                 :   825406337 :       parm_depth = TMPL_PARMS_DEPTH (parmlist);
   10055                 :  1650812674 :       arg_depth = TMPL_ARGS_DEPTH (arglist);
   10056                 :             : 
   10057                 :   825406337 :       if (arg_depth == 1 && parm_depth > 1)
   10058                 :             :         {
   10059                 :             :           /* We've been given an incomplete set of template arguments.
   10060                 :             :              For example, given:
   10061                 :             : 
   10062                 :             :                template <class T> struct S1 {
   10063                 :             :                  template <class U> struct S2 {};
   10064                 :             :                  template <class U> struct S2<U*> {};
   10065                 :             :                 };
   10066                 :             : 
   10067                 :             :              we will be called with an ARGLIST of `U*', but the
   10068                 :             :              TEMPLATE will be `template <class T> template
   10069                 :             :              <class U> struct S1<T>::S2'.  We must fill in the missing
   10070                 :             :              arguments.  */
   10071                 :     6618757 :           tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
   10072                 :     6618757 :           arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
   10073                 :    13237514 :           arg_depth = TMPL_ARGS_DEPTH (arglist);
   10074                 :             :         }
   10075                 :             : 
   10076                 :             :       /* Now we should have enough arguments.  */
   10077                 :   825406337 :       gcc_assert (parm_depth == arg_depth);
   10078                 :             : 
   10079                 :   825406337 :       if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
   10080                 :             :         {
   10081                 :             :           /* The user referred to a specialization of an alias
   10082                 :             :             template represented by GEN_TMPL.
   10083                 :             : 
   10084                 :             :             [temp.alias]/2 says:
   10085                 :             : 
   10086                 :             :                 When a template-id refers to the specialization of an
   10087                 :             :                 alias template, it is equivalent to the associated
   10088                 :             :                 type obtained by substitution of its
   10089                 :             :                 template-arguments for the template-parameters in the
   10090                 :             :                 type-id of the alias template.  */
   10091                 :             : 
   10092                 :    22757120 :           t = instantiate_alias_template (gen_tmpl, arglist, complain);
   10093                 :             :           /* Note that the call above (by indirectly calling
   10094                 :             :              register_specialization in tsubst_decl) registers the
   10095                 :             :              TYPE_DECL representing the specialization of the alias
   10096                 :             :              template.  So next time someone substitutes ARGLIST for
   10097                 :             :              the template parms into the alias template (GEN_TMPL),
   10098                 :             :              she'll get that TYPE_DECL back.  */
   10099                 :             : 
   10100                 :    22757120 :           if (t == error_mark_node)
   10101                 :             :             return error_mark_node;
   10102                 :    22751567 :           return TREE_TYPE (t);
   10103                 :             :         }
   10104                 :             : 
   10105                 :             :       /* From here on, we're only interested in the most general
   10106                 :             :          template.  */
   10107                 :             : 
   10108                 :             :       /* Shortcut looking up the current class scope again.  */
   10109                 :   802649217 :       for (tree cur = current_nonlambda_class_type ();
   10110                 :  1389275342 :            cur != NULL_TREE;
   10111                 :   586626125 :            cur = get_containing_scope (cur))
   10112                 :             :         {
   10113                 :  1035393310 :           if (!CLASS_TYPE_P (cur))
   10114                 :   399236608 :             continue;
   10115                 :             : 
   10116                 :   636156702 :           tree ti = CLASSTYPE_TEMPLATE_INFO (cur);
   10117                 :  1820185829 :           if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti)))
   10118                 :             :             break;
   10119                 :             : 
   10120                 :   590993864 :           if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
   10121                 :  1009851772 :               && comp_template_args (arglist, TI_ARGS (ti)))
   10122                 :   403604347 :             return cur;
   10123                 :             :         }
   10124                 :             : 
   10125                 :             :       /* Calculate the BOUND_ARGS.  These will be the args that are
   10126                 :             :          actually tsubst'd into the definition to create the
   10127                 :             :          instantiation.  */
   10128                 :   399044870 :       if (PRIMARY_TEMPLATE_P (gen_tmpl))
   10129                 :   393656339 :         arglist = coerce_template_parms (parmlist, arglist, gen_tmpl, complain);
   10130                 :             : 
   10131                 :   399044867 :       if (arglist == error_mark_node)
   10132                 :             :         /* We were unable to bind the arguments.  */
   10133                 :             :         return error_mark_node;
   10134                 :             : 
   10135                 :             :       /* In the scope of a template class, explicit references to the
   10136                 :             :          template class refer to the type of the template, not any
   10137                 :             :          instantiation of it.  For example, in:
   10138                 :             : 
   10139                 :             :            template <class T> class C { void f(C<T>); }
   10140                 :             : 
   10141                 :             :          the `C<T>' is just the same as `C'.  Outside of the
   10142                 :             :          class, however, such a reference is an instantiation.  */
   10143                 :   399043995 :       if (entering_scope
   10144                 :   273701775 :           || !PRIMARY_TEMPLATE_P (gen_tmpl)
   10145                 :   667719230 :           || currently_open_class (template_type))
   10146                 :             :         {
   10147                 :   133222134 :           tree tinfo = TYPE_TEMPLATE_INFO (template_type);
   10148                 :             : 
   10149                 :   266444268 :           if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
   10150                 :             :             return template_type;
   10151                 :             :         }
   10152                 :             : 
   10153                 :             :       /* If we already have this specialization, return it.  */
   10154                 :   385057174 :       elt.tmpl = gen_tmpl;
   10155                 :   385057174 :       elt.args = arglist;
   10156                 :   385057174 :       elt.spec = NULL_TREE;
   10157                 :   385057174 :       hash = spec_hasher::hash (&elt);
   10158                 :   385057174 :       entry = type_specializations->find_with_hash (&elt, hash);
   10159                 :             : 
   10160                 :   385057174 :       if (entry)
   10161                 :   297624570 :         return entry->spec;
   10162                 :             : 
   10163                 :             :       /* If the template's constraints are not satisfied,
   10164                 :             :          then we cannot form a valid type.
   10165                 :             : 
   10166                 :             :          Note that the check is deferred until after the hash
   10167                 :             :          lookup. This prevents redundant checks on previously
   10168                 :             :          instantiated specializations. */
   10169                 :    87432604 :       if (flag_concepts
   10170                 :    87432604 :           && !constraints_satisfied_p (gen_tmpl, arglist))
   10171                 :             :         {
   10172                 :         632 :           if (complain & tf_error)
   10173                 :             :             {
   10174                 :          33 :               auto_diagnostic_group d;
   10175                 :          33 :               error ("template constraint failure for %qD", gen_tmpl);
   10176                 :          33 :               diagnose_constraints (input_location, gen_tmpl, arglist);
   10177                 :          33 :             }
   10178                 :         632 :           return error_mark_node;
   10179                 :             :         }
   10180                 :             : 
   10181                 :    87431972 :       is_dependent_type = uses_template_parms (arglist);
   10182                 :             : 
   10183                 :             :       /* If the deduced arguments are invalid, then the binding
   10184                 :             :          failed.  */
   10185                 :    87431972 :       if (!is_dependent_type
   10186                 :    87431972 :           && check_instantiated_args (gen_tmpl,
   10187                 :             :                                       INNERMOST_TEMPLATE_ARGS (arglist),
   10188                 :             :                                       complain))
   10189                 :          10 :         return error_mark_node;
   10190                 :             : 
   10191                 :    87431962 :       if (!is_dependent_type
   10192                 :    36174909 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   10193                 :     2661350 :           && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
   10194                 :    88959284 :           && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
   10195                 :             :         /* This occurs when the user has tried to define a tagged type
   10196                 :             :            in a scope that forbids it.  We emitted an error during the
   10197                 :             :            parse.  We didn't complete the bail out then, so here we
   10198                 :             :            are.  */
   10199                 :          14 :         return error_mark_node;
   10200                 :             : 
   10201                 :    87431948 :       context = DECL_CONTEXT (gen_tmpl);
   10202                 :    87431948 :       if (context && TYPE_P (context))
   10203                 :             :         {
   10204                 :     4306270 :           if (!uses_template_parms (DECL_CONTEXT (templ)))
   10205                 :             :             /* If the context of the partially instantiated template is
   10206                 :             :                already non-dependent, then we might as well use it.  */
   10207                 :     1616760 :             context = DECL_CONTEXT (templ);
   10208                 :             :           else
   10209                 :             :             {
   10210                 :     2689510 :               context = tsubst_aggr_type (context, arglist,
   10211                 :             :                                           complain, in_decl, true);
   10212                 :             :               /* Try completing the enclosing context if it's not already so.  */
   10213                 :     2689510 :               if (context != error_mark_node
   10214                 :     2689510 :                   && !COMPLETE_TYPE_P (context))
   10215                 :             :                 {
   10216                 :     2525134 :                   context = complete_type (context);
   10217                 :     2525134 :                   if (COMPLETE_TYPE_P (context))
   10218                 :             :                     {
   10219                 :             :                       /* Completion could have caused us to register the desired
   10220                 :             :                          specialization already, so check the table again.  */
   10221                 :           6 :                       entry = type_specializations->find_with_hash (&elt, hash);
   10222                 :           6 :                       if (entry)
   10223                 :           6 :                         return entry->spec;
   10224                 :             :                     }
   10225                 :             :                 }
   10226                 :             :             }
   10227                 :             :         }
   10228                 :             :       else
   10229                 :    83125678 :         context = tsubst (context, arglist, complain, in_decl);
   10230                 :             : 
   10231                 :    87431942 :       if (context == error_mark_node)
   10232                 :             :         return error_mark_node;
   10233                 :             : 
   10234                 :    87431938 :       if (!context)
   10235                 :           0 :         context = global_namespace;
   10236                 :             : 
   10237                 :             :       /* Create the type.  */
   10238                 :    87431938 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE)
   10239                 :             :         {
   10240                 :      307179 :           if (!is_dependent_type)
   10241                 :             :             {
   10242                 :      307176 :               set_current_access_from_decl (TYPE_NAME (template_type));
   10243                 :      614352 :               t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
   10244                 :      307176 :                               tsubst (ENUM_UNDERLYING_TYPE (template_type),
   10245                 :             :                                       arglist, complain, in_decl),
   10246                 :      307176 :                               tsubst_attributes (TYPE_ATTRIBUTES (template_type),
   10247                 :             :                                                  arglist, complain, in_decl),
   10248                 :      307176 :                               SCOPED_ENUM_P (template_type), NULL);
   10249                 :             : 
   10250                 :      307176 :               if (t == error_mark_node)
   10251                 :             :                 return t;
   10252                 :             :             }
   10253                 :             :           else
   10254                 :             :             {
   10255                 :             :               /* We don't want to call start_enum for this type, since
   10256                 :             :                  the values for the enumeration constants may involve
   10257                 :             :                  template parameters.  And, no one should be interested
   10258                 :             :                  in the enumeration constants for such a type.  */
   10259                 :           3 :               t = cxx_make_type (ENUMERAL_TYPE);
   10260                 :           6 :               SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
   10261                 :             :             }
   10262                 :      614312 :           SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
   10263                 :      614342 :           ENUM_FIXED_UNDERLYING_TYPE_P (t)
   10264                 :      307171 :             = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
   10265                 :             :         }
   10266                 :    87124759 :       else if (CLASS_TYPE_P (template_type))
   10267                 :             :         {
   10268                 :             :           /* Lambda closures are regenerated in tsubst_lambda_expr, not
   10269                 :             :              instantiated here.  */
   10270                 :   174105416 :           gcc_assert (!LAMBDA_TYPE_P (template_type));
   10271                 :             : 
   10272                 :    87124759 :           t = make_class_type (TREE_CODE (template_type));
   10273                 :   261374277 :           CLASSTYPE_DECLARED_CLASS (t)
   10274                 :    87124759 :             = CLASSTYPE_DECLARED_CLASS (template_type);
   10275                 :    87124759 :           SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
   10276                 :             : 
   10277                 :             :           /* A local class.  Make sure the decl gets registered properly.  */
   10278                 :    87124759 :           if (context == current_function_decl)
   10279                 :      518538 :             if (pushtag (DECL_NAME (gen_tmpl), t)
   10280                 :      518538 :                 == error_mark_node)
   10281                 :             :               return error_mark_node;
   10282                 :             : 
   10283                 :    87124743 :           if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
   10284                 :             :             /* This instantiation is another name for the primary
   10285                 :             :                template type. Set the TYPE_CANONICAL field
   10286                 :             :                appropriately. */
   10287                 :     4005174 :             TYPE_CANONICAL (t) = template_type;
   10288                 :    83119569 :           else if (any_template_arguments_need_structural_equality_p (arglist))
   10289                 :     1009564 :             SET_TYPE_STRUCTURAL_EQUALITY (t);
   10290                 :             :         }
   10291                 :             :       else
   10292                 :           0 :         gcc_unreachable ();
   10293                 :             : 
   10294                 :             :       /* If we called start_enum or pushtag above, this information
   10295                 :             :          will already be set up.  */
   10296                 :    87431914 :       type_decl = TYPE_NAME (t);
   10297                 :    87431914 :       if (!type_decl)
   10298                 :             :         {
   10299                 :    86606224 :           TYPE_CONTEXT (t) = FROB_CONTEXT (context);
   10300                 :             : 
   10301                 :    86606224 :           type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
   10302                 :    86606224 :           DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
   10303                 :   173212448 :           DECL_SOURCE_LOCATION (type_decl)
   10304                 :    86606224 :             = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
   10305                 :             :         }
   10306                 :             : 
   10307                 :    87431914 :       set_instantiating_module (type_decl);
   10308                 :             :       /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
   10309                 :             :          of export flag.  We want to propagate this because it might
   10310                 :             :          be a friend declaration that pushes a new hidden binding.  */
   10311                 :    87431914 :       DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
   10312                 :             : 
   10313                 :    87431914 :       if (CLASS_TYPE_P (template_type))
   10314                 :             :         {
   10315                 :    87124743 :           TREE_PRIVATE (type_decl)
   10316                 :    87124743 :             = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
   10317                 :    87124743 :           TREE_PROTECTED (type_decl)
   10318                 :    87124743 :             = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
   10319                 :    87124743 :           if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
   10320                 :             :             {
   10321                 :    86164901 :               DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
   10322                 :    86164901 :               DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
   10323                 :             :             }
   10324                 :             :         }
   10325                 :             : 
   10326                 :    87431914 :       if (OVERLOAD_TYPE_P (t))
   10327                 :             :         {
   10328                 :             :           static const char *tags[] = {"abi_tag", "may_alias"};
   10329                 :             : 
   10330                 :   262295742 :           for (unsigned ix = 0; ix != 2; ix++)
   10331                 :             :             {
   10332                 :   174863828 :               tree attributes
   10333                 :   174863828 :                 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
   10334                 :             : 
   10335                 :   174863828 :               if (attributes)
   10336                 :          64 :                 TYPE_ATTRIBUTES (t)
   10337                 :         128 :                   = tree_cons (TREE_PURPOSE (attributes),
   10338                 :          64 :                                TREE_VALUE (attributes),
   10339                 :          64 :                                TYPE_ATTRIBUTES (t));
   10340                 :             :             }
   10341                 :             :         }
   10342                 :             : 
   10343                 :             :       /* Let's consider the explicit specialization of a member
   10344                 :             :          of a class template specialization that is implicitly instantiated,
   10345                 :             :          e.g.:
   10346                 :             :              template<class T>
   10347                 :             :              struct S
   10348                 :             :              {
   10349                 :             :                template<class U> struct M {}; //#0
   10350                 :             :              };
   10351                 :             : 
   10352                 :             :              template<>
   10353                 :             :              template<>
   10354                 :             :              struct S<int>::M<char> //#1
   10355                 :             :              {
   10356                 :             :                int i;
   10357                 :             :              };
   10358                 :             :         [temp.expl.spec]/4 says this is valid.
   10359                 :             : 
   10360                 :             :         In this case, when we write:
   10361                 :             :         S<int>::M<char> m;
   10362                 :             : 
   10363                 :             :         M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
   10364                 :             :         the one of #0.
   10365                 :             : 
   10366                 :             :         When we encounter #1, we want to store the partial instantiation
   10367                 :             :         of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
   10368                 :             : 
   10369                 :             :         For all cases other than this "explicit specialization of member of a
   10370                 :             :         class template", we just want to store the most general template into
   10371                 :             :         the CLASSTYPE_TI_TEMPLATE of M.
   10372                 :             : 
   10373                 :             :         This case of "explicit specialization of member of a class template"
   10374                 :             :         only happens when:
   10375                 :             :         1/ the enclosing class is an instantiation of, and therefore not
   10376                 :             :         the same as, the context of the most general template, and
   10377                 :             :         2/ we aren't looking at the partial instantiation itself, i.e.
   10378                 :             :         the innermost arguments are not the same as the innermost parms of
   10379                 :             :         the most general template.
   10380                 :             : 
   10381                 :             :         So it's only when 1/ and 2/ happens that we want to use the partial
   10382                 :             :         instantiation of the member template in lieu of its most general
   10383                 :             :         template.  */
   10384                 :             : 
   10385                 :    87431914 :       if (PRIMARY_TEMPLATE_P (gen_tmpl)
   10386                 :   171809222 :           && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
   10387                 :             :           /* the enclosing class must be an instantiation...  */
   10388                 :     1909740 :           && CLASS_TYPE_P (context)
   10389                 :    89341487 :           && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
   10390                 :             :         {
   10391                 :     1304121 :           TREE_VEC_LENGTH (arglist)--;
   10392                 :     1304121 :           ++processing_template_decl;
   10393                 :     1304121 :           tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl));
   10394                 :     1304121 :           tree partial_inst_args =
   10395                 :     1304121 :             tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
   10396                 :             :                     arglist, complain, NULL_TREE);
   10397                 :     1304121 :           --processing_template_decl;
   10398                 :     1304121 :           TREE_VEC_LENGTH (arglist)++;
   10399                 :     1304121 :           if (partial_inst_args == error_mark_node)
   10400                 :             :             return error_mark_node;
   10401                 :     2608236 :           use_partial_inst_tmpl =
   10402                 :             :             /*...and we must not be looking at the partial instantiation
   10403                 :             :              itself. */
   10404                 :     1304118 :             !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
   10405                 :             :                                  partial_inst_args);
   10406                 :             :         }
   10407                 :             : 
   10408                 :     1304118 :       if (!use_partial_inst_tmpl)
   10409                 :             :         /* This case is easy; there are no member templates involved.  */
   10410                 :             :         found = gen_tmpl;
   10411                 :             :       else
   10412                 :             :         {
   10413                 :             :           /* This is a full instantiation of a member template.  Find
   10414                 :             :              the partial instantiation of which this is an instance.  */
   10415                 :             : 
   10416                 :             :           /* Temporarily reduce by one the number of levels in the ARGLIST
   10417                 :             :              so as to avoid comparing the last set of arguments.  */
   10418                 :      639128 :           TREE_VEC_LENGTH (arglist)--;
   10419                 :             :           /* We don't use COMPLAIN in the following call because this isn't
   10420                 :             :              the immediate context of deduction.  For instance, tf_partial
   10421                 :             :              could be set here as we might be at the beginning of template
   10422                 :             :              argument deduction when any explicitly specified template
   10423                 :             :              arguments are substituted into the function type.  tf_partial
   10424                 :             :              could lead into trouble because we wouldn't find the partial
   10425                 :             :              instantiation that might have been created outside tf_partial
   10426                 :             :              context, because the levels of template parameters wouldn't
   10427                 :             :              match, because in a tf_partial context, tsubst doesn't reduce
   10428                 :             :              TEMPLATE_PARM_LEVEL.  */
   10429                 :      639128 :           found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
   10430                 :      639128 :           TREE_VEC_LENGTH (arglist)++;
   10431                 :     1278256 :           found = (TREE_CODE (found) == TEMPLATE_DECL
   10432                 :      639128 :                    ? found
   10433                 :           0 :                    : CLASSTYPE_TI_TEMPLATE (found));
   10434                 :             : 
   10435                 :      639128 :           if (DECL_CLASS_TEMPLATE_P (found)
   10436                 :     1278256 :               && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
   10437                 :             :             {
   10438                 :             :               /* If this partial instantiation is specialized, we want to
   10439                 :             :                  use it for hash table lookup.  */
   10440                 :          12 :               elt.tmpl = found;
   10441                 :          12 :               elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
   10442                 :          12 :               hash = spec_hasher::hash (&elt);
   10443                 :             :             }
   10444                 :             :         }
   10445                 :             : 
   10446                 :             :       /* Build template info for the new specialization.  */
   10447                 :    87431911 :       SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
   10448                 :             : 
   10449                 :    87431911 :       elt.spec = t;
   10450                 :    87431911 :       slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
   10451                 :    87431911 :       gcc_checking_assert (*slot == NULL);
   10452                 :    87431911 :       entry = ggc_alloc<spec_entry> ();
   10453                 :    87431911 :       *entry = elt;
   10454                 :    87431911 :       *slot = entry;
   10455                 :             : 
   10456                 :             :       /* Note this use of the partial instantiation so we can check it
   10457                 :             :          later in maybe_process_partial_specialization.  */
   10458                 :   174863822 :       DECL_TEMPLATE_INSTANTIATIONS (found)
   10459                 :    87431911 :         = tree_cons (arglist, t,
   10460                 :    87431911 :                      DECL_TEMPLATE_INSTANTIATIONS (found));
   10461                 :             : 
   10462                 :    87431911 :       if (TREE_CODE (template_type) == ENUMERAL_TYPE
   10463                 :    87431911 :           && !uses_template_parms (current_nonlambda_scope ()))
   10464                 :             :         /* Now that the type has been registered on the instantiations
   10465                 :             :            list, we set up the enumerators.  Because the enumeration
   10466                 :             :            constants may involve the enumeration type itself, we make
   10467                 :             :            sure to register the type first, and then create the
   10468                 :             :            constants.  That way, doing tsubst_expr for the enumeration
   10469                 :             :            constants won't result in recursive calls here; we'll find
   10470                 :             :            the instantiation and exit above.  */
   10471                 :      307171 :         tsubst_enum (template_type, t, arglist);
   10472                 :             : 
   10473                 :    87431851 :       if (CLASS_TYPE_P (template_type) && is_dependent_type)
   10474                 :             :         /* If the type makes use of template parameters, the
   10475                 :             :            code that generates debugging information will crash.  */
   10476                 :    51257039 :         DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
   10477                 :             : 
   10478                 :             :       /* Possibly limit visibility based on template args.  */
   10479                 :    87431851 :       TREE_PUBLIC (type_decl) = 1;
   10480                 :    87431851 :       determine_visibility (type_decl);
   10481                 :             : 
   10482                 :    87431851 :       inherit_targ_abi_tags (t);
   10483                 :             : 
   10484                 :    87431851 :       return t;
   10485                 :             :     }
   10486                 :   825628810 : }
   10487                 :             : 
   10488                 :             : /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST.  */
   10489                 :             : 
   10490                 :             : tree
   10491                 :    11290789 : lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain)
   10492                 :             : {
   10493                 :    11290789 :   if (flag_concepts && variable_concept_p (templ))
   10494                 :           0 :     return build_concept_check (templ, arglist, tf_none);
   10495                 :             : 
   10496                 :    11290789 :   tree gen_templ = most_general_template (templ);
   10497                 :    11290789 :   tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ);
   10498                 :    11290789 :   arglist = add_outermost_template_args (templ, arglist);
   10499                 :    11290789 :   arglist = coerce_template_parms (parms, arglist, templ, complain);
   10500                 :    11290789 :   if (arglist == error_mark_node)
   10501                 :             :     return error_mark_node;
   10502                 :             : 
   10503                 :             :   /* The type of the expression is NULL_TREE since the template-id could refer
   10504                 :             :      to an explicit or partial specialization. */
   10505                 :    11290759 :   return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
   10506                 :             : }
   10507                 :             : 
   10508                 :             : /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's
   10509                 :             :    not dependent.  */
   10510                 :             : 
   10511                 :             : tree
   10512                 :    10483458 : finish_template_variable (tree var, tsubst_flags_t complain)
   10513                 :             : {
   10514                 :    10483458 :   tree templ = TREE_OPERAND (var, 0);
   10515                 :    10483458 :   tree arglist = TREE_OPERAND (var, 1);
   10516                 :             : 
   10517                 :             :   /* If the template or arguments are dependent, then we
   10518                 :             :      can't resolve the TEMPLATE_ID_EXPR yet.  */
   10519                 :    10483458 :   if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1
   10520                 :    10483458 :       || any_dependent_template_arguments_p (arglist))
   10521                 :     5342528 :     return var;
   10522                 :             : 
   10523                 :     5140930 :   if (flag_concepts && !constraints_satisfied_p (templ, arglist))
   10524                 :             :     {
   10525                 :           6 :       if (complain & tf_error)
   10526                 :             :         {
   10527                 :           5 :           auto_diagnostic_group d;
   10528                 :           5 :           error ("use of invalid variable template %qE", var);
   10529                 :           5 :           diagnose_constraints (location_of (var), templ, arglist);
   10530                 :           5 :         }
   10531                 :           6 :       return error_mark_node;
   10532                 :             :     }
   10533                 :             : 
   10534                 :     5140924 :   return instantiate_template (templ, arglist, complain);
   10535                 :             : }
   10536                 :             : 
   10537                 :             : /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
   10538                 :             :    TARGS template args, and instantiate it if it's not dependent.  */
   10539                 :             : 
   10540                 :             : tree
   10541                 :     5890897 : lookup_and_finish_template_variable (tree templ, tree targs,
   10542                 :             :                                      tsubst_flags_t complain)
   10543                 :             : {
   10544                 :     5890897 :   tree var = lookup_template_variable (templ, targs, complain);
   10545                 :     5890897 :   if (var == error_mark_node)
   10546                 :             :     return error_mark_node;
   10547                 :     5890897 :   var = finish_template_variable (var, complain);
   10548                 :     5890897 :   mark_used (var, complain);
   10549                 :     5890897 :   return var;
   10550                 :             : }
   10551                 :             : 
   10552                 :             : /* If the set of template parameters PARMS contains a template parameter
   10553                 :             :    at the given LEVEL and INDEX, then return this parameter.  Otherwise
   10554                 :             :    return NULL_TREE.  */
   10555                 :             : 
   10556                 :             : static tree
   10557                 :    19557710 : corresponding_template_parameter_list (tree parms, int level, int index)
   10558                 :             : {
   10559                 :    20977665 :   while (TMPL_PARMS_DEPTH (parms) > level)
   10560                 :     1419955 :     parms = TREE_CHAIN (parms);
   10561                 :             : 
   10562                 :    19557710 :   if (TMPL_PARMS_DEPTH (parms) != level
   10563                 :    19557710 :       || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
   10564                 :             :     return NULL_TREE;
   10565                 :             : 
   10566                 :    19061813 :   return TREE_VEC_ELT (TREE_VALUE (parms), index);
   10567                 :             : }
   10568                 :             : 
   10569                 :             : /* Return the TREE_LIST for the template parameter from PARMS that positionally
   10570                 :             :    corresponds to the template parameter PARM, or else return NULL_TREE.  */
   10571                 :             : 
   10572                 :             : static tree
   10573                 :    19557710 : corresponding_template_parameter_list (tree parms, tree parm)
   10574                 :             : {
   10575                 :    19557710 :   int level, index;
   10576                 :    19557710 :   template_parm_level_and_index (parm, &level, &index);
   10577                 :    19557710 :   return corresponding_template_parameter_list (parms, level, index);
   10578                 :             : }
   10579                 :             : 
   10580                 :             : /* As above, but pull out the actual parameter.  */
   10581                 :             : 
   10582                 :             : static tree
   10583                 :    19557548 : corresponding_template_parameter (tree parms, tree parm)
   10584                 :             : {
   10585                 :    19557548 :   tree list = corresponding_template_parameter_list (parms, parm);
   10586                 :    19557548 :   if (!list)
   10587                 :             :     return NULL_TREE;
   10588                 :             : 
   10589                 :    19061651 :   tree t = TREE_VALUE (list);
   10590                 :             :   /* As in template_parm_to_arg.  */
   10591                 :    19061651 :   if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
   10592                 :    18501807 :     t = TREE_TYPE (t);
   10593                 :             :   else
   10594                 :      559844 :     t = DECL_INITIAL (t);
   10595                 :             : 
   10596                 :    19061651 :   gcc_assert (TEMPLATE_PARM_P (t));
   10597                 :             :   return t;
   10598                 :             : }
   10599                 :             : 
   10600                 :             : struct pair_fn_data
   10601                 :             : {
   10602                 :             :   tree_fn_t fn;
   10603                 :             :   tree_fn_t any_fn;
   10604                 :             :   void *data;
   10605                 :             :   /* True when we should also visit template parameters that occur in
   10606                 :             :      non-deduced contexts.  */
   10607                 :             :   bool include_nondeduced_p;
   10608                 :             :   hash_set<tree> *visited;
   10609                 :             : };
   10610                 :             : 
   10611                 :             : /* Called from for_each_template_parm via walk_tree.  */
   10612                 :             : 
   10613                 :             : static tree
   10614                 :   380080924 : for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
   10615                 :             : {
   10616                 :   380080924 :   tree t = *tp;
   10617                 :   380080924 :   struct pair_fn_data *pfd = (struct pair_fn_data *) d;
   10618                 :   380080924 :   tree_fn_t fn = pfd->fn;
   10619                 :   380080924 :   void *data = pfd->data;
   10620                 :   380080924 :   tree result = NULL_TREE;
   10621                 :             : 
   10622                 :             : #define WALK_SUBTREE(NODE)                                              \
   10623                 :             :   do                                                                    \
   10624                 :             :     {                                                                   \
   10625                 :             :       result = for_each_template_parm (NODE, fn, data, pfd->visited, \
   10626                 :             :                                        pfd->include_nondeduced_p,    \
   10627                 :             :                                        pfd->any_fn);                 \
   10628                 :             :       if (result) goto out;                                             \
   10629                 :             :     }                                                                   \
   10630                 :             :   while (0)
   10631                 :             : 
   10632                 :   380080924 :   if (pfd->any_fn && (*pfd->any_fn)(t, data))
   10633                 :             :     return t;
   10634                 :             : 
   10635                 :   380080924 :   if (TYPE_P (t)
   10636                 :   179280058 :       && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
   10637                 :   178827722 :     WALK_SUBTREE (TYPE_CONTEXT (t));
   10638                 :             : 
   10639                 :   379655464 :   switch (TREE_CODE (t))
   10640                 :             :     {
   10641                 :    29887216 :     case RECORD_TYPE:
   10642                 :    29887216 :       if (TYPE_PTRMEMFUNC_P (t))
   10643                 :             :         break;
   10644                 :             :       /* Fall through.  */
   10645                 :             : 
   10646                 :    30122646 :     case UNION_TYPE:
   10647                 :    30122646 :     case ENUMERAL_TYPE:
   10648                 :    30122646 :       if (!TYPE_TEMPLATE_INFO (t))
   10649                 :     3874287 :         *walk_subtrees = 0;
   10650                 :             :       else
   10651                 :    26248359 :         WALK_SUBTREE (TYPE_TI_ARGS (t));
   10652                 :             :       break;
   10653                 :             : 
   10654                 :     7365171 :     case INTEGER_TYPE:
   10655                 :     7365171 :       WALK_SUBTREE (TYPE_MIN_VALUE (t));
   10656                 :     7365171 :       WALK_SUBTREE (TYPE_MAX_VALUE (t));
   10657                 :             :       break;
   10658                 :             : 
   10659                 :     8087856 :     case METHOD_TYPE:
   10660                 :             :       /* Since we're not going to walk subtrees, we have to do this
   10661                 :             :          explicitly here.  */
   10662                 :     8087856 :       WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
   10663                 :             :       /* Fall through.  */
   10664                 :             : 
   10665                 :     2092114 :     case FUNCTION_TYPE:
   10666                 :             :       /* Check the return type.  */
   10667                 :     2092114 :       WALK_SUBTREE (TREE_TYPE (t));
   10668                 :             : 
   10669                 :             :       /* Check the parameter types.  Since default arguments are not
   10670                 :             :          instantiated until they are needed, the TYPE_ARG_TYPES may
   10671                 :             :          contain expressions that involve template parameters.  But,
   10672                 :             :          no-one should be looking at them yet.  And, once they're
   10673                 :             :          instantiated, they don't contain template parameters, so
   10674                 :             :          there's no point in looking at them then, either.  */
   10675                 :     1862741 :       {
   10676                 :     1862741 :         tree parm;
   10677                 :             : 
   10678                 :     6264593 :         for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
   10679                 :     4420894 :           WALK_SUBTREE (TREE_VALUE (parm));
   10680                 :             : 
   10681                 :             :         /* Since we've already handled the TYPE_ARG_TYPES, we don't
   10682                 :             :            want walk_tree walking into them itself.  */
   10683                 :     1843699 :         *walk_subtrees = 0;
   10684                 :             :       }
   10685                 :             : 
   10686                 :     1843699 :       if (flag_noexcept_type)
   10687                 :             :         {
   10688                 :     1819654 :           tree spec = TYPE_RAISES_EXCEPTIONS (t);
   10689                 :     1819654 :           if (spec)
   10690                 :      535394 :             WALK_SUBTREE (TREE_PURPOSE (spec));
   10691                 :             :         }
   10692                 :             :       break;
   10693                 :             : 
   10694                 :     3345649 :     case TYPEOF_TYPE:
   10695                 :     3345649 :     case DECLTYPE_TYPE:
   10696                 :     3345649 :       if (pfd->include_nondeduced_p
   10697                 :     3345649 :           && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
   10698                 :             :                                      pfd->visited,
   10699                 :             :                                      pfd->include_nondeduced_p,
   10700                 :             :                                      pfd->any_fn))
   10701                 :         431 :         return error_mark_node;
   10702                 :     3345218 :       *walk_subtrees = false;
   10703                 :     3345218 :       break;
   10704                 :             : 
   10705                 :         116 :     case TRAIT_TYPE:
   10706                 :         116 :       if (pfd->include_nondeduced_p)
   10707                 :             :         {
   10708                 :           0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
   10709                 :           0 :           WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
   10710                 :             :         }
   10711                 :         116 :       *walk_subtrees = false;
   10712                 :         116 :       break;
   10713                 :             : 
   10714                 :      405976 :     case FUNCTION_DECL:
   10715                 :      405976 :     case VAR_DECL:
   10716                 :      405976 :       if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   10717                 :       44681 :         WALK_SUBTREE (DECL_TI_ARGS (t));
   10718                 :             :       break;
   10719                 :             : 
   10720                 :     1817181 :     case PARM_DECL:
   10721                 :     1817181 :       WALK_SUBTREE (TREE_TYPE (t));
   10722                 :             :       break;
   10723                 :             : 
   10724                 :      389510 :     case CONST_DECL:
   10725                 :      389510 :       if (DECL_TEMPLATE_PARM_P (t))
   10726                 :           0 :         WALK_SUBTREE (DECL_INITIAL (t));
   10727                 :      389510 :       if (DECL_CONTEXT (t)
   10728                 :      389510 :           && pfd->include_nondeduced_p)
   10729                 :        6151 :         WALK_SUBTREE (DECL_CONTEXT (t));
   10730                 :             :       break;
   10731                 :             : 
   10732                 :       55977 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   10733                 :             :       /* Record template parameters such as `T' inside `TT<T>'.  */
   10734                 :       55977 :       WALK_SUBTREE (TYPE_TI_ARGS (t));
   10735                 :             :       /* Fall through.  */
   10736                 :             : 
   10737                 :    65412050 :     case TEMPLATE_TEMPLATE_PARM:
   10738                 :    65412050 :     case TEMPLATE_TYPE_PARM:
   10739                 :    65412050 :     case TEMPLATE_PARM_INDEX:
   10740                 :    65412050 :       if (fn && (*fn)(t, data))
   10741                 :             :         return t;
   10742                 :    57064741 :       else if (!fn)
   10743                 :             :         return t;
   10744                 :             :       break;
   10745                 :             : 
   10746                 :     9665010 :     case TEMPLATE_DECL:
   10747                 :             :       /* A template template parameter is encountered.  */
   10748                 :     9665010 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   10749                 :          28 :         WALK_SUBTREE (TREE_TYPE (t));
   10750                 :             : 
   10751                 :             :       /* Already substituted template template parameter */
   10752                 :     9665010 :       *walk_subtrees = 0;
   10753                 :     9665010 :       break;
   10754                 :             : 
   10755                 :    11695755 :     case TYPENAME_TYPE:
   10756                 :             :       /* A template-id in a TYPENAME_TYPE might be a deduced context after
   10757                 :             :          partial instantiation.  */
   10758                 :    11695755 :       WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
   10759                 :    11695755 :       *walk_subtrees = 0;
   10760                 :    11695755 :       break;
   10761                 :             : 
   10762                 :     4937674 :     case INDIRECT_REF:
   10763                 :     4937674 :     case COMPONENT_REF:
   10764                 :             :       /* If there's no type, then this thing must be some expression
   10765                 :             :          involving template parameters.  */
   10766                 :     4937674 :       if (!fn && !TREE_TYPE (t))
   10767                 :           0 :         return error_mark_node;
   10768                 :             :       break;
   10769                 :             : 
   10770                 :      557948 :     case CONSTRUCTOR:
   10771                 :      557948 :     case TRAIT_EXPR:
   10772                 :      557948 :     case PLUS_EXPR:
   10773                 :      557948 :     case MULT_EXPR:
   10774                 :      557948 :     case SCOPE_REF:
   10775                 :             :       /* These are non-deduced contexts.  */
   10776                 :      557948 :       if (!pfd->include_nondeduced_p)
   10777                 :       58019 :         *walk_subtrees = 0;
   10778                 :             :       break;
   10779                 :             : 
   10780                 :     3189613 :     case MODOP_EXPR:
   10781                 :     3189613 :     case CAST_EXPR:
   10782                 :     3189613 :     case IMPLICIT_CONV_EXPR:
   10783                 :     3189613 :     case REINTERPRET_CAST_EXPR:
   10784                 :     3189613 :     case CONST_CAST_EXPR:
   10785                 :     3189613 :     case STATIC_CAST_EXPR:
   10786                 :     3189613 :     case DYNAMIC_CAST_EXPR:
   10787                 :     3189613 :     case ARROW_EXPR:
   10788                 :     3189613 :     case DOTSTAR_EXPR:
   10789                 :     3189613 :     case TYPEID_EXPR:
   10790                 :     3189613 :     case PSEUDO_DTOR_EXPR:
   10791                 :     3189613 :       if (!fn)
   10792                 :           0 :         return error_mark_node;
   10793                 :             :       break;
   10794                 :             : 
   10795                 :             :     default:
   10796                 :             :       break;
   10797                 :             :     }
   10798                 :             : 
   10799                 :             :   #undef WALK_SUBTREE
   10800                 :             : 
   10801                 :             :   /* We didn't find any template parameters we liked.  */
   10802                 :             :  out:
   10803                 :             :   return result;
   10804                 :             : }
   10805                 :             : 
   10806                 :             : /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
   10807                 :             :    BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
   10808                 :             :    call FN with the parameter and the DATA.
   10809                 :             :    If FN returns nonzero, the iteration is terminated, and
   10810                 :             :    for_each_template_parm returns 1.  Otherwise, the iteration
   10811                 :             :    continues.  If FN never returns a nonzero value, the value
   10812                 :             :    returned by for_each_template_parm is 0.  If FN is NULL, it is
   10813                 :             :    considered to be the function which always returns 1.
   10814                 :             : 
   10815                 :             :    If INCLUDE_NONDEDUCED_P, then this routine will also visit template
   10816                 :             :    parameters that occur in non-deduced contexts.  When false, only
   10817                 :             :    visits those template parameters that can be deduced.  */
   10818                 :             : 
   10819                 :             : static tree
   10820                 :   347451891 : for_each_template_parm (tree t, tree_fn_t fn, void* data,
   10821                 :             :                         hash_set<tree> *visited,
   10822                 :             :                         bool include_nondeduced_p,
   10823                 :             :                         tree_fn_t any_fn)
   10824                 :             : {
   10825                 :   347451891 :   struct pair_fn_data pfd;
   10826                 :   347451891 :   tree result;
   10827                 :             : 
   10828                 :             :   /* Set up.  */
   10829                 :   347451891 :   pfd.fn = fn;
   10830                 :   347451891 :   pfd.any_fn = any_fn;
   10831                 :   347451891 :   pfd.data = data;
   10832                 :   347451891 :   pfd.include_nondeduced_p = include_nondeduced_p;
   10833                 :             : 
   10834                 :             :   /* Walk the tree.  (Conceptually, we would like to walk without
   10835                 :             :      duplicates, but for_each_template_parm_r recursively calls
   10836                 :             :      for_each_template_parm, so we would need to reorganize a fair
   10837                 :             :      bit to use walk_tree_without_duplicates, so we keep our own
   10838                 :             :      visited list.)  */
   10839                 :   347451891 :   if (visited)
   10840                 :   317110482 :     pfd.visited = visited;
   10841                 :             :   else
   10842                 :    30341409 :     pfd.visited = new hash_set<tree>;
   10843                 :   347451891 :   result = cp_walk_tree (&t,
   10844                 :             :                          for_each_template_parm_r,
   10845                 :             :                          &pfd,
   10846                 :             :                          pfd.visited);
   10847                 :             : 
   10848                 :             :   /* Clean up.  */
   10849                 :   347451891 :   if (!visited)
   10850                 :             :     {
   10851                 :    60682818 :       delete pfd.visited;
   10852                 :    30341409 :       pfd.visited = 0;
   10853                 :             :     }
   10854                 :             : 
   10855                 :   347451891 :   return result;
   10856                 :             : }
   10857                 :             : 
   10858                 :    11500785 : struct find_template_parameter_info
   10859                 :             : {
   10860                 :    11500785 :   explicit find_template_parameter_info (tree ctx_parms)
   10861                 :    23001570 :     : ctx_parms (ctx_parms),
   10862                 :    11500785 :       max_depth (TMPL_PARMS_DEPTH (ctx_parms))
   10863                 :    11500785 :   {}
   10864                 :             : 
   10865                 :             :   hash_set<tree> visited;
   10866                 :             :   hash_set<tree> parms;
   10867                 :             :   tree parm_list = NULL_TREE;
   10868                 :             :   tree *parm_list_tail = &parm_list;
   10869                 :             :   tree ctx_parms;
   10870                 :             :   int max_depth;
   10871                 :             : 
   10872                 :             :   tree find_in (tree);
   10873                 :             :   tree find_in_recursive (tree);
   10874                 :             :   bool found (tree);
   10875                 :         149 :   unsigned num_found () { return parms.elements (); }
   10876                 :             : };
   10877                 :             : 
   10878                 :             : /* Appends the declaration of T to the list in DATA.  */
   10879                 :             : 
   10880                 :             : static int
   10881                 :    21419943 : keep_template_parm (tree t, void* data)
   10882                 :             : {
   10883                 :    21419943 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   10884                 :             : 
   10885                 :             :   /* Template parameters declared within the expression are not part of
   10886                 :             :      the parameter mapping. For example, in this concept:
   10887                 :             : 
   10888                 :             :        template<typename T>
   10889                 :             :        concept C = requires { <expr> } -> same_as<int>;
   10890                 :             : 
   10891                 :             :      the return specifier same_as<int> declares a new decltype parameter
   10892                 :             :      that must not be part of the parameter mapping. The same is true
   10893                 :             :      for generic lambda parameters, lambda template parameters, etc.  */
   10894                 :    21419943 :   int level;
   10895                 :    21419943 :   int index;
   10896                 :    21419943 :   template_parm_level_and_index (t, &level, &index);
   10897                 :    21419943 :   if (level == 0 || level > ftpi->max_depth)
   10898                 :             :     return 0;
   10899                 :             : 
   10900                 :    19557548 :   if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
   10901                 :             :     /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
   10902                 :             :        BOUND_TEMPLATE_TEMPLATE_PARM itself.  */
   10903                 :        7966 :     t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
   10904                 :             : 
   10905                 :             :   /* This template parameter might be an argument to a cached dependent
   10906                 :             :      specalization that was formed earlier inside some other template, in
   10907                 :             :      which case the parameter is not among the ones that are in-scope.
   10908                 :             :      Look in CTX_PARMS to find the corresponding in-scope template
   10909                 :             :      parameter, and use it instead.  */
   10910                 :    19557548 :   if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
   10911                 :    19061651 :     t = in_scope;
   10912                 :             : 
   10913                 :             :   /* Arguments like const T yield parameters like const T. This means that
   10914                 :             :      a template-id like X<T, const T> would yield two distinct parameters:
   10915                 :             :      T and const T. Adjust types to their unqualified versions.  */
   10916                 :    19557548 :   if (TYPE_P (t))
   10917                 :    18997703 :     t = TYPE_MAIN_VARIANT (t);
   10918                 :    19557548 :   if (!ftpi->parms.add (t))
   10919                 :             :     {
   10920                 :             :       /* Append T to PARM_LIST.  */
   10921                 :    17319826 :       tree node = build_tree_list (NULL_TREE, t);
   10922                 :    17319826 :       *ftpi->parm_list_tail = node;
   10923                 :    17319826 :       ftpi->parm_list_tail = &TREE_CHAIN (node);
   10924                 :             :     }
   10925                 :             : 
   10926                 :             :   /* Verify the parameter we found has a valid index.  */
   10927                 :    19557548 :   if (flag_checking)
   10928                 :             :     {
   10929                 :    19557548 :       tree parms = ftpi->ctx_parms;
   10930                 :    20977503 :       while (TMPL_PARMS_DEPTH (parms) > level)
   10931                 :     1419955 :         parms = TREE_CHAIN (parms);
   10932                 :    19557548 :       if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
   10933                 :    19061651 :         gcc_assert (index < len);
   10934                 :             :     }
   10935                 :             : 
   10936                 :             :   return 0;
   10937                 :             : }
   10938                 :             : 
   10939                 :             : /* Ensure that we recursively examine certain terms that are not normally
   10940                 :             :    visited in for_each_template_parm_r.  */
   10941                 :             : 
   10942                 :             : static int
   10943                 :   170410681 : any_template_parm_r (tree t, void *data)
   10944                 :             : {
   10945                 :   170410681 :   find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
   10946                 :             : 
   10947                 :             : #define WALK_SUBTREE(NODE)                                              \
   10948                 :             :   do                                                                    \
   10949                 :             :     {                                                                   \
   10950                 :             :       for_each_template_parm (NODE, keep_template_parm, data,           \
   10951                 :             :                               &ftpi->visited, true,                      \
   10952                 :             :                               any_template_parm_r);                     \
   10953                 :             :     }                                                                   \
   10954                 :             :   while (0)
   10955                 :             : 
   10956                 :             :   /* A mention of a member alias/typedef is a use of all of its template
   10957                 :             :      arguments, including those from the enclosing class, so we don't use
   10958                 :             :      alias_template_specialization_p here.  */
   10959                 :   170410681 :   if (TYPE_P (t) && typedef_variant_p (t))
   10960                 :     2739604 :     if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
   10961                 :     2739590 :       WALK_SUBTREE (TI_ARGS (tinfo));
   10962                 :             : 
   10963                 :   170410681 :   switch (TREE_CODE (t))
   10964                 :             :     {
   10965                 :    20834363 :     case TEMPLATE_TYPE_PARM:
   10966                 :             :       /* Type constraints of a placeholder type may contain parameters.  */
   10967                 :    20834363 :       if (is_auto (t))
   10968                 :     2358293 :         if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
   10969                 :     2357534 :           WALK_SUBTREE (constr);
   10970                 :             :       break;
   10971                 :             : 
   10972                 :    10202029 :     case TEMPLATE_ID_EXPR:
   10973                 :             :       /* Search through references to variable templates.  */
   10974                 :    10202029 :       WALK_SUBTREE (TREE_OPERAND (t, 0));
   10975                 :    10202029 :       WALK_SUBTREE (TREE_OPERAND (t, 1));
   10976                 :    10202029 :       break;
   10977                 :             : 
   10978                 :      559847 :     case TEMPLATE_PARM_INDEX:
   10979                 :      559847 :       WALK_SUBTREE (TREE_TYPE (t));
   10980                 :      559847 :       break;
   10981                 :             : 
   10982                 :     9399225 :     case TEMPLATE_DECL:
   10983                 :             :       /* If T is a member template that shares template parameters with
   10984                 :             :          ctx_parms, we need to mark all those parameters for mapping.
   10985                 :             :          To that end, it should suffice to just walk the DECL_CONTEXT of
   10986                 :             :          the template (assuming the template is not overly general).  */
   10987                 :     9399225 :       WALK_SUBTREE (DECL_CONTEXT (t));
   10988                 :     9399225 :       break;
   10989                 :             : 
   10990                 :          19 :     case LAMBDA_EXPR:
   10991                 :          19 :       {
   10992                 :             :         /* Look in the parms and body.  */
   10993                 :          19 :         tree fn = lambda_function (t);
   10994                 :          19 :         WALK_SUBTREE (TREE_TYPE (fn));
   10995                 :          19 :         WALK_SUBTREE (DECL_SAVED_TREE (fn));
   10996                 :             :       }
   10997                 :          19 :       break;
   10998                 :             : 
   10999                 :     4781078 :     case IDENTIFIER_NODE:
   11000                 :     9544676 :       if (IDENTIFIER_CONV_OP_P (t))
   11001                 :             :         /* The conversion-type-id of a conversion operator may be dependent.  */
   11002                 :        3006 :         WALK_SUBTREE (TREE_TYPE (t));
   11003                 :             :       break;
   11004                 :             : 
   11005                 :           5 :     case CONVERT_EXPR:
   11006                 :           5 :       if (is_dummy_object (t))
   11007                 :           4 :         WALK_SUBTREE (TREE_TYPE (t));
   11008                 :             :       break;
   11009                 :             : 
   11010                 :             :     default:
   11011                 :             :       break;
   11012                 :             :     }
   11013                 :             : 
   11014                 :             :   /* Keep walking.  */
   11015                 :   170410681 :   return 0;
   11016                 :             : }
   11017                 :             : 
   11018                 :             : /* Look through T for template parameters.  */
   11019                 :             : 
   11020                 :             : tree
   11021                 :    11500947 : find_template_parameter_info::find_in (tree t)
   11022                 :             : {
   11023                 :    11500947 :   return for_each_template_parm (t, keep_template_parm, this, &visited,
   11024                 :             :                                  /*include_nondeduced*/true,
   11025                 :    11500947 :                                  any_template_parm_r);
   11026                 :             : }
   11027                 :             : 
   11028                 :             : /* As above, but also recursively look into the default arguments of template
   11029                 :             :    parameters we found.  Used for alias CTAD.  */
   11030                 :             : 
   11031                 :             : tree
   11032                 :         149 : find_template_parameter_info::find_in_recursive (tree t)
   11033                 :             : {
   11034                 :         149 :   if (tree r = find_in (t))
   11035                 :             :     return r;
   11036                 :             :   /* Since newly found parms are added to the end of the list, we
   11037                 :             :      can just walk it until we reach the end.  */
   11038                 :         311 :   for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl))
   11039                 :             :     {
   11040                 :         162 :       tree parm = TREE_VALUE (pl);
   11041                 :         162 :       tree list = corresponding_template_parameter_list (ctx_parms, parm);
   11042                 :         162 :       if (tree r = find_in (TREE_PURPOSE (list)))
   11043                 :           0 :         return r;
   11044                 :             :     }
   11045                 :             :   return NULL_TREE;
   11046                 :             : }
   11047                 :             : 
   11048                 :             : /* True if PARM was found by a previous call to find_in.  PARM can be a
   11049                 :             :    TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P.  */
   11050                 :             : 
   11051                 :             : bool
   11052                 :         175 : find_template_parameter_info::found (tree parm)
   11053                 :             : {
   11054                 :         175 :   if (TREE_CODE (parm) == TREE_LIST)
   11055                 :         175 :     parm = TREE_VALUE (parm);
   11056                 :         175 :   if (TREE_CODE (parm) == TYPE_DECL
   11057                 :         175 :       || TREE_CODE (parm) == TEMPLATE_DECL)
   11058                 :         167 :     parm = TREE_TYPE (parm);
   11059                 :             :   else
   11060                 :           8 :     parm = DECL_INITIAL (parm);
   11061                 :         175 :   gcc_checking_assert (TEMPLATE_PARM_P (parm));
   11062                 :         175 :   return parms.contains (parm);
   11063                 :             : }
   11064                 :             : 
   11065                 :             : /* Returns a list of unique template parameters found within T, where CTX_PARMS
   11066                 :             :    are the template parameters in scope.  */
   11067                 :             : 
   11068                 :             : tree
   11069                 :    11500668 : find_template_parameters (tree t, tree ctx_parms)
   11070                 :             : {
   11071                 :    11500668 :   if (!ctx_parms)
   11072                 :             :     return NULL_TREE;
   11073                 :             : 
   11074                 :    11500636 :   find_template_parameter_info ftpi (ctx_parms);
   11075                 :    11500636 :   ftpi.find_in (t);
   11076                 :    11500636 :   return ftpi.parm_list;
   11077                 :    11500636 : }
   11078                 :             : 
   11079                 :             : /* Returns true if T depends on any template parameter.  */
   11080                 :             : 
   11081                 :             : bool
   11082                 :  2820628653 : uses_template_parms (tree t)
   11083                 :             : {
   11084                 :  2820628653 :   if (t == NULL_TREE || t == error_mark_node)
   11085                 :             :     return false;
   11086                 :             : 
   11087                 :             :   /* Namespaces can't depend on any template parameters.  */
   11088                 :  2489391963 :   if (TREE_CODE (t) == NAMESPACE_DECL)
   11089                 :             :     return false;
   11090                 :             : 
   11091                 :  2489391960 :   processing_template_decl_sentinel ptds (/*reset*/false);
   11092                 :  2489391960 :   ++processing_template_decl;
   11093                 :             : 
   11094                 :  2489391960 :   if (TYPE_P (t))
   11095                 :  1552480817 :     return dependent_type_p (t);
   11096                 :   936911143 :   else if (TREE_CODE (t) == TREE_VEC)
   11097                 :   691751826 :     return any_dependent_template_arguments_p (t);
   11098                 :   245159317 :   else if (TREE_CODE (t) == TREE_LIST)
   11099                 :   119173624 :     return (uses_template_parms (TREE_VALUE (t))
   11100                 :   119173624 :             || uses_template_parms (TREE_CHAIN (t)));
   11101                 :   125985693 :   else if (TREE_CODE (t) == TYPE_DECL)
   11102                 :       10467 :     return dependent_type_p (TREE_TYPE (t));
   11103                 :             :   else
   11104                 :   125975226 :     return instantiation_dependent_expression_p (t);
   11105                 :  2489391960 : }
   11106                 :             : 
   11107                 :             : /* Returns true if T depends on any template parameter with level LEVEL.  */
   11108                 :             : 
   11109                 :             : bool
   11110                 :     1839522 : uses_template_parms_level (tree t, int level)
   11111                 :             : {
   11112                 :     1839522 :   return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
   11113                 :     1839522 :                                  /*include_nondeduced_p=*/true);
   11114                 :             : }
   11115                 :             : 
   11116                 :             : /* Returns true if the signature of DECL depends on any template parameter from
   11117                 :             :    its enclosing class.  */
   11118                 :             : 
   11119                 :             : static bool
   11120                 :   245062887 : uses_outer_template_parms (tree decl)
   11121                 :             : {
   11122                 :   245062887 :   int depth;
   11123                 :   245062887 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11124                 :      296591 :     depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1;
   11125                 :             :   else
   11126                 :   244766296 :     depth = template_class_depth (CP_DECL_CONTEXT (decl));
   11127                 :   245062887 :   if (depth == 0)
   11128                 :             :     return false;
   11129                 :     8570410 :   if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
   11130                 :             :                               &depth, NULL, /*include_nondeduced_p=*/true))
   11131                 :             :     return true;
   11132                 :      794771 :   if (PRIMARY_TEMPLATE_P (decl)
   11133                 :      794771 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   11134                 :             :     {
   11135                 :      794771 :       tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
   11136                 :     1856149 :       for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
   11137                 :             :         {
   11138                 :     1061427 :           tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
   11139                 :     1061427 :           tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
   11140                 :     1061427 :           if (TREE_CODE (parm) == PARM_DECL
   11141                 :     1061427 :               && for_each_template_parm (TREE_TYPE (parm),
   11142                 :             :                                          template_parm_outer_level,
   11143                 :             :                                          &depth, NULL, /*nondeduced*/true))
   11144                 :             :             return true;
   11145                 :     1061384 :           if (TREE_CODE (parm) == TEMPLATE_DECL
   11146                 :     1061384 :               && uses_outer_template_parms (parm))
   11147                 :             :             return true;
   11148                 :     1061384 :           if (defarg
   11149                 :     1061384 :               && for_each_template_parm (defarg, template_parm_outer_level,
   11150                 :             :                                          &depth, NULL, /*nondeduced*/true))
   11151                 :             :             return true;
   11152                 :             :         }
   11153                 :             :     }
   11154                 :      794722 :   if (uses_outer_template_parms_in_constraints (decl))
   11155                 :             :     return true;
   11156                 :             :   return false;
   11157                 :             : }
   11158                 :             : 
   11159                 :             : /* Returns true if the constraints of DECL depend on any template parameters
   11160                 :             :    from its enclosing scope.  */
   11161                 :             : 
   11162                 :             : bool
   11163                 :     3300993 : uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/)
   11164                 :             : {
   11165                 :     3300993 :   tree ci = get_constraints (decl);
   11166                 :     3300993 :   if (ci)
   11167                 :        4544 :     ci = CI_ASSOCIATED_CONSTRAINTS (ci);
   11168                 :        4544 :   if (!ci)
   11169                 :             :     return false;
   11170                 :        4544 :   if (!ctx)
   11171                 :             :     {
   11172                 :        1407 :       if (tree fc = DECL_FRIEND_CONTEXT (decl))
   11173                 :             :         ctx = fc;
   11174                 :             :       else
   11175                 :          41 :         ctx = CP_DECL_CONTEXT (decl);
   11176                 :             :     }
   11177                 :        4544 :   int depth = template_class_depth (ctx);
   11178                 :        4544 :   if (depth == 0)
   11179                 :             :     return false;
   11180                 :        4420 :   return for_each_template_parm (ci, template_parm_outer_level,
   11181                 :        4420 :                                  &depth, NULL, /*nondeduced*/true);
   11182                 :             : }
   11183                 :             : 
   11184                 :             : /* Returns TRUE iff INST is an instantiation we don't need to do in an
   11185                 :             :    ill-formed translation unit, i.e. a variable or function that isn't
   11186                 :             :    usable in a constant expression.  */
   11187                 :             : 
   11188                 :             : static inline bool
   11189                 :     4624428 : neglectable_inst_p (tree d)
   11190                 :             : {
   11191                 :     4558936 :   return (d && DECL_P (d)
   11192                 :     4103912 :           && !undeduced_auto_decl (d)
   11193                 :     8663164 :           && !(TREE_CODE (d) == FUNCTION_DECL
   11194                 :     4593864 :                ? FNDECL_MANIFESTLY_CONST_EVALUATED (d)
   11195                 :      555128 :                : decl_maybe_constant_var_p (d)));
   11196                 :             : }
   11197                 :             : 
   11198                 :             : /* Returns TRUE iff we should refuse to instantiate DECL because it's
   11199                 :             :    neglectable and instantiated from within an erroneous instantiation.  */
   11200                 :             : 
   11201                 :             : static bool
   11202                 :   144387582 : limit_bad_template_recursion (tree decl)
   11203                 :             : {
   11204                 :   144387582 :   struct tinst_level *lev = current_tinst_level;
   11205                 :   144387582 :   int errs = errorcount + sorrycount;
   11206                 :   144387582 :   if (errs == 0 || !neglectable_inst_p (decl))
   11207                 :   142178237 :     return false;
   11208                 :             : 
   11209                 :             :   /* Avoid instantiating members of an ill-formed class.  */
   11210                 :     2209345 :   bool refuse
   11211                 :     4418687 :     = (DECL_CLASS_SCOPE_P (decl)
   11212                 :     3815151 :        && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl)));
   11213                 :             : 
   11214                 :             :   if (!refuse)
   11215                 :             :     {
   11216                 :     2360217 :       for (; lev; lev = lev->next)
   11217                 :     2782192 :         if (neglectable_inst_p (lev->maybe_get_node ()))
   11218                 :             :           break;
   11219                 :     2208131 :       refuse = (lev && errs > lev->errors);
   11220                 :             :     }
   11221                 :             : 
   11222                 :             :   if (refuse)
   11223                 :             :     {
   11224                 :             :       /* Don't warn about it not being defined.  */
   11225                 :        2241 :       suppress_warning (decl, OPT_Wunused);
   11226                 :        2241 :       tree clone;
   11227                 :        3893 :       FOR_EACH_CLONE (clone, decl)
   11228                 :        1652 :         suppress_warning (clone, OPT_Wunused);
   11229                 :             :     }
   11230                 :             :   return refuse;
   11231                 :             : }
   11232                 :             : 
   11233                 :             : static int tinst_depth;
   11234                 :             : extern int max_tinst_depth;
   11235                 :             : int depth_reached;
   11236                 :             : 
   11237                 :             : static GTY(()) struct tinst_level *last_error_tinst_level;
   11238                 :             : 
   11239                 :             : /* We're starting to instantiate D; record the template instantiation context
   11240                 :             :    at LOC for diagnostics and to restore it later.  */
   11241                 :             : 
   11242                 :             : bool
   11243                 :   595644717 : push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
   11244                 :             : {
   11245                 :   595644717 :   struct tinst_level *new_level;
   11246                 :             : 
   11247                 :   595644717 :   if (tinst_depth >= max_tinst_depth)
   11248                 :             :     {
   11249                 :             :       /* Tell error.cc not to try to instantiate any templates.  */
   11250                 :          55 :       at_eof = 3;
   11251                 :          55 :       fatal_error (input_location,
   11252                 :             :                    "template instantiation depth exceeds maximum of %d"
   11253                 :             :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   11254                 :             :                    max_tinst_depth);
   11255                 :             :       return false;
   11256                 :             :     }
   11257                 :             : 
   11258                 :             :   /* If the current instantiation caused problems, don't let it instantiate
   11259                 :             :      anything else.  Do allow deduction substitution and decls usable in
   11260                 :             :      constant expressions.  */
   11261                 :   595644662 :   if (!targs && limit_bad_template_recursion (tldcl))
   11262                 :             :     {
   11263                 :             :       /* Avoid no_linkage_errors and unused function (and all other)
   11264                 :             :          warnings for this decl.  */
   11265                 :        2241 :       suppress_warning (tldcl);
   11266                 :        2241 :       return false;
   11267                 :             :     }
   11268                 :             : 
   11269                 :             :   /* When not -quiet, dump template instantiations other than functions, since
   11270                 :             :      announce_function will take care of those.  */
   11271                 :   595642421 :   if (!quiet_flag && !targs
   11272                 :           0 :       && TREE_CODE (tldcl) != TREE_LIST
   11273                 :           0 :       && TREE_CODE (tldcl) != FUNCTION_DECL)
   11274                 :           0 :     fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
   11275                 :             : 
   11276                 :   595642421 :   new_level = tinst_level_freelist ().alloc ();
   11277                 :   595642421 :   new_level->tldcl = tldcl;
   11278                 :   595642421 :   new_level->targs = targs;
   11279                 :   595642421 :   new_level->locus = loc;
   11280                 :   595642421 :   new_level->errors = errorcount + sorrycount;
   11281                 :   595642421 :   new_level->next = NULL;
   11282                 :   595642421 :   new_level->refcount = 0;
   11283                 :   595642421 :   new_level->path = new_level->visible = nullptr;
   11284                 :   595642421 :   set_refcount_ptr (new_level->next, current_tinst_level);
   11285                 :   595642421 :   set_refcount_ptr (current_tinst_level, new_level);
   11286                 :             : 
   11287                 :   595642421 :   ++tinst_depth;
   11288                 :   595642421 :   if (GATHER_STATISTICS && (tinst_depth > depth_reached))
   11289                 :             :     depth_reached = tinst_depth;
   11290                 :             : 
   11291                 :   595642421 :   return true;
   11292                 :             : }
   11293                 :             : 
   11294                 :             : /* We're starting substitution of TMPL<ARGS>; record the template
   11295                 :             :    substitution context for diagnostics and to restore it later.  */
   11296                 :             : 
   11297                 :             : bool
   11298                 :   451257100 : push_tinst_level (tree tmpl, tree args)
   11299                 :             : {
   11300                 :   451257100 :   return push_tinst_level_loc (tmpl, args, input_location);
   11301                 :             : }
   11302                 :             : 
   11303                 :             : /* We're starting to instantiate D; record INPUT_LOCATION and the
   11304                 :             :    template instantiation context for diagnostics and to restore it
   11305                 :             :    later.  */
   11306                 :             : 
   11307                 :             : bool
   11308                 :   144241650 : push_tinst_level (tree d)
   11309                 :             : {
   11310                 :   144241650 :   return push_tinst_level_loc (d, input_location);
   11311                 :             : }
   11312                 :             : 
   11313                 :             : /* Likewise, but record LOC as the program location.  */
   11314                 :             : 
   11315                 :             : bool
   11316                 :   144387617 : push_tinst_level_loc (tree d, location_t loc)
   11317                 :             : {
   11318                 :   144387617 :   gcc_assert (TREE_CODE (d) != TREE_LIST);
   11319                 :   144387617 :   return push_tinst_level_loc (d, NULL, loc);
   11320                 :             : }
   11321                 :             : 
   11322                 :             : /* We're done instantiating this template; return to the instantiation
   11323                 :             :    context.  */
   11324                 :             : 
   11325                 :             : void
   11326                 :   612540076 : pop_tinst_level (void)
   11327                 :             : {
   11328                 :             :   /* Restore the filename and line number stashed away when we started
   11329                 :             :      this instantiation.  */
   11330                 :   612540076 :   input_location = current_tinst_level->locus;
   11331                 :   612540076 :   set_refcount_ptr (current_tinst_level, current_tinst_level->next);
   11332                 :   612540076 :   --tinst_depth;
   11333                 :   612540076 : }
   11334                 :             : 
   11335                 :             : /* We're instantiating a deferred template; restore the template
   11336                 :             :    instantiation context in which the instantiation was requested, which
   11337                 :             :    is one step out from LEVEL.  Return the corresponding DECL or TYPE.  */
   11338                 :             : 
   11339                 :             : static tree
   11340                 :    16931059 : reopen_tinst_level (struct tinst_level *level)
   11341                 :             : {
   11342                 :    16931059 :   struct tinst_level *t;
   11343                 :             : 
   11344                 :    16931059 :   tinst_depth = 0;
   11345                 :    84396638 :   for (t = level; t; t = t->next)
   11346                 :    67465579 :     ++tinst_depth;
   11347                 :             : 
   11348                 :    16931059 :   set_refcount_ptr (current_tinst_level, level);
   11349                 :    16931059 :   pop_tinst_level ();
   11350                 :    16931059 :   if (current_tinst_level)
   11351                 :    13081635 :     current_tinst_level->errors = errorcount+sorrycount;
   11352                 :    16931059 :   return level->maybe_get_node ();
   11353                 :             : }
   11354                 :             : 
   11355                 :             : /* Returns the TINST_LEVEL which gives the original instantiation
   11356                 :             :    context.  */
   11357                 :             : 
   11358                 :             : struct tinst_level *
   11359                 :         164 : outermost_tinst_level (void)
   11360                 :             : {
   11361                 :         164 :   struct tinst_level *level = current_tinst_level;
   11362                 :         164 :   if (level)
   11363                 :          23 :     while (level->next)
   11364                 :             :       level = level->next;
   11365                 :         164 :   return level;
   11366                 :             : }
   11367                 :             : 
   11368                 :             : /* True iff T is a friend function declaration that is not itself a template
   11369                 :             :    and is not defined in a class template.  */
   11370                 :             : 
   11371                 :             : bool
   11372                 :   117330208 : non_templated_friend_p (tree t)
   11373                 :             : {
   11374                 :   117330208 :   if (t && TREE_CODE (t) == FUNCTION_DECL
   11375                 :   234339581 :       && DECL_UNIQUE_FRIEND_P (t))
   11376                 :             :     {
   11377                 :     1668422 :       tree ti = DECL_TEMPLATE_INFO (t);
   11378                 :     1668422 :       if (!ti)
   11379                 :             :         return true;
   11380                 :             :       /* DECL_FRIEND_CONTEXT is set for a friend defined in class.  */
   11381                 :     3336844 :       if (DECL_FRIEND_CONTEXT (t))
   11382                 :             :         return false;
   11383                 :             :       /* Non-templated friends in a class template are still represented with a
   11384                 :             :          TEMPLATE_DECL; check that its primary template is the befriending
   11385                 :             :          class.  Note that DECL_PRIMARY_TEMPLATE is null for
   11386                 :             :          template <class T> friend A<T>::f(); */
   11387                 :      606024 :       tree tmpl = TI_TEMPLATE (ti);
   11388                 :      606024 :       tree primary = DECL_PRIMARY_TEMPLATE (tmpl);
   11389                 :      606024 :       return (primary && primary != tmpl);
   11390                 :             :     }
   11391                 :             :   else
   11392                 :             :     return false;
   11393                 :             : }
   11394                 :             : 
   11395                 :             : /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
   11396                 :             :    vector of template arguments, as for tsubst.
   11397                 :             : 
   11398                 :             :    Returns an appropriate tsubst'd friend declaration.  */
   11399                 :             : 
   11400                 :             : static tree
   11401                 :     1614058 : tsubst_friend_function (tree decl, tree args)
   11402                 :             : {
   11403                 :     1614058 :   tree new_friend;
   11404                 :             : 
   11405                 :     1614058 :   if (TREE_CODE (decl) == FUNCTION_DECL
   11406                 :      653739 :       && DECL_TEMPLATE_INSTANTIATION (decl)
   11407                 :     1637269 :       && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
   11408                 :             :     /* This was a friend declared with an explicit template
   11409                 :             :        argument list, e.g.:
   11410                 :             : 
   11411                 :             :        friend void f<>(T);
   11412                 :             : 
   11413                 :             :        to indicate that f was a template instantiation, not a new
   11414                 :             :        function declaration.  Now, we have to figure out what
   11415                 :             :        instantiation of what template.  */
   11416                 :             :     {
   11417                 :       23211 :       tree template_id, arglist, fns;
   11418                 :       23211 :       tree new_args;
   11419                 :       23211 :       tree tmpl;
   11420                 :       23211 :       tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
   11421                 :             : 
   11422                 :             :       /* Friend functions are looked up in the containing namespace scope.
   11423                 :             :          We must enter that scope, to avoid finding member functions of the
   11424                 :             :          current class with same name.  */
   11425                 :       23211 :       push_nested_namespace (ns);
   11426                 :       23211 :       fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
   11427                 :             :                          tf_warning_or_error, NULL_TREE);
   11428                 :       23211 :       pop_nested_namespace (ns);
   11429                 :       23211 :       arglist = tsubst (DECL_TI_ARGS (decl), args,
   11430                 :             :                         tf_warning_or_error, NULL_TREE);
   11431                 :       23211 :       template_id = lookup_template_function (fns, arglist);
   11432                 :             : 
   11433                 :       23211 :       new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11434                 :       23211 :       tmpl = determine_specialization (template_id, new_friend,
   11435                 :             :                                        &new_args,
   11436                 :             :                                        /*need_member_template=*/0,
   11437                 :       23211 :                                        TREE_VEC_LENGTH (args),
   11438                 :             :                                        tsk_none);
   11439                 :       23211 :       return instantiate_template (tmpl, new_args, tf_error);
   11440                 :             :     }
   11441                 :             : 
   11442                 :     1590847 :   new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
   11443                 :     1590847 :   if (new_friend == error_mark_node)
   11444                 :             :     return error_mark_node;
   11445                 :             : 
   11446                 :             :   /* The NEW_FRIEND will look like an instantiation, to the
   11447                 :             :      compiler, but is not an instantiation from the point of view of
   11448                 :             :      the language.  For example, we might have had:
   11449                 :             : 
   11450                 :             :      template <class T> struct S {
   11451                 :             :        template <class U> friend void f(T, U);
   11452                 :             :      };
   11453                 :             : 
   11454                 :             :      Then, in S<int>, template <class U> void f(int, U) is not an
   11455                 :             :      instantiation of anything.  */
   11456                 :             : 
   11457                 :     1590847 :   DECL_USE_TEMPLATE (new_friend) = 0;
   11458                 :     1590847 :   if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11459                 :             :     {
   11460                 :      960319 :       DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
   11461                 :      960319 :       DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
   11462                 :     1920638 :       DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
   11463                 :      960319 :         = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
   11464                 :             : 
   11465                 :             :       /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
   11466                 :             :          match in decls_match.  */
   11467                 :      960319 :       tree parms = DECL_TEMPLATE_PARMS (new_friend);
   11468                 :      960319 :       tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
   11469                 :      960319 :       treqs = maybe_substitute_reqs_for (treqs, new_friend);
   11470                 :      960319 :       if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms))
   11471                 :             :         {
   11472                 :       22735 :           TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
   11473                 :             :           /* As well as each TEMPLATE_PARM_CONSTRAINTS.  */
   11474                 :       22735 :           tsubst_each_template_parm_constraints (parms, args,
   11475                 :             :                                                  tf_warning_or_error);
   11476                 :             :         }
   11477                 :             :     }
   11478                 :             : 
   11479                 :             :   /* The mangled name for the NEW_FRIEND is incorrect.  The function
   11480                 :             :      is not a template instantiation and should not be mangled like
   11481                 :             :      one.  Therefore, we forget the mangling here; we'll recompute it
   11482                 :             :      later if we need it.  */
   11483                 :     1590847 :   if (TREE_CODE (new_friend) != TEMPLATE_DECL)
   11484                 :             :     {
   11485                 :      630528 :       SET_DECL_RTL (new_friend, NULL);
   11486                 :      630528 :       SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
   11487                 :             :     }
   11488                 :             : 
   11489                 :     1590847 :   if (DECL_NAMESPACE_SCOPE_P (new_friend))
   11490                 :             :     {
   11491                 :     1590766 :       tree old_decl;
   11492                 :     1590766 :       tree ns;
   11493                 :             : 
   11494                 :             :       /* We must save some information from NEW_FRIEND before calling
   11495                 :             :          duplicate decls since that function will free NEW_FRIEND if
   11496                 :             :          possible.  */
   11497                 :     1590766 :       tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
   11498                 :     1590766 :       tree new_friend_result_template_info = NULL_TREE;
   11499                 :     1590766 :       bool new_friend_is_defn =
   11500                 :             :         (new_friend_template_info
   11501                 :     1590766 :          && (DECL_INITIAL (DECL_TEMPLATE_RESULT
   11502                 :             :                            (template_for_substitution (new_friend)))
   11503                 :     1590766 :              != NULL_TREE));
   11504                 :     1590766 :       tree not_tmpl = new_friend;
   11505                 :             : 
   11506                 :     1590766 :       if (TREE_CODE (new_friend) == TEMPLATE_DECL)
   11507                 :             :         {
   11508                 :             :           /* This declaration is a `primary' template.  */
   11509                 :      960267 :           DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
   11510                 :             : 
   11511                 :      960267 :           not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
   11512                 :      960267 :           new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
   11513                 :             :         }
   11514                 :             : 
   11515                 :             :       /* Inside pushdecl_namespace_level, we will push into the
   11516                 :             :          current namespace. However, the friend function should go
   11517                 :             :          into the namespace of the template.  */
   11518                 :     1590766 :       ns = decl_namespace_context (new_friend);
   11519                 :     1590766 :       push_nested_namespace (ns);
   11520                 :     1590766 :       old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
   11521                 :     1590766 :       pop_nested_namespace (ns);
   11522                 :             : 
   11523                 :     1590766 :       if (old_decl == error_mark_node)
   11524                 :             :         return error_mark_node;
   11525                 :             : 
   11526                 :     1590754 :       if (old_decl != new_friend)
   11527                 :             :         {
   11528                 :             :           /* This new friend declaration matched an existing
   11529                 :             :              declaration.  For example, given:
   11530                 :             : 
   11531                 :             :                template <class T> void f(T);
   11532                 :             :                template <class U> class C {
   11533                 :             :                  template <class T> friend void f(T) {}
   11534                 :             :                };
   11535                 :             : 
   11536                 :             :              the friend declaration actually provides the definition
   11537                 :             :              of `f', once C has been instantiated for some type.  So,
   11538                 :             :              old_decl will be the out-of-class template declaration,
   11539                 :             :              while new_friend is the in-class definition.
   11540                 :             : 
   11541                 :             :              But, if `f' was called before this point, the
   11542                 :             :              instantiation of `f' will have DECL_TI_ARGS corresponding
   11543                 :             :              to `T' but not to `U', references to which might appear
   11544                 :             :              in the definition of `f'.  Previously, the most general
   11545                 :             :              template for an instantiation of `f' was the out-of-class
   11546                 :             :              version; now it is the in-class version.  Therefore, we
   11547                 :             :              run through all specialization of `f', adding to their
   11548                 :             :              DECL_TI_ARGS appropriately.  In particular, they need a
   11549                 :             :              new set of outer arguments, corresponding to the
   11550                 :             :              arguments for this class instantiation.
   11551                 :             : 
   11552                 :             :              The same situation can arise with something like this:
   11553                 :             : 
   11554                 :             :                friend void f(int);
   11555                 :             :                template <class T> class C {
   11556                 :             :                  friend void f(T) {}
   11557                 :             :                };
   11558                 :             : 
   11559                 :             :              when `C<int>' is instantiated.  Now, `f(int)' is defined
   11560                 :             :              in the class.  */
   11561                 :             : 
   11562                 :      441801 :           if (!new_friend_is_defn)
   11563                 :             :             /* On the other hand, if the in-class declaration does
   11564                 :             :                *not* provide a definition, then we don't want to alter
   11565                 :             :                existing definitions.  We can just leave everything
   11566                 :             :                alone.  */
   11567                 :             :             ;
   11568                 :             :           else
   11569                 :             :             {
   11570                 :          36 :               tree new_template = TI_TEMPLATE (new_friend_template_info);
   11571                 :          36 :               tree new_args = TI_ARGS (new_friend_template_info);
   11572                 :             : 
   11573                 :             :               /* Overwrite whatever template info was there before, if
   11574                 :             :                  any, with the new template information pertaining to
   11575                 :             :                  the declaration.  */
   11576                 :          36 :               DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
   11577                 :             : 
   11578                 :          36 :               if (TREE_CODE (old_decl) != TEMPLATE_DECL)
   11579                 :             :                 {
   11580                 :             :                   /* We should have called reregister_specialization in
   11581                 :             :                      duplicate_decls.  */
   11582                 :          28 :                   gcc_assert (retrieve_specialization (new_template,
   11583                 :             :                                                        new_args, 0)
   11584                 :             :                               == old_decl);
   11585                 :             : 
   11586                 :             :                   /* Instantiate it if the global has already been used.  */
   11587                 :          28 :                   if (DECL_ODR_USED (old_decl))
   11588                 :           4 :                     instantiate_decl (old_decl, /*defer_ok=*/true,
   11589                 :             :                                       /*expl_inst_class_mem_p=*/false);
   11590                 :             :                 }
   11591                 :             :               else
   11592                 :             :                 {
   11593                 :           8 :                   tree t;
   11594                 :             : 
   11595                 :             :                   /* Indicate that the old function template is a partial
   11596                 :             :                      instantiation.  */
   11597                 :           8 :                   DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
   11598                 :           8 :                     = new_friend_result_template_info;
   11599                 :             : 
   11600                 :           8 :                   gcc_assert (new_template
   11601                 :             :                               == most_general_template (new_template));
   11602                 :           8 :                   gcc_assert (new_template != old_decl);
   11603                 :             : 
   11604                 :             :                   /* Reassign any specializations already in the hash table
   11605                 :             :                      to the new more general template, and add the
   11606                 :             :                      additional template args.  */
   11607                 :           8 :                   for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
   11608                 :          20 :                        t != NULL_TREE;
   11609                 :          12 :                        t = TREE_CHAIN (t))
   11610                 :             :                     {
   11611                 :          12 :                       tree spec = TREE_VALUE (t);
   11612                 :          12 :                       spec_entry elt;
   11613                 :             : 
   11614                 :          12 :                       elt.tmpl = old_decl;
   11615                 :          12 :                       elt.args = DECL_TI_ARGS (spec);
   11616                 :          12 :                       elt.spec = NULL_TREE;
   11617                 :             : 
   11618                 :          12 :                       decl_specializations->remove_elt (&elt);
   11619                 :             : 
   11620                 :          12 :                       DECL_TI_ARGS (spec)
   11621                 :          24 :                         = add_outermost_template_args (new_args,
   11622                 :          12 :                                                        DECL_TI_ARGS (spec));
   11623                 :             : 
   11624                 :          12 :                       register_specialization
   11625                 :          12 :                         (spec, new_template, DECL_TI_ARGS (spec), true, 0);
   11626                 :             : 
   11627                 :             :                     }
   11628                 :           8 :                   DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
   11629                 :             :                 }
   11630                 :             :             }
   11631                 :             : 
   11632                 :             :           /* The information from NEW_FRIEND has been merged into OLD_DECL
   11633                 :             :              by duplicate_decls.  */
   11634                 :             :           new_friend = old_decl;
   11635                 :             :         }
   11636                 :             : 
   11637                 :             :       /* We've just introduced a namespace-scope function in the purview
   11638                 :             :          without necessarily having opened the enclosing namespace, so
   11639                 :             :          make sure the namespace is in the purview now too.  */
   11640                 :     1590754 :       if (modules_p ()
   11641                 :        9980 :           && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend))
   11642                 :     1593294 :           && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL)
   11643                 :        2492 :         DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true;
   11644                 :             :     }
   11645                 :             :   else
   11646                 :             :     {
   11647                 :          81 :       tree context = DECL_CONTEXT (new_friend);
   11648                 :          81 :       bool dependent_p;
   11649                 :             : 
   11650                 :             :       /* In the code
   11651                 :             :            template <class T> class C {
   11652                 :             :              template <class U> friend void C1<U>::f (); // case 1
   11653                 :             :              friend void C2<T>::f ();                      // case 2
   11654                 :             :            };
   11655                 :             :          we only need to make sure CONTEXT is a complete type for
   11656                 :             :          case 2.  To distinguish between the two cases, we note that
   11657                 :             :          CONTEXT of case 1 remains dependent type after tsubst while
   11658                 :             :          this isn't true for case 2.  */
   11659                 :          81 :       ++processing_template_decl;
   11660                 :          81 :       dependent_p = dependent_type_p (context);
   11661                 :          81 :       --processing_template_decl;
   11662                 :             : 
   11663                 :          81 :       if (!dependent_p
   11664                 :          81 :           && !complete_type_or_else (context, NULL_TREE))
   11665                 :           0 :         return error_mark_node;
   11666                 :             : 
   11667                 :          81 :       if (COMPLETE_TYPE_P (context))
   11668                 :             :         {
   11669                 :          73 :           tree fn = new_friend;
   11670                 :             :           /* do_friend adds the TEMPLATE_DECL for any member friend
   11671                 :             :              template even if it isn't a member template, i.e.
   11672                 :             :                template <class T> friend A<T>::f();
   11673                 :             :              Look through it in that case.  */
   11674                 :          73 :           if (TREE_CODE (fn) == TEMPLATE_DECL
   11675                 :          73 :               && !PRIMARY_TEMPLATE_P (fn))
   11676                 :          24 :             fn = DECL_TEMPLATE_RESULT (fn);
   11677                 :             :           /* Check to see that the declaration is really present, and,
   11678                 :             :              possibly obtain an improved declaration.  */
   11679                 :          73 :           fn = check_classfn (context, fn, NULL_TREE);
   11680                 :             : 
   11681                 :          73 :           if (fn)
   11682                 :     1614058 :             new_friend = fn;
   11683                 :             :         }
   11684                 :             :     }
   11685                 :             : 
   11686                 :             :   return new_friend;
   11687                 :             : }
   11688                 :             : 
   11689                 :             : /* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
   11690                 :             :    template arguments, as for tsubst.
   11691                 :             : 
   11692                 :             :    Returns an appropriate tsubst'd friend type or error_mark_node on
   11693                 :             :    failure.  */
   11694                 :             : 
   11695                 :             : static tree
   11696                 :      563166 : tsubst_friend_class (tree friend_tmpl, tree args)
   11697                 :             : {
   11698                 :      563166 :   tree tmpl;
   11699                 :             : 
   11700                 :      563166 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
   11701                 :             :     {
   11702                 :           3 :       tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
   11703                 :           3 :       return TREE_TYPE (tmpl);
   11704                 :             :     }
   11705                 :             : 
   11706                 :      563163 :   tree context = CP_DECL_CONTEXT (friend_tmpl);
   11707                 :      563163 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   11708                 :      562727 :     push_nested_namespace (context);
   11709                 :             :   else
   11710                 :             :     {
   11711                 :         436 :       context = tsubst (context, args, tf_error, NULL_TREE);
   11712                 :         436 :       push_nested_class (context);
   11713                 :             :     }
   11714                 :             : 
   11715                 :      563163 :   tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
   11716                 :             :                       LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
   11717                 :             : 
   11718                 :      563163 :   if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
   11719                 :             :     {
   11720                 :             :       /* The friend template has already been declared.  Just
   11721                 :             :          check to see that the declarations match, and install any new
   11722                 :             :          default parameters.  We must tsubst the default parameters,
   11723                 :             :          of course.  We only need the innermost template parameters
   11724                 :             :          because that is all that redeclare_class_template will look
   11725                 :             :          at.  */
   11726                 :     1125980 :       if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
   11727                 :     1125980 :           > TMPL_ARGS_DEPTH (args))
   11728                 :             :         {
   11729                 :      352387 :           tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
   11730                 :             :                                               args, tf_warning_or_error);
   11731                 :      352387 :           tsubst_each_template_parm_constraints (parms, args,
   11732                 :             :                                                  tf_warning_or_error);
   11733                 :      352387 :           location_t saved_input_location = input_location;
   11734                 :      352387 :           input_location = DECL_SOURCE_LOCATION (friend_tmpl);
   11735                 :      352387 :           tree cons = get_constraints (friend_tmpl);
   11736                 :      352387 :           ++processing_template_decl;
   11737                 :      352387 :           cons = tsubst_constraint_info (cons, args, tf_warning_or_error,
   11738                 :      352387 :                                          DECL_FRIEND_CONTEXT (friend_tmpl));
   11739                 :      352387 :           --processing_template_decl;
   11740                 :      352387 :           redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
   11741                 :      352387 :           input_location = saved_input_location;
   11742                 :             :         }
   11743                 :             :     }
   11744                 :             :   else
   11745                 :             :     {
   11746                 :             :       /* The friend template has not already been declared.  In this
   11747                 :             :          case, the instantiation of the template class will cause the
   11748                 :             :          injection of this template into the namespace scope.  */
   11749                 :         173 :       tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
   11750                 :             : 
   11751                 :         173 :       if (tmpl != error_mark_node)
   11752                 :             :         {
   11753                 :             :           /* The new TMPL is not an instantiation of anything, so we
   11754                 :             :              forget its origins.  We don't reset CLASSTYPE_TI_TEMPLATE
   11755                 :             :              for the new type because that is supposed to be the
   11756                 :             :              corresponding template decl, i.e., TMPL.  */
   11757                 :         167 :           DECL_USE_TEMPLATE (tmpl) = 0;
   11758                 :         167 :           DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
   11759                 :         167 :           CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
   11760                 :         167 :           CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
   11761                 :         167 :             = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
   11762                 :             : 
   11763                 :             :           /* Substitute into and set the constraints on the new declaration.  */
   11764                 :         167 :           if (tree ci = get_constraints (friend_tmpl))
   11765                 :             :             {
   11766                 :           2 :               ++processing_template_decl;
   11767                 :           2 :               ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
   11768                 :           2 :                                            DECL_FRIEND_CONTEXT (friend_tmpl));
   11769                 :           2 :               --processing_template_decl;
   11770                 :           2 :               set_constraints (tmpl, ci);
   11771                 :           2 :               tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl),
   11772                 :             :                                                      args, tf_warning_or_error);
   11773                 :             :             }
   11774                 :             : 
   11775                 :             :           /* Inject this template into the enclosing namspace scope.  */
   11776                 :         167 :           tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
   11777                 :             :         }
   11778                 :             :     }
   11779                 :             : 
   11780                 :      563163 :   if (TREE_CODE (context) == NAMESPACE_DECL)
   11781                 :      562727 :     pop_nested_namespace (context);
   11782                 :             :   else
   11783                 :         436 :     pop_nested_class ();
   11784                 :             : 
   11785                 :      563163 :   return TREE_TYPE (tmpl);
   11786                 :             : }
   11787                 :             : 
   11788                 :             : /* Returns zero if TYPE cannot be completed later due to circularity.
   11789                 :             :    Otherwise returns one.  */
   11790                 :             : 
   11791                 :             : static int
   11792                 :     5859772 : can_complete_type_without_circularity (tree type)
   11793                 :             : {
   11794                 :     5860176 :   if (type == NULL_TREE || type == error_mark_node)
   11795                 :             :     return 0;
   11796                 :     5860176 :   else if (COMPLETE_TYPE_P (type))
   11797                 :             :     return 1;
   11798                 :      484717 :   else if (TREE_CODE (type) == ARRAY_TYPE)
   11799                 :         404 :     return can_complete_type_without_circularity (TREE_TYPE (type));
   11800                 :      484309 :   else if (CLASS_TYPE_P (type)
   11801                 :      968622 :            && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
   11802                 :             :     return 0;
   11803                 :             :   else
   11804                 :      484300 :     return 1;
   11805                 :             : }
   11806                 :             : 
   11807                 :             : static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
   11808                 :             :                                 tsubst_flags_t, tree);
   11809                 :             : 
   11810                 :             : /* Instantiate the contract statement.  */
   11811                 :             : 
   11812                 :             : static tree
   11813                 :         240 : tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain,
   11814                 :             :                  tree in_decl)
   11815                 :             : {
   11816                 :         240 :   tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE;
   11817                 :         240 :   bool auto_p  = type_uses_auto (type);
   11818                 :             : 
   11819                 :         240 :   tree r = copy_node (t);
   11820                 :             : 
   11821                 :             :   /* Rebuild the result variable.  */
   11822                 :         240 :   if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t))
   11823                 :             :     {
   11824                 :          27 :       tree oldvar = POSTCONDITION_IDENTIFIER (t);
   11825                 :             : 
   11826                 :          27 :       tree newvar = copy_node (oldvar);
   11827                 :          27 :       TREE_TYPE (newvar) = type;
   11828                 :          27 :       DECL_CONTEXT (newvar) = decl;
   11829                 :          27 :       POSTCONDITION_IDENTIFIER (r) = newvar;
   11830                 :             : 
   11831                 :             :       /* Make sure the postcondition is valid.  */
   11832                 :          27 :       location_t loc = DECL_SOURCE_LOCATION (oldvar);
   11833                 :          27 :       if (!auto_p)
   11834                 :          25 :         if (!check_postcondition_result (decl, type, loc))
   11835                 :           0 :           return invalidate_contract (r);
   11836                 :             : 
   11837                 :             :       /* Make the variable available for lookup.  */
   11838                 :          27 :       register_local_specialization (newvar, oldvar);
   11839                 :             :     }
   11840                 :             : 
   11841                 :             :   /* Instantiate the condition.  If the return type is undeduced, process
   11842                 :             :      the expression as if inside a template to avoid spurious type errors.  */
   11843                 :         240 :   if (auto_p)
   11844                 :           2 :     ++processing_template_decl;
   11845                 :         240 :   ++processing_contract_condition;
   11846                 :         240 :   CONTRACT_CONDITION (r)
   11847                 :         240 :       = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl);
   11848                 :         240 :   --processing_contract_condition;
   11849                 :         240 :   if (auto_p)
   11850                 :           2 :     --processing_template_decl;
   11851                 :             : 
   11852                 :             :   /* And the comment.  */
   11853                 :         240 :   CONTRACT_COMMENT (r)
   11854                 :         240 :       = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl);
   11855                 :             : 
   11856                 :         240 :   return r;
   11857                 :             : }
   11858                 :             : 
   11859                 :             : /* Update T by instantiating its contract attribute.  */
   11860                 :             : 
   11861                 :             : static void
   11862                 :         219 : tsubst_contract_attribute (tree decl, tree t, tree args,
   11863                 :             :                            tsubst_flags_t complain, tree in_decl)
   11864                 :             : {
   11865                 :             :   /* For non-specializations, adjust the current declaration to the most general
   11866                 :             :      version of in_decl. Because we defer the instantiation of contracts as long
   11867                 :             :      as possible, they are still written in terms of the parameters (and return
   11868                 :             :      type) of the most general template.  */
   11869                 :         219 :   tree tmpl = DECL_TI_TEMPLATE (in_decl);
   11870                 :         219 :   if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
   11871                 :         213 :     in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl));
   11872                 :         219 :   local_specialization_stack specs (lss_copy);
   11873                 :         219 :   register_parameter_specializations (in_decl, decl);
   11874                 :             : 
   11875                 :             :   /* Get the contract to be instantiated.  */
   11876                 :         219 :   tree contract = CONTRACT_STATEMENT (t);
   11877                 :             : 
   11878                 :             :   /* Use the complete set of template arguments for instantiation. The
   11879                 :             :      contract may not have been instantiated and still refer to outer levels
   11880                 :             :      of template parameters.  */
   11881                 :         219 :   args = DECL_TI_ARGS (decl);
   11882                 :             : 
   11883                 :             :   /* For member functions, make this available for semantic analysis.  */
   11884                 :         219 :   tree save_ccp = current_class_ptr;
   11885                 :         219 :   tree save_ccr = current_class_ref;
   11886                 :         219 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (decl))
   11887                 :             :     {
   11888                 :         108 :       tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
   11889                 :         108 :       tree this_type = TREE_TYPE (TREE_VALUE (arg_types));
   11890                 :         108 :       inject_this_parameter (this_type, cp_type_quals (this_type));
   11891                 :             :     }
   11892                 :             : 
   11893                 :         219 :   contract = tsubst_contract (decl, contract, args, complain, in_decl);
   11894                 :             : 
   11895                 :         219 :   current_class_ptr = save_ccp;
   11896                 :         219 :   current_class_ref = save_ccr;
   11897                 :             : 
   11898                 :             :   /* Rebuild the attribute.  */
   11899                 :         219 :   TREE_VALUE (t) = build_tree_list (NULL_TREE, contract);
   11900                 :         219 : }
   11901                 :             : 
   11902                 :             : /* Rebuild the attribute list for DECL, substituting into contracts
   11903                 :             :    as needed.  */
   11904                 :             : 
   11905                 :             : void
   11906                 :         146 : tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl)
   11907                 :             : {
   11908                 :         146 :   tree list = copy_list (DECL_ATTRIBUTES (decl));
   11909                 :         367 :   for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr))
   11910                 :             :     {
   11911                 :         221 :       if (cxx_contract_attribute_p (attr))
   11912                 :         219 :         tsubst_contract_attribute (decl, attr, args, complain, in_decl);
   11913                 :             :     }
   11914                 :         146 :   DECL_ATTRIBUTES (decl) = list;
   11915                 :         146 : }
   11916                 :             : 
   11917                 :             : /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
   11918                 :             :    T or a new TREE_LIST, possibly a chain in the case of a pack expansion.  */
   11919                 :             : 
   11920                 :             : static tree
   11921                 :     1406871 : tsubst_attribute (tree t, tree *decl_p, tree args,
   11922                 :             :                   tsubst_flags_t complain, tree in_decl)
   11923                 :             : {
   11924                 :     1406871 :   gcc_assert (ATTR_IS_DEPENDENT (t));
   11925                 :             : 
   11926                 :             :   /* Note that contract attributes are never substituted from this function.
   11927                 :             :      Their instantiation is triggered by regenerate_from_template_decl when
   11928                 :             :      we instantiate the body of the function.  */
   11929                 :             : 
   11930                 :     1406871 :   tree val = TREE_VALUE (t);
   11931                 :     1406871 :   if (val == NULL_TREE)
   11932                 :             :     /* Nothing to do.  */;
   11933                 :      194838 :   else if ((flag_openmp || flag_openmp_simd)
   11934                 :      195174 :            && is_attribute_p ("omp declare simd",
   11935                 :         212 :                               get_attribute_name (t)))
   11936                 :             :     {
   11937                 :         140 :       tree clauses = TREE_VALUE (val);
   11938                 :         140 :       clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
   11939                 :             :                                     complain, in_decl);
   11940                 :         140 :       c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   11941                 :         140 :       clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   11942                 :         140 :       tree parms = DECL_ARGUMENTS (*decl_p);
   11943                 :         140 :       clauses
   11944                 :         140 :         = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
   11945                 :         140 :       if (clauses)
   11946                 :         140 :         val = build_tree_list (NULL_TREE, clauses);
   11947                 :             :       else
   11948                 :             :         val = NULL_TREE;
   11949                 :             :     }
   11950                 :      194822 :   else if (flag_openmp
   11951                 :      194894 :            && is_attribute_p ("omp declare variant base",
   11952                 :          72 :                               get_attribute_name (t)))
   11953                 :             :     {
   11954                 :          72 :       ++cp_unevaluated_operand;
   11955                 :          72 :       tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl);
   11956                 :          72 :       --cp_unevaluated_operand;
   11957                 :          72 :       tree chain = TREE_CHAIN (val);
   11958                 :          72 :       location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
   11959                 :          72 :       tree ctx = copy_list (TREE_VALUE (val));
   11960                 :         140 :       for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
   11961                 :             :         {
   11962                 :          80 :           enum omp_tss_code set = OMP_TSS_CODE (tss);
   11963                 :          80 :           tree selectors = NULL_TREE;
   11964                 :         148 :           for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
   11965                 :          68 :                ts = TREE_CHAIN (ts))
   11966                 :             :             {
   11967                 :          80 :               tree properties = NULL_TREE;
   11968                 :          80 :               tree scoreval = NULL_TREE;
   11969                 :             :               /* FIXME: The body of this loop should really be dispatching
   11970                 :             :                  according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
   11971                 :             :                  of having hard-wired knowledge of specific selectors.  */
   11972                 :          80 :               if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
   11973                 :          80 :                   && set == OMP_TRAIT_SET_CONSTRUCT)
   11974                 :             :                 {
   11975                 :           0 :                   tree clauses = OMP_TS_PROPERTIES (ts);
   11976                 :           0 :                   clauses = tsubst_omp_clauses (clauses,
   11977                 :             :                                                 C_ORT_OMP_DECLARE_SIMD, args,
   11978                 :             :                                                 complain, in_decl);
   11979                 :           0 :                   c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
   11980                 :           0 :                   clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
   11981                 :           0 :                   properties = clauses;
   11982                 :             :                 }
   11983                 :             :               else
   11984                 :             :                 {
   11985                 :          80 :                   tree v = OMP_TS_SCORE (ts);
   11986                 :          56 :                   if (v)
   11987                 :             :                     {
   11988                 :          56 :                       v = tsubst_expr (v, args, complain, in_decl);
   11989                 :          56 :                       v = fold_non_dependent_expr (v);
   11990                 :         112 :                       if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   11991                 :         108 :                           || TREE_CODE (v) != INTEGER_CST)
   11992                 :             :                         {
   11993                 :           4 :                           location_t loc
   11994                 :           4 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   11995                 :             :                                                   match_loc);
   11996                 :           4 :                           error_at (loc, "score argument must be "
   11997                 :             :                                     "constant integer expression");
   11998                 :           4 :                           return NULL_TREE;
   11999                 :             :                         }
   12000                 :          52 :                       else if (tree_int_cst_sgn (v) < 0)
   12001                 :             :                         {
   12002                 :           4 :                           location_t loc
   12003                 :           4 :                             = cp_expr_loc_or_loc (OMP_TS_SCORE (ts),
   12004                 :             :                                                   match_loc);
   12005                 :           4 :                           error_at (loc, "score argument must be "
   12006                 :             :                                     "non-negative");
   12007                 :           4 :                           return NULL_TREE;
   12008                 :             :                         }
   12009                 :             :                       scoreval = v;
   12010                 :             :                     }
   12011                 :          72 :                   properties = copy_list (OMP_TS_PROPERTIES (ts));
   12012                 :         140 :                   for (tree p = properties; p; p = TREE_CHAIN (p))
   12013                 :          72 :                     if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
   12014                 :           8 :                       continue;
   12015                 :          64 :                     else if (OMP_TP_VALUE (p))
   12016                 :             :                       {
   12017                 :          64 :                         bool allow_string
   12018                 :          64 :                           = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
   12019                 :          64 :                              || set != OMP_TRAIT_SET_USER);
   12020                 :          64 :                         tree v = OMP_TP_VALUE (p);
   12021                 :          64 :                         if (TREE_CODE (v) == STRING_CST && allow_string)
   12022                 :           0 :                           continue;
   12023                 :          64 :                         v = tsubst_expr (v, args, complain, in_decl);
   12024                 :          64 :                         v = fold_non_dependent_expr (v);
   12025                 :         128 :                         if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
   12026                 :         124 :                             || !tree_fits_shwi_p (v))
   12027                 :             :                           {
   12028                 :           4 :                             location_t loc
   12029                 :           4 :                               = cp_expr_loc_or_loc (OMP_TP_VALUE (p),
   12030                 :             :                                                     match_loc);
   12031                 :           4 :                             if (allow_string)
   12032                 :           0 :                               error_at (loc, "property must be constant "
   12033                 :             :                                              "integer expression or string "
   12034                 :             :                                              "literal");
   12035                 :             :                             else
   12036                 :           4 :                               error_at (loc, "property must be constant "
   12037                 :             :                                              "integer expression");
   12038                 :           4 :                             return NULL_TREE;
   12039                 :             :                           }
   12040                 :          60 :                         OMP_TP_VALUE (p) = v;
   12041                 :             :                       }
   12042                 :             :                 }
   12043                 :          68 :               selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
   12044                 :             :                                                properties, selectors);
   12045                 :             :             }
   12046                 :          68 :           OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
   12047                 :             :         }
   12048                 :          60 :       val = tree_cons (varid, ctx, chain);
   12049                 :             :     }
   12050                 :             :   /* If the first attribute argument is an identifier, don't
   12051                 :             :      pass it through tsubst.  Attributes like mode, format,
   12052                 :             :      cleanup and several target specific attributes expect it
   12053                 :             :      unmodified.  */
   12054                 :      194750 :   else if (attribute_takes_identifier_p (get_attribute_name (t)))
   12055                 :             :     {
   12056                 :         180 :       tree chain
   12057                 :         180 :         = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl);
   12058                 :         180 :       if (chain != TREE_CHAIN (val))
   12059                 :           0 :         val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
   12060                 :             :     }
   12061                 :      194570 :   else if (PACK_EXPANSION_P (val))
   12062                 :             :     {
   12063                 :             :       /* An attribute pack expansion.  */
   12064                 :          15 :       tree purp = TREE_PURPOSE (t);
   12065                 :          15 :       tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
   12066                 :          15 :       if (pack == error_mark_node)
   12067                 :             :         return error_mark_node;
   12068                 :          12 :       int len = TREE_VEC_LENGTH (pack);
   12069                 :          12 :       tree list = NULL_TREE;
   12070                 :          12 :       tree *q = &list;
   12071                 :          24 :       for (int i = 0; i < len; ++i)
   12072                 :             :         {
   12073                 :          12 :           tree elt = TREE_VEC_ELT (pack, i);
   12074                 :          12 :           *q = build_tree_list (purp, elt);
   12075                 :          12 :           q = &TREE_CHAIN (*q);
   12076                 :             :         }
   12077                 :          12 :       return list;
   12078                 :             :     }
   12079                 :             :   else
   12080                 :      194555 :     val = tsubst_expr (val, args, complain, in_decl);
   12081                 :             : 
   12082                 :     1406844 :   if (val == error_mark_node)
   12083                 :             :     return error_mark_node;
   12084                 :     1406832 :   if (val != TREE_VALUE (t))
   12085                 :      194094 :     return build_tree_list (TREE_PURPOSE (t), val);
   12086                 :             :   return t;
   12087                 :             : }
   12088                 :             : 
   12089                 :             : /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
   12090                 :             :    unchanged or a new TREE_LIST chain.  */
   12091                 :             : 
   12092                 :             : static tree
   12093                 :      307176 : tsubst_attributes (tree attributes, tree args,
   12094                 :             :                    tsubst_flags_t complain, tree in_decl)
   12095                 :             : {
   12096                 :      307176 :   tree last_dep = NULL_TREE;
   12097                 :             : 
   12098                 :      307176 :   for (tree t = attributes; t; t = TREE_CHAIN (t))
   12099                 :          16 :     if (ATTR_IS_DEPENDENT (t))
   12100                 :             :       {
   12101                 :          16 :         last_dep = t;
   12102                 :          16 :         attributes = copy_list (attributes);
   12103                 :          16 :         break;
   12104                 :             :       }
   12105                 :             : 
   12106                 :      307176 :   if (last_dep)
   12107                 :          32 :     for (tree *p = &attributes; *p; )
   12108                 :             :       {
   12109                 :          16 :         tree t = *p;
   12110                 :          16 :         if (ATTR_IS_DEPENDENT (t))
   12111                 :             :           {
   12112                 :          16 :             tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
   12113                 :          16 :             if (subst != t)
   12114                 :             :               {
   12115                 :           6 :                 *p = subst;
   12116                 :           9 :                 while (*p)
   12117                 :           3 :                   p = &TREE_CHAIN (*p);
   12118                 :           6 :                 *p = TREE_CHAIN (t);
   12119                 :           6 :                 continue;
   12120                 :             :               }
   12121                 :             :           }
   12122                 :          10 :         p = &TREE_CHAIN (*p);
   12123                 :             :       }
   12124                 :             : 
   12125                 :      307176 :   return attributes;
   12126                 :             : }
   12127                 :             : 
   12128                 :             : /* Apply any attributes which had to be deferred until instantiation
   12129                 :             :    time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
   12130                 :             :    ARGS, COMPLAIN, IN_DECL are as tsubst.  Returns true normally,
   12131                 :             :    false on error.  */
   12132                 :             : 
   12133                 :             : static bool
   12134                 :   495565622 : apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
   12135                 :             :                                 tree args, tsubst_flags_t complain, tree in_decl)
   12136                 :             : {
   12137                 :   495565622 :   tree t;
   12138                 :   495565622 :   tree *p;
   12139                 :             : 
   12140                 :   495565622 :   if (attributes == NULL_TREE)
   12141                 :             :     return true;
   12142                 :             : 
   12143                 :    22001203 :   if (DECL_P (*decl_p))
   12144                 :             :     {
   12145                 :    21971085 :       if (TREE_TYPE (*decl_p) == error_mark_node)
   12146                 :             :         return false;
   12147                 :    21971081 :       p = &DECL_ATTRIBUTES (*decl_p);
   12148                 :             :       /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
   12149                 :             :          to our attributes parameter.  */
   12150                 :    21971081 :       gcc_assert (*p == attributes);
   12151                 :             :     }
   12152                 :             :   else
   12153                 :             :     {
   12154                 :       30118 :       p = &TYPE_ATTRIBUTES (*decl_p);
   12155                 :             :       /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
   12156                 :             :          lookup_template_class_1, and should be preserved.  */
   12157                 :       30118 :       gcc_assert (*p != attributes);
   12158                 :       30567 :       while (*p)
   12159                 :         449 :         p = &TREE_CHAIN (*p);
   12160                 :             :     }
   12161                 :             : 
   12162                 :             :   /* save_template_attributes puts the dependent attributes at the beginning of
   12163                 :             :      the list; find the non-dependent ones.  */
   12164                 :    23408054 :   for (t = attributes; t; t = TREE_CHAIN (t))
   12165                 :    22434220 :     if (!ATTR_IS_DEPENDENT (t))
   12166                 :             :       break;
   12167                 :    22001199 :   tree nondep = t;
   12168                 :             : 
   12169                 :             :   /* Apply any non-dependent attributes.  */
   12170                 :    22001199 :   *p = nondep;
   12171                 :             : 
   12172                 :    22001199 :   if (nondep == attributes)
   12173                 :             :     return true;
   12174                 :             : 
   12175                 :             :   /* And then any dependent ones.  */
   12176                 :     1406754 :   tree late_attrs = NULL_TREE;
   12177                 :     1406754 :   tree *q = &late_attrs;
   12178                 :     2813594 :   for (t = attributes; t != nondep; t = TREE_CHAIN (t))
   12179                 :             :     {
   12180                 :     1406855 :       *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
   12181                 :     1406855 :       if (*q == error_mark_node)
   12182                 :             :         return false;
   12183                 :     1406840 :       if (*q == t)
   12184                 :             :         {
   12185                 :     1212728 :           *q = copy_node (t);
   12186                 :     1212728 :           TREE_CHAIN (*q) = NULL_TREE;
   12187                 :             :         }
   12188                 :     2813671 :       while (*q)
   12189                 :     1406831 :         q = &TREE_CHAIN (*q);
   12190                 :             :     }
   12191                 :             : 
   12192                 :             :   /* cplus_decl_attributes can add some attributes implicitly.  For templates,
   12193                 :             :      those attributes should have been added already when those templates were
   12194                 :             :      parsed, and shouldn't be added based on from which context they are
   12195                 :             :      first time instantiated.  */
   12196                 :     1406739 :   auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE);
   12197                 :     1406739 :   auto o2 = make_temp_override (optimization_current_node,
   12198                 :     1406739 :                                 optimization_default_node);
   12199                 :     1406739 :   auto o3 = make_temp_override (current_target_pragma, NULL_TREE);
   12200                 :     1406739 :   auto o4 = make_temp_override (scope_chain->omp_declare_target_attribute,
   12201                 :     1406739 :                                 NULL);
   12202                 :     1406739 :   auto o5 = make_temp_override (scope_chain->omp_begin_assumes, NULL);
   12203                 :             : 
   12204                 :     1406739 :   cplus_decl_attributes (decl_p, late_attrs, attr_flags);
   12205                 :             : 
   12206                 :     1406739 :   return true;
   12207                 :     1406739 : }
   12208                 :             : 
   12209                 :             : /* The template TMPL is being instantiated with the template arguments TARGS.
   12210                 :             :    Perform the access checks that we deferred when parsing the template.  */
   12211                 :             : 
   12212                 :             : static void
   12213                 :    43703641 : perform_instantiation_time_access_checks (tree tmpl, tree targs)
   12214                 :             : {
   12215                 :    43703641 :   unsigned i;
   12216                 :    43703641 :   deferred_access_check *chk;
   12217                 :             : 
   12218                 :    43703641 :   if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
   12219                 :    43703641 :     return;
   12220                 :             : 
   12221                 :    87407282 :   if (vec<deferred_access_check, va_gc> *access_checks
   12222                 :    43703641 :       = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
   12223                 :         156 :     FOR_EACH_VEC_ELT (*access_checks, i, chk)
   12224                 :             :       {
   12225                 :          84 :         tree decl = chk->decl;
   12226                 :          84 :         tree diag_decl = chk->diag_decl;
   12227                 :          84 :         tree type_scope = TREE_TYPE (chk->binfo);
   12228                 :             : 
   12229                 :          84 :         if (uses_template_parms (type_scope))
   12230                 :          16 :           type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
   12231                 :             : 
   12232                 :             :         /* Make access check error messages point to the location
   12233                 :             :            of the use of the typedef.  */
   12234                 :          84 :         iloc_sentinel ils (chk->loc);
   12235                 :          84 :         perform_or_defer_access_check (TYPE_BINFO (type_scope),
   12236                 :             :                                        decl, diag_decl, tf_warning_or_error);
   12237                 :          84 :       }
   12238                 :             : }
   12239                 :             : 
   12240                 :             : tree
   12241                 :   719512723 : instantiate_class_template (tree type)
   12242                 :             : {
   12243                 :   719512723 :   auto_timevar tv (TV_TEMPLATE_INST);
   12244                 :             : 
   12245                 :   719512723 :   tree templ, args, pattern, t, member;
   12246                 :   719512723 :   tree typedecl;
   12247                 :   719512723 :   tree pbinfo;
   12248                 :   719512723 :   tree base_list;
   12249                 :   719512723 :   unsigned int saved_maximum_field_alignment;
   12250                 :   719512723 :   tree fn_context;
   12251                 :             : 
   12252                 :   719512723 :   if (type == error_mark_node)
   12253                 :             :     return error_mark_node;
   12254                 :             : 
   12255                 :  1433862445 :   if (COMPLETE_OR_OPEN_TYPE_P (type)
   12256                 :   754397010 :       || uses_template_parms (type))
   12257                 :   691666563 :     return type;
   12258                 :             : 
   12259                 :             :   /* Figure out which template is being instantiated.  */
   12260                 :    27846160 :   templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
   12261                 :    27846160 :   gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
   12262                 :             : 
   12263                 :             :   /* Mark the type as in the process of being defined.  */
   12264                 :    27846160 :   TYPE_BEING_DEFINED (type) = 1;
   12265                 :             : 
   12266                 :             :   /* We may be in the middle of deferred access check.  Disable
   12267                 :             :      it now.  */
   12268                 :    27846160 :   deferring_access_check_sentinel acs (dk_no_deferred);
   12269                 :             : 
   12270                 :             :   /* Determine what specialization of the original template to
   12271                 :             :      instantiate.  */
   12272                 :    27846160 :   t = most_specialized_partial_spec (type, tf_warning_or_error);
   12273                 :    27846160 :   if (t == error_mark_node)
   12274                 :             :     return error_mark_node;
   12275                 :    27846143 :   else if (t)
   12276                 :             :     {
   12277                 :             :       /* This TYPE is actually an instantiation of a partial
   12278                 :             :          specialization.  We replace the innermost set of ARGS with
   12279                 :             :          the arguments appropriate for substitution.  For example,
   12280                 :             :          given:
   12281                 :             : 
   12282                 :             :            template <class T> struct S {};
   12283                 :             :            template <class T> struct S<T*> {};
   12284                 :             : 
   12285                 :             :          and supposing that we are instantiating S<int*>, ARGS will
   12286                 :             :          presently be {int*} -- but we need {int}.  */
   12287                 :     5133104 :       pattern = TREE_TYPE (TI_TEMPLATE (t));
   12288                 :     5133104 :       args = TI_ARGS (t);
   12289                 :             :     }
   12290                 :             :   else
   12291                 :             :     {
   12292                 :    22713039 :       pattern = TREE_TYPE (templ);
   12293                 :    22713039 :       args = CLASSTYPE_TI_ARGS (type);
   12294                 :             :     }
   12295                 :             : 
   12296                 :             :   /* If the template we're instantiating is incomplete, then clearly
   12297                 :             :      there's nothing we can do.  */
   12298                 :    27846143 :   if (!COMPLETE_TYPE_P (pattern))
   12299                 :             :     {
   12300                 :             :       /* We can try again later.  */
   12301                 :      112210 :       TYPE_BEING_DEFINED (type) = 0;
   12302                 :      112210 :       return type;
   12303                 :             :     }
   12304                 :             : 
   12305                 :             :   /* If we've recursively instantiated too many templates, stop.  */
   12306                 :    27733933 :   if (! push_tinst_level (type))
   12307                 :           0 :     return type;
   12308                 :             : 
   12309                 :    27733914 :   int saved_unevaluated_operand = cp_unevaluated_operand;
   12310                 :    27733914 :   int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
   12311                 :             : 
   12312                 :    27733914 :   fn_context = decl_function_context (TYPE_MAIN_DECL (type));
   12313                 :             :   /* Also avoid push_to_top_level for a lambda in an NSDMI.  */
   12314                 :    54861509 :   if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
   12315                 :           0 :     fn_context = error_mark_node;
   12316                 :    27733914 :   if (!fn_context)
   12317                 :    27215341 :     push_to_top_level ();
   12318                 :             :   else
   12319                 :             :     {
   12320                 :      518573 :       cp_unevaluated_operand = 0;
   12321                 :      518573 :       c_inhibit_evaluation_warnings = 0;
   12322                 :             :     }
   12323                 :             : 
   12324                 :    27733914 :   mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type));
   12325                 :             : 
   12326                 :             :   /* Use #pragma pack from the template context.  */
   12327                 :    27733914 :   saved_maximum_field_alignment = maximum_field_alignment;
   12328                 :    27733914 :   maximum_field_alignment = TYPE_PRECISION (pattern);
   12329                 :             : 
   12330                 :    27733914 :   SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
   12331                 :             : 
   12332                 :             :   /* Set the input location to the most specialized template definition.
   12333                 :             :      This is needed if tsubsting causes an error.  */
   12334                 :    27733914 :   typedecl = TYPE_MAIN_DECL (pattern);
   12335                 :    27733914 :   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
   12336                 :    27733914 :     DECL_SOURCE_LOCATION (typedecl);
   12337                 :             : 
   12338                 :    27733914 :   set_instantiating_module (TYPE_NAME (type));
   12339                 :             : 
   12340                 :    27733914 :   TYPE_PACKED (type) = TYPE_PACKED (pattern);
   12341                 :    27733914 :   SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
   12342                 :    27733914 :   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
   12343                 :    27733914 :   CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
   12344                 :    27733914 :   if (ANON_AGGR_TYPE_P (pattern))
   12345                 :       56435 :     SET_ANON_AGGR_TYPE_P (type);
   12346                 :    27733914 :   if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
   12347                 :             :     {
   12348                 :    27469649 :       CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
   12349                 :    27469649 :       CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
   12350                 :             :       /* Adjust visibility for template arguments.  */
   12351                 :    27469649 :       determine_visibility (TYPE_MAIN_DECL (type));
   12352                 :             :     }
   12353                 :    27733914 :   if (CLASS_TYPE_P (type))
   12354                 :    27733914 :     CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
   12355                 :             : 
   12356                 :    27733914 :   pbinfo = TYPE_BINFO (pattern);
   12357                 :             : 
   12358                 :             :   /* We should never instantiate a nested class before its enclosing
   12359                 :             :      class; we need to look up the nested class by name before we can
   12360                 :             :      instantiate it, and that lookup should instantiate the enclosing
   12361                 :             :      class.  */
   12362                 :    27733914 :   gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
   12363                 :             :               || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
   12364                 :             : 
   12365                 :    27733914 :   base_list = NULL_TREE;
   12366                 :             :   /* Defer access checking while we substitute into the types named in
   12367                 :             :      the base-clause.  */
   12368                 :    27733914 :   push_deferring_access_checks (dk_deferred);
   12369                 :    27733914 :   if (BINFO_N_BASE_BINFOS (pbinfo))
   12370                 :             :     {
   12371                 :             :       tree pbase_binfo;
   12372                 :             :       int i;
   12373                 :             : 
   12374                 :             :       /* Substitute into each of the bases to determine the actual
   12375                 :             :          basetypes.  */
   12376                 :    33402844 :       for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
   12377                 :             :         {
   12378                 :    16986267 :           tree base;
   12379                 :    16986267 :           tree access = BINFO_BASE_ACCESS (pbinfo, i);
   12380                 :    16986267 :           tree expanded_bases = NULL_TREE;
   12381                 :    16986267 :           int idx, len = 1;
   12382                 :             : 
   12383                 :    16986267 :           if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
   12384                 :             :             {
   12385                 :         278 :               expanded_bases = 
   12386                 :         278 :                 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
   12387                 :             :                                        args, tf_error, NULL_TREE);
   12388                 :         278 :               if (expanded_bases == error_mark_node)
   12389                 :           0 :                 continue;
   12390                 :             : 
   12391                 :         278 :               len = TREE_VEC_LENGTH (expanded_bases);
   12392                 :             :             }
   12393                 :             : 
   12394                 :    33972881 :           for (idx = 0; idx < len; idx++)
   12395                 :             :             {
   12396                 :    16986614 :               if (expanded_bases)
   12397                 :             :                 /* Extract the already-expanded base class.  */
   12398                 :         625 :                 base = TREE_VEC_ELT (expanded_bases, idx);
   12399                 :             :               else
   12400                 :             :                 /* Substitute to figure out the base class.  */
   12401                 :    16985989 :                 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, 
   12402                 :             :                                NULL_TREE);
   12403                 :             : 
   12404                 :    16986614 :               if (base == error_mark_node)
   12405                 :          24 :                 continue;
   12406                 :             : 
   12407                 :    16986590 :               base_list = tree_cons (access, base, base_list);
   12408                 :    16986590 :               if (BINFO_VIRTUAL_P (pbase_binfo))
   12409                 :       41962 :                 TREE_TYPE (base_list) = integer_type_node;
   12410                 :             :             }
   12411                 :             :         }
   12412                 :             : 
   12413                 :             :       /* The list is now in reverse order; correct that.  */
   12414                 :    16416577 :       base_list = nreverse (base_list);
   12415                 :             :     }
   12416                 :             :   /* Now call xref_basetypes to set up all the base-class
   12417                 :             :      information.  */
   12418                 :    27733914 :   xref_basetypes (type, base_list);
   12419                 :             : 
   12420                 :    27733914 :   apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
   12421                 :             :                                   (int) ATTR_FLAG_TYPE_IN_PLACE,
   12422                 :             :                                   args, tf_error, NULL_TREE);
   12423                 :    27733914 :   fixup_attribute_variants (type);
   12424                 :             : 
   12425                 :             :   /* Now that our base classes are set up, enter the scope of the
   12426                 :             :      class, so that name lookups into base classes, etc. will work
   12427                 :             :      correctly.  This is precisely analogous to what we do in
   12428                 :             :      begin_class_definition when defining an ordinary non-template
   12429                 :             :      class, except we also need to push the enclosing classes.  */
   12430                 :    27733914 :   push_nested_class (type);
   12431                 :             : 
   12432                 :             :   /* Now check accessibility of the types named in its base-clause,
   12433                 :             :      relative to the scope of the class.  */
   12434                 :    27733914 :   pop_to_parent_deferring_access_checks ();
   12435                 :             : 
   12436                 :             :   /* A vector to hold members marked with attribute used. */
   12437                 :    27733914 :   auto_vec<tree> used;
   12438                 :             : 
   12439                 :             :   /* Now members are processed in the order of declaration.  */
   12440                 :    27733914 :   for (member = CLASSTYPE_DECL_LIST (pattern);
   12441                 :   194731394 :        member; member = TREE_CHAIN (member))
   12442                 :             :     {
   12443                 :   167001148 :       tree t = TREE_VALUE (member);
   12444                 :             : 
   12445                 :   167001148 :       if (TREE_PURPOSE (member))
   12446                 :             :         {
   12447                 :   164440832 :           if (TYPE_P (t))
   12448                 :             :             {
   12449                 :     2953683 :               if (LAMBDA_TYPE_P (t))
   12450                 :             :                 /* A closure type for a lambda in an NSDMI or default argument.
   12451                 :             :                    Ignore it; it will be regenerated when needed.  */
   12452                 :          28 :                 continue;
   12453                 :             : 
   12454                 :             :               /* If the member is a class template, we've
   12455                 :             :                  already substituted its type.  */
   12456                 :     1366623 :               if (CLASS_TYPE_P (t)
   12457                 :     3040101 :                   && CLASSTYPE_IS_TEMPLATE (t))
   12458                 :      665013 :                 continue;
   12459                 :             : 
   12460                 :     1008465 :               tree newtag = tsubst (t, args, tf_error, NULL_TREE);
   12461                 :     1008465 :               if (newtag == error_mark_node)
   12462                 :           0 :                 continue;
   12463                 :             : 
   12464                 :     1008465 :               if (TREE_CODE (newtag) != ENUMERAL_TYPE)
   12465                 :             :                 {
   12466                 :      701610 :                   tree name = TYPE_IDENTIFIER (t);
   12467                 :             : 
   12468                 :             :                   /* Now, install the tag.  We don't use pushtag
   12469                 :             :                      because that does too much work -- creating an
   12470                 :             :                      implicit typedef, which we've already done.  */
   12471                 :      701610 :                   set_identifier_type_value (name, TYPE_NAME (newtag));
   12472                 :      701610 :                   maybe_add_class_template_decl_list (type, newtag, false);
   12473                 :      701610 :                   TREE_PUBLIC (TYPE_NAME (newtag)) = true;
   12474                 :      701610 :                   determine_visibility (TYPE_NAME (newtag));
   12475                 :             :                 }
   12476                 :             :             }
   12477                 :   162767326 :           else if (DECL_DECLARES_FUNCTION_P (t))
   12478                 :             :             {
   12479                 :    85815747 :               tree r;
   12480                 :             : 
   12481                 :    85815747 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12482                 :    16564096 :                 ++processing_template_decl;
   12483                 :    85815747 :               r = tsubst (t, args, tf_error, NULL_TREE);
   12484                 :    85815747 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12485                 :    16564096 :                 --processing_template_decl;
   12486                 :             : 
   12487                 :    85815747 :               set_current_access_from_decl (r);
   12488                 :    85815747 :               finish_member_declaration (r);
   12489                 :             :               /* Instantiate members marked with attribute used.  */
   12490                 :    85815747 :               if (r != error_mark_node && DECL_PRESERVE_P (r))
   12491                 :          26 :                 used.safe_push (r);
   12492                 :    85815747 :               if (TREE_CODE (r) == FUNCTION_DECL
   12493                 :    85815747 :                   && DECL_OMP_DECLARE_REDUCTION_P (r))
   12494                 :         168 :                 cp_check_omp_declare_reduction (r);
   12495                 :             :             }
   12496                 :    76951579 :           else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
   12497                 :    79905342 :                    && LAMBDA_TYPE_P (TREE_TYPE (t)))
   12498                 :             :             /* A closure type for a lambda in an NSDMI or default argument.
   12499                 :             :                Ignore it; it will be regenerated when needed.  */;
   12500                 :             :           else
   12501                 :             :             {
   12502                 :             :               /* Build new TYPE_FIELDS.  */
   12503                 :    76951541 :               if (TREE_CODE (t) == STATIC_ASSERT)
   12504                 :     3579232 :                 tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE);
   12505                 :    73372309 :               else if (TREE_CODE (t) != CONST_DECL)
   12506                 :             :                 {
   12507                 :    73372309 :                   tree r;
   12508                 :    73372309 :                   tree vec = NULL_TREE;
   12509                 :    73372309 :                   int len = 1;
   12510                 :             : 
   12511                 :    73372309 :                   gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
   12512                 :             :                   /* The file and line for this declaration, to
   12513                 :             :                      assist in error message reporting.  Since we
   12514                 :             :                      called push_tinst_level above, we don't need to
   12515                 :             :                      restore these.  */
   12516                 :    73372309 :                   input_location = DECL_SOURCE_LOCATION (t);
   12517                 :             : 
   12518                 :    73372309 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   12519                 :     2828491 :                     ++processing_template_decl;
   12520                 :    73372309 :                   r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
   12521                 :    73372241 :                   if (TREE_CODE (t) == TEMPLATE_DECL)
   12522                 :     2828491 :                     --processing_template_decl;
   12523                 :             : 
   12524                 :    73372241 :                   if (TREE_CODE (r) == TREE_VEC)
   12525                 :             :                     {
   12526                 :             :                       /* A capture pack became multiple fields.  */
   12527                 :         232 :                       vec = r;
   12528                 :         232 :                       len = TREE_VEC_LENGTH (vec);
   12529                 :             :                     }
   12530                 :             : 
   12531                 :   146741209 :                   for (int i = 0; i < len; ++i)
   12532                 :             :                     {
   12533                 :    73372568 :                       if (vec)
   12534                 :         559 :                         r = TREE_VEC_ELT (vec, i);
   12535                 :    73372568 :                       if (VAR_P (r))
   12536                 :             :                         {
   12537                 :             :                           /* In [temp.inst]:
   12538                 :             : 
   12539                 :             :                              [t]he initialization (and any associated
   12540                 :             :                              side-effects) of a static data member does
   12541                 :             :                              not occur unless the static data member is
   12542                 :             :                              itself used in a way that requires the
   12543                 :             :                              definition of the static data member to
   12544                 :             :                              exist.
   12545                 :             : 
   12546                 :             :                              Therefore, we do not substitute into the
   12547                 :             :                              initialized for the static data member here.  */
   12548                 :     2331580 :                           finish_static_data_member_decl
   12549                 :     2331580 :                             (r,
   12550                 :             :                              /*init=*/NULL_TREE,
   12551                 :             :                              /*init_const_expr_p=*/false,
   12552                 :             :                              /*asmspec_tree=*/NULL_TREE,
   12553                 :             :                              /*flags=*/0);
   12554                 :             :                           /* Instantiate members marked with attribute used. */
   12555                 :     2331580 :                           if (r != error_mark_node && DECL_PRESERVE_P (r))
   12556                 :           8 :                             used.safe_push (r);
   12557                 :             :                         }
   12558                 :    71040988 :                       else if (TREE_CODE (r) == FIELD_DECL)
   12559                 :             :                         {
   12560                 :             :                           /* Determine whether R has a valid type and can be
   12561                 :             :                              completed later.  If R is invalid, then its type
   12562                 :             :                              is replaced by error_mark_node.  */
   12563                 :     5859772 :                           tree rtype = TREE_TYPE (r);
   12564                 :     5859772 :                           if (can_complete_type_without_circularity (rtype))
   12565                 :     5859759 :                             complete_type (rtype);
   12566                 :             : 
   12567                 :     5856172 :                           if (!complete_or_array_type_p (rtype))
   12568                 :             :                             {
   12569                 :             :                               /* If R's type couldn't be completed and
   12570                 :             :                                  it isn't a flexible array member (whose
   12571                 :             :                                  type is incomplete by definition) give
   12572                 :             :                                  an error.  */
   12573                 :          36 :                               cxx_incomplete_type_error (r, rtype);
   12574                 :          36 :                               TREE_TYPE (r) = error_mark_node;
   12575                 :             :                             }
   12576                 :     5856136 :                           else if (TREE_CODE (rtype) == ARRAY_TYPE
   12577                 :      163429 :                                    && TYPE_DOMAIN (rtype) == NULL_TREE
   12578                 :     5856136 :                                    && (TREE_CODE (type) == UNION_TYPE
   12579                 :          55 :                                        || TREE_CODE (type) == QUAL_UNION_TYPE))
   12580                 :             :                             {
   12581                 :           5 :                               error ("flexible array member %qD in union", r);
   12582                 :           5 :                               TREE_TYPE (r) = error_mark_node;
   12583                 :             :                             }
   12584                 :     5856131 :                           else if (!verify_type_context (input_location,
   12585                 :             :                                                          TCTX_FIELD, rtype))
   12586                 :           0 :                             TREE_TYPE (r) = error_mark_node;
   12587                 :             :                         }
   12588                 :             : 
   12589                 :             :                       /* If it is a TYPE_DECL for a class-scoped
   12590                 :             :                          ENUMERAL_TYPE, such a thing will already have
   12591                 :             :                          been added to the field list by tsubst_enum
   12592                 :             :                          in finish_member_declaration case above.  */
   12593                 :    73368968 :                       if (!(TREE_CODE (r) == TYPE_DECL
   12594                 :    61098974 :                             && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
   12595                 :      473149 :                             && DECL_ARTIFICIAL (r)))
   12596                 :             :                         {
   12597                 :    73062113 :                           set_current_access_from_decl (r);
   12598                 :    73062113 :                           finish_member_declaration (r);
   12599                 :             :                         }
   12600                 :             :                     }
   12601                 :             :                 }
   12602                 :             :             }
   12603                 :             :         }
   12604                 :             :       else
   12605                 :             :         {
   12606                 :     2177224 :           if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
   12607                 :     4174377 :               || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   12608                 :             :             {
   12609                 :             :               /* Build new CLASSTYPE_FRIEND_CLASSES.  */
   12610                 :             : 
   12611                 :      946258 :               tree friend_type = t;
   12612                 :      946258 :               if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   12613                 :             :                 {
   12614                 :             :                   /* template <class T> friend class C;  */
   12615                 :      563166 :                   friend_type = tsubst_friend_class (friend_type, args);
   12616                 :             :                 }
   12617                 :      383092 :               else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
   12618                 :             :                 {
   12619                 :             :                   /* template <class T> friend class C::D;  */
   12620                 :          12 :                   friend_type = tsubst (friend_type, args,
   12621                 :             :                                         tf_warning_or_error, NULL_TREE);
   12622                 :          12 :                   if (TREE_CODE (friend_type) == TEMPLATE_DECL)
   12623                 :          12 :                     friend_type = TREE_TYPE (friend_type);
   12624                 :             :                 }
   12625                 :      383080 :               else if (TREE_CODE (friend_type) == TYPENAME_TYPE
   12626                 :      383080 :                        || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
   12627                 :             :                 {
   12628                 :             :                   /* This could be either
   12629                 :             : 
   12630                 :             :                        friend class T::C;
   12631                 :             : 
   12632                 :             :                      when dependent_type_p is false or
   12633                 :             : 
   12634                 :             :                        template <class U> friend class T::C;
   12635                 :             : 
   12636                 :             :                      otherwise.  */
   12637                 :             :                   /* Bump processing_template_decl in case this is something like
   12638                 :             :                      template <class T> friend struct A<T>::B.  */
   12639                 :        2016 :                   ++processing_template_decl;
   12640                 :        2016 :                   friend_type = tsubst (friend_type, args,
   12641                 :             :                                         tf_warning_or_error, NULL_TREE);
   12642                 :        2016 :                   --processing_template_decl;
   12643                 :             :                 }
   12644                 :      381064 :               else if (uses_template_parms (friend_type))
   12645                 :             :                 /* friend class C<T>;  */
   12646                 :      358828 :                 friend_type = tsubst (friend_type, args,
   12647                 :             :                                       tf_warning_or_error, NULL_TREE);
   12648                 :             :               
   12649                 :             :               /* Otherwise it's
   12650                 :             : 
   12651                 :             :                    friend class C;
   12652                 :             : 
   12653                 :             :                  where C is already declared or
   12654                 :             : 
   12655                 :             :                    friend class C<int>;
   12656                 :             : 
   12657                 :             :                  We don't have to do anything in these cases.  */
   12658                 :             : 
   12659                 :      946258 :               if (friend_type != error_mark_node)
   12660                 :      946243 :                 make_friend_class (type, friend_type, /*complain=*/false);
   12661                 :             :             }
   12662                 :             :           else
   12663                 :             :             {
   12664                 :             :               /* Build new DECL_FRIENDLIST.  */
   12665                 :     1614058 :               tree r;
   12666                 :             : 
   12667                 :             :               /* The file and line for this declaration, to
   12668                 :             :                  assist in error message reporting.  Since we
   12669                 :             :                  called push_tinst_level above, we don't need to
   12670                 :             :                  restore these.  */
   12671                 :     1614058 :               input_location = DECL_SOURCE_LOCATION (t);
   12672                 :             : 
   12673                 :     1614058 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12674                 :             :                 {
   12675                 :      960319 :                   ++processing_template_decl;
   12676                 :      960319 :                   push_deferring_access_checks (dk_no_check);
   12677                 :             :                 }
   12678                 :             : 
   12679                 :     1614058 :               r = tsubst_friend_function (t, args);
   12680                 :     1614058 :               add_friend (type, r, /*complain=*/false);
   12681                 :     1614058 :               if (TREE_CODE (t) == TEMPLATE_DECL)
   12682                 :             :                 {
   12683                 :      960319 :                   pop_deferring_access_checks ();
   12684                 :      960319 :                   --processing_template_decl;
   12685                 :             :                 }
   12686                 :             :             }
   12687                 :             :         }
   12688                 :             :     }
   12689                 :             : 
   12690                 :    27730246 :   if (fn_context)
   12691                 :             :     {
   12692                 :             :       /* Restore these before substituting into the lambda capture
   12693                 :             :          initializers.  */
   12694                 :      518573 :       cp_unevaluated_operand = saved_unevaluated_operand;
   12695                 :      518573 :       c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
   12696                 :             :     }
   12697                 :             : 
   12698                 :             :   /* Set the file and line number information to whatever is given for
   12699                 :             :      the class itself.  This puts error messages involving generated
   12700                 :             :      implicit functions at a predictable point, and the same point
   12701                 :             :      that would be used for non-template classes.  */
   12702                 :    27730246 :   input_location = DECL_SOURCE_LOCATION (typedecl);
   12703                 :             : 
   12704                 :    27730246 :   unreverse_member_declarations (type);
   12705                 :    27730246 :   finish_struct_1 (type);
   12706                 :    27730246 :   TYPE_BEING_DEFINED (type) = 0;
   12707                 :             : 
   12708                 :             :   /* Remember if instantiating this class ran into errors, so we can avoid
   12709                 :             :      instantiating member functions in limit_bad_template_recursion.  We set
   12710                 :             :      this flag even if the problem was in another instantiation triggered by
   12711                 :             :      this one, as that will likely also cause trouble for member functions.  */
   12712                 :    27730246 :   if (errorcount + sorrycount > current_tinst_level->errors)
   12713                 :        1092 :     CLASSTYPE_ERRONEOUS (type) = true;
   12714                 :             : 
   12715                 :             :   /* We don't instantiate default arguments for member functions.  14.7.1:
   12716                 :             : 
   12717                 :             :      The implicit instantiation of a class template specialization causes
   12718                 :             :      the implicit instantiation of the declarations, but not of the
   12719                 :             :      definitions or default arguments, of the class member functions,
   12720                 :             :      member classes, static data members and member templates....  */
   12721                 :             : 
   12722                 :    27730246 :   perform_instantiation_time_access_checks (pattern, args);
   12723                 :    27730246 :   perform_deferred_access_checks (tf_warning_or_error);
   12724                 :             : 
   12725                 :             :   /* Now that we've gone through all the members, instantiate those
   12726                 :             :      marked with attribute used.  We must do this in the context of
   12727                 :             :      the class -- not the context we pushed from, as that might be
   12728                 :             :      inside a template and change the behaviour of mark_used.  */
   12729                 :    27730340 :   for (tree x : used)
   12730                 :          34 :     mark_used (x);
   12731                 :             : 
   12732                 :    27730246 :   pop_nested_class ();
   12733                 :    27730246 :   maximum_field_alignment = saved_maximum_field_alignment;
   12734                 :    27730246 :   if (!fn_context)
   12735                 :    27211673 :     pop_from_top_level ();
   12736                 :    27730246 :   pop_tinst_level ();
   12737                 :             : 
   12738                 :             :   /* The vtable for a template class can be emitted in any translation
   12739                 :             :      unit in which the class is instantiated.  When there is no key
   12740                 :             :      method, however, finish_struct_1 will already have added TYPE to
   12741                 :             :      the keyed_classes.  */
   12742                 :    27730246 :   if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
   12743                 :           0 :     vec_safe_push (keyed_classes, type);
   12744                 :             : 
   12745                 :    27730246 :   return type;
   12746                 :   747239282 : }
   12747                 :             : 
   12748                 :             : tree
   12749                 :  2528335217 : tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   12750                 :             : {
   12751                 :  2528335217 :   tree r;
   12752                 :             : 
   12753                 :  2528335217 :   if (!t)
   12754                 :             :     r = t;
   12755                 :  2500132993 :   else if (TYPE_P (t))
   12756                 :  2287323765 :     r = tsubst (t, args, complain, in_decl);
   12757                 :             :   else
   12758                 :             :     {
   12759                 :   212809228 :       if (!(complain & tf_warning))
   12760                 :   144803922 :         ++c_inhibit_evaluation_warnings;
   12761                 :   212809228 :       r = tsubst_expr (t, args, complain, in_decl);
   12762                 :   212809190 :       if (!(complain & tf_warning))
   12763                 :   144803892 :         --c_inhibit_evaluation_warnings;
   12764                 :             :     }
   12765                 :             : 
   12766                 :  2528335179 :   return r;
   12767                 :             : }
   12768                 :             : 
   12769                 :             : /* Given a function parameter pack TMPL_PARM and some function parameters
   12770                 :             :    instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
   12771                 :             :    and set *SPEC_P to point at the next point in the list.  */
   12772                 :             : 
   12773                 :             : tree
   12774                 :      539096 : extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
   12775                 :             : {
   12776                 :             :   /* Collect all of the extra "packed" parameters into an
   12777                 :             :      argument pack.  */
   12778                 :      539096 :   tree argpack;
   12779                 :      539096 :   tree spec_parm = *spec_p;
   12780                 :      539096 :   int len;
   12781                 :             : 
   12782                 :      860851 :   for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
   12783                 :      321900 :     if (tmpl_parm
   12784                 :      321900 :         && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
   12785                 :             :       break;
   12786                 :             : 
   12787                 :      539096 :   spec_parm = *spec_p;
   12788                 :      539096 :   if (len == 1 && DECL_PACK_P (spec_parm))
   12789                 :             :     {
   12790                 :             :       /* The instantiation is still a parameter pack; don't wrap it in a
   12791                 :             :          NONTYPE_ARGUMENT_PACK.  */
   12792                 :         930 :       argpack = spec_parm;
   12793                 :         930 :       spec_parm = DECL_CHAIN (spec_parm);
   12794                 :             :     }
   12795                 :             :   else
   12796                 :             :     {
   12797                 :             :       /* Fill in PARMVEC with all of the parameters.  */
   12798                 :      538166 :       tree parmvec = make_tree_vec (len);
   12799                 :      538166 :       argpack = make_node (NONTYPE_ARGUMENT_PACK);
   12800                 :      858991 :       for (int i = 0; i < len; i++)
   12801                 :             :         {
   12802                 :      320825 :           tree elt = spec_parm;
   12803                 :      320825 :           if (DECL_PACK_P (elt))
   12804                 :           0 :             elt = make_pack_expansion (elt);
   12805                 :      320825 :           TREE_VEC_ELT (parmvec, i) = elt;
   12806                 :      320825 :           spec_parm = DECL_CHAIN (spec_parm);
   12807                 :             :         }
   12808                 :             : 
   12809                 :             :       /* Build the argument packs.  */
   12810                 :      538166 :       ARGUMENT_PACK_ARGS (argpack) = parmvec;
   12811                 :             :     }
   12812                 :      539096 :   *spec_p = spec_parm;
   12813                 :             : 
   12814                 :      539096 :   return argpack;
   12815                 :             : }
   12816                 :             : 
   12817                 :             : /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
   12818                 :             :    NONTYPE_ARGUMENT_PACK.  */
   12819                 :             : 
   12820                 :             : static tree
   12821                 :        2584 : make_fnparm_pack (tree spec_parm)
   12822                 :             : {
   12823                 :           0 :   return extract_fnparm_pack (NULL_TREE, &spec_parm);
   12824                 :             : }
   12825                 :             : 
   12826                 :             : /* Return 1 if the Ith element of the argument pack ARG_PACK is a
   12827                 :             :    pack expansion with no extra args, 2 if it has extra args, or 0
   12828                 :             :    if it is not a pack expansion.  */
   12829                 :             : 
   12830                 :             : static int
   12831                 :    20732490 : argument_pack_element_is_expansion_p (tree arg_pack, int i)
   12832                 :             : {
   12833                 :    20732490 :   if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   12834                 :             :     /* We're being called before this happens in tsubst_pack_expansion.  */
   12835                 :           0 :     arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   12836                 :    20732490 :   tree vec = ARGUMENT_PACK_ARGS (arg_pack);
   12837                 :    20732490 :   if (i >= TREE_VEC_LENGTH (vec))
   12838                 :             :     return 0;
   12839                 :    20732490 :   tree elt = TREE_VEC_ELT (vec, i);
   12840                 :    20732490 :   if (DECL_P (elt))
   12841                 :             :     /* A decl pack is itself an expansion.  */
   12842                 :      650821 :     elt = TREE_TYPE (elt);
   12843                 :    20732490 :   if (!PACK_EXPANSION_P (elt))
   12844                 :             :     return 0;
   12845                 :      449275 :   if (PACK_EXPANSION_EXTRA_ARGS (elt))
   12846                 :           3 :     return 2;
   12847                 :             :   return 1;
   12848                 :             : }
   12849                 :             : 
   12850                 :             : 
   12851                 :             : /* Creates and return an ARGUMENT_PACK_SELECT tree node.  */
   12852                 :             : 
   12853                 :             : static tree
   12854                 :     4657886 : make_argument_pack_select (tree arg_pack, unsigned index)
   12855                 :             : {
   12856                 :     4657886 :   tree aps = make_node (ARGUMENT_PACK_SELECT);
   12857                 :             : 
   12858                 :     4657886 :   ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
   12859                 :     4657886 :   ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   12860                 :             : 
   12861                 :     4657886 :   return aps;
   12862                 :             : }
   12863                 :             : 
   12864                 :             : /*  This is a subroutine of tsubst_pack_expansion.
   12865                 :             : 
   12866                 :             :     It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
   12867                 :             :     mechanism to store the (non complete list of) arguments of the
   12868                 :             :     substitution and return a non substituted pack expansion, in order
   12869                 :             :     to wait for when we have enough arguments to really perform the
   12870                 :             :     substitution.  */
   12871                 :             : 
   12872                 :             : static bool
   12873                 :    16565029 : use_pack_expansion_extra_args_p (tree t,
   12874                 :             :                                  tree parm_packs,
   12875                 :             :                                  int arg_pack_len,
   12876                 :             :                                  bool has_empty_arg)
   12877                 :             : {
   12878                 :    16565029 :   if (has_empty_arg
   12879                 :    16565029 :       && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t))
   12880                 :             :     return true;
   12881                 :             : 
   12882                 :             :   /* If one pack has an expansion and another pack has a normal
   12883                 :             :      argument or if one pack has an empty argument and an another
   12884                 :             :      one hasn't then tsubst_pack_expansion cannot perform the
   12885                 :             :      substitution and need to fall back on the
   12886                 :             :      PACK_EXPANSION_EXTRA mechanism.  */
   12887                 :    16565024 :   if (parm_packs == NULL_TREE)
   12888                 :             :     return false;
   12889                 :     5496586 :   else if (has_empty_arg)
   12890                 :             :     {
   12891                 :             :       /* If all the actual packs are pack expansions, we can still
   12892                 :             :          subsitute directly.  */
   12893                 :       61155 :       for (tree p = parm_packs; p; p = TREE_CHAIN (p))
   12894                 :             :         {
   12895                 :       61155 :           tree a = TREE_VALUE (p);
   12896                 :       61155 :           if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
   12897                 :           0 :             a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
   12898                 :       61155 :           a = ARGUMENT_PACK_ARGS (a);
   12899                 :       61155 :           if (TREE_VEC_LENGTH (a) == 1)
   12900                 :        1313 :             a = TREE_VEC_ELT (a, 0);
   12901                 :       61155 :           if (PACK_EXPANSION_P (a))
   12902                 :           0 :             continue;
   12903                 :             :           return true;
   12904                 :             :         }
   12905                 :             :       return false;
   12906                 :             :     }
   12907                 :             : 
   12908                 :    16023572 :   for (int i = 0 ; i < arg_pack_len; ++i)
   12909                 :             :     {
   12910                 :             :       bool has_expansion_arg = false;
   12911                 :             :       bool has_non_expansion_arg = false;
   12912                 :    11006885 :       for (tree parm_pack = parm_packs;
   12913                 :    21595029 :            parm_pack;
   12914                 :    11006885 :            parm_pack = TREE_CHAIN (parm_pack))
   12915                 :             :         {
   12916                 :    11006888 :           tree arg = TREE_VALUE (parm_pack);
   12917                 :             : 
   12918                 :    11006888 :           int exp = argument_pack_element_is_expansion_p (arg, i);
   12919                 :    11006888 :           if (exp == 2)
   12920                 :             :             /* We can't substitute a pack expansion with extra args into
   12921                 :             :                our pattern.  */
   12922                 :             :             return true;
   12923                 :    11006885 :           else if (exp)
   12924                 :             :             has_expansion_arg = true;
   12925                 :             :           else
   12926                 :    10782249 :             has_non_expansion_arg = true;
   12927                 :             :         }
   12928                 :             : 
   12929                 :    10588141 :       if (has_expansion_arg && has_non_expansion_arg)
   12930                 :             :         {
   12931                 :           0 :           gcc_checking_assert (false);
   12932                 :             :           return true;
   12933                 :             :         }
   12934                 :             :     }
   12935                 :             :   return false;
   12936                 :             : }
   12937                 :             : 
   12938                 :             : /* [temp.variadic]/6 says that:
   12939                 :             : 
   12940                 :             :        The instantiation of a pack expansion [...]
   12941                 :             :        produces a list E1,E2, ..., En, where N is the number of elements
   12942                 :             :        in the pack expansion parameters.
   12943                 :             : 
   12944                 :             :    This subroutine of tsubst_pack_expansion produces one of these Ei.
   12945                 :             : 
   12946                 :             :    PATTERN is the pattern of the pack expansion.  PARM_PACKS is a
   12947                 :             :    TREE_LIST in which each TREE_PURPOSE is a parameter pack of
   12948                 :             :    PATTERN, and each TREE_VALUE is its corresponding argument pack.
   12949                 :             :    INDEX is the index 'i' of the element Ei to produce.  ARGS,
   12950                 :             :    COMPLAIN, and IN_DECL are the same parameters as for the
   12951                 :             :    tsubst_pack_expansion function.
   12952                 :             : 
   12953                 :             :    The function returns the resulting Ei upon successful completion,
   12954                 :             :    or error_mark_node.
   12955                 :             : 
   12956                 :             :    Note that this function possibly modifies the ARGS parameter, so
   12957                 :             :    it's the responsibility of the caller to restore it.  */
   12958                 :             : 
   12959                 :             : static tree
   12960                 :     9306858 : gen_elem_of_pack_expansion_instantiation (tree pattern,
   12961                 :             :                                           tree parm_packs,
   12962                 :             :                                           unsigned index,
   12963                 :             :                                           tree args /* This parm gets
   12964                 :             :                                                        modified.  */,
   12965                 :             :                                           tsubst_flags_t complain,
   12966                 :             :                                           tree in_decl)
   12967                 :             : {
   12968                 :     9306858 :   tree t;
   12969                 :     9306858 :   bool ith_elem_is_expansion = false;
   12970                 :             : 
   12971                 :             :   /* For each parameter pack, change the substitution of the parameter
   12972                 :             :      pack to the ith argument in its argument pack, then expand the
   12973                 :             :      pattern.  */
   12974                 :    19032460 :   for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
   12975                 :             :     {
   12976                 :     9725602 :       tree parm = TREE_PURPOSE (pack);
   12977                 :     9725602 :       tree arg_pack = TREE_VALUE (pack);
   12978                 :     9725602 :       tree aps;                 /* instance of ARGUMENT_PACK_SELECT.  */
   12979                 :             : 
   12980                 :    19451204 :       ith_elem_is_expansion |=
   12981                 :     9725602 :         argument_pack_element_is_expansion_p (arg_pack, index);
   12982                 :             : 
   12983                 :             :       /* Select the Ith argument from the pack.  */
   12984                 :     9725602 :       if (TREE_CODE (parm) == PARM_DECL
   12985                 :     9400357 :           || VAR_P (parm)
   12986                 :     9400298 :           || TREE_CODE (parm) == FIELD_DECL)
   12987                 :             :         {
   12988                 :      325304 :           if (index == 0)
   12989                 :             :             {
   12990                 :      254407 :               aps = make_argument_pack_select (arg_pack, index);
   12991                 :      254407 :               if (!mark_used (parm, complain) && !(complain & tf_error))
   12992                 :           0 :                 return error_mark_node;
   12993                 :      254407 :               register_local_specialization (aps, parm);
   12994                 :             :             }
   12995                 :             :           else
   12996                 :       70897 :             aps = retrieve_local_specialization (parm);
   12997                 :             :         }
   12998                 :             :       else
   12999                 :             :         {
   13000                 :     9400298 :           int idx, level;
   13001                 :     9400298 :           template_parm_level_and_index (parm, &level, &idx);
   13002                 :             : 
   13003                 :     9400298 :           if (index == 0)
   13004                 :             :             {
   13005                 :     4403479 :               aps = make_argument_pack_select (arg_pack, index);
   13006                 :             :               /* Update the corresponding argument.  */
   13007                 :     8806958 :               TMPL_ARG (args, level, idx) = aps;
   13008                 :             :             }
   13009                 :             :           else
   13010                 :             :             /* Re-use the ARGUMENT_PACK_SELECT.  */
   13011                 :     9993638 :             aps = TMPL_ARG (args, level, idx);
   13012                 :             :         }
   13013                 :     9725602 :       ARGUMENT_PACK_SELECT_INDEX (aps) = index;
   13014                 :             :     }
   13015                 :             : 
   13016                 :             :   /* Substitute into the PATTERN with the (possibly altered)
   13017                 :             :      arguments.  */
   13018                 :     9306858 :   if (pattern == in_decl)
   13019                 :             :     /* Expanding a fixed parameter pack from
   13020                 :             :        coerce_template_parameter_pack.  */
   13021                 :          83 :     t = tsubst_decl (pattern, args, complain);
   13022                 :     9306775 :   else if (pattern == error_mark_node)
   13023                 :             :     t = error_mark_node;
   13024                 :     9306772 :   else if (!TYPE_P (pattern))
   13025                 :      795709 :     t = tsubst_expr (pattern, args, complain, in_decl);
   13026                 :             :   else
   13027                 :             :     {
   13028                 :     8511063 :       t = tsubst (pattern, args, complain, in_decl);
   13029                 :     8511063 :       if (is_auto (t) && !ith_elem_is_expansion)
   13030                 :             :         /* When expanding the fake auto... pack expansion from add_capture, we
   13031                 :             :            need to mark that the expansion is no longer a pack.  */
   13032                 :          12 :         TEMPLATE_TYPE_PARAMETER_PACK (t) = false;
   13033                 :             :     }
   13034                 :             : 
   13035                 :             :   /*  If the Ith argument pack element is a pack expansion, then
   13036                 :             :       the Ith element resulting from the substituting is going to
   13037                 :             :       be a pack expansion as well.  */
   13038                 :     9306858 :   if (ith_elem_is_expansion)
   13039                 :      202210 :     t = make_pack_expansion (t, complain);
   13040                 :             : 
   13041                 :             :   return t;
   13042                 :             : }
   13043                 :             : 
   13044                 :             : /* When the unexpanded parameter pack in a fold expression expands to an empty
   13045                 :             :    sequence, the value of the expression is as follows; the program is
   13046                 :             :    ill-formed if the operator is not listed in this table.
   13047                 :             : 
   13048                 :             :    &&   true
   13049                 :             :    ||   false
   13050                 :             :    ,    void()  */
   13051                 :             : 
   13052                 :             : tree
   13053                 :       40109 : expand_empty_fold (tree t, tsubst_flags_t complain)
   13054                 :             : {
   13055                 :       40109 :   tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
   13056                 :       40109 :   if (!FOLD_EXPR_MODIFY_P (t))
   13057                 :       40087 :     switch (code)
   13058                 :             :       {
   13059                 :       40037 :       case TRUTH_ANDIF_EXPR:
   13060                 :       40037 :         return boolean_true_node;
   13061                 :          10 :       case TRUTH_ORIF_EXPR:
   13062                 :          10 :         return boolean_false_node;
   13063                 :           4 :       case COMPOUND_EXPR:
   13064                 :           4 :         return void_node;
   13065                 :             :       default:
   13066                 :             :         break;
   13067                 :             :       }
   13068                 :             : 
   13069                 :          58 :   if (complain & tf_error)
   13070                 :          58 :     error_at (location_of (t),
   13071                 :             :               "fold of empty expansion over %O", code);
   13072                 :          58 :   return error_mark_node;
   13073                 :             : }
   13074                 :             : 
   13075                 :             : /* Given a fold-expression T and a current LEFT and RIGHT operand,
   13076                 :             :    form an expression that combines the two terms using the
   13077                 :             :    operator of T. */
   13078                 :             : 
   13079                 :             : static tree
   13080                 :      146129 : fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
   13081                 :             : {
   13082                 :      146129 :   tree_code code = FOLD_EXPR_OP (t);
   13083                 :             : 
   13084                 :      146129 :   tree lookups = templated_operator_saved_lookups (t);
   13085                 :             : 
   13086                 :             :   // Handle compound assignment operators.
   13087                 :      146129 :   if (FOLD_EXPR_MODIFY_P (t))
   13088                 :         344 :     return build_x_modify_expr (input_location, left, code, right,
   13089                 :             :                                 lookups, complain);
   13090                 :             : 
   13091                 :      145785 :   warning_sentinel s(warn_parentheses);
   13092                 :      145785 :   switch (code)
   13093                 :             :     {
   13094                 :         795 :     case COMPOUND_EXPR:
   13095                 :         795 :       return build_x_compound_expr (input_location, left, right,
   13096                 :         795 :                                     lookups, complain);
   13097                 :      144990 :     default:
   13098                 :      144990 :       return build_x_binary_op (input_location, code,
   13099                 :      144990 :                                 left, TREE_CODE (left),
   13100                 :      144990 :                                 right, TREE_CODE (right),
   13101                 :             :                                 lookups, /*overload=*/NULL,
   13102                 :             :                                 complain);
   13103                 :             :     }
   13104                 :      145785 : }
   13105                 :             : 
   13106                 :             : /* Substitute ARGS into the pack of a fold expression T. */
   13107                 :             : 
   13108                 :             : static inline tree
   13109                 :      163172 : tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13110                 :             : {
   13111                 :      163172 :   return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
   13112                 :             : }
   13113                 :             : 
   13114                 :             : /* Substitute ARGS into the pack of a fold expression T. */
   13115                 :             : 
   13116                 :             : static inline tree
   13117                 :        1000 : tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13118                 :             : {
   13119                 :        1000 :   return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl);
   13120                 :             : }
   13121                 :             : 
   13122                 :             : /* Expand a PACK of arguments into a grouped as left fold.
   13123                 :             :    Given a pack containing elements A0, A1, ..., An and an
   13124                 :             :    operator @, this builds the expression:
   13125                 :             : 
   13126                 :             :       ((A0 @ A1) @ A2) ... @ An
   13127                 :             : 
   13128                 :             :    Note that PACK must not be empty.
   13129                 :             : 
   13130                 :             :    The operator is defined by the original fold expression T. */
   13131                 :             : 
   13132                 :             : static tree
   13133                 :        1822 : expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
   13134                 :             : {
   13135                 :        1822 :   tree left = TREE_VEC_ELT (pack, 0);
   13136                 :        7081 :   for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
   13137                 :             :     {
   13138                 :        5259 :       tree right = TREE_VEC_ELT (pack, i);
   13139                 :        5259 :       left = fold_expression (t, left, right, complain);
   13140                 :             :     }
   13141                 :        1822 :   return left;
   13142                 :             : }
   13143                 :             : 
   13144                 :             : /* Substitute into a unary left fold expression. */
   13145                 :             : 
   13146                 :             : static tree
   13147                 :        1421 : tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13148                 :             :                         tree in_decl)
   13149                 :             : {
   13150                 :        1421 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13151                 :        1421 :   if (pack == error_mark_node)
   13152                 :             :     return error_mark_node;
   13153                 :        1421 :   if (PACK_EXPANSION_P (pack))
   13154                 :             :     {
   13155                 :         156 :       tree r = copy_node (t);
   13156                 :         156 :       FOLD_EXPR_PACK (r) = pack;
   13157                 :         156 :       return r;
   13158                 :             :     }
   13159                 :        1265 :   if (TREE_VEC_LENGTH (pack) == 0)
   13160                 :          76 :     return expand_empty_fold (t, complain);
   13161                 :             :   else
   13162                 :        1189 :     return expand_left_fold (t, pack, complain);
   13163                 :             : }
   13164                 :             : 
   13165                 :             : /* Substitute into a binary left fold expression.
   13166                 :             : 
   13167                 :             :    Do ths by building a single (non-empty) vector of argumnts and
   13168                 :             :    building the expression from those elements. */
   13169                 :             : 
   13170                 :             : static tree
   13171                 :         695 : tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
   13172                 :             :                          tree in_decl)
   13173                 :             : {
   13174                 :         695 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13175                 :         695 :   if (pack == error_mark_node)
   13176                 :             :     return error_mark_node;
   13177                 :         695 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13178                 :         695 :   if (init == error_mark_node)
   13179                 :             :     return error_mark_node;
   13180                 :             : 
   13181                 :         695 :   if (PACK_EXPANSION_P (pack))
   13182                 :             :     {
   13183                 :          62 :       tree r = copy_node (t);
   13184                 :          62 :       FOLD_EXPR_PACK (r) = pack;
   13185                 :          62 :       FOLD_EXPR_INIT (r) = init;
   13186                 :          62 :       return r;
   13187                 :             :     }
   13188                 :             : 
   13189                 :         633 :   tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
   13190                 :         633 :   TREE_VEC_ELT (vec, 0) = init;
   13191                 :        1370 :   for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
   13192                 :         737 :     TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
   13193                 :             : 
   13194                 :         633 :   return expand_left_fold (t, vec, complain);
   13195                 :             : }
   13196                 :             : 
   13197                 :             : /* Expand a PACK of arguments into a grouped as right fold.
   13198                 :             :    Given a pack containing elementns A0, A1, ..., and an
   13199                 :             :    operator @, this builds the expression:
   13200                 :             : 
   13201                 :             :       A0@ ... (An-2 @ (An-1 @ An))
   13202                 :             : 
   13203                 :             :    Note that PACK must not be empty.
   13204                 :             : 
   13205                 :             :    The operator is defined by the original fold expression T. */
   13206                 :             : 
   13207                 :             : tree
   13208                 :      109088 : expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
   13209                 :             : {
   13210                 :             :   // Build the expression.
   13211                 :      109088 :   int n = TREE_VEC_LENGTH (pack);
   13212                 :      109088 :   tree right = TREE_VEC_ELT (pack, n - 1);
   13213                 :      249958 :   for (--n; n != 0; --n)
   13214                 :             :     {
   13215                 :      140870 :       tree left = TREE_VEC_ELT (pack, n - 1);
   13216                 :      140870 :       right = fold_expression (t, left, right, complain);
   13217                 :             :     }
   13218                 :      109088 :   return right;
   13219                 :             : }
   13220                 :             : 
   13221                 :             : /* Substitute into a unary right fold expression. */
   13222                 :             : 
   13223                 :             : static tree
   13224                 :      160751 : tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13225                 :             :                          tree in_decl)
   13226                 :             : {
   13227                 :      160751 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13228                 :      160751 :   if (pack == error_mark_node)
   13229                 :             :     return error_mark_node;
   13230                 :      160748 :   if (PACK_EXPANSION_P (pack))
   13231                 :             :     {
   13232                 :       11872 :       tree r = copy_node (t);
   13233                 :       11872 :       FOLD_EXPR_PACK (r) = pack;
   13234                 :       11872 :       return r;
   13235                 :             :     }
   13236                 :      148876 :   if (TREE_VEC_LENGTH (pack) == 0)
   13237                 :       40033 :     return expand_empty_fold (t, complain);
   13238                 :             :   else
   13239                 :      108843 :     return expand_right_fold (t, pack, complain);
   13240                 :             : }
   13241                 :             : 
   13242                 :             : /* Substitute into a binary right fold expression.
   13243                 :             : 
   13244                 :             :    Do ths by building a single (non-empty) vector of arguments and
   13245                 :             :    building the expression from those elements. */
   13246                 :             : 
   13247                 :             : static tree
   13248                 :         305 : tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
   13249                 :             :                          tree in_decl)
   13250                 :             : {
   13251                 :         305 :   tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
   13252                 :         305 :   if (pack == error_mark_node)
   13253                 :             :     return error_mark_node;
   13254                 :         305 :   tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
   13255                 :         305 :   if (init == error_mark_node)
   13256                 :             :     return error_mark_node;
   13257                 :             : 
   13258                 :         305 :   if (PACK_EXPANSION_P (pack))
   13259                 :             :     {
   13260                 :          60 :       tree r = copy_node (t);
   13261                 :          60 :       FOLD_EXPR_PACK (r) = pack;
   13262                 :          60 :       FOLD_EXPR_INIT (r) = init;
   13263                 :          60 :       return r;
   13264                 :             :     }
   13265                 :             : 
   13266                 :         245 :   int n = TREE_VEC_LENGTH (pack);
   13267                 :         245 :   tree vec = make_tree_vec (n + 1);
   13268                 :         558 :   for (int i = 0; i < n; ++i)
   13269                 :         313 :     TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
   13270                 :         245 :   TREE_VEC_ELT (vec, n) = init;
   13271                 :             : 
   13272                 :         245 :   return expand_right_fold (t, vec, complain);
   13273                 :             : }
   13274                 :             : 
   13275                 :             : /* Walk through the pattern of a pack expansion, adding everything in
   13276                 :             :    local_specializations to a list.  */
   13277                 :             : 
   13278                 :             : class el_data
   13279                 :             : {
   13280                 :             : public:
   13281                 :             :   /* Set of variables declared within the pattern.  */
   13282                 :             :   hash_set<tree> internal;
   13283                 :             :   /* Set of AST nodes that have been visited by the traversal.  */
   13284                 :             :   hash_set<tree> visited;
   13285                 :             :   /* List of local_specializations used within the pattern.  */
   13286                 :             :   tree extra;
   13287                 :             :   tsubst_flags_t complain;
   13288                 :             :   /* True iff we don't want to walk into unevaluated contexts.  */
   13289                 :             :   bool skip_unevaluated_operands = false;
   13290                 :             :   /* The unevaluated contexts that we avoided walking.  */
   13291                 :             :   auto_vec<tree> skipped_trees;
   13292                 :             : 
   13293                 :        7586 :   el_data (tsubst_flags_t c)
   13294                 :        7586 :     : extra (NULL_TREE), complain (c) {}
   13295                 :             : };
   13296                 :             : static tree
   13297                 :     1123110 : extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
   13298                 :             : {
   13299                 :     1123110 :   el_data &data = *reinterpret_cast<el_data*>(data_);
   13300                 :     1123110 :   tree *extra = &data.extra;
   13301                 :     1123110 :   tsubst_flags_t complain = data.complain;
   13302                 :             : 
   13303                 :     1123110 :   if (data.skip_unevaluated_operands
   13304                 :     1123110 :       && unevaluated_p (TREE_CODE (*tp)))
   13305                 :             :     {
   13306                 :        8190 :       data.skipped_trees.safe_push (*tp);
   13307                 :        8190 :       *walk_subtrees = 0;
   13308                 :        8190 :       return NULL_TREE;
   13309                 :             :     }
   13310                 :             : 
   13311                 :     1114920 :   if (TYPE_P (*tp) && typedef_variant_p (*tp))
   13312                 :             :     /* Remember local typedefs (85214).  */
   13313                 :        3116 :     tp = &TYPE_NAME (*tp);
   13314                 :             : 
   13315                 :     1114920 :   if (has_extra_args_mechanism_p (*tp))
   13316                 :             :     /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
   13317                 :             :        potentially see a previously captured local in an evaluated context
   13318                 :             :        that's really only used in an unevaluated context (PR114303).  This
   13319                 :             :        means callers of build_extra_args need to clear *_EXTRA_ARGS of the
   13320                 :             :        outermost tree.  Nested *_EXTRA_ARGS should naturally be empty since
   13321                 :             :        the outermost (extra-args) tree will intercept any substitution before
   13322                 :             :        a nested tree can.  */
   13323                 :       22429 :     gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE);
   13324                 :             : 
   13325                 :     1114920 :   if (TREE_CODE (*tp) == DECL_EXPR)
   13326                 :             :     {
   13327                 :       27330 :       tree decl = DECL_EXPR_DECL (*tp);
   13328                 :       27330 :       data.internal.add (decl);
   13329                 :       27330 :       if (VAR_P (decl)
   13330                 :       26671 :           && DECL_DECOMPOSITION_P (decl)
   13331                 :       27333 :           && TREE_TYPE (decl) != error_mark_node)
   13332                 :             :         {
   13333                 :           3 :           gcc_assert (DECL_NAME (decl) == NULL_TREE);
   13334                 :           3 :           for (tree decl2 = DECL_CHAIN (decl);
   13335                 :           9 :                decl2
   13336                 :           6 :                && VAR_P (decl2)
   13337                 :           6 :                && DECL_DECOMPOSITION_P (decl2)
   13338                 :           6 :                && DECL_NAME (decl2)
   13339                 :          15 :                && TREE_TYPE (decl2) != error_mark_node;
   13340                 :           6 :                decl2 = DECL_CHAIN (decl2))
   13341                 :             :             {
   13342                 :           6 :               gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
   13343                 :           6 :               data.internal.add (decl2);
   13344                 :             :             }
   13345                 :             :         }
   13346                 :             :     }
   13347                 :     1087590 :   else if (TREE_CODE (*tp) == LAMBDA_EXPR)
   13348                 :             :     {
   13349                 :             :       /* Since we defer implicit capture, look in the parms and body.  */
   13350                 :         176 :       tree fn = lambda_function (*tp);
   13351                 :         176 :       cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
   13352                 :             :                     &data.visited);
   13353                 :         176 :       cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
   13354                 :             :                     &data.visited);
   13355                 :             :     }
   13356                 :     1087414 :   else if (tree spec = retrieve_local_specialization (*tp))
   13357                 :             :     {
   13358                 :       15572 :       if (data.internal.contains (*tp))
   13359                 :             :         /* Don't mess with variables declared within the pattern.  */
   13360                 :             :         return NULL_TREE;
   13361                 :       15564 :       if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13362                 :             :         {
   13363                 :             :           /* Maybe pull out the PARM_DECL for a partial instantiation.  */
   13364                 :          13 :           tree args = ARGUMENT_PACK_ARGS (spec);
   13365                 :          13 :           if (TREE_VEC_LENGTH (args) == 1)
   13366                 :             :             {
   13367                 :           5 :               tree elt = TREE_VEC_ELT (args, 0);
   13368                 :           5 :               if (PACK_EXPANSION_P (elt))
   13369                 :           0 :                 elt = PACK_EXPANSION_PATTERN (elt);
   13370                 :           5 :               if (DECL_PACK_P (elt))
   13371                 :             :                 spec = elt;
   13372                 :             :             }
   13373                 :          13 :           if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
   13374                 :             :             {
   13375                 :             :               /* Handle lambda capture here, since we aren't doing any
   13376                 :             :                  substitution now, and so tsubst_copy won't call
   13377                 :             :                  process_outer_var_ref.  */
   13378                 :          13 :               tree args = ARGUMENT_PACK_ARGS (spec);
   13379                 :          13 :               int len = TREE_VEC_LENGTH (args);
   13380                 :          28 :               for (int i = 0; i < len; ++i)
   13381                 :             :                 {
   13382                 :          15 :                   tree arg = TREE_VEC_ELT (args, i);
   13383                 :          15 :                   tree carg = arg;
   13384                 :          15 :                   if (outer_automatic_var_p (arg))
   13385                 :          15 :                     carg = process_outer_var_ref (arg, complain);
   13386                 :          15 :                   if (carg != arg)
   13387                 :             :                     {
   13388                 :             :                       /* Make a new NONTYPE_ARGUMENT_PACK of the capture
   13389                 :             :                          proxies.  */
   13390                 :          15 :                       if (i == 0)
   13391                 :             :                         {
   13392                 :          10 :                           spec = copy_node (spec);
   13393                 :          10 :                           args = copy_node (args);
   13394                 :          10 :                           ARGUMENT_PACK_ARGS (spec) = args;
   13395                 :          10 :                           register_local_specialization (spec, *tp);
   13396                 :             :                         }
   13397                 :          15 :                       TREE_VEC_ELT (args, i) = carg;
   13398                 :             :                     }
   13399                 :             :                 }
   13400                 :             :             }
   13401                 :             :         }
   13402                 :       15564 :       if (outer_automatic_var_p (spec))
   13403                 :          11 :         spec = process_outer_var_ref (spec, complain);
   13404                 :       15564 :       *extra = tree_cons (*tp, spec, *extra);
   13405                 :             :     }
   13406                 :             :   return NULL_TREE;
   13407                 :             : }
   13408                 :             : static tree
   13409                 :        7586 : extract_local_specs (tree pattern, tsubst_flags_t complain)
   13410                 :             : {
   13411                 :        7586 :   el_data data (complain);
   13412                 :             :   /* Walk the pattern twice, ignoring unevaluated operands the first time
   13413                 :             :      around, so that if a local specialization appears in both an evaluated
   13414                 :             :      and unevaluated context we prefer to process it in the evaluated context
   13415                 :             :      (since e.g. process_outer_var_ref is a no-op inside an unevaluated
   13416                 :             :      context).  */
   13417                 :        7586 :   data.skip_unevaluated_operands = true;
   13418                 :        7586 :   cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
   13419                 :             :   /* Now walk the unevaluated contexts we skipped the first time around.  */
   13420                 :        7586 :   data.skip_unevaluated_operands = false;
   13421                 :       30640 :   for (tree t : data.skipped_trees)
   13422                 :             :     {
   13423                 :        8190 :       data.visited.remove (t);
   13424                 :        8190 :       cp_walk_tree (&t, extract_locals_r, &data, &data.visited);
   13425                 :             :     }
   13426                 :       15172 :   return data.extra;
   13427                 :        7586 : }
   13428                 :             : 
   13429                 :             : /* Extract any uses of local_specializations from PATTERN and add them to ARGS
   13430                 :             :    for use in PACK_EXPANSION_EXTRA_ARGS.  */
   13431                 :             : 
   13432                 :             : tree
   13433                 :       68713 : build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
   13434                 :             : {
   13435                 :             :   /* Make a copy of the extra arguments so that they won't get changed
   13436                 :             :      out from under us.  */
   13437                 :       68713 :   tree extra = preserve_args (copy_template_args (args), /*cow_p=*/false);
   13438                 :       68713 :   if (local_specializations)
   13439                 :        7586 :     if (tree locals = extract_local_specs (pattern, complain))
   13440                 :        6505 :       extra = tree_cons (NULL_TREE, extra, locals);
   13441                 :       68713 :   return extra;
   13442                 :             : }
   13443                 :             : 
   13444                 :             : /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
   13445                 :             :    normal template args to ARGS.  */
   13446                 :             : 
   13447                 :             : tree
   13448                 :    68754225 : add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl)
   13449                 :             : {
   13450                 :    68754225 :   if (extra && TREE_CODE (extra) == TREE_LIST)
   13451                 :             :     {
   13452                 :      345967 :       for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
   13453                 :             :         {
   13454                 :             :           /* The partial instantiation involved local declarations collected in
   13455                 :             :              extract_local_specs; map from the general template to our local
   13456                 :             :              context.  */
   13457                 :      242615 :           tree gen = TREE_PURPOSE (elt);
   13458                 :      242615 :           tree inst = TREE_VALUE (elt);
   13459                 :      242615 :           if (DECL_P (inst))
   13460                 :      242602 :             if (tree local = retrieve_local_specialization (inst))
   13461                 :      242615 :               inst = local;
   13462                 :             :           /* else inst is already a full instantiation of the pack.  */
   13463                 :      242615 :           register_local_specialization (inst, gen);
   13464                 :             :         }
   13465                 :      103352 :       gcc_assert (!TREE_PURPOSE (extra));
   13466                 :      103352 :       extra = TREE_VALUE (extra);
   13467                 :             :     }
   13468                 :    68754225 :   if (uses_template_parms (extra))
   13469                 :             :     {
   13470                 :             :       /* This can happen after dependent substitution into a
   13471                 :             :          requires-expr or a lambda that uses constexpr if.  */
   13472                 :           7 :       extra = tsubst_template_args (extra, args, complain, in_decl);
   13473                 :           7 :       args = add_outermost_template_args (args, extra);
   13474                 :             :     }
   13475                 :             :   else
   13476                 :    68754218 :     args = add_to_template_args (extra, args);
   13477                 :    68754225 :   return args;
   13478                 :             : }
   13479                 :             : 
   13480                 :             : /* Substitute ARGS into T, which is an pack expansion
   13481                 :             :    (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
   13482                 :             :    TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
   13483                 :             :    (if only a partial substitution could be performed) or
   13484                 :             :    ERROR_MARK_NODE if there was an error.  */
   13485                 :             : tree
   13486                 :    64051094 : tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   13487                 :             :                        tree in_decl)
   13488                 :             : {
   13489                 :    64051094 :   tree pattern;
   13490                 :    64051094 :   tree pack, packs = NULL_TREE;
   13491                 :    64051094 :   bool unsubstituted_packs = false;
   13492                 :    64051094 :   int i, len = -1;
   13493                 :    64051094 :   tree result;
   13494                 :    64051094 :   bool need_local_specializations = false;
   13495                 :    64051094 :   int levels;
   13496                 :             : 
   13497                 :    64051094 :   gcc_assert (PACK_EXPANSION_P (t));
   13498                 :    64051094 :   pattern = PACK_EXPANSION_PATTERN (t);
   13499                 :             : 
   13500                 :             :   /* Add in any args remembered from an earlier partial instantiation.  */
   13501                 :    64051094 :   args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl);
   13502                 :             : 
   13503                 :   128102188 :   levels = TMPL_ARGS_DEPTH (args);
   13504                 :             : 
   13505                 :             :   /* Determine the argument packs that will instantiate the parameter
   13506                 :             :      packs used in the expansion expression. While we're at it,
   13507                 :             :      compute the number of arguments to be expanded and make sure it
   13508                 :             :      is consistent.  */
   13509                 :   186035250 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
   13510                 :    61768928 :        pack = TREE_CHAIN (pack))
   13511                 :             :     {
   13512                 :    64585982 :       tree parm_pack = TREE_VALUE (pack);
   13513                 :    64585982 :       tree arg_pack = NULL_TREE;
   13514                 :    64585982 :       tree orig_arg = NULL_TREE;
   13515                 :    64585982 :       int level = 0;
   13516                 :             : 
   13517                 :    64585982 :       if (TREE_CODE (parm_pack) == BASES)
   13518                 :             :         {
   13519                 :          27 :           gcc_assert (parm_pack == pattern);
   13520                 :          27 :           tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl);
   13521                 :          27 :           if (BASES_DIRECT (parm_pack))
   13522                 :     2817051 :             return calculate_direct_bases (type, complain);
   13523                 :             :           else
   13524                 :          12 :             return calculate_bases (type, complain);
   13525                 :             :         }
   13526                 :    64585955 :       else if (builtin_pack_call_p (parm_pack))
   13527                 :             :         {
   13528                 :      101623 :           if (parm_pack != pattern)
   13529                 :             :             {
   13530                 :           2 :               if (complain & tf_error)
   13531                 :           2 :                 sorry ("%qE is not the entire pattern of the pack expansion",
   13532                 :             :                        parm_pack);
   13533                 :           2 :               return error_mark_node;
   13534                 :             :             }
   13535                 :      101621 :           return expand_builtin_pack_call (parm_pack, args,
   13536                 :      101621 :                                            complain, in_decl);
   13537                 :             :         }
   13538                 :    64484332 :       else if (TREE_CODE (parm_pack) == PARM_DECL)
   13539                 :             :         {
   13540                 :             :           /* We know we have correct local_specializations if this
   13541                 :             :              expansion is at function scope, or if we're dealing with a
   13542                 :             :              local parameter in a requires expression; for the latter,
   13543                 :             :              tsubst_requires_expr set it up appropriately.  */
   13544                 :      539502 :           if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
   13545                 :      535511 :             arg_pack = retrieve_local_specialization (parm_pack);
   13546                 :             :           else
   13547                 :             :             /* We can't rely on local_specializations for a parameter
   13548                 :             :                name used later in a function declaration (such as in a
   13549                 :             :                late-specified return type).  Even if it exists, it might
   13550                 :             :                have the wrong value for a recursive call.  */
   13551                 :             :             need_local_specializations = true;
   13552                 :             : 
   13553                 :      535511 :           if (!arg_pack)
   13554                 :             :             {
   13555                 :             :               /* This parameter pack was used in an unevaluated context.  Just
   13556                 :             :                  make a dummy decl, since it's only used for its type.  */
   13557                 :        3991 :               ++cp_unevaluated_operand;
   13558                 :        3991 :               arg_pack = tsubst_decl (parm_pack, args, complain);
   13559                 :        3991 :               --cp_unevaluated_operand;
   13560                 :        3991 :               if (arg_pack && DECL_PACK_P (arg_pack))
   13561                 :             :                 /* Partial instantiation of the parm_pack, we can't build
   13562                 :             :                    up an argument pack yet.  */
   13563                 :             :                 arg_pack = NULL_TREE;
   13564                 :             :               else
   13565                 :        2584 :                 arg_pack = make_fnparm_pack (arg_pack);
   13566                 :             :             }
   13567                 :      535511 :           else if (DECL_PACK_P (arg_pack))
   13568                 :             :             /* This argument pack isn't fully instantiated yet.  */
   13569                 :             :             arg_pack = NULL_TREE;
   13570                 :             :         }
   13571                 :    63944830 :       else if (is_capture_proxy (parm_pack))
   13572                 :             :         {
   13573                 :          60 :           arg_pack = retrieve_local_specialization (parm_pack);
   13574                 :          60 :           if (DECL_PACK_P (arg_pack))
   13575                 :             :             arg_pack = NULL_TREE;
   13576                 :             :         }
   13577                 :             :       else
   13578                 :             :         {
   13579                 :    63944770 :           int idx;
   13580                 :    63944770 :           template_parm_level_and_index (parm_pack, &level, &idx);
   13581                 :    63944770 :           if (level <= levels)
   13582                 :   110909516 :             arg_pack = TMPL_ARG (args, level, idx);
   13583                 :             : 
   13584                 :    52816119 :           if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
   13585                 :    55454788 :               && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
   13586                 :          29 :             arg_pack = NULL_TREE;
   13587                 :             :         }
   13588                 :             : 
   13589                 :    75613012 :       orig_arg = arg_pack;
   13590                 :    64481738 :       if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
   13591                 :        1955 :         arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
   13592                 :             : 
   13593                 :    53353058 :       if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
   13594                 :             :         /* This can only happen if we forget to expand an argument
   13595                 :             :            pack somewhere else. Just return an error, silently.  */
   13596                 :             :         {
   13597                 :           1 :           result = make_tree_vec (1);
   13598                 :           1 :           TREE_VEC_ELT (result, 0) = error_mark_node;
   13599                 :           1 :           return result;
   13600                 :             :         }
   13601                 :             : 
   13602                 :    64484331 :       if (arg_pack)
   13603                 :             :         {
   13604                 :    53353057 :           int my_len = 
   13605                 :    53353057 :             TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
   13606                 :             : 
   13607                 :             :           /* Don't bother trying to do a partial substitution with
   13608                 :             :              incomplete packs; we'll try again after deduction.  */
   13609                 :    53353057 :           if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
   13610                 :             :             return t;
   13611                 :             : 
   13612                 :    50637692 :           if (len < 0)
   13613                 :             :             len = my_len;
   13614                 :      472055 :           else if (len != my_len)
   13615                 :             :             {
   13616                 :          35 :               if (!(complain & tf_error))
   13617                 :             :                 /* Fail quietly.  */;
   13618                 :          13 :               else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
   13619                 :           7 :                 error ("mismatched argument pack lengths while expanding %qT",
   13620                 :             :                        pattern);
   13621                 :             :               else
   13622                 :           6 :                 error ("mismatched argument pack lengths while expanding %qE",
   13623                 :             :                        pattern);
   13624                 :          35 :               return error_mark_node;
   13625                 :             :             }
   13626                 :             : 
   13627                 :             :           /* Keep track of the parameter packs and their corresponding
   13628                 :             :              argument packs.  */
   13629                 :    50637657 :           packs = tree_cons (parm_pack, arg_pack, packs);
   13630                 :    50637657 :           TREE_TYPE (packs) = orig_arg;
   13631                 :             :         }
   13632                 :             :       else
   13633                 :             :         {
   13634                 :             :           /* We can't substitute for this parameter pack.  We use a flag as
   13635                 :             :              well as the missing_level counter because function parameter
   13636                 :             :              packs don't have a level.  */
   13637                 :    11131274 :           gcc_assert (processing_template_decl || is_auto (parm_pack));
   13638                 :             :           unsubstituted_packs = true;
   13639                 :             :         }
   13640                 :             :     }
   13641                 :             : 
   13642                 :             :   /* If the expansion is just T..., return the matching argument pack, unless
   13643                 :             :      we need to call convert_from_reference on all the elements.  This is an
   13644                 :             :      important optimization; see c++/68422.  */
   13645                 :    61234040 :   if (!unsubstituted_packs
   13646                 :   111338482 :       && TREE_PURPOSE (packs) == pattern)
   13647                 :             :     {
   13648                 :    44692923 :       tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
   13649                 :             : 
   13650                 :             :       /* If the argument pack is a single pack expansion, pull it out.  */
   13651                 :    44692923 :       if (TREE_VEC_LENGTH (args) == 1
   13652                 :    44692923 :           && pack_expansion_args_count (args))
   13653                 :             :         {
   13654                 :     2820809 :           tree arg = TREE_VEC_ELT (args, 0);
   13655                 :     2820809 :           if (PACK_EXPANSION_SIZEOF_P (t)
   13656                 :     2820809 :               && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
   13657                 :             :             /* Except if this isn't a simple sizeof...(T) which gets sZ
   13658                 :             :                mangling, keep the TREE_VEC to get sP mangling.  */;
   13659                 :             :           else
   13660                 :     2820800 :             return TREE_VEC_ELT (args, 0);
   13661                 :             :         }
   13662                 :             : 
   13663                 :             :       /* Types need no adjustment, nor does sizeof..., and if we still have
   13664                 :             :          some pack expansion args we won't do anything yet.  */
   13665                 :    41872123 :       if (TREE_CODE (t) == TYPE_PACK_EXPANSION
   13666                 :     2178460 :           || PACK_EXPANSION_SIZEOF_P (t)
   13667                 :    44049974 :           || pack_expansion_args_count (args))
   13668                 :             :         return args;
   13669                 :             :       /* Also optimize expression pack expansions if we can tell that the
   13670                 :             :          elements won't have reference type.  */
   13671                 :     2090897 :       tree type = TREE_TYPE (pattern);
   13672                 :     2090897 :       if (type && !TYPE_REF_P (type)
   13673                 :             :           && !PACK_EXPANSION_P (type)
   13674                 :             :           && !WILDCARD_TYPE_P (type))
   13675                 :             :         return args;
   13676                 :             :       /* Otherwise use the normal path so we get convert_from_reference.  */
   13677                 :             :     }
   13678                 :             : 
   13679                 :             :   /* We cannot expand this expansion expression, because we don't have
   13680                 :             :      all of the argument packs we need.  */
   13681                 :    16565029 :   if (use_pack_expansion_extra_args_p (t, packs, len, unsubstituted_packs))
   13682                 :             :     {
   13683                 :             :       /* We got some full packs, but we can't substitute them in until we
   13684                 :             :          have values for all the packs.  So remember these until then.  */
   13685                 :             : 
   13686                 :       61163 :       t = make_pack_expansion (pattern, complain);
   13687                 :       61163 :       PACK_EXPANSION_EXTRA_ARGS (t)
   13688                 :       61163 :         = build_extra_args (pattern, args, complain);
   13689                 :       61163 :       return t;
   13690                 :             :     }
   13691                 :             : 
   13692                 :             :   /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
   13693                 :             :      type, so create our own local specializations map; the current map is
   13694                 :             :      either NULL or (in the case of recursive unification) might have
   13695                 :             :      bindings that we don't want to use or alter.  */
   13696                 :    16503866 :   local_specialization_stack lss (need_local_specializations
   13697                 :    33003751 :                                   ? lss_blank : lss_nop);
   13698                 :             : 
   13699                 :    16503866 :   if (unsubstituted_packs)
   13700                 :             :     {
   13701                 :             :       /* There were no real arguments, we're just replacing a parameter
   13702                 :             :          pack with another version of itself. Substitute into the
   13703                 :             :          pattern and return a PACK_EXPANSION_*. The caller will need to
   13704                 :             :          deal with that.  */
   13705                 :    11068438 :       if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
   13706                 :      504887 :         result = tsubst_expr (pattern, args, complain, in_decl);
   13707                 :             :       else
   13708                 :    10563551 :         result = tsubst (pattern, args, complain, in_decl);
   13709                 :    11068438 :       result = make_pack_expansion (result, complain);
   13710                 :    11068438 :       PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
   13711                 :    11068438 :       PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
   13712                 :    11068438 :       if (PACK_EXPANSION_AUTO_P (t))
   13713                 :             :         {
   13714                 :             :           /* This is a fake auto... pack expansion created in add_capture with
   13715                 :             :              _PACKS that don't appear in the pattern.  Copy one over.  */
   13716                 :           2 :           packs = PACK_EXPANSION_PARAMETER_PACKS (t);
   13717                 :           1 :           pack = retrieve_local_specialization (TREE_VALUE (packs));
   13718                 :           1 :           gcc_checking_assert (DECL_PACK_P (pack));
   13719                 :           2 :           PACK_EXPANSION_PARAMETER_PACKS (result)
   13720                 :           1 :             = build_tree_list (NULL_TREE, pack);
   13721                 :           1 :           PACK_EXPANSION_AUTO_P (result) = true;
   13722                 :             :         }
   13723                 :    11068438 :       return result;
   13724                 :             :     }
   13725                 :             : 
   13726                 :     5435428 :   gcc_assert (len >= 0);
   13727                 :             : 
   13728                 :             :   /* For each argument in each argument pack, substitute into the
   13729                 :             :      pattern.  */
   13730                 :     5435428 :   result = make_tree_vec (len);
   13731                 :     5435428 :   tree elem_args = copy_template_args (args);
   13732                 :    19425970 :   for (i = 0; i < len; ++i)
   13733                 :             :     {
   13734                 :     9306858 :       t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
   13735                 :             :                                                     i,
   13736                 :             :                                                     elem_args, complain,
   13737                 :             :                                                     in_decl);
   13738                 :     9306858 :       TREE_VEC_ELT (result, i) = t;
   13739                 :     9306858 :       if (t == error_mark_node)
   13740                 :             :         {
   13741                 :             :           result = error_mark_node;
   13742                 :             :           break;
   13743                 :             :         }
   13744                 :             :     }
   13745                 :             : 
   13746                 :             :   /* Update ARGS to restore the substitution from parameter packs to
   13747                 :             :      their argument packs.  */
   13748                 :    11342869 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   13749                 :             :     {
   13750                 :     5907441 :       tree parm = TREE_PURPOSE (pack);
   13751                 :             : 
   13752                 :     5907441 :       if (TREE_CODE (parm) == PARM_DECL
   13753                 :     5370817 :           || VAR_P (parm)
   13754                 :     5370764 :           || TREE_CODE (parm) == FIELD_DECL)
   13755                 :      536677 :         register_local_specialization (TREE_TYPE (pack), parm);
   13756                 :             :       else
   13757                 :             :         {
   13758                 :     5370764 :           int idx, level;
   13759                 :             : 
   13760                 :     5370764 :           if (TREE_VALUE (pack) == NULL_TREE)
   13761                 :           0 :             continue;
   13762                 :             : 
   13763                 :     5370764 :           template_parm_level_and_index (parm, &level, &idx);
   13764                 :             : 
   13765                 :             :           /* Update the corresponding argument.  */
   13766                 :    10741528 :           if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   13767                 :      850297 :             TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
   13768                 :      850297 :               TREE_TYPE (pack);
   13769                 :             :           else
   13770                 :     4520467 :             TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
   13771                 :             :         }
   13772                 :             :     }
   13773                 :             : 
   13774                 :             :   /* If the dependent pack arguments were such that we end up with only a
   13775                 :             :      single pack expansion again, there's no need to keep it in a TREE_VEC.  */
   13776                 :     1148326 :   if (len == 1 && TREE_CODE (result) == TREE_VEC
   13777                 :     6581286 :       && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
   13778                 :      193040 :     return TREE_VEC_ELT (result, 0);
   13779                 :             : 
   13780                 :             :   return result;
   13781                 :    16503866 : }
   13782                 :             : 
   13783                 :             : /* Make an argument pack out of the TREE_VEC VEC.  */
   13784                 :             : 
   13785                 :             : static tree
   13786                 :          27 : make_argument_pack (tree vec)
   13787                 :             : {
   13788                 :          27 :   tree pack;
   13789                 :             : 
   13790                 :          27 :   if (TYPE_P (TREE_VEC_ELT (vec, 0)))
   13791                 :          15 :     pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   13792                 :             :   else
   13793                 :             :     {
   13794                 :          12 :       pack = make_node (NONTYPE_ARGUMENT_PACK);
   13795                 :          12 :       TREE_CONSTANT (pack) = 1;
   13796                 :             :     }
   13797                 :          27 :   ARGUMENT_PACK_ARGS (pack) = vec;
   13798                 :          27 :   return pack;
   13799                 :             : }
   13800                 :             : 
   13801                 :             : /* Return an exact copy of template args T that can be modified
   13802                 :             :    independently.  */
   13803                 :             : 
   13804                 :             : static tree
   13805                 :    16770096 : copy_template_args (tree t)
   13806                 :             : {
   13807                 :    16770096 :   if (t == error_mark_node)
   13808                 :             :     return t;
   13809                 :             : 
   13810                 :    16770096 :   int len = TREE_VEC_LENGTH (t);
   13811                 :    16770096 :   tree new_vec = make_tree_vec (len);
   13812                 :             : 
   13813                 :    53380389 :   for (int i = 0; i < len; ++i)
   13814                 :             :     {
   13815                 :    36610293 :       tree elt = TREE_VEC_ELT (t, i);
   13816                 :    36610293 :       if (elt && TREE_CODE (elt) == TREE_VEC)
   13817                 :     1471433 :         elt = copy_template_args (elt);
   13818                 :    36610293 :       TREE_VEC_ELT (new_vec, i) = elt;
   13819                 :             :     }
   13820                 :             : 
   13821                 :    33540192 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
   13822                 :    16770096 :     = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   13823                 :             : 
   13824                 :    16770096 :   return new_vec;
   13825                 :             : }
   13826                 :             : 
   13827                 :             : /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg.  */
   13828                 :             : 
   13829                 :             : tree
   13830                 :    94669519 : tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
   13831                 :             :                       tree in_decl)
   13832                 :             : {
   13833                 :             :   /* This flag is used only during deduction, and we don't expect to
   13834                 :             :      substitute such ARGUMENT_PACKs.  */
   13835                 :    94669519 :   gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg));
   13836                 :             : 
   13837                 :             :   /* Substitute into each of the arguments.  */
   13838                 :    94669519 :   tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
   13839                 :             :                                          args, complain, in_decl);
   13840                 :    94669519 :   if (pack_args == error_mark_node)
   13841                 :             :     return error_mark_node;
   13842                 :             : 
   13843                 :    93490465 :   if (pack_args == ARGUMENT_PACK_ARGS (orig_arg))
   13844                 :             :     return orig_arg;
   13845                 :             : 
   13846                 :             :   /* If we're substituting into a generic ARGUMENT_PACK for a variadic
   13847                 :             :      template parameter, we might be able to avoid allocating a new
   13848                 :             :      ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS
   13849                 :             :      if the substituted result is identical to it.  */
   13850                 :    89259470 :   if (tree parm = template_arg_to_parm (orig_arg))
   13851                 :             :     {
   13852                 :    36136620 :       int level, index;
   13853                 :    36136620 :       template_parm_level_and_index (parm, &level, &index);
   13854                 :   105734046 :       if (TMPL_ARGS_DEPTH (args) >= level)
   13855                 :    63492960 :         if (tree arg = TMPL_ARG (args, level, index))
   13856                 :    29835439 :           if (TREE_CODE (arg) == TREE_CODE (orig_arg)
   13857                 :    29835439 :               && ARGUMENT_PACK_ARGS (arg) == pack_args)
   13858                 :             :             {
   13859                 :    27700648 :               gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg));
   13860                 :    27700648 :               return arg;
   13861                 :             :             }
   13862                 :             :     }
   13863                 :             : 
   13864                 :    61558822 :   tree new_arg;
   13865                 :    61558822 :   if (TYPE_P (orig_arg))
   13866                 :             :     {
   13867                 :    60890931 :       new_arg = cxx_make_type (TREE_CODE (orig_arg));
   13868                 :    60890931 :       SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
   13869                 :             :     }
   13870                 :             :   else
   13871                 :             :     {
   13872                 :      667891 :       new_arg = make_node (TREE_CODE (orig_arg));
   13873                 :      667891 :       TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
   13874                 :             :     }
   13875                 :    61558822 :   ARGUMENT_PACK_ARGS (new_arg) = pack_args;
   13876                 :             :   return new_arg;
   13877                 :             : }
   13878                 :             : 
   13879                 :             : /* Substitute ARGS into the vector or list of template arguments T.  */
   13880                 :             : 
   13881                 :             : tree
   13882                 :  1248520104 : tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   13883                 :             : {
   13884                 :  1248520104 :   if (t == error_mark_node)
   13885                 :             :     return error_mark_node;
   13886                 :             : 
   13887                 :             :   /* In "sizeof(X<I>)" we need to evaluate "I".  */
   13888                 :  1248520104 :   cp_evaluated ev;
   13889                 :             : 
   13890                 :  1248520104 :   const int len = TREE_VEC_LENGTH (t);
   13891                 :  1248520104 :   tree *elts = XALLOCAVEC (tree, len);
   13892                 :  1248520104 :   int expanded_len_adjust = 0;
   13893                 :             : 
   13894                 :             :   /* True iff the substituted result is identical to T.  */
   13895                 :  1248520104 :   bool const_subst_p = true;
   13896                 :             : 
   13897                 :  3889792402 :   for (int i = 0; i < len; i++)
   13898                 :             :     {
   13899                 :  2644936201 :       tree orig_arg = TREE_VEC_ELT (t, i);
   13900                 :  2644936201 :       tree new_arg;
   13901                 :             : 
   13902                 :  2644936201 :       if (!orig_arg)
   13903                 :             :         new_arg = NULL_TREE;
   13904                 :  2644936193 :       else if (TREE_CODE (orig_arg) == TREE_VEC)
   13905                 :    63718997 :         new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
   13906                 :  2581217196 :       else if (PACK_EXPANSION_P (orig_arg))
   13907                 :             :         {
   13908                 :             :           /* Substitute into an expansion expression.  */
   13909                 :    53423869 :           new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
   13910                 :             : 
   13911                 :    53423869 :           if (TREE_CODE (new_arg) == TREE_VEC)
   13912                 :             :             /* Add to the expanded length adjustment the number of
   13913                 :             :                expanded arguments. We subtract one from this
   13914                 :             :                measurement, because the argument pack expression
   13915                 :             :                itself is already counted as 1 in
   13916                 :             :                LEN. EXPANDED_LEN_ADJUST can actually be negative, if
   13917                 :             :                the argument pack is empty.  */
   13918                 :    40460870 :             expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
   13919                 :             :         }
   13920                 :  2527793327 :       else if (ARGUMENT_PACK_P (orig_arg))
   13921                 :    93694905 :         new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
   13922                 :             :       else
   13923                 :  2434098422 :         new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
   13924                 :             : 
   13925                 :  2644936163 :       if (new_arg == error_mark_node)
   13926                 :     3663865 :         return error_mark_node;
   13927                 :             : 
   13928                 :  2641272298 :       elts[i] = new_arg;
   13929                 :  2641272298 :       if (new_arg != orig_arg)
   13930                 :  2529504456 :         const_subst_p = false;
   13931                 :             :     }
   13932                 :             : 
   13933                 :  1244856201 :   if (const_subst_p)
   13934                 :             :     return t;
   13935                 :             : 
   13936                 :  1228119850 :   tree maybe_reuse = NULL_TREE;
   13937                 :             : 
   13938                 :             :   /* If ARGS and T are both multi-level, the substituted result may be
   13939                 :             :      identical to ARGS.  */
   13940                 :  1228119850 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t)
   13941                 :    63585938 :       && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
   13942                 :     6364555 :       && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args))
   13943                 :             :     maybe_reuse = args;
   13944                 :             :   /* If T appears to be a vector of generic template arguments, the
   13945                 :             :      substituted result may be identical to the corresponding level
   13946                 :             :      from ARGS.  */
   13947                 :  1222409654 :   else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0)))
   13948                 :             :     {
   13949                 :   953623227 :       int level, index;
   13950                 :   953623227 :       template_parm_level_and_index (parm, &level, &index);
   13951                 :  2723263257 :       if (index == 0 && TMPL_ARGS_DEPTH (args) >= level)
   13952                 :  1725773632 :         maybe_reuse = TMPL_ARGS_LEVEL (args, level);
   13953                 :             :     }
   13954                 :             : 
   13955                 :             :   /* If the substituted result is identical to MAYBE_REUSE, return
   13956                 :             :      it and avoid allocating a new TREE_VEC, as an optimization.  */
   13957                 :    90736411 :   if (maybe_reuse != NULL_TREE
   13958                 :   868597012 :       && TREE_VEC_LENGTH (maybe_reuse) == len
   13959                 :  1653954313 :       && std::equal (elts, elts+len, TREE_VEC_BEGIN (maybe_reuse)))
   13960                 :             :     return maybe_reuse;
   13961                 :             : 
   13962                 :             :   /* If T consists of only a pack expansion for which substitution yielded
   13963                 :             :      a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead
   13964                 :             :      of effectively making a copy.  */
   13965                 :   463856973 :   if (len == 1
   13966                 :   244107113 :       && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0))
   13967                 :   507076110 :       && TREE_CODE (elts[0]) == TREE_VEC)
   13968                 :             :     return elts[0];
   13969                 :             : 
   13970                 :             :   /* Make space for the expanded arguments coming from template
   13971                 :             :      argument packs.  */
   13972                 :   429573941 :   tree r = make_tree_vec (len + expanded_len_adjust);
   13973                 :             :   /* T can contain TREE_VECs. That happens if T contains the
   13974                 :             :      arguments for a member template.
   13975                 :             :      In that case each TREE_VEC in T represents a level of template
   13976                 :             :      arguments, and T won't carry any non defaulted argument count.
   13977                 :             :      It will rather be the nested TREE_VECs that will carry one.
   13978                 :             :      In other words, T carries a non defaulted argument count only
   13979                 :             :      if it doesn't contain any nested TREE_VEC.  */
   13980                 :   429573941 :   if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t))
   13981                 :             :     {
   13982                 :   400556919 :       int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
   13983                 :   400556919 :       count += expanded_len_adjust;
   13984                 :   400556919 :       SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count);
   13985                 :             :     }
   13986                 :             : 
   13987                 :             :   int out = 0;
   13988                 :  1156199489 :   for (int i = 0; i < len; i++)
   13989                 :             :     {
   13990                 :   726625548 :       tree orig_arg = TREE_VEC_ELT (t, i);
   13991                 :   726625548 :       if (orig_arg
   13992                 :   726625543 :           && PACK_EXPANSION_P (orig_arg)
   13993                 :    15746412 :           && TREE_CODE (elts[i]) == TREE_VEC)
   13994                 :             :         {
   13995                 :             :           /* Now expand the template argument pack "in place".  */
   13996                 :    15160339 :           for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
   13997                 :     8982501 :             TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx);
   13998                 :             :         }
   13999                 :             :       else
   14000                 :             :         {
   14001                 :   720447710 :           TREE_VEC_ELT (r, out) = elts[i];
   14002                 :   720447710 :           out++;
   14003                 :             :         }
   14004                 :             :     }
   14005                 :   429573941 :   gcc_assert (out == TREE_VEC_LENGTH (r));
   14006                 :             : 
   14007                 :             :   return r;
   14008                 :  1248520066 : }
   14009                 :             : 
   14010                 :             : /* Substitute ARGS into one level PARMS of template parameters.  */
   14011                 :             : 
   14012                 :             : static tree
   14013                 :    20894597 : tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
   14014                 :             : {
   14015                 :    20894597 :   if (parms == error_mark_node)
   14016                 :             :     return error_mark_node;
   14017                 :             : 
   14018                 :    20894597 :   tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
   14019                 :             : 
   14020                 :    54980705 :   for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
   14021                 :             :     {
   14022                 :    34086108 :       tree tuple = TREE_VEC_ELT (parms, i);
   14023                 :             : 
   14024                 :    34086108 :       if (tuple == error_mark_node)
   14025                 :           0 :         continue;
   14026                 :             : 
   14027                 :    34086108 :       TREE_VEC_ELT (new_vec, i) =
   14028                 :    34086108 :         tsubst_template_parm (tuple, args, complain);
   14029                 :             :     }
   14030                 :             : 
   14031                 :             :   return new_vec;
   14032                 :             : }
   14033                 :             : 
   14034                 :             : /* Return the result of substituting ARGS into the template parameters
   14035                 :             :    given by PARMS.  If there are m levels of ARGS and m + n levels of
   14036                 :             :    PARMS, then the result will contain n levels of PARMS.  For
   14037                 :             :    example, if PARMS is `template <class T> template <class U>
   14038                 :             :    template <T*, U, class V>' and ARGS is {{int}, {double}} then the
   14039                 :             :    result will be `template <int*, double, class V>'.  */
   14040                 :             : 
   14041                 :             : static tree
   14042                 :    20892764 : tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
   14043                 :             : {
   14044                 :    20892764 :   tree r = NULL_TREE;
   14045                 :    20892764 :   tree* new_parms;
   14046                 :             : 
   14047                 :             :   /* When substituting into a template, we must set
   14048                 :             :      PROCESSING_TEMPLATE_DECL as the template parameters may be
   14049                 :             :      dependent if they are based on one-another, and the dependency
   14050                 :             :      predicates are short-circuit outside of templates.  */
   14051                 :    20892764 :   ++processing_template_decl;
   14052                 :             : 
   14053                 :    20892764 :   for (new_parms = &r;
   14054                 :   125361957 :        parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
   14055                 :    41789114 :        new_parms = &(TREE_CHAIN (*new_parms)),
   14056                 :    20894557 :          parms = TREE_CHAIN (parms))
   14057                 :             :     {
   14058                 :    20894557 :       tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
   14059                 :             :                                                   args, complain);
   14060                 :    41789114 :       *new_parms =
   14061                 :    41789112 :         tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
   14062                 :             :                              - TMPL_ARGS_DEPTH (args)),
   14063                 :             :                    new_vec, NULL_TREE);
   14064                 :    41789114 :       TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
   14065                 :    41789114 :         = TEMPLATE_PARMS_CONSTRAINTS (parms);
   14066                 :             :     }
   14067                 :             : 
   14068                 :    20892764 :   --processing_template_decl;
   14069                 :             : 
   14070                 :    20892764 :   return r;
   14071                 :             : }
   14072                 :             : 
   14073                 :             : /* Return the result of substituting ARGS into one template parameter
   14074                 :             :    given by T. T Must be a TREE_LIST which TREE_VALUE is the template
   14075                 :             :    parameter and which TREE_PURPOSE is the default argument of the
   14076                 :             :    template parameter.  */
   14077                 :             : 
   14078                 :             : static tree
   14079                 :    34086108 : tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
   14080                 :             : {
   14081                 :    34086108 :   tree default_value, parm_decl;
   14082                 :             : 
   14083                 :    34086108 :   if (args == NULL_TREE
   14084                 :    34086108 :       || t == NULL_TREE
   14085                 :    34086106 :       || t == error_mark_node)
   14086                 :             :     return t;
   14087                 :             : 
   14088                 :    34086106 :   gcc_assert (TREE_CODE (t) == TREE_LIST);
   14089                 :             : 
   14090                 :    34086106 :   default_value = TREE_PURPOSE (t);
   14091                 :    34086106 :   parm_decl = TREE_VALUE (t);
   14092                 :             : 
   14093                 :    34086106 :   parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
   14094                 :    34086106 :   if (TREE_CODE (parm_decl) == PARM_DECL
   14095                 :    34086106 :       && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
   14096                 :          37 :     parm_decl = error_mark_node;
   14097                 :    34086106 :   default_value = tsubst_template_arg (default_value, args,
   14098                 :             :                                        complain, NULL_TREE);
   14099                 :             : 
   14100                 :    34086106 :   tree r = build_tree_list (default_value, parm_decl);
   14101                 :    34086106 :   TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t);
   14102                 :    34086106 :   return r;
   14103                 :             : }
   14104                 :             : 
   14105                 :             : /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template
   14106                 :             :    parameter in PARMS for sake of declaration matching.  */
   14107                 :             : 
   14108                 :             : static void
   14109                 :      375124 : tsubst_each_template_parm_constraints (tree parms, tree args,
   14110                 :             :                                        tsubst_flags_t complain)
   14111                 :             : {
   14112                 :      375124 :   ++processing_template_decl;
   14113                 :      750248 :   for (; parms; parms = TREE_CHAIN (parms))
   14114                 :             :     {
   14115                 :      375124 :       tree level = TREE_VALUE (parms);
   14116                 :     1171396 :       for (tree parm : tree_vec_range (level))
   14117                 :     1592544 :         TEMPLATE_PARM_CONSTRAINTS (parm)
   14118                 :     1592544 :           = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args,
   14119                 :             :                                complain, NULL_TREE);
   14120                 :             :     }
   14121                 :      375124 :   --processing_template_decl;
   14122                 :      375124 : }
   14123                 :             : 
   14124                 :             : /* Substitute the ARGS into the indicated aggregate (or enumeration)
   14125                 :             :    type T.  If T is not an aggregate or enumeration type, it is
   14126                 :             :    handled as if by tsubst.  IN_DECL is as for tsubst.  If
   14127                 :             :    ENTERING_SCOPE is nonzero, T is the context for a template which
   14128                 :             :    we are presently tsubst'ing.  Return the substituted value.  */
   14129                 :             : 
   14130                 :             : static tree
   14131                 :   317198085 : tsubst_aggr_type (tree t,
   14132                 :             :                   tree args,
   14133                 :             :                   tsubst_flags_t complain,
   14134                 :             :                   tree in_decl,
   14135                 :             :                   int entering_scope)
   14136                 :             : {
   14137                 :   317198085 :   if (t == NULL_TREE)
   14138                 :             :     return NULL_TREE;
   14139                 :             : 
   14140                 :             :   /* Handle typedefs via tsubst so that they get consistently reused.  */
   14141                 :   317198085 :   if (typedef_variant_p (t))
   14142                 :             :     {
   14143                 :     5929911 :       t = tsubst (t, args, complain, in_decl);
   14144                 :     5929911 :       if (t == error_mark_node)
   14145                 :             :         return error_mark_node;
   14146                 :             : 
   14147                 :             :       /* The effect of entering_scope is that for a dependent specialization
   14148                 :             :          A<T>, lookup_template_class prefers to return A's primary template
   14149                 :             :          type instead of the implicit instantiation.  So when entering_scope,
   14150                 :             :          we mirror this behavior by inspecting TYPE_CANONICAL appropriately,
   14151                 :             :          taking advantage of the fact that lookup_template_class links the two
   14152                 :             :          types by setting TYPE_CANONICAL of the latter to the former.  */
   14153                 :     5929793 :       if (entering_scope
   14154                 :     5929793 :           && CLASS_TYPE_P (t)
   14155                 :     5808974 :           && dependent_type_p (t)
   14156                 :      138087 :           && TYPE_TEMPLATE_INFO (t)
   14157                 :     6067877 :           && TYPE_CANONICAL (t) == TREE_TYPE (TYPE_TI_TEMPLATE (t)))
   14158                 :           4 :         t = TYPE_CANONICAL (t);
   14159                 :             : 
   14160                 :     5929793 :       return t;
   14161                 :             :     }
   14162                 :             : 
   14163                 :   311268174 :   switch (TREE_CODE (t))
   14164                 :             :     {
   14165                 :   293539952 :       case RECORD_TYPE:
   14166                 :   293539952 :       case ENUMERAL_TYPE:
   14167                 :   293539952 :       case UNION_TYPE:
   14168                 :   293539952 :         return tsubst_aggr_type_1 (t, args, complain, in_decl, entering_scope);
   14169                 :             : 
   14170                 :    17728222 :       default:
   14171                 :    17728222 :         return tsubst (t, args, complain, in_decl);
   14172                 :             :     }
   14173                 :             : }
   14174                 :             : 
   14175                 :             : /* The part of tsubst_aggr_type that's shared with the RECORD_, UNION_
   14176                 :             :    and ENUMERAL_TYPE cases of tsubst.  */
   14177                 :             : 
   14178                 :             : static tree
   14179                 :   754843946 : tsubst_aggr_type_1 (tree t,
   14180                 :             :                     tree args,
   14181                 :             :                     tsubst_flags_t complain,
   14182                 :             :                     tree in_decl,
   14183                 :             :                     int entering_scope)
   14184                 :             : {
   14185                 :   754843946 :   if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
   14186                 :             :     {
   14187                 :   670555848 :       complain &= ~tf_qualifying_scope;
   14188                 :             : 
   14189                 :             :       /* Figure out what arguments are appropriate for the
   14190                 :             :          type we are trying to find.  For example, given:
   14191                 :             : 
   14192                 :             :            template <class T> struct S;
   14193                 :             :            template <class T, class U> void f(T, U) { S<U> su; }
   14194                 :             : 
   14195                 :             :          and supposing that we are instantiating f<int, double>,
   14196                 :             :          then our ARGS will be {int, double}, but, when looking up
   14197                 :             :          S we only want {double}.  */
   14198                 :  1341111696 :       tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
   14199                 :             :                                           complain, in_decl);
   14200                 :   670555810 :       if (argvec == error_mark_node)
   14201                 :             :         return error_mark_node;
   14202                 :             : 
   14203                 :   669613479 :       tree r = lookup_template_class (t, argvec, in_decl, NULL_TREE,
   14204                 :             :                                       entering_scope, complain);
   14205                 :   669613419 :       return cp_build_qualified_type (r, cp_type_quals (t), complain);
   14206                 :             :     }
   14207                 :             :   else
   14208                 :             :     /* This is not a template type, so there's nothing to do.  */
   14209                 :    84288098 :     return t;
   14210                 :             : }
   14211                 :             : 
   14212                 :             : /* Map from a FUNCTION_DECL to a vec of default argument instantiations,
   14213                 :             :    indexed in reverse order of the parameters.  */
   14214                 :             : 
   14215                 :             : static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst;
   14216                 :             : 
   14217                 :             : /* Return a reference to the vec* of defarg insts for FN.  */
   14218                 :             : 
   14219                 :             : static vec<tree,va_gc> *&
   14220                 :      716253 : defarg_insts_for (tree fn)
   14221                 :             : {
   14222                 :      716253 :   if (!defarg_inst)
   14223                 :       12382 :     defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (13);
   14224                 :      716253 :   tree_vec_map in = { { fn }, nullptr };
   14225                 :      716253 :   tree_vec_map **slot
   14226                 :      716253 :     = defarg_inst->find_slot_with_hash (&in, DECL_UID (fn), INSERT);
   14227                 :      716253 :   if (!*slot)
   14228                 :             :     {
   14229                 :      456279 :       *slot = ggc_alloc<tree_vec_map> ();
   14230                 :      456279 :       **slot = in;
   14231                 :             :     }
   14232                 :      716253 :   return (*slot)->to;
   14233                 :             : }
   14234                 :             : 
   14235                 :             : /* Substitute into the default argument ARG (a default argument for
   14236                 :             :    FN), which has the indicated TYPE.  */
   14237                 :             : 
   14238                 :             : tree
   14239                 :      724356 : tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
   14240                 :             :                          tsubst_flags_t complain)
   14241                 :             : {
   14242                 :      724356 :   int errs = errorcount + sorrycount;
   14243                 :             : 
   14244                 :             :   /* This can happen in invalid code.  */
   14245                 :      724356 :   if (TREE_CODE (arg) == DEFERRED_PARSE)
   14246                 :             :     return arg;
   14247                 :             : 
   14248                 :             :   /* Shortcut {}.  */
   14249                 :       17866 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg)
   14250                 :      732459 :       && CONSTRUCTOR_NELTS (arg) == 0)
   14251                 :             :     return arg;
   14252                 :             : 
   14253                 :      716253 :   tree parm = FUNCTION_FIRST_USER_PARM (fn);
   14254                 :      716253 :   parm = chain_index (parmnum, parm);
   14255                 :      716253 :   tree parmtype = TREE_TYPE (parm);
   14256                 :      716253 :   if (DECL_BY_REFERENCE (parm))
   14257                 :           3 :     parmtype = TREE_TYPE (parmtype);
   14258                 :      716253 :   if (parmtype == error_mark_node)
   14259                 :             :     return error_mark_node;
   14260                 :             : 
   14261                 :      716253 :   gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
   14262                 :             : 
   14263                 :             :   /* Remember the location of the pointer to the vec rather than the location
   14264                 :             :      of the particular element, in case the vec grows in tsubst_expr.  */
   14265                 :      716253 :   vec<tree,va_gc> *&defs = defarg_insts_for (fn);
   14266                 :             :   /* Index in reverse order to avoid allocating space for initial parameters
   14267                 :             :      that don't have default arguments.  */
   14268                 :      716253 :   unsigned ridx = list_length (parm);
   14269                 :      976227 :   if (vec_safe_length (defs) < ridx)
   14270                 :      456307 :     vec_safe_grow_cleared (defs, ridx);
   14271                 :      259946 :   else if (tree inst = (*defs)[ridx - 1])
   14272                 :             :     return inst;
   14273                 :             : 
   14274                 :             :   /* This default argument came from a template.  Instantiate the
   14275                 :             :      default argument here, not in tsubst.  In the case of
   14276                 :             :      something like:
   14277                 :             : 
   14278                 :             :        template <class T>
   14279                 :             :        struct S {
   14280                 :             :          static T t();
   14281                 :             :          void f(T = t());
   14282                 :             :        };
   14283                 :             : 
   14284                 :             :      we must be careful to do name lookup in the scope of S<T>,
   14285                 :             :      rather than in the current class.  */
   14286                 :      522230 :   push_to_top_level ();
   14287                 :      522230 :   push_access_scope (fn);
   14288                 :      522230 :   push_deferring_access_checks (dk_no_deferred);
   14289                 :             :   /* So in_immediate_context knows this is a default argument.  */
   14290                 :      522230 :   begin_scope (sk_function_parms, fn);
   14291                 :      522230 :   start_lambda_scope (parm);
   14292                 :             : 
   14293                 :             :   /* The default argument expression may cause implicitly defined
   14294                 :             :      member functions to be synthesized, which will result in garbage
   14295                 :             :      collection.  We must treat this situation as if we were within
   14296                 :             :      the body of function so as to avoid collecting live data on the
   14297                 :             :      stack.  */
   14298                 :      522230 :   ++function_depth;
   14299                 :      522230 :   arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE);
   14300                 :      522230 :   --function_depth;
   14301                 :             : 
   14302                 :      522230 :   finish_lambda_scope ();
   14303                 :             : 
   14304                 :             :   /* Make sure the default argument is reasonable.  */
   14305                 :      522230 :   arg = check_default_argument (type, arg, complain);
   14306                 :             : 
   14307                 :      522230 :   if (errorcount+sorrycount > errs
   14308                 :      522230 :       && (complain & tf_warning_or_error))
   14309                 :          18 :     inform (input_location,
   14310                 :             :             "  when instantiating default argument for call to %qD", fn);
   14311                 :             : 
   14312                 :      522230 :   leave_scope ();
   14313                 :      522230 :   pop_deferring_access_checks ();
   14314                 :      522230 :   pop_access_scope (fn);
   14315                 :      522230 :   pop_from_top_level ();
   14316                 :             : 
   14317                 :      522230 :   if (arg != error_mark_node && !cp_unevaluated_operand)
   14318                 :      401275 :     (*defs)[ridx - 1] = arg;
   14319                 :             : 
   14320                 :             :   return arg;
   14321                 :             : }
   14322                 :             : 
   14323                 :             : /* Substitute into all the default arguments for FN.  */
   14324                 :             : 
   14325                 :             : static void
   14326                 :      630387 : tsubst_default_arguments (tree fn, tsubst_flags_t complain)
   14327                 :             : {
   14328                 :      630387 :   tree arg;
   14329                 :      630387 :   tree tmpl_args;
   14330                 :             : 
   14331                 :      630387 :   tmpl_args = DECL_TI_ARGS (fn);
   14332                 :             : 
   14333                 :             :   /* If this function is not yet instantiated, we certainly don't need
   14334                 :             :      its default arguments.  */
   14335                 :      630387 :   if (uses_template_parms (tmpl_args))
   14336                 :             :     return;
   14337                 :             :   /* Don't do this again for clones.  */
   14338                 :      630387 :   if (DECL_CLONED_FUNCTION_P (fn))
   14339                 :             :     return;
   14340                 :             : 
   14341                 :      630387 :   int i = 0;
   14342                 :      630387 :   for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
   14343                 :     2535228 :        arg;
   14344                 :     1904841 :        arg = TREE_CHAIN (arg), ++i)
   14345                 :     1904841 :     if (TREE_PURPOSE (arg))
   14346                 :          14 :       TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
   14347                 :          14 :                                                     TREE_VALUE (arg),
   14348                 :          14 :                                                     TREE_PURPOSE (arg),
   14349                 :             :                                                     complain);
   14350                 :             : }
   14351                 :             : 
   14352                 :             : /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier.  */
   14353                 :             : static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
   14354                 :             : 
   14355                 :             : /* Store a pair to EXPLICIT_SPECIFIER_MAP.  */
   14356                 :             : 
   14357                 :             : void
   14358                 :      423893 : store_explicit_specifier (tree v, tree t)
   14359                 :             : {
   14360                 :      423893 :   if (!explicit_specifier_map)
   14361                 :        2750 :     explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
   14362                 :      423893 :   DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
   14363                 :      423893 :   explicit_specifier_map->put (v, t);
   14364                 :      423893 : }
   14365                 :             : 
   14366                 :             : /* Lookup an element in EXPLICIT_SPECIFIER_MAP.  */
   14367                 :             : 
   14368                 :             : tree
   14369                 :      454035 : lookup_explicit_specifier (tree v)
   14370                 :             : {
   14371                 :      454035 :   return *explicit_specifier_map->get (v);
   14372                 :             : }
   14373                 :             : 
   14374                 :             : /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
   14375                 :             :    FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
   14376                 :             :    are ARG_TYPES, and exception specification is RAISES, and otherwise is
   14377                 :             :    identical to T.  */
   14378                 :             : 
   14379                 :             : static tree
   14380                 :   122716342 : rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
   14381                 :             :                                  tree raises, tsubst_flags_t complain)
   14382                 :             : {
   14383                 :   122716342 :   gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
   14384                 :             : 
   14385                 :   122716342 :   tree new_type;
   14386                 :   122716342 :   if (TREE_CODE (t) == FUNCTION_TYPE)
   14387                 :             :     {
   14388                 :    41950695 :       new_type = build_function_type (return_type, arg_types);
   14389                 :    41950695 :       new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
   14390                 :             :     }
   14391                 :             :   else
   14392                 :             :     {
   14393                 :    80765647 :       tree r = TREE_TYPE (TREE_VALUE (arg_types));
   14394                 :             :       /* Don't pick up extra function qualifiers from the basetype.  */
   14395                 :    80765647 :       r = cp_build_qualified_type (r, type_memfn_quals (t), complain);
   14396                 :    80765647 :       if (! MAYBE_CLASS_TYPE_P (r))
   14397                 :             :         {
   14398                 :             :           /* [temp.deduct]
   14399                 :             : 
   14400                 :             :              Type deduction may fail for any of the following
   14401                 :             :              reasons:
   14402                 :             : 
   14403                 :             :              -- Attempting to create "pointer to member of T" when T
   14404                 :             :              is not a class type.  */
   14405                 :           0 :           if (complain & tf_error)
   14406                 :           0 :             error ("creating pointer to member function of non-class type %qT",
   14407                 :             :                    r);
   14408                 :           0 :           return error_mark_node;
   14409                 :             :         }
   14410                 :             : 
   14411                 :    80765647 :       new_type = build_method_type_directly (r, return_type,
   14412                 :    80765647 :                                              TREE_CHAIN (arg_types));
   14413                 :             :     }
   14414                 :   122716342 :   new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
   14415                 :             : 
   14416                 :   122716342 :   cp_ref_qualifier rqual = type_memfn_rqual (t);
   14417                 :   122716342 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   14418                 :   122716342 :   return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
   14419                 :             : }
   14420                 :             : 
   14421                 :             : /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
   14422                 :             :    each of its formal parameters.  If there is a disagreement then rebuild
   14423                 :             :    DECL's function type according to its formal parameter types, as part of a
   14424                 :             :    resolution for Core issues 1001/1322.  */
   14425                 :             : 
   14426                 :             : static void
   14427                 :   107353932 : maybe_rebuild_function_decl_type (tree decl)
   14428                 :             : {
   14429                 :   107353932 :   bool function_type_needs_rebuilding = false;
   14430                 :   107353932 :   if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
   14431                 :             :     {
   14432                 :    78667731 :       tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
   14433                 :   296734454 :       while (parm_type_list && parm_type_list != void_list_node)
   14434                 :             :         {
   14435                 :   139399082 :           tree parm_type = TREE_VALUE (parm_type_list);
   14436                 :   139399082 :           tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   14437                 :   139399082 :           if (!same_type_p (parm_type, formal_parm_type_unqual))
   14438                 :             :             {
   14439                 :             :               function_type_needs_rebuilding = true;
   14440                 :             :               break;
   14441                 :             :             }
   14442                 :             : 
   14443                 :   139398992 :           parm_list = DECL_CHAIN (parm_list);
   14444                 :   139398992 :           parm_type_list = TREE_CHAIN (parm_type_list);
   14445                 :             :         }
   14446                 :             :     }
   14447                 :             : 
   14448                 :    78667731 :   if (!function_type_needs_rebuilding)
   14449                 :   107353842 :     return;
   14450                 :             : 
   14451                 :          90 :   const tree fntype = TREE_TYPE (decl);
   14452                 :          90 :   tree parm_list = DECL_ARGUMENTS (decl);
   14453                 :          90 :   tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
   14454                 :          90 :   tree new_parm_type_list = NULL_TREE;
   14455                 :          90 :   tree *q = &new_parm_type_list;
   14456                 :         106 :   for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
   14457                 :             :     {
   14458                 :          16 :       *q = copy_node (old_parm_type_list);
   14459                 :          16 :       parm_list = DECL_CHAIN (parm_list);
   14460                 :          16 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   14461                 :          16 :       q = &TREE_CHAIN (*q);
   14462                 :             :     }
   14463                 :         184 :   while (old_parm_type_list && old_parm_type_list != void_list_node)
   14464                 :             :     {
   14465                 :          94 :       *q = copy_node (old_parm_type_list);
   14466                 :          94 :       tree *new_parm_type = &TREE_VALUE (*q);
   14467                 :          94 :       tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
   14468                 :          94 :       if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
   14469                 :          90 :         *new_parm_type = formal_parm_type_unqual;
   14470                 :             : 
   14471                 :          94 :       parm_list = DECL_CHAIN (parm_list);
   14472                 :          94 :       old_parm_type_list = TREE_CHAIN (old_parm_type_list);
   14473                 :          94 :       q = &TREE_CHAIN (*q);
   14474                 :             :     }
   14475                 :          90 :   if (old_parm_type_list == void_list_node)
   14476                 :          90 :     *q = void_list_node;
   14477                 :             : 
   14478                 :          90 :   TREE_TYPE (decl)
   14479                 :         180 :     = rebuild_function_or_method_type (fntype,
   14480                 :          90 :                                        TREE_TYPE (fntype), new_parm_type_list,
   14481                 :          90 :                                        TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
   14482                 :             : }
   14483                 :             : 
   14484                 :             : /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL.  */
   14485                 :             : 
   14486                 :             : static tree
   14487                 :   109597079 : tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
   14488                 :             :                       tree lambda_fntype, bool use_spec_table = true)
   14489                 :             : {
   14490                 :   109597079 :   tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
   14491                 :   109597079 :   hashval_t hash = 0;
   14492                 :   109597079 :   tree in_decl = t;
   14493                 :             : 
   14494                 :             :   /* Nobody should be tsubst'ing into non-template functions.  */
   14495                 :   109597079 :   gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
   14496                 :             :               || DECL_LOCAL_DECL_P (t));
   14497                 :             : 
   14498                 :   109597079 :   if (DECL_LOCAL_DECL_P (t))
   14499                 :             :     {
   14500                 :         325 :       if (tree spec = retrieve_local_specialization (t))
   14501                 :             :         return spec;
   14502                 :             :     }
   14503                 :   109596754 :   else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
   14504                 :             :     {
   14505                 :             :       /* If T is not dependent, just return it.  */
   14506                 :   109573543 :       if (!uses_template_parms (DECL_TI_ARGS (t))
   14507                 :   109573552 :           && !LAMBDA_FUNCTION_P (t))
   14508                 :             :         return t;
   14509                 :             : 
   14510                 :             :       /* A non-templated friend doesn't get DECL_TEMPLATE_INFO.  */
   14511                 :   109274370 :       if (non_templated_friend_p (t))
   14512                 :         141 :         goto friend_case;
   14513                 :             : 
   14514                 :             :       /* Calculate the most general template of which R is a
   14515                 :             :          specialization.  */
   14516                 :   109274229 :       gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
   14517                 :             : 
   14518                 :             :       /* We're substituting a lambda function under tsubst_lambda_expr but not
   14519                 :             :          directly from it; find the matching function we're already inside.
   14520                 :             :          But don't do this if T is a generic lambda with a single level of
   14521                 :             :          template parms, as in that case we're doing a normal instantiation. */
   14522                 :   110258986 :       if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
   14523                 :   109392322 :           && (!generic_lambda_fn_p (t)
   14524                 :      118069 :               || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
   14525                 :          57 :         return enclosing_instantiation_of (t);
   14526                 :             : 
   14527                 :             :       /* Calculate the complete set of arguments used to
   14528                 :             :          specialize R.  */
   14529                 :   109274172 :       if (use_spec_table && !lambda_fntype)
   14530                 :             :         {
   14531                 :    70400902 :           argvec = tsubst_template_args (DECL_TI_ARGS
   14532                 :             :                                          (DECL_TEMPLATE_RESULT
   14533                 :             :                                           (DECL_TI_TEMPLATE (t))),
   14534                 :             :                                          args, complain, in_decl);
   14535                 :    70400902 :           if (argvec == error_mark_node)
   14536                 :             :             return error_mark_node;
   14537                 :             : 
   14538                 :             :           /* Check to see if we already have this specialization.  */
   14539                 :    70400902 :           hash = spec_hasher::hash (gen_tmpl, argvec);
   14540                 :    70400902 :           if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
   14541                 :             :             /* The spec for these args might be a partial instantiation of the
   14542                 :             :                template, but here what we want is the FUNCTION_DECL.  */
   14543                 :      518752 :             return STRIP_TEMPLATE (spec);
   14544                 :             :         }
   14545                 :             :       else
   14546                 :             :         argvec = args;
   14547                 :             :     }
   14548                 :             :   else
   14549                 :             :     {
   14550                 :             :       /* This special case arises when we have something like this:
   14551                 :             : 
   14552                 :             :          template <class T> struct S {
   14553                 :             :            friend void f<int>(int, double);
   14554                 :             :          };
   14555                 :             : 
   14556                 :             :          Here, the DECL_TI_TEMPLATE for the friend declaration
   14557                 :             :          will be an IDENTIFIER_NODE.  We are being called from
   14558                 :             :          tsubst_friend_function, and we want only to create a
   14559                 :             :          new decl (R) with appropriate types so that we can call
   14560                 :             :          determine_specialization.  */
   14561                 :       23211 :     friend_case:
   14562                 :             :       gen_tmpl = NULL_TREE;
   14563                 :             :       argvec = NULL_TREE;
   14564                 :             :     }
   14565                 :             : 
   14566                 :   108779097 :   tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
   14567                 :   108779097 :                   : NULL_TREE);
   14568                 :   108779097 :   tree ctx = closure ? closure : DECL_CONTEXT (t);
   14569                 :   108779097 :   bool member = ctx && TYPE_P (ctx);
   14570                 :             : 
   14571                 :             :   /* If this is a static or xobj lambda, remove the 'this' pointer added in
   14572                 :             :      tsubst_lambda_expr now that we know the closure type.  */
   14573                 :   108779097 :   if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t))
   14574                 :          82 :     lambda_fntype = static_fn_type (lambda_fntype);
   14575                 :             : 
   14576                 :   108779097 :   if (member && !closure)
   14577                 :    90029412 :     ctx = tsubst_aggr_type (ctx, args,
   14578                 :             :                             complain, t, /*entering_scope=*/1);
   14579                 :             : 
   14580                 :   108779097 :   tree type = (lambda_fntype ? lambda_fntype
   14581                 :   108640679 :                : tsubst (TREE_TYPE (t), args,
   14582                 :   108770961 :                          complain | tf_fndecl_type, in_decl));
   14583                 :   108770961 :   if (type == error_mark_node)
   14584                 :             :     return error_mark_node;
   14585                 :             : 
   14586                 :             :   /* If we hit excessive deduction depth, the type is bogus even if
   14587                 :             :      it isn't error_mark_node, so don't build a decl.  */
   14588                 :   107353939 :   if (excessive_deduction_depth)
   14589                 :             :     return error_mark_node;
   14590                 :             : 
   14591                 :             :   /* We do NOT check for matching decls pushed separately at this
   14592                 :             :      point, as they may not represent instantiations of this
   14593                 :             :      template, and in any case are considered separate under the
   14594                 :             :      discrete model.  */
   14595                 :   107353939 :   tree r = copy_decl (t);
   14596                 :   107353939 :   DECL_USE_TEMPLATE (r) = 0;
   14597                 :   107353939 :   TREE_TYPE (r) = type;
   14598                 :             :   /* Clear out the mangled name and RTL for the instantiation.  */
   14599                 :   107353939 :   SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   14600                 :   107353939 :   SET_DECL_RTL (r, NULL);
   14601                 :             :   /* Leave DECL_INITIAL set on deleted instantiations.  */
   14602                 :   107353939 :   if (!DECL_DELETED_FN (r))
   14603                 :   105577941 :     DECL_INITIAL (r) = NULL_TREE;
   14604                 :   107353939 :   DECL_CONTEXT (r) = ctx;
   14605                 :   107353939 :   set_instantiating_module (r);
   14606                 :             : 
   14607                 :             :   /* Handle explicit(dependent-expr).  */
   14608                 :   107353939 :   if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
   14609                 :             :     {
   14610                 :      448823 :       tree spec = lookup_explicit_specifier (t);
   14611                 :      448823 :       spec = tsubst_expr (spec, args, complain, in_decl);
   14612                 :      448823 :       spec = build_explicit_specifier (spec, complain);
   14613                 :      448823 :       if (spec == error_mark_node)
   14614                 :             :         return error_mark_node;
   14615                 :      448816 :       if (instantiation_dependent_expression_p (spec))
   14616                 :      323330 :         store_explicit_specifier (r, spec);
   14617                 :             :       else
   14618                 :             :         {
   14619                 :      125486 :           DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
   14620                 :      125486 :           DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false;
   14621                 :             :         }
   14622                 :             :     }
   14623                 :             : 
   14624                 :             :   /* OpenMP UDRs have the only argument a reference to the declared
   14625                 :             :      type.  We want to diagnose if the declared type is a reference,
   14626                 :             :      which is invalid, but as references to references are usually
   14627                 :             :      quietly merged, diagnose it here.  */
   14628                 :   107353932 :   if (DECL_OMP_DECLARE_REDUCTION_P (t))
   14629                 :             :     {
   14630                 :         338 :       tree argtype
   14631                 :         338 :         = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
   14632                 :         338 :       argtype = tsubst (argtype, args, complain, in_decl);
   14633                 :         338 :       if (TYPE_REF_P (argtype))
   14634                 :           8 :         error_at (DECL_SOURCE_LOCATION (t),
   14635                 :             :                   "reference type %qT in "
   14636                 :             :                   "%<#pragma omp declare reduction%>", argtype);
   14637                 :         338 :       if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
   14638                 :         270 :         DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
   14639                 :             :                                           argtype);
   14640                 :             :     }
   14641                 :             : 
   14642                 :   182210509 :   if (member && DECL_CONV_FN_P (r))
   14643                 :             :     /* Type-conversion operator.  Reconstruct the name, in
   14644                 :             :        case it's the name of one of the template's parameters.  */
   14645                 :      836104 :     DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
   14646                 :             : 
   14647                 :   107353932 :   tree parms = DECL_ARGUMENTS (t);
   14648                 :   107353932 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   14649                 :      138336 :     parms = DECL_CHAIN (parms);
   14650                 :   107353932 :   parms = tsubst (parms, args, complain, t);
   14651                 :   327404327 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   14652                 :   220050395 :     DECL_CONTEXT (parm) = r;
   14653                 :   107353932 :   if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t))
   14654                 :             :     {
   14655                 :      138336 :       tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
   14656                 :      138336 :       DECL_NAME (tparm) = closure_identifier;
   14657                 :      138336 :       DECL_CHAIN (tparm) = parms;
   14658                 :      138336 :       parms = tparm;
   14659                 :             :     }
   14660                 :   107353932 :   DECL_ARGUMENTS (r) = parms;
   14661                 :   107353932 :   DECL_RESULT (r) = NULL_TREE;
   14662                 :             : 
   14663                 :   107353932 :   maybe_rebuild_function_decl_type (r);
   14664                 :             : 
   14665                 :   107353932 :   TREE_STATIC (r) = 0;
   14666                 :   107353932 :   TREE_PUBLIC (r) = TREE_PUBLIC (t);
   14667                 :   107353932 :   DECL_EXTERNAL (r) = 1;
   14668                 :             :   /* If this is an instantiation of a function with internal
   14669                 :             :      linkage, we already know what object file linkage will be
   14670                 :             :      assigned to the instantiation.  */
   14671                 :   107353932 :   DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
   14672                 :   107353932 :   DECL_DEFER_OUTPUT (r) = 0;
   14673                 :   107353932 :   DECL_CHAIN (r) = NULL_TREE;
   14674                 :   107353932 :   DECL_PENDING_INLINE_INFO (r) = 0;
   14675                 :   107353932 :   DECL_PENDING_INLINE_P (r) = 0;
   14676                 :   107353932 :   DECL_SAVED_TREE (r) = NULL_TREE;
   14677                 :   107353932 :   DECL_STRUCT_FUNCTION (r) = NULL;
   14678                 :   107353932 :   TREE_USED (r) = 0;
   14679                 :             :   /* We'll re-clone as appropriate in instantiate_template.  */
   14680                 :   107353932 :   DECL_CLONED_FUNCTION (r) = NULL_TREE;
   14681                 :             : 
   14682                 :             :   /* If we aren't complaining now, return on error before we register
   14683                 :             :      the specialization so that we'll complain eventually.  */
   14684                 :   107353932 :   if ((complain & tf_error) == 0
   14685                 :    19785276 :       && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   14686                 :   108739291 :       && !grok_op_properties (r, /*complain=*/false))
   14687                 :           4 :     return error_mark_node;
   14688                 :             : 
   14689                 :             :   /* If we are looking at an xobj lambda, we might need to check the type of
   14690                 :             :      its xobj parameter.  */
   14691                 :   108337876 :   if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r))
   14692                 :             :     {
   14693                 :         410 :       tree closure_obj = DECL_CONTEXT (r);
   14694                 :         410 :       tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj);
   14695                 :         410 :       tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r));
   14696                 :             : 
   14697                 :         410 :       if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
   14698                 :         306 :             || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)))
   14699                 :             :         /* If a lambda has an empty capture clause, an xobj parameter of
   14700                 :             :            unrelated type is not an error.  */;
   14701                 :         272 :       else if (dependent_type_p (obj_param))
   14702                 :             :         /* If we are coming from tsubst_lambda_expr we might not have
   14703                 :             :            substituted into our xobj parameter yet.  We can't error out until
   14704                 :             :            we know what the type really is so do nothing...
   14705                 :             :            ...but if we are instantiating the call op for real and we don't
   14706                 :             :            have a real type then something has gone incredibly wrong.  */
   14707                 :          64 :         gcc_assert (lambda_fntype);
   14708                 :             :       else
   14709                 :             :         {
   14710                 :             :           /* We have a lambda with captures, and know the type of the xobj
   14711                 :             :              parameter, time to check it.  */
   14712                 :         208 :           tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param));
   14713                 :         208 :           if (!same_or_base_type_p (closure_obj, obj_param_type))
   14714                 :             :             {
   14715                 :             :               /* This error does not emit when the lambda's call operator
   14716                 :             :                  template is instantiated by taking its address, such as in
   14717                 :             :                  the following case:
   14718                 :             : 
   14719                 :             :                  auto f = [x = 0](this auto&&){};
   14720                 :             :                  int (*fp)(int&) = &decltype(f)::operator();
   14721                 :             : 
   14722                 :             :                  It only emits when explicitly calling the call operator with
   14723                 :             :                  an explicit template parameter:
   14724                 :             : 
   14725                 :             :                  template<typename T>
   14726                 :             :                  struct S : T {
   14727                 :             :                    using T::operator();
   14728                 :             :                    operator int() const {return {};}
   14729                 :             :                  };
   14730                 :             : 
   14731                 :             :                  auto s = S{[x = 0](this auto&&) {}};
   14732                 :             :                  s.operator()<int>();
   14733                 :             : 
   14734                 :             :                  This is due to resolve_address_of_overloaded_function being
   14735                 :             :                  deficient at reporting candidates when overload resolution
   14736                 :             :                  fails.
   14737                 :             : 
   14738                 :             :                  This diagnostic will be active in the first case if/when
   14739                 :             :                  resolve_address_of_overloaded_function is fixed to properly
   14740                 :             :                  emit candidates upon failure to resolve to an overload.  */
   14741                 :          36 :               if (complain & tf_error)
   14742                 :           2 :                 error ("a lambda with captures may not have an explicit "
   14743                 :             :                        "object parameter of an unrelated type");
   14744                 :          36 :               return error_mark_node;
   14745                 :             :             }
   14746                 :             :         }
   14747                 :             :     }
   14748                 :             : 
   14749                 :             :   /* Associate the constraints directly with the instantiation. We
   14750                 :             :      don't substitute through the constraints; that's only done when
   14751                 :             :      they are checked.  */
   14752                 :   107353892 :   if (tree ci = get_constraints (t))
   14753                 :     1733472 :     set_constraints (r, ci);
   14754                 :             : 
   14755                 :   211581514 :   if (DECL_FRIEND_CONTEXT (t))
   14756                 :     1062406 :     SET_DECL_FRIEND_CONTEXT (r,
   14757                 :             :                              tsubst (DECL_FRIEND_CONTEXT (t),
   14758                 :             :                                      args, complain, in_decl));
   14759                 :             : 
   14760                 :   107353892 :   if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   14761                 :             :                                        args, complain, in_decl))
   14762                 :           6 :     return error_mark_node;
   14763                 :             : 
   14764                 :             :   /* Set up the DECL_TEMPLATE_INFO for R.  There's no need to do
   14765                 :             :      this in the special friend case mentioned above where
   14766                 :             :      GEN_TMPL is NULL.  */
   14767                 :   107353886 :   if (gen_tmpl && !closure)
   14768                 :             :     {
   14769                 :   107191795 :       DECL_TEMPLATE_INFO (r)
   14770                 :   107191795 :         = build_template_info (gen_tmpl, argvec);
   14771                 :   107191795 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   14772                 :             : 
   14773                 :   107191795 :       if (use_spec_table)
   14774                 :             :         {
   14775                 :    69882072 :           tree new_r
   14776                 :    69882072 :             = register_specialization (r, gen_tmpl, argvec, false, hash);
   14777                 :    69882072 :           if (new_r != r)
   14778                 :             :             /* We instantiated this while substituting into
   14779                 :             :                the type earlier (template/friend54.C).  */
   14780                 :             :             return new_r;
   14781                 :             :         }
   14782                 :             : 
   14783                 :             :       /* We're not supposed to instantiate default arguments
   14784                 :             :          until they are called, for a template.  But, for a
   14785                 :             :          declaration like:
   14786                 :             : 
   14787                 :             :          template <class T> void f ()
   14788                 :             :          { extern void g(int i = T()); }
   14789                 :             : 
   14790                 :             :          we should do the substitution when the template is
   14791                 :             :          instantiated.  We handle the member function case in
   14792                 :             :          instantiate_class_template since the default arguments
   14793                 :             :          might refer to other members of the class.  */
   14794                 :   107191795 :       if (!member
   14795                 :    17324930 :           && !PRIMARY_TEMPLATE_P (gen_tmpl)
   14796                 :   107822182 :           && !uses_template_parms (argvec))
   14797                 :      630387 :         tsubst_default_arguments (r, complain);
   14798                 :             :     }
   14799                 :      162091 :   else if (DECL_LOCAL_DECL_P (r))
   14800                 :             :     {
   14801                 :         325 :       if (!cp_unevaluated_operand)
   14802                 :         325 :         register_local_specialization (r, t);
   14803                 :             :     }
   14804                 :             :   else
   14805                 :      161766 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   14806                 :             : 
   14807                 :             :   /* Copy the list of befriending classes.  */
   14808                 :   107353886 :   for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
   14809                 :   108251551 :        *friends;
   14810                 :      897665 :        friends = &TREE_CHAIN (*friends))
   14811                 :             :     {
   14812                 :      897665 :       *friends = copy_node (*friends);
   14813                 :      897665 :       TREE_VALUE (*friends)
   14814                 :     1795330 :         = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
   14815                 :             :     }
   14816                 :             : 
   14817                 :   214707772 :   if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
   14818                 :             :     {
   14819                 :    21358169 :       maybe_retrofit_in_chrg (r);
   14820                 :    42716338 :       if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
   14821                 :           8 :         return error_mark_node;
   14822                 :             :       /* If this is an instantiation of a member template, clone it.
   14823                 :             :          If it isn't, that'll be handled by
   14824                 :             :          clone_constructors_and_destructors.  */
   14825                 :    42716317 :       if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl))
   14826                 :     7648952 :         clone_cdtor (r, /*update_methods=*/false);
   14827                 :             :     }
   14828                 :    85995717 :   else if ((complain & tf_error) != 0
   14829                 :    67142783 :            && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
   14830                 :   101950234 :            && !grok_op_properties (r, /*complain=*/true))
   14831                 :           4 :     return error_mark_node;
   14832                 :             : 
   14833                 :             :   /* Possibly limit visibility based on template args.  */
   14834                 :   107353874 :   DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   14835                 :   107353874 :   if (DECL_VISIBILITY_SPECIFIED (t))
   14836                 :             :     {
   14837                 :    88020006 :       DECL_VISIBILITY_SPECIFIED (r) = 0;
   14838                 :    88020006 :       DECL_ATTRIBUTES (r)
   14839                 :   176040012 :         = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   14840                 :             :     }
   14841                 :   107353874 :   determine_visibility (r);
   14842                 :   107353874 :   if (DECL_SECTION_NAME (t))
   14843                 :           6 :     set_decl_section_name (r, t);
   14844                 :   110753813 :   if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
   14845                 :       42319 :       && COMPLETE_TYPE_P (DECL_CONTEXT (r))
   14846                 :   107353874 :       && !processing_template_decl)
   14847                 :           0 :     defaulted_late_check (r);
   14848                 :             : 
   14849                 :   107353874 :   if (flag_openmp)
   14850                 :       38025 :     if (tree attr = lookup_attribute ("omp declare variant base",
   14851                 :       38025 :                                       DECL_ATTRIBUTES (r)))
   14852                 :          60 :       omp_declare_variant_finalize (r, attr);
   14853                 :             : 
   14854                 :   107353874 :   return r;
   14855                 :             : }
   14856                 :             : 
   14857                 :             : /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL.  */
   14858                 :             : 
   14859                 :             : static tree
   14860                 :    21191807 : tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
   14861                 :             :                       tree lambda_fntype, tree lambda_tparms)
   14862                 :             : {
   14863                 :             :   /* We can get here when processing a member function template,
   14864                 :             :      member class template, or template template parameter.  */
   14865                 :    21191807 :   tree decl = DECL_TEMPLATE_RESULT (t);
   14866                 :    21191807 :   tree in_decl = t;
   14867                 :    21191807 :   tree spec;
   14868                 :    21191807 :   tree tmpl_args;
   14869                 :    21191807 :   tree full_args = NULL_TREE;
   14870                 :    21191807 :   tree r;
   14871                 :    21191807 :   hashval_t hash = 0;
   14872                 :             : 
   14873                 :    21191807 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   14874                 :             :     {
   14875                 :             :       /* Template template parameter is treated here.  */
   14876                 :       14058 :       tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   14877                 :       14058 :       if (new_type == error_mark_node)
   14878                 :             :         r = error_mark_node;
   14879                 :             :       /* If we get a real template back, return it.  This can happen in
   14880                 :             :          the context of most_specialized_partial_spec.  */
   14881                 :       14058 :       else if (TREE_CODE (new_type) == TEMPLATE_DECL)
   14882                 :             :         r = new_type;
   14883                 :             :       else
   14884                 :             :         /* The new TEMPLATE_DECL was built in
   14885                 :             :            reduce_template_parm_level.  */
   14886                 :       28116 :         r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
   14887                 :       14058 :       return r;
   14888                 :             :     }
   14889                 :             : 
   14890                 :    21177749 :   if (!lambda_fntype)
   14891                 :             :     {
   14892                 :             :       /* We might already have an instance of this template.
   14893                 :             :          The ARGS are for the surrounding class type, so the
   14894                 :             :          full args contain the tsubst'd args for the context,
   14895                 :             :          plus the innermost args from the template decl.  */
   14896                 :    21162023 :       tmpl_args = DECL_CLASS_TEMPLATE_P (t)
   14897                 :     1474043 :         ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
   14898                 :    19687980 :         : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
   14899                 :             :       /* Because this is a template, the arguments will still be
   14900                 :             :          dependent, even after substitution.  If
   14901                 :             :          PROCESSING_TEMPLATE_DECL is not set, the dependency
   14902                 :             :          predicates will short-circuit.  */
   14903                 :    21162023 :       ++processing_template_decl;
   14904                 :    21162023 :       full_args = tsubst_template_args (tmpl_args, args,
   14905                 :             :                                         complain, in_decl);
   14906                 :    21162023 :       --processing_template_decl;
   14907                 :    21162023 :       if (full_args == error_mark_node)
   14908                 :             :         return error_mark_node;
   14909                 :             : 
   14910                 :             :       /* If this is a default template template argument,
   14911                 :             :          tsubst might not have changed anything.  */
   14912                 :    21162001 :       if (full_args == tmpl_args)
   14913                 :             :         return t;
   14914                 :             : 
   14915                 :    21162001 :       hash = spec_hasher::hash (t, full_args);
   14916                 :    21162001 :       spec = retrieve_specialization (t, full_args, hash);
   14917                 :    21162001 :       if (spec != NULL_TREE)
   14918                 :             :         {
   14919                 :      639155 :           if (TYPE_P (spec))
   14920                 :             :             /* Type partial instantiations are stored as the type by
   14921                 :             :                lookup_template_class_1, not here as the template.  */
   14922                 :      639125 :             spec = CLASSTYPE_TI_TEMPLATE (spec);
   14923                 :          30 :           else if (TREE_CODE (spec) != TEMPLATE_DECL)
   14924                 :          30 :             spec = DECL_TI_TEMPLATE (spec);
   14925                 :      639155 :           return spec;
   14926                 :             :         }
   14927                 :             :     }
   14928                 :             : 
   14929                 :             :   /* Make a new template decl.  It will be similar to the
   14930                 :             :      original, but will record the current template arguments.
   14931                 :             :      We also create a new function declaration, which is just
   14932                 :             :      like the old one, but points to this new template, rather
   14933                 :             :      than the old one.  */
   14934                 :    20538572 :   r = copy_decl (t);
   14935                 :    20538572 :   gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
   14936                 :    20538572 :   DECL_CHAIN (r) = NULL_TREE;
   14937                 :             : 
   14938                 :             :   // Build new template info linking to the original template decl.
   14939                 :    20538572 :   if (!lambda_fntype)
   14940                 :             :     {
   14941                 :    20522846 :       DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   14942                 :    20522846 :       SET_DECL_IMPLICIT_INSTANTIATION (r);
   14943                 :             :     }
   14944                 :             :   else
   14945                 :       15726 :     DECL_TEMPLATE_INFO (r) = NULL_TREE;
   14946                 :             : 
   14947                 :             :   /* The template parameters for this new template are all the
   14948                 :             :      template parameters for the old template, except the
   14949                 :             :      outermost level of parameters.  */
   14950                 :    20538572 :   auto tparm_guard = make_temp_override (current_template_parms);
   14951                 :    41077144 :   DECL_TEMPLATE_PARMS (r)
   14952                 :    41077144 :     = current_template_parms
   14953                 :    20538572 :     = (lambda_tparms
   14954                 :    20538572 :        ? lambda_tparms
   14955                 :    20522846 :        : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
   14956                 :             :                                 complain));
   14957                 :             : 
   14958                 :    20538572 :   bool class_p = false;
   14959                 :    20538572 :   tree inner = decl;
   14960                 :    20538572 :   ++processing_template_decl;
   14961                 :    20538572 :   if (TREE_CODE (inner) == FUNCTION_DECL)
   14962                 :    17540171 :     inner = tsubst_function_decl (inner, args, complain, lambda_fntype,
   14963                 :             :                                   /*use_spec_table=*/false);
   14964                 :             :   else
   14965                 :             :     {
   14966                 :     2998401 :       if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
   14967                 :             :         {
   14968                 :      834899 :           class_p = true;
   14969                 :      834899 :           inner = TREE_TYPE (inner);
   14970                 :             :         }
   14971                 :     2998401 :       if (class_p)
   14972                 :      834899 :         inner = tsubst_aggr_type (inner, args, complain,
   14973                 :             :                                   in_decl, /*entering*/1);
   14974                 :             :       else
   14975                 :     2163502 :         inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false);
   14976                 :             :     }
   14977                 :    20538572 :   --processing_template_decl;
   14978                 :    20538572 :   if (inner == error_mark_node)
   14979                 :             :     return error_mark_node;
   14980                 :             : 
   14981                 :    20538507 :   if (class_p)
   14982                 :             :     {
   14983                 :             :       /* For a partial specialization, we need to keep pointing to
   14984                 :             :          the primary template.  */
   14985                 :      834877 :       if (!DECL_TEMPLATE_SPECIALIZATION (t))
   14986                 :             :         {
   14987                 :      665157 :           CLASSTYPE_TI_TEMPLATE (inner) = r;
   14988                 :      665157 :           CLASSTYPE_USE_TEMPLATE (inner) = 0;
   14989                 :             :         }
   14990                 :             : 
   14991                 :      834877 :       DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
   14992                 :      834877 :       inner = TYPE_MAIN_DECL (inner);
   14993                 :             :     }
   14994                 :    19703630 :   else if (lambda_fntype)
   14995                 :             :     {
   14996                 :       15726 :       tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
   14997                 :       15726 :       DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
   14998                 :             :     }
   14999                 :             :   else
   15000                 :             :     {
   15001                 :    19687904 :       DECL_TI_TEMPLATE (inner) = r;
   15002                 :             :       /* Set DECL_TI_ARGS to the full set of template arguments,
   15003                 :             :          which tsubst_function_decl / tsubst_decl didn't do due to
   15004                 :             :          use_spec_table=false.  */
   15005                 :    19687904 :       DECL_TI_ARGS (inner) = full_args;
   15006                 :    19687904 :       DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
   15007                 :             :     }
   15008                 :             : 
   15009                 :    20538507 :   DECL_TEMPLATE_RESULT (r) = inner;
   15010                 :    20538507 :   TREE_TYPE (r) = TREE_TYPE (inner);
   15011                 :    20538507 :   DECL_CONTEXT (r) = DECL_CONTEXT (inner);
   15012                 :             : 
   15013                 :    20538507 :   if (modules_p ())
   15014                 :             :     {
   15015                 :             :       /* Propagate module information from the decl.  */
   15016                 :       72388 :       DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
   15017                 :       72388 :       if (DECL_LANG_SPECIFIC (inner))
   15018                 :             :         /* If this is a constrained template, the above tsubst of
   15019                 :             :            inner can find the unconstrained template, which may have
   15020                 :             :            come from an import.  This is ok, because we don't
   15021                 :             :            register this instantiation (see below).  */
   15022                 :       70772 :         gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
   15023                 :             :                              || (TEMPLATE_PARMS_CONSTRAINTS
   15024                 :             :                                  (DECL_TEMPLATE_PARMS (t))));
   15025                 :             :     }
   15026                 :             : 
   15027                 :    20538507 :   DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
   15028                 :    20538507 :   DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
   15029                 :             : 
   15030                 :    20538507 :   if (PRIMARY_TEMPLATE_P (t))
   15031                 :    20368710 :     DECL_PRIMARY_TEMPLATE (r) = r;
   15032                 :             : 
   15033                 :    20538507 :   DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (r) = false;
   15034                 :             : 
   15035                 :    20538507 :   if (!lambda_fntype && !class_p)
   15036                 :             :     {
   15037                 :             :       /* Record this non-type partial instantiation.  */
   15038                 :             :       /* FIXME we'd like to always register the TEMPLATE_DECL, or always
   15039                 :             :          the DECL_TEMPLATE_RESULT, but it seems the modules code relies
   15040                 :             :          on this current behavior.  */
   15041                 :    19687904 :       if (TREE_CODE (inner) == FUNCTION_DECL)
   15042                 :    17524402 :         register_specialization (r, t, full_args, false, hash);
   15043                 :             :       else
   15044                 :     2163502 :         register_specialization (inner, t, full_args, false, hash);
   15045                 :             :     }
   15046                 :             : 
   15047                 :             :   return r;
   15048                 :    20538572 : }
   15049                 :             : 
   15050                 :             : /* True if FN is the op() for a lambda in an uninstantiated template.  */
   15051                 :             : 
   15052                 :             : bool
   15053                 :   760238561 : lambda_fn_in_template_p (tree fn)
   15054                 :             : {
   15055                 :   763737310 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15056                 :             :     return false;
   15057                 :      592673 :   tree closure = DECL_CONTEXT (fn);
   15058                 :      592673 :   return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
   15059                 :             : }
   15060                 :             : 
   15061                 :             : /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
   15062                 :             :    which the above is true.  */
   15063                 :             : 
   15064                 :             : bool
   15065                 :   232921805 : regenerated_lambda_fn_p (tree fn)
   15066                 :             : {
   15067                 :   235213339 :   if (!fn || !LAMBDA_FUNCTION_P (fn))
   15068                 :             :     return false;
   15069                 :      694768 :   tree closure = DECL_CONTEXT (fn);
   15070                 :      694768 :   tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
   15071                 :      694768 :   return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
   15072                 :             : }
   15073                 :             : 
   15074                 :             : /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
   15075                 :             :    If T is not a regenerated LAMBDA_EXPR, return T.  */
   15076                 :             : 
   15077                 :             : tree
   15078                 :      313180 : most_general_lambda (tree t)
   15079                 :             : {
   15080                 :      626443 :   while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15081                 :      313263 :     t = TI_TEMPLATE (ti);
   15082                 :      313180 :   return t;
   15083                 :             : }
   15084                 :             : 
   15085                 :             : /* Return the set of template arguments used to regenerate the lambda T
   15086                 :             :    from its most general lambda.  */
   15087                 :             : 
   15088                 :             : tree
   15089                 :      201056 : lambda_regenerating_args (tree t)
   15090                 :             : {
   15091                 :      402112 :   if (LAMBDA_FUNCTION_P (t))
   15092                 :      201056 :     t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t));
   15093                 :      201056 :   gcc_assert (TREE_CODE (t) == LAMBDA_EXPR);
   15094                 :      201056 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   15095                 :      201056 :     return TI_ARGS (ti);
   15096                 :             :   else
   15097                 :             :     return NULL_TREE;
   15098                 :             : }
   15099                 :             : 
   15100                 :             : /* We're instantiating a variable from template function TCTX.  Return the
   15101                 :             :    corresponding current enclosing scope.  We can match them up using
   15102                 :             :    DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
   15103                 :             :    the DECL_SOURCE_LOCATION for a function instantiation is updated to match
   15104                 :             :    the template definition in regenerate_decl_from_template.  */
   15105                 :             : 
   15106                 :             : static tree
   15107                 :       96255 : enclosing_instantiation_of (tree tctx)
   15108                 :             : {
   15109                 :       96255 :   tree fn = current_function_decl;
   15110                 :             : 
   15111                 :             :   /* We shouldn't ever need to do this for other artificial functions.  */
   15112                 :       96355 :   gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
   15113                 :             : 
   15114                 :       96258 :   for (; fn; fn = decl_function_context (fn))
   15115                 :       96258 :     if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
   15116                 :       96255 :       return fn;
   15117                 :           0 :   gcc_unreachable ();
   15118                 :             : }
   15119                 :             : 
   15120                 :             : /* Substitute the ARGS into the T, which is a _DECL.  Return the
   15121                 :             :    result of the substitution.  Issue error and warning messages under
   15122                 :             :    control of COMPLAIN.  The flag USE_SPEC_TABLE controls if we look up
   15123                 :             :    and insert into the specializations table or if we can assume it's
   15124                 :             :    the caller's responsibility; this is used by instantiate_template
   15125                 :             :    to avoid doing some redundant work.  */
   15126                 :             : 
   15127                 :             : static tree
   15128                 :   535988443 : tsubst_decl (tree t, tree args, tsubst_flags_t complain,
   15129                 :             :              bool use_spec_table /* = true */)
   15130                 :             : {
   15131                 :             : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   15132                 :   535988443 :   location_t saved_loc;
   15133                 :   535988443 :   tree r = NULL_TREE;
   15134                 :   535988443 :   tree in_decl = t;
   15135                 :   535988443 :   hashval_t hash = 0;
   15136                 :             : 
   15137                 :   535988443 :   if (t == error_mark_node)
   15138                 :             :     return error_mark_node;
   15139                 :             : 
   15140                 :             :   /* Set the filename and linenumber to improve error-reporting.  */
   15141                 :   535988440 :   saved_loc = input_location;
   15142                 :   535988440 :   input_location = DECL_SOURCE_LOCATION (t);
   15143                 :             : 
   15144                 :   535988440 :   switch (TREE_CODE (t))
   15145                 :             :     {
   15146                 :    21176081 :     case TEMPLATE_DECL:
   15147                 :    21176081 :       r = tsubst_template_decl (t, args, complain,
   15148                 :             :                                 /*lambda_fntype=*/NULL_TREE,
   15149                 :             :                                 /*lambda_tparms=*/NULL_TREE);
   15150                 :    21176081 :       break;
   15151                 :             : 
   15152                 :    91934216 :     case FUNCTION_DECL:
   15153                 :    91934216 :       r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE,
   15154                 :             :                                 use_spec_table);
   15155                 :    91926080 :       break;
   15156                 :             : 
   15157                 :   246356209 :     case PARM_DECL:
   15158                 :   246356209 :       {
   15159                 :   246356209 :         tree type = NULL_TREE;
   15160                 :   246356209 :         int i, len = 1;
   15161                 :   246356209 :         tree expanded_types = NULL_TREE;
   15162                 :   246356209 :         tree prev_r = NULL_TREE;
   15163                 :   246356209 :         tree first_r = NULL_TREE;
   15164                 :             : 
   15165                 :   246356209 :         if (DECL_PACK_P (t))
   15166                 :             :           {
   15167                 :             :             /* If there is a local specialization that isn't a
   15168                 :             :                parameter pack, it means that we're doing a "simple"
   15169                 :             :                substitution from inside tsubst_pack_expansion. Just
   15170                 :             :                return the local specialization (which will be a single
   15171                 :             :                parm).  */
   15172                 :     2924505 :             tree spec = retrieve_local_specialization (t);
   15173                 :     2924505 :             if (spec 
   15174                 :          31 :                 && TREE_CODE (spec) == PARM_DECL
   15175                 :     2924511 :                 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
   15176                 :           0 :               RETURN (spec);
   15177                 :             : 
   15178                 :             :             /* Expand the TYPE_PACK_EXPANSION that provides the types for
   15179                 :             :                the parameters in this function parameter pack.  */
   15180                 :     2924505 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   15181                 :             :                                                     complain, in_decl);
   15182                 :     2924505 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   15183                 :             :               {
   15184                 :     1209020 :                 len = TREE_VEC_LENGTH (expanded_types);
   15185                 :             : 
   15186                 :             :                 /* Zero-length parameter packs are boring. Just substitute
   15187                 :             :                    into the chain.  */
   15188                 :     1209020 :                 if (len == 0 && !cp_unevaluated_operand)
   15189                 :      589542 :                   RETURN (tsubst (TREE_CHAIN (t), args, complain,
   15190                 :             :                                   TREE_CHAIN (t)));
   15191                 :             :               }
   15192                 :             :             else
   15193                 :             :               {
   15194                 :             :                 /* All we did was update the type. Make a note of that.  */
   15195                 :             :                 type = expanded_types;
   15196                 :             :                 expanded_types = NULL_TREE;
   15197                 :             :               }
   15198                 :             :           }
   15199                 :             : 
   15200                 :             :         /* Loop through all of the parameters we'll build. When T is
   15201                 :             :            a function parameter pack, LEN is the number of expanded
   15202                 :             :            types in EXPANDED_TYPES; otherwise, LEN is 1.  */
   15203                 :   245766667 :         r = NULL_TREE;
   15204                 :   491700460 :         for (i = 0; i < len; ++i)
   15205                 :             :           {
   15206                 :   245933793 :             prev_r = r;
   15207                 :   245933793 :             r = copy_node (t);
   15208                 :   245933793 :             if (DECL_TEMPLATE_PARM_P (t))
   15209                 :     3170060 :               SET_DECL_TEMPLATE_PARM_P (r);
   15210                 :             : 
   15211                 :   245933793 :             if (expanded_types)
   15212                 :             :               /* We're on the Ith parameter of the function parameter
   15213                 :             :                  pack.  */
   15214                 :             :               {
   15215                 :             :                 /* Get the Ith type.  */
   15216                 :      786604 :                 type = TREE_VEC_ELT (expanded_types, i);
   15217                 :             : 
   15218                 :             :                 /* Rename the parameter to include the index.  */
   15219                 :      786604 :                 DECL_NAME (r)
   15220                 :     1573208 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   15221                 :             :               }
   15222                 :   245147189 :             else if (!type)
   15223                 :             :               /* We're dealing with a normal parameter.  */
   15224                 :   243431704 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15225                 :             : 
   15226                 :   245933793 :             type = type_decays_to (type);
   15227                 :   245933793 :             TREE_TYPE (r) = type;
   15228                 :   245933793 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15229                 :             : 
   15230                 :   245933793 :             if (DECL_INITIAL (r))
   15231                 :             :               {
   15232                 :    38485453 :                 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
   15233                 :    35315393 :                   DECL_INITIAL (r) = TREE_TYPE (r);
   15234                 :             :                 else
   15235                 :     3170060 :                   DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
   15236                 :             :                                              complain, in_decl);
   15237                 :             :               }
   15238                 :             : 
   15239                 :   245933793 :             DECL_CONTEXT (r) = NULL_TREE;
   15240                 :             : 
   15241                 :   245933793 :             if (!DECL_TEMPLATE_PARM_P (r))
   15242                 :   242763733 :               DECL_ARG_TYPE (r) = type_passed_as (type);
   15243                 :             : 
   15244                 :   245933793 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15245                 :             :                                                  args, complain, in_decl))
   15246                 :           0 :               return error_mark_node;
   15247                 :             : 
   15248                 :             :             /* Keep track of the first new parameter we
   15249                 :             :                generate. That's what will be returned to the
   15250                 :             :                caller.  */
   15251                 :   245933793 :             if (!first_r)
   15252                 :   245766351 :               first_r = r;
   15253                 :             : 
   15254                 :             :             /* Build a proper chain of parameters when substituting
   15255                 :             :                into a function parameter pack.  */
   15256                 :   245933793 :             if (prev_r)
   15257                 :      167442 :               DECL_CHAIN (prev_r) = r;
   15258                 :             :           }
   15259                 :             : 
   15260                 :             :         /* If cp_unevaluated_operand is set, we're just looking for a
   15261                 :             :            single dummy parameter, so don't keep going.  */
   15262                 :   245766667 :         if (DECL_CHAIN (t) && !cp_unevaluated_operand)
   15263                 :   129781180 :           DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
   15264                 :   129781180 :                                    complain, DECL_CHAIN (t));
   15265                 :             : 
   15266                 :             :         /* FIRST_R contains the start of the chain we've built.  */
   15267                 :   245766667 :         r = first_r;
   15268                 :             :       }
   15269                 :   245766667 :       break;
   15270                 :             : 
   15271                 :     6114340 :     case FIELD_DECL:
   15272                 :     6114340 :       {
   15273                 :     6114340 :         tree type = NULL_TREE;
   15274                 :     6114340 :         tree vec = NULL_TREE;
   15275                 :     6114340 :         tree expanded_types = NULL_TREE;
   15276                 :     6114340 :         int len = 1;
   15277                 :             : 
   15278                 :     6114340 :         if (PACK_EXPANSION_P (TREE_TYPE (t)))
   15279                 :             :           {
   15280                 :             :             /* This field is a lambda capture pack.  Return a TREE_VEC of
   15281                 :             :                the expanded fields to instantiate_class_template_1.  */
   15282                 :          58 :             expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
   15283                 :             :                                                     complain, in_decl);
   15284                 :          58 :             if (TREE_CODE (expanded_types) == TREE_VEC)
   15285                 :             :               {
   15286                 :          51 :                 len = TREE_VEC_LENGTH (expanded_types);
   15287                 :          51 :                 vec = make_tree_vec (len);
   15288                 :             :               }
   15289                 :             :             else
   15290                 :             :               {
   15291                 :             :                 /* All we did was update the type. Make a note of that.  */
   15292                 :             :                 type = expanded_types;
   15293                 :             :                 expanded_types = NULL_TREE;
   15294                 :             :               }
   15295                 :             :           }
   15296                 :             : 
   15297                 :    12228515 :         for (int i = 0; i < len; ++i)
   15298                 :             :           {
   15299                 :     6114357 :             r = copy_decl (t);
   15300                 :     6114357 :             if (expanded_types)
   15301                 :             :               {
   15302                 :          68 :                 type = TREE_VEC_ELT (expanded_types, i);
   15303                 :          68 :                 DECL_NAME (r)
   15304                 :         136 :                   = make_ith_pack_parameter_name (DECL_NAME (r), i);
   15305                 :             :               }
   15306                 :     6114289 :             else if (!type)
   15307                 :     6114282 :               type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15308                 :             : 
   15309                 :     6114349 :             if (type == error_mark_node)
   15310                 :         174 :               RETURN (error_mark_node);
   15311                 :     6114175 :             TREE_TYPE (r) = type;
   15312                 :     6114175 :             cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15313                 :             : 
   15314                 :     6114175 :             if (DECL_C_BIT_FIELD (r))
   15315                 :             :               /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
   15316                 :             :                  number of bits.  */
   15317                 :      104364 :               DECL_BIT_FIELD_REPRESENTATIVE (r)
   15318                 :      104364 :                 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
   15319                 :             :                                complain, in_decl);
   15320                 :     6114175 :             if (DECL_INITIAL (t))
   15321                 :             :               {
   15322                 :             :                 /* Set up DECL_TEMPLATE_INFO so that we can get at the
   15323                 :             :                    NSDMI in perform_member_init.  Still set DECL_INITIAL
   15324                 :             :                    so that we know there is one.  */
   15325                 :      326067 :                 DECL_INITIAL (r) = void_node;
   15326                 :      326067 :                 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
   15327                 :      326067 :                 retrofit_lang_decl (r);
   15328                 :      326067 :                 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
   15329                 :             :               }
   15330                 :             :             /* We don't have to set DECL_CONTEXT here; it is set by
   15331                 :             :                finish_member_declaration.  */
   15332                 :     6114175 :             DECL_CHAIN (r) = NULL_TREE;
   15333                 :             : 
   15334                 :     6114175 :             if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
   15335                 :             :                                                  args, complain, in_decl))
   15336                 :           0 :               return error_mark_node;
   15337                 :             : 
   15338                 :     6114175 :             if (vec)
   15339                 :          68 :               TREE_VEC_ELT (vec, i) = r;
   15340                 :             :           }
   15341                 :             : 
   15342                 :     6114158 :         if (vec)
   15343                 :          51 :           r = vec;
   15344                 :             :       }
   15345                 :             :       break;
   15346                 :             : 
   15347                 :     1253242 :     case USING_DECL:
   15348                 :             :       /* We reach here only for member using decls.  We also need to check
   15349                 :             :          uses_template_parms because DECL_DEPENDENT_P is not set for a
   15350                 :             :          using-declaration that designates a member of the current
   15351                 :             :          instantiation (c++/53549).  */
   15352                 :     1253242 :       if (DECL_DEPENDENT_P (t)
   15353                 :     1253242 :           || uses_template_parms (USING_DECL_SCOPE (t)))
   15354                 :             :         {
   15355                 :             :           /* True iff this using-decl was written as a pack expansion
   15356                 :             :              (and a pack appeared in its scope or name).  If a pack
   15357                 :             :              appeared in both, we expand the packs separately and
   15358                 :             :              manually merge them.  */
   15359                 :     1253030 :           bool variadic_p = false;
   15360                 :             : 
   15361                 :     1253030 :           tree scope = USING_DECL_SCOPE (t);
   15362                 :     1253030 :           if (PACK_EXPANSION_P (scope))
   15363                 :             :             {
   15364                 :         228 :               scope = tsubst_pack_expansion (scope, args,
   15365                 :             :                                              complain | tf_qualifying_scope,
   15366                 :             :                                              in_decl);
   15367                 :         228 :               variadic_p = true;
   15368                 :             :             }
   15369                 :             :           else
   15370                 :     1252802 :             scope = tsubst_scope (scope, args, complain, in_decl);
   15371                 :             : 
   15372                 :     1253030 :           tree name = DECL_NAME (t);
   15373                 :     1253030 :           if (IDENTIFIER_CONV_OP_P (name)
   15374                 :     1253030 :               && PACK_EXPANSION_P (TREE_TYPE (name)))
   15375                 :             :             {
   15376                 :          22 :               name = tsubst_pack_expansion (TREE_TYPE (name), args,
   15377                 :             :                                             complain, in_decl);
   15378                 :          22 :               if (name == error_mark_node)
   15379                 :             :                 {
   15380                 :           0 :                   r = error_mark_node;
   15381                 :           0 :                   break;
   15382                 :             :                 }
   15383                 :          56 :               for (tree& elt : tree_vec_range (name))
   15384                 :          34 :                 elt = make_conv_op_name (elt);
   15385                 :          22 :               variadic_p = true;
   15386                 :             :             }
   15387                 :             :           else
   15388                 :     1253008 :             name = tsubst_name (name, args, complain, in_decl);
   15389                 :             : 
   15390                 :     1253030 :           int len;
   15391                 :     1253030 :           if (!variadic_p)
   15392                 :             :             len = 1;
   15393                 :         234 :           else if (TREE_CODE (scope) == TREE_VEC
   15394                 :         228 :                    && TREE_CODE (name) == TREE_VEC)
   15395                 :             :             {
   15396                 :          16 :               if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name))
   15397                 :             :                 {
   15398                 :           2 :                   error ("mismatched argument pack lengths (%d vs %d)",
   15399                 :           2 :                          TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name));
   15400                 :           2 :                   r = error_mark_node;
   15401                 :           2 :                   break;
   15402                 :             :                 }
   15403                 :             :               len = TREE_VEC_LENGTH (scope);
   15404                 :             :             }
   15405                 :         218 :           else if (TREE_CODE (scope) == TREE_VEC)
   15406                 :         212 :             len = TREE_VEC_LENGTH (scope);
   15407                 :             :           else /* TREE_CODE (name) == TREE_VEC  */
   15408                 :           6 :             len = TREE_VEC_LENGTH (name);
   15409                 :             : 
   15410                 :     1253028 :           r = make_tree_vec (len);
   15411                 :     2506346 :           for (int i = 0; i < len; ++i)
   15412                 :             :             {
   15413                 :     1253355 :               tree escope = (TREE_CODE (scope) == TREE_VEC
   15414                 :     1253355 :                              ? TREE_VEC_ELT (scope, i)
   15415                 :     1253355 :                              : scope);
   15416                 :     1253355 :               tree ename = (TREE_CODE (name) == TREE_VEC
   15417                 :     1253355 :                             ? TREE_VEC_ELT (name, i)
   15418                 :     1253355 :                             : name);
   15419                 :     1253355 :               tree elt = do_class_using_decl (escope, ename);
   15420                 :     1253355 :               if (!elt)
   15421                 :             :                 {
   15422                 :          37 :                   r = error_mark_node;
   15423                 :          37 :                   break;
   15424                 :             :                 }
   15425                 :     1253318 :               TREE_PROTECTED (elt) = TREE_PROTECTED (t);
   15426                 :     1253318 :               TREE_PRIVATE (elt) = TREE_PRIVATE (t);
   15427                 :     1253318 :               TREE_VEC_ELT (r, i) = elt;
   15428                 :             :             }
   15429                 :             : 
   15430                 :     1253028 :           if (!variadic_p && r != error_mark_node)
   15431                 :     1252759 :             r = TREE_VEC_ELT (r, 0);
   15432                 :             :         }
   15433                 :             :       else
   15434                 :             :         {
   15435                 :         212 :           r = copy_node (t);
   15436                 :         212 :           DECL_CHAIN (r) = NULL_TREE;
   15437                 :             :         }
   15438                 :             :       break;
   15439                 :             : 
   15440                 :   169154352 :     case TYPE_DECL:
   15441                 :   169154352 :     case VAR_DECL:
   15442                 :   169154352 :       {
   15443                 :   169154352 :         tree argvec = NULL_TREE;
   15444                 :   169154352 :         tree gen_tmpl = NULL_TREE;
   15445                 :   169154352 :         tree tmpl = NULL_TREE;
   15446                 :   169154352 :         tree type = NULL_TREE;
   15447                 :             : 
   15448                 :   169154352 :         if (TREE_TYPE (t) == error_mark_node)
   15449                 :           6 :           RETURN (error_mark_node);
   15450                 :             : 
   15451                 :   169154346 :         if (TREE_CODE (t) == TYPE_DECL
   15452                 :   169154346 :             && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
   15453                 :             :           {
   15454                 :             :             /* If this is the canonical decl, we don't have to
   15455                 :             :                mess with instantiations, and often we can't (for
   15456                 :             :                typename, template type parms and such).  Note that
   15457                 :             :                TYPE_NAME is not correct for the above test if
   15458                 :             :                we've copied the type for a typedef.  */
   15459                 :    32437755 :             type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15460                 :    32437695 :             if (type == error_mark_node)
   15461                 :           8 :               RETURN (error_mark_node);
   15462                 :    32437687 :             r = TYPE_NAME (type);
   15463                 :    32437687 :             break;
   15464                 :             :           }
   15465                 :             : 
   15466                 :             :         /* Check to see if we already have the specialization we
   15467                 :             :            need.  */
   15468                 :   136716591 :         tree spec = NULL_TREE;
   15469                 :   136716591 :         bool local_p = false;
   15470                 :   136716591 :         tree ctx = DECL_CONTEXT (t);
   15471                 :    27092978 :         if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
   15472                 :   163809466 :             && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
   15473                 :             :           {
   15474                 :   117189582 :             local_p = false;
   15475                 :   117189582 :             if (DECL_CLASS_SCOPE_P (t))
   15476                 :             :               {
   15477                 :    83756371 :                 ctx = tsubst_aggr_type (ctx, args,
   15478                 :             :                                         complain,
   15479                 :             :                                         in_decl, /*entering_scope=*/1);
   15480                 :    83756371 :                 if (DECL_SELF_REFERENCE_P (t))
   15481                 :             :                   /* The context and type of an injected-class-name are
   15482                 :             :                      the same, so we don't need to substitute both.  */
   15483                 :             :                   type = ctx;
   15484                 :             :                 /* If CTX is unchanged, then T is in fact the
   15485                 :             :                    specialization we want.  That situation occurs when
   15486                 :             :                    referencing a static data member within in its own
   15487                 :             :                    class.  We can use pointer equality, rather than
   15488                 :             :                    same_type_p, because DECL_CONTEXT is always
   15489                 :             :                    canonical...  */
   15490                 :    83756371 :                 if (ctx == DECL_CONTEXT (t)
   15491                 :             :                     /* ... unless T is a member template; in which
   15492                 :             :                        case our caller can be willing to create a
   15493                 :             :                        specialization of that template represented
   15494                 :             :                        by T.  */
   15495                 :    83756371 :                     && !(DECL_TI_TEMPLATE (t)
   15496                 :    15568507 :                          && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
   15497                 :             :                   spec = t;
   15498                 :             :               }
   15499                 :             : 
   15500                 :   117189582 :             if (!spec)
   15501                 :             :               {
   15502                 :   115398151 :                 tmpl = DECL_TI_TEMPLATE (t);
   15503                 :   115398151 :                 if (use_spec_table)
   15504                 :             :                   {
   15505                 :    64172641 :                     argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
   15506                 :    64172641 :                     if (argvec == error_mark_node)
   15507                 :           0 :                       RETURN (error_mark_node);
   15508                 :    64172641 :                     gen_tmpl = most_general_template (tmpl);
   15509                 :    64172641 :                     hash = spec_hasher::hash (gen_tmpl, argvec);
   15510                 :    64172641 :                     spec = retrieve_specialization (gen_tmpl, argvec, hash);
   15511                 :             :                   }
   15512                 :             :                 else
   15513                 :             :                   argvec = args;
   15514                 :             :               }
   15515                 :             :           }
   15516                 :             :         else
   15517                 :             :           {
   15518                 :    19527009 :             if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
   15519                 :             :               /* Subsequent calls to pushdecl will fill this in.  */
   15520                 :             :               ctx = NULL_TREE;
   15521                 :             :             /* A local variable.  */
   15522                 :    19527009 :             local_p = true;
   15523                 :             :             /* Unless this is a reference to a static variable from an
   15524                 :             :                enclosing function, in which case we need to fill it in now.  */
   15525                 :    19527009 :             if (TREE_STATIC (t))
   15526                 :             :               {
   15527                 :       96145 :                 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
   15528                 :       96145 :                 if (fn != current_function_decl)
   15529                 :    19527009 :                   ctx = fn;
   15530                 :             :               }
   15531                 :    19527009 :             spec = retrieve_local_specialization (t);
   15532                 :             :           }
   15533                 :             :         /* If we already have the specialization we need, there is
   15534                 :             :            nothing more to do.  */
   15535                 :   136716591 :         if (spec)
   15536                 :             :           {
   15537                 :     3542360 :             r = spec;
   15538                 :     3542360 :             break;
   15539                 :             :           }
   15540                 :             : 
   15541                 :             :         /* Create a new node for the specialization we need.  */
   15542                 :   133174231 :         if (type == NULL_TREE)
   15543                 :             :           {
   15544                 :   105440321 :             if (is_typedef_decl (t))
   15545                 :             :               type = DECL_ORIGINAL_TYPE (t);
   15546                 :             :             else
   15547                 :    23550775 :               type = TREE_TYPE (t);
   15548                 :   105440321 :             if (VAR_P (t)
   15549                 :    23550622 :                 && VAR_HAD_UNKNOWN_BOUND (t)
   15550                 :   105440547 :                 && type != error_mark_node)
   15551                 :         226 :               type = strip_array_domain (type);
   15552                 :   105440321 :             tsubst_flags_t tcomplain = complain;
   15553                 :   105440321 :             if (VAR_P (t))
   15554                 :    23550622 :               tcomplain |= tf_tst_ok;
   15555                 :   105440321 :             type = tsubst (type, args, tcomplain, in_decl);
   15556                 :             :             /* Substituting the type might have recursively instantiated this
   15557                 :             :                same alias (c++/86171).  */
   15558                 :    34688183 :             if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
   15559                 :   137796924 :                 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
   15560                 :             :               {
   15561                 :           0 :                 r = spec;
   15562                 :           0 :                 break;
   15563                 :             :               }
   15564                 :             :           }
   15565                 :   133174231 :         if (type == error_mark_node && !(complain & tf_error))
   15566                 :    24744375 :           RETURN (error_mark_node);
   15567                 :   108429856 :         r = copy_decl (t);
   15568                 :   108429856 :         if (VAR_P (r))
   15569                 :             :           {
   15570                 :    23550622 :             DECL_INITIALIZED_P (r) = 0;
   15571                 :    23550622 :             DECL_TEMPLATE_INSTANTIATED (r) = 0;
   15572                 :    23550622 :             if (TREE_CODE (type) == FUNCTION_TYPE)
   15573                 :             :               {
   15574                 :             :                 /* It may seem that this case cannot occur, since:
   15575                 :             : 
   15576                 :             :                    typedef void f();
   15577                 :             :                    void g() { f x; }
   15578                 :             : 
   15579                 :             :                    declares a function, not a variable.  However:
   15580                 :             : 
   15581                 :             :                    typedef void f();
   15582                 :             :                    template <typename T> void g() { T t; }
   15583                 :             :                    template void g<f>();
   15584                 :             : 
   15585                 :             :                    is an attempt to declare a variable with function
   15586                 :             :                    type.  */
   15587                 :           4 :                 error ("variable %qD has function type",
   15588                 :             :                        /* R is not yet sufficiently initialized, so we
   15589                 :             :                           just use its name.  */
   15590                 :           4 :                        DECL_NAME (r));
   15591                 :           4 :                 RETURN (error_mark_node);
   15592                 :             :               }
   15593                 :    23550618 :             type = complete_type (type);
   15594                 :             :             /* Wait until cp_finish_decl to set this again, to handle
   15595                 :             :                circular dependency (template/instantiate6.C). */
   15596                 :    23550614 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
   15597                 :    23550614 :             type = check_var_type (DECL_NAME (r), type,
   15598                 :    23550614 :                                    DECL_SOURCE_LOCATION (r));
   15599                 :    23550614 :             if (DECL_HAS_VALUE_EXPR_P (t))
   15600                 :             :               {
   15601                 :      148065 :                 tree ve = DECL_VALUE_EXPR (t);
   15602                 :             :                 /* If the DECL_VALUE_EXPR is converted to the declared type,
   15603                 :             :                    preserve the identity so that gimplify_type_sizes works.  */
   15604                 :      148065 :                 bool nop = (TREE_CODE (ve) == NOP_EXPR);
   15605                 :      148065 :                 if (nop)
   15606                 :           3 :                   ve = TREE_OPERAND (ve, 0);
   15607                 :      148065 :                 ve = tsubst_expr (ve, args, complain, in_decl);
   15608                 :      148065 :                 if (REFERENCE_REF_P (ve))
   15609                 :             :                   {
   15610                 :       78272 :                     gcc_assert (TYPE_REF_P (type));
   15611                 :       78272 :                     ve = TREE_OPERAND (ve, 0);
   15612                 :             :                   }
   15613                 :      148065 :                 if (nop)
   15614                 :           3 :                   ve = build_nop (type, ve);
   15615                 :      148062 :                 else if (DECL_LANG_SPECIFIC (t)
   15616                 :      147872 :                          && DECL_OMP_PRIVATIZED_MEMBER (t)
   15617                 :         198 :                          && TREE_CODE (ve) == COMPONENT_REF
   15618                 :         198 :                          && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
   15619                 :      148260 :                          && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
   15620                 :           8 :                   type = TREE_TYPE (ve);
   15621                 :             :                 else
   15622                 :      148054 :                   gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
   15623                 :             :                                        == TYPE_MAIN_VARIANT (type));
   15624                 :      148065 :                 SET_DECL_VALUE_EXPR (r, ve);
   15625                 :             :               }
   15626                 :    23550614 :             if (CP_DECL_THREAD_LOCAL_P (r)
   15627                 :    23550614 :                 && !processing_template_decl)
   15628                 :          40 :               set_decl_tls_model (r, decl_default_tls_model (r));
   15629                 :             :           }
   15630                 :    84879234 :         else if (DECL_SELF_REFERENCE_P (t))
   15631                 :    27733910 :           SET_DECL_SELF_REFERENCE_P (r);
   15632                 :   108429848 :         TREE_TYPE (r) = type;
   15633                 :   108429848 :         cp_apply_type_quals_to_decl (cp_type_quals (type), r);
   15634                 :   108429848 :         DECL_CONTEXT (r) = ctx;
   15635                 :             :         /* Clear out the mangled name and RTL for the instantiation.  */
   15636                 :   108429848 :         SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
   15637                 :   108429848 :         if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
   15638                 :   108429848 :           SET_DECL_RTL (r, NULL);
   15639                 :   108429848 :         set_instantiating_module (r);
   15640                 :             : 
   15641                 :             :         /* The initializer must not be expanded until it is required;
   15642                 :             :            see [temp.inst].  */
   15643                 :   108429848 :         DECL_INITIAL (r) = NULL_TREE;
   15644                 :   108429848 :         DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
   15645                 :   108429848 :         if (VAR_P (r))
   15646                 :             :           {
   15647                 :    23550614 :             if (DECL_LANG_SPECIFIC (r))
   15648                 :     5969040 :               SET_DECL_DEPENDENT_INIT_P (r, false);
   15649                 :             : 
   15650                 :    23550614 :             SET_DECL_MODE (r, VOIDmode);
   15651                 :             : 
   15652                 :             :             /* Possibly limit visibility based on template args.  */
   15653                 :    23550614 :             DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
   15654                 :    23550614 :             if (DECL_VISIBILITY_SPECIFIED (t))
   15655                 :             :               {
   15656                 :           0 :                 DECL_VISIBILITY_SPECIFIED (r) = 0;
   15657                 :           0 :                 DECL_ATTRIBUTES (r)
   15658                 :           0 :                   = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
   15659                 :             :               }
   15660                 :    23550614 :             determine_visibility (r);
   15661                 :    23550614 :             if ((!local_p || TREE_STATIC (t)) && DECL_SECTION_NAME (t))
   15662                 :           9 :               set_decl_section_name (r, t);
   15663                 :             :           }
   15664                 :             : 
   15665                 :   108429848 :         if (!local_p)
   15666                 :             :           {
   15667                 :             :             /* A static data member declaration is always marked
   15668                 :             :                external when it is declared in-class, even if an
   15669                 :             :                initializer is present.  We mimic the non-template
   15670                 :             :                processing here.  */
   15671                 :    88903228 :             DECL_EXTERNAL (r) = 1;
   15672                 :    88903228 :             if (DECL_NAMESPACE_SCOPE_P (t))
   15673                 :    20415383 :               DECL_NOT_REALLY_EXTERN (r) = 1;
   15674                 :             : 
   15675                 :    88903228 :             DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
   15676                 :    88903228 :             SET_DECL_IMPLICIT_INSTANTIATION (r);
   15677                 :    88903228 :             if (use_spec_table)
   15678                 :    62422093 :               register_specialization (r, gen_tmpl, argvec, false, hash);
   15679                 :             :           }
   15680                 :             :         else
   15681                 :             :           {
   15682                 :    19526620 :             if (DECL_LANG_SPECIFIC (r))
   15683                 :      659298 :               DECL_TEMPLATE_INFO (r) = NULL_TREE;
   15684                 :    19526620 :             if (!cp_unevaluated_operand)
   15685                 :    19526620 :               register_local_specialization (r, t);
   15686                 :             :           }
   15687                 :             : 
   15688                 :   108429848 :         DECL_CHAIN (r) = NULL_TREE;
   15689                 :             : 
   15690                 :   108429848 :         if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
   15691                 :             :                                              /*flags=*/0,
   15692                 :             :                                              args, complain, in_decl))
   15693                 :           4 :           return error_mark_node;
   15694                 :             : 
   15695                 :             :         /* Preserve a typedef that names a type.  */
   15696                 :   193308921 :         if (is_typedef_decl (r) && type != error_mark_node)
   15697                 :             :           {
   15698                 :    84878708 :             DECL_ORIGINAL_TYPE (r) = NULL_TREE;
   15699                 :    84878708 :             set_underlying_type (r);
   15700                 :             : 
   15701                 :             :             /* common_handle_aligned_attribute doesn't apply the alignment
   15702                 :             :                to DECL_ORIGINAL_TYPE.  */
   15703                 :    84878708 :             if (TYPE_USER_ALIGN (TREE_TYPE (t)))
   15704                 :          84 :               TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r),
   15705                 :          84 :                                                   TYPE_ALIGN (TREE_TYPE (t)));
   15706                 :             :           }
   15707                 :             : 
   15708                 :   108429844 :         layout_decl (r, 0);
   15709                 :             :       }
   15710                 :   108429844 :       break;
   15711                 :             : 
   15712                 :           0 :     default:
   15713                 :           0 :       gcc_unreachable ();
   15714                 :             :     }
   15715                 :             : #undef RETURN
   15716                 :             : 
   15717                 :   535980228 :  out:
   15718                 :             :   /* Restore the file and line information.  */
   15719                 :   535980228 :   input_location = saved_loc;
   15720                 :             : 
   15721                 :   535980228 :   return r;
   15722                 :             : }
   15723                 :             : 
   15724                 :             : /* Substitute into the complete parameter type list PARMS.  */
   15725                 :             : 
   15726                 :             : tree
   15727                 :     1374887 : tsubst_function_parms (tree parms,
   15728                 :             :                        tree args,
   15729                 :             :                        tsubst_flags_t complain,
   15730                 :             :                        tree in_decl)
   15731                 :             : {
   15732                 :     1374887 :   return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
   15733                 :             : }
   15734                 :             : 
   15735                 :             : /* Substitute into the ARG_TYPES of a function type.
   15736                 :             :    If END is a TREE_CHAIN, leave it and any following types
   15737                 :             :    un-substituted.  */
   15738                 :             : 
   15739                 :             : static tree
   15740                 :   364890082 : tsubst_arg_types (tree arg_types,
   15741                 :             :                   tree args,
   15742                 :             :                   tree end,
   15743                 :             :                   tsubst_flags_t complain,
   15744                 :             :                   tree in_decl)
   15745                 :             : {
   15746                 :   364890082 :   tree type = NULL_TREE;
   15747                 :   364890082 :   int len = 1;
   15748                 :   364890082 :   tree expanded_args = NULL_TREE;
   15749                 :             : 
   15750                 :   364890082 :   if (!arg_types || arg_types == void_list_node || arg_types == end)
   15751                 :             :     return arg_types;
   15752                 :             : 
   15753                 :   238156184 :   if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
   15754                 :             :     {
   15755                 :             :       /* For a pack expansion, perform substitution on the
   15756                 :             :          entire expression. Later on, we'll handle the arguments
   15757                 :             :          one-by-one.  */
   15758                 :     5598618 :       expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
   15759                 :             :                                             args, complain, in_decl);
   15760                 :             : 
   15761                 :     5598618 :       if (TREE_CODE (expanded_args) == TREE_VEC)
   15762                 :             :         /* So that we'll spin through the parameters, one by one.  */
   15763                 :     3289081 :         len = TREE_VEC_LENGTH (expanded_args);
   15764                 :             :       else
   15765                 :             :         {
   15766                 :             :           /* We only partially substituted into the parameter
   15767                 :             :              pack. Our type is TYPE_PACK_EXPANSION.  */
   15768                 :             :           type = expanded_args;
   15769                 :             :           expanded_args = NULL_TREE;
   15770                 :             :         }
   15771                 :             :     }
   15772                 :             :   else
   15773                 :   232557566 :     type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
   15774                 :             : 
   15775                 :             :   /* Check if a substituted type is erroneous before substituting into
   15776                 :             :      the rest of the chain.  */
   15777                 :   474712664 :   for (int i = 0; i < len; i++)
   15778                 :             :     {
   15779                 :   236561406 :       if (expanded_args)
   15780                 :     1694303 :         type = TREE_VEC_ELT (expanded_args, i);
   15781                 :             : 
   15782                 :   236561406 :       if (type == error_mark_node)
   15783                 :        4905 :         return error_mark_node;
   15784                 :   236556501 :       if (VOID_TYPE_P (type))
   15785                 :             :         {
   15786                 :          21 :           if (complain & tf_error)
   15787                 :             :             {
   15788                 :           5 :               error ("invalid parameter type %qT", type);
   15789                 :           5 :               if (in_decl)
   15790                 :           4 :                 error ("in declaration %q+D", in_decl);
   15791                 :             :             }
   15792                 :          21 :           return error_mark_node;
   15793                 :             :         }
   15794                 :             :     }
   15795                 :             : 
   15796                 :             :   /* We do not substitute into default arguments here.  The standard
   15797                 :             :      mandates that they be instantiated only when needed, which is
   15798                 :             :      done in build_over_call.  */
   15799                 :   238151258 :   tree default_arg = TREE_PURPOSE (arg_types);
   15800                 :             : 
   15801                 :             :   /* Except that we do substitute default arguments under tsubst_lambda_expr,
   15802                 :             :      since the new op() won't have any associated template arguments for us
   15803                 :             :      to refer to later.  */
   15804                 :   238151258 :   if (lambda_fn_in_template_p (in_decl)
   15805                 :   238151258 :       || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL
   15806                 :   222815228 :           && DECL_LOCAL_DECL_P (in_decl)))
   15807                 :      107010 :     default_arg = tsubst_expr (default_arg, args, complain, in_decl);
   15808                 :             : 
   15809                 :   238151258 :   tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
   15810                 :   238151258 :                                                args, end, complain, in_decl);
   15811                 :   238151258 :   if (remaining_arg_types == error_mark_node)
   15812                 :             :     return error_mark_node;
   15813                 :             : 
   15814                 :   474694162 :   for (int i = len-1; i >= 0; i--)
   15815                 :             :     {
   15816                 :   236549692 :       if (expanded_args)
   15817                 :     1694303 :         type = TREE_VEC_ELT (expanded_args, i);
   15818                 :             : 
   15819                 :             :       /* Do array-to-pointer, function-to-pointer conversion, and ignore
   15820                 :             :          top-level qualifiers as required.  */
   15821                 :   236549692 :       type = cv_unqualified (type_decays_to (type));
   15822                 :             : 
   15823                 :   236549692 :       if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
   15824                 :             :         {
   15825                 :             :           /* We've instantiated a template before its default arguments
   15826                 :             :              have been parsed.  This can happen for a nested template
   15827                 :             :              class, and is not an error unless we require the default
   15828                 :             :              argument in a call of this function.  */
   15829                 :           4 :           remaining_arg_types
   15830                 :           4 :             = tree_cons (default_arg, type, remaining_arg_types);
   15831                 :           4 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
   15832                 :             :                          remaining_arg_types);
   15833                 :             :         }
   15834                 :             :       else
   15835                 :   236549688 :         remaining_arg_types
   15836                 :   236549688 :           = hash_tree_cons (default_arg, type, remaining_arg_types);
   15837                 :             :     }
   15838                 :             : 
   15839                 :   238144470 :   return remaining_arg_types;
   15840                 :             : }
   15841                 :             : 
   15842                 :             : /* Substitute into a FUNCTION_TYPE or METHOD_TYPE.  This routine does
   15843                 :             :    *not* handle the exception-specification for FNTYPE, because the
   15844                 :             :    initial substitution of explicitly provided template parameters
   15845                 :             :    during argument deduction forbids substitution into the
   15846                 :             :    exception-specification:
   15847                 :             : 
   15848                 :             :      [temp.deduct]
   15849                 :             : 
   15850                 :             :      All references in the function type of the function template to  the
   15851                 :             :      corresponding template parameters are replaced by the specified tem-
   15852                 :             :      plate argument values.  If a substitution in a template parameter or
   15853                 :             :      in  the function type of the function template results in an invalid
   15854                 :             :      type, type deduction fails.  [Note: The equivalent  substitution  in
   15855                 :             :      exception specifications is done only when the function is instanti-
   15856                 :             :      ated, at which point a program is  ill-formed  if  the  substitution
   15857                 :             :      results in an invalid type.]  */
   15858                 :             : 
   15859                 :             : static tree
   15860                 :   124144783 : tsubst_function_type (tree t,
   15861                 :             :                       tree args,
   15862                 :             :                       tsubst_flags_t complain,
   15863                 :             :                       tree in_decl)
   15864                 :             : {
   15865                 :   124144783 :   tree return_type;
   15866                 :   124144783 :   tree arg_types = NULL_TREE;
   15867                 :             : 
   15868                 :             :   /* The TYPE_CONTEXT is not used for function/method types.  */
   15869                 :   124144783 :   gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
   15870                 :             : 
   15871                 :             :   /* DR 1227: Mixing immediate and non-immediate contexts in deduction
   15872                 :             :      failure.  */
   15873                 :   124144783 :   bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
   15874                 :             : 
   15875                 :   124144783 :   if (late_return_type_p)
   15876                 :             :     {
   15877                 :             :       /* Substitute the argument types.  */
   15878                 :    12739026 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   15879                 :             :                                     complain, in_decl);
   15880                 :    12739026 :       if (arg_types == error_mark_node)
   15881                 :             :         return error_mark_node;
   15882                 :             : 
   15883                 :    12739014 :       tree save_ccp = current_class_ptr;
   15884                 :    12739014 :       tree save_ccr = current_class_ref;
   15885                 :    12739014 :       tree this_type = (TREE_CODE (t) == METHOD_TYPE
   15886                 :    14008646 :                         ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
   15887                 :     1269632 :       bool do_inject = this_type && CLASS_TYPE_P (this_type);
   15888                 :     1269632 :       if (do_inject)
   15889                 :             :         {
   15890                 :             :           /* DR 1207: 'this' is in scope in the trailing return type.  */
   15891                 :     1269632 :           inject_this_parameter (this_type, cp_type_quals (this_type));
   15892                 :             :         }
   15893                 :             : 
   15894                 :             :       /* Substitute the return type.  */
   15895                 :    12739014 :       return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15896                 :             : 
   15897                 :    12736314 :       if (do_inject)
   15898                 :             :         {
   15899                 :     1269632 :           current_class_ptr = save_ccp;
   15900                 :     1269632 :           current_class_ref = save_ccr;
   15901                 :             :         }
   15902                 :             :     }
   15903                 :             :   else
   15904                 :             :     /* Substitute the return type.  */
   15905                 :   111405757 :     return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   15906                 :             : 
   15907                 :   124133938 :   if (return_type == error_mark_node)
   15908                 :             :     return error_mark_node;
   15909                 :             :   /* DR 486 clarifies that creation of a function type with an
   15910                 :             :      invalid return type is a deduction failure.  */
   15911                 :   122721315 :   if (TREE_CODE (return_type) == ARRAY_TYPE
   15912                 :   122721253 :       || TREE_CODE (return_type) == FUNCTION_TYPE)
   15913                 :             :     {
   15914                 :         206 :       if (complain & tf_error)
   15915                 :             :         {
   15916                 :          28 :           if (TREE_CODE (return_type) == ARRAY_TYPE)
   15917                 :          13 :             error ("function returning an array");
   15918                 :             :           else
   15919                 :          15 :             error ("function returning a function");
   15920                 :             :         }
   15921                 :         206 :       return error_mark_node;
   15922                 :             :     }
   15923                 :             : 
   15924                 :   122721109 :   if (!late_return_type_p)
   15925                 :             :     {
   15926                 :             :       /* Substitute the argument types.  */
   15927                 :   110819440 :       arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
   15928                 :             :                                     complain, in_decl);
   15929                 :   110819440 :       if (arg_types == error_mark_node)
   15930                 :             :         return error_mark_node;
   15931                 :             :     }
   15932                 :             : 
   15933                 :             :   /* Construct a new type node and return it.  */
   15934                 :   122716252 :   return rebuild_function_or_method_type (t, return_type, arg_types,
   15935                 :   122716252 :                                           /*raises=*/NULL_TREE, complain);
   15936                 :             : }
   15937                 :             : 
   15938                 :             : /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
   15939                 :             :    ARGS into that specification, and return the substituted
   15940                 :             :    specification.  If there is no specification, return NULL_TREE.  */
   15941                 :             : 
   15942                 :             : static tree
   15943                 :   122716216 : tsubst_exception_specification (tree fntype,
   15944                 :             :                                 tree args,
   15945                 :             :                                 tsubst_flags_t complain,
   15946                 :             :                                 tree in_decl,
   15947                 :             :                                 bool defer_ok)
   15948                 :             : {
   15949                 :   122716216 :   tree specs;
   15950                 :   122716216 :   tree new_specs;
   15951                 :             : 
   15952                 :   122716216 :   specs = TYPE_RAISES_EXCEPTIONS (fntype);
   15953                 :   122716216 :   new_specs = NULL_TREE;
   15954                 :   165961255 :   if (specs && TREE_PURPOSE (specs))
   15955                 :             :     {
   15956                 :             :       /* A noexcept-specifier.  */
   15957                 :    43212140 :       tree expr = TREE_PURPOSE (specs);
   15958                 :    43212140 :       if (TREE_CODE (expr) == INTEGER_CST)
   15959                 :    38658497 :         new_specs = expr;
   15960                 :     4553643 :       else if (defer_ok)
   15961                 :             :         {
   15962                 :             :           /* Defer instantiation of noexcept-specifiers to avoid
   15963                 :             :              excessive instantiations (c++/49107).  */
   15964                 :     4518443 :           new_specs = make_node (DEFERRED_NOEXCEPT);
   15965                 :     4518443 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   15966                 :             :             {
   15967                 :             :               /* We already partially instantiated this member template,
   15968                 :             :                  so combine the new args with the old.  */
   15969                 :          16 :               DEFERRED_NOEXCEPT_PATTERN (new_specs)
   15970                 :          16 :                 = DEFERRED_NOEXCEPT_PATTERN (expr);
   15971                 :          32 :               DEFERRED_NOEXCEPT_ARGS (new_specs)
   15972                 :          32 :                 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
   15973                 :             :             }
   15974                 :             :           else
   15975                 :             :             {
   15976                 :     4518427 :               DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
   15977                 :     4518427 :               DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
   15978                 :             :             }
   15979                 :             :         }
   15980                 :             :       else
   15981                 :             :         {
   15982                 :       35200 :           if (DEFERRED_NOEXCEPT_SPEC_P (specs))
   15983                 :             :             {
   15984                 :           0 :               args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
   15985                 :             :                                            args);
   15986                 :           0 :               expr = DEFERRED_NOEXCEPT_PATTERN (expr);
   15987                 :             :             }
   15988                 :       35200 :           new_specs = tsubst_expr (expr, args, complain, in_decl);
   15989                 :             :         }
   15990                 :    43212140 :       new_specs = build_noexcept_spec (new_specs, complain);
   15991                 :             :       /* We've instantiated a template before a noexcept-specifier
   15992                 :             :          contained therein has been parsed.  This can happen for
   15993                 :             :          a nested template class:
   15994                 :             : 
   15995                 :             :           struct S {
   15996                 :             :             template<typename> struct B { B() noexcept(...); };
   15997                 :             :             struct A : B<int> { ... use B() ... };
   15998                 :             :           };
   15999                 :             : 
   16000                 :             :          where completing B<int> will trigger instantiating the
   16001                 :             :          noexcept, even though we only parse it at the end of S.  */
   16002                 :    43212140 :       if (UNPARSED_NOEXCEPT_SPEC_P (specs))
   16003                 :             :         {
   16004                 :           9 :           gcc_checking_assert (defer_ok);
   16005                 :           9 :           vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
   16006                 :             :         }
   16007                 :             :     }
   16008                 :    79504076 :   else if (specs)
   16009                 :             :     {
   16010                 :       32899 :       if (! TREE_VALUE (specs))
   16011                 :       32832 :         new_specs = specs;
   16012                 :             :       else
   16013                 :         130 :         while (specs)
   16014                 :             :           {
   16015                 :          67 :             tree spec;
   16016                 :          67 :             int i, len = 1;
   16017                 :          67 :             tree expanded_specs = NULL_TREE;
   16018                 :             : 
   16019                 :          67 :             if (PACK_EXPANSION_P (TREE_VALUE (specs)))
   16020                 :             :               {
   16021                 :             :                 /* Expand the pack expansion type.  */
   16022                 :          13 :                 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
   16023                 :             :                                                        args, complain,
   16024                 :             :                                                        in_decl);
   16025                 :             : 
   16026                 :          13 :                 if (expanded_specs == error_mark_node)
   16027                 :           2 :                   return error_mark_node;
   16028                 :          11 :                 else if (TREE_CODE (expanded_specs) == TREE_VEC)
   16029                 :           4 :                   len = TREE_VEC_LENGTH (expanded_specs);
   16030                 :             :                 else
   16031                 :             :                   {
   16032                 :             :                     /* We're substituting into a member template, so
   16033                 :             :                        we got a TYPE_PACK_EXPANSION back.  Add that
   16034                 :             :                        expansion and move on.  */
   16035                 :           7 :                     gcc_assert (TREE_CODE (expanded_specs)
   16036                 :             :                                 == TYPE_PACK_EXPANSION);
   16037                 :           7 :                     new_specs = add_exception_specifier (new_specs,
   16038                 :             :                                                          expanded_specs,
   16039                 :             :                                                          complain);
   16040                 :           7 :                     specs = TREE_CHAIN (specs);
   16041                 :           7 :                     continue;
   16042                 :             :                   }
   16043                 :             :               }
   16044                 :             : 
   16045                 :         122 :             for (i = 0; i < len; ++i)
   16046                 :             :               {
   16047                 :          66 :                 if (expanded_specs)
   16048                 :          12 :                   spec = TREE_VEC_ELT (expanded_specs, i);
   16049                 :             :                 else
   16050                 :          54 :                   spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
   16051                 :          66 :                 if (spec == error_mark_node)
   16052                 :           2 :                   return spec;
   16053                 :          64 :                 new_specs = add_exception_specifier (new_specs, spec, 
   16054                 :             :                                                      complain);
   16055                 :             :               }
   16056                 :             : 
   16057                 :          56 :             specs = TREE_CHAIN (specs);
   16058                 :             :           }
   16059                 :             :     }
   16060                 :   122716212 :   return new_specs;
   16061                 :             : }
   16062                 :             : 
   16063                 :             : /* Substitute through a TREE_LIST of types or expressions, handling pack
   16064                 :             :    expansions.  */
   16065                 :             : 
   16066                 :             : tree
   16067                 :    28181035 : tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16068                 :             : {
   16069                 :    28181035 :   if (t == void_list_node)
   16070                 :             :     return t;
   16071                 :             : 
   16072                 :    28181015 :   tree purpose = TREE_PURPOSE (t);
   16073                 :    28181015 :   tree purposevec = NULL_TREE;
   16074                 :    28181015 :   if (!purpose)
   16075                 :             :     ;
   16076                 :           1 :   else if (PACK_EXPANSION_P (purpose))
   16077                 :             :     {
   16078                 :           0 :       purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
   16079                 :           0 :       if (TREE_CODE (purpose) == TREE_VEC)
   16080                 :           0 :         purposevec = purpose;
   16081                 :             :     }
   16082                 :           1 :   else if (TYPE_P (purpose))
   16083                 :           0 :     purpose = tsubst (purpose, args, complain, in_decl);
   16084                 :             :   else
   16085                 :           1 :     purpose = tsubst_expr (purpose, args, complain, in_decl);
   16086                 :    28181015 :   if (purpose == error_mark_node || purposevec == error_mark_node)
   16087                 :             :     return error_mark_node;
   16088                 :             : 
   16089                 :    28181015 :   tree value = TREE_VALUE (t);
   16090                 :    28181015 :   tree valuevec = NULL_TREE;
   16091                 :    28181015 :   if (!value)
   16092                 :             :     ;
   16093                 :    28181015 :   else if (PACK_EXPANSION_P (value))
   16094                 :             :     {
   16095                 :      215757 :       value = tsubst_pack_expansion (value, args, complain, in_decl);
   16096                 :      215757 :       if (TREE_CODE (value) == TREE_VEC)
   16097                 :      212166 :         valuevec = value;
   16098                 :             :     }
   16099                 :    27965258 :   else if (TYPE_P (value))
   16100                 :           4 :     value = tsubst (value, args, complain, in_decl);
   16101                 :             :   else
   16102                 :    27965254 :     value = tsubst_expr (value, args, complain, in_decl);
   16103                 :    28181015 :   if (value == error_mark_node || valuevec == error_mark_node)
   16104                 :             :     return error_mark_node;
   16105                 :             : 
   16106                 :    28180546 :   tree chain = TREE_CHAIN (t);
   16107                 :    28180546 :   if (!chain)
   16108                 :             :     ;
   16109                 :      523781 :   else if (TREE_CODE (chain) == TREE_LIST)
   16110                 :      523781 :     chain = tsubst_tree_list (chain, args, complain, in_decl);
   16111                 :           0 :   else if (TYPE_P (chain))
   16112                 :           0 :     chain = tsubst (chain, args, complain, in_decl);
   16113                 :             :   else
   16114                 :           0 :     chain = tsubst_expr (chain, args, complain, in_decl);
   16115                 :    28180546 :   if (chain == error_mark_node)
   16116                 :             :     return error_mark_node;
   16117                 :             : 
   16118                 :    28180546 :   if (purpose == TREE_PURPOSE (t)
   16119                 :    28180545 :       && value == TREE_VALUE (t)
   16120                 :    28660770 :       && chain == TREE_CHAIN (t))
   16121                 :             :     return t;
   16122                 :             : 
   16123                 :    27700526 :   int len;
   16124                 :             :   /* Determine the number of arguments.  */
   16125                 :    27700526 :   if (purposevec)
   16126                 :             :     {
   16127                 :           0 :       len = TREE_VEC_LENGTH (purposevec);
   16128                 :           0 :       gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
   16129                 :             :     }
   16130                 :    27700526 :   else if (valuevec)
   16131                 :      212166 :     len = TREE_VEC_LENGTH (valuevec);
   16132                 :             :   else
   16133                 :             :     len = 1;
   16134                 :             : 
   16135                 :    55301164 :   for (int i = len; i-- > 0; )
   16136                 :             :     {
   16137                 :    27600638 :       if (purposevec)
   16138                 :           0 :         purpose = TREE_VEC_ELT (purposevec, i);
   16139                 :    27600638 :       if (valuevec)
   16140                 :      112278 :         value = TREE_VEC_ELT (valuevec, i);
   16141                 :             : 
   16142                 :    27600638 :       if (value && TYPE_P (value))
   16143                 :           5 :         chain = hash_tree_cons (purpose, value, chain);
   16144                 :             :       else
   16145                 :    27600633 :         chain = tree_cons (purpose, value, chain);
   16146                 :             :     }
   16147                 :             : 
   16148                 :             :   return chain;
   16149                 :             : }
   16150                 :             : 
   16151                 :             : /* Take the tree structure T and replace template parameters used
   16152                 :             :    therein with the argument vector ARGS.  IN_DECL is an associated
   16153                 :             :    decl for diagnostics.  If an error occurs, returns ERROR_MARK_NODE.
   16154                 :             :    Issue error and warning messages under control of COMPLAIN.  Note
   16155                 :             :    that we must be relatively non-tolerant of extensions here, in
   16156                 :             :    order to preserve conformance; if we allow substitutions that
   16157                 :             :    should not be allowed, we may allow argument deductions that should
   16158                 :             :    not succeed, and therefore report ambiguous overload situations
   16159                 :             :    where there are none.  In theory, we could allow the substitution,
   16160                 :             :    but indicate that it should have failed, and allow our caller to
   16161                 :             :    make sure that the right thing happens, but we don't try to do this
   16162                 :             :    yet.
   16163                 :             : 
   16164                 :             :    This function is used for dealing with types, decls and the like;
   16165                 :             :    for expressions, use tsubst_expr or tsubst_copy.  */
   16166                 :             : 
   16167                 :             : tree
   16168                 :  5135420878 : tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   16169                 :             : {
   16170                 :  5135454011 :   enum tree_code code;
   16171                 :  5135454011 :   tree type, r = NULL_TREE;
   16172                 :             : 
   16173                 :  5135454011 :   if (t == NULL_TREE || t == error_mark_node
   16174                 :  5104568391 :       || t == integer_type_node
   16175                 :  5048015462 :       || t == void_type_node
   16176                 :  4979545027 :       || t == char_type_node
   16177                 :  4963366304 :       || t == unknown_type_node
   16178                 :  4963366285 :       || TREE_CODE (t) == NAMESPACE_DECL
   16179                 :  4880837611 :       || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
   16180                 :             :     return t;
   16181                 :             : 
   16182                 :  4880662878 :   tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok);
   16183                 :  4880662878 :   complain &= ~tf_tst_ok;
   16184                 :             : 
   16185                 :  4880662878 :   tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope);
   16186                 :  4880662878 :   complain &= ~tf_qualifying_scope;
   16187                 :             : 
   16188                 :  4880662878 :   if (DECL_P (t))
   16189                 :   447885333 :     return tsubst_decl (t, args, complain);
   16190                 :             : 
   16191                 :  4432777545 :   if (args == NULL_TREE)
   16192                 :             :     return t;
   16193                 :             : 
   16194                 :  4375436677 :   code = TREE_CODE (t);
   16195                 :             : 
   16196                 :  4375436677 :   gcc_assert (code != IDENTIFIER_NODE);
   16197                 :  4375436677 :   type = TREE_TYPE (t);
   16198                 :             : 
   16199                 :  4375436677 :   gcc_assert (type != unknown_type_node);
   16200                 :             : 
   16201                 :  4375436677 :   if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   16202                 :             :     return d;
   16203                 :             : 
   16204                 :             :   /* Reuse typedefs.  We need to do this to handle dependent attributes,
   16205                 :             :      such as attribute aligned.  */
   16206                 :  4375431656 :   if (TYPE_P (t)
   16207                 :  4375431656 :       && typedef_variant_p (t))
   16208                 :             :     {
   16209                 :   242401113 :       tree decl = TYPE_NAME (t);
   16210                 :             : 
   16211                 :   242401113 :       if (alias_template_specialization_p (t, nt_opaque))
   16212                 :             :         {
   16213                 :             :           /* DECL represents an alias template and we want to
   16214                 :             :              instantiate it.  */
   16215                 :    68485479 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   16216                 :    68485479 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   16217                 :    68485479 :           r = instantiate_alias_template (tmpl, gen_args, complain);
   16218                 :             :         }
   16219                 :   347831268 :       else if (DECL_CLASS_SCOPE_P (decl)
   16220                 :   138819044 :                && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
   16221                 :   308085454 :                && uses_template_parms (DECL_CONTEXT (decl)))
   16222                 :             :         {
   16223                 :   133981973 :           tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
   16224                 :   133981973 :           tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
   16225                 :   133981973 :           r = retrieve_specialization (tmpl, gen_args, 0);
   16226                 :             :         }
   16227                 :    79867322 :       else if (DECL_FUNCTION_SCOPE_P (decl)
   16228                 :     1455074 :                && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
   16229                 :    41388735 :                && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
   16230                 :     1454263 :         r = retrieve_local_specialization (decl);
   16231                 :             :       else
   16232                 :             :         /* The typedef is from a non-template context.  */
   16233                 :    38479398 :         return t;
   16234                 :             : 
   16235                 :   203921712 :       if (r)
   16236                 :             :         {
   16237                 :   203728312 :           r = TREE_TYPE (r);
   16238                 :   203728312 :           r = cp_build_qualified_type
   16239                 :   203728312 :             (r, cp_type_quals (t) | cp_type_quals (r),
   16240                 :             :              complain | tf_ignore_bad_quals);
   16241                 :   203728312 :           return r;
   16242                 :             :         }
   16243                 :             :       else
   16244                 :             :         {
   16245                 :             :           /* We don't have an instantiation yet, so drop the typedef.  */
   16246                 :      193400 :           int quals = cp_type_quals (t);
   16247                 :      193400 :           t = DECL_ORIGINAL_TYPE (decl);
   16248                 :      193400 :           t = cp_build_qualified_type (t, quals,
   16249                 :             :                                        complain | tf_ignore_bad_quals);
   16250                 :             :         }
   16251                 :             :     }
   16252                 :             : 
   16253                 :  4133223943 :   bool fndecl_type = (complain & tf_fndecl_type);
   16254                 :  4133223943 :   complain &= ~tf_fndecl_type;
   16255                 :             : 
   16256                 :  4133223943 :   if (type
   16257                 :  4133223943 :       && code != TYPENAME_TYPE
   16258                 :   724686113 :       && code != TEMPLATE_TYPE_PARM
   16259                 :   724686113 :       && code != TEMPLATE_PARM_INDEX
   16260                 :             :       && code != IDENTIFIER_NODE
   16261                 :   586526560 :       && code != FUNCTION_TYPE
   16262                 :   543291042 :       && code != METHOD_TYPE)
   16263                 :   462381837 :     type = tsubst (type, args, complain, in_decl);
   16264                 :  4133223943 :   if (type == error_mark_node)
   16265                 :             :     return error_mark_node;
   16266                 :             : 
   16267                 :  4133078032 :   switch (code)
   16268                 :             :     {
   16269                 :   447293381 :     case RECORD_TYPE:
   16270                 :   447293381 :       if (TYPE_PTRMEMFUNC_P (t))
   16271                 :       33133 :         return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
   16272                 :             :       /* Fall through.  */
   16273                 :   461303994 :     case UNION_TYPE:
   16274                 :   461303994 :     case ENUMERAL_TYPE:
   16275                 :   461303994 :       return tsubst_aggr_type_1 (t, args, complain, in_decl,
   16276                 :   461303896 :                                  /*entering_scope=*/0);
   16277                 :             : 
   16278                 :             :     case ERROR_MARK:
   16279                 :             :     case IDENTIFIER_NODE:
   16280                 :             :     case VOID_TYPE:
   16281                 :             :     case OPAQUE_TYPE:
   16282                 :             :     case REAL_TYPE:
   16283                 :             :     case COMPLEX_TYPE:
   16284                 :             :     case VECTOR_TYPE:
   16285                 :             :     case BOOLEAN_TYPE:
   16286                 :             :     case NULLPTR_TYPE:
   16287                 :             :     case LANG_TYPE:
   16288                 :             :       return t;
   16289                 :             : 
   16290                 :    77327509 :     case INTEGER_TYPE:
   16291                 :    77327509 :       if (t == integer_type_node)
   16292                 :             :         return t;
   16293                 :             : 
   16294                 :    77327495 :       if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
   16295                 :    77327495 :           && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
   16296                 :             :         return t;
   16297                 :             : 
   16298                 :      245988 :       {
   16299                 :      245988 :         tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
   16300                 :             : 
   16301                 :      245988 :         max = tsubst_expr (omax, args, complain, in_decl);
   16302                 :             : 
   16303                 :             :         /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
   16304                 :             :            needed.  */
   16305                 :      245988 :         if (TREE_CODE (max) == NOP_EXPR
   16306                 :       12382 :             && TREE_SIDE_EFFECTS (omax)
   16307                 :      245994 :             && !TREE_TYPE (max))
   16308                 :           0 :           TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
   16309                 :             : 
   16310                 :             :         /* If we're in a partial instantiation, preserve the magic NOP_EXPR
   16311                 :             :            with TREE_SIDE_EFFECTS that indicates this is not an integral
   16312                 :             :            constant expression.  */
   16313                 :      245988 :         if (processing_template_decl
   16314                 :      245988 :             && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
   16315                 :             :           {
   16316                 :           0 :             gcc_assert (TREE_CODE (max) == NOP_EXPR);
   16317                 :           0 :             TREE_SIDE_EFFECTS (max) = 1;
   16318                 :             :           }
   16319                 :             : 
   16320                 :      245988 :         return compute_array_index_type (NULL_TREE, max, complain);
   16321                 :             :       }
   16322                 :             : 
   16323                 :  2278973673 :     case TEMPLATE_TYPE_PARM:
   16324                 :  2278973673 :       if (TEMPLATE_TYPE_LEVEL (t) == 0)
   16325                 :             :         {
   16326                 :             :           /* This is either an ordinary level-less auto or a CTAD placeholder
   16327                 :             :              auto.  These get replaced only via do_auto_deduction which, in the
   16328                 :             :              ordinary case, temporarily overrides its level to 1 before calling
   16329                 :             :              tsubst.  CTAD placeholders are replaced via do_class_deduction.  */
   16330                 :       10305 :           gcc_checking_assert (is_auto (t));
   16331                 :       10305 :           tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t);
   16332                 :       10305 :           if (!tmpl)
   16333                 :             :             /* Ordinary level-less auto has nothing to substitute.  */
   16334                 :             :             return t;
   16335                 :             : 
   16336                 :             :           /* Substitute the template of this CTAD placeholder.  */
   16337                 :       10237 :           tmpl = tsubst_expr (tmpl, args, complain, in_decl);
   16338                 :       10237 :           if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   16339                 :        3239 :             tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl);
   16340                 :             : 
   16341                 :       10237 :           if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t))
   16342                 :        3366 :             return make_template_placeholder (tmpl);
   16343                 :             :           else
   16344                 :             :             return t;
   16345                 :             :         }
   16346                 :             :       /* Fall through.  */
   16347                 :  2419220157 :     case TEMPLATE_TEMPLATE_PARM:
   16348                 :  2419220157 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   16349                 :  2419220157 :     case TEMPLATE_PARM_INDEX:
   16350                 :  2419220157 :       {
   16351                 :  2419220157 :         int idx;
   16352                 :  2419220157 :         int level;
   16353                 :  2419220157 :         int levels;
   16354                 :  2419220157 :         tree arg = NULL_TREE;
   16355                 :             : 
   16356                 :  2419220157 :         r = NULL_TREE;
   16357                 :             : 
   16358                 :  2419220157 :         gcc_assert (TREE_VEC_LENGTH (args) > 0);
   16359                 :  2419220157 :         template_parm_level_and_index (t, &level, &idx);
   16360                 :             : 
   16361                 :  2419220157 :         levels = TMPL_ARGS_DEPTH (args);
   16362                 :  2419220157 :         if (level <= levels
   16363                 :  6947025701 :             && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
   16364                 :             :           {
   16365                 :  2263902735 :             arg = TMPL_ARG (args, level, idx);
   16366                 :             : 
   16367                 :             :             /* See through ARGUMENT_PACK_SELECT arguments. */
   16368                 :  2263902735 :             if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   16369                 :     9690777 :               arg = argument_pack_select_arg (arg);
   16370                 :             :           }
   16371                 :             : 
   16372                 :  2419220157 :         if (arg == error_mark_node)
   16373                 :             :           return error_mark_node;
   16374                 :  2419220113 :         else if (arg != NULL_TREE)
   16375                 :             :           {
   16376                 :  2253661009 :             if (ARGUMENT_PACK_P (arg))
   16377                 :             :               /* If ARG is an argument pack, we don't actually want to
   16378                 :             :                  perform a substitution here, because substitutions
   16379                 :             :                  for argument packs are only done
   16380                 :             :                  element-by-element. We can get to this point when
   16381                 :             :                  substituting the type of a non-type template
   16382                 :             :                  parameter pack, when that type actually contains
   16383                 :             :                  template parameter packs from an outer template, e.g.,
   16384                 :             : 
   16385                 :             :                  template<typename... Types> struct A {
   16386                 :             :                    template<Types... Values> struct B { };
   16387                 :             :                  };  */
   16388                 :             :               return t;
   16389                 :             : 
   16390                 :  2253660657 :             if (code == TEMPLATE_TYPE_PARM)
   16391                 :             :               {
   16392                 :  2122934488 :                 int quals;
   16393                 :             : 
   16394                 :             :                 /* When building concept checks for the purpose of
   16395                 :             :                    deducing placeholders, we can end up with wildcards
   16396                 :             :                    where types are expected. Adjust this to the deduced
   16397                 :             :                    value.  */
   16398                 :  2122934488 :                 if (TREE_CODE (arg) == WILDCARD_DECL)
   16399                 :          10 :                   arg = TREE_TYPE (TREE_TYPE (arg));
   16400                 :             : 
   16401                 :  2122934488 :                 gcc_assert (TYPE_P (arg));
   16402                 :             : 
   16403                 :  2122934488 :                 quals = cp_type_quals (arg) | cp_type_quals (t);
   16404                 :             : 
   16405                 :  2122934488 :                 return cp_build_qualified_type
   16406                 :  2122934488 :                   (arg, quals, complain | tf_ignore_bad_quals);
   16407                 :             :               }
   16408                 :   130726169 :             else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
   16409                 :             :               {
   16410                 :             :                 /* We are processing a type constructed from a
   16411                 :             :                    template template parameter.  */
   16412                 :      621866 :                 tree argvec = tsubst (TYPE_TI_ARGS (t),
   16413                 :             :                                       args, complain, in_decl);
   16414                 :      310933 :                 if (argvec == error_mark_node)
   16415                 :             :                   return error_mark_node;
   16416                 :             : 
   16417                 :      310933 :                 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
   16418                 :             :                             || TREE_CODE (arg) == TEMPLATE_DECL
   16419                 :             :                             || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
   16420                 :             : 
   16421                 :      310933 :                 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
   16422                 :             :                   /* Consider this code:
   16423                 :             : 
   16424                 :             :                         template <template <class> class Template>
   16425                 :             :                         struct Internal {
   16426                 :             :                         template <class Arg> using Bind = Template<Arg>;
   16427                 :             :                         };
   16428                 :             : 
   16429                 :             :                         template <template <class> class Template, class Arg>
   16430                 :             :                         using Instantiate = Template<Arg>; //#0
   16431                 :             : 
   16432                 :             :                         template <template <class> class Template,
   16433                 :             :                                   class Argument>
   16434                 :             :                         using Bind =
   16435                 :             :                           Instantiate<Internal<Template>::template Bind,
   16436                 :             :                                       Argument>; //#1
   16437                 :             : 
   16438                 :             :                      When #1 is parsed, the
   16439                 :             :                      BOUND_TEMPLATE_TEMPLATE_PARM representing the
   16440                 :             :                      parameter `Template' in #0 matches the
   16441                 :             :                      UNBOUND_CLASS_TEMPLATE representing the argument
   16442                 :             :                      `Internal<Template>::template Bind'; We then want
   16443                 :             :                      to assemble the type `Bind<Argument>' that can't
   16444                 :             :                      be fully created right now, because
   16445                 :             :                      `Internal<Template>' not being complete, the Bind
   16446                 :             :                      template cannot be looked up in that context.  So
   16447                 :             :                      we need to "store" `Bind<Argument>' for later
   16448                 :             :                      when the context of Bind becomes complete.  Let's
   16449                 :             :                      store that in a TYPENAME_TYPE.  */
   16450                 :           6 :                   return make_typename_type (TYPE_CONTEXT (arg),
   16451                 :             :                                              build_nt (TEMPLATE_ID_EXPR,
   16452                 :           6 :                                                        TYPE_IDENTIFIER (arg),
   16453                 :             :                                                        argvec),
   16454                 :             :                                              typename_type,
   16455                 :           6 :                                              complain);
   16456                 :             : 
   16457                 :             :                 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
   16458                 :             :                    are resolving nested-types in the signature of a
   16459                 :             :                    member function templates.  Otherwise ARG is a
   16460                 :             :                    TEMPLATE_DECL and is the real template to be
   16461                 :             :                    instantiated.  */
   16462                 :      310927 :                 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   16463                 :         103 :                   arg = TYPE_NAME (arg);
   16464                 :             : 
   16465                 :      310927 :                 r = lookup_template_class (arg,
   16466                 :             :                                            argvec, in_decl,
   16467                 :      310927 :                                            DECL_CONTEXT (arg),
   16468                 :             :                                             /*entering_scope=*/0,
   16469                 :             :                                            complain);
   16470                 :      310927 :                 return cp_build_qualified_type
   16471                 :      310927 :                   (r, cp_type_quals (t) | cp_type_quals (r), complain);
   16472                 :             :               }
   16473                 :   130415236 :             else if (code == TEMPLATE_TEMPLATE_PARM)
   16474                 :             :               return arg;
   16475                 :             :             else
   16476                 :             :               /* TEMPLATE_PARM_INDEX.  */
   16477                 :   128674456 :               return convert_from_reference (unshare_expr (arg));
   16478                 :             :           }
   16479                 :             : 
   16480                 :   165559104 :         if (level == 1)
   16481                 :             :           /* This can happen during the attempted tsubst'ing in
   16482                 :             :              unify.  This means that we don't yet have any information
   16483                 :             :              about the template parameter in question.  */
   16484                 :             :           return t;
   16485                 :             : 
   16486                 :             :         /* Early in template argument deduction substitution, we don't
   16487                 :             :            want to reduce the level of 'auto', or it will be confused
   16488                 :             :            with a normal template parm in subsequent deduction.
   16489                 :             :            Similarly, don't reduce the level of template parameters to
   16490                 :             :            avoid mismatches when deducing their types.  */
   16491                 :   155317389 :         if (complain & tf_partial)
   16492                 :             :           return t;
   16493                 :             : 
   16494                 :             :         /* If we get here, we must have been looking at a parm for a
   16495                 :             :            more deeply nested template.  Make a new version of this
   16496                 :             :            template parameter, but with a lower level.  */
   16497                 :   155176217 :         int quals;
   16498                 :   155176217 :         switch (code)
   16499                 :             :           {
   16500                 :   146503213 :           case TEMPLATE_TYPE_PARM:
   16501                 :   146503213 :           case TEMPLATE_TEMPLATE_PARM:
   16502                 :   146503213 :             quals = cp_type_quals (t);
   16503                 :   146503213 :             if (quals)
   16504                 :             :               {
   16505                 :    10741888 :                 gcc_checking_assert (code == TEMPLATE_TYPE_PARM);
   16506                 :    10741888 :                 t = TYPE_MAIN_VARIANT (t);
   16507                 :             :               }
   16508                 :             : 
   16509                 :   146503213 :             if (tree d = TEMPLATE_TYPE_DESCENDANTS (t))
   16510                 :   135009270 :               if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels
   16511                 :   135009270 :                   && (code == TEMPLATE_TYPE_PARM
   16512                 :       35632 :                       || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t)))
   16513                 :             :                 /* Cache lowering a type parameter or a simple template
   16514                 :             :                    template parameter.  */
   16515                 :   134986401 :                 r = TREE_TYPE (d);
   16516                 :             : 
   16517                 :   134986401 :             if (!r)
   16518                 :             :               {
   16519                 :    11516812 :                 r = copy_type (t);
   16520                 :    11516812 :                 TEMPLATE_TYPE_PARM_INDEX (r)
   16521                 :    11516812 :                   = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
   16522                 :             :                                                 r, levels, args, complain);
   16523                 :    11516812 :                 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
   16524                 :    11516812 :                 TYPE_MAIN_VARIANT (r) = r;
   16525                 :    11516812 :                 TYPE_POINTER_TO (r) = NULL_TREE;
   16526                 :    11516812 :                 TYPE_REFERENCE_TO (r) = NULL_TREE;
   16527                 :             : 
   16528                 :    11516812 :                 if (code == TEMPLATE_TYPE_PARM)
   16529                 :    11515119 :                   if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
   16530                 :             :                     /* Propagate constraints on placeholders since they are
   16531                 :             :                        only instantiated during satisfaction.  */
   16532                 :      101719 :                     PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
   16533                 :             : 
   16534                 :    11516812 :                 if (TYPE_STRUCTURAL_EQUALITY_P (t))
   16535                 :           0 :                   SET_TYPE_STRUCTURAL_EQUALITY (r);
   16536                 :             :                 else
   16537                 :    11516812 :                   TYPE_CANONICAL (r) = canonical_type_parameter (r);
   16538                 :             :               }
   16539                 :             : 
   16540                 :   146503213 :             if (quals)
   16541                 :    10741888 :               r = cp_build_qualified_type (r, quals,
   16542                 :             :                                            complain | tf_ignore_bad_quals);
   16543                 :             :             break;
   16544                 :             : 
   16545                 :        8341 :           case BOUND_TEMPLATE_TEMPLATE_PARM:
   16546                 :        8341 :             {
   16547                 :        8341 :               tree tinfo = TYPE_TEMPLATE_INFO (t);
   16548                 :             :               /* We might need to substitute into the types of non-type
   16549                 :             :                  template parameters.  This also lowers the level of
   16550                 :             :                  the ttp appropriately.  */
   16551                 :        8341 :               tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
   16552                 :             :                                   complain, in_decl);
   16553                 :        8341 :               if (tmpl == error_mark_node)
   16554                 :             :                 return error_mark_node;
   16555                 :        8341 :               tree argvec = tsubst (TI_ARGS (tinfo), args,
   16556                 :             :                                     complain, in_decl);
   16557                 :        8341 :               if (argvec == error_mark_node)
   16558                 :             :                 return error_mark_node;
   16559                 :        8341 :               r = lookup_template_class (tmpl, argvec, in_decl, NULL_TREE,
   16560                 :             :                                          /*entering_scope=*/false, complain);
   16561                 :        8341 :               r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   16562                 :        8341 :               break;
   16563                 :             :             }
   16564                 :             : 
   16565                 :     8664663 :           case TEMPLATE_PARM_INDEX:
   16566                 :             :             /* OK, now substitute the type of the non-type parameter.  We
   16567                 :             :                couldn't do it earlier because it might be an auto parameter,
   16568                 :             :                and we wouldn't need to if we had an argument.  */
   16569                 :     8664663 :             type = tsubst (type, args, complain, in_decl);
   16570                 :     8664663 :             if (type == error_mark_node)
   16571                 :             :               return error_mark_node;
   16572                 :     8664656 :             r = reduce_template_parm_level (t, type, levels, args, complain);
   16573                 :     8664656 :             break;
   16574                 :             : 
   16575                 :           0 :           default:
   16576                 :           0 :             gcc_unreachable ();
   16577                 :             :           }
   16578                 :             : 
   16579                 :             :         return r;
   16580                 :             :       }
   16581                 :             : 
   16582                 :           5 :     case TREE_LIST:
   16583                 :           5 :       return tsubst_tree_list (t, args, complain, in_decl);
   16584                 :             : 
   16585                 :           0 :     case TREE_BINFO:
   16586                 :             :       /* We should never be tsubsting a binfo.  */
   16587                 :           0 :       gcc_unreachable ();
   16588                 :             : 
   16589                 :   285403787 :     case TREE_VEC:
   16590                 :             :       /* A vector of template arguments.  */
   16591                 :   285403787 :       gcc_assert (!type);
   16592                 :   285403787 :       return tsubst_template_args (t, args, complain, in_decl);
   16593                 :             : 
   16594                 :   438004008 :     case POINTER_TYPE:
   16595                 :   438004008 :     case REFERENCE_TYPE:
   16596                 :   438004008 :       {
   16597                 :   438004008 :         if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
   16598                 :             :           return t;
   16599                 :             : 
   16600                 :             :         /* [temp.deduct]
   16601                 :             : 
   16602                 :             :            Type deduction may fail for any of the following
   16603                 :             :            reasons:
   16604                 :             : 
   16605                 :             :            -- Attempting to create a pointer to reference type.
   16606                 :             :            -- Attempting to create a reference to a reference type or
   16607                 :             :               a reference to void.
   16608                 :             : 
   16609                 :             :           Core issue 106 says that creating a reference to a reference
   16610                 :             :           during instantiation is no longer a cause for failure. We
   16611                 :             :           only enforce this check in strict C++98 mode.  */
   16612                 :   408469515 :         if ((TYPE_REF_P (type)
   16613                 :    11185707 :              && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
   16614                 :   408469394 :             || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
   16615                 :             :           {
   16616                 :         609 :             static location_t last_loc;
   16617                 :             : 
   16618                 :             :             /* We keep track of the last time we issued this error
   16619                 :             :                message to avoid spewing a ton of messages during a
   16620                 :             :                single bad template instantiation.  */
   16621                 :         609 :             if (complain & tf_error
   16622                 :          31 :                 && last_loc != input_location)
   16623                 :             :               {
   16624                 :          31 :                 if (VOID_TYPE_P (type))
   16625                 :          10 :                   error ("forming reference to void");
   16626                 :          21 :                else if (code == POINTER_TYPE)
   16627                 :          20 :                  error ("forming pointer to reference type %qT", type);
   16628                 :             :                else
   16629                 :           1 :                   error ("forming reference to reference type %qT", type);
   16630                 :          31 :                 last_loc = input_location;
   16631                 :             :               }
   16632                 :             : 
   16633                 :         609 :             return error_mark_node;
   16634                 :             :           }
   16635                 :   408468906 :         else if (TREE_CODE (type) == FUNCTION_TYPE
   16636                 :   408468906 :                  && (type_memfn_quals (type) != TYPE_UNQUALIFIED
   16637                 :     2561294 :                      || type_memfn_rqual (type) != REF_QUAL_NONE))
   16638                 :             :           {
   16639                 :         108 :             if (complain & tf_error)
   16640                 :             :               {
   16641                 :          14 :                 if (code == POINTER_TYPE)
   16642                 :           8 :                   error ("forming pointer to qualified function type %qT",
   16643                 :             :                          type);
   16644                 :             :                 else
   16645                 :           6 :                   error ("forming reference to qualified function type %qT",
   16646                 :             :                          type);
   16647                 :             :               }
   16648                 :         108 :             return error_mark_node;
   16649                 :             :           }
   16650                 :   408468798 :         else if (code == POINTER_TYPE)
   16651                 :             :           {
   16652                 :   211077351 :             r = build_pointer_type (type);
   16653                 :   211077351 :             if (TREE_CODE (type) == METHOD_TYPE)
   16654                 :       33125 :               r = build_ptrmemfunc_type (r);
   16655                 :             :           }
   16656                 :   197391447 :         else if (TYPE_REF_P (type))
   16657                 :             :           /* In C++0x, during template argument substitution, when there is an
   16658                 :             :              attempt to create a reference to a reference type, reference
   16659                 :             :              collapsing is applied as described in [14.3.1/4 temp.arg.type]:
   16660                 :             : 
   16661                 :             :              "If a template-argument for a template-parameter T names a type
   16662                 :             :              that is a reference to a type A, an attempt to create the type
   16663                 :             :              'lvalue reference to cv T' creates the type 'lvalue reference to
   16664                 :             :              A,' while an attempt to create the type type rvalue reference to
   16665                 :             :              cv T' creates the type T"
   16666                 :             :           */
   16667                 :    11185586 :           r = cp_build_reference_type
   16668                 :    11185586 :               (TREE_TYPE (type),
   16669                 :    11185586 :                TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
   16670                 :             :         else
   16671                 :   186205861 :           r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
   16672                 :   408468798 :         r = cp_build_qualified_type (r, cp_type_quals (t), complain);
   16673                 :             : 
   16674                 :   408468798 :         if (r != error_mark_node)
   16675                 :             :           /* Will this ever be needed for TYPE_..._TO values?  */
   16676                 :   408468798 :           layout_type (r);
   16677                 :             : 
   16678                 :             :         return r;
   16679                 :             :       }
   16680                 :      198328 :     case OFFSET_TYPE:
   16681                 :      198328 :       {
   16682                 :      198328 :         r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
   16683                 :      198328 :         if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
   16684                 :             :           {
   16685                 :             :             /* [temp.deduct]
   16686                 :             : 
   16687                 :             :                Type deduction may fail for any of the following
   16688                 :             :                reasons:
   16689                 :             : 
   16690                 :             :                -- Attempting to create "pointer to member of T" when T
   16691                 :             :                   is not a class type.  */
   16692                 :           0 :             if (complain & tf_error)
   16693                 :           0 :               error ("creating pointer to member of non-class type %qT", r);
   16694                 :           0 :             return error_mark_node;
   16695                 :             :           }
   16696                 :      198328 :         if (TYPE_REF_P (type))
   16697                 :             :           {
   16698                 :           8 :             if (complain & tf_error)
   16699                 :           4 :               error ("creating pointer to member reference type %qT", type);
   16700                 :           8 :             return error_mark_node;
   16701                 :             :           }
   16702                 :      198320 :         if (VOID_TYPE_P (type))
   16703                 :             :           {
   16704                 :           4 :             if (complain & tf_error)
   16705                 :           4 :               error ("creating pointer to member of type void");
   16706                 :           4 :             return error_mark_node;
   16707                 :             :           }
   16708                 :      198316 :         gcc_assert (TREE_CODE (type) != METHOD_TYPE);
   16709                 :      198316 :         if (TREE_CODE (type) == FUNCTION_TYPE)
   16710                 :             :           {
   16711                 :             :             /* The type of the implicit object parameter gets its
   16712                 :             :                cv-qualifiers from the FUNCTION_TYPE. */
   16713                 :      149509 :             tree memptr;
   16714                 :      149509 :             tree method_type
   16715                 :      149509 :               = build_memfn_type (type, r, type_memfn_quals (type),
   16716                 :             :                                   type_memfn_rqual (type));
   16717                 :      149509 :             memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
   16718                 :      149509 :             return cp_build_qualified_type (memptr, cp_type_quals (t),
   16719                 :      149509 :                                             complain);
   16720                 :             :           }
   16721                 :             :         else
   16722                 :       48807 :           return cp_build_qualified_type (build_ptrmem_type (r, type),
   16723                 :             :                                           cp_type_quals (t),
   16724                 :       48807 :                                           complain);
   16725                 :             :       }
   16726                 :   124144723 :     case FUNCTION_TYPE:
   16727                 :   124144723 :     case METHOD_TYPE:
   16728                 :   124144723 :       {
   16729                 :   124144723 :         tree fntype;
   16730                 :   124144723 :         tree specs;
   16731                 :   124144723 :         fntype = tsubst_function_type (t, args, complain, in_decl);
   16732                 :   124133890 :         if (fntype == error_mark_node)
   16733                 :             :           return error_mark_node;
   16734                 :             : 
   16735                 :             :         /* Substitute the exception specification.  */
   16736                 :   122716192 :         specs = tsubst_exception_specification (t, args, complain, in_decl,
   16737                 :             :                                                 /*defer_ok*/fndecl_type);
   16738                 :   122716192 :         if (specs == error_mark_node)
   16739                 :             :           return error_mark_node;
   16740                 :   122716180 :         if (specs)
   16741                 :    43244999 :           fntype = build_exception_variant (fntype, specs);
   16742                 :             :         return fntype;
   16743                 :             :       }
   16744                 :     6400859 :     case ARRAY_TYPE:
   16745                 :     6400859 :       {
   16746                 :     6400859 :         tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
   16747                 :     6400859 :         if (domain == error_mark_node)
   16748                 :             :           return error_mark_node;
   16749                 :             : 
   16750                 :             :         /* As an optimization, we avoid regenerating the array type if
   16751                 :             :            it will obviously be the same as T.  */
   16752                 :    12401662 :         if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
   16753                 :             :           return t;
   16754                 :             : 
   16755                 :             :         /* These checks should match the ones in create_array_type_for_decl.
   16756                 :             : 
   16757                 :             :            [temp.deduct]
   16758                 :             : 
   16759                 :             :            The deduction may fail for any of the following reasons:
   16760                 :             : 
   16761                 :             :            -- Attempting to create an array with an element type that
   16762                 :             :               is void, a function type, or a reference type, or [DR337]
   16763                 :             :               an abstract class type.  */
   16764                 :      475762 :         if (VOID_TYPE_P (type)
   16765                 :      475762 :             || TREE_CODE (type) == FUNCTION_TYPE
   16766                 :      475752 :             || (TREE_CODE (type) == ARRAY_TYPE
   16767                 :        1542 :                 && TYPE_DOMAIN (type) == NULL_TREE)
   16768                 :      951504 :             || TYPE_REF_P (type))
   16769                 :             :           {
   16770                 :          20 :             if (complain & tf_error)
   16771                 :           4 :               error ("creating array of %qT", type);
   16772                 :          20 :             return error_mark_node;
   16773                 :             :           }
   16774                 :             : 
   16775                 :      475742 :         if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
   16776                 :      475742 :                                   !(complain & tf_error)))
   16777                 :           0 :           return error_mark_node;
   16778                 :             : 
   16779                 :      475742 :         r = build_cplus_array_type (type, domain);
   16780                 :             : 
   16781                 :      475742 :         if (!valid_array_size_p (input_location, r, in_decl,
   16782                 :             :                                  (complain & tf_error)))
   16783                 :          20 :           return error_mark_node;
   16784                 :             : 
   16785                 :      475722 :         if (TYPE_USER_ALIGN (t))
   16786                 :             :           {
   16787                 :           8 :             SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
   16788                 :           8 :             TYPE_USER_ALIGN (r) = 1;
   16789                 :             :           }
   16790                 :             : 
   16791                 :             :         return r;
   16792                 :             :       }
   16793                 :             : 
   16794                 :   128943806 :     case TYPENAME_TYPE:
   16795                 :   128943806 :       {
   16796                 :   128943806 :         tree ctx = TYPE_CONTEXT (t);
   16797                 :   128943806 :         if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
   16798                 :             :           {
   16799                 :           6 :             ctx = tsubst_pack_expansion (ctx, args,
   16800                 :             :                                          complain | tf_qualifying_scope,
   16801                 :             :                                          in_decl);
   16802                 :           6 :             if (ctx == error_mark_node
   16803                 :          12 :                 || TREE_VEC_LENGTH (ctx) > 1)
   16804                 :             :               return error_mark_node;
   16805                 :           6 :             if (TREE_VEC_LENGTH (ctx) == 0)
   16806                 :             :               {
   16807                 :           3 :                 if (complain & tf_error)
   16808                 :           6 :                   error ("%qD is instantiated for an empty pack",
   16809                 :           3 :                          TYPENAME_TYPE_FULLNAME (t));
   16810                 :           3 :                 return error_mark_node;
   16811                 :             :               }
   16812                 :           3 :             ctx = TREE_VEC_ELT (ctx, 0);
   16813                 :             :           }
   16814                 :             :         else
   16815                 :   128943800 :           ctx = tsubst_aggr_type (ctx, args,
   16816                 :             :                                   complain | tf_qualifying_scope,
   16817                 :             :                                   in_decl, /*entering_scope=*/1);
   16818                 :   128943803 :         if (ctx == error_mark_node)
   16819                 :             :           return error_mark_node;
   16820                 :             : 
   16821                 :   128526545 :         tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args,
   16822                 :             :                               complain, in_decl);
   16823                 :   128526545 :         if (f == error_mark_node)
   16824                 :             :           return error_mark_node;
   16825                 :             : 
   16826                 :   128526511 :         if (!MAYBE_CLASS_TYPE_P (ctx))
   16827                 :             :           {
   16828                 :       10616 :             if (complain & tf_error)
   16829                 :          67 :               error ("%qT is not a class, struct, or union type", ctx);
   16830                 :       10616 :             return error_mark_node;
   16831                 :             :           }
   16832                 :   128515895 :         else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
   16833                 :             :           {
   16834                 :             :             /* Normally, make_typename_type does not require that the CTX
   16835                 :             :                have complete type in order to allow things like:
   16836                 :             : 
   16837                 :             :                  template <class T> struct S { typename S<T>::X Y; };
   16838                 :             : 
   16839                 :             :                But, such constructs have already been resolved by this
   16840                 :             :                point, so here CTX really should have complete type, unless
   16841                 :             :                it's a partial instantiation.  */
   16842                 :   107069892 :             if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
   16843                 :        2159 :               return error_mark_node;
   16844                 :             :           }
   16845                 :             : 
   16846                 :             :         /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union'
   16847                 :             :            tags.  TYPENAME_TYPE should probably remember the exact tag that
   16848                 :             :            was written.  */
   16849                 :   128513736 :         enum tag_types tag_type
   16850                 :   128513736 :           = TYPENAME_IS_CLASS_P (t) ? class_type
   16851                 :   128513644 :           : TYPENAME_IS_ENUM_P (t) ? enum_type
   16852                 :   128513736 :           : typename_type;
   16853                 :   128513736 :         tsubst_flags_t tcomplain = complain | tf_keep_type_decl;
   16854                 :   128513736 :         tcomplain |= tst_ok_flag | qualifying_scope_flag;
   16855                 :   128513736 :         f = make_typename_type (ctx, f, tag_type, tcomplain);
   16856                 :   128513736 :         if (f == error_mark_node)
   16857                 :             :           return f;
   16858                 :   113063061 :         if (TREE_CODE (f) == TYPE_DECL)
   16859                 :             :           {
   16860                 :    90996748 :             complain |= tf_ignore_bad_quals;
   16861                 :    90996748 :             f = TREE_TYPE (f);
   16862                 :             :           }
   16863                 :             : 
   16864                 :   113063061 :         if (TREE_CODE (f) != TYPENAME_TYPE)
   16865                 :             :           {
   16866                 :    92718095 :             if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
   16867                 :             :               {
   16868                 :           4 :                 if (complain & tf_error)
   16869                 :           4 :                   error ("%qT resolves to %qT, which is not an enumeration type",
   16870                 :             :                          t, f);
   16871                 :             :                 else
   16872                 :             :                   return error_mark_node;
   16873                 :             :               }
   16874                 :    92718091 :             else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
   16875                 :             :               {
   16876                 :           8 :                 if (complain & tf_error)
   16877                 :           4 :                   error ("%qT resolves to %qT, which is not a class type",
   16878                 :             :                          t, f);
   16879                 :             :                 else
   16880                 :             :                   return error_mark_node;
   16881                 :             :               }
   16882                 :             :           }
   16883                 :             : 
   16884                 :   113063057 :         return cp_build_qualified_type
   16885                 :   113063057 :           (f, cp_type_quals (f) | cp_type_quals (t), complain);
   16886                 :             :       }
   16887                 :             : 
   16888                 :       27273 :     case UNBOUND_CLASS_TEMPLATE:
   16889                 :       27273 :       {
   16890                 :       27273 :         tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
   16891                 :             :                                      in_decl, /*entering_scope=*/1);
   16892                 :       27273 :         tree name = TYPE_IDENTIFIER (t);
   16893                 :       27273 :         tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
   16894                 :             : 
   16895                 :       27273 :         if (ctx == error_mark_node || name == error_mark_node)
   16896                 :             :           return error_mark_node;
   16897                 :             : 
   16898                 :       27273 :         if (parm_list)
   16899                 :          12 :           parm_list = tsubst_template_parms (parm_list, args, complain);
   16900                 :       27273 :         return make_unbound_class_template (ctx, name, parm_list, complain);
   16901                 :             :       }
   16902                 :             : 
   16903                 :         241 :     case TYPEOF_TYPE:
   16904                 :         241 :       {
   16905                 :         241 :         tree type;
   16906                 :             : 
   16907                 :         241 :         ++cp_unevaluated_operand;
   16908                 :         241 :         ++c_inhibit_evaluation_warnings;
   16909                 :             : 
   16910                 :         241 :         type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl);
   16911                 :             : 
   16912                 :         241 :         --cp_unevaluated_operand;
   16913                 :         241 :         --c_inhibit_evaluation_warnings;
   16914                 :             : 
   16915                 :         241 :         type = finish_typeof (type);
   16916                 :         241 :         return cp_build_qualified_type (type,
   16917                 :         241 :                                         cp_type_quals (t)
   16918                 :         241 :                                         | cp_type_quals (type),
   16919                 :         241 :                                         complain);
   16920                 :             :       }
   16921                 :             : 
   16922                 :    12820291 :     case DECLTYPE_TYPE:
   16923                 :    12820291 :       {
   16924                 :    12820291 :         tree type;
   16925                 :             : 
   16926                 :    12820291 :         ++cp_unevaluated_operand;
   16927                 :    12820291 :         ++c_inhibit_evaluation_warnings;
   16928                 :             : 
   16929                 :    12820291 :         type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
   16930                 :             :                             complain|tf_decltype, in_decl);
   16931                 :             : 
   16932                 :    12809491 :         --cp_unevaluated_operand;
   16933                 :    12809491 :         --c_inhibit_evaluation_warnings;
   16934                 :             : 
   16935                 :    12809491 :         if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
   16936                 :      233856 :           type = lambda_capture_field_type (type,
   16937                 :             :                                             false /*explicit_init*/,
   16938                 :      116928 :                                             DECLTYPE_FOR_REF_CAPTURE (t));
   16939                 :    12692563 :         else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
   16940                 :          92 :           type = lambda_proxy_type (type);
   16941                 :             :         else
   16942                 :             :           {
   16943                 :    12692471 :             bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
   16944                 :      675273 :             if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
   16945                 :    12692471 :                 && EXPR_P (type))
   16946                 :             :               /* In a template ~id could be either a complement expression
   16947                 :             :                  or an unqualified-id naming a destructor; if instantiating
   16948                 :             :                  it produces an expression, it's not an id-expression or
   16949                 :             :                  member access.  */
   16950                 :             :               id = false;
   16951                 :    12692471 :             type = finish_decltype_type (type, id, complain);
   16952                 :             :           }
   16953                 :    12809491 :         return cp_build_qualified_type (type,
   16954                 :    12809491 :                                         cp_type_quals (t)
   16955                 :    12809491 :                                         | cp_type_quals (type),
   16956                 :    12809491 :                                         complain | tf_ignore_bad_quals);
   16957                 :             :       }
   16958                 :             : 
   16959                 :     1953703 :     case TRAIT_TYPE:
   16960                 :     1953703 :       {
   16961                 :     1953703 :         tree type1 = TRAIT_TYPE_TYPE1 (t);
   16962                 :     1953703 :         if (TYPE_P (type1))
   16963                 :     1873730 :           type1 = tsubst (type1, args, complain, in_decl);
   16964                 :             :         else
   16965                 :       79973 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   16966                 :     1953703 :         tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl);
   16967                 :     1953703 :         type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain);
   16968                 :     1953703 :         return cp_build_qualified_type (type,
   16969                 :     1953703 :                                         cp_type_quals (t) | cp_type_quals (type),
   16970                 :     1953703 :                                         complain | tf_ignore_bad_quals);
   16971                 :             :       }
   16972                 :             : 
   16973                 :           0 :     case TYPE_ARGUMENT_PACK:
   16974                 :           0 :     case NONTYPE_ARGUMENT_PACK:
   16975                 :           0 :       return tsubst_argument_pack (t, args, complain, in_decl);
   16976                 :             : 
   16977                 :           0 :     case VOID_CST:
   16978                 :           0 :     case INTEGER_CST:
   16979                 :           0 :     case REAL_CST:
   16980                 :           0 :     case STRING_CST:
   16981                 :           0 :     case PLUS_EXPR:
   16982                 :           0 :     case MINUS_EXPR:
   16983                 :           0 :     case NEGATE_EXPR:
   16984                 :           0 :     case NOP_EXPR:
   16985                 :           0 :     case INDIRECT_REF:
   16986                 :           0 :     case ADDR_EXPR:
   16987                 :           0 :     case CALL_EXPR:
   16988                 :           0 :     case ARRAY_REF:
   16989                 :           0 :     case SCOPE_REF:
   16990                 :           0 :     case OMP_ARRAY_SECTION:
   16991                 :             :       /* We should use one of the expression tsubsts for these codes.  */
   16992                 :           0 :       gcc_unreachable ();
   16993                 :             : 
   16994                 :           0 :     default:
   16995                 :           0 :       sorry ("use of %qs in template", get_tree_code_name (code));
   16996                 :           0 :       return error_mark_node;
   16997                 :             :     }
   16998                 :             : }
   16999                 :             : 
   17000                 :             : /* Convenience wrapper over tsubst for substituting into the LHS
   17001                 :             :    of the :: scope resolution operator.  */
   17002                 :             : 
   17003                 :             : static tree
   17004                 :    63825736 : tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17005                 :             : {
   17006                 :    63825736 :   gcc_checking_assert (TYPE_P (t));
   17007                 :    63825736 :   return tsubst (t, args, complain | tf_qualifying_scope, in_decl);
   17008                 :             : }
   17009                 :             : 
   17010                 :             : /* Convenience wrapper over tsubst for substituting into an id-expression
   17011                 :             :    without resolving its terminal name.  */
   17012                 :             : 
   17013                 :             : static tree
   17014                 :   209695264 : tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   17015                 :             : {
   17016                 :   147123079 :   return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl);
   17017                 :             : }
   17018                 :             : 
   17019                 :             : /* OLDFNS is a lookup set of member functions from some class template, and
   17020                 :             :    NEWFNS is a lookup set of member functions from NEWTYPE, a specialization
   17021                 :             :    of that class template.  Return the subset of NEWFNS which are
   17022                 :             :    specializations of a function from OLDFNS.  */
   17023                 :             : 
   17024                 :             : static tree
   17025                 :     2432795 : filter_memfn_lookup (tree oldfns, tree newfns, tree newtype)
   17026                 :             : {
   17027                 :             :   /* Record all member functions from the old lookup set OLDFNS into
   17028                 :             :      VISIBLE_SET.  */
   17029                 :     2432795 :   hash_set<tree> visible_set;
   17030                 :     2432795 :   bool seen_dep_using = false;
   17031                 :     4915342 :   for (tree fn : lkp_range (oldfns))
   17032                 :             :     {
   17033                 :     2482547 :       if (TREE_CODE (fn) == USING_DECL)
   17034                 :             :         {
   17035                 :             :           /* Imprecisely handle dependent using-decl by keeping all members
   17036                 :             :              in the new lookup set that are defined in a base class, i.e.
   17037                 :             :              members that could plausibly have been introduced by this
   17038                 :             :              dependent using-decl.
   17039                 :             :              FIXME: Track which members are introduced by a dependent
   17040                 :             :              using-decl precisely, perhaps by performing another lookup
   17041                 :             :              from the substituted USING_DECL_SCOPE.  */
   17042                 :           0 :           gcc_checking_assert (DECL_DEPENDENT_P (fn));
   17043                 :             :           seen_dep_using = true;
   17044                 :             :         }
   17045                 :             :       else
   17046                 :     2482547 :         visible_set.add (fn);
   17047                 :             :     }
   17048                 :             : 
   17049                 :             :   /* Returns true iff (a less specialized version of) FN appeared in
   17050                 :             :      the old lookup set OLDFNS.  */
   17051                 :     4918301 :   auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) {
   17052                 :     2485506 :     if (DECL_CONTEXT (fn) != newtype)
   17053                 :             :       /* FN is a member function from a base class, introduced via a
   17054                 :             :          using-decl; if it might have been introduced by a dependent
   17055                 :             :          using-decl then just conservatively keep it, otherwise look
   17056                 :             :          in the old lookup set for FN exactly.  */
   17057                 :          36 :       return seen_dep_using || visible_set.contains (fn);
   17058                 :     2485470 :     else if (TREE_CODE (fn) == TEMPLATE_DECL)
   17059                 :             :       /* FN is a member function template from the current class;
   17060                 :             :          look in the old lookup set for the TEMPLATE_DECL from which
   17061                 :             :          it was specialized.  */
   17062                 :     1187045 :       return visible_set.contains (DECL_TI_TEMPLATE (fn));
   17063                 :             :     else
   17064                 :             :       /* FN is a non-template member function from the current class;
   17065                 :             :          look in the old lookup set for the FUNCTION_DECL from which
   17066                 :             :          it was specialized.  */
   17067                 :     1298425 :       return visible_set.contains (DECL_TEMPLATE_RESULT
   17068                 :     1298425 :                                    (DECL_TI_TEMPLATE (fn)));
   17069                 :     2432795 :   };
   17070                 :             : 
   17071                 :     2432795 :   bool lookup_changed_p = false;
   17072                 :     4915181 :   for (tree fn : lkp_range (newfns))
   17073                 :     2482550 :     if (!visible_p (fn))
   17074                 :             :       {
   17075                 :             :         lookup_changed_p = true;
   17076                 :             :         break;
   17077                 :             :       }
   17078                 :     2432795 :   if (!lookup_changed_p)
   17079                 :             :     return newfns;
   17080                 :             : 
   17081                 :             :   /* Filter out from NEWFNS the member functions that weren't
   17082                 :             :      previously visible according to OLDFNS.  */
   17083                 :         164 :   tree filtered_fns = NULL_TREE;
   17084                 :         164 :   unsigned filtered_size = 0;
   17085                 :        3120 :   for (tree fn : lkp_range (newfns))
   17086                 :        2956 :     if (visible_p (fn))
   17087                 :             :       {
   17088                 :         164 :         filtered_fns = lookup_add (fn, filtered_fns);
   17089                 :         164 :         filtered_size++;
   17090                 :             :       }
   17091                 :         164 :   gcc_checking_assert (seen_dep_using
   17092                 :             :                        ? filtered_size >= visible_set.elements ()
   17093                 :             :                        : filtered_size == visible_set.elements ());
   17094                 :             : 
   17095                 :             :   return filtered_fns;
   17096                 :     2432795 : }
   17097                 :             : 
   17098                 :             : /* tsubst a BASELINK.  OBJECT_TYPE, if non-NULL, is the type of the
   17099                 :             :    expression on the left-hand side of the "." or "->" operator.  We
   17100                 :             :    only do the lookup if we had a dependent BASELINK.  Otherwise we
   17101                 :             :    adjust it onto the instantiated heirarchy.  */
   17102                 :             : 
   17103                 :             : static tree
   17104                 :    17446894 : tsubst_baselink (tree baselink, tree object_type,
   17105                 :             :                  tree args, tsubst_flags_t complain, tree in_decl)
   17106                 :             : {
   17107                 :    17446894 :   bool qualified_p = BASELINK_QUALIFIED_P (baselink);
   17108                 :    17446894 :   tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
   17109                 :    17446894 :   qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
   17110                 :             : 
   17111                 :    17446894 :   tree optype = BASELINK_OPTYPE (baselink);
   17112                 :    17446894 :   optype = tsubst (optype, args, complain, in_decl);
   17113                 :             : 
   17114                 :    17446894 :   tree template_args = NULL_TREE;
   17115                 :    17446894 :   bool template_id_p = false;
   17116                 :    17446894 :   tree fns = BASELINK_FUNCTIONS (baselink);
   17117                 :    17446894 :   if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
   17118                 :             :     {
   17119                 :     6886464 :       template_id_p = true;
   17120                 :     6886464 :       template_args = TREE_OPERAND (fns, 1);
   17121                 :     6886464 :       fns = TREE_OPERAND (fns, 0);
   17122                 :     6886464 :       if (template_args)
   17123                 :     6886464 :         template_args = tsubst_template_args (template_args, args,
   17124                 :             :                                               complain, in_decl);
   17125                 :             :     }
   17126                 :             : 
   17127                 :    17446894 :   tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
   17128                 :    17446894 :   binfo_type = tsubst (binfo_type, args, complain, in_decl);
   17129                 :    17446894 :   bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink))
   17130                 :    31007541 :                       || optype != BASELINK_OPTYPE (baselink));
   17131                 :             : 
   17132                 :     7976367 :   if (dependent_p)
   17133                 :             :     {
   17134                 :     9470527 :       tree name = OVL_NAME (fns);
   17135                 :    18875975 :       if (IDENTIFIER_CONV_OP_P (name))
   17136                 :          10 :         name = make_conv_op_name (optype);
   17137                 :             : 
   17138                 :             :       /* See maybe_dependent_member_ref.  */
   17139                 :     9470527 :       if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope))
   17140                 :             :         {
   17141                 :      117410 :           if (template_id_p)
   17142                 :      117253 :             name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
   17143                 :             :                            template_args);
   17144                 :      117410 :           return build_qualified_name (NULL_TREE, qualifying_scope, name,
   17145                 :      117410 :                                        /* ::template */false);
   17146                 :             :         }
   17147                 :             : 
   17148                 :     9353117 :       if (name == complete_dtor_identifier)
   17149                 :             :         /* Treat as-if non-dependent below.  */
   17150                 :           0 :         dependent_p = false;
   17151                 :             : 
   17152                 :     9353117 :       bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink);
   17153                 :     9353117 :       baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
   17154                 :             :                                   complain);
   17155                 :     9353117 :       if (maybe_incomplete)
   17156                 :             :         {
   17157                 :             :           /* Filter out from the new lookup set those functions which didn't
   17158                 :             :              appear in the original lookup set (in a less specialized form).
   17159                 :             :              This is needed to preserve the consistency of member lookup
   17160                 :             :              performed in an incomplete-class context, within which
   17161                 :             :              later-declared members ought to remain invisible.  */
   17162                 :     2432795 :           BASELINK_FUNCTIONS (baselink)
   17163                 :     2432795 :             = filter_memfn_lookup (fns, BASELINK_FUNCTIONS (baselink),
   17164                 :             :                                    binfo_type);
   17165                 :     2432795 :           BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true;
   17166                 :             :         }
   17167                 :             : 
   17168                 :     9353117 :       if (!baselink)
   17169                 :             :         {
   17170                 :           0 :           if ((complain & tf_error)
   17171                 :           0 :               && constructor_name_p (name, qualifying_scope))
   17172                 :           0 :             error ("cannot call constructor %<%T::%D%> directly",
   17173                 :             :                    qualifying_scope, name);
   17174                 :           0 :           return error_mark_node;
   17175                 :             :         }
   17176                 :             : 
   17177                 :     9353117 :       fns = BASELINK_FUNCTIONS (baselink);
   17178                 :             :     }
   17179                 :             :   else
   17180                 :             :     {
   17181                 :             :       /* We're going to overwrite pieces below, make a duplicate.  */
   17182                 :     7976367 :       baselink = copy_node (baselink);
   17183                 :             : 
   17184                 :     7976367 :       if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
   17185                 :             :         {
   17186                 :             :           /* The decl we found was from non-dependent scope, but we still need
   17187                 :             :              to update the binfos for the instantiated qualifying_scope.  */
   17188                 :      259444 :           BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
   17189                 :      259444 :           BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
   17190                 :             :                                                    ba_unique, nullptr, complain);
   17191                 :             :         }
   17192                 :             :     }
   17193                 :             : 
   17194                 :             :   /* If lookup found a single function, mark it as used at this point.
   17195                 :             :      (If lookup found multiple functions the one selected later by
   17196                 :             :      overload resolution will be marked as used at that point.)  */
   17197                 :    17329484 :   if (!template_id_p && !really_overloaded_fn (fns))
   17198                 :             :     {
   17199                 :     7583442 :       tree fn = OVL_FIRST (fns);
   17200                 :     7583442 :       bool ok = mark_used (fn, complain);
   17201                 :     7583442 :       if (!ok && !(complain & tf_error))
   17202                 :           6 :         return error_mark_node;
   17203                 :     7583433 :       if (ok && BASELINK_P (baselink))
   17204                 :             :         /* We might have instantiated an auto function.  */
   17205                 :     7583433 :         TREE_TYPE (baselink) = TREE_TYPE (fn);
   17206                 :             :     }
   17207                 :             : 
   17208                 :    17329478 :   if (BASELINK_P (baselink))
   17209                 :             :     {
   17210                 :             :       /* Add back the template arguments, if present.  */
   17211                 :    17329478 :       if (template_id_p)
   17212                 :    13538422 :         BASELINK_FUNCTIONS (baselink)
   17213                 :     6769211 :           = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
   17214                 :             : 
   17215                 :             :       /* Update the conversion operator type.  */
   17216                 :    17329478 :       BASELINK_OPTYPE (baselink) = optype;
   17217                 :             :     }
   17218                 :             : 
   17219                 :    17329478 :   if (!object_type)
   17220                 :      322754 :     object_type = current_class_type;
   17221                 :             : 
   17222                 :    17329478 :   if (qualified_p || !dependent_p)
   17223                 :             :     {
   17224                 :     7987478 :       baselink = adjust_result_of_qualified_name_lookup (baselink,
   17225                 :             :                                                          qualifying_scope,
   17226                 :             :                                                          object_type);
   17227                 :     7987478 :       if (!qualified_p)
   17228                 :             :         /* We need to call adjust_result_of_qualified_name_lookup in case the
   17229                 :             :            destructor names a base class, but we unset BASELINK_QUALIFIED_P
   17230                 :             :            so that we still get virtual function binding.  */
   17231                 :     2919794 :         BASELINK_QUALIFIED_P (baselink) = false;
   17232                 :             :     }
   17233                 :             : 
   17234                 :             :   return baselink;
   17235                 :             : }
   17236                 :             : 
   17237                 :             : /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
   17238                 :             :    true if the qualified-id will be a postfix-expression in-and-of
   17239                 :             :    itself; false if more of the postfix-expression follows the
   17240                 :             :    QUALIFIED_ID.  ADDRESS_P is true if the qualified-id is the operand
   17241                 :             :    of "&".  */
   17242                 :             : 
   17243                 :             : static tree
   17244                 :    64634382 : tsubst_qualified_id (tree qualified_id, tree args,
   17245                 :             :                      tsubst_flags_t complain, tree in_decl,
   17246                 :             :                      bool done, bool address_p)
   17247                 :             : {
   17248                 :    64634382 :   tree expr;
   17249                 :    64634382 :   tree scope;
   17250                 :    64634382 :   tree name;
   17251                 :    64634382 :   bool is_template;
   17252                 :    64634382 :   tree template_args;
   17253                 :    64634382 :   location_t loc = EXPR_LOCATION (qualified_id);
   17254                 :             : 
   17255                 :    64634382 :   gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
   17256                 :             : 
   17257                 :             :   /* Figure out what name to look up.  */
   17258                 :    64634382 :   name = TREE_OPERAND (qualified_id, 1);
   17259                 :    64634382 :   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
   17260                 :             :     {
   17261                 :     4899206 :       is_template = true;
   17262                 :     4899206 :       template_args = TREE_OPERAND (name, 1);
   17263                 :     4899206 :       if (template_args)
   17264                 :     4899206 :         template_args = tsubst_template_args (template_args, args,
   17265                 :             :                                               complain, in_decl);
   17266                 :     4899206 :       if (template_args == error_mark_node)
   17267                 :             :         return error_mark_node;
   17268                 :     4899200 :       name = TREE_OPERAND (name, 0);
   17269                 :             :     }
   17270                 :             :   else
   17271                 :             :     {
   17272                 :             :       is_template = false;
   17273                 :             :       template_args = NULL_TREE;
   17274                 :             :     }
   17275                 :             : 
   17276                 :             :   /* Substitute into the qualifying scope.  When there are no ARGS, we
   17277                 :             :      are just trying to simplify a non-dependent expression.  In that
   17278                 :             :      case the qualifying scope may be dependent, and, in any case,
   17279                 :             :      substituting will not help.  */
   17280                 :    64634376 :   scope = TREE_OPERAND (qualified_id, 0);
   17281                 :    64634376 :   if (args)
   17282                 :             :     {
   17283                 :    62572185 :       scope = tsubst_scope (scope, args, complain, in_decl);
   17284                 :    62572185 :       expr = tsubst_name (name, args, complain, in_decl);
   17285                 :             :     }
   17286                 :             :   else
   17287                 :             :     expr = name;
   17288                 :             : 
   17289                 :    64634376 :   if (dependent_scope_p (scope))
   17290                 :             :     {
   17291                 :    15608610 :       if (TREE_CODE (expr) == SCOPE_REF)
   17292                 :             :         /* We built one in tsubst_baselink.  */
   17293                 :           4 :         gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0)));
   17294                 :             :       else
   17295                 :             :         {
   17296                 :    15608606 :           if (is_template)
   17297                 :     2009333 :             expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr,
   17298                 :             :                                      template_args);
   17299                 :    15608606 :           expr = build_qualified_name (NULL_TREE, scope, expr,
   17300                 :    15608606 :                                        QUALIFIED_NAME_IS_TEMPLATE
   17301                 :             :                                        (qualified_id));
   17302                 :             :         }
   17303                 :    15608610 :       REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id);
   17304                 :    15608610 :       return expr;
   17305                 :             :     }
   17306                 :             : 
   17307                 :    49025766 :   if (!BASELINK_P (name) && !DECL_P (expr))
   17308                 :             :     {
   17309                 :    45068846 :       if (TREE_CODE (expr) == BIT_NOT_EXPR)
   17310                 :             :         {
   17311                 :             :           /* A BIT_NOT_EXPR is used to represent a destructor.  */
   17312                 :           8 :           if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
   17313                 :             :             {
   17314                 :           0 :               error ("qualifying type %qT does not match destructor name ~%qT",
   17315                 :           0 :                      scope, TREE_OPERAND (expr, 0));
   17316                 :           0 :               expr = error_mark_node;
   17317                 :             :             }
   17318                 :             :           else
   17319                 :           8 :             expr = lookup_qualified_name (scope, complete_dtor_identifier,
   17320                 :             :                                           LOOK_want::NORMAL, false);
   17321                 :             :         }
   17322                 :             :       else
   17323                 :    45068838 :         expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
   17324                 :    45068782 :       if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
   17325                 :             :                      ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
   17326                 :             :         {
   17327                 :          33 :           if (complain & tf_error)
   17328                 :             :             {
   17329                 :          27 :               error ("dependent-name %qE is parsed as a non-type, but "
   17330                 :             :                      "instantiation yields a type", qualified_id);
   17331                 :          27 :               inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
   17332                 :             :             }
   17333                 :          33 :           return error_mark_node;
   17334                 :             :         }
   17335                 :             :     }
   17336                 :             : 
   17337                 :    49025669 :   if (DECL_P (expr))
   17338                 :             :     {
   17339                 :    41581250 :       if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
   17340                 :             :                                                 scope, complain))
   17341                 :           5 :         return error_mark_node;
   17342                 :             :       /* Remember that there was a reference to this entity.  */
   17343                 :    41581245 :       if (!mark_used (expr, complain) && !(complain & tf_error))
   17344                 :           0 :         return error_mark_node;
   17345                 :             :     }
   17346                 :             : 
   17347                 :    49022056 :   if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
   17348                 :             :     {
   17349                 :      297126 :       if (complain & tf_error)
   17350                 :         115 :         qualified_name_lookup_error (scope,
   17351                 :         115 :                                      TREE_OPERAND (qualified_id, 1),
   17352                 :             :                                      expr, input_location);
   17353                 :      297126 :       return error_mark_node;
   17354                 :             :     }
   17355                 :             : 
   17356                 :    48724930 :   if (is_template)
   17357                 :             :     {
   17358                 :             :       /* We may be repeating a check already done during parsing, but
   17359                 :             :          if it was well-formed and passed then, it will pass again
   17360                 :             :          now, and if it didn't, we wouldn't have got here.  The case
   17361                 :             :          we want to catch is when we couldn't tell then, and can now,
   17362                 :             :          namely when templ prior to substitution was an
   17363                 :             :          identifier.  */
   17364                 :     2886245 :       if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
   17365                 :           0 :         return error_mark_node;
   17366                 :             : 
   17367                 :     2886245 :       if (variable_template_p (expr))
   17368                 :        9734 :         expr = lookup_and_finish_template_variable (expr, template_args,
   17369                 :             :                                                     complain);
   17370                 :             :       else
   17371                 :     2876511 :         expr = lookup_template_function (expr, template_args);
   17372                 :             :     }
   17373                 :             : 
   17374                 :    48724930 :   if (expr == error_mark_node && complain & tf_error)
   17375                 :           4 :     qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
   17376                 :             :                                  expr, input_location);
   17377                 :    48724926 :   else if (TYPE_P (scope))
   17378                 :             :     {
   17379                 :    48724926 :       expr = (adjust_result_of_qualified_name_lookup
   17380                 :    48724926 :               (expr, scope, current_nonlambda_class_type ()));
   17381                 :    48724926 :       expr = (finish_qualified_id_expr
   17382                 :    48724934 :               (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
   17383                 :    48724926 :                QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
   17384                 :             :                /*template_arg_p=*/false, complain));
   17385                 :             :     }
   17386                 :             : 
   17387                 :             :   /* Expressions do not generally have reference type.  */
   17388                 :    48724930 :   if (TREE_CODE (expr) != SCOPE_REF
   17389                 :             :       /* However, if we're about to form a pointer-to-member, we just
   17390                 :             :          want the referenced member referenced.  */
   17391                 :    45450352 :       && TREE_CODE (expr) != OFFSET_REF)
   17392                 :    45420324 :     expr = convert_from_reference (expr);
   17393                 :             : 
   17394                 :    48724930 :   if (REF_PARENTHESIZED_P (qualified_id))
   17395                 :         130 :     expr = force_paren_expr (expr);
   17396                 :             : 
   17397                 :    48724930 :   expr = maybe_wrap_with_location (expr, loc);
   17398                 :             : 
   17399                 :    48724930 :   return expr;
   17400                 :             : }
   17401                 :             : 
   17402                 :             : /* tsubst the initializer for a VAR_DECL.  INIT is the unsubstituted
   17403                 :             :    initializer, DECL is the substituted VAR_DECL.  Other arguments are as
   17404                 :             :    for tsubst.  */
   17405                 :             : 
   17406                 :             : static tree
   17407                 :    23725808 : tsubst_init (tree init, tree decl, tree args,
   17408                 :             :              tsubst_flags_t complain, tree in_decl)
   17409                 :             : {
   17410                 :    23725808 :   if (!init)
   17411                 :             :     return NULL_TREE;
   17412                 :             : 
   17413                 :    19005417 :   init = tsubst_expr (init, args, complain, in_decl);
   17414                 :             : 
   17415                 :    19005417 :   tree type = TREE_TYPE (decl);
   17416                 :             : 
   17417                 :    19005417 :   if (!init && type != error_mark_node)
   17418                 :             :     {
   17419                 :          51 :       if (tree auto_node = type_uses_auto (type))
   17420                 :             :         {
   17421                 :           6 :           if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   17422                 :             :             {
   17423                 :           6 :               if (complain & tf_error)
   17424                 :           6 :                 error ("initializer for %q#D expands to an empty list "
   17425                 :             :                        "of expressions", decl);
   17426                 :           6 :               return error_mark_node;
   17427                 :             :             }
   17428                 :             :         }
   17429                 :          45 :       else if (!dependent_type_p (type))
   17430                 :             :         {
   17431                 :             :           /* If we had an initializer but it
   17432                 :             :              instantiated to nothing,
   17433                 :             :              value-initialize the object.  This will
   17434                 :             :              only occur when the initializer was a
   17435                 :             :              pack expansion where the parameter packs
   17436                 :             :              used in that expansion were of length
   17437                 :             :              zero.  */
   17438                 :          45 :           init = build_value_init (type, complain);
   17439                 :          45 :           if (TREE_CODE (init) == AGGR_INIT_EXPR)
   17440                 :          23 :             init = get_target_expr (init, complain);
   17441                 :          45 :           if (TREE_CODE (init) == TARGET_EXPR)
   17442                 :          23 :             TARGET_EXPR_DIRECT_INIT_P (init) = true;
   17443                 :             :         }
   17444                 :             :     }
   17445                 :             : 
   17446                 :             :   return init;
   17447                 :             : }
   17448                 :             : 
   17449                 :             : /* If T is a reference to a dependent member of the current instantiation C and
   17450                 :             :    we are trying to refer to that member in a partial instantiation of C,
   17451                 :             :    return a SCOPE_REF; otherwise, return NULL_TREE.
   17452                 :             : 
   17453                 :             :    This can happen when forming a C++17 deduction guide, as in PR96199.  */
   17454                 :             : 
   17455                 :             : static tree
   17456                 :  5422058482 : maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
   17457                 :             :                             tree in_decl)
   17458                 :             : {
   17459                 :  5422058482 :   if (!(complain & tf_dguide))
   17460                 :             :     return NULL_TREE;
   17461                 :             : 
   17462                 :  5424085327 :   tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t;
   17463                 :     2037281 :   if (!decl || !DECL_P (decl))
   17464                 :             :     return NULL_TREE;
   17465                 :             : 
   17466                 :     1040080 :   tree ctx = context_for_name_lookup (decl);
   17467                 :     1040080 :   if (!CLASS_TYPE_P (ctx))
   17468                 :             :     return NULL_TREE;
   17469                 :             : 
   17470                 :       41964 :   ctx = tsubst (ctx, args, complain, in_decl);
   17471                 :       41964 :   if (!dependent_scope_p (ctx))
   17472                 :             :     return NULL_TREE;
   17473                 :             : 
   17474                 :       41898 :   if (TYPE_P (t))
   17475                 :             :     {
   17476                 :       26508 :       if (typedef_variant_p (t))
   17477                 :       16211 :         t = strip_typedefs (t);
   17478                 :       26508 :       tree decl = TYPE_NAME (t);
   17479                 :       26508 :       if (decl)
   17480                 :       26056 :         decl = maybe_dependent_member_ref (decl, args, complain, in_decl);
   17481                 :       26056 :       if (!decl)
   17482                 :       21487 :         return NULL_TREE;
   17483                 :        5021 :       return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t),
   17484                 :        5021 :                                       complain);
   17485                 :             :     }
   17486                 :             : 
   17487                 :       15390 :   tree name = DECL_NAME (t);
   17488                 :       15390 :   tree fullname = name;
   17489                 :       15390 :   if (instantiates_primary_template_p (t))
   17490                 :             :     {
   17491                 :        4348 :       tree tinfo = get_template_info (t);
   17492                 :        4348 :       name = DECL_NAME (TI_TEMPLATE (tinfo));
   17493                 :        4348 :       tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
   17494                 :        4348 :       targs = tsubst_template_args (targs, args, complain, in_decl);
   17495                 :        4348 :       fullname = build_nt (TEMPLATE_ID_EXPR, name, targs);
   17496                 :             :     }
   17497                 :             : 
   17498                 :       15390 :   if (TREE_CODE (t) == TYPE_DECL)
   17499                 :             :     {
   17500                 :       14996 :       if (TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE
   17501                 :       14996 :           && TYPE_NAME (TREE_TYPE (t)) == t)
   17502                 :             :         /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename
   17503                 :             :            scope, but it doesn't need to be rewritten again.  */
   17504                 :             :         return NULL_TREE;
   17505                 :        5021 :       tree type = build_typename_type (ctx, name, fullname, typename_type);
   17506                 :        5021 :       return TYPE_NAME (type);
   17507                 :             :     }
   17508                 :         394 :   else if (DECL_TYPE_TEMPLATE_P (t))
   17509                 :           8 :     return make_unbound_class_template (ctx, name,
   17510                 :           8 :                                         NULL_TREE, complain);
   17511                 :             :   else
   17512                 :         386 :     return build_qualified_name (NULL_TREE, ctx, fullname,
   17513                 :         386 :                                  TREE_CODE (t) == TEMPLATE_DECL);
   17514                 :             : }
   17515                 :             : 
   17516                 :             : /* Helper function for tsubst_omp_clauses, used for instantiation of
   17517                 :             :    OMP_CLAUSE_DECL of clauses.  */
   17518                 :             : 
   17519                 :             : static tree
   17520                 :        7198 : tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
   17521                 :             :                         tree in_decl, tree *iterator_cache)
   17522                 :             : {
   17523                 :        7198 :   if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY])
   17524                 :             :     return decl;
   17525                 :             : 
   17526                 :             :   /* Handle OpenMP iterators.  */
   17527                 :        7140 :   if (TREE_CODE (decl) == TREE_LIST
   17528                 :         212 :       && TREE_PURPOSE (decl)
   17529                 :        7352 :       && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
   17530                 :             :     {
   17531                 :         176 :       tree ret;
   17532                 :         176 :       if (iterator_cache[0] == TREE_PURPOSE (decl))
   17533                 :          26 :         ret = iterator_cache[1];
   17534                 :             :       else
   17535                 :             :         {
   17536                 :         150 :           tree *tp = &ret;
   17537                 :         150 :           begin_scope (sk_omp, NULL);
   17538                 :         330 :           for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
   17539                 :             :             {
   17540                 :         180 :               *tp = copy_node (it);
   17541                 :         180 :               TREE_VEC_ELT (*tp, 0)
   17542                 :         180 :                 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
   17543                 :         180 :               DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl;
   17544                 :         180 :               pushdecl (TREE_VEC_ELT (*tp, 0));
   17545                 :         180 :               TREE_VEC_ELT (*tp, 1)
   17546                 :         180 :                 = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl);
   17547                 :         180 :               TREE_VEC_ELT (*tp, 2)
   17548                 :         180 :                 = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl);
   17549                 :         180 :               TREE_VEC_ELT (*tp, 3)
   17550                 :         180 :                 = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl);
   17551                 :         180 :               TREE_CHAIN (*tp) = NULL_TREE;
   17552                 :         180 :               tp = &TREE_CHAIN (*tp);
   17553                 :             :             }
   17554                 :         150 :           TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
   17555                 :         150 :           iterator_cache[0] = TREE_PURPOSE (decl);
   17556                 :         150 :           iterator_cache[1] = ret;
   17557                 :             :         }
   17558                 :         176 :       return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
   17559                 :             :                                                            args, complain,
   17560                 :             :                                                            in_decl, NULL));
   17561                 :             :     }
   17562                 :             : 
   17563                 :             :   /* Handle an OpenMP array section represented as a TREE_LIST (or
   17564                 :             :      OMP_CLAUSE_DOACROSS_KIND).  An OMP_CLAUSE_DOACROSS (with a depend
   17565                 :             :      kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a
   17566                 :             :      TREE_LIST.  We can handle it exactly the same as an array section
   17567                 :             :      (purpose, value, and a chain), even though the nomenclature
   17568                 :             :      (low_bound, length, etc) is different.  */
   17569                 :        6964 :   if (TREE_CODE (decl) == TREE_LIST)
   17570                 :             :     {
   17571                 :          36 :       tree low_bound
   17572                 :          36 :         = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl);
   17573                 :          36 :       tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl);
   17574                 :          36 :       tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
   17575                 :             :                                            in_decl, NULL);
   17576                 :          36 :       if (TREE_PURPOSE (decl) == low_bound
   17577                 :          36 :           && TREE_VALUE (decl) == length
   17578                 :          48 :           && TREE_CHAIN (decl) == chain)
   17579                 :             :         return decl;
   17580                 :          24 :       tree ret = tree_cons (low_bound, length, chain);
   17581                 :          24 :       OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret)
   17582                 :          24 :         = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl);
   17583                 :          24 :       return ret;
   17584                 :             :     }
   17585                 :        6928 :   else if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
   17586                 :             :     {
   17587                 :        2147 :       tree low_bound
   17588                 :        2147 :         = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl);
   17589                 :        2147 :       tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain,
   17590                 :             :                                  in_decl);
   17591                 :        2147 :       tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args,
   17592                 :             :                                            complain, in_decl, NULL);
   17593                 :        2147 :       if (TREE_OPERAND (decl, 0) == base
   17594                 :         687 :           && TREE_OPERAND (decl, 1) == low_bound
   17595                 :        2709 :           && TREE_OPERAND (decl, 2) == length)
   17596                 :             :         return decl;
   17597                 :        1594 :       return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound,
   17598                 :        1594 :                      length);
   17599                 :             :     }
   17600                 :        4781 :   tree ret = tsubst_stmt (decl, args, complain, in_decl);
   17601                 :             :   /* Undo convert_from_reference tsubst_expr could have called.  */
   17602                 :        4781 :   if (decl
   17603                 :        4781 :       && REFERENCE_REF_P (ret)
   17604                 :         594 :       && !REFERENCE_REF_P (decl))
   17605                 :         584 :     ret = TREE_OPERAND (ret, 0);
   17606                 :             :   return ret;
   17607                 :             : }
   17608                 :             : 
   17609                 :             : /* Like tsubst_copy, but specifically for OpenMP clauses.  */
   17610                 :             : 
   17611                 :             : static tree
   17612                 :        5516 : tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
   17613                 :             :                     tree args, tsubst_flags_t complain, tree in_decl)
   17614                 :             : {
   17615                 :        5516 :   tree new_clauses = NULL_TREE, nc, oc;
   17616                 :        5516 :   tree linear_no_step = NULL_TREE;
   17617                 :        5516 :   tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
   17618                 :             : 
   17619                 :       11995 :   for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
   17620                 :             :     {
   17621                 :        6479 :       nc = copy_node (oc);
   17622                 :        6479 :       OMP_CLAUSE_CHAIN (nc) = new_clauses;
   17623                 :        6479 :       new_clauses = nc;
   17624                 :             : 
   17625                 :        6479 :       switch (OMP_CLAUSE_CODE (nc))
   17626                 :             :         {
   17627                 :         211 :         case OMP_CLAUSE_LASTPRIVATE:
   17628                 :         211 :           if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
   17629                 :             :             {
   17630                 :          38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
   17631                 :          38 :               tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args,
   17632                 :             :                            complain, in_decl);
   17633                 :          38 :               OMP_CLAUSE_LASTPRIVATE_STMT (nc)
   17634                 :          76 :                 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
   17635                 :             :             }
   17636                 :             :           /* FALLTHRU */
   17637                 :        3167 :         case OMP_CLAUSE_PRIVATE:
   17638                 :        3167 :         case OMP_CLAUSE_SHARED:
   17639                 :        3167 :         case OMP_CLAUSE_FIRSTPRIVATE:
   17640                 :        3167 :         case OMP_CLAUSE_COPYIN:
   17641                 :        3167 :         case OMP_CLAUSE_COPYPRIVATE:
   17642                 :        3167 :         case OMP_CLAUSE_UNIFORM:
   17643                 :        3167 :         case OMP_CLAUSE_DEPEND:
   17644                 :        3167 :         case OMP_CLAUSE_DOACROSS:
   17645                 :        3167 :         case OMP_CLAUSE_AFFINITY:
   17646                 :        3167 :         case OMP_CLAUSE_FROM:
   17647                 :        3167 :         case OMP_CLAUSE_TO:
   17648                 :        3167 :         case OMP_CLAUSE_MAP:
   17649                 :        3167 :         case OMP_CLAUSE__CACHE_:
   17650                 :        3167 :         case OMP_CLAUSE_NONTEMPORAL:
   17651                 :        3167 :         case OMP_CLAUSE_USE_DEVICE_PTR:
   17652                 :        3167 :         case OMP_CLAUSE_USE_DEVICE_ADDR:
   17653                 :        3167 :         case OMP_CLAUSE_IS_DEVICE_PTR:
   17654                 :        3167 :         case OMP_CLAUSE_HAS_DEVICE_ADDR:
   17655                 :        3167 :         case OMP_CLAUSE_INCLUSIVE:
   17656                 :        3167 :         case OMP_CLAUSE_EXCLUSIVE:
   17657                 :        6334 :           OMP_CLAUSE_DECL (nc)
   17658                 :        3167 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   17659                 :             :                                       in_decl, iterator_cache);
   17660                 :        3167 :           break;
   17661                 :         196 :         case OMP_CLAUSE_NUM_TEAMS:
   17662                 :         196 :           if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc))
   17663                 :          88 :             OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc)
   17664                 :         176 :               = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args,
   17665                 :             :                              complain, in_decl);
   17666                 :             :           /* FALLTHRU */
   17667                 :         884 :         case OMP_CLAUSE_TILE:
   17668                 :         884 :         case OMP_CLAUSE_IF:
   17669                 :         884 :         case OMP_CLAUSE_SELF:
   17670                 :         884 :         case OMP_CLAUSE_NUM_THREADS:
   17671                 :         884 :         case OMP_CLAUSE_SCHEDULE:
   17672                 :         884 :         case OMP_CLAUSE_COLLAPSE:
   17673                 :         884 :         case OMP_CLAUSE_FINAL:
   17674                 :         884 :         case OMP_CLAUSE_DEVICE:
   17675                 :         884 :         case OMP_CLAUSE_DIST_SCHEDULE:
   17676                 :         884 :         case OMP_CLAUSE_THREAD_LIMIT:
   17677                 :         884 :         case OMP_CLAUSE_SAFELEN:
   17678                 :         884 :         case OMP_CLAUSE_SIMDLEN:
   17679                 :         884 :         case OMP_CLAUSE_NUM_TASKS:
   17680                 :         884 :         case OMP_CLAUSE_GRAINSIZE:
   17681                 :         884 :         case OMP_CLAUSE_PRIORITY:
   17682                 :         884 :         case OMP_CLAUSE_ORDERED:
   17683                 :         884 :         case OMP_CLAUSE_HINT:
   17684                 :         884 :         case OMP_CLAUSE_FILTER:
   17685                 :         884 :         case OMP_CLAUSE_NUM_GANGS:
   17686                 :         884 :         case OMP_CLAUSE_NUM_WORKERS:
   17687                 :         884 :         case OMP_CLAUSE_VECTOR_LENGTH:
   17688                 :         884 :         case OMP_CLAUSE_WORKER:
   17689                 :         884 :         case OMP_CLAUSE_VECTOR:
   17690                 :         884 :         case OMP_CLAUSE_ASYNC:
   17691                 :         884 :         case OMP_CLAUSE_WAIT:
   17692                 :         884 :         case OMP_CLAUSE_DETACH:
   17693                 :         884 :           OMP_CLAUSE_OPERAND (nc, 0)
   17694                 :         884 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl);
   17695                 :         884 :           break;
   17696                 :        1040 :         case OMP_CLAUSE_REDUCTION:
   17697                 :        1040 :         case OMP_CLAUSE_IN_REDUCTION:
   17698                 :        1040 :         case OMP_CLAUSE_TASK_REDUCTION:
   17699                 :        1040 :           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
   17700                 :             :             {
   17701                 :          45 :               tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
   17702                 :          45 :               if (TREE_CODE (placeholder) == SCOPE_REF)
   17703                 :             :                 {
   17704                 :           6 :                   tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
   17705                 :             :                                        complain, in_decl);
   17706                 :           6 :                   OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
   17707                 :          12 :                     = build_qualified_name (NULL_TREE, scope,
   17708                 :           6 :                                             TREE_OPERAND (placeholder, 1),
   17709                 :             :                                             false);
   17710                 :             :                 }
   17711                 :             :               else
   17712                 :          39 :                 gcc_assert (identifier_p (placeholder));
   17713                 :             :             }
   17714                 :        2080 :           OMP_CLAUSE_DECL (nc)
   17715                 :        1040 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   17716                 :             :                                       in_decl, NULL);
   17717                 :        1040 :           break;
   17718                 :          82 :         case OMP_CLAUSE_GANG:
   17719                 :          82 :         case OMP_CLAUSE_ALIGNED:
   17720                 :         164 :           OMP_CLAUSE_DECL (nc)
   17721                 :          82 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   17722                 :             :                                       in_decl, NULL);
   17723                 :          82 :           OMP_CLAUSE_OPERAND (nc, 1)
   17724                 :          82 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   17725                 :          82 :           break;
   17726                 :         384 :         case OMP_CLAUSE_ALLOCATE:
   17727                 :         768 :           OMP_CLAUSE_DECL (nc)
   17728                 :         384 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   17729                 :             :                                       in_decl, NULL);
   17730                 :         384 :           OMP_CLAUSE_OPERAND (nc, 1)
   17731                 :         384 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl);
   17732                 :         384 :           OMP_CLAUSE_OPERAND (nc, 2)
   17733                 :         384 :             = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl);
   17734                 :         384 :           break;
   17735                 :         166 :         case OMP_CLAUSE_LINEAR:
   17736                 :         332 :           OMP_CLAUSE_DECL (nc)
   17737                 :         166 :             = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
   17738                 :             :                                       in_decl, NULL);
   17739                 :         166 :           if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
   17740                 :             :             {
   17741                 :           2 :               gcc_assert (!linear_no_step);
   17742                 :             :               linear_no_step = nc;
   17743                 :             :             }
   17744                 :         164 :           else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
   17745                 :           0 :             OMP_CLAUSE_LINEAR_STEP (nc)
   17746                 :           0 :               = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
   17747                 :             :                                         complain, in_decl, NULL);
   17748                 :             :           else
   17749                 :         164 :             OMP_CLAUSE_LINEAR_STEP (nc)
   17750                 :         328 :               = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args,
   17751                 :             :                              complain, in_decl);
   17752                 :             :           break;
   17753                 :             :         case OMP_CLAUSE_NOWAIT:
   17754                 :             :         case OMP_CLAUSE_DEFAULT:
   17755                 :             :         case OMP_CLAUSE_UNTIED:
   17756                 :             :         case OMP_CLAUSE_MERGEABLE:
   17757                 :             :         case OMP_CLAUSE_INBRANCH:
   17758                 :             :         case OMP_CLAUSE_NOTINBRANCH:
   17759                 :             :         case OMP_CLAUSE_PROC_BIND:
   17760                 :             :         case OMP_CLAUSE_FOR:
   17761                 :             :         case OMP_CLAUSE_PARALLEL:
   17762                 :             :         case OMP_CLAUSE_SECTIONS:
   17763                 :             :         case OMP_CLAUSE_TASKGROUP:
   17764                 :             :         case OMP_CLAUSE_NOGROUP:
   17765                 :             :         case OMP_CLAUSE_THREADS:
   17766                 :             :         case OMP_CLAUSE_SIMD:
   17767                 :             :         case OMP_CLAUSE_DEFAULTMAP:
   17768                 :             :         case OMP_CLAUSE_ORDER:
   17769                 :             :         case OMP_CLAUSE_BIND:
   17770                 :             :         case OMP_CLAUSE_INDEPENDENT:
   17771                 :             :         case OMP_CLAUSE_AUTO:
   17772                 :             :         case OMP_CLAUSE_SEQ:
   17773                 :             :         case OMP_CLAUSE_IF_PRESENT:
   17774                 :             :         case OMP_CLAUSE_FINALIZE:
   17775                 :             :         case OMP_CLAUSE_NOHOST:
   17776                 :             :           break;
   17777                 :           0 :         default:
   17778                 :           0 :           gcc_unreachable ();
   17779                 :             :         }
   17780                 :        6479 :       if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
   17781                 :        5429 :         switch (OMP_CLAUSE_CODE (nc))
   17782                 :             :           {
   17783                 :        2258 :           case OMP_CLAUSE_SHARED:
   17784                 :        2258 :           case OMP_CLAUSE_PRIVATE:
   17785                 :        2258 :           case OMP_CLAUSE_FIRSTPRIVATE:
   17786                 :        2258 :           case OMP_CLAUSE_LASTPRIVATE:
   17787                 :        2258 :           case OMP_CLAUSE_COPYPRIVATE:
   17788                 :        2258 :           case OMP_CLAUSE_LINEAR:
   17789                 :        2258 :           case OMP_CLAUSE_REDUCTION:
   17790                 :        2258 :           case OMP_CLAUSE_IN_REDUCTION:
   17791                 :        2258 :           case OMP_CLAUSE_TASK_REDUCTION:
   17792                 :        2258 :           case OMP_CLAUSE_USE_DEVICE_PTR:
   17793                 :        2258 :           case OMP_CLAUSE_USE_DEVICE_ADDR:
   17794                 :        2258 :           case OMP_CLAUSE_IS_DEVICE_PTR:
   17795                 :        2258 :           case OMP_CLAUSE_HAS_DEVICE_ADDR:
   17796                 :        2258 :           case OMP_CLAUSE_INCLUSIVE:
   17797                 :        2258 :           case OMP_CLAUSE_EXCLUSIVE:
   17798                 :        2258 :           case OMP_CLAUSE_ALLOCATE:
   17799                 :             :             /* tsubst_expr on SCOPE_REF results in returning
   17800                 :             :                finish_non_static_data_member result.  Undo that here.  */
   17801                 :        2258 :             if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
   17802                 :        2258 :                 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
   17803                 :             :                     == IDENTIFIER_NODE))
   17804                 :             :               {
   17805                 :          94 :                 tree t = OMP_CLAUSE_DECL (nc);
   17806                 :          94 :                 tree v = t;
   17807                 :         554 :                 while (v)
   17808                 :         554 :                   switch (TREE_CODE (v))
   17809                 :             :                     {
   17810                 :         460 :                     case COMPONENT_REF:
   17811                 :         460 :                     case MEM_REF:
   17812                 :         460 :                     case INDIRECT_REF:
   17813                 :         460 :                     CASE_CONVERT:
   17814                 :         460 :                     case POINTER_PLUS_EXPR:
   17815                 :         460 :                       v = TREE_OPERAND (v, 0);
   17816                 :         460 :                       continue;
   17817                 :          92 :                     case PARM_DECL:
   17818                 :          92 :                       if (DECL_CONTEXT (v) == current_function_decl
   17819                 :          92 :                           && DECL_ARTIFICIAL (v)
   17820                 :         184 :                           && DECL_NAME (v) == this_identifier)
   17821                 :          92 :                         OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
   17822                 :             :                       /* FALLTHRU */
   17823                 :             :                     default:
   17824                 :             :                       v = NULL_TREE;
   17825                 :             :                       break;
   17826                 :             :                     }
   17827                 :             :               }
   17828                 :        2164 :             else if (VAR_P (OMP_CLAUSE_DECL (oc))
   17829                 :        1062 :                      && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
   17830                 :         214 :                      && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
   17831                 :         214 :                      && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
   17832                 :        2378 :                      && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
   17833                 :             :               {
   17834                 :         214 :                 tree decl = OMP_CLAUSE_DECL (nc);
   17835                 :         214 :                 if (VAR_P (decl))
   17836                 :             :                   {
   17837                 :         214 :                     retrofit_lang_decl (decl);
   17838                 :         214 :                     DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
   17839                 :             :                   }
   17840                 :             :               }
   17841                 :             :             break;
   17842                 :             :           default:
   17843                 :             :             break;
   17844                 :             :           }
   17845                 :             :     }
   17846                 :             : 
   17847                 :        5516 :   new_clauses = nreverse (new_clauses);
   17848                 :        5516 :   if (ort != C_ORT_OMP_DECLARE_SIMD)
   17849                 :             :     {
   17850                 :        5376 :       new_clauses = finish_omp_clauses (new_clauses, ort);
   17851                 :        5376 :       if (linear_no_step)
   17852                 :           2 :         for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
   17853                 :           2 :           if (nc == linear_no_step)
   17854                 :             :             {
   17855                 :           2 :               OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
   17856                 :           2 :               break;
   17857                 :             :             }
   17858                 :             :     }
   17859                 :        5516 :   return new_clauses;
   17860                 :             : }
   17861                 :             : 
   17862                 :             : /* Like tsubst_expr, but unshare TREE_LIST nodes.  */
   17863                 :             : 
   17864                 :             : static tree
   17865                 :      141849 : tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
   17866                 :             :                           tree in_decl)
   17867                 :             : {
   17868                 :             : #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
   17869                 :             : 
   17870                 :      141849 :   tree purpose, value, chain;
   17871                 :             : 
   17872                 :      141849 :   if (t == NULL)
   17873                 :             :     return t;
   17874                 :             : 
   17875                 :       99682 :   if (TREE_CODE (t) != TREE_LIST)
   17876                 :       49863 :     return tsubst_expr (t, args, complain, in_decl);
   17877                 :             : 
   17878                 :       49819 :   if (t == void_list_node)
   17879                 :             :     return t;
   17880                 :             : 
   17881                 :       49819 :   purpose = TREE_PURPOSE (t);
   17882                 :       49819 :   if (purpose)
   17883                 :        6058 :     purpose = RECUR (purpose);
   17884                 :       49819 :   value = TREE_VALUE (t);
   17885                 :       49819 :   if (value)
   17886                 :             :     {
   17887                 :       49819 :       if (TREE_CODE (value) != LABEL_DECL)
   17888                 :       49815 :         value = RECUR (value);
   17889                 :             :       else
   17890                 :             :         {
   17891                 :           4 :           value = lookup_label (DECL_NAME (value));
   17892                 :           4 :           gcc_assert (TREE_CODE (value) == LABEL_DECL);
   17893                 :           4 :           TREE_USED (value) = 1;
   17894                 :             :         }
   17895                 :             :     }
   17896                 :       49819 :   chain = TREE_CHAIN (t);
   17897                 :       49819 :   if (chain && chain != void_type_node)
   17898                 :       22324 :     chain = RECUR (chain);
   17899                 :       49819 :   return tree_cons (purpose, value, chain);
   17900                 :             : #undef RECUR
   17901                 :             : }
   17902                 :             : 
   17903                 :             : /* Used to temporarily communicate the list of #pragma omp parallel
   17904                 :             :    clauses to #pragma omp for instantiation if they are combined
   17905                 :             :    together.  */
   17906                 :             : 
   17907                 :             : static tree *omp_parallel_combined_clauses;
   17908                 :             : 
   17909                 :             : static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
   17910                 :             :                                  cp_decomp *);
   17911                 :             : 
   17912                 :             : /* Substitute one OMP_FOR iterator.  */
   17913                 :             : 
   17914                 :             : static bool
   17915                 :        1298 : tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
   17916                 :             :                          tree initv, tree condv, tree incrv, tree *clauses,
   17917                 :             :                          tree args, tsubst_flags_t complain, tree in_decl)
   17918                 :             : {
   17919                 :             : #define RECUR(NODE)                             \
   17920                 :             :   tsubst_stmt ((NODE), args, complain, in_decl)
   17921                 :        1298 :   tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
   17922                 :        1298 :   bool ret = false;
   17923                 :             : 
   17924                 :        1298 :   init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
   17925                 :        1298 :   gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
   17926                 :             : 
   17927                 :        1298 :   decl = TREE_OPERAND (init, 0);
   17928                 :        1298 :   init = TREE_OPERAND (init, 1);
   17929                 :        1298 :   tree decl_expr = NULL_TREE;
   17930                 :        1298 :   bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
   17931                 :        1298 :   if (range_for)
   17932                 :             :     {
   17933                 :          76 :       bool decomp = false;
   17934                 :          76 :       if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
   17935                 :             :         {
   17936                 :          37 :           tree v = DECL_VALUE_EXPR (decl);
   17937                 :          37 :           if (TREE_CODE (v) == ARRAY_REF
   17938                 :          37 :               && VAR_P (TREE_OPERAND (v, 0))
   17939                 :          74 :               && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
   17940                 :             :             {
   17941                 :          37 :               cp_decomp decomp_d = { NULL_TREE, 0 };
   17942                 :          37 :               tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
   17943                 :          37 :               maybe_push_decl (d);
   17944                 :          37 :               d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
   17945                 :             :                                        in_decl, &decomp_d);
   17946                 :          37 :               decomp = true;
   17947                 :          37 :               if (d == error_mark_node)
   17948                 :           0 :                 decl = error_mark_node;
   17949                 :             :               else
   17950                 :         139 :                 for (unsigned int i = 0; i < decomp_d.count; i++)
   17951                 :             :                   {
   17952                 :         102 :                     if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl))
   17953                 :             :                       {
   17954                 :         102 :                         tree v = build_nt (ARRAY_REF, d,
   17955                 :         102 :                                            size_int (decomp_d.count - i - 1),
   17956                 :             :                                            NULL_TREE, NULL_TREE);
   17957                 :         102 :                         SET_DECL_VALUE_EXPR (decomp_d.decl, v);
   17958                 :         102 :                         DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1;
   17959                 :             :                       }
   17960                 :         102 :                     fit_decomposition_lang_decl (decomp_d.decl, d);
   17961                 :         102 :                     decomp_d.decl = DECL_CHAIN (decomp_d.decl);
   17962                 :             :                   }
   17963                 :             :             }
   17964                 :             :         }
   17965                 :          76 :       decl = tsubst_decl (decl, args, complain);
   17966                 :          76 :       if (!decomp)
   17967                 :          39 :         maybe_push_decl (decl);
   17968                 :             :     }
   17969                 :        1222 :   else if (init && TREE_CODE (init) == DECL_EXPR)
   17970                 :             :     {
   17971                 :             :       /* We need to jump through some hoops to handle declarations in the
   17972                 :             :          init-statement, since we might need to handle auto deduction,
   17973                 :             :          but we need to keep control of initialization.  */
   17974                 :         274 :       decl_expr = init;
   17975                 :         274 :       init = DECL_INITIAL (DECL_EXPR_DECL (init));
   17976                 :         274 :       decl = tsubst_decl (decl, args, complain);
   17977                 :             :     }
   17978                 :             :   else
   17979                 :             :     {
   17980                 :         948 :       if (TREE_CODE (decl) == SCOPE_REF)
   17981                 :             :         {
   17982                 :           8 :           decl = RECUR (decl);
   17983                 :           8 :           if (TREE_CODE (decl) == COMPONENT_REF)
   17984                 :             :             {
   17985                 :             :               tree v = decl;
   17986                 :          24 :               while (v)
   17987                 :          24 :                 switch (TREE_CODE (v))
   17988                 :             :                   {
   17989                 :          20 :                   case COMPONENT_REF:
   17990                 :          20 :                   case MEM_REF:
   17991                 :          20 :                   case INDIRECT_REF:
   17992                 :          20 :                   CASE_CONVERT:
   17993                 :          20 :                   case POINTER_PLUS_EXPR:
   17994                 :          20 :                     v = TREE_OPERAND (v, 0);
   17995                 :          20 :                     continue;
   17996                 :           4 :                   case PARM_DECL:
   17997                 :           4 :                     if (DECL_CONTEXT (v) == current_function_decl
   17998                 :           4 :                         && DECL_ARTIFICIAL (v)
   17999                 :           8 :                         && DECL_NAME (v) == this_identifier)
   18000                 :             :                       {
   18001                 :           4 :                         decl = TREE_OPERAND (decl, 1);
   18002                 :           4 :                         decl = omp_privatize_field (decl, false);
   18003                 :             :                       }
   18004                 :             :                     /* FALLTHRU */
   18005                 :             :                   default:
   18006                 :             :                     v = NULL_TREE;
   18007                 :             :                     break;
   18008                 :             :                   }
   18009                 :             :             }
   18010                 :             :         }
   18011                 :             :       else
   18012                 :         940 :         decl = RECUR (decl);
   18013                 :             :     }
   18014                 :        1298 :   if (init && TREE_CODE (init) == TREE_VEC)
   18015                 :             :     {
   18016                 :          16 :       init = copy_node (init);
   18017                 :          16 :       TREE_VEC_ELT (init, 0)
   18018                 :          16 :         = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
   18019                 :          16 :       TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
   18020                 :          16 :       TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
   18021                 :             :     }
   18022                 :             :   else
   18023                 :        1282 :     init = RECUR (init);
   18024                 :             : 
   18025                 :        1298 :   if (orig_declv && OMP_FOR_ORIG_DECLS (t))
   18026                 :             :     {
   18027                 :         698 :       tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
   18028                 :         698 :       if (TREE_CODE (o) == TREE_LIST)
   18029                 :          97 :         TREE_VEC_ELT (orig_declv, i)
   18030                 :         194 :           = tree_cons (RECUR (TREE_PURPOSE (o)),
   18031                 :          97 :                        RECUR (TREE_VALUE (o)),
   18032                 :             :                        NULL_TREE);
   18033                 :             :       else
   18034                 :         601 :         TREE_VEC_ELT (orig_declv, i) = RECUR (o);
   18035                 :             :     }
   18036                 :             : 
   18037                 :        1298 :   if (range_for)
   18038                 :             :     {
   18039                 :          76 :       tree this_pre_body = NULL_TREE;
   18040                 :          76 :       tree orig_init = NULL_TREE;
   18041                 :          76 :       tree orig_decl = NULL_TREE;
   18042                 :          76 :       tree init_sl = NULL_TREE;
   18043                 :          76 :       cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
   18044                 :             :                                 orig_init, cond, incr);
   18045                 :          76 :       if (orig_decl)
   18046                 :             :         {
   18047                 :          71 :           if (orig_declv == NULL_TREE)
   18048                 :          64 :             orig_declv = copy_node (declv);
   18049                 :          71 :           TREE_VEC_ELT (orig_declv, i) = orig_decl;
   18050                 :          71 :           ret = true;
   18051                 :             :         }
   18052                 :           5 :       else if (orig_declv)
   18053                 :           0 :         TREE_VEC_ELT (orig_declv, i) = decl;
   18054                 :             :     }
   18055                 :             : 
   18056                 :        1298 :   tree auto_node = type_uses_auto (TREE_TYPE (decl));
   18057                 :        1298 :   if (!range_for && auto_node && init)
   18058                 :           2 :     TREE_TYPE (decl)
   18059                 :           4 :       = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
   18060                 :             : 
   18061                 :        1298 :   gcc_assert (!type_dependent_expression_p (decl));
   18062                 :             : 
   18063                 :        1298 :   if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
   18064                 :             :     {
   18065                 :        1094 :       if (decl_expr)
   18066                 :             :         {
   18067                 :             :           /* Declare the variable, but don't let that initialize it.  */
   18068                 :         219 :           tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
   18069                 :         219 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
   18070                 :         219 :           RECUR (decl_expr);
   18071                 :         219 :           DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
   18072                 :             :         }
   18073                 :             : 
   18074                 :        1094 :       if (!range_for)
   18075                 :             :         {
   18076                 :        1018 :           cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   18077                 :        1018 :           if (COMPARISON_CLASS_P (cond)
   18078                 :        1018 :               && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
   18079                 :             :             {
   18080                 :          16 :               tree lhs = RECUR (TREE_OPERAND (cond, 0));
   18081                 :          16 :               tree rhs = copy_node (TREE_OPERAND (cond, 1));
   18082                 :          16 :               TREE_VEC_ELT (rhs, 0)
   18083                 :          16 :                 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
   18084                 :          16 :               TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
   18085                 :          16 :               TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
   18086                 :          16 :               cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
   18087                 :             :                              lhs, rhs);       
   18088                 :             :             }
   18089                 :             :           else
   18090                 :        1002 :             cond = RECUR (cond);
   18091                 :        1018 :           incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   18092                 :        1018 :           if (TREE_CODE (incr) == MODIFY_EXPR)
   18093                 :             :             {
   18094                 :         238 :               tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18095                 :         238 :               tree rhs = RECUR (TREE_OPERAND (incr, 1));
   18096                 :         238 :               incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
   18097                 :             :                                           NOP_EXPR, rhs, NULL_TREE, complain);
   18098                 :             :             }
   18099                 :             :           else
   18100                 :         780 :             incr = RECUR (incr);
   18101                 :        1018 :           if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   18102                 :          12 :             TREE_VEC_ELT (orig_declv, i) = decl;
   18103                 :             :         }
   18104                 :        1094 :       TREE_VEC_ELT (declv, i) = decl;
   18105                 :        1094 :       TREE_VEC_ELT (initv, i) = init;
   18106                 :        1094 :       TREE_VEC_ELT (condv, i) = cond;
   18107                 :        1094 :       TREE_VEC_ELT (incrv, i) = incr;
   18108                 :        1094 :       return ret;
   18109                 :             :     }
   18110                 :             : 
   18111                 :         204 :   if (decl_expr)
   18112                 :             :     {
   18113                 :             :       /* Declare and initialize the variable.  */
   18114                 :          55 :       RECUR (decl_expr);
   18115                 :          55 :       init = NULL_TREE;
   18116                 :             :     }
   18117                 :         149 :   else if (init)
   18118                 :             :     {
   18119                 :          62 :       tree *pc;
   18120                 :          62 :       int j;
   18121                 :          50 :       for (j = ((omp_parallel_combined_clauses == NULL
   18122                 :         174 :                 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
   18123                 :             :         {
   18124                 :         216 :           for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
   18125                 :             :             {
   18126                 :          82 :               if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
   18127                 :          82 :                   && OMP_CLAUSE_DECL (*pc) == decl)
   18128                 :             :                 break;
   18129                 :          78 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
   18130                 :          78 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   18131                 :             :                 {
   18132                 :          31 :                   if (j)
   18133                 :             :                     break;
   18134                 :             :                   /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
   18135                 :           1 :                   tree c = *pc;
   18136                 :           1 :                   *pc = OMP_CLAUSE_CHAIN (c);
   18137                 :           1 :                   OMP_CLAUSE_CHAIN (c) = *clauses;
   18138                 :           1 :                   *clauses = c;
   18139                 :             :                 }
   18140                 :          47 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
   18141                 :          47 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   18142                 :             :                 {
   18143                 :           0 :                   error ("iteration variable %qD should not be firstprivate",
   18144                 :             :                          decl);
   18145                 :           0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   18146                 :             :                 }
   18147                 :          47 :               else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
   18148                 :          47 :                        && OMP_CLAUSE_DECL (*pc) == decl)
   18149                 :             :                 {
   18150                 :           0 :                   error ("iteration variable %qD should not be reduction",
   18151                 :             :                          decl);
   18152                 :           0 :                   *pc = OMP_CLAUSE_CHAIN (*pc);
   18153                 :             :                 }
   18154                 :             :               else
   18155                 :          47 :                 pc = &OMP_CLAUSE_CHAIN (*pc);
   18156                 :             :             }
   18157                 :          84 :           if (*pc)
   18158                 :             :             break;
   18159                 :             :         }
   18160                 :          62 :       if (*pc == NULL_TREE)
   18161                 :             :         {
   18162                 :          28 :           tree c = build_omp_clause (input_location,
   18163                 :          28 :                                      TREE_CODE (t) == OMP_LOOP
   18164                 :             :                                      ? OMP_CLAUSE_LASTPRIVATE
   18165                 :             :                                      : OMP_CLAUSE_PRIVATE);
   18166                 :          28 :           OMP_CLAUSE_DECL (c) = decl;
   18167                 :          28 :           c = finish_omp_clauses (c, C_ORT_OMP);
   18168                 :          28 :           if (c)
   18169                 :             :             {
   18170                 :          28 :               OMP_CLAUSE_CHAIN (c) = *clauses;
   18171                 :          28 :               *clauses = c;
   18172                 :             :             }
   18173                 :             :         }
   18174                 :             :     }
   18175                 :         204 :   cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
   18176                 :         204 :   if (COMPARISON_CLASS_P (cond))
   18177                 :             :     {
   18178                 :         204 :       tree op0 = RECUR (TREE_OPERAND (cond, 0));
   18179                 :         204 :       tree op1 = RECUR (TREE_OPERAND (cond, 1));
   18180                 :         204 :       cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
   18181                 :             :     }
   18182                 :             :   else
   18183                 :           0 :     cond = RECUR (cond);
   18184                 :         204 :   incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
   18185                 :         204 :   switch (TREE_CODE (incr))
   18186                 :             :     {
   18187                 :          94 :     case PREINCREMENT_EXPR:
   18188                 :          94 :     case PREDECREMENT_EXPR:
   18189                 :          94 :     case POSTINCREMENT_EXPR:
   18190                 :          94 :     case POSTDECREMENT_EXPR:
   18191                 :          94 :       incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
   18192                 :          94 :                      RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
   18193                 :          94 :       break;
   18194                 :         110 :     case MODIFY_EXPR:
   18195                 :         110 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   18196                 :         110 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   18197                 :             :         {
   18198                 :         110 :           tree rhs = TREE_OPERAND (incr, 1);
   18199                 :         110 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18200                 :         110 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   18201                 :         110 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   18202                 :         110 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   18203                 :         110 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   18204                 :             :                                  rhs0, rhs1));
   18205                 :             :         }
   18206                 :             :       else
   18207                 :           0 :         incr = RECUR (incr);
   18208                 :             :       break;
   18209                 :           0 :     case MODOP_EXPR:
   18210                 :           0 :       if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
   18211                 :           0 :           || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
   18212                 :             :         {
   18213                 :           0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18214                 :           0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   18215                 :           0 :                          build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
   18216                 :           0 :                                  TREE_TYPE (decl), lhs,
   18217                 :           0 :                                  RECUR (TREE_OPERAND (incr, 2))));
   18218                 :             :         }
   18219                 :           0 :       else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
   18220                 :           0 :                && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
   18221                 :           0 :                    || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
   18222                 :             :         {
   18223                 :           0 :           tree rhs = TREE_OPERAND (incr, 2);
   18224                 :           0 :           tree lhs = RECUR (TREE_OPERAND (incr, 0));
   18225                 :           0 :           tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
   18226                 :           0 :           tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
   18227                 :           0 :           incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
   18228                 :           0 :                          build2 (TREE_CODE (rhs), TREE_TYPE (decl),
   18229                 :             :                                  rhs0, rhs1));
   18230                 :             :         }
   18231                 :             :       else
   18232                 :           0 :         incr = RECUR (incr);
   18233                 :             :       break;
   18234                 :           0 :     default:
   18235                 :           0 :       incr = RECUR (incr);
   18236                 :           0 :       break;
   18237                 :             :     }
   18238                 :             : 
   18239                 :         204 :   if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
   18240                 :           5 :     TREE_VEC_ELT (orig_declv, i) = decl;
   18241                 :         204 :   TREE_VEC_ELT (declv, i) = decl;
   18242                 :         204 :   TREE_VEC_ELT (initv, i) = init;
   18243                 :         204 :   TREE_VEC_ELT (condv, i) = cond;
   18244                 :         204 :   TREE_VEC_ELT (incrv, i) = incr;
   18245                 :         204 :   return false;
   18246                 :             : #undef RECUR
   18247                 :             : }
   18248                 :             : 
   18249                 :             : /* Helper function of tsubst_expr, find OMP_TEAMS inside
   18250                 :             :    of OMP_TARGET's body.  */
   18251                 :             : 
   18252                 :             : static tree
   18253                 :         216 : tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
   18254                 :             : {
   18255                 :         216 :   *walk_subtrees = 0;
   18256                 :         216 :   switch (TREE_CODE (*tp))
   18257                 :             :     {
   18258                 :             :     case OMP_TEAMS:
   18259                 :             :       return *tp;
   18260                 :         108 :     case BIND_EXPR:
   18261                 :         108 :     case STATEMENT_LIST:
   18262                 :         108 :       *walk_subtrees = 1;
   18263                 :         108 :       break;
   18264                 :             :     default:
   18265                 :             :       break;
   18266                 :             :     }
   18267                 :             :   return NULL_TREE;
   18268                 :             : }
   18269                 :             : 
   18270                 :             : /* Helper function for tsubst_expr.  For decomposition declaration
   18271                 :             :    artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
   18272                 :             :    also the corresponding decls representing the identifiers
   18273                 :             :    of the decomposition declaration.  Return DECL if successful
   18274                 :             :    or error_mark_node otherwise, set *FIRST to the first decl
   18275                 :             :    in the list chained through DECL_CHAIN and *CNT to the number
   18276                 :             :    of such decls.  */
   18277                 :             : 
   18278                 :             : static tree
   18279                 :       22531 : tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
   18280                 :             :                      tsubst_flags_t complain, tree in_decl, cp_decomp *decomp)
   18281                 :             : {
   18282                 :       22531 :   tree decl2, decl3, prev = decl;
   18283                 :       22531 :   decomp->count = 0;
   18284                 :       22531 :   gcc_assert (DECL_NAME (decl) == NULL_TREE);
   18285                 :       22531 :   for (decl2 = DECL_CHAIN (pattern_decl);
   18286                 :             :        decl2
   18287                 :       49208 :        && VAR_P (decl2)
   18288                 :       49202 :        && DECL_DECOMPOSITION_P (decl2)
   18289                 :      112799 :        && DECL_NAME (decl2);
   18290                 :       45109 :        decl2 = DECL_CHAIN (decl2))
   18291                 :             :     {
   18292                 :       45112 :       if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0)
   18293                 :             :         {
   18294                 :           3 :           gcc_assert (errorcount);
   18295                 :             :           return error_mark_node;
   18296                 :             :         }
   18297                 :       45109 :       decomp->count++;
   18298                 :       45109 :       gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
   18299                 :       45109 :       gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
   18300                 :       45109 :       tree v = DECL_VALUE_EXPR (decl2);
   18301                 :       45109 :       DECL_HAS_VALUE_EXPR_P (decl2) = 0;
   18302                 :       45109 :       SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
   18303                 :       45109 :       decl3 = tsubst (decl2, args, complain, in_decl);
   18304                 :       45109 :       SET_DECL_VALUE_EXPR (decl2, v);
   18305                 :       45109 :       DECL_HAS_VALUE_EXPR_P (decl2) = 1;
   18306                 :       45109 :       if (VAR_P (decl3))
   18307                 :       45109 :         DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
   18308                 :             :       else
   18309                 :             :         {
   18310                 :           0 :           gcc_assert (errorcount);
   18311                 :           0 :           decl = error_mark_node;
   18312                 :           0 :           continue;
   18313                 :             :         }
   18314                 :       45109 :       maybe_push_decl (decl3);
   18315                 :       45109 :       if (error_operand_p (decl3))
   18316                 :           0 :         decl = error_mark_node;
   18317                 :       45109 :       else if (decl != error_mark_node
   18318                 :       45109 :                && DECL_CHAIN (decl3) != prev
   18319                 :       45109 :                && decl != prev)
   18320                 :             :         {
   18321                 :           0 :           gcc_assert (errorcount);
   18322                 :             :           decl = error_mark_node;
   18323                 :             :         }
   18324                 :             :       else
   18325                 :             :         prev = decl3;
   18326                 :             :     }
   18327                 :       22528 :   decomp->decl = prev;
   18328                 :       22528 :   return decl;
   18329                 :             : }
   18330                 :             : 
   18331                 :             : /* Return the proper local_specialization for init-capture pack DECL.  */
   18332                 :             : 
   18333                 :             : static tree
   18334                 :          12 : lookup_init_capture_pack (tree decl)
   18335                 :             : {
   18336                 :             :   /* We handle normal pack captures by forwarding to the specialization of the
   18337                 :             :      captured parameter.  We can't do that for pack init-captures; we need them
   18338                 :             :      to have their own local_specialization.  We created the individual
   18339                 :             :      VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
   18340                 :             :      when we process the DECL_EXPR for the pack init-capture in the template.
   18341                 :             :      So, how do we find them?  We don't know the capture proxy pack when
   18342                 :             :      building the individual resulting proxies, and we don't know the
   18343                 :             :      individual proxies when instantiating the pack.  What we have in common is
   18344                 :             :      the FIELD_DECL.
   18345                 :             : 
   18346                 :             :      So...when we instantiate the FIELD_DECL, we stick the result in
   18347                 :             :      local_specializations.  Then at the DECL_EXPR we look up that result, see
   18348                 :             :      how many elements it has, synthesize the names, and look them up.  */
   18349                 :             : 
   18350                 :          12 :   tree cname = DECL_NAME (decl);
   18351                 :          12 :   tree val = DECL_VALUE_EXPR (decl);
   18352                 :          12 :   tree field = TREE_OPERAND (val, 1);
   18353                 :          12 :   gcc_assert (TREE_CODE (field) == FIELD_DECL);
   18354                 :          12 :   tree fpack = retrieve_local_specialization (field);
   18355                 :          12 :   if (fpack == error_mark_node)
   18356                 :             :     return error_mark_node;
   18357                 :             : 
   18358                 :          12 :   int len = 1;
   18359                 :          12 :   tree vec = NULL_TREE;
   18360                 :          12 :   tree r = NULL_TREE;
   18361                 :          12 :   if (TREE_CODE (fpack) == TREE_VEC)
   18362                 :             :     {
   18363                 :          11 :       len = TREE_VEC_LENGTH (fpack);
   18364                 :          11 :       vec = make_tree_vec (len);
   18365                 :          11 :       r = make_node (NONTYPE_ARGUMENT_PACK);
   18366                 :          11 :       ARGUMENT_PACK_ARGS (r) = vec;
   18367                 :             :     }
   18368                 :          28 :   for (int i = 0; i < len; ++i)
   18369                 :             :     {
   18370                 :          16 :       tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
   18371                 :          16 :       tree elt = lookup_name (ename);
   18372                 :          16 :       if (vec)
   18373                 :          15 :         TREE_VEC_ELT (vec, i) = elt;
   18374                 :             :       else
   18375                 :             :         r = elt;
   18376                 :             :     }
   18377                 :             :   return r;
   18378                 :             : }
   18379                 :             : 
   18380                 :             : /* T is an operand of a template tree being substituted.  Return whether
   18381                 :             :    T is dependent such that we should suppress some warnings that would
   18382                 :             :    make sense if the substituted expression were written directly, like
   18383                 :             :      template <int I> bool f() { return I == 2; }
   18384                 :             :    We don't want to warn when instantiating f that comparing two constants
   18385                 :             :    always has the same value.
   18386                 :             : 
   18387                 :             :    This is a more limited concept of dependence than instantiation-dependent;
   18388                 :             :    here we don't care whether substitution could fail.  */
   18389                 :             : 
   18390                 :             : static bool
   18391                 :    88056711 : dependent_operand_p (tree t)
   18392                 :             : {
   18393                 :    88468871 :   while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
   18394                 :      412160 :     t = TREE_OPERAND (t, 0);
   18395                 :    88056711 :   ++processing_template_decl;
   18396                 :    88056711 :   bool r = (potential_constant_expression (t)
   18397                 :    88056711 :             ? value_dependent_expression_p (t)
   18398                 :     4733947 :             : type_dependent_expression_p (t));
   18399                 :    88056711 :   --processing_template_decl;
   18400                 :    88056711 :   return r;
   18401                 :             : }
   18402                 :             : 
   18403                 :             : /* A superset of tsubst_expr that also handles statement trees.  */
   18404                 :             : 
   18405                 :             : static tree
   18406                 :   274437221 : tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   18407                 :             : {
   18408                 :             : #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
   18409                 :             : #define RECUR(NODE)                             \
   18410                 :             :   tsubst_stmt ((NODE), args, complain, in_decl)
   18411                 :             : 
   18412                 :   274437221 :   tree stmt, tmp;
   18413                 :   274437221 :   tree r;
   18414                 :   274437221 :   location_t loc;
   18415                 :             : 
   18416                 :   274437221 :   if (t == NULL_TREE || t == error_mark_node)
   18417                 :             :     return t;
   18418                 :             : 
   18419                 :   272287721 :   loc = input_location;
   18420                 :   272287721 :   if (location_t eloc = cp_expr_location (t))
   18421                 :   236289851 :     input_location = eloc;
   18422                 :   272287721 :   if (STATEMENT_CODE_P (TREE_CODE (t)))
   18423                 :    39878205 :     current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
   18424                 :             : 
   18425                 :   272287721 :   switch (TREE_CODE (t))
   18426                 :             :     {
   18427                 :    33396665 :     case STATEMENT_LIST:
   18428                 :    33396665 :       {
   18429                 :   179101687 :         for (tree stmt : tsi_range (t))
   18430                 :   145705026 :           RECUR (stmt);
   18431                 :             :         break;
   18432                 :             :       }
   18433                 :             : 
   18434                 :     2291688 :     case CTOR_INITIALIZER:
   18435                 :     2291688 :       finish_mem_initializers (tsubst_initializer_list
   18436                 :     2291688 :                                (TREE_OPERAND (t, 0), args));
   18437                 :     2291688 :       break;
   18438                 :             : 
   18439                 :    14925307 :     case RETURN_EXPR:
   18440                 :    14925307 :       finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
   18441                 :    14925304 :       break;
   18442                 :             : 
   18443                 :         145 :     case CO_RETURN_EXPR:
   18444                 :         145 :       finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
   18445                 :         145 :       break;
   18446                 :             : 
   18447                 :    21315646 :     case EXPR_STMT:
   18448                 :    21315646 :       tmp = RECUR (EXPR_STMT_EXPR (t));
   18449                 :    21315642 :       if (EXPR_STMT_STMT_EXPR_RESULT (t))
   18450                 :         160 :         finish_stmt_expr_expr (tmp, cur_stmt_expr);
   18451                 :             :       else
   18452                 :    21315482 :         finish_expr_stmt (tmp);
   18453                 :             :       break;
   18454                 :             : 
   18455                 :       21529 :     case USING_STMT:
   18456                 :       21529 :       finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
   18457                 :       21529 :       break;
   18458                 :             : 
   18459                 :           0 :     case PRECONDITION_STMT:
   18460                 :           0 :     case POSTCONDITION_STMT:
   18461                 :           0 :       gcc_unreachable ();
   18462                 :             : 
   18463                 :          21 :     case ASSERTION_STMT:
   18464                 :          21 :       {
   18465                 :          21 :         r = tsubst_contract (NULL_TREE, t, args, complain, in_decl);
   18466                 :          21 :         if (r != error_mark_node)
   18467                 :          21 :           add_stmt (r);
   18468                 :          21 :         RETURN (r);
   18469                 :             :       }
   18470                 :             :       break;
   18471                 :             : 
   18472                 :    20479132 :     case DECL_EXPR:
   18473                 :    20479132 :       {
   18474                 :    20479132 :         tree decl, pattern_decl;
   18475                 :    20479132 :         tree init;
   18476                 :             : 
   18477                 :    20479132 :         pattern_decl = decl = DECL_EXPR_DECL (t);
   18478                 :    20479132 :         if (TREE_CODE (decl) == LABEL_DECL)
   18479                 :          52 :           finish_label_decl (DECL_NAME (decl));
   18480                 :    20479080 :         else if (TREE_CODE (decl) == USING_DECL)
   18481                 :             :           {
   18482                 :      124530 :             tree scope = USING_DECL_SCOPE (decl);
   18483                 :      124530 :             if (DECL_DEPENDENT_P (decl))
   18484                 :             :               {
   18485                 :           3 :                 scope = tsubst (scope, args, complain, in_decl);
   18486                 :           3 :                 if (!MAYBE_CLASS_TYPE_P (scope)
   18487                 :           2 :                     && TREE_CODE (scope) != ENUMERAL_TYPE)
   18488                 :             :                   {
   18489                 :           1 :                     if (complain & tf_error)
   18490                 :           1 :                       error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
   18491                 :             :                                 "class, namespace, or enumeration", scope);
   18492                 :           1 :                     return error_mark_node;
   18493                 :             :                   }
   18494                 :           2 :                 finish_nonmember_using_decl (scope, DECL_NAME (decl));
   18495                 :             :               }
   18496                 :             :             else
   18497                 :             :               {
   18498                 :             :                 /* This is a non-dependent using-decl, and we'll have
   18499                 :             :                    used the names it found during template parsing.  We do
   18500                 :             :                    not want to do the lookup again, because we might not
   18501                 :             :                    find the things we found then.  */
   18502                 :      124527 :                 gcc_checking_assert (scope == tsubst (scope, args,
   18503                 :             :                                                       complain, in_decl));
   18504                 :             :                 /* We still need to push the bindings so that we can look up
   18505                 :             :                    this name later.  */
   18506                 :      249054 :                 push_using_decl_bindings (DECL_NAME (decl),
   18507                 :      124527 :                                           USING_DECL_DECLS (decl));
   18508                 :             :               }
   18509                 :             :           }
   18510                 :    20354550 :         else if (is_capture_proxy (decl)
   18511                 :    20354550 :                  && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
   18512                 :             :           {
   18513                 :             :             /* We're in tsubst_lambda_expr, we've already inserted a new
   18514                 :             :                capture proxy, so look it up and register it.  */
   18515                 :      254395 :             tree inst;
   18516                 :      254395 :             if (!DECL_PACK_P (decl))
   18517                 :             :               {
   18518                 :      254337 :                 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
   18519                 :             :                                     LOOK_want::HIDDEN_LAMBDA);
   18520                 :      254337 :                 gcc_assert (inst != decl && is_capture_proxy (inst));
   18521                 :             :               }
   18522                 :          58 :             else if (is_normal_capture_proxy (decl))
   18523                 :             :               {
   18524                 :          46 :                 inst = (retrieve_local_specialization
   18525                 :          46 :                         (DECL_CAPTURED_VARIABLE (decl)));
   18526                 :          46 :                 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
   18527                 :             :                             || DECL_PACK_P (inst));
   18528                 :             :               }
   18529                 :             :             else
   18530                 :          12 :               inst = lookup_init_capture_pack (decl);
   18531                 :             : 
   18532                 :      254395 :             register_local_specialization (inst, decl);
   18533                 :      254395 :             break;
   18534                 :             :           }
   18535                 :    20100155 :         else if (DECL_PRETTY_FUNCTION_P (decl))
   18536                 :        7247 :           decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
   18537                 :        7247 :                                   DECL_NAME (decl),
   18538                 :             :                                   true/*DECL_PRETTY_FUNCTION_P (decl)*/);
   18539                 :     1945652 :         else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   18540                 :    21406377 :                  && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   18541                 :             :           /* Don't copy the old closure; we'll create a new one in
   18542                 :             :              tsubst_lambda_expr.  */
   18543                 :             :           break;
   18544                 :             :         else
   18545                 :             :           {
   18546                 :    19954798 :             init = DECL_INITIAL (decl);
   18547                 :    19954798 :             decl = tsubst (decl, args, complain, in_decl);
   18548                 :    19954794 :             if (decl != error_mark_node)
   18549                 :             :               {
   18550                 :             :                 /* By marking the declaration as instantiated, we avoid
   18551                 :             :                    trying to instantiate it.  Since instantiate_decl can't
   18552                 :             :                    handle local variables, and since we've already done
   18553                 :             :                    all that needs to be done, that's the right thing to
   18554                 :             :                    do.  */
   18555                 :    19954782 :                 if (VAR_P (decl))
   18556                 :    18146923 :                   DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   18557                 :    18146923 :                 if (VAR_P (decl) && !DECL_NAME (decl)
   18558                 :    19977682 :                     && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   18559                 :             :                   /* Anonymous aggregates are a special case.  */
   18560                 :          37 :                   finish_anon_union (decl);
   18561                 :    19954745 :                 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
   18562                 :             :                   {
   18563                 :      147731 :                     DECL_CONTEXT (decl) = current_function_decl;
   18564                 :      147731 :                     if (DECL_NAME (decl) == this_identifier)
   18565                 :             :                       {
   18566                 :       68637 :                         tree lam = DECL_CONTEXT (current_function_decl);
   18567                 :       68637 :                         lam = CLASSTYPE_LAMBDA_EXPR (lam);
   18568                 :       68637 :                         LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
   18569                 :             :                       }
   18570                 :      147731 :                     insert_capture_proxy (decl);
   18571                 :             :                   }
   18572                 :    19807014 :                 else if (DECL_IMPLICIT_TYPEDEF_P (t))
   18573                 :             :                   /* We already did a pushtag.  */;
   18574                 :     1807859 :                 else if (VAR_OR_FUNCTION_DECL_P (decl)
   18575                 :    19807339 :                          && DECL_LOCAL_DECL_P (decl))
   18576                 :             :                   {
   18577                 :         428 :                     if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   18578                 :         170 :                       DECL_CONTEXT (decl) = NULL_TREE;
   18579                 :         428 :                     decl = pushdecl (decl);
   18580                 :         428 :                     if (TREE_CODE (decl) == FUNCTION_DECL
   18581                 :         313 :                         && DECL_OMP_DECLARE_REDUCTION_P (decl)
   18582                 :         586 :                         && cp_check_omp_declare_reduction (decl))
   18583                 :         110 :                       instantiate_body (pattern_decl, args, decl, true);
   18584                 :             :                   }
   18585                 :             :                 else
   18586                 :             :                   {
   18587                 :    19806586 :                     bool const_init = false;
   18588                 :    19806586 :                     cp_decomp decomp_d, *decomp = NULL;
   18589                 :    19806586 :                     tree ndecl = error_mark_node;
   18590                 :    19806586 :                     tree asmspec_tree = NULL_TREE;
   18591                 :    19806586 :                     maybe_push_decl (decl);
   18592                 :             : 
   18593                 :    19806586 :                     if (VAR_P (decl)
   18594                 :    17999052 :                         && DECL_LANG_SPECIFIC (decl)
   18595                 :    20269499 :                         && DECL_OMP_PRIVATIZED_MEMBER (decl))
   18596                 :             :                       break;
   18597                 :             : 
   18598                 :    19806388 :                     if (VAR_P (decl)
   18599                 :    17998854 :                         && DECL_DECOMPOSITION_P (decl)
   18600                 :    19828821 :                         && TREE_TYPE (pattern_decl) != error_mark_node)
   18601                 :             :                       {
   18602                 :       22433 :                         decomp = &decomp_d;
   18603                 :       22433 :                         ndecl = tsubst_decomp_names (decl, pattern_decl, args,
   18604                 :             :                                                      complain, in_decl, decomp);
   18605                 :             :                       }
   18606                 :             : 
   18607                 :    19806388 :                     init = tsubst_init (init, decl, args, complain, in_decl);
   18608                 :             : 
   18609                 :    19806388 :                     if (VAR_P (decl))
   18610                 :    17998854 :                       const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
   18611                 :             :                                     (pattern_decl));
   18612                 :             : 
   18613                 :             :                     /* In a non-template function, VLA type declarations are
   18614                 :             :                        handled in grokdeclarator; for templates, handle them
   18615                 :             :                        now.  */
   18616                 :    19806388 :                     predeclare_vla (decl);
   18617                 :             : 
   18618                 :    37805242 :                     if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
   18619                 :             :                       {
   18620                 :           4 :                         tree id = DECL_ASSEMBLER_NAME (pattern_decl);
   18621                 :           4 :                         const char *asmspec = IDENTIFIER_POINTER (id);
   18622                 :           4 :                         gcc_assert (asmspec[0] == '*');
   18623                 :           4 :                         asmspec_tree
   18624                 :           4 :                           = build_string (IDENTIFIER_LENGTH (id) - 1,
   18625                 :             :                                           asmspec + 1);
   18626                 :           4 :                         TREE_TYPE (asmspec_tree) = char_array_type_node;
   18627                 :             :                       }
   18628                 :             : 
   18629                 :    19806388 :                     cp_finish_decl (decl, init, const_init, asmspec_tree, 0,
   18630                 :             :                                     decomp);
   18631                 :             : 
   18632                 :    19806388 :                     if (ndecl != error_mark_node)
   18633                 :       22430 :                       cp_finish_decomp (ndecl, decomp);
   18634                 :             :                   }
   18635                 :             :               }
   18636                 :             :           }
   18637                 :             : 
   18638                 :             :         break;
   18639                 :             :       }
   18640                 :             : 
   18641                 :     1037468 :     case FOR_STMT:
   18642                 :     1037468 :       stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
   18643                 :     1037468 :       RECUR (FOR_INIT_STMT (t));
   18644                 :     1037468 :       finish_init_stmt (stmt);
   18645                 :     1037468 :       tmp = RECUR (FOR_COND (t));
   18646                 :     1037468 :       finish_for_cond (tmp, stmt, false, 0, false);
   18647                 :     1037468 :       tmp = RECUR (FOR_EXPR (t));
   18648                 :     1037468 :       finish_for_expr (tmp, stmt);
   18649                 :     1037468 :       {
   18650                 :     1037468 :         bool prev = note_iteration_stmt_body_start ();
   18651                 :     1037468 :         RECUR (FOR_BODY (t));
   18652                 :     1037468 :         note_iteration_stmt_body_end (prev);
   18653                 :             :       }
   18654                 :     1037468 :       finish_for_stmt (stmt);
   18655                 :     1037468 :       break;
   18656                 :             : 
   18657                 :       43468 :     case RANGE_FOR_STMT:
   18658                 :       43468 :       {
   18659                 :             :         /* Construct another range_for, if this is not a final
   18660                 :             :            substitution (for inside a generic lambda of a
   18661                 :             :            template).  Otherwise convert to a regular for.  */
   18662                 :       43468 :         tree decl, expr;
   18663                 :       86936 :         stmt = (processing_template_decl
   18664                 :       43468 :                 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
   18665                 :       43462 :                 : begin_for_stmt (NULL_TREE, NULL_TREE));
   18666                 :       43468 :         RECUR (RANGE_FOR_INIT_STMT (t));
   18667                 :       43468 :         decl = RANGE_FOR_DECL (t);
   18668                 :       43468 :         decl = tsubst (decl, args, complain, in_decl);
   18669                 :       43468 :         maybe_push_decl (decl);
   18670                 :       43468 :         expr = RECUR (RANGE_FOR_EXPR (t));
   18671                 :             : 
   18672                 :       43468 :         cp_decomp decomp_d, *decomp = NULL;
   18673                 :       43468 :         if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
   18674                 :             :           {
   18675                 :          61 :             decomp = &decomp_d;
   18676                 :          61 :             decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
   18677                 :             :                                         complain, in_decl, decomp);
   18678                 :             :           }
   18679                 :             : 
   18680                 :       43468 :         tree unroll = RECUR (RANGE_FOR_UNROLL (t));
   18681                 :       43468 :         if (unroll)
   18682                 :          33 :           unroll
   18683                 :          33 :             = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)),
   18684                 :             :                                       unroll);
   18685                 :       43468 :         if (processing_template_decl)
   18686                 :             :           {
   18687                 :           6 :             RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
   18688                 :           6 :             RANGE_FOR_UNROLL (stmt) = unroll;
   18689                 :           6 :             RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t);
   18690                 :           6 :             finish_range_for_decl (stmt, decl, expr);
   18691                 :           6 :             if (decomp && decl != error_mark_node)
   18692                 :           3 :               cp_finish_decomp (decl, decomp);
   18693                 :             :           }
   18694                 :             :         else
   18695                 :       86924 :           stmt = cp_convert_range_for (stmt, decl, expr, decomp,
   18696                 :       43462 :                                        RANGE_FOR_IVDEP (t), unroll,
   18697                 :       43462 :                                        RANGE_FOR_NOVECTOR (t));
   18698                 :             : 
   18699                 :       43468 :         bool prev = note_iteration_stmt_body_start ();
   18700                 :       43468 :         RECUR (RANGE_FOR_BODY (t));
   18701                 :       43468 :         note_iteration_stmt_body_end (prev);
   18702                 :       43468 :         finish_for_stmt (stmt);
   18703                 :             :       }
   18704                 :       43468 :       break;
   18705                 :             : 
   18706                 :      309657 :     case WHILE_STMT:
   18707                 :      309657 :       stmt = begin_while_stmt ();
   18708                 :      309657 :       tmp = RECUR (WHILE_COND (t));
   18709                 :      309657 :       finish_while_stmt_cond (tmp, stmt, false, 0, false);
   18710                 :      309657 :       {
   18711                 :      309657 :         bool prev = note_iteration_stmt_body_start ();
   18712                 :      309657 :         RECUR (WHILE_BODY (t));
   18713                 :      309657 :         note_iteration_stmt_body_end (prev);
   18714                 :             :       }
   18715                 :      309657 :       finish_while_stmt (stmt);
   18716                 :      309657 :       break;
   18717                 :             : 
   18718                 :      586617 :     case DO_STMT:
   18719                 :      586617 :       stmt = begin_do_stmt ();
   18720                 :      586617 :       {
   18721                 :      586617 :         bool prev = note_iteration_stmt_body_start ();
   18722                 :      586617 :         RECUR (DO_BODY (t));
   18723                 :      586617 :         note_iteration_stmt_body_end (prev);
   18724                 :             :       }
   18725                 :      586617 :       finish_do_body (stmt);
   18726                 :      586617 :       tmp = RECUR (DO_COND (t));
   18727                 :      586617 :       finish_do_stmt (tmp, stmt, false, 0, false);
   18728                 :      586617 :       break;
   18729                 :             : 
   18730                 :    12336038 :     case IF_STMT:
   18731                 :    12336038 :       stmt = begin_if_stmt ();
   18732                 :    12336038 :       IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
   18733                 :    12336038 :       IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
   18734                 :    12336038 :       if (IF_STMT_CONSTEXPR_P (t))
   18735                 :     2127834 :         args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl);
   18736                 :    12336038 :       {
   18737                 :    12336038 :         tree cond = IF_COND (t);
   18738                 :    12336038 :         bool was_dep = dependent_operand_p (cond);
   18739                 :    12336038 :         cond = RECUR (cond);
   18740                 :    12336038 :         warning_sentinel s1(warn_address, was_dep);
   18741                 :    12336038 :         tmp = finish_if_stmt_cond (cond, stmt);
   18742                 :    12336038 :       }
   18743                 :    12336038 :       if (IF_STMT_CONSTEXPR_P (t)
   18744                 :    12336038 :           && instantiation_dependent_expression_p (tmp))
   18745                 :             :         {
   18746                 :             :           /* We're partially instantiating a generic lambda, but the condition
   18747                 :             :              of the constexpr if is still dependent.  Don't substitute into the
   18748                 :             :              branches now, just remember the template arguments.  */
   18749                 :        6456 :           do_poplevel (IF_SCOPE (stmt));
   18750                 :        6456 :           IF_SCOPE (stmt) = NULL_TREE;
   18751                 :        6456 :           IF_COND (stmt) = IF_COND (t);
   18752                 :        6456 :           THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
   18753                 :        6456 :           ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
   18754                 :        6456 :           IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
   18755                 :        6456 :           add_stmt (stmt);
   18756                 :        6456 :           break;
   18757                 :             :         }
   18758                 :    12329582 :       if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
   18759                 :             :         /* Don't instantiate the THEN_CLAUSE. */;
   18760                 :    11070019 :       else if (IF_STMT_CONSTEVAL_P (t))
   18761                 :             :         {
   18762                 :          43 :           bool save_in_consteval_if_p = in_consteval_if_p;
   18763                 :          43 :           in_consteval_if_p = true;
   18764                 :          43 :           RECUR (THEN_CLAUSE (t));
   18765                 :          43 :           in_consteval_if_p = save_in_consteval_if_p;
   18766                 :             :         }
   18767                 :             :       else
   18768                 :             :         {
   18769                 :    11069976 :           tree folded = fold_non_dependent_expr (tmp, complain);
   18770                 :    11069976 :           bool inhibit = integer_zerop (folded);
   18771                 :    11069976 :           if (inhibit)
   18772                 :      205549 :             ++c_inhibit_evaluation_warnings;
   18773                 :    11069976 :           RECUR (THEN_CLAUSE (t));
   18774                 :    11069976 :           if (inhibit)
   18775                 :      205549 :             --c_inhibit_evaluation_warnings;
   18776                 :             :         }
   18777                 :    12329582 :       finish_then_clause (stmt);
   18778                 :             : 
   18779                 :    12329582 :       if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
   18780                 :             :         /* Don't instantiate the ELSE_CLAUSE. */;
   18781                 :    11467781 :       else if (ELSE_CLAUSE (t))
   18782                 :             :         {
   18783                 :     4811290 :           tree folded = fold_non_dependent_expr (tmp, complain);
   18784                 :     4811290 :           bool inhibit = integer_nonzerop (folded);
   18785                 :     4811290 :           begin_else_clause (stmt);
   18786                 :     4811290 :           if (inhibit)
   18787                 :       26027 :             ++c_inhibit_evaluation_warnings;
   18788                 :     4811290 :           RECUR (ELSE_CLAUSE (t));
   18789                 :     4811290 :           if (inhibit)
   18790                 :       26027 :             --c_inhibit_evaluation_warnings;
   18791                 :     4811290 :           finish_else_clause (stmt);
   18792                 :             :         }
   18793                 :             : 
   18794                 :    12329582 :       finish_if_stmt (stmt);
   18795                 :    12329582 :       break;
   18796                 :             : 
   18797                 :    34375358 :     case BIND_EXPR:
   18798                 :    34375358 :       if (BIND_EXPR_BODY_BLOCK (t))
   18799                 :     3208052 :         stmt = begin_function_body ();
   18800                 :             :       else
   18801                 :    62239085 :         stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
   18802                 :             :                                     ? BCS_TRY_BLOCK : 0);
   18803                 :             : 
   18804                 :    34375358 :       RECUR (BIND_EXPR_BODY (t));
   18805                 :             : 
   18806                 :    34375347 :       if (BIND_EXPR_BODY_BLOCK (t))
   18807                 :     3208052 :         finish_function_body (stmt);
   18808                 :             :       else
   18809                 :    31167295 :         finish_compound_stmt (stmt);
   18810                 :             :       break;
   18811                 :             : 
   18812                 :      982675 :     case BREAK_STMT:
   18813                 :      982675 :       finish_break_stmt ();
   18814                 :      982675 :       break;
   18815                 :             : 
   18816                 :        1393 :     case CONTINUE_STMT:
   18817                 :        1393 :       finish_continue_stmt ();
   18818                 :        1393 :       break;
   18819                 :             : 
   18820                 :      241294 :     case SWITCH_STMT:
   18821                 :      241294 :       stmt = begin_switch_stmt ();
   18822                 :      241294 :       tmp = RECUR (SWITCH_STMT_COND (t));
   18823                 :      241294 :       finish_switch_cond (tmp, stmt);
   18824                 :      241294 :       RECUR (SWITCH_STMT_BODY (t));
   18825                 :      241294 :       finish_switch_stmt (stmt);
   18826                 :      241294 :       break;
   18827                 :             : 
   18828                 :     1465978 :     case CASE_LABEL_EXPR:
   18829                 :     1465978 :       {
   18830                 :     1465978 :         tree decl = CASE_LABEL (t);
   18831                 :     1465978 :         tree low = RECUR (CASE_LOW (t));
   18832                 :     1465978 :         tree high = RECUR (CASE_HIGH (t));
   18833                 :     1465978 :         tree l = finish_case_label (EXPR_LOCATION (t), low, high);
   18834                 :     1465978 :         if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
   18835                 :             :           {
   18836                 :     1465968 :             tree label = CASE_LABEL (l);
   18837                 :     1465968 :             FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   18838                 :     1465968 :             if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   18839                 :           2 :               cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   18840                 :             :           }
   18841                 :             :       }
   18842                 :             :       break;
   18843                 :             : 
   18844                 :         248 :     case LABEL_EXPR:
   18845                 :         248 :       {
   18846                 :         248 :         tree decl = LABEL_EXPR_LABEL (t);
   18847                 :         248 :         tree label;
   18848                 :             : 
   18849                 :         248 :         label = finish_label_stmt (DECL_NAME (decl));
   18850                 :         248 :         if (TREE_CODE (label) == LABEL_DECL)
   18851                 :         248 :           FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
   18852                 :         248 :         if (DECL_ATTRIBUTES (decl) != NULL_TREE)
   18853                 :          18 :           cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
   18854                 :             :       }
   18855                 :         248 :       break;
   18856                 :             : 
   18857                 :         226 :     case GOTO_EXPR:
   18858                 :         226 :       tmp = GOTO_DESTINATION (t);
   18859                 :         226 :       if (TREE_CODE (tmp) != LABEL_DECL)
   18860                 :             :         /* Computed goto's must be tsubst'd into.  On the other hand,
   18861                 :             :            non-computed gotos must not be; the identifier in question
   18862                 :             :            will have no binding.  */
   18863                 :          28 :         tmp = RECUR (tmp);
   18864                 :             :       else
   18865                 :         198 :         tmp = DECL_NAME (tmp);
   18866                 :         226 :       finish_goto_stmt (tmp);
   18867                 :         226 :       break;
   18868                 :             : 
   18869                 :       15913 :     case ASM_EXPR:
   18870                 :       15913 :       {
   18871                 :       15913 :         tree string = RECUR (ASM_STRING (t));
   18872                 :       15913 :         tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
   18873                 :             :                                                  complain, in_decl);
   18874                 :       15913 :         tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
   18875                 :             :                                                 complain, in_decl);
   18876                 :       15913 :         tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
   18877                 :             :                                                   complain, in_decl);
   18878                 :       15913 :         tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
   18879                 :             :                                                 complain, in_decl);
   18880                 :       15913 :         tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
   18881                 :             :                                outputs, inputs, clobbers, labels,
   18882                 :       15913 :                                ASM_INLINE_P (t));
   18883                 :       15913 :         tree asm_expr = tmp;
   18884                 :       15913 :         if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
   18885                 :       15913 :           asm_expr = TREE_OPERAND (asm_expr, 0);
   18886                 :       15913 :         ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
   18887                 :             :       }
   18888                 :       15913 :       break;
   18889                 :             : 
   18890                 :       95527 :     case TRY_BLOCK:
   18891                 :       95527 :       if (CLEANUP_P (t))
   18892                 :             :         {
   18893                 :           0 :           stmt = begin_try_block ();
   18894                 :           0 :           RECUR (TRY_STMTS (t));
   18895                 :           0 :           finish_cleanup_try_block (stmt);
   18896                 :           0 :           finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
   18897                 :             :         }
   18898                 :             :       else
   18899                 :             :         {
   18900                 :       95527 :           tree compound_stmt = NULL_TREE;
   18901                 :             : 
   18902                 :       95527 :           if (FN_TRY_BLOCK_P (t))
   18903                 :          51 :             stmt = begin_function_try_block (&compound_stmt);
   18904                 :             :           else
   18905                 :       95476 :             stmt = begin_try_block ();
   18906                 :             : 
   18907                 :       95527 :           RECUR (TRY_STMTS (t));
   18908                 :             : 
   18909                 :       95527 :           if (FN_TRY_BLOCK_P (t))
   18910                 :          51 :             finish_function_try_block (stmt);
   18911                 :             :           else
   18912                 :       95476 :             finish_try_block (stmt);
   18913                 :             : 
   18914                 :       95527 :           RECUR (TRY_HANDLERS (t));
   18915                 :       95527 :           if (FN_TRY_BLOCK_P (t))
   18916                 :          51 :             finish_function_handler_sequence (stmt, compound_stmt);
   18917                 :             :           else
   18918                 :       95476 :             finish_handler_sequence (stmt);
   18919                 :             :         }
   18920                 :             :       break;
   18921                 :             : 
   18922                 :       96289 :     case HANDLER:
   18923                 :       96289 :       {
   18924                 :       96289 :         tree decl = HANDLER_PARMS (t);
   18925                 :             : 
   18926                 :       96289 :         if (decl)
   18927                 :             :           {
   18928                 :        1783 :             decl = tsubst (decl, args, complain, in_decl);
   18929                 :             :             /* Prevent instantiate_decl from trying to instantiate
   18930                 :             :                this variable.  We've already done all that needs to be
   18931                 :             :                done.  */
   18932                 :        1783 :             if (decl != error_mark_node)
   18933                 :        1779 :               DECL_TEMPLATE_INSTANTIATED (decl) = 1;
   18934                 :             :           }
   18935                 :       96289 :         stmt = begin_handler ();
   18936                 :       96289 :         finish_handler_parms (decl, stmt);
   18937                 :       96289 :         RECUR (HANDLER_BODY (t));
   18938                 :       96289 :         finish_handler (stmt);
   18939                 :             :       }
   18940                 :       96289 :       break;
   18941                 :             : 
   18942                 :      518767 :     case TAG_DEFN:
   18943                 :      518767 :       tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
   18944                 :      518767 :       if (dependent_type_p (tmp))
   18945                 :             :         /* This is a partial instantiation, try again when full.  */
   18946                 :          21 :         add_stmt (build_min (TAG_DEFN, tmp));
   18947                 :      518746 :       else if (CLASS_TYPE_P (tmp))
   18948                 :             :         {
   18949                 :             :           /* Local classes are not independent templates; they are
   18950                 :             :              instantiated along with their containing function.  And this
   18951                 :             :              way we don't have to deal with pushing out of one local class
   18952                 :             :              to instantiate a member of another local class.  */
   18953                 :             :           /* Closures are handled by the LAMBDA_EXPR.  */
   18954                 :     1036883 :           gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
   18955                 :      518485 :           complete_type (tmp);
   18956                 :      518485 :           tree save_ccp = current_class_ptr;
   18957                 :      518485 :           tree save_ccr = current_class_ref;
   18958                 :     3610623 :           for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
   18959                 :     3092138 :             if ((VAR_P (fld)
   18960                 :     3092134 :                  || (TREE_CODE (fld) == FUNCTION_DECL
   18961                 :     2133591 :                      && !DECL_ARTIFICIAL (fld)))
   18962                 :     5225729 :                 && DECL_TEMPLATE_INSTANTIATION (fld))
   18963                 :     2133595 :               instantiate_decl (fld, /*defer_ok=*/false,
   18964                 :             :                                 /*expl_inst_class=*/false);
   18965                 :      958543 :             else if (TREE_CODE (fld) == FIELD_DECL)
   18966                 :      439864 :               maybe_instantiate_nsdmi_init (fld, tf_warning_or_error);
   18967                 :      518485 :           current_class_ptr = save_ccp;
   18968                 :      518485 :           current_class_ref = save_ccr;
   18969                 :             :         }
   18970                 :             :       break;
   18971                 :             : 
   18972                 :     4238797 :     case STATIC_ASSERT:
   18973                 :     4238797 :       {
   18974                 :     4238797 :         tree condition, message;
   18975                 :             : 
   18976                 :     4238797 :         ++c_inhibit_evaluation_warnings;
   18977                 :     4238797 :         condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
   18978                 :             :                                  complain, in_decl);
   18979                 :     4238797 :         message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args,
   18980                 :             :                                complain, in_decl);
   18981                 :     4238797 :         if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST
   18982                 :     4238797 :             && TREE_CODE (message) == STRING_CST)
   18983                 :           0 :           message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t),
   18984                 :           0 :                                 PAREN_EXPR, TREE_TYPE (message), message);
   18985                 :     4238797 :         --c_inhibit_evaluation_warnings;
   18986                 :             : 
   18987                 :     4238797 :         finish_static_assert (condition, message,
   18988                 :     4238797 :                               STATIC_ASSERT_SOURCE_LOCATION (t),
   18989                 :             :                               /*member_p=*/false, /*show_expr_p=*/true);
   18990                 :             :       }
   18991                 :     4238797 :       break;
   18992                 :             : 
   18993                 :         320 :     case OACC_KERNELS:
   18994                 :         320 :     case OACC_PARALLEL:
   18995                 :         320 :     case OACC_SERIAL:
   18996                 :         320 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC_TARGET, args,
   18997                 :             :                                 complain, in_decl);
   18998                 :         320 :       stmt = begin_omp_parallel ();
   18999                 :         320 :       RECUR (OMP_BODY (t));
   19000                 :         320 :       finish_omp_construct (TREE_CODE (t), stmt, tmp);
   19001                 :         320 :       break;
   19002                 :             : 
   19003                 :         958 :     case OMP_PARALLEL:
   19004                 :         958 :       r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
   19005                 :         958 :       tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
   19006                 :             :                                 complain, in_decl);
   19007                 :         958 :       if (OMP_PARALLEL_COMBINED (t))
   19008                 :         509 :         omp_parallel_combined_clauses = &tmp;
   19009                 :         958 :       stmt = begin_omp_parallel ();
   19010                 :         958 :       RECUR (OMP_PARALLEL_BODY (t));
   19011                 :         958 :       gcc_assert (omp_parallel_combined_clauses == NULL);
   19012                 :        1916 :       OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
   19013                 :         958 :         = OMP_PARALLEL_COMBINED (t);
   19014                 :         958 :       pop_omp_privatization_clauses (r);
   19015                 :         958 :       break;
   19016                 :             : 
   19017                 :         449 :     case OMP_TASK:
   19018                 :         449 :       if (OMP_TASK_BODY (t) == NULL_TREE)
   19019                 :             :         {
   19020                 :           0 :           tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   19021                 :             :                                     complain, in_decl);
   19022                 :           0 :           t = copy_node (t);
   19023                 :           0 :           OMP_TASK_CLAUSES (t) = tmp;
   19024                 :           0 :           add_stmt (t);
   19025                 :           0 :           break;
   19026                 :             :         }
   19027                 :         449 :       r = push_omp_privatization_clauses (false);
   19028                 :         449 :       tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
   19029                 :             :                                 complain, in_decl);
   19030                 :         449 :       stmt = begin_omp_task ();
   19031                 :         449 :       RECUR (OMP_TASK_BODY (t));
   19032                 :         449 :       finish_omp_task (tmp, stmt);
   19033                 :         449 :       pop_omp_privatization_clauses (r);
   19034                 :         449 :       break;
   19035                 :             : 
   19036                 :        1297 :     case OMP_FOR:
   19037                 :        1297 :     case OMP_LOOP:
   19038                 :        1297 :     case OMP_SIMD:
   19039                 :        1297 :     case OMP_DISTRIBUTE:
   19040                 :        1297 :     case OMP_TASKLOOP:
   19041                 :        1297 :     case OACC_LOOP:
   19042                 :        1297 :       {
   19043                 :        1297 :         tree clauses, body, pre_body;
   19044                 :        1297 :         tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
   19045                 :        1297 :         tree orig_declv = NULL_TREE;
   19046                 :        1297 :         tree incrv = NULL_TREE;
   19047                 :        1297 :         enum c_omp_region_type ort = C_ORT_OMP;
   19048                 :        1297 :         bool any_range_for = false;
   19049                 :        1297 :         int i;
   19050                 :             : 
   19051                 :        1297 :         if (TREE_CODE (t) == OACC_LOOP)
   19052                 :          42 :           ort = C_ORT_ACC;
   19053                 :             : 
   19054                 :        1297 :         r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
   19055                 :        1297 :         clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
   19056                 :             :                                       in_decl);
   19057                 :        1297 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   19058                 :             :           {
   19059                 :        1077 :             declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19060                 :        1077 :             if (OMP_FOR_ORIG_DECLS (t))
   19061                 :         601 :               orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19062                 :        1077 :             initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19063                 :        1077 :             condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19064                 :        1077 :             incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
   19065                 :             :           }
   19066                 :             : 
   19067                 :        1297 :         keep_next_level (true);
   19068                 :        1297 :         stmt = begin_omp_structured_block ();
   19069                 :             : 
   19070                 :        1297 :         pre_body = push_stmt_list ();
   19071                 :        1297 :         RECUR (OMP_FOR_PRE_BODY (t));
   19072                 :        1297 :         pre_body = pop_stmt_list (pre_body);
   19073                 :             : 
   19074                 :        1297 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   19075                 :        2375 :           for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   19076                 :        1298 :             any_range_for
   19077                 :        1298 :               |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
   19078                 :             :                                           condv, incrv, &clauses, args,
   19079                 :             :                                           complain, in_decl);
   19080                 :        1297 :         omp_parallel_combined_clauses = NULL;
   19081                 :             : 
   19082                 :        1297 :         if (any_range_for)
   19083                 :             :           {
   19084                 :          64 :             gcc_assert (orig_declv);
   19085                 :          64 :             body = begin_omp_structured_block ();
   19086                 :         216 :             for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
   19087                 :          88 :               if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
   19088                 :          71 :                   && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
   19089                 :         159 :                   && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
   19090                 :          71 :                 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
   19091                 :          71 :                                          TREE_VEC_ELT (declv, i));
   19092                 :             :           }
   19093                 :             :         else
   19094                 :        1233 :           body = push_stmt_list ();
   19095                 :        1297 :         RECUR (OMP_FOR_BODY (t));
   19096                 :        1297 :         if (any_range_for)
   19097                 :          64 :           body = finish_omp_structured_block (body);
   19098                 :             :         else
   19099                 :        1233 :           body = pop_stmt_list (body);
   19100                 :             : 
   19101                 :        1297 :         if (OMP_FOR_INIT (t) != NULL_TREE)
   19102                 :        1077 :           t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
   19103                 :             :                               orig_declv, initv, condv, incrv, body, pre_body,
   19104                 :             :                               NULL, clauses);
   19105                 :             :         else
   19106                 :             :           {
   19107                 :         220 :             t = make_node (TREE_CODE (t));
   19108                 :         220 :             TREE_TYPE (t) = void_type_node;
   19109                 :         220 :             OMP_FOR_BODY (t) = body;
   19110                 :         220 :             OMP_FOR_PRE_BODY (t) = pre_body;
   19111                 :         220 :             OMP_FOR_CLAUSES (t) = clauses;
   19112                 :         220 :             SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
   19113                 :         220 :             add_stmt (t);
   19114                 :             :           }
   19115                 :             : 
   19116                 :        1297 :         add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
   19117                 :             :                                         t));
   19118                 :        1297 :         pop_omp_privatization_clauses (r);
   19119                 :             :       }
   19120                 :        1297 :       break;
   19121                 :             : 
   19122                 :          25 :     case OMP_SECTIONS:
   19123                 :          25 :     case OMP_MASKED:
   19124                 :          25 :       omp_parallel_combined_clauses = NULL;
   19125                 :             :       /* FALLTHRU */
   19126                 :         591 :     case OMP_SINGLE:
   19127                 :         591 :     case OMP_SCOPE:
   19128                 :         591 :     case OMP_TEAMS:
   19129                 :         591 :     case OMP_CRITICAL:
   19130                 :         591 :     case OMP_TASKGROUP:
   19131                 :         591 :     case OMP_SCAN:
   19132                 :         591 :       r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
   19133                 :         591 :                                           && OMP_TEAMS_COMBINED (t));
   19134                 :         591 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
   19135                 :             :                                 in_decl);
   19136                 :         591 :       if (TREE_CODE (t) == OMP_TEAMS)
   19137                 :             :         {
   19138                 :         332 :           keep_next_level (true);
   19139                 :         332 :           stmt = begin_omp_structured_block ();
   19140                 :         332 :           RECUR (OMP_BODY (t));
   19141                 :         332 :           stmt = finish_omp_structured_block (stmt);
   19142                 :             :         }
   19143                 :             :       else
   19144                 :             :         {
   19145                 :         259 :           stmt = push_stmt_list ();
   19146                 :         259 :           RECUR (OMP_BODY (t));
   19147                 :         259 :           stmt = pop_stmt_list (stmt);
   19148                 :             :         }
   19149                 :             : 
   19150                 :         591 :       if (TREE_CODE (t) == OMP_CRITICAL
   19151                 :          24 :           && tmp != NULL_TREE
   19152                 :         607 :           && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
   19153                 :             :         {
   19154                 :           8 :           error_at (OMP_CLAUSE_LOCATION (tmp),
   19155                 :             :                     "%<#pragma omp critical%> with %<hint%> clause requires "
   19156                 :             :                     "a name, except when %<omp_sync_hint_none%> is used");
   19157                 :           8 :           RETURN (error_mark_node);
   19158                 :             :         }
   19159                 :         583 :       t = copy_node (t);
   19160                 :         583 :       OMP_BODY (t) = stmt;
   19161                 :         583 :       OMP_CLAUSES (t) = tmp;
   19162                 :         583 :       add_stmt (t);
   19163                 :         583 :       pop_omp_privatization_clauses (r);
   19164                 :         583 :       break;
   19165                 :             : 
   19166                 :         149 :     case OMP_DEPOBJ:
   19167                 :         149 :       r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
   19168                 :         149 :       if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
   19169                 :             :         {
   19170                 :         137 :           enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
   19171                 :         137 :           if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
   19172                 :             :             {
   19173                 :          76 :               tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
   19174                 :             :                                         args, complain, in_decl);
   19175                 :          76 :               if (tmp == NULL_TREE)
   19176                 :           8 :                 tmp = error_mark_node;
   19177                 :             :             }
   19178                 :             :           else
   19179                 :             :             {
   19180                 :         122 :               kind = (enum omp_clause_depend_kind)
   19181                 :          61 :                      tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
   19182                 :          61 :               tmp = NULL_TREE;
   19183                 :             :             }
   19184                 :         137 :           finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
   19185                 :             :         }
   19186                 :             :       else
   19187                 :          12 :         finish_omp_depobj (EXPR_LOCATION (t), r,
   19188                 :             :                            OMP_CLAUSE_DEPEND_INVALID,
   19189                 :          12 :                            OMP_DEPOBJ_CLAUSES (t));
   19190                 :             :       break;
   19191                 :             : 
   19192                 :         939 :     case OACC_DATA:
   19193                 :         939 :     case OMP_TARGET_DATA:
   19194                 :         939 :     case OMP_TARGET:
   19195                 :        1862 :       tmp = tsubst_omp_clauses (OMP_CLAUSES (t),
   19196                 :             :                                 TREE_CODE (t) == OACC_DATA
   19197                 :             :                                 ? C_ORT_ACC
   19198                 :             :                                 : TREE_CODE (t) == OMP_TARGET
   19199                 :         923 :                                 ? C_ORT_OMP_TARGET : C_ORT_OMP,
   19200                 :             :                                 args, complain, in_decl);
   19201                 :         939 :       keep_next_level (true);
   19202                 :         939 :       stmt = begin_omp_structured_block ();
   19203                 :             : 
   19204                 :         939 :       RECUR (OMP_BODY (t));
   19205                 :         939 :       stmt = finish_omp_structured_block (stmt);
   19206                 :             : 
   19207                 :         939 :       t = copy_node (t);
   19208                 :         939 :       OMP_BODY (t) = stmt;
   19209                 :         939 :       OMP_CLAUSES (t) = tmp;
   19210                 :             : 
   19211                 :         939 :       if (TREE_CODE (t) == OMP_TARGET)
   19212                 :         911 :         finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t),
   19213                 :             :                                    &OMP_CLAUSES (t));
   19214                 :             : 
   19215                 :         939 :       if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
   19216                 :             :         {
   19217                 :         108 :           tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
   19218                 :         108 :           if (teams)
   19219                 :             :             /* For combined target teams, ensure the num_teams and
   19220                 :             :                thread_limit clause expressions are evaluated on the host,
   19221                 :             :                before entering the target construct.  */
   19222                 :         212 :             for (tree c = OMP_TEAMS_CLAUSES (teams);
   19223                 :         212 :                  c; c = OMP_CLAUSE_CHAIN (c))
   19224                 :         104 :               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
   19225                 :         104 :                   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
   19226                 :         188 :                 for (int i = 0;
   19227                 :         292 :                      i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
   19228                 :         188 :                   if (OMP_CLAUSE_OPERAND (c, i)
   19229                 :         188 :                       && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
   19230                 :             :                     {
   19231                 :          56 :                       tree expr = OMP_CLAUSE_OPERAND (c, i);
   19232                 :          56 :                       expr = force_target_expr (TREE_TYPE (expr), expr,
   19233                 :             :                                                 tf_none);
   19234                 :          56 :                       if (expr == error_mark_node)
   19235                 :           0 :                         continue;
   19236                 :          56 :                       tmp = TARGET_EXPR_SLOT (expr);
   19237                 :          56 :                       add_stmt (expr);
   19238                 :          56 :                       OMP_CLAUSE_OPERAND (c, i) = expr;
   19239                 :          56 :                       tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
   19240                 :             :                                                   OMP_CLAUSE_FIRSTPRIVATE);
   19241                 :          56 :                       OMP_CLAUSE_DECL (tc) = tmp;
   19242                 :          56 :                       OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
   19243                 :          56 :                       OMP_TARGET_CLAUSES (t) = tc;
   19244                 :             :                     }
   19245                 :             :         }
   19246                 :         939 :       add_stmt (t);
   19247                 :         939 :       break;
   19248                 :             : 
   19249                 :           1 :     case OACC_DECLARE:
   19250                 :           1 :       t = copy_node (t);
   19251                 :           1 :       tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
   19252                 :             :                                 complain, in_decl);
   19253                 :           1 :       OACC_DECLARE_CLAUSES (t) = tmp;
   19254                 :           1 :       add_stmt (t);
   19255                 :           1 :       break;
   19256                 :             : 
   19257                 :         175 :     case OMP_TARGET_UPDATE:
   19258                 :         175 :     case OMP_TARGET_ENTER_DATA:
   19259                 :         175 :     case OMP_TARGET_EXIT_DATA:
   19260                 :         175 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
   19261                 :             :                                 complain, in_decl);
   19262                 :         175 :       t = copy_node (t);
   19263                 :         175 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   19264                 :         175 :       add_stmt (t);
   19265                 :         175 :       break;
   19266                 :             : 
   19267                 :         486 :     case OACC_CACHE:
   19268                 :         486 :     case OACC_ENTER_DATA:
   19269                 :         486 :     case OACC_EXIT_DATA:
   19270                 :         486 :     case OACC_UPDATE:
   19271                 :         486 :       tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
   19272                 :             :                                 complain, in_decl);
   19273                 :         486 :       t = copy_node (t);
   19274                 :         486 :       OMP_STANDALONE_CLAUSES (t) = tmp;
   19275                 :         486 :       add_stmt (t);
   19276                 :         486 :       break;
   19277                 :             : 
   19278                 :          44 :     case OMP_ORDERED:
   19279                 :          44 :       tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
   19280                 :             :                                 complain, in_decl);
   19281                 :          44 :       if (OMP_BODY (t))
   19282                 :             :         {
   19283                 :           4 :           stmt = push_stmt_list ();
   19284                 :           4 :           RECUR (OMP_BODY (t));
   19285                 :           4 :           stmt = pop_stmt_list (stmt);
   19286                 :             :         }
   19287                 :             :       else
   19288                 :          40 :         stmt = NULL_TREE;
   19289                 :             : 
   19290                 :          44 :       t = copy_node (t);
   19291                 :          44 :       OMP_BODY (t) = stmt;
   19292                 :          44 :       OMP_ORDERED_CLAUSES (t) = tmp;
   19293                 :          44 :       add_stmt (t);
   19294                 :          44 :       break;
   19295                 :             : 
   19296                 :          92 :     case OMP_MASTER:
   19297                 :          92 :     case OMP_STRUCTURED_BLOCK:
   19298                 :          92 :       omp_parallel_combined_clauses = NULL;
   19299                 :             :       /* FALLTHRU */
   19300                 :         117 :     case OMP_SECTION:
   19301                 :         117 :       stmt = push_stmt_list ();
   19302                 :         117 :       RECUR (OMP_BODY (t));
   19303                 :         117 :       stmt = pop_stmt_list (stmt);
   19304                 :             : 
   19305                 :         117 :       t = copy_node (t);
   19306                 :         117 :       OMP_BODY (t) = stmt;
   19307                 :         117 :       add_stmt (t);
   19308                 :         117 :       break;
   19309                 :             : 
   19310                 :         665 :     case OMP_ATOMIC:
   19311                 :         665 :       gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
   19312                 :         665 :       tmp = NULL_TREE;
   19313                 :         665 :       if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
   19314                 :          40 :         tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
   19315                 :             :                                   complain, in_decl);
   19316                 :         665 :       if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
   19317                 :             :         {
   19318                 :         220 :           tree op1 = TREE_OPERAND (t, 1);
   19319                 :         220 :           tree rhs1 = NULL_TREE;
   19320                 :         220 :           tree r = NULL_TREE;
   19321                 :         220 :           tree lhs, rhs;
   19322                 :         220 :           if (TREE_CODE (op1) == COMPOUND_EXPR)
   19323                 :             :             {
   19324                 :         119 :               rhs1 = RECUR (TREE_OPERAND (op1, 0));
   19325                 :         119 :               op1 = TREE_OPERAND (op1, 1);
   19326                 :             :             }
   19327                 :         220 :           if (TREE_CODE (op1) == COND_EXPR)
   19328                 :             :             {
   19329                 :          56 :               gcc_assert (rhs1 == NULL_TREE);
   19330                 :          56 :               tree c = TREE_OPERAND (op1, 0);
   19331                 :          56 :               if (TREE_CODE (c) == MODIFY_EXPR)
   19332                 :             :                 {
   19333                 :          16 :                   r = RECUR (TREE_OPERAND (c, 0));
   19334                 :          16 :                   c = TREE_OPERAND (c, 1);
   19335                 :             :                 }
   19336                 :          56 :               gcc_assert (TREE_CODE (c) == EQ_EXPR);
   19337                 :          56 :               rhs = RECUR (TREE_OPERAND (c, 1));
   19338                 :          56 :               lhs = RECUR (TREE_OPERAND (op1, 2));
   19339                 :          56 :               rhs1 = RECUR (TREE_OPERAND (op1, 1));
   19340                 :             :             }
   19341                 :             :           else
   19342                 :             :             {
   19343                 :         164 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   19344                 :         164 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   19345                 :             :             }
   19346                 :         220 :           finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
   19347                 :             :                              lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r,
   19348                 :         220 :                              tmp, OMP_ATOMIC_MEMORY_ORDER (t),
   19349                 :         220 :                              OMP_ATOMIC_WEAK (t));
   19350                 :             :         }
   19351                 :             :       else
   19352                 :             :         {
   19353                 :         445 :           tree op1 = TREE_OPERAND (t, 1);
   19354                 :         445 :           tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
   19355                 :         445 :           tree rhs1 = NULL_TREE, r = NULL_TREE;
   19356                 :         445 :           enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
   19357                 :         445 :           enum tree_code opcode = NOP_EXPR;
   19358                 :         445 :           if (code == OMP_ATOMIC_READ)
   19359                 :             :             {
   19360                 :         165 :               v = RECUR (TREE_OPERAND (op1, 0));
   19361                 :         165 :               lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   19362                 :             :             }
   19363                 :         280 :           else if (code == OMP_ATOMIC_CAPTURE_OLD
   19364                 :         280 :                    || code == OMP_ATOMIC_CAPTURE_NEW)
   19365                 :             :             {
   19366                 :         255 :               tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
   19367                 :         255 :               v = RECUR (TREE_OPERAND (op1, 0));
   19368                 :         255 :               lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
   19369                 :         255 :               if (TREE_CODE (op11) == COMPOUND_EXPR)
   19370                 :             :                 {
   19371                 :         103 :                   rhs1 = RECUR (TREE_OPERAND (op11, 0));
   19372                 :         103 :                   op11 = TREE_OPERAND (op11, 1);
   19373                 :             :                 }
   19374                 :         255 :               if (TREE_CODE (op11) == COND_EXPR)
   19375                 :             :                 {
   19376                 :          88 :                   gcc_assert (rhs1 == NULL_TREE);
   19377                 :          88 :                   tree c = TREE_OPERAND (op11, 0);
   19378                 :          88 :                   if (TREE_CODE (c) == MODIFY_EXPR)
   19379                 :             :                     {
   19380                 :          40 :                       r = RECUR (TREE_OPERAND (c, 0));
   19381                 :          40 :                       c = TREE_OPERAND (c, 1);
   19382                 :             :                     }
   19383                 :          88 :                   gcc_assert (TREE_CODE (c) == EQ_EXPR);
   19384                 :          88 :                   rhs = RECUR (TREE_OPERAND (c, 1));
   19385                 :          88 :                   lhs = RECUR (TREE_OPERAND (op11, 2));
   19386                 :          88 :                   rhs1 = RECUR (TREE_OPERAND (op11, 1));
   19387                 :             :                 }
   19388                 :             :               else
   19389                 :             :                 {
   19390                 :         167 :                   lhs = RECUR (TREE_OPERAND (op11, 0));
   19391                 :         167 :                   rhs = RECUR (TREE_OPERAND (op11, 1));
   19392                 :             :                 }
   19393                 :         255 :               opcode = TREE_CODE (op11);
   19394                 :         255 :               if (opcode == MODIFY_EXPR)
   19395                 :          11 :                 opcode = NOP_EXPR;
   19396                 :             :             }
   19397                 :             :           else
   19398                 :             :             {
   19399                 :          25 :               code = OMP_ATOMIC;
   19400                 :          25 :               lhs = RECUR (TREE_OPERAND (op1, 0));
   19401                 :          25 :               rhs = RECUR (TREE_OPERAND (op1, 1));
   19402                 :             :             }
   19403                 :         445 :           finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
   19404                 :             :                              lhs1, rhs1, r, tmp,
   19405                 :         445 :                              OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t));
   19406                 :             :         }
   19407                 :             :       break;
   19408                 :             : 
   19409                 :          84 :     case TRANSACTION_EXPR:
   19410                 :          84 :       {
   19411                 :          84 :         int flags = 0;
   19412                 :          84 :         flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
   19413                 :          84 :         flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
   19414                 :             : 
   19415                 :          84 :         if (TRANSACTION_EXPR_IS_STMT (t))
   19416                 :             :           {
   19417                 :          50 :             tree body = TRANSACTION_EXPR_BODY (t);
   19418                 :          50 :             tree noex = NULL_TREE;
   19419                 :          50 :             if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
   19420                 :             :               {
   19421                 :          12 :                 noex = MUST_NOT_THROW_COND (body);
   19422                 :          12 :                 if (noex == NULL_TREE)
   19423                 :           3 :                   noex = boolean_true_node;
   19424                 :          12 :                 body = TREE_OPERAND (body, 0);
   19425                 :             :               }
   19426                 :          50 :             stmt = begin_transaction_stmt (input_location, NULL, flags);
   19427                 :          50 :             RECUR (body);
   19428                 :          50 :             finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
   19429                 :             :           }
   19430                 :             :         else
   19431                 :             :           {
   19432                 :          34 :             stmt = build_transaction_expr (EXPR_LOCATION (t),
   19433                 :          34 :                                            RECUR (TRANSACTION_EXPR_BODY (t)),
   19434                 :             :                                            flags, NULL_TREE);
   19435                 :          34 :             RETURN (stmt);
   19436                 :             :           }
   19437                 :             :       }
   19438                 :          50 :       break;
   19439                 :             : 
   19440                 :          21 :     case MUST_NOT_THROW_EXPR:
   19441                 :          21 :       {
   19442                 :          21 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   19443                 :          21 :         tree cond = RECUR (MUST_NOT_THROW_COND (t));
   19444                 :          21 :         RETURN (build_must_not_throw_expr (op0, cond));
   19445                 :             :       }
   19446                 :             : 
   19447                 :           0 :     case EXPR_PACK_EXPANSION:
   19448                 :           0 :       error ("invalid use of pack expansion expression");
   19449                 :           0 :       RETURN (error_mark_node);
   19450                 :             : 
   19451                 :           0 :     case NONTYPE_ARGUMENT_PACK:
   19452                 :           0 :       error ("use %<...%> to expand argument pack");
   19453                 :           0 :       RETURN (error_mark_node);
   19454                 :             : 
   19455                 :      173038 :     case COMPOUND_EXPR:
   19456                 :      173038 :       tmp = RECUR (TREE_OPERAND (t, 0));
   19457                 :      173038 :       if (tmp == NULL_TREE)
   19458                 :             :         /* If the first operand was a statement, we're done with it.  */
   19459                 :      114370 :         RETURN (RECUR (TREE_OPERAND (t, 1)));
   19460                 :       58668 :       RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
   19461                 :             :                                     RECUR (TREE_OPERAND (t, 1)),
   19462                 :             :                                     templated_operator_saved_lookups (t),
   19463                 :             :                                     complain));
   19464                 :             : 
   19465                 :      201854 :     case PREDICT_EXPR:
   19466                 :      201854 :       RETURN (add_stmt (copy_node (t)));
   19467                 :             : 
   19468                 :          49 :     case ANNOTATE_EXPR:
   19469                 :          49 :       {
   19470                 :             :         /* Although ANNOTATE_EXPR is an expression, it can only appear in
   19471                 :             :            WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted
   19472                 :             :            using tsubst_stmt rather than tsubst_expr and can contain
   19473                 :             :            DECL_EXPRs.  */
   19474                 :          49 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   19475                 :          49 :         tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   19476                 :          49 :         tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
   19477                 :          49 :         if (TREE_CODE (op2) == INTEGER_CST
   19478                 :          49 :             && wi::to_widest (op2) == (int) annot_expr_unroll_kind)
   19479                 :          34 :           op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)),
   19480                 :             :                                         op3);
   19481                 :          49 :         RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
   19482                 :             :                             TREE_TYPE (op1), op1, op2, op3));
   19483                 :             :       }
   19484                 :             : 
   19485                 :   123130638 :     default:
   19486                 :   123130638 :       gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
   19487                 :             : 
   19488                 :   123130638 :       RETURN (tsubst_expr (t, args, complain, in_decl));
   19489                 :             :     }
   19490                 :             : 
   19491                 :   148782031 :   RETURN (NULL_TREE);
   19492                 :   272287687 :  out:
   19493                 :   272287687 :   input_location = loc;
   19494                 :   272287687 :   return r;
   19495                 :             : #undef RECUR
   19496                 :             : #undef RETURN
   19497                 :             : }
   19498                 :             : 
   19499                 :             : /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
   19500                 :             :    function.  For description of the body see comment above
   19501                 :             :    cp_parser_omp_declare_reduction_exprs.  */
   19502                 :             : 
   19503                 :             : static void
   19504                 :         126 : tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   19505                 :             : {
   19506                 :         126 :   if (t == NULL_TREE || t == error_mark_node)
   19507                 :           0 :     return;
   19508                 :             : 
   19509                 :         126 :   gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
   19510                 :             : 
   19511                 :         126 :   tree_stmt_iterator tsi;
   19512                 :         126 :   int i;
   19513                 :         126 :   tree stmts[7];
   19514                 :         126 :   memset (stmts, 0, sizeof stmts);
   19515                 :         126 :   for (i = 0, tsi = tsi_start (t);
   19516                 :         736 :        i < 7 && !tsi_end_p (tsi);
   19517                 :         610 :        i++, tsi_next (&tsi))
   19518                 :         610 :     stmts[i] = tsi_stmt (tsi);
   19519                 :         126 :   gcc_assert (tsi_end_p (tsi));
   19520                 :             : 
   19521                 :         126 :   if (i >= 3)
   19522                 :             :     {
   19523                 :         126 :       gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
   19524                 :             :                   && TREE_CODE (stmts[1]) == DECL_EXPR);
   19525                 :         126 :       tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
   19526                 :             :                              args, complain, in_decl);
   19527                 :         126 :       tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
   19528                 :             :                             args, complain, in_decl);
   19529                 :             :       /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
   19530                 :             :          expect to be pushing it.  */
   19531                 :         126 :       DECL_CONTEXT (omp_out) = current_function_decl;
   19532                 :         126 :       DECL_CONTEXT (omp_in) = current_function_decl;
   19533                 :         126 :       keep_next_level (true);
   19534                 :         126 :       tree block = begin_omp_structured_block ();
   19535                 :         126 :       tsubst_stmt (stmts[2], args, complain, in_decl);
   19536                 :         126 :       block = finish_omp_structured_block (block);
   19537                 :         126 :       block = maybe_cleanup_point_expr_void (block);
   19538                 :         126 :       add_decl_expr (omp_out);
   19539                 :         126 :       copy_warning (omp_out, DECL_EXPR_DECL (stmts[0]));
   19540                 :         126 :       add_decl_expr (omp_in);
   19541                 :         126 :       finish_expr_stmt (block);
   19542                 :             :     }
   19543                 :         126 :   if (i >= 6)
   19544                 :             :     {
   19545                 :          66 :       gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
   19546                 :             :                   && TREE_CODE (stmts[4]) == DECL_EXPR);
   19547                 :          66 :       tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
   19548                 :             :                               args, complain, in_decl);
   19549                 :          66 :       tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
   19550                 :             :                               args, complain, in_decl);
   19551                 :          66 :       DECL_CONTEXT (omp_priv) = current_function_decl;
   19552                 :          66 :       DECL_CONTEXT (omp_orig) = current_function_decl;
   19553                 :          66 :       keep_next_level (true);
   19554                 :          66 :       tree block = begin_omp_structured_block ();
   19555                 :          66 :       tsubst_stmt (stmts[5], args, complain, in_decl);
   19556                 :          66 :       block = finish_omp_structured_block (block);
   19557                 :          66 :       block = maybe_cleanup_point_expr_void (block);
   19558                 :          66 :       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
   19559                 :          66 :       add_decl_expr (omp_priv);
   19560                 :          66 :       add_decl_expr (omp_orig);
   19561                 :          66 :       finish_expr_stmt (block);
   19562                 :          66 :       if (i == 7)
   19563                 :          34 :         add_decl_expr (omp_orig);
   19564                 :             :     }
   19565                 :             : }
   19566                 :             : 
   19567                 :             : /* T is a postfix-expression that is not being used in a function
   19568                 :             :    call.  Return the substituted version of T.  */
   19569                 :             : 
   19570                 :             : static tree
   19571                 :    26043890 : tsubst_non_call_postfix_expression (tree t, tree args,
   19572                 :             :                                     tsubst_flags_t complain,
   19573                 :             :                                     tree in_decl)
   19574                 :             : {
   19575                 :    26043890 :   if (TREE_CODE (t) == SCOPE_REF)
   19576                 :        8320 :     t = tsubst_qualified_id (t, args, complain, in_decl,
   19577                 :             :                              /*done=*/false, /*address_p=*/false);
   19578                 :             :   else
   19579                 :    26035570 :     t = tsubst_expr (t, args, complain, in_decl);
   19580                 :             : 
   19581                 :    26043890 :   return t;
   19582                 :             : }
   19583                 :             : 
   19584                 :             : /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
   19585                 :             :    LAMBDA_EXPR_CAPTURE_LIST passed in LIST.  Do deduction for a previously
   19586                 :             :    dependent init-capture.  EXPLICIT_P is true if the original list had
   19587                 :             :    explicit captures.  */
   19588                 :             : 
   19589                 :             : static void
   19590                 :      254403 : prepend_one_capture (tree field, tree init, tree &list, bool explicit_p,
   19591                 :             :                      tsubst_flags_t complain)
   19592                 :             : {
   19593                 :      254403 :   if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
   19594                 :             :     {
   19595                 :          57 :       tree type = NULL_TREE;
   19596                 :          57 :       if (!init)
   19597                 :             :         {
   19598                 :           3 :           if (complain & tf_error)
   19599                 :           3 :             error ("empty initializer in lambda init-capture");
   19600                 :           3 :           init = error_mark_node;
   19601                 :             :         }
   19602                 :          54 :       else if (TREE_CODE (init) == TREE_LIST)
   19603                 :           0 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   19604                 :          57 :       if (!type)
   19605                 :          57 :         type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
   19606                 :          57 :       TREE_TYPE (field) = type;
   19607                 :          57 :       cp_apply_type_quals_to_decl (cp_type_quals (type), field);
   19608                 :             :     }
   19609                 :      254403 :   list = tree_cons (field, init, list);
   19610                 :      254403 :   LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p;
   19611                 :      254403 : }
   19612                 :             : 
   19613                 :             : /* T is a LAMBDA_EXPR.  Generate a new LAMBDA_EXPR for the current
   19614                 :             :    instantiation context.  Instantiating a pack expansion containing a lambda
   19615                 :             :    might result in multiple lambdas all based on the same lambda in the
   19616                 :             :    template.  */
   19617                 :             : 
   19618                 :             : tree
   19619                 :      138442 : tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   19620                 :             : {
   19621                 :      138442 :   tree oldfn = lambda_function (t);
   19622                 :      138442 :   in_decl = oldfn;
   19623                 :             : 
   19624                 :      138442 :   args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl);
   19625                 :      138442 :   if (processing_template_decl
   19626                 :      138442 :       && (!in_template_context || any_dependent_template_arguments_p (args)))
   19627                 :             :     {
   19628                 :             :       /* Defer templated substitution into a lambda-expr if we lost the
   19629                 :             :          necessary template context.  This may happen for a lambda-expr
   19630                 :             :          used as a default template argument.
   19631                 :             : 
   19632                 :             :          Defer dependent substitution as well so that we don't prematurely
   19633                 :             :          lower the level of a deduced return type or any other auto or
   19634                 :             :          template parameter belonging to the lambda.  */
   19635                 :          10 :       t = copy_node (t);
   19636                 :          10 :       LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE;
   19637                 :          10 :       LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain);
   19638                 :          10 :       return t;
   19639                 :             :     }
   19640                 :             : 
   19641                 :      138432 :   tree r = build_lambda_expr ();
   19642                 :             : 
   19643                 :      138432 :   LAMBDA_EXPR_LOCATION (r)
   19644                 :      138432 :     = LAMBDA_EXPR_LOCATION (t);
   19645                 :      138432 :   LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
   19646                 :      138432 :     = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
   19647                 :      138432 :   if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
   19648                 :         320 :     LAMBDA_EXPR_REGEN_INFO (r)
   19649                 :         160 :       = build_template_info (t, add_to_template_args (TI_ARGS (ti),
   19650                 :             :                                                       preserve_args (args)));
   19651                 :             :   else
   19652                 :      276544 :     LAMBDA_EXPR_REGEN_INFO (r)
   19653                 :      138272 :       = build_template_info (t, preserve_args (args));
   19654                 :             : 
   19655                 :      138432 :   gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
   19656                 :             :               && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
   19657                 :             : 
   19658                 :      138432 :   vec<tree,va_gc>* field_packs = NULL;
   19659                 :      138432 :   unsigned name_independent_cnt = 0;
   19660                 :      392827 :   for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
   19661                 :      254395 :        cap = TREE_CHAIN (cap))
   19662                 :             :     {
   19663                 :      254399 :       tree ofield = TREE_PURPOSE (cap);
   19664                 :      254399 :       tree init = TREE_VALUE (cap);
   19665                 :      254399 :       if (PACK_EXPANSION_P (init))
   19666                 :          58 :         init = tsubst_pack_expansion (init, args, complain, in_decl);
   19667                 :             :       else
   19668                 :      254341 :         init = tsubst_expr (init, args, complain, in_decl);
   19669                 :             : 
   19670                 :      254399 :       if (init == error_mark_node)
   19671                 :           4 :         return error_mark_node;
   19672                 :             : 
   19673                 :      254395 :       if (init && TREE_CODE (init) == TREE_LIST)
   19674                 :           6 :         init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
   19675                 :             : 
   19676                 :      254395 :       if (!processing_template_decl
   19677                 :      254271 :           && init && TREE_CODE (init) != TREE_VEC
   19678                 :      508612 :           && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
   19679                 :             :         {
   19680                 :             :           /* For a VLA, simply tsubsting the field type won't work, we need to
   19681                 :             :              go through add_capture again.  XXX do we want to do this for all
   19682                 :             :              captures?  */
   19683                 :           9 :           tree name = (get_identifier
   19684                 :             :                        (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
   19685                 :           9 :           tree ftype = TREE_TYPE (ofield);
   19686                 :           9 :           bool by_ref = (TYPE_REF_P (ftype)
   19687                 :           9 :                          || (TREE_CODE (ftype) == DECLTYPE_TYPE
   19688                 :           0 :                              && DECLTYPE_FOR_REF_CAPTURE (ftype)));
   19689                 :           9 :           add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield),
   19690                 :             :                        &name_independent_cnt);
   19691                 :           9 :           continue;
   19692                 :           9 :         }
   19693                 :             : 
   19694                 :      254386 :       if (PACK_EXPANSION_P (ofield))
   19695                 :          48 :         ofield = PACK_EXPANSION_PATTERN (ofield);
   19696                 :      254386 :       tree field = tsubst_decl (ofield, args, complain);
   19697                 :             : 
   19698                 :      254386 :       if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
   19699                 :             :         {
   19700                 :             :           /* Remember these for when we've pushed local_specializations.  */
   19701                 :          12 :           vec_safe_push (field_packs, ofield);
   19702                 :          12 :           vec_safe_push (field_packs, field);
   19703                 :             :         }
   19704                 :             : 
   19705                 :      254386 :       if (field == error_mark_node)
   19706                 :           0 :         return error_mark_node;
   19707                 :             : 
   19708                 :      254386 :       if (TREE_CODE (field) == TREE_VEC)
   19709                 :             :         {
   19710                 :          51 :           int len = TREE_VEC_LENGTH (field);
   19711                 :          51 :           gcc_assert (TREE_CODE (init) == TREE_VEC
   19712                 :             :                       && TREE_VEC_LENGTH (init) == len);
   19713                 :         119 :           for (int i = 0; i < len; ++i)
   19714                 :         136 :             prepend_one_capture (TREE_VEC_ELT (field, i),
   19715                 :          68 :                                  TREE_VEC_ELT (init, i),
   19716                 :          68 :                                  LAMBDA_EXPR_CAPTURE_LIST (r),
   19717                 :          68 :                                  LAMBDA_CAPTURE_EXPLICIT_P (cap),
   19718                 :             :                                  complain);
   19719                 :             :         }
   19720                 :             :       else
   19721                 :             :         {
   19722                 :      508670 :           prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
   19723                 :      254335 :                                LAMBDA_CAPTURE_EXPLICIT_P (cap), complain);
   19724                 :             : 
   19725                 :      254335 :           if (id_equal (DECL_NAME (field), "__this"))
   19726                 :       23334 :             LAMBDA_EXPR_THIS_CAPTURE (r) = field;
   19727                 :             :         }
   19728                 :             :     }
   19729                 :             : 
   19730                 :      138428 :   tree type = begin_lambda_type (r);
   19731                 :      138428 :   if (type == error_mark_node)
   19732                 :             :     {
   19733                 :           0 :       gcc_checking_assert (!(complain & tf_error) || seen_error ());
   19734                 :           0 :       return error_mark_node;
   19735                 :             :     }
   19736                 :             : 
   19737                 :      138428 :   if (LAMBDA_EXPR_EXTRA_SCOPE (t))
   19738                 :      138260 :     record_lambda_scope (r);
   19739                 :         168 :   else if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
   19740                 :             :     /* If we're pushed into another scope (PR105652), fix it.  */
   19741                 :         242 :     TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
   19742                 :         242 :       = TYPE_CONTEXT (TREE_TYPE (t));
   19743                 :      138428 :   record_lambda_scope_discriminator (r);
   19744                 :             : 
   19745                 :             :   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
   19746                 :      138428 :   determine_visibility (TYPE_NAME (type));
   19747                 :             : 
   19748                 :      138428 :   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
   19749                 :             : 
   19750                 :      138428 :   tree oldtmpl = (generic_lambda_fn_p (oldfn)
   19751                 :      138428 :                   ? DECL_TI_TEMPLATE (oldfn)
   19752                 :      154154 :                   : NULL_TREE);
   19753                 :             : 
   19754                 :       15726 :   tree tparms = NULL_TREE;
   19755                 :       15726 :   if (oldtmpl)
   19756                 :       15726 :     tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain);
   19757                 :             : 
   19758                 :      138428 :   tree fntype = static_fn_type (oldfn);
   19759                 :             : 
   19760                 :      138428 :   tree saved_ctp = current_template_parms;
   19761                 :      138428 :   if (oldtmpl)
   19762                 :             :     {
   19763                 :       15726 :       ++processing_template_decl;
   19764                 :       15726 :       current_template_parms = tparms;
   19765                 :             :     }
   19766                 :      138428 :   fntype = tsubst (fntype, args, complain, in_decl);
   19767                 :      138428 :   if (oldtmpl)
   19768                 :             :     {
   19769                 :       15726 :       current_template_parms = saved_ctp;
   19770                 :       15726 :       --processing_template_decl;
   19771                 :             :     }
   19772                 :             : 
   19773                 :      138428 :   if (fntype == error_mark_node)
   19774                 :             :     r = error_mark_node;
   19775                 :             :   else
   19776                 :             :     {
   19777                 :             :       /* The body of a lambda-expression is not a subexpression of the
   19778                 :             :          enclosing expression.  Parms are to have DECL_CHAIN tsubsted,
   19779                 :             :          which would be skipped if cp_unevaluated_operand.  */
   19780                 :      138418 :       cp_evaluated ev;
   19781                 :             : 
   19782                 :             :       /* Fix the type of 'this'.
   19783                 :             :          For static and xobj member functions we use this to transport the
   19784                 :             :          lambda's closure type.  It appears that in the regular case the
   19785                 :             :          object parameter is still pulled off, and then re-added again anyway.
   19786                 :             :          So perhaps we could do something better here?  */
   19787                 :      138418 :       fntype = build_memfn_type (fntype, type,
   19788                 :             :                                  type_memfn_quals (fntype),
   19789                 :             :                                  type_memfn_rqual (fntype));
   19790                 :      138418 :       tree inst = (oldtmpl
   19791                 :      138418 :                    ? tsubst_template_decl (oldtmpl, args, complain,
   19792                 :             :                                            fntype, tparms)
   19793                 :      122692 :                    : tsubst_function_decl (oldfn, args, complain, fntype));
   19794                 :      138418 :       if (inst == error_mark_node)
   19795                 :             :         {
   19796                 :           0 :           r = error_mark_node;
   19797                 :           0 :           goto out;
   19798                 :             :         }
   19799                 :      138418 :       finish_member_declaration (inst);
   19800                 :      138418 :       record_lambda_scope_sig_discriminator (r, inst);
   19801                 :             : 
   19802                 :      154144 :       tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst;
   19803                 :             : 
   19804                 :             :       /* Let finish_function set this.  */
   19805                 :      138418 :       DECL_DECLARED_CONSTEXPR_P (fn) = false;
   19806                 :             : 
   19807                 :      138418 :       bool nested = cfun;
   19808                 :      138418 :       if (nested)
   19809                 :      138102 :         push_function_context ();
   19810                 :             :       else
   19811                 :             :         /* Still increment function_depth so that we don't GC in the
   19812                 :             :            middle of an expression.  */
   19813                 :         316 :         ++function_depth;
   19814                 :             : 
   19815                 :      138418 :       local_specialization_stack s (lss_copy);
   19816                 :             : 
   19817                 :      138418 :       bool save_in_consteval_if_p = in_consteval_if_p;
   19818                 :      138418 :       in_consteval_if_p = false;
   19819                 :             : 
   19820                 :      138418 :       tree body = start_lambda_function (fn, r);
   19821                 :             : 
   19822                 :             :       /* Now record them for lookup_init_capture_pack.  */
   19823                 :      138418 :       int fplen = vec_safe_length (field_packs);
   19824                 :      138430 :       for (int i = 0; i < fplen; )
   19825                 :             :         {
   19826                 :          12 :           tree pack = (*field_packs)[i++];
   19827                 :          12 :           tree inst = (*field_packs)[i++];
   19828                 :          12 :           register_local_specialization (inst, pack);
   19829                 :             :         }
   19830                 :      138418 :       release_tree_vector (field_packs);
   19831                 :             : 
   19832                 :      138418 :       register_parameter_specializations (oldfn, fn);
   19833                 :             : 
   19834                 :      138418 :       if (oldtmpl)
   19835                 :             :         {
   19836                 :             :           /* We might not partially instantiate some parts of the function, so
   19837                 :             :              copy these flags from the original template.  */
   19838                 :       15726 :           language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
   19839                 :       15726 :           current_function_returns_value = ol->returns_value;
   19840                 :       15726 :           current_function_returns_null = ol->returns_null;
   19841                 :       15726 :           current_function_returns_abnormally = ol->returns_abnormally;
   19842                 :       15726 :           current_function_infinite_loop = ol->infinite_loop;
   19843                 :             :         }
   19844                 :             : 
   19845                 :             :       /* [temp.deduct] A lambda-expression appearing in a function type or a
   19846                 :             :          template parameter is not considered part of the immediate context for
   19847                 :             :          the purposes of template argument deduction. */
   19848                 :      138418 :       complain = tf_warning_or_error;
   19849                 :             : 
   19850                 :      138418 :       tree saved = DECL_SAVED_TREE (oldfn);
   19851                 :      138418 :       if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
   19852                 :             :         /* We already have a body block from start_lambda_function, we don't
   19853                 :             :            need another to confuse NRV (91217).  */
   19854                 :      138418 :         saved = BIND_EXPR_BODY (saved);
   19855                 :             : 
   19856                 :      138418 :       tsubst_stmt (saved, args, complain, r);
   19857                 :             : 
   19858                 :      138418 :       finish_lambda_function (body);
   19859                 :             : 
   19860                 :      138418 :       in_consteval_if_p = save_in_consteval_if_p;
   19861                 :             : 
   19862                 :      138418 :       if (nested)
   19863                 :      138102 :         pop_function_context ();
   19864                 :             :       else
   19865                 :         316 :         --function_depth;
   19866                 :             : 
   19867                 :             :       /* The capture list was built up in reverse order; fix that now.  */
   19868                 :      138418 :       LAMBDA_EXPR_CAPTURE_LIST (r)
   19869                 :      138418 :         = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
   19870                 :             : 
   19871                 :      138418 :       LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
   19872                 :             : 
   19873                 :      138418 :       maybe_add_lambda_conv_op (type);
   19874                 :      138418 :     }
   19875                 :             : 
   19876                 :      138428 : out:
   19877                 :      138428 :   finish_struct (type, /*attr*/NULL_TREE);
   19878                 :             : 
   19879                 :      138428 :   insert_pending_capture_proxies ();
   19880                 :             : 
   19881                 :      138428 :   return r;
   19882                 :             : }
   19883                 :             : 
   19884                 :             : /* Subroutine of maybe_fold_fn_template_args.  */
   19885                 :             : 
   19886                 :             : static bool
   19887                 :    17805430 : fold_targs_r (tree targs, tsubst_flags_t complain)
   19888                 :             : {
   19889                 :    17805430 :   int len = TREE_VEC_LENGTH (targs);
   19890                 :    44421954 :   for (int i = 0; i < len; ++i)
   19891                 :             :     {
   19892                 :    26616527 :       tree &elt = TREE_VEC_ELT (targs, i);
   19893                 :    26616527 :       if (!elt || TYPE_P (elt)
   19894                 :      414151 :           || TREE_CODE (elt) == TEMPLATE_DECL)
   19895                 :    26202592 :         continue;
   19896                 :      413935 :       if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK)
   19897                 :             :         {
   19898                 :           0 :           if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain))
   19899                 :             :             return false;
   19900                 :             :         }
   19901                 :      413935 :       else if (/* We can only safely preevaluate scalar prvalues.  */
   19902                 :      827862 :                SCALAR_TYPE_P (TREE_TYPE (elt))
   19903                 :      413765 :                && !glvalue_p (elt)
   19904                 :      825155 :                && !TREE_CONSTANT (elt))
   19905                 :             :         {
   19906                 :        2767 :           elt = cxx_constant_value (elt, complain);
   19907                 :        2767 :           if (elt == error_mark_node)
   19908                 :             :             return false;
   19909                 :             :         }
   19910                 :             :     }
   19911                 :             : 
   19912                 :             :   return true;
   19913                 :             : }
   19914                 :             : 
   19915                 :             : /* Try to do constant evaluation of any explicit template arguments in FN
   19916                 :             :    before overload resolution, to get any errors only once.  Return true iff
   19917                 :             :    we didn't have any problems folding.  */
   19918                 :             : 
   19919                 :             : static bool
   19920                 :    73733954 : maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain)
   19921                 :             : {
   19922                 :    73733954 :   if (processing_template_decl || fn == NULL_TREE)
   19923                 :             :     return true;
   19924                 :    61331344 :   if (fn == error_mark_node)
   19925                 :             :     return false;
   19926                 :    61239929 :   if (TREE_CODE (fn) == OFFSET_REF
   19927                 :    61156276 :       || TREE_CODE (fn) == COMPONENT_REF)
   19928                 :    11307424 :     fn = TREE_OPERAND (fn, 1);
   19929                 :    61239929 :   if (BASELINK_P (fn))
   19930                 :    20988576 :     fn = BASELINK_FUNCTIONS (fn);
   19931                 :    61239929 :   if (TREE_CODE (fn) != TEMPLATE_ID_EXPR)
   19932                 :             :     return true;
   19933                 :    17805438 :   tree targs = TREE_OPERAND (fn, 1);
   19934                 :    17805438 :   if (targs == NULL_TREE)
   19935                 :             :     return true;
   19936                 :    17805438 :   if (targs == error_mark_node)
   19937                 :             :     return false;
   19938                 :    17805430 :   return fold_targs_r (targs, complain);
   19939                 :             : }
   19940                 :             : 
   19941                 :             : /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling.  */
   19942                 :             : 
   19943                 :             : static void
   19944                 :    73787612 : tsubst_call_args (tree t, tree args, tsubst_flags_t complain,
   19945                 :             :                   tree in_decl, releasing_vec &call_args)
   19946                 :             : {
   19947                 :    73787612 :   unsigned int nargs = call_expr_nargs (t);
   19948                 :   128223488 :   for (unsigned int i = 0; i < nargs; ++i)
   19949                 :             :     {
   19950                 :    54435879 :       tree arg = CALL_EXPR_ARG (t, i);
   19951                 :             : 
   19952                 :    54435879 :       if (!PACK_EXPANSION_P (arg))
   19953                 :    53800128 :         vec_safe_push (call_args, tsubst_expr (arg, args, complain, in_decl));
   19954                 :             :       else
   19955                 :             :         {
   19956                 :             :           /* Expand the pack expansion and push each entry onto CALL_ARGS.  */
   19957                 :      635751 :           arg = tsubst_pack_expansion (arg, args, complain, in_decl);
   19958                 :      635751 :           if (TREE_CODE (arg) == TREE_VEC)
   19959                 :             :             {
   19960                 :      537101 :               unsigned int len, j;
   19961                 :             : 
   19962                 :      537101 :               len = TREE_VEC_LENGTH (arg);
   19963                 :      920649 :               for (j = 0; j < len; ++j)
   19964                 :             :                 {
   19965                 :      383548 :                   tree value = TREE_VEC_ELT (arg, j);
   19966                 :      383548 :                   if (value != NULL_TREE)
   19967                 :      383548 :                     value = convert_from_reference (value);
   19968                 :      383548 :                   vec_safe_push (call_args, value);
   19969                 :             :                 }
   19970                 :             :             }
   19971                 :             :           else
   19972                 :             :             /* A partial substitution.  Add one entry.  */
   19973                 :       98650 :             vec_safe_push (call_args, arg);
   19974                 :             :         }
   19975                 :             :     }
   19976                 :    73787609 : }
   19977                 :             : 
   19978                 :             : /* Like tsubst but deals with expressions and performs semantic
   19979                 :             :    analysis.  */
   19980                 :             : 
   19981                 :             : tree
   19982                 :  1258505912 : tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   19983                 :             : {
   19984                 :             : #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
   19985                 :             : #define RECUR(NODE)                                             \
   19986                 :             :   tsubst_expr (NODE, args, complain, in_decl)
   19987                 :             : 
   19988                 :  1258505912 :   tree retval, op1;
   19989                 :  1258505912 :   location_t save_loc;
   19990                 :             : 
   19991                 :  1258505912 :   if (t == NULL_TREE || t == error_mark_node)
   19992                 :             :     return t;
   19993                 :             : 
   19994                 :  1256291013 :   save_loc = input_location;
   19995                 :  1256291013 :   if (location_t eloc = cp_expr_location (t))
   19996                 :   515497441 :     input_location = eloc;
   19997                 :             : 
   19998                 :             :   /* N3276 decltype magic only applies to calls at the top level or on the
   19999                 :             :      right side of a comma.  */
   20000                 :  1256291013 :   tsubst_flags_t decltype_flag = (complain & tf_decltype);
   20001                 :  1256291013 :   complain &= ~tf_decltype;
   20002                 :             : 
   20003                 :             :   /* This flag only applies to id-expressions at the top level, and
   20004                 :             :      controls resolution thereof.  */
   20005                 :  1256291013 :   tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup);
   20006                 :  1256291013 :   complain &= ~tf_no_name_lookup;
   20007                 :             : 
   20008                 :  1256291013 :   if (!no_name_lookup_flag)
   20009                 :  1046595749 :     if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl))
   20010                 :             :       return d;
   20011                 :             : 
   20012                 :  1256290619 :   switch (TREE_CODE (t))
   20013                 :             :     {
   20014                 :      140462 :     case USING_DECL:
   20015                 :      140462 :       t = DECL_NAME (t);
   20016                 :             :       /* Fall through.  */
   20017                 :   198547040 :     case IDENTIFIER_NODE:
   20018                 :   198547040 :       {
   20019                 :   198547040 :         tree decl;
   20020                 :   198547040 :         cp_id_kind idk;
   20021                 :   198547040 :         const char *error_msg;
   20022                 :             : 
   20023                 :   397068790 :         if (IDENTIFIER_CONV_OP_P (t))
   20024                 :             :           {
   20025                 :        1489 :             tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20026                 :        1489 :             t = make_conv_op_name (new_type);
   20027                 :             :           }
   20028                 :             : 
   20029                 :   198547040 :         if (no_name_lookup_flag)
   20030                 :   198388042 :           RETURN (t);
   20031                 :             : 
   20032                 :             :         /* Look up the name.  */
   20033                 :      158998 :         decl = lookup_name (t);
   20034                 :             : 
   20035                 :             :         /* By convention, expressions use ERROR_MARK_NODE to indicate
   20036                 :             :            failure, not NULL_TREE.  */
   20037                 :      158998 :         if (decl == NULL_TREE)
   20038                 :          36 :           decl = error_mark_node;
   20039                 :             : 
   20040                 :      158998 :         decl = finish_id_expression (t, decl, NULL_TREE,
   20041                 :             :                                      &idk,
   20042                 :             :                                      /*i_c_e_p=*/false,
   20043                 :             :                                      /*allow_i_c_e_p=*/true,
   20044                 :             :                                      /*non_i_c_e_p=*/nullptr,
   20045                 :             :                                      /*template_p=*/false,
   20046                 :             :                                      /*done=*/true,
   20047                 :             :                                      /*address_p=*/false,
   20048                 :             :                                      /*template_arg_p=*/false,
   20049                 :             :                                      &error_msg,
   20050                 :             :                                      input_location);
   20051                 :      158998 :         if (error_msg)
   20052                 :           0 :           error (error_msg);
   20053                 :      158998 :         if (identifier_p (decl))
   20054                 :             :           {
   20055                 :          36 :             if (complain & tf_error)
   20056                 :          36 :               unqualified_name_lookup_error (decl);
   20057                 :          36 :             decl = error_mark_node;
   20058                 :             :           }
   20059                 :   198547040 :         RETURN (decl);
   20060                 :             :       }
   20061                 :             : 
   20062                 :    25776366 :     case TEMPLATE_ID_EXPR:
   20063                 :    25776366 :       {
   20064                 :    25776366 :         tree object;
   20065                 :    25776366 :         tree templ = TREE_OPERAND (t, 0);
   20066                 :    25776366 :         tree targs = TREE_OPERAND (t, 1);
   20067                 :             : 
   20068                 :    25776366 :         if (no_name_lookup_flag)
   20069                 :     4205441 :           templ = tsubst_name (templ, args, complain, in_decl);
   20070                 :             :         else
   20071                 :    21570925 :           templ = tsubst_expr (templ, args, complain, in_decl);
   20072                 :             : 
   20073                 :    25776366 :         if (targs)
   20074                 :    25776366 :           targs = tsubst_template_args (targs, args, complain, in_decl);
   20075                 :    25776366 :         if (targs == error_mark_node)
   20076                 :         206 :           RETURN (error_mark_node);
   20077                 :             : 
   20078                 :    25776160 :         if (TREE_CODE (templ) == SCOPE_REF)
   20079                 :             :           {
   20080                 :          53 :             tree name = TREE_OPERAND (templ, 1);
   20081                 :          53 :             tree tid = lookup_template_function (name, targs);
   20082                 :          53 :             TREE_OPERAND (templ, 1) = tid;
   20083                 :          53 :             RETURN (templ);
   20084                 :             :           }
   20085                 :             : 
   20086                 :    25776107 :         if (concept_definition_p (templ))
   20087                 :             :           {
   20088                 :      662678 :             tree check = build_concept_check (templ, targs, complain);
   20089                 :      662678 :             if (check == error_mark_node)
   20090                 :           1 :               RETURN (error_mark_node);
   20091                 :             : 
   20092                 :      662677 :             tree id = unpack_concept_check (check);
   20093                 :             : 
   20094                 :             :             /* If we built a function concept check, return the underlying
   20095                 :             :                template-id. So we can evaluate it as a function call.  */
   20096                 :      662677 :             if (function_concept_p (TREE_OPERAND (id, 0)))
   20097                 :          27 :               RETURN (id);
   20098                 :             : 
   20099                 :      662650 :             RETURN (check);
   20100                 :             :           }
   20101                 :             : 
   20102                 :    25113429 :         if (variable_template_p (templ))
   20103                 :             :           {
   20104                 :     5881148 :             if (no_name_lookup_flag)
   20105                 :           9 :               RETURN (lookup_template_variable (templ, targs, complain));
   20106                 :             : 
   20107                 :     5881139 :             tree r = lookup_and_finish_template_variable (templ, targs,
   20108                 :             :                                                           complain);
   20109                 :     5881139 :             r = convert_from_reference (r);
   20110                 :     5881139 :             r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
   20111                 :     5881139 :             RETURN (r);
   20112                 :             :           }
   20113                 :             : 
   20114                 :    19232281 :         if (TREE_CODE (templ) == COMPONENT_REF)
   20115                 :             :           {
   20116                 :           0 :             object = TREE_OPERAND (templ, 0);
   20117                 :           0 :             templ = TREE_OPERAND (templ, 1);
   20118                 :             :           }
   20119                 :             :         else
   20120                 :             :           object = NULL_TREE;
   20121                 :             : 
   20122                 :    19232281 :         tree tid = lookup_template_function (templ, targs);
   20123                 :    19232281 :         protected_set_expr_location (tid, EXPR_LOCATION (t));
   20124                 :             : 
   20125                 :    19232281 :         if (object)
   20126                 :           0 :           RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
   20127                 :             :                          object, tid, NULL_TREE));
   20128                 :    19232281 :         else if (no_name_lookup_flag)
   20129                 :     4205394 :           RETURN (tid);
   20130                 :    15026887 :         else if (identifier_p (templ))
   20131                 :             :           {
   20132                 :             :             /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
   20133                 :             :                name lookup found nothing when parsing the template name.  */
   20134                 :           0 :             gcc_assert (cxx_dialect >= cxx20 || seen_error ());
   20135                 :           0 :             RETURN (tid);
   20136                 :             :           }
   20137                 :             :         else
   20138                 :    15026887 :           RETURN (baselink_for_fns (tid));
   20139                 :             :       }
   20140                 :             : 
   20141                 :    29225890 :     case INDIRECT_REF:
   20142                 :    29225890 :       {
   20143                 :    29225890 :         tree r = RECUR (TREE_OPERAND (t, 0));
   20144                 :             : 
   20145                 :    29225890 :         if (REFERENCE_REF_P (t))
   20146                 :             :           {
   20147                 :             :             /* A type conversion to reference type will be enclosed in
   20148                 :             :                such an indirect ref, but the substitution of the cast
   20149                 :             :                will have also added such an indirect ref.  */
   20150                 :    16547318 :             r = convert_from_reference (r);
   20151                 :             :           }
   20152                 :             :         else
   20153                 :    12678572 :           r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
   20154                 :             :                                     templated_operator_saved_lookups (t),
   20155                 :             :                                     complain|decltype_flag);
   20156                 :             : 
   20157                 :    29225890 :         if (REF_PARENTHESIZED_P (t))
   20158                 :         260 :           r = force_paren_expr (r);
   20159                 :             : 
   20160                 :    29225890 :         RETURN (r);
   20161                 :             :       }
   20162                 :             : 
   20163                 :          10 :     case MEM_REF:
   20164                 :          10 :       {
   20165                 :          10 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20166                 :          10 :         tree op1 = RECUR (TREE_OPERAND (t, 0));
   20167                 :          10 :         tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20168                 :          10 :         RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1));
   20169                 :             :       }
   20170                 :             : 
   20171                 :    12297185 :     case NOP_EXPR:
   20172                 :    12297185 :       {
   20173                 :    12297185 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20174                 :    12297185 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20175                 :    12297181 :         RETURN (build_nop (type, op0));
   20176                 :             :       }
   20177                 :             : 
   20178                 :    20002181 :     case IMPLICIT_CONV_EXPR:
   20179                 :    20002181 :       {
   20180                 :    20002181 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20181                 :    20002181 :         tree expr = RECUR (TREE_OPERAND (t, 0));
   20182                 :    20002181 :         if (dependent_type_p (type) || type_dependent_expression_p (expr))
   20183                 :             :           {
   20184                 :     4679390 :             retval = copy_node (t);
   20185                 :     4679390 :             TREE_TYPE (retval) = type;
   20186                 :     4679390 :             TREE_OPERAND (retval, 0) = expr;
   20187                 :     4679390 :             RETURN (retval);
   20188                 :             :           }
   20189                 :    15322791 :         if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
   20190                 :             :           {
   20191                 :    14419892 :             tree r = convert_nontype_argument (type, expr, complain);
   20192                 :    14419892 :             if (r == NULL_TREE)
   20193                 :          27 :               r = error_mark_node;
   20194                 :    14419892 :             RETURN (r);
   20195                 :             :           }
   20196                 :      902899 :         int flags = LOOKUP_IMPLICIT;
   20197                 :      902899 :         if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
   20198                 :      152252 :           flags = LOOKUP_NORMAL;
   20199                 :      902899 :         if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
   20200                 :        4767 :           flags |= LOOKUP_NO_NARROWING;
   20201                 :      902899 :         RETURN (perform_implicit_conversion_flags (type, expr, complain,
   20202                 :             :                                                   flags));
   20203                 :             :       }
   20204                 :             : 
   20205                 :        7274 :     case CONVERT_EXPR:
   20206                 :        7274 :       {
   20207                 :        7274 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20208                 :        7274 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20209                 :        7274 :         if (op0 == error_mark_node)
   20210                 :          29 :           RETURN (error_mark_node);
   20211                 :        7245 :         RETURN (build1 (CONVERT_EXPR, type, op0));
   20212                 :             :       }
   20213                 :             : 
   20214                 :    28658230 :     case CAST_EXPR:
   20215                 :    28658230 :     case REINTERPRET_CAST_EXPR:
   20216                 :    28658230 :     case CONST_CAST_EXPR:
   20217                 :    28658230 :     case DYNAMIC_CAST_EXPR:
   20218                 :    28658230 :     case STATIC_CAST_EXPR:
   20219                 :    28658230 :       {
   20220                 :    28658230 :         tree type;
   20221                 :    28658230 :         tree op, r = NULL_TREE;
   20222                 :             : 
   20223                 :    28658230 :         tsubst_flags_t tcomplain = complain;
   20224                 :    28658230 :         if (TREE_CODE (t) == CAST_EXPR)
   20225                 :    25477385 :           tcomplain |= tf_tst_ok;
   20226                 :    28658230 :         type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   20227                 :             : 
   20228                 :    28658230 :         op = RECUR (TREE_OPERAND (t, 0));
   20229                 :             : 
   20230                 :    28658230 :         warning_sentinel s(warn_useless_cast);
   20231                 :    28658230 :         warning_sentinel s2(warn_ignored_qualifiers);
   20232                 :    28658230 :         warning_sentinel s3(warn_int_in_bool_context);
   20233                 :    28658230 :         switch (TREE_CODE (t))
   20234                 :             :           {
   20235                 :    25477385 :           case CAST_EXPR:
   20236                 :    25477385 :             r = build_functional_cast (input_location, type, op, complain);
   20237                 :    25477385 :             break;
   20238                 :       45378 :           case REINTERPRET_CAST_EXPR:
   20239                 :       45378 :             r = build_reinterpret_cast (input_location, type, op, complain);
   20240                 :       45378 :             break;
   20241                 :      117355 :           case CONST_CAST_EXPR:
   20242                 :      117355 :             r = build_const_cast (input_location, type, op, complain);
   20243                 :      117355 :             break;
   20244                 :        6818 :           case DYNAMIC_CAST_EXPR:
   20245                 :        6818 :             r = build_dynamic_cast (input_location, type, op, complain);
   20246                 :        6818 :             break;
   20247                 :     3011294 :           case STATIC_CAST_EXPR:
   20248                 :     3011294 :             r = build_static_cast (input_location, type, op, complain);
   20249                 :     3011294 :             if (IMPLICIT_RVALUE_P (t))
   20250                 :           0 :               set_implicit_rvalue_p (r);
   20251                 :             :             break;
   20252                 :           0 :           default:
   20253                 :           0 :             gcc_unreachable ();
   20254                 :             :           }
   20255                 :             : 
   20256                 :    28658227 :         RETURN (r);
   20257                 :    28658227 :       }
   20258                 :             : 
   20259                 :         229 :     case BIT_CAST_EXPR:
   20260                 :         229 :       {
   20261                 :         229 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20262                 :         229 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20263                 :         229 :         RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
   20264                 :             :       }
   20265                 :             : 
   20266                 :      419576 :     case POSTDECREMENT_EXPR:
   20267                 :      419576 :     case POSTINCREMENT_EXPR:
   20268                 :      419576 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   20269                 :             :                                                 args, complain, in_decl);
   20270                 :      419576 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
   20271                 :             :                                 templated_operator_saved_lookups (t),
   20272                 :             :                                 complain|decltype_flag));
   20273                 :             : 
   20274                 :      361266 :     case BIT_NOT_EXPR:
   20275                 :      361266 :       if (identifier_p (TREE_OPERAND (t, 0)))
   20276                 :             :         {
   20277                 :          19 :           gcc_checking_assert (no_name_lookup_flag);
   20278                 :          19 :           RETURN (t);
   20279                 :             :         }
   20280                 :      361247 :       else if (TYPE_P (TREE_OPERAND (t, 0)))
   20281                 :             :         {
   20282                 :      168773 :           gcc_checking_assert (no_name_lookup_flag);
   20283                 :      168773 :           tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
   20284                 :      168773 :           RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0));
   20285                 :             :         }
   20286                 :             :       /* Fall through.  */
   20287                 :    14580485 :     case PREDECREMENT_EXPR:
   20288                 :    14580485 :     case PREINCREMENT_EXPR:
   20289                 :    14580485 :     case NEGATE_EXPR:
   20290                 :    14580485 :     case ABS_EXPR:
   20291                 :    14580485 :     case TRUTH_NOT_EXPR:
   20292                 :    14580485 :     case UNARY_PLUS_EXPR:  /* Unary + */
   20293                 :    14580485 :     case REALPART_EXPR:
   20294                 :    14580485 :     case IMAGPART_EXPR:
   20295                 :    14580485 :       RETURN (build_x_unary_op (input_location, TREE_CODE (t),
   20296                 :             :                                 RECUR (TREE_OPERAND (t, 0)),
   20297                 :             :                                 templated_operator_saved_lookups (t),
   20298                 :             :                                 complain|decltype_flag));
   20299                 :             : 
   20300                 :        3263 :     case EXCESS_PRECISION_EXPR:
   20301                 :        3263 :       {
   20302                 :        3263 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   20303                 :        3263 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20304                 :        3263 :         if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
   20305                 :           0 :           RETURN (op0);
   20306                 :        3263 :         RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR,
   20307                 :             :                             type, op0));
   20308                 :             :       }
   20309                 :             : 
   20310                 :           0 :     case FIX_TRUNC_EXPR:
   20311                 :             :       /* convert_like should have created an IMPLICIT_CONV_EXPR.  */
   20312                 :           0 :       gcc_unreachable ();
   20313                 :             : 
   20314                 :      891677 :     case ADDR_EXPR:
   20315                 :      891677 :       op1 = TREE_OPERAND (t, 0);
   20316                 :      891677 :       if (TREE_CODE (op1) == LABEL_DECL)
   20317                 :          36 :         RETURN (finish_label_address_expr (DECL_NAME (op1),
   20318                 :             :                                           EXPR_LOCATION (op1)));
   20319                 :      891641 :       if (TREE_CODE (op1) == SCOPE_REF)
   20320                 :       73129 :         op1 = tsubst_qualified_id (op1, args, complain, in_decl,
   20321                 :             :                                    /*done=*/true, /*address_p=*/true);
   20322                 :             :       else
   20323                 :      818512 :         op1 = tsubst_non_call_postfix_expression (op1, args, complain,
   20324                 :             :                                                   in_decl);
   20325                 :      891641 :       RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
   20326                 :             :                                 templated_operator_saved_lookups (t),
   20327                 :             :                                 complain|decltype_flag));
   20328                 :             : 
   20329                 :    64143588 :     case PLUS_EXPR:
   20330                 :    64143588 :     case MINUS_EXPR:
   20331                 :    64143588 :     case MULT_EXPR:
   20332                 :    64143588 :     case TRUNC_DIV_EXPR:
   20333                 :    64143588 :     case CEIL_DIV_EXPR:
   20334                 :    64143588 :     case FLOOR_DIV_EXPR:
   20335                 :    64143588 :     case ROUND_DIV_EXPR:
   20336                 :    64143588 :     case EXACT_DIV_EXPR:
   20337                 :    64143588 :     case BIT_AND_EXPR:
   20338                 :    64143588 :     case BIT_IOR_EXPR:
   20339                 :    64143588 :     case BIT_XOR_EXPR:
   20340                 :    64143588 :     case TRUNC_MOD_EXPR:
   20341                 :    64143588 :     case FLOOR_MOD_EXPR:
   20342                 :    64143588 :     case TRUTH_ANDIF_EXPR:
   20343                 :    64143588 :     case TRUTH_ORIF_EXPR:
   20344                 :    64143588 :     case TRUTH_AND_EXPR:
   20345                 :    64143588 :     case TRUTH_OR_EXPR:
   20346                 :    64143588 :     case RSHIFT_EXPR:
   20347                 :    64143588 :     case LSHIFT_EXPR:
   20348                 :    64143588 :     case EQ_EXPR:
   20349                 :    64143588 :     case NE_EXPR:
   20350                 :    64143588 :     case MAX_EXPR:
   20351                 :    64143588 :     case MIN_EXPR:
   20352                 :    64143588 :     case LE_EXPR:
   20353                 :    64143588 :     case GE_EXPR:
   20354                 :    64143588 :     case LT_EXPR:
   20355                 :    64143588 :     case GT_EXPR:
   20356                 :    64143588 :     case SPACESHIP_EXPR:
   20357                 :    64143588 :     case MEMBER_REF:
   20358                 :    64143588 :     case DOTSTAR_EXPR:
   20359                 :    64143588 :       {
   20360                 :             :         /* If either OP0 or OP1 was value- or type-dependent, suppress
   20361                 :             :            warnings that depend on the range of the types involved.  */
   20362                 :    64143588 :         tree op0 = TREE_OPERAND (t, 0);
   20363                 :    64143588 :         tree op1 = TREE_OPERAND (t, 1);
   20364                 :    64143588 :         const bool was_dep = (dependent_operand_p (op0)
   20365                 :    64143588 :                               || dependent_operand_p (op1));
   20366                 :    64143588 :         op0 = RECUR (op0);
   20367                 :    64143588 :         op1 = RECUR (op1);
   20368                 :             : 
   20369                 :    64143528 :         warning_sentinel s1(warn_type_limits, was_dep);
   20370                 :    64143528 :         warning_sentinel s2(warn_div_by_zero, was_dep);
   20371                 :    64143528 :         warning_sentinel s3(warn_logical_op, was_dep);
   20372                 :    64143528 :         warning_sentinel s4(warn_tautological_compare, was_dep);
   20373                 :    64143528 :         warning_sentinel s5(warn_address, was_dep);
   20374                 :             : 
   20375                 :    64143528 :         tree r = build_x_binary_op
   20376                 :   245735656 :           (input_location, TREE_CODE (t),
   20377                 :             :            op0,
   20378                 :    64143528 :            (warning_suppressed_p (TREE_OPERAND (t, 0))
   20379                 :             :             ? ERROR_MARK
   20380                 :    53305072 :             : TREE_CODE (TREE_OPERAND (t, 0))),
   20381                 :             :            op1,
   20382                 :    64143528 :            (warning_suppressed_p (TREE_OPERAND (t, 1))
   20383                 :             :             ? ERROR_MARK
   20384                 :    53561667 :             : TREE_CODE (TREE_OPERAND (t, 1))),
   20385                 :             :            templated_operator_saved_lookups (t),
   20386                 :             :            /*overload=*/NULL,
   20387                 :             :            complain|decltype_flag);
   20388                 :    64143498 :         if (EXPR_P (r))
   20389                 :    64122186 :           copy_warning (r, t);
   20390                 :             : 
   20391                 :    64143498 :         RETURN (r);
   20392                 :    64143498 :       }
   20393                 :             : 
   20394                 :           6 :     case POINTER_PLUS_EXPR:
   20395                 :           6 :       {
   20396                 :           6 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20397                 :           6 :         if (op0 == error_mark_node)
   20398                 :           0 :           RETURN (error_mark_node);
   20399                 :           6 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   20400                 :           6 :         if (op1 == error_mark_node)
   20401                 :           0 :           RETURN (error_mark_node);
   20402                 :           6 :         RETURN (fold_build_pointer_plus (op0, op1));
   20403                 :             :       }
   20404                 :             : 
   20405                 :    54926728 :     case SCOPE_REF:
   20406                 :    54926728 :       if (no_name_lookup_flag)
   20407                 :             :         {
   20408                 :         749 :           tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl);
   20409                 :         749 :           tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl);
   20410                 :         749 :           RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1,
   20411                 :             :                                         QUALIFIED_NAME_IS_TEMPLATE (t)));
   20412                 :             :         }
   20413                 :             :       else
   20414                 :    54925979 :         RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
   20415                 :             :                                     /*address_p=*/false));
   20416                 :             : 
   20417                 :     9810700 :     case BASELINK:
   20418                 :     9810700 :       RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
   20419                 :             :                                args, complain, in_decl));
   20420                 :             : 
   20421                 :     1633180 :     case ARRAY_REF:
   20422                 :     1633180 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   20423                 :             :                                                 args, complain, in_decl);
   20424                 :     1633180 :       if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
   20425                 :     1633180 :           && (CALL_EXPR_FN (TREE_OPERAND (t, 1))
   20426                 :        1922 :               == ovl_op_identifier (ARRAY_REF)))
   20427                 :             :         {
   20428                 :         103 :           tree c = TREE_OPERAND (t, 1);
   20429                 :         103 :           releasing_vec index_exp_list;
   20430                 :         103 :           tsubst_call_args (c, args, complain, in_decl, index_exp_list);
   20431                 :             : 
   20432                 :         103 :           tree r;
   20433                 :         103 :           if (vec_safe_length (index_exp_list) == 1
   20434                 :         116 :               && !PACK_EXPANSION_P (index_exp_list[0]))
   20435                 :          13 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   20436                 :          13 :                                  index_exp_list[0], NULL,
   20437                 :             :                                  complain | decltype_flag);
   20438                 :             :           else
   20439                 :          90 :             r = grok_array_decl (EXPR_LOCATION (t), op1,
   20440                 :             :                                  NULL_TREE, &index_exp_list,
   20441                 :             :                                  complain | decltype_flag);
   20442                 :         103 :           RETURN (r);
   20443                 :         103 :         }
   20444                 :     1633077 :       RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
   20445                 :             :                                  RECUR (TREE_OPERAND (t, 1)),
   20446                 :             :                                  complain|decltype_flag));
   20447                 :             : 
   20448                 :          40 :     case OMP_ARRAY_SECTION:
   20449                 :          40 :       {
   20450                 :          40 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20451                 :          40 :         tree op1 = NULL_TREE, op2 = NULL_TREE;
   20452                 :          40 :         if (op0 == error_mark_node)
   20453                 :           0 :           RETURN (error_mark_node);
   20454                 :          40 :         if (TREE_OPERAND (t, 1))
   20455                 :             :           {
   20456                 :          36 :             op1 = RECUR (TREE_OPERAND (t, 1));
   20457                 :          36 :             if (op1 == error_mark_node)
   20458                 :           0 :               RETURN (error_mark_node);
   20459                 :             :           }
   20460                 :          40 :         if (TREE_OPERAND (t, 2))
   20461                 :             :           {
   20462                 :          40 :             op2 = RECUR (TREE_OPERAND (t, 2));
   20463                 :          40 :             if (op2 == error_mark_node)
   20464                 :           0 :               RETURN (error_mark_node);
   20465                 :             :           }
   20466                 :          40 :         RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2));
   20467                 :             :       }
   20468                 :             : 
   20469                 :     5184258 :     case SIZEOF_EXPR:
   20470                 :     9284592 :       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
   20471                 :     9283761 :           || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
   20472                 :             :         {
   20473                 :     1084767 :           tree expanded, op = TREE_OPERAND (t, 0);
   20474                 :     1084767 :           int len = 0;
   20475                 :             : 
   20476                 :     1084767 :           if (SIZEOF_EXPR_TYPE_P (t))
   20477                 :           0 :             op = TREE_TYPE (op);
   20478                 :             : 
   20479                 :     1084767 :           ++cp_unevaluated_operand;
   20480                 :     1084767 :           ++c_inhibit_evaluation_warnings;
   20481                 :             :           /* We only want to compute the number of arguments.  */
   20482                 :     1084767 :           if (PACK_EXPANSION_P (op))
   20483                 :     1084755 :             expanded = tsubst_pack_expansion (op, args, complain, in_decl);
   20484                 :             :           else
   20485                 :          12 :             expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
   20486                 :             :                                              args, complain, in_decl);
   20487                 :     1084767 :           --cp_unevaluated_operand;
   20488                 :     1084767 :           --c_inhibit_evaluation_warnings;
   20489                 :             : 
   20490                 :     1084767 :           if (TREE_CODE (expanded) == TREE_VEC)
   20491                 :             :             {
   20492                 :      576710 :               len = TREE_VEC_LENGTH (expanded);
   20493                 :             :               /* Set TREE_USED for the benefit of -Wunused.  */
   20494                 :     1638118 :               for (int i = 0; i < len; i++)
   20495                 :     1061408 :                 if (DECL_P (TREE_VEC_ELT (expanded, i)))
   20496                 :         497 :                   TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
   20497                 :             :             }
   20498                 :             : 
   20499                 :     1084767 :           if (expanded == error_mark_node)
   20500                 :           0 :             RETURN (error_mark_node);
   20501                 :     1084767 :           else if (PACK_EXPANSION_P (expanded)
   20502                 :     1084767 :                    || (TREE_CODE (expanded) == TREE_VEC
   20503                 :      576710 :                        && pack_expansion_args_count (expanded)))
   20504                 :             : 
   20505                 :             :             {
   20506                 :      508084 :               if (PACK_EXPANSION_P (expanded))
   20507                 :             :                 /* OK.  */;
   20508                 :             :               else
   20509                 :          27 :                 expanded = make_argument_pack (expanded);
   20510                 :             : 
   20511                 :      508084 :               if (TYPE_P (expanded))
   20512                 :      507850 :                 RETURN (cxx_sizeof_or_alignof_type (input_location,
   20513                 :             :                                                     expanded, SIZEOF_EXPR,
   20514                 :             :                                                     false,
   20515                 :             :                                                     complain & tf_error));
   20516                 :             :               else
   20517                 :         234 :                 RETURN (cxx_sizeof_or_alignof_expr (input_location,
   20518                 :             :                                                     expanded, SIZEOF_EXPR,
   20519                 :             :                                                     false,
   20520                 :             :                                                     complain & tf_error));
   20521                 :             :             }
   20522                 :             :           else
   20523                 :      576683 :             RETURN (build_int_cst (size_type_node, len));
   20524                 :             :         }
   20525                 :             :       /* Fall through */
   20526                 :             : 
   20527                 :     4857552 :     case ALIGNOF_EXPR:
   20528                 :     4857552 :       {
   20529                 :     4857552 :         tree r;
   20530                 :             : 
   20531                 :     4857552 :         op1 = TREE_OPERAND (t, 0);
   20532                 :     4857552 :         if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
   20533                 :           0 :           op1 = TREE_TYPE (op1);
   20534                 :     4857552 :         bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
   20535                 :     4857552 :                             && ALIGNOF_EXPR_STD_P (t));
   20536                 :     4857552 :         if (!args)
   20537                 :             :           {
   20538                 :             :             /* When there are no ARGS, we are trying to evaluate a
   20539                 :             :                non-dependent expression from the parser.  Trying to do
   20540                 :             :                the substitutions may not work.  */
   20541                 :      152366 :             if (!TYPE_P (op1))
   20542                 :       20822 :               op1 = TREE_TYPE (op1);
   20543                 :             :           }
   20544                 :             :         else
   20545                 :             :           {
   20546                 :     4705186 :             ++cp_unevaluated_operand;
   20547                 :     4705186 :             ++c_inhibit_evaluation_warnings;
   20548                 :     4705186 :             if (TYPE_P (op1))
   20549                 :     4517955 :               op1 = tsubst (op1, args, complain, in_decl);
   20550                 :             :             else
   20551                 :      187231 :               op1 = tsubst_expr (op1, args, complain, in_decl);
   20552                 :     4705186 :             --cp_unevaluated_operand;
   20553                 :     4705186 :             --c_inhibit_evaluation_warnings;
   20554                 :             :           }
   20555                 :     4857552 :         if (TYPE_P (op1))
   20556                 :     4670310 :           r = cxx_sizeof_or_alignof_type (input_location,
   20557                 :     4670310 :                                           op1, TREE_CODE (t), std_alignof,
   20558                 :             :                                           complain & tf_error);
   20559                 :             :         else
   20560                 :      187242 :           r = cxx_sizeof_or_alignof_expr (input_location,
   20561                 :      187242 :                                           op1, TREE_CODE (t), std_alignof,
   20562                 :             :                                           complain & tf_error);
   20563                 :     4857552 :         if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
   20564                 :             :           {
   20565                 :     4096402 :             if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
   20566                 :             :               {
   20567                 :     4096279 :                 if (!processing_template_decl && TYPE_P (op1))
   20568                 :             :                   {
   20569                 :     3691292 :                     r = build_min (SIZEOF_EXPR, size_type_node,
   20570                 :             :                                    build1 (NOP_EXPR, op1, error_mark_node));
   20571                 :     3691292 :                     SIZEOF_EXPR_TYPE_P (r) = 1;
   20572                 :             :                   }
   20573                 :             :                 else
   20574                 :      404987 :                   r = build_min (SIZEOF_EXPR, size_type_node, op1);
   20575                 :     4096279 :                 TREE_SIDE_EFFECTS (r) = 0;
   20576                 :     4096279 :                 TREE_READONLY (r) = 1;
   20577                 :             :               }
   20578                 :     4096402 :             SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
   20579                 :             :           }
   20580                 :     4857552 :         RETURN (r);
   20581                 :             :       }
   20582                 :             : 
   20583                 :           0 :     case AT_ENCODE_EXPR:
   20584                 :           0 :       {
   20585                 :           0 :         op1 = TREE_OPERAND (t, 0);
   20586                 :           0 :         ++cp_unevaluated_operand;
   20587                 :           0 :         ++c_inhibit_evaluation_warnings;
   20588                 :           0 :         op1 = tsubst (op1, args, complain, in_decl);
   20589                 :           0 :         --cp_unevaluated_operand;
   20590                 :           0 :         --c_inhibit_evaluation_warnings;
   20591                 :           0 :         RETURN (objc_build_encode_expr (op1));
   20592                 :             :       }
   20593                 :             : 
   20594                 :      372039 :     case NOEXCEPT_EXPR:
   20595                 :      372039 :       op1 = TREE_OPERAND (t, 0);
   20596                 :      372039 :       ++cp_unevaluated_operand;
   20597                 :      372039 :       ++c_inhibit_evaluation_warnings;
   20598                 :      372039 :       ++cp_noexcept_operand;
   20599                 :      372039 :       op1 = tsubst_expr (op1, args, complain, in_decl);
   20600                 :      372039 :       --cp_unevaluated_operand;
   20601                 :      372039 :       --c_inhibit_evaluation_warnings;
   20602                 :      372039 :       --cp_noexcept_operand;
   20603                 :      372039 :       RETURN (finish_noexcept_expr (op1, complain));
   20604                 :             : 
   20605                 :    13574839 :     case MODOP_EXPR:
   20606                 :    13574839 :       {
   20607                 :    13574839 :         warning_sentinel s(warn_div_by_zero);
   20608                 :    13574839 :         tree lhs = RECUR (TREE_OPERAND (t, 0));
   20609                 :    13574839 :         tree rhs = RECUR (TREE_OPERAND (t, 2));
   20610                 :             : 
   20611                 :    13574839 :         tree r = build_x_modify_expr
   20612                 :    13574839 :           (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
   20613                 :             :            templated_operator_saved_lookups (t),
   20614                 :             :            complain|decltype_flag);
   20615                 :             :         /* TREE_NO_WARNING must be set if either the expression was
   20616                 :             :            parenthesized or it uses an operator such as >>= rather
   20617                 :             :            than plain assignment.  In the former case, it was already
   20618                 :             :            set and must be copied.  In the latter case,
   20619                 :             :            build_x_modify_expr sets it and it must not be reset
   20620                 :             :            here.  */
   20621                 :    13574839 :         if (warning_suppressed_p (t, OPT_Wparentheses))
   20622                 :         682 :           suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
   20623                 :             : 
   20624                 :    13574839 :         RETURN (r);
   20625                 :    13574839 :       }
   20626                 :             : 
   20627                 :     2400000 :     case ARROW_EXPR:
   20628                 :     2400000 :       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   20629                 :             :                                                 args, complain, in_decl);
   20630                 :             :       /* Remember that there was a reference to this entity.  */
   20631                 :     2400000 :       if (DECL_P (op1)
   20632                 :     2400000 :           && !mark_used (op1, complain) && !(complain & tf_error))
   20633                 :           0 :         RETURN (error_mark_node);
   20634                 :     2400000 :       RETURN (build_x_arrow (input_location, op1, complain));
   20635                 :             : 
   20636                 :      243548 :     case NEW_EXPR:
   20637                 :      243548 :       {
   20638                 :      243548 :         tree placement = RECUR (TREE_OPERAND (t, 0));
   20639                 :      243548 :         tree init = RECUR (TREE_OPERAND (t, 3));
   20640                 :      243548 :         vec<tree, va_gc> *placement_vec;
   20641                 :      243548 :         vec<tree, va_gc> *init_vec;
   20642                 :      243548 :         tree ret;
   20643                 :      243548 :         location_t loc = EXPR_LOCATION (t);
   20644                 :             : 
   20645                 :      243548 :         if (placement == NULL_TREE)
   20646                 :       56719 :           placement_vec = NULL;
   20647                 :      186829 :         else if (placement == error_mark_node)
   20648                 :           4 :           RETURN (error_mark_node);
   20649                 :             :         else
   20650                 :             :           {
   20651                 :      186825 :             placement_vec = make_tree_vector ();
   20652                 :      373650 :             for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
   20653                 :      186825 :               vec_safe_push (placement_vec, TREE_VALUE (placement));
   20654                 :             :           }
   20655                 :             : 
   20656                 :             :         /* If there was an initializer in the original tree, but it
   20657                 :             :            instantiated to an empty list, then we should pass a
   20658                 :             :            non-NULL empty vector to tell build_new that it was an
   20659                 :             :            empty initializer() rather than no initializer.  This can
   20660                 :             :            only happen when the initializer is a pack expansion whose
   20661                 :             :            parameter packs are of length zero.  */
   20662                 :      243544 :         if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
   20663                 :       55534 :           init_vec = NULL;
   20664                 :      188010 :         else if (init == error_mark_node)
   20665                 :           6 :           RETURN (error_mark_node);
   20666                 :             :         else
   20667                 :             :           {
   20668                 :      188004 :             init_vec = make_tree_vector ();
   20669                 :      188004 :             if (init == void_node)
   20670                 :         688 :               gcc_assert (init_vec != NULL);
   20671                 :             :             else
   20672                 :             :               {
   20673                 :      342117 :                 for (; init != NULL_TREE; init = TREE_CHAIN (init))
   20674                 :      154801 :                   vec_safe_push (init_vec, TREE_VALUE (init));
   20675                 :             :               }
   20676                 :             :           }
   20677                 :             : 
   20678                 :             :         /* Avoid passing an enclosing decl to valid_array_size_p.  */
   20679                 :      243538 :         in_decl = NULL_TREE;
   20680                 :             : 
   20681                 :      243538 :         tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
   20682                 :      243538 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   20683                 :      243538 :         ret = build_new (loc, &placement_vec, op1, op2,
   20684                 :      243538 :                          &init_vec, NEW_EXPR_USE_GLOBAL (t),
   20685                 :             :                          complain);
   20686                 :             : 
   20687                 :      243538 :         if (placement_vec != NULL)
   20688                 :      243292 :           release_tree_vector (placement_vec);
   20689                 :      243538 :         if (init_vec != NULL)
   20690                 :      150574 :           release_tree_vector (init_vec);
   20691                 :             : 
   20692                 :      243548 :         RETURN (ret);
   20693                 :             :       }
   20694                 :             : 
   20695                 :       41401 :     case DELETE_EXPR:
   20696                 :       41401 :       {
   20697                 :       41401 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   20698                 :       41401 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   20699                 :       41401 :         RETURN (delete_sanity (input_location, op0, op1,
   20700                 :             :                                DELETE_EXPR_USE_VEC (t),
   20701                 :             :                                DELETE_EXPR_USE_GLOBAL (t),
   20702                 :             :                                complain));
   20703                 :             :       }
   20704                 :             : 
   20705                 :       36197 :     case COMPOUND_EXPR:
   20706                 :       36197 :       {
   20707                 :       36197 :         tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args,
   20708                 :             :                                 complain & ~tf_decltype, in_decl);
   20709                 :       36197 :         RETURN (build_x_compound_expr (EXPR_LOCATION (t),
   20710                 :             :                                        op0,
   20711                 :             :                                        RECUR (TREE_OPERAND (t, 1)),
   20712                 :             :                                        templated_operator_saved_lookups (t),
   20713                 :             :                                        complain|decltype_flag));
   20714                 :             :       }
   20715                 :             : 
   20716                 :    73879013 :     case CALL_EXPR:
   20717                 :    73879013 :       {
   20718                 :    73879013 :         tree function;
   20719                 :    73879013 :         unsigned int nargs;
   20720                 :    73879013 :         bool qualified_p;
   20721                 :    73879013 :         bool koenig_p;
   20722                 :    73879013 :         tree ret;
   20723                 :             : 
   20724                 :    73879013 :         function = CALL_EXPR_FN (t);
   20725                 :             :         /* Internal function with no arguments.  */
   20726                 :    73879013 :         if (function == NULL_TREE && call_expr_nargs (t) == 0)
   20727                 :    73775880 :           RETURN (t);
   20728                 :             : 
   20729                 :             :         /* When we parsed the expression, we determined whether or
   20730                 :             :            not Koenig lookup should be performed.  */
   20731                 :    73787608 :         koenig_p = KOENIG_LOOKUP_P (t);
   20732                 :    73787608 :         if (function == NULL_TREE)
   20733                 :             :           {
   20734                 :             :             koenig_p = false;
   20735                 :             :             qualified_p = false;
   20736                 :             :           }
   20737                 :    73787446 :         else if (TREE_CODE (function) == SCOPE_REF)
   20738                 :             :           {
   20739                 :     9626954 :             qualified_p = true;
   20740                 :     9626954 :             function = tsubst_qualified_id (function, args, complain, in_decl,
   20741                 :             :                                             /*done=*/false,
   20742                 :             :                                             /*address_p=*/false);
   20743                 :             :           }
   20744                 :    64160492 :         else if (CALL_EXPR_STATIC_CHAIN (t)
   20745                 :          99 :                  && TREE_CODE (function) == FUNCTION_DECL
   20746                 :    64160591 :                  && fndecl_built_in_p (function, BUILT_IN_CLASSIFY_TYPE))
   20747                 :             :           {
   20748                 :          99 :             tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain,
   20749                 :             :                                 in_decl);
   20750                 :          99 :             if (dependent_type_p (type))
   20751                 :             :               {
   20752                 :           0 :                 ret = build_vl_exp (CALL_EXPR, 4);
   20753                 :           0 :                 CALL_EXPR_FN (ret) = function;
   20754                 :           0 :                 CALL_EXPR_STATIC_CHAIN (ret) = type;
   20755                 :           0 :                 CALL_EXPR_ARG (ret, 0)
   20756                 :           0 :                   = build_min (SIZEOF_EXPR, size_type_node, type);
   20757                 :           0 :                 TREE_TYPE (ret) = integer_type_node;
   20758                 :             :               }
   20759                 :             :             else
   20760                 :          99 :               ret = build_int_cst (integer_type_node, type_to_class (type));
   20761                 :          99 :             RETURN (ret);
   20762                 :             :           }
   20763                 :    64160393 :         else if (koenig_p
   20764                 :    64160393 :                  && (identifier_p (function)
   20765                 :     5575007 :                      || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   20766                 :      894948 :                          && identifier_p (TREE_OPERAND (function, 0)))))
   20767                 :             :           {
   20768                 :             :             /* Do nothing; calling tsubst_expr on an identifier
   20769                 :             :                would incorrectly perform unqualified lookup again.
   20770                 :             : 
   20771                 :             :                Note that we can also have an IDENTIFIER_NODE if the earlier
   20772                 :             :                unqualified lookup found a dependent local extern declaration
   20773                 :             :                (as per finish_call_expr); in that case koenig_p will be false
   20774                 :             :                and we do want to do the lookup again to find the substituted
   20775                 :             :                declaration.  */
   20776                 :      124385 :             qualified_p = false;
   20777                 :             : 
   20778                 :      124385 :             if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
   20779                 :         908 :               function = tsubst_name (function, args, complain, in_decl);
   20780                 :             :           }
   20781                 :             :         else
   20782                 :             :           {
   20783                 :    64036008 :             if (TREE_CODE (function) == COMPONENT_REF)
   20784                 :             :               {
   20785                 :    11861256 :                 tree op = TREE_OPERAND (function, 1);
   20786                 :             : 
   20787                 :    11861256 :                 qualified_p = (TREE_CODE (op) == SCOPE_REF
   20788                 :    11861256 :                                || (BASELINK_P (op)
   20789                 :     7636132 :                                    && BASELINK_QUALIFIED_P (op)));
   20790                 :             :               }
   20791                 :             :             else
   20792                 :             :               qualified_p = false;
   20793                 :             : 
   20794                 :    64036008 :             if (TREE_CODE (function) == ADDR_EXPR
   20795                 :    64036008 :                 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
   20796                 :             :               /* Avoid error about taking the address of a constructor.  */
   20797                 :       12942 :               function = TREE_OPERAND (function, 0);
   20798                 :             : 
   20799                 :    64036008 :             tsubst_flags_t subcomplain = complain;
   20800                 :    64036008 :             if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
   20801                 :             :               /* When KOENIG_P, we don't want to mark_used the callee before
   20802                 :             :                  augmenting the overload set via ADL, so during this initial
   20803                 :             :                  substitution we disable mark_used by setting tf_conv (68942).  */
   20804                 :     1740002 :               subcomplain |= tf_conv;
   20805                 :    64036008 :             function = tsubst_expr (function, args, subcomplain, in_decl);
   20806                 :             : 
   20807                 :    64036008 :             if (BASELINK_P (function))
   20808                 :     9692346 :               qualified_p = true;
   20809                 :             :           }
   20810                 :             : 
   20811                 :    73787509 :         nargs = call_expr_nargs (t);
   20812                 :    73787509 :         releasing_vec call_args;
   20813                 :    73787509 :         tsubst_call_args (t, args, complain, in_decl, call_args);
   20814                 :             : 
   20815                 :             :         /* Stripped-down processing for a call in a thunk.  Specifically, in
   20816                 :             :            the thunk template for a generic lambda.  */
   20817                 :    73787506 :         if (call_from_lambda_thunk_p (t))
   20818                 :             :           {
   20819                 :             :             /* Now that we've expanded any packs, the number of call args
   20820                 :             :                might be different.  */
   20821                 :         103 :             unsigned int cargs = call_args->length ();
   20822                 :         103 :             tree thisarg = NULL_TREE;
   20823                 :         103 :             if (TREE_CODE (function) == COMPONENT_REF)
   20824                 :             :               {
   20825                 :         103 :                 thisarg = TREE_OPERAND (function, 0);
   20826                 :         103 :                 if (TREE_CODE (thisarg) == INDIRECT_REF)
   20827                 :         103 :                   thisarg = TREE_OPERAND (thisarg, 0);
   20828                 :         103 :                 function = TREE_OPERAND (function, 1);
   20829                 :         103 :                 if (TREE_CODE (function) == BASELINK)
   20830                 :         103 :                   function = BASELINK_FUNCTIONS (function);
   20831                 :             :               }
   20832                 :             :             /* We aren't going to do normal overload resolution, so force the
   20833                 :             :                template-id to resolve.  */
   20834                 :         103 :             function = resolve_nondeduced_context (function, complain);
   20835                 :         323 :             for (unsigned i = 0; i < cargs; ++i)
   20836                 :             :               {
   20837                 :             :                 /* In a thunk, pass through args directly, without any
   20838                 :             :                    conversions.  */
   20839                 :         220 :                 tree arg = (*call_args)[i];
   20840                 :         220 :                 while (TREE_CODE (arg) != PARM_DECL)
   20841                 :           0 :                   arg = TREE_OPERAND (arg, 0);
   20842                 :         220 :                 (*call_args)[i] = arg;
   20843                 :             :               }
   20844                 :         103 :             if (thisarg)
   20845                 :             :               {
   20846                 :             :                 /* If there are no other args, just push 'this'.  */
   20847                 :         103 :                 if (cargs == 0)
   20848                 :           9 :                   vec_safe_push (call_args, thisarg);
   20849                 :             :                 else
   20850                 :             :                   {
   20851                 :             :                     /* Otherwise, shift the other args over to make room.  */
   20852                 :          94 :                     tree last = (*call_args)[cargs - 1];
   20853                 :          94 :                     vec_safe_push (call_args, last);
   20854                 :         220 :                     for (int i = cargs - 1; i > 0; --i)
   20855                 :         126 :                       (*call_args)[i] = (*call_args)[i - 1];
   20856                 :          94 :                     (*call_args)[0] = thisarg;
   20857                 :             :                   }
   20858                 :             :               }
   20859                 :         103 :             ret = build_call_a (function, call_args->length (),
   20860                 :             :                                 call_args->address ());
   20861                 :             :             /* The thunk location is not interesting.  */
   20862                 :         103 :             SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
   20863                 :         103 :             CALL_FROM_THUNK_P (ret) = true;
   20864                 :         103 :             if (CLASS_TYPE_P (TREE_TYPE (ret)))
   20865                 :           9 :               CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
   20866                 :             : 
   20867                 :         103 :             RETURN (ret);
   20868                 :             :           }
   20869                 :             : 
   20870                 :             :         /* We do not perform argument-dependent lookup if normal
   20871                 :             :            lookup finds a non-function, in accordance with the
   20872                 :             :            resolution of DR 218.  */
   20873                 :    73787403 :         if (koenig_p
   20874                 :     5698481 :             && ((is_overloaded_fn (function)
   20875                 :             :                  /* If lookup found a member function, the Koenig lookup is
   20876                 :             :                     not appropriate, even if an unqualified-name was used
   20877                 :             :                     to denote the function.  */
   20878                 :     5574096 :                  && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
   20879                 :     5699389 :                 || identifier_p (function)
   20880                 :             :                 /* C++20 P0846: Lookup found nothing.  */
   20881                 :         908 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   20882                 :         908 :                     && identifier_p (TREE_OPERAND (function, 0))))
   20883                 :             :             /* Only do this when substitution turns a dependent call
   20884                 :             :                into a non-dependent call.  */
   20885                 :     5698481 :             && type_dependent_expression_p_push (t)
   20886                 :    78257652 :             && !any_type_dependent_arguments_p (call_args))
   20887                 :     4370971 :           function = perform_koenig_lookup (function, call_args, tf_none);
   20888                 :             : 
   20889                 :    73787403 :         if (function != NULL_TREE
   20890                 :      115736 :             && (identifier_p (function)
   20891                 :    73671507 :                 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
   20892                 :    15018534 :                     && identifier_p (TREE_OPERAND (function, 0))
   20893                 :           3 :                     && !any_dependent_template_arguments_p (TREE_OPERAND
   20894                 :             :                                                             (function, 1))))
   20895                 :    73903139 :             && !any_type_dependent_arguments_p (call_args))
   20896                 :             :           {
   20897                 :       20086 :             bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR);
   20898                 :       20086 :             if (template_id_p)
   20899                 :           2 :               function = TREE_OPERAND (function, 0);
   20900                 :       20086 :             if (koenig_p && (complain & tf_warning_or_error))
   20901                 :             :               {
   20902                 :             :                 /* For backwards compatibility and good diagnostics, try
   20903                 :             :                    the unqualified lookup again if we aren't in SFINAE
   20904                 :             :                    context.  */
   20905                 :          42 :                 tree unq = tsubst_expr (function, args, complain, in_decl);
   20906                 :          42 :                 if (unq == error_mark_node)
   20907                 :          15 :                   RETURN (error_mark_node);
   20908                 :             : 
   20909                 :          27 :                 if (unq != function)
   20910                 :             :                   {
   20911                 :          27 :                     char const *const msg
   20912                 :             :                       = G_("%qD was not declared in this scope, "
   20913                 :             :                            "and no declarations were found by "
   20914                 :             :                            "argument-dependent lookup at the point "
   20915                 :             :                            "of instantiation");
   20916                 :             : 
   20917                 :          27 :                     bool in_lambda = (current_class_type
   20918                 :          43 :                                       && LAMBDA_TYPE_P (current_class_type));
   20919                 :             :                     /* In a lambda fn, we have to be careful to not
   20920                 :             :                        introduce new this captures.  Legacy code can't
   20921                 :             :                        be using lambdas anyway, so it's ok to be
   20922                 :             :                        stricter.  Be strict with C++20 template-id ADL too.
   20923                 :             :                        And be strict if we're already failing anyway.  */
   20924                 :          27 :                     bool strict = in_lambda || template_id_p || seen_error();
   20925                 :          27 :                     bool diag = true;
   20926                 :          27 :                     if (strict)
   20927                 :          15 :                       error_at (cp_expr_loc_or_input_loc (t),
   20928                 :             :                                 msg, function);
   20929                 :             :                     else
   20930                 :          12 :                       diag = permerror (cp_expr_loc_or_input_loc (t),
   20931                 :             :                                         msg, function);
   20932                 :          27 :                     if (diag)
   20933                 :             :                       {
   20934                 :          27 :                         tree fn = unq;
   20935                 :             : 
   20936                 :          27 :                         if (INDIRECT_REF_P (fn))
   20937                 :           4 :                           fn = TREE_OPERAND (fn, 0);
   20938                 :          27 :                         if (is_overloaded_fn (fn))
   20939                 :          23 :                           fn = get_first_fn (fn);
   20940                 :             : 
   20941                 :          27 :                         if (!DECL_P (fn))
   20942                 :             :                           /* Can't say anything more.  */;
   20943                 :          23 :                         else if (DECL_CLASS_SCOPE_P (fn))
   20944                 :             :                           {
   20945                 :          12 :                             location_t loc = cp_expr_loc_or_input_loc (t);
   20946                 :          12 :                             inform (loc,
   20947                 :             :                                     "declarations in dependent base %qT are "
   20948                 :             :                                     "not found by unqualified lookup",
   20949                 :          12 :                                     DECL_CLASS_CONTEXT (fn));
   20950                 :          12 :                             if (current_class_ptr)
   20951                 :           8 :                               inform (loc,
   20952                 :             :                                       "use %<this->%D%> instead", function);
   20953                 :             :                             else
   20954                 :           4 :                               inform (loc,
   20955                 :             :                                       "use %<%T::%D%> instead",
   20956                 :             :                                       current_class_name, function);
   20957                 :             :                           }
   20958                 :             :                         else
   20959                 :          11 :                           inform (DECL_SOURCE_LOCATION (fn),
   20960                 :             :                                   "%qD declared here, later in the "
   20961                 :             :                                   "translation unit", fn);
   20962                 :          27 :                         if (strict)
   20963                 :          15 :                           RETURN (error_mark_node);
   20964                 :             :                       }
   20965                 :             : 
   20966                 :             :                     function = unq;
   20967                 :             :                   }
   20968                 :             :               }
   20969                 :       20056 :             if (identifier_p (function))
   20970                 :             :               {
   20971                 :       20044 :                 if (complain & tf_error)
   20972                 :           0 :                   unqualified_name_lookup_error (function);
   20973                 :       20044 :                 RETURN (error_mark_node);
   20974                 :             :               }
   20975                 :             :           }
   20976                 :             : 
   20977                 :             :         /* Remember that there was a reference to this entity.  */
   20978                 :    73767329 :         if (function != NULL_TREE
   20979                 :    73767167 :             && DECL_P (function)
   20980                 :    80735572 :             && !mark_used (function, complain) && !(complain & tf_error))
   20981                 :       33375 :           RETURN (error_mark_node);
   20982                 :             : 
   20983                 :    73733954 :         if (!maybe_fold_fn_template_args (function, complain))
   20984                 :       91426 :           return error_mark_node;
   20985                 :             : 
   20986                 :             :         /* Put back tf_decltype for the actual call.  */
   20987                 :    73642528 :         complain |= decltype_flag;
   20988                 :             : 
   20989                 :    73642528 :         if (function == NULL_TREE)
   20990                 :         162 :           switch (CALL_EXPR_IFN (t))
   20991                 :             :             {
   20992                 :          45 :             case IFN_LAUNDER:
   20993                 :          45 :               gcc_assert (nargs == 1);
   20994                 :          45 :               if (vec_safe_length (call_args) != 1)
   20995                 :             :                 {
   20996                 :           6 :                   error_at (cp_expr_loc_or_input_loc (t),
   20997                 :             :                             "wrong number of arguments to "
   20998                 :             :                             "%<__builtin_launder%>");
   20999                 :           6 :                   ret = error_mark_node;
   21000                 :             :                 }
   21001                 :             :               else
   21002                 :          39 :                 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
   21003                 :          39 :                                               (*call_args)[0], complain);
   21004                 :             :               break;
   21005                 :             : 
   21006                 :          36 :             case IFN_VEC_CONVERT:
   21007                 :          36 :               gcc_assert (nargs == 1);
   21008                 :          36 :               if (vec_safe_length (call_args) != 1)
   21009                 :             :                 {
   21010                 :           0 :                   error_at (cp_expr_loc_or_input_loc (t),
   21011                 :             :                             "wrong number of arguments to "
   21012                 :             :                             "%<__builtin_convertvector%>");
   21013                 :           0 :                   ret = error_mark_node;
   21014                 :           0 :                   break;
   21015                 :             :                 }
   21016                 :          36 :               ret = cp_build_vec_convert ((*call_args)[0], input_location,
   21017                 :          36 :                                           tsubst (TREE_TYPE (t), args,
   21018                 :             :                                                   complain, in_decl),
   21019                 :             :                                           complain);
   21020                 :          36 :               if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
   21021                 :           4 :                 RETURN (ret);
   21022                 :             :               break;
   21023                 :             : 
   21024                 :          16 :             case IFN_SHUFFLEVECTOR:
   21025                 :          16 :               {
   21026                 :          16 :                 ret = build_x_shufflevector (input_location, call_args,
   21027                 :             :                                              complain);
   21028                 :          16 :                 if (ret != error_mark_node)
   21029                 :          12 :                   RETURN (ret);
   21030                 :             :                 break;
   21031                 :             :               }
   21032                 :             : 
   21033                 :          65 :             case IFN_ASSUME:
   21034                 :          65 :               gcc_assert (nargs == 1);
   21035                 :          65 :               if (vec_safe_length (call_args) != 1)
   21036                 :             :                 {
   21037                 :           0 :                   error_at (cp_expr_loc_or_input_loc (t),
   21038                 :             :                             "wrong number of arguments to "
   21039                 :             :                             "%<assume%> attribute");
   21040                 :           0 :                   ret = error_mark_node;
   21041                 :             :                 }
   21042                 :             :               else
   21043                 :             :                 {
   21044                 :          65 :                   tree &arg = (*call_args)[0];
   21045                 :          65 :                   if (!type_dependent_expression_p (arg))
   21046                 :          65 :                     arg = contextual_conv_bool (arg, tf_warning_or_error);
   21047                 :          65 :                   if (error_operand_p (arg))
   21048                 :             :                     {
   21049                 :           6 :                       ret = error_mark_node;
   21050                 :           6 :                       break;
   21051                 :             :                     }
   21052                 :          59 :                   ret = build_assume_call (EXPR_LOCATION (t), arg);
   21053                 :          59 :                   RETURN (ret);
   21054                 :             :                 }
   21055                 :           0 :               break;
   21056                 :             : 
   21057                 :           0 :             default:
   21058                 :             :               /* Unsupported internal function with arguments.  */
   21059                 :           0 :               gcc_unreachable ();
   21060                 :             :             }
   21061                 :    73642366 :         else if (TREE_CODE (function) == OFFSET_REF
   21062                 :             :                  || TREE_CODE (function) == DOTSTAR_EXPR
   21063                 :             :                  || TREE_CODE (function) == MEMBER_REF)
   21064                 :      102268 :           ret = build_offset_ref_call_from_tree (function, &call_args,
   21065                 :             :                                                  complain);
   21066                 :    73540098 :         else if (concept_check_p (function))
   21067                 :             :           {
   21068                 :             :             /* FUNCTION is a template-id referring to a concept definition.  */
   21069                 :          27 :             tree id = unpack_concept_check (function);
   21070                 :          27 :             tree tmpl = TREE_OPERAND (id, 0);
   21071                 :          27 :             tree args = TREE_OPERAND (id, 1);
   21072                 :             : 
   21073                 :             :             /* Calls to standard and variable concepts should have been
   21074                 :             :                previously diagnosed.  */
   21075                 :          27 :             gcc_assert (function_concept_p (tmpl));
   21076                 :             : 
   21077                 :             :             /* Ensure the result is wrapped as a call expression.  */
   21078                 :          27 :             ret = build_concept_check (tmpl, args, tf_warning_or_error);
   21079                 :             :           }
   21080                 :             :         else
   21081                 :    73540071 :           ret = finish_call_expr (function, &call_args,
   21082                 :             :                                   /*disallow_virtual=*/qualified_p,
   21083                 :             :                                   koenig_p,
   21084                 :             :                                   complain);
   21085                 :             : 
   21086                 :    73630749 :         if (ret != error_mark_node)
   21087                 :             :           {
   21088                 :    73542577 :             bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
   21089                 :    73542577 :             bool ord = CALL_EXPR_ORDERED_ARGS (t);
   21090                 :    73542577 :             bool rev = CALL_EXPR_REVERSE_ARGS (t);
   21091                 :    73542577 :             if (op || ord || rev)
   21092                 :      257458 :               if (tree call = extract_call_expr (ret))
   21093                 :             :                 {
   21094                 :      257458 :                   CALL_EXPR_OPERATOR_SYNTAX (call) = op;
   21095                 :      257458 :                   CALL_EXPR_ORDERED_ARGS (call) = ord;
   21096                 :      257458 :                   CALL_EXPR_REVERSE_ARGS (call) = rev;
   21097                 :             :                 }
   21098                 :    73542577 :             if (warning_suppressed_p (t, OPT_Wpessimizing_move))
   21099                 :             :               /* This also suppresses -Wredundant-move.  */
   21100                 :    66331824 :               suppress_warning (ret, OPT_Wpessimizing_move);
   21101                 :             :           }
   21102                 :             : 
   21103                 :    73684376 :         RETURN (ret);
   21104                 :    73775802 :       }
   21105                 :             : 
   21106                 :     2114328 :     case COND_EXPR:
   21107                 :     2114328 :       {
   21108                 :     2114328 :         tree cond = RECUR (TREE_OPERAND (t, 0));
   21109                 :     2114328 :         cond = mark_rvalue_use (cond);
   21110                 :     2114328 :         tree folded_cond = fold_non_dependent_expr (cond, complain);
   21111                 :     2114328 :         tree exp1, exp2;
   21112                 :             : 
   21113                 :     2114328 :         if (TREE_CODE (folded_cond) == INTEGER_CST)
   21114                 :             :           {
   21115                 :     1436920 :             if (integer_zerop (folded_cond))
   21116                 :             :               {
   21117                 :     1065352 :                 ++c_inhibit_evaluation_warnings;
   21118                 :     1065352 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   21119                 :     1065352 :                 --c_inhibit_evaluation_warnings;
   21120                 :     1065352 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   21121                 :             :               }
   21122                 :             :             else
   21123                 :             :               {
   21124                 :      371568 :                 exp1 = RECUR (TREE_OPERAND (t, 1));
   21125                 :      371568 :                 ++c_inhibit_evaluation_warnings;
   21126                 :      371568 :                 exp2 = RECUR (TREE_OPERAND (t, 2));
   21127                 :      371568 :                 --c_inhibit_evaluation_warnings;
   21128                 :             :               }
   21129                 :             :             cond = folded_cond;
   21130                 :             :           }
   21131                 :             :         else
   21132                 :             :           {
   21133                 :      677408 :             exp1 = RECUR (TREE_OPERAND (t, 1));
   21134                 :      677408 :             exp2 = RECUR (TREE_OPERAND (t, 2));
   21135                 :             :           }
   21136                 :             : 
   21137                 :     2114328 :         warning_sentinel s(warn_duplicated_branches);
   21138                 :     2114328 :         RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
   21139                 :             :                                          cond, exp1, exp2, complain));
   21140                 :     2114328 :       }
   21141                 :             : 
   21142                 :          24 :     case PSEUDO_DTOR_EXPR:
   21143                 :          24 :       {
   21144                 :          24 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21145                 :          24 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21146                 :          24 :         tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
   21147                 :          24 :         RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
   21148                 :             :                                                input_location));
   21149                 :             :       }
   21150                 :             : 
   21151                 :    27657249 :     case TREE_LIST:
   21152                 :    27657249 :       RETURN (tsubst_tree_list (t, args, complain, in_decl));
   21153                 :             : 
   21154                 :    20772622 :     case COMPONENT_REF:
   21155                 :    20772622 :       {
   21156                 :    20772622 :         tree object;
   21157                 :    20772622 :         tree object_type;
   21158                 :    20772622 :         tree member;
   21159                 :    20772622 :         tree r;
   21160                 :             : 
   21161                 :    20772622 :         object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
   21162                 :             :                                                      args, complain, in_decl);
   21163                 :             :         /* Remember that there was a reference to this entity.  */
   21164                 :    20772622 :         if (DECL_P (object)
   21165                 :    20772622 :             && !mark_used (object, complain) && !(complain & tf_error))
   21166                 :           0 :           RETURN (error_mark_node);
   21167                 :    20772622 :         object_type = TREE_TYPE (object);
   21168                 :             : 
   21169                 :    20772622 :         member = TREE_OPERAND (t, 1);
   21170                 :    20772622 :         if (BASELINK_P (member))
   21171                 :     7636194 :           member = tsubst_baselink (member,
   21172                 :     7636194 :                                     non_reference (TREE_TYPE (object)),
   21173                 :             :                                     args, complain, in_decl);
   21174                 :             :         else
   21175                 :    13136428 :           member = tsubst_name (member, args, complain, in_decl);
   21176                 :    20772622 :         if (member == error_mark_node)
   21177                 :          10 :           RETURN (error_mark_node);
   21178                 :             : 
   21179                 :    20280777 :         if (object_type && TYPE_PTRMEMFUNC_P (object_type)
   21180                 :    20772634 :             && TREE_CODE (member) == FIELD_DECL)
   21181                 :             :           {
   21182                 :           4 :             r = build_ptrmemfunc_access_expr (object, DECL_NAME (member));
   21183                 :           4 :             RETURN (r);
   21184                 :             :           }
   21185                 :    20772608 :         else if (TREE_CODE (member) == FIELD_DECL)
   21186                 :             :           {
   21187                 :     5014797 :             r = finish_non_static_data_member (member, object, NULL_TREE,
   21188                 :             :                                                complain);
   21189                 :     5014797 :             if (TREE_CODE (r) == COMPONENT_REF)
   21190                 :     4849470 :               REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
   21191                 :     5014797 :             RETURN (r);
   21192                 :             :           }
   21193                 :    15757811 :         else if (type_dependent_expression_p (object))
   21194                 :             :           /* We can't do much here.  */;
   21195                 :    15259354 :         else if (!CLASS_TYPE_P (object_type))
   21196                 :             :           {
   21197                 :       81767 :             if (scalarish_type_p (object_type))
   21198                 :             :               {
   21199                 :       39676 :                 tree s = NULL_TREE;
   21200                 :       39676 :                 tree dtor = member;
   21201                 :             : 
   21202                 :       39676 :                 if (TREE_CODE (dtor) == SCOPE_REF)
   21203                 :             :                   {
   21204                 :          20 :                     s = TREE_OPERAND (dtor, 0);
   21205                 :          20 :                     dtor = TREE_OPERAND (dtor, 1);
   21206                 :             :                   }
   21207                 :       39676 :                 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
   21208                 :             :                   {
   21209                 :       34447 :                     dtor = TREE_OPERAND (dtor, 0);
   21210                 :       34447 :                     if (TYPE_P (dtor))
   21211                 :       34447 :                       RETURN (finish_pseudo_destructor_expr
   21212                 :             :                               (object, s, dtor, input_location));
   21213                 :             :                   }
   21214                 :             :               }
   21215                 :             :           }
   21216                 :    15177587 :         else if (TREE_CODE (member) == SCOPE_REF
   21217                 :    15177587 :                  && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
   21218                 :             :           {
   21219                 :             :             /* Lookup the template functions now that we know what the
   21220                 :             :                scope is.  */
   21221                 :          19 :             tree scope = TREE_OPERAND (member, 0);
   21222                 :          19 :             tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
   21223                 :          19 :             tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
   21224                 :          19 :             member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
   21225                 :             :                                             /*complain=*/false);
   21226                 :          19 :             if (BASELINK_P (member))
   21227                 :             :               {
   21228                 :          38 :                 BASELINK_FUNCTIONS (member)
   21229                 :          19 :                   = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
   21230                 :             :                               args);
   21231                 :          19 :                 member = (adjust_result_of_qualified_name_lookup
   21232                 :          19 :                           (member, BINFO_TYPE (BASELINK_BINFO (member)),
   21233                 :             :                            object_type));
   21234                 :             :               }
   21235                 :             :             else
   21236                 :             :               {
   21237                 :           0 :                 qualified_name_lookup_error (scope, tmpl, member,
   21238                 :             :                                              input_location);
   21239                 :          19 :                 RETURN (error_mark_node);
   21240                 :             :               }
   21241                 :             :           }
   21242                 :    15177568 :         else if (TREE_CODE (member) == SCOPE_REF
   21243                 :         710 :                  && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
   21244                 :    15177572 :                  && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
   21245                 :             :           {
   21246                 :           4 :             if (complain & tf_error)
   21247                 :             :               {
   21248                 :           4 :                 if (TYPE_P (TREE_OPERAND (member, 0)))
   21249                 :           4 :                   error ("%qT is not a class or namespace",
   21250                 :           4 :                          TREE_OPERAND (member, 0));
   21251                 :             :                 else
   21252                 :           0 :                   error ("%qD is not a class or namespace",
   21253                 :           0 :                          TREE_OPERAND (member, 0));
   21254                 :             :               }
   21255                 :           4 :             RETURN (error_mark_node);
   21256                 :             :           }
   21257                 :             : 
   21258                 :    15723360 :         r = finish_class_member_access_expr (object, member,
   21259                 :             :                                              /*template_p=*/false,
   21260                 :             :                                              complain);
   21261                 :    15723360 :         if (REF_PARENTHESIZED_P (t))
   21262                 :         942 :           r = force_paren_expr (r);
   21263                 :    15723360 :         RETURN (r);
   21264                 :             :       }
   21265                 :             : 
   21266                 :       13799 :     case THROW_EXPR:
   21267                 :       13799 :       RETURN (build_throw
   21268                 :             :        (input_location, RECUR (TREE_OPERAND (t, 0)), complain));
   21269                 :             : 
   21270                 :     4224326 :     case CONSTRUCTOR:
   21271                 :     4224326 :       {
   21272                 :     4224326 :         vec<constructor_elt, va_gc> *n;
   21273                 :     4224326 :         constructor_elt *ce;
   21274                 :     4224326 :         unsigned HOST_WIDE_INT idx;
   21275                 :     4224326 :         bool process_index_p;
   21276                 :     4224326 :         int newlen;
   21277                 :     4224326 :         bool need_copy_p = false;
   21278                 :     4224326 :         tree r;
   21279                 :             : 
   21280                 :     4224326 :         tsubst_flags_t tcomplain = complain;
   21281                 :     4224326 :         if (COMPOUND_LITERAL_P (t))
   21282                 :     2486994 :           tcomplain |= tf_tst_ok;
   21283                 :     4224326 :         tree type = tsubst (TREE_TYPE (t), args, tcomplain, in_decl);
   21284                 :     4224326 :         if (type == error_mark_node)
   21285                 :           3 :           RETURN (error_mark_node);
   21286                 :             : 
   21287                 :             :         /* We do not want to process the index of aggregate
   21288                 :             :            initializers as they are identifier nodes which will be
   21289                 :             :            looked up by digest_init.  */
   21290                 :     4224323 :         process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
   21291                 :             : 
   21292                 :     4224323 :         if (null_member_pointer_value_p (t))
   21293                 :             :           {
   21294                 :          18 :             gcc_assert (same_type_p (type, TREE_TYPE (t)));
   21295                 :          18 :             RETURN (t);
   21296                 :             :           }
   21297                 :             : 
   21298                 :     4224305 :         n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
   21299                 :     4224305 :         newlen = vec_safe_length (n);
   21300                 :     6691593 :         FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
   21301                 :             :           {
   21302                 :     2467288 :             if (ce->index && process_index_p
   21303                 :             :                 /* An identifier index is looked up in the type
   21304                 :             :                    being initialized, not the current scope.  */
   21305                 :         287 :                 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
   21306                 :         265 :               ce->index = RECUR (ce->index);
   21307                 :             : 
   21308                 :     2467288 :             if (PACK_EXPANSION_P (ce->value))
   21309                 :             :               {
   21310                 :             :                 /* Substitute into the pack expansion.  */
   21311                 :        3871 :                 ce->value = tsubst_pack_expansion (ce->value, args, complain,
   21312                 :             :                                                   in_decl);
   21313                 :             : 
   21314                 :        3871 :                 if (ce->value == error_mark_node
   21315                 :        3868 :                     || PACK_EXPANSION_P (ce->value))
   21316                 :             :                   ;
   21317                 :        3773 :                 else if (TREE_VEC_LENGTH (ce->value) == 1)
   21318                 :             :                   /* Just move the argument into place.  */
   21319                 :        2419 :                   ce->value = TREE_VEC_ELT (ce->value, 0);
   21320                 :             :                 else
   21321                 :             :                   {
   21322                 :             :                     /* Update the length of the final CONSTRUCTOR
   21323                 :             :                        arguments vector, and note that we will need to
   21324                 :             :                        copy.*/
   21325                 :        1354 :                     newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
   21326                 :        1354 :                     need_copy_p = true;
   21327                 :             :                   }
   21328                 :             :               }
   21329                 :             :             else
   21330                 :     2463417 :               ce->value = RECUR (ce->value);
   21331                 :             :           }
   21332                 :             : 
   21333                 :     4224305 :         if (need_copy_p)
   21334                 :             :           {
   21335                 :        1332 :             vec<constructor_elt, va_gc> *old_n = n;
   21336                 :             : 
   21337                 :        1332 :             vec_alloc (n, newlen);
   21338                 :        2885 :             FOR_EACH_VEC_ELT (*old_n, idx, ce)
   21339                 :             :               {
   21340                 :        1553 :                 if (TREE_CODE (ce->value) == TREE_VEC)
   21341                 :             :                   {
   21342                 :        1354 :                     int i, len = TREE_VEC_LENGTH (ce->value);
   21343                 :        6120 :                     for (i = 0; i < len; ++i)
   21344                 :        4766 :                       CONSTRUCTOR_APPEND_ELT (n, 0,
   21345                 :             :                                               TREE_VEC_ELT (ce->value, i));
   21346                 :             :                   }
   21347                 :             :                 else
   21348                 :        1752 :                   CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
   21349                 :             :               }
   21350                 :             :           }
   21351                 :             : 
   21352                 :     4224305 :         r = build_constructor (init_list_type_node, n);
   21353                 :     4224305 :         CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
   21354                 :     8448610 :         CONSTRUCTOR_IS_DESIGNATED_INIT (r)
   21355                 :     4224305 :           = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
   21356                 :             : 
   21357                 :     4224305 :         if (TREE_HAS_CONSTRUCTOR (t))
   21358                 :             :           {
   21359                 :     2486991 :             fcl_t cl = fcl_functional;
   21360                 :     2486991 :             if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
   21361                 :         135 :               cl = fcl_c99;
   21362                 :     2486991 :             RETURN (finish_compound_literal (type, r, complain, cl));
   21363                 :             :           }
   21364                 :             : 
   21365                 :     1737314 :         TREE_TYPE (r) = type;
   21366                 :     4224326 :         RETURN (r);
   21367                 :             :       }
   21368                 :             : 
   21369                 :       72714 :     case TYPEID_EXPR:
   21370                 :       72714 :       {
   21371                 :       72714 :         tree operand_0 = TREE_OPERAND (t, 0);
   21372                 :       72714 :         if (TYPE_P (operand_0))
   21373                 :             :           {
   21374                 :       72577 :             operand_0 = tsubst (operand_0, args, complain, in_decl);
   21375                 :       72577 :             RETURN (get_typeid (operand_0, complain));
   21376                 :             :           }
   21377                 :             :         else
   21378                 :             :           {
   21379                 :         137 :             operand_0 = RECUR (operand_0);
   21380                 :         137 :             RETURN (build_typeid (operand_0, complain));
   21381                 :             :           }
   21382                 :             :       }
   21383                 :             : 
   21384                 :   127859713 :     case FUNCTION_DECL:
   21385                 :   127859713 :     case PARM_DECL:
   21386                 :   127859713 :     case VAR_DECL:
   21387                 :   127859713 :       if (!args)
   21388                 :     1474491 :         RETURN (t);
   21389                 :   126385222 :       tree r;
   21390                 :    65031158 :       if (VAR_OR_FUNCTION_DECL_P (t)
   21391                 :   132431273 :           && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
   21392                 :     3841271 :         r = tsubst_decl (t, args, complain);
   21393                 :   122543951 :       else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
   21394                 :             :         {
   21395                 :             :           /* Local specialization will usually have been created when
   21396                 :             :              we instantiated the DECL_EXPR_DECL. */
   21397                 :         400 :           r = retrieve_local_specialization (t);
   21398                 :         400 :           if (!r)
   21399                 :             :             {
   21400                 :             :               /* We're in a generic lambda referencing a local extern
   21401                 :             :                  from an outer block-scope of a non-template.  */
   21402                 :          18 :               gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
   21403                 :             :               r = t;
   21404                 :             :             }
   21405                 :             :         }
   21406                 :   122543551 :       else if (local_variable_p (t)
   21407                 :   122543551 :                && ((r = retrieve_local_specialization (t))
   21408                 :      619697 :                    || TREE_CODE (t) == PARM_DECL
   21409                 :         471 :                    || uses_template_parms (DECL_CONTEXT (t))))
   21410                 :             :         {
   21411                 :   115610682 :           if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
   21412                 :             :             {
   21413                 :             :               /* We get here for a use of 'this' in an NSDMI.  */
   21414                 :      619226 :               if (DECL_NAME (t) == this_identifier && current_class_ptr)
   21415                 :      146417 :                 RETURN (current_class_ptr);
   21416                 :             : 
   21417                 :             :               /* This can happen for a parameter name used later in a function
   21418                 :             :                  declaration (such as in a late-specified return type).  Just
   21419                 :             :                  make a dummy decl, since it's only used for its type.  */
   21420                 :      472809 :               gcc_assert (cp_unevaluated_operand);
   21421                 :      472809 :               r = tsubst_decl (t, args, complain);
   21422                 :             :               /* Give it the template pattern as its context; its true context
   21423                 :             :                  hasn't been instantiated yet and this is good enough for
   21424                 :             :                  mangling.  */
   21425                 :      472809 :               DECL_CONTEXT (r) = DECL_CONTEXT (t);
   21426                 :             :             }
   21427                 :          57 :           else if (r == NULL_TREE)
   21428                 :             :             {
   21429                 :             :               /* First try name lookup to find the instantiation.  */
   21430                 :          57 :               r = lookup_name (DECL_NAME (t));
   21431                 :          57 :               if (r)
   21432                 :             :                 {
   21433                 :          57 :                   if (!VAR_P (r))
   21434                 :             :                     {
   21435                 :             :                       /* During error-recovery we may find a non-variable,
   21436                 :             :                          even an OVERLOAD: just bail out and avoid ICEs and
   21437                 :             :                          duplicate diagnostics (c++/62207).  */
   21438                 :           4 :                       gcc_assert (seen_error ());
   21439                 :           4 :                       RETURN (error_mark_node);
   21440                 :             :                     }
   21441                 :          53 :                   if (!is_capture_proxy (r))
   21442                 :             :                     {
   21443                 :             :                       /* Make sure the one we found is the one we want.  */
   21444                 :          53 :                       tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
   21445                 :          53 :                       if (ctx != DECL_CONTEXT (r))
   21446                 :             :                         r = NULL_TREE;
   21447                 :             :                     }
   21448                 :             :                 }
   21449                 :             : 
   21450                 :             :               if (r)
   21451                 :             :                 /* OK */;
   21452                 :             :               else
   21453                 :             :                 {
   21454                 :             :                   /* This can happen for a variable used in a
   21455                 :             :                      late-specified return type of a local lambda, or for a
   21456                 :             :                      local static or constant.  Building a new VAR_DECL
   21457                 :             :                      should be OK in all those cases.  */
   21458                 :           0 :                   r = tsubst_decl (t, args, complain);
   21459                 :           0 :                   if (local_specializations)
   21460                 :             :                     /* Avoid infinite recursion (79640).  */
   21461                 :           0 :                     register_local_specialization (r, t);
   21462                 :           0 :                   if (decl_maybe_constant_var_p (r))
   21463                 :             :                     {
   21464                 :             :                       /* We can't call cp_finish_decl, so handle the
   21465                 :             :                          initializer by hand.  */
   21466                 :           0 :                       tree init = tsubst_init (DECL_INITIAL (t), r, args,
   21467                 :             :                                                complain, in_decl);
   21468                 :           0 :                       if (!processing_template_decl)
   21469                 :           0 :                         init = maybe_constant_init (init);
   21470                 :           0 :                       if (processing_template_decl
   21471                 :           0 :                           ? potential_constant_expression (init)
   21472                 :           0 :                           : reduced_constant_expression_p (init))
   21473                 :           0 :                         DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
   21474                 :           0 :                           = TREE_CONSTANT (r) = true;
   21475                 :           0 :                       DECL_INITIAL (r) = init;
   21476                 :           0 :                       if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
   21477                 :           0 :                         TREE_TYPE (r)
   21478                 :           0 :                           = do_auto_deduction (TREE_TYPE (r), init, auto_node,
   21479                 :             :                                                complain, adc_variable_type);
   21480                 :             :                     }
   21481                 :           0 :                   gcc_assert (cp_unevaluated_operand
   21482                 :             :                               || processing_contract_condition
   21483                 :             :                               || TREE_STATIC (r)
   21484                 :             :                               || decl_constant_var_p (r)
   21485                 :             :                               || seen_error ());
   21486                 :           0 :                   if (!processing_template_decl
   21487                 :           0 :                       && !TREE_STATIC (r))
   21488                 :           0 :                     r = process_outer_var_ref (r, complain);
   21489                 :             :                 }
   21490                 :             :               /* Remember this for subsequent uses.  */
   21491                 :          53 :               if (local_specializations)
   21492                 :          53 :                 register_local_specialization (r, t);
   21493                 :             :             }
   21494                 :   115464261 :           if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
   21495                 :      325522 :             r = argument_pack_select_arg (r);
   21496                 :             :         }
   21497                 :             :       else
   21498                 :             :         r = t;
   21499                 :   126238801 :       if (!mark_used (r, complain))
   21500                 :          15 :         RETURN (error_mark_node);
   21501                 :             : 
   21502                 :   126238782 :       if (!no_name_lookup_flag
   21503                 :   124416820 :           && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL))
   21504                 :             :         {
   21505                 :             :           /* ??? We're doing a subset of finish_id_expression here.  */
   21506                 :   118370776 :           if (tree wrap = maybe_get_tls_wrapper_call (r))
   21507                 :             :             /* Replace an evaluated use of the thread_local variable with
   21508                 :             :                a call to its wrapper.  */
   21509                 :             :             r = wrap;
   21510                 :   118370618 :           else if (outer_automatic_var_p (r))
   21511                 :      402595 :             r = process_outer_var_ref (r, complain);
   21512                 :             : 
   21513                 :   118370776 :           if (!TYPE_REF_P (TREE_TYPE (t)))
   21514                 :             :             /* If the original type was a reference, we'll be wrapped in
   21515                 :             :                the appropriate INDIRECT_REF.  */
   21516                 :   103778850 :             r = convert_from_reference (r);
   21517                 :             :         }
   21518                 :   126238782 :       RETURN (r);
   21519                 :             : 
   21520                 :     2369655 :     case CONST_DECL:
   21521                 :     2369655 :       {
   21522                 :     2369655 :         tree enum_type;
   21523                 :     2369655 :         tree v;
   21524                 :             : 
   21525                 :     2369655 :         if (DECL_TEMPLATE_PARM_P (t))
   21526                 :           8 :           RETURN (RECUR (DECL_INITIAL (t)));
   21527                 :     2369647 :         if (!uses_template_parms (DECL_CONTEXT (t)))
   21528                 :     2121354 :           RETURN (t);
   21529                 :             : 
   21530                 :             :         /* Unfortunately, we cannot just call lookup_name here.
   21531                 :             :            Consider:
   21532                 :             : 
   21533                 :             :              template <int I> int f() {
   21534                 :             :              enum E { a = I };
   21535                 :             :              struct S { void g() { E e = a; } };
   21536                 :             :              };
   21537                 :             : 
   21538                 :             :            When we instantiate f<7>::S::g(), say, lookup_name is not
   21539                 :             :            clever enough to find f<7>::a.  */
   21540                 :      248293 :         enum_type
   21541                 :      248293 :           = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
   21542                 :             :                               /*entering_scope=*/0);
   21543                 :             : 
   21544                 :      248293 :         for (v = TYPE_VALUES (enum_type);
   21545                 :      275190 :              v != NULL_TREE;
   21546                 :       26897 :              v = TREE_CHAIN (v))
   21547                 :      275190 :           if (TREE_PURPOSE (v) == DECL_NAME (t))
   21548                 :      275190 :             RETURN (TREE_VALUE (v));
   21549                 :             : 
   21550                 :             :           /* We didn't find the name.  That should never happen; if
   21551                 :             :              name-lookup found it during preliminary parsing, we
   21552                 :             :              should find it again here during instantiation.  */
   21553                 :           0 :         gcc_unreachable ();
   21554                 :     7092308 :         RETURN (t);
   21555                 :             :       }
   21556                 :             : 
   21557                 :     7092308 :     case FIELD_DECL:
   21558                 :     7092308 :       if (DECL_CONTEXT (t))
   21559                 :             :         {
   21560                 :     7092308 :           tree ctx;
   21561                 :             : 
   21562                 :     7092308 :           ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
   21563                 :             :                                   /*entering_scope=*/1);
   21564                 :     7092308 :           if (ctx != DECL_CONTEXT (t))
   21565                 :             :             {
   21566                 :     6726106 :               tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
   21567                 :     6726106 :               if (!r)
   21568                 :             :                 {
   21569                 :           0 :                   if (complain & tf_error)
   21570                 :           0 :                     error ("using invalid field %qD", t);
   21571                 :           0 :                   RETURN (error_mark_node);
   21572                 :             :                 }
   21573                 :     6726106 :               RETURN (r);
   21574                 :             :             }
   21575                 :             :         }
   21576                 :      366202 :       RETURN (t);
   21577                 :             : 
   21578                 :    32792452 :     case NAMESPACE_DECL:
   21579                 :    32792452 :     case OVERLOAD:
   21580                 :    32792452 :       RETURN (t);
   21581                 :             : 
   21582                 :     6966025 :     case TEMPLATE_DECL:
   21583                 :     6966025 :       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
   21584                 :        3722 :         RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
   21585                 :             :                         args, complain, in_decl));
   21586                 :     6962303 :       else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
   21587                 :           0 :         RETURN (tsubst (t, args, complain, in_decl));
   21588                 :    13924606 :       else if (DECL_CLASS_SCOPE_P (t)
   21589                 :     7104469 :                && uses_template_parms (DECL_CONTEXT (t)))
   21590                 :             :         {
   21591                 :             :           /* Template template argument like the following example need
   21592                 :             :              special treatment:
   21593                 :             : 
   21594                 :             :                template <template <class> class TT> struct C {};
   21595                 :             :                template <class T> struct D {
   21596                 :             :                  template <class U> struct E {};
   21597                 :             :                  C<E> c;                          // #1
   21598                 :             :                };
   21599                 :             :                D<int> d;                          // #2
   21600                 :             : 
   21601                 :             :              We are processing the template argument `E' in #1 for
   21602                 :             :              the template instantiation #2.  Originally, `E' is a
   21603                 :             :              TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT.  Now we
   21604                 :             :              have to substitute this with one having context `D<int>'.  */
   21605                 :             : 
   21606                 :      123914 :           tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain,
   21607                 :             :                                            in_decl, /*entering_scope=*/true);
   21608                 :      123914 :           RETURN (lookup_field (context, DECL_NAME(t), 0, false));
   21609                 :             :         }
   21610                 :             :       else
   21611                 :             :         /* Ordinary template template argument.  */
   21612                 :     6838389 :         RETURN (t);
   21613                 :             : 
   21614                 :   134989545 :     case TEMPLATE_PARM_INDEX:
   21615                 :   134989545 :     case TYPE_DECL:
   21616                 :   134989545 :       RETURN (tsubst (t, args, complain, in_decl));
   21617                 :             : 
   21618                 :           0 :     case CLEANUP_POINT_EXPR:
   21619                 :             :       /* We shouldn't have built any of these during initial template
   21620                 :             :          generation.  Instead, they should be built during instantiation
   21621                 :             :          in response to the saved STMT_IS_FULL_EXPR_P setting.  */
   21622                 :           0 :       gcc_unreachable ();
   21623                 :             : 
   21624                 :         166 :     case OFFSET_REF:
   21625                 :         166 :       {
   21626                 :         166 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21627                 :         166 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21628                 :         166 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21629                 :         166 :         r = build2 (OFFSET_REF, type, op0, op1);
   21630                 :         166 :         PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
   21631                 :         166 :         if (!mark_used (TREE_OPERAND (r, 1), complain)
   21632                 :         166 :             && !(complain & tf_error))
   21633                 :           0 :           RETURN (error_mark_node);
   21634                 :         166 :         RETURN (r);
   21635                 :             :       }
   21636                 :             : 
   21637                 :           0 :     case EXPR_PACK_EXPANSION:
   21638                 :           0 :       error ("invalid use of pack expansion expression");
   21639                 :           0 :       RETURN (error_mark_node);
   21640                 :             : 
   21641                 :           0 :     case NONTYPE_ARGUMENT_PACK:
   21642                 :           0 :       error ("use %<...%> to expand argument pack");
   21643                 :           0 :       RETURN (error_mark_node);
   21644                 :             : 
   21645                 :        1586 :     case VOID_CST:
   21646                 :        1586 :       gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
   21647                 :        1586 :       RETURN (t);
   21648                 :             : 
   21649                 :   115569102 :     case INTEGER_CST:
   21650                 :   115569102 :     case REAL_CST:
   21651                 :   115569102 :     case COMPLEX_CST:
   21652                 :   115569102 :     case VECTOR_CST:
   21653                 :   115569102 :       {
   21654                 :             :         /* Instantiate any typedefs in the type.  */
   21655                 :   115569102 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21656                 :   115569102 :         r = fold_convert (type, t);
   21657                 :   115569102 :         gcc_assert (TREE_CODE (r) == TREE_CODE (t));
   21658                 :   115569102 :         RETURN (r);
   21659                 :             :       }
   21660                 :             : 
   21661                 :     5733592 :     case STRING_CST:
   21662                 :     5733592 :       {
   21663                 :     5733592 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21664                 :     5733592 :         r = t;
   21665                 :     5733592 :         if (type != TREE_TYPE (t))
   21666                 :             :           {
   21667                 :           3 :             r = copy_node (t);
   21668                 :           3 :             TREE_TYPE (r) = type;
   21669                 :             :           }
   21670                 :     5733592 :         RETURN (r);
   21671                 :             :       }
   21672                 :             : 
   21673                 :          28 :     case PTRMEM_CST:
   21674                 :             :       /* These can sometimes show up in a partial instantiation, but never
   21675                 :             :          involve template parms.  */
   21676                 :          28 :       gcc_assert (!uses_template_parms (t));
   21677                 :          28 :       RETURN (t);
   21678                 :             : 
   21679                 :        1421 :     case UNARY_LEFT_FOLD_EXPR:
   21680                 :        1421 :       RETURN (tsubst_unary_left_fold (t, args, complain, in_decl));
   21681                 :      160751 :     case UNARY_RIGHT_FOLD_EXPR:
   21682                 :      160751 :       RETURN (tsubst_unary_right_fold (t, args, complain, in_decl));
   21683                 :         695 :     case BINARY_LEFT_FOLD_EXPR:
   21684                 :         695 :       RETURN (tsubst_binary_left_fold (t, args, complain, in_decl));
   21685                 :         305 :     case BINARY_RIGHT_FOLD_EXPR:
   21686                 :         305 :       RETURN (tsubst_binary_right_fold (t, args, complain, in_decl));
   21687                 :           0 :     case PREDICT_EXPR:
   21688                 :           0 :       RETURN (t);
   21689                 :             : 
   21690                 :    70376824 :     case DEBUG_BEGIN_STMT:
   21691                 :             :       /* ??? There's no point in copying it for now, but maybe some
   21692                 :             :          day it will contain more information, such as a pointer back
   21693                 :             :          to the containing function, inlined copy or so.  */
   21694                 :    70376824 :       RETURN (t);
   21695                 :             : 
   21696                 :          51 :     case CO_YIELD_EXPR:
   21697                 :          51 :       RETURN (finish_co_yield_expr (input_location,
   21698                 :             :                                     RECUR (TREE_OPERAND (t, 0))));
   21699                 :             : 
   21700                 :          86 :     case CO_AWAIT_EXPR:
   21701                 :          86 :       RETURN (finish_co_await_expr (input_location,
   21702                 :             :                                     RECUR (TREE_OPERAND (t, 0))));
   21703                 :             : 
   21704                 :          56 :     case VA_ARG_EXPR:
   21705                 :          56 :       {
   21706                 :          56 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21707                 :          56 :         tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21708                 :          56 :         RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
   21709                 :             :       }
   21710                 :             : 
   21711                 :          60 :     case OFFSETOF_EXPR:
   21712                 :          60 :       {
   21713                 :          60 :         tree object_ptr
   21714                 :          60 :           = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
   21715                 :          60 :         RETURN (finish_offsetof (object_ptr,
   21716                 :             :                                  RECUR (TREE_OPERAND (t, 0)),
   21717                 :             :                                  EXPR_LOCATION (t)));
   21718                 :             :       }
   21719                 :             : 
   21720                 :      238063 :     case ADDRESSOF_EXPR:
   21721                 :      238063 :       RETURN (cp_build_addressof (EXPR_LOCATION (t),
   21722                 :             :                                   RECUR (TREE_OPERAND (t, 0)), complain));
   21723                 :             : 
   21724                 :     8520502 :     case TRAIT_EXPR:
   21725                 :     8520502 :       {
   21726                 :     8520502 :         tree type1 = TRAIT_EXPR_TYPE1 (t);
   21727                 :     8520502 :         if (TYPE_P (type1))
   21728                 :     8520473 :           type1 = tsubst (type1, args, complain, in_decl);
   21729                 :             :         else
   21730                 :          29 :           type1 = tsubst_expr (type1, args, complain, in_decl);
   21731                 :     8520502 :         tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
   21732                 :             :                              complain, in_decl);
   21733                 :     8520502 :         RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
   21734                 :             :                                    TRAIT_EXPR_KIND (t), type1, type2));
   21735                 :             :       }
   21736                 :             : 
   21737                 :         175 :     case STMT_EXPR:
   21738                 :         175 :       {
   21739                 :         175 :         tree old_stmt_expr = cur_stmt_expr;
   21740                 :         175 :         tree stmt_expr = begin_stmt_expr ();
   21741                 :             : 
   21742                 :         175 :         cur_stmt_expr = stmt_expr;
   21743                 :         175 :         tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl);
   21744                 :         175 :         stmt_expr = finish_stmt_expr (stmt_expr, false);
   21745                 :         175 :         cur_stmt_expr = old_stmt_expr;
   21746                 :             : 
   21747                 :             :         /* If the resulting list of expression statement is empty,
   21748                 :             :            fold it further into void_node.  */
   21749                 :         175 :         if (empty_expr_stmt_p (stmt_expr))
   21750                 :          12 :           stmt_expr = void_node;
   21751                 :             : 
   21752                 :         175 :         RETURN (stmt_expr);
   21753                 :             :       }
   21754                 :             : 
   21755                 :      138442 :     case LAMBDA_EXPR:
   21756                 :      138442 :       {
   21757                 :      138442 :         if (complain & tf_partial)
   21758                 :             :           {
   21759                 :             :             /* We don't have a full set of template arguments yet; don't touch
   21760                 :             :                the lambda at all.  */
   21761                 :           0 :             gcc_assert (processing_template_decl);
   21762                 :             :             return t;
   21763                 :             :           }
   21764                 :      138442 :         tree r = tsubst_lambda_expr (t, args, complain, in_decl);
   21765                 :             : 
   21766                 :      138442 :         RETURN (build_lambda_object (r));
   21767                 :             :       }
   21768                 :             : 
   21769                 :          12 :     case TRANSACTION_EXPR:
   21770                 :          12 :       gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t));
   21771                 :          12 :       RETURN (tsubst_stmt (t, args, complain, in_decl));
   21772                 :             : 
   21773                 :      392075 :     case PAREN_EXPR:
   21774                 :      392075 :       if (REF_PARENTHESIZED_P (t))
   21775                 :      392074 :         RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
   21776                 :             :       else
   21777                 :             :         /* Recreate the PAREN_EXPR from __builtin_assoc_barrier.  */
   21778                 :             :         {
   21779                 :           1 :           tree op0 = RECUR (TREE_OPERAND (t, 0));
   21780                 :           1 :           RETURN (build1_loc (input_location, PAREN_EXPR,
   21781                 :             :                               TREE_TYPE (op0), op0));
   21782                 :             :         }
   21783                 :             : 
   21784                 :          10 :     case VEC_PERM_EXPR:
   21785                 :          10 :       {
   21786                 :          10 :         tree op0 = RECUR (TREE_OPERAND (t, 0));
   21787                 :          10 :         tree op1 = RECUR (TREE_OPERAND (t, 1));
   21788                 :          10 :         tree op2 = RECUR (TREE_OPERAND (t, 2));
   21789                 :          10 :         RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
   21790                 :             :                                        complain));
   21791                 :             :       }
   21792                 :             : 
   21793                 :     2436565 :     case REQUIRES_EXPR:
   21794                 :     2436565 :       {
   21795                 :     2436565 :         complain &= ~tf_warning_or_error;
   21796                 :     2436565 :         tree r = tsubst_requires_expr (t, args, complain, in_decl);
   21797                 :     2435665 :         RETURN (r);
   21798                 :             :       }
   21799                 :             : 
   21800                 :           6 :     case RANGE_EXPR:
   21801                 :             :       /* No need to substitute further, a RANGE_EXPR will always be built
   21802                 :             :          with constant operands.  */
   21803                 :           6 :       RETURN (t);
   21804                 :             : 
   21805                 :   128213490 :     case NON_LVALUE_EXPR:
   21806                 :   128213490 :     case VIEW_CONVERT_EXPR:
   21807                 :   128213490 :       {
   21808                 :   128213490 :         tree op = RECUR (TREE_OPERAND (t, 0));
   21809                 :             : 
   21810                 :   128213490 :         if (location_wrapper_p (t))
   21811                 :             :           /* We need to do this here as well as in tsubst_copy so we get the
   21812                 :             :              other tsubst_copy_and_build semantics for a PARM_DECL operand.  */
   21813                 :   128209393 :           RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t)));
   21814                 :             : 
   21815                 :        4097 :         gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR);
   21816                 :        4097 :         if (REF_PARENTHESIZED_P (t))
   21817                 :             :           /* force_paren_expr can also create a VIEW_CONVERT_EXPR.  */
   21818                 :           0 :           RETURN (finish_parenthesized_expr (op));
   21819                 :             : 
   21820                 :             :         /* Otherwise, we're dealing with a wrapper to make a C++20 template
   21821                 :             :            parameter object const.  */
   21822                 :        4097 :         if (TREE_TYPE (op) == NULL_TREE
   21823                 :        4097 :             || !CP_TYPE_CONST_P (TREE_TYPE (op)))
   21824                 :             :           {
   21825                 :             :             /* The template argument is not const, presumably because
   21826                 :             :                it is still dependent, and so not the const template parm
   21827                 :             :                object.  */
   21828                 :        4052 :             tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
   21829                 :        4052 :             if (TREE_CODE (op) == CONSTRUCTOR
   21830                 :        4051 :                 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
   21831                 :             :               {
   21832                 :             :                 /* Don't add a wrapper to these.  */
   21833                 :           4 :                 op = copy_node (op);
   21834                 :           4 :                 TREE_TYPE (op) = type;
   21835                 :             :               }
   21836                 :             :             else
   21837                 :             :               /* Do add a wrapper otherwise (in particular, if op is
   21838                 :             :                  another TEMPLATE_PARM_INDEX).  */
   21839                 :        4048 :               op = build1 (VIEW_CONVERT_EXPR, type, op);
   21840                 :             :           }
   21841                 :        4097 :         RETURN (op);
   21842                 :             :       }
   21843                 :             : 
   21844                 :           0 :     default:
   21845                 :             :       /* Handle Objective-C++ constructs, if appropriate.  */
   21846                 :           0 :       if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl))
   21847                 :           0 :         RETURN (subst);
   21848                 :             : 
   21849                 :             :       /* We shouldn't get here, but keep going if !flag_checking.  */
   21850                 :           0 :       if (flag_checking)
   21851                 :           0 :         gcc_unreachable ();
   21852                 :           0 :       RETURN (t);
   21853                 :             :     }
   21854                 :             : 
   21855                 :             : #undef RECUR
   21856                 :             : #undef RETURN
   21857                 :  1256182813 :  out:
   21858                 :  1256182813 :   input_location = save_loc;
   21859                 :  1256182813 :   return retval;
   21860                 :             : }
   21861                 :             : 
   21862                 :             : /* Verify that the instantiated ARGS are valid. For type arguments,
   21863                 :             :    make sure that the type's linkage is ok. For non-type arguments,
   21864                 :             :    make sure they are constants if they are integral or enumerations.
   21865                 :             :    Emit an error under control of COMPLAIN, and return TRUE on error.  */
   21866                 :             : 
   21867                 :             : static bool
   21868                 :   198014770 : check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
   21869                 :             : {
   21870                 :   198014770 :   if (dependent_template_arg_p (t))
   21871                 :             :     return false;
   21872                 :   178531220 :   if (ARGUMENT_PACK_P (t))
   21873                 :             :     {
   21874                 :    15045330 :       tree vec = ARGUMENT_PACK_ARGS (t);
   21875                 :    15045330 :       int len = TREE_VEC_LENGTH (vec);
   21876                 :    15045330 :       bool result = false;
   21877                 :    15045330 :       int i;
   21878                 :             : 
   21879                 :    47575106 :       for (i = 0; i < len; ++i)
   21880                 :    32529776 :         if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
   21881                 :           0 :           result = true;
   21882                 :    15045330 :       return result;
   21883                 :             :     }
   21884                 :   163485890 :   else if (TYPE_P (t))
   21885                 :             :     {
   21886                 :             :       /* [basic.link]: A name with no linkage (notably, the name
   21887                 :             :          of a class or enumeration declared in a local scope)
   21888                 :             :          shall not be used to declare an entity with linkage.
   21889                 :             :          This implies that names with no linkage cannot be used as
   21890                 :             :          template arguments
   21891                 :             : 
   21892                 :             :          DR 757 relaxes this restriction for C++0x.  */
   21893                 :   139502994 :       tree nt = (cxx_dialect > cxx98 ? NULL_TREE
   21894                 :      149136 :                  : no_linkage_check (t, /*relaxed_p=*/false));
   21895                 :             : 
   21896                 :      149136 :       if (nt)
   21897                 :             :         {
   21898                 :             :           /* DR 488 makes use of a type with no linkage cause
   21899                 :             :              type deduction to fail.  */
   21900                 :           8 :           if (complain & tf_error)
   21901                 :             :             {
   21902                 :          10 :               if (TYPE_UNNAMED_P (nt))
   21903                 :           0 :                 error ("%qT is/uses unnamed type", t);
   21904                 :             :               else
   21905                 :           5 :                 error ("template argument for %qD uses local type %qT",
   21906                 :             :                        tmpl, t);
   21907                 :             :             }
   21908                 :           8 :           return true;
   21909                 :             :         }
   21910                 :             :       /* In order to avoid all sorts of complications, we do not
   21911                 :             :          allow variably-modified types as template arguments.  */
   21912                 :   139502986 :       else if (variably_modified_type_p (t, NULL_TREE))
   21913                 :             :         {
   21914                 :           8 :           if (complain & tf_error)
   21915                 :           4 :             error ("%qT is a variably modified type", t);
   21916                 :           8 :           return true;
   21917                 :             :         }
   21918                 :             :     }
   21919                 :             :   /* Class template and alias template arguments should be OK.  */
   21920                 :    23982896 :   else if (DECL_TYPE_TEMPLATE_P (t))
   21921                 :             :     ;
   21922                 :             :   /* A non-type argument of integral or enumerated type must be a
   21923                 :             :      constant.  */
   21924                 :    23567020 :   else if (TREE_TYPE (t)
   21925                 :    23567020 :            && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
   21926                 :    23565102 :            && !REFERENCE_REF_P (t)
   21927                 :    47132036 :            && !TREE_CONSTANT (t))
   21928                 :             :     {
   21929                 :           0 :       if (complain & tf_error)
   21930                 :           0 :         error ("integral expression %qE is not constant", t);
   21931                 :           0 :       return true;
   21932                 :             :     }
   21933                 :             :   return false;
   21934                 :             : }
   21935                 :             : 
   21936                 :             : static bool
   21937                 :   106455833 : check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
   21938                 :             : {
   21939                 :   106455833 :   int ix, len = DECL_NTPARMS (tmpl);
   21940                 :   106455833 :   bool result = false;
   21941                 :             : 
   21942                 :   271940827 :   for (ix = 0; ix != len; ix++)
   21943                 :             :     {
   21944                 :   165484994 :       if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
   21945                 :          16 :         result = true;
   21946                 :             :     }
   21947                 :   106455833 :   if (result && (complain & tf_error))
   21948                 :           9 :     error ("  trying to instantiate %qD", tmpl);
   21949                 :   106455833 :   return result;
   21950                 :             : }
   21951                 :             : 
   21952                 :             : /* Call mark_used on each entity within the non-type template arguments in
   21953                 :             :    ARGS for an instantiation of TMPL, to ensure that each such entity is
   21954                 :             :    considered odr-used (and therefore marked for instantiation) regardless of
   21955                 :             :    whether the specialization was first formed in a template context (which
   21956                 :             :    inhibits mark_used).
   21957                 :             : 
   21958                 :             :    This function assumes push_to_top_level has been called beforehand.  */
   21959                 :             : 
   21960                 :             : static void
   21961                 :    47626901 : mark_template_arguments_used (tree tmpl, tree args)
   21962                 :             : {
   21963                 :             :   /* It suffices to do this only when instantiating a primary template.  */
   21964                 :    47626901 :   if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl))
   21965                 :             :     return;
   21966                 :             : 
   21967                 :             :   /* We already marked outer arguments when specializing the context.  */
   21968                 :    37227876 :   args = INNERMOST_TEMPLATE_ARGS (args);
   21969                 :             : 
   21970                 :    96457325 :   for (tree arg : tree_vec_range (args))
   21971                 :             :     {
   21972                 :             :       /* A (pointer/reference to) function or variable NTTP argument.  */
   21973                 :    59229449 :       if (TREE_CODE (arg) == ADDR_EXPR
   21974                 :    59228856 :           || TREE_CODE (arg) == INDIRECT_REF)
   21975                 :             :         {
   21976                 :        1926 :           while (TREE_CODE (arg) == ADDR_EXPR
   21977                 :        1148 :                  || REFERENCE_REF_P (arg)
   21978                 :        2889 :                  || CONVERT_EXPR_P (arg))
   21979                 :        1148 :             arg = TREE_OPERAND (arg, 0);
   21980                 :         778 :           if (VAR_OR_FUNCTION_DECL_P (arg))
   21981                 :             :             {
   21982                 :             :               /* Pass tf_none to avoid duplicate diagnostics: if this call
   21983                 :             :                  fails then an earlier call to mark_used for this argument
   21984                 :             :                  must have also failed and emitted a diagnostic.  */
   21985                 :         772 :               bool ok = mark_used (arg, tf_none);
   21986                 :         772 :               gcc_checking_assert (ok || seen_error ());
   21987                 :             :             }
   21988                 :             :         }
   21989                 :             :       /* A class NTTP argument.  */
   21990                 :    59228671 :       else if (VAR_P (arg)
   21991                 :    59228671 :                && DECL_NTTP_OBJECT_P (arg))
   21992                 :             :         {
   21993                 :         587 :           auto mark_used_r = [](tree *tp, int *, void *) {
   21994                 :         421 :             if (VAR_OR_FUNCTION_DECL_P (*tp))
   21995                 :             :               {
   21996                 :           4 :                 bool ok = mark_used (*tp, tf_none);
   21997                 :           4 :                 gcc_checking_assert (ok || seen_error ());
   21998                 :             :               }
   21999                 :         421 :             return NULL_TREE;
   22000                 :             :           };
   22001                 :         166 :           cp_walk_tree_without_duplicates (&DECL_INITIAL (arg),
   22002                 :             :                                            mark_used_r, nullptr);
   22003                 :             :         }
   22004                 :             :     }
   22005                 :             : }
   22006                 :             : 
   22007                 :             : /* We're out of SFINAE context now, so generate diagnostics for the access
   22008                 :             :    errors we saw earlier when instantiating D from TMPL and ARGS.  */
   22009                 :             : 
   22010                 :             : static void
   22011                 :          10 : recheck_decl_substitution (tree d, tree tmpl, tree args)
   22012                 :             : {
   22013                 :          10 :   tree pattern = DECL_TEMPLATE_RESULT (tmpl);
   22014                 :          10 :   tree type = TREE_TYPE (pattern);
   22015                 :          10 :   location_t loc = input_location;
   22016                 :             : 
   22017                 :          10 :   push_access_scope (d);
   22018                 :          10 :   push_deferring_access_checks (dk_no_deferred);
   22019                 :          10 :   input_location = DECL_SOURCE_LOCATION (pattern);
   22020                 :          10 :   tsubst (type, args, tf_warning_or_error, d);
   22021                 :          10 :   input_location = loc;
   22022                 :          10 :   pop_deferring_access_checks ();
   22023                 :          10 :   pop_access_scope (d);
   22024                 :          10 : }
   22025                 :             : 
   22026                 :             : /* Instantiate the indicated variable, function, or alias template TMPL with
   22027                 :             :    the template arguments in TARG_PTR.  */
   22028                 :             : 
   22029                 :             : tree
   22030                 :   153773515 : instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
   22031                 :             : {
   22032                 :   153773515 :   auto_timevar tv (TV_TEMPLATE_INST);
   22033                 :             : 
   22034                 :   153773515 :   tree targ_ptr = orig_args;
   22035                 :   153773515 :   tree fndecl;
   22036                 :   153773515 :   tree gen_tmpl;
   22037                 :   153773515 :   bool access_ok = true;
   22038                 :             : 
   22039                 :   153773515 :   if (tmpl == error_mark_node)
   22040                 :             :     return error_mark_node;
   22041                 :             : 
   22042                 :             :   /* The other flags are not relevant anymore here, especially tf_partial
   22043                 :             :      shouldn't be set.  For instance, we may be called while doing a partial
   22044                 :             :      substitution of a template variable, but the type of the variable
   22045                 :             :      template may be auto, in which case we will call do_auto_deduction
   22046                 :             :      in mark_used (which clears tf_partial) and the auto must be properly
   22047                 :             :      reduced at that time for the deduction to work.  */
   22048                 :   153773506 :   complain &= tf_warning_or_error;
   22049                 :             : 
   22050                 :   153773506 :   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
   22051                 :             : 
   22052                 :   153773506 :   if (modules_p ())
   22053                 :      809748 :     lazy_load_pendings (tmpl);
   22054                 :             : 
   22055                 :             :   /* If this function is a clone, handle it specially.  */
   22056                 :   153773506 :   if (DECL_CLONED_FUNCTION_P (tmpl))
   22057                 :             :     {
   22058                 :     3279696 :       tree spec;
   22059                 :     3279696 :       tree clone;
   22060                 :             : 
   22061                 :             :       /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
   22062                 :             :          DECL_CLONED_FUNCTION.  */
   22063                 :     3279696 :       spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
   22064                 :             :                                    targ_ptr, complain);
   22065                 :     3279696 :       if (spec == error_mark_node)
   22066                 :             :         return error_mark_node;
   22067                 :             : 
   22068                 :             :       /* Look for the clone.  */
   22069                 :     6348236 :       FOR_EACH_CLONE (clone, spec)
   22070                 :     6348236 :         if (DECL_NAME (clone) == DECL_NAME (tmpl))
   22071                 :     3275596 :           return clone;
   22072                 :             :       /* We should always have found the clone by now.  */
   22073                 :           0 :       gcc_unreachable ();
   22074                 :             :       return NULL_TREE;
   22075                 :             :     }
   22076                 :             : 
   22077                 :   150493810 :   if (targ_ptr == error_mark_node)
   22078                 :             :     return error_mark_node;
   22079                 :             : 
   22080                 :             :   /* Check to see if we already have this specialization.  */
   22081                 :   150493810 :   gen_tmpl = most_general_template (tmpl);
   22082                 :   300987620 :   if (TMPL_ARGS_DEPTH (targ_ptr)
   22083                 :   150493810 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
   22084                 :             :     /* targ_ptr only has the innermost template args, so add the outer ones
   22085                 :             :        from tmpl, which could be either a partial instantiation or gen_tmpl (in
   22086                 :             :        the case of a non-dependent call within a template definition).  */
   22087                 :    10819292 :     targ_ptr = (add_outermost_template_args
   22088                 :    10819292 :                 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
   22089                 :             :                  targ_ptr));
   22090                 :             : 
   22091                 :   150493810 :   hashval_t hash = spec_hasher::hash (gen_tmpl, targ_ptr);
   22092                 :   150493810 :   tree spec = retrieve_specialization (gen_tmpl, targ_ptr, hash);
   22093                 :             : 
   22094                 :   150493810 :   gcc_checking_assert (tmpl == gen_tmpl
   22095                 :             :                        || ((fndecl
   22096                 :             :                             = retrieve_specialization (tmpl, orig_args, 0))
   22097                 :             :                            == spec)
   22098                 :             :                        || fndecl == NULL_TREE);
   22099                 :             : 
   22100                 :   150493810 :   if (spec != NULL_TREE)
   22101                 :             :     {
   22102                 :    80221535 :       if (FNDECL_HAS_ACCESS_ERRORS (spec))
   22103                 :             :         {
   22104                 :          16 :           if (complain & tf_error)
   22105                 :          10 :             recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
   22106                 :          16 :           return error_mark_node;
   22107                 :             :         }
   22108                 :             :       return spec;
   22109                 :             :     }
   22110                 :             : 
   22111                 :    70272275 :   if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
   22112                 :             :                                complain))
   22113                 :           6 :     return error_mark_node;
   22114                 :             : 
   22115                 :             :   /* We are building a FUNCTION_DECL, during which the access of its
   22116                 :             :      parameters and return types have to be checked.  However this
   22117                 :             :      FUNCTION_DECL which is the desired context for access checking
   22118                 :             :      is not built yet.  We solve this chicken-and-egg problem by
   22119                 :             :      deferring all checks until we have the FUNCTION_DECL.  */
   22120                 :    70272269 :   push_deferring_access_checks (dk_deferred);
   22121                 :             : 
   22122                 :             :   /* Instantiation of the function happens in the context of the function
   22123                 :             :      template, not the context of the overload resolution we're doing.  */
   22124                 :    70272269 :   push_to_top_level ();
   22125                 :             :   /* If there are dependent arguments, e.g. because we're doing partial
   22126                 :             :      ordering, make sure processing_template_decl stays set.  */
   22127                 :    70272269 :   if (uses_template_parms (targ_ptr))
   22128                 :    14071299 :     ++processing_template_decl;
   22129                 :    70272269 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   22130                 :             :     {
   22131                 :    19845482 :       tree ctx;
   22132                 :    19845482 :       if (!uses_template_parms (DECL_CONTEXT (tmpl)))
   22133                 :             :         /* If the context of the partially instantiated template is
   22134                 :             :            already non-dependent, then we might as well use it.  */
   22135                 :    16393177 :         ctx = DECL_CONTEXT (tmpl);
   22136                 :             :       else
   22137                 :     3452305 :         ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
   22138                 :             :                                 complain, gen_tmpl, true);
   22139                 :    19845482 :       push_nested_class (ctx);
   22140                 :             :     }
   22141                 :             : 
   22142                 :    70272269 :   tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
   22143                 :             : 
   22144                 :    70272269 :   tree partial_ti = NULL_TREE;
   22145                 :    70272269 :   fndecl = NULL_TREE;
   22146                 :    70272269 :   if (VAR_P (pattern))
   22147                 :             :     {
   22148                 :             :       /* We need to determine if we're using a partial or explicit
   22149                 :             :          specialization now, because the type of the variable could be
   22150                 :             :          different.  */
   22151                 :     2956771 :       tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr);
   22152                 :     2956771 :       partial_ti = most_specialized_partial_spec (tid, complain);
   22153                 :     2956771 :       if (partial_ti == error_mark_node)
   22154                 :             :         pattern = error_mark_node;
   22155                 :     2956768 :       else if (partial_ti)
   22156                 :             :         {
   22157                 :      212193 :           tree partial_tmpl = TI_TEMPLATE (partial_ti);
   22158                 :      212193 :           tree partial_args = TI_ARGS (partial_ti);
   22159                 :      212193 :           tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
   22160                 :      212193 :           fndecl = tsubst_decl (partial_pat, partial_args, complain,
   22161                 :             :                                 /*use_spec_table=*/false);
   22162                 :             :         }
   22163                 :             :     }
   22164                 :             : 
   22165                 :             :   /* Substitute template parameters to obtain the specialization.  */
   22166                 :    70272269 :   if (fndecl == NULL_TREE)
   22167                 :    70060076 :     fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false);
   22168                 :    70264133 :   if (DECL_CLASS_SCOPE_P (gen_tmpl))
   22169                 :    19845482 :     pop_nested_class ();
   22170                 :    70264133 :   pop_from_top_level ();
   22171                 :             : 
   22172                 :    70264133 :   if (fndecl == error_mark_node)
   22173                 :             :     {
   22174                 :    26161332 :       pop_deferring_access_checks ();
   22175                 :    26161332 :       return error_mark_node;
   22176                 :             :     }
   22177                 :             : 
   22178                 :             :   /* The DECL_TI_TEMPLATE should always be the immediate parent
   22179                 :             :      template, not the most general template.  */
   22180                 :    44102801 :   DECL_TI_TEMPLATE (fndecl) = tmpl;
   22181                 :    44102801 :   DECL_TI_ARGS (fndecl) = targ_ptr;
   22182                 :    44102801 :   if (VAR_P (pattern))
   22183                 :             :     {
   22184                 :             :       /* Now that we we've formed this variable template specialization,
   22185                 :             :          remember the result of most_specialized_partial_spec for it.  */
   22186                 :     2956768 :       TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti;
   22187                 :             : 
   22188                 :             :       /* And remember if the variable was declared with [].  */
   22189                 :     2956768 :       if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE
   22190                 :     2956768 :           && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE)
   22191                 :           6 :         SET_VAR_HAD_UNKNOWN_BOUND (fndecl);
   22192                 :             :     }
   22193                 :             : 
   22194                 :    44102801 :   fndecl = register_specialization (fndecl, gen_tmpl, targ_ptr, false, hash);
   22195                 :    44102801 :   if (fndecl == error_mark_node)
   22196                 :             :     return error_mark_node;
   22197                 :             : 
   22198                 :    44102798 :   set_instantiating_module (fndecl);
   22199                 :             : 
   22200                 :             :   /* Now we know the specialization, compute access previously
   22201                 :             :      deferred.  Do no access control for inheriting constructors,
   22202                 :             :      as we already checked access for the inherited constructor.  */
   22203                 :    44102798 :   if (!(flag_new_inheriting_ctors
   22204                 :    63886826 :         && DECL_INHERITED_CTOR (fndecl)))
   22205                 :             :     {
   22206                 :    44092428 :       push_access_scope (fndecl);
   22207                 :    44092428 :       if (!perform_deferred_access_checks (complain))
   22208                 :             :         access_ok = false;
   22209                 :    44092428 :       pop_access_scope (fndecl);
   22210                 :             :     }
   22211                 :    44102798 :   pop_deferring_access_checks ();
   22212                 :             : 
   22213                 :             :   /* If we've just instantiated the main entry point for a function,
   22214                 :             :      instantiate all the alternate entry points as well.  We do this
   22215                 :             :      by cloning the instantiation of the main entry point, not by
   22216                 :             :      instantiating the template clones.  */
   22217                 :    44102798 :   if (tree chain = DECL_CHAIN (gen_tmpl))
   22218                 :    43805293 :     if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
   22219                 :       86152 :       clone_cdtor (fndecl, /*update_methods=*/false);
   22220                 :             : 
   22221                 :    44102798 :   if (!access_ok)
   22222                 :             :     {
   22223                 :          99 :       if (!(complain & tf_error))
   22224                 :             :         {
   22225                 :             :           /* Remember to reinstantiate when we're out of SFINAE so the user
   22226                 :             :              can see the errors.  */
   22227                 :          99 :           FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
   22228                 :             :         }
   22229                 :          99 :       return error_mark_node;
   22230                 :             :     }
   22231                 :             : 
   22232                 :             :   return fndecl;
   22233                 :   153765379 : }
   22234                 :             : 
   22235                 :             : /* Instantiate the alias template TMPL with ARGS.  Also push a template
   22236                 :             :    instantiation level, which instantiate_template doesn't do because
   22237                 :             :    functions and variables have sufficient context established by the
   22238                 :             :    callers.  */
   22239                 :             : 
   22240                 :             : static tree
   22241                 :    91242599 : instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
   22242                 :             : {
   22243                 :    91242599 :   if (tmpl == error_mark_node || args == error_mark_node)
   22244                 :             :     return error_mark_node;
   22245                 :             : 
   22246                 :    90189083 :   args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl),
   22247                 :             :                                 args, tmpl, complain);
   22248                 :    90189083 :   if (args == error_mark_node)
   22249                 :             :     return error_mark_node;
   22250                 :             : 
   22251                 :             :   /* FIXME check for satisfaction in check_instantiated_args.  */
   22252                 :    90189067 :   if (!constraints_satisfied_p (tmpl, args))
   22253                 :             :     {
   22254                 :       51605 :       if (complain & tf_error)
   22255                 :             :         {
   22256                 :          21 :           auto_diagnostic_group d;
   22257                 :          21 :           error ("template constraint failure for %qD", tmpl);
   22258                 :          21 :           diagnose_constraints (input_location, tmpl, args);
   22259                 :          21 :         }
   22260                 :       51605 :       return error_mark_node;
   22261                 :             :     }
   22262                 :             : 
   22263                 :    90137462 :   if (!push_tinst_level (tmpl, args))
   22264                 :           0 :     return error_mark_node;
   22265                 :    90137459 :   tree r = instantiate_template (tmpl, args, complain);
   22266                 :    90137459 :   pop_tinst_level ();
   22267                 :             : 
   22268                 :    90137459 :   if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
   22269                 :             :     {
   22270                 :             :       /* An alias template specialization can be dependent
   22271                 :             :          even if its underlying type is not.  */
   22272                 :     6751349 :       TYPE_DEPENDENT_P (d) = true;
   22273                 :     6751349 :       TYPE_DEPENDENT_P_VALID (d) = true;
   22274                 :             :       /* Sometimes a dependent alias spec is equivalent to its expansion,
   22275                 :             :          sometimes not.  So always use structural_comptypes.  */
   22276                 :     6751349 :       SET_TYPE_STRUCTURAL_EQUALITY (d);
   22277                 :             :     }
   22278                 :             : 
   22279                 :             :   return r;
   22280                 :             : }
   22281                 :             : 
   22282                 :             : /* PARM is a template parameter pack for FN.  Returns true iff
   22283                 :             :    PARM is used in a deducible way in the argument list of FN.  */
   22284                 :             : 
   22285                 :             : static bool
   22286                 :     5812932 : pack_deducible_p (tree parm, tree fn)
   22287                 :             : {
   22288                 :     5812932 :   tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
   22289                 :    11656545 :   for (; t; t = TREE_CHAIN (t))
   22290                 :             :     {
   22291                 :     5843848 :       tree type = TREE_VALUE (t);
   22292                 :     5843848 :       tree packs;
   22293                 :     5843848 :       if (!PACK_EXPANSION_P (type))
   22294                 :     5843580 :         continue;
   22295                 :         536 :       for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
   22296                 :         304 :            packs; packs = TREE_CHAIN (packs))
   22297                 :         271 :         if (template_args_equal (TREE_VALUE (packs), parm))
   22298                 :             :           {
   22299                 :             :             /* The template parameter pack is used in a function parameter
   22300                 :             :                pack.  If this is the end of the parameter list, the
   22301                 :             :                template parameter pack is deducible.  */
   22302                 :         235 :             if (TREE_CHAIN (t) == void_list_node)
   22303                 :             :               return true;
   22304                 :             :             else
   22305                 :             :               /* Otherwise, not.  Well, it could be deduced from
   22306                 :             :                  a non-pack parameter, but doing so would end up with
   22307                 :             :                  a deduction mismatch, so don't bother.  */
   22308                 :             :               return false;
   22309                 :             :           }
   22310                 :             :     }
   22311                 :             :   /* The template parameter pack isn't used in any function parameter
   22312                 :             :      packs, but it might be used deeper, e.g. tuple<Args...>.  */
   22313                 :             :   return true;
   22314                 :             : }
   22315                 :             : 
   22316                 :             : /* Subroutine of fn_type_unification: check non-dependent parms for
   22317                 :             :    convertibility.  */
   22318                 :             : 
   22319                 :             : static int
   22320                 :    69390703 : check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
   22321                 :             :                                  tree fn, unification_kind_t strict, int flags,
   22322                 :             :                                  struct conversion **convs, bool explain_p,
   22323                 :             :                                  bool noninst_only_p)
   22324                 :             : {
   22325                 :             :   /* Non-constructor methods need to leave a conversion for 'this', which
   22326                 :             :      isn't included in nargs here.  */
   22327                 :    69390703 :   unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   22328                 :    81363042 :                      && !DECL_CONSTRUCTOR_P (fn));
   22329                 :             : 
   22330                 :    69390703 :   for (unsigned ia = 0;
   22331                 :   159681503 :        parms && parms != void_list_node && ia < nargs; )
   22332                 :             :     {
   22333                 :    92879645 :       tree parm = TREE_VALUE (parms);
   22334                 :             : 
   22335                 :    92879645 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   22336                 :    92879645 :           && (!TREE_CHAIN (parms)
   22337                 :      667951 :               || TREE_CHAIN (parms) == void_list_node))
   22338                 :             :         /* For a function parameter pack that occurs at the end of the
   22339                 :             :            parameter-declaration-list, the type A of each remaining
   22340                 :             :            argument of the call is compared with the type P of the
   22341                 :             :            declarator-id of the function parameter pack.  */
   22342                 :             :         break;
   22343                 :             : 
   22344                 :    92211772 :       parms = TREE_CHAIN (parms);
   22345                 :             : 
   22346                 :    92211772 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   22347                 :             :         /* For a function parameter pack that does not occur at the
   22348                 :             :            end of the parameter-declaration-list, the type of the
   22349                 :             :            parameter pack is a non-deduced context.  */
   22350                 :          80 :         continue;
   22351                 :             : 
   22352                 :    92211692 :       if (!uses_template_parms (parm))
   22353                 :             :         {
   22354                 :    21908311 :           tree arg = args[ia];
   22355                 :    21908311 :           conversion **conv_p = convs ? &convs[ia+offset] : NULL;
   22356                 :    21908311 :           int lflags = conv_flags (ia, nargs, fn, arg, flags);
   22357                 :             : 
   22358                 :    21908311 :           if (check_non_deducible_conversion (parm, arg, strict, lflags,
   22359                 :             :                                               conv_p, explain_p, noninst_only_p))
   22360                 :             :             return 1;
   22361                 :             :         }
   22362                 :             : 
   22363                 :    90290720 :       ++ia;
   22364                 :             :     }
   22365                 :             : 
   22366                 :             :   return 0;
   22367                 :             : }
   22368                 :             : 
   22369                 :             : /* The FN is a TEMPLATE_DECL for a function.  ARGS is an array with
   22370                 :             :    NARGS elements of the arguments that are being used when calling
   22371                 :             :    it.  TARGS is a vector into which the deduced template arguments
   22372                 :             :    are placed.
   22373                 :             : 
   22374                 :             :    Returns either a FUNCTION_DECL for the matching specialization of FN or
   22375                 :             :    NULL_TREE if no suitable specialization can be found.  If EXPLAIN_P is
   22376                 :             :    true, diagnostics will be printed to explain why it failed.
   22377                 :             : 
   22378                 :             :    If FN is a conversion operator, or we are trying to produce a specific
   22379                 :             :    specialization, RETURN_TYPE is the return type desired.
   22380                 :             : 
   22381                 :             :    The EXPLICIT_TARGS are explicit template arguments provided via a
   22382                 :             :    template-id.
   22383                 :             : 
   22384                 :             :    The parameter STRICT is one of:
   22385                 :             : 
   22386                 :             :    DEDUCE_CALL:
   22387                 :             :      We are deducing arguments for a function call, as in
   22388                 :             :      [temp.deduct.call].  If RETURN_TYPE is non-null, we are
   22389                 :             :      deducing arguments for a call to the result of a conversion
   22390                 :             :      function template, as in [over.call.object].
   22391                 :             : 
   22392                 :             :    DEDUCE_CONV:
   22393                 :             :      We are deducing arguments for a conversion function, as in
   22394                 :             :      [temp.deduct.conv].
   22395                 :             : 
   22396                 :             :    DEDUCE_EXACT:
   22397                 :             :      We are deducing arguments when doing an explicit instantiation
   22398                 :             :      as in [temp.explicit], when determining an explicit specialization
   22399                 :             :      as in [temp.expl.spec], or when taking the address of a function
   22400                 :             :      template, as in [temp.deduct.funcaddr].  */
   22401                 :             : 
   22402                 :             : tree
   22403                 :   289726908 : fn_type_unification (tree fn,
   22404                 :             :                      tree explicit_targs,
   22405                 :             :                      tree targs,
   22406                 :             :                      const tree *args,
   22407                 :             :                      unsigned int nargs,
   22408                 :             :                      tree return_type,
   22409                 :             :                      unification_kind_t strict,
   22410                 :             :                      int flags,
   22411                 :             :                      struct conversion **convs,
   22412                 :             :                      bool explain_p,
   22413                 :             :                      bool decltype_p)
   22414                 :             : {
   22415                 :   289726908 :   tree parms;
   22416                 :   289726908 :   tree fntype;
   22417                 :   289726908 :   tree decl = NULL_TREE;
   22418                 :   289726908 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   22419                 :   289726908 :   bool ok;
   22420                 :   289726908 :   static int deduction_depth;
   22421                 :             :   /* type_unification_real will pass back any access checks from default
   22422                 :             :      template argument substitution.  */
   22423                 :   289726908 :   vec<deferred_access_check, va_gc> *checks = NULL;
   22424                 :             :   /* We don't have all the template args yet.  */
   22425                 :   289726908 :   bool incomplete = true;
   22426                 :             : 
   22427                 :   289726908 :   tree orig_fn = fn;
   22428                 :   289726908 :   if (flag_new_inheriting_ctors)
   22429                 :   289725062 :     fn = strip_inheriting_ctors (fn);
   22430                 :             : 
   22431                 :   289726908 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
   22432                 :   289726908 :   tree r = error_mark_node;
   22433                 :             : 
   22434                 :   289726908 :   tree full_targs = targs;
   22435                 :   579453816 :   if (TMPL_ARGS_DEPTH (targs)
   22436                 :   289726908 :       < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
   22437                 :       32070 :     full_targs = (add_outermost_template_args
   22438                 :       32070 :                   (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
   22439                 :             :                    targs));
   22440                 :             : 
   22441                 :   289726908 :   if (decltype_p)
   22442                 :    13730073 :     complain |= tf_decltype;
   22443                 :             : 
   22444                 :             :   /* In C++0x, it's possible to have a function template whose type depends
   22445                 :             :      on itself recursively.  This is most obvious with decltype, but can also
   22446                 :             :      occur with enumeration scope (c++/48969).  So we need to catch infinite
   22447                 :             :      recursion and reject the substitution at deduction time; this function
   22448                 :             :      will return error_mark_node for any repeated substitution.
   22449                 :             : 
   22450                 :             :      This also catches excessive recursion such as when f<N> depends on
   22451                 :             :      f<N-1> across all integers, and returns error_mark_node for all the
   22452                 :             :      substitutions back up to the initial one.
   22453                 :             : 
   22454                 :             :      This is, of course, not reentrant.  */
   22455                 :   289726908 :   if (excessive_deduction_depth)
   22456                 :           0 :     return error_mark_node;
   22457                 :   289726908 :   ++deduction_depth;
   22458                 :             : 
   22459                 :   289726908 :   gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
   22460                 :             : 
   22461                 :   289726908 :   fntype = TREE_TYPE (fn);
   22462                 :   289726908 :   if (explicit_targs)
   22463                 :             :     {
   22464                 :             :       /* [temp.deduct]
   22465                 :             : 
   22466                 :             :          The specified template arguments must match the template
   22467                 :             :          parameters in kind (i.e., type, nontype, template), and there
   22468                 :             :          must not be more arguments than there are parameters;
   22469                 :             :          otherwise type deduction fails.
   22470                 :             : 
   22471                 :             :          Nontype arguments must match the types of the corresponding
   22472                 :             :          nontype template parameters, or must be convertible to the
   22473                 :             :          types of the corresponding nontype parameters as specified in
   22474                 :             :          _temp.arg.nontype_, otherwise type deduction fails.
   22475                 :             : 
   22476                 :             :          All references in the function type of the function template
   22477                 :             :          to the corresponding template parameters are replaced by the
   22478                 :             :          specified template argument values.  If a substitution in a
   22479                 :             :          template parameter or in the function type of the function
   22480                 :             :          template results in an invalid type, type deduction fails.  */
   22481                 :    31587193 :       int i, len = TREE_VEC_LENGTH (tparms);
   22482                 :    31587193 :       location_t loc = input_location;
   22483                 :    31587193 :       incomplete = false;
   22484                 :             : 
   22485                 :    31587193 :       if (explicit_targs == error_mark_node)
   22486                 :          16 :         goto fail;
   22487                 :             : 
   22488                 :    63174354 :       if (TMPL_ARGS_DEPTH (explicit_targs)
   22489                 :    63174354 :           < TMPL_ARGS_DEPTH (full_targs))
   22490                 :        3798 :         explicit_targs = add_outermost_template_args (full_targs,
   22491                 :             :                                                       explicit_targs);
   22492                 :             : 
   22493                 :             :       /* Adjust any explicit template arguments before entering the
   22494                 :             :          substitution context.  */
   22495                 :    31587177 :       explicit_targs
   22496                 :    31587177 :         = (coerce_template_parms (tparms, explicit_targs, fn,
   22497                 :             :                                   complain|tf_partial,
   22498                 :             :                                   /*require_all_args=*/false));
   22499                 :    31587177 :       if (explicit_targs == error_mark_node)
   22500                 :     2049709 :         goto fail;
   22501                 :             : 
   22502                 :             :       /* Substitute the explicit args into the function type.  This is
   22503                 :             :          necessary so that, for instance, explicitly declared function
   22504                 :             :          arguments can match null pointed constants.  If we were given
   22505                 :             :          an incomplete set of explicit args, we must not do semantic
   22506                 :             :          processing during substitution as we could create partial
   22507                 :             :          instantiations.  */
   22508                 :    71637417 :       for (i = 0; i < len; i++)
   22509                 :             :         {
   22510                 :    42099949 :           tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
   22511                 :    42099949 :           bool parameter_pack = false;
   22512                 :    42099949 :           tree targ = TREE_VEC_ELT (explicit_targs, i);
   22513                 :             : 
   22514                 :             :           /* Dig out the actual parm.  */
   22515                 :    42099949 :           if (TREE_CODE (parm) == TYPE_DECL
   22516                 :     3798258 :               || TREE_CODE (parm) == TEMPLATE_DECL)
   22517                 :             :             {
   22518                 :    38304714 :               parm = TREE_TYPE (parm);
   22519                 :    38304714 :               parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
   22520                 :             :             }
   22521                 :     3795235 :           else if (TREE_CODE (parm) == PARM_DECL)
   22522                 :             :             {
   22523                 :     3795235 :               parm = DECL_INITIAL (parm);
   22524                 :     3795235 :               parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
   22525                 :             :             }
   22526                 :             : 
   22527                 :    42099949 :           if (targ == NULL_TREE)
   22528                 :             :             /* No explicit argument for this template parameter.  */
   22529                 :             :             incomplete = true;
   22530                 :    35367276 :           else if (parameter_pack && pack_deducible_p (parm, fn))
   22531                 :             :             {
   22532                 :             :               /* Mark the argument pack as "incomplete". We could
   22533                 :             :                  still deduce more arguments during unification.
   22534                 :             :                  We remove this mark in type_unification_real.  */
   22535                 :     5812919 :               ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
   22536                 :     5812919 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ)
   22537                 :     5812919 :                 = ARGUMENT_PACK_ARGS (targ);
   22538                 :             : 
   22539                 :             :               /* We have some incomplete argument packs.  */
   22540                 :     5812919 :               incomplete = true;
   22541                 :             :             }
   22542                 :             :         }
   22543                 :             : 
   22544                 :    29537468 :       if (incomplete)
   22545                 :             :         {
   22546                 :    10172694 :           if (!push_tinst_level (fn, explicit_targs))
   22547                 :             :             {
   22548                 :           0 :               excessive_deduction_depth = true;
   22549                 :           0 :               goto fail;
   22550                 :             :             }
   22551                 :    10172690 :           ++processing_template_decl;
   22552                 :    10172690 :           input_location = DECL_SOURCE_LOCATION (fn);
   22553                 :             :           /* Ignore any access checks; we'll see them again in
   22554                 :             :              instantiate_template and they might have the wrong
   22555                 :             :              access path at this point.  */
   22556                 :    10172690 :           push_deferring_access_checks (dk_deferred);
   22557                 :    10172690 :           tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
   22558                 :    10172690 :           fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
   22559                 :    10169993 :           pop_deferring_access_checks ();
   22560                 :    10169993 :           input_location = loc;
   22561                 :    10169993 :           --processing_template_decl;
   22562                 :    10169993 :           pop_tinst_level ();
   22563                 :             : 
   22564                 :    10169993 :           if (fntype == error_mark_node)
   22565                 :         638 :             goto fail;
   22566                 :             :         }
   22567                 :             : 
   22568                 :             :       /* Place the explicitly specified arguments in TARGS.  */
   22569                 :    29534129 :       explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
   22570                 :    71627265 :       for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
   22571                 :    42093136 :         TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
   22572                 :    29534129 :       if (!incomplete && CHECKING_P
   22573                 :    48898903 :           && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   22574                 :    19364774 :         SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
   22575                 :             :           (targs, NUM_TMPL_ARGS (explicit_targs));
   22576                 :             :     }
   22577                 :             : 
   22578                 :   287673844 :   if (return_type && strict != DEDUCE_CALL)
   22579                 :             :     {
   22580                 :    11514001 :       tree *new_args = XALLOCAVEC (tree, nargs + 1);
   22581                 :    11514001 :       new_args[0] = return_type;
   22582                 :    11514001 :       memcpy (new_args + 1, args, nargs * sizeof (tree));
   22583                 :    11514001 :       args = new_args;
   22584                 :    11514001 :       ++nargs;
   22585                 :             :     }
   22586                 :             : 
   22587                 :   287673844 :   if (!incomplete)
   22588                 :    19364774 :     goto deduced;
   22589                 :             : 
   22590                 :             :   /* Never do unification on the 'this' parameter.  */
   22591                 :   268309070 :   parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
   22592                 :             : 
   22593                 :   268309070 :   if (return_type && strict == DEDUCE_CALL)
   22594                 :             :     {
   22595                 :             :       /* We're deducing for a call to the result of a template conversion
   22596                 :             :          function.  The parms we really want are in return_type.  */
   22597                 :          17 :       if (INDIRECT_TYPE_P (return_type))
   22598                 :          17 :         return_type = TREE_TYPE (return_type);
   22599                 :          17 :       parms = TYPE_ARG_TYPES (return_type);
   22600                 :             :     }
   22601                 :   268309053 :   else if (return_type)
   22602                 :             :     {
   22603                 :    10670296 :       parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
   22604                 :             :     }
   22605                 :             : 
   22606                 :             :   /* We allow incomplete unification without an error message here
   22607                 :             :      because the standard doesn't seem to explicitly prohibit it.  Our
   22608                 :             :      callers must be ready to deal with unification failures in any
   22609                 :             :      event.  */
   22610                 :             : 
   22611                 :             :   /* If we aren't explaining yet, push tinst context so we can see where
   22612                 :             :      any errors (e.g. from class instantiations triggered by instantiation
   22613                 :             :      of default template arguments) come from.  If we are explaining, this
   22614                 :             :      context is redundant.  */
   22615                 :   268309070 :   if (!explain_p && !push_tinst_level (fn, targs))
   22616                 :             :     {
   22617                 :           0 :       excessive_deduction_depth = true;
   22618                 :           0 :       goto fail;
   22619                 :             :     }
   22620                 :             : 
   22621                 :   268309064 :   ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   22622                 :             :                                full_targs, parms, args, nargs, /*subr=*/0,
   22623                 :             :                                strict, &checks, explain_p);
   22624                 :   268309064 :   if (!explain_p)
   22625                 :   268306357 :     pop_tinst_level ();
   22626                 :   268309064 :   if (!ok)
   22627                 :   231964987 :     goto fail;
   22628                 :             : 
   22629                 :             :   /* Now that we have bindings for all of the template arguments,
   22630                 :             :      ensure that the arguments deduced for the template template
   22631                 :             :      parameters have compatible template parameter lists.  We cannot
   22632                 :             :      check this property before we have deduced all template
   22633                 :             :      arguments, because the template parameter types of a template
   22634                 :             :      template parameter might depend on prior template parameters
   22635                 :             :      deduced after the template template parameter.  The following
   22636                 :             :      ill-formed example illustrates this issue:
   22637                 :             : 
   22638                 :             :        template<typename T, template<T> class C> void f(C<5>, T);
   22639                 :             : 
   22640                 :             :        template<int N> struct X {};
   22641                 :             : 
   22642                 :             :        void g() {
   22643                 :             :          f(X<5>(), 5l); // error: template argument deduction fails
   22644                 :             :        }
   22645                 :             : 
   22646                 :             :      The template parameter list of 'C' depends on the template type
   22647                 :             :      parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
   22648                 :             :      'long'.  Thus, we can't check that 'C' cannot bind to 'X' at the
   22649                 :             :      time that we deduce 'C'.  */
   22650                 :    72688154 :   if (!template_template_parm_bindings_ok_p
   22651                 :    36344077 :            (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
   22652                 :             :     {
   22653                 :          32 :       unify_inconsistent_template_template_parameters (explain_p);
   22654                 :          32 :       goto fail;
   22655                 :             :     }
   22656                 :             : 
   22657                 :    36344045 :  deduced:
   22658                 :             : 
   22659                 :             :   /* As a refinement of CWG2369, check first and foremost non-dependent
   22660                 :             :      conversions that we know are not going to induce template instantiation
   22661                 :             :      (PR99599).  */
   22662                 :    55708819 :   if (strict == DEDUCE_CALL
   22663                 :    55708819 :       && incomplete
   22664                 :    55708819 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   22665                 :             :                                           convs, explain_p,
   22666                 :             :                                           /*noninst_only_p=*/true))
   22667                 :     1215544 :     goto fail;
   22668                 :             : 
   22669                 :             :   /* CWG2369: Check satisfaction before non-deducible conversions.  */
   22670                 :    54493275 :   if (!constraints_satisfied_p (fn, targs))
   22671                 :             :     {
   22672                 :      518907 :       if (explain_p)
   22673                 :         310 :         diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
   22674                 :      518907 :       goto fail;
   22675                 :             :     }
   22676                 :             : 
   22677                 :             :   /* DR 1391: All parameters have args, now check non-dependent parms for
   22678                 :             :      convertibility.  We don't do this if all args were explicitly specified,
   22679                 :             :      as the standard says that we substitute explicit args immediately.  */
   22680                 :    53973468 :   if (incomplete
   22681                 :    53973468 :       && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
   22682                 :             :                                           convs, explain_p,
   22683                 :             :                                           /*noninst_only_p=*/false))
   22684                 :      705428 :     goto fail;
   22685                 :             : 
   22686                 :             :   /* All is well so far.  Now, check:
   22687                 :             : 
   22688                 :             :      [temp.deduct]
   22689                 :             : 
   22690                 :             :      When all template arguments have been deduced, all uses of
   22691                 :             :      template parameters in nondeduced contexts are replaced with
   22692                 :             :      the corresponding deduced argument values.  If the
   22693                 :             :      substitution results in an invalid type, as described above,
   22694                 :             :      type deduction fails.  */
   22695                 :    53268040 :   if (!push_tinst_level (fn, targs))
   22696                 :             :     {
   22697                 :           0 :       excessive_deduction_depth = true;
   22698                 :           0 :       goto fail;
   22699                 :             :     }
   22700                 :             : 
   22701                 :             :   /* Also collect access checks from the instantiation.  */
   22702                 :    53268033 :   reopen_deferring_access_checks (checks);
   22703                 :             : 
   22704                 :    53268033 :   decl = instantiate_template (fn, targs, complain);
   22705                 :             : 
   22706                 :    53259897 :   checks = get_deferred_access_checks ();
   22707                 :    53259897 :   pop_deferring_access_checks ();
   22708                 :             : 
   22709                 :    53259897 :   pop_tinst_level ();
   22710                 :             : 
   22711                 :    53259897 :   if (decl == error_mark_node)
   22712                 :     1417023 :     goto fail;
   22713                 :             : 
   22714                 :             :   /* Now perform any access checks encountered during substitution.  */
   22715                 :    51842874 :   push_access_scope (decl);
   22716                 :    51842874 :   ok = perform_access_checks (checks, complain);
   22717                 :    51842874 :   pop_access_scope (decl);
   22718                 :    51842874 :   if (!ok)
   22719                 :          22 :     goto fail;
   22720                 :             : 
   22721                 :             :   /* If we're looking for an exact match, check that what we got
   22722                 :             :      is indeed an exact match.  It might not be if some template
   22723                 :             :      parameters are used in non-deduced contexts.  But don't check
   22724                 :             :      for an exact match if we have dependent template arguments;
   22725                 :             :      in that case we're doing partial ordering, and we already know
   22726                 :             :      that we have two candidates that will provide the actual type.  */
   22727                 :    51842852 :   if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
   22728                 :             :     {
   22729                 :     2159466 :       tree substed = TREE_TYPE (decl);
   22730                 :     2159466 :       unsigned int i;
   22731                 :             : 
   22732                 :     2159466 :       tree sarg
   22733                 :     2159466 :         = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
   22734                 :     2159466 :       if (return_type)
   22735                 :     2159286 :         sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
   22736                 :     9450549 :       for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
   22737                 :     7361812 :         if (!same_type_p (args[i], TREE_VALUE (sarg)))
   22738                 :             :           {
   22739                 :       70729 :             unify_type_mismatch (explain_p, args[i],
   22740                 :       70729 :                                  TREE_VALUE (sarg));
   22741                 :       70729 :             goto fail;
   22742                 :             :           }
   22743                 :     2088737 :       if ((i < nargs || sarg)
   22744                 :             :           /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args
   22745                 :             :              doesn't contain the trailing void, and conv fns are always ().  */
   22746                 :     2088745 :           && !DECL_CONV_FN_P (decl))
   22747                 :             :         {
   22748                 :           8 :           unsigned nsargs = i + list_length (sarg);
   22749                 :           8 :           unify_arity (explain_p, nargs, nsargs);
   22750                 :           8 :           goto fail;
   22751                 :             :         }
   22752                 :             :     }
   22753                 :             : 
   22754                 :             :   /* After doing deduction with the inherited constructor, actually return an
   22755                 :             :      instantiation of the inheriting constructor.  */
   22756                 :    51772115 :   if (orig_fn != fn)
   22757                 :       13781 :     decl = instantiate_template (orig_fn, targs, complain);
   22758                 :             : 
   22759                 :             :   r = decl;
   22760                 :             : 
   22761                 :   289715158 :  fail:
   22762                 :   289715158 :   --deduction_depth;
   22763                 :   289715158 :   if (excessive_deduction_depth)
   22764                 :             :     {
   22765                 :           0 :       if (deduction_depth == 0)
   22766                 :             :         /* Reset once we're all the way out.  */
   22767                 :           0 :         excessive_deduction_depth = false;
   22768                 :             :     }
   22769                 :             : 
   22770                 :             :   return r;
   22771                 :             : }
   22772                 :             : 
   22773                 :             : /* Returns true iff PARM is a forwarding reference in the context of
   22774                 :             :    template argument deduction for TMPL.  */
   22775                 :             : 
   22776                 :             : static bool
   22777                 :   295216277 : forwarding_reference_p (tree parm, tree tmpl)
   22778                 :             : {
   22779                 :             :   /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a
   22780                 :             :      cv-unqualified template parameter ..."  */
   22781                 :   295216277 :   if (TYPE_REF_P (parm)
   22782                 :   249210264 :       && TYPE_REF_IS_RVALUE (parm)
   22783                 :    11391305 :       && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM
   22784                 :   302280895 :       && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED)
   22785                 :             :     {
   22786                 :     6963085 :       parm = TREE_TYPE (parm);
   22787                 :             :       /* [temp.deduct.call], "... that does not represent a template parameter
   22788                 :             :          of a class template (during class template argument deduction)."  */
   22789                 :     6963085 :       if (tmpl
   22790                 :     6869515 :           && deduction_guide_p (tmpl)
   22791                 :     6976325 :           && DECL_ARTIFICIAL (tmpl))
   22792                 :             :         {
   22793                 :             :           /* Since the template parameters of a synthesized guide consist of
   22794                 :             :              the template parameters of the class template followed by those of
   22795                 :             :              the constructor (if any), we can tell if PARM represents a template
   22796                 :             :              parameter of the class template by comparing its index with the
   22797                 :             :              arity of the class template.  */
   22798                 :        7150 :           tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl)));
   22799                 :        7150 :           if (TEMPLATE_TYPE_IDX (parm)
   22800                 :        7150 :               < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl)))
   22801                 :             :             return false;
   22802                 :             :         }
   22803                 :     6962742 :       return true;
   22804                 :             :     }
   22805                 :             :   return false;
   22806                 :             : }
   22807                 :             : 
   22808                 :             : /* Adjust types before performing type deduction, as described in
   22809                 :             :    [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
   22810                 :             :    sections are symmetric.  PARM is the type of a function parameter
   22811                 :             :    or the return type of the conversion function.  ARG is the type of
   22812                 :             :    the argument passed to the call, or the type of the value
   22813                 :             :    initialized with the result of the conversion function.
   22814                 :             :    ARG_EXPR is the original argument expression, which may be null.  */
   22815                 :             : 
   22816                 :             : static int
   22817                 :   295216277 : maybe_adjust_types_for_deduction (tree tparms,
   22818                 :             :                                   unification_kind_t strict,
   22819                 :             :                                   tree* parm,
   22820                 :             :                                   tree* arg,
   22821                 :             :                                   tree arg_expr)
   22822                 :             : {
   22823                 :   295216277 :   int result = 0;
   22824                 :             : 
   22825                 :   295216277 :   switch (strict)
   22826                 :             :     {
   22827                 :             :     case DEDUCE_CALL:
   22828                 :             :       break;
   22829                 :             : 
   22830                 :       64249 :     case DEDUCE_CONV:
   22831                 :             :       /* [temp.deduct.conv] First remove a reference type on parm.
   22832                 :             :          DRs 322 & 976 affected this.  */
   22833                 :       64249 :       if (TYPE_REF_P (*parm))
   22834                 :         100 :         *parm = TREE_TYPE (*parm);
   22835                 :             : 
   22836                 :             :       /* Swap PARM and ARG throughout the remainder of this
   22837                 :             :          function; the handling is precisely symmetric since PARM
   22838                 :             :          will initialize ARG rather than vice versa.  */
   22839                 :             :       std::swap (parm, arg);
   22840                 :             : 
   22841                 :             :       break;
   22842                 :             : 
   22843                 :    27946106 :     case DEDUCE_EXACT:
   22844                 :             :       /* Core issue #873: Do the DR606 thing (see below) for these cases,
   22845                 :             :          too, but here handle it by stripping the reference from PARM
   22846                 :             :          rather than by adding it to ARG.  */
   22847                 :    27946106 :       if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   22848                 :     1148717 :           && TYPE_REF_P (*arg)
   22849                 :    28984264 :           && !TYPE_REF_IS_RVALUE (*arg))
   22850                 :     1038119 :         *parm = TREE_TYPE (*parm);
   22851                 :             :       /* Nothing else to do in this case.  */
   22852                 :             :       return 0;
   22853                 :             : 
   22854                 :           0 :     default:
   22855                 :           0 :       gcc_unreachable ();
   22856                 :             :     }
   22857                 :             : 
   22858                 :   267270171 :   if (!TYPE_REF_P (*parm))
   22859                 :             :     {
   22860                 :             :       /* [temp.deduct.call]
   22861                 :             : 
   22862                 :             :          If P is not a reference type:
   22863                 :             : 
   22864                 :             :          --If A is an array type, the pointer type produced by the
   22865                 :             :          array-to-pointer standard conversion (_conv.array_) is
   22866                 :             :          used in place of A for type deduction; otherwise,
   22867                 :             : 
   22868                 :             :          --If A is a function type, the pointer type produced by
   22869                 :             :          the function-to-pointer standard conversion
   22870                 :             :          (_conv.func_) is used in place of A for type deduction;
   22871                 :             :          otherwise,
   22872                 :             : 
   22873                 :             :          --If A is a cv-qualified type, the top level
   22874                 :             :          cv-qualifiers of A's type are ignored for type
   22875                 :             :          deduction.  */
   22876                 :    39568204 :       if (TREE_CODE (*arg) == ARRAY_TYPE)
   22877                 :      337171 :         *arg = build_pointer_type (TREE_TYPE (*arg));
   22878                 :    39231033 :       else if (TREE_CODE (*arg) == FUNCTION_TYPE)
   22879                 :        3900 :         *arg = build_pointer_type (*arg);
   22880                 :             :       else
   22881                 :    39227133 :         *arg = TYPE_MAIN_VARIANT (*arg);
   22882                 :             :     }
   22883                 :             : 
   22884                 :             :   /* [temp.deduct.call], "If P is a forwarding reference and the argument is
   22885                 :             :      an lvalue, the type 'lvalue reference to A' is used in place of A for
   22886                 :             :      type deduction."  */
   22887                 :   267270171 :   if (forwarding_reference_p (*parm, TPARMS_PRIMARY_TEMPLATE (tparms))
   22888                 :   267270171 :       && (arg_expr ? lvalue_p (arg_expr)
   22889                 :             :           /* try_one_overload doesn't provide an arg_expr, but
   22890                 :             :              functions are always lvalues.  */
   22891                 :          47 :           : TREE_CODE (*arg) == FUNCTION_TYPE))
   22892                 :     2102003 :     *arg = build_reference_type (*arg);
   22893                 :             : 
   22894                 :             :   /* [temp.deduct.call]
   22895                 :             : 
   22896                 :             :      If P is a cv-qualified type, the top level cv-qualifiers
   22897                 :             :      of P's type are ignored for type deduction.  If P is a
   22898                 :             :      reference type, the type referred to by P is used for
   22899                 :             :      type deduction.  */
   22900                 :   267270171 :   *parm = TYPE_MAIN_VARIANT (*parm);
   22901                 :   267270171 :   if (TYPE_REF_P (*parm))
   22902                 :             :     {
   22903                 :   227701967 :       *parm = TREE_TYPE (*parm);
   22904                 :   227701967 :       result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   22905                 :             :     }
   22906                 :             : 
   22907                 :             :   return result;
   22908                 :             : }
   22909                 :             : 
   22910                 :             : /* Return true if computing a conversion from FROM to TO might induce template
   22911                 :             :    instantiation.  Conversely, if this predicate returns false then computing
   22912                 :             :    the conversion definitely won't induce template instantiation.  */
   22913                 :             : 
   22914                 :             : static bool
   22915                 :     4434312 : conversion_may_instantiate_p (tree to, tree from)
   22916                 :             : {
   22917                 :     4434312 :   to = non_reference (to);
   22918                 :     4434312 :   from = non_reference (from);
   22919                 :             : 
   22920                 :     4434312 :   bool ptr_conv_p = false;
   22921                 :     4434312 :   if (TYPE_PTR_P (to)
   22922                 :     1281652 :       && TYPE_PTR_P (from))
   22923                 :             :     {
   22924                 :      191977 :       to = TREE_TYPE (to);
   22925                 :      191977 :       from = TREE_TYPE (from);
   22926                 :      191977 :       ptr_conv_p = true;
   22927                 :             :     }
   22928                 :             : 
   22929                 :             :   /* If one of the types is a not-yet-instantiated class template
   22930                 :             :      specialization, then computing the conversion might instantiate
   22931                 :             :      it in order to inspect bases, conversion functions and/or
   22932                 :             :      converting constructors.  */
   22933                 :     1787925 :   if ((CLASS_TYPE_P (to)
   22934                 :     1787853 :        && !COMPLETE_TYPE_P (to)
   22935                 :        9691 :        && CLASSTYPE_TEMPLATE_INSTANTIATION (to))
   22936                 :     6212554 :       || (CLASS_TYPE_P (from)
   22937                 :     2159573 :           && !COMPLETE_TYPE_P (from)
   22938                 :          14 :           && CLASSTYPE_TEMPLATE_INSTANTIATION (from)))
   22939                 :             :     return true;
   22940                 :             : 
   22941                 :             :   /* Converting from one pointer type to another, or between
   22942                 :             :      reference-related types, always yields a standard conversion.  */
   22943                 :     4424629 :   if (ptr_conv_p || reference_related_p (to, from))
   22944                 :     1676886 :     return false;
   22945                 :             : 
   22946                 :             :   /* Converting to a non-aggregate class type will consider its
   22947                 :             :      user-declared constructors, which might induce instantiation.  */
   22948                 :      893187 :   if (CLASS_TYPE_P (to)
   22949                 :     3640930 :       && CLASSTYPE_NON_AGGREGATE (to))
   22950                 :             :     return true;
   22951                 :             : 
   22952                 :             :   /* Similarly, converting from a class type will consider its conversion
   22953                 :             :      functions.  */
   22954                 :     1003410 :   if (CLASS_TYPE_P (from)
   22955                 :     3288673 :       && TYPE_HAS_CONVERSION (from))
   22956                 :             :     return true;
   22957                 :             : 
   22958                 :             :   /* Otherwise, computing this conversion definitely won't induce
   22959                 :             :      template instantiation.  */
   22960                 :             :   return false;
   22961                 :             : }
   22962                 :             : 
   22963                 :             : /* Subroutine of fn_type_unification.  PARM is a function parameter of a
   22964                 :             :    template which doesn't contain any deducible template parameters; check if
   22965                 :             :    ARG is a suitable match for it.  STRICT, FLAGS and EXPLAIN_P are as in
   22966                 :             :    unify_one_argument.  */
   22967                 :             : 
   22968                 :             : static int
   22969                 :    21908311 : check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict,
   22970                 :             :                                 int flags, struct conversion **conv_p,
   22971                 :             :                                 bool explain_p, bool noninst_only_p)
   22972                 :             : {
   22973                 :    21908311 :   tree type;
   22974                 :             : 
   22975                 :    21908311 :   if (!TYPE_P (arg))
   22976                 :    20910904 :     type = TREE_TYPE (arg);
   22977                 :             :   else
   22978                 :             :     type = arg;
   22979                 :             : 
   22980                 :    21908311 :   if (same_type_p (parm, type))
   22981                 :    19987339 :     return unify_success (explain_p);
   22982                 :             : 
   22983                 :     7468740 :   tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
   22984                 :     7468740 :   if (strict == DEDUCE_CONV)
   22985                 :             :     {
   22986                 :          25 :       if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
   22987                 :    19987339 :         return unify_success (explain_p);
   22988                 :             :     }
   22989                 :     7468715 :   else if (strict == DEDUCE_CALL)
   22990                 :             :     {
   22991                 :     7468715 :       if (conv_p && *conv_p)
   22992                 :             :         {
   22993                 :             :           /* This conversion was already computed earlier (when
   22994                 :             :              computing only non-instantiating conversions).  */
   22995                 :     2229013 :           gcc_checking_assert (!noninst_only_p);
   22996                 :    19987339 :           return unify_success (explain_p);
   22997                 :             :         }
   22998                 :             : 
   22999                 :     5239702 :       if (noninst_only_p
   23000                 :     5239702 :           && conversion_may_instantiate_p (parm, type))
   23001                 :    19987339 :         return unify_success (explain_p);
   23002                 :             : 
   23003                 :     4332570 :       bool ok = false;
   23004                 :     4332570 :       tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
   23005                 :     4332570 :       if (conv_p)
   23006                 :             :         /* Avoid recalculating this in add_function_candidate.  */
   23007                 :     8664678 :         ok = (*conv_p
   23008                 :     4332339 :               = good_conversion (parm, type, conv_arg, flags, complain));
   23009                 :             :       else
   23010                 :         231 :         ok = can_convert_arg (parm, type, conv_arg, flags, complain);
   23011                 :     4332570 :       if (ok)
   23012                 :    19987339 :         return unify_success (explain_p);
   23013                 :             :     }
   23014                 :             : 
   23015                 :     1920972 :   if (strict == DEDUCE_EXACT)
   23016                 :           0 :     return unify_type_mismatch (explain_p, parm, arg);
   23017                 :             :   else
   23018                 :     1920972 :     return unify_arg_conversion (explain_p, parm, type, arg);
   23019                 :             : }
   23020                 :             : 
   23021                 :             : static bool uses_deducible_template_parms (tree type);
   23022                 :             : 
   23023                 :             : /* Returns true iff the expression EXPR is one from which a template
   23024                 :             :    argument can be deduced.  In other words, if it's an undecorated
   23025                 :             :    use of a template non-type parameter.  */
   23026                 :             : 
   23027                 :             : static bool
   23028                 :      888114 : deducible_expression (tree expr)
   23029                 :             : {
   23030                 :             :   /* Strip implicit conversions and implicit INDIRECT_REFs.  */
   23031                 :     1776258 :   while (CONVERT_EXPR_P (expr)
   23032                 :      888123 :          || TREE_CODE (expr) == VIEW_CONVERT_EXPR
   23033                 :     1776481 :          || REFERENCE_REF_P (expr))
   23034                 :         247 :     expr = TREE_OPERAND (expr, 0);
   23035                 :      888114 :   return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
   23036                 :             : }
   23037                 :             : 
   23038                 :             : /* Returns true iff the array domain DOMAIN uses a template parameter in a
   23039                 :             :    deducible way; that is, if it has a max value of <PARM> - 1.  */
   23040                 :             : 
   23041                 :             : static bool
   23042                 :       28029 : deducible_array_bound (tree domain)
   23043                 :             : {
   23044                 :       28029 :   if (domain == NULL_TREE)
   23045                 :             :     return false;
   23046                 :             : 
   23047                 :       27960 :   tree max = TYPE_MAX_VALUE (domain);
   23048                 :       27960 :   if (TREE_CODE (max) != MINUS_EXPR)
   23049                 :             :     return false;
   23050                 :             : 
   23051                 :       27727 :   return deducible_expression (TREE_OPERAND (max, 0));
   23052                 :             : }
   23053                 :             : 
   23054                 :             : /* Returns true iff the template arguments ARGS use a template parameter
   23055                 :             :    in a deducible way.  */
   23056                 :             : 
   23057                 :             : static bool
   23058                 :   230545925 : deducible_template_args (tree args)
   23059                 :             : {
   23060                 :   237822403 :   for (tree elt : tree_vec_range (args))
   23061                 :             :     {
   23062                 :   233252946 :       bool deducible;
   23063                 :   233252946 :       if (ARGUMENT_PACK_P (elt))
   23064                 :    10589017 :         deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
   23065                 :             :       else
   23066                 :             :         {
   23067                 :   222663929 :           if (PACK_EXPANSION_P (elt))
   23068                 :    10272158 :             elt = PACK_EXPANSION_PATTERN (elt);
   23069                 :   222663929 :           if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
   23070                 :             :             deducible = true;
   23071                 :   222663895 :           else if (TYPE_P (elt))
   23072                 :   221803524 :             deducible = uses_deducible_template_parms (elt);
   23073                 :             :           else
   23074                 :      860371 :             deducible = deducible_expression (elt);
   23075                 :             :         }
   23076                 :   233252912 :       if (deducible)
   23077                 :   225976468 :         return true;
   23078                 :             :     }
   23079                 :     4569457 :   return false;
   23080                 :             : }
   23081                 :             : 
   23082                 :             : /* Returns true iff TYPE contains any deducible references to template
   23083                 :             :    parameters, as per 14.8.2.5.  */
   23084                 :             : 
   23085                 :             : static bool
   23086                 :   528940104 : uses_deducible_template_parms (tree type)
   23087                 :             : {
   23088                 :   776754331 :   if (PACK_EXPANSION_P (type))
   23089                 :          89 :     type = PACK_EXPANSION_PATTERN (type);
   23090                 :             : 
   23091                 :             :   /* T
   23092                 :             :      cv-list T
   23093                 :             :      TT<T>
   23094                 :             :      TT<i>
   23095                 :             :      TT<> */
   23096                 :   776754331 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   23097                 :   509551227 :       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   23098                 :             :     return true;
   23099                 :             : 
   23100                 :             :   /* T*
   23101                 :             :      T&
   23102                 :             :      T&&  */
   23103                 :   509550488 :   if (INDIRECT_TYPE_P (type))
   23104                 :   247814227 :     return uses_deducible_template_parms (TREE_TYPE (type));
   23105                 :             : 
   23106                 :             :   /* T[integer-constant ]
   23107                 :             :      type [i]  */
   23108                 :   261736261 :   if (TREE_CODE (type) == ARRAY_TYPE)
   23109                 :      502510 :     return (uses_deducible_template_parms (TREE_TYPE (type))
   23110                 :      530476 :             || deducible_array_bound (TYPE_DOMAIN (type)));
   23111                 :             : 
   23112                 :             :   /* T type ::*
   23113                 :             :      type T::*
   23114                 :             :      T T::*
   23115                 :             :      T (type ::*)()
   23116                 :             :      type (T::*)()
   23117                 :             :      type (type ::*)(T)
   23118                 :             :      type (T::*)(T)
   23119                 :             :      T (type ::*)(T)
   23120                 :             :      T (T::*)()
   23121                 :             :      T (T::*)(T) */
   23122                 :   261233751 :   if (TYPE_PTRMEM_P (type))
   23123                 :       22555 :     return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
   23124                 :       22904 :             || (uses_deducible_template_parms
   23125                 :         349 :                 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
   23126                 :             : 
   23127                 :             :   /* template-name <T> (where template-name refers to a class template)
   23128                 :             :      template-name <i> (where template-name refers to a class template) */
   23129                 :   224293831 :   if (CLASS_TYPE_P (type)
   23130                 :   224293815 :       && CLASSTYPE_TEMPLATE_INFO (type)
   23131                 :   482138273 :       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
   23132                 :   219956908 :     return deducible_template_args (INNERMOST_TEMPLATE_ARGS
   23133                 :   219956908 :                                     (CLASSTYPE_TI_ARGS (type)));
   23134                 :             : 
   23135                 :             :   /* type (T)
   23136                 :             :      T()
   23137                 :             :      T(T)  */
   23138                 :    41254288 :   if (FUNC_OR_METHOD_TYPE_P (type))
   23139                 :             :     {
   23140                 :     5773208 :       if (uses_deducible_template_parms (TREE_TYPE (type)))
   23141                 :             :         return true;
   23142                 :       57218 :       tree parm = TYPE_ARG_TYPES (type);
   23143                 :       57218 :       if (TREE_CODE (type) == METHOD_TYPE)
   23144                 :         259 :         parm = TREE_CHAIN (parm);
   23145                 :       59745 :       for (; parm; parm = TREE_CHAIN (parm))
   23146                 :       58184 :         if (uses_deducible_template_parms (TREE_VALUE (parm)))
   23147                 :             :           return true;
   23148                 :        1561 :       if (flag_noexcept_type
   23149                 :        1541 :           && TYPE_RAISES_EXCEPTIONS (type)
   23150                 :          16 :           && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))
   23151                 :        1577 :           && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type))))
   23152                 :             :         return true;
   23153                 :             :     }
   23154                 :             : 
   23155                 :             :   return false;
   23156                 :             : }
   23157                 :             : 
   23158                 :             : /* Subroutine of type_unification_real and unify_pack_expansion to
   23159                 :             :    handle unification of a single P/A pair.  Parameters are as
   23160                 :             :    for those functions.  */
   23161                 :             : 
   23162                 :             : static int
   23163                 :   335289499 : unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
   23164                 :             :                     int subr, unification_kind_t strict,
   23165                 :             :                     bool explain_p)
   23166                 :             : {
   23167                 :   335289499 :   tree arg_expr = NULL_TREE;
   23168                 :   335289499 :   int arg_strict;
   23169                 :             : 
   23170                 :   335289499 :   if (arg == error_mark_node || parm == error_mark_node)
   23171                 :          10 :     return unify_invalid (explain_p);
   23172                 :   335289490 :   if (arg == unknown_type_node)
   23173                 :             :     /* We can't deduce anything from this, but we might get all the
   23174                 :             :        template args from other function args.  */
   23175                 :    33512652 :     return unify_success (explain_p);
   23176                 :             : 
   23177                 :             :   /* Implicit conversions (Clause 4) will be performed on a function
   23178                 :             :      argument to convert it to the type of the corresponding function
   23179                 :             :      parameter if the parameter type contains no template-parameters that
   23180                 :             :      participate in template argument deduction.  */
   23181                 :   335289490 :   if (strict != DEDUCE_EXACT
   23182                 :   335289490 :       && TYPE_P (parm) && !uses_deducible_template_parms (parm))
   23183                 :             :     /* For function parameters with no deducible template parameters,
   23184                 :             :        just return.  We'll check non-dependent conversions later.  */
   23185                 :    33512652 :     return unify_success (explain_p);
   23186                 :             : 
   23187                 :   301855704 :   switch (strict)
   23188                 :             :     {
   23189                 :             :     case DEDUCE_CALL:
   23190                 :             :       arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
   23191                 :             :                     | UNIFY_ALLOW_MORE_CV_QUAL
   23192                 :             :                     | UNIFY_ALLOW_DERIVED);
   23193                 :             :       break;
   23194                 :             : 
   23195                 :             :     case DEDUCE_CONV:
   23196                 :             :       arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
   23197                 :             :       break;
   23198                 :             : 
   23199                 :             :     case DEDUCE_EXACT:
   23200                 :             :       arg_strict = UNIFY_ALLOW_NONE;
   23201                 :             :       break;
   23202                 :             : 
   23203                 :           0 :     default:
   23204                 :           0 :       gcc_unreachable ();
   23205                 :             :     }
   23206                 :             : 
   23207                 :             :   /* We only do these transformations if this is the top-level
   23208                 :             :      parameter_type_list in a call or declaration matching; in other
   23209                 :             :      situations (nested function declarators, template argument lists) we
   23210                 :             :      won't be comparing a type to an expression, and we don't do any type
   23211                 :             :      adjustments.  */
   23212                 :   301855704 :   if (!subr)
   23213                 :             :     {
   23214                 :   295290706 :       if (!TYPE_P (arg))
   23215                 :             :         {
   23216                 :   267280351 :           gcc_assert (TREE_TYPE (arg) != NULL_TREE);
   23217                 :   267280351 :           if (type_unknown_p (arg))
   23218                 :             :             {
   23219                 :             :               /* [temp.deduct.type] A template-argument can be
   23220                 :             :                  deduced from a pointer to function or pointer
   23221                 :             :                  to member function argument if the set of
   23222                 :             :                  overloaded functions does not contain function
   23223                 :             :                  templates and at most one of a set of
   23224                 :             :                  overloaded functions provides a unique
   23225                 :             :                  match.  */
   23226                 :       78866 :               resolve_overloaded_unification (tparms, targs, parm,
   23227                 :             :                                               arg, strict,
   23228                 :             :                                               arg_strict, explain_p);
   23229                 :             :               /* If a unique match was not found, this is a
   23230                 :             :                  non-deduced context, so we still succeed. */
   23231                 :       78866 :               return unify_success (explain_p);
   23232                 :             :             }
   23233                 :             : 
   23234                 :   267201485 :           arg_expr = arg;
   23235                 :   267201485 :           arg = unlowered_expr_type (arg);
   23236                 :   267201485 :           if (arg == error_mark_node)
   23237                 :          10 :             return unify_invalid (explain_p);
   23238                 :             :         }
   23239                 :             : 
   23240                 :   295211840 :       arg_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   23241                 :             :                                                       &parm, &arg, arg_expr);
   23242                 :             :     }
   23243                 :             :   else
   23244                 :    13129996 :     if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
   23245                 :     6564998 :         != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
   23246                 :           1 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   23247                 :             : 
   23248                 :             :   /* For deduction from an init-list we need the actual list.  */
   23249                 :   295211840 :   if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
   23250                 :        5263 :     arg = arg_expr;
   23251                 :   301776837 :   return unify (tparms, targs, parm, arg, arg_strict, explain_p);
   23252                 :             : }
   23253                 :             : 
   23254                 :             : /* for_each_template_parm callback that always returns 0.  */
   23255                 :             : 
   23256                 :             : static int
   23257                 :    17789705 : zero_r (tree, void *)
   23258                 :             : {
   23259                 :    17789705 :   return 0;
   23260                 :             : }
   23261                 :             : 
   23262                 :             : /* for_each_template_parm any_fn callback to handle deduction of a template
   23263                 :             :    type argument from the type of an array bound.  */
   23264                 :             : 
   23265                 :             : static int
   23266                 :   107191759 : array_deduction_r (tree t, void *data)
   23267                 :             : {
   23268                 :   107191759 :   tree_pair_p d = (tree_pair_p)data;
   23269                 :   107191759 :   tree &tparms = d->purpose;
   23270                 :   107191759 :   tree &targs = d->value;
   23271                 :             : 
   23272                 :   107191759 :   if (TREE_CODE (t) == ARRAY_TYPE)
   23273                 :         150 :     if (tree dom = TYPE_DOMAIN (t))
   23274                 :          47 :       if (tree max = TYPE_MAX_VALUE (dom))
   23275                 :             :         {
   23276                 :          47 :           if (TREE_CODE (max) == MINUS_EXPR)
   23277                 :          18 :             max = TREE_OPERAND (max, 0);
   23278                 :          47 :           if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
   23279                 :          18 :             unify (tparms, targs, TREE_TYPE (max), size_type_node,
   23280                 :             :                    UNIFY_ALLOW_NONE, /*explain*/false);
   23281                 :             :         }
   23282                 :             : 
   23283                 :             :   /* Keep walking.  */
   23284                 :   107191759 :   return 0;
   23285                 :             : }
   23286                 :             : 
   23287                 :             : /* Try to deduce any not-yet-deduced template type arguments from the type of
   23288                 :             :    an array bound.  This is handled separately from unify because 14.8.2.5 says
   23289                 :             :    "The type of a type parameter is only deduced from an array bound if it is
   23290                 :             :    not otherwise deduced."  */
   23291                 :             : 
   23292                 :             : static void
   23293                 :    18416677 : try_array_deduction (tree tparms, tree targs, tree parm)
   23294                 :             : {
   23295                 :    18416677 :   tree_pair_s data = { tparms, targs };
   23296                 :    18416677 :   hash_set<tree> visited;
   23297                 :    18416677 :   for_each_template_parm (parm, zero_r, &data, &visited,
   23298                 :             :                           /*nondeduced*/false, array_deduction_r);
   23299                 :    18416677 : }
   23300                 :             : 
   23301                 :             : /* Most parms like fn_type_unification.
   23302                 :             : 
   23303                 :             :    If SUBR is 1, we're being called recursively (to unify the
   23304                 :             :    arguments of a function or method parameter of a function
   23305                 :             :    template).
   23306                 :             : 
   23307                 :             :    CHECKS is a pointer to a vector of access checks encountered while
   23308                 :             :    substituting default template arguments.  */
   23309                 :             : 
   23310                 :             : static int
   23311                 :   271272308 : type_unification_real (tree tparms,
   23312                 :             :                        tree full_targs,
   23313                 :             :                        tree xparms,
   23314                 :             :                        const tree *xargs,
   23315                 :             :                        unsigned int xnargs,
   23316                 :             :                        int subr,
   23317                 :             :                        unification_kind_t strict,
   23318                 :             :                        vec<deferred_access_check, va_gc> **checks,
   23319                 :             :                        bool explain_p)
   23320                 :             : {
   23321                 :   271272308 :   tree parm, arg;
   23322                 :   271272308 :   int i;
   23323                 :   271272308 :   int ntparms = TREE_VEC_LENGTH (tparms);
   23324                 :   271272308 :   int saw_undeduced = 0;
   23325                 :   271272308 :   tree parms;
   23326                 :   271272308 :   const tree *args;
   23327                 :   271272308 :   unsigned int nargs;
   23328                 :   271272308 :   unsigned int ia;
   23329                 :             : 
   23330                 :   271272308 :   gcc_assert (TREE_CODE (tparms) == TREE_VEC);
   23331                 :   271272308 :   gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
   23332                 :   271272308 :   gcc_assert (ntparms > 0);
   23333                 :             : 
   23334                 :   271272308 :   tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
   23335                 :             : 
   23336                 :             :   /* Reset the number of non-defaulted template arguments contained
   23337                 :             :      in TARGS.  */
   23338                 :   271272308 :   NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
   23339                 :             : 
   23340                 :   271856757 :  again:
   23341                 :   271856757 :   parms = xparms;
   23342                 :   271856757 :   args = xargs;
   23343                 :   271856757 :   nargs = xnargs;
   23344                 :             : 
   23345                 :             :   /* Only fn_type_unification cares about terminal void.  */
   23346                 :   271856757 :   if (nargs && args[nargs-1] == void_type_node)
   23347                 :    10606137 :     --nargs;
   23348                 :             : 
   23349                 :   271856757 :   ia = 0;
   23350                 :   647983663 :   while (parms && parms != void_list_node
   23351                 :   714369546 :          && ia < nargs)
   23352                 :             :     {
   23353                 :   329211636 :       parm = TREE_VALUE (parms);
   23354                 :             : 
   23355                 :   329211636 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
   23356                 :   329211636 :           && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
   23357                 :             :         /* For a function parameter pack that occurs at the end of the
   23358                 :             :            parameter-declaration-list, the type A of each remaining
   23359                 :             :            argument of the call is compared with the type P of the
   23360                 :             :            declarator-id of the function parameter pack.  */
   23361                 :             :         break;
   23362                 :             : 
   23363                 :   328698318 :       parms = TREE_CHAIN (parms);
   23364                 :             : 
   23365                 :   328698318 :       if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
   23366                 :             :         /* For a function parameter pack that does not occur at the
   23367                 :             :            end of the parameter-declaration-list, the type of the
   23368                 :             :            parameter pack is a non-deduced context.  */
   23369                 :          46 :         continue;
   23370                 :             : 
   23371                 :             :       /* [temp.deduct.conv] only applies to the deduction of the return
   23372                 :             :          type, which is always the first argument here.  Other arguments
   23373                 :             :          (notably, explicit object parameters) should undergo normal
   23374                 :             :          call-like unification.  */
   23375                 :   328698272 :       unification_kind_t kind = strict;
   23376                 :   328698272 :       if (strict == DEDUCE_CONV && ia > 0)
   23377                 :          24 :         kind = DEDUCE_CALL;
   23378                 :             : 
   23379                 :   328698272 :       arg = args[ia];
   23380                 :   328698272 :       ++ia;
   23381                 :             : 
   23382                 :   328698272 :       if (unify_one_argument (tparms, full_targs, parm, arg, subr, kind,
   23383                 :             :                               explain_p))
   23384                 :             :         return 1;
   23385                 :             :     }
   23386                 :             : 
   23387                 :    53892205 :   if (parms
   23388                 :    47428588 :       && parms != void_list_node
   23389                 :    56972910 :       && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
   23390                 :             :     {
   23391                 :     1047406 :       gcc_assert (strict != DEDUCE_CONV);
   23392                 :             : 
   23393                 :             :       /* Unify the remaining arguments with the pack expansion type.  */
   23394                 :     1047406 :       tree argvec;
   23395                 :     1047406 :       tree parmvec = make_tree_vec (1);
   23396                 :             : 
   23397                 :             :       /* Allocate a TREE_VEC and copy in all of the arguments */
   23398                 :     1047406 :       argvec = make_tree_vec (nargs - ia);
   23399                 :     2742128 :       for (i = 0; ia < nargs; ++ia, ++i)
   23400                 :      647316 :         TREE_VEC_ELT (argvec, i) = args[ia];
   23401                 :             : 
   23402                 :             :       /* Copy the parameter into parmvec.  */
   23403                 :     1047406 :       TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
   23404                 :     1047406 :       if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
   23405                 :             :                                 /*subr=*/subr, explain_p))
   23406                 :             :         return 1;
   23407                 :             : 
   23408                 :             :       /* Advance to the end of the list of parameters.  */
   23409                 :     1047263 :       parms = TREE_CHAIN (parms);
   23410                 :             :     }
   23411                 :             : 
   23412                 :             :   /* Fail if we've reached the end of the parm list, and more args
   23413                 :             :      are present, and the parm list isn't variadic.  */
   23414                 :    53892062 :   if (ia < nargs && parms == void_list_node)
   23415                 :       89108 :     return unify_too_many_arguments (explain_p, nargs, ia);
   23416                 :             :   /* Fail if parms are left and they don't have default values and
   23417                 :             :      they aren't all deduced as empty packs (c++/57397).  This is
   23418                 :             :      consistent with sufficient_parms_p.  */
   23419                 :    47339167 :   if (parms && parms != void_list_node
   23420                 :    55836259 :       && TREE_PURPOSE (parms) == NULL_TREE)
   23421                 :             :     {
   23422                 :             :       unsigned int count = nargs;
   23423                 :             :       tree p = parms;
   23424                 :       44685 :       bool type_pack_p;
   23425                 :       44685 :       do
   23426                 :             :         {
   23427                 :       44685 :           type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
   23428                 :       44685 :           if (!type_pack_p)
   23429                 :       44679 :             count++;
   23430                 :       44685 :           p = TREE_CHAIN (p);
   23431                 :             :         }
   23432                 :       44685 :       while (p && p != void_list_node);
   23433                 :       44667 :       if (count != nargs)
   23434                 :       44661 :         return unify_too_few_arguments (explain_p, ia, count,
   23435                 :       44661 :                                         type_pack_p);
   23436                 :             :     }
   23437                 :             : 
   23438                 :    53758293 :   if (!subr)
   23439                 :             :     {
   23440                 :   107003216 :       tsubst_flags_t complain = (explain_p
   23441                 :    53501608 :                                  ? tf_warning_or_error
   23442                 :             :                                  : tf_none);
   23443                 :    53501608 :       bool tried_array_deduction = (cxx_dialect < cxx17);
   23444                 :             : 
   23445                 :   140053845 :       for (i = 0; i < ntparms; i++)
   23446                 :             :         {
   23447                 :    86716795 :           tree targ = TREE_VEC_ELT (targs, i);
   23448                 :    86716795 :           tree tparm = TREE_VEC_ELT (tparms, i);
   23449                 :             : 
   23450                 :             :           /* Clear the "incomplete" flags on all argument packs now so that
   23451                 :             :              substituting them into later default arguments works.  */
   23452                 :    86716795 :           if (targ && ARGUMENT_PACK_P (targ))
   23453                 :             :             {
   23454                 :     7537758 :               ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
   23455                 :     7537758 :               ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
   23456                 :             :             }
   23457                 :             : 
   23458                 :    31509567 :           if (targ || tparm == error_mark_node)
   23459                 :    62744986 :             continue;
   23460                 :    23971809 :           tparm = TREE_VALUE (tparm);
   23461                 :             : 
   23462                 :    23971809 :           if (TREE_CODE (tparm) == TYPE_DECL
   23463                 :    19255326 :               && !tried_array_deduction)
   23464                 :             :             {
   23465                 :    18416655 :               try_array_deduction (tparms, targs, xparms);
   23466                 :    18416655 :               tried_array_deduction = true;
   23467                 :    18416655 :               if (TREE_VEC_ELT (targs, i))
   23468                 :           2 :                 continue;
   23469                 :             :             }
   23470                 :             : 
   23471                 :             :           /* If this is an undeduced nontype parameter that depends on
   23472                 :             :              a type parameter, try another pass; its type may have been
   23473                 :             :              deduced from a later argument than the one from which
   23474                 :             :              this parameter can be deduced.  */
   23475                 :    26114970 :           if (TREE_CODE (tparm) == PARM_DECL
   23476                 :     4716466 :               && !is_auto (TREE_TYPE (tparm))
   23477                 :     4716419 :               && uses_template_parms (TREE_TYPE (tparm))
   23478                 :    26709480 :               && saw_undeduced < 2)
   23479                 :             :             {
   23480                 :     2143163 :               saw_undeduced = 1;
   23481                 :     2143163 :               continue;
   23482                 :             :             }
   23483                 :             : 
   23484                 :             :           /* Core issue #226 (C++0x) [temp.deduct]:
   23485                 :             : 
   23486                 :             :              If a template argument has not been deduced, its
   23487                 :             :              default template argument, if any, is used.
   23488                 :             : 
   23489                 :             :              When we are in C++98 mode, TREE_PURPOSE will either
   23490                 :             :              be NULL_TREE or ERROR_MARK_NODE, so we do not need
   23491                 :             :              to explicitly check cxx_dialect here.  */
   23492                 :    21828644 :           if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
   23493                 :             :             /* OK, there is a default argument.  Wait until after the
   23494                 :             :                conversion check to do substitution.  */
   23495                 :    21553865 :             continue;
   23496                 :             : 
   23497                 :             :           /* If the type parameter is a parameter pack, then it will
   23498                 :             :              be deduced to an empty parameter pack.  */
   23499                 :      274779 :           if (template_parameter_pack_p (tparm))
   23500                 :             :             {
   23501                 :      110221 :               tree arg;
   23502                 :             : 
   23503                 :      110221 :               if (TREE_CODE (tparm) == PARM_DECL)
   23504                 :             :                 {
   23505                 :          91 :                   arg = make_node (NONTYPE_ARGUMENT_PACK);
   23506                 :          91 :                   TREE_CONSTANT (arg) = 1;
   23507                 :             :                 }
   23508                 :             :               else
   23509                 :      110130 :                 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   23510                 :             : 
   23511                 :      110221 :               ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   23512                 :             : 
   23513                 :      110221 :               TREE_VEC_ELT (targs, i) = arg;
   23514                 :      110221 :               continue;
   23515                 :      110221 :             }
   23516                 :             : 
   23517                 :      164558 :           return unify_parameter_deduction_failure (explain_p, tparm);
   23518                 :             :         }
   23519                 :             : 
   23520                 :             :       /* During partial ordering, we deduce dependent template args.  */
   23521                 :             :       bool any_dependent_targs = false;
   23522                 :             : 
   23523                 :             :       /* Now substitute into the default template arguments.  */
   23524                 :   126072653 :       for (i = 0; i < ntparms; i++)
   23525                 :             :         {
   23526                 :    86437896 :           tree targ = TREE_VEC_ELT (targs, i);
   23527                 :    86437896 :           tree tparm = TREE_VEC_ELT (tparms, i);
   23528                 :             : 
   23529                 :    86437896 :           if (targ)
   23530                 :             :             {
   23531                 :    62743614 :               if (!any_dependent_targs && dependent_template_arg_p (targ))
   23532                 :             :                 any_dependent_targs = true;
   23533                 :    62743614 :               continue;
   23534                 :             :             }
   23535                 :    23694282 :           if (tparm == error_mark_node)
   23536                 :           0 :             continue;
   23537                 :             : 
   23538                 :    23694282 :           tree parm = TREE_VALUE (tparm);
   23539                 :    23694282 :           tree arg = TREE_PURPOSE (tparm);
   23540                 :    23694282 :           reopen_deferring_access_checks (*checks);
   23541                 :    23694282 :           location_t save_loc = input_location;
   23542                 :    23694282 :           if (DECL_P (parm))
   23543                 :    23694276 :             input_location = DECL_SOURCE_LOCATION (parm);
   23544                 :             : 
   23545                 :    23694282 :           if (saw_undeduced == 1
   23546                 :     3249409 :               && TREE_CODE (parm) == PARM_DECL
   23547                 :     2289316 :               && !is_auto (TREE_TYPE (parm))
   23548                 :    25983598 :               && uses_template_parms (TREE_TYPE (parm)))
   23549                 :             :             {
   23550                 :             :               /* The type of this non-type parameter depends on undeduced
   23551                 :             :                  parameters.  Don't try to use its default argument yet,
   23552                 :             :                  since we might deduce an argument for it on the next pass,
   23553                 :             :                  but do check whether the arguments we already have cause
   23554                 :             :                  substitution failure, so that that happens before we try
   23555                 :             :                  later default arguments (78489).  */
   23556                 :     2142623 :               ++processing_template_decl;
   23557                 :     2142623 :               tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
   23558                 :             :                                   NULL_TREE);
   23559                 :     2142623 :               --processing_template_decl;
   23560                 :     2142623 :               if (type == error_mark_node)
   23561                 :     1548096 :                 arg = error_mark_node;
   23562                 :             :               else
   23563                 :             :                 arg = NULL_TREE;
   23564                 :             :             }
   23565                 :             :           else
   23566                 :             :             {
   23567                 :             :               /* Even if the call is happening in template context, getting
   23568                 :             :                  here means it's non-dependent, and a default argument is
   23569                 :             :                  considered a separate definition under [temp.decls], so we can
   23570                 :             :                  do this substitution without processing_template_decl.  This
   23571                 :             :                  is important if the default argument contains something that
   23572                 :             :                  might be instantiation-dependent like access (87480).  */
   23573                 :    21551659 :               processing_template_decl_sentinel s (!any_dependent_targs);
   23574                 :    21551659 :               tree substed = NULL_TREE;
   23575                 :    21551659 :               if (saw_undeduced == 1 && !any_dependent_targs)
   23576                 :             :                 {
   23577                 :             :                   /* First instatiate in template context, in case we still
   23578                 :             :                      depend on undeduced template parameters.  */
   23579                 :     1106786 :                   ++processing_template_decl;
   23580                 :     1106786 :                   substed = tsubst_template_arg (arg, full_targs, complain,
   23581                 :             :                                                  NULL_TREE);
   23582                 :     1106786 :                   --processing_template_decl;
   23583                 :     1106786 :                   if (substed != error_mark_node
   23584                 :     1106786 :                       && !uses_template_parms (substed))
   23585                 :             :                     /* We replaced all the tparms, substitute again out of
   23586                 :             :                        template context.  */
   23587                 :             :                     substed = NULL_TREE;
   23588                 :             :                 }
   23589                 :         546 :               if (!substed)
   23590                 :    21551113 :                 substed = tsubst_template_arg (arg, full_targs, complain,
   23591                 :             :                                                NULL_TREE);
   23592                 :             : 
   23593                 :    21551659 :               if (!uses_template_parms (substed))
   23594                 :    21551644 :                 arg = convert_template_argument (parm, substed, full_targs,
   23595                 :             :                                                  complain, i, NULL_TREE);
   23596                 :          15 :               else if (saw_undeduced == 1)
   23597                 :             :                 arg = NULL_TREE;
   23598                 :           9 :               else if (!any_dependent_targs)
   23599                 :           0 :                 arg = error_mark_node;
   23600                 :    21551659 :             }
   23601                 :             : 
   23602                 :    23694282 :           input_location = save_loc;
   23603                 :    23694282 :           *checks = get_deferred_access_checks ();
   23604                 :    23694282 :           pop_deferring_access_checks ();
   23605                 :             : 
   23606                 :    23694282 :           if (arg == error_mark_node)
   23607                 :             :             return 1;
   23608                 :     9991989 :           else if (arg)
   23609                 :             :             {
   23610                 :     9397456 :               TREE_VEC_ELT (targs, i) = arg;
   23611                 :             :               /* The position of the first default template argument,
   23612                 :             :                  is also the number of non-defaulted arguments in TARGS.
   23613                 :             :                  Record that.  */
   23614                 :     9397456 :               if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   23615                 :     8079994 :                 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
   23616                 :             :             }
   23617                 :             :         }
   23618                 :             : 
   23619                 :    39634757 :       if (saw_undeduced++ == 1)
   23620                 :      584449 :         goto again;
   23621                 :             :     }
   23622                 :             : 
   23623                 :    39306993 :   if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
   23624                 :    31329917 :     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
   23625                 :             : 
   23626                 :    39306993 :   return unify_success (explain_p);
   23627                 :             : }
   23628                 :             : 
   23629                 :             : /* Subroutine of type_unification_real.  Args are like the variables
   23630                 :             :    at the call site.  ARG is an overloaded function (or template-id);
   23631                 :             :    we try deducing template args from each of the overloads, and if
   23632                 :             :    only one succeeds, we go with that.  Modifies TARGS and returns
   23633                 :             :    true on success.  */
   23634                 :             : 
   23635                 :             : static bool
   23636                 :       78866 : resolve_overloaded_unification (tree tparms,
   23637                 :             :                                 tree targs,
   23638                 :             :                                 tree parm,
   23639                 :             :                                 tree arg,
   23640                 :             :                                 unification_kind_t strict,
   23641                 :             :                                 int sub_strict,
   23642                 :             :                                 bool explain_p)
   23643                 :             : {
   23644                 :       78866 :   tree tempargs = copy_node (targs);
   23645                 :       78866 :   int good = 0;
   23646                 :       78866 :   tree goodfn = NULL_TREE;
   23647                 :       78866 :   bool addr_p;
   23648                 :             : 
   23649                 :       78866 :   if (TREE_CODE (arg) == ADDR_EXPR)
   23650                 :             :     {
   23651                 :         685 :       arg = TREE_OPERAND (arg, 0);
   23652                 :         685 :       addr_p = true;
   23653                 :             :     }
   23654                 :             :   else
   23655                 :             :     addr_p = false;
   23656                 :             : 
   23657                 :       78866 :   if (TREE_CODE (arg) == COMPONENT_REF)
   23658                 :             :     /* Handle `&x' where `x' is some static or non-static member
   23659                 :             :        function name.  */
   23660                 :         208 :     arg = TREE_OPERAND (arg, 1);
   23661                 :             : 
   23662                 :       78866 :   if (TREE_CODE (arg) == OFFSET_REF)
   23663                 :         413 :     arg = TREE_OPERAND (arg, 1);
   23664                 :             : 
   23665                 :             :   /* Strip baselink information.  */
   23666                 :       78866 :   if (BASELINK_P (arg))
   23667                 :         629 :     arg = BASELINK_FUNCTIONS (arg);
   23668                 :             : 
   23669                 :       78866 :   if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
   23670                 :             :     {
   23671                 :             :       /* If we got some explicit template args, we need to plug them into
   23672                 :             :          the affected templates before we try to unify, in case the
   23673                 :             :          explicit args will completely resolve the templates in question.  */
   23674                 :             : 
   23675                 :         391 :       int ok = 0;
   23676                 :         391 :       tree expl_subargs = TREE_OPERAND (arg, 1);
   23677                 :         391 :       arg = TREE_OPERAND (arg, 0);
   23678                 :             : 
   23679                 :         838 :       for (lkp_iterator iter (arg); iter; ++iter)
   23680                 :             :         {
   23681                 :         447 :           tree fn = *iter;
   23682                 :         447 :           tree subargs, elem;
   23683                 :             : 
   23684                 :         447 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   23685                 :           0 :             continue;
   23686                 :             : 
   23687                 :         447 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   23688                 :             :                                            expl_subargs, NULL_TREE, tf_none);
   23689                 :         447 :           if (subargs != error_mark_node
   23690                 :         447 :               && !any_dependent_template_arguments_p (subargs))
   23691                 :             :             {
   23692                 :         417 :               fn = instantiate_template (fn, subargs, tf_none);
   23693                 :         417 :               if (!constraints_satisfied_p (fn))
   23694                 :          26 :                 continue;
   23695                 :         391 :               if (undeduced_auto_decl (fn))
   23696                 :             :                 {
   23697                 :             :                   /* Instantiate the function to deduce its return type.  */
   23698                 :           6 :                   ++function_depth;
   23699                 :           6 :                   instantiate_decl (fn, /*defer*/false, /*class*/false);
   23700                 :           6 :                   --function_depth;
   23701                 :             :                 }
   23702                 :             : 
   23703                 :         391 :               if (flag_noexcept_type)
   23704                 :         328 :                 maybe_instantiate_noexcept (fn, tf_none);
   23705                 :             : 
   23706                 :         391 :               elem = TREE_TYPE (fn);
   23707                 :         391 :               if (try_one_overload (tparms, targs, tempargs, parm,
   23708                 :             :                                     elem, strict, sub_strict, addr_p, explain_p)
   23709                 :         391 :                   && (!goodfn || !same_type_p (goodfn, elem)))
   23710                 :             :                 {
   23711                 :         363 :                   goodfn = elem;
   23712                 :         363 :                   ++good;
   23713                 :             :                 }
   23714                 :             :             }
   23715                 :          30 :           else if (subargs)
   23716                 :          30 :             ++ok;
   23717                 :             :         }
   23718                 :             :       /* If no templates (or more than one) are fully resolved by the
   23719                 :             :          explicit arguments, this template-id is a non-deduced context; it
   23720                 :             :          could still be OK if we deduce all template arguments for the
   23721                 :             :          enclosing call through other arguments.  */
   23722                 :         391 :       if (good != 1)
   23723                 :             :         good = ok;
   23724                 :             :     }
   23725                 :       78475 :   else if (!OVL_P (arg))
   23726                 :             :     /* If ARG is, for example, "(0, &f)" then its type will be unknown
   23727                 :             :        -- but the deduction does not succeed because the expression is
   23728                 :             :        not just the function on its own.  */
   23729                 :             :     return false;
   23730                 :             :   else
   23731                 :      157455 :     for (lkp_iterator iter (arg); iter; ++iter)
   23732                 :             :       {
   23733                 :       78980 :         tree fn = *iter;
   23734                 :       78980 :         if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
   23735                 :             :                               strict, sub_strict, addr_p, explain_p)
   23736                 :       78980 :             && (!goodfn || !decls_match (goodfn, fn)))
   23737                 :             :           {
   23738                 :       78696 :             goodfn = fn;
   23739                 :       78696 :             ++good;
   23740                 :             :           }
   23741                 :             :       }
   23742                 :             : 
   23743                 :             :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   23744                 :             :      to function or pointer to member function argument if the set of
   23745                 :             :      overloaded functions does not contain function templates and at most
   23746                 :             :      one of a set of overloaded functions provides a unique match.
   23747                 :             : 
   23748                 :             :      So if we found multiple possibilities, we return success but don't
   23749                 :             :      deduce anything.  */
   23750                 :             : 
   23751                 :       78535 :   if (good == 1)
   23752                 :             :     {
   23753                 :       78561 :       int i = TREE_VEC_LENGTH (targs);
   23754                 :      307288 :       for (; i--; )
   23755                 :      228727 :         if (TREE_VEC_ELT (tempargs, i))
   23756                 :             :           {
   23757                 :      128003 :             tree old = TREE_VEC_ELT (targs, i);
   23758                 :      128003 :             tree new_ = TREE_VEC_ELT (tempargs, i);
   23759                 :      127320 :             if (new_ && old && ARGUMENT_PACK_P (old)
   23760                 :      128012 :                 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
   23761                 :             :               /* Don't forget explicit template arguments in a pack.  */
   23762                 :           6 :               ARGUMENT_PACK_EXPLICIT_ARGS (new_)
   23763                 :          12 :                 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
   23764                 :      128003 :             TREE_VEC_ELT (targs, i) = new_;
   23765                 :             :           }
   23766                 :             :     }
   23767                 :       78866 :   if (good)
   23768                 :             :     return true;
   23769                 :             : 
   23770                 :             :   return false;
   23771                 :             : }
   23772                 :             : 
   23773                 :             : /* Core DR 115: In contexts where deduction is done and fails, or in
   23774                 :             :    contexts where deduction is not done, if a template argument list is
   23775                 :             :    specified and it, along with any default template arguments, identifies
   23776                 :             :    a single function template specialization, then the template-id is an
   23777                 :             :    lvalue for the function template specialization.  */
   23778                 :             : 
   23779                 :             : tree
   23780                 :  1143697305 : resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
   23781                 :             : {
   23782                 :  1143697305 :   tree expr, offset, baselink;
   23783                 :  1143697305 :   bool addr;
   23784                 :             : 
   23785                 :  1143697305 :   if (!type_unknown_p (orig_expr))
   23786                 :             :     return orig_expr;
   23787                 :             : 
   23788                 :        7848 :   expr = orig_expr;
   23789                 :        7848 :   addr = false;
   23790                 :        7848 :   offset = NULL_TREE;
   23791                 :        7848 :   baselink = NULL_TREE;
   23792                 :             : 
   23793                 :        7848 :   if (TREE_CODE (expr) == ADDR_EXPR)
   23794                 :             :     {
   23795                 :         275 :       expr = TREE_OPERAND (expr, 0);
   23796                 :         275 :       addr = true;
   23797                 :             :     }
   23798                 :        7848 :   if (TREE_CODE (expr) == OFFSET_REF)
   23799                 :             :     {
   23800                 :          66 :       offset = expr;
   23801                 :          66 :       expr = TREE_OPERAND (expr, 1);
   23802                 :             :     }
   23803                 :        7848 :   if (BASELINK_P (expr))
   23804                 :             :     {
   23805                 :        2621 :       baselink = expr;
   23806                 :        2621 :       expr = BASELINK_FUNCTIONS (expr);
   23807                 :             :     }
   23808                 :             : 
   23809                 :        7848 :   if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
   23810                 :             :     {
   23811                 :        3234 :       int good = 0;
   23812                 :        3234 :       tree goodfn = NULL_TREE;
   23813                 :             : 
   23814                 :             :       /* If we got some explicit template args, we need to plug them into
   23815                 :             :          the affected templates before we try to unify, in case the
   23816                 :             :          explicit args will completely resolve the templates in question.  */
   23817                 :             : 
   23818                 :        3234 :       tree expl_subargs = TREE_OPERAND (expr, 1);
   23819                 :        3234 :       tree arg = TREE_OPERAND (expr, 0);
   23820                 :        3234 :       tree badfn = NULL_TREE;
   23821                 :        3234 :       tree badargs = NULL_TREE;
   23822                 :             : 
   23823                 :        6559 :       for (lkp_iterator iter (arg); iter; ++iter)
   23824                 :             :         {
   23825                 :        3325 :           tree fn = *iter;
   23826                 :        3325 :           tree subargs, elem;
   23827                 :             : 
   23828                 :        3325 :           if (TREE_CODE (fn) != TEMPLATE_DECL)
   23829                 :           0 :             continue;
   23830                 :             : 
   23831                 :        3325 :           subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
   23832                 :             :                                            expl_subargs, NULL_TREE, tf_none);
   23833                 :        3325 :           if (subargs != error_mark_node
   23834                 :        3325 :               && !any_dependent_template_arguments_p (subargs))
   23835                 :             :             {
   23836                 :        1232 :               elem = instantiate_template (fn, subargs, tf_none);
   23837                 :        1232 :               if (elem == error_mark_node)
   23838                 :             :                 {
   23839                 :             :                   badfn = fn;
   23840                 :             :                   badargs = subargs;
   23841                 :             :                 }
   23842                 :        1192 :               else if (elem && (!goodfn || !decls_match (goodfn, elem))
   23843                 :        2384 :                        && constraints_satisfied_p (elem))
   23844                 :             :                 {
   23845                 :        1166 :                   goodfn = elem;
   23846                 :        1166 :                   ++good;
   23847                 :             :                 }
   23848                 :             :             }
   23849                 :             :         }
   23850                 :        3234 :       if (good == 1)
   23851                 :             :         {
   23852                 :        1112 :           mark_used (goodfn);
   23853                 :        1112 :           expr = goodfn;
   23854                 :        1112 :           if (baselink)
   23855                 :         596 :             expr = build_baselink (BASELINK_BINFO (baselink),
   23856                 :         596 :                                    BASELINK_ACCESS_BINFO (baselink),
   23857                 :         596 :                                    expr, BASELINK_OPTYPE (baselink));
   23858                 :        1112 :           if (offset)
   23859                 :             :             {
   23860                 :          43 :               tree base
   23861                 :          43 :                 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
   23862                 :          43 :               expr = build_offset_ref (base, expr, addr, complain);
   23863                 :             :             }
   23864                 :        1112 :           if (addr)
   23865                 :         186 :             expr = cp_build_addr_expr (expr, complain);
   23866                 :        1112 :           return expr;
   23867                 :             :         }
   23868                 :        2122 :       else if (good == 0 && badargs && (complain & tf_error))
   23869                 :             :         /* There were no good options and at least one bad one, so let the
   23870                 :             :            user know what the problem is.  */
   23871                 :           4 :         instantiate_template (badfn, badargs, complain);
   23872                 :             :     }
   23873                 :             :   return orig_expr;
   23874                 :             : }
   23875                 :             : 
   23876                 :             : /* As above, but error out if the expression remains overloaded.  */
   23877                 :             : 
   23878                 :             : tree
   23879                 :   827735126 : resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
   23880                 :             : {
   23881                 :   827735126 :   exp = resolve_nondeduced_context (exp, complain);
   23882                 :   827735126 :   if (type_unknown_p (exp))
   23883                 :             :     {
   23884                 :         117 :       if (complain & tf_error)
   23885                 :         117 :         cxx_incomplete_type_error (exp, TREE_TYPE (exp));
   23886                 :         117 :       return error_mark_node;
   23887                 :             :     }
   23888                 :             :   return exp;
   23889                 :             : }
   23890                 :             : 
   23891                 :             : /* Subroutine of resolve_overloaded_unification; does deduction for a single
   23892                 :             :    overload.  Fills TARGS with any deduced arguments, or error_mark_node if
   23893                 :             :    different overloads deduce different arguments for a given parm.
   23894                 :             :    ADDR_P is true if the expression for which deduction is being
   23895                 :             :    performed was of the form "& fn" rather than simply "fn".
   23896                 :             : 
   23897                 :             :    Returns 1 on success.  */
   23898                 :             : 
   23899                 :             : static int
   23900                 :       79371 : try_one_overload (tree tparms,
   23901                 :             :                   tree orig_targs,
   23902                 :             :                   tree targs,
   23903                 :             :                   tree parm,
   23904                 :             :                   tree arg,
   23905                 :             :                   unification_kind_t strict,
   23906                 :             :                   int sub_strict,
   23907                 :             :                   bool addr_p,
   23908                 :             :                   bool explain_p)
   23909                 :             : {
   23910                 :       79371 :   int nargs;
   23911                 :       79371 :   tree tempargs;
   23912                 :       79371 :   int i;
   23913                 :             : 
   23914                 :       79371 :   if (arg == error_mark_node)
   23915                 :             :     return 0;
   23916                 :             : 
   23917                 :             :   /* [temp.deduct.type] A template-argument can be deduced from a pointer
   23918                 :             :      to function or pointer to member function argument if the set of
   23919                 :             :      overloaded functions does not contain function templates and at most
   23920                 :             :      one of a set of overloaded functions provides a unique match.
   23921                 :             : 
   23922                 :             :      So if this is a template, just return success.  */
   23923                 :             : 
   23924                 :       79368 :   if (uses_template_parms (arg))
   23925                 :             :     return 1;
   23926                 :             : 
   23927                 :        1357 :   if (TREE_CODE (arg) == METHOD_TYPE)
   23928                 :         440 :     arg = build_ptrmemfunc_type (build_pointer_type (arg));
   23929                 :         917 :   else if (addr_p)
   23930                 :         673 :     arg = build_pointer_type (arg);
   23931                 :             : 
   23932                 :        1357 :   sub_strict |= maybe_adjust_types_for_deduction (tparms, strict,
   23933                 :             :                                                   &parm, &arg, NULL_TREE);
   23934                 :             : 
   23935                 :             :   /* We don't copy orig_targs for this because if we have already deduced
   23936                 :             :      some template args from previous args, unify would complain when we
   23937                 :             :      try to deduce a template parameter for the same argument, even though
   23938                 :             :      there isn't really a conflict.  */
   23939                 :        1357 :   nargs = TREE_VEC_LENGTH (targs);
   23940                 :        1357 :   tempargs = make_tree_vec (nargs);
   23941                 :             : 
   23942                 :        1357 :   if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
   23943                 :             :     return 0;
   23944                 :             : 
   23945                 :             :   /* First make sure we didn't deduce anything that conflicts with
   23946                 :             :      explicitly specified args.  */
   23947                 :        2855 :   for (i = nargs; i--; )
   23948                 :             :     {
   23949                 :        1795 :       tree elt = TREE_VEC_ELT (tempargs, i);
   23950                 :        1795 :       tree oldelt = TREE_VEC_ELT (orig_targs, i);
   23951                 :             : 
   23952                 :        1795 :       if (!elt)
   23953                 :             :         /*NOP*/;
   23954                 :        1251 :       else if (uses_template_parms (elt))
   23955                 :             :         /* Since we're unifying against ourselves, we will fill in
   23956                 :             :            template args used in the function parm list with our own
   23957                 :             :            template parms.  Discard them.  */
   23958                 :           0 :         TREE_VEC_ELT (tempargs, i) = NULL_TREE;
   23959                 :        1251 :       else if (oldelt && ARGUMENT_PACK_P (oldelt))
   23960                 :             :         {
   23961                 :             :           /* Check that the argument at each index of the deduced argument pack
   23962                 :             :              is equivalent to the corresponding explicitly specified argument.
   23963                 :             :              We may have deduced more arguments than were explicitly specified,
   23964                 :             :              and that's OK.  */
   23965                 :             : 
   23966                 :             :           /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
   23967                 :             :              that's wrong if we deduce the same argument pack from multiple
   23968                 :             :              function arguments: it's only incomplete the first time.  */
   23969                 :             : 
   23970                 :          24 :           tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
   23971                 :          24 :           tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
   23972                 :             : 
   23973                 :          24 :           if (TREE_VEC_LENGTH (deduced_pack)
   23974                 :          24 :               < TREE_VEC_LENGTH (explicit_pack))
   23975                 :             :             return 0;
   23976                 :             : 
   23977                 :          30 :           for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
   23978                 :          21 :             if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
   23979                 :          21 :                                       TREE_VEC_ELT (deduced_pack, j)))
   23980                 :             :               return 0;
   23981                 :             :         }
   23982                 :          24 :       else if (oldelt && !template_args_equal (oldelt, elt))
   23983                 :             :         return 0;
   23984                 :             :     }
   23985                 :             : 
   23986                 :        2802 :   for (i = nargs; i--; )
   23987                 :             :     {
   23988                 :        1742 :       tree elt = TREE_VEC_ELT (tempargs, i);
   23989                 :             : 
   23990                 :        1742 :       if (elt)
   23991                 :        1216 :         TREE_VEC_ELT (targs, i) = elt;
   23992                 :             :     }
   23993                 :             : 
   23994                 :             :   return 1;
   23995                 :             : }
   23996                 :             : 
   23997                 :             : /* PARM is a template class (perhaps with unbound template
   23998                 :             :    parameters).  ARG is a fully instantiated type.  If ARG can be
   23999                 :             :    bound to PARM, return ARG, otherwise return NULL_TREE.  TPARMS and
   24000                 :             :    TARGS are as for unify.  */
   24001                 :             : 
   24002                 :             : static tree
   24003                 :    94645623 : try_class_unification (tree tparms, tree targs, tree parm, tree arg,
   24004                 :             :                        bool explain_p)
   24005                 :             : {
   24006                 :    94645623 :   if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   24007                 :             :     return NULL_TREE;
   24008                 :    71387338 :   else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24009                 :             :     /* Matches anything.  */;
   24010                 :    71386637 :   else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
   24011                 :             :     return NULL_TREE;
   24012                 :             : 
   24013                 :             :   /* We need to make a new template argument vector for the call to
   24014                 :             :      unify.  If we used TARGS, we'd clutter it up with the result of
   24015                 :             :      the attempted unification, even if this class didn't work out.
   24016                 :             :      We also don't want to commit ourselves to all the unifications
   24017                 :             :      we've already done, since unification is supposed to be done on
   24018                 :             :      an argument-by-argument basis.  In other words, consider the
   24019                 :             :      following pathological case:
   24020                 :             : 
   24021                 :             :        template <int I, int J, int K>
   24022                 :             :        struct S {};
   24023                 :             : 
   24024                 :             :        template <int I, int J>
   24025                 :             :        struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
   24026                 :             : 
   24027                 :             :        template <int I, int J, int K>
   24028                 :             :        void f(S<I, J, K>, S<I, I, I>);
   24029                 :             : 
   24030                 :             :        void g() {
   24031                 :             :          S<0, 0, 0> s0;
   24032                 :             :          S<0, 1, 2> s2;
   24033                 :             : 
   24034                 :             :          f(s0, s2);
   24035                 :             :        }
   24036                 :             : 
   24037                 :             :      Now, by the time we consider the unification involving `s2', we
   24038                 :             :      already know that we must have `f<0, 0, 0>'.  But, even though
   24039                 :             :      `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
   24040                 :             :      because there are two ways to unify base classes of S<0, 1, 2>
   24041                 :             :      with S<I, I, I>.  If we kept the already deduced knowledge, we
   24042                 :             :      would reject the possibility I=1.  */
   24043                 :     9794497 :   targs = copy_template_args (targs);
   24044                 :    34563977 :   for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
   24045                 :    24769480 :     targ = NULL_TREE;
   24046                 :             : 
   24047                 :     9794497 :   int err;
   24048                 :     9794497 :   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24049                 :         701 :     err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
   24050                 :             :   else
   24051                 :     9793796 :     err = unify (tparms, targs,
   24052                 :     9793796 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   24053                 :     9793796 :                  INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)),
   24054                 :             :                  UNIFY_ALLOW_NONE, explain_p);
   24055                 :             : 
   24056                 :     9794497 :   return err ? NULL_TREE : arg;
   24057                 :             : }
   24058                 :             : 
   24059                 :             : /* Given a template type PARM and a class type ARG, find the unique
   24060                 :             :    base type in ARG that is an instance of PARM.  We do not examine
   24061                 :             :    ARG itself; only its base-classes.  If there is not exactly one
   24062                 :             :    appropriate base class, return NULL_TREE.  PARM may be the type of
   24063                 :             :    a partial specialization, as well as a plain template type.  Used
   24064                 :             :    by unify.  */
   24065                 :             : 
   24066                 :             : static enum template_base_result
   24067                 :    68509581 : get_template_base (tree tparms, tree targs, tree parm, tree arg,
   24068                 :             :                    bool explain_p, tree *result)
   24069                 :             : {
   24070                 :    68509581 :   tree rval = NULL_TREE;
   24071                 :    68509581 :   tree binfo;
   24072                 :             : 
   24073                 :    68509581 :   gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
   24074                 :             : 
   24075                 :    68509581 :   binfo = TYPE_BINFO (complete_type (arg));
   24076                 :    68509581 :   if (!binfo)
   24077                 :             :     {
   24078                 :             :       /* The type could not be completed.  */
   24079                 :           8 :       *result = NULL_TREE;
   24080                 :           8 :       return tbr_incomplete_type;
   24081                 :             :     }
   24082                 :             : 
   24083                 :             :   /* Walk in inheritance graph order.  The search order is not
   24084                 :             :      important, and this avoids multiple walks of virtual bases.  */
   24085                 :    85812602 :   for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
   24086                 :             :     {
   24087                 :    34606094 :       tree r = try_class_unification (tparms, targs, parm,
   24088                 :    17303047 :                                       BINFO_TYPE (binfo), explain_p);
   24089                 :             : 
   24090                 :    17303047 :       if (r)
   24091                 :             :         {
   24092                 :             :           /* If there is more than one satisfactory baseclass, then:
   24093                 :             : 
   24094                 :             :                [temp.deduct.call]
   24095                 :             : 
   24096                 :             :               If they yield more than one possible deduced A, the type
   24097                 :             :               deduction fails.
   24098                 :             : 
   24099                 :             :              applies.  */
   24100                 :      666210 :           if (rval && !same_type_p (r, rval))
   24101                 :             :             {
   24102                 :             :               /* [temp.deduct.call]/4.3: If there is a class C that is a
   24103                 :             :                  (direct or indirect) base class of D and derived (directly or
   24104                 :             :                  indirectly) from a class B and that would be a valid deduced
   24105                 :             :                  A, the deduced A cannot be B or pointer to B, respectively. */
   24106                 :          48 :               if (DERIVED_FROM_P (r, rval))
   24107                 :             :                 /* Ignore r.  */
   24108                 :          27 :                 continue;
   24109                 :          21 :               else if (DERIVED_FROM_P (rval, r))
   24110                 :             :                 /* Ignore rval.  */;
   24111                 :             :               else
   24112                 :             :                 {
   24113                 :          18 :                   *result = NULL_TREE;
   24114                 :          18 :                   return tbr_ambiguous_baseclass;
   24115                 :             :                 }
   24116                 :             :             }
   24117                 :             : 
   24118                 :             :           rval = r;
   24119                 :             :         }
   24120                 :             :     }
   24121                 :             : 
   24122                 :    68509555 :   *result = rval;
   24123                 :    68509555 :   return tbr_success;
   24124                 :             : }
   24125                 :             : 
   24126                 :             : /* Returns the level of DECL, which declares a template parameter.  */
   24127                 :             : 
   24128                 :             : static int
   24129                 :   177015110 : template_decl_level (tree decl)
   24130                 :             : {
   24131                 :   177015110 :   switch (TREE_CODE (decl))
   24132                 :             :     {
   24133                 :   165309514 :     case TYPE_DECL:
   24134                 :   165309514 :     case TEMPLATE_DECL:
   24135                 :   165309514 :       return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
   24136                 :             : 
   24137                 :    11705596 :     case PARM_DECL:
   24138                 :    11705596 :       return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
   24139                 :             : 
   24140                 :           0 :     default:
   24141                 :           0 :       gcc_unreachable ();
   24142                 :             :     }
   24143                 :             :   return 0;
   24144                 :             : }
   24145                 :             : 
   24146                 :             : /* Decide whether ARG can be unified with PARM, considering only the
   24147                 :             :    cv-qualifiers of each type, given STRICT as documented for unify.
   24148                 :             :    Returns nonzero iff the unification is OK on that basis.  */
   24149                 :             : 
   24150                 :             : static int
   24151                 :   311627006 : check_cv_quals_for_unify (int strict, tree arg, tree parm)
   24152                 :             : {
   24153                 :   311627006 :   int arg_quals = cp_type_quals (arg);
   24154                 :   311627006 :   int parm_quals = cp_type_quals (parm);
   24155                 :             : 
   24156                 :   311627006 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   24157                 :   169613746 :       && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   24158                 :             :     {
   24159                 :             :       /*  Although a CVR qualifier is ignored when being applied to a
   24160                 :             :           substituted template parameter ([8.3.2]/1 for example), that
   24161                 :             :           does not allow us to unify "const T" with "int&" because both
   24162                 :             :           types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
   24163                 :             :           It is ok when we're allowing additional CV qualifiers
   24164                 :             :           at the outer level [14.8.2.1]/3,1st bullet.  */
   24165                 :   147123621 :       if ((TYPE_REF_P (arg)
   24166                 :   142888011 :            || FUNC_OR_METHOD_TYPE_P (arg))
   24167                 :     4443507 :           && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
   24168                 :             :         return 0;
   24169                 :             : 
   24170                 :   147123254 :       if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
   24171                 :   102985897 :           && (parm_quals & TYPE_QUAL_RESTRICT))
   24172                 :             :         return 0;
   24173                 :             :     }
   24174                 :             : 
   24175                 :   311626639 :   if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
   24176                 :   183168785 :       && (arg_quals & parm_quals) != parm_quals)
   24177                 :             :     return 0;
   24178                 :             : 
   24179                 :   310197066 :   if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
   24180                 :   141965234 :       && (parm_quals & arg_quals) != arg_quals)
   24181                 :      824359 :     return 0;
   24182                 :             : 
   24183                 :             :   return 1;
   24184                 :             : }
   24185                 :             : 
   24186                 :             : /* Determines the LEVEL and INDEX for the template parameter PARM.  */
   24187                 :             : void
   24188                 :  4916475874 : template_parm_level_and_index (tree parm, int* level, int* index)
   24189                 :             : {
   24190                 :  4916475874 :   if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   24191                 :  4916475874 :       || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   24192                 :   213539005 :       || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24193                 :             :     {
   24194                 :  4703312069 :       *index = TEMPLATE_TYPE_IDX (parm);
   24195                 :  4703312069 :       *level = TEMPLATE_TYPE_LEVEL (parm);
   24196                 :             :     }
   24197                 :             :   else
   24198                 :             :     {
   24199                 :   213163805 :       *index = TEMPLATE_PARM_IDX (parm);
   24200                 :   213163805 :       *level = TEMPLATE_PARM_LEVEL (parm);
   24201                 :             :     }
   24202                 :  4916475874 : }
   24203                 :             : 
   24204                 :             : #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP)                    \
   24205                 :             :   do {                                                                  \
   24206                 :             :     if (unify (TP, TA, P, A, S, EP))                                    \
   24207                 :             :       return 1;                                                         \
   24208                 :             :   } while (0)
   24209                 :             : 
   24210                 :             : /* Unifies the remaining arguments in PACKED_ARGS with the pack
   24211                 :             :    expansion at the end of PACKED_PARMS. Returns 0 if the type
   24212                 :             :    deduction succeeds, 1 otherwise. STRICT is the same as in
   24213                 :             :    fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
   24214                 :             :    function call argument list. We'll need to adjust the arguments to make them
   24215                 :             :    types. SUBR tells us if this is from a recursive call to
   24216                 :             :    type_unification_real, or for comparing two template argument
   24217                 :             :    lists. */
   24218                 :             : 
   24219                 :             : static int
   24220                 :     5099702 : unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
   24221                 :             :                       tree packed_args, unification_kind_t strict,
   24222                 :             :                       bool subr, bool explain_p)
   24223                 :             : {
   24224                 :     5099702 :   tree parm
   24225                 :     5099702 :     = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
   24226                 :     5099702 :   tree pattern = PACK_EXPANSION_PATTERN (parm);
   24227                 :     5099702 :   tree pack, packs = NULL_TREE;
   24228                 :     5099702 :   int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
   24229                 :             : 
   24230                 :             :   /* Add in any args remembered from an earlier partial instantiation.  */
   24231                 :     5099702 :   targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
   24232                 :    10199404 :   int levels = TMPL_ARGS_DEPTH (targs);
   24233                 :             : 
   24234                 :     5099702 :   packed_args = expand_template_argument_pack (packed_args);
   24235                 :             : 
   24236                 :     5099702 :   int len = TREE_VEC_LENGTH (packed_args);
   24237                 :             : 
   24238                 :             :   /* Determine the parameter packs we will be deducing from the
   24239                 :             :      pattern, and record their current deductions.  */
   24240                 :    10068382 :   for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
   24241                 :    10199425 :        pack; pack = TREE_CHAIN (pack))
   24242                 :             :     {
   24243                 :     5099723 :       tree parm_pack = TREE_VALUE (pack);
   24244                 :     5099723 :       int idx, level;
   24245                 :             : 
   24246                 :             :       /* Only template parameter packs can be deduced, not e.g. function
   24247                 :             :          parameter packs or __bases or __integer_pack.  */
   24248                 :     5099723 :       if (!TEMPLATE_PARM_P (parm_pack))
   24249                 :      265914 :         continue;
   24250                 :             : 
   24251                 :             :       /* Determine the index and level of this parameter pack.  */
   24252                 :     5099717 :       template_parm_level_and_index (parm_pack, &level, &idx);
   24253                 :     5099717 :       if (level > levels)
   24254                 :      265908 :         continue;
   24255                 :             : 
   24256                 :             :       /* Keep track of the parameter packs and their corresponding
   24257                 :             :          argument packs.  */
   24258                 :     9667618 :       packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
   24259                 :     4833809 :       TREE_TYPE (packs) = make_tree_vec (len - start);
   24260                 :             :     }
   24261                 :             : 
   24262                 :             :   /* Loop through all of the arguments that have not yet been
   24263                 :             :      unified and unify each with the pattern.  */
   24264                 :    11690495 :   for (i = start; i < len; i++)
   24265                 :             :     {
   24266                 :     6591227 :       tree parm;
   24267                 :     6591227 :       bool any_explicit = false;
   24268                 :     6591227 :       tree arg = TREE_VEC_ELT (packed_args, i);
   24269                 :             : 
   24270                 :             :       /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
   24271                 :             :          or the element of its argument pack at the current index if
   24272                 :             :          this argument was explicitly specified.  */
   24273                 :    12916579 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   24274                 :             :         {
   24275                 :     6325352 :           int idx, level;
   24276                 :     6325352 :           tree arg, pargs;
   24277                 :     6325352 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   24278                 :             : 
   24279                 :     6325352 :           arg = NULL_TREE;
   24280                 :     6325352 :           if (TREE_VALUE (pack)
   24281                 :      119545 :               && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
   24282                 :     6325907 :               && (i - start < TREE_VEC_LENGTH (pargs)))
   24283                 :             :             {
   24284                 :         492 :               any_explicit = true;
   24285                 :         492 :               arg = TREE_VEC_ELT (pargs, i - start);
   24286                 :             :             }
   24287                 :    12650704 :           TMPL_ARG (targs, level, idx) = arg;
   24288                 :             :         }
   24289                 :             : 
   24290                 :             :       /* If we had explicit template arguments, substitute them into the
   24291                 :             :          pattern before deduction.  */
   24292                 :     6591227 :       if (any_explicit)
   24293                 :             :         {
   24294                 :             :           /* Some arguments might still be unspecified or dependent.  */
   24295                 :         492 :           bool dependent;
   24296                 :         492 :           ++processing_template_decl;
   24297                 :         492 :           dependent = any_dependent_template_arguments_p (targs);
   24298                 :         492 :           if (!dependent)
   24299                 :         477 :             --processing_template_decl;
   24300                 :         984 :           parm = tsubst (pattern, targs,
   24301                 :             :                          explain_p ? tf_warning_or_error : tf_none,
   24302                 :             :                          NULL_TREE);
   24303                 :         492 :           if (dependent)
   24304                 :          15 :             --processing_template_decl;
   24305                 :         492 :           if (parm == error_mark_node)
   24306                 :             :             return 1;
   24307                 :             :         }
   24308                 :             :       else
   24309                 :             :         parm = pattern;
   24310                 :             : 
   24311                 :             :       /* Unify the pattern with the current argument.  */
   24312                 :     6591227 :       if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
   24313                 :             :                               explain_p))
   24314                 :             :         return 1;
   24315                 :             : 
   24316                 :             :       /* For each parameter pack, collect the deduced value.  */
   24317                 :    12915708 :       for (pack = packs; pack; pack = TREE_CHAIN (pack))
   24318                 :             :         {
   24319                 :     6324915 :           int idx, level;
   24320                 :     6324915 :           template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   24321                 :             : 
   24322                 :     6324915 :           TREE_VEC_ELT (TREE_TYPE (pack), i - start) = 
   24323                 :    12649830 :             TMPL_ARG (targs, level, idx);
   24324                 :             :         }
   24325                 :             :     }
   24326                 :             : 
   24327                 :             :   /* Verify that the results of unification with the parameter packs
   24328                 :             :      produce results consistent with what we've seen before, and make
   24329                 :             :      the deduced argument packs available.  */
   24330                 :     9932628 :   for (pack = packs; pack; pack = TREE_CHAIN (pack))
   24331                 :             :     {
   24332                 :     4833372 :       tree old_pack = TREE_VALUE (pack);
   24333                 :     4833372 :       tree new_args = TREE_TYPE (pack);
   24334                 :     4833372 :       int i, len = TREE_VEC_LENGTH (new_args);
   24335                 :     4833372 :       int idx, level;
   24336                 :     4833372 :       bool nondeduced_p = false;
   24337                 :             : 
   24338                 :             :       /* By default keep the original deduced argument pack.
   24339                 :             :          If necessary, more specific code is going to update the
   24340                 :             :          resulting deduced argument later down in this function.  */
   24341                 :     4833372 :       template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
   24342                 :     9666744 :       TMPL_ARG (targs, level, idx) = old_pack;
   24343                 :             : 
   24344                 :             :       /* If NEW_ARGS contains any NULL_TREE entries, we didn't
   24345                 :             :          actually deduce anything.  */
   24346                 :    11157891 :       for (i = 0; i < len && !nondeduced_p; ++i)
   24347                 :     6324519 :         if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
   24348                 :         204 :           nondeduced_p = true;
   24349                 :     4833372 :       if (nondeduced_p)
   24350                 :         204 :         continue;
   24351                 :             : 
   24352                 :     4833168 :       if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
   24353                 :             :         {
   24354                 :             :           /* If we had fewer function args than explicit template args,
   24355                 :             :              just use the explicits.  */
   24356                 :         216 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   24357                 :         216 :           int explicit_len = TREE_VEC_LENGTH (explicit_args);
   24358                 :         216 :           if (len < explicit_len)
   24359                 :     4833168 :             new_args = explicit_args;
   24360                 :             :         }
   24361                 :             : 
   24362                 :     4833168 :       if (!old_pack)
   24363                 :             :         {
   24364                 :     4642958 :           tree result;
   24365                 :             :           /* Build the deduced *_ARGUMENT_PACK.  */
   24366                 :     4642958 :           if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
   24367                 :             :             {
   24368                 :      131006 :               result = make_node (NONTYPE_ARGUMENT_PACK);
   24369                 :      131006 :               TREE_CONSTANT (result) = 1;
   24370                 :             :             }
   24371                 :             :           else
   24372                 :     4511952 :             result = cxx_make_type (TYPE_ARGUMENT_PACK);
   24373                 :             : 
   24374                 :     4642958 :           ARGUMENT_PACK_ARGS (result) = new_args;
   24375                 :             : 
   24376                 :             :           /* Note the deduced argument packs for this parameter
   24377                 :             :              pack.  */
   24378                 :     9285916 :           TMPL_ARG (targs, level, idx) = result;
   24379                 :             :         }
   24380                 :      190210 :       else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
   24381                 :      190210 :                && (ARGUMENT_PACK_ARGS (old_pack) 
   24382                 :         216 :                    == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
   24383                 :             :         {
   24384                 :             :           /* We only had the explicitly-provided arguments before, but
   24385                 :             :              now we have a complete set of arguments.  */
   24386                 :         216 :           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
   24387                 :             : 
   24388                 :         216 :           ARGUMENT_PACK_ARGS (old_pack) = new_args;
   24389                 :         216 :           ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
   24390                 :         216 :           ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
   24391                 :             :         }
   24392                 :             :       else
   24393                 :             :         {
   24394                 :      189994 :           tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
   24395                 :      189994 :           tree old_args = ARGUMENT_PACK_ARGS (old_pack);
   24396                 :      189994 :           temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
   24397                 :             :           /* During template argument deduction for the aggregate deduction
   24398                 :             :              candidate, the number of elements in a trailing parameter pack
   24399                 :             :              is only deduced from the number of remaining function
   24400                 :             :              arguments if it is not otherwise deduced.  */
   24401                 :      189994 :           if (cxx_dialect >= cxx20
   24402                 :       43828 :               && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
   24403                 :             :               /* FIXME This isn't set properly for partial instantiations.  */
   24404                 :           5 :               && TPARMS_PRIMARY_TEMPLATE (tparms)
   24405                 :      189998 :               && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
   24406                 :           4 :             TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
   24407                 :      189994 :           if (!comp_template_args (old_args, new_args,
   24408                 :             :                                    &bad_old_arg, &bad_new_arg))
   24409                 :             :             /* Inconsistent unification of this parameter pack.  */
   24410                 :          13 :             return unify_parameter_pack_inconsistent (explain_p,
   24411                 :             :                                                       bad_old_arg,
   24412                 :             :                                                       bad_new_arg);
   24413                 :      189994 :         }
   24414                 :             :     }
   24415                 :             : 
   24416                 :     5099702 :   return unify_success (explain_p);
   24417                 :             : }
   24418                 :             : 
   24419                 :             : /* Handle unification of the domain of an array.  PARM_DOM and ARG_DOM are
   24420                 :             :    INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs.  The other
   24421                 :             :    parameters and return value are as for unify.  */
   24422                 :             : 
   24423                 :             : static int
   24424                 :       50698 : unify_array_domain (tree tparms, tree targs,
   24425                 :             :                     tree parm_dom, tree arg_dom,
   24426                 :             :                     bool explain_p)
   24427                 :             : {
   24428                 :       50698 :   tree parm_max;
   24429                 :       50698 :   tree arg_max;
   24430                 :       50698 :   bool parm_cst;
   24431                 :       50698 :   bool arg_cst;
   24432                 :             : 
   24433                 :             :   /* Our representation of array types uses "N - 1" as the
   24434                 :             :      TYPE_MAX_VALUE for an array with "N" elements, if "N" is
   24435                 :             :      not an integer constant.  We cannot unify arbitrarily
   24436                 :             :      complex expressions, so we eliminate the MINUS_EXPRs
   24437                 :             :      here.  */
   24438                 :       50698 :   parm_max = TYPE_MAX_VALUE (parm_dom);
   24439                 :       50698 :   parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
   24440                 :       50698 :   if (!parm_cst)
   24441                 :             :     {
   24442                 :       50613 :       gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
   24443                 :       50613 :       parm_max = TREE_OPERAND (parm_max, 0);
   24444                 :             :     }
   24445                 :       50698 :   arg_max = TYPE_MAX_VALUE (arg_dom);
   24446                 :       50698 :   arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
   24447                 :       50698 :   if (!arg_cst)
   24448                 :             :     {
   24449                 :             :       /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
   24450                 :             :          trying to unify the type of a variable with the type
   24451                 :             :          of a template parameter.  For example:
   24452                 :             : 
   24453                 :             :            template <unsigned int N>
   24454                 :             :            void f (char (&) [N]);
   24455                 :             :            int g();
   24456                 :             :            void h(int i) {
   24457                 :             :              char a[g(i)];
   24458                 :             :              f(a);
   24459                 :             :            }
   24460                 :             : 
   24461                 :             :          Here, the type of the ARG will be "int [g(i)]", and
   24462                 :             :          may be a SAVE_EXPR, etc.  */
   24463                 :        5840 :       if (TREE_CODE (arg_max) != MINUS_EXPR)
   24464                 :           8 :         return unify_vla_arg (explain_p, arg_dom);
   24465                 :        5832 :       arg_max = TREE_OPERAND (arg_max, 0);
   24466                 :             :     }
   24467                 :             : 
   24468                 :             :   /* If only one of the bounds used a MINUS_EXPR, compensate
   24469                 :             :      by adding one to the other bound.  */
   24470                 :       50690 :   if (parm_cst && !arg_cst)
   24471                 :           0 :     parm_max = fold_build2_loc (input_location, PLUS_EXPR,
   24472                 :             :                                 integer_type_node,
   24473                 :             :                                 parm_max,
   24474                 :             :                                 integer_one_node);
   24475                 :       50690 :   else if (arg_cst && !parm_cst)
   24476                 :       44773 :     arg_max = fold_build2_loc (input_location, PLUS_EXPR,
   24477                 :             :                                integer_type_node,
   24478                 :             :                                arg_max,
   24479                 :             :                                integer_one_node);
   24480                 :             : 
   24481                 :       50690 :   return unify (tparms, targs, parm_max, arg_max,
   24482                 :       50690 :                 UNIFY_ALLOW_INTEGER, explain_p);
   24483                 :             : }
   24484                 :             : 
   24485                 :             : /* Returns whether T, a P or A in unify, is a type, template or expression.  */
   24486                 :             : 
   24487                 :             : enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
   24488                 :             : 
   24489                 :             : static pa_kind_t
   24490                 :  1126811272 : pa_kind (tree t)
   24491                 :             : {
   24492                 :  1126811272 :   if (PACK_EXPANSION_P (t))
   24493                 :     1114178 :     t = PACK_EXPANSION_PATTERN (t);
   24494                 :  1126811272 :   if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
   24495                 :  1126356752 :       || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
   24496                 :  2253167996 :       || DECL_TYPE_TEMPLATE_P (t))
   24497                 :             :     return pa_tmpl;
   24498                 :  1126038007 :   else if (TYPE_P (t))
   24499                 :             :     return pa_type;
   24500                 :             :   else
   24501                 :   195546405 :     return pa_expr;
   24502                 :             : }
   24503                 :             : 
   24504                 :             : /* Deduce the value of template parameters.  TPARMS is the (innermost)
   24505                 :             :    set of template parameters to a template.  TARGS is the bindings
   24506                 :             :    for those template parameters, as determined thus far; TARGS may
   24507                 :             :    include template arguments for outer levels of template parameters
   24508                 :             :    as well.  PARM is a parameter to a template function, or a
   24509                 :             :    subcomponent of that parameter; ARG is the corresponding argument.
   24510                 :             :    This function attempts to match PARM with ARG in a manner
   24511                 :             :    consistent with the existing assignments in TARGS.  If more values
   24512                 :             :    are deduced, then TARGS is updated.
   24513                 :             : 
   24514                 :             :    Returns 0 if the type deduction succeeds, 1 otherwise.  The
   24515                 :             :    parameter STRICT is a bitwise or of the following flags:
   24516                 :             : 
   24517                 :             :      UNIFY_ALLOW_NONE:
   24518                 :             :        Require an exact match between PARM and ARG.
   24519                 :             :      UNIFY_ALLOW_MORE_CV_QUAL:
   24520                 :             :        Allow the deduced ARG to be more cv-qualified (by qualification
   24521                 :             :        conversion) than ARG.
   24522                 :             :      UNIFY_ALLOW_LESS_CV_QUAL:
   24523                 :             :        Allow the deduced ARG to be less cv-qualified than ARG.
   24524                 :             :      UNIFY_ALLOW_DERIVED:
   24525                 :             :        Allow the deduced ARG to be a template base class of ARG,
   24526                 :             :        or a pointer to a template base class of the type pointed to by
   24527                 :             :        ARG.
   24528                 :             :      UNIFY_ALLOW_INTEGER:
   24529                 :             :        Allow any integral type to be deduced.  See the TEMPLATE_PARM_INDEX
   24530                 :             :        case for more information.
   24531                 :             :      UNIFY_ALLOW_OUTER_LEVEL:
   24532                 :             :        This is the outermost level of a deduction. Used to determine validity
   24533                 :             :        of qualification conversions. A valid qualification conversion must
   24534                 :             :        have const qualified pointers leading up to the inner type which
   24535                 :             :        requires additional CV quals, except at the outer level, where const
   24536                 :             :        is not required [conv.qual]. It would be normal to set this flag in
   24537                 :             :        addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
   24538                 :             :      UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
   24539                 :             :        This is the outermost level of a deduction, and PARM can be more CV
   24540                 :             :        qualified at this point.
   24541                 :             :      UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
   24542                 :             :        This is the outermost level of a deduction, and PARM can be less CV
   24543                 :             :        qualified at this point.  */
   24544                 :             : 
   24545                 :             : static int
   24546                 :   574433728 : unify (tree tparms, tree targs, tree parm, tree arg, int strict,
   24547                 :             :        bool explain_p)
   24548                 :             : {
   24549                 :   574433728 :   int idx;
   24550                 :   574433728 :   tree targ;
   24551                 :   574433728 :   tree tparm;
   24552                 :   574433728 :   int strict_in = strict;
   24553                 :  1148867456 :   tsubst_flags_t complain = (explain_p
   24554                 :   574433728 :                              ? tf_warning_or_error
   24555                 :             :                              : tf_none);
   24556                 :             : 
   24557                 :             :   /* I don't think this will do the right thing with respect to types.
   24558                 :             :      But the only case I've seen it in so far has been array bounds, where
   24559                 :             :      signedness is the only information lost, and I think that will be
   24560                 :             :      okay.  VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
   24561                 :             :      finish_id_expression_1, and are also OK.  */
   24562                 :   574435888 :   while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
   24563                 :        2160 :     parm = TREE_OPERAND (parm, 0);
   24564                 :             : 
   24565                 :   574433728 :   if (arg == error_mark_node)
   24566                 :   180141591 :     return unify_invalid (explain_p);
   24567                 :   574433728 :   if (arg == unknown_type_node
   24568                 :   574433728 :       || arg == init_list_type_node)
   24569                 :             :     /* We can't deduce anything from this, but we might get all the
   24570                 :             :        template args from other function args.  */
   24571                 :   252365261 :     return unify_success (explain_p);
   24572                 :             : 
   24573                 :   574433728 :   if (parm == any_targ_node || arg == any_targ_node)
   24574                 :   252365261 :     return unify_success (explain_p);
   24575                 :             : 
   24576                 :             :   /* If PARM uses template parameters, then we can't bail out here,
   24577                 :             :      even if ARG == PARM, since we won't record unifications for the
   24578                 :             :      template parameters.  We might need them if we're trying to
   24579                 :             :      figure out which of two things is more specialized.  */
   24580                 :   574433720 :   if (arg == parm
   24581                 :   574433720 :       && (DECL_P (parm) || !uses_template_parms (parm)))
   24582                 :    11022293 :     return unify_success (explain_p);
   24583                 :             : 
   24584                 :             :   /* Handle init lists early, so the rest of the function can assume
   24585                 :             :      we're dealing with a type. */
   24586                 :   563411427 :   if (BRACE_ENCLOSED_INITIALIZER_P (arg))
   24587                 :             :     {
   24588                 :        5791 :       tree elttype;
   24589                 :        5791 :       tree orig_parm = parm;
   24590                 :             : 
   24591                 :        5791 :       if (!is_std_init_list (parm)
   24592                 :        5791 :           && TREE_CODE (parm) != ARRAY_TYPE)
   24593                 :             :         /* We can only deduce from an initializer list argument if the
   24594                 :             :            parameter is std::initializer_list or an array; otherwise this
   24595                 :             :            is a non-deduced context. */
   24596                 :        5305 :         return unify_success (explain_p);
   24597                 :             : 
   24598                 :        1391 :       if (TREE_CODE (parm) == ARRAY_TYPE)
   24599                 :          78 :         elttype = TREE_TYPE (parm);
   24600                 :             :       else
   24601                 :             :         {
   24602                 :        1313 :           elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
   24603                 :             :           /* Deduction is defined in terms of a single type, so just punt
   24604                 :             :              on the (bizarre) std::initializer_list<T...>.  */
   24605                 :        1313 :           if (PACK_EXPANSION_P (elttype))
   24606                 :        5305 :             return unify_success (explain_p);
   24607                 :             :         }
   24608                 :             : 
   24609                 :        1388 :       if (strict != DEDUCE_EXACT
   24610                 :        1388 :           && TYPE_P (elttype)
   24611                 :        2776 :           && !uses_deducible_template_parms (elttype))
   24612                 :             :         /* If ELTTYPE has no deducible template parms, skip deduction from
   24613                 :             :            the list elements.  */;
   24614                 :             :       else
   24615                 :        7193 :         for (auto &e: CONSTRUCTOR_ELTS (arg))
   24616                 :             :           {
   24617                 :        3620 :             tree elt = e.value;
   24618                 :        3620 :             int elt_strict = strict;
   24619                 :             : 
   24620                 :        3620 :             if (elt == error_mark_node)
   24621                 :         439 :               return unify_invalid (explain_p);
   24622                 :             : 
   24623                 :        3614 :             if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
   24624                 :             :               {
   24625                 :        3086 :                 tree type = TREE_TYPE (elt);
   24626                 :        3086 :                 if (type == error_mark_node)
   24627                 :           6 :                   return unify_invalid (explain_p);
   24628                 :             :                 /* It should only be possible to get here for a call.  */
   24629                 :        3080 :                 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
   24630                 :        6160 :                 elt_strict |= maybe_adjust_types_for_deduction
   24631                 :        3080 :                   (tparms, DEDUCE_CALL, &elttype, &type, elt);
   24632                 :        3080 :                 elt = type;
   24633                 :             :               }
   24634                 :             : 
   24635                 :        3608 :           RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
   24636                 :             :                                    explain_p);
   24637                 :             :         }
   24638                 :             : 
   24639                 :         949 :       if (TREE_CODE (parm) == ARRAY_TYPE
   24640                 :         949 :           && deducible_array_bound (TYPE_DOMAIN (parm)))
   24641                 :             :         {
   24642                 :             :           /* Also deduce from the length of the initializer list.  */
   24643                 :          94 :           tree max = size_int (CONSTRUCTOR_NELTS (arg));
   24644                 :          47 :           tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
   24645                 :          47 :           if (idx == error_mark_node)
   24646                 :         439 :             return unify_invalid (explain_p);
   24647                 :          47 :           return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   24648                 :          47 :                                      idx, explain_p);
   24649                 :             :         }
   24650                 :             : 
   24651                 :             :       /* If the std::initializer_list<T> deduction worked, replace the
   24652                 :             :          deduced A with std::initializer_list<A>.  */
   24653                 :             :       if (orig_parm != parm)
   24654                 :             :         {
   24655                 :             :           idx = TEMPLATE_TYPE_IDX (orig_parm);
   24656                 :             :           targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   24657                 :             :           targ = listify (targ);
   24658                 :             :           TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
   24659                 :             :         }
   24660                 :        5305 :       return unify_success (explain_p);
   24661                 :             :     }
   24662                 :             : 
   24663                 :             :   /* If parm and arg aren't the same kind of thing (template, type, or
   24664                 :             :      expression), fail early.  */
   24665                 :   563405636 :   if (pa_kind (parm) != pa_kind (arg))
   24666                 :   180141591 :     return unify_invalid (explain_p);
   24667                 :             : 
   24668                 :             :   /* Immediately reject some pairs that won't unify because of
   24669                 :             :      cv-qualification mismatches.  */
   24670                 :   563405620 :   if (TREE_CODE (arg) == TREE_CODE (parm)
   24671                 :   263324169 :       && TYPE_P (arg)
   24672                 :             :       /* It is the elements of the array which hold the cv quals of an array
   24673                 :             :          type, and the elements might be template type parms. We'll check
   24674                 :             :          when we recurse.  */
   24675                 :   171227582 :       && TREE_CODE (arg) != ARRAY_TYPE
   24676                 :             :       /* We check the cv-qualifiers when unifying with template type
   24677                 :             :          parameters below.  We want to allow ARG `const T' to unify with
   24678                 :             :          PARM `T' for example, when computing which of two templates
   24679                 :             :          is more specialized, for example.  */
   24680                 :   171167514 :       && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
   24681                 :   705399743 :       && !check_cv_quals_for_unify (strict_in, arg, parm))
   24682                 :      863179 :     return unify_cv_qual_mismatch (explain_p, parm, arg);
   24683                 :             : 
   24684                 :   562542441 :   if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
   24685                 :   296158301 :       && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)
   24686                 :   758245723 :       && !FUNC_OR_METHOD_TYPE_P (parm))
   24687                 :   195440497 :     strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
   24688                 :             :   /* PMFs recurse at the same level, so don't strip this yet.  */
   24689                 :   562542441 :   if (!TYPE_PTRMEMFUNC_P (parm))
   24690                 :   562538568 :     strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
   24691                 :   562542441 :   strict &= ~UNIFY_ALLOW_DERIVED;
   24692                 :   562542441 :   strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
   24693                 :   562542441 :   strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
   24694                 :             : 
   24695                 :   562542441 :   switch (TREE_CODE (parm))
   24696                 :             :     {
   24697                 :             :     case TYPENAME_TYPE:
   24698                 :             :     case SCOPE_REF:
   24699                 :             :     case UNBOUND_CLASS_TEMPLATE:
   24700                 :             :       /* In a type which contains a nested-name-specifier, template
   24701                 :             :          argument values cannot be deduced for template parameters used
   24702                 :             :          within the nested-name-specifier.  */
   24703                 :   252365261 :       return unify_success (explain_p);
   24704                 :             : 
   24705                 :   170037393 :     case TEMPLATE_TYPE_PARM:
   24706                 :   170037393 :     case TEMPLATE_TEMPLATE_PARM:
   24707                 :   170037393 :     case BOUND_TEMPLATE_TEMPLATE_PARM:
   24708                 :   170037393 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   24709                 :   170037393 :       if (error_operand_p (tparm))
   24710                 :   180141591 :         return unify_invalid (explain_p);
   24711                 :             : 
   24712                 :   340074774 :       if (TEMPLATE_TYPE_LEVEL (parm)
   24713                 :   170037387 :           != template_decl_level (tparm))
   24714                 :             :         /* The PARM is not one we're trying to unify.  Just check
   24715                 :             :            to see if it matches ARG.  */
   24716                 :             :         {
   24717                 :          50 :           if (TREE_CODE (arg) == TREE_CODE (parm)
   24718                 :          58 :               && (is_auto (parm) ? is_auto (arg)
   24719                 :           8 :                   : same_type_p (parm, arg)))
   24720                 :          21 :             return unify_success (explain_p);
   24721                 :             :           else
   24722                 :          29 :             return unify_type_mismatch (explain_p, parm, arg);
   24723                 :             :         }
   24724                 :   170037337 :       idx = TEMPLATE_TYPE_IDX (parm);
   24725                 :   170037337 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   24726                 :   170037337 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
   24727                 :   170037337 :       if (error_operand_p (tparm))
   24728                 :   180141591 :         return unify_invalid (explain_p);
   24729                 :             : 
   24730                 :             :       /* Check for mixed types and values.  */
   24731                 :   170037337 :       if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
   24732                 :   169610661 :            && TREE_CODE (tparm) != TYPE_DECL)
   24733                 :   170037337 :           || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   24734                 :      384379 :               && TREE_CODE (tparm) != TEMPLATE_DECL))
   24735                 :           0 :         gcc_unreachable ();
   24736                 :             : 
   24737                 :   170037337 :       if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24738                 :             :         {
   24739                 :       42297 :           if ((strict_in & UNIFY_ALLOW_DERIVED)
   24740                 :       42297 :               && CLASS_TYPE_P (arg))
   24741                 :             :             {
   24742                 :             :               /* First try to match ARG directly.  */
   24743                 :         701 :               tree t = try_class_unification (tparms, targs, parm, arg,
   24744                 :         701 :                                               explain_p);
   24745                 :         701 :               if (!t)
   24746                 :             :                 {
   24747                 :             :                   /* Otherwise, look for a suitable base of ARG, as below.  */
   24748                 :          42 :                   enum template_base_result r;
   24749                 :          42 :                   r = get_template_base (tparms, targs, parm, arg,
   24750                 :             :                                          explain_p, &t);
   24751                 :          42 :                   if (!t)
   24752                 :          34 :                     return unify_no_common_base (explain_p, r, parm, arg);
   24753                 :           8 :                   arg = t;
   24754                 :             :                 }
   24755                 :             :             }
   24756                 :             :           /* ARG must be constructed from a template class or a template
   24757                 :             :              template parameter.  */
   24758                 :       41596 :           else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
   24759                 :       41596 :                    && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
   24760                 :        1087 :             return unify_template_deduction_failure (explain_p, parm, arg);
   24761                 :             : 
   24762                 :             :           /* Deduce arguments T, i from TT<T> or TT<i>.  */
   24763                 :       41176 :           if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
   24764                 :             :             return 1;
   24765                 :             : 
   24766                 :       41128 :           arg = TYPE_TI_TEMPLATE (arg);
   24767                 :       41128 :           if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
   24768                 :             :             /* If the template is a template template parameter, use the
   24769                 :             :                TEMPLATE_TEMPLATE_PARM for matching.  */
   24770                 :          50 :             arg = TREE_TYPE (arg);
   24771                 :             : 
   24772                 :             :           /* Fall through to deduce template name.  */
   24773                 :             :         }
   24774                 :             : 
   24775                 :   170036168 :       if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
   24776                 :   169651789 :           || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
   24777                 :             :         {
   24778                 :             :           /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>.  */
   24779                 :             : 
   24780                 :             :           /* Simple cases: Value already set, does match or doesn't.  */
   24781                 :      425507 :           if (targ != NULL_TREE && template_args_equal (targ, arg))
   24782                 :   252365261 :             return unify_success (explain_p);
   24783                 :      421178 :           else if (targ)
   24784                 :        4690 :             return unify_inconsistency (explain_p, parm, targ, arg);
   24785                 :             :         }
   24786                 :             :       else
   24787                 :             :         {
   24788                 :             :           /* If PARM is `const T' and ARG is only `int', we don't have
   24789                 :             :              a match unless we are allowing additional qualification.
   24790                 :             :              If ARG is `const int' and PARM is just `T' that's OK;
   24791                 :             :              that binds `const int' to `T'.  */
   24792                 :   169610661 :           if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
   24793                 :             :                                          arg, parm))
   24794                 :     1388994 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   24795                 :             : 
   24796                 :             :           /* Consider the case where ARG is `const volatile int' and
   24797                 :             :              PARM is `const T'.  Then, T should be `volatile int'.  */
   24798                 :   336443334 :           arg = cp_build_qualified_type
   24799                 :   168221667 :             (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
   24800                 :   168221667 :           if (arg == error_mark_node)
   24801                 :   180141591 :             return unify_invalid (explain_p);
   24802                 :             : 
   24803                 :             :           /* Simple cases: Value already set, does match or doesn't.  */
   24804                 :   168221667 :           if (targ != NULL_TREE && same_type_p (targ, arg))
   24805                 :   252365261 :             return unify_success (explain_p);
   24806                 :   145113705 :           else if (targ)
   24807                 :     1219065 :             return unify_inconsistency (explain_p, parm, targ, arg);
   24808                 :             : 
   24809                 :             :           /* Make sure that ARG is not a variable-sized array.  (Note
   24810                 :             :              that were talking about variable-sized arrays (like
   24811                 :             :              `int[n]'), rather than arrays of unknown size (like
   24812                 :             :              `int[]').)  We'll get very confused by such a type since
   24813                 :             :              the bound of the array is not constant, and therefore
   24814                 :             :              not mangleable.  Besides, such types are not allowed in
   24815                 :             :              ISO C++, so we can do as we please here.  We do allow
   24816                 :             :              them for 'auto' deduction, since that isn't ABI-exposed.  */
   24817                 :   143894640 :           if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
   24818                 :          18 :             return unify_vla_arg (explain_p, arg);
   24819                 :             : 
   24820                 :             :           /* Strip typedefs as in convert_template_argument.  */
   24821                 :   143894622 :           arg = canonicalize_type_argument (arg, tf_none);
   24822                 :             :         }
   24823                 :             : 
   24824                 :             :       /* If ARG is a parameter pack or an expansion, we cannot unify
   24825                 :             :          against it unless PARM is also a parameter pack.  */
   24826                 :   288621814 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   24827                 :   145358222 :           && !template_parameter_pack_p (parm))
   24828                 :      438996 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   24829                 :             : 
   24830                 :             :       /* If the argument deduction results is a METHOD_TYPE,
   24831                 :             :          then there is a problem.
   24832                 :             :          METHOD_TYPE doesn't map to any real C++ type the result of
   24833                 :             :          the deduction cannot be of that type.  */
   24834                 :   143872114 :       if (TREE_CODE (arg) == METHOD_TYPE)
   24835                 :           8 :         return unify_method_type_error (explain_p, arg);
   24836                 :             : 
   24837                 :   143872106 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   24838                 :   143872106 :       return unify_success (explain_p);
   24839                 :             : 
   24840                 :     6977723 :     case TEMPLATE_PARM_INDEX:
   24841                 :     6977723 :       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
   24842                 :     6977723 :       if (error_operand_p (tparm))
   24843                 :   180141591 :         return unify_invalid (explain_p);
   24844                 :             : 
   24845                 :     6977723 :       if (TEMPLATE_PARM_LEVEL (parm)
   24846                 :     6977723 :           != template_decl_level (tparm))
   24847                 :             :         {
   24848                 :             :           /* The PARM is not one we're trying to unify.  Just check
   24849                 :             :              to see if it matches ARG.  */
   24850                 :           0 :           int result = !(TREE_CODE (arg) == TREE_CODE (parm)
   24851                 :           0 :                          && cp_tree_equal (parm, arg));
   24852                 :           0 :           if (result)
   24853                 :           0 :             unify_expression_unequal (explain_p, parm, arg);
   24854                 :           0 :           return result;
   24855                 :             :         }
   24856                 :             : 
   24857                 :     6977723 :       idx = TEMPLATE_PARM_IDX (parm);
   24858                 :     6977723 :       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
   24859                 :             : 
   24860                 :     6977723 :       if (targ)
   24861                 :             :         {
   24862                 :        6024 :           if ((strict & UNIFY_ALLOW_INTEGER)
   24863                 :         372 :               && TREE_TYPE (targ) && TREE_TYPE (arg)
   24864                 :        6396 :               && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
   24865                 :             :             /* We're deducing from an array bound, the type doesn't matter.
   24866                 :             :                This conversion should match the one below.  */
   24867                 :         372 :             arg = fold (build_nop (TREE_TYPE (targ), arg));
   24868                 :        6024 :           int x = !cp_tree_equal (targ, arg);
   24869                 :        6024 :           if (x)
   24870                 :         591 :             unify_inconsistency (explain_p, parm, targ, arg);
   24871                 :        6024 :           return x;
   24872                 :             :         }
   24873                 :             : 
   24874                 :             :       /* [temp.deduct.type] If, in the declaration of a function template
   24875                 :             :          with a non-type template-parameter, the non-type
   24876                 :             :          template-parameter is used in an expression in the function
   24877                 :             :          parameter-list and, if the corresponding template-argument is
   24878                 :             :          deduced, the template-argument type shall match the type of the
   24879                 :             :          template-parameter exactly, except that a template-argument
   24880                 :             :          deduced from an array bound may be of any integral type.
   24881                 :             :          The non-type parameter might use already deduced type parameters.  */
   24882                 :     6971699 :       tparm = TREE_TYPE (parm);
   24883                 :    20915051 :       if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
   24884                 :             :         /* We don't have enough levels of args to do any substitution.  This
   24885                 :             :            can happen in the context of -fnew-ttp-matching.  */;
   24886                 :             :       else
   24887                 :             :         {
   24888                 :     6965722 :           ++processing_template_decl;
   24889                 :     6965722 :           tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
   24890                 :     6965722 :           --processing_template_decl;
   24891                 :             : 
   24892                 :     6965722 :           if (tree a = type_uses_auto (tparm))
   24893                 :             :             {
   24894                 :         842 :               tparm = do_auto_deduction (tparm, arg, a,
   24895                 :             :                                          complain, adc_unify, targs,
   24896                 :             :                                          LOOKUP_NORMAL,
   24897                 :         421 :                                          TPARMS_PRIMARY_TEMPLATE (tparms));
   24898                 :         421 :               if (tparm == error_mark_node)
   24899                 :             :                 return 1;
   24900                 :             :             }
   24901                 :             :         }
   24902                 :             : 
   24903                 :     6971691 :       if (!TREE_TYPE (arg)
   24904                 :     6971691 :           || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE)
   24905                 :             :         /* Template-parameter dependent expression.  Just accept it for now.
   24906                 :             :            It will later be processed in convert_template_argument.  */
   24907                 :             :         ;
   24908                 :     6971677 :       else if (same_type_ignoring_top_level_qualifiers_p
   24909                 :     6971677 :                (non_reference (TREE_TYPE (arg)),
   24910                 :             :                 non_reference (tparm)))
   24911                 :             :         /* OK.  Ignore top-level quals here because a class-type template
   24912                 :             :            parameter object is const.  */;
   24913                 :       44372 :       else if ((strict & UNIFY_ALLOW_INTEGER)
   24914                 :       44173 :                && CP_INTEGRAL_TYPE_P (tparm))
   24915                 :             :         /* Convert the ARG to the type of PARM; the deduced non-type
   24916                 :             :            template argument must exactly match the types of the
   24917                 :             :            corresponding parameter.  This conversion should match the
   24918                 :             :            one above.  */
   24919                 :       44161 :         arg = fold (build_nop (tparm, arg));
   24920                 :         211 :       else if (uses_template_parms (tparm))
   24921                 :             :         {
   24922                 :             :           /* We haven't deduced the type of this parameter yet.  */
   24923                 :         136 :           if (cxx_dialect >= cxx17
   24924                 :             :               /* We deduce from array bounds in try_array_deduction.  */
   24925                 :         104 :               && !(strict & UNIFY_ALLOW_INTEGER)
   24926                 :         414 :               && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
   24927                 :             :             {
   24928                 :             :               /* Deduce it from the non-type argument.  As above, ignore
   24929                 :             :                  top-level quals here too.  */
   24930                 :          74 :               tree atype = cv_unqualified (TREE_TYPE (arg));
   24931                 :          74 :               RECUR_AND_CHECK_FAILURE (tparms, targs,
   24932                 :             :                                        tparm, atype,
   24933                 :             :                                        UNIFY_ALLOW_NONE, explain_p);
   24934                 :             :               /* Now check whether the type of this parameter is still
   24935                 :             :                  dependent, and give up if so.  */
   24936                 :          68 :               ++processing_template_decl;
   24937                 :          68 :               tparm = tsubst (TREE_TYPE (parm), targs, tf_none, NULL_TREE);
   24938                 :          68 :               --processing_template_decl;
   24939                 :          68 :               if (uses_template_parms (tparm))
   24940                 :             :                 return unify_success (explain_p);
   24941                 :             :             }
   24942                 :             :           else
   24943                 :             :             /* Try again later.  */
   24944                 :   252365261 :             return unify_success (explain_p);
   24945                 :             :         }
   24946                 :             :       else
   24947                 :          75 :         return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
   24948                 :             : 
   24949                 :             :       /* If ARG is a parameter pack or an expansion, we cannot unify
   24950                 :             :          against it unless PARM is also a parameter pack.  */
   24951                 :    13943010 :       if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
   24952                 :     7038455 :           && !TEMPLATE_PARM_PARAMETER_PACK (parm))
   24953                 :           5 :         return unify_parameter_pack_mismatch (explain_p, parm, arg);
   24954                 :             : 
   24955                 :     6971500 :       {
   24956                 :     6971500 :         bool removed_attr = false;
   24957                 :     6971500 :         arg = strip_typedefs_expr (arg, &removed_attr);
   24958                 :             :       }
   24959                 :     6971500 :       TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
   24960                 :     6971500 :       return unify_success (explain_p);
   24961                 :             : 
   24962                 :          19 :     case PTRMEM_CST:
   24963                 :          19 :      {
   24964                 :             :         /* A pointer-to-member constant can be unified only with
   24965                 :             :          another constant.  */
   24966                 :          19 :       if (TREE_CODE (arg) != PTRMEM_CST)
   24967                 :           3 :         return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
   24968                 :             : 
   24969                 :             :       /* Just unify the class member. It would be useless (and possibly
   24970                 :             :          wrong, depending on the strict flags) to unify also
   24971                 :             :          PTRMEM_CST_CLASS, because we want to be sure that both parm and
   24972                 :             :          arg refer to the same variable, even if through different
   24973                 :             :          classes. For instance:
   24974                 :             : 
   24975                 :             :          struct A { int x; };
   24976                 :             :          struct B : A { };
   24977                 :             : 
   24978                 :             :          Unification of &A::x and &B::x must succeed.  */
   24979                 :          16 :       return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
   24980                 :          16 :                     PTRMEM_CST_MEMBER (arg), strict, explain_p);
   24981                 :             :      }
   24982                 :             : 
   24983                 :     7678713 :     case POINTER_TYPE:
   24984                 :     7678713 :       {
   24985                 :     7678713 :         if (!TYPE_PTR_P (arg))
   24986                 :     4477097 :           return unify_type_mismatch (explain_p, parm, arg);
   24987                 :             : 
   24988                 :             :         /* [temp.deduct.call]
   24989                 :             : 
   24990                 :             :            A can be another pointer or pointer to member type that can
   24991                 :             :            be converted to the deduced A via a qualification
   24992                 :             :            conversion (_conv.qual_).
   24993                 :             : 
   24994                 :             :            We pass down STRICT here rather than UNIFY_ALLOW_NONE.
   24995                 :             :            This will allow for additional cv-qualification of the
   24996                 :             :            pointed-to types if appropriate.  */
   24997                 :             : 
   24998                 :     3201616 :         if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
   24999                 :             :           /* The derived-to-base conversion only persists through one
   25000                 :             :              level of pointers.  */
   25001                 :      564179 :           strict |= (strict_in & UNIFY_ALLOW_DERIVED);
   25002                 :             : 
   25003                 :     3201616 :         return unify (tparms, targs, TREE_TYPE (parm),
   25004                 :     6403232 :                       TREE_TYPE (arg), strict, explain_p);
   25005                 :             :       }
   25006                 :             : 
   25007                 :    25695440 :     case REFERENCE_TYPE:
   25008                 :    25695440 :       if (!TYPE_REF_P (arg))
   25009                 :     2310928 :         return unify_type_mismatch (explain_p, parm, arg);
   25010                 :    23384512 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   25011                 :    23384512 :                     strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   25012                 :             : 
   25013                 :     1649500 :     case ARRAY_TYPE:
   25014                 :     1649500 :       if (TREE_CODE (arg) != ARRAY_TYPE)
   25015                 :     1589432 :         return unify_type_mismatch (explain_p, parm, arg);
   25016                 :       60068 :       if ((TYPE_DOMAIN (parm) == NULL_TREE)
   25017                 :       60068 :           != (TYPE_DOMAIN (arg) == NULL_TREE))
   25018                 :        7965 :         return unify_type_mismatch (explain_p, parm, arg);
   25019                 :       52103 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   25020                 :             :                                strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
   25021                 :       51971 :       if (TYPE_DOMAIN (parm) != NULL_TREE)
   25022                 :      101302 :         return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
   25023                 :       50651 :                                    TYPE_DOMAIN (arg), explain_p);
   25024                 :             :       return unify_success (explain_p);
   25025                 :             : 
   25026                 :     5758536 :     case REAL_TYPE:
   25027                 :     5758536 :     case COMPLEX_TYPE:
   25028                 :     5758536 :     case VECTOR_TYPE:
   25029                 :     5758536 :     case INTEGER_TYPE:
   25030                 :     5758536 :     case BOOLEAN_TYPE:
   25031                 :     5758536 :     case ENUMERAL_TYPE:
   25032                 :     5758536 :     case VOID_TYPE:
   25033                 :     5758536 :     case OPAQUE_TYPE:
   25034                 :     5758536 :     case NULLPTR_TYPE:
   25035                 :     5758536 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   25036                 :     1661527 :         return unify_type_mismatch (explain_p, parm, arg);
   25037                 :             : 
   25038                 :             :       /* We have already checked cv-qualification at the top of the
   25039                 :             :          function.  */
   25040                 :     4097009 :       if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
   25041                 :     1957018 :         return unify_type_mismatch (explain_p, parm, arg);
   25042                 :             : 
   25043                 :             :       /* As far as unification is concerned, this wins.  Later checks
   25044                 :             :          will invalidate it if necessary.  */
   25045                 :   252365261 :       return unify_success (explain_p);
   25046                 :             : 
   25047                 :             :       /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
   25048                 :             :       /* Type INTEGER_CST can come from ordinary constant template args.  */
   25049                 :     4063189 :     case INTEGER_CST:
   25050                 :     4063189 :     case REAL_CST:
   25051                 :     4063189 :       if (TREE_TYPE (arg) == NULL_TREE
   25052                 :     4063189 :           || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg)))
   25053                 :          10 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   25054                 :     4063179 :       while (CONVERT_EXPR_P (arg))
   25055                 :           0 :         arg = TREE_OPERAND (arg, 0);
   25056                 :             : 
   25057                 :     4063179 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   25058                 :        4571 :         return unify_template_argument_mismatch (explain_p, parm, arg);
   25059                 :     4058608 :       return (simple_cst_equal (parm, arg)
   25060                 :     4058608 :               ? unify_success (explain_p)
   25061                 :     4058606 :               : unify_template_argument_mismatch (explain_p, parm, arg));
   25062                 :             : 
   25063                 :    86366347 :     case TREE_VEC:
   25064                 :    86366347 :       {
   25065                 :    86366347 :         int i, len, argslen;
   25066                 :    86366347 :         int parm_variadic_p = 0;
   25067                 :             : 
   25068                 :    86366347 :         if (TREE_CODE (arg) != TREE_VEC)
   25069                 :           0 :           return unify_template_argument_mismatch (explain_p, parm, arg);
   25070                 :             : 
   25071                 :    86366347 :         len = TREE_VEC_LENGTH (parm);
   25072                 :    86366347 :         argslen = TREE_VEC_LENGTH (arg);
   25073                 :             : 
   25074                 :             :         /* Check for pack expansions in the parameters.  */
   25075                 :   253274323 :         for (i = 0; i < len; ++i)
   25076                 :             :           {
   25077                 :   166907989 :             if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
   25078                 :             :               {
   25079                 :     4192731 :                 if (i == len - 1)
   25080                 :             :                   /* We can unify against something with a trailing
   25081                 :             :                      parameter pack.  */
   25082                 :             :                   parm_variadic_p = 1;
   25083                 :             :                 else
   25084                 :             :                   /* [temp.deduct.type]/9: If the template argument list of
   25085                 :             :                      P contains a pack expansion that is not the last
   25086                 :             :                      template argument, the entire template argument list
   25087                 :             :                      is a non-deduced context.  */
   25088                 :   252365261 :                   return unify_success (explain_p);
   25089                 :             :               }
   25090                 :             :           }
   25091                 :             : 
   25092                 :             :         /* If we don't have enough arguments to satisfy the parameters
   25093                 :             :            (not counting the pack expression at the end), or we have
   25094                 :             :            too many arguments for a parameter list that doesn't end in
   25095                 :             :            a pack expression, we can't unify.  */
   25096                 :    86366334 :         if (parm_variadic_p
   25097                 :    86366334 :             ? argslen < len - parm_variadic_p
   25098                 :             :             : argslen != len)
   25099                 :     1251845 :           return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
   25100                 :             : 
   25101                 :             :         /* Unify all of the parameters that precede the (optional)
   25102                 :             :            pack expression.  */
   25103                 :   218676493 :         for (i = 0; i < len - parm_variadic_p; ++i)
   25104                 :             :           {
   25105                 :   151067055 :             RECUR_AND_CHECK_FAILURE (tparms, targs,
   25106                 :             :                                      TREE_VEC_ELT (parm, i),
   25107                 :             :                                      TREE_VEC_ELT (arg, i),
   25108                 :             :                                      UNIFY_ALLOW_NONE, explain_p);
   25109                 :             :           }
   25110                 :    67609438 :         if (parm_variadic_p)
   25111                 :     4050766 :           return unify_pack_expansion (tparms, targs, parm, arg,
   25112                 :             :                                        DEDUCE_EXACT,
   25113                 :     4050766 :                                        /*subr=*/true, explain_p);
   25114                 :   252365261 :         return unify_success (explain_p);
   25115                 :             :       }
   25116                 :             : 
   25117                 :   246416440 :     case RECORD_TYPE:
   25118                 :   246416440 :     case UNION_TYPE:
   25119                 :   246416440 :       if (TREE_CODE (arg) != TREE_CODE (parm))
   25120                 :   142111758 :         return unify_type_mismatch (explain_p, parm, arg);
   25121                 :             : 
   25122                 :   104304682 :       if (TYPE_PTRMEMFUNC_P (parm))
   25123                 :             :         {
   25124                 :        3463 :           if (!TYPE_PTRMEMFUNC_P (arg))
   25125                 :         270 :             return unify_type_mismatch (explain_p, parm, arg);
   25126                 :             : 
   25127                 :        9579 :           return unify (tparms, targs,
   25128                 :        3193 :                         TYPE_PTRMEMFUNC_FN_TYPE (parm),
   25129                 :        3193 :                         TYPE_PTRMEMFUNC_FN_TYPE (arg),
   25130                 :        3193 :                         strict, explain_p);
   25131                 :             :         }
   25132                 :   104301219 :       else if (TYPE_PTRMEMFUNC_P (arg))
   25133                 :         254 :         return unify_type_mismatch (explain_p, parm, arg);
   25134                 :             : 
   25135                 :   104300965 :       if (CLASSTYPE_TEMPLATE_INFO (parm))
   25136                 :             :         {
   25137                 :   102566771 :           tree t = NULL_TREE;
   25138                 :             : 
   25139                 :   102566771 :           if (strict_in & UNIFY_ALLOW_DERIVED)
   25140                 :             :             {
   25141                 :             :               /* First, we try to unify the PARM and ARG directly.  */
   25142                 :    77341875 :               t = try_class_unification (tparms, targs,
   25143                 :             :                                          parm, arg, explain_p);
   25144                 :             : 
   25145                 :    77341875 :               if (!t)
   25146                 :             :                 {
   25147                 :             :                   /* Fallback to the special case allowed in
   25148                 :             :                      [temp.deduct.call]:
   25149                 :             : 
   25150                 :             :                        If P is a class, and P has the form
   25151                 :             :                        template-id, then A can be a derived class of
   25152                 :             :                        the deduced A.  Likewise, if P is a pointer to
   25153                 :             :                        a class of the form template-id, A can be a
   25154                 :             :                        pointer to a derived class pointed to by the
   25155                 :             :                        deduced A.  */
   25156                 :    68509539 :                   enum template_base_result r;
   25157                 :    68509539 :                   r = get_template_base (tparms, targs, parm, arg,
   25158                 :             :                                          explain_p, &t);
   25159                 :             : 
   25160                 :    68509539 :                   if (!t)
   25161                 :             :                     {
   25162                 :             :                       /* Don't give the derived diagnostic if we're
   25163                 :             :                          already dealing with the same template.  */
   25164                 :    67843403 :                       bool same_template
   25165                 :    67843403 :                         = (CLASSTYPE_TEMPLATE_INFO (arg)
   25166                 :    67843403 :                            && (CLASSTYPE_TI_TEMPLATE (parm)
   25167                 :    47604153 :                                == CLASSTYPE_TI_TEMPLATE (arg)));
   25168                 :    67843403 :                       return unify_no_common_base (explain_p && !same_template,
   25169                 :    67843403 :                                                    r, parm, arg);
   25170                 :             :                     }
   25171                 :             :                 }
   25172                 :             :             }
   25173                 :    25224896 :           else if (CLASSTYPE_TEMPLATE_INFO (arg)
   25174                 :    25224896 :                    && (CLASSTYPE_TI_TEMPLATE (parm)
   25175                 :    24301182 :                        == CLASSTYPE_TI_TEMPLATE (arg)))
   25176                 :             :             /* Perhaps PARM is something like S<U> and ARG is S<int>.
   25177                 :             :                Then, we should unify `int' and `U'.  */
   25178                 :    23049993 :             t = arg;
   25179                 :             :           else
   25180                 :             :             /* There's no chance of unification succeeding.  */
   25181                 :     2174903 :             return unify_type_mismatch (explain_p, parm, arg);
   25182                 :             : 
   25183                 :    32548465 :           if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
   25184                 :    32548465 :             return unify (tparms, targs,
   25185                 :    32548465 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)),
   25186                 :    32548465 :                           INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)),
   25187                 :    32548465 :                           UNIFY_ALLOW_NONE, explain_p);
   25188                 :             :           else
   25189                 :   102566771 :             return unify_success (explain_p);
   25190                 :             :         }
   25191                 :     1734194 :       else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
   25192                 :     1734191 :         return unify_type_mismatch (explain_p, parm, arg);
   25193                 :   252365261 :       return unify_success (explain_p);
   25194                 :             : 
   25195                 :      262789 :     case METHOD_TYPE:
   25196                 :      262789 :     case FUNCTION_TYPE:
   25197                 :      262789 :       {
   25198                 :      262789 :         unsigned int nargs;
   25199                 :      262789 :         tree *args;
   25200                 :      262789 :         tree a;
   25201                 :      262789 :         unsigned int i;
   25202                 :             : 
   25203                 :      262789 :         if (TREE_CODE (arg) != TREE_CODE (parm))
   25204                 :        2335 :           return unify_type_mismatch (explain_p, parm, arg);
   25205                 :             : 
   25206                 :             :         /* CV qualifications for methods can never be deduced, they must
   25207                 :             :            match exactly.  We need to check them explicitly here,
   25208                 :             :            because type_unification_real treats them as any other
   25209                 :             :            cv-qualified parameter.  */
   25210                 :      260454 :         if (TREE_CODE (parm) == METHOD_TYPE
   25211                 :      263647 :             && (!check_cv_quals_for_unify
   25212                 :        3193 :                 (UNIFY_ALLOW_NONE,
   25213                 :             :                  class_of_this_parm (arg),
   25214                 :             :                  class_of_this_parm (parm))))
   25215                 :        2102 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   25216                 :      258352 :         if (TREE_CODE (arg) == FUNCTION_TYPE
   25217                 :      258352 :             && type_memfn_quals (parm) != type_memfn_quals (arg))
   25218                 :         384 :           return unify_cv_qual_mismatch (explain_p, parm, arg);
   25219                 :      257968 :         if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
   25220                 :         504 :           return unify_type_mismatch (explain_p, parm, arg);
   25221                 :             : 
   25222                 :      257464 :         RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
   25223                 :             :                                  TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
   25224                 :             : 
   25225                 :      256890 :         nargs = list_length (TYPE_ARG_TYPES (arg));
   25226                 :      256890 :         args = XALLOCAVEC (tree, nargs);
   25227                 :      256890 :         for (a = TYPE_ARG_TYPES (arg), i = 0;
   25228                 :      877957 :              a != NULL_TREE && a != void_list_node;
   25229                 :      621067 :              a = TREE_CHAIN (a), ++i)
   25230                 :      621067 :           args[i] = TREE_VALUE (a);
   25231                 :      256890 :         nargs = i;
   25232                 :             : 
   25233                 :      256890 :         if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
   25234                 :             :                                    args, nargs, 1, DEDUCE_EXACT,
   25235                 :             :                                    NULL, explain_p))
   25236                 :             :           return 1;
   25237                 :             : 
   25238                 :      256685 :         if (flag_noexcept_type)
   25239                 :             :           {
   25240                 :      250217 :             tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
   25241                 :      250217 :             tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
   25242                 :      250217 :             if (pspec == NULL_TREE) pspec = noexcept_false_spec;
   25243                 :      250217 :             if (aspec == NULL_TREE) aspec = noexcept_false_spec;
   25244                 :      500434 :             if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
   25245                 :      500434 :                 && uses_template_parms (TREE_PURPOSE (pspec)))
   25246                 :         169 :               RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
   25247                 :             :                                        TREE_PURPOSE (aspec),
   25248                 :             :                                        UNIFY_ALLOW_NONE, explain_p);
   25249                 :             :             else
   25250                 :             :               {
   25251                 :      250048 :                 bool pn = nothrow_spec_p (pspec);
   25252                 :      250048 :                 bool an = nothrow_spec_p (aspec);
   25253                 :             :                 /* Here "less cv-qual" means the deduced arg (i.e. parm) has
   25254                 :             :                    /more/ noexcept, since function pointer conversions are the
   25255                 :             :                    reverse of qualification conversions.  */
   25256                 :      250048 :                 if (an == pn
   25257                 :      205308 :                     || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   25258                 :      205302 :                     || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   25259                 :             :                   /* OK.  */;
   25260                 :             :                 else
   25261                 :         100 :                   return unify_type_mismatch (explain_p, parm, arg);
   25262                 :             :               }
   25263                 :             :           }
   25264                 :      256585 :         if (flag_tm)
   25265                 :             :           {
   25266                 :             :             /* As for noexcept.  */
   25267                 :          63 :             bool pn = tx_safe_fn_type_p (parm);
   25268                 :          63 :             bool an = tx_safe_fn_type_p (arg);
   25269                 :          63 :             if (an == pn
   25270                 :           5 :                 || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL))
   25271                 :           5 :                 || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL)))
   25272                 :             :               /* OK.  */;
   25273                 :             :             else
   25274                 :           1 :               return unify_type_mismatch (explain_p, parm, arg);
   25275                 :             :           }
   25276                 :             : 
   25277                 :             :         return 0;
   25278                 :             :       }
   25279                 :             : 
   25280                 :       41773 :     case OFFSET_TYPE:
   25281                 :             :       /* Unify a pointer to member with a pointer to member function, which
   25282                 :             :          deduces the type of the member as a function type. */
   25283                 :       41773 :       if (TYPE_PTRMEMFUNC_P (arg))
   25284                 :             :         {
   25285                 :             :           /* Check top-level cv qualifiers */
   25286                 :       19029 :           if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
   25287                 :          24 :             return unify_cv_qual_mismatch (explain_p, parm, arg);
   25288                 :             : 
   25289                 :       19005 :           RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   25290                 :             :                                    TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
   25291                 :             :                                    UNIFY_ALLOW_NONE, explain_p);
   25292                 :             : 
   25293                 :             :           /* Determine the type of the function we are unifying against. */
   25294                 :       19005 :           tree fntype = static_fn_type (arg);
   25295                 :             : 
   25296                 :       19005 :           return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
   25297                 :             :         }
   25298                 :             : 
   25299                 :       22744 :       if (TREE_CODE (arg) != OFFSET_TYPE)
   25300                 :       21796 :         return unify_type_mismatch (explain_p, parm, arg);
   25301                 :         948 :       RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
   25302                 :             :                                TYPE_OFFSET_BASETYPE (arg),
   25303                 :             :                                UNIFY_ALLOW_NONE, explain_p);
   25304                 :         948 :       return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
   25305                 :         948 :                     strict, explain_p);
   25306                 :             : 
   25307                 :           0 :     case CONST_DECL:
   25308                 :             :       /* CONST_DECL should already have been folded to its DECL_INITIAL.  */
   25309                 :           0 :       gcc_unreachable ();
   25310                 :             : 
   25311                 :        2246 :     case FIELD_DECL:
   25312                 :        2246 :     case FUNCTION_DECL:
   25313                 :        2246 :     case TEMPLATE_DECL:
   25314                 :             :       /* Matched cases are handled by the ARG == PARM test above.  */
   25315                 :        2246 :       return unify_template_argument_mismatch (explain_p, parm, arg);
   25316                 :             : 
   25317                 :           8 :     case VAR_DECL:
   25318                 :             :       /* We might get a variable as a non-type template argument in parm if the
   25319                 :             :          corresponding parameter is type-dependent.  Make any necessary
   25320                 :             :          adjustments based on whether arg is a reference.  */
   25321                 :           8 :       if (CONSTANT_CLASS_P (arg))
   25322                 :           0 :         parm = fold_non_dependent_expr (parm, complain);
   25323                 :           8 :       else if (REFERENCE_REF_P (arg))
   25324                 :             :         {
   25325                 :           0 :           tree sub = TREE_OPERAND (arg, 0);
   25326                 :           0 :           STRIP_NOPS (sub);
   25327                 :           0 :           if (TREE_CODE (sub) == ADDR_EXPR)
   25328                 :           0 :             arg = TREE_OPERAND (sub, 0);
   25329                 :             :         }
   25330                 :             :       /* Now use the normal expression code to check whether they match.  */
   25331                 :           8 :       goto expr;
   25332                 :             : 
   25333                 :     6161891 :     case TYPE_ARGUMENT_PACK:
   25334                 :     6161891 :     case NONTYPE_ARGUMENT_PACK:
   25335                 :     6161891 :       return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
   25336                 :    12323782 :                     ARGUMENT_PACK_ARGS (arg), strict, explain_p);
   25337                 :             : 
   25338                 :             :     case TYPEOF_TYPE:
   25339                 :             :     case DECLTYPE_TYPE:
   25340                 :             :     case TRAIT_TYPE:
   25341                 :             :       /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
   25342                 :             :          or TRAIT_TYPE nodes.  */
   25343                 :   252365261 :       return unify_success (explain_p);
   25344                 :             : 
   25345                 :             :     case ERROR_MARK:
   25346                 :             :       /* Unification fails if we hit an error node.  */
   25347                 :   180141591 :       return unify_invalid (explain_p);
   25348                 :             : 
   25349                 :          58 :     case INDIRECT_REF:
   25350                 :          58 :       if (REFERENCE_REF_P (parm))
   25351                 :             :         {
   25352                 :          58 :           bool pexp = PACK_EXPANSION_P (arg);
   25353                 :          58 :           if (pexp)
   25354                 :           3 :             arg = PACK_EXPANSION_PATTERN (arg);
   25355                 :          58 :           if (REFERENCE_REF_P (arg))
   25356                 :          58 :             arg = TREE_OPERAND (arg, 0);
   25357                 :          58 :           if (pexp)
   25358                 :           3 :             arg = make_pack_expansion (arg, complain);
   25359                 :          58 :           return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
   25360                 :          58 :                         strict, explain_p);
   25361                 :             :         }
   25362                 :             :       /* FALLTHRU */
   25363                 :             : 
   25364                 :         399 :     default:
   25365                 :             :       /* An unresolved overload is a nondeduced context.  */
   25366                 :         399 :       if (is_overloaded_fn (parm) || type_unknown_p (parm))
   25367                 :   252365261 :         return unify_success (explain_p);
   25368                 :         399 :       gcc_assert (EXPR_P (parm)
   25369                 :             :                   || TREE_CODE (parm) == CONSTRUCTOR
   25370                 :             :                   || TREE_CODE (parm) == TRAIT_EXPR);
   25371                 :         399 :     expr:
   25372                 :             :       /* We must be looking at an expression.  This can happen with
   25373                 :             :          something like:
   25374                 :             : 
   25375                 :             :            template <int I>
   25376                 :             :            void foo(S<I>, S<I + 2>);
   25377                 :             : 
   25378                 :             :          or
   25379                 :             : 
   25380                 :             :            template<typename T>
   25381                 :             :            void foo(A<T, T{}>);
   25382                 :             : 
   25383                 :             :          This is a "non-deduced context":
   25384                 :             : 
   25385                 :             :            [deduct.type]
   25386                 :             : 
   25387                 :             :            The non-deduced contexts are:
   25388                 :             : 
   25389                 :             :            --A non-type template argument or an array bound in which
   25390                 :             :              a subexpression references a template parameter.
   25391                 :             : 
   25392                 :             :          In these cases, we assume deduction succeeded, but don't
   25393                 :             :          actually infer any unifications.  */
   25394                 :             : 
   25395                 :         407 :       if (!uses_template_parms (parm)
   25396                 :         407 :           && !template_args_equal (parm, arg))
   25397                 :          13 :         return unify_expression_unequal (explain_p, parm, arg);
   25398                 :             :       else
   25399                 :         394 :         return unify_success (explain_p);
   25400                 :             :     }
   25401                 :             : }
   25402                 :             : #undef RECUR_AND_CHECK_FAILURE
   25403                 :             : 
   25404                 :             : /* Note that DECL can be defined in this translation unit, if
   25405                 :             :    required.  */
   25406                 :             : 
   25407                 :             : static void
   25408                 :    15079096 : mark_definable (tree decl)
   25409                 :             : {
   25410                 :    15079096 :   tree clone;
   25411                 :    15079096 :   DECL_NOT_REALLY_EXTERN (decl) = 1;
   25412                 :    19817604 :   FOR_EACH_CLONE (clone, decl)
   25413                 :     4738508 :     DECL_NOT_REALLY_EXTERN (clone) = 1;
   25414                 :    15079096 : }
   25415                 :             : 
   25416                 :             : /* Called if RESULT is explicitly instantiated, or is a member of an
   25417                 :             :    explicitly instantiated class.  */
   25418                 :             : 
   25419                 :             : void
   25420                 :    21784628 : mark_decl_instantiated (tree result, int extern_p)
   25421                 :             : {
   25422                 :    21784628 :   SET_DECL_EXPLICIT_INSTANTIATION (result);
   25423                 :             : 
   25424                 :             :   /* If this entity has already been written out, it's too late to
   25425                 :             :      make any modifications.  */
   25426                 :    21784628 :   if (TREE_ASM_WRITTEN (result))
   25427                 :             :     return;
   25428                 :             : 
   25429                 :             :   /* consteval functions are never emitted.  */
   25430                 :    21766364 :   if (TREE_CODE (result) == FUNCTION_DECL
   25431                 :    43192226 :       && DECL_IMMEDIATE_FUNCTION_P (result))
   25432                 :             :     return;
   25433                 :             : 
   25434                 :             :   /* For anonymous namespace we don't need to do anything.  */
   25435                 :    21766362 :   if (decl_internal_context_p (result))
   25436                 :             :     {
   25437                 :         958 :       gcc_assert (!TREE_PUBLIC (result));
   25438                 :             :       return;
   25439                 :             :     }
   25440                 :             : 
   25441                 :    21765404 :   if (TREE_CODE (result) != FUNCTION_DECL)
   25442                 :             :     /* The TREE_PUBLIC flag for function declarations will have been
   25443                 :             :        set correctly by tsubst.  */
   25444                 :      340492 :     TREE_PUBLIC (result) = 1;
   25445                 :             : 
   25446                 :    21765404 :   if (extern_p)
   25447                 :             :     {
   25448                 :    21734462 :       DECL_EXTERNAL (result) = 1;
   25449                 :    21734462 :       DECL_NOT_REALLY_EXTERN (result) = 0;
   25450                 :             :     }
   25451                 :             :   else
   25452                 :             :     {
   25453                 :       30942 :       mark_definable (result);
   25454                 :       30942 :       mark_needed (result);
   25455                 :             :       /* Always make artificials weak.  */
   25456                 :       30942 :       if (DECL_ARTIFICIAL (result) && flag_weak)
   25457                 :           0 :         comdat_linkage (result);
   25458                 :             :       /* For WIN32 we also want to put explicit instantiations in
   25459                 :             :          linkonce sections.  */
   25460                 :       30942 :       else if (TREE_PUBLIC (result))
   25461                 :       30920 :         maybe_make_one_only (result);
   25462                 :       30942 :       if (TREE_CODE (result) == FUNCTION_DECL
   25463                 :       30942 :           && DECL_TEMPLATE_INSTANTIATED (result))
   25464                 :             :         /* If the function has already been instantiated, clear DECL_EXTERNAL,
   25465                 :             :            since start_preparsed_function wouldn't have if we had an earlier
   25466                 :             :            extern explicit instantiation.  */
   25467                 :          22 :         DECL_EXTERNAL (result) = 0;
   25468                 :             :     }
   25469                 :             : 
   25470                 :             :   /* If EXTERN_P, then this function will not be emitted -- unless
   25471                 :             :      followed by an explicit instantiation, at which point its linkage
   25472                 :             :      will be adjusted.  If !EXTERN_P, then this function will be
   25473                 :             :      emitted here.  In neither circumstance do we want
   25474                 :             :      import_export_decl to adjust the linkage.  */
   25475                 :    21765404 :   DECL_INTERFACE_KNOWN (result) = 1;
   25476                 :             : }
   25477                 :             : 
   25478                 :             : /* Subroutine of more_specialized_fn: check whether TARGS is missing any
   25479                 :             :    important template arguments.  If any are missing, we check whether
   25480                 :             :    they're important by using error_mark_node for substituting into any
   25481                 :             :    args that were used for partial ordering (the ones between ARGS and END)
   25482                 :             :    and seeing if it bubbles up.  */
   25483                 :             : 
   25484                 :             : static bool
   25485                 :     3396140 : check_undeduced_parms (tree targs, tree args, tree end)
   25486                 :             : {
   25487                 :     3396140 :   bool found = false;
   25488                 :    12196806 :   for (tree& targ : tree_vec_range (targs))
   25489                 :     8800666 :     if (targ == NULL_TREE)
   25490                 :             :       {
   25491                 :     1733211 :         found = true;
   25492                 :     1733211 :         targ = error_mark_node;
   25493                 :             :       }
   25494                 :     3396140 :   if (found)
   25495                 :             :     {
   25496                 :     1730889 :       tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
   25497                 :     1730889 :       if (substed == error_mark_node)
   25498                 :             :         return true;
   25499                 :             :     }
   25500                 :             :   return false;
   25501                 :             : }
   25502                 :             : 
   25503                 :             : /* Given two function templates PAT1 and PAT2, return:
   25504                 :             : 
   25505                 :             :    1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   25506                 :             :    -1 if PAT2 is more specialized than PAT1.
   25507                 :             :    0 if neither is more specialized.
   25508                 :             : 
   25509                 :             :    LEN indicates the number of parameters we should consider
   25510                 :             :    (defaulted parameters should not be considered).
   25511                 :             : 
   25512                 :             :    The 1998 std underspecified function template partial ordering, and
   25513                 :             :    DR214 addresses the issue.  We take pairs of arguments, one from
   25514                 :             :    each of the templates, and deduce them against each other.  One of
   25515                 :             :    the templates will be more specialized if all the *other*
   25516                 :             :    template's arguments deduce against its arguments and at least one
   25517                 :             :    of its arguments *does* *not* deduce against the other template's
   25518                 :             :    corresponding argument.  Deduction is done as for class templates.
   25519                 :             :    The arguments used in deduction have reference and top level cv
   25520                 :             :    qualifiers removed.  Iff both arguments were originally reference
   25521                 :             :    types *and* deduction succeeds in both directions, an lvalue reference
   25522                 :             :    wins against an rvalue reference and otherwise the template
   25523                 :             :    with the more cv-qualified argument wins for that pairing (if
   25524                 :             :    neither is more cv-qualified, they both are equal).  Unlike regular
   25525                 :             :    deduction, after all the arguments have been deduced in this way,
   25526                 :             :    we do *not* verify the deduced template argument values can be
   25527                 :             :    substituted into non-deduced contexts.
   25528                 :             : 
   25529                 :             :    The logic can be a bit confusing here, because we look at deduce1 and
   25530                 :             :    targs1 to see if pat2 is at least as specialized, and vice versa; if we
   25531                 :             :    can find template arguments for pat1 to make arg1 look like arg2, that
   25532                 :             :    means that arg2 is at least as specialized as arg1.  */
   25533                 :             : 
   25534                 :             : int
   25535                 :     2841601 : more_specialized_fn (tree pat1, tree pat2, int len)
   25536                 :             : {
   25537                 :     2841601 :   tree decl1 = DECL_TEMPLATE_RESULT (pat1);
   25538                 :     2841601 :   tree decl2 = DECL_TEMPLATE_RESULT (pat2);
   25539                 :     2841601 :   tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
   25540                 :     2841601 :   tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
   25541                 :     2841601 :   tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
   25542                 :     2841601 :   tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
   25543                 :     2841601 :   tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
   25544                 :     2841601 :   tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
   25545                 :     2841601 :   tree origs1, origs2;
   25546                 :     2841601 :   bool lose1 = false;
   25547                 :     2841601 :   bool lose2 = false;
   25548                 :             : 
   25549                 :             :   /* C++17 [temp.func.order]/3 (CWG532)
   25550                 :             : 
   25551                 :             :      If only one of the function templates M is a non-static member of some
   25552                 :             :      class A, M is considered to have a new first parameter inserted in its
   25553                 :             :      function parameter list. Given cv as the cv-qualifiers of M (if any), the
   25554                 :             :      new parameter is of type "rvalue reference to cv A" if the optional
   25555                 :             :      ref-qualifier of M is && or if M has no ref-qualifier and the first
   25556                 :             :      parameter of the other template has rvalue reference type. Otherwise, the
   25557                 :             :      new parameter is of type "lvalue reference to cv A".  */
   25558                 :             : 
   25559                 :     2841601 :   if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2))
   25560                 :             :     {
   25561                 :             :       /* Note C++20 DR2445 extended the above to static member functions, but
   25562                 :             :          I think the old G++ behavior of just skipping the object
   25563                 :             :          parameter when comparing to a static member function was better, so
   25564                 :             :          let's stick with that for now.  This is CWG2834.  --jason 2023-12 */
   25565                 :         490 :       if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1))
   25566                 :             :         {
   25567                 :          30 :           len--; /* LEN is the number of significant arguments for DECL1 */
   25568                 :          30 :           args1 = TREE_CHAIN (args1);
   25569                 :             :         }
   25570                 :         460 :       else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2))
   25571                 :          20 :         args2 = TREE_CHAIN (args2);
   25572                 :             :     }
   25573                 :     2841111 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   25574                 :     2841111 :            && DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   25575                 :             :     {
   25576                 :             :       /* Note DR2445 also (IMO wrongly) removed the "only one" above, which
   25577                 :             :          would break e.g.  cpp1y/lambda-generic-variadic5.C.  */
   25578                 :       12519 :       len--;
   25579                 :       12519 :       args1 = TREE_CHAIN (args1);
   25580                 :       12519 :       args2 = TREE_CHAIN (args2);
   25581                 :             :     }
   25582                 :     2828592 :   else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)
   25583                 :     2828592 :            || DECL_IOBJ_MEMBER_FUNCTION_P (decl2))
   25584                 :             :     {
   25585                 :             :       /* The other is a non-member or explicit object member function;
   25586                 :             :          rewrite the implicit object parameter to a reference.  */
   25587                 :         120 :       tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1;
   25588                 :         120 :       tree &nsargs = ns == decl2 ? args2 : args1;
   25589                 :         120 :       tree obtype = TREE_TYPE (TREE_VALUE (nsargs));
   25590                 :             : 
   25591                 :         120 :       nsargs = TREE_CHAIN (nsargs);
   25592                 :             : 
   25593                 :         120 :       cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns));
   25594                 :         120 :       if (rqual == REF_QUAL_NONE)
   25595                 :             :         {
   25596                 :          88 :           tree otherfirst = ns == decl1 ? args2 : args1;
   25597                 :          88 :           otherfirst = TREE_VALUE (otherfirst);
   25598                 :          88 :           if (TREE_CODE (otherfirst) == REFERENCE_TYPE
   25599                 :          88 :               && TYPE_REF_IS_RVALUE (otherfirst))
   25600                 :             :             rqual = REF_QUAL_RVALUE;
   25601                 :             :         }
   25602                 :         120 :       obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE);
   25603                 :         120 :       nsargs = tree_cons (NULL_TREE, obtype, nsargs);
   25604                 :             :     }
   25605                 :             : 
   25606                 :             :   /* If only one is a conversion operator, they are unordered.  */
   25607                 :     6559517 :   if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
   25608                 :             :     return 0;
   25609                 :             : 
   25610                 :             :   /* Consider the return type for a conversion function */
   25611                 :     4700535 :   if (DECL_CONV_FN_P (decl1))
   25612                 :             :     {
   25613                 :          12 :       args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
   25614                 :          12 :       args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
   25615                 :          12 :       len++;
   25616                 :             :     }
   25617                 :             : 
   25618                 :     2841585 :   processing_template_decl++;
   25619                 :             : 
   25620                 :     2841585 :   origs1 = args1;
   25621                 :     2841585 :   origs2 = args2;
   25622                 :             : 
   25623                 :     2841585 :   while (len--
   25624                 :             :          /* Stop when an ellipsis is seen.  */
   25625                 :     6746653 :          && args1 != NULL_TREE && args2 != NULL_TREE)
   25626                 :             :     {
   25627                 :     3920994 :       tree arg1 = TREE_VALUE (args1);
   25628                 :     3920994 :       tree arg2 = TREE_VALUE (args2);
   25629                 :     3920994 :       int deduce1, deduce2;
   25630                 :     3920994 :       int quals1 = -1;
   25631                 :     3920994 :       int quals2 = -1;
   25632                 :     3920994 :       int ref1 = 0;
   25633                 :     3920994 :       int ref2 = 0;
   25634                 :             : 
   25635                 :     3920994 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   25636                 :        1293 :           && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   25637                 :             :         {
   25638                 :             :           /* When both arguments are pack expansions, we need only
   25639                 :             :              unify the patterns themselves.  */
   25640                 :         206 :           arg1 = PACK_EXPANSION_PATTERN (arg1);
   25641                 :         206 :           arg2 = PACK_EXPANSION_PATTERN (arg2);
   25642                 :             : 
   25643                 :             :           /* This is the last comparison we need to do.  */
   25644                 :             :           len = 0;
   25645                 :             :         }
   25646                 :             : 
   25647                 :     3920994 :       if (TYPE_REF_P (arg1))
   25648                 :             :         {
   25649                 :     2014098 :           ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
   25650                 :     2014098 :           arg1 = TREE_TYPE (arg1);
   25651                 :     2014098 :           quals1 = cp_type_quals (arg1);
   25652                 :             :         }
   25653                 :             : 
   25654                 :     3920994 :       if (TYPE_REF_P (arg2))
   25655                 :             :         {
   25656                 :     2082552 :           ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
   25657                 :     2082552 :           arg2 = TREE_TYPE (arg2);
   25658                 :     2082552 :           quals2 = cp_type_quals (arg2);
   25659                 :             :         }
   25660                 :             : 
   25661                 :     3920994 :       arg1 = TYPE_MAIN_VARIANT (arg1);
   25662                 :     3920994 :       arg2 = TYPE_MAIN_VARIANT (arg2);
   25663                 :             : 
   25664                 :     3920994 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
   25665                 :             :         {
   25666                 :        1087 :           int i, len2 = remaining_arguments (args2);
   25667                 :        1087 :           tree parmvec = make_tree_vec (1);
   25668                 :        1087 :           tree argvec = make_tree_vec (len2);
   25669                 :        1087 :           tree ta = args2;
   25670                 :             : 
   25671                 :             :           /* Setup the parameter vector, which contains only ARG1.  */
   25672                 :        1087 :           TREE_VEC_ELT (parmvec, 0) = arg1;
   25673                 :             : 
   25674                 :             :           /* Setup the argument vector, which contains the remaining
   25675                 :             :              arguments.  */
   25676                 :        2434 :           for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
   25677                 :        1347 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   25678                 :             : 
   25679                 :        1087 :           deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
   25680                 :             :                                            argvec, DEDUCE_EXACT,
   25681                 :             :                                            /*subr=*/true, /*explain_p=*/false)
   25682                 :        1087 :                      == 0);
   25683                 :             : 
   25684                 :             :           /* We cannot deduce in the other direction, because ARG1 is
   25685                 :             :              a pack expansion but ARG2 is not.  */
   25686                 :        1087 :           deduce2 = 0;
   25687                 :             :         }
   25688                 :     3919907 :       else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   25689                 :             :         {
   25690                 :         407 :           int i, len1 = remaining_arguments (args1);
   25691                 :         407 :           tree parmvec = make_tree_vec (1);
   25692                 :         407 :           tree argvec = make_tree_vec (len1);
   25693                 :         407 :           tree ta = args1;
   25694                 :             : 
   25695                 :             :           /* Setup the parameter vector, which contains only ARG1.  */
   25696                 :         407 :           TREE_VEC_ELT (parmvec, 0) = arg2;
   25697                 :             : 
   25698                 :             :           /* Setup the argument vector, which contains the remaining
   25699                 :             :              arguments.  */
   25700                 :        1037 :           for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
   25701                 :         630 :             TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
   25702                 :             : 
   25703                 :         407 :           deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
   25704                 :             :                                            argvec, DEDUCE_EXACT,
   25705                 :             :                                            /*subr=*/true, /*explain_p=*/false)
   25706                 :         407 :                      == 0);
   25707                 :             : 
   25708                 :             :           /* We cannot deduce in the other direction, because ARG2 is
   25709                 :             :              a pack expansion but ARG1 is not.*/
   25710                 :         407 :           deduce1 = 0;
   25711                 :             :         }
   25712                 :             : 
   25713                 :             :       else
   25714                 :             :         {
   25715                 :             :           /* The normal case, where neither argument is a pack
   25716                 :             :              expansion.  */
   25717                 :     3919500 :           deduce1 = (unify (tparms1, targs1, arg1, arg2,
   25718                 :             :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   25719                 :     3919500 :                      == 0);
   25720                 :     3919500 :           deduce2 = (unify (tparms2, targs2, arg2, arg1,
   25721                 :             :                             UNIFY_ALLOW_NONE, /*explain_p=*/false)
   25722                 :     3919500 :                      == 0);
   25723                 :             :         }
   25724                 :             : 
   25725                 :             :       /* If we couldn't deduce arguments for tparms1 to make arg1 match
   25726                 :             :          arg2, then arg2 is not as specialized as arg1.  */
   25727                 :     3920994 :       if (!deduce1)
   25728                 :             :         lose2 = true;
   25729                 :     3920994 :       if (!deduce2)
   25730                 :      312459 :         lose1 = true;
   25731                 :             : 
   25732                 :             :       /* "If, for a given type, deduction succeeds in both directions
   25733                 :             :          (i.e., the types are identical after the transformations above)
   25734                 :             :          and both P and A were reference types (before being replaced with
   25735                 :             :          the type referred to above):
   25736                 :             :          - if the type from the argument template was an lvalue reference and
   25737                 :             :          the type from the parameter template was not, the argument type is
   25738                 :             :          considered to be more specialized than the other; otherwise,
   25739                 :             :          - if the type from the argument template is more cv-qualified
   25740                 :             :          than the type from the parameter template (as described above),
   25741                 :             :          the argument type is considered to be more specialized than the other;
   25742                 :             :          otherwise,
   25743                 :             :          - neither type is more specialized than the other."  */
   25744                 :             : 
   25745                 :     3920994 :       if (deduce1 && deduce2)
   25746                 :             :         {
   25747                 :     1633279 :           if (ref1 && ref2 && ref1 != ref2)
   25748                 :             :             {
   25749                 :           3 :               if (ref1 > ref2)
   25750                 :             :                 lose1 = true;
   25751                 :             :               else
   25752                 :           3 :                 lose2 = true;
   25753                 :             :             }
   25754                 :     1633276 :           else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
   25755                 :             :             {
   25756                 :         975 :               if ((quals1 & quals2) == quals2)
   25757                 :           8 :                 lose2 = true;
   25758                 :         975 :               if ((quals1 & quals2) == quals1)
   25759                 :         967 :                 lose1 = true;
   25760                 :             :             }
   25761                 :             :         }
   25762                 :             : 
   25763                 :     3920994 :       if (lose1 && lose2)
   25764                 :             :         /* We've failed to deduce something in either direction.
   25765                 :             :            These must be unordered.  */
   25766                 :             :         break;
   25767                 :             : 
   25768                 :     3905068 :       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
   25769                 :     3904076 :           || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
   25770                 :             :         /* We have already processed all of the arguments in our
   25771                 :             :            handing of the pack expansion type.  */
   25772                 :        1232 :         len = 0;
   25773                 :             : 
   25774                 :     3905068 :       args1 = TREE_CHAIN (args1);
   25775                 :     3905068 :       args2 = TREE_CHAIN (args2);
   25776                 :             :     }
   25777                 :             : 
   25778                 :             :   /* "In most cases, all template parameters must have values in order for
   25779                 :             :      deduction to succeed, but for partial ordering purposes a template
   25780                 :             :      parameter may remain without a value provided it is not used in the
   25781                 :             :      types being used for partial ordering."
   25782                 :             : 
   25783                 :             :      Thus, if we are missing any of the targs1 we need to substitute into
   25784                 :             :      origs1, then pat2 is not as specialized as pat1.  This can happen when
   25785                 :             :      there is a nondeduced context.  */
   25786                 :     2841585 :   if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
   25787                 :             :     lose2 = true;
   25788                 :     2841585 :   if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
   25789                 :             :     lose1 = true;
   25790                 :             : 
   25791                 :     2841585 :   processing_template_decl--;
   25792                 :             : 
   25793                 :             :   /* If both deductions succeed, the partial ordering selects the more
   25794                 :             :      constrained template.  */
   25795                 :             :   /* P2113: If the corresponding template-parameters of the
   25796                 :             :      template-parameter-lists are not equivalent ([temp.over.link]) or if
   25797                 :             :      the function parameters that positionally correspond between the two
   25798                 :             :      templates are not of the same type, neither template is more
   25799                 :             :      specialized than the other.  */
   25800                 :     2841585 :   if (!lose1 && !lose2
   25801                 :      570469 :       && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
   25802                 :      570469 :                               DECL_TEMPLATE_PARMS (pat2))
   25803                 :     3411865 :       && compparms (origs1, origs2))
   25804                 :             :     {
   25805                 :      564315 :       int winner = more_constrained (decl1, decl2);
   25806                 :      564315 :       if (winner > 0)
   25807                 :             :         lose2 = true;
   25808                 :      433241 :       else if (winner < 0)
   25809                 :         106 :         lose1 = true;
   25810                 :             :     }
   25811                 :             : 
   25812                 :             :   /* All things being equal, if the next argument is a pack expansion
   25813                 :             :      for one function but not for the other, prefer the
   25814                 :             :      non-variadic function.  FIXME this is bogus; see c++/41958.  */
   25815                 :     2841585 :   if (lose1 == lose2
   25816                 :      455224 :       && args1 && TREE_VALUE (args1)
   25817                 :     3296801 :       && args2 && TREE_VALUE (args2))
   25818                 :             :     {
   25819                 :      455211 :       lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
   25820                 :      455211 :       lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
   25821                 :             :     }
   25822                 :             : 
   25823                 :     2841585 :   if (lose1 == lose2)
   25824                 :             :     return 0;
   25825                 :     2387783 :   else if (!lose1)
   25826                 :             :     return 1;
   25827                 :             :   else
   25828                 :      291031 :     return -1;
   25829                 :             : }
   25830                 :             : 
   25831                 :             : /* Determine which of two partial specializations of TMPL is more
   25832                 :             :    specialized.
   25833                 :             : 
   25834                 :             :    PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
   25835                 :             :    to the first partial specialization.  The TREE_PURPOSE is the
   25836                 :             :    innermost set of template parameters for the partial
   25837                 :             :    specialization.  PAT2 is similar, but for the second template.
   25838                 :             : 
   25839                 :             :    Return 1 if the first partial specialization is more specialized;
   25840                 :             :    -1 if the second is more specialized; 0 if neither is more
   25841                 :             :    specialized.
   25842                 :             : 
   25843                 :             :    See [temp.class.order] for information about determining which of
   25844                 :             :    two templates is more specialized.  */
   25845                 :             : 
   25846                 :             : static int
   25847                 :      536272 : more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
   25848                 :             : {
   25849                 :      536272 :   tree targs;
   25850                 :      536272 :   int winner = 0;
   25851                 :      536272 :   bool any_deductions = false;
   25852                 :             : 
   25853                 :      536272 :   tree tmpl1 = TREE_VALUE (pat1);
   25854                 :      536272 :   tree tmpl2 = TREE_VALUE (pat2);
   25855                 :      536272 :   tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
   25856                 :      536272 :   tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
   25857                 :             : 
   25858                 :             :   /* Just like what happens for functions, if we are ordering between
   25859                 :             :      different template specializations, we may encounter dependent
   25860                 :             :      types in the arguments, and we need our dependency check functions
   25861                 :             :      to behave correctly.  */
   25862                 :      536272 :   ++processing_template_decl;
   25863                 :      536272 :   targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
   25864                 :      536272 :   if (targs)
   25865                 :             :     {
   25866                 :      141139 :       --winner;
   25867                 :      141139 :       any_deductions = true;
   25868                 :             :     }
   25869                 :             : 
   25870                 :      536272 :   targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
   25871                 :      536272 :   if (targs)
   25872                 :             :     {
   25873                 :      400593 :       ++winner;
   25874                 :      400593 :       any_deductions = true;
   25875                 :             :     }
   25876                 :      536272 :   --processing_template_decl;
   25877                 :             : 
   25878                 :             :   /* If both deductions succeed, the partial ordering selects the more
   25879                 :             :      constrained template.  */
   25880                 :      536272 :   if (!winner && any_deductions)
   25881                 :        5541 :     winner = more_constrained (tmpl1, tmpl2);
   25882                 :             : 
   25883                 :             :   /* In the case of a tie where at least one of the templates
   25884                 :             :      has a parameter pack at the end, the template with the most
   25885                 :             :      non-packed parameters wins.  */
   25886                 :      536272 :   if (winner == 0
   25887                 :      536272 :       && any_deductions
   25888                 :      536276 :       && (template_args_variadic_p (TREE_PURPOSE (pat1))
   25889                 :           4 :           || template_args_variadic_p (TREE_PURPOSE (pat2))))
   25890                 :             :     {
   25891                 :           0 :       tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
   25892                 :           0 :       tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
   25893                 :           0 :       int len1 = TREE_VEC_LENGTH (args1);
   25894                 :           0 :       int len2 = TREE_VEC_LENGTH (args2);
   25895                 :             : 
   25896                 :             :       /* We don't count the pack expansion at the end.  */
   25897                 :           0 :       if (template_args_variadic_p (TREE_PURPOSE (pat1)))
   25898                 :           0 :         --len1;
   25899                 :           0 :       if (template_args_variadic_p (TREE_PURPOSE (pat2)))
   25900                 :           0 :         --len2;
   25901                 :             : 
   25902                 :           0 :       if (len1 > len2)
   25903                 :             :         return 1;
   25904                 :           0 :       else if (len1 < len2)
   25905                 :             :         return -1;
   25906                 :             :     }
   25907                 :             : 
   25908                 :             :   return winner;
   25909                 :             : }
   25910                 :             : 
   25911                 :             : /* Return the template arguments that will produce the function signature
   25912                 :             :    DECL from the function template FN, with the explicit template
   25913                 :             :    arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is true, the return type must
   25914                 :             :    also match.  Return NULL_TREE if no satisfactory arguments could be
   25915                 :             :    found.  */
   25916                 :             : 
   25917                 :             : static tree
   25918                 :    11408614 : get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
   25919                 :             : {
   25920                 :    11408614 :   int ntparms = DECL_NTPARMS (fn);
   25921                 :    11408614 :   tree targs = make_tree_vec (ntparms);
   25922                 :    11408614 :   tree decl_type = TREE_TYPE (decl);
   25923                 :    11408614 :   tree decl_arg_types;
   25924                 :    11408614 :   tree *args;
   25925                 :    11408614 :   unsigned int nargs, ix;
   25926                 :    11408614 :   tree arg;
   25927                 :             : 
   25928                 :    11408614 :   gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
   25929                 :             : 
   25930                 :             :   /* Never do unification on the 'this' parameter.  */
   25931                 :    11408614 :   decl_arg_types = skip_artificial_parms_for (decl,
   25932                 :    11408614 :                                               TYPE_ARG_TYPES (decl_type));
   25933                 :             : 
   25934                 :    11408614 :   nargs = list_length (decl_arg_types);
   25935                 :    11408614 :   args = XALLOCAVEC (tree, nargs);
   25936                 :    11408614 :   for (arg = decl_arg_types, ix = 0;
   25937                 :    44660916 :        arg != NULL_TREE;
   25938                 :    33252302 :        arg = TREE_CHAIN (arg), ++ix)
   25939                 :    33252302 :     args[ix] = TREE_VALUE (arg);
   25940                 :             : 
   25941                 :    22817228 :   if (fn_type_unification (fn, explicit_args, targs,
   25942                 :             :                            args, ix,
   25943                 :           0 :                            (check_rettype || DECL_CONV_FN_P (fn)
   25944                 :    11408614 :                             ? TREE_TYPE (decl_type) : NULL_TREE),
   25945                 :             :                            DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
   25946                 :             :                            /*explain_p=*/false,
   25947                 :             :                            /*decltype*/false)
   25948                 :    11408614 :       == error_mark_node)
   25949                 :     9310744 :     return NULL_TREE;
   25950                 :             : 
   25951                 :             :   return targs;
   25952                 :             : }
   25953                 :             : 
   25954                 :             : /* Return the innermost template arguments that, when applied to a partial
   25955                 :             :    specialization SPEC_TMPL of TMPL, yield the ARGS.
   25956                 :             : 
   25957                 :             :    For example, suppose we have:
   25958                 :             : 
   25959                 :             :      template <class T, class U> struct S {};
   25960                 :             :      template <class T> struct S<T*, int> {};
   25961                 :             : 
   25962                 :             :    Then, suppose we want to get `S<double*, int>'.  SPEC_TMPL will be the
   25963                 :             :    partial specialization and the ARGS will be {double*, int}.  The resulting
   25964                 :             :    vector will be {double}, indicating that `T' is bound to `double'.  */
   25965                 :             : 
   25966                 :             : static tree
   25967                 :    29467299 : get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
   25968                 :             : {
   25969                 :    29467299 :   tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
   25970                 :    29467299 :   tree spec_args
   25971                 :    29467299 :     = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
   25972                 :    29467299 :   int i, ntparms = TREE_VEC_LENGTH (tparms);
   25973                 :    29467299 :   tree deduced_args;
   25974                 :    29467299 :   tree innermost_deduced_args;
   25975                 :             : 
   25976                 :    29467299 :   innermost_deduced_args = make_tree_vec (ntparms);
   25977                 :    58934598 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   25978                 :             :     {
   25979                 :         244 :       deduced_args = copy_node (args);
   25980                 :         488 :       SET_TMPL_ARGS_LEVEL (deduced_args,
   25981                 :             :                            TMPL_ARGS_DEPTH (deduced_args),
   25982                 :             :                            innermost_deduced_args);
   25983                 :             :     }
   25984                 :             :   else
   25985                 :             :     deduced_args = innermost_deduced_args;
   25986                 :             : 
   25987                 :    29467299 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   25988                 :    29467301 :  again:
   25989                 :    29467301 :   if (unify (tparms, deduced_args,
   25990                 :             :              INNERMOST_TEMPLATE_ARGS (spec_args),
   25991                 :             :              INNERMOST_TEMPLATE_ARGS (args),
   25992                 :             :              UNIFY_ALLOW_NONE, /*explain_p=*/false))
   25993                 :             :     return NULL_TREE;
   25994                 :             : 
   25995                 :    45639820 :   for (i =  0; i < ntparms; ++i)
   25996                 :    30138247 :     if (! TREE_VEC_ELT (innermost_deduced_args, i))
   25997                 :             :       {
   25998                 :          27 :         if (!tried_array_deduction)
   25999                 :             :           {
   26000                 :          18 :             try_array_deduction (tparms, innermost_deduced_args,
   26001                 :             :                                  INNERMOST_TEMPLATE_ARGS (spec_args));
   26002                 :          18 :             tried_array_deduction = true;
   26003                 :          18 :             if (TREE_VEC_ELT (innermost_deduced_args, i))
   26004                 :           2 :               goto again;
   26005                 :             :           }
   26006                 :          25 :         return NULL_TREE;
   26007                 :             :       }
   26008                 :             : 
   26009                 :    15501573 :   if (!push_tinst_level (spec_tmpl, deduced_args))
   26010                 :             :     {
   26011                 :           0 :       excessive_deduction_depth = true;
   26012                 :           0 :       return NULL_TREE;
   26013                 :             :     }
   26014                 :             : 
   26015                 :             :   /* Verify that nondeduced template arguments agree with the type
   26016                 :             :      obtained from argument deduction.
   26017                 :             : 
   26018                 :             :      For example:
   26019                 :             : 
   26020                 :             :        struct A { typedef int X; };
   26021                 :             :        template <class T, class U> struct C {};
   26022                 :             :        template <class T> struct C<T, typename T::X> {};
   26023                 :             : 
   26024                 :             :      Then with the instantiation `C<A, int>', we can deduce that
   26025                 :             :      `T' is `A' but unify () does not check whether `typename T::X'
   26026                 :             :      is `int'.  */
   26027                 :    15501573 :   spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
   26028                 :             : 
   26029                 :    15501573 :   if (spec_args != error_mark_node)
   26030                 :    15074323 :     spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
   26031                 :             :                                        INNERMOST_TEMPLATE_ARGS (spec_args),
   26032                 :             :                                        tmpl, tf_none, false);
   26033                 :             : 
   26034                 :    15501573 :   pop_tinst_level ();
   26035                 :             : 
   26036                 :    15501573 :   if (spec_args == error_mark_node
   26037                 :             :       /* We only need to check the innermost arguments; the other
   26038                 :             :          arguments will always agree.  */
   26039                 :    15501573 :       || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
   26040                 :             :                                      INNERMOST_TEMPLATE_ARGS (args)))
   26041                 :     1921975 :     return NULL_TREE;
   26042                 :             : 
   26043                 :             :   /* Now that we have bindings for all of the template arguments,
   26044                 :             :      ensure that the arguments deduced for the template template
   26045                 :             :      parameters have compatible template parameter lists.  See the use
   26046                 :             :      of template_template_parm_bindings_ok_p in fn_type_unification
   26047                 :             :      for more information.  */
   26048                 :    13579598 :   if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
   26049                 :             :     return NULL_TREE;
   26050                 :             : 
   26051                 :             :   return deduced_args;
   26052                 :             : }
   26053                 :             : 
   26054                 :             : // Compare two function templates T1 and T2 by deducing bindings
   26055                 :             : // from one against the other. If both deductions succeed, compare
   26056                 :             : // constraints to see which is more constrained.
   26057                 :             : static int
   26058                 :       61791 : more_specialized_inst (tree t1, tree t2)
   26059                 :             : {
   26060                 :       61791 :   int fate = 0;
   26061                 :       61791 :   int count = 0;
   26062                 :             : 
   26063                 :       61791 :   if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
   26064                 :             :     {
   26065                 :         148 :       --fate;
   26066                 :         148 :       ++count;
   26067                 :             :     }
   26068                 :             : 
   26069                 :       61791 :   if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
   26070                 :             :     {
   26071                 :       41177 :       ++fate;
   26072                 :       41177 :       ++count;
   26073                 :             :     }
   26074                 :             : 
   26075                 :             :   // If both deductions succeed, then one may be more constrained.
   26076                 :       61791 :   if (count == 2 && fate == 0)
   26077                 :          66 :     fate = more_constrained (t1, t2);
   26078                 :             : 
   26079                 :       61791 :   return fate;
   26080                 :             : }
   26081                 :             : 
   26082                 :             : /* TEMPLATES is a TREE_LIST.  Each TREE_VALUE is a TEMPLATE_DECL.
   26083                 :             :    Return the TREE_LIST node with the most specialized template, if
   26084                 :             :    any.  If there is no most specialized template, the error_mark_node
   26085                 :             :    is returned.
   26086                 :             : 
   26087                 :             :    Note that this function does not look at, or modify, the
   26088                 :             :    TREE_PURPOSE or TREE_TYPE of any of the nodes.  Since the node
   26089                 :             :    returned is one of the elements of INSTANTIATIONS, callers may
   26090                 :             :    store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
   26091                 :             :    and retrieve it from the value returned.  */
   26092                 :             : 
   26093                 :             : tree
   26094                 :       52533 : most_specialized_instantiation (tree templates)
   26095                 :             : {
   26096                 :       52533 :   tree fn, champ;
   26097                 :             : 
   26098                 :       52533 :   ++processing_template_decl;
   26099                 :             : 
   26100                 :       52533 :   champ = templates;
   26101                 :       73179 :   for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
   26102                 :             :     {
   26103                 :       20680 :       gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
   26104                 :       20680 :       int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
   26105                 :       20680 :       if (fate == -1)
   26106                 :             :         champ = fn;
   26107                 :       20565 :       else if (!fate)
   26108                 :             :         {
   26109                 :             :           /* Equally specialized, move to next function.  If there
   26110                 :             :              is no next function, nothing's most specialized.  */
   26111                 :       20532 :           fn = TREE_CHAIN (fn);
   26112                 :       20532 :           champ = fn;
   26113                 :       20532 :           if (!fn)
   26114                 :             :             break;
   26115                 :             :         }
   26116                 :             :     }
   26117                 :             : 
   26118                 :       52533 :   if (champ)
   26119                 :             :     /* Now verify that champ is better than everything earlier in the
   26120                 :             :        instantiation list.  */
   26121                 :       93610 :     for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
   26122                 :       41111 :       if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
   26123                 :             :       {
   26124                 :             :         champ = NULL_TREE;
   26125                 :             :         break;
   26126                 :             :       }
   26127                 :             :     }
   26128                 :             : 
   26129                 :       52533 :   processing_template_decl--;
   26130                 :             : 
   26131                 :       52533 :   if (!champ)
   26132                 :          34 :     return error_mark_node;
   26133                 :             : 
   26134                 :             :   return champ;
   26135                 :             : }
   26136                 :             : 
   26137                 :             : /* If DECL is a specialization of some template, return the most
   26138                 :             :    general such template.  Otherwise, returns NULL_TREE.
   26139                 :             : 
   26140                 :             :    For example, given:
   26141                 :             : 
   26142                 :             :      template <class T> struct S { template <class U> void f(U); };
   26143                 :             : 
   26144                 :             :    if TMPL is `template <class U> void S<int>::f(U)' this will return
   26145                 :             :    the full template.  This function will not trace past partial
   26146                 :             :    specializations, however.  For example, given in addition:
   26147                 :             : 
   26148                 :             :      template <class T> struct S<T*> { template <class U> void f(U); };
   26149                 :             : 
   26150                 :             :    if TMPL is `template <class U> void S<int*>::f(U)' this will return
   26151                 :             :    `template <class T> template <class U> S<T*>::f(U)'.  */
   26152                 :             : 
   26153                 :             : tree
   26154                 :  2834313298 : most_general_template (const_tree decl)
   26155                 :             : {
   26156                 :  2834313298 :   if (TREE_CODE (decl) != TEMPLATE_DECL)
   26157                 :             :     {
   26158                 :   533404585 :       if (tree tinfo = get_template_info (decl))
   26159                 :   343488562 :         decl = TI_TEMPLATE (tinfo);
   26160                 :             :       /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
   26161                 :             :          template friend, or a FIELD_DECL for a capture pack.  */
   26162                 :   533404585 :       if (TREE_CODE (decl) != TEMPLATE_DECL)
   26163                 :             :         return NULL_TREE;
   26164                 :             :     }
   26165                 :             : 
   26166                 :  2644397069 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
   26167                 :      348185 :     return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl));
   26168                 :             : 
   26169                 :             :   /* Look for more and more general templates.  */
   26170                 :  2674600429 :   while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
   26171                 :             :     {
   26172                 :             :       /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
   26173                 :             :          (See cp-tree.h for details.)  */
   26174                 :    30551951 :       if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
   26175                 :             :         break;
   26176                 :             : 
   26177                 :    61103902 :       if (CLASS_TYPE_P (TREE_TYPE (decl))
   26178                 :     6483955 :           && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
   26179                 :    36424902 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   26180                 :             :         break;
   26181                 :             : 
   26182                 :             :       /* Stop if we run into an explicitly specialized class template.  */
   26183                 :    61103090 :       if (!DECL_NAMESPACE_SCOPE_P (decl)
   26184                 :    30450534 :           && DECL_CONTEXT (decl)
   26185                 :    61002079 :           && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
   26186                 :             :         break;
   26187                 :             : 
   26188                 :    30551545 :       decl = DECL_TI_TEMPLATE (decl);
   26189                 :             :     }
   26190                 :             : 
   26191                 :             :   return CONST_CAST_TREE (decl);
   26192                 :             : }
   26193                 :             : 
   26194                 :             : /* Return the most specialized of the template partial specializations
   26195                 :             :    which can produce TARGET, a specialization of some class or variable
   26196                 :             :    template.  The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a
   26197                 :             :    TEMPLATE_DECL node corresponding to the partial specialization, while
   26198                 :             :    the TI_ARGS is the set of template arguments that must be substituted
   26199                 :             :    into the template pattern in order to generate TARGET.  The result is
   26200                 :             :    cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless
   26201                 :             :    RECHECKING is true.
   26202                 :             : 
   26203                 :             :    If the choice of partial specialization is ambiguous, a diagnostic
   26204                 :             :    is issued, and the error_mark_node is returned.  If there are no
   26205                 :             :    partial specializations matching TARGET, then NULL_TREE is
   26206                 :             :    returned, indicating that the primary template should be used.  */
   26207                 :             : 
   26208                 :             : tree
   26209                 :    33841669 : most_specialized_partial_spec (tree target, tsubst_flags_t complain,
   26210                 :             :                                bool rechecking /* = false */)
   26211                 :             : {
   26212                 :    33841669 :   tree tinfo = NULL_TREE;
   26213                 :    33841669 :   tree tmpl, args, decl;
   26214                 :    33841669 :   if (TYPE_P (target))
   26215                 :             :     {
   26216                 :    27938221 :       tinfo = CLASSTYPE_TEMPLATE_INFO (target);
   26217                 :    27938221 :       tmpl = TI_TEMPLATE (tinfo);
   26218                 :    27938221 :       args = TI_ARGS (tinfo);
   26219                 :    27938221 :       decl = TYPE_NAME (target);
   26220                 :             :     }
   26221                 :     5903448 :   else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
   26222                 :             :     {
   26223                 :     2956771 :       tmpl = TREE_OPERAND (target, 0);
   26224                 :     2956771 :       args = TREE_OPERAND (target, 1);
   26225                 :     2956771 :       decl = DECL_TEMPLATE_RESULT (tmpl);
   26226                 :             :     }
   26227                 :     2946677 :   else if (VAR_P (target))
   26228                 :             :     {
   26229                 :     2946677 :       tinfo = DECL_TEMPLATE_INFO (target);
   26230                 :     2946677 :       tmpl = TI_TEMPLATE (tinfo);
   26231                 :     2946677 :       args = TI_ARGS (tinfo);
   26232                 :     2946677 :       decl = target;
   26233                 :             :     }
   26234                 :             :   else
   26235                 :           0 :     gcc_unreachable ();
   26236                 :             : 
   26237                 :    33841669 :   if (!PRIMARY_TEMPLATE_P (tmpl))
   26238                 :             :     return NULL_TREE;
   26239                 :             : 
   26240                 :    32841823 :   if (!rechecking
   26241                 :    32841823 :       && tinfo
   26242                 :    32841823 :       && (VAR_P (target) || COMPLETE_TYPE_P (target)))
   26243                 :     2946682 :     return TI_PARTIAL_INFO (tinfo);
   26244                 :             : 
   26245                 :    29895141 :   tree main_tmpl = most_general_template (tmpl);
   26246                 :    29895141 :   tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl);
   26247                 :    29895141 :   if (!specs)
   26248                 :             :     /* There are no partial specializations of this template.  */
   26249                 :             :     return NULL_TREE;
   26250                 :             : 
   26251                 :     8738872 :   push_access_scope_guard pas (decl);
   26252                 :     8738872 :   deferring_access_check_sentinel acs (dk_no_deferred);
   26253                 :             : 
   26254                 :             :   /* For determining which partial specialization to use, only the
   26255                 :             :      innermost args are interesting.  */
   26256                 :     8738872 :   tree outer_args = NULL_TREE;
   26257                 :    17477744 :   if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
   26258                 :             :     {
   26259                 :      147961 :       outer_args = strip_innermost_template_args (args, 1);
   26260                 :      147961 :       args = INNERMOST_TEMPLATE_ARGS (args);
   26261                 :             :     }
   26262                 :             : 
   26263                 :             :   /* The caller hasn't called push_to_top_level yet, but we need
   26264                 :             :      get_partial_spec_bindings to be done in non-template context so that we'll
   26265                 :             :      fully resolve everything.  */
   26266                 :     8738872 :   processing_template_decl_sentinel ptds;
   26267                 :             : 
   26268                 :     8738872 :   tree list = NULL_TREE;
   26269                 :    30434171 :   for (tree t = specs; t; t = TREE_CHAIN (t))
   26270                 :             :     {
   26271                 :    21695307 :       const tree ospec_tmpl = TREE_VALUE (t);
   26272                 :             : 
   26273                 :    21695307 :       tree spec_tmpl;
   26274                 :    21695307 :       if (outer_args)
   26275                 :             :         {
   26276                 :             :           /* Substitute in the template args from the enclosing class.  */
   26277                 :      169782 :           ++processing_template_decl;
   26278                 :      169782 :           spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
   26279                 :      169782 :           --processing_template_decl;
   26280                 :      169782 :           if (spec_tmpl == error_mark_node)
   26281                 :           8 :             return error_mark_node;
   26282                 :             :         }
   26283                 :             :       else
   26284                 :             :         spec_tmpl = ospec_tmpl;
   26285                 :             : 
   26286                 :    21695299 :       tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
   26287                 :    21695299 :       if (spec_args)
   26288                 :             :         {
   26289                 :     6338415 :           if (outer_args)
   26290                 :      102268 :             spec_args = add_to_template_args (outer_args, spec_args);
   26291                 :             : 
   26292                 :             :           /* Keep the candidate only if its constraints are satisfied.  */
   26293                 :     6338415 :           if (constraints_satisfied_p (ospec_tmpl, spec_args))
   26294                 :     5750158 :             list = tree_cons (spec_args, ospec_tmpl, list);
   26295                 :             :         }
   26296                 :             :     }
   26297                 :             : 
   26298                 :     8738864 :   if (! list)
   26299                 :             :     return NULL_TREE;
   26300                 :             : 
   26301                 :     5391235 :   tree champ = list;
   26302                 :     5391235 :   bool ambiguous_p = false;
   26303                 :     5750073 :   for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t))
   26304                 :             :     {
   26305                 :      358851 :       int fate = more_specialized_partial_spec (tmpl, champ, t);
   26306                 :      358851 :       if (fate == 1)
   26307                 :             :         ;
   26308                 :             :       else
   26309                 :             :         {
   26310                 :      136124 :           if (fate == 0)
   26311                 :             :             {
   26312                 :          85 :               t = TREE_CHAIN (t);
   26313                 :          85 :               if (! t)
   26314                 :             :                 {
   26315                 :             :                   ambiguous_p = true;
   26316                 :             :                   break;
   26317                 :             :                 }
   26318                 :             :             }
   26319                 :             :           champ = t;
   26320                 :             :         }
   26321                 :             :     }
   26322                 :             : 
   26323                 :     5391235 :   if (!ambiguous_p)
   26324                 :     5568640 :     for (tree t = list; t && t != champ; t = TREE_CHAIN (t))
   26325                 :             :       {
   26326                 :      177421 :         int fate = more_specialized_partial_spec (tmpl, champ, t);
   26327                 :      177421 :         if (fate != 1)
   26328                 :             :           {
   26329                 :             :             ambiguous_p = true;
   26330                 :             :             break;
   26331                 :             :           }
   26332                 :             :       }
   26333                 :             : 
   26334                 :     5391222 :   if (ambiguous_p)
   26335                 :             :     {
   26336                 :          16 :       const char *str;
   26337                 :          16 :       char *spaces = NULL;
   26338                 :          16 :       if (!(complain & tf_error))
   26339                 :           4 :         return error_mark_node;
   26340                 :          12 :       if (TYPE_P (target))
   26341                 :           9 :         error ("ambiguous template instantiation for %q#T", target);
   26342                 :             :       else
   26343                 :           3 :         error ("ambiguous template instantiation for %q#D", target);
   26344                 :          12 :       str = ngettext ("candidate is:", "candidates are:", list_length (list));
   26345                 :          45 :       for (tree t = list; t; t = TREE_CHAIN (t))
   26346                 :             :         {
   26347                 :          33 :           tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
   26348                 :          66 :           inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
   26349                 :             :                   "%s %#qS", spaces ? spaces : str, subst);
   26350                 :          33 :           spaces = spaces ? spaces : get_spaces (str);
   26351                 :             :         }
   26352                 :          12 :       free (spaces);
   26353                 :          12 :       return error_mark_node;
   26354                 :             :     }
   26355                 :             : 
   26356                 :     5391219 :   tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ));
   26357                 :     5391219 :   if (!rechecking && tinfo)
   26358                 :     5133151 :     TI_PARTIAL_INFO (tinfo) = result;
   26359                 :             :   return result;
   26360                 :    17477744 : }
   26361                 :             : 
   26362                 :             : /* Explicitly instantiate DECL.  */
   26363                 :             : 
   26364                 :             : void
   26365                 :     1908981 : do_decl_instantiation (tree decl, tree storage)
   26366                 :             : {
   26367                 :     1908981 :   tree result = NULL_TREE;
   26368                 :     1908981 :   int extern_p = 0;
   26369                 :             : 
   26370                 :     1908981 :   if (!decl || decl == error_mark_node)
   26371                 :             :     /* An error occurred, for which grokdeclarator has already issued
   26372                 :             :        an appropriate message.  */
   26373                 :             :     return;
   26374                 :     1908805 :   else if (! DECL_LANG_SPECIFIC (decl))
   26375                 :             :     {
   26376                 :           0 :       error ("explicit instantiation of non-template %q#D", decl);
   26377                 :           0 :       return;
   26378                 :             :     }
   26379                 :     1908805 :   else if (DECL_DECLARED_CONCEPT_P (decl))
   26380                 :             :     {
   26381                 :           2 :       if (VAR_P (decl))
   26382                 :           1 :         error ("explicit instantiation of variable concept %q#D", decl);
   26383                 :             :       else
   26384                 :           1 :         error ("explicit instantiation of function concept %q#D", decl);
   26385                 :           2 :       return;
   26386                 :             :     }
   26387                 :             : 
   26388                 :     1908803 :   bool var_templ = (DECL_TEMPLATE_INFO (decl)
   26389                 :     1908803 :                     && variable_template_p (DECL_TI_TEMPLATE (decl)));
   26390                 :             : 
   26391                 :     1908803 :   if (VAR_P (decl) && !var_templ)
   26392                 :             :     {
   26393                 :             :       /* There is an asymmetry here in the way VAR_DECLs and
   26394                 :             :          FUNCTION_DECLs are handled by grokdeclarator.  In the case of
   26395                 :             :          the latter, the DECL we get back will be marked as a
   26396                 :             :          template instantiation, and the appropriate
   26397                 :             :          DECL_TEMPLATE_INFO will be set up.  This does not happen for
   26398                 :             :          VAR_DECLs so we do the lookup here.  Probably, grokdeclarator
   26399                 :             :          should handle VAR_DECLs as it currently handles
   26400                 :             :          FUNCTION_DECLs.  */
   26401                 :          47 :       if (!DECL_CLASS_SCOPE_P (decl))
   26402                 :             :         {
   26403                 :           0 :           error ("%qD is not a static data member of a class template", decl);
   26404                 :           0 :           return;
   26405                 :             :         }
   26406                 :          47 :       result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
   26407                 :          47 :       if (!result || !VAR_P (result))
   26408                 :             :         {
   26409                 :           0 :           error ("no matching template for %qD found", decl);
   26410                 :           0 :           return;
   26411                 :             :         }
   26412                 :          47 :       if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
   26413                 :             :         {
   26414                 :          12 :           error ("type %qT for explicit instantiation %qD does not match "
   26415                 :           4 :                  "declared type %qT", TREE_TYPE (result), decl,
   26416                 :           4 :                  TREE_TYPE (decl));
   26417                 :           4 :           return;
   26418                 :             :         }
   26419                 :             :     }
   26420                 :     1908756 :   else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
   26421                 :             :     {
   26422                 :           0 :       error ("explicit instantiation of %q#D", decl);
   26423                 :           0 :       return;
   26424                 :             :     }
   26425                 :             :   else
   26426                 :             :     result = decl;
   26427                 :             : 
   26428                 :             :   /* Check for various error cases.  Note that if the explicit
   26429                 :             :      instantiation is valid the RESULT will currently be marked as an
   26430                 :             :      *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
   26431                 :             :      until we get here.  */
   26432                 :             : 
   26433                 :     1908799 :   if (DECL_TEMPLATE_SPECIALIZATION (result))
   26434                 :             :     {
   26435                 :             :       /* DR 259 [temp.spec].
   26436                 :             : 
   26437                 :             :          Both an explicit instantiation and a declaration of an explicit
   26438                 :             :          specialization shall not appear in a program unless the explicit
   26439                 :             :          instantiation follows a declaration of the explicit specialization.
   26440                 :             : 
   26441                 :             :          For a given set of template parameters, if an explicit
   26442                 :             :          instantiation of a template appears after a declaration of an
   26443                 :             :          explicit specialization for that template, the explicit
   26444                 :             :          instantiation has no effect.  */
   26445                 :             :       return;
   26446                 :             :     }
   26447                 :     1875076 :   else if (DECL_EXPLICIT_INSTANTIATION (result))
   26448                 :             :     {
   26449                 :             :       /* [temp.spec]
   26450                 :             : 
   26451                 :             :          No program shall explicitly instantiate any template more
   26452                 :             :          than once.
   26453                 :             : 
   26454                 :             :          We check DECL_NOT_REALLY_EXTERN so as not to complain when
   26455                 :             :          the first instantiation was `extern' and the second is not,
   26456                 :             :          and EXTERN_P for the opposite case.  */
   26457                 :         772 :       if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
   26458                 :           8 :         permerror (input_location, "duplicate explicit instantiation of %q#D", result);
   26459                 :             :       /* If an "extern" explicit instantiation follows an ordinary
   26460                 :             :          explicit instantiation, the template is instantiated.  */
   26461                 :             :       if (extern_p)
   26462                 :             :         return;
   26463                 :             :     }
   26464                 :     1874304 :   else if (!DECL_IMPLICIT_INSTANTIATION (result))
   26465                 :             :     {
   26466                 :           0 :       error ("no matching template for %qD found", result);
   26467                 :           0 :       return;
   26468                 :             :     }
   26469                 :     1874304 :   else if (!DECL_TEMPLATE_INFO (result))
   26470                 :             :     {
   26471                 :           0 :       permerror (input_location, "explicit instantiation of non-template %q#D", result);
   26472                 :           0 :       return;
   26473                 :             :     }
   26474                 :             : 
   26475                 :     1875076 :   if (storage == NULL_TREE)
   26476                 :             :     ;
   26477                 :     1871941 :   else if (storage == ridpointers[(int) RID_EXTERN])
   26478                 :             :     {
   26479                 :     1871941 :       if (cxx_dialect == cxx98)
   26480                 :       22127 :         pedwarn (input_location, OPT_Wpedantic,
   26481                 :             :                  "ISO C++ 1998 forbids the use of %<extern%> on explicit "
   26482                 :             :                  "instantiations");
   26483                 :             :       extern_p = 1;
   26484                 :             :     }
   26485                 :             :   else
   26486                 :           0 :     error ("storage class %qD applied to template instantiation", storage);
   26487                 :             : 
   26488                 :     1875076 :   check_explicit_instantiation_namespace (result);
   26489                 :     1875076 :   mark_decl_instantiated (result, extern_p);
   26490                 :     1875076 :   if (! extern_p)
   26491                 :        3135 :     instantiate_decl (result, /*defer_ok=*/true,
   26492                 :             :                       /*expl_inst_class_mem_p=*/false);
   26493                 :             : }
   26494                 :             : 
   26495                 :             : static void
   26496                 :      843026 : mark_class_instantiated (tree t, int extern_p)
   26497                 :             : {
   26498                 :      843026 :   SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
   26499                 :      843026 :   SET_CLASSTYPE_INTERFACE_KNOWN (t);
   26500                 :      843026 :   CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
   26501                 :      843026 :   TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
   26502                 :      843026 :   if (! extern_p)
   26503                 :             :     {
   26504                 :        2724 :       CLASSTYPE_DEBUG_REQUESTED (t) = 1;
   26505                 :        2724 :       rest_of_type_compilation (t, 1);
   26506                 :             :     }
   26507                 :      843026 : }
   26508                 :             : 
   26509                 :             : /* Perform an explicit instantiation of template class T.  STORAGE, if
   26510                 :             :    non-null, is the RID for extern, inline or static.  COMPLAIN is
   26511                 :             :    nonzero if this is called from the parser, zero if called recursively,
   26512                 :             :    since the standard is unclear (as detailed below).  */
   26513                 :             : 
   26514                 :             : void
   26515                 :      888377 : do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
   26516                 :             : {
   26517                 :      888377 :   if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
   26518                 :             :     {
   26519                 :           4 :       if (tree ti = TYPE_TEMPLATE_INFO (t))
   26520                 :           0 :         error ("explicit instantiation of non-class template %qD",
   26521                 :           0 :                TI_TEMPLATE (ti));
   26522                 :             :       else
   26523                 :           4 :         error ("explicit instantiation of non-template type %qT", t);
   26524                 :           4 :       return;
   26525                 :             :     }
   26526                 :             : 
   26527                 :      888373 :   complete_type (t);
   26528                 :             : 
   26529                 :      888373 :   if (!COMPLETE_TYPE_P (t))
   26530                 :             :     {
   26531                 :         194 :       if (complain & tf_error)
   26532                 :           0 :         error ("explicit instantiation of %q#T before definition of template",
   26533                 :             :                t);
   26534                 :         194 :       return;
   26535                 :             :     }
   26536                 :             : 
   26537                 :             :   /* At most one of these will be true.  */
   26538                 :      888179 :   bool extern_p = false;
   26539                 :      888179 :   bool nomem_p = false;
   26540                 :      888179 :   bool static_p = false;
   26541                 :             : 
   26542                 :      888179 :   if (storage != NULL_TREE)
   26543                 :             :     {
   26544                 :      885418 :       if (storage == ridpointers[(int) RID_EXTERN])
   26545                 :             :         {
   26546                 :      885390 :           if (cxx_dialect == cxx98)
   26547                 :        9792 :             pedwarn (input_location, OPT_Wpedantic,
   26548                 :             :                      "ISO C++ 1998 forbids the use of %<extern%> on "
   26549                 :             :                      "explicit instantiations");
   26550                 :             :         }
   26551                 :             :       else
   26552                 :          28 :         pedwarn (input_location, OPT_Wpedantic,
   26553                 :             :                  "ISO C++ forbids the use of %qE"
   26554                 :             :                  " on explicit instantiations", storage);
   26555                 :             : 
   26556                 :      885418 :       if (storage == ridpointers[(int) RID_INLINE])
   26557                 :             :         nomem_p = true;
   26558                 :      885394 :       else if (storage == ridpointers[(int) RID_EXTERN])
   26559                 :             :         extern_p = true;
   26560                 :           4 :       else if (storage == ridpointers[(int) RID_STATIC])
   26561                 :             :         static_p = true;
   26562                 :             :       else
   26563                 :           0 :         error ("storage class %qD applied to template instantiation",
   26564                 :             :                storage);
   26565                 :             :     }
   26566                 :             : 
   26567                 :      888179 :   if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
   26568                 :             :     /* DR 259 [temp.spec].
   26569                 :             : 
   26570                 :             :        Both an explicit instantiation and a declaration of an explicit
   26571                 :             :        specialization shall not appear in a program unless the
   26572                 :             :        explicit instantiation follows a declaration of the explicit
   26573                 :             :        specialization.
   26574                 :             : 
   26575                 :             :        For a given set of template parameters, if an explicit
   26576                 :             :        instantiation of a template appears after a declaration of an
   26577                 :             :        explicit specialization for that template, the explicit
   26578                 :             :        instantiation has no effect.  */
   26579                 :             :     return;
   26580                 :             : 
   26581                 :      843030 :   if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
   26582                 :             :     {
   26583                 :             :       /* We've already instantiated the template.  */
   26584                 :             : 
   26585                 :             :       /* [temp.spec]
   26586                 :             : 
   26587                 :             :          No program shall explicitly instantiate any template more
   26588                 :             :          than once.
   26589                 :             : 
   26590                 :             :          If EXTERN_P then this is ok.  */
   26591                 :           4 :       if (!extern_p && (complain & tf_error))
   26592                 :           4 :         permerror (input_location,
   26593                 :             :                    "duplicate explicit instantiation of %q#T", t);
   26594                 :             : 
   26595                 :           4 :       return;
   26596                 :             :     }
   26597                 :             : 
   26598                 :      843026 :   check_explicit_instantiation_namespace (TYPE_NAME (t));
   26599                 :      843026 :   mark_class_instantiated (t, extern_p);
   26600                 :             : 
   26601                 :      843026 :   if (nomem_p)
   26602                 :             :     return;
   26603                 :             : 
   26604                 :             :   /* In contrast to implicit instantiation, where only the
   26605                 :             :      declarations, and not the definitions, of members are
   26606                 :             :      instantiated, we have here:
   26607                 :             : 
   26608                 :             :          [temp.explicit]
   26609                 :             : 
   26610                 :             :          An explicit instantiation that names a class template
   26611                 :             :          specialization is also an explicit instantiation of the same
   26612                 :             :          kind (declaration or definition) of each of its members (not
   26613                 :             :          including members inherited from base classes and members
   26614                 :             :          that are templates) that has not been previously explicitly
   26615                 :             :          specialized in the translation unit containing the explicit
   26616                 :             :          instantiation, provided that the associated constraints, if
   26617                 :             :          any, of that member are satisfied by the template arguments
   26618                 :             :          of the explicit instantiation.  */
   26619                 :    32549461 :   for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
   26620                 :    31706463 :     if ((VAR_P (fld)
   26621                 :    31366031 :          || (TREE_CODE (fld) == FUNCTION_DECL
   26622                 :    21267654 :              && !static_p
   26623                 :    21267654 :              && user_provided_p (fld)))
   26624                 :    20234168 :         && DECL_TEMPLATE_INSTANTIATION (fld)
   26625                 :    51616016 :         && constraints_satisfied_p (fld))
   26626                 :             :       {
   26627                 :    19909552 :         mark_decl_instantiated (fld, extern_p);
   26628                 :    19909552 :         if (! extern_p)
   26629                 :       28775 :           instantiate_decl (fld, /*defer_ok=*/true,
   26630                 :             :                             /*expl_inst_class_mem_p=*/true);
   26631                 :             :       }
   26632                 :    11796911 :     else if (DECL_IMPLICIT_TYPEDEF_P (fld))
   26633                 :             :       {
   26634                 :      130883 :         tree type = TREE_TYPE (fld);
   26635                 :             : 
   26636                 :      113065 :         if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   26637                 :      243946 :             && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
   26638                 :      113063 :           do_type_instantiation (type, storage, 0);
   26639                 :             :       }
   26640                 :             : }
   26641                 :             : 
   26642                 :             : /* Given a function DECL, which is a specialization of TMPL, modify
   26643                 :             :    DECL to be a re-instantiation of TMPL with the same template
   26644                 :             :    arguments.  TMPL should be the template into which tsubst'ing
   26645                 :             :    should occur for DECL, not the most general template.
   26646                 :             : 
   26647                 :             :    One reason for doing this is a scenario like this:
   26648                 :             : 
   26649                 :             :      template <class T>
   26650                 :             :      void f(const T&, int i);
   26651                 :             : 
   26652                 :             :      void g() { f(3, 7); }
   26653                 :             : 
   26654                 :             :      template <class T>
   26655                 :             :      void f(const T& t, const int i) { }
   26656                 :             : 
   26657                 :             :    Note that when the template is first instantiated, with
   26658                 :             :    instantiate_template, the resulting DECL will have no name for the
   26659                 :             :    first parameter, and the wrong type for the second.  So, when we go
   26660                 :             :    to instantiate the DECL, we regenerate it.  */
   26661                 :             : 
   26662                 :             : static void
   26663                 :    19892877 : regenerate_decl_from_template (tree decl, tree tmpl, tree args)
   26664                 :             : {
   26665                 :             :   /* The arguments used to instantiate DECL, from the most general
   26666                 :             :      template.  */
   26667                 :    19892877 :   tree code_pattern = DECL_TEMPLATE_RESULT (tmpl);
   26668                 :             : 
   26669                 :             :   /* Make sure that we can see identifiers, and compute access correctly.  */
   26670                 :    19892877 :   push_access_scope (decl);
   26671                 :             : 
   26672                 :    19892877 :   if (TREE_CODE (decl) == FUNCTION_DECL)
   26673                 :             :     {
   26674                 :    15973457 :       tree specs;
   26675                 :    15973457 :       int args_depth;
   26676                 :    15973457 :       int parms_depth;
   26677                 :             : 
   26678                 :             :       /* Don't bother with this for unique friends that can't be redeclared and
   26679                 :             :          might change type if regenerated (PR69836).  */
   26680                 :    15973457 :       if (DECL_UNIQUE_FRIEND_P (decl))
   26681                 :       39052 :         goto done;
   26682                 :             : 
   26683                 :             :       /* Use the source location of the definition.  */
   26684                 :    15934405 :       DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
   26685                 :             : 
   26686                 :    31868810 :       args_depth = TMPL_ARGS_DEPTH (args);
   26687                 :    15934405 :       parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   26688                 :    15934405 :       if (args_depth > parms_depth)
   26689                 :           0 :         args = get_innermost_template_args (args, parms_depth);
   26690                 :             : 
   26691                 :             :       /* Instantiate a dynamic exception-specification.  noexcept will be
   26692                 :             :          handled below.  */
   26693                 :    15934405 :       if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
   26694                 :     6622355 :         if (TREE_VALUE (raises))
   26695                 :             :           {
   26696                 :          24 :             specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
   26697                 :             :                                                     args, tf_error, NULL_TREE,
   26698                 :             :                                                     /*defer_ok*/false);
   26699                 :          24 :             if (specs && specs != error_mark_node)
   26700                 :          22 :               TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
   26701                 :             :                                                           specs);
   26702                 :             :           }
   26703                 :             : 
   26704                 :             :       /* Merge parameter declarations.  */
   26705                 :    15934405 :       if (tree pattern_parm
   26706                 :    15934405 :           = skip_artificial_parms_for (code_pattern,
   26707                 :    15934405 :                                        DECL_ARGUMENTS (code_pattern)))
   26708                 :             :         {
   26709                 :    11094051 :           tree *p = &DECL_ARGUMENTS (decl);
   26710                 :    15255526 :           for (int skip = num_artificial_parms_for (decl); skip; --skip)
   26711                 :     4161475 :             p = &DECL_CHAIN (*p);
   26712                 :    11094051 :           *p = tsubst_decl (pattern_parm, args, tf_error);
   26713                 :    31079823 :           for (tree t = *p; t; t = DECL_CHAIN (t))
   26714                 :    19985772 :             DECL_CONTEXT (t) = decl;
   26715                 :             :         }
   26716                 :             : 
   26717                 :    15934405 :       if (DECL_CONTRACTS (decl))
   26718                 :             :         {
   26719                 :             :           /* If we're regenerating a specialization, the contracts will have
   26720                 :             :              been copied from the most general template. Replace those with
   26721                 :             :              the ones from the actual specialization.  */
   26722                 :         146 :           tree tmpl = DECL_TI_TEMPLATE (decl);
   26723                 :         146 :           if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
   26724                 :             :             {
   26725                 :           4 :               remove_contract_attributes (decl);
   26726                 :           4 :               copy_contract_attributes (decl, code_pattern);
   26727                 :             :             }
   26728                 :             : 
   26729                 :         146 :           tsubst_contract_attributes (decl, args, tf_warning_or_error, code_pattern);
   26730                 :             :         }
   26731                 :             : 
   26732                 :             :       /* Merge additional specifiers from the CODE_PATTERN.  */
   26733                 :    15934405 :       if (DECL_DECLARED_INLINE_P (code_pattern)
   26734                 :    30155727 :           && !DECL_DECLARED_INLINE_P (decl))
   26735                 :       56657 :         DECL_DECLARED_INLINE_P (decl) = 1;
   26736                 :             : 
   26737                 :    15934405 :       maybe_instantiate_noexcept (decl, tf_error);
   26738                 :             :     }
   26739                 :     3919420 :   else if (VAR_P (decl))
   26740                 :             :     {
   26741                 :     3919420 :       start_lambda_scope (decl);
   26742                 :     3919420 :       DECL_INITIAL (decl) =
   26743                 :     3919420 :         tsubst_init (DECL_INITIAL (code_pattern), decl, args,
   26744                 :     3919420 :                      tf_error, DECL_TI_TEMPLATE (decl));
   26745                 :     3919420 :       finish_lambda_scope ();
   26746                 :     3919420 :       if (VAR_HAD_UNKNOWN_BOUND (decl))
   26747                 :          74 :         TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
   26748                 :          74 :                                    tf_error, DECL_TI_TEMPLATE (decl));
   26749                 :             :     }
   26750                 :             :   else
   26751                 :           0 :     gcc_unreachable ();
   26752                 :             : 
   26753                 :    19892877 :  done:
   26754                 :    19892877 :   pop_access_scope (decl);
   26755                 :    19892877 : }
   26756                 :             : 
   26757                 :             : /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
   26758                 :             :    substituted to get DECL.  */
   26759                 :             : 
   26760                 :             : tree
   26761                 :    67127244 : template_for_substitution (tree decl)
   26762                 :             : {
   26763                 :    67127244 :   tree tmpl = DECL_TI_TEMPLATE (decl);
   26764                 :             : 
   26765                 :             :   /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
   26766                 :             :      for the instantiation.  This is not always the most general
   26767                 :             :      template.  Consider, for example:
   26768                 :             : 
   26769                 :             :         template <class T>
   26770                 :             :         struct S { template <class U> void f();
   26771                 :             :                    template <> void f<int>(); };
   26772                 :             : 
   26773                 :             :      and an instantiation of S<double>::f<int>.  We want TD to be the
   26774                 :             :      specialization S<T>::f<int>, not the more general S<T>::f<U>.  */
   26775                 :    67127244 :   while (/* An instantiation cannot have a definition, so we need a
   26776                 :             :             more general template.  */
   26777                 :   147802906 :          DECL_TEMPLATE_INSTANTIATION (tmpl)
   26778                 :             :            /* We must also deal with friend templates.  Given:
   26779                 :             : 
   26780                 :             :                 template <class T> struct S {
   26781                 :             :                   template <class U> friend void f() {};
   26782                 :             :                 };
   26783                 :             : 
   26784                 :             :               S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
   26785                 :             :               so far as the language is concerned, but that's still
   26786                 :             :               where we get the pattern for the instantiation from.  On
   26787                 :             :               other hand, if the definition comes outside the class, say:
   26788                 :             : 
   26789                 :             :                 template <class T> struct S {
   26790                 :             :                   template <class U> friend void f();
   26791                 :             :                 };
   26792                 :             :                 template <class U> friend void f() {}
   26793                 :             : 
   26794                 :             :               we don't need to look any further.  That's what the check for
   26795                 :             :               DECL_INITIAL is for.  */
   26796                 :    73901453 :           || (TREE_CODE (decl) == FUNCTION_DECL
   26797                 :    59263061 :               && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
   26798                 :      581598 :               && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
   26799                 :             :     {
   26800                 :             :       /* The present template, TD, should not be a definition.  If it
   26801                 :             :          were a definition, we should be using it!  Note that we
   26802                 :             :          cannot restructure the loop to just keep going until we find
   26803                 :             :          a template with a definition, since that might go too far if
   26804                 :             :          a specialization was declared, but not defined.  */
   26805                 :             : 
   26806                 :             :       /* Fetch the more general template.  */
   26807                 :     6774209 :       tmpl = DECL_TI_TEMPLATE (tmpl);
   26808                 :             :     }
   26809                 :             : 
   26810                 :    67127244 :   return tmpl;
   26811                 :             : }
   26812                 :             : 
   26813                 :             : /* Returns true if we need to instantiate this template instance even if we
   26814                 :             :    know we aren't going to emit it.  */
   26815                 :             : 
   26816                 :             : bool
   26817                 :     2729027 : always_instantiate_p (tree decl)
   26818                 :             : {
   26819                 :             :   /* We always instantiate inline functions so that we can inline them.  An
   26820                 :             :      explicit instantiation declaration prohibits implicit instantiation of
   26821                 :             :      non-inline functions.  With high levels of optimization, we would
   26822                 :             :      normally inline non-inline functions -- but we're not allowed to do
   26823                 :             :      that for "extern template" functions.  Therefore, we check
   26824                 :             :      DECL_DECLARED_INLINE_P, rather than possibly_inlined_p.  */
   26825                 :     2729027 :   return ((TREE_CODE (decl) == FUNCTION_DECL
   26826                 :     2668975 :            && (DECL_DECLARED_INLINE_P (decl)
   26827                 :      114538 :                || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
   26828                 :             :           /* And we need to instantiate static data members so that
   26829                 :             :              their initializers are available in integral constant
   26830                 :             :              expressions.  */
   26831                 :     2843565 :           || (VAR_P (decl)
   26832                 :       60052 :               && decl_maybe_constant_var_p (decl)));
   26833                 :             : }
   26834                 :             : 
   26835                 :             : /* If FN has a noexcept-specifier that hasn't been instantiated yet,
   26836                 :             :    instantiate it now, modifying TREE_TYPE (fn).  Returns false on
   26837                 :             :    error, true otherwise.  */
   26838                 :             : 
   26839                 :             : bool
   26840                 :   301706171 : maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
   26841                 :             : {
   26842                 :   301706171 :   if (fn == error_mark_node)
   26843                 :             :     return false;
   26844                 :             : 
   26845                 :             :   /* Don't instantiate a noexcept-specification from template context.  */
   26846                 :   301706170 :   if (processing_template_decl
   26847                 :   301706170 :       && (!flag_noexcept_type || type_dependent_expression_p (fn)))
   26848                 :      325735 :     return true;
   26849                 :             : 
   26850                 :   301380435 :   tree fntype = TREE_TYPE (fn);
   26851                 :   301380435 :   tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
   26852                 :             : 
   26853                 :   146780742 :   if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec))
   26854                 :   307970879 :       && DECL_MAYBE_DELETED (fn))
   26855                 :             :     {
   26856                 :        8758 :       if (fn == current_function_decl)
   26857                 :             :         /* We're in start_preparsed_function, keep going.  */
   26858                 :             :         return true;
   26859                 :             : 
   26860                 :           5 :       ++function_depth;
   26861                 :           5 :       maybe_synthesize_method (fn);
   26862                 :           5 :       --function_depth;
   26863                 :           5 :       return !DECL_DELETED_FN (fn);
   26864                 :             :     }
   26865                 :             : 
   26866                 :   448152419 :   if (!spec || !TREE_PURPOSE (spec))
   26867                 :             :     return true;
   26868                 :             : 
   26869                 :   145861380 :   tree noex = TREE_PURPOSE (spec);
   26870                 :   145861380 :   if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   26871                 :   145861380 :       && TREE_CODE (noex) != DEFERRED_PARSE)
   26872                 :             :     return true;
   26873                 :             : 
   26874                 :     7667083 :   tree orig_fn = NULL_TREE;
   26875                 :             :   /* For a member friend template we can get a TEMPLATE_DECL.  Let's use
   26876                 :             :      its FUNCTION_DECL for the rest of this function -- push_access_scope
   26877                 :             :      doesn't accept TEMPLATE_DECLs.  */
   26878                 :     7667083 :   if (DECL_FUNCTION_TEMPLATE_P (fn))
   26879                 :             :     {
   26880                 :          67 :       orig_fn = fn;
   26881                 :          67 :       fn = DECL_TEMPLATE_RESULT (fn);
   26882                 :             :     }
   26883                 :             : 
   26884                 :     7667083 :   if (DECL_CLONED_FUNCTION_P (fn))
   26885                 :             :     {
   26886                 :     3667819 :       tree prime = DECL_CLONED_FUNCTION (fn);
   26887                 :     3667819 :       if (!maybe_instantiate_noexcept (prime, complain))
   26888                 :             :         return false;
   26889                 :     3667717 :       spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
   26890                 :             :     }
   26891                 :     3999264 :   else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
   26892                 :             :     {
   26893                 :     3999264 :       static hash_set<tree>* fns = new hash_set<tree>;
   26894                 :     3999264 :       bool added = false;
   26895                 :     3999264 :       if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
   26896                 :             :         {
   26897                 :     3258142 :           spec = get_defaulted_eh_spec (fn, complain);
   26898                 :     3258142 :           if (spec == error_mark_node)
   26899                 :             :             /* This might have failed because of an unparsed DMI, so
   26900                 :             :                let's try again later.  */
   26901                 :             :             return false;
   26902                 :             :         }
   26903                 :      741122 :       else if (!(added = !fns->add (fn)))
   26904                 :             :         {
   26905                 :             :           /* If hash_set::add returns true, the element was already there.  */
   26906                 :           3 :           location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
   26907                 :           3 :                                             DECL_SOURCE_LOCATION (fn));
   26908                 :           3 :           error_at (loc,
   26909                 :             :                     "exception specification of %qD depends on itself",
   26910                 :             :                     fn);
   26911                 :           3 :           spec = noexcept_false_spec;
   26912                 :             :         }
   26913                 :      741119 :       else if (push_tinst_level (fn))
   26914                 :             :         {
   26915                 :      741073 :           const bool push_to_top = maybe_push_to_top_level (fn);
   26916                 :      741073 :           push_access_scope (fn);
   26917                 :      741073 :           push_deferring_access_checks (dk_no_deferred);
   26918                 :      741073 :           input_location = DECL_SOURCE_LOCATION (fn);
   26919                 :             : 
   26920                 :      741073 :           if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   26921                 :      741073 :               && !DECL_LOCAL_DECL_P (fn))
   26922                 :             :             {
   26923                 :             :               /* If needed, set current_class_ptr for the benefit of
   26924                 :             :                  tsubst_copy/PARM_DECL.  */
   26925                 :      356376 :               tree this_parm = DECL_ARGUMENTS (fn);
   26926                 :      356376 :               current_class_ptr = NULL_TREE;
   26927                 :      356376 :               current_class_ref = cp_build_fold_indirect_ref (this_parm);
   26928                 :      356376 :               current_class_ptr = this_parm;
   26929                 :             :             }
   26930                 :             : 
   26931                 :             :           /* If this function is represented by a TEMPLATE_DECL, then
   26932                 :             :              the deferred noexcept-specification might still contain
   26933                 :             :              dependent types, even after substitution.  And we need the
   26934                 :             :              dependency check functions to work in build_noexcept_spec.  */
   26935                 :      741073 :           if (orig_fn)
   26936                 :          67 :             ++processing_template_decl;
   26937                 :             : 
   26938                 :             :           /* Do deferred instantiation of the noexcept-specifier.  */
   26939                 :      741073 :           noex = tsubst_expr (DEFERRED_NOEXCEPT_PATTERN (noex),
   26940                 :      741073 :                               DEFERRED_NOEXCEPT_ARGS (noex),
   26941                 :             :                               tf_warning_or_error, fn);
   26942                 :             :           /* Build up the noexcept-specification.  */
   26943                 :      741073 :           spec = build_noexcept_spec (noex, tf_warning_or_error);
   26944                 :             : 
   26945                 :      741073 :           if (orig_fn)
   26946                 :          67 :             --processing_template_decl;
   26947                 :             : 
   26948                 :      741073 :           pop_deferring_access_checks ();
   26949                 :      741073 :           pop_access_scope (fn);
   26950                 :      741073 :           pop_tinst_level ();
   26951                 :      741073 :           maybe_pop_from_top_level (push_to_top);
   26952                 :             :         }
   26953                 :             :       else
   26954                 :          46 :         spec = noexcept_false_spec;
   26955                 :             : 
   26956                 :      741122 :       if (added)
   26957                 :      741119 :         fns->remove (fn);
   26958                 :             :     }
   26959                 :             : 
   26960                 :     7666879 :   if (spec == error_mark_node)
   26961                 :             :     {
   26962                 :             :       /* This failed with a hard error, so let's go with false.  */
   26963                 :          16 :       gcc_assert (seen_error ());
   26964                 :          16 :       spec = noexcept_false_spec;
   26965                 :             :     }
   26966                 :             : 
   26967                 :     7666879 :   TREE_TYPE (fn) = build_exception_variant (fntype, spec);
   26968                 :     7666879 :   if (orig_fn)
   26969                 :          67 :     TREE_TYPE (orig_fn) = TREE_TYPE (fn);
   26970                 :             : 
   26971                 :             :   return true;
   26972                 :             : }
   26973                 :             : 
   26974                 :             : /* We're starting to process the function INST, an instantiation of PATTERN;
   26975                 :             :    add their parameters to local_specializations.  */
   26976                 :             : 
   26977                 :             : void
   26978                 :    16112142 : register_parameter_specializations (tree pattern, tree inst)
   26979                 :             : {
   26980                 :    16112142 :   tree tmpl_parm = DECL_ARGUMENTS (pattern);
   26981                 :    16112142 :   tree spec_parm = DECL_ARGUMENTS (inst);
   26982                 :    16112142 :   if (DECL_IOBJ_MEMBER_FUNCTION_P (inst))
   26983                 :             :     {
   26984                 :     8077079 :       register_local_specialization (spec_parm, tmpl_parm);
   26985                 :     8077079 :       spec_parm = skip_artificial_parms_for (inst, spec_parm);
   26986                 :     8077079 :       tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
   26987                 :             :     }
   26988                 :    36501723 :   for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
   26989                 :             :     {
   26990                 :    20389581 :       if (!DECL_PACK_P (tmpl_parm))
   26991                 :             :         {
   26992                 :    19853315 :           register_local_specialization (spec_parm, tmpl_parm);
   26993                 :    19853315 :           spec_parm = DECL_CHAIN (spec_parm);
   26994                 :             :         }
   26995                 :             :       else
   26996                 :             :         {
   26997                 :             :           /* Register the (value) argument pack as a specialization of
   26998                 :             :              TMPL_PARM, then move on.  */
   26999                 :      536266 :           tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
   27000                 :      536266 :           register_local_specialization (argpack, tmpl_parm);
   27001                 :             :         }
   27002                 :             :     }
   27003                 :    16112142 :   gcc_assert (!spec_parm);
   27004                 :    16112142 : }
   27005                 :             : 
   27006                 :             : /* Instantiate the body of D using PATTERN with ARGS.  We have
   27007                 :             :    already determined PATTERN is the correct template to use.
   27008                 :             :    NESTED_P is true if this is a nested function, in which case
   27009                 :             :    PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL.  */
   27010                 :             : 
   27011                 :             : static void
   27012                 :    19892987 : instantiate_body (tree pattern, tree args, tree d, bool nested_p)
   27013                 :             : {
   27014                 :    19892987 :   tree td = NULL_TREE;
   27015                 :    19892987 :   tree code_pattern = pattern;
   27016                 :             : 
   27017                 :    19892987 :   if (!nested_p)
   27018                 :             :     {
   27019                 :    19892877 :       td = pattern;
   27020                 :    19892877 :       code_pattern = DECL_TEMPLATE_RESULT (td);
   27021                 :             :     }
   27022                 :             :   else
   27023                 :             :     /* Only OMP reductions are nested.  */
   27024                 :         110 :     gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
   27025                 :             : 
   27026                 :    19892987 :   vec<tree> omp_privatization_save;
   27027                 :    19892987 :   if (current_function_decl)
   27028                 :     5969131 :     save_omp_privatization_clauses (omp_privatization_save);
   27029                 :             : 
   27030                 :    19892987 :   bool push_to_top = maybe_push_to_top_level (d);
   27031                 :             : 
   27032                 :    19892987 :   mark_template_arguments_used (pattern, args);
   27033                 :             : 
   27034                 :    19892987 :   if (VAR_P (d))
   27035                 :             :     {
   27036                 :             :       /* The variable might be a lambda's extra scope, and that
   27037                 :             :          lambda's visibility depends on D's.  */
   27038                 :     3919420 :       maybe_commonize_var (d);
   27039                 :     3919420 :       determine_visibility (d);
   27040                 :             :     }
   27041                 :             : 
   27042                 :             :   /* Mark D as instantiated so that recursive calls to
   27043                 :             :      instantiate_decl do not try to instantiate it again.  */
   27044                 :    19892987 :   DECL_TEMPLATE_INSTANTIATED (d) = 1;
   27045                 :             : 
   27046                 :    19892987 :   if (td)
   27047                 :             :     /* Regenerate the declaration in case the template has been modified
   27048                 :             :        by a subsequent redeclaration.  */
   27049                 :    19892877 :     regenerate_decl_from_template (d, td, args);
   27050                 :             : 
   27051                 :             :   /* We already set the file and line above.  Reset them now in case
   27052                 :             :      they changed as a result of calling regenerate_decl_from_template.  */
   27053                 :    19892987 :   input_location = DECL_SOURCE_LOCATION (d);
   27054                 :             : 
   27055                 :    19892987 :   if (VAR_P (d))
   27056                 :             :     {
   27057                 :             :       /* Clear out DECL_RTL; whatever was there before may not be right
   27058                 :             :          since we've reset the type of the declaration.  */
   27059                 :     3919420 :       SET_DECL_RTL (d, NULL);
   27060                 :     3919420 :       DECL_IN_AGGR_P (d) = 0;
   27061                 :             : 
   27062                 :             :       /* The initializer is placed in DECL_INITIAL by
   27063                 :             :          regenerate_decl_from_template so we don't need to
   27064                 :             :          push/pop_access_scope again here.  Pull it out so that
   27065                 :             :          cp_finish_decl can process it.  */
   27066                 :     3919420 :       bool const_init = false;
   27067                 :     3919420 :       tree init = DECL_INITIAL (d);
   27068                 :     3919420 :       DECL_INITIAL (d) = NULL_TREE;
   27069                 :     3919420 :       DECL_INITIALIZED_P (d) = 0;
   27070                 :             : 
   27071                 :             :       /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
   27072                 :             :          initializer.  That function will defer actual emission until
   27073                 :             :          we have a chance to determine linkage.  */
   27074                 :     3919420 :       DECL_EXTERNAL (d) = 0;
   27075                 :             : 
   27076                 :             :       /* Enter the scope of D so that access-checking works correctly.  */
   27077                 :     3919420 :       bool enter_context = DECL_CLASS_SCOPE_P (d);
   27078                 :      983304 :       if (enter_context)
   27079                 :      983304 :         push_nested_class (DECL_CONTEXT (d));
   27080                 :             : 
   27081                 :     3919420 :       const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   27082                 :     3919420 :       cp_finish_decl (d, init, const_init, NULL_TREE, 0);
   27083                 :             : 
   27084                 :     3919420 :       if (enter_context)
   27085                 :      983304 :         pop_nested_class ();
   27086                 :             :     }
   27087                 :    15973567 :   else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
   27088                 :          62 :     synthesize_method (d);
   27089                 :    15973505 :   else if (TREE_CODE (d) == FUNCTION_DECL)
   27090                 :             :     {
   27091                 :             :       /* Set up the list of local specializations.  */
   27092                 :    16916849 :       local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
   27093                 :    15973505 :       tree block = NULL_TREE;
   27094                 :             : 
   27095                 :             :       /* Set up context.  */
   27096                 :    15973505 :       if (nested_p)
   27097                 :         110 :         block = push_stmt_list ();
   27098                 :             :       else
   27099                 :             :         {
   27100                 :    15973395 :           start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
   27101                 :             : 
   27102                 :    15973395 :           perform_instantiation_time_access_checks (code_pattern, args);
   27103                 :             :         }
   27104                 :             : 
   27105                 :             :       /* Create substitution entries for the parameters.  */
   27106                 :    15973505 :       register_parameter_specializations (code_pattern, d);
   27107                 :             : 
   27108                 :             :       /* Substitute into the body of the function.  */
   27109                 :    15973505 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   27110                 :         126 :         tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
   27111                 :             :                         tf_warning_or_error, d);
   27112                 :             :       else
   27113                 :             :         {
   27114                 :    15973379 :           tsubst_stmt (DECL_SAVED_TREE (code_pattern), args,
   27115                 :    15973379 :                        tf_warning_or_error, DECL_TI_TEMPLATE (d));
   27116                 :             : 
   27117                 :             :           /* Set the current input_location to the end of the function
   27118                 :             :              so that finish_function knows where we are.  */
   27119                 :    15973368 :           input_location
   27120                 :    15973368 :             = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
   27121                 :             : 
   27122                 :             :           /* Remember if we saw an infinite loop in the template.  */
   27123                 :    15973368 :           current_function_infinite_loop
   27124                 :    15973368 :             = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
   27125                 :             :         }
   27126                 :             : 
   27127                 :             :       /* Finish the function.  */
   27128                 :    15973494 :       if (nested_p)
   27129                 :         110 :         DECL_SAVED_TREE (d) = pop_stmt_list (block);
   27130                 :             :       else
   27131                 :             :         {
   27132                 :    15973384 :           d = finish_function (/*inline_p=*/false);
   27133                 :    15973384 :           expand_or_defer_fn (d);
   27134                 :             :         }
   27135                 :             : 
   27136                 :    15973494 :       if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
   27137                 :         126 :         cp_check_omp_declare_reduction (d);
   27138                 :    15973494 :     }
   27139                 :             : 
   27140                 :             :   /* We're not deferring instantiation any more.  */
   27141                 :    19892976 :   if (!nested_p)
   27142                 :    19892866 :     TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
   27143                 :             : 
   27144                 :    19892976 :   maybe_pop_from_top_level (push_to_top);
   27145                 :             : 
   27146                 :    19892976 :   if (current_function_decl)
   27147                 :     5969131 :     restore_omp_privatization_clauses (omp_privatization_save);
   27148                 :    19892976 : }
   27149                 :             : 
   27150                 :             : /* Produce the definition of D, a _DECL generated from a template.  If
   27151                 :             :    DEFER_OK is true, then we don't have to actually do the
   27152                 :             :    instantiation now; we just have to do it sometime.  Normally it is
   27153                 :             :    an error if this is an explicit instantiation but D is undefined.
   27154                 :             :    EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
   27155                 :             :    instantiated class template.  */
   27156                 :             : 
   27157                 :             : tree
   27158                 :    95905485 : instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
   27159                 :             : {
   27160                 :    95905485 :   tree tmpl = DECL_TI_TEMPLATE (d);
   27161                 :    95905485 :   tree gen_args;
   27162                 :    95905485 :   tree args;
   27163                 :    95905485 :   tree td;
   27164                 :    95905485 :   tree code_pattern;
   27165                 :    95905485 :   tree spec;
   27166                 :    95905485 :   tree gen_tmpl;
   27167                 :    95905485 :   bool pattern_defined;
   27168                 :    95905485 :   location_t saved_loc = input_location;
   27169                 :    95905485 :   bool external_p;
   27170                 :    95905485 :   bool deleted_p;
   27171                 :             : 
   27172                 :             :   /* This function should only be used to instantiate templates for
   27173                 :             :      functions and static member variables.  */
   27174                 :    95905485 :   gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
   27175                 :             : 
   27176                 :             :   /* A concept is never instantiated. */
   27177                 :    95905485 :   gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
   27178                 :             : 
   27179                 :    95905485 :   gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
   27180                 :             : 
   27181                 :    95905485 :   if (modules_p ())
   27182                 :             :     /* We may have a pending instantiation of D itself.  */
   27183                 :      433160 :     lazy_load_pendings (d);
   27184                 :             : 
   27185                 :             :   /* Variables are never deferred; if instantiation is required, they
   27186                 :             :      are instantiated right away.  That allows for better code in the
   27187                 :             :      case that an expression refers to the value of the variable --
   27188                 :             :      if the variable has a constant value the referring expression can
   27189                 :             :      take advantage of that fact.  */
   27190                 :    95905485 :   if (VAR_P (d))
   27191                 :    62270916 :     defer_ok = false;
   27192                 :             : 
   27193                 :             :   /* Don't instantiate cloned functions.  Instead, instantiate the
   27194                 :             :      functions they cloned.  */
   27195                 :    95905485 :   if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
   27196                 :     4478661 :     d = DECL_CLONED_FUNCTION (d);
   27197                 :             : 
   27198                 :    95905485 :   if (DECL_TEMPLATE_INSTANTIATED (d)
   27199                 :    38813572 :       || TREE_TYPE (d) == error_mark_node
   27200                 :    38813567 :       || (TREE_CODE (d) == FUNCTION_DECL
   27201                 :    31286385 :           && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
   27202                 :   134700293 :       || DECL_TEMPLATE_SPECIALIZATION (d))
   27203                 :             :     /* D has already been instantiated or explicitly specialized, so
   27204                 :             :        there's nothing for us to do here.
   27205                 :             : 
   27206                 :             :        It might seem reasonable to check whether or not D is an explicit
   27207                 :             :        instantiation, and, if so, stop here.  But when an explicit
   27208                 :             :        instantiation is deferred until the end of the compilation,
   27209                 :             :        DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
   27210                 :             :        the instantiation.  */
   27211                 :             :     return d;
   27212                 :             : 
   27213                 :             :   /* Check to see whether we know that this template will be
   27214                 :             :      instantiated in some other file, as with "extern template"
   27215                 :             :      extension.  */
   27216                 :    38738759 :   external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
   27217                 :             : 
   27218                 :             :   /* In general, we do not instantiate such templates.  */
   27219                 :     1764918 :   if (external_p && !always_instantiate_p (d))
   27220                 :             :     return d;
   27221                 :             : 
   27222                 :    38732119 :   gen_tmpl = most_general_template (tmpl);
   27223                 :    38732119 :   gen_args = DECL_TI_ARGS (d);
   27224                 :             : 
   27225                 :             :   /* We should already have the extra args.  */
   27226                 :    45426393 :   gcc_checking_assert (tmpl == gen_tmpl
   27227                 :             :                        || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
   27228                 :             :                            == TMPL_ARGS_DEPTH (gen_args)));
   27229                 :             :   /* And what's in the hash table should match D.  */
   27230                 :    38732119 :   gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
   27231                 :             :                        == d
   27232                 :             :                        || spec == NULL_TREE);
   27233                 :             : 
   27234                 :             :   /* This needs to happen before any tsubsting.  */
   27235                 :    38732119 :   if (! push_tinst_level (d))
   27236                 :             :     return d;
   27237                 :             : 
   27238                 :    38731013 :   auto_timevar tv (TV_TEMPLATE_INST);
   27239                 :             : 
   27240                 :             :   /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
   27241                 :             :      for the instantiation.  */
   27242                 :    38731013 :   td = template_for_substitution (d);
   27243                 :    38731013 :   args = gen_args;
   27244                 :             : 
   27245                 :    38731013 :   if (variable_template_specialization_p (d))
   27246                 :             :     {
   27247                 :             :       /* Look up an explicit specialization, if any.  */
   27248                 :     2946674 :       tree partial_ti = most_specialized_partial_spec (d, tf_warning_or_error);
   27249                 :     2946674 :       if (partial_ti && partial_ti != error_mark_node)
   27250                 :             :         {
   27251                 :      212308 :           td = TI_TEMPLATE (partial_ti);
   27252                 :      212308 :           args = TI_ARGS (partial_ti);
   27253                 :             :         }
   27254                 :             :     }
   27255                 :             : 
   27256                 :    38731013 :   code_pattern = DECL_TEMPLATE_RESULT (td);
   27257                 :             : 
   27258                 :             :   /* We should never be trying to instantiate a member of a class
   27259                 :             :      template or partial specialization.  */
   27260                 :    38731013 :   gcc_assert (d != code_pattern);
   27261                 :             : 
   27262                 :    77462026 :   if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
   27263                 :    64146419 :       || DECL_TEMPLATE_SPECIALIZATION (td))
   27264                 :             :     /* In the case of a friend template whose definition is provided
   27265                 :             :        outside the class, we may have too many arguments.  Drop the
   27266                 :             :        ones we don't need.  The same is true for specializations.  */
   27267                 :    13316339 :     args = get_innermost_template_args
   27268                 :    13316339 :       (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
   27269                 :             : 
   27270                 :    38731013 :   if (TREE_CODE (d) == FUNCTION_DECL)
   27271                 :             :     {
   27272                 :    31247800 :       deleted_p = DECL_DELETED_FN (code_pattern);
   27273                 :    31247800 :       pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
   27274                 :    30411401 :                           && DECL_INITIAL (code_pattern) != error_mark_node)
   27275                 :      836404 :                          || DECL_DEFAULTED_FN (code_pattern)
   27276                 :    32084058 :                          || deleted_p);
   27277                 :             :     }
   27278                 :             :   else
   27279                 :             :     {
   27280                 :     7483213 :       deleted_p = false;
   27281                 :     7483213 :       if (DECL_CLASS_SCOPE_P (code_pattern))
   27282                 :     4547006 :         pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
   27283                 :             :       else
   27284                 :     2936207 :         pattern_defined = ! DECL_EXTERNAL (code_pattern);
   27285                 :             :     }
   27286                 :             : 
   27287                 :             :   /* We may be in the middle of deferred access check.  Disable it now.  */
   27288                 :    38731013 :   push_deferring_access_checks (dk_no_deferred);
   27289                 :             : 
   27290                 :             :   /* Unless an explicit instantiation directive has already determined
   27291                 :             :      the linkage of D, remember that a definition is available for
   27292                 :             :      this entity.  */
   27293                 :    38731013 :   if (pattern_defined
   27294                 :    34378563 :       && !DECL_INTERFACE_KNOWN (d)
   27295                 :    69908643 :       && !DECL_NOT_REALLY_EXTERN (d))
   27296                 :    15048154 :     mark_definable (d);
   27297                 :             : 
   27298                 :    38731013 :   DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
   27299                 :    38731013 :   DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
   27300                 :    38731013 :   input_location = DECL_SOURCE_LOCATION (d);
   27301                 :             : 
   27302                 :             :   /* If D is a member of an explicitly instantiated class template,
   27303                 :             :      and no definition is available, treat it like an implicit
   27304                 :             :      instantiation.  */
   27305                 :    38731013 :   if (!pattern_defined && expl_inst_class_mem_p
   27306                 :    38731013 :       && DECL_EXPLICIT_INSTANTIATION (d))
   27307                 :             :     {
   27308                 :             :       /* Leave linkage flags alone on instantiations with anonymous
   27309                 :             :          visibility.  */
   27310                 :         402 :       if (TREE_PUBLIC (d))
   27311                 :             :         {
   27312                 :         390 :           DECL_NOT_REALLY_EXTERN (d) = 0;
   27313                 :         390 :           DECL_INTERFACE_KNOWN (d) = 0;
   27314                 :             :         }
   27315                 :         402 :       SET_DECL_IMPLICIT_INSTANTIATION (d);
   27316                 :             :     }
   27317                 :             : 
   27318                 :             :   /* Defer all other templates, unless we have been explicitly
   27319                 :             :      forbidden from doing so.  */
   27320                 :    38731013 :   if (/* If there is no definition, we cannot instantiate the
   27321                 :             :          template.  */
   27322                 :             :       ! pattern_defined
   27323                 :             :       /* If it's OK to postpone instantiation, do so.  */
   27324                 :    38731013 :       || defer_ok
   27325                 :             :       /* If this is a static data member that will be defined
   27326                 :             :          elsewhere, we don't want to instantiate the entire data
   27327                 :             :          member, but we do want to instantiate the initializer so that
   27328                 :             :          we can substitute that elsewhere.  */
   27329                 :    19940469 :       || (external_p && VAR_P (d))
   27330                 :             :       /* Handle here a deleted function too, avoid generating
   27331                 :             :          its body (c++/61080).  */
   27332                 :    19893390 :       || deleted_p)
   27333                 :             :     {
   27334                 :             :       /* The definition of the static data member is now required so
   27335                 :             :          we must substitute the initializer.  */
   27336                 :    18838136 :       if (VAR_P (d)
   27337                 :     3563793 :           && !DECL_INITIAL (d)
   27338                 :    19173468 :           && DECL_INITIAL (code_pattern))
   27339                 :             :         {
   27340                 :      334820 :           tree ns;
   27341                 :      334820 :           tree init;
   27342                 :      334820 :           bool const_init = false;
   27343                 :      334820 :           bool enter_context = DECL_CLASS_SCOPE_P (d);
   27344                 :             : 
   27345                 :      334820 :           ns = decl_namespace_context (d);
   27346                 :      334820 :           push_nested_namespace (ns);
   27347                 :      334820 :           if (enter_context)
   27348                 :      334805 :             push_nested_class (DECL_CONTEXT (d));
   27349                 :      334820 :           init = tsubst_expr (DECL_INITIAL (code_pattern),
   27350                 :             :                               args,
   27351                 :             :                               tf_warning_or_error, NULL_TREE);
   27352                 :             :           /* If instantiating the initializer involved instantiating this
   27353                 :             :              again, don't call cp_finish_decl twice.  */
   27354                 :      331212 :           if (!DECL_INITIAL (d))
   27355                 :             :             {
   27356                 :             :               /* Make sure the initializer is still constant, in case of
   27357                 :             :                  circular dependency (template/instantiate6.C). */
   27358                 :      331208 :               const_init
   27359                 :      331208 :                 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
   27360                 :      331208 :               cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
   27361                 :             :                               /*asmspec_tree=*/NULL_TREE, 0);
   27362                 :             :             }
   27363                 :      320428 :           if (enter_context)
   27364                 :      320413 :             pop_nested_class ();
   27365                 :      320428 :           pop_nested_namespace (ns);
   27366                 :             :         }
   27367                 :             : 
   27368                 :             :       /* We restore the source position here because it's used by
   27369                 :             :          add_pending_template.  */
   27370                 :    18823744 :       input_location = saved_loc;
   27371                 :             : 
   27372                 :    13490851 :       if (at_eof && !pattern_defined
   27373                 :     3840531 :           && DECL_EXPLICIT_INSTANTIATION (d)
   27374                 :    18823764 :           && DECL_NOT_REALLY_EXTERN (d))
   27375                 :             :         /* [temp.explicit]
   27376                 :             : 
   27377                 :             :            The definition of a non-exported function template, a
   27378                 :             :            non-exported member function template, or a non-exported
   27379                 :             :            member function or static data member of a class template
   27380                 :             :            shall be present in every translation unit in which it is
   27381                 :             :            explicitly instantiated.  */
   27382                 :          20 :         permerror (input_location,  "explicit instantiation of %qD "
   27383                 :             :                    "but no definition available", d);
   27384                 :             : 
   27385                 :             :       /* If we're in unevaluated context, we just wanted to get the
   27386                 :             :          constant value; this isn't an odr use, so don't queue
   27387                 :             :          a full instantiation.  */
   27388                 :    18823744 :       if (!cp_unevaluated_operand
   27389                 :             :           /* ??? Historically, we have instantiated inline functions, even
   27390                 :             :              when marked as "extern template".  */
   27391                 :    18823657 :           && !(external_p && VAR_P (d)))
   27392                 :    18776578 :         add_pending_template (d);
   27393                 :             :     }
   27394                 :             :   else
   27395                 :             :     {
   27396                 :    19892877 :       set_instantiating_module (d);
   27397                 :    19892877 :       if (variable_template_p (gen_tmpl))
   27398                 :     2946344 :         note_vague_linkage_variable (d);
   27399                 :    19892877 :       instantiate_body (td, args, d, false);
   27400                 :             :     }
   27401                 :             : 
   27402                 :    38716610 :   pop_deferring_access_checks ();
   27403                 :    38716610 :   pop_tinst_level ();
   27404                 :    38716610 :   input_location = saved_loc;
   27405                 :             : 
   27406                 :    38716610 :   return d;
   27407                 :    38716610 : }
   27408                 :             : 
   27409                 :             : /* Run through the list of templates that we wish we could
   27410                 :             :    instantiate, and instantiate any we can.  RETRIES is the
   27411                 :             :    number of times we retry pending template instantiation.  */
   27412                 :             : 
   27413                 :             : void
   27414                 :      144472 : instantiate_pending_templates (int retries)
   27415                 :             : {
   27416                 :      144472 :   int reconsider;
   27417                 :      144472 :   location_t saved_loc = input_location;
   27418                 :             : 
   27419                 :             :   /* Instantiating templates may trigger vtable generation.  This in turn
   27420                 :             :      may require further template instantiations.  We place a limit here
   27421                 :             :      to avoid infinite loop.  */
   27422                 :      144472 :   if (pending_templates && retries >= max_tinst_depth)
   27423                 :             :     {
   27424                 :           4 :       tree decl = pending_templates->tinst->maybe_get_node ();
   27425                 :             : 
   27426                 :           4 :       fatal_error (input_location,
   27427                 :             :                    "template instantiation depth exceeds maximum of %d"
   27428                 :             :                    " instantiating %q+D, possibly from virtual table generation"
   27429                 :             :                    " (use %<-ftemplate-depth=%> to increase the maximum)",
   27430                 :             :                    max_tinst_depth, decl);
   27431                 :             :       if (TREE_CODE (decl) == FUNCTION_DECL)
   27432                 :             :         /* Pretend that we defined it.  */
   27433                 :             :         DECL_INITIAL (decl) = error_mark_node;
   27434                 :             :       return;
   27435                 :             :     }
   27436                 :             : 
   27437                 :      179802 :   do
   27438                 :             :     {
   27439                 :      179802 :       struct pending_template **t = &pending_templates;
   27440                 :      179802 :       struct pending_template *last = NULL;
   27441                 :      179802 :       reconsider = 0;
   27442                 :    17110850 :       while (*t)
   27443                 :             :         {
   27444                 :    16931059 :           tree instantiation = reopen_tinst_level ((*t)->tinst);
   27445                 :    16931059 :           bool complete = false;
   27446                 :             : 
   27447                 :    16931059 :           if (TYPE_P (instantiation))
   27448                 :             :             {
   27449                 :           0 :               if (!COMPLETE_TYPE_P (instantiation))
   27450                 :             :                 {
   27451                 :           0 :                   instantiate_class_template (instantiation);
   27452                 :           0 :                   if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
   27453                 :           0 :                     for (tree fld = TYPE_FIELDS (instantiation);
   27454                 :           0 :                          fld; fld = TREE_CHAIN (fld))
   27455                 :           0 :                       if ((VAR_P (fld)
   27456                 :           0 :                            || (TREE_CODE (fld) == FUNCTION_DECL
   27457                 :           0 :                                && !DECL_ARTIFICIAL (fld)))
   27458                 :           0 :                           && DECL_TEMPLATE_INSTANTIATION (fld))
   27459                 :           0 :                         instantiate_decl (fld,
   27460                 :             :                                           /*defer_ok=*/false,
   27461                 :             :                                           /*expl_inst_class_mem_p=*/false);
   27462                 :             : 
   27463                 :           0 :                   if (COMPLETE_TYPE_P (instantiation))
   27464                 :           0 :                     reconsider = 1;
   27465                 :             :                 }
   27466                 :             : 
   27467                 :           0 :               complete = COMPLETE_TYPE_P (instantiation);
   27468                 :             :             }
   27469                 :             :           else
   27470                 :             :             {
   27471                 :    16931059 :               if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
   27472                 :    16931059 :                   && !DECL_TEMPLATE_INSTANTIATED (instantiation))
   27473                 :             :                 {
   27474                 :    13301938 :                   instantiation
   27475                 :    13301938 :                     = instantiate_decl (instantiation,
   27476                 :             :                                         /*defer_ok=*/false,
   27477                 :             :                                         /*expl_inst_class_mem_p=*/false);
   27478                 :    13301927 :                   if (DECL_TEMPLATE_INSTANTIATED (instantiation))
   27479                 :    10889892 :                     reconsider = 1;
   27480                 :             :                 }
   27481                 :             : 
   27482                 :    16931048 :               complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
   27483                 :    16931048 :                           || DECL_TEMPLATE_INSTANTIATED (instantiation));
   27484                 :             :             }
   27485                 :             : 
   27486                 :           0 :           if (complete)
   27487                 :             :             {
   27488                 :             :               /* If INSTANTIATION has been instantiated, then we don't
   27489                 :             :                  need to consider it again in the future.  */
   27490                 :    14519013 :               struct pending_template *drop = *t;
   27491                 :    14519013 :               *t = (*t)->next;
   27492                 :    14519013 :               set_refcount_ptr (drop->tinst);
   27493                 :    14519013 :               pending_template_freelist ().free (drop);
   27494                 :             :             }
   27495                 :             :           else
   27496                 :             :             {
   27497                 :     2412035 :               last = *t;
   27498                 :     2412035 :               t = &(*t)->next;
   27499                 :             :             }
   27500                 :    16931048 :           tinst_depth = 0;
   27501                 :    16931048 :           set_refcount_ptr (current_tinst_level);
   27502                 :             :         }
   27503                 :      179791 :       last_pending_template = last;
   27504                 :             :     }
   27505                 :      179791 :   while (reconsider);
   27506                 :             : 
   27507                 :      144457 :   input_location = saved_loc;
   27508                 :             : }
   27509                 :             : 
   27510                 :             : /* Substitute ARGVEC into T, which is a list of initializers for
   27511                 :             :    either base class or a non-static data member.  The TREE_PURPOSEs
   27512                 :             :    are DECLs, and the TREE_VALUEs are the initializer values.  Used by
   27513                 :             :    instantiate_decl.  */
   27514                 :             : 
   27515                 :             : static tree
   27516                 :     2291688 : tsubst_initializer_list (tree t, tree argvec)
   27517                 :             : {
   27518                 :     2291688 :   tree inits = NULL_TREE;
   27519                 :     2291688 :   tree target_ctor = error_mark_node;
   27520                 :             : 
   27521                 :     4875475 :   for (; t; t = TREE_CHAIN (t))
   27522                 :             :     {
   27523                 :     2583790 :       tree decl;
   27524                 :     2583790 :       tree init;
   27525                 :     2583790 :       tree expanded_bases = NULL_TREE;
   27526                 :     2583790 :       tree expanded_arguments = NULL_TREE;
   27527                 :     2583790 :       int i, len = 1;
   27528                 :             : 
   27529                 :     2583790 :       if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
   27530                 :             :         {
   27531                 :          28 :           tree expr;
   27532                 :          28 :           tree arg;
   27533                 :             : 
   27534                 :             :           /* Expand the base class expansion type into separate base
   27535                 :             :              classes.  */
   27536                 :          28 :           expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
   27537                 :             :                                                  tf_warning_or_error,
   27538                 :             :                                                  NULL_TREE);
   27539                 :          28 :           if (expanded_bases == error_mark_node)
   27540                 :           0 :             continue;
   27541                 :             : 
   27542                 :             :           /* We'll be building separate TREE_LISTs of arguments for
   27543                 :             :              each base.  */
   27544                 :          28 :           len = TREE_VEC_LENGTH (expanded_bases);
   27545                 :          28 :           expanded_arguments = make_tree_vec (len);
   27546                 :          97 :           for (i = 0; i < len; i++)
   27547                 :          41 :             TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
   27548                 :             : 
   27549                 :             :           /* Build a dummy EXPR_PACK_EXPANSION that will be used to
   27550                 :             :              expand each argument in the TREE_VALUE of t.  */
   27551                 :          28 :           expr = make_node (EXPR_PACK_EXPANSION);
   27552                 :          28 :           PACK_EXPANSION_LOCAL_P (expr) = true;
   27553                 :          56 :           PACK_EXPANSION_PARAMETER_PACKS (expr) =
   27554                 :          28 :             PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
   27555                 :             : 
   27556                 :          28 :           if (TREE_VALUE (t) == void_type_node)
   27557                 :             :             /* VOID_TYPE_NODE is used to indicate
   27558                 :             :                value-initialization.  */
   27559                 :             :             {
   27560                 :           9 :               for (i = 0; i < len; i++)
   27561                 :           3 :                 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
   27562                 :             :             }
   27563                 :             :           else
   27564                 :             :             {
   27565                 :             :               /* Substitute parameter packs into each argument in the
   27566                 :             :                  TREE_LIST.  */
   27567                 :          22 :               in_base_initializer = 1;
   27568                 :          53 :               for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
   27569                 :             :                 {
   27570                 :          31 :                   tree expanded_exprs;
   27571                 :             : 
   27572                 :             :                   /* Expand the argument.  */
   27573                 :          31 :                   tree value;
   27574                 :          31 :                   if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   27575                 :             :                     value = TREE_VALUE (arg);
   27576                 :             :                   else
   27577                 :             :                     {
   27578                 :          28 :                       value = expr;
   27579                 :          28 :                       PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg);
   27580                 :             :                     }
   27581                 :          31 :                   expanded_exprs
   27582                 :          31 :                     = tsubst_pack_expansion (value, argvec,
   27583                 :             :                                              tf_warning_or_error,
   27584                 :             :                                              NULL_TREE);
   27585                 :          31 :                   if (expanded_exprs == error_mark_node)
   27586                 :           3 :                     continue;
   27587                 :             : 
   27588                 :             :                   /* Prepend each of the expanded expressions to the
   27589                 :             :                      corresponding TREE_LIST in EXPANDED_ARGUMENTS.  */
   27590                 :          87 :                   for (i = 0; i < len; i++)
   27591                 :          59 :                     if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
   27592                 :          36 :                       for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
   27593                 :          27 :                         TREE_VEC_ELT (expanded_arguments, i)
   27594                 :          54 :                           = tree_cons (NULL_TREE,
   27595                 :          27 :                                        TREE_VEC_ELT (expanded_exprs, j),
   27596                 :          27 :                                        TREE_VEC_ELT (expanded_arguments, i));
   27597                 :             :                     else
   27598                 :          50 :                       TREE_VEC_ELT (expanded_arguments, i)
   27599                 :         100 :                         = tree_cons (NULL_TREE,
   27600                 :          50 :                                      TREE_VEC_ELT (expanded_exprs, i),
   27601                 :          50 :                                      TREE_VEC_ELT (expanded_arguments, i));
   27602                 :             :                 }
   27603                 :          22 :               in_base_initializer = 0;
   27604                 :             : 
   27605                 :             :               /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
   27606                 :             :                  since we built them backwards.  */
   27607                 :          60 :               for (i = 0; i < len; i++)
   27608                 :             :                 {
   27609                 :          38 :                   TREE_VEC_ELT (expanded_arguments, i) =
   27610                 :          38 :                     nreverse (TREE_VEC_ELT (expanded_arguments, i));
   27611                 :             :                 }
   27612                 :             :             }
   27613                 :             :         }
   27614                 :             : 
   27615                 :     5167590 :       for (i = 0; i < len; ++i)
   27616                 :             :         {
   27617                 :     2583803 :           if (expanded_bases)
   27618                 :             :             {
   27619                 :          41 :               decl = TREE_VEC_ELT (expanded_bases, i);
   27620                 :          41 :               decl = expand_member_init (decl);
   27621                 :          41 :               init = TREE_VEC_ELT (expanded_arguments, i);
   27622                 :             :             }
   27623                 :             :           else
   27624                 :             :             {
   27625                 :     2583762 :               tree tmp;
   27626                 :     2583762 :               if (TYPE_P (TREE_PURPOSE (t)))
   27627                 :      518405 :                 decl = tsubst (TREE_PURPOSE (t), argvec,
   27628                 :             :                                tf_warning_or_error, NULL_TREE);
   27629                 :             :               else
   27630                 :     2065357 :                 decl = tsubst_expr (TREE_PURPOSE (t), argvec,
   27631                 :             :                                     tf_warning_or_error, NULL_TREE);
   27632                 :             : 
   27633                 :     2583762 :               decl = expand_member_init (decl);
   27634                 :     2583762 :               if (decl && !DECL_P (decl))
   27635                 :      518398 :                 in_base_initializer = 1;
   27636                 :             : 
   27637                 :     2583762 :               init = TREE_VALUE (t);
   27638                 :     2583762 :               tmp = init;
   27639                 :     2583762 :               if (init != void_type_node)
   27640                 :     2165485 :                 init = tsubst_expr (init, argvec,
   27641                 :             :                                     tf_warning_or_error, NULL_TREE);
   27642                 :     2583762 :               if (init == NULL_TREE && tmp != NULL_TREE)
   27643                 :             :                 /* If we had an initializer but it instantiated to nothing,
   27644                 :             :                    value-initialize the object.  This will only occur when
   27645                 :             :                    the initializer was a pack expansion where the parameter
   27646                 :             :                    packs used in that expansion were of length zero.  */
   27647                 :         578 :                 init = void_type_node;
   27648                 :     2583762 :               in_base_initializer = 0;
   27649                 :             :             }
   27650                 :             : 
   27651                 :     2583803 :           if (target_ctor != error_mark_node
   27652                 :           3 :               && init != error_mark_node)
   27653                 :             :             {
   27654                 :           3 :               error ("mem-initializer for %qD follows constructor delegation",
   27655                 :             :                      decl);
   27656                 :           3 :               return inits;
   27657                 :             :             }
   27658                 :             :           /* Look for a target constructor. */
   27659                 :     2583800 :           if (init != error_mark_node
   27660                 :     2583786 :               && decl && CLASS_TYPE_P (decl)
   27661                 :     2596422 :               && same_type_p (decl, current_class_type))
   27662                 :             :             {
   27663                 :       12622 :               maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
   27664                 :       12622 :               if (inits)
   27665                 :             :                 {
   27666                 :           0 :                   error ("constructor delegation follows mem-initializer for %qD",
   27667                 :           0 :                          TREE_PURPOSE (inits));
   27668                 :           0 :                   continue;
   27669                 :             :                 }
   27670                 :             :               target_ctor = init;
   27671                 :             :             }
   27672                 :             : 
   27673                 :     2583800 :           if (decl)
   27674                 :             :             {
   27675                 :     2583793 :               init = build_tree_list (decl, init);
   27676                 :             :               /* Carry over the dummy TREE_TYPE node containing the source
   27677                 :             :                  location.  */
   27678                 :     2583793 :               TREE_TYPE (init) = TREE_TYPE (t);
   27679                 :     2583793 :               TREE_CHAIN (init) = inits;
   27680                 :     2583793 :               inits = init;
   27681                 :             :             }
   27682                 :             :         }
   27683                 :             :     }
   27684                 :             :   return inits;
   27685                 :             : }
   27686                 :             : 
   27687                 :             : /* Instantiate an enumerated type.  TAG is the template type, NEWTAG
   27688                 :             :    is the instantiation (which should have been created with
   27689                 :             :    start_enum) and ARGS are the template arguments to use.  */
   27690                 :             : 
   27691                 :             : static void
   27692                 :      307171 : tsubst_enum (tree tag, tree newtag, tree args)
   27693                 :             : {
   27694                 :      307171 :   tree e;
   27695                 :             : 
   27696                 :      307171 :   if (SCOPED_ENUM_P (newtag))
   27697                 :         350 :     begin_scope (sk_scoped_enum, newtag);
   27698                 :             : 
   27699                 :      633721 :   for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
   27700                 :             :     {
   27701                 :      326610 :       tree value;
   27702                 :      326610 :       tree decl = TREE_VALUE (e);
   27703                 :             : 
   27704                 :             :       /* Note that in a template enum, the TREE_VALUE is the
   27705                 :             :          CONST_DECL, not the corresponding INTEGER_CST.  */
   27706                 :      326610 :       value = tsubst_expr (DECL_INITIAL (decl),
   27707                 :             :                            args, tf_warning_or_error, NULL_TREE);
   27708                 :             : 
   27709                 :             :       /* Give this enumeration constant the correct access.  */
   27710                 :      326550 :       set_current_access_from_decl (decl);
   27711                 :             : 
   27712                 :             :       /* Actually build the enumerator itself.  Here we're assuming that
   27713                 :             :          enumerators can't have dependent attributes.  */
   27714                 :      326550 :       tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag,
   27715                 :      326550 :                                        DECL_ATTRIBUTES (decl),
   27716                 :      326550 :                                        DECL_SOURCE_LOCATION (decl));
   27717                 :             :       /* Attribute deprecated without an argument isn't sticky: it'll
   27718                 :             :          melt into a tree flag, so we need to propagate the flag here,
   27719                 :             :          since we just created a new enumerator.  */
   27720                 :      326550 :       TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl);
   27721                 :      326550 :       TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl);
   27722                 :             :     }
   27723                 :             : 
   27724                 :      307111 :   if (SCOPED_ENUM_P (newtag))
   27725                 :         350 :     finish_scope ();
   27726                 :             : 
   27727                 :      307111 :   finish_enum_value_list (newtag);
   27728                 :      307111 :   finish_enum (newtag);
   27729                 :             : 
   27730                 :      307111 :   DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
   27731                 :      307111 :     = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
   27732                 :      307111 :   TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag);
   27733                 :      307111 :   TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag);
   27734                 :      307111 : }
   27735                 :             : 
   27736                 :             : /* DECL is a FUNCTION_DECL that is a template specialization.  Return
   27737                 :             :    its type -- but without substituting the innermost set of template
   27738                 :             :    arguments.  So, innermost set of template parameters will appear in
   27739                 :             :    the type.  */
   27740                 :             : 
   27741                 :             : tree
   27742                 :    11959486 : get_mostly_instantiated_function_type (tree decl)
   27743                 :             : {
   27744                 :             :   /* For a function, DECL_TI_TEMPLATE is partially instantiated.  */
   27745                 :    11959486 :   return TREE_TYPE (DECL_TI_TEMPLATE (decl));
   27746                 :             : }
   27747                 :             : 
   27748                 :             : /* Return truthvalue if we're processing a template different from
   27749                 :             :    the last one involved in diagnostics.  */
   27750                 :             : bool
   27751                 :      210995 : problematic_instantiation_changed (void)
   27752                 :             : {
   27753                 :      210995 :   return current_tinst_level != last_error_tinst_level;
   27754                 :             : }
   27755                 :             : 
   27756                 :             : /* Remember current template involved in diagnostics.  */
   27757                 :             : void
   27758                 :        4414 : record_last_problematic_instantiation (void)
   27759                 :             : {
   27760                 :        4414 :   set_refcount_ptr (last_error_tinst_level, current_tinst_level);
   27761                 :        4414 : }
   27762                 :             : 
   27763                 :             : struct tinst_level *
   27764                 :   267259427 : current_instantiation (void)
   27765                 :             : {
   27766                 :   267259427 :   return current_tinst_level;
   27767                 :             : }
   27768                 :             : 
   27769                 :             : /* Return TRUE if current_function_decl is being instantiated, false
   27770                 :             :    otherwise.  */
   27771                 :             : 
   27772                 :             : bool
   27773                 :   190018506 : instantiating_current_function_p (void)
   27774                 :             : {
   27775                 :   190018506 :   return (current_instantiation ()
   27776                 :   190018506 :           && (current_instantiation ()->maybe_get_node ()
   27777                 :     1847877 :               == current_function_decl));
   27778                 :             : }
   27779                 :             : 
   27780                 :             : /* [temp.param] Check that template non-type parm TYPE is of an allowable
   27781                 :             :    type.  Return false for ok, true for disallowed.  Issue error and
   27782                 :             :    inform messages under control of COMPLAIN.  */
   27783                 :             : 
   27784                 :             : static bool
   27785                 :   149705046 : invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
   27786                 :             : {
   27787                 :   149705046 :   if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
   27788                 :             :     return false;
   27789                 :     4655767 :   else if (TYPE_PTR_P (type))
   27790                 :             :     return false;
   27791                 :     4653251 :   else if (TYPE_REF_P (type)
   27792                 :     4653251 :            && !TYPE_REF_IS_RVALUE (type))
   27793                 :             :     return false;
   27794                 :     4652401 :   else if (TYPE_PTRMEM_P (type))
   27795                 :             :     return false;
   27796                 :     4648811 :   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
   27797                 :             :     {
   27798                 :     2369634 :       if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
   27799                 :             :         {
   27800                 :           1 :           if (complain & tf_error)
   27801                 :           1 :             error ("non-type template parameters of deduced class type only "
   27802                 :             :                    "available with %<-std=c++20%> or %<-std=gnu++20%>");
   27803                 :           1 :           return true;
   27804                 :             :         }
   27805                 :             :       return false;
   27806                 :             :     }
   27807                 :     2279177 :   else if (TREE_CODE (type) == NULLPTR_TYPE)
   27808                 :             :     return false;
   27809                 :     2279144 :   else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
   27810                 :          15 :            && cxx_dialect < cxx11)
   27811                 :             :     /* Fall through; before C++11 alias templates, a bound ttp
   27812                 :             :        always instantiates into a class type.  */;
   27813                 :     2279144 :   else if (WILDCARD_TYPE_P (type))
   27814                 :             :     /* Any other wildcard type not already handled above is allowed.  */
   27815                 :             :     return false;
   27816                 :             :   else if (TREE_CODE (type) == COMPLEX_TYPE)
   27817                 :             :     /* Fall through.  */;
   27818                 :             :   else if (VOID_TYPE_P (type))
   27819                 :             :     /* Fall through.  */;
   27820                 :         919 :   else if (cxx_dialect >= cxx20)
   27821                 :             :     {
   27822                 :         825 :       if (dependent_type_p (type))
   27823                 :             :         return false;
   27824                 :         814 :       if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
   27825                 :             :         return true;
   27826                 :         800 :       if (structural_type_p (type))
   27827                 :             :         return false;
   27828                 :           5 :       if (complain & tf_error)
   27829                 :             :         {
   27830                 :           3 :           auto_diagnostic_group d;
   27831                 :           3 :           error ("%qT is not a valid type for a template non-type "
   27832                 :             :                  "parameter because it is not structural", type);
   27833                 :           3 :           structural_type_p (type, true);
   27834                 :           3 :         }
   27835                 :           5 :       return true;
   27836                 :             :     }
   27837                 :          94 :   else if (CLASS_TYPE_P (type))
   27838                 :             :     {
   27839                 :          23 :       if (complain & tf_error)
   27840                 :          18 :         error ("non-type template parameters of class type only available "
   27841                 :             :                "with %<-std=c++20%> or %<-std=gnu++20%>");
   27842                 :          23 :       return true;
   27843                 :             :     }
   27844                 :             : 
   27845                 :         178 :   if (complain & tf_error)
   27846                 :             :     {
   27847                 :         172 :       if (type == error_mark_node)
   27848                 :          16 :         inform (input_location, "invalid template non-type parameter");
   27849                 :             :       else
   27850                 :         156 :         error ("%q#T is not a valid type for a template non-type parameter",
   27851                 :             :                type);
   27852                 :             :     }
   27853                 :             :   return true;
   27854                 :             : }
   27855                 :             : 
   27856                 :             : /* Returns true iff the noexcept-specifier for TYPE is value-dependent.  */
   27857                 :             : 
   27858                 :             : static bool
   27859                 :     5085059 : value_dependent_noexcept_spec_p (tree type)
   27860                 :             : {
   27861                 :     5085059 :   if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
   27862                 :     2245045 :     if (tree noex = TREE_PURPOSE (spec))
   27863                 :             :       /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
   27864                 :             :          affect overload resolution and treating it as dependent breaks
   27865                 :             :          things.  Same for an unparsed noexcept expression.  */
   27866                 :     2244866 :       if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
   27867                 :     2244866 :           && TREE_CODE (noex) != DEFERRED_PARSE
   27868                 :     2244866 :           && value_dependent_expression_p (noex))
   27869                 :             :         return true;
   27870                 :             : 
   27871                 :             :   return false;
   27872                 :             : }
   27873                 :             : 
   27874                 :             : /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
   27875                 :             :    Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
   27876                 :             : 
   27877                 :             : static bool
   27878                 :   440030441 : dependent_type_p_r (tree type)
   27879                 :             : {
   27880                 :   440030441 :   tree scope;
   27881                 :             : 
   27882                 :             :   /* [temp.dep.type]
   27883                 :             : 
   27884                 :             :      A type is dependent if it is:
   27885                 :             : 
   27886                 :             :      -- a template parameter. Template template parameters are types
   27887                 :             :         for us (since TYPE_P holds true for them) so we handle
   27888                 :             :         them here.  */
   27889                 :   440030441 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   27890                 :   440030441 :       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
   27891                 :             :     return true;
   27892                 :             :   /* -- a qualified-id with a nested-name-specifier which contains a
   27893                 :             :         class-name that names a dependent type or whose unqualified-id
   27894                 :             :         names a dependent type.  */
   27895                 :   341437793 :   if (TREE_CODE (type) == TYPENAME_TYPE)
   27896                 :             :     return true;
   27897                 :             : 
   27898                 :             :   /* An alias template specialization can be dependent even if the
   27899                 :             :      resulting type is not.  */
   27900                 :   321950154 :   if (dependent_alias_template_spec_p (type, nt_transparent))
   27901                 :             :     return true;
   27902                 :             : 
   27903                 :             :   /* -- a cv-qualified type where the cv-unqualified type is
   27904                 :             :         dependent.
   27905                 :             :      No code is necessary for this bullet; the code below handles
   27906                 :             :      cv-qualified types, and we don't want to strip aliases with
   27907                 :             :      TYPE_MAIN_VARIANT because of DR 1558.  */
   27908                 :             :   /* -- a compound type constructed from any dependent type.  */
   27909                 :   321839755 :   if (TYPE_PTRMEM_P (type))
   27910                 :      960224 :     return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
   27911                 :      960224 :             || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
   27912                 :             :                                            (type)));
   27913                 :   320879531 :   else if (INDIRECT_TYPE_P (type))
   27914                 :    61658782 :     return dependent_type_p (TREE_TYPE (type));
   27915                 :   259220749 :   else if (FUNC_OR_METHOD_TYPE_P (type))
   27916                 :             :     {
   27917                 :    63428305 :       tree arg_type;
   27918                 :             : 
   27919                 :    63428305 :       if (dependent_type_p (TREE_TYPE (type)))
   27920                 :             :         return true;
   27921                 :    35360957 :       for (arg_type = TYPE_ARG_TYPES (type);
   27922                 :    50054017 :            arg_type;
   27923                 :    14693060 :            arg_type = TREE_CHAIN (arg_type))
   27924                 :    44870096 :         if (dependent_type_p (TREE_VALUE (arg_type)))
   27925                 :             :           return true;
   27926                 :     5183921 :       if (cxx_dialect >= cxx17
   27927                 :     5183921 :           && value_dependent_noexcept_spec_p (type))
   27928                 :             :         /* A value-dependent noexcept-specifier makes the type dependent.  */
   27929                 :             :         return true;
   27930                 :     5183895 :       return false;
   27931                 :             :     }
   27932                 :             :   /* -- an array type constructed from any dependent type or whose
   27933                 :             :         size is specified by a constant expression that is
   27934                 :             :         value-dependent.
   27935                 :             : 
   27936                 :             :         We checked for type- and value-dependence of the bounds in
   27937                 :             :         compute_array_index_type, so TYPE_DEPENDENT_P is already set.  */
   27938                 :   195792444 :   if (TREE_CODE (type) == ARRAY_TYPE)
   27939                 :             :     {
   27940                 :       11246 :       if (TYPE_DOMAIN (type)
   27941                 :       11246 :           && dependent_type_p (TYPE_DOMAIN (type)))
   27942                 :             :         return true;
   27943                 :       11246 :       return dependent_type_p (TREE_TYPE (type));
   27944                 :             :     }
   27945                 :             : 
   27946                 :             :   /* -- a template-id in which either the template name is a template
   27947                 :             :      parameter ...  */
   27948                 :   195781198 :   if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
   27949                 :             :     return true;
   27950                 :             :   /* ... or any of the template arguments is a dependent type or
   27951                 :             :         an expression that is type-dependent or value-dependent.  */
   27952                 :    99343458 :   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
   27953                 :   290604692 :            && (any_dependent_template_arguments_p
   27954                 :    95035795 :                (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
   27955                 :             :     return true;
   27956                 :             : 
   27957                 :             :   /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are
   27958                 :             :      dependent; if the argument of the `typeof' expression is not
   27959                 :             :      type-dependent, then it should already been have resolved.  */
   27960                 :   133527116 :   if (TREE_CODE (type) == TYPEOF_TYPE
   27961                 :   119171008 :       || TREE_CODE (type) == DECLTYPE_TYPE
   27962                 :   115939486 :       || TREE_CODE (type) == TRAIT_TYPE)
   27963                 :             :     return true;
   27964                 :             : 
   27965                 :             :   /* A template argument pack is dependent if any of its packed
   27966                 :             :      arguments are.  */
   27967                 :   115869525 :   if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
   27968                 :             :     {
   27969                 :         223 :       tree args = ARGUMENT_PACK_ARGS (type);
   27970                 :         271 :       for (tree arg : tree_vec_range (args))
   27971                 :         215 :         if (dependent_template_arg_p (arg))
   27972                 :         167 :           return true;
   27973                 :             :     }
   27974                 :             : 
   27975                 :             :   /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
   27976                 :             :      be template parameters.  */
   27977                 :   115869358 :   if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
   27978                 :             :     return true;
   27979                 :             : 
   27980                 :   105411055 :   if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE)
   27981                 :             :     return true;
   27982                 :             : 
   27983                 :    40274900 :   if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
   27984                 :             :     return true;
   27985                 :             : 
   27986                 :             :   /* The standard does not specifically mention types that are local
   27987                 :             :      to template functions or local classes, but they should be
   27988                 :             :      considered dependent too.  For example:
   27989                 :             : 
   27990                 :             :        template <int I> void f() {
   27991                 :             :          enum E { a = I };
   27992                 :             :          S<sizeof (E)> s;
   27993                 :             :        }
   27994                 :             : 
   27995                 :             :      The size of `E' cannot be known until the value of `I' has been
   27996                 :             :      determined.  Therefore, `E' must be considered dependent.  */
   27997                 :    40274900 :   scope = TYPE_CONTEXT (type);
   27998                 :    40274900 :   if (scope && TYPE_P (scope))
   27999                 :     2261773 :     return dependent_type_p (scope);
   28000                 :             :   /* Don't use type_dependent_expression_p here, as it can lead
   28001                 :             :      to infinite recursion trying to determine whether a lambda
   28002                 :             :      nested in a lambda is dependent (c++/47687).  */
   28003                 :    35769476 :   else if (scope && TREE_CODE (scope) == FUNCTION_DECL
   28004                 :      754328 :            && DECL_LANG_SPECIFIC (scope)
   28005                 :      754328 :            && DECL_TEMPLATE_INFO (scope)
   28006                 :      655058 :            && (any_dependent_template_arguments_p
   28007                 :      655058 :                (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
   28008                 :             :     return true;
   28009                 :             : 
   28010                 :             :   /* Other types are non-dependent.  */
   28011                 :             :   return false;
   28012                 :             : }
   28013                 :             : 
   28014                 :             : /* Returns TRUE if TYPE is dependent, in the sense of
   28015                 :             :    [temp.dep.type].  Note that a NULL type is considered dependent.  */
   28016                 :             : 
   28017                 :             : bool
   28018                 : 14039953415 : dependent_type_p (tree type)
   28019                 :             : {
   28020                 :             :   /* If there are no template parameters in scope, then there can't be
   28021                 :             :      any dependent types.  */
   28022                 : 14039953415 :   if (!processing_template_decl)
   28023                 :             :     {
   28024                 :             :       /* If we are not processing a template, then nobody should be
   28025                 :             :          providing us with a dependent type.  */
   28026                 :  2141981120 :       gcc_assert (type);
   28027                 :  2141981120 :       gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
   28028                 :  2141981120 :       return false;
   28029                 :             :     }
   28030                 :             : 
   28031                 :             :   /* If the type is NULL, we have not computed a type for the entity
   28032                 :             :      in question; in that case, the type is dependent.  */
   28033                 : 11897972295 :   if (!type)
   28034                 :             :     return true;
   28035                 :             : 
   28036                 :             :   /* Erroneous types can be considered non-dependent.  */
   28037                 : 11680074375 :   if (type == error_mark_node)
   28038                 :             :     return false;
   28039                 :             : 
   28040                 :             :   /* If we have not already computed the appropriate value for TYPE,
   28041                 :             :      do so now.  */
   28042                 : 11680074253 :   if (!TYPE_DEPENDENT_P_VALID (type))
   28043                 :             :     {
   28044                 :   440030441 :       TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
   28045                 :   440030441 :       TYPE_DEPENDENT_P_VALID (type) = 1;
   28046                 :             :     }
   28047                 :             : 
   28048                 : 11680074253 :   return TYPE_DEPENDENT_P (type);
   28049                 :             : }
   28050                 :             : 
   28051                 :             : /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
   28052                 :             :    lookup.  In other words, a dependent type that is not the current
   28053                 :             :    instantiation.  */
   28054                 :             : 
   28055                 :             : bool
   28056                 :   880888813 : dependent_scope_p (tree scope)
   28057                 :             : {
   28058                 :   880888813 :   return (scope && TYPE_P (scope) && dependent_type_p (scope)
   28059                 :  1418923792 :           && !currently_open_class (scope));
   28060                 :             : }
   28061                 :             : 
   28062                 :             : /* True if we might find more declarations in SCOPE during instantiation than
   28063                 :             :    we can when parsing the template.  */
   28064                 :             : 
   28065                 :             : bool
   28066                 :   174472424 : dependentish_scope_p (tree scope)
   28067                 :             : {
   28068                 :   174472424 :   return dependent_scope_p (scope) || any_dependent_bases_p (scope);
   28069                 :             : }
   28070                 :             : 
   28071                 :             : /* T is a SCOPE_REF.  Return whether it represents a non-static member of
   28072                 :             :    an unknown base of 'this' (and is therefore instantiation-dependent).  */
   28073                 :             : 
   28074                 :             : static bool
   28075                 :     5100016 : unknown_base_ref_p (tree t)
   28076                 :             : {
   28077                 :     5100016 :   if (!current_class_ptr)
   28078                 :             :     return false;
   28079                 :             : 
   28080                 :     2852998 :   tree mem = TREE_OPERAND (t, 1);
   28081                 :     2852998 :   if (shared_member_p (mem))
   28082                 :             :     return false;
   28083                 :             : 
   28084                 :           4 :   tree cur = current_nonlambda_class_type ();
   28085                 :           4 :   if (!any_dependent_bases_p (cur))
   28086                 :             :     return false;
   28087                 :             : 
   28088                 :           0 :   tree ctx = TREE_OPERAND (t, 0);
   28089                 :           0 :   if (DERIVED_FROM_P (ctx, cur))
   28090                 :             :     return false;
   28091                 :             : 
   28092                 :             :   return true;
   28093                 :             : }
   28094                 :             : 
   28095                 :             : /* T is a SCOPE_REF; return whether we need to consider it
   28096                 :             :     instantiation-dependent so that we can check access at instantiation
   28097                 :             :     time even though we know which member it resolves to.  */
   28098                 :             : 
   28099                 :             : static bool
   28100                 :    10521743 : instantiation_dependent_scope_ref_p (tree t)
   28101                 :             : {
   28102                 :    10521743 :   if (DECL_P (TREE_OPERAND (t, 1))
   28103                 :     5100024 :       && CLASS_TYPE_P (TREE_OPERAND (t, 0))
   28104                 :     5100024 :       && !dependent_scope_p (TREE_OPERAND (t, 0))
   28105                 :     5100016 :       && !unknown_base_ref_p (t)
   28106                 :    15621759 :       && accessible_in_template_p (TREE_OPERAND (t, 0),
   28107                 :     5100016 :                                    TREE_OPERAND (t, 1)))
   28108                 :             :     return false;
   28109                 :             :   else
   28110                 :     5421953 :     return true;
   28111                 :             : }
   28112                 :             : 
   28113                 :             : /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
   28114                 :             :    [temp.dep.constexpr].  EXPRESSION is already known to be a constant
   28115                 :             :    expression.  */
   28116                 :             : 
   28117                 :             : /* Note that this predicate is not appropriate for general expressions;
   28118                 :             :    only constant expressions (that satisfy potential_constant_expression)
   28119                 :             :    can be tested for value dependence.  */
   28120                 :             : 
   28121                 :             : bool
   28122                 :   996408091 : value_dependent_expression_p (tree expression)
   28123                 :             : {
   28124                 :  1017803843 :   if (!processing_template_decl || expression == NULL_TREE)
   28125                 :             :     return false;
   28126                 :             : 
   28127                 :             :   /* A type-dependent expression is also value-dependent.  */
   28128                 :   804872528 :   if (type_dependent_expression_p (expression))
   28129                 :             :     return true;
   28130                 :             : 
   28131                 :   671665591 :   switch (TREE_CODE (expression))
   28132                 :             :     {
   28133                 :     4622754 :     case BASELINK:
   28134                 :             :       /* A dependent member function of the current instantiation.  */
   28135                 :     4622754 :       return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
   28136                 :             : 
   28137                 :     1955941 :     case FUNCTION_DECL:
   28138                 :             :       /* A dependent member function of the current instantiation.  */
   28139                 :     3911882 :       if (DECL_CLASS_SCOPE_P (expression)
   28140                 :     1978225 :           && dependent_type_p (DECL_CONTEXT (expression)))
   28141                 :             :         return true;
   28142                 :             :       break;
   28143                 :             : 
   28144                 :             :     case IDENTIFIER_NODE:
   28145                 :             :       /* A name that has not been looked up -- must be dependent.  */
   28146                 :             :       return true;
   28147                 :             : 
   28148                 :             :     case TEMPLATE_PARM_INDEX:
   28149                 :             :       /* A non-type template parm.  */
   28150                 :             :       return true;
   28151                 :             : 
   28152                 :      634214 :     case CONST_DECL:
   28153                 :             :       /* A non-type template parm.  */
   28154                 :      634214 :       if (DECL_TEMPLATE_PARM_P (expression))
   28155                 :             :         return true;
   28156                 :      634214 :       return value_dependent_expression_p (DECL_INITIAL (expression));
   28157                 :             : 
   28158                 :    15336825 :     case VAR_DECL:
   28159                 :             :        /* A constant with literal type and is initialized
   28160                 :             :           with an expression that is value-dependent.  */
   28161                 :    15336825 :       if (DECL_DEPENDENT_INIT_P (expression))
   28162                 :             :         return true;
   28163                 :     8621296 :       if (DECL_HAS_VALUE_EXPR_P (expression))
   28164                 :             :         {
   28165                 :      100480 :           tree value_expr = DECL_VALUE_EXPR (expression);
   28166                 :      100480 :           if (value_dependent_expression_p (value_expr)
   28167                 :             :               /* __PRETTY_FUNCTION__ inside a template function is dependent
   28168                 :             :                  on the name of the function.  */
   28169                 :      100480 :               || (DECL_PRETTY_FUNCTION_P (expression)
   28170                 :             :                   /* It might be used in a template, but not a template
   28171                 :             :                      function, in which case its DECL_VALUE_EXPR will be
   28172                 :             :                      "top level".  */
   28173                 :           6 :                   && value_expr == error_mark_node))
   28174                 :             :             return true;
   28175                 :             :         }
   28176                 :     8520816 :       else if (TYPE_REF_P (TREE_TYPE (expression)))
   28177                 :             :         /* FIXME cp_finish_decl doesn't fold reference initializers.  */
   28178                 :             :         return true;
   28179                 :             :       /* We have a constexpr variable and we're processing a template.  When
   28180                 :             :          there's lifetime extension involved (for which finish_compound_literal
   28181                 :             :          used to create a temporary), we'll not be able to evaluate the
   28182                 :             :          variable until instantiating, so pretend it's value-dependent.  */
   28183                 :     8516962 :       else if (DECL_DECLARED_CONSTEXPR_P (expression)
   28184                 :     8516962 :                && !TREE_CONSTANT (expression))
   28185                 :             :         return true;
   28186                 :             :       return false;
   28187                 :             : 
   28188                 :    22833249 :     case DYNAMIC_CAST_EXPR:
   28189                 :    22833249 :     case STATIC_CAST_EXPR:
   28190                 :    22833249 :     case CONST_CAST_EXPR:
   28191                 :    22833249 :     case REINTERPRET_CAST_EXPR:
   28192                 :    22833249 :     case CAST_EXPR:
   28193                 :    22833249 :     case IMPLICIT_CONV_EXPR:
   28194                 :             :       /* These expressions are value-dependent if the type to which
   28195                 :             :          the cast occurs is dependent or the expression being casted
   28196                 :             :          is value-dependent.  */
   28197                 :    22833249 :       {
   28198                 :    22833249 :         tree type = TREE_TYPE (expression);
   28199                 :             : 
   28200                 :    22833249 :         if (dependent_type_p (type))
   28201                 :             :           return true;
   28202                 :             : 
   28203                 :             :         /* A functional cast has a list of operands.  */
   28204                 :    22833249 :         expression = TREE_OPERAND (expression, 0);
   28205                 :    22833249 :         if (!expression)
   28206                 :             :           {
   28207                 :             :             /* If there are no operands, it must be an expression such
   28208                 :             :                as "int()". This should not happen for aggregate types
   28209                 :             :                because it would form non-constant expressions.  */
   28210                 :         134 :             gcc_assert (cxx_dialect >= cxx11
   28211                 :             :                         || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
   28212                 :             : 
   28213                 :             :             return false;
   28214                 :             :           }
   28215                 :             : 
   28216                 :    22833115 :         if (TREE_CODE (expression) == TREE_LIST)
   28217                 :     2071558 :           return any_value_dependent_elements_p (expression);
   28218                 :             : 
   28219                 :    20761557 :         if (TREE_CODE (type) == REFERENCE_TYPE
   28220                 :    20761557 :             && has_value_dependent_address (expression))
   28221                 :             :           return true;
   28222                 :             : 
   28223                 :    20761538 :         return value_dependent_expression_p (expression);
   28224                 :             :       }
   28225                 :             : 
   28226                 :     8194563 :     case SIZEOF_EXPR:
   28227                 :     8194563 :       if (SIZEOF_EXPR_TYPE_P (expression))
   28228                 :     1705135 :         return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
   28229                 :             :       /* FALLTHRU */
   28230                 :     7109625 :     case ALIGNOF_EXPR:
   28231                 :     7109625 :     case TYPEID_EXPR:
   28232                 :             :       /* A `sizeof' expression is value-dependent if the operand is
   28233                 :             :          type-dependent or is a pack expansion.  */
   28234                 :     7109625 :       expression = TREE_OPERAND (expression, 0);
   28235                 :     7109625 :       if (PACK_EXPANSION_P (expression))
   28236                 :             :         return true;
   28237                 :     3615768 :       else if (TYPE_P (expression))
   28238                 :     3465649 :         return dependent_type_p (expression);
   28239                 :      150119 :       return instantiation_dependent_uneval_expression_p (expression);
   28240                 :             : 
   28241                 :           0 :     case AT_ENCODE_EXPR:
   28242                 :             :       /* An 'encode' expression is value-dependent if the operand is
   28243                 :             :          type-dependent.  */
   28244                 :           0 :       expression = TREE_OPERAND (expression, 0);
   28245                 :           0 :       return dependent_type_p (expression);
   28246                 :             : 
   28247                 :      425923 :     case NOEXCEPT_EXPR:
   28248                 :      425923 :       expression = TREE_OPERAND (expression, 0);
   28249                 :      425923 :       return instantiation_dependent_uneval_expression_p (expression);
   28250                 :             : 
   28251                 :     3002001 :     case SCOPE_REF:
   28252                 :             :       /* All instantiation-dependent expressions should also be considered
   28253                 :             :          value-dependent.  */
   28254                 :     3002001 :       return instantiation_dependent_scope_ref_p (expression);
   28255                 :             : 
   28256                 :      852162 :     case COMPONENT_REF:
   28257                 :      852162 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   28258                 :      852162 :               || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
   28259                 :             : 
   28260                 :           0 :     case NONTYPE_ARGUMENT_PACK:
   28261                 :             :       /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
   28262                 :             :          is value-dependent.  */
   28263                 :           0 :       for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression)))
   28264                 :           0 :         if (value_dependent_expression_p (arg))
   28265                 :           0 :           return true;
   28266                 :           0 :       return false;
   28267                 :             : 
   28268                 :    13259967 :     case TRAIT_EXPR:
   28269                 :    13259967 :       {
   28270                 :    13259967 :         if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
   28271                 :             :           return true;
   28272                 :             : 
   28273                 :       22836 :         tree type2 = TRAIT_EXPR_TYPE2 (expression);
   28274                 :       22836 :         if (!type2)
   28275                 :             :           return false;
   28276                 :             : 
   28277                 :       22832 :         if (TREE_CODE (type2) != TREE_VEC)
   28278                 :       22792 :           return dependent_type_p (type2);
   28279                 :             : 
   28280                 :          52 :         for (tree arg : tree_vec_range (type2))
   28281                 :          37 :           if (dependent_type_p (arg))
   28282                 :          25 :             return true;
   28283                 :             : 
   28284                 :          15 :         return false;
   28285                 :             :       }
   28286                 :             : 
   28287                 :          42 :     case MODOP_EXPR:
   28288                 :          42 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   28289                 :          42 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
   28290                 :             : 
   28291                 :      218676 :     case ARRAY_REF:
   28292                 :      218676 :       return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
   28293                 :      218676 :               || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
   28294                 :             : 
   28295                 :       51195 :     case ADDR_EXPR:
   28296                 :       51195 :       {
   28297                 :       51195 :         tree op = TREE_OPERAND (expression, 0);
   28298                 :       51195 :         return (value_dependent_expression_p (op)
   28299                 :       51195 :                 || has_value_dependent_address (op));
   28300                 :             :       }
   28301                 :             : 
   28302                 :             :     case REQUIRES_EXPR:
   28303                 :             :       /* Treat all requires-expressions as value-dependent so
   28304                 :             :          we don't try to fold them.  */
   28305                 :             :       return true;
   28306                 :             : 
   28307                 :           0 :     case TYPE_REQ:
   28308                 :           0 :       return dependent_type_p (TREE_OPERAND (expression, 0));
   28309                 :             : 
   28310                 :     6565465 :     case CALL_EXPR:
   28311                 :     6565465 :       {
   28312                 :     6565465 :         if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
   28313                 :             :           return true;
   28314                 :     6374881 :         tree fn = get_callee_fndecl (expression);
   28315                 :     6374881 :         int i, nargs;
   28316                 :     6374881 :         nargs = call_expr_nargs (expression);
   28317                 :     7764183 :         for (i = 0; i < nargs; ++i)
   28318                 :             :           {
   28319                 :     1899579 :             tree op = CALL_EXPR_ARG (expression, i);
   28320                 :             :             /* In a call to a constexpr member function, look through the
   28321                 :             :                implicit ADDR_EXPR on the object argument so that it doesn't
   28322                 :             :                cause the call to be considered value-dependent.  We also
   28323                 :             :                look through it in potential_constant_expression.  */
   28324                 :        1952 :             if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
   28325                 :        1952 :                 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
   28326                 :     1899581 :                 && TREE_CODE (op) == ADDR_EXPR)
   28327                 :           2 :               op = TREE_OPERAND (op, 0);
   28328                 :     1899579 :             if (value_dependent_expression_p (op))
   28329                 :             :               return true;
   28330                 :             :           }
   28331                 :             :         return false;
   28332                 :             :       }
   28333                 :             : 
   28334                 :     1854628 :     case TEMPLATE_ID_EXPR:
   28335                 :     1854628 :       return concept_definition_p (TREE_OPERAND (expression, 0))
   28336                 :     1854628 :         && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1));
   28337                 :             : 
   28338                 :     2312580 :     case CONSTRUCTOR:
   28339                 :     2312580 :       {
   28340                 :     2312580 :         unsigned ix;
   28341                 :     2312580 :         tree val;
   28342                 :     2312580 :         if (dependent_type_p (TREE_TYPE (expression)))
   28343                 :             :           return true;
   28344                 :     2368013 :         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
   28345                 :       55649 :           if (value_dependent_expression_p (val))
   28346                 :             :             return true;
   28347                 :             :         return false;
   28348                 :             :       }
   28349                 :             : 
   28350                 :             :     case STMT_EXPR:
   28351                 :             :       /* Treat a GNU statement expression as dependent to avoid crashing
   28352                 :             :          under instantiate_non_dependent_expr; it can't be constant.  */
   28353                 :             :       return true;
   28354                 :             : 
   28355                 :           8 :     case NEW_EXPR:
   28356                 :           8 :     case VEC_NEW_EXPR:
   28357                 :             :       /* The second operand is a type, which type_dependent_expression_p
   28358                 :             :          (and therefore value_dependent_expression_p) doesn't want to see.  */
   28359                 :           8 :       return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
   28360                 :           8 :               || value_dependent_expression_p (TREE_OPERAND (expression, 2))
   28361                 :          16 :               || value_dependent_expression_p (TREE_OPERAND (expression, 3)));
   28362                 :             : 
   28363                 :   489602561 :     default:
   28364                 :             :       /* A constant expression is value-dependent if any subexpression is
   28365                 :             :          value-dependent.  */
   28366                 :   489602561 :       switch (TREE_CODE_CLASS (TREE_CODE (expression)))
   28367                 :             :         {
   28368                 :    52554254 :         case tcc_reference:
   28369                 :    52554254 :         case tcc_unary:
   28370                 :    52554254 :         case tcc_comparison:
   28371                 :    52554254 :         case tcc_binary:
   28372                 :    52554254 :         case tcc_expression:
   28373                 :    52554254 :         case tcc_vl_exp:
   28374                 :    52554254 :           {
   28375                 :    52554254 :             int i, len = cp_tree_operand_length (expression);
   28376                 :             : 
   28377                 :    92791308 :             for (i = 0; i < len; i++)
   28378                 :             :               {
   28379                 :    59481964 :                 tree t = TREE_OPERAND (expression, i);
   28380                 :             : 
   28381                 :             :                 /* In some cases, some of the operands may be missing.
   28382                 :             :                    (For example, in the case of PREDECREMENT_EXPR, the
   28383                 :             :                    amount to increment by may be missing.)  That doesn't
   28384                 :             :                    make the expression dependent.  */
   28385                 :    59481964 :                 if (t && value_dependent_expression_p (t))
   28386                 :             :                   return true;
   28387                 :             :               }
   28388                 :             :           }
   28389                 :             :           break;
   28390                 :             :         default:
   28391                 :             :           break;
   28392                 :             :         }
   28393                 :             :       break;
   28394                 :             :     }
   28395                 :             : 
   28396                 :             :   /* The expression is not value-dependent.  */
   28397                 :             :   return false;
   28398                 :             : }
   28399                 :             : 
   28400                 :             : /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
   28401                 :             :    [temp.dep.expr].  Note that an expression with no type is
   28402                 :             :    considered dependent.  Other parts of the compiler arrange for an
   28403                 :             :    expression with type-dependent subexpressions to have no type, so
   28404                 :             :    this function doesn't have to be fully recursive.  */
   28405                 :             : 
   28406                 :             : bool
   28407                 :  3291001354 : type_dependent_expression_p (tree expression)
   28408                 :             : {
   28409                 :  3291001532 :   if (!processing_template_decl)
   28410                 :             :     return false;
   28411                 :             : 
   28412                 :  2845894707 :   if (expression == NULL_TREE || expression == error_mark_node)
   28413                 :             :     return false;
   28414                 :             : 
   28415                 :  2845035817 :   gcc_checking_assert (!TYPE_P (expression));
   28416                 :             : 
   28417                 :  2845035817 :   STRIP_ANY_LOCATION_WRAPPER (expression);
   28418                 :             : 
   28419                 :             :   /* An unresolved name is always dependent.  */
   28420                 :  2845035817 :   if (identifier_p (expression)
   28421                 :             :       || TREE_CODE (expression) == USING_DECL
   28422                 :             :       || TREE_CODE (expression) == WILDCARD_DECL)
   28423                 :             :     return true;
   28424                 :             : 
   28425                 :             :   /* A lambda-expression in template context is dependent.  dependent_type_p is
   28426                 :             :      true for a lambda in the scope of a class or function template, but that
   28427                 :             :      doesn't cover all template contexts, like a default template argument.  */
   28428                 :             :   if (TREE_CODE (expression) == LAMBDA_EXPR)
   28429                 :             :     return true;
   28430                 :             : 
   28431                 :             :   /* A fold expression is type-dependent. */
   28432                 :             :   if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
   28433                 :             :       || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
   28434                 :             :       || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
   28435                 :             :       || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
   28436                 :             :     return true;
   28437                 :             : 
   28438                 :             :   /* Some expression forms are never type-dependent.  */
   28439                 :             :   if (TREE_CODE (expression) == SIZEOF_EXPR
   28440                 :             :       || TREE_CODE (expression) == ALIGNOF_EXPR
   28441                 :             :       || TREE_CODE (expression) == AT_ENCODE_EXPR
   28442                 :             :       || TREE_CODE (expression) == NOEXCEPT_EXPR
   28443                 :             :       || TREE_CODE (expression) == TRAIT_EXPR
   28444                 :             :       || TREE_CODE (expression) == TYPEID_EXPR
   28445                 :             :       || TREE_CODE (expression) == DELETE_EXPR
   28446                 :             :       || TREE_CODE (expression) == VEC_DELETE_EXPR
   28447                 :             :       || TREE_CODE (expression) == THROW_EXPR
   28448                 :             :       || TREE_CODE (expression) == REQUIRES_EXPR)
   28449                 :             :     return false;
   28450                 :             : 
   28451                 :             :   /* The types of these expressions depends only on the type to which
   28452                 :             :      the cast occurs.  */
   28453                 :             :   if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
   28454                 :             :       || TREE_CODE (expression) == STATIC_CAST_EXPR
   28455                 :             :       || TREE_CODE (expression) == CONST_CAST_EXPR
   28456                 :             :       || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
   28457                 :             :       || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
   28458                 :             :       || TREE_CODE (expression) == CAST_EXPR)
   28459                 :    52297932 :     return dependent_type_p (TREE_TYPE (expression));
   28460                 :             : 
   28461                 :             :   /* The types of these expressions depends only on the type created
   28462                 :             :      by the expression.  */
   28463                 :             :   if (TREE_CODE (expression) == NEW_EXPR
   28464                 :             :       || TREE_CODE (expression) == VEC_NEW_EXPR)
   28465                 :             :     {
   28466                 :             :       /* For NEW_EXPR tree nodes created inside a template, either
   28467                 :             :          the object type itself or a TREE_LIST may appear as the
   28468                 :             :          operand 1.  */
   28469                 :      456445 :       tree type = TREE_OPERAND (expression, 1);
   28470                 :      456445 :       if (TREE_CODE (type) == TREE_LIST)
   28471                 :             :         /* This is an array type.  We need to check array dimensions
   28472                 :             :            as well.  */
   28473                 :           0 :         return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
   28474                 :           0 :                || value_dependent_expression_p
   28475                 :           0 :                     (TREE_OPERAND (TREE_VALUE (type), 1));
   28476                 :             :       /* Array type whose dimension has to be deduced.  */
   28477                 :      456445 :       else if (TREE_CODE (type) == ARRAY_TYPE
   28478                 :      456445 :                && TREE_OPERAND (expression, 2) == NULL_TREE)
   28479                 :             :         return true;
   28480                 :             :       else
   28481                 :      456432 :         return dependent_type_p (type);
   28482                 :             :     }
   28483                 :             : 
   28484                 :             :   if (TREE_CODE (expression) == SCOPE_REF)
   28485                 :             :     {
   28486                 :    96469811 :       tree scope = TREE_OPERAND (expression, 0);
   28487                 :    96469811 :       tree name = TREE_OPERAND (expression, 1);
   28488                 :             : 
   28489                 :             :       /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
   28490                 :             :          contains an identifier associated by name lookup with one or more
   28491                 :             :          declarations declared with a dependent type, or...a
   28492                 :             :          nested-name-specifier or qualified-id that names a member of an
   28493                 :             :          unknown specialization.  */
   28494                 :    96469811 :       return (type_dependent_expression_p (name)
   28495                 :    96469811 :               || dependent_scope_p (scope));
   28496                 :             :     }
   28497                 :             : 
   28498                 :             :   if (TREE_CODE (expression) == TEMPLATE_DECL
   28499                 :   244766296 :       && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
   28500                 :   244766296 :     return uses_outer_template_parms (expression);
   28501                 :             : 
   28502                 :  2333295739 :   if (TREE_CODE (expression) == STMT_EXPR)
   28503                 :         188 :     expression = stmt_expr_value_expr (expression);
   28504                 :             : 
   28505                 :  2333295739 :   if (BRACE_ENCLOSED_INITIALIZER_P (expression))
   28506                 :             :     {
   28507                 :     7243506 :       for (auto &elt : CONSTRUCTOR_ELTS (expression))
   28508                 :      901461 :         if (type_dependent_expression_p (elt.value))
   28509                 :             :           return true;
   28510                 :             :       return false;
   28511                 :             :     }
   28512                 :             : 
   28513                 :             :   /* A static data member of the current instantiation with incomplete
   28514                 :             :      array type is type-dependent, as the definition and specializations
   28515                 :             :      can have different bounds.  */
   28516                 :  2327894648 :   if (VAR_P (expression)
   28517                 :   327104001 :       && DECL_CLASS_SCOPE_P (expression)
   28518                 :    42528479 :       && dependent_type_p (DECL_CONTEXT (expression))
   28519                 :  2343688294 :       && VAR_HAD_UNKNOWN_BOUND (expression))
   28520                 :             :     return true;
   28521                 :             : 
   28522                 :             :   /* An array of unknown bound depending on a variadic parameter, eg:
   28523                 :             : 
   28524                 :             :      template<typename... Args>
   28525                 :             :        void foo (Args... args)
   28526                 :             :        {
   28527                 :             :          int arr[] = { args... };
   28528                 :             :        }
   28529                 :             : 
   28530                 :             :      template<int... vals>
   28531                 :             :        void bar ()
   28532                 :             :        {
   28533                 :             :          int arr[] = { vals... };
   28534                 :             :        }
   28535                 :             : 
   28536                 :             :      If the array has no length and has an initializer, it must be that
   28537                 :             :      we couldn't determine its length in cp_complete_array_type because
   28538                 :             :      it is dependent.  */
   28539                 :   327103784 :   if (((VAR_P (expression) && DECL_INITIAL (expression))
   28540                 :  2037421660 :        || COMPOUND_LITERAL_P (expression))
   28541                 :   292038494 :       && TREE_TYPE (expression) != NULL_TREE
   28542                 :   292038494 :       && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
   28543                 :  2328647043 :       && !TYPE_DOMAIN (TREE_TYPE (expression)))
   28544                 :             :    return true;
   28545                 :             : 
   28546                 :             :   /* Pull a FUNCTION_DECL out of a BASELINK if we can.  */
   28547                 :  2327717897 :   if (BASELINK_P (expression))
   28548                 :             :     {
   28549                 :    20782785 :       if (BASELINK_OPTYPE (expression)
   28550                 :    20782785 :           && dependent_type_p (BASELINK_OPTYPE (expression)))
   28551                 :             :         return true;
   28552                 :    20782738 :       expression = BASELINK_FUNCTIONS (expression);
   28553                 :             :     }
   28554                 :             : 
   28555                 :             :   /* A function or variable template-id is type-dependent if it has any
   28556                 :             :      dependent template arguments.  */
   28557                 :  2000790594 :   if (VAR_OR_FUNCTION_DECL_P (expression)
   28558                 :   494705654 :       && DECL_LANG_SPECIFIC (expression)
   28559                 :  2568649156 :       && DECL_TEMPLATE_INFO (expression))
   28560                 :             :     {
   28561                 :             :       /* Consider the innermost template arguments, since those are the ones
   28562                 :             :          that come from the template-id; the template arguments for the
   28563                 :             :          enclosing class do not make it type-dependent unless they are used in
   28564                 :             :          the type of the decl.  */
   28565                 :    95870955 :       if (instantiates_primary_template_p (expression)
   28566                 :   102994858 :           && (any_dependent_template_arguments_p
   28567                 :     7123903 :               (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
   28568                 :             :         return true;
   28569                 :             :     }
   28570                 :             : 
   28571                 :             :   /* Otherwise, if the function decl isn't from a dependent scope, it can't be
   28572                 :             :      type-dependent.  Checking this is important for functions with auto return
   28573                 :             :      type, which looks like a dependent type.  */
   28574                 :  2325694541 :   if (TREE_CODE (expression) == FUNCTION_DECL
   28575                 :   235520991 :       && !(DECL_CLASS_SCOPE_P (expression)
   28576                 :    69765896 :            && dependent_type_p (DECL_CONTEXT (expression)))
   28577                 :   110473789 :       && !(DECL_LANG_SPECIFIC (expression)
   28578                 :   110473789 :            && DECL_UNIQUE_FRIEND_P (expression)
   28579                 :      684102 :            && (!DECL_FRIEND_CONTEXT (expression)
   28580                 :      302063 :                || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
   28581                 :  2436128342 :       && !DECL_LOCAL_DECL_P (expression))
   28582                 :             :     {
   28583                 :   110400448 :       gcc_assert (!dependent_type_p (TREE_TYPE (expression))
   28584                 :             :                   || undeduced_auto_decl (expression));
   28585                 :   110400448 :       return false;
   28586                 :             :     }
   28587                 :             : 
   28588                 :             :   /* Otherwise, its constraints could still depend on outer template parameters
   28589                 :             :      from its (dependent) scope.  */
   28590                 :  2215294093 :   if (TREE_CODE (expression) == FUNCTION_DECL
   28591                 :             :       /* As an optimization, check this cheaper sufficient condition first.
   28592                 :             :          (At this point we've established that we're looking at a member of
   28593                 :             :          a dependent class, so it makes sense to start treating say undeduced
   28594                 :             :          auto as dependent.)  */
   28595                 :    55354647 :       && !dependent_type_p (TREE_TYPE (expression))
   28596                 :  2217660959 :       && uses_outer_template_parms_in_constraints (expression))
   28597                 :             :     return true;
   28598                 :             : 
   28599                 :             :   /* Always dependent, on the number of arguments if nothing else.  */
   28600                 :  2215294091 :   if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
   28601                 :             :     return true;
   28602                 :             : 
   28603                 :  2211910134 :   if (TREE_TYPE (expression) == unknown_type_node)
   28604                 :             :     {
   28605                 :   171745388 :       if (TREE_CODE (expression) == ADDR_EXPR)
   28606                 :         178 :         return type_dependent_expression_p (TREE_OPERAND (expression, 0));
   28607                 :   171745210 :       if (TREE_CODE (expression) == COMPONENT_REF
   28608                 :   136167879 :           || TREE_CODE (expression) == OFFSET_REF)
   28609                 :             :         {
   28610                 :    35577491 :           if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0)))
   28611                 :             :             return true;
   28612                 :    35577491 :           expression = TREE_OPERAND (expression, 1);
   28613                 :   745275463 :           if (identifier_p (expression))
   28614                 :             :             return false;
   28615                 :             :         }
   28616                 :             :       /* SCOPE_REF with non-null TREE_TYPE is always non-dependent.  */
   28617                 :   171745210 :       if (TREE_CODE (expression) == SCOPE_REF)
   28618                 :             :         return false;
   28619                 :             : 
   28620                 :             :       /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent.  */
   28621                 :   171745210 :       if (TREE_CODE (expression) == CO_AWAIT_EXPR
   28622                 :   171745210 :           || TREE_CODE (expression) == CO_YIELD_EXPR)
   28623                 :             :         return true;
   28624                 :             : 
   28625                 :   171743991 :       if (BASELINK_P (expression))
   28626                 :             :         {
   28627                 :    35577491 :           if (BASELINK_OPTYPE (expression)
   28628                 :    35577491 :               && dependent_type_p (BASELINK_OPTYPE (expression)))
   28629                 :             :             return true;
   28630                 :    35577484 :           expression = BASELINK_FUNCTIONS (expression);
   28631                 :             :         }
   28632                 :             : 
   28633                 :   171743984 :       if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
   28634                 :             :         {
   28635                 :    48703737 :           if (any_dependent_template_arguments_p
   28636                 :    48703737 :               (TREE_OPERAND (expression, 1)))
   28637                 :             :             return true;
   28638                 :    12642648 :           expression = TREE_OPERAND (expression, 0);
   28639                 :   256262256 :           if (identifier_p (expression))
   28640                 :             :             return true;
   28641                 :             :         }
   28642                 :             : 
   28643                 :   135682895 :       gcc_assert (OVL_P (expression));
   28644                 :             : 
   28645                 :   426142211 :       for (lkp_iterator iter (expression); iter; ++iter)
   28646                 :   334441760 :         if (type_dependent_expression_p (*iter))
   28647                 :    43982444 :           return true;
   28648                 :             : 
   28649                 :    91700451 :       return false;
   28650                 :             :     }
   28651                 :             : 
   28652                 :             :   /* The type of a non-type template parm declared with a placeholder type
   28653                 :             :      depends on the corresponding template argument, even though
   28654                 :             :      placeholders are not normally considered dependent.  */
   28655                 :  2040164746 :   if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
   28656                 :  2040164746 :       && is_auto (TREE_TYPE (expression)))
   28657                 :             :     return true;
   28658                 :             : 
   28659                 :  2040163174 :   gcc_assert (TREE_CODE (expression) != TYPE_DECL);
   28660                 :             : 
   28661                 :             :   /* Dependent type attributes might not have made it from the decl to
   28662                 :             :      the type yet.  */
   28663                 :  2040163174 :   if (DECL_P (expression)
   28664                 :  2040163174 :       && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
   28665                 :             :     return true;
   28666                 :             : 
   28667                 :  2040163114 :   return (dependent_type_p (TREE_TYPE (expression)));
   28668                 :             : }
   28669                 :             : 
   28670                 :             : /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
   28671                 :             :    type-dependent if the expression refers to a member of the current
   28672                 :             :    instantiation and the type of the referenced member is dependent, or the
   28673                 :             :    class member access expression refers to a member of an unknown
   28674                 :             :    specialization.
   28675                 :             : 
   28676                 :             :    This function returns true if the OBJECT in such a class member access
   28677                 :             :    expression is of an unknown specialization.  */
   28678                 :             : 
   28679                 :             : bool
   28680                 :   269220551 : type_dependent_object_expression_p (tree object)
   28681                 :             : {
   28682                 :             :   /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
   28683                 :             :      dependent.  */
   28684                 :   269220551 :   if (TREE_CODE (object) == IDENTIFIER_NODE)
   28685                 :             :     return true;
   28686                 :   269220551 :   tree scope = TREE_TYPE (object);
   28687                 :   269220551 :   return (!scope || dependent_scope_p (scope));
   28688                 :             : }
   28689                 :             : 
   28690                 :             : /* walk_tree callback function for instantiation_dependent_expression_p,
   28691                 :             :    below.  Returns non-zero if a dependent subexpression is found.  */
   28692                 :             : 
   28693                 :             : static tree
   28694                 :   345015644 : instantiation_dependent_r (tree *tp, int *walk_subtrees,
   28695                 :             :                            void * /*data*/)
   28696                 :             : {
   28697                 :   345015644 :   if (TYPE_P (*tp))
   28698                 :             :     {
   28699                 :             :       /* We don't have to worry about decltype currently because decltype
   28700                 :             :          of an instantiation-dependent expr is a dependent type.  This
   28701                 :             :          might change depending on the resolution of DR 1172.  */
   28702                 :    17964085 :       *walk_subtrees = false;
   28703                 :    17964085 :       return NULL_TREE;
   28704                 :             :     }
   28705                 :   327051559 :   enum tree_code code = TREE_CODE (*tp);
   28706                 :   327051559 :   switch (code)
   28707                 :             :     {
   28708                 :             :       /* Don't treat an argument list as dependent just because it has no
   28709                 :             :          TREE_TYPE.  */
   28710                 :             :     case TREE_LIST:
   28711                 :             :     case TREE_VEC:
   28712                 :             :     case NONTYPE_ARGUMENT_PACK:
   28713                 :             :       return NULL_TREE;
   28714                 :             : 
   28715                 :    30288165 :     case TEMPLATE_PARM_INDEX:
   28716                 :    30288165 :       if (dependent_type_p (TREE_TYPE (*tp)))
   28717                 :         234 :         return *tp;
   28718                 :    30287931 :       if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
   28719                 :             :         return *tp;
   28720                 :             :       /* We'll check value-dependence separately.  */
   28721                 :             :       return NULL_TREE;
   28722                 :             : 
   28723                 :             :       /* Handle expressions with type operands.  */
   28724                 :     4874979 :     case SIZEOF_EXPR:
   28725                 :     4874979 :     case ALIGNOF_EXPR:
   28726                 :     4874979 :     case TYPEID_EXPR:
   28727                 :     4874979 :     case AT_ENCODE_EXPR:
   28728                 :     4874979 :       {
   28729                 :     4874979 :         tree op = TREE_OPERAND (*tp, 0);
   28730                 :     4874979 :         if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
   28731                 :     1705135 :           op = TREE_TYPE (op);
   28732                 :     4874979 :         if (TYPE_P (op))
   28733                 :             :           {
   28734                 :     4830113 :             if (dependent_type_p (op))
   28735                 :     2958843 :               return *tp;
   28736                 :             :             else
   28737                 :             :               {
   28738                 :     1871270 :                 *walk_subtrees = false;
   28739                 :     1871270 :                 return NULL_TREE;
   28740                 :             :               }
   28741                 :             :           }
   28742                 :             :         break;
   28743                 :             :       }
   28744                 :             : 
   28745                 :      177152 :     case COMPONENT_REF:
   28746                 :      177152 :       if (identifier_p (TREE_OPERAND (*tp, 1)))
   28747                 :             :         /* In a template, finish_class_member_access_expr creates a
   28748                 :             :            COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
   28749                 :             :            type-dependent, so that we can check access control at
   28750                 :             :            instantiation time (PR 42277).  See also Core issue 1273.  */
   28751                 :             :         return *tp;
   28752                 :             :       break;
   28753                 :             : 
   28754                 :     7519742 :     case SCOPE_REF:
   28755                 :     7519742 :       if (instantiation_dependent_scope_ref_p (*tp))
   28756                 :     5421927 :         return *tp;
   28757                 :             :       else
   28758                 :             :         break;
   28759                 :             : 
   28760                 :             :       /* Treat statement-expressions as dependent.  */
   28761                 :             :     case BIND_EXPR:
   28762                 :             :       return *tp;
   28763                 :             : 
   28764                 :             :       /* Treat requires-expressions as dependent. */
   28765                 :             :     case REQUIRES_EXPR:
   28766                 :             :       return *tp;
   28767                 :             : 
   28768                 :     3101680 :     case CONSTRUCTOR:
   28769                 :     3101680 :       if (CONSTRUCTOR_IS_DEPENDENT (*tp))
   28770                 :             :         return *tp;
   28771                 :             :       break;
   28772                 :             : 
   28773                 :     3499747 :     case TEMPLATE_DECL:
   28774                 :     3499747 :     case FUNCTION_DECL:
   28775                 :             :       /* Before C++17, a noexcept-specifier isn't part of the function type
   28776                 :             :          so it doesn't affect type dependence, but we still want to consider it
   28777                 :             :          for instantiation dependence.  */
   28778                 :     3499747 :       if (cxx_dialect < cxx17
   28779                 :       14720 :           && DECL_DECLARES_FUNCTION_P (*tp)
   28780                 :     3514463 :           && value_dependent_noexcept_spec_p (TREE_TYPE (*tp)))
   28781                 :           4 :         return *tp;
   28782                 :             :       break;
   28783                 :             : 
   28784                 :             :     default:
   28785                 :             :       break;
   28786                 :             :     }
   28787                 :             : 
   28788                 :   281923256 :   if (type_dependent_expression_p (*tp))
   28789                 :    19524540 :     return *tp;
   28790                 :             :   else
   28791                 :             :     return NULL_TREE;
   28792                 :             : }
   28793                 :             : 
   28794                 :             : /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
   28795                 :             :    sense defined by the ABI:
   28796                 :             : 
   28797                 :             :    "An expression is instantiation-dependent if it is type-dependent
   28798                 :             :    or value-dependent, or it has a subexpression that is type-dependent
   28799                 :             :    or value-dependent."
   28800                 :             : 
   28801                 :             :    Except don't actually check value-dependence for unevaluated expressions,
   28802                 :             :    because in sizeof(i) we don't care about the value of i.  Checking
   28803                 :             :    type-dependence will in turn check value-dependence of array bounds/template
   28804                 :             :    arguments as needed.  */
   28805                 :             : 
   28806                 :             : bool
   28807                 :   780638511 : instantiation_dependent_uneval_expression_p (tree expression)
   28808                 :             : {
   28809                 :   780638511 :   tree result;
   28810                 :             : 
   28811                 :   780638511 :   if (!processing_template_decl)
   28812                 :             :     return false;
   28813                 :             : 
   28814                 :   279687025 :   if (expression == error_mark_node)
   28815                 :             :     return false;
   28816                 :             : 
   28817                 :   279687012 :   result = cp_walk_tree_without_duplicates (&expression,
   28818                 :             :                                             instantiation_dependent_r, NULL);
   28819                 :   279687012 :   return result != NULL_TREE;
   28820                 :             : }
   28821                 :             : 
   28822                 :             : /* As above, but also check value-dependence of the expression as a whole.  */
   28823                 :             : 
   28824                 :             : bool
   28825                 :   732827626 : instantiation_dependent_expression_p (tree expression)
   28826                 :             : {
   28827                 :   732827626 :   return (instantiation_dependent_uneval_expression_p (expression)
   28828                 :   732827626 :           || (processing_template_decl
   28829                 :   222456710 :               && potential_constant_expression (expression)
   28830                 :   222456632 :               && value_dependent_expression_p (expression)));
   28831                 :             : }
   28832                 :             : 
   28833                 :             : /* Like type_dependent_expression_p, but it also works while not processing
   28834                 :             :    a template definition, i.e. during substitution or mangling.  */
   28835                 :             : 
   28836                 :             : bool
   28837                 :     5759648 : type_dependent_expression_p_push (tree expr)
   28838                 :             : {
   28839                 :     5759648 :   bool b;
   28840                 :     5759648 :   ++processing_template_decl;
   28841                 :     5759648 :   b = type_dependent_expression_p (expr);
   28842                 :     5759648 :   --processing_template_decl;
   28843                 :     5759648 :   return b;
   28844                 :             : }
   28845                 :             : 
   28846                 :             : /* Returns TRUE if ARGS contains a type-dependent expression.  */
   28847                 :             : 
   28848                 :             : bool
   28849                 :   101622767 : any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
   28850                 :             : {
   28851                 :   101622767 :   if (!processing_template_decl || !args)
   28852                 :             :     return false;
   28853                 :             : 
   28854                 :   109194234 :   for (tree arg : *args)
   28855                 :    79698866 :     if (type_dependent_expression_p (arg))
   28856                 :             :       return true;
   28857                 :             : 
   28858                 :             :   return false;
   28859                 :             : }
   28860                 :             : 
   28861                 :             : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   28862                 :             :    expressions) contains any type-dependent expressions.  */
   28863                 :             : 
   28864                 :             : bool
   28865                 :           0 : any_type_dependent_elements_p (const_tree list)
   28866                 :             : {
   28867                 :           0 :   for (; list; list = TREE_CHAIN (list))
   28868                 :           0 :     if (type_dependent_expression_p (TREE_VALUE (list)))
   28869                 :             :       return true;
   28870                 :             : 
   28871                 :             :   return false;
   28872                 :             : }
   28873                 :             : 
   28874                 :             : /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
   28875                 :             :    expressions) contains any value-dependent expressions.  */
   28876                 :             : 
   28877                 :             : bool
   28878                 :     2071605 : any_value_dependent_elements_p (const_tree list)
   28879                 :             : {
   28880                 :     3024314 :   for (; list; list = TREE_CHAIN (list))
   28881                 :     2071828 :     if (value_dependent_expression_p (TREE_VALUE (list)))
   28882                 :             :       return true;
   28883                 :             : 
   28884                 :             :   return false;
   28885                 :             : }
   28886                 :             : 
   28887                 :             : /* Returns TRUE if the ARG (a template argument) is dependent.  */
   28888                 :             : 
   28889                 :             : bool
   28890                 :  3506652367 : dependent_template_arg_p (tree arg)
   28891                 :             : {
   28892                 :  3506652367 :   if (!processing_template_decl)
   28893                 :             :     return false;
   28894                 :             : 
   28895                 :             :   /* Assume a template argument that was wrongly written by the user
   28896                 :             :      is dependent. This is consistent with what
   28897                 :             :      any_dependent_template_arguments_p [that calls this function]
   28898                 :             :      does.  */
   28899                 :  3227796320 :   if (!arg || arg == error_mark_node)
   28900                 :             :     return true;
   28901                 :             : 
   28902                 :  3227795893 :   if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
   28903                 :           0 :     arg = argument_pack_select_arg (arg);
   28904                 :             : 
   28905                 :  3227795893 :   if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
   28906                 :             :     return true;
   28907                 :  3227530386 :   if (TREE_CODE (arg) == TEMPLATE_DECL)
   28908                 :             :     {
   28909                 :     9796372 :       if (DECL_TEMPLATE_PARM_P (arg))
   28910                 :             :         return true;
   28911                 :             :       /* A member template of a dependent class is not necessarily
   28912                 :             :          type-dependent, but it is a dependent template argument because it
   28913                 :             :          will be a member of an unknown specialization to that template.  */
   28914                 :     9791918 :       tree scope = CP_DECL_CONTEXT (arg);
   28915                 :     9791918 :       return TYPE_P (scope) && dependent_type_p (scope);
   28916                 :             :     }
   28917                 :  3217734014 :   else if (ARGUMENT_PACK_P (arg))
   28918                 :             :     {
   28919                 :   128043915 :       tree args = ARGUMENT_PACK_ARGS (arg);
   28920                 :   286772893 :       for (tree arg : tree_vec_range (args))
   28921                 :   203014878 :         if (dependent_template_arg_p (arg))
   28922                 :    44285900 :           return true;
   28923                 :    83758015 :       return false;
   28924                 :             :     }
   28925                 :  3089690099 :   else if (TYPE_P (arg))
   28926                 :  2754626210 :     return dependent_type_p (arg);
   28927                 :             :   else
   28928                 :   335063889 :     return value_dependent_expression_p (arg);
   28929                 :             : }
   28930                 :             : 
   28931                 :             : /* Identify any expressions that use function parms.  */
   28932                 :             : 
   28933                 :             : static tree
   28934                 :   442308272 : find_parm_usage_r (tree *tp, int *walk_subtrees, void*)
   28935                 :             : {
   28936                 :   442308272 :   tree t = *tp;
   28937                 :   442308272 :   if (TREE_CODE (t) == PARM_DECL)
   28938                 :             :     {
   28939                 :       69717 :       *walk_subtrees = 0;
   28940                 :       69717 :       return t;
   28941                 :             :     }
   28942                 :             :   return NULL_TREE;
   28943                 :             : }
   28944                 :             : 
   28945                 :             : /* Returns true if a type specialization formed using the template
   28946                 :             :    arguments ARGS needs to use structural equality.  */
   28947                 :             : 
   28948                 :             : bool
   28949                 :    83341752 : any_template_arguments_need_structural_equality_p (tree args)
   28950                 :             : {
   28951                 :    83341752 :   int i;
   28952                 :    83341752 :   int j;
   28953                 :             : 
   28954                 :    83341752 :   if (!args)
   28955                 :             :     return false;
   28956                 :    83341752 :   if (args == error_mark_node)
   28957                 :             :     return true;
   28958                 :             : 
   28959                 :   329425332 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   28960                 :             :     {
   28961                 :    85386785 :       tree level = TMPL_ARGS_LEVEL (args, i + 1);
   28962                 :   240227642 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   28963                 :             :         {
   28964                 :   155850433 :           tree arg = TREE_VEC_ELT (level, j);
   28965                 :   155850433 :           tree packed_args = NULL_TREE;
   28966                 :   155850433 :           int k, len = 1;
   28967                 :             : 
   28968                 :   155850433 :           if (ARGUMENT_PACK_P (arg))
   28969                 :             :             {
   28970                 :             :               /* Look inside the argument pack.  */
   28971                 :    12947411 :               packed_args = ARGUMENT_PACK_ARGS (arg);
   28972                 :    12947411 :               len = TREE_VEC_LENGTH (packed_args);
   28973                 :             :             }
   28974                 :             : 
   28975                 :   322485579 :           for (k = 0; k < len; ++k)
   28976                 :             :             {
   28977                 :   167644722 :               if (packed_args)
   28978                 :    24741700 :                 arg = TREE_VEC_ELT (packed_args, k);
   28979                 :             : 
   28980                 :   167644722 :               if (error_operand_p (arg))
   28981                 :     1009576 :                 return true;
   28982                 :   167644722 :               else if (TREE_CODE (arg) == TEMPLATE_DECL)
   28983                 :     3908573 :                 continue;
   28984                 :   163736149 :               else if (arg == any_targ_node)
   28985                 :             :                 /* An any_targ_node argument (added by add_defaults_to_ttp)
   28986                 :             :                    makes the corresponding specialization not canonicalizable,
   28987                 :             :                    since template_args_equal always return true for it.  We
   28988                 :             :                    may see this when called from bind_template_template_parm.  */
   28989                 :             :                 return true;
   28990                 :             :               /* Checking current_function_decl because this structural
   28991                 :             :                  comparison is only necessary for redeclaration.  */
   28992                 :   163736145 :               else if (!current_function_decl
   28993                 :   148519344 :                        && dependent_template_arg_p (arg)
   28994                 :   235116043 :                        && (cp_walk_tree_without_duplicates
   28995                 :             :                            (&arg, find_parm_usage_r, NULL)))
   28996                 :             :                 /* The identity of a class template specialization that uses
   28997                 :             :                    a function parameter depends on the identity of the function.
   28998                 :             :                    And if this specialization appeared in the trailing return
   28999                 :             :                    type thereof, we don't know the identity of the function
   29000                 :             :                    (e.g. if it's a redeclaration or a new function) until we
   29001                 :             :                    form its signature and go through duplicate_decls.  Thus
   29002                 :             :                    it's unsafe to decide on a canonical type now (which depends
   29003                 :             :                    on the DECL_CONTEXT of the function parameter, which can get
   29004                 :             :                    mutated after the fact by duplicate_decls), so just require
   29005                 :             :                    structural equality in this case (PR52830).  */
   29006                 :             :                 return true;
   29007                 :   163666428 :               else if (TYPE_P (arg)
   29008                 :   135063066 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg)
   29009                 :   178789301 :                        && dependent_alias_template_spec_p (arg, nt_transparent))
   29010                 :             :                 /* Require structural equality for specializations written
   29011                 :             :                    in terms of a dependent alias template specialization.  */
   29012                 :             :                 return true;
   29013                 :    48254436 :               else if (CLASS_TYPE_P (arg)
   29014                 :    47377103 :                        && TYPE_TEMPLATE_INFO (arg)
   29015                 :   199825262 :                        && TYPE_STRUCTURAL_EQUALITY_P (arg))
   29016                 :             :                 /* Require structural equality for specializations written
   29017                 :             :                    in terms of a class template specialization that itself
   29018                 :             :                    needs structural equality.  */
   29019                 :             :                 return true;
   29020                 :             :             }
   29021                 :             :         }
   29022                 :             :     }
   29023                 :             : 
   29024                 :             :   return false;
   29025                 :             : }
   29026                 :             : 
   29027                 :             : /* Returns true if ARGS (a collection of template arguments) contains
   29028                 :             :    any dependent arguments.  */
   29029                 :             : 
   29030                 :             : bool
   29031                 :  1958314252 : any_dependent_template_arguments_p (const_tree args)
   29032                 :             : {
   29033                 :  1958314252 :   if (args == error_mark_node)
   29034                 :             :     return true;
   29035                 :  1958314252 :   if (!processing_template_decl || !args)
   29036                 :             :     return false;
   29037                 :             : 
   29038                 :  4413617925 :   for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i)
   29039                 :             :     {
   29040                 :  1845184877 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   29041                 :  3882348445 :       for (tree arg : tree_vec_range (CONST_CAST_TREE (level)))
   29042                 :  2883997325 :         if (dependent_template_arg_p (arg))
   29043                 :   846833757 :           return true;
   29044                 :             :     }
   29045                 :             : 
   29046                 :             :   return false;
   29047                 :             : }
   29048                 :             : 
   29049                 :             : /* Returns true if ARGS contains any errors.  */
   29050                 :             : 
   29051                 :             : bool
   29052                 :    21381531 : any_erroneous_template_args_p (const_tree args)
   29053                 :             : {
   29054                 :    21381531 :   int i;
   29055                 :    21381531 :   int j;
   29056                 :             : 
   29057                 :    21381531 :   if (args == error_mark_node)
   29058                 :             :     return true;
   29059                 :             : 
   29060                 :    21381489 :   if (args && TREE_CODE (args) != TREE_VEC)
   29061                 :             :     {
   29062                 :    21381489 :       if (tree ti = get_template_info (args))
   29063                 :    17433678 :         args = TI_ARGS (ti);
   29064                 :             :       else
   29065                 :             :         args = NULL_TREE;
   29066                 :             :     }
   29067                 :             : 
   29068                 :    17433678 :   if (!args)
   29069                 :     3947811 :     return false;
   29070                 :             : 
   29071                 :    69637303 :   for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
   29072                 :             :     {
   29073                 :    17530847 :       const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
   29074                 :    48308689 :       for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
   29075                 :    30777970 :         if (error_operand_p (TREE_VEC_ELT (level, j)))
   29076                 :             :           return true;
   29077                 :             :     }
   29078                 :             : 
   29079                 :             :   return false;
   29080                 :             : }
   29081                 :             : 
   29082                 :             : /* Returns TRUE if the template TMPL is type-dependent.  */
   29083                 :             : 
   29084                 :             : bool
   29085                 :     1485557 : dependent_template_p (tree tmpl)
   29086                 :             : {
   29087                 :     1485557 :   if (TREE_CODE (tmpl) == OVERLOAD)
   29088                 :             :     {
   29089                 :     1476226 :       for (lkp_iterator iter (tmpl); iter; ++iter)
   29090                 :     1004438 :         if (dependent_template_p (*iter))
   29091                 :           0 :           return true;
   29092                 :      471788 :       return false;
   29093                 :             :     }
   29094                 :             : 
   29095                 :             :   /* Template template parameters are dependent.  */
   29096                 :      829689 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
   29097                 :     1843458 :       || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
   29098                 :             :     return true;
   29099                 :             :   /* So are names that have not been looked up.  */
   29100                 :     1023088 :   if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
   29101                 :             :     return true;
   29102                 :             :   return false;
   29103                 :             : }
   29104                 :             : 
   29105                 :             : /* Returns TRUE if the specialization TMPL<ARGS> is dependent.  */
   29106                 :             : 
   29107                 :             : bool
   29108                 :      481119 : dependent_template_id_p (tree tmpl, tree args)
   29109                 :             : {
   29110                 :      481119 :   return (dependent_template_p (tmpl)
   29111                 :      481119 :           || any_dependent_template_arguments_p (args));
   29112                 :             : }
   29113                 :             : 
   29114                 :             : /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
   29115                 :             :    are dependent.  */
   29116                 :             : 
   29117                 :             : bool
   29118                 :       21182 : dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
   29119                 :             : {
   29120                 :       21182 :   int i;
   29121                 :             : 
   29122                 :       21182 :   if (!processing_template_decl)
   29123                 :             :     return false;
   29124                 :             : 
   29125                 :        2151 :   for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
   29126                 :             :     {
   29127                 :        1373 :       tree decl = TREE_VEC_ELT (declv, i);
   29128                 :        1373 :       tree init = TREE_VEC_ELT (initv, i);
   29129                 :        1373 :       tree cond = TREE_VEC_ELT (condv, i);
   29130                 :        1373 :       tree incr = TREE_VEC_ELT (incrv, i);
   29131                 :             : 
   29132                 :        1373 :       if (type_dependent_expression_p (decl)
   29133                 :        1373 :           || TREE_CODE (decl) == SCOPE_REF)
   29134                 :             :         return true;
   29135                 :             : 
   29136                 :        1047 :       if (init && type_dependent_expression_p (init))
   29137                 :             :         return true;
   29138                 :             : 
   29139                 :        1047 :       if (cond == global_namespace)
   29140                 :             :         return true;
   29141                 :             : 
   29142                 :        1006 :       if (type_dependent_expression_p (cond))
   29143                 :             :         return true;
   29144                 :             : 
   29145                 :         986 :       if (COMPARISON_CLASS_P (cond)
   29146                 :         986 :           && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
   29147                 :         982 :               || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
   29148                 :          13 :         return true;
   29149                 :             : 
   29150                 :         973 :       if (TREE_CODE (incr) == MODOP_EXPR)
   29151                 :             :         {
   29152                 :           1 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
   29153                 :           1 :               || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
   29154                 :           0 :             return true;
   29155                 :             :         }
   29156                 :         972 :       else if (type_dependent_expression_p (incr))
   29157                 :             :         return true;
   29158                 :         972 :       else if (TREE_CODE (incr) == MODIFY_EXPR)
   29159                 :             :         {
   29160                 :         284 :           if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
   29161                 :             :             return true;
   29162                 :         284 :           else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
   29163                 :             :             {
   29164                 :         284 :               tree t = TREE_OPERAND (incr, 1);
   29165                 :         284 :               if (type_dependent_expression_p (TREE_OPERAND (t, 0))
   29166                 :         284 :                   || type_dependent_expression_p (TREE_OPERAND (t, 1)))
   29167                 :           5 :                 return true;
   29168                 :             : 
   29169                 :             :               /* If this loop has a class iterator with != comparison
   29170                 :             :                  with increment other than i++/++i/i--/--i, make sure the
   29171                 :             :                  increment is constant.  */
   29172                 :         558 :               if (CLASS_TYPE_P (TREE_TYPE (decl))
   29173                 :         340 :                   && TREE_CODE (cond) == NE_EXPR)
   29174                 :             :                 {
   29175                 :           8 :                   if (TREE_OPERAND (t, 0) == decl)
   29176                 :           8 :                     t = TREE_OPERAND (t, 1);
   29177                 :             :                   else
   29178                 :           0 :                     t = TREE_OPERAND (t, 0);
   29179                 :           8 :                   if (TREE_CODE (t) != INTEGER_CST)
   29180                 :             :                     return true;
   29181                 :             :                 }
   29182                 :             :             }
   29183                 :             :         }
   29184                 :             :     }
   29185                 :             : 
   29186                 :             :   return false;
   29187                 :             : }
   29188                 :             : 
   29189                 :             : /* TYPE is a TYPENAME_TYPE.  Returns the ordinary TYPE to which the
   29190                 :             :    TYPENAME_TYPE corresponds.  Returns the original TYPENAME_TYPE if
   29191                 :             :    no such TYPE can be found.  Note that this function peers inside
   29192                 :             :    uninstantiated templates and therefore should be used only in
   29193                 :             :    extremely limited situations.  ONLY_CURRENT_P restricts this
   29194                 :             :    peering to the currently open classes hierarchy (which is required
   29195                 :             :    when comparing types).  */
   29196                 :             : 
   29197                 :             : tree
   29198                 :   169078750 : resolve_typename_type (tree type, bool only_current_p)
   29199                 :             : {
   29200                 :   169078750 :   tree scope;
   29201                 :   169078750 :   tree name;
   29202                 :   169078750 :   tree decl;
   29203                 :   169078750 :   int quals;
   29204                 :   169078750 :   tree pushed_scope;
   29205                 :   169078750 :   tree result;
   29206                 :             : 
   29207                 :   169078750 :   gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
   29208                 :             : 
   29209                 :   169078750 :   scope = TYPE_CONTEXT (type);
   29210                 :             :   /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope.  */
   29211                 :   169078750 :   gcc_checking_assert (uses_template_parms (scope));
   29212                 :             : 
   29213                 :             :   /* Usually the non-qualified identifier of a TYPENAME_TYPE is
   29214                 :             :      TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
   29215                 :             :      TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
   29216                 :             :      representing the typedef. In that case TYPE_IDENTIFIER (type) is
   29217                 :             :      not the non-qualified identifier of the TYPENAME_TYPE anymore.
   29218                 :             :      So by getting the TYPE_IDENTIFIER of the _main declaration_ of
   29219                 :             :      the TYPENAME_TYPE instead, we avoid messing up with a possible
   29220                 :             :      typedef variant case.  */
   29221                 :   169078750 :   name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
   29222                 :             : 
   29223                 :             :   /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
   29224                 :             :      it first before we can figure out what NAME refers to.  */
   29225                 :   169078750 :   if (TREE_CODE (scope) == TYPENAME_TYPE)
   29226                 :             :     {
   29227                 :     2913430 :       if (TYPENAME_IS_RESOLVING_P (scope))
   29228                 :             :         /* Given a class template A with a dependent base with nested type C,
   29229                 :             :            typedef typename A::C::C C will land us here, as trying to resolve
   29230                 :             :            the initial A::C leads to the local C typedef, which leads back to
   29231                 :             :            A::C::C.  So we break the recursion now.  */
   29232                 :             :         return type;
   29233                 :             :       else
   29234                 :     2913430 :         scope = resolve_typename_type (scope, only_current_p);
   29235                 :             :     }
   29236                 :             :   /* If we don't know what SCOPE refers to, then we cannot resolve the
   29237                 :             :      TYPENAME_TYPE.  */
   29238                 :   169078750 :   if (!CLASS_TYPE_P (scope))
   29239                 :             :     return type;
   29240                 :             :   /* If this is a typedef, we don't want to look inside (c++/11987).  */
   29241                 :   146132803 :   if (typedef_variant_p (type))
   29242                 :             :     return type;
   29243                 :             :   /* If SCOPE isn't the template itself, it will not have a valid
   29244                 :             :      TYPE_FIELDS list.  */
   29245                 :   101226609 :   if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
   29246                 :             :     /* scope is either the template itself or a compatible instantiation
   29247                 :             :        like X<T>, so look up the name in the original template.  */
   29248                 :    38607826 :     scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
   29249                 :             :   /* If scope has no fields, it can't be a current instantiation.  Check this
   29250                 :             :      before currently_open_class to avoid infinite recursion (71515).  */
   29251                 :   101226609 :   if (!TYPE_FIELDS (scope))
   29252                 :             :     return type;
   29253                 :             :   /* If the SCOPE is not the current instantiation, there's no reason
   29254                 :             :      to look inside it.  */
   29255                 :    38259554 :   if (only_current_p && !currently_open_class (scope))
   29256                 :             :     return type;
   29257                 :             :   /* Enter the SCOPE so that name lookup will be resolved as if we
   29258                 :             :      were in the class definition.  In particular, SCOPE will no
   29259                 :             :      longer be considered a dependent type.  */
   29260                 :       91868 :   pushed_scope = push_scope (scope);
   29261                 :             :   /* Look up the declaration.  */
   29262                 :       91868 :   decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
   29263                 :             :                         tf_warning_or_error);
   29264                 :             : 
   29265                 :       91868 :   result = NULL_TREE;
   29266                 :             : 
   29267                 :             :   /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
   29268                 :             :      find a TEMPLATE_DECL.  Otherwise, we want to find a TYPE_DECL.  */
   29269                 :       91868 :   tree fullname = TYPENAME_TYPE_FULLNAME (type);
   29270                 :       91868 :   if (!decl)
   29271                 :             :     /*nop*/;
   29272                 :       91838 :   else if (identifier_p (fullname)
   29273                 :       91831 :            && TREE_CODE (decl) == TYPE_DECL)
   29274                 :             :     {
   29275                 :       91831 :       result = TREE_TYPE (decl);
   29276                 :       91831 :       if (result == error_mark_node)
   29277                 :          33 :         result = NULL_TREE;
   29278                 :             :     }
   29279                 :           7 :   else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
   29280                 :           7 :            && DECL_CLASS_TEMPLATE_P (decl))
   29281                 :             :     {
   29282                 :             :       /* Obtain the template and the arguments.  */
   29283                 :           4 :       tree tmpl = TREE_OPERAND (fullname, 0);
   29284                 :           4 :       if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
   29285                 :             :         {
   29286                 :             :           /* We get here with a plain identifier because a previous tentative
   29287                 :             :              parse of the nested-name-specifier as part of a ptr-operator saw
   29288                 :             :              ::template X<A>.  The use of ::template is necessary in a
   29289                 :             :              ptr-operator, but wrong in a declarator-id.
   29290                 :             : 
   29291                 :             :              [temp.names]: In a qualified-id of a declarator-id, the keyword
   29292                 :             :              template shall not appear at the top level.  */
   29293                 :           4 :           pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
   29294                 :             :                    "keyword %<template%> not allowed in declarator-id");
   29295                 :           4 :           tmpl = decl;
   29296                 :             :         }
   29297                 :           4 :       tree args = TREE_OPERAND (fullname, 1);
   29298                 :             :       /* Instantiate the template.  */
   29299                 :           4 :       result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
   29300                 :             :                                       /*entering_scope=*/true,
   29301                 :             :                                       tf_error | tf_user);
   29302                 :           4 :       if (result == error_mark_node)
   29303                 :          33 :         result = NULL_TREE;
   29304                 :             :     }
   29305                 :             : 
   29306                 :             :   /* Leave the SCOPE.  */
   29307                 :       91868 :   if (pushed_scope)
   29308                 :       91852 :     pop_scope (pushed_scope);
   29309                 :             : 
   29310                 :             :   /* If we failed to resolve it, return the original typename.  */
   29311                 :       91868 :   if (!result)
   29312                 :             :     return type;
   29313                 :             : 
   29314                 :             :   /* If lookup found a typename type, resolve that too.  */
   29315                 :       91835 :   if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
   29316                 :             :     {
   29317                 :             :       /* Ill-formed programs can cause infinite recursion here, so we
   29318                 :             :          must catch that.  */
   29319                 :           6 :       TYPENAME_IS_RESOLVING_P (result) = 1;
   29320                 :           6 :       result = resolve_typename_type (result, only_current_p);
   29321                 :           6 :       TYPENAME_IS_RESOLVING_P (result) = 0;
   29322                 :             :     }
   29323                 :             : 
   29324                 :             :   /* Qualify the resulting type.  */
   29325                 :       91835 :   quals = cp_type_quals (type);
   29326                 :       91835 :   if (quals)
   29327                 :           0 :     result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
   29328                 :             : 
   29329                 :             :   return result;
   29330                 :             : }
   29331                 :             : 
   29332                 :             : /* Returns a type which represents 'auto' or 'decltype(auto)'.  We use a
   29333                 :             :    TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms,
   29334                 :             :    by default.  If set_canonical is true, we set TYPE_CANONICAL on it.  */
   29335                 :             : 
   29336                 :             : static tree
   29337                 :    18050362 : make_auto_1 (tree name, bool set_canonical, int level = -1)
   29338                 :             : {
   29339                 :    18050362 :   if (level == -1)
   29340                 :     9877679 :     level = current_template_depth + 1;
   29341                 :    18050362 :   tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
   29342                 :    18050362 :   TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
   29343                 :    18050362 :   TYPE_STUB_DECL (au) = TYPE_NAME (au);
   29344                 :    18050362 :   TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
   29345                 :    18050362 :     (0, level, level, TYPE_NAME (au), NULL_TREE);
   29346                 :    18050362 :   if (set_canonical)
   29347                 :     9607569 :     TYPE_CANONICAL (au) = canonical_type_parameter (au);
   29348                 :    18050362 :   DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
   29349                 :    18050362 :   SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
   29350                 :    18050362 :   if (name == decltype_auto_identifier)
   29351                 :      544414 :     AUTO_IS_DECLTYPE (au) = true;
   29352                 :             : 
   29353                 :    18050362 :   return au;
   29354                 :             : }
   29355                 :             : 
   29356                 :             : tree
   29357                 :      275721 : make_decltype_auto (void)
   29358                 :             : {
   29359                 :      275721 :   return make_auto_1 (decltype_auto_identifier, true);
   29360                 :             : }
   29361                 :             : 
   29362                 :             : tree
   29363                 :     9328173 : make_auto (void)
   29364                 :             : {
   29365                 :     9328173 :   return make_auto_1 (auto_identifier, true);
   29366                 :             : }
   29367                 :             : 
   29368                 :             : /* Return a C++17 deduction placeholder for class template TMPL.
   29369                 :             :    There are represented as an 'auto' with the special level 0 and
   29370                 :             :    CLASS_PLACEHOLDER_TEMPLATE set.  */
   29371                 :             : 
   29372                 :             : tree
   29373                 :     8169008 : make_template_placeholder (tree tmpl)
   29374                 :             : {
   29375                 :     8169008 :   tree t = make_auto_1 (auto_identifier, false, /*level=*/0);
   29376                 :     8169008 :   CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
   29377                 :             :   /* Our canonical type depends on the placeholder.  */
   29378                 :     8169008 :   TYPE_CANONICAL (t) = canonical_type_parameter (t);
   29379                 :     8169008 :   return t;
   29380                 :             : }
   29381                 :             : 
   29382                 :             : /* True iff T is a C++17 class template deduction placeholder.  */
   29383                 :             : 
   29384                 :             : bool
   29385                 :  1391778984 : template_placeholder_p (tree t)
   29386                 :             : {
   29387                 :  1406578298 :   return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
   29388                 :             : }
   29389                 :             : 
   29390                 :             : /* Return an auto for an explicit cast expression auto(x).
   29391                 :             :    Like CTAD placeholders, these have level 0 so that they're
   29392                 :             :    not accidentally replaced via tsubst and are always directly
   29393                 :             :    resolved via do_auto_deduction.  */
   29394                 :             : 
   29395                 :             : tree
   29396                 :        3675 : make_cast_auto ()
   29397                 :             : {
   29398                 :        3675 :   return make_auto_1 (auto_identifier, true, /*level=*/0);
   29399                 :             : }
   29400                 :             : 
   29401                 :             : /* Make a "constrained auto" type-specifier. This is an auto or
   29402                 :             :   decltype(auto) type with constraints that must be associated after
   29403                 :             :   deduction.  The constraint is formed from the given concept CON
   29404                 :             :   and its optional sequence of template arguments ARGS.
   29405                 :             : 
   29406                 :             :   TYPE must be the result of make_auto_type or make_decltype_auto_type. */
   29407                 :             : 
   29408                 :             : static tree
   29409                 :      273751 : make_constrained_placeholder_type (tree type, tree con, tree args)
   29410                 :             : {
   29411                 :             :   /* Build the constraint. */
   29412                 :      273751 :   tree tmpl = DECL_TI_TEMPLATE (con);
   29413                 :      273751 :   tree expr = tmpl;
   29414                 :      273751 :   if (TREE_CODE (con) == FUNCTION_DECL)
   29415                 :          10 :     expr = ovl_make (tmpl);
   29416                 :      273751 :   ++processing_template_decl;
   29417                 :      273751 :   expr = build_concept_check (expr, type, args, tf_warning_or_error);
   29418                 :      273751 :   --processing_template_decl;
   29419                 :             : 
   29420                 :      273751 :   PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
   29421                 :      273751 :     = build_tree_list (current_template_parms, expr);
   29422                 :             : 
   29423                 :             :   /* Our canonical type depends on the constraint.  */
   29424                 :      273751 :   TYPE_CANONICAL (type) = canonical_type_parameter (type);
   29425                 :             : 
   29426                 :             :   /* Attach the constraint to the type declaration. */
   29427                 :      273751 :   return TYPE_NAME (type);
   29428                 :             : }
   29429                 :             : 
   29430                 :             : /* Make a "constrained auto" type-specifier.  */
   29431                 :             : 
   29432                 :             : tree
   29433                 :        5065 : make_constrained_auto (tree con, tree args)
   29434                 :             : {
   29435                 :        5065 :   tree type = make_auto_1 (auto_identifier, false);
   29436                 :        5065 :   return make_constrained_placeholder_type (type, con, args);
   29437                 :             : }
   29438                 :             : 
   29439                 :             : /* Make a "constrained decltype(auto)" type-specifier.  */
   29440                 :             : 
   29441                 :             : tree
   29442                 :      268686 : make_constrained_decltype_auto (tree con, tree args)
   29443                 :             : {
   29444                 :      268686 :   tree type = make_auto_1 (decltype_auto_identifier, false);
   29445                 :      268686 :   return make_constrained_placeholder_type (type, con, args);
   29446                 :             : }
   29447                 :             : 
   29448                 :             : /* Returns true if the placeholder type constraint T has any dependent
   29449                 :             :    (explicit) template arguments.  */
   29450                 :             : 
   29451                 :             : static bool
   29452                 :          20 : placeholder_type_constraint_dependent_p (tree t)
   29453                 :             : {
   29454                 :          20 :   tree id = unpack_concept_check (t);
   29455                 :          20 :   tree args = TREE_OPERAND (id, 1);
   29456                 :          20 :   tree first = TREE_VEC_ELT (args, 0);
   29457                 :          20 :   if (ARGUMENT_PACK_P (first))
   29458                 :             :     {
   29459                 :           6 :       args = expand_template_argument_pack (args);
   29460                 :           6 :       first = TREE_VEC_ELT (args, 0);
   29461                 :             :     }
   29462                 :          20 :   gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
   29463                 :             :                        || is_auto (first));
   29464                 :          26 :   for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
   29465                 :          14 :     if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
   29466                 :             :       return true;
   29467                 :             :   return false;
   29468                 :             : }
   29469                 :             : 
   29470                 :             : /* Build and return a concept definition. Like other templates, the
   29471                 :             :    CONCEPT_DECL node is wrapped by a TEMPLATE_DECL.  This returns the
   29472                 :             :    the TEMPLATE_DECL. */
   29473                 :             : 
   29474                 :             : tree
   29475                 :      542344 : finish_concept_definition (cp_expr id, tree init, tree attrs)
   29476                 :             : {
   29477                 :      542344 :   gcc_assert (identifier_p (id));
   29478                 :      542344 :   gcc_assert (processing_template_decl);
   29479                 :             : 
   29480                 :      542344 :   location_t loc = id.get_location();
   29481                 :             : 
   29482                 :             :   /* A concept-definition shall not have associated constraints.  */
   29483                 :      542344 :   if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
   29484                 :             :     {
   29485                 :           1 :       error_at (loc, "a concept cannot be constrained");
   29486                 :           1 :       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
   29487                 :             :     }
   29488                 :             : 
   29489                 :             :   /* A concept-definition shall appear in namespace scope.  Templates
   29490                 :             :      aren't allowed in block scope, so we only need to check for class
   29491                 :             :      scope.  */
   29492                 :      542344 :   if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
   29493                 :             :     {
   29494                 :           2 :       error_at (loc, "concept %qE not in namespace scope", *id);
   29495                 :           2 :       return error_mark_node;
   29496                 :             :     }
   29497                 :             : 
   29498                 :      542342 :   if (current_template_depth > 1)
   29499                 :             :     {
   29500                 :           1 :       error_at (loc, "concept %qE has multiple template parameter lists", *id);
   29501                 :           1 :       return error_mark_node;
   29502                 :             :     }
   29503                 :             : 
   29504                 :             :   /* Initially build the concept declaration; its type is bool.  */
   29505                 :      542341 :   tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
   29506                 :      542341 :   DECL_CONTEXT (decl) = current_scope ();
   29507                 :      542341 :   DECL_INITIAL (decl) = init;
   29508                 :             : 
   29509                 :      542341 :   if (attrs)
   29510                 :           4 :     cplus_decl_attributes (&decl, attrs, 0);
   29511                 :             : 
   29512                 :      542341 :   set_originating_module (decl, false);
   29513                 :             : 
   29514                 :             :   /* Push the enclosing template.  */
   29515                 :      542341 :   return push_template_decl (decl);
   29516                 :             : }
   29517                 :             : 
   29518                 :             : /* Given type ARG, return std::initializer_list<ARG>.  */
   29519                 :             : 
   29520                 :             : static tree
   29521                 :         314 : listify (tree arg)
   29522                 :             : {
   29523                 :         314 :   tree std_init_list = lookup_qualified_name (std_node, init_list_identifier);
   29524                 :             : 
   29525                 :         314 :   if (std_init_list == error_mark_node
   29526                 :         314 :       || !DECL_CLASS_TEMPLATE_P (std_init_list))
   29527                 :             :     {
   29528                 :          15 :       gcc_rich_location richloc (input_location);
   29529                 :          15 :       maybe_add_include_fixit (&richloc, "<initializer_list>", false);
   29530                 :          15 :       error_at (&richloc,
   29531                 :             :                 "deducing from brace-enclosed initializer list"
   29532                 :             :                 " requires %<#include <initializer_list>%>");
   29533                 :             : 
   29534                 :          15 :       return error_mark_node;
   29535                 :          15 :     }
   29536                 :         299 :   tree argvec = make_tree_vec (1);
   29537                 :         299 :   TREE_VEC_ELT (argvec, 0) = arg;
   29538                 :             : 
   29539                 :         299 :   return lookup_template_class (std_init_list, argvec, NULL_TREE,
   29540                 :         299 :                                 NULL_TREE, 0, tf_warning_or_error);
   29541                 :             : }
   29542                 :             : 
   29543                 :             : /* Replace auto in TYPE with std::initializer_list<auto>.  */
   29544                 :             : 
   29545                 :             : static tree
   29546                 :         314 : listify_autos (tree type, tree auto_node)
   29547                 :             : {
   29548                 :         314 :   tree init_auto = listify (strip_top_quals (auto_node));
   29549                 :         314 :   tree argvec = make_tree_vec (1);
   29550                 :         314 :   TREE_VEC_ELT (argvec, 0) = init_auto;
   29551                 :         314 :   if (processing_template_decl)
   29552                 :           3 :     argvec = add_to_template_args (current_template_args (), argvec);
   29553                 :         314 :   return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
   29554                 :             : }
   29555                 :             : 
   29556                 :             : /* Hash traits for hashing possibly constrained 'auto'
   29557                 :             :    TEMPLATE_TYPE_PARMs for use by do_auto_deduction.  */
   29558                 :             : 
   29559                 :             : struct auto_hash : default_hash_traits<tree>
   29560                 :             : {
   29561                 :             :   static inline hashval_t hash (tree);
   29562                 :             :   static inline bool equal (tree, tree);
   29563                 :             : };
   29564                 :             : 
   29565                 :             : /* Hash the 'auto' T.  */
   29566                 :             : 
   29567                 :             : inline hashval_t
   29568                 :         396 : auto_hash::hash (tree t)
   29569                 :             : {
   29570                 :         396 :   if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
   29571                 :             :     /* Matching constrained-type-specifiers denote the same template
   29572                 :             :        parameter, so hash the constraint.  */
   29573                 :          30 :     return hash_placeholder_constraint (c);
   29574                 :             :   else
   29575                 :             :     /* But unconstrained autos are all separate, so just hash the pointer.  */
   29576                 :         366 :     return iterative_hash_object (t, 0);
   29577                 :             : }
   29578                 :             : 
   29579                 :             : /* Compare two 'auto's.  */
   29580                 :             : 
   29581                 :             : inline bool
   29582                 :          14 : auto_hash::equal (tree t1, tree t2)
   29583                 :             : {
   29584                 :          14 :   if (t1 == t2)
   29585                 :             :     return true;
   29586                 :             : 
   29587                 :          14 :   tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
   29588                 :          14 :   tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
   29589                 :             : 
   29590                 :             :   /* Two unconstrained autos are distinct.  */
   29591                 :          14 :   if (!c1 || !c2)
   29592                 :             :     return false;
   29593                 :             : 
   29594                 :           5 :   return equivalent_placeholder_constraints (c1, c2);
   29595                 :             : }
   29596                 :             : 
   29597                 :             : /* for_each_template_parm callback for extract_autos: if t is a (possibly
   29598                 :             :    constrained) auto, add it to the vector.  */
   29599                 :             : 
   29600                 :             : static int
   29601                 :         385 : extract_autos_r (tree t, void *data)
   29602                 :             : {
   29603                 :         385 :   hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
   29604                 :         385 :   if (is_auto (t) && !template_placeholder_p (t))
   29605                 :             :     {
   29606                 :             :       /* All the autos were built with index 0; fix that up now.  */
   29607                 :         385 :       tree *p = hash.find_slot (t, INSERT);
   29608                 :         385 :       int idx;
   29609                 :         385 :       if (*p)
   29610                 :             :         /* If this is a repeated constrained-type-specifier, use the index we
   29611                 :             :            chose before.  */
   29612                 :           5 :         idx = TEMPLATE_TYPE_IDX (*p);
   29613                 :             :       else
   29614                 :             :         {
   29615                 :             :           /* Otherwise this is new, so use the current count.  */
   29616                 :         380 :           *p = t;
   29617                 :         380 :           idx = hash.elements () - 1;
   29618                 :             :         }
   29619                 :         385 :       if (idx != TEMPLATE_TYPE_IDX (t))
   29620                 :             :         {
   29621                 :           6 :           gcc_checking_assert (TEMPLATE_TYPE_IDX (t) == 0);
   29622                 :           6 :           gcc_checking_assert (TYPE_CANONICAL (t) != t);
   29623                 :           6 :           TEMPLATE_TYPE_IDX (t) = idx;
   29624                 :           6 :           TYPE_CANONICAL (t) = canonical_type_parameter (t);
   29625                 :             :         }
   29626                 :             :     }
   29627                 :             : 
   29628                 :             :   /* Always keep walking.  */
   29629                 :         385 :   return 0;
   29630                 :             : }
   29631                 :             : 
   29632                 :             : /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
   29633                 :             :    says they can appear anywhere in the type.  */
   29634                 :             : 
   29635                 :             : static tree
   29636                 :         374 : extract_autos (tree type)
   29637                 :             : {
   29638                 :         374 :   hash_set<tree> visited;
   29639                 :         374 :   hash_table<auto_hash> hash (2);
   29640                 :             : 
   29641                 :         374 :   for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
   29642                 :             : 
   29643                 :         374 :   tree tree_vec = make_tree_vec (hash.elements());
   29644                 :         754 :   for (tree elt : hash)
   29645                 :             :     {
   29646                 :         380 :       unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
   29647                 :         380 :       TREE_VEC_ELT (tree_vec, i)
   29648                 :         760 :         = build_tree_list (NULL_TREE, TYPE_NAME (elt));
   29649                 :             :     }
   29650                 :             : 
   29651                 :         748 :   return tree_vec;
   29652                 :         374 : }
   29653                 :             : 
   29654                 :             : /* The stem for deduction guide names.  */
   29655                 :             : const char *const dguide_base = "__dguide_";
   29656                 :             : 
   29657                 :             : /* Return the name for a deduction guide for class template TMPL.  */
   29658                 :             : 
   29659                 :             : tree
   29660                 :      901536 : dguide_name (tree tmpl)
   29661                 :             : {
   29662                 :      901536 :   tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
   29663                 :      901536 :   tree tname = TYPE_IDENTIFIER (type);
   29664                 :      901536 :   char *buf = (char *) alloca (1 + strlen (dguide_base)
   29665                 :             :                                + IDENTIFIER_LENGTH (tname));
   29666                 :      901536 :   memcpy (buf, dguide_base, strlen (dguide_base));
   29667                 :      901536 :   memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
   29668                 :      901536 :           IDENTIFIER_LENGTH (tname) + 1);
   29669                 :      901536 :   tree dname = get_identifier (buf);
   29670                 :      901536 :   TREE_TYPE (dname) = type;
   29671                 :      901536 :   return dname;
   29672                 :             : }
   29673                 :             : 
   29674                 :             : /* True if NAME is the name of a deduction guide.  */
   29675                 :             : 
   29676                 :             : bool
   29677                 :   715242444 : dguide_name_p (tree name)
   29678                 :             : {
   29679                 :   715242444 :   return (TREE_CODE (name) == IDENTIFIER_NODE
   29680                 :   715242442 :           && TREE_TYPE (name)
   29681                 :   762950720 :           && startswith (IDENTIFIER_POINTER (name), dguide_base));
   29682                 :             : }
   29683                 :             : 
   29684                 :             : /* True if FN is a deduction guide.  */
   29685                 :             : 
   29686                 :             : bool
   29687                 :   421989528 : deduction_guide_p (const_tree fn)
   29688                 :             : {
   29689                 :   421989528 :   if (DECL_P (fn))
   29690                 :   421989486 :     if (tree name = DECL_NAME (fn))
   29691                 :   421989486 :       return dguide_name_p (name);
   29692                 :             :   return false;
   29693                 :             : }
   29694                 :             : 
   29695                 :             : /* True if FN is the copy deduction guide, i.e. A(A)->A.  */
   29696                 :             : 
   29697                 :             : bool
   29698                 :        8137 : copy_guide_p (const_tree fn)
   29699                 :             : {
   29700                 :        8137 :   gcc_assert (deduction_guide_p (fn));
   29701                 :        8137 :   if (!DECL_ARTIFICIAL (fn))
   29702                 :             :     return false;
   29703                 :        8137 :   tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
   29704                 :        8137 :   return (TREE_CHAIN (parms) == void_list_node
   29705                 :        8137 :           && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
   29706                 :             : }
   29707                 :             : 
   29708                 :             : /* True if FN is a guide generated from a constructor template.  */
   29709                 :             : 
   29710                 :             : bool
   29711                 :          32 : template_guide_p (const_tree fn)
   29712                 :             : {
   29713                 :          32 :   gcc_assert (deduction_guide_p (fn));
   29714                 :          32 :   if (!DECL_ARTIFICIAL (fn))
   29715                 :             :     return false;
   29716                 :          32 :   tree tmpl = DECL_TI_TEMPLATE (fn);
   29717                 :          32 :   if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
   29718                 :          32 :     return PRIMARY_TEMPLATE_P (org);
   29719                 :             :   return false;
   29720                 :             : }
   29721                 :             : 
   29722                 :             : /* True if FN is an aggregate initialization guide or the copy deduction
   29723                 :             :    guide.  */
   29724                 :             : 
   29725                 :             : bool
   29726                 :           4 : builtin_guide_p (const_tree fn)
   29727                 :             : {
   29728                 :           4 :   if (!deduction_guide_p (fn))
   29729                 :             :     return false;
   29730                 :           4 :   if (!DECL_ARTIFICIAL (fn))
   29731                 :             :     /* Explicitly declared.  */
   29732                 :             :     return false;
   29733                 :           4 :   if (DECL_ABSTRACT_ORIGIN (fn))
   29734                 :             :     /* Derived from a constructor.  */
   29735                 :           0 :     return false;
   29736                 :             :   return true;
   29737                 :             : }
   29738                 :             : 
   29739                 :             : /* OLDDECL is a _DECL for a template parameter.  Return a similar parameter at
   29740                 :             :    LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
   29741                 :             :    template parameter types.  Note that the handling of template template
   29742                 :             :    parameters relies on current_template_parms being set appropriately for the
   29743                 :             :    new template.  */
   29744                 :             : 
   29745                 :             : static tree
   29746                 :      112030 : rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
   29747                 :             :                        tree tsubst_args, tsubst_flags_t complain)
   29748                 :             : {
   29749                 :      112030 :   if (olddecl == error_mark_node)
   29750                 :             :     return error_mark_node;
   29751                 :             : 
   29752                 :      112028 :   tree oldidx = get_template_parm_index (olddecl);
   29753                 :             : 
   29754                 :      112028 :   tree newtype;
   29755                 :      112028 :   if (TREE_CODE (olddecl) == TYPE_DECL
   29756                 :        8432 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   29757                 :             :     {
   29758                 :      103616 :       tree oldtype = TREE_TYPE (olddecl);
   29759                 :      103616 :       newtype = cxx_make_type (TREE_CODE (oldtype));
   29760                 :      103616 :       TYPE_MAIN_VARIANT (newtype) = newtype;
   29761                 :      103616 :     }
   29762                 :             :   else
   29763                 :             :     {
   29764                 :        8412 :       newtype = TREE_TYPE (olddecl);
   29765                 :        8412 :       if (type_uses_auto (newtype))
   29766                 :             :         {
   29767                 :             :           // Substitute once to fix references to other template parameters.
   29768                 :           3 :           newtype = tsubst (newtype, tsubst_args,
   29769                 :             :                             complain|tf_partial, NULL_TREE);
   29770                 :             :           // Now substitute again to reduce the level of the auto.
   29771                 :           3 :           newtype = tsubst (newtype, current_template_args (),
   29772                 :             :                             complain, NULL_TREE);
   29773                 :             :         }
   29774                 :             :       else
   29775                 :        8409 :         newtype = tsubst (newtype, tsubst_args,
   29776                 :             :                           complain, NULL_TREE);
   29777                 :             :     }
   29778                 :             : 
   29779                 :      112028 :   tree newdecl
   29780                 :      112028 :     = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
   29781                 :      112028 :                   DECL_NAME (olddecl), newtype);
   29782                 :      112028 :   SET_DECL_TEMPLATE_PARM_P (newdecl);
   29783                 :             : 
   29784                 :      112028 :   tree newidx;
   29785                 :      112028 :   if (TREE_CODE (olddecl) == TYPE_DECL
   29786                 :        8432 :       || TREE_CODE (olddecl) == TEMPLATE_DECL)
   29787                 :             :     {
   29788                 :      103616 :       newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
   29789                 :      103616 :         = build_template_parm_index (index, level, level,
   29790                 :             :                                      newdecl, newtype);
   29791                 :      207232 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   29792                 :      103616 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   29793                 :      103616 :       TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
   29794                 :             : 
   29795                 :      103616 :       if (TREE_CODE (olddecl) == TEMPLATE_DECL)
   29796                 :             :         {
   29797                 :          20 :           tree newresult
   29798                 :          20 :             = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
   29799                 :          20 :                                    DECL_NAME (olddecl), newtype);
   29800                 :          20 :           DECL_ARTIFICIAL (newresult) = true;
   29801                 :          20 :           DECL_TEMPLATE_RESULT (newdecl) = newresult;
   29802                 :             :           // First create a copy (ttargs) of tsubst_args with an
   29803                 :             :           // additional level for the template template parameter's own
   29804                 :             :           // template parameters (ttparms).
   29805                 :          20 :           tree ttparms = (INNERMOST_TEMPLATE_PARMS
   29806                 :          20 :                           (DECL_TEMPLATE_PARMS (olddecl)));
   29807                 :          40 :           const int depth = TMPL_ARGS_DEPTH (tsubst_args);
   29808                 :          20 :           tree ttargs = make_tree_vec (depth + 1);
   29809                 :          54 :           for (int i = 0; i < depth; ++i)
   29810                 :          68 :             TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1);
   29811                 :          20 :           TREE_VEC_ELT (ttargs, depth)
   29812                 :          20 :             = template_parms_level_to_args (ttparms);
   29813                 :             :           // Substitute ttargs into ttparms to fix references to
   29814                 :             :           // other template parameters.
   29815                 :          20 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   29816                 :             :                                                  complain|tf_partial);
   29817                 :             :           // Now substitute again with args based on tparms, to reduce
   29818                 :             :           // the level of the ttparms.
   29819                 :          20 :           ttargs = current_template_args ();
   29820                 :          20 :           ttparms = tsubst_template_parms_level (ttparms, ttargs,
   29821                 :             :                                                  complain);
   29822                 :             :           // Finally, tack the adjusted parms onto tparms.
   29823                 :          20 :           ttparms = tree_cons (size_int (level + 1), ttparms,
   29824                 :             :                                copy_node (current_template_parms));
   29825                 :             :           // As with all template template parms, the parameter list captured
   29826                 :             :           // by this template template parm that corresponds to its own level
   29827                 :             :           // should be empty.  This avoids infinite recursion when structurally
   29828                 :             :           // comparing two such rewritten template template parms (PR102479).
   29829                 :          20 :           gcc_assert (!TREE_VEC_LENGTH
   29830                 :             :                       (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl)))));
   29831                 :          20 :           gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level);
   29832                 :          20 :           TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0);
   29833                 :             :           // All done.
   29834                 :          20 :           DECL_TEMPLATE_PARMS (newdecl) = ttparms;
   29835                 :          20 :           DECL_TEMPLATE_INFO (newresult)
   29836                 :          40 :             = build_template_info (newdecl, template_parms_to_args (ttparms));
   29837                 :             :         }
   29838                 :             : 
   29839                 :      103616 :       if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
   29840                 :           0 :         SET_TYPE_STRUCTURAL_EQUALITY (newtype);
   29841                 :             :       else
   29842                 :      103616 :         TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
   29843                 :             :     }
   29844                 :             :   else
   29845                 :             :     {
   29846                 :        8412 :       tree oldconst = TEMPLATE_PARM_DECL (oldidx);
   29847                 :        8412 :       tree newconst
   29848                 :        8412 :         = build_decl (DECL_SOURCE_LOCATION (oldconst),
   29849                 :        8412 :                       TREE_CODE (oldconst),
   29850                 :        8412 :                       DECL_NAME (oldconst), newtype);
   29851                 :       33648 :       TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
   29852                 :        8412 :         = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
   29853                 :        8412 :       SET_DECL_TEMPLATE_PARM_P (newconst);
   29854                 :        8412 :       newidx = build_template_parm_index (index, level, level,
   29855                 :             :                                           newconst, newtype);
   29856                 :       16824 :       TEMPLATE_PARM_PARAMETER_PACK (newidx)
   29857                 :        8412 :         = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
   29858                 :        8412 :       DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
   29859                 :             :     }
   29860                 :             : 
   29861                 :             :   return newdecl;
   29862                 :             : }
   29863                 :             : 
   29864                 :             : /* As rewrite_template_parm, but for the whole TREE_LIST representing a
   29865                 :             :    template parameter.  */
   29866                 :             : 
   29867                 :             : static tree
   29868                 :      112030 : rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
   29869                 :             :                     tree targs, unsigned targs_index, tsubst_flags_t complain)
   29870                 :             : {
   29871                 :      112030 :   tree olddecl = TREE_VALUE (oldelt);
   29872                 :      112030 :   tree newdecl = rewrite_template_parm (olddecl, index, level,
   29873                 :             :                                         targs, complain);
   29874                 :      112030 :   if (newdecl == error_mark_node)
   29875                 :             :     return error_mark_node;
   29876                 :      112028 :   tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
   29877                 :             :                                      targs, complain, NULL_TREE);
   29878                 :      112028 :   tree list = build_tree_list (newdef, newdecl);
   29879                 :      224056 :   TEMPLATE_PARM_CONSTRAINTS (list)
   29880                 :      112028 :     = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
   29881                 :             :                               targs, complain, NULL_TREE);
   29882                 :      224056 :   int depth = TMPL_ARGS_DEPTH (targs);
   29883                 :      224056 :   TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
   29884                 :      112028 :   return list;
   29885                 :             : }
   29886                 :             : 
   29887                 :             : /* Returns a C++17 class deduction guide template based on the constructor
   29888                 :             :    CTOR.  As a special case, CTOR can be a RECORD_TYPE for an implicit default
   29889                 :             :    guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
   29890                 :             :    aggregate initialization guide.  OUTER_ARGS are the template arguments
   29891                 :             :    for the enclosing scope of the class.  */
   29892                 :             : 
   29893                 :             : static tree
   29894                 :       79774 : build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
   29895                 :             : {
   29896                 :       79774 :   tree tparms, targs, fparms, fargs, ci;
   29897                 :       79774 :   bool memtmpl = false;
   29898                 :       79774 :   bool explicit_p;
   29899                 :       79774 :   location_t loc;
   29900                 :       79774 :   tree fn_tmpl = NULL_TREE;
   29901                 :             : 
   29902                 :       79774 :   if (outer_args)
   29903                 :             :     {
   29904                 :          82 :       ++processing_template_decl;
   29905                 :          82 :       type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
   29906                 :          82 :       --processing_template_decl;
   29907                 :             :     }
   29908                 :             : 
   29909                 :       79774 :   if (!DECL_DECLARES_FUNCTION_P (ctor))
   29910                 :             :     {
   29911                 :        5192 :       if (TYPE_P (ctor))
   29912                 :             :         {
   29913                 :        5014 :           bool copy_p = TYPE_REF_P (ctor);
   29914                 :        5014 :           if (copy_p)
   29915                 :        4692 :             fparms = tree_cons (NULL_TREE, type, void_list_node);
   29916                 :             :           else
   29917                 :         322 :             fparms = void_list_node;
   29918                 :             :         }
   29919                 :         178 :       else if (TREE_CODE (ctor) == TREE_LIST)
   29920                 :             :         fparms = ctor;
   29921                 :             :       else
   29922                 :           0 :         gcc_unreachable ();
   29923                 :             : 
   29924                 :        5192 :       tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
   29925                 :        5192 :       tparms = DECL_TEMPLATE_PARMS (ctmpl);
   29926                 :        5192 :       targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   29927                 :        5192 :       ci = NULL_TREE;
   29928                 :        5192 :       fargs = NULL_TREE;
   29929                 :        5192 :       loc = DECL_SOURCE_LOCATION (ctmpl);
   29930                 :        5192 :       explicit_p = false;
   29931                 :             :     }
   29932                 :             :   else
   29933                 :             :     {
   29934                 :       74582 :       ++processing_template_decl;
   29935                 :       74582 :       bool ok = true;
   29936                 :             : 
   29937                 :       74582 :       complain |= tf_dguide;
   29938                 :             : 
   29939                 :       74582 :       fn_tmpl
   29940                 :       74582 :         = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
   29941                 :       10877 :            : DECL_TI_TEMPLATE (ctor));
   29942                 :       74582 :       if (outer_args)
   29943                 :          33 :         fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
   29944                 :       74582 :       ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
   29945                 :             : 
   29946                 :       74582 :       tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
   29947                 :             :       /* If type is a member class template, DECL_TI_ARGS (ctor) will have
   29948                 :             :          fully specialized args for the enclosing class.  Strip those off, as
   29949                 :             :          the deduction guide won't have those template parameters.  */
   29950                 :      149164 :       targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
   29951                 :       74582 :                                                 TMPL_PARMS_DEPTH (tparms));
   29952                 :             :       /* Discard the 'this' parameter.  */
   29953                 :       74582 :       fparms = FUNCTION_ARG_CHAIN (ctor);
   29954                 :       74582 :       fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
   29955                 :       74582 :       ci = get_constraints (ctor);
   29956                 :       74582 :       loc = DECL_SOURCE_LOCATION (ctor);
   29957                 :       74582 :       explicit_p = DECL_NONCONVERTING_P (ctor);
   29958                 :             : 
   29959                 :       74582 :       if (PRIMARY_TEMPLATE_P (fn_tmpl))
   29960                 :             :         {
   29961                 :       63705 :           memtmpl = true;
   29962                 :             : 
   29963                 :             :           /* For a member template constructor, we need to flatten the two
   29964                 :             :              template parameter lists into one, and then adjust the function
   29965                 :             :              signature accordingly.  This gets...complicated.  */
   29966                 :       63705 :           tree save_parms = current_template_parms;
   29967                 :             : 
   29968                 :             :           /* For a member template we should have two levels of parms/args, one
   29969                 :             :              for the class and one for the constructor.  We stripped
   29970                 :             :              specialized args for further enclosing classes above.  */
   29971                 :       63705 :           const int depth = 2;
   29972                 :      127410 :           gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
   29973                 :             : 
   29974                 :             :           /* Template args for translating references to the two-level template
   29975                 :             :              parameters into references to the one-level template parameters we
   29976                 :             :              are creating.  */
   29977                 :       63705 :           tree tsubst_args = copy_node (targs);
   29978                 :       63705 :           TMPL_ARGS_LEVEL (tsubst_args, depth)
   29979                 :      127410 :             = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
   29980                 :             : 
   29981                 :             :           /* Template parms for the constructor template.  */
   29982                 :       63705 :           tree ftparms = TREE_VALUE (tparms);
   29983                 :       63705 :           unsigned flen = TREE_VEC_LENGTH (ftparms);
   29984                 :             :           /* Template parms for the class template.  */
   29985                 :       63705 :           tparms = TREE_CHAIN (tparms);
   29986                 :       63705 :           tree ctparms = TREE_VALUE (tparms);
   29987                 :       63705 :           unsigned clen = TREE_VEC_LENGTH (ctparms);
   29988                 :             :           /* Template parms for the deduction guide start as a copy of the
   29989                 :             :              template parms for the class.  We set current_template_parms for
   29990                 :             :              lookup_template_class_1.  */
   29991                 :       63705 :           current_template_parms = tparms = copy_node (tparms);
   29992                 :       63705 :           tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
   29993                 :      143010 :           for (unsigned i = 0; i < clen; ++i)
   29994                 :       79305 :             TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
   29995                 :             : 
   29996                 :             :           /* Now we need to rewrite the constructor parms to append them to the
   29997                 :             :              class parms.  */
   29998                 :      175538 :           for (unsigned i = 0; i < flen; ++i)
   29999                 :             :             {
   30000                 :      111833 :               unsigned index = i + clen;
   30001                 :      111833 :               unsigned level = 1;
   30002                 :      111833 :               tree oldelt = TREE_VEC_ELT (ftparms, i);
   30003                 :      111833 :               tree newelt
   30004                 :      111833 :                 = rewrite_tparm_list (oldelt, index, level,
   30005                 :             :                                       tsubst_args, i, complain);
   30006                 :      111833 :               if (newelt == error_mark_node)
   30007                 :           2 :                 ok = false;
   30008                 :      111833 :               TREE_VEC_ELT (new_vec, index) = newelt;
   30009                 :             :             }
   30010                 :             : 
   30011                 :             :           /* Now we have a final set of template parms to substitute into the
   30012                 :             :              function signature.  */
   30013                 :       63705 :           targs = template_parms_to_args (tparms);
   30014                 :       63705 :           fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
   30015                 :             :                                      complain, ctor);
   30016                 :       63705 :           if (fparms == error_mark_node)
   30017                 :           0 :             ok = false;
   30018                 :       63705 :           if (ci)
   30019                 :             :             {
   30020                 :       62101 :               if (outer_args)
   30021                 :             :                 /* FIXME: We'd like to avoid substituting outer template
   30022                 :             :                    arguments into the constraint ahead of time, but the
   30023                 :             :                    construction of tsubst_args assumes that outer arguments
   30024                 :             :                    are already substituted in.  */
   30025                 :           2 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   30026                 :       62101 :               ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
   30027                 :             :             }
   30028                 :             : 
   30029                 :             :           /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
   30030                 :             :              cp_unevaluated_operand.  */
   30031                 :       63705 :           cp_evaluated ev;
   30032                 :       63705 :           fargs = tsubst (fargs, tsubst_args, complain, ctor);
   30033                 :       63705 :           current_template_parms = save_parms;
   30034                 :       63705 :         }
   30035                 :             :       else
   30036                 :             :         {
   30037                 :             :           /* Substitute in the same arguments to rewrite class members into
   30038                 :             :              references to members of an unknown specialization.  */
   30039                 :       10877 :           cp_evaluated ev;
   30040                 :       10877 :           fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
   30041                 :       10877 :           fargs = tsubst (fargs, targs, complain, ctor);
   30042                 :       10877 :           if (ci)
   30043                 :             :             {
   30044                 :        5032 :               if (outer_args)
   30045                 :             :                 /* FIXME: As above.  */
   30046                 :           2 :                 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
   30047                 :        5032 :               ci = tsubst_constraint_info (ci, targs, complain, ctor);
   30048                 :             :             }
   30049                 :       10877 :         }
   30050                 :             : 
   30051                 :       74582 :       --processing_template_decl;
   30052                 :       74582 :       if (!ok)
   30053                 :           2 :         return error_mark_node;
   30054                 :             :     }
   30055                 :             : 
   30056                 :       79772 :   if (!memtmpl)
   30057                 :             :     {
   30058                 :             :       /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE.  */
   30059                 :       16069 :       tparms = copy_node (tparms);
   30060                 :       16069 :       INNERMOST_TEMPLATE_PARMS (tparms)
   30061                 :       16069 :         = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
   30062                 :             :     }
   30063                 :             : 
   30064                 :       79772 :   tree fntype = build_function_type (type, fparms);
   30065                 :       79772 :   tree ded_fn = build_lang_decl_loc (loc,
   30066                 :             :                                      FUNCTION_DECL,
   30067                 :             :                                      dguide_name (type), fntype);
   30068                 :       79772 :   DECL_ARGUMENTS (ded_fn) = fargs;
   30069                 :       79772 :   DECL_ARTIFICIAL (ded_fn) = true;
   30070                 :       79772 :   DECL_NONCONVERTING_P (ded_fn) = explicit_p;
   30071                 :       79772 :   tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
   30072                 :       79772 :   DECL_ARTIFICIAL (ded_tmpl) = true;
   30073                 :       79772 :   DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
   30074                 :       79772 :   DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
   30075                 :       79772 :   if (DECL_P (ctor))
   30076                 :       74580 :     DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
   30077                 :       79772 :   if (ci)
   30078                 :       67133 :     set_constraints (ded_tmpl, ci);
   30079                 :             : 
   30080                 :             :   return ded_tmpl;
   30081                 :             : }
   30082                 :             : 
   30083                 :             : /* Add to LIST the member types for the reshaped initializer CTOR.  */
   30084                 :             : 
   30085                 :             : static tree
   30086                 :         263 : collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
   30087                 :             : {
   30088                 :         263 :   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
   30089                 :         263 :   tree idx, val; unsigned i;
   30090                 :         500 :   FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
   30091                 :             :     {
   30092                 :         237 :       tree ftype = elt ? elt : TREE_TYPE (idx);
   30093                 :          23 :       if (BRACE_ENCLOSED_INITIALIZER_P (val)
   30094                 :         260 :           && CONSTRUCTOR_BRACES_ELIDED_P (val))
   30095                 :             :         {
   30096                 :           9 :           tree subelt = NULL_TREE;
   30097                 :           9 :           if (TREE_CODE (ftype) == ARRAY_TYPE)
   30098                 :           7 :             subelt = TREE_TYPE (ftype);
   30099                 :           9 :           list = collect_ctor_idx_types (val, list, subelt);
   30100                 :           9 :           continue;
   30101                 :           9 :         }
   30102                 :         228 :       tree arg = NULL_TREE;
   30103                 :         228 :       if (i == v->length() - 1
   30104                 :         228 :           && PACK_EXPANSION_P (ftype))
   30105                 :             :         /* Give the trailing pack expansion parameter a default argument to
   30106                 :             :            match aggregate initialization behavior, even if we deduce the
   30107                 :             :            length of the pack separately to more than we have initializers. */
   30108                 :           7 :         arg = build_constructor (init_list_type_node, NULL);
   30109                 :             :       /* if ei is of array type and xi is a braced-init-list or string literal,
   30110                 :             :          Ti is an rvalue reference to the declared type of ei */
   30111                 :         228 :       STRIP_ANY_LOCATION_WRAPPER (val);
   30112                 :         228 :       if (TREE_CODE (ftype) == ARRAY_TYPE
   30113                 :         228 :           && (BRACE_ENCLOSED_INITIALIZER_P (val)
   30114                 :           2 :               || TREE_CODE (val) == STRING_CST))
   30115                 :             :         {
   30116                 :           8 :           if (TREE_CODE (val) == STRING_CST)
   30117                 :           2 :             ftype = cp_build_qualified_type
   30118                 :           2 :               (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
   30119                 :           8 :           ftype = (cp_build_reference_type
   30120                 :          10 :                    (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
   30121                 :             :         }
   30122                 :         228 :       list = tree_cons (arg, ftype, list);
   30123                 :             :     }
   30124                 :             : 
   30125                 :         263 :   return list;
   30126                 :             : }
   30127                 :             : 
   30128                 :             : /* Return whether ETYPE is, or is derived from, a specialization of TMPL.  */
   30129                 :             : 
   30130                 :             : static bool
   30131                 :        1772 : is_spec_or_derived (tree etype, tree tmpl)
   30132                 :             : {
   30133                 :        1772 :   if (!etype || !CLASS_TYPE_P (etype))
   30134                 :             :     return false;
   30135                 :             : 
   30136                 :        1049 :   etype = cv_unqualified (etype);
   30137                 :        1049 :   tree type = TREE_TYPE (tmpl);
   30138                 :        1049 :   tree tparms = (INNERMOST_TEMPLATE_PARMS
   30139                 :        1049 :                  (DECL_TEMPLATE_PARMS (tmpl)));
   30140                 :        1049 :   tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   30141                 :        1049 :   int err = unify (tparms, targs, type, etype,
   30142                 :             :                    UNIFY_ALLOW_DERIVED, /*explain*/false);
   30143                 :        1049 :   ggc_free (targs);
   30144                 :        1049 :   return !err;
   30145                 :             : }
   30146                 :             : 
   30147                 :             : /* Return a C++20 aggregate deduction candidate for TYPE initialized from
   30148                 :             :    INIT.  */
   30149                 :             : 
   30150                 :             : static tree
   30151                 :        1639 : maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
   30152                 :             : {
   30153                 :        1639 :   if (cxx_dialect < cxx20)
   30154                 :             :     return NULL_TREE;
   30155                 :             : 
   30156                 :        1415 :   if (init == NULL_TREE)
   30157                 :             :     return NULL_TREE;
   30158                 :             : 
   30159                 :        1386 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   30160                 :             :     {
   30161                 :          77 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30162                 :          77 :       tree tinfo = get_template_info (under);
   30163                 :          77 :       if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args))
   30164                 :           1 :         return alias_ctad_tweaks (tmpl, guide);
   30165                 :             :       return NULL_TREE;
   30166                 :             :     }
   30167                 :             : 
   30168                 :             :   /* We might be creating a guide for a class member template, e.g.,
   30169                 :             : 
   30170                 :             :        template<typename U> struct A {
   30171                 :             :          template<typename T> struct B { T t; };
   30172                 :             :        };
   30173                 :             : 
   30174                 :             :      At this point, A will have been instantiated.  Below, we need to
   30175                 :             :      use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types.  */
   30176                 :        1309 :   const bool member_template_p
   30177                 :        1309 :     = (DECL_TEMPLATE_INFO (tmpl)
   30178                 :        1309 :        && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
   30179                 :        1309 :   tree type = TREE_TYPE (tmpl);
   30180                 :        1309 :   tree template_type = (member_template_p
   30181                 :        1309 :                         ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
   30182                 :        1309 :                         : type);
   30183                 :        1309 :   if (!CP_AGGREGATE_TYPE_P (template_type))
   30184                 :             :     return NULL_TREE;
   30185                 :             : 
   30186                 :             :   /* No aggregate candidate for copy-initialization.  */
   30187                 :         299 :   if (args->length() == 1)
   30188                 :             :     {
   30189                 :         299 :       tree val = (*args)[0];
   30190                 :         299 :       if (is_spec_or_derived (TREE_TYPE (val), tmpl))
   30191                 :             :         return NULL_TREE;
   30192                 :             :     }
   30193                 :             : 
   30194                 :             :   /* If we encounter a problem, we just won't add the candidate.  */
   30195                 :         262 :   tsubst_flags_t complain = tf_none;
   30196                 :             : 
   30197                 :         262 :   tree parms = NULL_TREE;
   30198                 :         262 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   30199                 :             :     {
   30200                 :         258 :       init = reshape_init (template_type, init, complain);
   30201                 :         258 :       if (init == error_mark_node)
   30202                 :             :         return NULL_TREE;
   30203                 :         254 :       parms = collect_ctor_idx_types (init, parms);
   30204                 :             :       /* If we're creating a deduction guide for a member class template,
   30205                 :             :          we've used the original template pattern type for the reshape_init
   30206                 :             :          above; this is done because we want PARMS to be a template parameter
   30207                 :             :          type, something that can be deduced when used as a function template
   30208                 :             :          parameter.  At this point the outer class template has already been
   30209                 :             :          partially instantiated (we deferred the deduction until the enclosing
   30210                 :             :          scope is non-dependent).  Therefore we have to partially instantiate
   30211                 :             :          PARMS, so that its template level is properly reduced and we don't get
   30212                 :             :          mismatches when deducing types using the guide with PARMS.  */
   30213                 :         254 :       if (member_template_p)
   30214                 :             :         {
   30215                 :           5 :           ++processing_template_decl;
   30216                 :           5 :           parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
   30217                 :           5 :           --processing_template_decl;
   30218                 :             :         }
   30219                 :             :     }
   30220                 :           4 :   else if (TREE_CODE (init) == TREE_LIST)
   30221                 :             :     {
   30222                 :           3 :       int len = list_length (init);
   30223                 :           3 :       for (tree field = TYPE_FIELDS (type);
   30224                 :           3 :            len;
   30225                 :           0 :            --len, field = DECL_CHAIN (field))
   30226                 :             :         {
   30227                 :           3 :           field = next_aggregate_field (field);
   30228                 :           3 :           if (!field)
   30229                 :             :             return NULL_TREE;
   30230                 :           0 :           tree ftype = finish_decltype_type (field, true, complain);
   30231                 :           0 :           parms = tree_cons (NULL_TREE, ftype, parms);
   30232                 :             :         }
   30233                 :             :     }
   30234                 :             :   else
   30235                 :             :     /* Aggregate initialization doesn't apply to an initializer expression.  */
   30236                 :             :     return NULL_TREE;
   30237                 :             : 
   30238                 :         254 :   if (parms)
   30239                 :             :     {
   30240                 :         178 :       tree last = parms;
   30241                 :         178 :       parms = nreverse (parms);
   30242                 :         178 :       TREE_CHAIN (last) = void_list_node;
   30243                 :         178 :       tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
   30244                 :         178 :       return guide;
   30245                 :             :     }
   30246                 :             : 
   30247                 :             :   return NULL_TREE;
   30248                 :             : }
   30249                 :             : 
   30250                 :             : /* UGUIDES are the deduction guides for the underlying template of alias
   30251                 :             :    template TMPL; adjust them to be deduction guides for TMPL.
   30252                 :             : 
   30253                 :             :    This routine also handles C++23 inherited CTAD, in which case TMPL is a
   30254                 :             :    TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is
   30255                 :             :    the template parameter list of the alias template (equivalently, of the
   30256                 :             :    derived class) and TREE_VALUE the defining-type-id (equivalently, the
   30257                 :             :    base whose guides we're inheriting).  UGUIDES are the base's guides.  */
   30258                 :             : 
   30259                 :             : static tree
   30260                 :          59 : alias_ctad_tweaks (tree tmpl, tree uguides)
   30261                 :             : {
   30262                 :             :   /* [over.match.class.deduct]: When resolving a placeholder for a deduced
   30263                 :             :      class type (9.2.8.2) where the template-name names an alias template A,
   30264                 :             :      the defining-type-id of A must be of the form
   30265                 :             : 
   30266                 :             :      typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   30267                 :             : 
   30268                 :             :      as specified in 9.2.8.2. The guides of A are the set of functions or
   30269                 :             :      function templates formed as follows. For each function or function
   30270                 :             :      template f in the guides of the template named by the simple-template-id
   30271                 :             :      of the defining-type-id, the template arguments of the return type of f
   30272                 :             :      are deduced from the defining-type-id of A according to the process in
   30273                 :             :      13.10.2.5 with the exception that deduction does not fail if not all
   30274                 :             :      template arguments are deduced. Let g denote the result of substituting
   30275                 :             :      these deductions into f. If substitution succeeds, form a function or
   30276                 :             :      function template f' with the following properties and add it to the set
   30277                 :             :      of guides of A:
   30278                 :             : 
   30279                 :             :      * The function type of f' is the function type of g.
   30280                 :             : 
   30281                 :             :      * If f is a function template, f' is a function template whose template
   30282                 :             :      parameter list consists of all the template parameters of A (including
   30283                 :             :      their default template arguments) that appear in the above deductions or
   30284                 :             :      (recursively) in their default template arguments, followed by the
   30285                 :             :      template parameters of f that were not deduced (including their default
   30286                 :             :      template arguments), otherwise f' is not a function template.
   30287                 :             : 
   30288                 :             :      * The associated constraints (13.5.2) are the conjunction of the
   30289                 :             :      associated constraints of g and a constraint that is satisfied if and only
   30290                 :             :      if the arguments of A are deducible (see below) from the return type.
   30291                 :             : 
   30292                 :             :      * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
   30293                 :             :      be so as well.
   30294                 :             : 
   30295                 :             :      * If f was generated from a deduction-guide (12.4.1.8), then f' is
   30296                 :             :      considered to be so as well.
   30297                 :             : 
   30298                 :             :      * The explicit-specifier of f' is the explicit-specifier of g (if
   30299                 :             :      any).  */
   30300                 :             : 
   30301                 :          59 :   enum { alias, inherited } ctad_kind;
   30302                 :          59 :   tree atype, fullatparms, utype;
   30303                 :          59 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   30304                 :             :     {
   30305                 :          47 :       ctad_kind = alias;
   30306                 :          47 :       atype = TREE_TYPE (tmpl);
   30307                 :          47 :       fullatparms = DECL_TEMPLATE_PARMS (tmpl);
   30308                 :          47 :       utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30309                 :             :     }
   30310                 :             :   else
   30311                 :             :     {
   30312                 :          12 :       ctad_kind = inherited;
   30313                 :          12 :       atype = NULL_TREE;
   30314                 :          12 :       fullatparms = TREE_PURPOSE (tmpl);
   30315                 :          12 :       utype = TREE_VALUE (tmpl);
   30316                 :             :     }
   30317                 :             : 
   30318                 :          59 :   tsubst_flags_t complain = tf_warning_or_error;
   30319                 :          59 :   tree aguides = NULL_TREE;
   30320                 :          59 :   tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms);
   30321                 :          59 :   unsigned natparms = TREE_VEC_LENGTH (atparms);
   30322                 :         366 :   for (ovl_iterator iter (uguides); iter; ++iter)
   30323                 :             :     {
   30324                 :         154 :       tree f = *iter;
   30325                 :         154 :       tree in_decl = f;
   30326                 :         154 :       location_t loc = DECL_SOURCE_LOCATION (f);
   30327                 :         154 :       tree ret = TREE_TYPE (TREE_TYPE (f));
   30328                 :         154 :       tree fprime = f;
   30329                 :         154 :       if (TREE_CODE (f) == TEMPLATE_DECL)
   30330                 :             :         {
   30331                 :         149 :           processing_template_decl_sentinel ptds (/*reset*/false);
   30332                 :         149 :           ++processing_template_decl;
   30333                 :             : 
   30334                 :             :           /* Deduce template arguments for f from the type-id of A.  */
   30335                 :         149 :           tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
   30336                 :         149 :           unsigned len = TREE_VEC_LENGTH (ftparms);
   30337                 :         149 :           tree targs = make_tree_vec (len);
   30338                 :         149 :           int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
   30339                 :         149 :           if (err)
   30340                 :             :             /* CWG2664: Discard any deductions, still build the guide.  */
   30341                 :           8 :             for (unsigned i = 0; i < len; ++i)
   30342                 :           4 :               TREE_VEC_ELT (targs, i) = NULL_TREE;
   30343                 :             : 
   30344                 :             :           /* The number of parms for f' is the number of parms of A used in
   30345                 :             :              the deduced arguments plus non-deduced parms of f.  */
   30346                 :             :           unsigned ndlen = 0;
   30347                 :             :           unsigned j;
   30348                 :         436 :           for (unsigned i = 0; i < len; ++i)
   30349                 :         287 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   30350                 :          35 :               ++ndlen;
   30351                 :         149 :           find_template_parameter_info ftpi (fullatparms);
   30352                 :         149 :           ftpi.find_in_recursive (targs);
   30353                 :         149 :           unsigned nusedatparms = ftpi.num_found ();
   30354                 :         149 :           unsigned nfparms = nusedatparms + ndlen;
   30355                 :         149 :           tree gtparms = make_tree_vec (nfparms);
   30356                 :             : 
   30357                 :             :           /* Set current_template_parms as in build_deduction_guide.  */
   30358                 :         149 :           auto ctp = make_temp_override (current_template_parms);
   30359                 :         149 :           current_template_parms = copy_node (fullatparms);
   30360                 :         149 :           TREE_VALUE (current_template_parms) = gtparms;
   30361                 :             : 
   30362                 :         149 :           j = 0;
   30363                 :         149 :           unsigned level = 1;
   30364                 :             : 
   30365                 :             :           /* First copy over the used parms of A.  */
   30366                 :         149 :           tree atargs = make_tree_vec (natparms);
   30367                 :         324 :           for (unsigned i = 0; i < natparms; ++i)
   30368                 :             :             {
   30369                 :         175 :               tree elt = TREE_VEC_ELT (atparms, i);
   30370                 :         175 :               if (ftpi.found (elt))
   30371                 :             :                 {
   30372                 :         162 :                   unsigned index = j++;
   30373                 :         162 :                   tree nelt = rewrite_tparm_list (elt, index, level,
   30374                 :             :                                                   atargs, i, complain);
   30375                 :         162 :                   TREE_VEC_ELT (gtparms, index) = nelt;
   30376                 :             :                 }
   30377                 :             :             }
   30378                 :         149 :           gcc_checking_assert (j == nusedatparms);
   30379                 :             : 
   30380                 :             :           /* Adjust the deduced template args for f to refer to the A parms
   30381                 :             :              with their new indexes.  */
   30382                 :         149 :           if (nusedatparms && nusedatparms != natparms)
   30383                 :           3 :             targs = tsubst_template_args (targs, atargs, complain, in_decl);
   30384                 :             : 
   30385                 :             :           /* Now rewrite the non-deduced parms of f.  */
   30386                 :         217 :           for (unsigned i = 0; ndlen && i < len; ++i)
   30387                 :          68 :             if (TREE_VEC_ELT (targs, i) == NULL_TREE)
   30388                 :             :               {
   30389                 :          35 :                 --ndlen;
   30390                 :          35 :                 unsigned index = j++;
   30391                 :          35 :                 tree oldlist = TREE_VEC_ELT (ftparms, i);
   30392                 :          35 :                 tree list = rewrite_tparm_list (oldlist, index, level,
   30393                 :             :                                                 targs, i, complain);
   30394                 :          35 :                 TREE_VEC_ELT (gtparms, index) = list;
   30395                 :             :               }
   30396                 :         149 :           gtparms = build_tree_list (size_one_node, gtparms);
   30397                 :             : 
   30398                 :             :           /* Substitute the deduced arguments plus the rewritten template
   30399                 :             :              parameters into f to get g.  This covers the type, copyness,
   30400                 :             :              guideness, and explicit-specifier.  */
   30401                 :         149 :           tree g;
   30402                 :         149 :             {
   30403                 :             :               /* Parms are to have DECL_CHAIN tsubsted, which would be skipped
   30404                 :             :                  if cp_unevaluated_operand.  */
   30405                 :         149 :               cp_evaluated ev;
   30406                 :         149 :               g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain,
   30407                 :             :                                /*use_spec_table=*/false);
   30408                 :         149 :             }
   30409                 :         149 :           if (g == error_mark_node)
   30410                 :           0 :             continue;
   30411                 :         149 :           if (nfparms == 0)
   30412                 :             :             {
   30413                 :             :               /* The targs are all non-dependent, so g isn't a template.  */
   30414                 :           4 :               fprime = g;
   30415                 :           4 :               ret = TREE_TYPE (TREE_TYPE (fprime));
   30416                 :           4 :               goto non_template;
   30417                 :             :             }
   30418                 :         145 :           DECL_USE_TEMPLATE (g) = 0;
   30419                 :         145 :           fprime = build_template_decl (g, gtparms, false);
   30420                 :         145 :           DECL_TEMPLATE_RESULT (fprime) = g;
   30421                 :         145 :           TREE_TYPE (fprime) = TREE_TYPE (g);
   30422                 :         145 :           tree gtargs = template_parms_to_args (gtparms);
   30423                 :         145 :           DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
   30424                 :         145 :           DECL_PRIMARY_TEMPLATE (fprime) = fprime;
   30425                 :             : 
   30426                 :             :           /* Substitute the associated constraints.  */
   30427                 :         145 :           tree ci = get_constraints (f);
   30428                 :         145 :           if (ci)
   30429                 :           9 :             ci = tsubst_constraint_info (ci, targs, complain, in_decl);
   30430                 :         145 :           if (ci == error_mark_node)
   30431                 :           0 :             continue;
   30432                 :             : 
   30433                 :             :           /* Add a constraint that the return type matches the instantiation of
   30434                 :             :              A with the same template arguments.  */
   30435                 :         145 :           ret = TREE_TYPE (TREE_TYPE (fprime));
   30436                 :         145 :           if (ctad_kind == alias
   30437                 :         145 :               && (!same_type_p (atype, ret)
   30438                 :             :                   /* FIXME this should mean they don't compare as equivalent. */
   30439                 :         104 :                   || dependent_alias_template_spec_p (atype, nt_opaque)))
   30440                 :             :             {
   30441                 :          23 :               tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret);
   30442                 :          23 :               ci = append_constraint (ci, same);
   30443                 :             :             }
   30444                 :             : 
   30445                 :         145 :           if (ci)
   30446                 :             :             {
   30447                 :          28 :               remove_constraints (fprime);
   30448                 :          28 :               set_constraints (fprime, ci);
   30449                 :             :             }
   30450                 :         298 :         }
   30451                 :             :       else
   30452                 :             :         {
   30453                 :             :           /* For a non-template deduction guide, if the arguments of A aren't
   30454                 :             :              deducible from the return type, don't add the candidate.  */
   30455                 :           5 :         non_template:
   30456                 :          10 :           if (ctad_kind == alias
   30457                 :           9 :               && !type_targs_deducible_from (tmpl, ret))
   30458                 :           1 :             continue;
   30459                 :             :         }
   30460                 :             : 
   30461                 :             :       /* Rewrite the return type of the inherited guide in terms of the
   30462                 :             :          derived class.  This is specified as replacing the return type R
   30463                 :             :          with typename CC<R>::type where the partially specialized CC maps a
   30464                 :             :          base class specialization to a specialization of the derived class
   30465                 :             :          having such a base (inducing substitution failure if no such derived
   30466                 :             :          class exists).
   30467                 :             : 
   30468                 :             :          As specified this mapping would be done at instantiation time using
   30469                 :             :          non-dependent template arguments, but we do it ahead of time using
   30470                 :             :          the generic arguments.  This seems to be good enough since generic
   30471                 :             :          deduction should succeed only if concrete deduction would.  */
   30472                 :         153 :       if (ctad_kind == inherited)
   30473                 :             :         {
   30474                 :          34 :           processing_template_decl_sentinel ptds (/*reset*/false);
   30475                 :          34 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   30476                 :          30 :             ++processing_template_decl;
   30477                 :             : 
   30478                 :          34 :           tree targs = type_targs_deducible_from (tmpl, ret);
   30479                 :          34 :           if (!targs)
   30480                 :           0 :             continue;
   30481                 :             : 
   30482                 :          34 :           if (TREE_CODE (f) != TEMPLATE_DECL)
   30483                 :           4 :             fprime = copy_decl (fprime);
   30484                 :          34 :           tree fntype = TREE_TYPE (fprime);
   30485                 :          34 :           ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), targs,
   30486                 :             :                                        in_decl, NULL_TREE, false, complain);
   30487                 :          34 :           fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype));
   30488                 :          34 :           TREE_TYPE (fprime) = fntype;
   30489                 :          34 :           if (TREE_CODE (fprime) == TEMPLATE_DECL)
   30490                 :          30 :             TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype;
   30491                 :          34 :         }
   30492                 :             : 
   30493                 :         153 :       aguides = lookup_add (fprime, aguides);
   30494                 :             :     }
   30495                 :             : 
   30496                 :          59 :   return aguides;
   30497                 :             : }
   30498                 :             : 
   30499                 :             : /* CTOR is a using-decl inheriting the constructors of some base of the class
   30500                 :             :    template TMPL; adjust the base's guides be deduction guides for TMPL.  */
   30501                 :             : 
   30502                 :             : static tree
   30503                 :          14 : inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain)
   30504                 :             : {
   30505                 :             :   /* [over.match.class.deduct]: In addition, if C is defined and inherits
   30506                 :             :      constructors ([namespace.udecl]) from a direct base class denoted in the
   30507                 :             :      base-specifier-list by a class-or-decltype B, let A be an alias template
   30508                 :             :      whose template parameter list is that of C and whose defining-type-id is
   30509                 :             :      B.  If A is a deducible template ([dcl.type.simple]), the set contains the
   30510                 :             :      guides of A with the return type R of each guide replaced with typename
   30511                 :             :      CC::type given a class template
   30512                 :             : 
   30513                 :             :      template <typename> class CC;
   30514                 :             : 
   30515                 :             :      whose primary template is not defined and with a single partial
   30516                 :             :      specialization whose template parameter list is that of A and whose
   30517                 :             :      template argument list is a specialization of A with the template argument
   30518                 :             :      list of A ([temp.dep.type]) having a member typedef type designating a
   30519                 :             :      template specialization with the template argument list of A but with C as
   30520                 :             :      the template.  */
   30521                 :             : 
   30522                 :             :   /* FIXME: Also recognize inherited constructors of the form 'using C::B::B',
   30523                 :             :      which seem to be represented with TYPENAME_TYPE C::B as USING_DECL_SCOPE?
   30524                 :             :      And recognize constructors inherited from a non-dependent base class, which
   30525                 :             :      seem to be missing from the overload set entirely?  */
   30526                 :          14 :   tree scope = USING_DECL_SCOPE (ctor);
   30527                 :          12 :   if (!CLASS_TYPE_P (scope)
   30528                 :          12 :       || !CLASSTYPE_TEMPLATE_INFO (scope)
   30529                 :          26 :       || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
   30530                 :             :     return NULL_TREE;
   30531                 :             : 
   30532                 :          12 :   tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope);
   30533                 :          12 :   bool any_dguides_p;
   30534                 :          12 :   tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope),
   30535                 :             :                                        any_dguides_p, complain);
   30536                 :          12 :   return alias_ctad_tweaks (t, uguides);
   30537                 :             : }
   30538                 :             : 
   30539                 :             : /* If template arguments for TMPL can be deduced from TYPE, return
   30540                 :             :    the deduced arguments, otherwise return NULL_TREE.
   30541                 :             :    Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to
   30542                 :             :    [over.match.class.deduct].
   30543                 :             : 
   30544                 :             :    This check is specified in terms of partial specialization, so the behavior
   30545                 :             :    should be parallel to that of get_partial_spec_bindings.  */
   30546                 :             : 
   30547                 :             : tree
   30548                 :          63 : type_targs_deducible_from (tree tmpl, tree type)
   30549                 :             : {
   30550                 :          63 :   tree tparms, ttype;
   30551                 :          63 :   if (TREE_CODE (tmpl) == TEMPLATE_DECL)
   30552                 :             :     {
   30553                 :             :       /* If tmpl is a class template, this is trivial: it's deducible if
   30554                 :             :          TYPE is a specialization of TMPL.  */
   30555                 :          29 :       if (DECL_CLASS_TEMPLATE_P (tmpl))
   30556                 :             :         {
   30557                 :           0 :           if (CLASS_TYPE_P (type)
   30558                 :           0 :               && CLASSTYPE_TEMPLATE_INFO (type)
   30559                 :           0 :               && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
   30560                 :           0 :             return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
   30561                 :             :           else
   30562                 :             :             return NULL_TREE;
   30563                 :             :         }
   30564                 :             : 
   30565                 :             :       /* Otherwise it's an alias template.  */
   30566                 :          29 :       tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
   30567                 :          29 :       ttype = TREE_TYPE (tmpl);
   30568                 :             :     }
   30569                 :             :   else
   30570                 :             :     {
   30571                 :             :       /* TMPL is a synthetic alias template represented as a TREE_LIST as
   30572                 :             :          per alias_ctad_tweaks.  */
   30573                 :          34 :       tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl));
   30574                 :          34 :       ttype = TREE_VALUE (tmpl);
   30575                 :          68 :       tmpl = TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype));
   30576                 :             :     }
   30577                 :             : 
   30578                 :          63 :   int len = TREE_VEC_LENGTH (tparms);
   30579                 :          63 :   tree targs = make_tree_vec (len);
   30580                 :          63 :   bool tried_array_deduction = (cxx_dialect < cxx17);
   30581                 :             : 
   30582                 :          63 :  again:
   30583                 :          63 :   if (unify (tparms, targs, ttype, type,
   30584                 :             :              UNIFY_ALLOW_NONE, false))
   30585                 :             :     return NULL_TREE;
   30586                 :             : 
   30587                 :             :   /* We don't fail on an undeduced targ the second time through (like
   30588                 :             :      get_partial_spec_bindings) because we're going to try defaults.  */
   30589                 :         127 :   for (int i =  0; i < len; ++i)
   30590                 :          72 :     if (! TREE_VEC_ELT (targs, i))
   30591                 :             :       {
   30592                 :           6 :         tree tparm = TREE_VEC_ELT (tparms, i);
   30593                 :           6 :         tparm = TREE_VALUE (tparm);
   30594                 :             : 
   30595                 :           6 :         if (!tried_array_deduction
   30596                 :           6 :             && TREE_CODE (tparm) == TYPE_DECL)
   30597                 :             :           {
   30598                 :           4 :             try_array_deduction (tparms, targs, ttype);
   30599                 :           4 :             tried_array_deduction = true;
   30600                 :           4 :             if (TREE_VEC_ELT (targs, i))
   30601                 :           0 :               goto again;
   30602                 :             :           }
   30603                 :             :         /* If the type parameter is a parameter pack, then it will be deduced
   30604                 :             :            to an empty parameter pack.  This is another case that doesn't model
   30605                 :             :            well as partial specialization.  */
   30606                 :           6 :         if (template_parameter_pack_p (tparm))
   30607                 :             :           {
   30608                 :           2 :             tree arg;
   30609                 :           2 :             if (TREE_CODE (tparm) == PARM_DECL)
   30610                 :             :               {
   30611                 :           2 :                 arg = make_node (NONTYPE_ARGUMENT_PACK);
   30612                 :           2 :                 TREE_CONSTANT (arg) = 1;
   30613                 :             :               }
   30614                 :             :             else
   30615                 :           0 :               arg = cxx_make_type (TYPE_ARGUMENT_PACK);
   30616                 :           2 :             ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0);
   30617                 :           2 :             TREE_VEC_ELT (targs, i) = arg;
   30618                 :             :           }
   30619                 :             :       }
   30620                 :             : 
   30621                 :             :   /* Maybe add in default template args.  This seems like a flaw in the
   30622                 :             :      specification in terms of partial specialization, since it says the
   30623                 :             :      partial specialization has the template parameter list of A, but a
   30624                 :             :      partial specialization can't have default targs.  */
   30625                 :          55 :   targs = coerce_template_parms (tparms, targs, tmpl, tf_none);
   30626                 :          55 :   if (targs == error_mark_node)
   30627                 :             :     return NULL_TREE;
   30628                 :             : 
   30629                 :             :   /* I believe we don't need the template_template_parm_bindings_ok_p call
   30630                 :             :      because coerce_template_parms did coerce_template_template_parms.  */
   30631                 :             : 
   30632                 :          53 :   if (!constraints_satisfied_p (tmpl, targs))
   30633                 :             :     return NULL_TREE;
   30634                 :             : 
   30635                 :             :   return targs;
   30636                 :             : }
   30637                 :             : 
   30638                 :             : /* Return artificial deduction guides built from the constructors of class
   30639                 :             :    template TMPL.  */
   30640                 :             : 
   30641                 :             : static tree
   30642                 :        4692 : ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
   30643                 :             : {
   30644                 :        4692 :   tree outer_args = outer_template_args (tmpl);
   30645                 :        4692 :   tree type = TREE_TYPE (most_general_template (tmpl));
   30646                 :             : 
   30647                 :        4692 :   tree cands = NULL_TREE;
   30648                 :             : 
   30649                 :      149740 :   for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
   30650                 :             :     {
   30651                 :             :       /* We handle C++23 inherited CTAD below.  */
   30652                 :       74604 :       if (iter.using_p ())
   30653                 :          22 :         continue;
   30654                 :             : 
   30655                 :       74582 :       tree guide = build_deduction_guide (type, *iter, outer_args, complain);
   30656                 :       74582 :       cands = lookup_add (guide, cands);
   30657                 :             :     }
   30658                 :             : 
   30659                 :        4692 :   if (cxx_dialect >= cxx23)
   30660                 :      129005 :     for (tree ctor : ovl_range (CLASSTYPE_CONSTRUCTORS (type)))
   30661                 :       64390 :       if (TREE_CODE (ctor) == USING_DECL)
   30662                 :             :         {
   30663                 :          14 :           tree uguides = inherited_ctad_tweaks (tmpl, ctor, complain);
   30664                 :          14 :           if (uguides)
   30665                 :          12 :             cands = lookup_add (uguides, cands);
   30666                 :             :         }
   30667                 :             : 
   30668                 :             :   /* Add implicit default constructor deduction guide.  */
   30669                 :        4692 :   if (!TYPE_HAS_USER_CONSTRUCTOR (type))
   30670                 :             :     {
   30671                 :         322 :       tree guide = build_deduction_guide (type, type, outer_args,
   30672                 :             :                                           complain);
   30673                 :         322 :       cands = lookup_add (guide, cands);
   30674                 :             :     }
   30675                 :             : 
   30676                 :             :   /* Add copy guide.  */
   30677                 :        4692 :   {
   30678                 :        4692 :     tree gtype = build_reference_type (type);
   30679                 :        4692 :     tree guide = build_deduction_guide (type, gtype, outer_args,
   30680                 :             :                                         complain);
   30681                 :        4692 :     cands = lookup_add (guide, cands);
   30682                 :             :   }
   30683                 :             : 
   30684                 :        4692 :   return cands;
   30685                 :             : }
   30686                 :             : 
   30687                 :             : static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
   30688                 :             : 
   30689                 :             : /* Return the non-aggregate deduction guides for deducible template TMPL.  The
   30690                 :             :    aggregate candidate is added separately because it depends on the
   30691                 :             :    initializer.  Set ANY_DGUIDES_P if we find a non-implicit deduction
   30692                 :             :    guide.  */
   30693                 :             : 
   30694                 :             : static tree
   30695                 :       10502 : deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
   30696                 :             : {
   30697                 :       10502 :   tree guides = NULL_TREE;
   30698                 :       10502 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   30699                 :             :     {
   30700                 :         113 :       tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30701                 :         113 :       tree tinfo = get_template_info (under);
   30702                 :         113 :       guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
   30703                 :             :                                      complain);
   30704                 :             :     }
   30705                 :             :   else
   30706                 :             :     {
   30707                 :       10389 :       guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
   30708                 :             :                                       dguide_name (tmpl),
   30709                 :             :                                       LOOK_want::NORMAL, /*complain*/false);
   30710                 :       10389 :       if (guides == error_mark_node)
   30711                 :             :         guides = NULL_TREE;
   30712                 :             :       else
   30713                 :        8817 :         any_dguides_p = true;
   30714                 :             :     }
   30715                 :             : 
   30716                 :             :   /* Cache the deduction guides for a template.  We also remember the result of
   30717                 :             :      lookup, and rebuild everything if it changes; should be very rare.  */
   30718                 :             :   /* FIXME: Also rebuild if this is a class template that inherits guides from a
   30719                 :             :      base class, and lookup for the latter changed.  */
   30720                 :       10502 :   tree_pair_p cache = NULL;
   30721                 :       21004 :   if (tree_pair_p &r
   30722                 :       10502 :       = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
   30723                 :             :     {
   30724                 :        5776 :       cache = r;
   30725                 :        5776 :       if (cache->purpose == guides)
   30726                 :        5764 :         return cache->value;
   30727                 :             :     }
   30728                 :             :   else
   30729                 :             :     {
   30730                 :        4726 :       r = cache = ggc_cleared_alloc<tree_pair_s> ();
   30731                 :        4726 :       cache->purpose = guides;
   30732                 :             :     }
   30733                 :             : 
   30734                 :        4738 :   tree cands = NULL_TREE;
   30735                 :        4738 :   if (DECL_ALIAS_TEMPLATE_P (tmpl))
   30736                 :          46 :     cands = alias_ctad_tweaks (tmpl, guides);
   30737                 :             :   else
   30738                 :             :     {
   30739                 :        4692 :       cands = ctor_deduction_guides_for (tmpl, complain);
   30740                 :       21645 :       for (ovl_iterator it (guides); it; ++it)
   30741                 :       16953 :         cands = lookup_add (*it, cands);
   30742                 :             :     }
   30743                 :             : 
   30744                 :        4738 :   cache->value = cands;
   30745                 :        4738 :   return cands;
   30746                 :             : }
   30747                 :             : 
   30748                 :             : /* Return whether TMPL is a (class template argument-) deducible template.  */
   30749                 :             : 
   30750                 :             : bool
   30751                 :   678034606 : ctad_template_p (tree tmpl)
   30752                 :             : {
   30753                 :             :   /* A deducible template is either a class template or is an alias template
   30754                 :             :      whose defining-type-id is of the form
   30755                 :             : 
   30756                 :             :       typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
   30757                 :             : 
   30758                 :             :      where the nested-name-specifier (if any) is non-dependent and the
   30759                 :             :      template-name of the simple-template-id names a deducible template.  */
   30760                 :             : 
   30761                 :     8673994 :   if (DECL_CLASS_TEMPLATE_P (tmpl)
   30762                 :   679515629 :       || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   30763                 :             :     return true;
   30764                 :   670693224 :   if (!DECL_ALIAS_TEMPLATE_P (tmpl))
   30765                 :             :     return false;
   30766                 :      125206 :   tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
   30767                 :      125206 :   if (tree tinfo = get_template_info (orig))
   30768                 :       23298 :     return ctad_template_p (TI_TEMPLATE (tinfo));
   30769                 :             :   return false;
   30770                 :             : }
   30771                 :             : 
   30772                 :             : /* Deduce template arguments for the class template placeholder PTYPE for
   30773                 :             :    template TMPL based on the initializer INIT, and return the resulting
   30774                 :             :    type.  */
   30775                 :             : 
   30776                 :             : static tree
   30777                 :       10524 : do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs,
   30778                 :             :                     int flags, tsubst_flags_t complain)
   30779                 :             : {
   30780                 :             :   /* We should have handled this in the caller.  */
   30781                 :       10524 :   if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
   30782                 :             :     return ptype;
   30783                 :             : 
   30784                 :             :   /* If the class was erroneous, don't try to deduce, because that
   30785                 :             :      can generate a lot of diagnostic.  */
   30786                 :       10448 :   if (TREE_TYPE (tmpl)
   30787                 :       10448 :       && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl))
   30788                 :       20894 :       && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl)))
   30789                 :             :     return ptype;
   30790                 :             : 
   30791                 :             :   /* Wait until the enclosing scope is non-dependent.  */
   30792                 :       20888 :   if (DECL_CLASS_SCOPE_P (tmpl)
   30793                 :       10529 :       && dependent_type_p (DECL_CONTEXT (tmpl)))
   30794                 :             :     return ptype;
   30795                 :             : 
   30796                 :             :   /* Initializing one placeholder from another.  */
   30797                 :       10434 :   if (init
   30798                 :       10340 :       && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
   30799                 :       10308 :           || (TREE_CODE (init) == EXPR_PACK_EXPANSION
   30800                 :           1 :               && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
   30801                 :             :                   == TEMPLATE_PARM_INDEX)))
   30802                 :          33 :       && is_auto (TREE_TYPE (init))
   30803                 :       10467 :       && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
   30804                 :          13 :     return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
   30805                 :             : 
   30806                 :       10421 :   if (!ctad_template_p (tmpl))
   30807                 :             :     {
   30808                 :           2 :       if (complain & tf_error)
   30809                 :           2 :         error ("non-deducible template %qT used without template arguments", tmpl);
   30810                 :           2 :       return error_mark_node;
   30811                 :             :     }
   30812                 :       10419 :   else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
   30813                 :             :     {
   30814                 :           7 :       if (complain & tf_error)
   30815                 :             :         {
   30816                 :             :           /* Be permissive with equivalent alias templates.  */
   30817                 :           7 :           tree u = get_underlying_template (tmpl);
   30818                 :           7 :           diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN;
   30819                 :           7 :           bool complained
   30820                 :           7 :             = emit_diagnostic (dk, input_location, 0,
   30821                 :             :                                "alias template deduction only available "
   30822                 :             :                                "with %<-std=c++20%> or %<-std=gnu++20%>");
   30823                 :           7 :           if (u == tmpl)
   30824                 :           6 :             return error_mark_node;
   30825                 :           1 :           else if (complained)
   30826                 :             :             {
   30827                 :           1 :               inform (input_location, "use %qD directly instead", u);
   30828                 :           1 :               tmpl = u;
   30829                 :             :             }
   30830                 :             :         }
   30831                 :             :       else
   30832                 :           0 :         return error_mark_node;
   30833                 :             :     }
   30834                 :             : 
   30835                 :             :   /* Wait until the initializer is non-dependent.  */
   30836                 :       10413 :   if (type_dependent_expression_p (init))
   30837                 :             :     return ptype;
   30838                 :             : 
   30839                 :       10383 :   if (outer_targs)
   30840                 :             :     {
   30841                 :          33 :       int args_depth = TMPL_ARGS_DEPTH (outer_targs);
   30842                 :          33 :       int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
   30843                 :          33 :       if (parms_depth > 1)
   30844                 :             :         {
   30845                 :             :           /* Substitute outer arguments into this CTAD template from the
   30846                 :             :              current instantiation.  */
   30847                 :           1 :           int want = std::min (args_depth, parms_depth - 1);
   30848                 :           1 :           outer_targs = strip_innermost_template_args (outer_targs,
   30849                 :             :                                                        args_depth - want);
   30850                 :           1 :           tmpl = tsubst (tmpl, outer_targs, complain, NULL_TREE);
   30851                 :           1 :           if (tmpl == error_mark_node)
   30852                 :           0 :             return error_mark_node;
   30853                 :             :         }
   30854                 :             :     }
   30855                 :             : 
   30856                 :             :   /* Don't bother with the alias rules for an equivalent template.  */
   30857                 :       10383 :   tmpl = get_underlying_template (tmpl);
   30858                 :             : 
   30859                 :       10383 :   tree type = TREE_TYPE (tmpl);
   30860                 :             : 
   30861                 :       10383 :   bool try_list_cand = false;
   30862                 :       10383 :   bool list_init_p = false;
   30863                 :             : 
   30864                 :       10383 :   releasing_vec rv_args = NULL;
   30865                 :       10383 :   vec<tree,va_gc> *&args = *&rv_args;
   30866                 :       10383 :   if (init == NULL_TREE)
   30867                 :          93 :     args = make_tree_vector ();
   30868                 :       10290 :   else if (BRACE_ENCLOSED_INITIALIZER_P (init))
   30869                 :             :     {
   30870                 :        3385 :       list_init_p = true;
   30871                 :        3385 :       try_list_cand = true;
   30872                 :        3385 :       if (CONSTRUCTOR_NELTS (init) == 1
   30873                 :        2207 :           && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
   30874                 :             :         {
   30875                 :             :           /* As an exception, the first phase in 16.3.1.7 (considering the
   30876                 :             :              initializer list as a single argument) is omitted if the
   30877                 :             :              initializer list consists of a single expression of type cv U,
   30878                 :             :              where U is a specialization of C or a class derived from a
   30879                 :             :              specialization of C.  */
   30880                 :        1473 :           tree elt = CONSTRUCTOR_ELT (init, 0)->value;
   30881                 :        1473 :           if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
   30882                 :          24 :             try_list_cand = false;
   30883                 :             :         }
   30884                 :          24 :       if (try_list_cand || is_std_init_list (type))
   30885                 :        3361 :         args = make_tree_vector_single (init);
   30886                 :             :       else
   30887                 :          24 :         args = make_tree_vector_from_ctor (init);
   30888                 :             :     }
   30889                 :        6905 :   else if (TREE_CODE (init) == TREE_LIST)
   30890                 :        2639 :     args = make_tree_vector_from_list (init);
   30891                 :             :   else
   30892                 :        4266 :     args = make_tree_vector_single (init);
   30893                 :             : 
   30894                 :             :   /* Do this now to avoid problems with erroneous args later on.  */
   30895                 :       10383 :   args = resolve_args (args, complain);
   30896                 :       10383 :   if (args == NULL)
   30897                 :           6 :     return error_mark_node;
   30898                 :             : 
   30899                 :       10377 :   bool any_dguides_p = false;
   30900                 :       10377 :   tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
   30901                 :       10377 :   if (cands == error_mark_node)
   30902                 :             :     return error_mark_node;
   30903                 :             : 
   30904                 :             :   /* Prune explicit deduction guides in copy-initialization context (but
   30905                 :             :      not copy-list-initialization).  */
   30906                 :       10375 :   bool elided = false;
   30907                 :       10375 :   if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
   30908                 :             :     {
   30909                 :       65079 :       for (lkp_iterator iter (cands); !elided && iter; ++iter)
   30910                 :       60821 :         if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   30911                 :         106 :           elided = true;
   30912                 :             : 
   30913                 :        4258 :       if (elided)
   30914                 :             :         {
   30915                 :             :           /* Found a nonconverting guide, prune the candidates.  */
   30916                 :         106 :           tree pruned = NULL_TREE;
   30917                 :        1654 :           for (lkp_iterator iter (cands); iter; ++iter)
   30918                 :        1548 :             if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
   30919                 :        1186 :               pruned = lookup_add (*iter, pruned);
   30920                 :             : 
   30921                 :         106 :           cands = pruned;
   30922                 :             :         }
   30923                 :             :     }
   30924                 :             : 
   30925                 :       10375 :   if (!any_dguides_p)
   30926                 :        1562 :     if (tree guide = maybe_aggr_guide (tmpl, init, args))
   30927                 :         178 :       cands = lookup_add (guide, cands);
   30928                 :             : 
   30929                 :       10375 :   tree fndecl = error_mark_node;
   30930                 :             : 
   30931                 :             :   /* If this is list-initialization and the class has a list guide, first
   30932                 :             :      try deducing from the list as a single argument, as [over.match.list].  */
   30933                 :       10375 :   if (try_list_cand)
   30934                 :             :     {
   30935                 :        3361 :       tree list_cands = NULL_TREE;
   30936                 :       79202 :       for (tree dg : lkp_range (cands))
   30937                 :       75841 :         if (is_list_ctor (dg))
   30938                 :         667 :           list_cands = lookup_add (dg, list_cands);
   30939                 :        3361 :       if (list_cands)
   30940                 :         349 :         fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
   30941                 :        3361 :       if (fndecl == error_mark_node)
   30942                 :             :         {
   30943                 :             :           /* That didn't work, now try treating the list as a sequence of
   30944                 :             :              arguments.  */
   30945                 :        3310 :           release_tree_vector (args);
   30946                 :        3310 :           args = make_tree_vector_from_ctor (init);
   30947                 :        3310 :           args = resolve_args (args, complain);
   30948                 :        3310 :           if (args == NULL)
   30949                 :           4 :             return error_mark_node;
   30950                 :             :         }
   30951                 :             :     }
   30952                 :             : 
   30953                 :       10371 :   if (elided && !cands)
   30954                 :             :     {
   30955                 :           0 :       error ("cannot deduce template arguments for copy-initialization"
   30956                 :             :              " of %qT, as it has no non-explicit deduction guides or "
   30957                 :             :              "user-declared constructors", type);
   30958                 :           0 :       return error_mark_node;
   30959                 :             :     }
   30960                 :       10371 :   else if (!cands && fndecl == error_mark_node)
   30961                 :             :     {
   30962                 :           0 :       error ("cannot deduce template arguments of %qT, as it has no viable "
   30963                 :             :              "deduction guides", type);
   30964                 :           0 :       return error_mark_node;
   30965                 :             :     }
   30966                 :             : 
   30967                 :       10371 :   if (fndecl == error_mark_node)
   30968                 :       10320 :     fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
   30969                 :             : 
   30970                 :       10371 :   if (fndecl == error_mark_node)
   30971                 :             :     {
   30972                 :         260 :       if (complain & tf_warning_or_error)
   30973                 :             :         {
   30974                 :         120 :           error ("class template argument deduction failed:");
   30975                 :         120 :           perform_dguide_overload_resolution (cands, args, complain);
   30976                 :         120 :           if (elided)
   30977                 :          17 :             inform (input_location, "explicit deduction guides not considered "
   30978                 :             :                     "for copy-initialization");
   30979                 :             :         }
   30980                 :         260 :       return error_mark_node;
   30981                 :             :     }
   30982                 :             :   /* [over.match.list]/1: In copy-list-initialization, if an explicit
   30983                 :             :      constructor is chosen, the initialization is ill-formed.  */
   30984                 :       10111 :   else if (flags & LOOKUP_ONLYCONVERTING)
   30985                 :             :     {
   30986                 :        4358 :       if (DECL_NONCONVERTING_P (fndecl))
   30987                 :             :         {
   30988                 :          23 :           if (complain & tf_warning_or_error)
   30989                 :             :             {
   30990                 :             :               // TODO: Pass down location from cp_finish_decl.
   30991                 :          23 :               error ("class template argument deduction for %qT failed: "
   30992                 :             :                      "explicit deduction guide selected in "
   30993                 :             :                      "copy-list-initialization", type);
   30994                 :          23 :               inform (DECL_SOURCE_LOCATION (fndecl),
   30995                 :             :                       "explicit deduction guide declared here");
   30996                 :             : 
   30997                 :             :             }
   30998                 :          23 :           return error_mark_node;
   30999                 :             :         }
   31000                 :             :     }
   31001                 :             : 
   31002                 :             :   /* If CTAD succeeded but the type doesn't have any explicit deduction
   31003                 :             :      guides, this deduction might not be what the user intended.  */
   31004                 :       10088 :   if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning))
   31005                 :             :     {
   31006                 :        1379 :       if ((!DECL_IN_SYSTEM_HEADER (fndecl)
   31007                 :         865 :            || global_dc->m_warn_system_headers)
   31008                 :        1381 :           && warning (OPT_Wctad_maybe_unsupported,
   31009                 :             :                       "%qT may not intend to support class template argument "
   31010                 :             :                       "deduction", type))
   31011                 :          16 :         inform (input_location, "add a deduction guide to suppress this "
   31012                 :             :                 "warning");
   31013                 :             :     }
   31014                 :             : 
   31015                 :       10088 :   return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
   31016                 :       10088 :                                   cp_type_quals (ptype));
   31017                 :       10383 : }
   31018                 :             : 
   31019                 :             : /* Return true if INIT is an unparenthesized id-expression or an
   31020                 :             :    unparenthesized class member access.  Used for the argument of
   31021                 :             :    decltype(auto).  */
   31022                 :             : 
   31023                 :             : bool
   31024                 :     2231035 : unparenthesized_id_or_class_member_access_p (tree init)
   31025                 :             : {
   31026                 :     2231035 :   STRIP_ANY_LOCATION_WRAPPER (init);
   31027                 :             : 
   31028                 :             :   /* We need to be able to tell '(r)' and 'r' apart (when it's of
   31029                 :             :      reference type).  Only the latter is an id-expression.  */
   31030                 :      330347 :   if (REFERENCE_REF_P (init)
   31031                 :     2450675 :       && !REF_PARENTHESIZED_P (init))
   31032                 :        2945 :     init = TREE_OPERAND (init, 0);
   31033                 :     2231035 :   return (DECL_P (init)
   31034                 :     2231035 :           || ((TREE_CODE (init) == COMPONENT_REF
   31035                 :     2230354 :                || TREE_CODE (init) == SCOPE_REF)
   31036                 :         247 :               && !REF_PARENTHESIZED_P (init)));
   31037                 :             : }
   31038                 :             : 
   31039                 :             : /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
   31040                 :             :    from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
   31041                 :             :    The CONTEXT determines the context in which auto deduction is performed
   31042                 :             :    and is used to control error diagnostics.  FLAGS are the LOOKUP_* flags.
   31043                 :             : 
   31044                 :             :    OUTER_TARGS is used during template argument deduction (context == adc_unify)
   31045                 :             :    to properly substitute the result.  It's also used in the adc_unify and
   31046                 :             :    adc_requirement contexts to communicate the necessary template arguments
   31047                 :             :    to satisfaction.  OUTER_TARGS is ignored in other contexts.
   31048                 :             : 
   31049                 :             :    Additionally for adc_unify contexts TMPL is the template for which TYPE
   31050                 :             :    is a template parameter type.
   31051                 :             : 
   31052                 :             :    For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current
   31053                 :             :    scope may be appended to the list of deduced template arguments prior to
   31054                 :             :    determining constraint satisfaction as appropriate.  */
   31055                 :             : 
   31056                 :             : tree
   31057                 :     9919227 : do_auto_deduction (tree type, tree init, tree auto_node,
   31058                 :             :                    tsubst_flags_t complain /* = tf_warning_or_error */,
   31059                 :             :                    auto_deduction_context context /* = adc_unspecified */,
   31060                 :             :                    tree outer_targs /* = NULL_TREE */,
   31061                 :             :                    int flags /* = LOOKUP_NORMAL */,
   31062                 :             :                    tree tmpl /* = NULL_TREE */)
   31063                 :             : {
   31064                 :     9919227 :   if (type == error_mark_node || init == error_mark_node)
   31065                 :             :     return error_mark_node;
   31066                 :             : 
   31067                 :     9918632 :   if (init && type_dependent_expression_p (init)
   31068                 :    14993516 :       && context != adc_unify)
   31069                 :             :     /* Defining a subset of type-dependent expressions that we can deduce
   31070                 :             :        from ahead of time isn't worth the trouble.  */
   31071                 :             :     return type;
   31072                 :             : 
   31073                 :             :   /* Similarly, we can't deduce from another undeduced decl.  */
   31074                 :     4846160 :   if (init && undeduced_auto_decl (init))
   31075                 :             :     return type;
   31076                 :             : 
   31077                 :             :   /* We may be doing a partial substitution, but we still want to replace
   31078                 :             :      auto_node.  */
   31079                 :     4846153 :   complain &= ~tf_partial;
   31080                 :             : 
   31081                 :     4846153 :   if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
   31082                 :             :     {
   31083                 :             :       /* We don't recurse here because we can't deduce from a nested
   31084                 :             :          initializer_list.  */
   31085                 :        3901 :       if (CONSTRUCTOR_ELTS (init))
   31086                 :        7492 :         for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
   31087                 :        4792 :           elt.value = resolve_nondeduced_context (elt.value, complain);
   31088                 :             :     }
   31089                 :     4842252 :   else if (init)
   31090                 :     4842133 :     init = resolve_nondeduced_context (init, complain);
   31091                 :             : 
   31092                 :             :   /* In C++23, we must deduce the type to int&& for code like
   31093                 :             :        decltype(auto) f(int&& x) { return (x); }
   31094                 :             :      or
   31095                 :             :        auto&& f(int x) { return x; }
   31096                 :             :      so we use treat_lvalue_as_rvalue_p.  But don't do it for
   31097                 :             :        decltype(auto) f(int x) { return x; }
   31098                 :             :      where we should deduce 'int' rather than 'int&&'; transmogrifying
   31099                 :             :      INIT to an rvalue would break that.  */
   31100                 :     4846153 :   tree r;
   31101                 :     4846153 :   if (cxx_dialect >= cxx23
   31102                 :     2168374 :       && context == adc_return_type
   31103                 :      377580 :       && (!AUTO_IS_DECLTYPE (auto_node)
   31104                 :      103842 :           || !unparenthesized_id_or_class_member_access_p (init))
   31105                 :     5223605 :       && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
   31106                 :             :                                         /*return*/true)))
   31107                 :       13066 :     init = r;
   31108                 :             : 
   31109                 :     4846153 :   if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
   31110                 :             :     /* C++17 class template argument deduction.  */
   31111                 :       10524 :     return do_class_deduction (type, ctmpl, init, outer_targs, flags, complain);
   31112                 :             : 
   31113                 :     4835629 :   if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
   31114                 :             :     /* Nothing we can do with this, even in deduction context.  */
   31115                 :             :     return type;
   31116                 :             : 
   31117                 :     4833680 :   location_t loc = cp_expr_loc_or_input_loc (init);
   31118                 :             : 
   31119                 :             :   /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
   31120                 :             :      with either a new invented type template parameter U or, if the
   31121                 :             :      initializer is a braced-init-list (8.5.4), with
   31122                 :             :      std::initializer_list<U>.  */
   31123                 :     4833680 :   if (BRACE_ENCLOSED_INITIALIZER_P (init))
   31124                 :             :     {
   31125                 :         456 :       if (!DIRECT_LIST_INIT_P (init))
   31126                 :         308 :         type = listify_autos (type, auto_node);
   31127                 :         148 :       else if (CONSTRUCTOR_NELTS (init) == 1)
   31128                 :         142 :         init = CONSTRUCTOR_ELT (init, 0)->value;
   31129                 :             :       else
   31130                 :             :         {
   31131                 :           6 :           if (complain & tf_warning_or_error)
   31132                 :             :             {
   31133                 :           6 :               if (permerror (loc, "direct-list-initialization of "
   31134                 :             :                              "%<auto%> requires exactly one element"))
   31135                 :           6 :                 inform (loc,
   31136                 :             :                         "for deduction to %<std::initializer_list%>, use copy-"
   31137                 :             :                         "list-initialization (i.e. add %<=%> before the %<{%>)");
   31138                 :             :             }
   31139                 :           6 :           type = listify_autos (type, auto_node);
   31140                 :             :         }
   31141                 :             :     }
   31142                 :             : 
   31143                 :     4833680 :   if (type == error_mark_node || init == error_mark_node)
   31144                 :             :     return error_mark_node;
   31145                 :             : 
   31146                 :     4833663 :   tree targs;
   31147                 :     4833663 :   if (context == adc_decomp_type
   31148                 :     4833663 :       && auto_node == type
   31149                 :     4833663 :       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
   31150                 :             :     {
   31151                 :             :       /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
   31152                 :             :          and initializer has array type, deduce cv-qualified array type.  */
   31153                 :         163 :       targs = make_tree_vec (1);
   31154                 :         163 :       TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
   31155                 :             :     }
   31156                 :     4833500 :   else if (AUTO_IS_DECLTYPE (auto_node))
   31157                 :             :     {
   31158                 :     2127193 :       const bool id = unparenthesized_id_or_class_member_access_p (init);
   31159                 :     2127193 :       tree deduced = finish_decltype_type (init, id, complain);
   31160                 :     2127193 :       deduced = canonicalize_type_argument (deduced, complain);
   31161                 :     2127193 :       if (deduced == error_mark_node)
   31162                 :             :         return error_mark_node;
   31163                 :     2127191 :       targs = make_tree_vec (1);
   31164                 :     2127191 :       TREE_VEC_ELT (targs, 0) = deduced;
   31165                 :             :     }
   31166                 :             :   else
   31167                 :             :     {
   31168                 :     2706307 :       if (error_operand_p (init))
   31169                 :             :         return error_mark_node;
   31170                 :             : 
   31171                 :     2706297 :       tree parms = build_tree_list (NULL_TREE, type);
   31172                 :     2706297 :       tree tparms;
   31173                 :             : 
   31174                 :     2706297 :       if (flag_concepts_ts)
   31175                 :         370 :         tparms = extract_autos (type);
   31176                 :             :       else
   31177                 :             :         {
   31178                 :     2705927 :           tparms = make_tree_vec (1);
   31179                 :     2705927 :           TREE_VEC_ELT (tparms, 0)
   31180                 :     5411854 :             = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
   31181                 :             :         }
   31182                 :             : 
   31183                 :     2706297 :       targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
   31184                 :     2706297 :       int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
   31185                 :             :                                        DEDUCE_CALL,
   31186                 :             :                                        NULL, /*explain_p=*/false);
   31187                 :     2706297 :       if (val > 0)
   31188                 :             :         {
   31189                 :          66 :           if (processing_template_decl)
   31190                 :             :             /* Try again at instantiation time.  */
   31191                 :             :             return type;
   31192                 :          59 :           if (type && type != error_mark_node
   31193                 :          59 :               && (complain & tf_error))
   31194                 :             :             /* If type is error_mark_node a diagnostic must have been
   31195                 :             :                emitted by now.  Also, having a mention to '<type error>'
   31196                 :             :                in the diagnostic is not really useful to the user.  */
   31197                 :             :             {
   31198                 :          57 :               if (cfun
   31199                 :          28 :                   && FNDECL_USED_AUTO (current_function_decl)
   31200                 :          12 :                   && (auto_node
   31201                 :          12 :                       == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
   31202                 :          69 :                   && LAMBDA_FUNCTION_P (current_function_decl))
   31203                 :           6 :                 error_at (loc, "unable to deduce lambda return type from %qE",
   31204                 :             :                           init);
   31205                 :             :               else
   31206                 :          51 :                 error_at (loc, "unable to deduce %qT from %qE", type, init);
   31207                 :          57 :               type_unification_real (tparms, targs, parms, &init, 1, 0,
   31208                 :             :                                      DEDUCE_CALL,
   31209                 :             :                                      NULL, /*explain_p=*/true);
   31210                 :             :             }
   31211                 :          59 :           return error_mark_node;
   31212                 :             :         }
   31213                 :             :     }
   31214                 :             : 
   31215                 :             :   /* Check any placeholder constraints against the deduced type. */
   31216                 :     4833585 :   if (processing_template_decl && context == adc_unify)
   31217                 :             :     /* Constraints will be checked after deduction.  */;
   31218                 :     4833363 :   else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
   31219                 :             :     {
   31220                 :     1942135 :       if (processing_template_decl)
   31221                 :             :         {
   31222                 :          20 :           gcc_checking_assert (context == adc_variable_type
   31223                 :             :                                || context == adc_return_type
   31224                 :             :                                || context == adc_decomp_type);
   31225                 :          20 :           gcc_checking_assert (!type_dependent_expression_p (init));
   31226                 :             :           /* If the constraint is dependent, we need to wait until
   31227                 :             :              instantiation time to resolve the placeholder.  */
   31228                 :          20 :           if (placeholder_type_constraint_dependent_p (constr))
   31229                 :             :             return type;
   31230                 :             :         }
   31231                 :             : 
   31232                 :     1942127 :       if (context == adc_return_type
   31233                 :     1942127 :           || context == adc_variable_type
   31234                 :     1942127 :           || context == adc_decomp_type)
   31235                 :         205 :         if (tree fn = current_function_decl)
   31236                 :         169 :           if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn))
   31237                 :             :             {
   31238                 :          42 :               outer_targs = DECL_TEMPLATE_INFO (fn)
   31239                 :          42 :                 ? DECL_TI_ARGS (fn) : NULL_TREE;
   31240                 :          46 :               if (LAMBDA_FUNCTION_P (fn))
   31241                 :             :                 {
   31242                 :             :                   /* As in satisfy_declaration_constraints.  */
   31243                 :           4 :                   tree regen_args = lambda_regenerating_args (fn);
   31244                 :           4 :                   if (outer_targs)
   31245                 :           0 :                     outer_targs = add_to_template_args (regen_args, outer_targs);
   31246                 :             :                   else
   31247                 :             :                     outer_targs = regen_args;
   31248                 :             :                 }
   31249                 :             :             }
   31250                 :             : 
   31251                 :     1942127 :       tree full_targs = outer_targs;
   31252                 :     1942127 :       if (context == adc_unify && tmpl)
   31253                 :          61 :         full_targs = add_outermost_template_args (tmpl, full_targs);
   31254                 :     1942127 :       full_targs = add_to_template_args (full_targs, targs);
   31255                 :             : 
   31256                 :             :       /* HACK: Compensate for callers not always communicating all levels of
   31257                 :             :          outer template arguments by filling in the outermost missing levels
   31258                 :             :          with dummy levels before checking satisfaction.  We'll still crash
   31259                 :             :          if the constraint depends on a template argument belonging to one of
   31260                 :             :          these missing levels, but this hack otherwise allows us to handle a
   31261                 :             :          large subset of possible constraints (including all non-dependent
   31262                 :             :          constraints).  */
   31263                 :     1942127 :       if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
   31264                 :     3884417 :                                 - TMPL_ARGS_DEPTH (full_targs)))
   31265                 :             :         {
   31266                 :           4 :           tree dummy_levels = make_tree_vec (missing_levels);
   31267                 :           8 :           for (int i = 0; i < missing_levels; ++i)
   31268                 :           4 :             TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
   31269                 :           4 :           full_targs = add_to_template_args (dummy_levels, full_targs);
   31270                 :             :         }
   31271                 :             : 
   31272                 :     1942127 :       if (!constraints_satisfied_p (auto_node, full_targs))
   31273                 :             :         {
   31274                 :         559 :           if (complain & tf_warning_or_error)
   31275                 :             :             {
   31276                 :          56 :               auto_diagnostic_group d;
   31277                 :          56 :               switch (context)
   31278                 :             :                 {
   31279                 :           9 :                 case adc_unspecified:
   31280                 :           9 :                 case adc_unify:
   31281                 :           9 :                   error_at (loc, "placeholder constraints not satisfied");
   31282                 :           9 :                   break;
   31283                 :          26 :                 case adc_variable_type:
   31284                 :          26 :                 case adc_decomp_type:
   31285                 :          26 :                   error_at (loc, "deduced initializer does not satisfy "
   31286                 :             :                             "placeholder constraints");
   31287                 :          26 :                   break;
   31288                 :          17 :                 case adc_return_type:
   31289                 :          17 :                   error_at (loc, "deduced return type does not satisfy "
   31290                 :             :                             "placeholder constraints");
   31291                 :          17 :                   break;
   31292                 :           4 :                 case adc_requirement:
   31293                 :           4 :                   error_at (loc, "deduced expression type does not satisfy "
   31294                 :             :                             "placeholder constraints");
   31295                 :           4 :                   break;
   31296                 :             :                 }
   31297                 :          56 :               diagnose_constraints (loc, auto_node, full_targs);
   31298                 :          56 :             }
   31299                 :         559 :           return error_mark_node;
   31300                 :             :         }
   31301                 :             :     }
   31302                 :             : 
   31303                 :     4833018 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 0)
   31304                 :             :     {
   31305                 :             :       /* Substitute this level-less auto via tsubst by temporarily
   31306                 :             :          overriding its level to 1.  */
   31307                 :         245 :       TEMPLATE_TYPE_LEVEL (auto_node) = 1;
   31308                 :         245 :       type = tsubst (type, targs, complain, NULL_TREE);
   31309                 :         245 :       TEMPLATE_TYPE_LEVEL (auto_node) = 0;
   31310                 :         245 :       return type;
   31311                 :             :     }
   31312                 :             : 
   31313                 :     4832773 :   if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
   31314                 :             :     /* The outer template arguments are already substituted into type
   31315                 :             :        (but we still may have used them for constraint checking above).  */;
   31316                 :      515349 :   else if (context == adc_unify)
   31317                 :        1624 :     targs = add_to_template_args (outer_targs, targs);
   31318                 :      513725 :   else if (processing_template_decl)
   31319                 :      513725 :     targs = add_to_template_args (current_template_args (), targs);
   31320                 :     4832773 :   return tsubst (type, targs, complain, NULL_TREE);
   31321                 :             : }
   31322                 :             : 
   31323                 :             : /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
   31324                 :             :    result.  */
   31325                 :             : 
   31326                 :             : tree
   31327                 :   160194534 : splice_late_return_type (tree type, tree late_return_type)
   31328                 :             : {
   31329                 :   160194534 :   if (late_return_type)
   31330                 :             :     {
   31331                 :     2593866 :       gcc_assert (is_auto (type) || seen_error ());
   31332                 :     2593866 :       return late_return_type;
   31333                 :             :     }
   31334                 :             : 
   31335                 :   157600668 :   if (tree auto_node = find_type_usage (type, is_auto))
   31336                 :     1671565 :     if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth)
   31337                 :             :       {
   31338                 :             :         /* In an abbreviated function template we didn't know we were dealing
   31339                 :             :            with a function template when we saw the auto return type, so rebuild
   31340                 :             :            the return type using an auto with the correct level.  */
   31341                 :          34 :         tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
   31342                 :          34 :         tree auto_vec = make_tree_vec (1);
   31343                 :          34 :         TREE_VEC_ELT (auto_vec, 0) = new_auto;
   31344                 :          34 :         tree targs = add_outermost_template_args (current_template_args (),
   31345                 :             :                                                   auto_vec);
   31346                 :             :         /* Also rebuild the constraint info in terms of the new auto.  */
   31347                 :          34 :         if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
   31348                 :           4 :           PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
   31349                 :           4 :             = build_tree_list (current_template_parms,
   31350                 :           2 :                                tsubst_constraint (TREE_VALUE (ci), targs,
   31351                 :             :                                                   tf_none, NULL_TREE));
   31352                 :          34 :         TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
   31353                 :          34 :         return tsubst (type, targs, tf_none, NULL_TREE);
   31354                 :             :       }
   31355                 :             :   return type;
   31356                 :             : }
   31357                 :             : 
   31358                 :             : /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
   31359                 :             :    'decltype(auto)' or a deduced class template.  */
   31360                 :             : 
   31361                 :             : bool
   31362                 :  7240557963 : is_auto (const_tree type)
   31363                 :             : {
   31364                 :  7240557963 :   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
   31365                 :  7240557963 :       && (TYPE_IDENTIFIER (type) == auto_identifier
   31366                 :  3322471684 :           || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
   31367                 :             :     return true;
   31368                 :             :   else
   31369                 :  6952592023 :     return false;
   31370                 :             : }
   31371                 :             : 
   31372                 :             : /* for_each_template_parm callback for type_uses_auto.  */
   31373                 :             : 
   31374                 :             : int
   31375                 :      107190 : is_auto_r (tree tp, void */*data*/)
   31376                 :             : {
   31377                 :      107190 :   return is_auto (tp);
   31378                 :             : }
   31379                 :             : 
   31380                 :             : /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
   31381                 :             :    a use of `auto'.  Returns NULL_TREE otherwise.  */
   31382                 :             : 
   31383                 :             : tree
   31384                 :  2602959019 : type_uses_auto (tree type)
   31385                 :             : {
   31386                 :  2602959019 :   if (type == NULL_TREE)
   31387                 :             :     return NULL_TREE;
   31388                 :             : 
   31389                 :             :   /* For parameter packs, check the contents of the pack.  */
   31390                 :  2601157242 :   if (PACK_EXPANSION_P (type))
   31391                 :     6202657 :     type = PACK_EXPANSION_PATTERN (type);
   31392                 :             : 
   31393                 :  2601157242 :   if (flag_concepts_ts)
   31394                 :             :     {
   31395                 :             :       /* The Concepts TS allows multiple autos in one type-specifier; just
   31396                 :             :          return the first one we find, do_auto_deduction will collect all of
   31397                 :             :          them.  */
   31398                 :      247927 :       if (uses_template_parms (type))
   31399                 :      100102 :         return for_each_template_parm (type, is_auto_r, /*data*/NULL,
   31400                 :      100102 :                                        /*visited*/NULL, /*nondeduced*/false);
   31401                 :             :       else
   31402                 :             :         return NULL_TREE;
   31403                 :             :     }
   31404                 :             :   else
   31405                 :  2600909315 :     return find_type_usage (type, is_auto);
   31406                 :             : }
   31407                 :             : 
   31408                 :             : /* Report ill-formed occurrences of auto types in ARGUMENTS.  If
   31409                 :             :    concepts are enabled, auto is acceptable in template arguments, but
   31410                 :             :    only when TEMPL identifies a template class.  Return TRUE if any
   31411                 :             :    such errors were reported.  */
   31412                 :             : 
   31413                 :             : bool
   31414                 :    51053950 : check_auto_in_tmpl_args (tree tmpl, tree args)
   31415                 :             : {
   31416                 :    51053950 :   if (!flag_concepts_ts)
   31417                 :             :     /* Only the concepts TS allows 'auto' as a type-id; it'd otherwise
   31418                 :             :        have already been rejected by the parser more generally.  */
   31419                 :             :     return false;
   31420                 :             : 
   31421                 :             :   /* If there were previous errors, nevermind.  */
   31422                 :       36778 :   if (!args || TREE_CODE (args) != TREE_VEC)
   31423                 :             :     return false;
   31424                 :             : 
   31425                 :             :   /* If TMPL is an identifier, we're parsing and we can't tell yet
   31426                 :             :      whether TMPL is supposed to be a type, a function or a variable.
   31427                 :             :      We'll only be able to tell during template substitution, so we
   31428                 :             :      expect to be called again then.  If concepts are enabled and we
   31429                 :             :      know we have a type, we're ok.  */
   31430                 :    51090630 :   if (identifier_p (tmpl)
   31431                 :       36684 :       || (DECL_P (tmpl)
   31432                 :       32127 :           &&  (DECL_TYPE_TEMPLATE_P (tmpl)
   31433                 :        6423 :                || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))))
   31434                 :             :     return false;
   31435                 :             : 
   31436                 :             :   /* Quickly search for any occurrences of auto; usually there won't
   31437                 :             :      be any, and then we'll avoid allocating the vector.  */
   31438                 :       10980 :   if (!type_uses_auto (args))
   31439                 :             :     return false;
   31440                 :             : 
   31441                 :           4 :   bool errors = false;
   31442                 :             : 
   31443                 :           4 :   tree vec = extract_autos (args);
   31444                 :           8 :   for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
   31445                 :             :     {
   31446                 :           4 :       tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
   31447                 :           4 :       error_at (DECL_SOURCE_LOCATION (xauto),
   31448                 :             :                 "invalid use of %qT in template argument", xauto);
   31449                 :           4 :       errors = true;
   31450                 :             :     }
   31451                 :             : 
   31452                 :             :   return errors;
   31453                 :             : }
   31454                 :             : 
   31455                 :             : /* Recursively walk over && expressions searching for EXPR. Return a reference
   31456                 :             :    to that expression.  */
   31457                 :             : 
   31458                 :          16 : static tree *find_template_requirement (tree *t, tree key)
   31459                 :             : {
   31460                 :          16 :   if (*t == key)
   31461                 :             :     return t;
   31462                 :           8 :   if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
   31463                 :             :     {
   31464                 :           4 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
   31465                 :             :         return p;
   31466                 :           4 :       if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
   31467                 :             :         return p;
   31468                 :             :     }
   31469                 :             :   return 0;
   31470                 :             : }
   31471                 :             : 
   31472                 :             : /* Convert the generic type parameters in PARM that match the types given in the
   31473                 :             :    range [START_IDX, END_IDX) from the current_template_parms into generic type
   31474                 :             :    packs.  */
   31475                 :             : 
   31476                 :             : tree
   31477                 :       30188 : convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
   31478                 :             : {
   31479                 :       30188 :   tree current = current_template_parms;
   31480                 :       30188 :   int depth = TMPL_PARMS_DEPTH (current);
   31481                 :       30188 :   current = INNERMOST_TEMPLATE_PARMS (current);
   31482                 :       30188 :   tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
   31483                 :             : 
   31484                 :       31088 :   for (int i = 0; i < start_idx; ++i)
   31485                 :        1800 :     TREE_VEC_ELT (replacement, i)
   31486                 :         900 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   31487                 :             : 
   31488                 :       60382 :   for (int i = start_idx; i < end_idx; ++i)
   31489                 :             :     {
   31490                 :             :       /* Create a distinct parameter pack type from the current parm and add it
   31491                 :             :          to the replacement args to tsubst below into the generic function
   31492                 :             :          parameter.  */
   31493                 :       30194 :       tree node = TREE_VEC_ELT (current, i);
   31494                 :       30194 :       tree o = TREE_TYPE (TREE_VALUE (node));
   31495                 :       30194 :       tree t = copy_type (o);
   31496                 :       30194 :       TEMPLATE_TYPE_PARM_INDEX (t)
   31497                 :       30194 :         = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
   31498                 :             :                                       t, 0, 0, tf_none);
   31499                 :       30194 :       TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
   31500                 :       30194 :       TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
   31501                 :       30194 :       TYPE_MAIN_VARIANT (t) = t;
   31502                 :       30194 :       TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
   31503                 :       30194 :       TYPE_CANONICAL (t) = canonical_type_parameter (t);
   31504                 :       30194 :       TREE_VEC_ELT (replacement, i) = t;
   31505                 :             : 
   31506                 :             :       /* Replace the current template parameter with new pack.  */
   31507                 :       30194 :       TREE_VALUE (node) = TREE_CHAIN (t);
   31508                 :             : 
   31509                 :             :       /* Surgically adjust the associated constraint of adjusted parameter
   31510                 :             :          and it's corresponding contribution to the current template
   31511                 :             :          requirements.  */
   31512                 :       30194 :       if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
   31513                 :             :         {
   31514                 :           8 :           tree id = unpack_concept_check (constr);
   31515                 :           8 :           TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
   31516                 :             :           /* Use UNKNOWN_LOCATION so write_template_args can tell the
   31517                 :             :              difference between this and a fold the user wrote.  */
   31518                 :           8 :           location_t loc = UNKNOWN_LOCATION;
   31519                 :           8 :           tree fold = finish_left_unary_fold_expr (loc, constr,
   31520                 :             :                                                    TRUTH_ANDIF_EXPR);
   31521                 :           8 :           TEMPLATE_PARM_CONSTRAINTS (node) = fold;
   31522                 :             : 
   31523                 :             :           /* If there was a constraint, we also need to replace that in
   31524                 :             :              the template requirements, which we've already built.  */
   31525                 :           8 :           tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
   31526                 :           8 :           reqs = find_template_requirement (reqs, constr);
   31527                 :           8 :           *reqs = fold;
   31528                 :             :         }
   31529                 :             :     }
   31530                 :             : 
   31531                 :       30188 :   for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
   31532                 :           0 :     TREE_VEC_ELT (replacement, i)
   31533                 :           0 :       = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
   31534                 :             : 
   31535                 :             :   /* If there are more levels then build up the replacement with the outer
   31536                 :             :      template parms.  */
   31537                 :       30188 :   if (depth > 1)
   31538                 :       30009 :     replacement = add_to_template_args (template_parms_to_args
   31539                 :       30009 :                                         (TREE_CHAIN (current_template_parms)),
   31540                 :             :                                         replacement);
   31541                 :             : 
   31542                 :       30188 :   return tsubst (parm, replacement, tf_none, NULL_TREE);
   31543                 :             : }
   31544                 :             : 
   31545                 :             : /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
   31546                 :             :    0..N-1.  */
   31547                 :             : 
   31548                 :             : void
   31549                 :       87568 : declare_integer_pack (void)
   31550                 :             : {
   31551                 :       87568 :   tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
   31552                 :             :                                build_function_type_list (integer_type_node,
   31553                 :             :                                                          integer_type_node,
   31554                 :             :                                                          NULL_TREE),
   31555                 :             :                                NULL_TREE, ECF_CONST);
   31556                 :       87568 :   DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
   31557                 :       87568 :   set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
   31558                 :             :                               CP_BUILT_IN_INTEGER_PACK);
   31559                 :       87568 : }
   31560                 :             : 
   31561                 :             : /* Walk the decl or type specialization table calling FN on each
   31562                 :             :    entry.  */
   31563                 :             : 
   31564                 :             : void
   31565                 :        3636 : walk_specializations (bool decls_p,
   31566                 :             :                       void (*fn) (bool decls_p, spec_entry *entry, void *data),
   31567                 :             :                       void *data)
   31568                 :             : {
   31569                 :        3636 :   spec_hash_table *table = decls_p ? decl_specializations
   31570                 :             :     : type_specializations;
   31571                 :        3636 :   spec_hash_table::iterator end (table->end ());
   31572                 :     1547100 :   for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
   31573                 :      771732 :     fn (decls_p, *iter, data);
   31574                 :        3636 : }
   31575                 :             : 
   31576                 :             : /* Lookup the specialization of *ELT, in the decl or type
   31577                 :             :    specialization table.  Return the SPEC that's already there, or
   31578                 :             :    NULL if nothing.  */
   31579                 :             : 
   31580                 :             : tree
   31581                 :     2377730 : match_mergeable_specialization (bool decl_p, spec_entry *elt)
   31582                 :             : {
   31583                 :     4755460 :   hash_table<spec_hasher> *specializations
   31584                 :     2377730 :     = decl_p ? decl_specializations : type_specializations;
   31585                 :     2377730 :   hashval_t hash = spec_hasher::hash (elt);
   31586                 :     2377730 :   auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
   31587                 :             : 
   31588                 :     2377730 :   if (slot)
   31589                 :     1893567 :     return (*slot)->spec;
   31590                 :             : 
   31591                 :             :   return NULL_TREE;
   31592                 :             : }
   31593                 :             : 
   31594                 :             : /* Return flags encoding whether SPEC is on the instantiation and/or
   31595                 :             :    specialization lists of TMPL.  */
   31596                 :             : 
   31597                 :             : unsigned
   31598                 :      521563 : get_mergeable_specialization_flags (tree tmpl, tree decl)
   31599                 :             : {
   31600                 :      521563 :   unsigned flags = 0;
   31601                 :             : 
   31602                 :      521563 :   for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
   31603                 :    11087998 :        inst; inst = TREE_CHAIN (inst))
   31604                 :    10568144 :     if (TREE_VALUE (inst) == decl)
   31605                 :             :       {
   31606                 :             :         flags |= 1;
   31607                 :             :         break;
   31608                 :             :       }
   31609                 :             : 
   31610                 :     1043126 :   if (CLASS_TYPE_P (TREE_TYPE (decl))
   31611                 :      227284 :       && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
   31612                 :      742732 :       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   31613                 :             :     /* Only need to search if DECL is a partial specialization.  */
   31614                 :       17016 :     for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   31615                 :       37953 :          part; part = TREE_CHAIN (part))
   31616                 :       20937 :       if (TREE_VALUE (part) == decl)
   31617                 :             :         {
   31618                 :           0 :           flags |= 2;
   31619                 :           0 :           break;
   31620                 :             :         }
   31621                 :             : 
   31622                 :      521563 :   return flags;
   31623                 :             : }
   31624                 :             : 
   31625                 :             : /* Add a new specialization described by SPEC.  DECL is the
   31626                 :             :    maybe-template decl and FLAGS is as returned from
   31627                 :             :    get_mergeable_specialization_flags.  */
   31628                 :             : 
   31629                 :             : void
   31630                 :      244483 : add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl,
   31631                 :             :                               unsigned flags)
   31632                 :             : {
   31633                 :      244483 :   hashval_t hash = spec_hasher::hash (elt);
   31634                 :      244483 :   if (decl_p)
   31635                 :             :     {
   31636                 :      172633 :       auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
   31637                 :             : 
   31638                 :      172633 :       gcc_checking_assert (!*slot);
   31639                 :      172633 :       auto entry = ggc_alloc<spec_entry> ();
   31640                 :      172633 :       *entry = *elt;
   31641                 :      172633 :       *slot = entry;
   31642                 :             :     }
   31643                 :             :   else
   31644                 :             :     {
   31645                 :       71850 :       auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
   31646                 :             : 
   31647                 :             :       /* We don't distinguish different constrained partial type
   31648                 :             :          specializations, so there could be duplicates.  Everything else
   31649                 :             :          must be new.   */
   31650                 :       71850 :       if (!(flags & 2 && *slot))
   31651                 :             :         {
   31652                 :       71655 :           gcc_checking_assert (!*slot);
   31653                 :             : 
   31654                 :       71655 :           auto entry = ggc_alloc<spec_entry> ();
   31655                 :       71655 :           *entry = *elt;
   31656                 :       71655 :           *slot = entry;
   31657                 :             :         }
   31658                 :             :     }
   31659                 :             : 
   31660                 :      244483 :   if (flags & 1)
   31661                 :         627 :     DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
   31662                 :        1254 :       = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
   31663                 :             : 
   31664                 :      244483 :   if (flags & 2)
   31665                 :             :     {
   31666                 :             :       /* A partial specialization.  */
   31667                 :        8103 :       tree cons = tree_cons (elt->args, decl,
   31668                 :        8103 :                              DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
   31669                 :        8103 :       TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec;
   31670                 :        8103 :       DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
   31671                 :             :     }
   31672                 :      244483 : }
   31673                 :             : 
   31674                 :             : /* Set up the hash tables for template instantiations.  */
   31675                 :             : 
   31676                 :             : void
   31677                 :      101073 : init_template_processing (void)
   31678                 :             : {
   31679                 :      101073 :   decl_specializations = hash_table<spec_hasher>::create_ggc (37);
   31680                 :      101073 :   type_specializations = hash_table<spec_hasher>::create_ggc (37);
   31681                 :             : 
   31682                 :      101073 :   if (cxx_dialect >= cxx11)
   31683                 :       87568 :     declare_integer_pack ();
   31684                 :      101073 : }
   31685                 :             : 
   31686                 :             : /* Print stats about the template hash tables for -fstats.  */
   31687                 :             : 
   31688                 :             : void
   31689                 :           0 : print_template_statistics (void)
   31690                 :             : {
   31691                 :           0 :   fprintf (stderr, "decl_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   31692                 :             :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   31693                 :           0 :            (fmt_size_t) decl_specializations->size (),
   31694                 :           0 :            (fmt_size_t) decl_specializations->elements (),
   31695                 :             :            decl_specializations->collisions ());
   31696                 :           0 :   fprintf (stderr, "type_specializations: size " HOST_SIZE_T_PRINT_DEC ", "
   31697                 :             :            HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n",
   31698                 :           0 :            (fmt_size_t) type_specializations->size (),
   31699                 :           0 :            (fmt_size_t) type_specializations->elements (),
   31700                 :             :            type_specializations->collisions ());
   31701                 :           0 : }
   31702                 :             : 
   31703                 :             : #if CHECKING_P
   31704                 :             : 
   31705                 :             : namespace selftest {
   31706                 :             : 
   31707                 :             : /* Verify that type_dependent_expression_p () works correctly, even
   31708                 :             :    in the presence of location wrapper nodes.  */
   31709                 :             : 
   31710                 :             : static void
   31711                 :           1 : test_type_dependent_expression_p ()
   31712                 :             : {
   31713                 :           1 :   location_t loc = BUILTINS_LOCATION;
   31714                 :             : 
   31715                 :           1 :   tree name = get_identifier ("foo");
   31716                 :             : 
   31717                 :             :   /* If no templates are involved, nothing is type-dependent.  */
   31718                 :           1 :   gcc_assert (!processing_template_decl);
   31719                 :           1 :   ASSERT_FALSE (type_dependent_expression_p (name));
   31720                 :             : 
   31721                 :           1 :   ++processing_template_decl;
   31722                 :             : 
   31723                 :             :   /* Within a template, an unresolved name is always type-dependent.  */
   31724                 :           1 :   ASSERT_TRUE (type_dependent_expression_p (name));
   31725                 :             : 
   31726                 :             :   /* Ensure it copes with NULL_TREE and errors.  */
   31727                 :           1 :   ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
   31728                 :           1 :   ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
   31729                 :             : 
   31730                 :             :   /* A USING_DECL in a template should be type-dependent, even if wrapped
   31731                 :             :      with a location wrapper (PR c++/83799).  */
   31732                 :           1 :   tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
   31733                 :           1 :   TREE_TYPE (using_decl) = integer_type_node;
   31734                 :           1 :   ASSERT_TRUE (type_dependent_expression_p (using_decl));
   31735                 :           1 :   tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
   31736                 :           1 :   ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
   31737                 :           1 :   ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
   31738                 :             : 
   31739                 :           1 :   --processing_template_decl;
   31740                 :           1 : }
   31741                 :             : 
   31742                 :             : /* Run all of the selftests within this file.  */
   31743                 :             : 
   31744                 :             : void
   31745                 :           1 : cp_pt_cc_tests ()
   31746                 :             : {
   31747                 :           1 :   test_type_dependent_expression_p ();
   31748                 :           1 : }
   31749                 :             : 
   31750                 :             : } // namespace selftest
   31751                 :             : 
   31752                 :             : #endif /* #if CHECKING_P */
   31753                 :             : 
   31754                 :             : #include "gt-cp-pt.h"
        

Generated by: LCOV version 2.1-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.