pt.c revision 122180
118334Speter/* Handle parameterized types (templates) for GNU C++. 290075Sobrien Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 390075Sobrien 2001, 2002 Free Software Foundation, Inc. 418334Speter Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. 550397Sobrien Rewritten by Jason Merrill (jason@cygnus.com). 618334Speter 718334SpeterThis file is part of GNU CC. 818334Speter 918334SpeterGNU CC is free software; you can redistribute it and/or modify 1018334Speterit under the terms of the GNU General Public License as published by 1118334Speterthe Free Software Foundation; either version 2, or (at your option) 1218334Speterany later version. 1318334Speter 1418334SpeterGNU CC is distributed in the hope that it will be useful, 1518334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of 1618334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1718334SpeterGNU General Public License for more details. 1818334Speter 1918334SpeterYou should have received a copy of the GNU General Public License 2018334Speteralong with GNU CC; see the file COPYING. If not, write to 2118334Speterthe Free Software Foundation, 59 Temple Place - Suite 330, 2218334SpeterBoston, MA 02111-1307, USA. */ 2318334Speter 2418334Speter/* Known bugs or deficiencies include: 2518334Speter 2650397Sobrien all methods must be provided in header files; can't use a source 2750397Sobrien file that contains only the method templates and "just win". */ 2850397Sobrien 2918334Speter#include "config.h" 3050397Sobrien#include "system.h" 3118334Speter#include "obstack.h" 3218334Speter#include "tree.h" 3318334Speter#include "flags.h" 3418334Speter#include "cp-tree.h" 3590075Sobrien#include "tree-inline.h" 3618334Speter#include "decl.h" 3718334Speter#include "parse.h" 3818334Speter#include "lex.h" 3918334Speter#include "output.h" 4050397Sobrien#include "except.h" 4150397Sobrien#include "toplev.h" 4252284Sobrien#include "rtl.h" 4390075Sobrien#include "ggc.h" 4490075Sobrien#include "timevar.h" 4518334Speter 4650397Sobrien/* The type of functions taking a tree, and some additional data, and 4750397Sobrien returning an int. */ 4890075Sobrientypedef int (*tree_fn_t) PARAMS ((tree, void*)); 4950397Sobrien 5052284Sobrien/* The PENDING_TEMPLATES is a TREE_LIST of templates whose 5152284Sobrien instantiations have been deferred, either because their definitions 5252284Sobrien were not yet available, or because we were putting off doing the 5352284Sobrien work. The TREE_PURPOSE of each entry is a SRCLOC indicating where 54117395Skan the instantiate request occurred; the TREE_VALUE is either a DECL 5552284Sobrien (for a function or static data member), or a TYPE (for a class) 5652284Sobrien indicating what we are hoping to instantiate. */ 57117395Skanstatic GTY(()) tree pending_templates; 5890075Sobrienstatic tree last_pending_template; 5918334Speter 6050397Sobrienint processing_template_parmlist; 6150397Sobrienstatic int template_header_count; 6250397Sobrien 63117395Skanstatic GTY(()) tree saved_trees; 64117395Skanstatic GTY(()) varray_type inline_parm_levels; 6552284Sobrienstatic size_t inline_parm_levels_used; 6650397Sobrien 67117395Skanstatic GTY(()) tree current_tinst_level; 6890075Sobrien 69117395Skanstatic GTY(()) tree saved_access_scope; 70117395Skan 7190075Sobrien/* A map from local variable declarations in the body of the template 7290075Sobrien presently being instantiated to the corresponding instantiated 7390075Sobrien local variables. */ 7490075Sobrienstatic htab_t local_specializations; 7590075Sobrien 7650397Sobrien#define UNIFY_ALLOW_NONE 0 7750397Sobrien#define UNIFY_ALLOW_MORE_CV_QUAL 1 7850397Sobrien#define UNIFY_ALLOW_LESS_CV_QUAL 2 7950397Sobrien#define UNIFY_ALLOW_DERIVED 4 8052284Sobrien#define UNIFY_ALLOW_INTEGER 8 8190075Sobrien#define UNIFY_ALLOW_OUTER_LEVEL 16 8290075Sobrien#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 8390075Sobrien#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 8490075Sobrien#define UNIFY_ALLOW_MAX_CORRECTION 128 8518334Speter 8652284Sobrien#define GTB_VIA_VIRTUAL 1 /* The base class we are examining is 8752284Sobrien virtual, or a base class of a virtual 8852284Sobrien base. */ 8952284Sobrien#define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current 9052284Sobrien type with the desired type. */ 9152284Sobrien 92117395Skanstatic void push_access_scope_real PARAMS ((tree, tree, tree)); 93117395Skanstatic void push_access_scope PARAMS ((tree)); 94117395Skanstatic void pop_access_scope PARAMS ((tree)); 9590075Sobrienstatic int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree, 9696263Sobrien unification_kind_t, int)); 9790075Sobrienstatic int try_one_overload PARAMS ((tree, tree, tree, tree, tree, 98122180Skan unification_kind_t, int, bool)); 9990075Sobrienstatic int unify PARAMS ((tree, tree, tree, tree, int)); 10090075Sobrienstatic void add_pending_template PARAMS ((tree)); 10190075Sobrienstatic void reopen_tinst_level PARAMS ((tree)); 10290075Sobrienstatic tree classtype_mangled_name PARAMS ((tree)); 10396263Sobrienstatic char *mangle_class_name_for_template PARAMS ((const char *, 10496263Sobrien tree, tree)); 10590075Sobrienstatic tree tsubst_initializer_list PARAMS ((tree, tree)); 10690075Sobrienstatic int list_eq PARAMS ((tree, tree)); 10790075Sobrienstatic tree get_class_bindings PARAMS ((tree, tree, tree)); 10896263Sobrienstatic tree coerce_template_parms PARAMS ((tree, tree, tree, 10996263Sobrien tsubst_flags_t, int)); 11090075Sobrienstatic void tsubst_enum PARAMS ((tree, tree, tree)); 11190075Sobrienstatic tree add_to_template_args PARAMS ((tree, tree)); 11290075Sobrienstatic tree add_outermost_template_args PARAMS ((tree, tree)); 11390075Sobrienstatic int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*, 11496263Sobrien tree*)); 11590075Sobrienstatic int type_unification_real PARAMS ((tree, tree, tree, tree, 11696263Sobrien int, unification_kind_t, int, int)); 11790075Sobrienstatic void note_template_header PARAMS ((int)); 11890075Sobrienstatic tree maybe_fold_nontype_arg PARAMS ((tree)); 11990075Sobrienstatic tree convert_nontype_argument PARAMS ((tree, tree)); 12096263Sobrienstatic tree convert_template_argument PARAMS ((tree, tree, tree, 12196263Sobrien tsubst_flags_t, int, tree)); 12290075Sobrienstatic tree get_bindings_overload PARAMS ((tree, tree, tree)); 123117395Skanstatic int for_each_template_parm PARAMS ((tree, tree_fn_t, void*, htab_t)); 12490075Sobrienstatic tree build_template_parm_index PARAMS ((int, int, int, tree, tree)); 12590075Sobrienstatic int inline_needs_template_parms PARAMS ((tree)); 12690075Sobrienstatic void push_inline_template_parms_recursive PARAMS ((tree, int)); 12790075Sobrienstatic tree retrieve_specialization PARAMS ((tree, tree)); 12890075Sobrienstatic tree retrieve_local_specialization PARAMS ((tree)); 12990075Sobrienstatic tree register_specialization PARAMS ((tree, tree, tree)); 13090075Sobrienstatic void register_local_specialization PARAMS ((tree, tree)); 131119256Skanstatic int reregister_specialization PARAMS ((tree, tree, tree)); 13290075Sobrienstatic tree reduce_template_parm_level PARAMS ((tree, tree, int)); 13390075Sobrienstatic tree build_template_decl PARAMS ((tree, tree)); 13490075Sobrienstatic int mark_template_parm PARAMS ((tree, void *)); 135117395Skanstatic int template_parm_this_level_p PARAMS ((tree, void *)); 13690075Sobrienstatic tree tsubst_friend_function PARAMS ((tree, tree)); 13790075Sobrienstatic tree tsubst_friend_class PARAMS ((tree, tree)); 138102780Skanstatic int can_complete_type_without_circularity PARAMS ((tree)); 13990075Sobrienstatic tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int)); 14090075Sobrienstatic int template_decl_level PARAMS ((tree)); 14190075Sobrienstatic tree maybe_get_template_decl_from_type_decl PARAMS ((tree)); 14290075Sobrienstatic int check_cv_quals_for_unify PARAMS ((int, tree, tree)); 14396263Sobrienstatic tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t)); 14496263Sobrienstatic tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t)); 14590075Sobrienstatic void regenerate_decl_from_template PARAMS ((tree, tree)); 14690075Sobrienstatic tree most_specialized PARAMS ((tree, tree, tree)); 14790075Sobrienstatic tree most_specialized_class PARAMS ((tree, tree)); 14890075Sobrienstatic int template_class_depth_real PARAMS ((tree, int)); 14996263Sobrienstatic tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int)); 15096263Sobrienstatic tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t)); 15196263Sobrienstatic tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree)); 15296263Sobrienstatic tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree)); 15390075Sobrienstatic void check_specialization_scope PARAMS ((void)); 15490075Sobrienstatic tree process_partial_specialization PARAMS ((tree)); 15590075Sobrienstatic void set_current_access_from_decl PARAMS ((tree)); 15690075Sobrienstatic void check_default_tmpl_args PARAMS ((tree, tree, int, int)); 15796263Sobrienstatic tree tsubst_call_declarator_parms PARAMS ((tree, tree, 15896263Sobrien tsubst_flags_t, tree)); 15990075Sobrienstatic tree get_template_base_recursive PARAMS ((tree, tree, 16096263Sobrien tree, tree, tree, int)); 16190075Sobrienstatic tree get_template_base PARAMS ((tree, tree, tree, tree)); 16290075Sobrienstatic int verify_class_unification PARAMS ((tree, tree, tree)); 16390075Sobrienstatic tree try_class_unification PARAMS ((tree, tree, tree, tree)); 16496263Sobrienstatic int coerce_template_template_parms PARAMS ((tree, tree, tsubst_flags_t, 16596263Sobrien tree, tree)); 16690075Sobrienstatic tree determine_specialization PARAMS ((tree, tree, tree *, int)); 16790075Sobrienstatic int template_args_equal PARAMS ((tree, tree)); 16890075Sobrienstatic void tsubst_default_arguments PARAMS ((tree)); 16990075Sobrienstatic tree for_each_template_parm_r PARAMS ((tree *, int *, void *)); 17090075Sobrienstatic tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree)); 17190075Sobrienstatic void copy_default_args_to_explicit_spec PARAMS ((tree)); 17296263Sobrienstatic int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t)); 17318334Speter 174117395Skan/* Make the current scope suitable for access checking when we are 175117395Skan processing T. T can be FUNCTION_DECL for instantiated function 176117395Skan template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for 177117395Skan static member variable (need by instantiate_decl). ARGS is the 178117395Skan template argument for TEMPLATE_DECL. If CONTEXT is not NULL_TREE, 179117395Skan this is used instead of the context of T. */ 18052284Sobrien 18190075Sobrienvoid 182117395Skanpush_access_scope_real (t, args, context) 183117395Skan tree t, args, context; 18490075Sobrien{ 185117395Skan if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) 186117395Skan { 187117395Skan /* When we are processing specialization `foo<Outer>' for code like 188117395Skan 189117395Skan template <class U> typename U::Inner foo (); 190117395Skan class Outer { 191117395Skan struct Inner {}; 192117395Skan friend Outer::Inner foo<Outer> (); 193117395Skan }; 194117395Skan 195117395Skan `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of 196117395Skan its specialization. We can get the FUNCTION_DECL with the right 197117395Skan information because this specialization has already been 198117395Skan registered by the friend declaration above. */ 199117395Skan 200117395Skan if (DECL_FUNCTION_TEMPLATE_P (t) && args) 201117395Skan { 202117395Skan tree full_args = tsubst_template_arg_vector 203117395Skan (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)), args, tf_none); 204117395Skan tree spec = NULL_TREE; 205117395Skan if (full_args != error_mark_node) 206117395Skan spec = retrieve_specialization (t, full_args); 207117395Skan if (spec) 208117395Skan t = spec; 209117395Skan } 210117395Skan } 211117395Skan 212117395Skan if (!context) 213117395Skan context = DECL_CONTEXT (t); 214117395Skan if (context && TYPE_P (context)) 215117395Skan push_nested_class (context, 2); 216117395Skan else 217117395Skan push_to_top_level (); 218117395Skan 219117395Skan if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) 220117395Skan { 221117395Skan saved_access_scope = tree_cons 222117395Skan (NULL_TREE, current_function_decl, saved_access_scope); 223117395Skan current_function_decl = t; 224117395Skan } 22590075Sobrien} 22652284Sobrien 227117395Skan/* Like push_access_scope_real, but always uses DECL_CONTEXT. */ 228117395Skan 229117395Skanvoid 230117395Skanpush_access_scope (t) 231117395Skan tree t; 232117395Skan{ 233117395Skan push_access_scope_real (t, NULL_TREE, NULL_TREE); 234117395Skan} 235117395Skan 236117395Skan/* Restore the scope set up by push_access_scope. T is the node we 237117395Skan are processing. */ 238117395Skan 239117395Skanvoid 240117395Skanpop_access_scope (t) 241117395Skan tree t; 242117395Skan{ 243117395Skan if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) 244117395Skan { 245117395Skan current_function_decl = TREE_VALUE (saved_access_scope); 246117395Skan saved_access_scope = TREE_CHAIN (saved_access_scope); 247117395Skan } 248117395Skan 249117395Skan if (DECL_CLASS_SCOPE_P (t)) 250117395Skan pop_nested_class (); 251117395Skan else 252117395Skan pop_from_top_level (); 253117395Skan} 254117395Skan 25550397Sobrien/* Do any processing required when DECL (a member template declaration 25650397Sobrien using TEMPLATE_PARAMETERS as its innermost parameter list) is 25750397Sobrien finished. Returns the TEMPLATE_DECL corresponding to DECL, unless 25850397Sobrien it is a specialization, in which case the DECL itself is returned. */ 25950397Sobrien 26050397Sobrientree 26152284Sobrienfinish_member_template_decl (decl) 26250397Sobrien tree decl; 26350397Sobrien{ 26450397Sobrien if (decl == NULL_TREE || decl == void_type_node) 26550397Sobrien return NULL_TREE; 26652284Sobrien else if (decl == error_mark_node) 26752284Sobrien /* By returning NULL_TREE, the parser will just ignore this 26852284Sobrien declaration. We have already issued the error. */ 26952284Sobrien return NULL_TREE; 27050397Sobrien else if (TREE_CODE (decl) == TREE_LIST) 27150397Sobrien { 27250397Sobrien /* Assume that the class is the only declspec. */ 27350397Sobrien decl = TREE_VALUE (decl); 27450397Sobrien if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) 27550397Sobrien && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) 27650397Sobrien { 27750397Sobrien tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); 27850397Sobrien check_member_template (tmpl); 27950397Sobrien return tmpl; 28050397Sobrien } 28150397Sobrien return NULL_TREE; 28250397Sobrien } 28390075Sobrien else if (TREE_CODE (decl) == FIELD_DECL) 28490075Sobrien error ("data member `%D' cannot be a member template", decl); 28550397Sobrien else if (DECL_TEMPLATE_INFO (decl)) 28650397Sobrien { 28750397Sobrien if (!DECL_TEMPLATE_SPECIALIZATION (decl)) 28850397Sobrien { 28950397Sobrien check_member_template (DECL_TI_TEMPLATE (decl)); 29050397Sobrien return DECL_TI_TEMPLATE (decl); 29150397Sobrien } 29250397Sobrien else 29350397Sobrien return decl; 29450397Sobrien } 29550397Sobrien else 29690075Sobrien error ("invalid member template declaration `%D'", decl); 29750397Sobrien 29850397Sobrien return error_mark_node; 29950397Sobrien} 30050397Sobrien 30150397Sobrien/* Returns the template nesting level of the indicated class TYPE. 30250397Sobrien 30350397Sobrien For example, in: 30450397Sobrien template <class T> 30550397Sobrien struct A 30650397Sobrien { 30750397Sobrien template <class U> 30850397Sobrien struct B {}; 30950397Sobrien }; 31050397Sobrien 31152284Sobrien A<T>::B<U> has depth two, while A<T> has depth one. 31252284Sobrien Both A<T>::B<int> and A<int>::B<U> have depth one, if 31352284Sobrien COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not 31452284Sobrien specializations. 31550397Sobrien 31652284Sobrien This function is guaranteed to return 0 if passed NULL_TREE so 31752284Sobrien that, for example, `template_class_depth (current_class_type)' is 31852284Sobrien always safe. */ 31952284Sobrien 32052284Sobrienstatic int 32152284Sobrientemplate_class_depth_real (type, count_specializations) 32250397Sobrien tree type; 32352284Sobrien int count_specializations; 32450397Sobrien{ 32550397Sobrien int depth; 32650397Sobrien 32750397Sobrien for (depth = 0; 32852284Sobrien type && TREE_CODE (type) != NAMESPACE_DECL; 32952284Sobrien type = (TREE_CODE (type) == FUNCTION_DECL) 33090075Sobrien ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) 33152284Sobrien { 33252284Sobrien if (TREE_CODE (type) != FUNCTION_DECL) 33352284Sobrien { 33452284Sobrien if (CLASSTYPE_TEMPLATE_INFO (type) 33552284Sobrien && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) 33652284Sobrien && ((count_specializations 33752284Sobrien && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) 33852284Sobrien || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) 33952284Sobrien ++depth; 34052284Sobrien } 34152284Sobrien else 34252284Sobrien { 34352284Sobrien if (DECL_TEMPLATE_INFO (type) 34452284Sobrien && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) 34552284Sobrien && ((count_specializations 34652284Sobrien && DECL_TEMPLATE_SPECIALIZATION (type)) 34752284Sobrien || uses_template_parms (DECL_TI_ARGS (type)))) 34852284Sobrien ++depth; 34952284Sobrien } 35052284Sobrien } 35150397Sobrien 35250397Sobrien return depth; 35350397Sobrien} 35450397Sobrien 35552284Sobrien/* Returns the template nesting level of the indicated class TYPE. 35652284Sobrien Like template_class_depth_real, but instantiations do not count in 35752284Sobrien the depth. */ 35850397Sobrien 35952284Sobrienint 36052284Sobrientemplate_class_depth (type) 36152284Sobrien tree type; 36250397Sobrien{ 36352284Sobrien return template_class_depth_real (type, /*count_specializations=*/0); 36450397Sobrien} 36550397Sobrien 36650397Sobrien/* Returns 1 if processing DECL as part of do_pending_inlines 36750397Sobrien needs us to push template parms. */ 36850397Sobrien 36950397Sobrienstatic int 37050397Sobrieninline_needs_template_parms (decl) 37150397Sobrien tree decl; 37250397Sobrien{ 37350397Sobrien if (! DECL_TEMPLATE_INFO (decl)) 37450397Sobrien return 0; 37550397Sobrien 37652284Sobrien return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) 37750397Sobrien > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); 37850397Sobrien} 37950397Sobrien 38050397Sobrien/* Subroutine of maybe_begin_member_template_processing. 38150397Sobrien Push the template parms in PARMS, starting from LEVELS steps into the 38250397Sobrien chain, and ending at the beginning, since template parms are listed 38350397Sobrien innermost first. */ 38450397Sobrien 38550397Sobrienstatic void 38650397Sobrienpush_inline_template_parms_recursive (parmlist, levels) 38750397Sobrien tree parmlist; 38850397Sobrien int levels; 38950397Sobrien{ 39050397Sobrien tree parms = TREE_VALUE (parmlist); 39150397Sobrien int i; 39250397Sobrien 39350397Sobrien if (levels > 1) 39450397Sobrien push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); 39550397Sobrien 39650397Sobrien ++processing_template_decl; 39750397Sobrien current_template_parms 39890075Sobrien = tree_cons (size_int (processing_template_decl), 39950397Sobrien parms, current_template_parms); 40050397Sobrien TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; 40150397Sobrien 40250397Sobrien pushlevel (0); 40350397Sobrien for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) 40450397Sobrien { 40550397Sobrien tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 40690075Sobrien my_friendly_assert (DECL_P (parm), 0); 40750397Sobrien 40850397Sobrien switch (TREE_CODE (parm)) 40950397Sobrien { 41050397Sobrien case TYPE_DECL: 41150397Sobrien case TEMPLATE_DECL: 41250397Sobrien pushdecl (parm); 41350397Sobrien break; 41450397Sobrien 41550397Sobrien case PARM_DECL: 41650397Sobrien { 41752284Sobrien /* Make a CONST_DECL as is done in process_template_parm. 41852284Sobrien It is ugly that we recreate this here; the original 41952284Sobrien version built in process_template_parm is no longer 42052284Sobrien available. */ 42150397Sobrien tree decl = build_decl (CONST_DECL, DECL_NAME (parm), 42250397Sobrien TREE_TYPE (parm)); 42390075Sobrien DECL_ARTIFICIAL (decl) = 1; 42450397Sobrien DECL_INITIAL (decl) = DECL_INITIAL (parm); 42590075Sobrien SET_DECL_TEMPLATE_PARM_P (decl); 42650397Sobrien pushdecl (decl); 42750397Sobrien } 42850397Sobrien break; 42950397Sobrien 43050397Sobrien default: 43190075Sobrien abort (); 43250397Sobrien } 43350397Sobrien } 43450397Sobrien} 43550397Sobrien 43650397Sobrien/* Restore the template parameter context for a member template or 43750397Sobrien a friend template defined in a class definition. */ 43850397Sobrien 43918334Spetervoid 44050397Sobrienmaybe_begin_member_template_processing (decl) 44150397Sobrien tree decl; 44250397Sobrien{ 44350397Sobrien tree parms; 44452284Sobrien int levels = 0; 44550397Sobrien 44652284Sobrien if (inline_needs_template_parms (decl)) 44752284Sobrien { 44852284Sobrien parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); 44952284Sobrien levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; 45050397Sobrien 45152284Sobrien if (DECL_TEMPLATE_SPECIALIZATION (decl)) 45252284Sobrien { 45352284Sobrien --levels; 45452284Sobrien parms = TREE_CHAIN (parms); 45552284Sobrien } 45650397Sobrien 45752284Sobrien push_inline_template_parms_recursive (parms, levels); 45850397Sobrien } 45950397Sobrien 46052284Sobrien /* Remember how many levels of template parameters we pushed so that 46152284Sobrien we can pop them later. */ 46252284Sobrien if (!inline_parm_levels) 46352284Sobrien VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); 46452284Sobrien if (inline_parm_levels_used == inline_parm_levels->num_elements) 46552284Sobrien VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); 46652284Sobrien VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; 46752284Sobrien ++inline_parm_levels_used; 46850397Sobrien} 46950397Sobrien 470117395Skan/* Undo the effects of begin_member_template_processing. */ 47150397Sobrien 47250397Sobrienvoid 47352284Sobrienmaybe_end_member_template_processing () 47450397Sobrien{ 47552284Sobrien int i; 47652284Sobrien 47752284Sobrien if (!inline_parm_levels_used) 47850397Sobrien return; 47950397Sobrien 48052284Sobrien --inline_parm_levels_used; 48152284Sobrien for (i = 0; 48252284Sobrien i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); 48352284Sobrien ++i) 48450397Sobrien { 48550397Sobrien --processing_template_decl; 48650397Sobrien current_template_parms = TREE_CHAIN (current_template_parms); 48750397Sobrien poplevel (0, 0, 0); 48850397Sobrien } 48950397Sobrien} 49050397Sobrien 491117395Skan/* Returns nonzero iff T is a member template function. We must be 49250397Sobrien careful as in 49350397Sobrien 49450397Sobrien template <class T> class C { void f(); } 49550397Sobrien 49650397Sobrien Here, f is a template function, and a member, but not a member 49750397Sobrien template. This function does not concern itself with the origin of 49850397Sobrien T, only its present state. So if we have 49950397Sobrien 50050397Sobrien template <class T> class C { template <class U> void f(U); } 50150397Sobrien 50250397Sobrien then neither C<int>::f<char> nor C<T>::f<double> is considered 50352284Sobrien to be a member template. But, `template <class U> void 50452284Sobrien C<int>::f(U)' is considered a member template. */ 50550397Sobrien 50650397Sobrienint 50750397Sobrienis_member_template (t) 50850397Sobrien tree t; 50950397Sobrien{ 51052284Sobrien if (!DECL_FUNCTION_TEMPLATE_P (t)) 51150397Sobrien /* Anything that isn't a function or a template function is 51250397Sobrien certainly not a member template. */ 51350397Sobrien return 0; 51450397Sobrien 51550397Sobrien /* A local class can't have member templates. */ 51690075Sobrien if (decl_function_context (t)) 51750397Sobrien return 0; 51850397Sobrien 51952284Sobrien return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) 52050397Sobrien /* If there are more levels of template parameters than 52150397Sobrien there are template classes surrounding the declaration, 52250397Sobrien then we have a member template. */ 52352284Sobrien && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 52490075Sobrien template_class_depth (DECL_CONTEXT (t)))); 52550397Sobrien} 52650397Sobrien 52752284Sobrien#if 0 /* UNUSED */ 528117395Skan/* Returns nonzero iff T is a member template class. See 52950397Sobrien is_member_template for a description of what precisely constitutes 53050397Sobrien a member template. */ 53150397Sobrien 53250397Sobrienint 53350397Sobrienis_member_template_class (t) 53450397Sobrien tree t; 53550397Sobrien{ 53650397Sobrien if (!DECL_CLASS_TEMPLATE_P (t)) 53750397Sobrien /* Anything that isn't a class template, is certainly not a member 53850397Sobrien template. */ 53950397Sobrien return 0; 54050397Sobrien 54150397Sobrien if (!DECL_CLASS_SCOPE_P (t)) 54250397Sobrien /* Anything whose context isn't a class type is surely not a 54350397Sobrien member template. */ 54450397Sobrien return 0; 54550397Sobrien 54650397Sobrien /* If there are more levels of template parameters than there are 54750397Sobrien template classes surrounding the declaration, then we have a 54850397Sobrien member template. */ 54952284Sobrien return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 55050397Sobrien template_class_depth (DECL_CONTEXT (t))); 55150397Sobrien} 55252284Sobrien#endif 55350397Sobrien 55452284Sobrien/* Return a new template argument vector which contains all of ARGS, 55590075Sobrien but has as its innermost set of arguments the EXTRA_ARGS. */ 55650397Sobrien 55750397Sobrienstatic tree 55852284Sobrienadd_to_template_args (args, extra_args) 55952284Sobrien tree args; 56052284Sobrien tree extra_args; 56150397Sobrien{ 56252284Sobrien tree new_args; 56352284Sobrien int extra_depth; 56452284Sobrien int i; 56552284Sobrien int j; 56650397Sobrien 56752284Sobrien extra_depth = TMPL_ARGS_DEPTH (extra_args); 56890075Sobrien new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); 56950397Sobrien 57052284Sobrien for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) 57152284Sobrien SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); 57250397Sobrien 57352284Sobrien for (j = 1; j <= extra_depth; ++j, ++i) 57452284Sobrien SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); 57552284Sobrien 57650397Sobrien return new_args; 57750397Sobrien} 57850397Sobrien 57952284Sobrien/* Like add_to_template_args, but only the outermost ARGS are added to 58052284Sobrien the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH 58152284Sobrien (EXTRA_ARGS) levels are added. This function is used to combine 58252284Sobrien the template arguments from a partial instantiation with the 58352284Sobrien template arguments used to attain the full instantiation from the 58452284Sobrien partial instantiation. */ 58550397Sobrien 58650397Sobrienstatic tree 58752284Sobrienadd_outermost_template_args (args, extra_args) 58850397Sobrien tree args; 58950397Sobrien tree extra_args; 59050397Sobrien{ 59150397Sobrien tree new_args; 59250397Sobrien 59352284Sobrien /* If there are more levels of EXTRA_ARGS than there are ARGS, 59452284Sobrien something very fishy is going on. */ 59552284Sobrien my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), 59652284Sobrien 0); 59750397Sobrien 59852284Sobrien /* If *all* the new arguments will be the EXTRA_ARGS, just return 59952284Sobrien them. */ 60052284Sobrien if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) 60152284Sobrien return extra_args; 60250397Sobrien 60352284Sobrien /* For the moment, we make ARGS look like it contains fewer levels. */ 60452284Sobrien TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); 60552284Sobrien 60652284Sobrien new_args = add_to_template_args (args, extra_args); 60750397Sobrien 60852284Sobrien /* Now, we restore ARGS to its full dimensions. */ 60952284Sobrien TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); 61052284Sobrien 61150397Sobrien return new_args; 61250397Sobrien} 61350397Sobrien 61490075Sobrien/* Return the N levels of innermost template arguments from the ARGS. */ 61590075Sobrien 61690075Sobrientree 61790075Sobrienget_innermost_template_args (args, n) 61890075Sobrien tree args; 61990075Sobrien int n; 62090075Sobrien{ 62190075Sobrien tree new_args; 62290075Sobrien int extra_levels; 62390075Sobrien int i; 62490075Sobrien 62590075Sobrien my_friendly_assert (n >= 0, 20000603); 62690075Sobrien 62790075Sobrien /* If N is 1, just return the innermost set of template arguments. */ 62890075Sobrien if (n == 1) 62990075Sobrien return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); 63090075Sobrien 63190075Sobrien /* If we're not removing anything, just return the arguments we were 63290075Sobrien given. */ 63390075Sobrien extra_levels = TMPL_ARGS_DEPTH (args) - n; 63490075Sobrien my_friendly_assert (extra_levels >= 0, 20000603); 63590075Sobrien if (extra_levels == 0) 63690075Sobrien return args; 63790075Sobrien 63890075Sobrien /* Make a new set of arguments, not containing the outer arguments. */ 63990075Sobrien new_args = make_tree_vec (n); 64090075Sobrien for (i = 1; i <= n; ++i) 64190075Sobrien SET_TMPL_ARGS_LEVEL (new_args, i, 64290075Sobrien TMPL_ARGS_LEVEL (args, i + extra_levels)); 64390075Sobrien 64490075Sobrien return new_args; 64590075Sobrien} 64690075Sobrien 64750397Sobrien/* We've got a template header coming up; push to a new level for storing 64850397Sobrien the parms. */ 64950397Sobrien 65050397Sobrienvoid 65118334Speterbegin_template_parm_list () 65218334Speter{ 65350397Sobrien /* We use a non-tag-transparent scope here, which causes pushtag to 65450397Sobrien put tags in this scope, rather than in the enclosing class or 65550397Sobrien namespace scope. This is the right thing, since we want 65650397Sobrien TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a 65750397Sobrien global template class, push_template_decl handles putting the 65850397Sobrien TEMPLATE_DECL into top-level scope. For a nested template class, 65950397Sobrien e.g.: 66050397Sobrien 66150397Sobrien template <class T> struct S1 { 66250397Sobrien template <class T> struct S2 {}; 66350397Sobrien }; 66450397Sobrien 66550397Sobrien pushtag contains special code to call pushdecl_with_scope on the 66650397Sobrien TEMPLATE_DECL for S2. */ 66790075Sobrien begin_scope (sk_template_parms); 66850397Sobrien ++processing_template_decl; 66950397Sobrien ++processing_template_parmlist; 67050397Sobrien note_template_header (0); 67118334Speter} 67218334Speter 67352284Sobrien/* This routine is called when a specialization is declared. If it is 674117395Skan invalid to declare a specialization here, an error is reported. */ 67552284Sobrien 67652284Sobrienstatic void 67752284Sobriencheck_specialization_scope () 67852284Sobrien{ 67952284Sobrien tree scope = current_scope (); 68052284Sobrien 68152284Sobrien /* [temp.expl.spec] 68252284Sobrien 68352284Sobrien An explicit specialization shall be declared in the namespace of 68452284Sobrien which the template is a member, or, for member templates, in the 68552284Sobrien namespace of which the enclosing class or enclosing class 68652284Sobrien template is a member. An explicit specialization of a member 68752284Sobrien function, member class or static data member of a class template 68852284Sobrien shall be declared in the namespace of which the class template 68952284Sobrien is a member. */ 69052284Sobrien if (scope && TREE_CODE (scope) != NAMESPACE_DECL) 69190075Sobrien error ("explicit specialization in non-namespace scope `%D'", 69252284Sobrien scope); 69352284Sobrien 69452284Sobrien /* [temp.expl.spec] 69552284Sobrien 69652284Sobrien In an explicit specialization declaration for a member of a class 69752284Sobrien template or a member template that appears in namespace scope, 69852284Sobrien the member template and some of its enclosing class templates may 69952284Sobrien remain unspecialized, except that the declaration shall not 70052284Sobrien explicitly specialize a class member template if its enclosing 70152284Sobrien class templates are not explicitly specialized as well. */ 70252284Sobrien if (current_template_parms) 70390075Sobrien error ("enclosing class templates are not explicitly specialized"); 70452284Sobrien} 70552284Sobrien 706117395Skan/* We've just seen template <>. */ 70750397Sobrien 70850397Sobrienvoid 70950397Sobrienbegin_specialization () 71050397Sobrien{ 71190075Sobrien begin_scope (sk_template_spec); 71250397Sobrien note_template_header (1); 71352284Sobrien check_specialization_scope (); 71450397Sobrien} 71550397Sobrien 71690075Sobrien/* Called at then end of processing a declaration preceded by 71750397Sobrien template<>. */ 71850397Sobrien 71950397Sobrienvoid 72050397Sobrienend_specialization () 72150397Sobrien{ 72290075Sobrien finish_scope (); 72350397Sobrien reset_specialization (); 72450397Sobrien} 72550397Sobrien 72650397Sobrien/* Any template <>'s that we have seen thus far are not referring to a 727117395Skan function specialization. */ 72850397Sobrien 72950397Sobrienvoid 73050397Sobrienreset_specialization () 73150397Sobrien{ 73250397Sobrien processing_specialization = 0; 73350397Sobrien template_header_count = 0; 73450397Sobrien} 73550397Sobrien 736117395Skan/* We've just seen a template header. If SPECIALIZATION is nonzero, 73750397Sobrien it was of the form template <>. */ 73850397Sobrien 73950397Sobrienstatic void 74050397Sobriennote_template_header (specialization) 74150397Sobrien int specialization; 74250397Sobrien{ 74350397Sobrien processing_specialization = specialization; 74450397Sobrien template_header_count++; 74550397Sobrien} 74650397Sobrien 74750397Sobrien/* We're beginning an explicit instantiation. */ 74850397Sobrien 74950397Sobrienvoid 75050397Sobrienbegin_explicit_instantiation () 75150397Sobrien{ 75250397Sobrien ++processing_explicit_instantiation; 75350397Sobrien} 75450397Sobrien 75550397Sobrien 75650397Sobrienvoid 75750397Sobrienend_explicit_instantiation () 75850397Sobrien{ 75950397Sobrien my_friendly_assert(processing_explicit_instantiation > 0, 0); 76050397Sobrien --processing_explicit_instantiation; 76150397Sobrien} 76250397Sobrien 76352284Sobrien/* The TYPE is being declared. If it is a template type, that means it 76452284Sobrien is a partial specialization. Do appropriate error-checking. */ 76552284Sobrien 76652284Sobrienvoid 76752284Sobrienmaybe_process_partial_specialization (type) 76852284Sobrien tree type; 76952284Sobrien{ 770117395Skan /* TYPE maybe an ERROR_MARK_NODE. */ 771117395Skan tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE; 772117395Skan 773117395Skan if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) 77452284Sobrien { 775117395Skan /* This is for ordinary explicit specialization and partial 776117395Skan specialization of a template class such as: 777117395Skan 778117395Skan template <> class C<int>; 779117395Skan 780117395Skan or: 781117395Skan 782117395Skan template <class T> class C<T*>; 783117395Skan 784117395Skan Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ 785117395Skan 78652284Sobrien if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) 78790075Sobrien && !COMPLETE_TYPE_P (type)) 78852284Sobrien { 78952284Sobrien if (current_namespace 79052284Sobrien != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) 79152284Sobrien { 79290075Sobrien pedwarn ("specializing `%#T' in different namespace", type); 79352284Sobrien cp_pedwarn_at (" from definition of `%#D'", 79452284Sobrien CLASSTYPE_TI_TEMPLATE (type)); 79552284Sobrien } 79652284Sobrien SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); 79752284Sobrien if (processing_template_decl) 79852284Sobrien push_template_decl (TYPE_MAIN_DECL (type)); 79952284Sobrien } 80052284Sobrien else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) 80190075Sobrien error ("specialization of `%T' after instantiation", type); 80252284Sobrien } 803117395Skan else if (CLASS_TYPE_P (type) 804117395Skan && !CLASSTYPE_USE_TEMPLATE (type) 805117395Skan && CLASSTYPE_TEMPLATE_INFO (type) 806117395Skan && context && CLASS_TYPE_P (context) 807117395Skan && CLASSTYPE_TEMPLATE_INFO (context)) 808117395Skan { 809117395Skan /* This is for an explicit specialization of member class 810117395Skan template according to [temp.expl.spec/18]: 811117395Skan 812117395Skan template <> template <class U> class C<int>::D; 813117395Skan 814117395Skan The context `C<int>' must be an implicit instantiation. 815117395Skan Otherwise this is just a member class template declared 816117395Skan earlier like: 817117395Skan 818117395Skan template <> class C<int> { template <class U> class D; }; 819117395Skan template <> template <class U> class C<int>::D; 820117395Skan 821117395Skan In the first case, `C<int>::D' is a specialization of `C<T>::D' 822117395Skan while in the second case, `C<int>::D' is a primary template 823117395Skan and `C<T>::D' may not exist. */ 824117395Skan 825117395Skan if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) 826117395Skan && !COMPLETE_TYPE_P (type)) 827117395Skan { 828117395Skan tree t; 829117395Skan 830117395Skan if (current_namespace 831117395Skan != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) 832117395Skan { 833117395Skan pedwarn ("specializing `%#T' in different namespace", type); 834117395Skan cp_pedwarn_at (" from definition of `%#D'", 835117395Skan CLASSTYPE_TI_TEMPLATE (type)); 836117395Skan } 837117395Skan 838117395Skan /* Check for invalid specialization after instantiation: 839117395Skan 840117395Skan template <> template <> class C<int>::D<int>; 841117395Skan template <> template <class U> class C<int>::D; */ 842117395Skan 843117395Skan for (t = DECL_TEMPLATE_INSTANTIATIONS 844117395Skan (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); 845117395Skan t; t = TREE_CHAIN (t)) 846117395Skan if (TREE_VALUE (t) != type 847117395Skan && TYPE_CONTEXT (TREE_VALUE (t)) == context) 848117395Skan error ("specialization `%T' after instantiation `%T'", 849117395Skan type, TREE_VALUE (t)); 850117395Skan 851117395Skan /* Mark TYPE as a specialization. And as a result, we only 852117395Skan have one level of template argument for the innermost 853117395Skan class template. */ 854117395Skan SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); 855117395Skan CLASSTYPE_TI_ARGS (type) 856117395Skan = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); 857117395Skan } 858117395Skan } 85952284Sobrien else if (processing_specialization) 86090075Sobrien error ("explicit specialization of non-template `%T'", type); 86152284Sobrien} 86252284Sobrien 86350397Sobrien/* Retrieve the specialization (in the sense of [temp.spec] - a 86450397Sobrien specialization is either an instantiation or an explicit 86550397Sobrien specialization) of TMPL for the given template ARGS. If there is 86650397Sobrien no such specialization, return NULL_TREE. The ARGS are a vector of 86750397Sobrien arguments, or a vector of vectors of arguments, in the case of 86850397Sobrien templates with more than one level of parameters. */ 86950397Sobrien 87050397Sobrienstatic tree 87150397Sobrienretrieve_specialization (tmpl, args) 87250397Sobrien tree tmpl; 87350397Sobrien tree args; 87450397Sobrien{ 87550397Sobrien tree s; 87650397Sobrien 87750397Sobrien my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); 87850397Sobrien 87952284Sobrien /* There should be as many levels of arguments as there are 88052284Sobrien levels of parameters. */ 88152284Sobrien my_friendly_assert (TMPL_ARGS_DEPTH (args) 88252284Sobrien == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), 88352284Sobrien 0); 88452284Sobrien 88550397Sobrien for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 88650397Sobrien s != NULL_TREE; 88750397Sobrien s = TREE_CHAIN (s)) 88850397Sobrien if (comp_template_args (TREE_PURPOSE (s), args)) 88950397Sobrien return TREE_VALUE (s); 89050397Sobrien 89150397Sobrien return NULL_TREE; 89250397Sobrien} 89350397Sobrien 89490075Sobrien/* Like retrieve_specialization, but for local declarations. */ 89590075Sobrien 89690075Sobrienstatic tree 89790075Sobrienretrieve_local_specialization (tmpl) 89890075Sobrien tree tmpl; 89990075Sobrien{ 90090075Sobrien return (tree) htab_find (local_specializations, tmpl); 90190075Sobrien} 90290075Sobrien 903117395Skan/* Returns nonzero iff DECL is a specialization of TMPL. */ 90450397Sobrien 90550397Sobrienint 90650397Sobrienis_specialization_of (decl, tmpl) 90750397Sobrien tree decl; 90850397Sobrien tree tmpl; 90950397Sobrien{ 91050397Sobrien tree t; 91150397Sobrien 91250397Sobrien if (TREE_CODE (decl) == FUNCTION_DECL) 91350397Sobrien { 91450397Sobrien for (t = decl; 91550397Sobrien t != NULL_TREE; 91650397Sobrien t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) 91750397Sobrien if (t == tmpl) 91850397Sobrien return 1; 91950397Sobrien } 92050397Sobrien else 92150397Sobrien { 92250397Sobrien my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); 92350397Sobrien 92450397Sobrien for (t = TREE_TYPE (decl); 92550397Sobrien t != NULL_TREE; 92650397Sobrien t = CLASSTYPE_USE_TEMPLATE (t) 92750397Sobrien ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) 92890075Sobrien if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) 92950397Sobrien return 1; 93050397Sobrien } 93150397Sobrien 93250397Sobrien return 0; 93350397Sobrien} 93450397Sobrien 93550397Sobrien/* Register the specialization SPEC as a specialization of TMPL with 93652284Sobrien the indicated ARGS. Returns SPEC, or an equivalent prior 93752284Sobrien declaration, if available. */ 93850397Sobrien 93952284Sobrienstatic tree 94050397Sobrienregister_specialization (spec, tmpl, args) 94150397Sobrien tree spec; 94250397Sobrien tree tmpl; 94350397Sobrien tree args; 94450397Sobrien{ 94550397Sobrien tree s; 94650397Sobrien 94750397Sobrien my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); 94850397Sobrien 94952284Sobrien if (TREE_CODE (spec) == FUNCTION_DECL 95052284Sobrien && uses_template_parms (DECL_TI_ARGS (spec))) 95152284Sobrien /* This is the FUNCTION_DECL for a partial instantiation. Don't 95252284Sobrien register it; we want the corresponding TEMPLATE_DECL instead. 95352284Sobrien We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than 95452284Sobrien the more obvious `uses_template_parms (spec)' to avoid problems 95552284Sobrien with default function arguments. In particular, given 95652284Sobrien something like this: 95752284Sobrien 95852284Sobrien template <class T> void f(T t1, T t = T()) 95952284Sobrien 96052284Sobrien the default argument expression is not substituted for in an 96152284Sobrien instantiation unless and until it is actually needed. */ 96252284Sobrien return spec; 96350397Sobrien 96452284Sobrien /* There should be as many levels of arguments as there are 96552284Sobrien levels of parameters. */ 96652284Sobrien my_friendly_assert (TMPL_ARGS_DEPTH (args) 96752284Sobrien == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), 96852284Sobrien 0); 96952284Sobrien 97050397Sobrien for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 97150397Sobrien s != NULL_TREE; 97250397Sobrien s = TREE_CHAIN (s)) 97390075Sobrien { 97490075Sobrien tree fn = TREE_VALUE (s); 97550397Sobrien 97690075Sobrien /* We can sometimes try to re-register a specialization that we've 97790075Sobrien already got. In particular, regenerate_decl_from_template 97890075Sobrien calls duplicate_decls which will update the specialization 97990075Sobrien list. But, we'll still get called again here anyhow. It's 98090075Sobrien more convenient to simply allow this than to try to prevent it. */ 98190075Sobrien if (fn == spec) 98290075Sobrien return spec; 98390075Sobrien else if (comp_template_args (TREE_PURPOSE (s), args)) 98490075Sobrien { 98590075Sobrien if (DECL_TEMPLATE_SPECIALIZATION (spec)) 98690075Sobrien { 98790075Sobrien if (DECL_TEMPLATE_INSTANTIATION (fn)) 98890075Sobrien { 98990075Sobrien if (TREE_USED (fn) 99090075Sobrien || DECL_EXPLICIT_INSTANTIATION (fn)) 99190075Sobrien { 99290075Sobrien error ("specialization of %D after instantiation", 99390075Sobrien fn); 99490075Sobrien return spec; 99590075Sobrien } 99690075Sobrien else 99790075Sobrien { 99890075Sobrien /* This situation should occur only if the first 99990075Sobrien specialization is an implicit instantiation, 100090075Sobrien the second is an explicit specialization, and 100190075Sobrien the implicit instantiation has not yet been 100290075Sobrien used. That situation can occur if we have 100390075Sobrien implicitly instantiated a member function and 100490075Sobrien then specialized it later. 100552284Sobrien 100690075Sobrien We can also wind up here if a friend 100790075Sobrien declaration that looked like an instantiation 100890075Sobrien turns out to be a specialization: 100952284Sobrien 101090075Sobrien template <class T> void foo(T); 101190075Sobrien class S { friend void foo<>(int) }; 101290075Sobrien template <> void foo(int); 101352284Sobrien 101490075Sobrien We transform the existing DECL in place so that 101590075Sobrien any pointers to it become pointers to the 101690075Sobrien updated declaration. 101752284Sobrien 101890075Sobrien If there was a definition for the template, but 101990075Sobrien not for the specialization, we want this to 102090075Sobrien look as if there is no definition, and vice 102190075Sobrien versa. */ 102290075Sobrien DECL_INITIAL (fn) = NULL_TREE; 102390075Sobrien duplicate_decls (spec, fn); 102452284Sobrien 102590075Sobrien return fn; 102690075Sobrien } 102790075Sobrien } 102890075Sobrien else if (DECL_TEMPLATE_SPECIALIZATION (fn)) 102990075Sobrien { 103090075Sobrien duplicate_decls (spec, fn); 103190075Sobrien return fn; 103290075Sobrien } 103390075Sobrien } 103490075Sobrien } 103550397Sobrien } 103650397Sobrien 103750397Sobrien DECL_TEMPLATE_SPECIALIZATIONS (tmpl) 103890075Sobrien = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); 103952284Sobrien 104052284Sobrien return spec; 104150397Sobrien} 104250397Sobrien 104352284Sobrien/* Unregister the specialization SPEC as a specialization of TMPL. 1044119256Skan Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true 1045119256Skan if the SPEC was listed as a specialization of TMPL. */ 104652284Sobrien 104752284Sobrienstatic int 1048119256Skanreregister_specialization (tree spec, tree tmpl, tree new_spec) 104952284Sobrien{ 105052284Sobrien tree* s; 105152284Sobrien 105252284Sobrien for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); 105352284Sobrien *s != NULL_TREE; 105452284Sobrien s = &TREE_CHAIN (*s)) 105552284Sobrien if (TREE_VALUE (*s) == spec) 105652284Sobrien { 1057119256Skan if (!new_spec) 1058119256Skan *s = TREE_CHAIN (*s); 1059119256Skan else 1060119256Skan TREE_VALUE (*s) = new_spec; 106152284Sobrien return 1; 106252284Sobrien } 106352284Sobrien 106452284Sobrien return 0; 106552284Sobrien} 106652284Sobrien 106790075Sobrien/* Like register_specialization, but for local declarations. We are 106890075Sobrien registering SPEC, an instantiation of TMPL. */ 106990075Sobrien 107090075Sobrienstatic void 107190075Sobrienregister_local_specialization (spec, tmpl) 107290075Sobrien tree spec; 107390075Sobrien tree tmpl; 107490075Sobrien{ 107590075Sobrien void **slot; 107690075Sobrien 107790075Sobrien slot = htab_find_slot (local_specializations, tmpl, INSERT); 107890075Sobrien *slot = spec; 107990075Sobrien} 108090075Sobrien 108150397Sobrien/* Print the list of candidate FNS in an error message. */ 108250397Sobrien 108352284Sobrienvoid 108450397Sobrienprint_candidates (fns) 108550397Sobrien tree fns; 108650397Sobrien{ 108750397Sobrien tree fn; 108850397Sobrien 108952284Sobrien const char *str = "candidates are:"; 109050397Sobrien 109150397Sobrien for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) 109250397Sobrien { 109352284Sobrien tree f; 109452284Sobrien 109552284Sobrien for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) 109652284Sobrien cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); 109750397Sobrien str = " "; 109850397Sobrien } 109950397Sobrien} 110050397Sobrien 110150397Sobrien/* Returns the template (one of the functions given by TEMPLATE_ID) 110250397Sobrien which can be specialized to match the indicated DECL with the 110352284Sobrien explicit template args given in TEMPLATE_ID. The DECL may be 110452284Sobrien NULL_TREE if none is available. In that case, the functions in 110552284Sobrien TEMPLATE_ID are non-members. 110650397Sobrien 1107117395Skan If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a 110852284Sobrien specialization of a member template. 110952284Sobrien 111052284Sobrien The template args (those explicitly specified and those deduced) 111152284Sobrien are output in a newly created vector *TARGS_OUT. 111252284Sobrien 111352284Sobrien If it is impossible to determine the result, an error message is 111452284Sobrien issued. The error_mark_node is returned to indicate failure. */ 111552284Sobrien 111652284Sobrienstatic tree 111750397Sobriendetermine_specialization (template_id, decl, targs_out, 111852284Sobrien need_member_template) 111950397Sobrien tree template_id; 112050397Sobrien tree decl; 112150397Sobrien tree* targs_out; 112250397Sobrien int need_member_template; 112350397Sobrien{ 112452284Sobrien tree fns; 112552284Sobrien tree targs; 112652284Sobrien tree explicit_targs; 112752284Sobrien tree candidates = NULL_TREE; 112850397Sobrien tree templates = NULL_TREE; 112950397Sobrien 113050397Sobrien *targs_out = NULL_TREE; 113150397Sobrien 113250397Sobrien if (template_id == error_mark_node) 113350397Sobrien return error_mark_node; 113450397Sobrien 113550397Sobrien fns = TREE_OPERAND (template_id, 0); 113652284Sobrien explicit_targs = TREE_OPERAND (template_id, 1); 113750397Sobrien 113850397Sobrien if (fns == error_mark_node) 113950397Sobrien return error_mark_node; 114050397Sobrien 1141117395Skan /* Check for baselinks. */ 114290075Sobrien if (BASELINK_P (fns)) 1143117395Skan fns = BASELINK_FUNCTIONS (fns); 114450397Sobrien 114590075Sobrien if (!is_overloaded_fn (fns)) 114690075Sobrien { 114790075Sobrien error ("`%D' is not a function template", fns); 114890075Sobrien return error_mark_node; 114990075Sobrien } 115090075Sobrien 115150397Sobrien for (; fns; fns = OVL_NEXT (fns)) 115250397Sobrien { 115390075Sobrien tree fn = OVL_CURRENT (fns); 115452284Sobrien 115552284Sobrien if (TREE_CODE (fn) == TEMPLATE_DECL) 1156117395Skan { 1157117395Skan tree decl_arg_types; 1158117395Skan 1159117395Skan /* DECL might be a specialization of FN. */ 1160117395Skan 1161117395Skan /* Adjust the type of DECL in case FN is a static member. */ 1162117395Skan decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); 1163117395Skan if (DECL_STATIC_FUNCTION_P (fn) 1164117395Skan && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 1165117395Skan decl_arg_types = TREE_CHAIN (decl_arg_types); 1166117395Skan 1167117395Skan /* Check that the number of function parameters matches. 1168117395Skan For example, 1169117395Skan template <class T> void f(int i = 0); 1170117395Skan template <> void f<int>(); 1171117395Skan The specialization f<int> is invalid but is not caught 1172117395Skan by get_bindings below. */ 1173117395Skan 1174117395Skan if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn))) 1175117395Skan != list_length (decl_arg_types)) 1176117395Skan continue; 1177117395Skan 1178117395Skan /* See whether this function might be a specialization of this 1179117395Skan template. */ 1180117395Skan targs = get_bindings (fn, decl, explicit_targs); 1181117395Skan 1182117395Skan if (!targs) 1183117395Skan /* We cannot deduce template arguments that when used to 1184117395Skan specialize TMPL will produce DECL. */ 1185117395Skan continue; 1186117395Skan 1187117395Skan /* Save this template, and the arguments deduced. */ 1188117395Skan templates = tree_cons (targs, fn, templates); 1189117395Skan } 119052284Sobrien else if (need_member_template) 119152284Sobrien /* FN is an ordinary member function, and we need a 119252284Sobrien specialization of a member template. */ 1193117395Skan ; 119452284Sobrien else if (TREE_CODE (fn) != FUNCTION_DECL) 119552284Sobrien /* We can get IDENTIFIER_NODEs here in certain erroneous 119652284Sobrien cases. */ 1197117395Skan ; 119852284Sobrien else if (!DECL_FUNCTION_MEMBER_P (fn)) 119952284Sobrien /* This is just an ordinary non-member function. Nothing can 120052284Sobrien be a specialization of that. */ 1201117395Skan ; 120290075Sobrien else if (DECL_ARTIFICIAL (fn)) 120390075Sobrien /* Cannot specialize functions that are created implicitly. */ 1204117395Skan ; 120550397Sobrien else 120652284Sobrien { 120752284Sobrien tree decl_arg_types; 120850397Sobrien 120952284Sobrien /* This is an ordinary member function. However, since 121052284Sobrien we're here, we can assume it's enclosing class is a 121152284Sobrien template class. For example, 121252284Sobrien 121352284Sobrien template <typename T> struct S { void f(); }; 121452284Sobrien template <> void S<int>::f() {} 121550397Sobrien 121652284Sobrien Here, S<int>::f is a non-template, but S<int> is a 121752284Sobrien template class. If FN has the same type as DECL, we 121852284Sobrien might be in business. */ 121990075Sobrien 122090075Sobrien if (!DECL_TEMPLATE_INFO (fn)) 122190075Sobrien /* Its enclosing class is an explicit specialization 122290075Sobrien of a template class. This is not a candidate. */ 122390075Sobrien continue; 122490075Sobrien 122552284Sobrien if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 122652284Sobrien TREE_TYPE (TREE_TYPE (fn)))) 122752284Sobrien /* The return types differ. */ 122852284Sobrien continue; 122950397Sobrien 123052284Sobrien /* Adjust the type of DECL in case FN is a static member. */ 123152284Sobrien decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); 123252284Sobrien if (DECL_STATIC_FUNCTION_P (fn) 123352284Sobrien && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 123452284Sobrien decl_arg_types = TREE_CHAIN (decl_arg_types); 123552284Sobrien 123652284Sobrien if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), 123752284Sobrien decl_arg_types)) 123852284Sobrien /* They match! */ 123952284Sobrien candidates = tree_cons (NULL_TREE, fn, candidates); 124050397Sobrien } 124150397Sobrien } 124252284Sobrien 124352284Sobrien if (templates && TREE_CHAIN (templates)) 124450397Sobrien { 124552284Sobrien /* We have: 124652284Sobrien 124752284Sobrien [temp.expl.spec] 124850397Sobrien 124952284Sobrien It is possible for a specialization with a given function 125052284Sobrien signature to be instantiated from more than one function 125152284Sobrien template. In such cases, explicit specification of the 125252284Sobrien template arguments must be used to uniquely identify the 125352284Sobrien function template specialization being specialized. 125450397Sobrien 125552284Sobrien Note that here, there's no suggestion that we're supposed to 125652284Sobrien determine which of the candidate templates is most 125752284Sobrien specialized. However, we, also have: 125852284Sobrien 125952284Sobrien [temp.func.order] 126052284Sobrien 126152284Sobrien Partial ordering of overloaded function template 126252284Sobrien declarations is used in the following contexts to select 126352284Sobrien the function template to which a function template 126452284Sobrien specialization refers: 126552284Sobrien 126652284Sobrien -- when an explicit specialization refers to a function 126752284Sobrien template. 126852284Sobrien 126952284Sobrien So, we do use the partial ordering rules, at least for now. 1270117395Skan This extension can only serve to make invalid programs valid, 127152284Sobrien so it's safe. And, there is strong anecdotal evidence that 127252284Sobrien the committee intended the partial ordering rules to apply; 127352284Sobrien the EDG front-end has that behavior, and John Spicer claims 127452284Sobrien that the committee simply forgot to delete the wording in 127552284Sobrien [temp.expl.spec]. */ 127652284Sobrien tree tmpl = most_specialized (templates, decl, explicit_targs); 127752284Sobrien if (tmpl && tmpl != error_mark_node) 127852284Sobrien { 127952284Sobrien targs = get_bindings (tmpl, decl, explicit_targs); 128090075Sobrien templates = tree_cons (targs, tmpl, NULL_TREE); 128152284Sobrien } 128250397Sobrien } 128350397Sobrien 128452284Sobrien if (templates == NULL_TREE && candidates == NULL_TREE) 128550397Sobrien { 128652284Sobrien cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", 128752284Sobrien template_id, decl); 128852284Sobrien return error_mark_node; 128950397Sobrien } 129052284Sobrien else if ((templates && TREE_CHAIN (templates)) 129152284Sobrien || (candidates && TREE_CHAIN (candidates)) 129252284Sobrien || (templates && candidates)) 129350397Sobrien { 129452284Sobrien cp_error_at ("ambiguous template specialization `%D' for `%+D'", 129552284Sobrien template_id, decl); 129652284Sobrien chainon (candidates, templates); 129752284Sobrien print_candidates (candidates); 129852284Sobrien return error_mark_node; 129950397Sobrien } 130050397Sobrien 1301117395Skan /* We have one, and exactly one, match. */ 130252284Sobrien if (candidates) 130352284Sobrien { 130452284Sobrien /* It was a specialization of an ordinary member function in a 130552284Sobrien template class. */ 130652284Sobrien *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); 130752284Sobrien return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); 130852284Sobrien } 130952284Sobrien 131052284Sobrien /* It was a specialization of a template. */ 131190075Sobrien targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); 131252284Sobrien if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) 131352284Sobrien { 131452284Sobrien *targs_out = copy_node (targs); 131552284Sobrien SET_TMPL_ARGS_LEVEL (*targs_out, 131652284Sobrien TMPL_ARGS_DEPTH (*targs_out), 131752284Sobrien TREE_PURPOSE (templates)); 131852284Sobrien } 131952284Sobrien else 132052284Sobrien *targs_out = TREE_PURPOSE (templates); 132150397Sobrien return TREE_VALUE (templates); 132250397Sobrien} 132390075Sobrien 132490075Sobrien/* Returns a chain of parameter types, exactly like the SPEC_TYPES, 132590075Sobrien but with the default argument values filled in from those in the 132690075Sobrien TMPL_TYPES. */ 132750397Sobrien 132890075Sobrienstatic tree 132990075Sobriencopy_default_args_to_explicit_spec_1 (spec_types, 133090075Sobrien tmpl_types) 133190075Sobrien tree spec_types; 133290075Sobrien tree tmpl_types; 133390075Sobrien{ 133490075Sobrien tree new_spec_types; 133590075Sobrien 133690075Sobrien if (!spec_types) 133790075Sobrien return NULL_TREE; 133890075Sobrien 133990075Sobrien if (spec_types == void_list_node) 134090075Sobrien return void_list_node; 134190075Sobrien 134290075Sobrien /* Substitute into the rest of the list. */ 134390075Sobrien new_spec_types = 134490075Sobrien copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), 134590075Sobrien TREE_CHAIN (tmpl_types)); 134690075Sobrien 134790075Sobrien /* Add the default argument for this parameter. */ 134890075Sobrien return hash_tree_cons (TREE_PURPOSE (tmpl_types), 134990075Sobrien TREE_VALUE (spec_types), 135090075Sobrien new_spec_types); 135190075Sobrien} 135290075Sobrien 135390075Sobrien/* DECL is an explicit specialization. Replicate default arguments 135490075Sobrien from the template it specializes. (That way, code like: 135590075Sobrien 135690075Sobrien template <class T> void f(T = 3); 135790075Sobrien template <> void f(double); 135890075Sobrien void g () { f (); } 135990075Sobrien 136090075Sobrien works, as required.) An alternative approach would be to look up 136190075Sobrien the correct default arguments at the call-site, but this approach 136290075Sobrien is consistent with how implicit instantiations are handled. */ 136390075Sobrien 136490075Sobrienstatic void 136590075Sobriencopy_default_args_to_explicit_spec (decl) 136690075Sobrien tree decl; 136790075Sobrien{ 136890075Sobrien tree tmpl; 136990075Sobrien tree spec_types; 137090075Sobrien tree tmpl_types; 137190075Sobrien tree new_spec_types; 137290075Sobrien tree old_type; 137390075Sobrien tree new_type; 137490075Sobrien tree t; 137590075Sobrien tree object_type = NULL_TREE; 137690075Sobrien tree in_charge = NULL_TREE; 137790075Sobrien tree vtt = NULL_TREE; 137890075Sobrien 137990075Sobrien /* See if there's anything we need to do. */ 138090075Sobrien tmpl = DECL_TI_TEMPLATE (decl); 138190075Sobrien tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); 138290075Sobrien for (t = tmpl_types; t; t = TREE_CHAIN (t)) 138390075Sobrien if (TREE_PURPOSE (t)) 138490075Sobrien break; 138590075Sobrien if (!t) 138690075Sobrien return; 138790075Sobrien 138890075Sobrien old_type = TREE_TYPE (decl); 138990075Sobrien spec_types = TYPE_ARG_TYPES (old_type); 139090075Sobrien 139190075Sobrien if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 139290075Sobrien { 139390075Sobrien /* Remove the this pointer, but remember the object's type for 139490075Sobrien CV quals. */ 139590075Sobrien object_type = TREE_TYPE (TREE_VALUE (spec_types)); 139690075Sobrien spec_types = TREE_CHAIN (spec_types); 139790075Sobrien tmpl_types = TREE_CHAIN (tmpl_types); 139890075Sobrien 139990075Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (decl)) 140090075Sobrien { 140190075Sobrien /* DECL may contain more parameters than TMPL due to the extra 140290075Sobrien in-charge parameter in constructors and destructors. */ 140390075Sobrien in_charge = spec_types; 140490075Sobrien spec_types = TREE_CHAIN (spec_types); 140590075Sobrien } 140690075Sobrien if (DECL_HAS_VTT_PARM_P (decl)) 140790075Sobrien { 140890075Sobrien vtt = spec_types; 140990075Sobrien spec_types = TREE_CHAIN (spec_types); 141090075Sobrien } 141190075Sobrien } 141290075Sobrien 141390075Sobrien /* Compute the merged default arguments. */ 141490075Sobrien new_spec_types = 141590075Sobrien copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); 141690075Sobrien 141790075Sobrien /* Compute the new FUNCTION_TYPE. */ 141890075Sobrien if (object_type) 141990075Sobrien { 142090075Sobrien if (vtt) 142190075Sobrien new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), 142290075Sobrien TREE_VALUE (vtt), 142390075Sobrien new_spec_types); 142490075Sobrien 142590075Sobrien if (in_charge) 142690075Sobrien /* Put the in-charge parameter back. */ 142790075Sobrien new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), 142890075Sobrien TREE_VALUE (in_charge), 142990075Sobrien new_spec_types); 143090075Sobrien 143190075Sobrien new_type = build_cplus_method_type (object_type, 143290075Sobrien TREE_TYPE (old_type), 143390075Sobrien new_spec_types); 143490075Sobrien } 143590075Sobrien else 143690075Sobrien new_type = build_function_type (TREE_TYPE (old_type), 143790075Sobrien new_spec_types); 143890075Sobrien new_type = build_type_attribute_variant (new_type, 143990075Sobrien TYPE_ATTRIBUTES (old_type)); 144090075Sobrien new_type = build_exception_variant (new_type, 144190075Sobrien TYPE_RAISES_EXCEPTIONS (old_type)); 144290075Sobrien TREE_TYPE (decl) = new_type; 144390075Sobrien} 144490075Sobrien 144550397Sobrien/* Check to see if the function just declared, as indicated in 144650397Sobrien DECLARATOR, and in DECL, is a specialization of a function 144750397Sobrien template. We may also discover that the declaration is an explicit 144850397Sobrien instantiation at this point. 144950397Sobrien 145050397Sobrien Returns DECL, or an equivalent declaration that should be used 145152284Sobrien instead if all goes well. Issues an error message if something is 145252284Sobrien amiss. Returns error_mark_node if the error is not easily 145352284Sobrien recoverable. 145450397Sobrien 145550397Sobrien FLAGS is a bitmask consisting of the following flags: 145650397Sobrien 145750397Sobrien 2: The function has a definition. 145850397Sobrien 4: The function is a friend. 145950397Sobrien 146050397Sobrien The TEMPLATE_COUNT is the number of references to qualifying 146150397Sobrien template classes that appeared in the name of the function. For 146250397Sobrien example, in 146350397Sobrien 146450397Sobrien template <class T> struct S { void f(); }; 146550397Sobrien void S<int>::f(); 146650397Sobrien 146750397Sobrien the TEMPLATE_COUNT would be 1. However, explicitly specialized 146850397Sobrien classes are not counted in the TEMPLATE_COUNT, so that in 146950397Sobrien 147050397Sobrien template <class T> struct S {}; 147150397Sobrien template <> struct S<int> { void f(); } 147252284Sobrien template <> void S<int>::f(); 147350397Sobrien 147450397Sobrien the TEMPLATE_COUNT would be 0. (Note that this declaration is 1475117395Skan invalid; there should be no template <>.) 147650397Sobrien 147750397Sobrien If the function is a specialization, it is marked as such via 147850397Sobrien DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO 147950397Sobrien is set up correctly, and it is added to the list of specializations 148050397Sobrien for that template. */ 148150397Sobrien 148250397Sobrientree 148350397Sobriencheck_explicit_specialization (declarator, decl, template_count, flags) 148450397Sobrien tree declarator; 148550397Sobrien tree decl; 148650397Sobrien int template_count; 148750397Sobrien int flags; 148850397Sobrien{ 148950397Sobrien int have_def = flags & 2; 149050397Sobrien int is_friend = flags & 4; 149150397Sobrien int specialization = 0; 149250397Sobrien int explicit_instantiation = 0; 149352284Sobrien int member_specialization = 0; 149450397Sobrien tree ctype = DECL_CLASS_CONTEXT (decl); 149550397Sobrien tree dname = DECL_NAME (decl); 149690075Sobrien tmpl_spec_kind tsk; 149750397Sobrien 149890075Sobrien tsk = current_tmpl_spec_kind (template_count); 149990075Sobrien 150090075Sobrien switch (tsk) 150150397Sobrien { 150290075Sobrien case tsk_none: 150390075Sobrien if (processing_specialization) 150450397Sobrien { 150552284Sobrien specialization = 1; 150652284Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (decl); 150752284Sobrien } 150890075Sobrien else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 150952284Sobrien { 151090075Sobrien if (is_friend) 151190075Sobrien /* This could be something like: 151250397Sobrien 151390075Sobrien template <class T> void f(T); 151490075Sobrien class S { friend void f<>(int); } */ 151590075Sobrien specialization = 1; 151690075Sobrien else 151790075Sobrien { 151890075Sobrien /* This case handles bogus declarations like template <> 151990075Sobrien template <class T> void f<int>(); */ 152090075Sobrien 152190075Sobrien error ("template-id `%D' in declaration of primary template", 152290075Sobrien declarator); 152390075Sobrien return decl; 152490075Sobrien } 152590075Sobrien } 152690075Sobrien break; 152790075Sobrien 152890075Sobrien case tsk_invalid_member_spec: 152990075Sobrien /* The error has already been reported in 153090075Sobrien check_specialization_scope. */ 153190075Sobrien return error_mark_node; 153290075Sobrien 153390075Sobrien case tsk_invalid_expl_inst: 153490075Sobrien error ("template parameter list used in explicit instantiation"); 153590075Sobrien 153690075Sobrien /* Fall through. */ 153790075Sobrien 153890075Sobrien case tsk_expl_inst: 153952284Sobrien if (have_def) 154090075Sobrien error ("definition provided for explicit instantiation"); 154190075Sobrien 154252284Sobrien explicit_instantiation = 1; 154390075Sobrien break; 154450397Sobrien 154590075Sobrien case tsk_excessive_parms: 154690075Sobrien error ("too many template parameter lists in declaration of `%D'", 154790075Sobrien decl); 154890075Sobrien return error_mark_node; 154950397Sobrien 155090075Sobrien /* Fall through. */ 155190075Sobrien case tsk_expl_spec: 155290075Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (decl); 155390075Sobrien if (ctype) 155490075Sobrien member_specialization = 1; 155590075Sobrien else 155690075Sobrien specialization = 1; 155790075Sobrien break; 155890075Sobrien 155990075Sobrien case tsk_insufficient_parms: 156052284Sobrien if (template_header_count) 156152284Sobrien { 156290075Sobrien error("too few template parameter lists in declaration of `%D'", 156390075Sobrien decl); 156452284Sobrien return decl; 156552284Sobrien } 156690075Sobrien else if (ctype != NULL_TREE 156790075Sobrien && !TYPE_BEING_DEFINED (ctype) 156890075Sobrien && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) 156990075Sobrien && !is_friend) 157090075Sobrien { 157190075Sobrien /* For backwards compatibility, we accept: 157250397Sobrien 157390075Sobrien template <class T> struct S { void f(); }; 157490075Sobrien void S<int>::f() {} // Missing template <> 157552284Sobrien 1576117395Skan That used to be valid C++. */ 157790075Sobrien if (pedantic) 157890075Sobrien pedwarn 157990075Sobrien ("explicit specialization not preceded by `template <>'"); 158090075Sobrien specialization = 1; 158190075Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (decl); 158290075Sobrien } 158390075Sobrien break; 158490075Sobrien 158590075Sobrien case tsk_template: 158690075Sobrien if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) 158750397Sobrien { 158852284Sobrien /* This case handles bogus declarations like template <> 158952284Sobrien template <class T> void f<int>(); */ 159050397Sobrien 159190075Sobrien if (uses_template_parms (declarator)) 159290075Sobrien error ("partial specialization `%D' of function template", 159390075Sobrien declarator); 159490075Sobrien else 159590075Sobrien error ("template-id `%D' in declaration of primary template", 159690075Sobrien declarator); 159750397Sobrien return decl; 159850397Sobrien } 159990075Sobrien 160090075Sobrien if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) 160190075Sobrien /* This is a specialization of a member template, without 160290075Sobrien specialization the containing class. Something like: 160390075Sobrien 160490075Sobrien template <class T> struct S { 160590075Sobrien template <class U> void f (U); 160690075Sobrien }; 160790075Sobrien template <> template <class U> void S<int>::f(U) {} 160890075Sobrien 160990075Sobrien That's a specialization -- but of the entire template. */ 161090075Sobrien specialization = 1; 161190075Sobrien break; 161290075Sobrien 161390075Sobrien default: 161490075Sobrien abort (); 161550397Sobrien } 161650397Sobrien 161750397Sobrien if (specialization || member_specialization) 161850397Sobrien { 161950397Sobrien tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); 162050397Sobrien for (; t; t = TREE_CHAIN (t)) 162150397Sobrien if (TREE_PURPOSE (t)) 162250397Sobrien { 162390075Sobrien pedwarn 162450397Sobrien ("default argument specified in explicit specialization"); 162550397Sobrien break; 162650397Sobrien } 162752284Sobrien if (current_lang_name == lang_name_c) 162890075Sobrien error ("template specialization with C linkage"); 162950397Sobrien } 163050397Sobrien 163150397Sobrien if (specialization || member_specialization || explicit_instantiation) 163250397Sobrien { 163350397Sobrien tree tmpl = NULL_TREE; 163450397Sobrien tree targs = NULL_TREE; 163550397Sobrien 163650397Sobrien /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ 163750397Sobrien if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) 163850397Sobrien { 163950397Sobrien tree fns; 164050397Sobrien 164150397Sobrien my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, 164250397Sobrien 0); 164350397Sobrien if (!ctype) 164450397Sobrien fns = IDENTIFIER_NAMESPACE_VALUE (dname); 164550397Sobrien else 164650397Sobrien fns = dname; 164750397Sobrien 164850397Sobrien declarator = 164950397Sobrien lookup_template_function (fns, NULL_TREE); 165050397Sobrien } 165150397Sobrien 165250397Sobrien if (declarator == error_mark_node) 165350397Sobrien return error_mark_node; 165450397Sobrien 165550397Sobrien if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) 165650397Sobrien { 165750397Sobrien if (!explicit_instantiation) 1658117395Skan /* A specialization in class scope. This is invalid, 165952284Sobrien but the error will already have been flagged by 166052284Sobrien check_specialization_scope. */ 166152284Sobrien return error_mark_node; 166252284Sobrien else 166350397Sobrien { 1664117395Skan /* It's not valid to write an explicit instantiation in 166552284Sobrien class scope, e.g.: 166650397Sobrien 166752284Sobrien class C { template void f(); } 166850397Sobrien 166952284Sobrien This case is caught by the parser. However, on 167052284Sobrien something like: 167150397Sobrien 167252284Sobrien template class C { void f(); }; 167350397Sobrien 1674117395Skan (which is invalid) we can get here. The error will be 167552284Sobrien issued later. */ 167652284Sobrien ; 167752284Sobrien } 167850397Sobrien 167950397Sobrien return decl; 168050397Sobrien } 168152284Sobrien else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) 168252284Sobrien { 168352284Sobrien /* A friend declaration. We can't do much, because we don't 168490075Sobrien know what this resolves to, yet. */ 168552284Sobrien my_friendly_assert (is_friend != 0, 0); 168652284Sobrien my_friendly_assert (!explicit_instantiation, 0); 168752284Sobrien SET_DECL_IMPLICIT_INSTANTIATION (decl); 168852284Sobrien return decl; 168952284Sobrien } 169050397Sobrien else if (ctype != NULL_TREE 169150397Sobrien && (TREE_CODE (TREE_OPERAND (declarator, 0)) == 169250397Sobrien IDENTIFIER_NODE)) 169350397Sobrien { 169450397Sobrien /* Find the list of functions in ctype that have the same 169550397Sobrien name as the declared function. */ 169650397Sobrien tree name = TREE_OPERAND (declarator, 0); 169752284Sobrien tree fns = NULL_TREE; 169852284Sobrien int idx; 169952284Sobrien 1700117395Skan if (constructor_name_p (name, ctype)) 170150397Sobrien { 170250397Sobrien int is_constructor = DECL_CONSTRUCTOR_P (decl); 170350397Sobrien 170450397Sobrien if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) 170550397Sobrien : !TYPE_HAS_DESTRUCTOR (ctype)) 170650397Sobrien { 170750397Sobrien /* From [temp.expl.spec]: 170850397Sobrien 170950397Sobrien If such an explicit specialization for the member 171050397Sobrien of a class template names an implicitly-declared 171150397Sobrien special member function (clause _special_), the 171250397Sobrien program is ill-formed. 171350397Sobrien 171450397Sobrien Similar language is found in [temp.explicit]. */ 171590075Sobrien error ("specialization of implicitly-declared special member function"); 171652284Sobrien return error_mark_node; 171750397Sobrien } 171850397Sobrien 171950397Sobrien name = is_constructor ? ctor_identifier : dtor_identifier; 172050397Sobrien } 172150397Sobrien 172290075Sobrien if (!DECL_CONV_FN_P (decl)) 172352284Sobrien { 172452284Sobrien idx = lookup_fnfields_1 (ctype, name); 172552284Sobrien if (idx >= 0) 172652284Sobrien fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); 172752284Sobrien } 172852284Sobrien else 172952284Sobrien { 173052284Sobrien tree methods; 173152284Sobrien 173252284Sobrien /* For a type-conversion operator, we cannot do a 173352284Sobrien name-based lookup. We might be looking for `operator 173452284Sobrien int' which will be a specialization of `operator T'. 173552284Sobrien So, we find *all* the conversion operators, and then 173652284Sobrien select from them. */ 173752284Sobrien fns = NULL_TREE; 173852284Sobrien 173952284Sobrien methods = CLASSTYPE_METHOD_VEC (ctype); 174052284Sobrien if (methods) 1741117395Skan for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; 1742117395Skan idx < TREE_VEC_LENGTH (methods); ++idx) 174352284Sobrien { 174452284Sobrien tree ovl = TREE_VEC_ELT (methods, idx); 174552284Sobrien 174652284Sobrien if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) 174752284Sobrien /* There are no more conversion functions. */ 174852284Sobrien break; 174952284Sobrien 175052284Sobrien /* Glue all these conversion functions together 175152284Sobrien with those we already have. */ 175252284Sobrien for (; ovl; ovl = OVL_NEXT (ovl)) 175352284Sobrien fns = ovl_cons (OVL_CURRENT (ovl), fns); 175452284Sobrien } 175552284Sobrien } 175652284Sobrien 175750397Sobrien if (fns == NULL_TREE) 175850397Sobrien { 175990075Sobrien error ("no member function `%D' declared in `%T'", 176052284Sobrien name, ctype); 176152284Sobrien return error_mark_node; 176250397Sobrien } 176350397Sobrien else 176450397Sobrien TREE_OPERAND (declarator, 0) = fns; 176550397Sobrien } 176650397Sobrien 176750397Sobrien /* Figure out what exactly is being specialized at this point. 176850397Sobrien Note that for an explicit instantiation, even one for a 176950397Sobrien member function, we cannot tell apriori whether the 177050397Sobrien instantiation is for a member template, or just a member 177152284Sobrien function of a template class. Even if a member template is 177252284Sobrien being instantiated, the member template arguments may be 177352284Sobrien elided if they can be deduced from the rest of the 177452284Sobrien declaration. */ 177550397Sobrien tmpl = determine_specialization (declarator, decl, 177650397Sobrien &targs, 177752284Sobrien member_specialization); 177850397Sobrien 177952284Sobrien if (!tmpl || tmpl == error_mark_node) 178052284Sobrien /* We couldn't figure out what this declaration was 178152284Sobrien specializing. */ 178252284Sobrien return error_mark_node; 178352284Sobrien else 178450397Sobrien { 178552284Sobrien tree gen_tmpl = most_general_template (tmpl); 178652284Sobrien 178750397Sobrien if (explicit_instantiation) 178850397Sobrien { 178952284Sobrien /* We don't set DECL_EXPLICIT_INSTANTIATION here; that 179052284Sobrien is done by do_decl_instantiation later. */ 179152284Sobrien 179252284Sobrien int arg_depth = TMPL_ARGS_DEPTH (targs); 179352284Sobrien int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 179452284Sobrien 179552284Sobrien if (arg_depth > parm_depth) 179652284Sobrien { 179752284Sobrien /* If TMPL is not the most general template (for 179852284Sobrien example, if TMPL is a friend template that is 179952284Sobrien injected into namespace scope), then there will 180090075Sobrien be too many levels of TARGS. Remove some of them 180152284Sobrien here. */ 180252284Sobrien int i; 180352284Sobrien tree new_targs; 180452284Sobrien 180590075Sobrien new_targs = make_tree_vec (parm_depth); 180652284Sobrien for (i = arg_depth - parm_depth; i < arg_depth; ++i) 180752284Sobrien TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) 180852284Sobrien = TREE_VEC_ELT (targs, i); 180952284Sobrien targs = new_targs; 181052284Sobrien } 181152284Sobrien 181290075Sobrien return instantiate_template (tmpl, targs); 181350397Sobrien } 181490075Sobrien 1815117395Skan /* If we thought that the DECL was a member function, but it 1816117395Skan turns out to be specializing a static member function, 1817117395Skan make DECL a static member function as well. We also have 1818117395Skan to adjust last_function_parms to avoid confusing 1819117395Skan start_function later. */ 1820117395Skan if (DECL_STATIC_FUNCTION_P (tmpl) 1821117395Skan && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 1822117395Skan { 1823117395Skan revert_static_member_fn (decl); 1824117395Skan last_function_parms = TREE_CHAIN (last_function_parms); 1825117395Skan } 1826117395Skan 182790075Sobrien /* If this is a specialization of a member template of a 182890075Sobrien template class. In we want to return the TEMPLATE_DECL, 182990075Sobrien not the specialization of it. */ 183090075Sobrien if (tsk == tsk_template) 183190075Sobrien { 183290075Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); 183390075Sobrien DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; 183490075Sobrien return tmpl; 183590075Sobrien } 183690075Sobrien 183752284Sobrien /* Set up the DECL_TEMPLATE_INFO for DECL. */ 183890075Sobrien DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); 183952284Sobrien 184090075Sobrien /* Inherit default function arguments from the template 184190075Sobrien DECL is specializing. */ 184290075Sobrien copy_default_args_to_explicit_spec (decl); 184352284Sobrien 184490075Sobrien /* This specialization has the same protection as the 184590075Sobrien template it specializes. */ 184690075Sobrien TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); 184790075Sobrien TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); 184852284Sobrien 184952284Sobrien if (is_friend && !have_def) 185052284Sobrien /* This is not really a declaration of a specialization. 185152284Sobrien It's just the name of an instantiation. But, it's not 185252284Sobrien a request for an instantiation, either. */ 185352284Sobrien SET_DECL_IMPLICIT_INSTANTIATION (decl); 185490075Sobrien else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) 185590075Sobrien /* This is indeed a specialization. In case of constructors 185690075Sobrien and destructors, we need in-charge and not-in-charge 185790075Sobrien versions in V3 ABI. */ 185890075Sobrien clone_function_decl (decl, /*update_method_vec_p=*/0); 185950397Sobrien 186052284Sobrien /* Register this specialization so that we can find it 186152284Sobrien again. */ 186252284Sobrien decl = register_specialization (decl, gen_tmpl, targs); 186352284Sobrien } 186452284Sobrien } 186552284Sobrien 186652284Sobrien return decl; 186752284Sobrien} 186850397Sobrien 186952284Sobrien/* TYPE is being declared. Verify that the use of template headers 187052284Sobrien and such is reasonable. Issue error messages if not. */ 187150397Sobrien 187252284Sobrienvoid 187352284Sobrienmaybe_check_template_type (type) 187452284Sobrien tree type; 187552284Sobrien{ 187652284Sobrien if (template_header_count) 187752284Sobrien { 187852284Sobrien /* We are in the scope of some `template <...>' header. */ 187950397Sobrien 188052284Sobrien int context_depth 188152284Sobrien = template_class_depth_real (TYPE_CONTEXT (type), 188252284Sobrien /*count_specializations=*/1); 188350397Sobrien 188452284Sobrien if (template_header_count <= context_depth) 188552284Sobrien /* This is OK; the template headers are for the context. We 188652284Sobrien are actually too lenient here; like 188752284Sobrien check_explicit_specialization we should consider the number 188852284Sobrien of template types included in the actual declaration. For 188952284Sobrien example, 189050397Sobrien 189152284Sobrien template <class T> struct S { 189252284Sobrien template <class U> template <class V> 189352284Sobrien struct I {}; 189452284Sobrien }; 189550397Sobrien 1896117395Skan is invalid, but: 189750397Sobrien 189852284Sobrien template <class T> struct S { 189952284Sobrien template <class U> struct I; 190052284Sobrien }; 190152284Sobrien 190252284Sobrien template <class T> template <class U. 190352284Sobrien struct S<T>::I {}; 190452284Sobrien 190552284Sobrien is not. */ 190652284Sobrien ; 190752284Sobrien else if (template_header_count > context_depth + 1) 190852284Sobrien /* There are two many template parameter lists. */ 190990075Sobrien error ("too many template parameter lists in declaration of `%T'", type); 191050397Sobrien } 191150397Sobrien} 191250397Sobrien 191350397Sobrien/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template 191450397Sobrien parameters. These are represented in the same format used for 191550397Sobrien DECL_TEMPLATE_PARMS. */ 191650397Sobrien 191750397Sobrienint comp_template_parms (parms1, parms2) 191850397Sobrien tree parms1; 191950397Sobrien tree parms2; 192050397Sobrien{ 192150397Sobrien tree p1; 192250397Sobrien tree p2; 192350397Sobrien 192450397Sobrien if (parms1 == parms2) 192550397Sobrien return 1; 192650397Sobrien 192750397Sobrien for (p1 = parms1, p2 = parms2; 192850397Sobrien p1 != NULL_TREE && p2 != NULL_TREE; 192950397Sobrien p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) 193050397Sobrien { 193150397Sobrien tree t1 = TREE_VALUE (p1); 193250397Sobrien tree t2 = TREE_VALUE (p2); 193350397Sobrien int i; 193450397Sobrien 193550397Sobrien my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); 193650397Sobrien my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); 193750397Sobrien 193850397Sobrien if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) 193950397Sobrien return 0; 194050397Sobrien 194150397Sobrien for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) 194250397Sobrien { 194350397Sobrien tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); 194450397Sobrien tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); 194550397Sobrien 194650397Sobrien if (TREE_CODE (parm1) != TREE_CODE (parm2)) 194750397Sobrien return 0; 194850397Sobrien 194950397Sobrien if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) 195050397Sobrien continue; 195152284Sobrien else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) 195250397Sobrien return 0; 195350397Sobrien } 195450397Sobrien } 195550397Sobrien 195650397Sobrien if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) 195750397Sobrien /* One set of parameters has more parameters lists than the 195850397Sobrien other. */ 195950397Sobrien return 0; 196050397Sobrien 196150397Sobrien return 1; 196250397Sobrien} 196350397Sobrien 196452284Sobrien/* Complain if DECL shadows a template parameter. 196552284Sobrien 196652284Sobrien [temp.local]: A template-parameter shall not be redeclared within its 196752284Sobrien scope (including nested scopes). */ 196852284Sobrien 196952284Sobrienvoid 197052284Sobriencheck_template_shadow (decl) 197152284Sobrien tree decl; 197252284Sobrien{ 197352284Sobrien tree olddecl; 197452284Sobrien 197552284Sobrien /* If we're not in a template, we can't possibly shadow a template 197652284Sobrien parameter. */ 197752284Sobrien if (!current_template_parms) 197852284Sobrien return; 197952284Sobrien 198052284Sobrien /* Figure out what we're shadowing. */ 198152284Sobrien if (TREE_CODE (decl) == OVERLOAD) 198252284Sobrien decl = OVL_CURRENT (decl); 198352284Sobrien olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); 198452284Sobrien 198552284Sobrien /* If there's no previous binding for this name, we're not shadowing 198652284Sobrien anything, let alone a template parameter. */ 198752284Sobrien if (!olddecl) 198852284Sobrien return; 198952284Sobrien 199052284Sobrien /* If we're not shadowing a template parameter, we're done. Note 199152284Sobrien that OLDDECL might be an OVERLOAD (or perhaps even an 199252284Sobrien ERROR_MARK), so we can't just blithely assume it to be a _DECL 199352284Sobrien node. */ 199490075Sobrien if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) 199552284Sobrien return; 199652284Sobrien 199752284Sobrien /* We check for decl != olddecl to avoid bogus errors for using a 199852284Sobrien name inside a class. We check TPFI to avoid duplicate errors for 199952284Sobrien inline member templates. */ 200052284Sobrien if (decl == olddecl 200152284Sobrien || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) 200252284Sobrien return; 200352284Sobrien 200452284Sobrien cp_error_at ("declaration of `%#D'", decl); 200552284Sobrien cp_error_at (" shadows template parm `%#D'", olddecl); 200652284Sobrien} 200752284Sobrien 200850397Sobrien/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, 200950397Sobrien ORIG_LEVEL, DECL, and TYPE. */ 201050397Sobrien 201150397Sobrienstatic tree 201250397Sobrienbuild_template_parm_index (index, level, orig_level, decl, type) 201350397Sobrien int index; 201450397Sobrien int level; 201550397Sobrien int orig_level; 201650397Sobrien tree decl; 201750397Sobrien tree type; 201850397Sobrien{ 201950397Sobrien tree t = make_node (TEMPLATE_PARM_INDEX); 202050397Sobrien TEMPLATE_PARM_IDX (t) = index; 202150397Sobrien TEMPLATE_PARM_LEVEL (t) = level; 202250397Sobrien TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; 202350397Sobrien TEMPLATE_PARM_DECL (t) = decl; 202450397Sobrien TREE_TYPE (t) = type; 202550397Sobrien 202650397Sobrien return t; 202750397Sobrien} 202850397Sobrien 202950397Sobrien/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose 203050397Sobrien TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a 203150397Sobrien TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a 203250397Sobrien new one is created. */ 203350397Sobrien 203450397Sobrienstatic tree 203550397Sobrienreduce_template_parm_level (index, type, levels) 203650397Sobrien tree index; 203750397Sobrien tree type; 203850397Sobrien int levels; 203950397Sobrien{ 204050397Sobrien if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE 204150397Sobrien || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) 204250397Sobrien != TEMPLATE_PARM_LEVEL (index) - levels)) 204350397Sobrien { 204450397Sobrien tree decl 204550397Sobrien = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), 204650397Sobrien DECL_NAME (TEMPLATE_PARM_DECL (index)), 204750397Sobrien type); 204850397Sobrien tree t 204950397Sobrien = build_template_parm_index (TEMPLATE_PARM_IDX (index), 205050397Sobrien TEMPLATE_PARM_LEVEL (index) - levels, 205150397Sobrien TEMPLATE_PARM_ORIG_LEVEL (index), 205250397Sobrien decl, type); 205350397Sobrien TEMPLATE_PARM_DESCENDANTS (index) = t; 205450397Sobrien 205590075Sobrien DECL_ARTIFICIAL (decl) = 1; 205690075Sobrien SET_DECL_TEMPLATE_PARM_P (decl); 205790075Sobrien 205850397Sobrien /* Template template parameters need this. */ 205950397Sobrien DECL_TEMPLATE_PARMS (decl) 206050397Sobrien = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); 206150397Sobrien } 206250397Sobrien 206350397Sobrien return TEMPLATE_PARM_DESCENDANTS (index); 206450397Sobrien} 206550397Sobrien 206618334Speter/* Process information from new template parameter NEXT and append it to the 206750397Sobrien LIST being built. */ 206850397Sobrien 206918334Spetertree 207018334Speterprocess_template_parm (list, next) 207118334Speter tree list, next; 207218334Speter{ 207318334Speter tree parm; 207418334Speter tree decl = 0; 207518334Speter tree defval; 207650397Sobrien int is_type, idx; 207750397Sobrien 207818334Speter parm = next; 207918334Speter my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); 208018334Speter defval = TREE_PURPOSE (parm); 208118334Speter parm = TREE_VALUE (parm); 208218334Speter is_type = TREE_PURPOSE (parm) == class_type_node; 208350397Sobrien 208450397Sobrien if (list) 208550397Sobrien { 208650397Sobrien tree p = TREE_VALUE (tree_last (list)); 208750397Sobrien 208890075Sobrien if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) 208950397Sobrien idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); 209050397Sobrien else 209150397Sobrien idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); 209250397Sobrien ++idx; 209350397Sobrien } 209450397Sobrien else 209550397Sobrien idx = 0; 209650397Sobrien 209718334Speter if (!is_type) 209818334Speter { 209918334Speter my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); 210018334Speter /* is a const-param */ 210118334Speter parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), 210290075Sobrien PARM, 0, NULL); 210390075Sobrien SET_DECL_TEMPLATE_PARM_P (parm); 210452284Sobrien 210552284Sobrien /* [temp.param] 210652284Sobrien 210752284Sobrien The top-level cv-qualifiers on the template-parameter are 210852284Sobrien ignored when determining its type. */ 210952284Sobrien TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); 211052284Sobrien 211118334Speter /* A template parameter is not modifiable. */ 211218334Speter TREE_READONLY (parm) = 1; 211390075Sobrien if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) 211490075Sobrien TREE_TYPE (parm) = void_type_node; 211518334Speter decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); 211650397Sobrien DECL_INITIAL (parm) = DECL_INITIAL (decl) 211750397Sobrien = build_template_parm_index (idx, processing_template_decl, 211850397Sobrien processing_template_decl, 211950397Sobrien decl, TREE_TYPE (parm)); 212018334Speter } 212118334Speter else 212218334Speter { 212350397Sobrien tree t; 212450397Sobrien parm = TREE_VALUE (parm); 212550397Sobrien 212650397Sobrien if (parm && TREE_CODE (parm) == TEMPLATE_DECL) 212718334Speter { 212890075Sobrien t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); 212950397Sobrien /* This is for distinguishing between real templates and template 213050397Sobrien template parameters */ 213150397Sobrien TREE_TYPE (parm) = t; 213250397Sobrien TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; 213350397Sobrien decl = parm; 213418334Speter } 213550397Sobrien else 213650397Sobrien { 213790075Sobrien t = make_aggr_type (TEMPLATE_TYPE_PARM); 213850397Sobrien /* parm is either IDENTIFIER_NODE or NULL_TREE */ 213950397Sobrien decl = build_decl (TYPE_DECL, parm, t); 214050397Sobrien } 214150397Sobrien 214250397Sobrien TYPE_NAME (t) = decl; 214350397Sobrien TYPE_STUB_DECL (t) = decl; 214450397Sobrien parm = decl; 214550397Sobrien TEMPLATE_TYPE_PARM_INDEX (t) 214650397Sobrien = build_template_parm_index (idx, processing_template_decl, 214750397Sobrien processing_template_decl, 214850397Sobrien decl, TREE_TYPE (parm)); 214918334Speter } 215090075Sobrien DECL_ARTIFICIAL (decl) = 1; 215190075Sobrien SET_DECL_TEMPLATE_PARM_P (decl); 215218334Speter pushdecl (decl); 215318334Speter parm = build_tree_list (defval, parm); 215418334Speter return chainon (list, parm); 215518334Speter} 215618334Speter 215718334Speter/* The end of a template parameter list has been reached. Process the 215818334Speter tree list into a parameter vector, converting each parameter into a more 215918334Speter useful form. Type parameters are saved as IDENTIFIER_NODEs, and others 216018334Speter as PARM_DECLs. */ 216118334Speter 216218334Spetertree 216318334Speterend_template_parm_list (parms) 216418334Speter tree parms; 216518334Speter{ 216650397Sobrien int nparms; 216790075Sobrien tree parm, next; 216850397Sobrien tree saved_parmlist = make_tree_vec (list_length (parms)); 216918334Speter 217050397Sobrien current_template_parms 217190075Sobrien = tree_cons (size_int (processing_template_decl), 217250397Sobrien saved_parmlist, current_template_parms); 217350397Sobrien 217490075Sobrien for (parm = parms, nparms = 0; parm; parm = next, nparms++) 217590075Sobrien { 217690075Sobrien next = TREE_CHAIN (parm); 217790075Sobrien TREE_VEC_ELT (saved_parmlist, nparms) = parm; 217890075Sobrien TREE_CHAIN (parm) = NULL_TREE; 217990075Sobrien } 218050397Sobrien 218150397Sobrien --processing_template_parmlist; 218250397Sobrien 218350397Sobrien return saved_parmlist; 218450397Sobrien} 218550397Sobrien 218650397Sobrien/* end_template_decl is called after a template declaration is seen. */ 218750397Sobrien 218850397Sobrienvoid 218950397Sobrienend_template_decl () 219050397Sobrien{ 219150397Sobrien reset_specialization (); 219250397Sobrien 219350397Sobrien if (! processing_template_decl) 219450397Sobrien return; 219550397Sobrien 219650397Sobrien /* This matches the pushlevel in begin_template_parm_list. */ 219790075Sobrien finish_scope (); 219850397Sobrien 219950397Sobrien --processing_template_decl; 220050397Sobrien current_template_parms = TREE_CHAIN (current_template_parms); 220150397Sobrien} 220250397Sobrien 220352284Sobrien/* Given a template argument vector containing the template PARMS. 220452284Sobrien The innermost PARMS are given first. */ 220550397Sobrien 220650397Sobrientree 220750397Sobriencurrent_template_args () 220850397Sobrien{ 220952284Sobrien tree header; 221052284Sobrien tree args = NULL_TREE; 221152284Sobrien int length = TMPL_PARMS_DEPTH (current_template_parms); 221250397Sobrien int l = length; 221350397Sobrien 221452284Sobrien /* If there is only one level of template parameters, we do not 221552284Sobrien create a TREE_VEC of TREE_VECs. Instead, we return a single 221652284Sobrien TREE_VEC containing the arguments. */ 221752284Sobrien if (length > 1) 221852284Sobrien args = make_tree_vec (length); 221952284Sobrien 222052284Sobrien for (header = current_template_parms; header; header = TREE_CHAIN (header)) 222118334Speter { 222250397Sobrien tree a = copy_node (TREE_VALUE (header)); 222352284Sobrien int i; 222452284Sobrien 222550397Sobrien TREE_TYPE (a) = NULL_TREE; 222652284Sobrien for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) 222718334Speter { 222850397Sobrien tree t = TREE_VEC_ELT (a, i); 222950397Sobrien 223052284Sobrien /* T will be a list if we are called from within a 223150397Sobrien begin/end_template_parm_list pair, but a vector directly 223250397Sobrien if within a begin/end_member_template_processing pair. */ 223350397Sobrien if (TREE_CODE (t) == TREE_LIST) 223450397Sobrien { 223550397Sobrien t = TREE_VALUE (t); 223650397Sobrien 223750397Sobrien if (TREE_CODE (t) == TYPE_DECL 223850397Sobrien || TREE_CODE (t) == TEMPLATE_DECL) 223950397Sobrien t = TREE_TYPE (t); 224050397Sobrien else 224150397Sobrien t = DECL_INITIAL (t); 224252284Sobrien TREE_VEC_ELT (a, i) = t; 224350397Sobrien } 224452284Sobrien } 224550397Sobrien 224652284Sobrien if (length > 1) 224752284Sobrien TREE_VEC_ELT (args, --l) = a; 224852284Sobrien else 224952284Sobrien args = a; 225050397Sobrien } 225118334Speter 225250397Sobrien return args; 225350397Sobrien} 225450397Sobrien 225550397Sobrien/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated 225650397Sobrien template PARMS. Used by push_template_decl below. */ 225750397Sobrien 225850397Sobrienstatic tree 225950397Sobrienbuild_template_decl (decl, parms) 226050397Sobrien tree decl; 226150397Sobrien tree parms; 226250397Sobrien{ 226350397Sobrien tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); 226450397Sobrien DECL_TEMPLATE_PARMS (tmpl) = parms; 226550397Sobrien DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); 226650397Sobrien if (DECL_LANG_SPECIFIC (decl)) 226750397Sobrien { 226852284Sobrien DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); 226952284Sobrien DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); 2270117395Skan DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); 227190075Sobrien DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); 227290075Sobrien DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); 227390075Sobrien if (DECL_OVERLOADED_OPERATOR_P (decl)) 227490075Sobrien SET_OVERLOADED_OPERATOR_CODE (tmpl, 227590075Sobrien DECL_OVERLOADED_OPERATOR_P (decl)); 227650397Sobrien } 227750397Sobrien 227850397Sobrien return tmpl; 227950397Sobrien} 228050397Sobrien 228150397Sobrienstruct template_parm_data 228250397Sobrien{ 228352284Sobrien /* The level of the template parameters we are currently 228452284Sobrien processing. */ 228550397Sobrien int level; 228652284Sobrien 228752284Sobrien /* The index of the specialization argument we are currently 228852284Sobrien processing. */ 228952284Sobrien int current_arg; 229052284Sobrien 229152284Sobrien /* An array whose size is the number of template parameters. The 2292117395Skan elements are nonzero if the parameter has been used in any one 229352284Sobrien of the arguments processed so far. */ 229450397Sobrien int* parms; 229552284Sobrien 229652284Sobrien /* An array whose size is the number of template arguments. The 2297117395Skan elements are nonzero if the argument makes use of template 229852284Sobrien parameters of this level. */ 229952284Sobrien int* arg_uses_template_parms; 230050397Sobrien}; 230150397Sobrien 230250397Sobrien/* Subroutine of push_template_decl used to see if each template 230350397Sobrien parameter in a partial specialization is used in the explicit 230450397Sobrien argument list. If T is of the LEVEL given in DATA (which is 230550397Sobrien treated as a template_parm_data*), then DATA->PARMS is marked 230650397Sobrien appropriately. */ 230750397Sobrien 230850397Sobrienstatic int 230950397Sobrienmark_template_parm (t, data) 231050397Sobrien tree t; 231150397Sobrien void* data; 231250397Sobrien{ 231350397Sobrien int level; 231450397Sobrien int idx; 231550397Sobrien struct template_parm_data* tpd = (struct template_parm_data*) data; 231650397Sobrien 231750397Sobrien if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) 231850397Sobrien { 231950397Sobrien level = TEMPLATE_PARM_LEVEL (t); 232050397Sobrien idx = TEMPLATE_PARM_IDX (t); 232150397Sobrien } 232250397Sobrien else 232350397Sobrien { 232450397Sobrien level = TEMPLATE_TYPE_LEVEL (t); 232550397Sobrien idx = TEMPLATE_TYPE_IDX (t); 232650397Sobrien } 232750397Sobrien 232850397Sobrien if (level == tpd->level) 232952284Sobrien { 233052284Sobrien tpd->parms[idx] = 1; 233152284Sobrien tpd->arg_uses_template_parms[tpd->current_arg] = 1; 233252284Sobrien } 233350397Sobrien 233450397Sobrien /* Return zero so that for_each_template_parm will continue the 233550397Sobrien traversal of the tree; we want to mark *every* template parm. */ 233650397Sobrien return 0; 233750397Sobrien} 233850397Sobrien 233952284Sobrien/* Process the partial specialization DECL. */ 234052284Sobrien 234152284Sobrienstatic tree 234252284Sobrienprocess_partial_specialization (decl) 234352284Sobrien tree decl; 234452284Sobrien{ 234552284Sobrien tree type = TREE_TYPE (decl); 234652284Sobrien tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); 234752284Sobrien tree specargs = CLASSTYPE_TI_ARGS (type); 234890075Sobrien tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); 234952284Sobrien tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); 235052284Sobrien tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); 235152284Sobrien int nargs = TREE_VEC_LENGTH (inner_args); 235252284Sobrien int ntparms = TREE_VEC_LENGTH (inner_parms); 235352284Sobrien int i; 235452284Sobrien int did_error_intro = 0; 235552284Sobrien struct template_parm_data tpd; 235652284Sobrien struct template_parm_data tpd2; 235752284Sobrien 235852284Sobrien /* We check that each of the template parameters given in the 235952284Sobrien partial specialization is used in the argument list to the 236052284Sobrien specialization. For example: 236152284Sobrien 236252284Sobrien template <class T> struct S; 236352284Sobrien template <class T> struct S<T*>; 236452284Sobrien 236552284Sobrien The second declaration is OK because `T*' uses the template 236652284Sobrien parameter T, whereas 236752284Sobrien 236852284Sobrien template <class T> struct S<int>; 236952284Sobrien 237052284Sobrien is no good. Even trickier is: 237152284Sobrien 237252284Sobrien template <class T> 237352284Sobrien struct S1 237452284Sobrien { 237552284Sobrien template <class U> 237652284Sobrien struct S2; 237752284Sobrien template <class U> 237852284Sobrien struct S2<T>; 237952284Sobrien }; 238052284Sobrien 2381117395Skan The S2<T> declaration is actually invalid; it is a 238252284Sobrien full-specialization. Of course, 238352284Sobrien 238452284Sobrien template <class U> 238552284Sobrien struct S2<T (*)(U)>; 238652284Sobrien 238752284Sobrien or some such would have been OK. */ 238852284Sobrien tpd.level = TMPL_PARMS_DEPTH (current_template_parms); 238952284Sobrien tpd.parms = alloca (sizeof (int) * ntparms); 239090075Sobrien memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms); 239152284Sobrien 239252284Sobrien tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); 239390075Sobrien memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); 239452284Sobrien for (i = 0; i < nargs; ++i) 239552284Sobrien { 239652284Sobrien tpd.current_arg = i; 239752284Sobrien for_each_template_parm (TREE_VEC_ELT (inner_args, i), 239852284Sobrien &mark_template_parm, 2399117395Skan &tpd, 2400117395Skan NULL); 240152284Sobrien } 240252284Sobrien for (i = 0; i < ntparms; ++i) 240352284Sobrien if (tpd.parms[i] == 0) 240452284Sobrien { 240552284Sobrien /* One of the template parms was not used in the 240690075Sobrien specialization. */ 240752284Sobrien if (!did_error_intro) 240852284Sobrien { 240990075Sobrien error ("template parameters not used in partial specialization:"); 241052284Sobrien did_error_intro = 1; 241152284Sobrien } 241252284Sobrien 241390075Sobrien error (" `%D'", 241452284Sobrien TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); 241552284Sobrien } 241652284Sobrien 241752284Sobrien /* [temp.class.spec] 241852284Sobrien 241952284Sobrien The argument list of the specialization shall not be identical to 242052284Sobrien the implicit argument list of the primary template. */ 242190075Sobrien if (comp_template_args 242290075Sobrien (inner_args, 242390075Sobrien INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE 242490075Sobrien (maintmpl))))) 242590075Sobrien error ("partial specialization `%T' does not specialize any template arguments", type); 242652284Sobrien 242752284Sobrien /* [temp.class.spec] 242852284Sobrien 242952284Sobrien A partially specialized non-type argument expression shall not 243052284Sobrien involve template parameters of the partial specialization except 243152284Sobrien when the argument expression is a simple identifier. 243252284Sobrien 243352284Sobrien The type of a template parameter corresponding to a specialized 243452284Sobrien non-type argument shall not be dependent on a parameter of the 243552284Sobrien specialization. */ 243652284Sobrien my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); 243752284Sobrien tpd2.parms = 0; 243852284Sobrien for (i = 0; i < nargs; ++i) 243952284Sobrien { 244052284Sobrien tree arg = TREE_VEC_ELT (inner_args, i); 244152284Sobrien if (/* These first two lines are the `non-type' bit. */ 244290075Sobrien !TYPE_P (arg) 244352284Sobrien && TREE_CODE (arg) != TEMPLATE_DECL 244452284Sobrien /* This next line is the `argument expression is not just a 244552284Sobrien simple identifier' condition and also the `specialized 244652284Sobrien non-type argument' bit. */ 244752284Sobrien && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) 244852284Sobrien { 244952284Sobrien if (tpd.arg_uses_template_parms[i]) 245090075Sobrien error ("template argument `%E' involves template parameter(s)", arg); 245152284Sobrien else 245252284Sobrien { 245352284Sobrien /* Look at the corresponding template parameter, 245452284Sobrien marking which template parameters its type depends 245552284Sobrien upon. */ 245652284Sobrien tree type = 245752284Sobrien TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, 245852284Sobrien i))); 245952284Sobrien 246052284Sobrien if (!tpd2.parms) 246152284Sobrien { 246252284Sobrien /* We haven't yet initialized TPD2. Do so now. */ 246352284Sobrien tpd2.arg_uses_template_parms 246452284Sobrien = (int*) alloca (sizeof (int) * nargs); 246552284Sobrien /* The number of parameters here is the number in the 246652284Sobrien main template, which, as checked in the assertion 246752284Sobrien above, is NARGS. */ 246852284Sobrien tpd2.parms = (int*) alloca (sizeof (int) * nargs); 246952284Sobrien tpd2.level = 247052284Sobrien TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); 247152284Sobrien } 247252284Sobrien 247352284Sobrien /* Mark the template parameters. But this time, we're 247452284Sobrien looking for the template parameters of the main 247552284Sobrien template, not in the specialization. */ 247652284Sobrien tpd2.current_arg = i; 247752284Sobrien tpd2.arg_uses_template_parms[i] = 0; 247890075Sobrien memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs); 247952284Sobrien for_each_template_parm (type, 248052284Sobrien &mark_template_parm, 2481117395Skan &tpd2, 2482117395Skan NULL); 248352284Sobrien 248452284Sobrien if (tpd2.arg_uses_template_parms [i]) 248552284Sobrien { 248652284Sobrien /* The type depended on some template parameters. 248752284Sobrien If they are fully specialized in the 248852284Sobrien specialization, that's OK. */ 248952284Sobrien int j; 249052284Sobrien for (j = 0; j < nargs; ++j) 249152284Sobrien if (tpd2.parms[j] != 0 249252284Sobrien && tpd.arg_uses_template_parms [j]) 249352284Sobrien { 249490075Sobrien error ("type `%T' of template argument `%E' depends on template parameter(s)", 249552284Sobrien type, 249652284Sobrien arg); 249752284Sobrien break; 249852284Sobrien } 249952284Sobrien } 250052284Sobrien } 250152284Sobrien } 250252284Sobrien } 250352284Sobrien 250452284Sobrien if (retrieve_specialization (maintmpl, specargs)) 250552284Sobrien /* We've already got this specialization. */ 250652284Sobrien return decl; 250752284Sobrien 250890075Sobrien DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) 250990075Sobrien = tree_cons (inner_args, inner_parms, 251090075Sobrien DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); 251152284Sobrien TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; 251252284Sobrien return decl; 251352284Sobrien} 251452284Sobrien 251552284Sobrien/* Check that a template declaration's use of default arguments is not 251652284Sobrien invalid. Here, PARMS are the template parameters. IS_PRIMARY is 2517117395Skan nonzero if DECL is the thing declared by a primary template. 2518117395Skan IS_PARTIAL is nonzero if DECL is a partial specialization. */ 251952284Sobrien 252052284Sobrienstatic void 252152284Sobriencheck_default_tmpl_args (decl, parms, is_primary, is_partial) 252252284Sobrien tree decl; 252352284Sobrien tree parms; 252452284Sobrien int is_primary; 252552284Sobrien int is_partial; 252652284Sobrien{ 252752284Sobrien const char *msg; 252890075Sobrien int last_level_to_check; 252990075Sobrien tree parm_level; 253052284Sobrien 253152284Sobrien /* [temp.param] 253252284Sobrien 253352284Sobrien A default template-argument shall not be specified in a 253452284Sobrien function template declaration or a function template definition, nor 253552284Sobrien in the template-parameter-list of the definition of a member of a 253652284Sobrien class template. */ 253752284Sobrien 253890075Sobrien if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) 253990075Sobrien /* You can't have a function template declaration in a local 254090075Sobrien scope, nor you can you define a member of a class template in a 254190075Sobrien local scope. */ 254290075Sobrien return; 254390075Sobrien 254452284Sobrien if (current_class_type 254552284Sobrien && !TYPE_BEING_DEFINED (current_class_type) 254652284Sobrien && DECL_LANG_SPECIFIC (decl) 254752284Sobrien /* If this is either a friend defined in the scope of the class 254852284Sobrien or a member function. */ 254990075Sobrien && ((DECL_CONTEXT (decl) 255090075Sobrien && same_type_p (DECL_CONTEXT (decl), current_class_type)) 255190075Sobrien || (DECL_FRIEND_CONTEXT (decl) 255290075Sobrien && same_type_p (DECL_FRIEND_CONTEXT (decl), 255390075Sobrien current_class_type))) 255452284Sobrien /* And, if it was a member function, it really was defined in 255552284Sobrien the scope of the class. */ 255690075Sobrien && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl))) 255752284Sobrien /* We already checked these parameters when the template was 255852284Sobrien declared, so there's no need to do it again now. This function 255952284Sobrien was defined in class scope, but we're processing it's body now 256052284Sobrien that the class is complete. */ 256152284Sobrien return; 256252284Sobrien 256390075Sobrien /* [temp.param] 256490075Sobrien 256590075Sobrien If a template-parameter has a default template-argument, all 256690075Sobrien subsequent template-parameters shall have a default 256790075Sobrien template-argument supplied. */ 256890075Sobrien for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) 256990075Sobrien { 257090075Sobrien tree inner_parms = TREE_VALUE (parm_level); 257190075Sobrien int ntparms = TREE_VEC_LENGTH (inner_parms); 257290075Sobrien int seen_def_arg_p = 0; 257390075Sobrien int i; 257490075Sobrien 257590075Sobrien for (i = 0; i < ntparms; ++i) 257690075Sobrien { 257790075Sobrien tree parm = TREE_VEC_ELT (inner_parms, i); 257890075Sobrien if (TREE_PURPOSE (parm)) 257990075Sobrien seen_def_arg_p = 1; 258090075Sobrien else if (seen_def_arg_p) 258190075Sobrien { 258290075Sobrien error ("no default argument for `%D'", TREE_VALUE (parm)); 258390075Sobrien /* For better subsequent error-recovery, we indicate that 258490075Sobrien there should have been a default argument. */ 258590075Sobrien TREE_PURPOSE (parm) = error_mark_node; 258690075Sobrien } 258790075Sobrien } 258890075Sobrien } 258990075Sobrien 259052284Sobrien if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) 259152284Sobrien /* For an ordinary class template, default template arguments are 259252284Sobrien allowed at the innermost level, e.g.: 259352284Sobrien template <class T = int> 259452284Sobrien struct S {}; 259552284Sobrien but, in a partial specialization, they're not allowed even 259652284Sobrien there, as we have in [temp.class.spec]: 259752284Sobrien 259852284Sobrien The template parameter list of a specialization shall not 259952284Sobrien contain default template argument values. 260052284Sobrien 260152284Sobrien So, for a partial specialization, or for a function template, 260252284Sobrien we look at all of them. */ 260352284Sobrien ; 260452284Sobrien else 260552284Sobrien /* But, for a primary class template that is not a partial 260652284Sobrien specialization we look at all template parameters except the 260752284Sobrien innermost ones. */ 260852284Sobrien parms = TREE_CHAIN (parms); 260952284Sobrien 261052284Sobrien /* Figure out what error message to issue. */ 261152284Sobrien if (TREE_CODE (decl) == FUNCTION_DECL) 261290075Sobrien msg = "default template arguments may not be used in function templates"; 261352284Sobrien else if (is_partial) 261490075Sobrien msg = "default template arguments may not be used in partial specializations"; 261552284Sobrien else 261652284Sobrien msg = "default argument for template parameter for class enclosing `%D'"; 261752284Sobrien 261852284Sobrien if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) 261952284Sobrien /* If we're inside a class definition, there's no need to 262052284Sobrien examine the parameters to the class itself. On the one 262152284Sobrien hand, they will be checked when the class is defined, and, 2622117395Skan on the other, default arguments are valid in things like: 262352284Sobrien template <class T = double> 262452284Sobrien struct S { template <class U> void f(U); }; 262552284Sobrien Here the default argument for `S' has no bearing on the 262652284Sobrien declaration of `f'. */ 262752284Sobrien last_level_to_check = template_class_depth (current_class_type) + 1; 262852284Sobrien else 262952284Sobrien /* Check everything. */ 263052284Sobrien last_level_to_check = 0; 263152284Sobrien 263290075Sobrien for (parm_level = parms; 263390075Sobrien parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; 263490075Sobrien parm_level = TREE_CHAIN (parm_level)) 263552284Sobrien { 263690075Sobrien tree inner_parms = TREE_VALUE (parm_level); 263790075Sobrien int i; 263890075Sobrien int ntparms; 263952284Sobrien 264052284Sobrien ntparms = TREE_VEC_LENGTH (inner_parms); 264152284Sobrien for (i = 0; i < ntparms; ++i) 264252284Sobrien if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) 264352284Sobrien { 264452284Sobrien if (msg) 264552284Sobrien { 264690075Sobrien error (msg, decl); 264752284Sobrien msg = 0; 264852284Sobrien } 264952284Sobrien 265052284Sobrien /* Clear out the default argument so that we are not 265152284Sobrien confused later. */ 265252284Sobrien TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; 265352284Sobrien } 265452284Sobrien 265552284Sobrien /* At this point, if we're still interested in issuing messages, 265652284Sobrien they must apply to classes surrounding the object declared. */ 265752284Sobrien if (msg) 265852284Sobrien msg = "default argument for template parameter for class enclosing `%D'"; 265952284Sobrien } 266052284Sobrien} 266152284Sobrien 2662117395Skan/* Worker for push_template_decl_real, called via 2663117395Skan for_each_template_parm. DATA is really an int, indicating the 2664117395Skan level of the parameters we are interested in. If T is a template 2665117395Skan parameter of that level, return nonzero. */ 2666117395Skan 2667117395Skanstatic int 2668117395Skantemplate_parm_this_level_p (t, data) 2669117395Skan tree t; 2670117395Skan void *data; 2671117395Skan{ 2672117395Skan int this_level = *(int *)data; 2673117395Skan int level; 2674117395Skan 2675117395Skan if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) 2676117395Skan level = TEMPLATE_PARM_LEVEL (t); 2677117395Skan else 2678117395Skan level = TEMPLATE_TYPE_LEVEL (t); 2679117395Skan return level == this_level; 2680117395Skan} 2681117395Skan 268250397Sobrien/* Creates a TEMPLATE_DECL for the indicated DECL using the template 268350397Sobrien parameters given by current_template_args, or reuses a 268450397Sobrien previously existing one, if appropriate. Returns the DECL, or an 268550397Sobrien equivalent one, if it is replaced via a call to duplicate_decls. 268650397Sobrien 2687117395Skan If IS_FRIEND is nonzero, DECL is a friend declaration. */ 268850397Sobrien 268950397Sobrientree 269050397Sobrienpush_template_decl_real (decl, is_friend) 269150397Sobrien tree decl; 269250397Sobrien int is_friend; 269350397Sobrien{ 269450397Sobrien tree tmpl; 269550397Sobrien tree args; 269650397Sobrien tree info; 269750397Sobrien tree ctx; 269850397Sobrien int primary; 269952284Sobrien int is_partial; 270090075Sobrien int new_template_p = 0; 270150397Sobrien 270252284Sobrien /* See if this is a partial specialization. */ 270390075Sobrien is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) 270452284Sobrien && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE 270552284Sobrien && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); 270652284Sobrien 270750397Sobrien is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); 270850397Sobrien 270950397Sobrien if (is_friend) 271050397Sobrien /* For a friend, we want the context of the friend function, not 271150397Sobrien the type of which it is a friend. */ 271250397Sobrien ctx = DECL_CONTEXT (decl); 271390075Sobrien else if (CP_DECL_CONTEXT (decl) 271490075Sobrien && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) 271550397Sobrien /* In the case of a virtual function, we want the class in which 271650397Sobrien it is defined. */ 271790075Sobrien ctx = CP_DECL_CONTEXT (decl); 271850397Sobrien else 271990075Sobrien /* Otherwise, if we're currently defining some class, the DECL 272050397Sobrien is assumed to be a member of the class. */ 272190075Sobrien ctx = current_scope (); 272250397Sobrien 272350397Sobrien if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) 272450397Sobrien ctx = NULL_TREE; 272550397Sobrien 272650397Sobrien if (!DECL_CONTEXT (decl)) 272750397Sobrien DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 272850397Sobrien 272952284Sobrien /* See if this is a primary template. */ 273090075Sobrien primary = template_parm_scope_p (); 273150397Sobrien 273250397Sobrien if (primary) 273350397Sobrien { 273450397Sobrien if (current_lang_name == lang_name_c) 273590075Sobrien error ("template with C linkage"); 273690075Sobrien else if (TREE_CODE (decl) == TYPE_DECL 273790075Sobrien && ANON_AGGRNAME_P (DECL_NAME (decl))) 273890075Sobrien error ("template class without a name"); 273990075Sobrien else if ((DECL_IMPLICIT_TYPEDEF_P (decl) 274090075Sobrien && CLASS_TYPE_P (TREE_TYPE (decl))) 274190075Sobrien || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) 274290075Sobrien || TREE_CODE (decl) == FUNCTION_DECL) 274390075Sobrien /* OK */; 274490075Sobrien else 274590075Sobrien error ("template declaration of `%#D'", decl); 274650397Sobrien } 274750397Sobrien 274852284Sobrien /* Check to see that the rules regarding the use of default 274952284Sobrien arguments are not being violated. */ 275052284Sobrien check_default_tmpl_args (decl, current_template_parms, 275152284Sobrien primary, is_partial); 275250397Sobrien 275352284Sobrien if (is_partial) 275452284Sobrien return process_partial_specialization (decl); 275550397Sobrien 275650397Sobrien args = current_template_args (); 275750397Sobrien 275850397Sobrien if (!ctx 275950397Sobrien || TREE_CODE (ctx) == FUNCTION_DECL 276096263Sobrien || (TREE_CODE (ctx) != TEMPLATE_TYPE_PARM 276196263Sobrien && TREE_CODE (ctx) != BOUND_TEMPLATE_TEMPLATE_PARM 276296263Sobrien && TYPE_BEING_DEFINED (ctx)) 276350397Sobrien || (is_friend && !DECL_TEMPLATE_INFO (decl))) 276450397Sobrien { 276550397Sobrien if (DECL_LANG_SPECIFIC (decl) 276650397Sobrien && DECL_TEMPLATE_INFO (decl) 276750397Sobrien && DECL_TI_TEMPLATE (decl)) 276850397Sobrien tmpl = DECL_TI_TEMPLATE (decl); 276990075Sobrien /* If DECL is a TYPE_DECL for a class-template, then there won't 277090075Sobrien be DECL_LANG_SPECIFIC. The information equivalent to 277190075Sobrien DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ 277290075Sobrien else if (DECL_IMPLICIT_TYPEDEF_P (decl) 277390075Sobrien && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) 277490075Sobrien && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) 277590075Sobrien { 277690075Sobrien /* Since a template declaration already existed for this 277790075Sobrien class-type, we must be redeclaring it here. Make sure 2778117395Skan that the redeclaration is valid. */ 277990075Sobrien redeclare_class_template (TREE_TYPE (decl), 278090075Sobrien current_template_parms); 278190075Sobrien /* We don't need to create a new TEMPLATE_DECL; just use the 278290075Sobrien one we already had. */ 278390075Sobrien tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); 278490075Sobrien } 278518334Speter else 278618334Speter { 278750397Sobrien tmpl = build_template_decl (decl, current_template_parms); 278890075Sobrien new_template_p = 1; 278990075Sobrien 279050397Sobrien if (DECL_LANG_SPECIFIC (decl) 279150397Sobrien && DECL_TEMPLATE_SPECIALIZATION (decl)) 279250397Sobrien { 279350397Sobrien /* A specialization of a member template of a template 2794117395Skan class. */ 279550397Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); 279650397Sobrien DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); 279750397Sobrien DECL_TEMPLATE_INFO (decl) = NULL_TREE; 279850397Sobrien } 279918334Speter } 280018334Speter } 280150397Sobrien else 280250397Sobrien { 280352284Sobrien tree a, t, current, parms; 280452284Sobrien int i; 280550397Sobrien 280650397Sobrien if (TREE_CODE (decl) == TYPE_DECL) 280750397Sobrien { 280852284Sobrien if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) 280952284Sobrien || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) 281052284Sobrien && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) 281152284Sobrien && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) 281252284Sobrien tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); 281350397Sobrien else 281450397Sobrien { 281590075Sobrien error ("`%D' does not declare a template type", decl); 281650397Sobrien return decl; 281750397Sobrien } 281850397Sobrien } 281990075Sobrien else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) 282050397Sobrien { 282190075Sobrien error ("template definition of non-template `%#D'", decl); 282250397Sobrien return decl; 282350397Sobrien } 282450397Sobrien else 282550397Sobrien tmpl = DECL_TI_TEMPLATE (decl); 282650397Sobrien 282752284Sobrien if (is_member_template (tmpl) 282852284Sobrien && DECL_FUNCTION_TEMPLATE_P (tmpl) 282952284Sobrien && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) 283052284Sobrien && DECL_TEMPLATE_SPECIALIZATION (decl)) 283150397Sobrien { 283252284Sobrien tree new_tmpl; 283350397Sobrien 283452284Sobrien /* The declaration is a specialization of a member 283552284Sobrien template, declared outside the class. Therefore, the 283652284Sobrien innermost template arguments will be NULL, so we 283752284Sobrien replace them with the arguments determined by the 283852284Sobrien earlier call to check_explicit_specialization. */ 283952284Sobrien args = DECL_TI_ARGS (decl); 284050397Sobrien 284152284Sobrien new_tmpl 284252284Sobrien = build_template_decl (decl, current_template_parms); 284352284Sobrien DECL_TEMPLATE_RESULT (new_tmpl) = decl; 284452284Sobrien TREE_TYPE (new_tmpl) = TREE_TYPE (decl); 284552284Sobrien DECL_TI_TEMPLATE (decl) = new_tmpl; 284652284Sobrien SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); 284790075Sobrien DECL_TEMPLATE_INFO (new_tmpl) 284890075Sobrien = tree_cons (tmpl, args, NULL_TREE); 284950397Sobrien 285090075Sobrien register_specialization (new_tmpl, 285190075Sobrien most_general_template (tmpl), 285290075Sobrien args); 285352284Sobrien return decl; 285450397Sobrien } 285550397Sobrien 285652284Sobrien /* Make sure the template headers we got make sense. */ 285750397Sobrien 285852284Sobrien parms = DECL_TEMPLATE_PARMS (tmpl); 285952284Sobrien i = TMPL_PARMS_DEPTH (parms); 286052284Sobrien if (TMPL_ARGS_DEPTH (args) != i) 286150397Sobrien { 286290075Sobrien error ("expected %d levels of template parms for `%#D', got %d", 286352284Sobrien i, decl, TMPL_ARGS_DEPTH (args)); 286450397Sobrien } 286552284Sobrien else 286652284Sobrien for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) 286752284Sobrien { 286852284Sobrien a = TMPL_ARGS_LEVEL (args, i); 286952284Sobrien t = INNERMOST_TEMPLATE_PARMS (parms); 287050397Sobrien 287152284Sobrien if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) 287252284Sobrien { 287352284Sobrien if (current == decl) 287490075Sobrien error ("got %d template parameters for `%#D'", 287552284Sobrien TREE_VEC_LENGTH (a), decl); 287652284Sobrien else 287790075Sobrien error ("got %d template parameters for `%#T'", 287852284Sobrien TREE_VEC_LENGTH (a), current); 287990075Sobrien error (" but %d required", TREE_VEC_LENGTH (t)); 288052284Sobrien } 288150397Sobrien 288252284Sobrien /* Perhaps we should also check that the parms are used in the 288352284Sobrien appropriate qualifying scopes in the declarator? */ 288452284Sobrien 288552284Sobrien if (current == decl) 288652284Sobrien current = ctx; 288752284Sobrien else 288852284Sobrien current = TYPE_CONTEXT (current); 288952284Sobrien } 289050397Sobrien } 289150397Sobrien 289250397Sobrien DECL_TEMPLATE_RESULT (tmpl) = decl; 289350397Sobrien TREE_TYPE (tmpl) = TREE_TYPE (decl); 289450397Sobrien 289552284Sobrien /* Push template declarations for global functions and types. Note 289652284Sobrien that we do not try to push a global template friend declared in a 289752284Sobrien template class; such a thing may well depend on the template 289852284Sobrien parameters of the class. */ 289990075Sobrien if (new_template_p && !ctx 290052284Sobrien && !(is_friend && template_class_depth (current_class_type) > 0)) 290150397Sobrien tmpl = pushdecl_namespace_level (tmpl); 290250397Sobrien 290350397Sobrien if (primary) 2904117395Skan { 2905117395Skan DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; 2906117395Skan if (DECL_CONV_FN_P (tmpl)) 2907117395Skan { 2908117395Skan int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 290950397Sobrien 2910117395Skan /* It is a conversion operator. See if the type converted to 2911117395Skan depends on innermost template operands. */ 2912117395Skan 2913117395Skan if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl)), 2914117395Skan template_parm_this_level_p, 2915117395Skan &depth, 2916117395Skan NULL)) 2917117395Skan DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; 2918117395Skan } 2919117395Skan } 2920117395Skan 292190075Sobrien info = tree_cons (tmpl, args, NULL_TREE); 292250397Sobrien 292390075Sobrien if (DECL_IMPLICIT_TYPEDEF_P (decl)) 292450397Sobrien { 292552284Sobrien SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); 292652284Sobrien if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) 292790075Sobrien && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE 292890075Sobrien /* Don't change the name if we've already set it up. */ 292990075Sobrien && !IDENTIFIER_TEMPLATE (DECL_NAME (decl))) 293050397Sobrien DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); 293150397Sobrien } 293290075Sobrien else if (DECL_LANG_SPECIFIC (decl)) 293350397Sobrien DECL_TEMPLATE_INFO (decl) = info; 293450397Sobrien 293550397Sobrien return DECL_TEMPLATE_RESULT (tmpl); 293618334Speter} 293718334Speter 293850397Sobrientree 293950397Sobrienpush_template_decl (decl) 294050397Sobrien tree decl; 294118334Speter{ 294250397Sobrien return push_template_decl_real (decl, 0); 294350397Sobrien} 294418334Speter 294550397Sobrien/* Called when a class template TYPE is redeclared with the indicated 294650397Sobrien template PARMS, e.g.: 294718334Speter 294850397Sobrien template <class T> struct S; 294950397Sobrien template <class T> struct S {}; */ 295018334Speter 295150397Sobrienvoid 295250397Sobrienredeclare_class_template (type, parms) 295350397Sobrien tree type; 295450397Sobrien tree parms; 295550397Sobrien{ 295652284Sobrien tree tmpl; 295750397Sobrien tree tmpl_parms; 295850397Sobrien int i; 295950397Sobrien 296052284Sobrien if (!TYPE_TEMPLATE_INFO (type)) 296152284Sobrien { 296290075Sobrien error ("`%T' is not a template type", type); 296352284Sobrien return; 296452284Sobrien } 296552284Sobrien 296652284Sobrien tmpl = TYPE_TI_TEMPLATE (type); 296750397Sobrien if (!PRIMARY_TEMPLATE_P (tmpl)) 296850397Sobrien /* The type is nested in some template class. Nothing to worry 296950397Sobrien about here; there are no new template parameters for the nested 297050397Sobrien type. */ 297150397Sobrien return; 297250397Sobrien 297350397Sobrien parms = INNERMOST_TEMPLATE_PARMS (parms); 297450397Sobrien tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); 297550397Sobrien 297650397Sobrien if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) 297718334Speter { 297850397Sobrien cp_error_at ("previous declaration `%D'", tmpl); 297990075Sobrien error ("used %d template parameter%s instead of %d", 298050397Sobrien TREE_VEC_LENGTH (tmpl_parms), 298150397Sobrien TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", 298250397Sobrien TREE_VEC_LENGTH (parms)); 298350397Sobrien return; 298418334Speter } 298518334Speter 298650397Sobrien for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) 298718334Speter { 298850397Sobrien tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); 298950397Sobrien tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 299050397Sobrien tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); 299150397Sobrien tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); 299250397Sobrien 299350397Sobrien if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) 299450397Sobrien { 299550397Sobrien cp_error_at ("template parameter `%#D'", tmpl_parm); 299690075Sobrien error ("redeclared here as `%#D'", parm); 299750397Sobrien return; 299850397Sobrien } 299950397Sobrien 300050397Sobrien if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) 300150397Sobrien { 300250397Sobrien /* We have in [temp.param]: 300350397Sobrien 300450397Sobrien A template-parameter may not be given default arguments 300550397Sobrien by two different declarations in the same scope. */ 300690075Sobrien error ("redefinition of default argument for `%#D'", parm); 300750397Sobrien cp_error_at (" original definition appeared here", tmpl_parm); 300850397Sobrien return; 300950397Sobrien } 301050397Sobrien 301150397Sobrien if (parm_default != NULL_TREE) 301250397Sobrien /* Update the previous template parameters (which are the ones 301350397Sobrien that will really count) with the new default value. */ 301450397Sobrien TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; 301590075Sobrien else if (tmpl_default != NULL_TREE) 301690075Sobrien /* Update the new parameters, too; they'll be used as the 301790075Sobrien parameters for any members. */ 301890075Sobrien TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; 301918334Speter } 302050397Sobrien} 302150397Sobrien 302250397Sobrien/* Attempt to convert the non-type template parameter EXPR to the 302350397Sobrien indicated TYPE. If the conversion is successful, return the 302490075Sobrien converted value. If the conversion is unsuccessful, return 302550397Sobrien NULL_TREE if we issued an error message, or error_mark_node if we 302650397Sobrien did not. We issue error messages for out-and-out bad template 302750397Sobrien parameters, but not simply because the conversion failed, since we 302850397Sobrien might be just trying to do argument deduction. By the time this 302950397Sobrien function is called, neither TYPE nor EXPR may make use of template 303050397Sobrien parameters. */ 303150397Sobrien 303250397Sobrienstatic tree 303350397Sobrienconvert_nontype_argument (type, expr) 303450397Sobrien tree type; 303550397Sobrien tree expr; 303650397Sobrien{ 303750397Sobrien tree expr_type = TREE_TYPE (expr); 303850397Sobrien 303950397Sobrien /* A template-argument for a non-type, non-template 304050397Sobrien template-parameter shall be one of: 304150397Sobrien 304250397Sobrien --an integral constant-expression of integral or enumeration 304350397Sobrien type; or 304450397Sobrien 304550397Sobrien --the name of a non-type template-parameter; or 304650397Sobrien 304750397Sobrien --the name of an object or function with external linkage, 304850397Sobrien including function templates and function template-ids but 304950397Sobrien excluding non-static class members, expressed as id-expression; 305050397Sobrien or 305150397Sobrien 305250397Sobrien --the address of an object or function with external linkage, 305350397Sobrien including function templates and function template-ids but 305450397Sobrien excluding non-static class members, expressed as & id-expression 305550397Sobrien where the & is optional if the name refers to a function or 305650397Sobrien array; or 305750397Sobrien 305850397Sobrien --a pointer to member expressed as described in _expr.unary.op_. */ 305950397Sobrien 306090075Sobrien /* An integral constant-expression can include const variables or 306190075Sobrien enumerators. Simplify things by folding them to their values, 306290075Sobrien unless we're about to bind the declaration to a reference 306390075Sobrien parameter. */ 306490075Sobrien if (INTEGRAL_TYPE_P (expr_type) 306590075Sobrien && TREE_CODE (type) != REFERENCE_TYPE) 306650397Sobrien expr = decl_constant_value (expr); 306750397Sobrien 306850397Sobrien if (is_overloaded_fn (expr)) 306950397Sobrien /* OK for now. We'll check that it has external linkage later. 307050397Sobrien Check this first since if expr_type is the unknown_type_node 307150397Sobrien we would otherwise complain below. */ 307250397Sobrien ; 307352284Sobrien else if (TYPE_PTRMEM_P (expr_type) 307452284Sobrien || TYPE_PTRMEMFUNC_P (expr_type)) 307518334Speter { 307652284Sobrien if (TREE_CODE (expr) != PTRMEM_CST) 307752284Sobrien goto bad_argument; 307850397Sobrien } 307952284Sobrien else if (TYPE_PTR_P (expr_type) 308052284Sobrien || TYPE_PTRMEM_P (expr_type) 308152284Sobrien || TREE_CODE (expr_type) == ARRAY_TYPE 308252284Sobrien || TREE_CODE (type) == REFERENCE_TYPE 308350397Sobrien /* If expr is the address of an overloaded function, we 308450397Sobrien will get the unknown_type_node at this point. */ 308550397Sobrien || expr_type == unknown_type_node) 308650397Sobrien { 308750397Sobrien tree referent; 308850397Sobrien tree e = expr; 308950397Sobrien STRIP_NOPS (e); 309018334Speter 309190075Sobrien if (TREE_CODE (expr_type) == ARRAY_TYPE 309290075Sobrien || (TREE_CODE (type) == REFERENCE_TYPE 309390075Sobrien && TREE_CODE (e) != ADDR_EXPR)) 309452284Sobrien referent = e; 309552284Sobrien else 309650397Sobrien { 309752284Sobrien if (TREE_CODE (e) != ADDR_EXPR) 309852284Sobrien { 309952284Sobrien bad_argument: 310090075Sobrien error ("`%E' is not a valid template argument", expr); 310152284Sobrien if (TYPE_PTR_P (expr_type)) 310252284Sobrien { 310352284Sobrien if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) 310490075Sobrien error ("it must be the address of a function with external linkage"); 310552284Sobrien else 310690075Sobrien error ("it must be the address of an object with external linkage"); 310752284Sobrien } 310852284Sobrien else if (TYPE_PTRMEM_P (expr_type) 310952284Sobrien || TYPE_PTRMEMFUNC_P (expr_type)) 311090075Sobrien error ("it must be a pointer-to-member of the form `&X::Y'"); 311152284Sobrien 311252284Sobrien return NULL_TREE; 311352284Sobrien } 311452284Sobrien 311552284Sobrien referent = TREE_OPERAND (e, 0); 311652284Sobrien STRIP_NOPS (referent); 311750397Sobrien } 311818334Speter 311950397Sobrien if (TREE_CODE (referent) == STRING_CST) 312018334Speter { 312190075Sobrien error ("string literal %E is not a valid template argument because it is the address of an object with static linkage", 312250397Sobrien referent); 312350397Sobrien return NULL_TREE; 312418334Speter } 312518334Speter 312650397Sobrien if (is_overloaded_fn (referent)) 312750397Sobrien /* We'll check that it has external linkage later. */ 312850397Sobrien ; 312950397Sobrien else if (TREE_CODE (referent) != VAR_DECL) 313050397Sobrien goto bad_argument; 313190075Sobrien else if (!DECL_EXTERNAL_LINKAGE_P (referent)) 313218334Speter { 313390075Sobrien error ("address of non-extern `%E' cannot be used as template argument", referent); 313450397Sobrien return error_mark_node; 313518334Speter } 313618334Speter } 313752284Sobrien else if (INTEGRAL_TYPE_P (expr_type) 313852284Sobrien || TYPE_PTRMEM_P (expr_type) 313990075Sobrien || TYPE_PTRMEMFUNC_P (expr_type)) 314018334Speter { 314152284Sobrien if (! TREE_CONSTANT (expr)) 314252284Sobrien { 314352284Sobrien non_constant: 314490075Sobrien error ("non-constant `%E' cannot be used as template argument", 314552284Sobrien expr); 314652284Sobrien return NULL_TREE; 314752284Sobrien } 314818334Speter } 314950397Sobrien else 315018334Speter { 315190075Sobrien error ("object `%E' cannot be used as template argument", expr); 315250397Sobrien return NULL_TREE; 315318334Speter } 315418334Speter 315550397Sobrien switch (TREE_CODE (type)) 315650397Sobrien { 315750397Sobrien case INTEGER_TYPE: 315850397Sobrien case BOOLEAN_TYPE: 315950397Sobrien case ENUMERAL_TYPE: 316050397Sobrien /* For a non-type template-parameter of integral or enumeration 316150397Sobrien type, integral promotions (_conv.prom_) and integral 3162117395Skan conversions (_conv.integral_) are applied. */ 316350397Sobrien if (!INTEGRAL_TYPE_P (expr_type)) 316450397Sobrien return error_mark_node; 316550397Sobrien 316650397Sobrien /* It's safe to call digest_init in this case; we know we're 316750397Sobrien just converting one integral constant expression to another. */ 316850397Sobrien expr = digest_init (type, expr, (tree*) 0); 316950397Sobrien 317050397Sobrien if (TREE_CODE (expr) != INTEGER_CST) 317152284Sobrien /* Curiously, some TREE_CONSTANT integral expressions do not 317250397Sobrien simplify to integer constants. For example, `3 % 0', 317350397Sobrien remains a TRUNC_MOD_EXPR. */ 317450397Sobrien goto non_constant; 317550397Sobrien 317650397Sobrien return expr; 317750397Sobrien 317850397Sobrien case POINTER_TYPE: 317950397Sobrien { 318050397Sobrien tree type_pointed_to = TREE_TYPE (type); 318150397Sobrien 318250397Sobrien if (TYPE_PTRMEM_P (type)) 318352284Sobrien { 318452284Sobrien tree e; 318552284Sobrien 318652284Sobrien /* For a non-type template-parameter of type pointer to data 318752284Sobrien member, qualification conversions (_conv.qual_) are 318852284Sobrien applied. */ 318952284Sobrien e = perform_qualification_conversions (type, expr); 319052284Sobrien if (TREE_CODE (e) == NOP_EXPR) 319152284Sobrien /* The call to perform_qualification_conversions will 319252284Sobrien insert a NOP_EXPR over EXPR to do express conversion, 319352284Sobrien if necessary. But, that will confuse us if we use 319452284Sobrien this (converted) template parameter to instantiate 319552284Sobrien another template; then the thing will not look like a 319652284Sobrien valid template argument. So, just make a new 319752284Sobrien constant, of the appropriate type. */ 319852284Sobrien e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); 319952284Sobrien return e; 320052284Sobrien } 320150397Sobrien else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) 320250397Sobrien { 320350397Sobrien /* For a non-type template-parameter of type pointer to 320450397Sobrien function, only the function-to-pointer conversion 320550397Sobrien (_conv.func_) is applied. If the template-argument 320650397Sobrien represents a set of overloaded functions (or a pointer to 320750397Sobrien such), the matching function is selected from the set 320850397Sobrien (_over.over_). */ 320950397Sobrien tree fns; 321050397Sobrien tree fn; 321150397Sobrien 321250397Sobrien if (TREE_CODE (expr) == ADDR_EXPR) 321350397Sobrien fns = TREE_OPERAND (expr, 0); 321450397Sobrien else 321550397Sobrien fns = expr; 321650397Sobrien 321796263Sobrien fn = instantiate_type (type_pointed_to, fns, tf_none); 321850397Sobrien 321950397Sobrien if (fn == error_mark_node) 322050397Sobrien return error_mark_node; 322150397Sobrien 322290075Sobrien if (!DECL_EXTERNAL_LINKAGE_P (fn)) 322350397Sobrien { 322450397Sobrien if (really_overloaded_fn (fns)) 322550397Sobrien return error_mark_node; 322650397Sobrien else 322750397Sobrien goto bad_argument; 322850397Sobrien } 322950397Sobrien 323050397Sobrien expr = build_unary_op (ADDR_EXPR, fn, 0); 323150397Sobrien 323252284Sobrien my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), 323350397Sobrien 0); 323450397Sobrien return expr; 323550397Sobrien } 323650397Sobrien else 323750397Sobrien { 323850397Sobrien /* For a non-type template-parameter of type pointer to 323950397Sobrien object, qualification conversions (_conv.qual_) and the 324050397Sobrien array-to-pointer conversion (_conv.array_) are applied. 324150397Sobrien [Note: In particular, neither the null pointer conversion 324250397Sobrien (_conv.ptr_) nor the derived-to-base conversion 324350397Sobrien (_conv.ptr_) are applied. Although 0 is a valid 324450397Sobrien template-argument for a non-type template-parameter of 324550397Sobrien integral type, it is not a valid template-argument for a 324650397Sobrien non-type template-parameter of pointer type.] 324750397Sobrien 324850397Sobrien The call to decay_conversion performs the 324950397Sobrien array-to-pointer conversion, if appropriate. */ 325050397Sobrien expr = decay_conversion (expr); 325150397Sobrien 325250397Sobrien if (expr == error_mark_node) 325350397Sobrien return error_mark_node; 325450397Sobrien else 325550397Sobrien return perform_qualification_conversions (type, expr); 325650397Sobrien } 325750397Sobrien } 325850397Sobrien break; 325950397Sobrien 326050397Sobrien case REFERENCE_TYPE: 326150397Sobrien { 326250397Sobrien tree type_referred_to = TREE_TYPE (type); 326350397Sobrien 326490075Sobrien /* If this expression already has reference type, get the 326590075Sobrien underling object. */ 326690075Sobrien if (TREE_CODE (expr_type) == REFERENCE_TYPE) 326790075Sobrien { 326890075Sobrien my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604); 326990075Sobrien expr = TREE_OPERAND (expr, 0); 327090075Sobrien expr_type = TREE_TYPE (expr); 327190075Sobrien } 327290075Sobrien 327350397Sobrien if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) 327450397Sobrien { 327550397Sobrien /* For a non-type template-parameter of type reference to 327652284Sobrien function, no conversions apply. If the 327752284Sobrien template-argument represents a set of overloaded 327852284Sobrien functions, the matching function is selected from the 327952284Sobrien set (_over.over_). */ 328050397Sobrien tree fn; 328150397Sobrien 328296263Sobrien fn = instantiate_type (type_referred_to, expr, tf_none); 328350397Sobrien 328452284Sobrien if (fn == error_mark_node) 328552284Sobrien return error_mark_node; 328652284Sobrien 328790075Sobrien if (!DECL_EXTERNAL_LINKAGE_P (fn)) 328850397Sobrien { 328990075Sobrien if (really_overloaded_fn (expr)) 329050397Sobrien /* Don't issue an error here; we might get a different 329150397Sobrien function if the overloading had worked out 329250397Sobrien differently. */ 329350397Sobrien return error_mark_node; 329450397Sobrien else 329550397Sobrien goto bad_argument; 329650397Sobrien } 329750397Sobrien 329852284Sobrien my_friendly_assert (same_type_p (type_referred_to, 329952284Sobrien TREE_TYPE (fn)), 330050397Sobrien 0); 330150397Sobrien 330290075Sobrien expr = fn; 330350397Sobrien } 330450397Sobrien else 330550397Sobrien { 330650397Sobrien /* For a non-type template-parameter of type reference to 330750397Sobrien object, no conversions apply. The type referred to by the 330850397Sobrien reference may be more cv-qualified than the (otherwise 330950397Sobrien identical) type of the template-argument. The 331050397Sobrien template-parameter is bound directly to the 331150397Sobrien template-argument, which must be an lvalue. */ 331290075Sobrien if (!same_type_p (TYPE_MAIN_VARIANT (expr_type), 331390075Sobrien TYPE_MAIN_VARIANT (type_referred_to)) 331452284Sobrien || !at_least_as_qualified_p (type_referred_to, 331552284Sobrien expr_type) 331650397Sobrien || !real_lvalue_p (expr)) 331750397Sobrien return error_mark_node; 331850397Sobrien } 331990075Sobrien 3320117395Skan cxx_mark_addressable (expr); 332190075Sobrien return build1 (ADDR_EXPR, type, expr); 332250397Sobrien } 332350397Sobrien break; 332450397Sobrien 332550397Sobrien case RECORD_TYPE: 332650397Sobrien { 332790075Sobrien my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112); 332850397Sobrien 332950397Sobrien /* For a non-type template-parameter of type pointer to member 333050397Sobrien function, no conversions apply. If the template-argument 333150397Sobrien represents a set of overloaded member functions, the 333250397Sobrien matching member function is selected from the set 333350397Sobrien (_over.over_). */ 333450397Sobrien 333550397Sobrien if (!TYPE_PTRMEMFUNC_P (expr_type) && 333650397Sobrien expr_type != unknown_type_node) 333750397Sobrien return error_mark_node; 333850397Sobrien 333952284Sobrien if (TREE_CODE (expr) == PTRMEM_CST) 334050397Sobrien { 334150397Sobrien /* A ptr-to-member constant. */ 334252284Sobrien if (!same_type_p (type, expr_type)) 334350397Sobrien return error_mark_node; 334450397Sobrien else 334550397Sobrien return expr; 334650397Sobrien } 334750397Sobrien 334850397Sobrien if (TREE_CODE (expr) != ADDR_EXPR) 334950397Sobrien return error_mark_node; 335050397Sobrien 335196263Sobrien expr = instantiate_type (type, expr, tf_none); 335250397Sobrien 335352284Sobrien if (expr == error_mark_node) 335450397Sobrien return error_mark_node; 335550397Sobrien 335652284Sobrien my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), 335750397Sobrien 0); 335850397Sobrien return expr; 335950397Sobrien } 336050397Sobrien break; 336150397Sobrien 336250397Sobrien default: 336350397Sobrien /* All non-type parameters must have one of these types. */ 336490075Sobrien abort (); 336550397Sobrien break; 336650397Sobrien } 336750397Sobrien 336850397Sobrien return error_mark_node; 336918334Speter} 337018334Speter 337150397Sobrien/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for 337250397Sobrien template template parameters. Both PARM_PARMS and ARG_PARMS are 337350397Sobrien vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL 337450397Sobrien or PARM_DECL. 337550397Sobrien 337650397Sobrien ARG_PARMS may contain more parameters than PARM_PARMS. If this is 337750397Sobrien the case, then extra parameters must have default arguments. 337818334Speter 337950397Sobrien Consider the example: 338050397Sobrien template <class T, class Allocator = allocator> class vector; 338150397Sobrien template<template <class U> class TT> class C; 338250397Sobrien 338350397Sobrien C<vector> is a valid instantiation. PARM_PARMS for the above code 338450397Sobrien contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for 338550397Sobrien T and Allocator) and OUTER_ARGS contains the argument that is used to 338650397Sobrien substitute the TT parameter. */ 338750397Sobrien 338850397Sobrienstatic int 338952284Sobriencoerce_template_template_parms (parm_parms, arg_parms, complain, 339052284Sobrien in_decl, outer_args) 339152284Sobrien tree parm_parms, arg_parms; 339296263Sobrien tsubst_flags_t complain; 339352284Sobrien tree in_decl, outer_args; 339450397Sobrien{ 339550397Sobrien int nparms, nargs, i; 339650397Sobrien tree parm, arg; 339750397Sobrien 339850397Sobrien my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); 339950397Sobrien my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); 340050397Sobrien 340150397Sobrien nparms = TREE_VEC_LENGTH (parm_parms); 340250397Sobrien nargs = TREE_VEC_LENGTH (arg_parms); 340350397Sobrien 340450397Sobrien /* The rule here is opposite of coerce_template_parms. */ 340550397Sobrien if (nargs < nparms 340650397Sobrien || (nargs > nparms 340750397Sobrien && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) 340850397Sobrien return 0; 340950397Sobrien 341050397Sobrien for (i = 0; i < nparms; ++i) 341150397Sobrien { 341250397Sobrien parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); 341350397Sobrien arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); 341450397Sobrien 341550397Sobrien if (arg == NULL_TREE || arg == error_mark_node 341650397Sobrien || parm == NULL_TREE || parm == error_mark_node) 341750397Sobrien return 0; 341850397Sobrien 341950397Sobrien if (TREE_CODE (arg) != TREE_CODE (parm)) 342050397Sobrien return 0; 342150397Sobrien 342250397Sobrien switch (TREE_CODE (parm)) 342350397Sobrien { 342450397Sobrien case TYPE_DECL: 342550397Sobrien break; 342650397Sobrien 342750397Sobrien case TEMPLATE_DECL: 342850397Sobrien /* We encounter instantiations of templates like 342950397Sobrien template <template <template <class> class> class TT> 343050397Sobrien class C; */ 343152284Sobrien { 343252284Sobrien tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); 343352284Sobrien tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); 343450397Sobrien 343552284Sobrien if (!coerce_template_template_parms (parmparm, argparm, 343652284Sobrien complain, in_decl, 343752284Sobrien outer_args)) 343852284Sobrien return 0; 343952284Sobrien } 344052284Sobrien break; 344152284Sobrien 344250397Sobrien case PARM_DECL: 344350397Sobrien /* The tsubst call is used to handle cases such as 344450397Sobrien template <class T, template <T> class TT> class D; 344550397Sobrien i.e. the parameter list of TT depends on earlier parameters. */ 344652284Sobrien if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, 344752284Sobrien complain, in_decl), 344852284Sobrien TREE_TYPE (arg))) 344950397Sobrien return 0; 345050397Sobrien break; 345150397Sobrien 345250397Sobrien default: 345390075Sobrien abort (); 345450397Sobrien } 345550397Sobrien } 345650397Sobrien return 1; 345750397Sobrien} 345850397Sobrien 345952284Sobrien/* Convert the indicated template ARG as necessary to match the 346052284Sobrien indicated template PARM. Returns the converted ARG, or 346196263Sobrien error_mark_node if the conversion was unsuccessful. Error and 346296263Sobrien warning messages are issued under control of COMPLAIN. This 346396263Sobrien conversion is for the Ith parameter in the parameter list. ARGS is 346496263Sobrien the full set of template arguments deduced so far. */ 346550397Sobrien 346652284Sobrienstatic tree 346752284Sobrienconvert_template_argument (parm, arg, args, complain, i, in_decl) 346852284Sobrien tree parm; 346952284Sobrien tree arg; 347052284Sobrien tree args; 347196263Sobrien tsubst_flags_t complain; 347252284Sobrien int i; 347352284Sobrien tree in_decl; 347452284Sobrien{ 347552284Sobrien tree val; 347652284Sobrien tree inner_args; 347752284Sobrien int is_type, requires_type, is_tmpl_type, requires_tmpl_type; 347852284Sobrien 347990075Sobrien inner_args = INNERMOST_TEMPLATE_ARGS (args); 348052284Sobrien 348152284Sobrien if (TREE_CODE (arg) == TREE_LIST 348252284Sobrien && TREE_TYPE (arg) != NULL_TREE 348352284Sobrien && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) 348452284Sobrien { 348552284Sobrien /* The template argument was the name of some 348652284Sobrien member function. That's usually 3487117395Skan invalid, but static members are OK. In any 348852284Sobrien case, grab the underlying fields/functions 348952284Sobrien and issue an error later if required. */ 349052284Sobrien arg = TREE_VALUE (arg); 349152284Sobrien TREE_TYPE (arg) = unknown_type_node; 349252284Sobrien } 349352284Sobrien 349452284Sobrien requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; 349552284Sobrien requires_type = (TREE_CODE (parm) == TYPE_DECL 349652284Sobrien || requires_tmpl_type); 349752284Sobrien 349890075Sobrien if (TREE_CODE (arg) != RECORD_TYPE) 349990075Sobrien is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL 350090075Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) 350190075Sobrien || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 350290075Sobrien || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); 3503110611Skan else if (CLASSTYPE_IS_TEMPLATE (arg) 3504110611Skan && is_base_of_enclosing_class (arg, current_class_type)) 3505110611Skan /* This is a template name used within the scope of the 3506110611Skan template. It could be the template, or it could be the 3507110611Skan instantiation. Choose whichever makes sense. */ 3508110611Skan is_tmpl_type = requires_tmpl_type; 350990075Sobrien else 351090075Sobrien /* It is a non-template class, or a specialization of a template 351190075Sobrien class, or a non-template member of a template class. */ 351290075Sobrien is_tmpl_type = 0; 351390075Sobrien 351490075Sobrien if (is_tmpl_type 351590075Sobrien && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 351690075Sobrien || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) 351752284Sobrien arg = TYPE_STUB_DECL (arg); 351852284Sobrien else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) 351952284Sobrien arg = CLASSTYPE_TI_TEMPLATE (arg); 352052284Sobrien 352190075Sobrien is_type = TYPE_P (arg) || is_tmpl_type; 352252284Sobrien 352352284Sobrien if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF 352452284Sobrien && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) 352552284Sobrien { 352690075Sobrien pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg); 352752284Sobrien 352852284Sobrien arg = make_typename_type (TREE_OPERAND (arg, 0), 352990075Sobrien TREE_OPERAND (arg, 1), 353096263Sobrien complain & tf_error); 353152284Sobrien is_type = 1; 353252284Sobrien } 353352284Sobrien if (is_type != requires_type) 353452284Sobrien { 353552284Sobrien if (in_decl) 353652284Sobrien { 353796263Sobrien if (complain & tf_error) 353852284Sobrien { 353990075Sobrien error ("type/value mismatch at argument %d in template parameter list for `%D'", 354052284Sobrien i + 1, in_decl); 354152284Sobrien if (is_type) 354290075Sobrien error (" expected a constant of type `%T', got `%T'", 354352284Sobrien TREE_TYPE (parm), 354452284Sobrien (is_tmpl_type ? DECL_NAME (arg) : arg)); 354552284Sobrien else 354690075Sobrien error (" expected a type, got `%E'", arg); 354752284Sobrien } 354852284Sobrien } 354952284Sobrien return error_mark_node; 355052284Sobrien } 355152284Sobrien if (is_tmpl_type ^ requires_tmpl_type) 355252284Sobrien { 355396263Sobrien if (in_decl && (complain & tf_error)) 355452284Sobrien { 355590075Sobrien error ("type/value mismatch at argument %d in template parameter list for `%D'", 355652284Sobrien i + 1, in_decl); 355752284Sobrien if (is_tmpl_type) 355890075Sobrien error (" expected a type, got `%T'", DECL_NAME (arg)); 355952284Sobrien else 356090075Sobrien error (" expected a class template, got `%T'", arg); 356152284Sobrien } 356252284Sobrien return error_mark_node; 356352284Sobrien } 356452284Sobrien 356552284Sobrien if (is_type) 356652284Sobrien { 356752284Sobrien if (requires_tmpl_type) 356852284Sobrien { 356990075Sobrien if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) 357090075Sobrien /* The number of argument required is not known yet. 357190075Sobrien Just accept it for now. */ 357290075Sobrien val = TREE_TYPE (arg); 357352284Sobrien else 357452284Sobrien { 357590075Sobrien tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); 357690075Sobrien tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); 357790075Sobrien 357890075Sobrien if (coerce_template_template_parms (parmparm, argparm, 357990075Sobrien complain, in_decl, 358090075Sobrien inner_args)) 358152284Sobrien { 358290075Sobrien val = arg; 358390075Sobrien 358490075Sobrien /* TEMPLATE_TEMPLATE_PARM node is preferred over 358590075Sobrien TEMPLATE_DECL. */ 358690075Sobrien if (val != error_mark_node 358790075Sobrien && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) 358890075Sobrien val = TREE_TYPE (val); 358952284Sobrien } 359090075Sobrien else 359190075Sobrien { 359296263Sobrien if (in_decl && (complain & tf_error)) 359390075Sobrien { 359490075Sobrien error ("type/value mismatch at argument %d in template parameter list for `%D'", 359590075Sobrien i + 1, in_decl); 359690075Sobrien error (" expected a template of type `%D', got `%D'", parm, arg); 359790075Sobrien } 359852284Sobrien 359990075Sobrien val = error_mark_node; 360090075Sobrien } 360152284Sobrien } 360252284Sobrien } 360352284Sobrien else 360452284Sobrien { 360552284Sobrien val = groktypename (arg); 360652284Sobrien if (! processing_template_decl) 360752284Sobrien { 360852284Sobrien /* [basic.link]: A name with no linkage (notably, the 360952284Sobrien name of a class or enumeration declared in a local 361052284Sobrien scope) shall not be used to declare an entity with 361152284Sobrien linkage. This implies that names with no linkage 361252284Sobrien cannot be used as template arguments. */ 361352284Sobrien tree t = no_linkage_check (val); 361452284Sobrien if (t) 361552284Sobrien { 361690075Sobrien if (TYPE_ANONYMOUS_P (t)) 361790075Sobrien pedwarn 361852284Sobrien ("template-argument `%T' uses anonymous type", val); 361952284Sobrien else 362090075Sobrien error 362152284Sobrien ("template-argument `%T' uses local type `%T'", 362252284Sobrien val, t); 362352284Sobrien return error_mark_node; 362452284Sobrien } 3625107590Sobrien 3626107590Sobrien /* In order to avoid all sorts of complications, we do 3627107590Sobrien not allow variably-modified types as template 3628107590Sobrien arguments. */ 3629107590Sobrien if (variably_modified_type_p (val)) 3630107590Sobrien { 3631107590Sobrien error ("template-argument `%T' is a variably modified type", 3632107590Sobrien val); 3633107590Sobrien return error_mark_node; 3634107590Sobrien } 363552284Sobrien } 363652284Sobrien } 363752284Sobrien } 363852284Sobrien else 363952284Sobrien { 364052284Sobrien tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); 364152284Sobrien 364290075Sobrien if (invalid_nontype_parm_type_p (t, complain)) 364390075Sobrien return error_mark_node; 364490075Sobrien 364552284Sobrien if (processing_template_decl) 364652284Sobrien arg = maybe_fold_nontype_arg (arg); 364752284Sobrien 364852284Sobrien if (!uses_template_parms (arg) && !uses_template_parms (t)) 364952284Sobrien /* We used to call digest_init here. However, digest_init 365052284Sobrien will report errors, which we don't want when complain 365152284Sobrien is zero. More importantly, digest_init will try too 365252284Sobrien hard to convert things: for example, `0' should not be 365352284Sobrien converted to pointer type at this point according to 365452284Sobrien the standard. Accepting this is not merely an 365552284Sobrien extension, since deciding whether or not these 365652284Sobrien conversions can occur is part of determining which 365790075Sobrien function template to call, or whether a given explicit 3658117395Skan argument specification is valid. */ 365952284Sobrien val = convert_nontype_argument (t, arg); 366052284Sobrien else 366152284Sobrien val = arg; 366252284Sobrien 366352284Sobrien if (val == NULL_TREE) 366452284Sobrien val = error_mark_node; 366596263Sobrien else if (val == error_mark_node && (complain & tf_error)) 366690075Sobrien error ("could not convert template argument `%E' to `%T'", 366752284Sobrien arg, t); 366852284Sobrien } 366952284Sobrien 367052284Sobrien return val; 367152284Sobrien} 367252284Sobrien 367352284Sobrien/* Convert all template arguments to their appropriate types, and 367452284Sobrien return a vector containing the innermost resulting template 367596263Sobrien arguments. If any error occurs, return error_mark_node. Error and 367696263Sobrien warning messages are issued under control of COMPLAIN. Some error 367796263Sobrien messages are issued even if COMPLAIN is zero; for instance, if a 367896263Sobrien template argument is composed from a local class. 367952284Sobrien 3680117395Skan If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be 368150397Sobrien provided in ARGLIST, or else trailing parameters must have default 368250397Sobrien values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument 368390075Sobrien deduction for any unspecified trailing arguments. */ 368450397Sobrien 368518334Speterstatic tree 368652284Sobriencoerce_template_parms (parms, args, in_decl, 368750397Sobrien complain, 368850397Sobrien require_all_arguments) 368952284Sobrien tree parms, args; 369018334Speter tree in_decl; 369196263Sobrien tsubst_flags_t complain; 369250397Sobrien int require_all_arguments; 369318334Speter{ 369418334Speter int nparms, nargs, i, lost = 0; 369552284Sobrien tree inner_args; 369652284Sobrien tree new_args; 369752284Sobrien tree new_inner_args; 369818334Speter 369990075Sobrien inner_args = INNERMOST_TEMPLATE_ARGS (args); 370052284Sobrien nargs = NUM_TMPL_ARGS (inner_args); 370118334Speter nparms = TREE_VEC_LENGTH (parms); 370218334Speter 370318334Speter if (nargs > nparms 370418334Speter || (nargs < nparms 370550397Sobrien && require_all_arguments 370618334Speter && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) 370718334Speter { 370896263Sobrien if (complain & tf_error) 370950397Sobrien { 371090075Sobrien error ("wrong number of template arguments (%d, should be %d)", 371152284Sobrien nargs, nparms); 371250397Sobrien 371350397Sobrien if (in_decl) 371452284Sobrien cp_error_at ("provided for `%D'", in_decl); 371550397Sobrien } 371650397Sobrien 371718334Speter return error_mark_node; 371818334Speter } 371918334Speter 372090075Sobrien new_inner_args = make_tree_vec (nparms); 372152284Sobrien new_args = add_outermost_template_args (args, new_inner_args); 372252284Sobrien for (i = 0; i < nparms; i++) 372318334Speter { 372452284Sobrien tree arg; 372552284Sobrien tree parm; 372650397Sobrien 372752284Sobrien /* Get the Ith template parameter. */ 372852284Sobrien parm = TREE_VEC_ELT (parms, i); 372952284Sobrien 373052284Sobrien /* Calculate the Ith argument. */ 373152284Sobrien if (inner_args && TREE_CODE (inner_args) == TREE_LIST) 373218334Speter { 373352284Sobrien arg = TREE_VALUE (inner_args); 373452284Sobrien inner_args = TREE_CHAIN (inner_args); 373518334Speter } 373652284Sobrien else if (i < nargs) 373752284Sobrien arg = TREE_VEC_ELT (inner_args, i); 373852284Sobrien /* If no template argument was supplied, look for a default 373952284Sobrien value. */ 374052284Sobrien else if (TREE_PURPOSE (parm) == NULL_TREE) 374152284Sobrien { 374252284Sobrien /* There was no default value. */ 374352284Sobrien my_friendly_assert (!require_all_arguments, 0); 374452284Sobrien break; 374552284Sobrien } 374652284Sobrien else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) 374752284Sobrien arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); 374852284Sobrien else 374952284Sobrien arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, 375052284Sobrien in_decl); 375118334Speter 375252284Sobrien /* Now, convert the Ith argument, as necessary. */ 375350397Sobrien if (arg == NULL_TREE) 375450397Sobrien /* We're out of arguments. */ 375550397Sobrien { 375650397Sobrien my_friendly_assert (!require_all_arguments, 0); 375750397Sobrien break; 375850397Sobrien } 375952284Sobrien else if (arg == error_mark_node) 376050397Sobrien { 376190075Sobrien error ("template argument %d is invalid", i + 1); 376252284Sobrien arg = error_mark_node; 376350397Sobrien } 376452284Sobrien else 376552284Sobrien arg = convert_template_argument (TREE_VALUE (parm), 376652284Sobrien arg, new_args, complain, i, 376752284Sobrien in_decl); 376852284Sobrien 376952284Sobrien if (arg == error_mark_node) 377018334Speter lost++; 377152284Sobrien TREE_VEC_ELT (new_inner_args, i) = arg; 377252284Sobrien } 377318334Speter 377418334Speter if (lost) 377518334Speter return error_mark_node; 377652284Sobrien 377752284Sobrien return new_inner_args; 377818334Speter} 377918334Speter 378052284Sobrien/* Returns 1 if template args OT and NT are equivalent. */ 378152284Sobrien 378252284Sobrienstatic int 378352284Sobrientemplate_args_equal (ot, nt) 378452284Sobrien tree ot, nt; 378552284Sobrien{ 378652284Sobrien if (nt == ot) 378752284Sobrien return 1; 378890075Sobrien 378952284Sobrien if (TREE_CODE (nt) == TREE_VEC) 379052284Sobrien /* For member templates */ 379190075Sobrien return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); 379290075Sobrien else if (TYPE_P (nt)) 379390075Sobrien return TYPE_P (ot) && same_type_p (ot, nt); 379490075Sobrien else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) 379590075Sobrien return 0; 379652284Sobrien else 379752284Sobrien return (cp_tree_equal (ot, nt) > 0); 379852284Sobrien} 379952284Sobrien 380052284Sobrien/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets 380150397Sobrien of template arguments. Returns 0 otherwise. */ 380250397Sobrien 380350397Sobrienint 380450397Sobriencomp_template_args (oldargs, newargs) 380550397Sobrien tree oldargs, newargs; 380650397Sobrien{ 380750397Sobrien int i; 380850397Sobrien 380950397Sobrien if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) 381050397Sobrien return 0; 381150397Sobrien 381250397Sobrien for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) 381350397Sobrien { 381450397Sobrien tree nt = TREE_VEC_ELT (newargs, i); 381550397Sobrien tree ot = TREE_VEC_ELT (oldargs, i); 381650397Sobrien 381752284Sobrien if (! template_args_equal (ot, nt)) 381850397Sobrien return 0; 381950397Sobrien } 382050397Sobrien return 1; 382150397Sobrien} 382250397Sobrien 382318334Speter/* Given class template name and parameter list, produce a user-friendly name 382418334Speter for the instantiation. */ 382550397Sobrien 382618334Speterstatic char * 382752284Sobrienmangle_class_name_for_template (name, parms, arglist) 382890075Sobrien const char *name; 382918334Speter tree parms, arglist; 383018334Speter{ 383118334Speter static struct obstack scratch_obstack; 383218334Speter static char *scratch_firstobj; 383318334Speter int i, nparms; 383418334Speter 383518334Speter if (!scratch_firstobj) 383650397Sobrien gcc_obstack_init (&scratch_obstack); 383718334Speter else 383818334Speter obstack_free (&scratch_obstack, scratch_firstobj); 383950397Sobrien scratch_firstobj = obstack_alloc (&scratch_obstack, 1); 384018334Speter 384190075Sobrien#define ccat(C) obstack_1grow (&scratch_obstack, (C)); 384290075Sobrien#define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S)) 384318334Speter 384418334Speter cat (name); 384518334Speter ccat ('<'); 384618334Speter nparms = TREE_VEC_LENGTH (parms); 384790075Sobrien arglist = INNERMOST_TEMPLATE_ARGS (arglist); 384818334Speter my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); 384918334Speter for (i = 0; i < nparms; i++) 385018334Speter { 385118334Speter tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); 385218334Speter tree arg = TREE_VEC_ELT (arglist, i); 385318334Speter 385418334Speter if (i) 385518334Speter ccat (','); 385618334Speter 385718334Speter if (TREE_CODE (parm) == TYPE_DECL) 385818334Speter { 385990075Sobrien cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); 386018334Speter continue; 386118334Speter } 386250397Sobrien else if (TREE_CODE (parm) == TEMPLATE_DECL) 386350397Sobrien { 386450397Sobrien if (TREE_CODE (arg) == TEMPLATE_DECL) 386550397Sobrien { 386650397Sobrien /* Already substituted with real template. Just output 386750397Sobrien the template name here */ 386850397Sobrien tree context = DECL_CONTEXT (arg); 386990075Sobrien if (context) 387090075Sobrien { 387190075Sobrien /* The template may be defined in a namespace, or 387290075Sobrien may be a member template. */ 387390075Sobrien my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL 387490075Sobrien || CLASS_TYPE_P (context), 387590075Sobrien 980422); 387690075Sobrien cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER)); 387750397Sobrien cat("::"); 387850397Sobrien } 387950397Sobrien cat (IDENTIFIER_POINTER (DECL_NAME (arg))); 388050397Sobrien } 388150397Sobrien else 388250397Sobrien /* Output the parameter declaration */ 388390075Sobrien cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); 388450397Sobrien continue; 388550397Sobrien } 388618334Speter else 388718334Speter my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); 388818334Speter 388918334Speter if (TREE_CODE (arg) == TREE_LIST) 389018334Speter { 389118334Speter /* New list cell was built because old chain link was in 389218334Speter use. */ 389318334Speter my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); 389418334Speter arg = TREE_VALUE (arg); 389518334Speter } 389618334Speter /* No need to check arglist against parmlist here; we did that 389718334Speter in coerce_template_parms, called from lookup_template_class. */ 389890075Sobrien cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER)); 389918334Speter } 390018334Speter { 390118334Speter char *bufp = obstack_next_free (&scratch_obstack); 390218334Speter int offset = 0; 390318334Speter while (bufp[offset - 1] == ' ') 390418334Speter offset--; 390518334Speter obstack_blank_fast (&scratch_obstack, offset); 390618334Speter 390718334Speter /* B<C<char> >, not B<C<char>> */ 390818334Speter if (bufp[offset - 1] == '>') 390918334Speter ccat (' '); 391018334Speter } 391118334Speter ccat ('>'); 391218334Speter ccat ('\0'); 391318334Speter return (char *) obstack_base (&scratch_obstack); 391418334Speter} 391518334Speter 391650397Sobrienstatic tree 391750397Sobrienclasstype_mangled_name (t) 391850397Sobrien tree t; 391950397Sobrien{ 392050397Sobrien if (CLASSTYPE_TEMPLATE_INFO (t) 392152284Sobrien /* Specializations have already had their names set up in 392252284Sobrien lookup_template_class. */ 392352284Sobrien && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) 392450397Sobrien { 392552284Sobrien tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); 392652284Sobrien 392752284Sobrien /* For non-primary templates, the template parameters are 392852284Sobrien implicit from their surrounding context. */ 392952284Sobrien if (PRIMARY_TEMPLATE_P (tmpl)) 393052284Sobrien { 393152284Sobrien tree name = DECL_NAME (tmpl); 393252284Sobrien char *mangled_name = mangle_class_name_for_template 393352284Sobrien (IDENTIFIER_POINTER (name), 393452284Sobrien DECL_INNERMOST_TEMPLATE_PARMS (tmpl), 393552284Sobrien CLASSTYPE_TI_ARGS (t)); 393652284Sobrien tree id = get_identifier (mangled_name); 393752284Sobrien IDENTIFIER_TEMPLATE (id) = name; 393852284Sobrien return id; 393952284Sobrien } 394050397Sobrien } 394152284Sobrien 394252284Sobrien return TYPE_IDENTIFIER (t); 394350397Sobrien} 394450397Sobrien 394550397Sobrienstatic void 394650397Sobrienadd_pending_template (d) 394750397Sobrien tree d; 394850397Sobrien{ 394990075Sobrien tree ti = (TYPE_P (d) 395090075Sobrien ? CLASSTYPE_TEMPLATE_INFO (d) 395190075Sobrien : DECL_TEMPLATE_INFO (d)); 395290075Sobrien tree pt; 395390075Sobrien int level; 395450397Sobrien 395550397Sobrien if (TI_PENDING_TEMPLATE_FLAG (ti)) 395650397Sobrien return; 395750397Sobrien 395890075Sobrien /* We are called both from instantiate_decl, where we've already had a 395990075Sobrien tinst_level pushed, and instantiate_template, where we haven't. 396090075Sobrien Compensate. */ 396190075Sobrien level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d); 396290075Sobrien 396390075Sobrien if (level) 396490075Sobrien push_tinst_level (d); 396590075Sobrien 396690075Sobrien pt = tree_cons (current_tinst_level, d, NULL_TREE); 396790075Sobrien if (last_pending_template) 396890075Sobrien TREE_CHAIN (last_pending_template) = pt; 396990075Sobrien else 397090075Sobrien pending_templates = pt; 397190075Sobrien 397290075Sobrien last_pending_template = pt; 397390075Sobrien 397450397Sobrien TI_PENDING_TEMPLATE_FLAG (ti) = 1; 397590075Sobrien 397690075Sobrien if (level) 397790075Sobrien pop_tinst_level (); 397850397Sobrien} 397950397Sobrien 398050397Sobrien 3981117395Skan/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and 3982117395Skan ARGLIST. Valid choices for FNS are given in the cp-tree.def 3983117395Skan documentation for TEMPLATE_ID_EXPR. */ 398450397Sobrien 398550397Sobrientree 398650397Sobrienlookup_template_function (fns, arglist) 398750397Sobrien tree fns, arglist; 398850397Sobrien{ 398950397Sobrien tree type; 399050397Sobrien 3991117395Skan if (fns == error_mark_node || arglist == error_mark_node) 3992117395Skan return error_mark_node; 3993117395Skan 3994122180Skan if (fns == NULL_TREE 3995122180Skan || TREE_CODE (fns) == FUNCTION_DECL) 399650397Sobrien { 399790075Sobrien error ("non-template used as template"); 399850397Sobrien return error_mark_node; 399950397Sobrien } 400050397Sobrien 4001117395Skan my_friendly_assert (TREE_CODE (fns) == TEMPLATE_DECL 4002117395Skan || TREE_CODE (fns) == OVERLOAD 4003117395Skan || BASELINK_P (fns) 4004117395Skan || TREE_CODE (fns) == IDENTIFIER_NODE 4005117395Skan || TREE_CODE (fns) == LOOKUP_EXPR, 4006117395Skan 20020730); 4007117395Skan 4008117395Skan if (BASELINK_P (fns)) 4009117395Skan { 4010117395Skan BASELINK_FUNCTIONS (fns) = build (TEMPLATE_ID_EXPR, 4011117395Skan unknown_type_node, 4012117395Skan BASELINK_FUNCTIONS (fns), 4013117395Skan arglist); 4014117395Skan return fns; 4015117395Skan } 4016117395Skan 401750397Sobrien type = TREE_TYPE (fns); 401850397Sobrien if (TREE_CODE (fns) == OVERLOAD || !type) 401950397Sobrien type = unknown_type_node; 4020117395Skan 4021117395Skan return build (TEMPLATE_ID_EXPR, type, fns, arglist); 402250397Sobrien} 402350397Sobrien 402450397Sobrien/* Within the scope of a template class S<T>, the name S gets bound 402550397Sobrien (in build_self_reference) to a TYPE_DECL for the class, not a 402650397Sobrien TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, 402750397Sobrien or one of its enclosing classes, and that type is a template, 402850397Sobrien return the associated TEMPLATE_DECL. Otherwise, the original 402950397Sobrien DECL is returned. */ 403050397Sobrien 403152284Sobrienstatic tree 403250397Sobrienmaybe_get_template_decl_from_type_decl (decl) 403350397Sobrien tree decl; 403450397Sobrien{ 403550397Sobrien return (decl != NULL_TREE 403650397Sobrien && TREE_CODE (decl) == TYPE_DECL 403750397Sobrien && DECL_ARTIFICIAL (decl) 403852284Sobrien && CLASS_TYPE_P (TREE_TYPE (decl)) 403950397Sobrien && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) 404050397Sobrien ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; 404150397Sobrien} 404250397Sobrien 404318334Speter/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of 404418334Speter parameters, find the desired type. 404518334Speter 404618334Speter D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. 404752284Sobrien (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will 404852284Sobrien be a TREE_LIST if called directly from the parser, and a TREE_VEC 404990075Sobrien otherwise.) 405018334Speter 405118334Speter IN_DECL, if non-NULL, is the template declaration we are trying to 405250397Sobrien instantiate. 405350397Sobrien 4054117395Skan If ENTERING_SCOPE is nonzero, we are about to enter the scope of 405552284Sobrien the class we are looking up. 405690075Sobrien 405796263Sobrien Issue error and warning messages under control of COMPLAIN. 405852284Sobrien 405950397Sobrien If the template class is really a local class in a template 406050397Sobrien function, then the FUNCTION_CONTEXT is the function in which it is 406150397Sobrien being instantiated. */ 406250397Sobrien 406318334Spetertree 406490075Sobrienlookup_template_class (d1, arglist, in_decl, context, entering_scope, complain) 406518334Speter tree d1, arglist; 406618334Speter tree in_decl; 406750397Sobrien tree context; 406852284Sobrien int entering_scope; 406996263Sobrien tsubst_flags_t complain; 407018334Speter{ 407150397Sobrien tree template = NULL_TREE, parmlist; 407252284Sobrien tree t; 407318334Speter 4074117395Skan timevar_push (TV_NAME_LOOKUP); 407550397Sobrien if (TREE_CODE (d1) == IDENTIFIER_NODE) 407650397Sobrien { 407752284Sobrien if (IDENTIFIER_VALUE (d1) 407852284Sobrien && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) 407952284Sobrien template = IDENTIFIER_VALUE (d1); 408050397Sobrien else 408150397Sobrien { 408250397Sobrien if (context) 408350397Sobrien push_decl_namespace (context); 408490075Sobrien template = lookup_name (d1, /*prefer_type=*/0); 408590075Sobrien template = maybe_get_template_decl_from_type_decl (template); 408650397Sobrien if (context) 408750397Sobrien pop_decl_namespace (); 408850397Sobrien } 408950397Sobrien if (template) 409050397Sobrien context = DECL_CONTEXT (template); 4091119256Skan if (template 4092119256Skan && TREE_CODE (template) == TYPE_DECL 4093119256Skan && IS_AGGR_TYPE (TREE_TYPE (template)) 4094119256Skan && TREE_CODE (TREE_TYPE (template)) != TEMPLATE_TYPE_PARM) 4095119256Skan { 4096119256Skan d1 = template; 4097119256Skan goto type_decl; 4098119256Skan } 409950397Sobrien } 410050397Sobrien else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) 410150397Sobrien { 4102119256Skan tree type; 4103119256Skan type_decl: 4104119256Skan type = TREE_TYPE (d1); 410552284Sobrien 410652284Sobrien /* If we are declaring a constructor, say A<T>::A<T>, we will get 410752284Sobrien an implicit typename for the second A. Deal with it. */ 410852284Sobrien if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) 410952284Sobrien type = TREE_TYPE (type); 411052284Sobrien 411152284Sobrien if (CLASSTYPE_TEMPLATE_INFO (type)) 411252284Sobrien { 411352284Sobrien template = CLASSTYPE_TI_TEMPLATE (type); 411452284Sobrien d1 = DECL_NAME (template); 411552284Sobrien } 411650397Sobrien } 411752284Sobrien else if (TREE_CODE (d1) == ENUMERAL_TYPE 411890075Sobrien || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) 411950397Sobrien { 412052284Sobrien template = TYPE_TI_TEMPLATE (d1); 412150397Sobrien d1 = DECL_NAME (template); 412250397Sobrien } 412350397Sobrien else if (TREE_CODE (d1) == TEMPLATE_DECL 412490075Sobrien && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) 412550397Sobrien { 412650397Sobrien template = d1; 412750397Sobrien d1 = DECL_NAME (template); 412850397Sobrien context = DECL_CONTEXT (template); 412950397Sobrien } 413050397Sobrien 413118334Speter /* With something like `template <class T> class X class X { ... };' 413252284Sobrien we could end up with D1 having nothing but an IDENTIFIER_VALUE. 413352284Sobrien We don't want to do that, but we have to deal with the situation, 413452284Sobrien so let's give them some syntax errors to chew on instead of a 413590075Sobrien crash. Alternatively D1 might not be a template type at all. */ 413618334Speter if (! template) 413752284Sobrien { 413896263Sobrien if (complain & tf_error) 413990075Sobrien error ("`%T' is not a template", d1); 4140117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 414152284Sobrien } 414250397Sobrien 414390075Sobrien if (TREE_CODE (template) != TEMPLATE_DECL 414490075Sobrien /* If we're called from the parser, make sure it's a user visible 414590075Sobrien template. */ 414690075Sobrien || ((!arglist || TREE_CODE (arglist) == TREE_LIST) 414790075Sobrien && !DECL_TEMPLATE_PARM_P (template) 414890075Sobrien && !PRIMARY_TEMPLATE_P (template))) 414918334Speter { 415096263Sobrien if (complain & tf_error) 415190075Sobrien { 415290075Sobrien error ("non-template type `%T' used as a template", d1); 415390075Sobrien if (in_decl) 415490075Sobrien cp_error_at ("for template declaration `%D'", in_decl); 415590075Sobrien } 4156117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 415718334Speter } 415818334Speter 415950397Sobrien if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) 416018334Speter { 416150397Sobrien /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store 416250397Sobrien template arguments */ 416318334Speter 416490075Sobrien tree parm; 416550397Sobrien tree arglist2; 416618334Speter 416750397Sobrien parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); 416818334Speter 416990075Sobrien /* Consider an example where a template template parameter declared as 417090075Sobrien 417190075Sobrien template <class T, class U = std::allocator<T> > class TT 417290075Sobrien 417390075Sobrien The template parameter level of T and U are one level larger than 417490075Sobrien of TT. To proper process the default argument of U, say when an 417590075Sobrien instantiation `TT<int>' is seen, we need to build the full 4176102780Skan arguments containing {int} as the innermost level. Outer levels, 4177102780Skan available when not appearing as default template argument, can be 4178102780Skan obtained from `current_template_args ()'. 417990075Sobrien 4180102780Skan Suppose that TT is later substituted with std::vector. The above 4181102780Skan instantiation is `TT<int, std::allocator<T> >' with TT at 4182102780Skan level 1, and T at level 2, while the template arguments at level 1 4183102780Skan becomes {std::vector} and the inner level 2 is {int}. */ 4184102780Skan 4185102780Skan if (current_template_parms) 418690075Sobrien arglist = add_to_template_args (current_template_args (), arglist); 418790075Sobrien 418890075Sobrien arglist2 = coerce_template_parms (parmlist, arglist, template, 418990075Sobrien complain, /*require_all_args=*/1); 419050397Sobrien if (arglist2 == error_mark_node) 4191117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 419250397Sobrien 419390075Sobrien parm = bind_template_template_parm (TREE_TYPE (template), arglist2); 4194117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); 419550397Sobrien } 419652284Sobrien else 419718334Speter { 419852284Sobrien tree template_type = TREE_TYPE (template); 419952284Sobrien tree gen_tmpl; 420052284Sobrien tree type_decl; 420152284Sobrien tree found = NULL_TREE; 420290075Sobrien tree *tp; 420352284Sobrien int arg_depth; 420452284Sobrien int parm_depth; 420552284Sobrien int is_partial_instantiation; 420618334Speter 420752284Sobrien gen_tmpl = most_general_template (template); 420852284Sobrien parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); 420952284Sobrien parm_depth = TMPL_PARMS_DEPTH (parmlist); 421052284Sobrien arg_depth = TMPL_ARGS_DEPTH (arglist); 421150397Sobrien 421252284Sobrien if (arg_depth == 1 && parm_depth > 1) 421318334Speter { 421452284Sobrien /* We've been given an incomplete set of template arguments. 421552284Sobrien For example, given: 421652284Sobrien 421752284Sobrien template <class T> struct S1 { 421852284Sobrien template <class U> struct S2 {}; 421952284Sobrien template <class U> struct S2<U*> {}; 422052284Sobrien }; 422152284Sobrien 422252284Sobrien we will be called with an ARGLIST of `U*', but the 422352284Sobrien TEMPLATE will be `template <class T> template 422452284Sobrien <class U> struct S1<T>::S2'. We must fill in the missing 422552284Sobrien arguments. */ 422652284Sobrien arglist 422752284Sobrien = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), 422852284Sobrien arglist); 422952284Sobrien arg_depth = TMPL_ARGS_DEPTH (arglist); 423052284Sobrien } 423152284Sobrien 423290075Sobrien /* Now we should have enough arguments. */ 423352284Sobrien my_friendly_assert (parm_depth == arg_depth, 0); 423452284Sobrien 423552284Sobrien /* From here on, we're only interested in the most general 423652284Sobrien template. */ 423752284Sobrien template = gen_tmpl; 423852284Sobrien 423952284Sobrien /* Calculate the BOUND_ARGS. These will be the args that are 424052284Sobrien actually tsubst'd into the definition to create the 424152284Sobrien instantiation. */ 424252284Sobrien if (parm_depth > 1) 424352284Sobrien { 424450397Sobrien /* We have multiple levels of arguments to coerce, at once. */ 424550397Sobrien int i; 424652284Sobrien int saved_depth = TMPL_ARGS_DEPTH (arglist); 424752284Sobrien 424890075Sobrien tree bound_args = make_tree_vec (parm_depth); 424950397Sobrien 425052284Sobrien for (i = saved_depth, 425150397Sobrien t = DECL_TEMPLATE_PARMS (template); 425252284Sobrien i > 0 && t != NULL_TREE; 425350397Sobrien --i, t = TREE_CHAIN (t)) 425452284Sobrien { 425552284Sobrien tree a = coerce_template_parms (TREE_VALUE (t), 425690075Sobrien arglist, template, 425790075Sobrien complain, /*require_all_args=*/1); 425852284Sobrien SET_TMPL_ARGS_LEVEL (bound_args, i, a); 425952284Sobrien 426052284Sobrien /* We temporarily reduce the length of the ARGLIST so 426152284Sobrien that coerce_template_parms will see only the arguments 426252284Sobrien corresponding to the template parameters it is 426352284Sobrien examining. */ 426452284Sobrien TREE_VEC_LENGTH (arglist)--; 426552284Sobrien } 426652284Sobrien 426752284Sobrien /* Restore the ARGLIST to its full size. */ 426852284Sobrien TREE_VEC_LENGTH (arglist) = saved_depth; 426952284Sobrien 427052284Sobrien arglist = bound_args; 427118334Speter } 427218334Speter else 427352284Sobrien arglist 427452284Sobrien = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), 427590075Sobrien INNERMOST_TEMPLATE_ARGS (arglist), 427690075Sobrien template, 427790075Sobrien complain, /*require_all_args=*/1); 427852284Sobrien 427952284Sobrien if (arglist == error_mark_node) 428052284Sobrien /* We were unable to bind the arguments. */ 4281117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 428252284Sobrien 428352284Sobrien /* In the scope of a template class, explicit references to the 428452284Sobrien template class refer to the type of the template, not any 428552284Sobrien instantiation of it. For example, in: 428652284Sobrien 428752284Sobrien template <class T> class C { void f(C<T>); } 428852284Sobrien 428952284Sobrien the `C<T>' is just the same as `C'. Outside of the 429052284Sobrien class, however, such a reference is an instantiation. */ 429152284Sobrien if (comp_template_args (TYPE_TI_ARGS (template_type), 429252284Sobrien arglist)) 429318334Speter { 429452284Sobrien found = template_type; 429552284Sobrien 429652284Sobrien if (!entering_scope && PRIMARY_TEMPLATE_P (template)) 429718334Speter { 429852284Sobrien tree ctx; 429952284Sobrien 430052284Sobrien /* Note that we use DECL_CONTEXT, rather than 430152284Sobrien CP_DECL_CONTEXT, so that the termination test is 430290075Sobrien always just `ctx'. We're not interested in namespace 430352284Sobrien scopes. */ 430452284Sobrien for (ctx = current_class_type; 430552284Sobrien ctx; 430690075Sobrien ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) 430752284Sobrien if (same_type_p (ctx, template_type)) 430852284Sobrien break; 430952284Sobrien 431052284Sobrien if (!ctx) 431152284Sobrien /* We're not in the scope of the class, so the 431252284Sobrien TEMPLATE_TYPE is not the type we want after 431352284Sobrien all. */ 431452284Sobrien found = NULL_TREE; 431518334Speter } 431652284Sobrien } 431752284Sobrien if (found) 4318117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); 431950397Sobrien 432090075Sobrien for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template); 432190075Sobrien *tp; 432290075Sobrien tp = &TREE_CHAIN (*tp)) 432390075Sobrien if (comp_template_args (TREE_PURPOSE (*tp), arglist)) 432490075Sobrien { 432590075Sobrien found = *tp; 432690075Sobrien 432790075Sobrien /* Use the move-to-front heuristic to speed up future 432890075Sobrien searches. */ 432990075Sobrien *tp = TREE_CHAIN (*tp); 433090075Sobrien TREE_CHAIN (found) 433190075Sobrien = DECL_TEMPLATE_INSTANTIATIONS (template); 433290075Sobrien DECL_TEMPLATE_INSTANTIATIONS (template) = found; 433390075Sobrien 4334117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found)); 433590075Sobrien } 433690075Sobrien 433752284Sobrien /* This type is a "partial instantiation" if any of the template 433890075Sobrien arguments still involve template parameters. Note that we set 433952284Sobrien IS_PARTIAL_INSTANTIATION for partial specializations as 434052284Sobrien well. */ 434152284Sobrien is_partial_instantiation = uses_template_parms (arglist); 434250397Sobrien 434390075Sobrien if (!is_partial_instantiation 434490075Sobrien && !PRIMARY_TEMPLATE_P (template) 434590075Sobrien && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) 434690075Sobrien { 434790075Sobrien found = xref_tag_from_type (TREE_TYPE (template), 434890075Sobrien DECL_NAME (template), 434990075Sobrien /*globalize=*/1); 4350117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); 435190075Sobrien } 435290075Sobrien 435390075Sobrien context = tsubst (DECL_CONTEXT (template), arglist, 435496263Sobrien complain, in_decl); 435590075Sobrien if (!context) 435690075Sobrien context = global_namespace; 435790075Sobrien 435852284Sobrien /* Create the type. */ 435952284Sobrien if (TREE_CODE (template_type) == ENUMERAL_TYPE) 436018334Speter { 436152284Sobrien if (!is_partial_instantiation) 436290075Sobrien { 436390075Sobrien set_current_access_from_decl (TYPE_NAME (template_type)); 436490075Sobrien t = start_enum (TYPE_IDENTIFIER (template_type)); 436590075Sobrien } 436652284Sobrien else 436752284Sobrien /* We don't want to call start_enum for this type, since 436852284Sobrien the values for the enumeration constants may involve 436952284Sobrien template parameters. And, no one should be interested 437052284Sobrien in the enumeration constants for such a type. */ 437152284Sobrien t = make_node (ENUMERAL_TYPE); 437218334Speter } 437352284Sobrien else 437452284Sobrien { 437590075Sobrien t = make_aggr_type (TREE_CODE (template_type)); 437652284Sobrien CLASSTYPE_DECLARED_CLASS (t) 437752284Sobrien = CLASSTYPE_DECLARED_CLASS (template_type); 437852284Sobrien CLASSTYPE_GOT_SEMICOLON (t) = 1; 437952284Sobrien SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); 438052284Sobrien TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); 438190075Sobrien 438290075Sobrien /* A local class. Make sure the decl gets registered properly. */ 438390075Sobrien if (context == current_function_decl) 438490075Sobrien pushtag (DECL_NAME (template), t, 0); 438552284Sobrien } 438650397Sobrien 438790075Sobrien /* If we called start_enum or pushtag above, this information 438890075Sobrien will already be set up. */ 438952284Sobrien if (!TYPE_NAME (t)) 439050397Sobrien { 439152284Sobrien TYPE_CONTEXT (t) = FROB_CONTEXT (context); 439252284Sobrien 439390075Sobrien type_decl = create_implicit_typedef (DECL_NAME (template), t); 439452284Sobrien DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); 439590075Sobrien TYPE_STUB_DECL (t) = type_decl; 4396117395Skan DECL_SOURCE_LOCATION (type_decl) 4397117395Skan = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); 439850397Sobrien } 439950397Sobrien else 440052284Sobrien type_decl = TYPE_NAME (t); 440152284Sobrien 4402117395Skan TREE_PRIVATE (type_decl) 4403117395Skan = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); 4404117395Skan TREE_PROTECTED (type_decl) 4405117395Skan = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); 4406117395Skan 440752284Sobrien /* Set up the template information. We have to figure out which 440852284Sobrien template is the immediate parent if this is a full 440952284Sobrien instantiation. */ 441052284Sobrien if (parm_depth == 1 || is_partial_instantiation 441152284Sobrien || !PRIMARY_TEMPLATE_P (template)) 441252284Sobrien /* This case is easy; there are no member templates involved. */ 441352284Sobrien found = template; 441452284Sobrien else 441550397Sobrien { 441690075Sobrien /* This is a full instantiation of a member template. Look 441790075Sobrien for a partial instantiation of which this is an instance. */ 441852284Sobrien 441952284Sobrien for (found = DECL_TEMPLATE_INSTANTIATIONS (template); 442052284Sobrien found; found = TREE_CHAIN (found)) 442152284Sobrien { 442252284Sobrien int success; 442352284Sobrien tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); 442452284Sobrien 442552284Sobrien /* We only want partial instantiations, here, not 442652284Sobrien specializations or full instantiations. */ 442752284Sobrien if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) 442852284Sobrien || !uses_template_parms (TREE_VALUE (found))) 442952284Sobrien continue; 443052284Sobrien 443152284Sobrien /* Temporarily reduce by one the number of levels in the 443252284Sobrien ARGLIST and in FOUND so as to avoid comparing the 443352284Sobrien last set of arguments. */ 443452284Sobrien TREE_VEC_LENGTH (arglist)--; 443552284Sobrien TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; 443652284Sobrien 443752284Sobrien /* See if the arguments match. If they do, then TMPL is 443852284Sobrien the partial instantiation we want. */ 443952284Sobrien success = comp_template_args (TREE_PURPOSE (found), arglist); 444052284Sobrien 444152284Sobrien /* Restore the argument vectors to their full size. */ 444252284Sobrien TREE_VEC_LENGTH (arglist)++; 444352284Sobrien TREE_VEC_LENGTH (TREE_PURPOSE (found))++; 444452284Sobrien 444552284Sobrien if (success) 444652284Sobrien { 444752284Sobrien found = tmpl; 444852284Sobrien break; 444952284Sobrien } 445052284Sobrien } 445152284Sobrien 445252284Sobrien if (!found) 445390075Sobrien { 445490075Sobrien /* There was no partial instantiation. This happens 445590075Sobrien where C<T> is a member template of A<T> and it's used 445690075Sobrien in something like 445790075Sobrien 445890075Sobrien template <typename T> struct B { A<T>::C<int> m; }; 445990075Sobrien B<float>; 446090075Sobrien 446190075Sobrien Create the partial instantiation. 446290075Sobrien */ 446390075Sobrien TREE_VEC_LENGTH (arglist)--; 446496263Sobrien found = tsubst (template, arglist, complain, NULL_TREE); 446590075Sobrien TREE_VEC_LENGTH (arglist)++; 446690075Sobrien } 446750397Sobrien } 446850397Sobrien 446990075Sobrien SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); 447052284Sobrien DECL_TEMPLATE_INSTANTIATIONS (template) 447152284Sobrien = tree_cons (arglist, t, 447252284Sobrien DECL_TEMPLATE_INSTANTIATIONS (template)); 447350397Sobrien 447452284Sobrien if (TREE_CODE (t) == ENUMERAL_TYPE 447552284Sobrien && !is_partial_instantiation) 447652284Sobrien /* Now that the type has been registered on the instantiations 447752284Sobrien list, we set up the enumerators. Because the enumeration 447852284Sobrien constants may involve the enumeration type itself, we make 447952284Sobrien sure to register the type first, and then create the 448052284Sobrien constants. That way, doing tsubst_expr for the enumeration 448152284Sobrien constants won't result in recursive calls here; we'll find 448252284Sobrien the instantiation and exit above. */ 448352284Sobrien tsubst_enum (template_type, t, arglist); 448450397Sobrien 448552284Sobrien /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO 448652284Sobrien is set up. */ 448752284Sobrien if (TREE_CODE (t) != ENUMERAL_TYPE) 448852284Sobrien DECL_NAME (type_decl) = classtype_mangled_name (t); 448952284Sobrien if (!is_partial_instantiation) 449052284Sobrien { 449152284Sobrien /* For backwards compatibility; code that uses 449252284Sobrien -fexternal-templates expects looking up a template to 449352284Sobrien instantiate it. I think DDD still relies on this. 449452284Sobrien (jason 8/20/1998) */ 449552284Sobrien if (TREE_CODE (t) != ENUMERAL_TYPE 449652284Sobrien && flag_external_templates 449752284Sobrien && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) 449852284Sobrien && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) 449952284Sobrien add_pending_template (t); 450052284Sobrien } 450152284Sobrien else 450250397Sobrien /* If the type makes use of template parameters, the 450350397Sobrien code that generates debugging information will crash. */ 450450397Sobrien DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; 450552284Sobrien 4506117395Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 450750397Sobrien } 4508117395Skan timevar_pop (TV_NAME_LOOKUP); 450918334Speter} 451018334Speter 451190075Sobrienstruct pair_fn_data 451290075Sobrien{ 451390075Sobrien tree_fn_t fn; 451490075Sobrien void *data; 4515117395Skan htab_t visited; 451690075Sobrien}; 451750397Sobrien 451890075Sobrien/* Called from for_each_template_parm via walk_tree. */ 451990075Sobrien 452090075Sobrienstatic tree 452190075Sobrienfor_each_template_parm_r (tp, walk_subtrees, d) 452290075Sobrien tree *tp; 452390075Sobrien int *walk_subtrees; 452490075Sobrien void *d; 452518334Speter{ 452690075Sobrien tree t = *tp; 452790075Sobrien struct pair_fn_data *pfd = (struct pair_fn_data *) d; 452890075Sobrien tree_fn_t fn = pfd->fn; 452990075Sobrien void *data = pfd->data; 4530117395Skan void **slot; 4531117395Skan 4532117395Skan /* If we have already visited this tree, there's no need to walk 4533117395Skan subtrees. Otherwise, add it to the visited table. */ 4534117395Skan slot = htab_find_slot (pfd->visited, *tp, INSERT); 4535117395Skan if (*slot) 4536117395Skan { 4537117395Skan *walk_subtrees = 0; 4538117395Skan return NULL_TREE; 4539117395Skan } 4540117395Skan *slot = *tp; 4541117395Skan 454290075Sobrien if (TYPE_P (t) 4543117395Skan && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) 454490075Sobrien return error_mark_node; 454550397Sobrien 454618334Speter switch (TREE_CODE (t)) 454718334Speter { 454818334Speter case RECORD_TYPE: 454990075Sobrien if (TYPE_PTRMEMFUNC_P (t)) 455090075Sobrien break; 455152284Sobrien /* Fall through. */ 455252284Sobrien 455318334Speter case UNION_TYPE: 455452284Sobrien case ENUMERAL_TYPE: 455590075Sobrien if (!TYPE_TEMPLATE_INFO (t)) 455690075Sobrien *walk_subtrees = 0; 455790075Sobrien else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), 4558117395Skan fn, data, pfd->visited)) 455990075Sobrien return error_mark_node; 456090075Sobrien break; 456190075Sobrien 456252284Sobrien case METHOD_TYPE: 456390075Sobrien /* Since we're not going to walk subtrees, we have to do this 456490075Sobrien explicitly here. */ 4565117395Skan if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, 4566117395Skan pfd->visited)) 456790075Sobrien return error_mark_node; 456852284Sobrien /* Fall through. */ 456952284Sobrien 457018334Speter case FUNCTION_TYPE: 457190075Sobrien /* Check the return type. */ 4572117395Skan if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) 457390075Sobrien return error_mark_node; 457490075Sobrien 457552284Sobrien /* Check the parameter types. Since default arguments are not 457652284Sobrien instantiated until they are needed, the TYPE_ARG_TYPES may 457752284Sobrien contain expressions that involve template parameters. But, 457852284Sobrien no-one should be looking at them yet. And, once they're 457952284Sobrien instantiated, they don't contain template parameters, so 458052284Sobrien there's no point in looking at them then, either. */ 458152284Sobrien { 458252284Sobrien tree parm; 458352284Sobrien 458452284Sobrien for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) 4585117395Skan if (for_each_template_parm (TREE_VALUE (parm), fn, data, 4586117395Skan pfd->visited)) 458790075Sobrien return error_mark_node; 458890075Sobrien 458990075Sobrien /* Since we've already handled the TYPE_ARG_TYPES, we don't 459090075Sobrien want walk_tree walking into them itself. */ 459190075Sobrien *walk_subtrees = 0; 459252284Sobrien } 459390075Sobrien break; 459452284Sobrien 459518334Speter case FUNCTION_DECL: 459650397Sobrien case VAR_DECL: 459750397Sobrien if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) 4598117395Skan && for_each_template_parm (DECL_TI_ARGS (t), fn, data, 4599117395Skan pfd->visited)) 460090075Sobrien return error_mark_node; 460190075Sobrien /* Fall through. */ 460290075Sobrien 460390075Sobrien case CONST_DECL: 460418334Speter case PARM_DECL: 460550397Sobrien if (DECL_CONTEXT (t) 4606117395Skan && for_each_template_parm (DECL_CONTEXT (t), fn, data, 4607117395Skan pfd->visited)) 460890075Sobrien return error_mark_node; 460990075Sobrien break; 461018334Speter 461190075Sobrien case BOUND_TEMPLATE_TEMPLATE_PARM: 461290075Sobrien /* Record template parameters such as `T' inside `TT<T>'. */ 4613117395Skan if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) 461490075Sobrien return error_mark_node; 461590075Sobrien /* Fall through. */ 461618334Speter 461750397Sobrien case TEMPLATE_TEMPLATE_PARM: 461818334Speter case TEMPLATE_TYPE_PARM: 461950397Sobrien case TEMPLATE_PARM_INDEX: 462090075Sobrien if (fn && (*fn)(t, data)) 462190075Sobrien return error_mark_node; 462290075Sobrien else if (!fn) 462390075Sobrien return error_mark_node; 462490075Sobrien break; 462518334Speter 462690075Sobrien case TEMPLATE_DECL: 462790075Sobrien /* A template template parameter is encountered */ 462890075Sobrien if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) 4629117395Skan && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) 463090075Sobrien return error_mark_node; 463118334Speter 463290075Sobrien /* Already substituted template template parameter */ 463390075Sobrien *walk_subtrees = 0; 463490075Sobrien break; 463518334Speter 463650397Sobrien case TYPENAME_TYPE: 4637117395Skan if (!fn 4638117395Skan || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, 4639117395Skan data, pfd->visited)) 464090075Sobrien return error_mark_node; 464190075Sobrien break; 464218334Speter 464318334Speter case CONSTRUCTOR: 464490075Sobrien if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) 464590075Sobrien && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE 4646117395Skan (TREE_TYPE (t)), fn, data, 4647117395Skan pfd->visited)) 464890075Sobrien return error_mark_node; 464990075Sobrien break; 465090075Sobrien 465190075Sobrien case INDIRECT_REF: 465290075Sobrien case COMPONENT_REF: 465390075Sobrien /* If there's no type, then this thing must be some expression 465490075Sobrien involving template parameters. */ 465590075Sobrien if (!fn && !TREE_TYPE (t)) 465690075Sobrien return error_mark_node; 465790075Sobrien break; 465818334Speter 465950397Sobrien case MODOP_EXPR: 466050397Sobrien case CAST_EXPR: 466150397Sobrien case REINTERPRET_CAST_EXPR: 466250397Sobrien case CONST_CAST_EXPR: 466350397Sobrien case STATIC_CAST_EXPR: 466450397Sobrien case DYNAMIC_CAST_EXPR: 466550397Sobrien case ARROW_EXPR: 466650397Sobrien case DOTSTAR_EXPR: 466750397Sobrien case TYPEID_EXPR: 466890075Sobrien case LOOKUP_EXPR: 466990075Sobrien case PSEUDO_DTOR_EXPR: 467090075Sobrien if (!fn) 467190075Sobrien return error_mark_node; 467290075Sobrien break; 467350397Sobrien 4674117395Skan case BASELINK: 4675117395Skan /* If we do not handle this case specially, we end up walking 4676117395Skan the BINFO hierarchy, which is circular, and therefore 4677117395Skan confuses walk_tree. */ 4678117395Skan *walk_subtrees = 0; 4679117395Skan if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data, 4680117395Skan pfd->visited)) 4681117395Skan return error_mark_node; 4682117395Skan break; 4683117395Skan 468418334Speter default: 468590075Sobrien break; 468618334Speter } 468790075Sobrien 468890075Sobrien /* We didn't find any template parameters we liked. */ 468990075Sobrien return NULL_TREE; 469018334Speter} 469118334Speter 469290075Sobrien/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, 469390075Sobrien BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, 469490075Sobrien call FN with the parameter and the DATA. 4695117395Skan If FN returns nonzero, the iteration is terminated, and 469690075Sobrien for_each_template_parm returns 1. Otherwise, the iteration 4697117395Skan continues. If FN never returns a nonzero value, the value 469890075Sobrien returned by for_each_template_parm is 0. If FN is NULL, it is 469990075Sobrien considered to be the function which always returns 1. */ 470090075Sobrien 470190075Sobrienstatic int 4702117395Skanfor_each_template_parm (t, fn, data, visited) 470390075Sobrien tree t; 470490075Sobrien tree_fn_t fn; 470590075Sobrien void* data; 4706117395Skan htab_t visited; 470790075Sobrien{ 470890075Sobrien struct pair_fn_data pfd; 4709117395Skan int result; 471090075Sobrien 471190075Sobrien /* Set up. */ 471290075Sobrien pfd.fn = fn; 471390075Sobrien pfd.data = data; 471490075Sobrien 471590075Sobrien /* Walk the tree. (Conceptually, we would like to walk without 471690075Sobrien duplicates, but for_each_template_parm_r recursively calls 471790075Sobrien for_each_template_parm, so we would need to reorganize a fair 4718117395Skan bit to use walk_tree_without_duplicates, so we keep our own 4719117395Skan visited list.) */ 4720117395Skan if (visited) 4721117395Skan pfd.visited = visited; 4722117395Skan else 4723117395Skan pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, 4724117395Skan NULL); 4725117395Skan result = walk_tree (&t, 4726117395Skan for_each_template_parm_r, 4727117395Skan &pfd, 4728117395Skan NULL) != NULL_TREE; 4729117395Skan 4730117395Skan /* Clean up. */ 4731117395Skan if (!visited) 4732117395Skan htab_delete (pfd.visited); 4733117395Skan 4734117395Skan return result; 473590075Sobrien} 473690075Sobrien 473750397Sobrienint 473850397Sobrienuses_template_parms (t) 473950397Sobrien tree t; 474018334Speter{ 4741117395Skan return for_each_template_parm (t, 0, 0, NULL); 474250397Sobrien} 474318334Speter 474450397Sobrienstatic int tinst_depth; 474550397Sobrienextern int max_tinst_depth; 474650397Sobrien#ifdef GATHER_STATISTICS 474750397Sobrienint depth_reached; 474850397Sobrien#endif 474990075Sobrienstatic int tinst_level_tick; 475090075Sobrienstatic int last_template_error_tick; 475150397Sobrien 475290075Sobrien/* We're starting to instantiate D; record the template instantiation context 475390075Sobrien for diagnostics and to restore it later. */ 475450397Sobrien 475590075Sobrienint 475650397Sobrienpush_tinst_level (d) 475750397Sobrien tree d; 475850397Sobrien{ 475990075Sobrien tree new; 476018334Speter 476118334Speter if (tinst_depth >= max_tinst_depth) 476218334Speter { 476350397Sobrien /* If the instantiation in question still has unbound template parms, 476450397Sobrien we don't really care if we can't instantiate it, so just return. 476550397Sobrien This happens with base instantiation for implicit `typename'. */ 476650397Sobrien if (uses_template_parms (d)) 476750397Sobrien return 0; 476850397Sobrien 476950397Sobrien last_template_error_tick = tinst_level_tick; 477090075Sobrien error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'", 477190075Sobrien max_tinst_depth, d); 477250397Sobrien 477390075Sobrien print_instantiation_context (); 477450397Sobrien 477518334Speter return 0; 477618334Speter } 477718334Speter 477890075Sobrien new = build_expr_wfl (d, input_filename, lineno, 0); 477990075Sobrien TREE_CHAIN (new) = current_tinst_level; 478018334Speter current_tinst_level = new; 478150397Sobrien 478218334Speter ++tinst_depth; 478350397Sobrien#ifdef GATHER_STATISTICS 478450397Sobrien if (tinst_depth > depth_reached) 478550397Sobrien depth_reached = tinst_depth; 478650397Sobrien#endif 478750397Sobrien 478850397Sobrien ++tinst_level_tick; 478918334Speter return 1; 479018334Speter} 479118334Speter 479290075Sobrien/* We're done instantiating this template; return to the instantiation 479390075Sobrien context. */ 479490075Sobrien 479518334Spetervoid 479618334Speterpop_tinst_level () 479718334Speter{ 479890075Sobrien tree old = current_tinst_level; 479918334Speter 480052284Sobrien /* Restore the filename and line number stashed away when we started 480152284Sobrien this instantiation. */ 480290075Sobrien lineno = TINST_LINE (old); 480390075Sobrien input_filename = TINST_FILE (old); 480452284Sobrien extract_interface_info (); 480552284Sobrien 480690075Sobrien current_tinst_level = TREE_CHAIN (old); 480718334Speter --tinst_depth; 480850397Sobrien ++tinst_level_tick; 480918334Speter} 481018334Speter 481190075Sobrien/* We're instantiating a deferred template; restore the template 481290075Sobrien instantiation context in which the instantiation was requested, which 481390075Sobrien is one step out from LEVEL. */ 481490075Sobrien 481590075Sobrienstatic void 481690075Sobrienreopen_tinst_level (level) 481790075Sobrien tree level; 481890075Sobrien{ 481990075Sobrien tree t; 482090075Sobrien 482190075Sobrien tinst_depth = 0; 482290075Sobrien for (t = level; t; t = TREE_CHAIN (t)) 482390075Sobrien ++tinst_depth; 482490075Sobrien 482590075Sobrien current_tinst_level = level; 482690075Sobrien pop_tinst_level (); 482790075Sobrien} 482890075Sobrien 482990075Sobrien/* Return the outermost template instantiation context, for use with 483090075Sobrien -falt-external-templates. */ 483190075Sobrien 483290075Sobrientree 483318334Spetertinst_for_decl () 483418334Speter{ 483590075Sobrien tree p = current_tinst_level; 483618334Speter 483718334Speter if (p) 483890075Sobrien for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p)) 483918334Speter ; 484018334Speter return p; 484118334Speter} 484218334Speter 484350397Sobrien/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the 484450397Sobrien vector of template arguments, as for tsubst. 484550397Sobrien 484690075Sobrien Returns an appropriate tsubst'd friend declaration. */ 484750397Sobrien 484850397Sobrienstatic tree 484950397Sobrientsubst_friend_function (decl, args) 485050397Sobrien tree decl; 485150397Sobrien tree args; 485250397Sobrien{ 485350397Sobrien tree new_friend; 485450397Sobrien int line = lineno; 485590075Sobrien const char *file = input_filename; 485650397Sobrien 485750397Sobrien lineno = DECL_SOURCE_LINE (decl); 485850397Sobrien input_filename = DECL_SOURCE_FILE (decl); 485950397Sobrien 486050397Sobrien if (TREE_CODE (decl) == FUNCTION_DECL 486150397Sobrien && DECL_TEMPLATE_INSTANTIATION (decl) 486250397Sobrien && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) 486350397Sobrien /* This was a friend declared with an explicit template 486450397Sobrien argument list, e.g.: 486550397Sobrien 486650397Sobrien friend void f<>(T); 486750397Sobrien 486850397Sobrien to indicate that f was a template instantiation, not a new 486950397Sobrien function declaration. Now, we have to figure out what 487050397Sobrien instantiation of what template. */ 487150397Sobrien { 487290075Sobrien tree template_id, arglist, fns; 487350397Sobrien tree new_args; 487450397Sobrien tree tmpl; 487596263Sobrien tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); 487690075Sobrien 487790075Sobrien /* Friend functions are looked up in the containing namespace scope. 487890075Sobrien We must enter that scope, to avoid finding member functions of the 487990075Sobrien current cless with same name. */ 488090075Sobrien push_nested_namespace (ns); 488190075Sobrien fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, 488296263Sobrien tf_error | tf_warning, NULL_TREE); 488390075Sobrien pop_nested_namespace (ns); 488490075Sobrien arglist = tsubst (DECL_TI_ARGS (decl), args, 488596263Sobrien tf_error | tf_warning, NULL_TREE); 488690075Sobrien template_id = lookup_template_function (fns, arglist); 488790075Sobrien 488896263Sobrien new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); 488952284Sobrien tmpl = determine_specialization (template_id, new_friend, 489052284Sobrien &new_args, 489152284Sobrien /*need_member_template=*/0); 489250397Sobrien new_friend = instantiate_template (tmpl, new_args); 489350397Sobrien goto done; 489450397Sobrien } 489552284Sobrien 489696263Sobrien new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); 489750397Sobrien 489852284Sobrien /* The NEW_FRIEND will look like an instantiation, to the 489950397Sobrien compiler, but is not an instantiation from the point of view of 490050397Sobrien the language. For example, we might have had: 490150397Sobrien 490250397Sobrien template <class T> struct S { 490350397Sobrien template <class U> friend void f(T, U); 490450397Sobrien }; 490550397Sobrien 490650397Sobrien Then, in S<int>, template <class U> void f(int, U) is not an 490750397Sobrien instantiation of anything. */ 490850397Sobrien DECL_USE_TEMPLATE (new_friend) = 0; 490950397Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 491090075Sobrien { 491190075Sobrien DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; 491290075Sobrien DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) 491390075Sobrien = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); 491490075Sobrien } 491552284Sobrien 491690075Sobrien /* The mangled name for the NEW_FRIEND is incorrect. The function 491790075Sobrien is not a template instantiation and should not be mangled like 491890075Sobrien one. Therefore, we forget the mangling here; we'll recompute it 491990075Sobrien later if we need it. */ 492052284Sobrien if (TREE_CODE (new_friend) != TEMPLATE_DECL) 492152284Sobrien { 492290075Sobrien SET_DECL_RTL (new_friend, NULL_RTX); 492390075Sobrien SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); 492452284Sobrien } 492552284Sobrien 492650397Sobrien if (DECL_NAMESPACE_SCOPE_P (new_friend)) 492750397Sobrien { 492852284Sobrien tree old_decl; 492952284Sobrien tree new_friend_template_info; 493052284Sobrien tree new_friend_result_template_info; 493190075Sobrien tree ns; 493252284Sobrien int new_friend_is_defn; 493352284Sobrien 493452284Sobrien /* We must save some information from NEW_FRIEND before calling 493552284Sobrien duplicate decls since that function will free NEW_FRIEND if 493652284Sobrien possible. */ 493752284Sobrien new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); 493850397Sobrien if (TREE_CODE (new_friend) == TEMPLATE_DECL) 493952284Sobrien { 494052284Sobrien /* This declaration is a `primary' template. */ 494152284Sobrien DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; 494252284Sobrien 494352284Sobrien new_friend_is_defn 494490075Sobrien = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE; 494552284Sobrien new_friend_result_template_info 494690075Sobrien = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); 494752284Sobrien } 494852284Sobrien else 494952284Sobrien { 495052284Sobrien new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; 495152284Sobrien new_friend_result_template_info = NULL_TREE; 495252284Sobrien } 495350397Sobrien 495490075Sobrien /* Inside pushdecl_namespace_level, we will push into the 495590075Sobrien current namespace. However, the friend function should go 4956117395Skan into the namespace of the template. */ 495790075Sobrien ns = decl_namespace_context (new_friend); 495890075Sobrien push_nested_namespace (ns); 495952284Sobrien old_decl = pushdecl_namespace_level (new_friend); 496090075Sobrien pop_nested_namespace (ns); 496152284Sobrien 496252284Sobrien if (old_decl != new_friend) 496352284Sobrien { 496452284Sobrien /* This new friend declaration matched an existing 496552284Sobrien declaration. For example, given: 496652284Sobrien 496752284Sobrien template <class T> void f(T); 496852284Sobrien template <class U> class C { 496952284Sobrien template <class T> friend void f(T) {} 497052284Sobrien }; 497152284Sobrien 497252284Sobrien the friend declaration actually provides the definition 497352284Sobrien of `f', once C has been instantiated for some type. So, 497452284Sobrien old_decl will be the out-of-class template declaration, 497552284Sobrien while new_friend is the in-class definition. 497652284Sobrien 497752284Sobrien But, if `f' was called before this point, the 497852284Sobrien instantiation of `f' will have DECL_TI_ARGS corresponding 497952284Sobrien to `T' but not to `U', references to which might appear 498052284Sobrien in the definition of `f'. Previously, the most general 498152284Sobrien template for an instantiation of `f' was the out-of-class 498252284Sobrien version; now it is the in-class version. Therefore, we 498352284Sobrien run through all specialization of `f', adding to their 498452284Sobrien DECL_TI_ARGS appropriately. In particular, they need a 498552284Sobrien new set of outer arguments, corresponding to the 498652284Sobrien arguments for this class instantiation. 498752284Sobrien 498852284Sobrien The same situation can arise with something like this: 498952284Sobrien 499052284Sobrien friend void f(int); 499152284Sobrien template <class T> class C { 499252284Sobrien friend void f(T) {} 499352284Sobrien }; 499452284Sobrien 499552284Sobrien when `C<int>' is instantiated. Now, `f(int)' is defined 499652284Sobrien in the class. */ 499752284Sobrien 499852284Sobrien if (!new_friend_is_defn) 499952284Sobrien /* On the other hand, if the in-class declaration does 500052284Sobrien *not* provide a definition, then we don't want to alter 500152284Sobrien existing definitions. We can just leave everything 500252284Sobrien alone. */ 500352284Sobrien ; 500452284Sobrien else 500552284Sobrien { 500652284Sobrien /* Overwrite whatever template info was there before, if 500752284Sobrien any, with the new template information pertaining to 500852284Sobrien the declaration. */ 500952284Sobrien DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; 501052284Sobrien 501152284Sobrien if (TREE_CODE (old_decl) != TEMPLATE_DECL) 5012119256Skan reregister_specialization (new_friend, 5013119256Skan most_general_template (old_decl), 5014119256Skan old_decl); 501552284Sobrien else 501652284Sobrien { 501752284Sobrien tree t; 501852284Sobrien tree new_friend_args; 501952284Sobrien 502090075Sobrien DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) 502152284Sobrien = new_friend_result_template_info; 502252284Sobrien 502352284Sobrien new_friend_args = TI_ARGS (new_friend_template_info); 502452284Sobrien for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); 502552284Sobrien t != NULL_TREE; 502652284Sobrien t = TREE_CHAIN (t)) 502752284Sobrien { 502852284Sobrien tree spec = TREE_VALUE (t); 502952284Sobrien 503052284Sobrien DECL_TI_ARGS (spec) 503152284Sobrien = add_outermost_template_args (new_friend_args, 503252284Sobrien DECL_TI_ARGS (spec)); 503352284Sobrien } 503452284Sobrien 503552284Sobrien /* Now, since specializations are always supposed to 503652284Sobrien hang off of the most general template, we must move 503752284Sobrien them. */ 503852284Sobrien t = most_general_template (old_decl); 503952284Sobrien if (t != old_decl) 504052284Sobrien { 504152284Sobrien DECL_TEMPLATE_SPECIALIZATIONS (t) 504252284Sobrien = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), 504352284Sobrien DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); 504452284Sobrien DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; 504552284Sobrien } 504652284Sobrien } 504752284Sobrien } 504852284Sobrien 504952284Sobrien /* The information from NEW_FRIEND has been merged into OLD_DECL 505052284Sobrien by duplicate_decls. */ 505152284Sobrien new_friend = old_decl; 505252284Sobrien } 505350397Sobrien } 505490075Sobrien else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend))) 505550397Sobrien { 505650397Sobrien /* Check to see that the declaration is really present, and, 505750397Sobrien possibly obtain an improved declaration. */ 505850397Sobrien tree fn = check_classfn (DECL_CONTEXT (new_friend), 505950397Sobrien new_friend); 506050397Sobrien 506150397Sobrien if (fn) 506250397Sobrien new_friend = fn; 506350397Sobrien } 506450397Sobrien 506550397Sobrien done: 506650397Sobrien lineno = line; 506750397Sobrien input_filename = file; 506850397Sobrien return new_friend; 506950397Sobrien} 507050397Sobrien 507150397Sobrien/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of 507250397Sobrien template arguments, as for tsubst. 507350397Sobrien 507490075Sobrien Returns an appropriate tsubst'd friend type or error_mark_node on 507590075Sobrien failure. */ 507650397Sobrien 507750397Sobrienstatic tree 507850397Sobrientsubst_friend_class (friend_tmpl, args) 507950397Sobrien tree friend_tmpl; 508050397Sobrien tree args; 508150397Sobrien{ 508250397Sobrien tree friend_type; 508352284Sobrien tree tmpl; 508496263Sobrien tree context; 508550397Sobrien 508696263Sobrien context = DECL_CONTEXT (friend_tmpl); 508796263Sobrien 508896263Sobrien if (context) 508996263Sobrien { 509096263Sobrien if (TREE_CODE (context) == NAMESPACE_DECL) 509196263Sobrien push_nested_namespace (context); 509296263Sobrien else 5093102780Skan push_nested_class (tsubst (context, args, tf_none, NULL_TREE), 2); 509496263Sobrien } 509596263Sobrien 509652284Sobrien /* First, we look for a class template. */ 509752284Sobrien tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); 509896263Sobrien 509952284Sobrien /* But, if we don't find one, it might be because we're in a 510052284Sobrien situation like this: 510150397Sobrien 510252284Sobrien template <class T> 510352284Sobrien struct S { 510496263Sobrien template <class U> 510552284Sobrien friend struct S; 510652284Sobrien }; 510752284Sobrien 510852284Sobrien Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL 510952284Sobrien for `S<int>', not the TEMPLATE_DECL. */ 511052284Sobrien if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) 511150397Sobrien { 511252284Sobrien tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); 511352284Sobrien tmpl = maybe_get_template_decl_from_type_decl (tmpl); 511452284Sobrien } 511552284Sobrien 511652284Sobrien if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) 511752284Sobrien { 511850397Sobrien /* The friend template has already been declared. Just 511952284Sobrien check to see that the declarations match, and install any new 512052284Sobrien default parameters. We must tsubst the default parameters, 512152284Sobrien of course. We only need the innermost template parameters 512252284Sobrien because that is all that redeclare_class_template will look 512352284Sobrien at. */ 512496263Sobrien if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) 512596263Sobrien > TMPL_ARGS_DEPTH (args)) 512696263Sobrien { 512796263Sobrien tree parms; 512896263Sobrien parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), 512996263Sobrien args, tf_error | tf_warning); 513096263Sobrien redeclare_class_template (TREE_TYPE (tmpl), parms); 513196263Sobrien } 513296263Sobrien 513350397Sobrien friend_type = TREE_TYPE (tmpl); 513450397Sobrien } 513550397Sobrien else 513650397Sobrien { 513750397Sobrien /* The friend template has not already been declared. In this 513850397Sobrien case, the instantiation of the template class will cause the 513950397Sobrien injection of this template into the global scope. */ 514096263Sobrien tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE); 514150397Sobrien 514250397Sobrien /* The new TMPL is not an instantiation of anything, so we 514350397Sobrien forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for 514450397Sobrien the new type because that is supposed to be the corresponding 514550397Sobrien template decl, i.e., TMPL. */ 514650397Sobrien DECL_USE_TEMPLATE (tmpl) = 0; 514750397Sobrien DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; 514850397Sobrien CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; 514950397Sobrien 515050397Sobrien /* Inject this template into the global scope. */ 515150397Sobrien friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); 515250397Sobrien } 515350397Sobrien 515496263Sobrien if (context) 515596263Sobrien { 515696263Sobrien if (TREE_CODE (context) == NAMESPACE_DECL) 515796263Sobrien pop_nested_namespace (context); 515896263Sobrien else 515996263Sobrien pop_nested_class (); 516096263Sobrien } 516196263Sobrien 516250397Sobrien return friend_type; 516350397Sobrien} 516450397Sobrien 5165102780Skan/* Returns zero if TYPE cannot be completed later due to circularity. 5166102780Skan Otherwise returns one. */ 5167102780Skan 5168102780Skanstatic int 5169102780Skancan_complete_type_without_circularity (type) 5170102780Skan tree type; 5171102780Skan{ 5172102780Skan if (type == NULL_TREE || type == error_mark_node) 5173102780Skan return 0; 5174102780Skan else if (COMPLETE_TYPE_P (type)) 5175102780Skan return 1; 5176102780Skan else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) 5177102780Skan return can_complete_type_without_circularity (TREE_TYPE (type)); 5178102780Skan else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) 5179102780Skan return 0; 5180102780Skan else 5181102780Skan return 1; 5182102780Skan} 5183102780Skan 518418334Spetertree 518550397Sobrieninstantiate_class_template (type) 518650397Sobrien tree type; 518718334Speter{ 5188117395Skan tree template, args, pattern, t, member; 518952284Sobrien tree typedecl; 519018334Speter 519150397Sobrien if (type == error_mark_node) 519218334Speter return error_mark_node; 519318334Speter 519490075Sobrien if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type)) 519550397Sobrien return type; 519650397Sobrien 519752284Sobrien /* Figure out which template is being instantiated. */ 519852284Sobrien template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); 519950397Sobrien my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); 520050397Sobrien 520152284Sobrien /* Figure out which arguments are being used to do the 520252284Sobrien instantiation. */ 520352284Sobrien args = CLASSTYPE_TI_ARGS (type); 520452284Sobrien PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); 520518334Speter 520652284Sobrien if (pedantic && PARTIAL_INSTANTIATION_P (type)) 520752284Sobrien /* If this is a partial instantiation, then we can't instantiate 520852284Sobrien the type; there's no telling whether or not one of the 520952284Sobrien template parameters might eventually be instantiated to some 521052284Sobrien value that results in a specialization being used. For 521152284Sobrien example, consider: 521218334Speter 521352284Sobrien template <class T> 521452284Sobrien struct S {}; 521552284Sobrien 521652284Sobrien template <class U> 521752284Sobrien void f(S<U>); 521852284Sobrien 521952284Sobrien template <> 522052284Sobrien struct S<int> {}; 522152284Sobrien 522252284Sobrien Now, the `S<U>' in `f<int>' is the specialization, not an 522352284Sobrien instantiation of the original template. */ 522490075Sobrien return type; 522552284Sobrien 522652284Sobrien /* Determine what specialization of the original template to 522752284Sobrien instantiate. */ 522852284Sobrien if (PARTIAL_INSTANTIATION_P (type)) 522952284Sobrien /* There's no telling which specialization is appropriate at this 523052284Sobrien point. Since all peeking at the innards of this partial 523152284Sobrien instantiation are extensions (like the "implicit typename" 523252284Sobrien extension, which allows users to omit the keyword `typename' on 523352284Sobrien names that are declared as types in template base classes), we 523452284Sobrien are free to do what we please. 523552284Sobrien 523652284Sobrien Trying to figure out which partial instantiation to use can 523752284Sobrien cause a crash. (Some of the template arguments don't even have 523852284Sobrien types.) So, we just use the most general version. */ 523952284Sobrien t = NULL_TREE; 524052284Sobrien else 524118334Speter { 524252284Sobrien t = most_specialized_class (template, args); 524352284Sobrien 524452284Sobrien if (t == error_mark_node) 524518334Speter { 524652284Sobrien const char *str = "candidates are:"; 524790075Sobrien error ("ambiguous class template instantiation for `%#T'", type); 524852284Sobrien for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; 524952284Sobrien t = TREE_CHAIN (t)) 525050397Sobrien { 525152284Sobrien if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), 525252284Sobrien args)) 525352284Sobrien { 525452284Sobrien cp_error_at ("%s %+#T", str, TREE_TYPE (t)); 525552284Sobrien str = " "; 525652284Sobrien } 525750397Sobrien } 525852284Sobrien TYPE_BEING_DEFINED (type) = 1; 525990075Sobrien return error_mark_node; 526018334Speter } 526118334Speter } 526252284Sobrien 526352284Sobrien if (t) 526450397Sobrien pattern = TREE_TYPE (t); 526550397Sobrien else 526650397Sobrien pattern = TREE_TYPE (template); 526718334Speter 526852284Sobrien /* If the template we're instantiating is incomplete, then clearly 526952284Sobrien there's nothing we can do. */ 527090075Sobrien if (!COMPLETE_TYPE_P (pattern)) 527190075Sobrien return type; 527218334Speter 527352284Sobrien /* If this is a partial instantiation, don't tsubst anything. We will 527452284Sobrien only use this type for implicit typename, so the actual contents don't 527552284Sobrien matter. All that matters is whether a particular name is a type. */ 527652284Sobrien if (PARTIAL_INSTANTIATION_P (type)) 527752284Sobrien { 527852284Sobrien /* The fields set here must be kept in sync with those cleared 527952284Sobrien in begin_class_definition. */ 528052284Sobrien TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); 528152284Sobrien TYPE_FIELDS (type) = TYPE_FIELDS (pattern); 528252284Sobrien TYPE_METHODS (type) = TYPE_METHODS (pattern); 5283117395Skan CLASSTYPE_DECL_LIST (type) = CLASSTYPE_DECL_LIST (pattern); 5284117395Skan CLASSTYPE_NESTED_UDTS (type) = CLASSTYPE_NESTED_UDTS (pattern); 528590075Sobrien CLASSTYPE_VBASECLASSES (type) = CLASSTYPE_VBASECLASSES (pattern); 528690075Sobrien 528752284Sobrien /* Pretend that the type is complete, so that we will look 528852284Sobrien inside it during name lookup and such. */ 528990075Sobrien TYPE_SIZE (type) = bitsize_zero_node; 529090075Sobrien return type; 529152284Sobrien } 529218334Speter 529352284Sobrien /* If we've recursively instantiated too many templates, stop. */ 529452284Sobrien if (! push_tinst_level (type)) 529590075Sobrien return type; 529650397Sobrien 529752284Sobrien /* Now we're really doing the instantiation. Mark the type as in 529852284Sobrien the process of being defined. */ 529950397Sobrien TYPE_BEING_DEFINED (type) = 1; 530050397Sobrien 530150397Sobrien maybe_push_to_top_level (uses_template_parms (type)); 530250397Sobrien 530352284Sobrien if (t) 530452284Sobrien { 5305117395Skan /* This TYPE is actually an instantiation of a partial 530652284Sobrien specialization. We replace the innermost set of ARGS with 530752284Sobrien the arguments appropriate for substitution. For example, 530852284Sobrien given: 530950397Sobrien 531052284Sobrien template <class T> struct S {}; 531152284Sobrien template <class T> struct S<T*> {}; 531252284Sobrien 531352284Sobrien and supposing that we are instantiating S<int*>, ARGS will 531452284Sobrien present be {int*} but we need {int}. */ 531552284Sobrien tree inner_args 531652284Sobrien = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), 531752284Sobrien args); 531852284Sobrien 531952284Sobrien /* If there were multiple levels in ARGS, replacing the 532052284Sobrien innermost level would alter CLASSTYPE_TI_ARGS, which we don't 532152284Sobrien want, so we make a copy first. */ 532252284Sobrien if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) 532352284Sobrien { 532452284Sobrien args = copy_node (args); 532552284Sobrien SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); 532652284Sobrien } 532752284Sobrien else 532852284Sobrien args = inner_args; 532952284Sobrien } 533052284Sobrien 533150397Sobrien if (flag_external_templates) 533218334Speter { 533350397Sobrien if (flag_alt_external_templates) 533450397Sobrien { 533550397Sobrien CLASSTYPE_INTERFACE_ONLY (type) = interface_only; 533650397Sobrien SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); 533750397Sobrien } 533850397Sobrien else 533950397Sobrien { 534050397Sobrien CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); 534150397Sobrien SET_CLASSTYPE_INTERFACE_UNKNOWN_X 534250397Sobrien (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); 534350397Sobrien } 534418334Speter } 534518334Speter else 534618334Speter { 534750397Sobrien SET_CLASSTYPE_INTERFACE_UNKNOWN (type); 534850397Sobrien } 534918334Speter 535050397Sobrien TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); 535150397Sobrien TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); 535250397Sobrien TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); 535350397Sobrien TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); 535450397Sobrien TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); 535590075Sobrien TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); 535690075Sobrien TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); 535750397Sobrien TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); 535850397Sobrien TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); 535950397Sobrien TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); 536050397Sobrien TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); 536150397Sobrien TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); 536250397Sobrien TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); 536350397Sobrien TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); 536450397Sobrien TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); 536590075Sobrien TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type) 536690075Sobrien = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern); 536750397Sobrien TYPE_USES_MULTIPLE_INHERITANCE (type) 536850397Sobrien = TYPE_USES_MULTIPLE_INHERITANCE (pattern); 536950397Sobrien TYPE_USES_VIRTUAL_BASECLASSES (type) 537050397Sobrien = TYPE_USES_VIRTUAL_BASECLASSES (pattern); 537150397Sobrien TYPE_PACKED (type) = TYPE_PACKED (pattern); 537250397Sobrien TYPE_ALIGN (type) = TYPE_ALIGN (pattern); 537390075Sobrien TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); 537450397Sobrien TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ 537590075Sobrien if (ANON_AGGR_TYPE_P (pattern)) 537690075Sobrien SET_ANON_AGGR_TYPE_P (type); 537718334Speter 5378117395Skan if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))) 5379117395Skan /* First instantiate our enclosing class. */ 5380117395Skan complete_type (TYPE_CONTEXT (type)); 5381117395Skan 538252284Sobrien if (TYPE_BINFO_BASETYPES (pattern)) 538350397Sobrien { 538452284Sobrien tree base_list = NULL_TREE; 538552284Sobrien tree pbases = TYPE_BINFO_BASETYPES (pattern); 538652284Sobrien int i; 538750397Sobrien 538852284Sobrien /* Substitute into each of the bases to determine the actual 538952284Sobrien basetypes. */ 539052284Sobrien for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) 539152284Sobrien { 539252284Sobrien tree base; 539352284Sobrien tree access; 539452284Sobrien tree pbase; 539550397Sobrien 539652284Sobrien pbase = TREE_VEC_ELT (pbases, i); 539750397Sobrien 539890075Sobrien /* Substitute to figure out the base class. */ 539996263Sobrien base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE); 540052284Sobrien if (base == error_mark_node) 540152284Sobrien continue; 540250397Sobrien 540352284Sobrien /* Calculate the correct access node. */ 540452284Sobrien if (TREE_VIA_VIRTUAL (pbase)) 540552284Sobrien { 540652284Sobrien if (TREE_VIA_PUBLIC (pbase)) 540752284Sobrien access = access_public_virtual_node; 540852284Sobrien else if (TREE_VIA_PROTECTED (pbase)) 540952284Sobrien access = access_protected_virtual_node; 541052284Sobrien else 541152284Sobrien access = access_private_virtual_node; 541252284Sobrien } 541352284Sobrien else 541452284Sobrien { 541552284Sobrien if (TREE_VIA_PUBLIC (pbase)) 541652284Sobrien access = access_public_node; 541752284Sobrien else if (TREE_VIA_PROTECTED (pbase)) 541852284Sobrien access = access_protected_node; 541952284Sobrien else 542052284Sobrien access = access_private_node; 542152284Sobrien } 542250397Sobrien 542352284Sobrien base_list = tree_cons (access, base, base_list); 542452284Sobrien } 542550397Sobrien 542652284Sobrien /* The list is now in reverse order; correct that. */ 542752284Sobrien base_list = nreverse (base_list); 542850397Sobrien 542952284Sobrien /* Now call xref_basetypes to set up all the base-class 543052284Sobrien information. */ 5431117395Skan xref_basetypes (type, base_list); 543252284Sobrien } 543350397Sobrien 543452284Sobrien /* Now that our base classes are set up, enter the scope of the 543552284Sobrien class, so that name lookups into base classes, etc. will work 543690075Sobrien correctly. This is precisely analogous to what we do in 543752284Sobrien begin_class_definition when defining an ordinary non-template 543852284Sobrien class. */ 543952284Sobrien pushclass (type, 1); 544052284Sobrien 5441117395Skan /* Now members are processed in the order of declaration. */ 5442117395Skan for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member)) 544350397Sobrien { 5444117395Skan tree t = TREE_VALUE (member); 544550397Sobrien 5446117395Skan if (TREE_PURPOSE (member)) 544718334Speter { 5448117395Skan if (TYPE_P (t)) 5449117395Skan { 5450117395Skan /* Build new CLASSTYPE_NESTED_UDTS. */ 545152284Sobrien 5452117395Skan tree tag = t; 5453117395Skan tree name = TYPE_IDENTIFIER (tag); 5454117395Skan tree newtag; 545518334Speter 5456117395Skan newtag = tsubst (tag, args, tf_error, NULL_TREE); 5457117395Skan my_friendly_assert (newtag != error_mark_node, 20010206); 5458117395Skan if (TREE_CODE (newtag) != ENUMERAL_TYPE) 5459117395Skan { 5460117395Skan if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) 5461117395Skan /* Unfortunately, lookup_template_class sets 5462117395Skan CLASSTYPE_IMPLICIT_INSTANTIATION for a partial 5463117395Skan instantiation (i.e., for the type of a member template 5464117395Skan class nested within a template class.) This behavior is 5465117395Skan required for maybe_process_partial_specialization to work 5466117395Skan correctly, but is not accurate in this case; the TAG is not 5467117395Skan an instantiation of anything. (The corresponding 5468117395Skan TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ 5469117395Skan CLASSTYPE_USE_TEMPLATE (newtag) = 0; 547052284Sobrien 5471117395Skan /* Now, we call pushtag to put this NEWTAG into the scope of 5472117395Skan TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid 5473117395Skan pushtag calling push_template_decl. We don't have to do 5474117395Skan this for enums because it will already have been done in 5475117395Skan tsubst_enum. */ 5476117395Skan if (name) 5477117395Skan SET_IDENTIFIER_TYPE_VALUE (name, newtag); 5478117395Skan pushtag (name, newtag, /*globalize=*/0); 5479117395Skan } 5480117395Skan } 5481117395Skan else if (TREE_CODE (t) == FUNCTION_DECL 5482117395Skan || DECL_FUNCTION_TEMPLATE_P (t)) 5483117395Skan { 5484117395Skan /* Build new TYPE_METHODS. */ 548552284Sobrien 5486117395Skan tree r = tsubst (t, args, tf_error, NULL_TREE); 5487117395Skan set_current_access_from_decl (r); 5488117395Skan grok_special_member_properties (r); 5489117395Skan finish_member_declaration (r); 5490117395Skan } 5491117395Skan else 5492117395Skan { 5493117395Skan /* Build new TYPE_FIELDS. */ 549452284Sobrien 5495117395Skan if (TREE_CODE (t) != CONST_DECL) 5496117395Skan { 5497117395Skan tree r; 549852284Sobrien 5499117395Skan /* The the file and line for this declaration, to assist 5500117395Skan in error message reporting. Since we called 5501117395Skan push_tinst_level above, we don't need to restore these. */ 5502117395Skan lineno = DECL_SOURCE_LINE (t); 5503117395Skan input_filename = DECL_SOURCE_FILE (t); 550452284Sobrien 5505117395Skan r = tsubst (t, args, tf_error | tf_warning, NULL_TREE); 5506117395Skan if (TREE_CODE (r) == VAR_DECL) 5507117395Skan { 5508117395Skan tree init; 5509102780Skan 5510117395Skan if (DECL_INITIALIZED_IN_CLASS_P (r)) 5511117395Skan init = tsubst_expr (DECL_INITIAL (t), args, 5512117395Skan tf_error | tf_warning, NULL_TREE); 5513117395Skan else 5514117395Skan init = NULL_TREE; 551550397Sobrien 5516117395Skan finish_static_data_member_decl (r, init, 5517117395Skan /*asmspec_tree=*/NULL_TREE, 5518117395Skan /*flags=*/0); 551950397Sobrien 5520117395Skan if (DECL_INITIALIZED_IN_CLASS_P (r)) 5521117395Skan check_static_variable_definition (r, TREE_TYPE (r)); 5522117395Skan } 5523117395Skan else if (TREE_CODE (r) == FIELD_DECL) 5524117395Skan { 5525117395Skan /* Determine whether R has a valid type and can be 5526117395Skan completed later. If R is invalid, then it is 5527117395Skan replaced by error_mark_node so that it will not be 5528117395Skan added to TYPE_FIELDS. */ 5529117395Skan tree rtype = TREE_TYPE (r); 5530117395Skan if (can_complete_type_without_circularity (rtype)) 5531117395Skan complete_type (rtype); 553250397Sobrien 5533117395Skan if (!COMPLETE_TYPE_P (rtype)) 5534117395Skan { 5535117395Skan cxx_incomplete_type_error (r, rtype); 5536117395Skan r = error_mark_node; 5537117395Skan } 5538117395Skan } 553950397Sobrien 5540117395Skan /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, 5541117395Skan such a thing will already have been added to the field 5542117395Skan list by tsubst_enum in finish_member_declaration in the 5543117395Skan CLASSTYPE_NESTED_UDTS case above. */ 5544117395Skan if (!(TREE_CODE (r) == TYPE_DECL 5545117395Skan && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE 5546117395Skan && DECL_ARTIFICIAL (r))) 5547117395Skan { 5548117395Skan set_current_access_from_decl (r); 5549117395Skan finish_member_declaration (r); 5550117395Skan } 5551117395Skan } 5552117395Skan } 5553117395Skan } 5554117395Skan else 555590075Sobrien { 5556117395Skan if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) 5557117395Skan { 5558117395Skan /* Build new CLASSTYPE_FRIEND_CLASSES. */ 555950397Sobrien 5560117395Skan tree friend_type = t; 5561117395Skan tree new_friend_type; 556252284Sobrien 5563117395Skan if (TREE_CODE (friend_type) == TEMPLATE_DECL) 5564117395Skan new_friend_type = tsubst_friend_class (friend_type, args); 5565117395Skan else if (uses_template_parms (friend_type)) 5566117395Skan new_friend_type = tsubst (friend_type, args, 5567117395Skan tf_error | tf_warning, NULL_TREE); 5568117395Skan else 5569117395Skan { 5570117395Skan tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); 557150397Sobrien 5572117395Skan /* The call to xref_tag_from_type does injection for friend 5573117395Skan classes. */ 5574117395Skan push_nested_namespace (ns); 5575117395Skan new_friend_type = 5576117395Skan xref_tag_from_type (friend_type, NULL_TREE, 1); 5577117395Skan pop_nested_namespace (ns); 5578117395Skan } 557950397Sobrien 5580117395Skan if (TREE_CODE (friend_type) == TEMPLATE_DECL) 5581117395Skan /* Trick make_friend_class into realizing that the friend 5582117395Skan we're adding is a template, not an ordinary class. It's 5583117395Skan important that we use make_friend_class since it will 5584117395Skan perform some error-checking and output cross-reference 5585117395Skan information. */ 5586117395Skan ++processing_template_decl; 5587117395Skan 5588117395Skan if (new_friend_type != error_mark_node) 5589117395Skan make_friend_class (type, new_friend_type); 5590117395Skan 5591117395Skan if (TREE_CODE (friend_type) == TEMPLATE_DECL) 5592117395Skan --processing_template_decl; 5593117395Skan } 5594117395Skan else 5595117395Skan /* Build new DECL_FRIENDLIST. */ 5596117395Skan add_friend (type, tsubst_friend_function (t, args)); 5597117395Skan } 559850397Sobrien } 559950397Sobrien 560052284Sobrien /* Set the file and line number information to whatever is given for 560152284Sobrien the class itself. This puts error messages involving generated 560252284Sobrien implicit functions at a predictable point, and the same point 560352284Sobrien that would be used for non-template classes. */ 5604117395Skan typedecl = TYPE_MAIN_DECL (type); 560552284Sobrien lineno = DECL_SOURCE_LINE (typedecl); 560652284Sobrien input_filename = DECL_SOURCE_FILE (typedecl); 560752284Sobrien 560852284Sobrien unreverse_member_declarations (type); 560990075Sobrien finish_struct_1 (type); 561050397Sobrien CLASSTYPE_GOT_SEMICOLON (type) = 1; 561150397Sobrien 561252284Sobrien /* Clear this now so repo_template_used is happy. */ 561352284Sobrien TYPE_BEING_DEFINED (type) = 0; 561450397Sobrien repo_template_used (type); 561550397Sobrien 561690075Sobrien /* Now that the class is complete, instantiate default arguments for 561790075Sobrien any member functions. We don't do this earlier because the 561890075Sobrien default arguments may reference members of the class. */ 561990075Sobrien if (!PRIMARY_TEMPLATE_P (template)) 562090075Sobrien for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) 562190075Sobrien if (TREE_CODE (t) == FUNCTION_DECL 562290075Sobrien /* Implicitly generated member functions will not have template 562390075Sobrien information; they are not instantiations, but instead are 562490075Sobrien created "fresh" for each instantiation. */ 562590075Sobrien && DECL_TEMPLATE_INFO (t)) 562690075Sobrien tsubst_default_arguments (t); 562790075Sobrien 562852284Sobrien popclass (); 562950397Sobrien pop_from_top_level (); 563050397Sobrien pop_tinst_level (); 563150397Sobrien 5632117395Skan if (TYPE_CONTAINS_VPTR_P (type)) 5633117395Skan keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); 5634117395Skan 563550397Sobrien return type; 563618334Speter} 563718334Speter 563818334Speterstatic int 563918334Speterlist_eq (t1, t2) 564018334Speter tree t1, t2; 564118334Speter{ 564218334Speter if (t1 == NULL_TREE) 564318334Speter return t2 == NULL_TREE; 564418334Speter if (t2 == NULL_TREE) 564518334Speter return 0; 564618334Speter /* Don't care if one declares its arg const and the other doesn't -- the 564718334Speter main variant of the arg type is all that matters. */ 564818334Speter if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) 564918334Speter != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) 565018334Speter return 0; 565118334Speter return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); 565218334Speter} 565318334Speter 565450397Sobrien/* If arg is a non-type template parameter that does not depend on template 565550397Sobrien arguments, fold it like we weren't in the body of a template. */ 565650397Sobrien 565718334Speterstatic tree 565850397Sobrienmaybe_fold_nontype_arg (arg) 565950397Sobrien tree arg; 566018334Speter{ 566190075Sobrien if (arg && !TYPE_P (arg) && !uses_template_parms (arg)) 566250397Sobrien { 566350397Sobrien /* Sometimes, one of the args was an expression involving a 566450397Sobrien template constant parameter, like N - 1. Now that we've 566550397Sobrien tsubst'd, we might have something like 2 - 1. This will 566650397Sobrien confuse lookup_template_class, so we do constant folding 566750397Sobrien here. We have to unset processing_template_decl, to 566850397Sobrien fool build_expr_from_tree() into building an actual 566950397Sobrien tree. */ 567018334Speter 567190075Sobrien /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already 567290075Sobrien as simple as it's going to get, and trying to reprocess 567390075Sobrien the trees will break. */ 567490075Sobrien if (!TREE_TYPE (arg)) 567590075Sobrien { 567690075Sobrien int saved_processing_template_decl = processing_template_decl; 567790075Sobrien processing_template_decl = 0; 567890075Sobrien arg = build_expr_from_tree (arg); 567990075Sobrien processing_template_decl = saved_processing_template_decl; 568090075Sobrien } 568190075Sobrien 568290075Sobrien arg = fold (arg); 568350397Sobrien } 568450397Sobrien return arg; 568550397Sobrien} 568650397Sobrien 568750397Sobrien/* Substitute ARGS into the vector of template arguments T. */ 568850397Sobrien 568952284Sobrienstatic tree 569052284Sobrientsubst_template_arg_vector (t, args, complain) 569150397Sobrien tree t; 569250397Sobrien tree args; 569396263Sobrien tsubst_flags_t complain; 569450397Sobrien{ 569550397Sobrien int len = TREE_VEC_LENGTH (t), need_new = 0, i; 569650397Sobrien tree *elts = (tree *) alloca (len * sizeof (tree)); 569750397Sobrien 569890075Sobrien memset ((char *) elts, 0, len * sizeof (tree)); 569950397Sobrien 570050397Sobrien for (i = 0; i < len; i++) 570150397Sobrien { 570250397Sobrien if (TREE_VEC_ELT (t, i) != NULL_TREE 570350397Sobrien && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) 570452284Sobrien elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), 570552284Sobrien args, complain); 570650397Sobrien else 570750397Sobrien elts[i] = maybe_fold_nontype_arg 570852284Sobrien (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, 570952284Sobrien NULL_TREE)); 571050397Sobrien 571190075Sobrien if (elts[i] == error_mark_node) 571290075Sobrien return error_mark_node; 571390075Sobrien 571450397Sobrien if (elts[i] != TREE_VEC_ELT (t, i)) 571550397Sobrien need_new = 1; 571650397Sobrien } 571750397Sobrien 571850397Sobrien if (!need_new) 571950397Sobrien return t; 572050397Sobrien 572190075Sobrien t = make_tree_vec (len); 572250397Sobrien for (i = 0; i < len; i++) 572350397Sobrien TREE_VEC_ELT (t, i) = elts[i]; 572450397Sobrien 572518334Speter return t; 572618334Speter} 572718334Speter 572852284Sobrien/* Return the result of substituting ARGS into the template parameters 572952284Sobrien given by PARMS. If there are m levels of ARGS and m + n levels of 573052284Sobrien PARMS, then the result will contain n levels of PARMS. For 573152284Sobrien example, if PARMS is `template <class T> template <class U> 573252284Sobrien template <T*, U, class V>' and ARGS is {{int}, {double}} then the 573352284Sobrien result will be `template <int*, double, class V>'. */ 573450397Sobrien 573552284Sobrienstatic tree 573652284Sobrientsubst_template_parms (parms, args, complain) 573752284Sobrien tree parms; 573852284Sobrien tree args; 573996263Sobrien tsubst_flags_t complain; 574018334Speter{ 574190075Sobrien tree r = NULL_TREE; 574290075Sobrien tree* new_parms; 574318334Speter 574452284Sobrien for (new_parms = &r; 574552284Sobrien TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); 574652284Sobrien new_parms = &(TREE_CHAIN (*new_parms)), 574752284Sobrien parms = TREE_CHAIN (parms)) 574852284Sobrien { 574952284Sobrien tree new_vec = 575052284Sobrien make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); 575152284Sobrien int i; 575252284Sobrien 575352284Sobrien for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) 575452284Sobrien { 575590075Sobrien tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); 575690075Sobrien tree default_value = TREE_PURPOSE (tuple); 575790075Sobrien tree parm_decl = TREE_VALUE (tuple); 575890075Sobrien 575990075Sobrien parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); 576090075Sobrien default_value = tsubst_expr (default_value, args, 576190075Sobrien complain, NULL_TREE); 576290075Sobrien tuple = build_tree_list (maybe_fold_nontype_arg (default_value), 576390075Sobrien parm_decl); 576490075Sobrien TREE_VEC_ELT (new_vec, i) = tuple; 576552284Sobrien } 576652284Sobrien 576752284Sobrien *new_parms = 576890075Sobrien tree_cons (size_int (TMPL_PARMS_DEPTH (parms) 576990075Sobrien - TMPL_ARGS_DEPTH (args)), 577052284Sobrien new_vec, NULL_TREE); 577152284Sobrien } 577218334Speter 577352284Sobrien return r; 577452284Sobrien} 577518334Speter 577652284Sobrien/* Substitute the ARGS into the indicated aggregate (or enumeration) 577752284Sobrien type T. If T is not an aggregate or enumeration type, it is 577852284Sobrien handled as if by tsubst. IN_DECL is as for tsubst. If 5779117395Skan ENTERING_SCOPE is nonzero, T is the context for a template which 578090075Sobrien we are presently tsubst'ing. Return the substituted value. */ 578150397Sobrien 578252284Sobrienstatic tree 578352284Sobrientsubst_aggr_type (t, args, complain, in_decl, entering_scope) 578452284Sobrien tree t; 578552284Sobrien tree args; 578696263Sobrien tsubst_flags_t complain; 578752284Sobrien tree in_decl; 578852284Sobrien int entering_scope; 578952284Sobrien{ 579052284Sobrien if (t == NULL_TREE) 579152284Sobrien return NULL_TREE; 579252284Sobrien 579318334Speter switch (TREE_CODE (t)) 579418334Speter { 579518334Speter case RECORD_TYPE: 579618334Speter if (TYPE_PTRMEMFUNC_P (t)) 579790075Sobrien return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); 579850397Sobrien 579950397Sobrien /* else fall through */ 580052284Sobrien case ENUMERAL_TYPE: 580150397Sobrien case UNION_TYPE: 580252284Sobrien if (TYPE_TEMPLATE_INFO (t)) 580350397Sobrien { 580452284Sobrien tree argvec; 580550397Sobrien tree context; 580650397Sobrien tree r; 580750397Sobrien 580852284Sobrien /* First, determine the context for the type we are looking 580952284Sobrien up. */ 581050397Sobrien if (TYPE_CONTEXT (t) != NULL_TREE) 581152284Sobrien context = tsubst_aggr_type (TYPE_CONTEXT (t), args, 581252284Sobrien complain, 581352284Sobrien in_decl, /*entering_scope=*/1); 581450397Sobrien else 581550397Sobrien context = NULL_TREE; 581650397Sobrien 581752284Sobrien /* Then, figure out what arguments are appropriate for the 581852284Sobrien type we are trying to find. For example, given: 581950397Sobrien 582052284Sobrien template <class T> struct S; 582152284Sobrien template <class T, class U> void f(T, U) { S<U> su; } 582250397Sobrien 582352284Sobrien and supposing that we are instantiating f<int, double>, 582452284Sobrien then our ARGS will be {int, double}, but, when looking up 582552284Sobrien S we only want {double}. */ 582652284Sobrien argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, 582752284Sobrien complain); 582890075Sobrien if (argvec == error_mark_node) 582990075Sobrien return error_mark_node; 583018334Speter 583152284Sobrien r = lookup_template_class (t, argvec, in_decl, context, 583290075Sobrien entering_scope, complain); 583350397Sobrien 583496263Sobrien return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); 583552284Sobrien } 583652284Sobrien else 583752284Sobrien /* This is not a template type, so there's nothing to do. */ 583818334Speter return t; 583918334Speter 584052284Sobrien default: 584152284Sobrien return tsubst (t, args, complain, in_decl); 584252284Sobrien } 584352284Sobrien} 584418334Speter 584590075Sobrien/* Substitute into the default argument ARG (a default argument for 584690075Sobrien FN), which has the indicated TYPE. */ 584790075Sobrien 584890075Sobrientree 584990075Sobrientsubst_default_argument (fn, type, arg) 585090075Sobrien tree fn; 585190075Sobrien tree type; 585290075Sobrien tree arg; 585390075Sobrien{ 585490075Sobrien /* This default argument came from a template. Instantiate the 585590075Sobrien default argument here, not in tsubst. In the case of 585690075Sobrien something like: 585790075Sobrien 585890075Sobrien template <class T> 585990075Sobrien struct S { 586090075Sobrien static T t(); 586190075Sobrien void f(T = t()); 586290075Sobrien }; 586390075Sobrien 586490075Sobrien we must be careful to do name lookup in the scope of S<T>, 5865117395Skan rather than in the current class. 586690075Sobrien 5867117395Skan ??? current_class_type affects a lot more than name lookup. This is 5868117395Skan very fragile. Fortunately, it will go away when we do 2-phase name 5869117395Skan binding properly. */ 5870117395Skan 5871117395Skan /* FN is already the desired FUNCTION_DECL. */ 5872117395Skan push_access_scope (fn); 5873117395Skan 587496263Sobrien arg = tsubst_expr (arg, DECL_TI_ARGS (fn), 587596263Sobrien tf_error | tf_warning, NULL_TREE); 587690075Sobrien 5877117395Skan pop_access_scope (fn); 587890075Sobrien 587990075Sobrien /* Make sure the default argument is reasonable. */ 588090075Sobrien arg = check_default_argument (type, arg); 588190075Sobrien 588290075Sobrien return arg; 588390075Sobrien} 588490075Sobrien 588590075Sobrien/* Substitute into all the default arguments for FN. */ 588690075Sobrien 588790075Sobrienstatic void 588890075Sobrientsubst_default_arguments (fn) 588990075Sobrien tree fn; 589090075Sobrien{ 589190075Sobrien tree arg; 589290075Sobrien tree tmpl_args; 589390075Sobrien 589490075Sobrien tmpl_args = DECL_TI_ARGS (fn); 589590075Sobrien 589690075Sobrien /* If this function is not yet instantiated, we certainly don't need 589790075Sobrien its default arguments. */ 589890075Sobrien if (uses_template_parms (tmpl_args)) 589990075Sobrien return; 590090075Sobrien 590190075Sobrien for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); 590290075Sobrien arg; 590390075Sobrien arg = TREE_CHAIN (arg)) 590490075Sobrien if (TREE_PURPOSE (arg)) 590590075Sobrien TREE_PURPOSE (arg) = tsubst_default_argument (fn, 590690075Sobrien TREE_VALUE (arg), 590790075Sobrien TREE_PURPOSE (arg)); 590890075Sobrien} 590990075Sobrien 591052284Sobrien/* Substitute the ARGS into the T, which is a _DECL. TYPE is the 591152284Sobrien (already computed) substitution of ARGS into TREE_TYPE (T), if 591296263Sobrien appropriate. Return the result of the substitution. Issue error 591396263Sobrien and warning messages under control of COMPLAIN. */ 591450397Sobrien 591552284Sobrienstatic tree 591696263Sobrientsubst_decl (t, args, type, complain) 591752284Sobrien tree t; 591852284Sobrien tree args; 591952284Sobrien tree type; 592096263Sobrien tsubst_flags_t complain; 592152284Sobrien{ 592252284Sobrien int saved_lineno; 592390075Sobrien const char *saved_filename; 592452284Sobrien tree r = NULL_TREE; 592590075Sobrien tree in_decl = t; 592618334Speter 592752284Sobrien /* Set the filename and linenumber to improve error-reporting. */ 592852284Sobrien saved_lineno = lineno; 592952284Sobrien saved_filename = input_filename; 593052284Sobrien lineno = DECL_SOURCE_LINE (t); 593152284Sobrien input_filename = DECL_SOURCE_FILE (t); 593218334Speter 593352284Sobrien switch (TREE_CODE (t)) 593452284Sobrien { 593550397Sobrien case TEMPLATE_DECL: 593650397Sobrien { 593750397Sobrien /* We can get here when processing a member template function 593850397Sobrien of a template class. */ 593950397Sobrien tree decl = DECL_TEMPLATE_RESULT (t); 594050397Sobrien tree spec; 594150397Sobrien int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); 594218334Speter 594350397Sobrien if (!is_template_template_parm) 594450397Sobrien { 594552284Sobrien /* We might already have an instance of this template. 594652284Sobrien The ARGS are for the surrounding class type, so the 594752284Sobrien full args contain the tsubst'd args for the context, 594852284Sobrien plus the innermost args from the template decl. */ 594952284Sobrien tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) 595052284Sobrien ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) 595190075Sobrien : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); 595252284Sobrien tree full_args; 595352284Sobrien 595452284Sobrien full_args = tsubst_template_arg_vector (tmpl_args, args, 595596263Sobrien complain); 595652284Sobrien 595752284Sobrien /* tsubst_template_arg_vector doesn't copy the vector if 595852284Sobrien nothing changed. But, *something* should have 595952284Sobrien changed. */ 596052284Sobrien my_friendly_assert (full_args != tmpl_args, 0); 596152284Sobrien 596252284Sobrien spec = retrieve_specialization (t, full_args); 596350397Sobrien if (spec != NULL_TREE) 596452284Sobrien { 596552284Sobrien r = spec; 596652284Sobrien break; 596752284Sobrien } 596850397Sobrien } 596918334Speter 597050397Sobrien /* Make a new template decl. It will be similar to the 597150397Sobrien original, but will record the current template arguments. 597250397Sobrien We also create a new function declaration, which is just 597350397Sobrien like the old one, but points to this new template, rather 597450397Sobrien than the old one. */ 597590075Sobrien r = copy_decl (t); 597652284Sobrien my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); 597752284Sobrien TREE_CHAIN (r) = NULL_TREE; 597818334Speter 597950397Sobrien if (is_template_template_parm) 598050397Sobrien { 598196263Sobrien tree new_decl = tsubst (decl, args, complain, in_decl); 598290075Sobrien DECL_TEMPLATE_RESULT (r) = new_decl; 598352284Sobrien TREE_TYPE (r) = TREE_TYPE (new_decl); 598452284Sobrien break; 598550397Sobrien } 598618334Speter 598752284Sobrien DECL_CONTEXT (r) 598890075Sobrien = tsubst_aggr_type (DECL_CONTEXT (t), args, 598996263Sobrien complain, in_decl, 599052284Sobrien /*entering_scope=*/1); 599152284Sobrien DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); 599218334Speter 599350397Sobrien if (TREE_CODE (decl) == TYPE_DECL) 599450397Sobrien { 599596263Sobrien tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 599652284Sobrien TREE_TYPE (r) = new_type; 599752284Sobrien CLASSTYPE_TI_TEMPLATE (new_type) = r; 599890075Sobrien DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); 599952284Sobrien DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); 600050397Sobrien } 600150397Sobrien else 600250397Sobrien { 600396263Sobrien tree new_decl = tsubst (decl, args, complain, in_decl); 600490075Sobrien 600590075Sobrien DECL_TEMPLATE_RESULT (r) = new_decl; 600652284Sobrien DECL_TI_TEMPLATE (new_decl) = r; 600752284Sobrien TREE_TYPE (r) = TREE_TYPE (new_decl); 600852284Sobrien DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); 600950397Sobrien } 601018334Speter 601152284Sobrien SET_DECL_IMPLICIT_INSTANTIATION (r); 601252284Sobrien DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; 601352284Sobrien DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; 601450397Sobrien 601550397Sobrien /* The template parameters for this new template are all the 601650397Sobrien template parameters for the old template, except the 6017117395Skan outermost level of parameters. */ 601852284Sobrien DECL_TEMPLATE_PARMS (r) 601952284Sobrien = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, 602096263Sobrien complain); 602150397Sobrien 602250397Sobrien if (PRIMARY_TEMPLATE_P (t)) 602352284Sobrien DECL_PRIMARY_TEMPLATE (r) = r; 602450397Sobrien 602550397Sobrien /* We don't partially instantiate partial specializations. */ 602650397Sobrien if (TREE_CODE (decl) == TYPE_DECL) 602752284Sobrien break; 602850397Sobrien 602950397Sobrien /* Record this partial instantiation. */ 603052284Sobrien register_specialization (r, t, 603190075Sobrien DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r))); 603250397Sobrien 603350397Sobrien } 603452284Sobrien break; 603550397Sobrien 603650397Sobrien case FUNCTION_DECL: 603750397Sobrien { 603850397Sobrien tree ctx; 603952284Sobrien tree argvec = NULL_TREE; 604052284Sobrien tree *friends; 604152284Sobrien tree gen_tmpl; 604250397Sobrien int member; 604352284Sobrien int args_depth; 604452284Sobrien int parms_depth; 604550397Sobrien 604652284Sobrien /* Nobody should be tsubst'ing into non-template functions. */ 604752284Sobrien my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); 604852284Sobrien 604952284Sobrien if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) 605052284Sobrien { 605152284Sobrien tree spec; 605252284Sobrien 6053117395Skan /* If T is not dependent, just return it. */ 6054117395Skan if (!uses_template_parms (DECL_TI_ARGS (t))) 6055117395Skan return t; 6056117395Skan 605752284Sobrien /* Calculate the most general template of which R is a 605852284Sobrien specialization, and the complete set of arguments used to 605952284Sobrien specialize R. */ 606052284Sobrien gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); 606152284Sobrien argvec 606252284Sobrien = tsubst_template_arg_vector (DECL_TI_ARGS 606352284Sobrien (DECL_TEMPLATE_RESULT (gen_tmpl)), 606496263Sobrien args, complain); 606552284Sobrien 606652284Sobrien /* Check to see if we already have this specialization. */ 606752284Sobrien spec = retrieve_specialization (gen_tmpl, argvec); 606852284Sobrien 606952284Sobrien if (spec) 607052284Sobrien { 607152284Sobrien r = spec; 607252284Sobrien break; 607352284Sobrien } 607452284Sobrien 607590075Sobrien /* We can see more levels of arguments than parameters if 607690075Sobrien there was a specialization of a member template, like 607790075Sobrien this: 607852284Sobrien 607990075Sobrien template <class T> struct S { template <class U> void f(); } 608090075Sobrien template <> template <class U> void S<int>::f(U); 608152284Sobrien 608290075Sobrien Here, we'll be substituting into the specialization, 608390075Sobrien because that's where we can find the code we actually 608490075Sobrien want to generate, but we'll have enough arguments for 608590075Sobrien the most general template. 608690075Sobrien 608790075Sobrien We also deal with the peculiar case: 608890075Sobrien 608952284Sobrien template <class T> struct S { 609052284Sobrien template <class U> friend void f(); 609152284Sobrien }; 609290075Sobrien template <class U> void f() {} 609352284Sobrien template S<int>; 609452284Sobrien template void f<double>(); 609552284Sobrien 609652284Sobrien Here, the ARGS for the instantiation of will be {int, 609752284Sobrien double}. But, we only need as many ARGS as there are 609852284Sobrien levels of template parameters in CODE_PATTERN. We are 609952284Sobrien careful not to get fooled into reducing the ARGS in 610052284Sobrien situations like: 610152284Sobrien 610252284Sobrien template <class T> struct S { template <class U> void f(U); } 610352284Sobrien template <class T> template <> void S<T>::f(int) {} 610452284Sobrien 610552284Sobrien which we can spot because the pattern will be a 610652284Sobrien specialization in this case. */ 610752284Sobrien args_depth = TMPL_ARGS_DEPTH (args); 610852284Sobrien parms_depth = 610952284Sobrien TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); 611052284Sobrien if (args_depth > parms_depth 611152284Sobrien && !DECL_TEMPLATE_SPECIALIZATION (t)) 611290075Sobrien args = get_innermost_template_args (args, parms_depth); 611352284Sobrien } 611452284Sobrien else 611552284Sobrien { 611652284Sobrien /* This special case arises when we have something like this: 611752284Sobrien 611852284Sobrien template <class T> struct S { 611952284Sobrien friend void f<int>(int, double); 612052284Sobrien }; 612152284Sobrien 612252284Sobrien Here, the DECL_TI_TEMPLATE for the friend declaration 612352284Sobrien will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are 612452284Sobrien being called from tsubst_friend_function, and we want 612552284Sobrien only to create a new decl (R) with appropriate types so 612652284Sobrien that we can call determine_specialization. */ 612752284Sobrien my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) 612852284Sobrien == LOOKUP_EXPR) 612952284Sobrien || (TREE_CODE (DECL_TI_TEMPLATE (t)) 613052284Sobrien == IDENTIFIER_NODE), 0); 613152284Sobrien gen_tmpl = NULL_TREE; 613252284Sobrien } 613352284Sobrien 613450397Sobrien if (DECL_CLASS_SCOPE_P (t)) 613550397Sobrien { 613650397Sobrien if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) 613750397Sobrien member = 2; 613850397Sobrien else 613950397Sobrien member = 1; 614090075Sobrien ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, 614196263Sobrien complain, t, 614252284Sobrien /*entering_scope=*/1); 614350397Sobrien } 614418334Speter else 614518334Speter { 614650397Sobrien member = 0; 614790075Sobrien ctx = DECL_CONTEXT (t); 614850397Sobrien } 614996263Sobrien type = tsubst (type, args, complain, in_decl); 615090075Sobrien if (type == error_mark_node) 615190075Sobrien return error_mark_node; 615218334Speter 615350397Sobrien /* We do NOT check for matching decls pushed separately at this 615450397Sobrien point, as they may not represent instantiations of this 615550397Sobrien template, and in any case are considered separate under the 615690075Sobrien discrete model. */ 615790075Sobrien r = copy_decl (t); 615850397Sobrien DECL_USE_TEMPLATE (r) = 0; 615950397Sobrien TREE_TYPE (r) = type; 616090075Sobrien /* Clear out the mangled name and RTL for the instantiation. */ 616190075Sobrien SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); 616290075Sobrien SET_DECL_RTL (r, NULL_RTX); 616350397Sobrien 616490075Sobrien DECL_CONTEXT (r) = ctx; 616550397Sobrien 616690075Sobrien if (member && DECL_CONV_FN_P (r)) 616752284Sobrien /* Type-conversion operator. Reconstruct the name, in 616852284Sobrien case it's the name of one of the template's parameters. */ 616990075Sobrien DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); 617050397Sobrien 617152284Sobrien DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, 617296263Sobrien complain, t); 617350397Sobrien DECL_RESULT (r) = NULL_TREE; 617450397Sobrien 617550397Sobrien TREE_STATIC (r) = 0; 617650397Sobrien TREE_PUBLIC (r) = TREE_PUBLIC (t); 617718334Speter DECL_EXTERNAL (r) = 1; 617818334Speter DECL_INTERFACE_KNOWN (r) = 0; 617950397Sobrien DECL_DEFER_OUTPUT (r) = 0; 618050397Sobrien TREE_CHAIN (r) = NULL_TREE; 618150397Sobrien DECL_PENDING_INLINE_INFO (r) = 0; 618290075Sobrien DECL_PENDING_INLINE_P (r) = 0; 618390075Sobrien DECL_SAVED_TREE (r) = NULL_TREE; 618450397Sobrien TREE_USED (r) = 0; 618590075Sobrien if (DECL_CLONED_FUNCTION (r)) 618690075Sobrien { 618790075Sobrien DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), 618896263Sobrien args, complain, t); 618990075Sobrien TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); 619090075Sobrien TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; 619190075Sobrien } 619218334Speter 619390075Sobrien /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do 619490075Sobrien this in the special friend case mentioned above where 619590075Sobrien GEN_TMPL is NULL. */ 619652284Sobrien if (gen_tmpl) 619750397Sobrien { 619852284Sobrien DECL_TEMPLATE_INFO (r) 619990075Sobrien = tree_cons (gen_tmpl, argvec, NULL_TREE); 620052284Sobrien SET_DECL_IMPLICIT_INSTANTIATION (r); 620152284Sobrien register_specialization (r, gen_tmpl, argvec); 620250397Sobrien 620390075Sobrien /* We're not supposed to instantiate default arguments 620490075Sobrien until they are called, for a template. But, for a 620590075Sobrien declaration like: 620660967Sobrien 620790075Sobrien template <class T> void f () 620890075Sobrien { extern void g(int i = T()); } 620990075Sobrien 621090075Sobrien we should do the substitution when the template is 621190075Sobrien instantiated. We handle the member function case in 621290075Sobrien instantiate_class_template since the default arguments 621390075Sobrien might refer to other members of the class. */ 621490075Sobrien if (!member 621590075Sobrien && !PRIMARY_TEMPLATE_P (gen_tmpl) 621690075Sobrien && !uses_template_parms (argvec)) 621790075Sobrien tsubst_default_arguments (r); 621850397Sobrien } 621950397Sobrien 622052284Sobrien /* Copy the list of befriending classes. */ 622152284Sobrien for (friends = &DECL_BEFRIENDING_CLASSES (r); 622252284Sobrien *friends; 622352284Sobrien friends = &TREE_CHAIN (*friends)) 622450397Sobrien { 622552284Sobrien *friends = copy_node (*friends); 622652284Sobrien TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), 622796263Sobrien args, complain, 622852284Sobrien in_decl); 622952284Sobrien } 623050397Sobrien 623190075Sobrien if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) 623252284Sobrien { 623352284Sobrien maybe_retrofit_in_chrg (r); 623490075Sobrien if (DECL_CONSTRUCTOR_P (r)) 623590075Sobrien grok_ctor_properties (ctx, r); 623690075Sobrien /* If this is an instantiation of a member template, clone it. 623790075Sobrien If it isn't, that'll be handled by 623890075Sobrien clone_constructors_and_destructors. */ 623990075Sobrien if (PRIMARY_TEMPLATE_P (gen_tmpl)) 624090075Sobrien clone_function_decl (r, /*update_method_vec_p=*/0); 624150397Sobrien } 624290075Sobrien else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) 624390075Sobrien grok_op_properties (r, DECL_FRIEND_P (r)); 624418334Speter } 624552284Sobrien break; 624618334Speter 624718334Speter case PARM_DECL: 624818334Speter { 624952284Sobrien r = copy_node (t); 625090075Sobrien if (DECL_TEMPLATE_PARM_P (t)) 625190075Sobrien SET_DECL_TEMPLATE_PARM_P (r); 6252117395Skan 625350397Sobrien TREE_TYPE (r) = type; 625490075Sobrien c_apply_type_quals_to_decl (cp_type_quals (type), r); 625552284Sobrien 625650397Sobrien if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) 625750397Sobrien DECL_INITIAL (r) = TREE_TYPE (r); 625850397Sobrien else 625952284Sobrien DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, 626096263Sobrien complain, in_decl); 626150397Sobrien 626250397Sobrien DECL_CONTEXT (r) = NULL_TREE; 6263117395Skan 6264117395Skan if (!DECL_TEMPLATE_PARM_P (r)) 6265117395Skan DECL_ARG_TYPE (r) = type_passed_as (type); 626618334Speter if (TREE_CHAIN (t)) 626752284Sobrien TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, 626896263Sobrien complain, TREE_CHAIN (t)); 626918334Speter } 627052284Sobrien break; 627118334Speter 627250397Sobrien case FIELD_DECL: 627350397Sobrien { 627490075Sobrien r = copy_decl (t); 627550397Sobrien TREE_TYPE (r) = type; 627690075Sobrien c_apply_type_quals_to_decl (cp_type_quals (type), r); 627752284Sobrien 627852284Sobrien /* We don't have to set DECL_CONTEXT here; it is set by 627952284Sobrien finish_member_declaration. */ 628052284Sobrien DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, 628196263Sobrien complain, in_decl); 628250397Sobrien TREE_CHAIN (r) = NULL_TREE; 628390075Sobrien if (VOID_TYPE_P (type)) 628490075Sobrien cp_error_at ("instantiation of `%D' as type `%T'", r, type); 628550397Sobrien } 628652284Sobrien break; 628750397Sobrien 628850397Sobrien case USING_DECL: 628950397Sobrien { 629052284Sobrien r = copy_node (t); 629150397Sobrien DECL_INITIAL (r) 629296263Sobrien = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); 629350397Sobrien TREE_CHAIN (r) = NULL_TREE; 629450397Sobrien } 629552284Sobrien break; 629650397Sobrien 629790075Sobrien case TYPE_DECL: 629890075Sobrien if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM 629990075Sobrien || t == TYPE_MAIN_DECL (TREE_TYPE (t))) 630090075Sobrien { 630190075Sobrien /* If this is the canonical decl, we don't have to mess with 630290075Sobrien instantiations, and often we can't (for typename, template 630390075Sobrien type parms and such). Note that TYPE_NAME is not correct for 630490075Sobrien the above test if we've copied the type for a typedef. */ 630590075Sobrien r = TYPE_NAME (type); 630690075Sobrien break; 630790075Sobrien } 630890075Sobrien 630990075Sobrien /* Fall through. */ 631090075Sobrien 631150397Sobrien case VAR_DECL: 631250397Sobrien { 631390075Sobrien tree argvec = NULL_TREE; 631490075Sobrien tree gen_tmpl = NULL_TREE; 631552284Sobrien tree spec; 631690075Sobrien tree tmpl = NULL_TREE; 631790075Sobrien tree ctx; 631890075Sobrien int local_p; 631950397Sobrien 632090075Sobrien /* Assume this is a non-local variable. */ 632190075Sobrien local_p = 0; 632290075Sobrien 632390075Sobrien if (TYPE_P (CP_DECL_CONTEXT (t))) 632490075Sobrien ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, 632596263Sobrien complain, 632690075Sobrien in_decl, /*entering_scope=*/1); 632790075Sobrien else if (DECL_NAMESPACE_SCOPE_P (t)) 632890075Sobrien ctx = DECL_CONTEXT (t); 632990075Sobrien else 633090075Sobrien { 633190075Sobrien /* Subsequent calls to pushdecl will fill this in. */ 633290075Sobrien ctx = NULL_TREE; 633390075Sobrien local_p = 1; 633490075Sobrien } 633590075Sobrien 633652284Sobrien /* Check to see if we already have this specialization. */ 633790075Sobrien if (!local_p) 633890075Sobrien { 633990075Sobrien tmpl = DECL_TI_TEMPLATE (t); 634090075Sobrien gen_tmpl = most_general_template (tmpl); 634196263Sobrien argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); 634290075Sobrien spec = retrieve_specialization (gen_tmpl, argvec); 634390075Sobrien } 634490075Sobrien else 634590075Sobrien spec = retrieve_local_specialization (t); 634690075Sobrien 634752284Sobrien if (spec) 634850397Sobrien { 634952284Sobrien r = spec; 635052284Sobrien break; 635150397Sobrien } 635250397Sobrien 635390075Sobrien r = copy_decl (t); 6354104752Skan if (TREE_CODE (r) == VAR_DECL) 6355104752Skan type = complete_type (type); 6356119256Skan else if (DECL_SELF_REFERENCE_P (t)) 6357119256Skan SET_DECL_SELF_REFERENCE_P (r); 635850397Sobrien TREE_TYPE (r) = type; 635990075Sobrien c_apply_type_quals_to_decl (cp_type_quals (type), r); 636050397Sobrien DECL_CONTEXT (r) = ctx; 636190075Sobrien /* Clear out the mangled name and RTL for the instantiation. */ 636290075Sobrien SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); 636390075Sobrien SET_DECL_RTL (r, NULL_RTX); 636450397Sobrien 636550397Sobrien /* Don't try to expand the initializer until someone tries to use 636650397Sobrien this variable; otherwise we run into circular dependencies. */ 636750397Sobrien DECL_INITIAL (r) = NULL_TREE; 636890075Sobrien SET_DECL_RTL (r, NULL_RTX); 636990075Sobrien DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; 637050397Sobrien 637190075Sobrien /* Even if the original location is out of scope, the newly 637290075Sobrien substituted one is not. */ 637390075Sobrien if (TREE_CODE (r) == VAR_DECL) 6374117395Skan { 6375117395Skan DECL_DEAD_FOR_LOCAL (r) = 0; 6376117395Skan DECL_INITIALIZED_P (r) = 0; 6377117395Skan } 637850397Sobrien 637990075Sobrien if (!local_p) 638090075Sobrien { 638190075Sobrien /* A static data member declaration is always marked 638290075Sobrien external when it is declared in-class, even if an 638390075Sobrien initializer is present. We mimic the non-template 638490075Sobrien processing here. */ 638590075Sobrien DECL_EXTERNAL (r) = 1; 638690075Sobrien 638790075Sobrien register_specialization (r, gen_tmpl, argvec); 638890075Sobrien DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); 638990075Sobrien SET_DECL_IMPLICIT_INSTANTIATION (r); 639090075Sobrien } 639190075Sobrien else 639290075Sobrien register_local_specialization (r, t); 639390075Sobrien 639450397Sobrien TREE_CHAIN (r) = NULL_TREE; 639590075Sobrien if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type)) 639690075Sobrien cp_error_at ("instantiation of `%D' as type `%T'", r, type); 6397104752Skan /* Compute the size, alignment, etc. of R. */ 6398104752Skan layout_decl (r, 0); 639950397Sobrien } 640052284Sobrien break; 640150397Sobrien 640252284Sobrien default: 640390075Sobrien abort (); 640452284Sobrien } 640552284Sobrien 640652284Sobrien /* Restore the file and line information. */ 640752284Sobrien lineno = saved_lineno; 640852284Sobrien input_filename = saved_filename; 640952284Sobrien 641052284Sobrien return r; 641152284Sobrien} 641252284Sobrien 641352284Sobrien/* Substitue into the ARG_TYPES of a function type. */ 641452284Sobrien 641552284Sobrienstatic tree 641652284Sobrientsubst_arg_types (arg_types, args, complain, in_decl) 641752284Sobrien tree arg_types; 641852284Sobrien tree args; 641996263Sobrien tsubst_flags_t complain; 642052284Sobrien tree in_decl; 642152284Sobrien{ 642252284Sobrien tree remaining_arg_types; 642352284Sobrien tree type; 642452284Sobrien 642552284Sobrien if (!arg_types || arg_types == void_list_node) 642652284Sobrien return arg_types; 642752284Sobrien 642852284Sobrien remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), 642952284Sobrien args, complain, in_decl); 643052284Sobrien if (remaining_arg_types == error_mark_node) 643152284Sobrien return error_mark_node; 643252284Sobrien 643352284Sobrien type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); 643452284Sobrien if (type == error_mark_node) 643552284Sobrien return error_mark_node; 643690075Sobrien if (VOID_TYPE_P (type)) 643790075Sobrien { 643896263Sobrien if (complain & tf_error) 643990075Sobrien { 644090075Sobrien error ("invalid parameter type `%T'", type); 644190075Sobrien if (in_decl) 644290075Sobrien cp_error_at ("in declaration `%D'", in_decl); 644390075Sobrien } 644490075Sobrien return error_mark_node; 644590075Sobrien } 644652284Sobrien 644752284Sobrien /* Do array-to-pointer, function-to-pointer conversion, and ignore 644852284Sobrien top-level qualifiers as required. */ 644952284Sobrien type = TYPE_MAIN_VARIANT (type_decays_to (type)); 645052284Sobrien 645152284Sobrien /* Note that we do not substitute into default arguments here. The 645252284Sobrien standard mandates that they be instantiated only when needed, 645352284Sobrien which is done in build_over_call. */ 645452284Sobrien return hash_tree_cons (TREE_PURPOSE (arg_types), type, 645552284Sobrien remaining_arg_types); 645652284Sobrien 645752284Sobrien} 645852284Sobrien 645952284Sobrien/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does 646052284Sobrien *not* handle the exception-specification for FNTYPE, because the 646152284Sobrien initial substitution of explicitly provided template parameters 646252284Sobrien during argument deduction forbids substitution into the 646352284Sobrien exception-specification: 646452284Sobrien 646552284Sobrien [temp.deduct] 646652284Sobrien 646752284Sobrien All references in the function type of the function template to the 646852284Sobrien corresponding template parameters are replaced by the specified tem- 646952284Sobrien plate argument values. If a substitution in a template parameter or 647052284Sobrien in the function type of the function template results in an invalid 647152284Sobrien type, type deduction fails. [Note: The equivalent substitution in 647252284Sobrien exception specifications is done only when the function is instanti- 647352284Sobrien ated, at which point a program is ill-formed if the substitution 647452284Sobrien results in an invalid type.] */ 647552284Sobrien 647652284Sobrienstatic tree 647752284Sobrientsubst_function_type (t, args, complain, in_decl) 647852284Sobrien tree t; 647952284Sobrien tree args; 648096263Sobrien tsubst_flags_t complain; 648152284Sobrien tree in_decl; 648252284Sobrien{ 648352284Sobrien tree return_type; 648452284Sobrien tree arg_types; 648552284Sobrien tree fntype; 648652284Sobrien 648752284Sobrien /* The TYPE_CONTEXT is not used for function/method types. */ 648852284Sobrien my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); 648952284Sobrien 649090075Sobrien /* Substitute the return type. */ 649152284Sobrien return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 649252284Sobrien if (return_type == error_mark_node) 649352284Sobrien return error_mark_node; 649452284Sobrien 649552284Sobrien /* Substitue the argument types. */ 649652284Sobrien arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, 649752284Sobrien complain, in_decl); 649852284Sobrien if (arg_types == error_mark_node) 649952284Sobrien return error_mark_node; 650052284Sobrien 650152284Sobrien /* Construct a new type node and return it. */ 650252284Sobrien if (TREE_CODE (t) == FUNCTION_TYPE) 650352284Sobrien fntype = build_function_type (return_type, arg_types); 650452284Sobrien else 650552284Sobrien { 650652284Sobrien tree r = TREE_TYPE (TREE_VALUE (arg_types)); 650752284Sobrien if (! IS_AGGR_TYPE (r)) 650852284Sobrien { 650952284Sobrien /* [temp.deduct] 651052284Sobrien 651152284Sobrien Type deduction may fail for any of the following 651252284Sobrien reasons: 651352284Sobrien 651452284Sobrien -- Attempting to create "pointer to member of T" when T 651552284Sobrien is not a class type. */ 651696263Sobrien if (complain & tf_error) 651790075Sobrien error ("creating pointer to member function of non-class type `%T'", 651852284Sobrien r); 651952284Sobrien return error_mark_node; 652052284Sobrien } 652152284Sobrien 652252284Sobrien fntype = build_cplus_method_type (r, return_type, TREE_CHAIN 652352284Sobrien (arg_types)); 652452284Sobrien } 652596263Sobrien fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); 652652284Sobrien fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); 652752284Sobrien 652852284Sobrien return fntype; 652952284Sobrien} 653052284Sobrien 653152284Sobrien/* Substitute into the PARMS of a call-declarator. */ 653252284Sobrien 653352284Sobrienstatic tree 653452284Sobrientsubst_call_declarator_parms (parms, args, complain, in_decl) 653552284Sobrien tree parms; 653652284Sobrien tree args; 653796263Sobrien tsubst_flags_t complain; 653852284Sobrien tree in_decl; 653952284Sobrien{ 654052284Sobrien tree new_parms; 654152284Sobrien tree type; 654252284Sobrien tree defarg; 654352284Sobrien 654452284Sobrien if (!parms || parms == void_list_node) 654552284Sobrien return parms; 654652284Sobrien 654752284Sobrien new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), 654852284Sobrien args, complain, in_decl); 654952284Sobrien 655052284Sobrien /* Figure out the type of this parameter. */ 655152284Sobrien type = tsubst (TREE_VALUE (parms), args, complain, in_decl); 655252284Sobrien 655352284Sobrien /* Figure out the default argument as well. Note that we use 655452284Sobrien tsubst_expr since the default argument is really an expression. */ 655552284Sobrien defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); 655652284Sobrien 655752284Sobrien /* Chain this parameter on to the front of those we have already 655852284Sobrien processed. We don't use hash_tree_cons because that function 655952284Sobrien doesn't check TREE_PARMLIST. */ 656052284Sobrien new_parms = tree_cons (defarg, type, new_parms); 656152284Sobrien 656252284Sobrien /* And note that these are parameters. */ 656352284Sobrien TREE_PARMLIST (new_parms) = 1; 656452284Sobrien 656552284Sobrien return new_parms; 656652284Sobrien} 656752284Sobrien 656852284Sobrien/* Take the tree structure T and replace template parameters used 656952284Sobrien therein with the argument vector ARGS. IN_DECL is an associated 657052284Sobrien decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. 657196263Sobrien Issue error and warning messages under control of COMPLAIN. Note 657296263Sobrien that we must be relatively non-tolerant of extensions here, in 657396263Sobrien order to preserve conformance; if we allow substitutions that 657496263Sobrien should not be allowed, we may allow argument deductions that should 657596263Sobrien not succeed, and therefore report ambiguous overload situations 657696263Sobrien where there are none. In theory, we could allow the substitution, 657796263Sobrien but indicate that it should have failed, and allow our caller to 657896263Sobrien make sure that the right thing happens, but we don't try to do this 657996263Sobrien yet. 658052284Sobrien 658152284Sobrien This function is used for dealing with types, decls and the like; 658252284Sobrien for expressions, use tsubst_expr or tsubst_copy. */ 658352284Sobrien 658452284Sobrientree 658552284Sobrientsubst (t, args, complain, in_decl) 658652284Sobrien tree t, args; 658796263Sobrien tsubst_flags_t complain; 658852284Sobrien tree in_decl; 658952284Sobrien{ 659052284Sobrien tree type, r; 659152284Sobrien 659252284Sobrien if (t == NULL_TREE || t == error_mark_node 659352284Sobrien || t == integer_type_node 659452284Sobrien || t == void_type_node 659552284Sobrien || t == char_type_node 659652284Sobrien || TREE_CODE (t) == NAMESPACE_DECL) 659752284Sobrien return t; 659852284Sobrien 659952284Sobrien if (TREE_CODE (t) == IDENTIFIER_NODE) 660052284Sobrien type = IDENTIFIER_TYPE_VALUE (t); 660152284Sobrien else 660252284Sobrien type = TREE_TYPE (t); 660352284Sobrien if (type == unknown_type_node) 660490075Sobrien abort (); 660552284Sobrien 660652284Sobrien if (type && TREE_CODE (t) != FUNCTION_DECL 660752284Sobrien && TREE_CODE (t) != TYPENAME_TYPE 660852284Sobrien && TREE_CODE (t) != TEMPLATE_DECL 660952284Sobrien && TREE_CODE (t) != IDENTIFIER_NODE 661052284Sobrien && TREE_CODE (t) != FUNCTION_TYPE 661152284Sobrien && TREE_CODE (t) != METHOD_TYPE) 661252284Sobrien type = tsubst (type, args, complain, in_decl); 661352284Sobrien if (type == error_mark_node) 661452284Sobrien return error_mark_node; 661552284Sobrien 661690075Sobrien if (DECL_P (t)) 661796263Sobrien return tsubst_decl (t, args, type, complain); 661852284Sobrien 661952284Sobrien switch (TREE_CODE (t)) 662052284Sobrien { 662152284Sobrien case RECORD_TYPE: 662252284Sobrien case UNION_TYPE: 662352284Sobrien case ENUMERAL_TYPE: 662452284Sobrien return tsubst_aggr_type (t, args, complain, in_decl, 662552284Sobrien /*entering_scope=*/0); 662652284Sobrien 662752284Sobrien case ERROR_MARK: 662852284Sobrien case IDENTIFIER_NODE: 662952284Sobrien case VOID_TYPE: 663052284Sobrien case REAL_TYPE: 663152284Sobrien case COMPLEX_TYPE: 663290075Sobrien case VECTOR_TYPE: 663352284Sobrien case BOOLEAN_TYPE: 663452284Sobrien case INTEGER_CST: 663552284Sobrien case REAL_CST: 663652284Sobrien case STRING_CST: 663752284Sobrien return t; 663852284Sobrien 663952284Sobrien case INTEGER_TYPE: 664052284Sobrien if (t == integer_type_node) 664152284Sobrien return t; 664252284Sobrien 664352284Sobrien if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST 664452284Sobrien && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) 664552284Sobrien return t; 664652284Sobrien 664750397Sobrien { 664852284Sobrien tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); 664952284Sobrien 665052284Sobrien max = tsubst_expr (omax, args, complain, in_decl); 665152284Sobrien if (max == error_mark_node) 665252284Sobrien return error_mark_node; 665352284Sobrien 665452750Sobrien /* See if we can reduce this expression to something simpler. */ 665552750Sobrien max = maybe_fold_nontype_arg (max); 665690075Sobrien if (!processing_template_decl) 665752750Sobrien max = decl_constant_value (max); 665852750Sobrien 665952284Sobrien if (processing_template_decl 666052284Sobrien /* When providing explicit arguments to a template 666152284Sobrien function, but leaving some arguments for subsequent 666252284Sobrien deduction, MAX may be template-dependent even if we're 666390075Sobrien not PROCESSING_TEMPLATE_DECL. We still need to check for 666490075Sobrien template parms, though; MAX won't be an INTEGER_CST for 666590075Sobrien dynamic arrays, either. */ 666690075Sobrien || (TREE_CODE (max) != INTEGER_CST 666790075Sobrien && uses_template_parms (max))) 666852284Sobrien { 666952750Sobrien tree itype = make_node (INTEGER_TYPE); 667052750Sobrien TYPE_MIN_VALUE (itype) = size_zero_node; 667152750Sobrien TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, 667252750Sobrien integer_one_node); 667352750Sobrien return itype; 667452284Sobrien } 667552284Sobrien 667652284Sobrien if (integer_zerop (omax)) 667752284Sobrien { 667852284Sobrien /* Still allow an explicit array of size zero. */ 667952284Sobrien if (pedantic) 668052284Sobrien pedwarn ("creating array with size zero"); 668152284Sobrien } 668290075Sobrien else if (integer_zerop (max) 668390075Sobrien || (TREE_CODE (max) == INTEGER_CST 668490075Sobrien && INT_CST_LT (max, integer_zero_node))) 668552284Sobrien { 668652284Sobrien /* [temp.deduct] 668752284Sobrien 668852284Sobrien Type deduction may fail for any of the following 668952284Sobrien reasons: 669052284Sobrien 669152284Sobrien Attempting to create an array with a size that is 669252284Sobrien zero or negative. */ 669396263Sobrien if (complain & tf_error) 669490075Sobrien error ("creating array with size zero (`%E')", max); 669552284Sobrien 669652284Sobrien return error_mark_node; 669752284Sobrien } 669852284Sobrien 669990075Sobrien return compute_array_index_type (NULL_TREE, max); 670052284Sobrien } 670152284Sobrien 670252284Sobrien case TEMPLATE_TYPE_PARM: 670352284Sobrien case TEMPLATE_TEMPLATE_PARM: 670490075Sobrien case BOUND_TEMPLATE_TEMPLATE_PARM: 670552284Sobrien case TEMPLATE_PARM_INDEX: 670652284Sobrien { 670752284Sobrien int idx; 670852284Sobrien int level; 670952284Sobrien int levels; 671052284Sobrien 671152284Sobrien r = NULL_TREE; 671252284Sobrien 671352284Sobrien if (TREE_CODE (t) == TEMPLATE_TYPE_PARM 671490075Sobrien || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM 671590075Sobrien || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 671652284Sobrien { 671752284Sobrien idx = TEMPLATE_TYPE_IDX (t); 671852284Sobrien level = TEMPLATE_TYPE_LEVEL (t); 671952284Sobrien } 672052284Sobrien else 672152284Sobrien { 672252284Sobrien idx = TEMPLATE_PARM_IDX (t); 672352284Sobrien level = TEMPLATE_PARM_LEVEL (t); 672452284Sobrien } 672552284Sobrien 672652284Sobrien if (TREE_VEC_LENGTH (args) > 0) 672752284Sobrien { 672852284Sobrien tree arg = NULL_TREE; 672952284Sobrien 673052284Sobrien levels = TMPL_ARGS_DEPTH (args); 673152284Sobrien if (level <= levels) 673252284Sobrien arg = TMPL_ARG (args, level, idx); 673352284Sobrien 673452284Sobrien if (arg == error_mark_node) 673552284Sobrien return error_mark_node; 673652284Sobrien else if (arg != NULL_TREE) 673752284Sobrien { 673852284Sobrien if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) 673952284Sobrien { 674090075Sobrien my_friendly_assert (TYPE_P (arg), 0); 674190075Sobrien return cp_build_qualified_type_real 674290075Sobrien (arg, cp_type_quals (arg) | cp_type_quals (t), 674396263Sobrien complain | tf_ignore_bad_quals); 674452284Sobrien } 674590075Sobrien else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 674652284Sobrien { 674790075Sobrien /* We are processing a type constructed from 674890075Sobrien a template template parameter */ 674990075Sobrien tree argvec = tsubst (TYPE_TI_ARGS (t), 675090075Sobrien args, complain, in_decl); 675190075Sobrien if (argvec == error_mark_node) 675290075Sobrien return error_mark_node; 675352284Sobrien 675490075Sobrien /* We can get a TEMPLATE_TEMPLATE_PARM here when 675590075Sobrien we are resolving nested-types in the signature of 675690075Sobrien a member function templates. 675790075Sobrien Otherwise ARG is a TEMPLATE_DECL and is the real 675890075Sobrien template to be instantiated. */ 675990075Sobrien if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) 676090075Sobrien arg = TYPE_NAME (arg); 676152284Sobrien 676290075Sobrien r = lookup_template_class (arg, 676390075Sobrien argvec, in_decl, 676490075Sobrien DECL_CONTEXT (arg), 676590075Sobrien /*entering_scope=*/0, 676690075Sobrien complain); 676796263Sobrien return cp_build_qualified_type_real 676896263Sobrien (r, TYPE_QUALS (t), complain); 676952284Sobrien } 677052284Sobrien else 677190075Sobrien /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ 677252284Sobrien return arg; 677352284Sobrien } 677452284Sobrien } 677552284Sobrien else 677690075Sobrien abort (); 677752284Sobrien 677852284Sobrien if (level == 1) 677952284Sobrien /* This can happen during the attempted tsubst'ing in 678052284Sobrien unify. This means that we don't yet have any information 678152284Sobrien about the template parameter in question. */ 678252284Sobrien return t; 678352284Sobrien 678452284Sobrien /* If we get here, we must have been looking at a parm for a 678552284Sobrien more deeply nested template. Make a new version of this 678652284Sobrien template parameter, but with a lower level. */ 678752284Sobrien switch (TREE_CODE (t)) 678852284Sobrien { 678952284Sobrien case TEMPLATE_TYPE_PARM: 679052284Sobrien case TEMPLATE_TEMPLATE_PARM: 679190075Sobrien case BOUND_TEMPLATE_TEMPLATE_PARM: 679290075Sobrien if (cp_type_quals (t)) 679352284Sobrien { 679490075Sobrien r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); 679596263Sobrien r = cp_build_qualified_type_real 679696263Sobrien (r, cp_type_quals (t), 679796263Sobrien complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM 679896263Sobrien ? tf_ignore_bad_quals : 0)); 679990075Sobrien } 680090075Sobrien else 680190075Sobrien { 680290075Sobrien r = copy_type (t); 680390075Sobrien TEMPLATE_TYPE_PARM_INDEX (r) 680490075Sobrien = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), 680590075Sobrien r, levels); 680690075Sobrien TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); 680790075Sobrien TYPE_MAIN_VARIANT (r) = r; 680890075Sobrien TYPE_POINTER_TO (r) = NULL_TREE; 680990075Sobrien TYPE_REFERENCE_TO (r) = NULL_TREE; 681052284Sobrien 681190075Sobrien if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) 681290075Sobrien { 681390075Sobrien tree argvec = tsubst (TYPE_TI_ARGS (t), args, 681490075Sobrien complain, in_decl); 681590075Sobrien if (argvec == error_mark_node) 681690075Sobrien return error_mark_node; 681790075Sobrien 681890075Sobrien TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) 681990075Sobrien = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); 682090075Sobrien } 682152284Sobrien } 682252284Sobrien break; 682352284Sobrien 682452284Sobrien case TEMPLATE_PARM_INDEX: 682552284Sobrien r = reduce_template_parm_level (t, type, levels); 682652284Sobrien break; 682752284Sobrien 682852284Sobrien default: 682990075Sobrien abort (); 683052284Sobrien } 683152284Sobrien 683250397Sobrien return r; 683352284Sobrien } 683450397Sobrien 683518334Speter case TREE_LIST: 683618334Speter { 683718334Speter tree purpose, value, chain, result; 683818334Speter 683918334Speter if (t == void_list_node) 684018334Speter return t; 684118334Speter 684218334Speter purpose = TREE_PURPOSE (t); 684318334Speter if (purpose) 684452284Sobrien { 684552284Sobrien purpose = tsubst (purpose, args, complain, in_decl); 684652284Sobrien if (purpose == error_mark_node) 684752284Sobrien return error_mark_node; 684852284Sobrien } 684918334Speter value = TREE_VALUE (t); 685018334Speter if (value) 685152284Sobrien { 685252284Sobrien value = tsubst (value, args, complain, in_decl); 685352284Sobrien if (value == error_mark_node) 685452284Sobrien return error_mark_node; 685552284Sobrien } 685618334Speter chain = TREE_CHAIN (t); 685718334Speter if (chain && chain != void_type_node) 685852284Sobrien { 685952284Sobrien chain = tsubst (chain, args, complain, in_decl); 686052284Sobrien if (chain == error_mark_node) 686152284Sobrien return error_mark_node; 686252284Sobrien } 686318334Speter if (purpose == TREE_PURPOSE (t) 686418334Speter && value == TREE_VALUE (t) 686518334Speter && chain == TREE_CHAIN (t)) 686618334Speter return t; 686790075Sobrien if (TREE_PARMLIST (t)) 686890075Sobrien { 686990075Sobrien result = tree_cons (purpose, value, chain); 687090075Sobrien TREE_PARMLIST (result) = 1; 687190075Sobrien } 687290075Sobrien else 687390075Sobrien result = hash_tree_cons (purpose, value, chain); 687418334Speter return result; 687518334Speter } 687618334Speter case TREE_VEC: 687750397Sobrien if (type != NULL_TREE) 687850397Sobrien { 687950397Sobrien /* A binfo node. We always need to make a copy, of the node 688050397Sobrien itself and of its BINFO_BASETYPES. */ 688118334Speter 688250397Sobrien t = copy_node (t); 688318334Speter 688450397Sobrien /* Make sure type isn't a typedef copy. */ 688550397Sobrien type = BINFO_TYPE (TYPE_BINFO (type)); 688650397Sobrien 688750397Sobrien TREE_TYPE (t) = complete_type (type); 688850397Sobrien if (IS_AGGR_TYPE (type)) 688950397Sobrien { 689050397Sobrien BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); 689150397Sobrien BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); 689250397Sobrien if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) 689350397Sobrien BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); 689450397Sobrien } 689518334Speter return t; 689650397Sobrien } 689718334Speter 689850397Sobrien /* Otherwise, a vector of template arguments. */ 689952284Sobrien return tsubst_template_arg_vector (t, args, complain); 690050397Sobrien 690118334Speter case POINTER_TYPE: 690218334Speter case REFERENCE_TYPE: 690318334Speter { 690418334Speter enum tree_code code; 690550397Sobrien 690690075Sobrien if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) 690718334Speter return t; 690818334Speter 690918334Speter code = TREE_CODE (t); 691052284Sobrien 691152284Sobrien 691252284Sobrien /* [temp.deduct] 691352284Sobrien 691452284Sobrien Type deduction may fail for any of the following 691552284Sobrien reasons: 691652284Sobrien 691752284Sobrien -- Attempting to create a pointer to reference type. 691852284Sobrien -- Attempting to create a reference to a reference type or 691952284Sobrien a reference to void. */ 692052284Sobrien if (TREE_CODE (type) == REFERENCE_TYPE 692152284Sobrien || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) 692250397Sobrien { 692350397Sobrien static int last_line = 0; 692490075Sobrien static const char* last_file = 0; 692550397Sobrien 692650397Sobrien /* We keep track of the last time we issued this error 692750397Sobrien message to avoid spewing a ton of messages during a 692850397Sobrien single bad template instantiation. */ 692996263Sobrien if (complain & tf_error 693096263Sobrien && (last_line != lineno || last_file != input_filename)) 693150397Sobrien { 693252284Sobrien if (TREE_CODE (type) == VOID_TYPE) 693390075Sobrien error ("forming reference to void"); 693452284Sobrien else 693590075Sobrien error ("forming %s to reference type `%T'", 693652284Sobrien (code == POINTER_TYPE) ? "pointer" : "reference", 693752284Sobrien type); 693850397Sobrien last_line = lineno; 693950397Sobrien last_file = input_filename; 694050397Sobrien } 694150397Sobrien 694252284Sobrien return error_mark_node; 694350397Sobrien } 694450397Sobrien else if (code == POINTER_TYPE) 694590075Sobrien { 694690075Sobrien r = build_pointer_type (type); 694790075Sobrien if (TREE_CODE (type) == METHOD_TYPE) 694890075Sobrien r = build_ptrmemfunc_type (r); 694990075Sobrien } 695018334Speter else 695118334Speter r = build_reference_type (type); 695290075Sobrien r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); 695350397Sobrien 695490075Sobrien if (r != error_mark_node) 695590075Sobrien /* Will this ever be needed for TYPE_..._TO values? */ 695690075Sobrien layout_type (r); 695790075Sobrien 695818334Speter return r; 695918334Speter } 696018334Speter case OFFSET_TYPE: 696152284Sobrien { 696252284Sobrien r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); 696352284Sobrien if (r == error_mark_node || !IS_AGGR_TYPE (r)) 696452284Sobrien { 696552284Sobrien /* [temp.deduct] 696652284Sobrien 696752284Sobrien Type deduction may fail for any of the following 696852284Sobrien reasons: 696952284Sobrien 697052284Sobrien -- Attempting to create "pointer to member of T" when T 697152284Sobrien is not a class type. */ 697296263Sobrien if (complain & tf_error) 697396263Sobrien error ("creating pointer to member of non-class type `%T'", r); 697452284Sobrien return error_mark_node; 697552284Sobrien } 697690075Sobrien if (TREE_CODE (type) == REFERENCE_TYPE) 697790075Sobrien { 6978117395Skan if (complain & tf_error) 697990075Sobrien error ("creating pointer to member reference type `%T'", type); 698090075Sobrien 698190075Sobrien return error_mark_node; 698290075Sobrien } 698390075Sobrien my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231); 698490075Sobrien if (TREE_CODE (type) == FUNCTION_TYPE) 698590075Sobrien /* This is really a method type. The cv qualifiers of the 698690075Sobrien this pointer should _not_ be determined by the cv 698790075Sobrien qualifiers of the class type. They should be held 698890075Sobrien somewhere in the FUNCTION_TYPE, but we don't do that at 698990075Sobrien the moment. Consider 699090075Sobrien typedef void (Func) () const; 699190075Sobrien 699290075Sobrien template <typename T1> void Foo (Func T1::*); 699390075Sobrien 699490075Sobrien */ 699590075Sobrien return build_cplus_method_type (TYPE_MAIN_VARIANT (r), 699690075Sobrien TREE_TYPE (type), 699790075Sobrien TYPE_ARG_TYPES (type)); 699890075Sobrien else 699990075Sobrien return build_offset_type (r, type); 700052284Sobrien } 700118334Speter case FUNCTION_TYPE: 700218334Speter case METHOD_TYPE: 700318334Speter { 700450397Sobrien tree fntype; 700552284Sobrien tree raises; 700618334Speter 700752284Sobrien fntype = tsubst_function_type (t, args, complain, in_decl); 700852284Sobrien if (fntype == error_mark_node) 700952284Sobrien return error_mark_node; 701050397Sobrien 7011117395Skan /* Substitue the exception specification. */ 701252284Sobrien raises = TYPE_RAISES_EXCEPTIONS (t); 701350397Sobrien if (raises) 701450397Sobrien { 701590075Sobrien tree list = NULL_TREE; 701690075Sobrien 701790075Sobrien if (! TREE_VALUE (raises)) 701890075Sobrien list = raises; 701990075Sobrien else 702090075Sobrien for (; raises != NULL_TREE; raises = TREE_CHAIN (raises)) 702190075Sobrien { 702290075Sobrien tree spec = TREE_VALUE (raises); 702390075Sobrien 702490075Sobrien spec = tsubst (spec, args, complain, in_decl); 702590075Sobrien if (spec == error_mark_node) 702690075Sobrien return spec; 702790075Sobrien list = add_exception_specifier (list, spec, complain); 702890075Sobrien } 702990075Sobrien fntype = build_exception_variant (fntype, list); 703050397Sobrien } 703150397Sobrien return fntype; 703218334Speter } 703318334Speter case ARRAY_TYPE: 703418334Speter { 703552284Sobrien tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); 703652284Sobrien if (domain == error_mark_node) 703752284Sobrien return error_mark_node; 703852284Sobrien 703952284Sobrien /* As an optimization, we avoid regenerating the array type if 704052284Sobrien it will obviously be the same as T. */ 704118334Speter if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) 704218334Speter return t; 704352284Sobrien 704452284Sobrien /* These checks should match the ones in grokdeclarator. 704552284Sobrien 704652284Sobrien [temp.deduct] 704752284Sobrien 704852284Sobrien The deduction may fail for any of the following reasons: 704952284Sobrien 705052284Sobrien -- Attempting to create an array with an element type that 705152284Sobrien is void, a function type, or a reference type. */ 705252284Sobrien if (TREE_CODE (type) == VOID_TYPE 705352284Sobrien || TREE_CODE (type) == FUNCTION_TYPE 705452284Sobrien || TREE_CODE (type) == REFERENCE_TYPE) 705552284Sobrien { 705696263Sobrien if (complain & tf_error) 705790075Sobrien error ("creating array of `%T'", type); 705852284Sobrien return error_mark_node; 705952284Sobrien } 706052284Sobrien 706118334Speter r = build_cplus_array_type (type, domain); 706218334Speter return r; 706318334Speter } 706418334Speter 706550397Sobrien case PLUS_EXPR: 706618334Speter case MINUS_EXPR: 706752284Sobrien { 706896263Sobrien tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 706996263Sobrien tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); 707018334Speter 707152284Sobrien if (e1 == error_mark_node || e2 == error_mark_node) 707252284Sobrien return error_mark_node; 707352284Sobrien 707452284Sobrien return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); 707552284Sobrien } 707652284Sobrien 707718334Speter case NEGATE_EXPR: 707818334Speter case NOP_EXPR: 707952284Sobrien { 708096263Sobrien tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 708152284Sobrien if (e == error_mark_node) 708252284Sobrien return error_mark_node; 708318334Speter 708452284Sobrien return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); 708552284Sobrien } 708652284Sobrien 708750397Sobrien case TYPENAME_TYPE: 708850397Sobrien { 708952284Sobrien tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, 709052284Sobrien in_decl, /*entering_scope=*/1); 709152284Sobrien tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, 709252284Sobrien complain, in_decl); 709352284Sobrien 709452284Sobrien if (ctx == error_mark_node || f == error_mark_node) 709552284Sobrien return error_mark_node; 709652284Sobrien 709752284Sobrien if (!IS_AGGR_TYPE (ctx)) 709852284Sobrien { 709996263Sobrien if (complain & tf_error) 710090075Sobrien error ("`%T' is not a class, struct, or union type", 710152284Sobrien ctx); 710252284Sobrien return error_mark_node; 710352284Sobrien } 710452284Sobrien else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) 710552284Sobrien { 710652284Sobrien /* Normally, make_typename_type does not require that the CTX 710752284Sobrien have complete type in order to allow things like: 710852284Sobrien 710952284Sobrien template <class T> struct S { typename S<T>::X Y; }; 711052284Sobrien 711152284Sobrien But, such constructs have already been resolved by this 711252284Sobrien point, so here CTX really should have complete type, unless 711352284Sobrien it's a partial instantiation. */ 711452284Sobrien ctx = complete_type (ctx); 711590075Sobrien if (!COMPLETE_TYPE_P (ctx)) 711652284Sobrien { 711796263Sobrien if (complain & tf_error) 7118117395Skan cxx_incomplete_type_error (NULL_TREE, ctx); 711952284Sobrien return error_mark_node; 712052284Sobrien } 712152284Sobrien } 712252284Sobrien 712396263Sobrien f = make_typename_type (ctx, f, 712496263Sobrien (complain & tf_error) | tf_keep_type_decl); 712552284Sobrien if (f == error_mark_node) 712652284Sobrien return f; 712796263Sobrien if (TREE_CODE (f) == TYPE_DECL) 712896263Sobrien { 712996263Sobrien complain |= tf_ignore_bad_quals; 713096263Sobrien f = TREE_TYPE (f); 713196263Sobrien } 713296263Sobrien 713396263Sobrien return cp_build_qualified_type_real 713496263Sobrien (f, cp_type_quals (f) | cp_type_quals (t), complain); 713550397Sobrien } 713696263Sobrien 713790075Sobrien case UNBOUND_CLASS_TEMPLATE: 713890075Sobrien { 713990075Sobrien tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, 714090075Sobrien in_decl, /*entering_scope=*/1); 714190075Sobrien tree name = TYPE_IDENTIFIER (t); 714290075Sobrien 714390075Sobrien if (ctx == error_mark_node || name == error_mark_node) 714490075Sobrien return error_mark_node; 714590075Sobrien 714690075Sobrien return make_unbound_class_template (ctx, name, complain); 714790075Sobrien } 714890075Sobrien 714950397Sobrien case INDIRECT_REF: 715052284Sobrien { 715196263Sobrien tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 715252284Sobrien if (e == error_mark_node) 715352284Sobrien return error_mark_node; 715452284Sobrien return make_pointer_declarator (type, e); 715552284Sobrien } 715652284Sobrien 715750397Sobrien case ADDR_EXPR: 715852284Sobrien { 715996263Sobrien tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 716052284Sobrien if (e == error_mark_node) 716152284Sobrien return error_mark_node; 716252284Sobrien return make_reference_declarator (type, e); 716352284Sobrien } 716450397Sobrien 716550397Sobrien case ARRAY_REF: 716652284Sobrien { 716796263Sobrien tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 716896263Sobrien tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); 716952284Sobrien if (e1 == error_mark_node || e2 == error_mark_node) 717052284Sobrien return error_mark_node; 717150397Sobrien 717290075Sobrien return build_nt (ARRAY_REF, e1, e2, tsubst_expr); 717352284Sobrien } 717452284Sobrien 717550397Sobrien case CALL_EXPR: 717652284Sobrien { 717796263Sobrien tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 717890075Sobrien tree e2 = (tsubst_call_declarator_parms 717990075Sobrien (CALL_DECLARATOR_PARMS (t), args, complain, in_decl)); 718090075Sobrien tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args, 718190075Sobrien complain, in_decl); 718250397Sobrien 718352284Sobrien if (e1 == error_mark_node || e2 == error_mark_node 718452284Sobrien || e3 == error_mark_node) 718552284Sobrien return error_mark_node; 718652284Sobrien 718790075Sobrien return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3); 718852284Sobrien } 718952284Sobrien 719050397Sobrien case SCOPE_REF: 719152284Sobrien { 719296263Sobrien tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); 719352284Sobrien tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); 719452284Sobrien if (e1 == error_mark_node || e2 == error_mark_node) 719552284Sobrien return error_mark_node; 719650397Sobrien 719790075Sobrien return build_nt (TREE_CODE (t), e1, e2); 719852284Sobrien } 719952284Sobrien 720052284Sobrien case TYPEOF_TYPE: 720152284Sobrien { 720296263Sobrien tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl); 720352284Sobrien if (e1 == error_mark_node) 720452284Sobrien return error_mark_node; 720552284Sobrien 7206117395Skan return cp_build_qualified_type_real (TREE_TYPE (e1), 7207117395Skan cp_type_quals (t) 7208117395Skan | cp_type_quals (TREE_TYPE (e1)), 7209117395Skan complain); 721052284Sobrien } 721152284Sobrien 721218334Speter default: 721350397Sobrien sorry ("use of `%s' in template", 721418334Speter tree_code_name [(int) TREE_CODE (t)]); 721518334Speter return error_mark_node; 721618334Speter } 721718334Speter} 721818334Speter 721950397Sobrien/* Like tsubst, but deals with expressions. This function just replaces 722050397Sobrien template parms; to finish processing the resultant expression, use 722150397Sobrien tsubst_expr. */ 722218334Speter 722350397Sobrientree 722452284Sobrientsubst_copy (t, args, complain, in_decl) 722550397Sobrien tree t, args; 722696263Sobrien tsubst_flags_t complain; 722750397Sobrien tree in_decl; 722850397Sobrien{ 722950397Sobrien enum tree_code code; 723052284Sobrien tree r; 723118334Speter 723250397Sobrien if (t == NULL_TREE || t == error_mark_node) 723350397Sobrien return t; 723450397Sobrien 723550397Sobrien code = TREE_CODE (t); 723650397Sobrien 723750397Sobrien switch (code) 723818334Speter { 723950397Sobrien case PARM_DECL: 724050397Sobrien return do_identifier (DECL_NAME (t), 0, NULL_TREE); 724118334Speter 724250397Sobrien case CONST_DECL: 724352284Sobrien { 724452284Sobrien tree enum_type; 724552284Sobrien tree v; 724652284Sobrien 724752284Sobrien if (!DECL_CONTEXT (t)) 724852284Sobrien /* This is a global enumeration constant. */ 724952284Sobrien return t; 725052284Sobrien 725152284Sobrien /* Unfortunately, we cannot just call lookup_name here. 725290075Sobrien Consider: 725390075Sobrien 725490075Sobrien template <int I> int f() { 725590075Sobrien enum E { a = I }; 725690075Sobrien struct S { void g() { E e = a; } }; 725790075Sobrien }; 725890075Sobrien 725990075Sobrien When we instantiate f<7>::S::g(), say, lookup_name is not 726090075Sobrien clever enough to find f<7>::a. */ 726152284Sobrien enum_type 726252284Sobrien = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, 726352284Sobrien /*entering_scope=*/0); 726452284Sobrien 726552284Sobrien for (v = TYPE_VALUES (enum_type); 726652284Sobrien v != NULL_TREE; 726752284Sobrien v = TREE_CHAIN (v)) 726852284Sobrien if (TREE_PURPOSE (v) == DECL_NAME (t)) 726952284Sobrien return TREE_VALUE (v); 727052284Sobrien 727152284Sobrien /* We didn't find the name. That should never happen; if 727252284Sobrien name-lookup found it during preliminary parsing, we 727352284Sobrien should find it again here during instantiation. */ 727490075Sobrien abort (); 727552284Sobrien } 727652284Sobrien return t; 727752284Sobrien 727850397Sobrien case FIELD_DECL: 727950397Sobrien if (DECL_CONTEXT (t)) 728050397Sobrien { 728150397Sobrien tree ctx; 728218334Speter 728352284Sobrien ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, 728452284Sobrien /*entering_scope=*/1); 728550397Sobrien if (ctx != DECL_CONTEXT (t)) 728650397Sobrien return lookup_field (ctx, DECL_NAME (t), 0, 0); 728750397Sobrien } 728850397Sobrien return t; 728918334Speter 729050397Sobrien case VAR_DECL: 729150397Sobrien case FUNCTION_DECL: 729250397Sobrien if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) 729352284Sobrien t = tsubst (t, args, complain, in_decl); 729450397Sobrien mark_used (t); 729550397Sobrien return t; 729618334Speter 729750397Sobrien case TEMPLATE_DECL: 729850397Sobrien if (is_member_template (t)) 729952284Sobrien return tsubst (t, args, complain, in_decl); 730050397Sobrien else 730150397Sobrien return t; 730218334Speter 730352284Sobrien case LOOKUP_EXPR: 730452284Sobrien { 730590075Sobrien /* We must tsubst into a LOOKUP_EXPR in case the names to 730652284Sobrien which it refers is a conversion operator; in that case the 730752284Sobrien name will change. We avoid making unnecessary copies, 730852284Sobrien however. */ 730952284Sobrien 731052284Sobrien tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); 731152284Sobrien 731252284Sobrien if (id != TREE_OPERAND (t, 0)) 731352284Sobrien { 731452284Sobrien r = build_nt (LOOKUP_EXPR, id); 731552284Sobrien LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); 731652284Sobrien t = r; 731752284Sobrien } 731852284Sobrien 731952284Sobrien return t; 732052284Sobrien } 732152284Sobrien 732250397Sobrien case CAST_EXPR: 732350397Sobrien case REINTERPRET_CAST_EXPR: 732450397Sobrien case CONST_CAST_EXPR: 732550397Sobrien case STATIC_CAST_EXPR: 732650397Sobrien case DYNAMIC_CAST_EXPR: 732752284Sobrien case NOP_EXPR: 732850397Sobrien return build1 732952284Sobrien (code, tsubst (TREE_TYPE (t), args, complain, in_decl), 733052284Sobrien tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); 733118334Speter 733250397Sobrien case INDIRECT_REF: 733350397Sobrien case NEGATE_EXPR: 733450397Sobrien case TRUTH_NOT_EXPR: 733550397Sobrien case BIT_NOT_EXPR: 733650397Sobrien case ADDR_EXPR: 733750397Sobrien case CONVERT_EXPR: /* Unary + */ 733850397Sobrien case SIZEOF_EXPR: 733950397Sobrien case ALIGNOF_EXPR: 734050397Sobrien case ARROW_EXPR: 734150397Sobrien case THROW_EXPR: 734250397Sobrien case TYPEID_EXPR: 734390075Sobrien case REALPART_EXPR: 734490075Sobrien case IMAGPART_EXPR: 734550397Sobrien return build1 734652284Sobrien (code, tsubst (TREE_TYPE (t), args, complain, in_decl), 734752284Sobrien tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); 734818334Speter 734950397Sobrien case PLUS_EXPR: 735050397Sobrien case MINUS_EXPR: 735150397Sobrien case MULT_EXPR: 735250397Sobrien case TRUNC_DIV_EXPR: 735350397Sobrien case CEIL_DIV_EXPR: 735450397Sobrien case FLOOR_DIV_EXPR: 735550397Sobrien case ROUND_DIV_EXPR: 735650397Sobrien case EXACT_DIV_EXPR: 735750397Sobrien case BIT_AND_EXPR: 735850397Sobrien case BIT_ANDTC_EXPR: 735950397Sobrien case BIT_IOR_EXPR: 736050397Sobrien case BIT_XOR_EXPR: 736150397Sobrien case TRUNC_MOD_EXPR: 736250397Sobrien case FLOOR_MOD_EXPR: 736350397Sobrien case TRUTH_ANDIF_EXPR: 736450397Sobrien case TRUTH_ORIF_EXPR: 736550397Sobrien case TRUTH_AND_EXPR: 736650397Sobrien case TRUTH_OR_EXPR: 736750397Sobrien case RSHIFT_EXPR: 736850397Sobrien case LSHIFT_EXPR: 736950397Sobrien case RROTATE_EXPR: 737050397Sobrien case LROTATE_EXPR: 737150397Sobrien case EQ_EXPR: 737250397Sobrien case NE_EXPR: 737350397Sobrien case MAX_EXPR: 737450397Sobrien case MIN_EXPR: 737550397Sobrien case LE_EXPR: 737650397Sobrien case GE_EXPR: 737750397Sobrien case LT_EXPR: 737850397Sobrien case GT_EXPR: 737950397Sobrien case COMPONENT_REF: 738050397Sobrien case ARRAY_REF: 738150397Sobrien case COMPOUND_EXPR: 738250397Sobrien case SCOPE_REF: 738350397Sobrien case DOTSTAR_EXPR: 738450397Sobrien case MEMBER_REF: 738590075Sobrien case PREDECREMENT_EXPR: 738690075Sobrien case PREINCREMENT_EXPR: 738790075Sobrien case POSTDECREMENT_EXPR: 738890075Sobrien case POSTINCREMENT_EXPR: 738950397Sobrien return build_nt 739052284Sobrien (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 739152284Sobrien tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); 739250397Sobrien 739350397Sobrien case CALL_EXPR: 739450397Sobrien { 739550397Sobrien tree fn = TREE_OPERAND (t, 0); 739650397Sobrien if (is_overloaded_fn (fn)) 739752284Sobrien fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); 739850397Sobrien else 739950397Sobrien /* Sometimes FN is a LOOKUP_EXPR. */ 740052284Sobrien fn = tsubst_copy (fn, args, complain, in_decl); 740150397Sobrien return build_nt 740252284Sobrien (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, 740352284Sobrien in_decl), 740450397Sobrien NULL_TREE); 740550397Sobrien } 740650397Sobrien 740750397Sobrien case METHOD_CALL_EXPR: 740850397Sobrien { 740950397Sobrien tree name = TREE_OPERAND (t, 0); 741050397Sobrien if (TREE_CODE (name) == BIT_NOT_EXPR) 741150397Sobrien { 741252284Sobrien name = tsubst_copy (TREE_OPERAND (name, 0), args, 741352284Sobrien complain, in_decl); 741450397Sobrien name = build1 (BIT_NOT_EXPR, NULL_TREE, name); 741550397Sobrien } 741650397Sobrien else if (TREE_CODE (name) == SCOPE_REF 741750397Sobrien && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) 741850397Sobrien { 741952284Sobrien tree base = tsubst_copy (TREE_OPERAND (name, 0), args, 742052284Sobrien complain, in_decl); 7421110611Skan name = TREE_OPERAND (TREE_OPERAND (name, 1), 0); 7422110611Skan if (TREE_CODE (name) == TYPE_DECL) 7423110611Skan name = TREE_TYPE (name); 7424110611Skan name = tsubst_copy (name, args, complain, in_decl); 742550397Sobrien name = build1 (BIT_NOT_EXPR, NULL_TREE, name); 742650397Sobrien name = build_nt (SCOPE_REF, base, name); 742750397Sobrien } 742850397Sobrien else 742952284Sobrien name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); 743050397Sobrien return build_nt 743152284Sobrien (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, 743252284Sobrien complain, in_decl), 743352284Sobrien tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), 743450397Sobrien NULL_TREE); 743550397Sobrien } 743650397Sobrien 743790075Sobrien case STMT_EXPR: 743890075Sobrien /* This processing should really occur in tsubst_expr, However, 743990075Sobrien tsubst_expr does not recurse into expressions, since it 744090075Sobrien assumes that there aren't any statements inside them. 744190075Sobrien Instead, it simply calls build_expr_from_tree. So, we need 744290075Sobrien to expand the STMT_EXPR here. */ 744390075Sobrien if (!processing_template_decl) 744490075Sobrien { 744590075Sobrien tree stmt_expr = begin_stmt_expr (); 744696263Sobrien tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl); 744790075Sobrien return finish_stmt_expr (stmt_expr); 744890075Sobrien } 744990075Sobrien 745090075Sobrien return t; 745190075Sobrien 745250397Sobrien case COND_EXPR: 745350397Sobrien case MODOP_EXPR: 745490075Sobrien case PSEUDO_DTOR_EXPR: 745550397Sobrien { 745652284Sobrien r = build_nt 745752284Sobrien (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 745852284Sobrien tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), 745952284Sobrien tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); 746050397Sobrien return r; 746150397Sobrien } 746250397Sobrien 746350397Sobrien case NEW_EXPR: 746450397Sobrien { 746552284Sobrien r = build_nt 746652284Sobrien (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 746752284Sobrien tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), 746852284Sobrien tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); 746950397Sobrien NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); 747050397Sobrien return r; 747150397Sobrien } 747250397Sobrien 747350397Sobrien case DELETE_EXPR: 747450397Sobrien { 747552284Sobrien r = build_nt 747652284Sobrien (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), 747752284Sobrien tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); 747850397Sobrien DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); 747950397Sobrien DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); 748050397Sobrien return r; 748150397Sobrien } 748250397Sobrien 748350397Sobrien case TEMPLATE_ID_EXPR: 748450397Sobrien { 748550397Sobrien /* Substituted template arguments */ 748652284Sobrien tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, 748752284Sobrien in_decl); 748850397Sobrien 748952284Sobrien if (targs && TREE_CODE (targs) == TREE_LIST) 749052284Sobrien { 749152284Sobrien tree chain; 749252284Sobrien for (chain = targs; chain; chain = TREE_CHAIN (chain)) 749352284Sobrien TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); 749452284Sobrien } 749552284Sobrien else if (targs) 749652284Sobrien { 749752284Sobrien int i; 749852284Sobrien for (i = 0; i < TREE_VEC_LENGTH (targs); ++i) 749952284Sobrien TREE_VEC_ELT (targs, i) 750052284Sobrien = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i)); 750152284Sobrien } 750252284Sobrien 750350397Sobrien return lookup_template_function 750452284Sobrien (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); 750550397Sobrien } 750650397Sobrien 750750397Sobrien case TREE_LIST: 750850397Sobrien { 750950397Sobrien tree purpose, value, chain; 751050397Sobrien 751150397Sobrien if (t == void_list_node) 751250397Sobrien return t; 751350397Sobrien 751450397Sobrien purpose = TREE_PURPOSE (t); 751550397Sobrien if (purpose) 751652284Sobrien purpose = tsubst_copy (purpose, args, complain, in_decl); 751750397Sobrien value = TREE_VALUE (t); 751850397Sobrien if (value) 751952284Sobrien value = tsubst_copy (value, args, complain, in_decl); 752050397Sobrien chain = TREE_CHAIN (t); 752150397Sobrien if (chain && chain != void_type_node) 752252284Sobrien chain = tsubst_copy (chain, args, complain, in_decl); 752350397Sobrien if (purpose == TREE_PURPOSE (t) 752450397Sobrien && value == TREE_VALUE (t) 752550397Sobrien && chain == TREE_CHAIN (t)) 752650397Sobrien return t; 752750397Sobrien return tree_cons (purpose, value, chain); 752850397Sobrien } 752950397Sobrien 753050397Sobrien case RECORD_TYPE: 753150397Sobrien case UNION_TYPE: 753250397Sobrien case ENUMERAL_TYPE: 753350397Sobrien case INTEGER_TYPE: 753450397Sobrien case TEMPLATE_TYPE_PARM: 753550397Sobrien case TEMPLATE_TEMPLATE_PARM: 753690075Sobrien case BOUND_TEMPLATE_TEMPLATE_PARM: 753750397Sobrien case TEMPLATE_PARM_INDEX: 753850397Sobrien case POINTER_TYPE: 753950397Sobrien case REFERENCE_TYPE: 754050397Sobrien case OFFSET_TYPE: 754150397Sobrien case FUNCTION_TYPE: 754250397Sobrien case METHOD_TYPE: 754350397Sobrien case ARRAY_TYPE: 754450397Sobrien case TYPENAME_TYPE: 754590075Sobrien case UNBOUND_CLASS_TEMPLATE: 754690075Sobrien case TYPEOF_TYPE: 754750397Sobrien case TYPE_DECL: 754852284Sobrien return tsubst (t, args, complain, in_decl); 754950397Sobrien 755050397Sobrien case IDENTIFIER_NODE: 755190075Sobrien if (IDENTIFIER_TYPENAME_P (t)) 755290075Sobrien { 755390075Sobrien tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); 755490075Sobrien return mangle_conv_op_name_for_type (new_type); 755590075Sobrien } 755650397Sobrien else 755750397Sobrien return t; 755850397Sobrien 755950397Sobrien case CONSTRUCTOR: 756052284Sobrien { 756152284Sobrien r = build 756252284Sobrien (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), 756352284Sobrien NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, 756452284Sobrien complain, in_decl)); 756552284Sobrien TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); 756652284Sobrien return r; 756752284Sobrien } 756850397Sobrien 756990075Sobrien case VA_ARG_EXPR: 757090075Sobrien return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, 757196263Sobrien in_decl), 757296263Sobrien tsubst (TREE_TYPE (t), args, complain, in_decl)); 757390075Sobrien 757450397Sobrien default: 757550397Sobrien return t; 757618334Speter } 757750397Sobrien} 757818334Speter 757990075Sobrien/* Like tsubst_copy, but also does semantic processing. */ 758050397Sobrien 758150397Sobrientree 758252284Sobrientsubst_expr (t, args, complain, in_decl) 758350397Sobrien tree t, args; 758496263Sobrien tsubst_flags_t complain; 758550397Sobrien tree in_decl; 758650397Sobrien{ 758790075Sobrien tree stmt, tmp; 758890075Sobrien 758950397Sobrien if (t == NULL_TREE || t == error_mark_node) 759050397Sobrien return t; 759150397Sobrien 759250397Sobrien if (processing_template_decl) 759352284Sobrien return tsubst_copy (t, args, complain, in_decl); 759450397Sobrien 759590075Sobrien if (!statement_code_p (TREE_CODE (t))) 759690075Sobrien return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); 759790075Sobrien 759850397Sobrien switch (TREE_CODE (t)) 759918334Speter { 760090075Sobrien case RETURN_INIT: 760190075Sobrien prep_stmt (t); 760290075Sobrien finish_named_return_value 760390075Sobrien (TREE_OPERAND (t, 0), 760496263Sobrien tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl)); 760590075Sobrien break; 760690075Sobrien 760790075Sobrien case CTOR_INITIALIZER: 7608117395Skan prep_stmt (t); 7609117395Skan finish_mem_initializers (tsubst_initializer_list 7610117395Skan (TREE_OPERAND (t, 0), args)); 7611117395Skan break; 761290075Sobrien 761350397Sobrien case RETURN_STMT: 761490075Sobrien prep_stmt (t); 7615117395Skan finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t), 761652284Sobrien args, complain, in_decl)); 761750397Sobrien break; 761850397Sobrien 761950397Sobrien case EXPR_STMT: 762090075Sobrien prep_stmt (t); 762150397Sobrien finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), 762252284Sobrien args, complain, in_decl)); 762350397Sobrien break; 762450397Sobrien 762590075Sobrien case USING_STMT: 762690075Sobrien prep_stmt (t); 762790075Sobrien do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t), 762890075Sobrien args, complain, in_decl)); 762990075Sobrien break; 763090075Sobrien 763150397Sobrien case DECL_STMT: 763218334Speter { 763390075Sobrien tree decl; 763490075Sobrien tree init; 763518334Speter 763690075Sobrien prep_stmt (t); 763790075Sobrien decl = DECL_STMT_DECL (t); 763890075Sobrien if (TREE_CODE (decl) == LABEL_DECL) 763990075Sobrien finish_label_decl (DECL_NAME (decl)); 764090075Sobrien else if (TREE_CODE (decl) == USING_DECL) 764190075Sobrien { 764290075Sobrien tree scope = DECL_INITIAL (decl); 764390075Sobrien tree name = DECL_NAME (decl); 764490075Sobrien 764590075Sobrien scope = tsubst_expr (scope, args, complain, in_decl); 764690075Sobrien do_local_using_decl (build_nt (SCOPE_REF, scope, name)); 764790075Sobrien } 764890075Sobrien else 764990075Sobrien { 765090075Sobrien init = DECL_INITIAL (decl); 765190075Sobrien decl = tsubst (decl, args, complain, in_decl); 765290075Sobrien if (decl != error_mark_node) 765390075Sobrien { 765490075Sobrien if (init) 765590075Sobrien DECL_INITIAL (decl) = error_mark_node; 765690075Sobrien /* By marking the declaration as instantiated, we avoid 765790075Sobrien trying to instantiate it. Since instantiate_decl can't 765890075Sobrien handle local variables, and since we've already done 765990075Sobrien all that needs to be done, that's the right thing to 766090075Sobrien do. */ 766190075Sobrien if (TREE_CODE (decl) == VAR_DECL) 766290075Sobrien DECL_TEMPLATE_INSTANTIATED (decl) = 1; 7663104752Skan if (TREE_CODE (decl) == VAR_DECL 7664104752Skan && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) 7665104752Skan /* Anonymous aggregates are a special case. */ 7666104752Skan finish_anon_union (decl); 7667104752Skan else 766896263Sobrien { 7669104752Skan maybe_push_decl (decl); 7670104752Skan if (DECL_PRETTY_FUNCTION_P (decl)) 7671104752Skan { 7672104752Skan /* For __PRETTY_FUNCTION__ we have to adjust the 7673104752Skan initializer. */ 7674104752Skan const char *const name 7675117395Skan = cxx_printable_name (current_function_decl, 2); 7676104752Skan init = cp_fname_init (name); 7677104752Skan TREE_TYPE (decl) = TREE_TYPE (init); 7678104752Skan } 7679104752Skan else 7680104752Skan init = tsubst_expr (init, args, complain, in_decl); 7681104752Skan cp_finish_decl (decl, init, NULL_TREE, 0); 768296263Sobrien } 768390075Sobrien } 768490075Sobrien } 768590075Sobrien 768690075Sobrien /* A DECL_STMT can also be used as an expression, in the condition 7687117395Skan clause of an if/for/while construct. If we aren't followed by 768890075Sobrien another statement, return our decl. */ 768990075Sobrien if (TREE_CHAIN (t) == NULL_TREE) 769090075Sobrien return decl; 769150397Sobrien } 769290075Sobrien break; 769318334Speter 769450397Sobrien case FOR_STMT: 769550397Sobrien { 769690075Sobrien prep_stmt (t); 769718334Speter 769890075Sobrien stmt = begin_for_stmt (); 769990075Sobrien tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl); 770090075Sobrien finish_for_init_stmt (stmt); 770196263Sobrien finish_for_cond (tsubst_expr (FOR_COND (t), 770296263Sobrien args, complain, in_decl), 770390075Sobrien stmt); 770452284Sobrien tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); 770590075Sobrien finish_for_expr (tmp, stmt); 770652284Sobrien tsubst_expr (FOR_BODY (t), args, complain, in_decl); 770790075Sobrien finish_for_stmt (stmt); 770850397Sobrien } 770950397Sobrien break; 771050397Sobrien 771150397Sobrien case WHILE_STMT: 771250397Sobrien { 771390075Sobrien prep_stmt (t); 771490075Sobrien stmt = begin_while_stmt (); 771550397Sobrien finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), 771652284Sobrien args, complain, in_decl), 771790075Sobrien stmt); 771852284Sobrien tsubst_expr (WHILE_BODY (t), args, complain, in_decl); 771990075Sobrien finish_while_stmt (stmt); 772050397Sobrien } 772150397Sobrien break; 772250397Sobrien 772350397Sobrien case DO_STMT: 772450397Sobrien { 772590075Sobrien prep_stmt (t); 772690075Sobrien stmt = begin_do_stmt (); 772752284Sobrien tsubst_expr (DO_BODY (t), args, complain, in_decl); 772890075Sobrien finish_do_body (stmt); 772996263Sobrien finish_do_stmt (tsubst_expr (DO_COND (t), 773096263Sobrien args, complain, in_decl), 773190075Sobrien stmt); 773250397Sobrien } 773350397Sobrien break; 773450397Sobrien 773550397Sobrien case IF_STMT: 773650397Sobrien { 773790075Sobrien prep_stmt (t); 773890075Sobrien stmt = begin_if_stmt (); 773950397Sobrien finish_if_stmt_cond (tsubst_expr (IF_COND (t), 774052284Sobrien args, complain, in_decl), 774190075Sobrien stmt); 774250397Sobrien 774350397Sobrien if (tmp = THEN_CLAUSE (t), tmp) 774418334Speter { 774552284Sobrien tsubst_expr (tmp, args, complain, in_decl); 774690075Sobrien finish_then_clause (stmt); 774718334Speter } 774818334Speter 774950397Sobrien if (tmp = ELSE_CLAUSE (t), tmp) 775050397Sobrien { 775150397Sobrien begin_else_clause (); 775252284Sobrien tsubst_expr (tmp, args, complain, in_decl); 775390075Sobrien finish_else_clause (stmt); 775450397Sobrien } 775518334Speter 775650397Sobrien finish_if_stmt (); 775750397Sobrien } 775850397Sobrien break; 775918334Speter 776050397Sobrien case COMPOUND_STMT: 776150397Sobrien { 776290075Sobrien prep_stmt (t); 776390075Sobrien if (COMPOUND_STMT_BODY_BLOCK (t)) 776490075Sobrien stmt = begin_function_body (); 776590075Sobrien else 776690075Sobrien stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); 776750397Sobrien 776890075Sobrien tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl); 776990075Sobrien 777090075Sobrien if (COMPOUND_STMT_BODY_BLOCK (t)) 777190075Sobrien finish_function_body (stmt); 777290075Sobrien else 777390075Sobrien finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt); 777418334Speter } 777550397Sobrien break; 777650397Sobrien 777750397Sobrien case BREAK_STMT: 777890075Sobrien prep_stmt (t); 777950397Sobrien finish_break_stmt (); 778050397Sobrien break; 778150397Sobrien 778250397Sobrien case CONTINUE_STMT: 778390075Sobrien prep_stmt (t); 778450397Sobrien finish_continue_stmt (); 778550397Sobrien break; 778650397Sobrien 778750397Sobrien case SWITCH_STMT: 778850397Sobrien { 778990075Sobrien tree val; 779050397Sobrien 779190075Sobrien prep_stmt (t); 779290075Sobrien stmt = begin_switch_stmt (); 779352284Sobrien val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); 779490075Sobrien finish_switch_cond (val, stmt); 779590075Sobrien tsubst_expr (SWITCH_BODY (t), args, complain, in_decl); 779690075Sobrien finish_switch_stmt (stmt); 779750397Sobrien } 779850397Sobrien break; 779950397Sobrien 780050397Sobrien case CASE_LABEL: 780190075Sobrien prep_stmt (t); 780252284Sobrien finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), 780390075Sobrien tsubst_expr (CASE_HIGH (t), args, complain, 780490075Sobrien in_decl)); 780550397Sobrien break; 780650397Sobrien 780790075Sobrien case LABEL_STMT: 780890075Sobrien lineno = STMT_LINENO (t); 780990075Sobrien finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t))); 781050397Sobrien break; 781150397Sobrien 7812102780Skan case FILE_STMT: 7813102780Skan input_filename = FILE_STMT_FILENAME (t); 7814102780Skan add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t))); 7815102780Skan break; 7816102780Skan 781750397Sobrien case GOTO_STMT: 781890075Sobrien prep_stmt (t); 781990075Sobrien tmp = GOTO_DESTINATION (t); 782090075Sobrien if (TREE_CODE (tmp) != LABEL_DECL) 782150397Sobrien /* Computed goto's must be tsubst'd into. On the other hand, 782250397Sobrien non-computed gotos must not be; the identifier in question 782350397Sobrien will have no binding. */ 782490075Sobrien tmp = tsubst_expr (tmp, args, complain, in_decl); 782590075Sobrien else 782690075Sobrien tmp = DECL_NAME (tmp); 782790075Sobrien finish_goto_stmt (tmp); 782850397Sobrien break; 782950397Sobrien 783050397Sobrien case ASM_STMT: 783190075Sobrien prep_stmt (t); 7832102780Skan tmp = finish_asm_stmt 7833102780Skan (ASM_CV_QUAL (t), 7834102780Skan tsubst_expr (ASM_STRING (t), args, complain, in_decl), 7835102780Skan tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), 7836102780Skan tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), 7837102780Skan tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl)); 7838102780Skan ASM_INPUT_P (tmp) = ASM_INPUT_P (t); 783950397Sobrien break; 784050397Sobrien 784150397Sobrien case TRY_BLOCK: 784290075Sobrien prep_stmt (t); 784390075Sobrien if (CLEANUP_P (t)) 784450397Sobrien { 784590075Sobrien stmt = begin_try_block (); 784690075Sobrien tsubst_expr (TRY_STMTS (t), args, complain, in_decl); 784790075Sobrien finish_cleanup_try_block (stmt); 784890075Sobrien finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, 784990075Sobrien complain, in_decl), 785090075Sobrien stmt); 785150397Sobrien } 785250397Sobrien else 785390075Sobrien { 785490075Sobrien if (FN_TRY_BLOCK_P (t)) 785590075Sobrien stmt = begin_function_try_block (); 785690075Sobrien else 785790075Sobrien stmt = begin_try_block (); 785890075Sobrien 785990075Sobrien tsubst_expr (TRY_STMTS (t), args, complain, in_decl); 786090075Sobrien 786190075Sobrien if (FN_TRY_BLOCK_P (t)) 786290075Sobrien finish_function_try_block (stmt); 786390075Sobrien else 786490075Sobrien finish_try_block (stmt); 786590075Sobrien 786690075Sobrien tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl); 786790075Sobrien if (FN_TRY_BLOCK_P (t)) 786890075Sobrien finish_function_handler_sequence (stmt); 786990075Sobrien else 787090075Sobrien finish_handler_sequence (stmt); 787190075Sobrien } 787250397Sobrien break; 787390075Sobrien 787490075Sobrien case HANDLER: 787590075Sobrien { 787690075Sobrien tree decl; 787750397Sobrien 787890075Sobrien prep_stmt (t); 787990075Sobrien stmt = begin_handler (); 788090075Sobrien if (HANDLER_PARMS (t)) 788190075Sobrien { 788290075Sobrien decl = DECL_STMT_DECL (HANDLER_PARMS (t)); 788390075Sobrien decl = tsubst (decl, args, complain, in_decl); 788490075Sobrien /* Prevent instantiate_decl from trying to instantiate 788590075Sobrien this variable. We've already done all that needs to be 788690075Sobrien done. */ 788790075Sobrien DECL_TEMPLATE_INSTANTIATED (decl) = 1; 788890075Sobrien } 788990075Sobrien else 789090075Sobrien decl = NULL_TREE; 789190075Sobrien finish_handler_parms (decl, stmt); 789290075Sobrien tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); 789390075Sobrien finish_handler (stmt); 789490075Sobrien } 789590075Sobrien break; 789690075Sobrien 789750397Sobrien case TAG_DEFN: 789890075Sobrien prep_stmt (t); 789990075Sobrien tsubst (TREE_TYPE (t), args, complain, NULL_TREE); 790050397Sobrien break; 790150397Sobrien 790250397Sobrien default: 790390075Sobrien abort (); 790418334Speter } 790590075Sobrien 790690075Sobrien return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); 790750397Sobrien} 790818334Speter 790952284Sobrien/* Instantiate the indicated variable or function template TMPL with 791052284Sobrien the template arguments in TARG_PTR. */ 791152284Sobrien 791250397Sobrientree 791350397Sobrieninstantiate_template (tmpl, targ_ptr) 791450397Sobrien tree tmpl, targ_ptr; 791550397Sobrien{ 791650397Sobrien tree fndecl; 791752284Sobrien tree gen_tmpl; 791852284Sobrien tree spec; 791950397Sobrien int i, len; 792052284Sobrien tree inner_args; 792118334Speter 792250397Sobrien if (tmpl == error_mark_node) 792350397Sobrien return error_mark_node; 792450397Sobrien 792550397Sobrien my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); 792650397Sobrien 792790075Sobrien /* If this function is a clone, handle it specially. */ 792890075Sobrien if (DECL_CLONED_FUNCTION_P (tmpl)) 792990075Sobrien { 793090075Sobrien tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr); 793190075Sobrien tree clone; 793290075Sobrien 7933117395Skan /* Look for the clone. */ 793490075Sobrien for (clone = TREE_CHAIN (spec); 793590075Sobrien clone && DECL_CLONED_FUNCTION_P (clone); 793690075Sobrien clone = TREE_CHAIN (clone)) 793790075Sobrien if (DECL_NAME (clone) == DECL_NAME (tmpl)) 793890075Sobrien return clone; 793990075Sobrien /* We should always have found the clone by now. */ 794090075Sobrien abort (); 794190075Sobrien return NULL_TREE; 794290075Sobrien } 794390075Sobrien 794452284Sobrien /* Check to see if we already have this specialization. */ 794552284Sobrien spec = retrieve_specialization (tmpl, targ_ptr); 794652284Sobrien if (spec != NULL_TREE) 794752284Sobrien return spec; 794852284Sobrien 794990075Sobrien gen_tmpl = most_general_template (tmpl); 795090075Sobrien if (tmpl != gen_tmpl) 795118334Speter { 795252284Sobrien /* The TMPL is a partial instantiation. To get a full set of 795352284Sobrien arguments we must add the arguments used to perform the 795452284Sobrien partial instantiation. */ 795552284Sobrien targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), 795652284Sobrien targ_ptr); 795752284Sobrien 795852284Sobrien /* Check to see if we already have this specialization. */ 795952284Sobrien spec = retrieve_specialization (gen_tmpl, targ_ptr); 796050397Sobrien if (spec != NULL_TREE) 796150397Sobrien return spec; 796218334Speter } 796350397Sobrien 796452284Sobrien len = DECL_NTPARMS (gen_tmpl); 796590075Sobrien inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr); 796650397Sobrien i = len; 796750397Sobrien while (i--) 796818334Speter { 796952284Sobrien tree t = TREE_VEC_ELT (inner_args, i); 797090075Sobrien if (TYPE_P (t)) 797150397Sobrien { 797250397Sobrien tree nt = target_type (t); 797350397Sobrien if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) 797450397Sobrien { 797590075Sobrien error ("type `%T' composed from a local class is not a valid template-argument", t); 797690075Sobrien error (" trying to instantiate `%D'", gen_tmpl); 797790075Sobrien return error_mark_node; 797850397Sobrien } 797950397Sobrien } 798018334Speter } 798150397Sobrien 7982117395Skan /* Make sure that we can see identifiers, and compute access 7983117395Skan correctly. The desired FUNCTION_DECL for FNDECL may or may not be 7984117395Skan created earlier. Let push_access_scope_real figure that out. */ 7985117395Skan push_access_scope_real 7986117395Skan (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr, 7987117395Skan tf_error, gen_tmpl)); 7988117395Skan 798950397Sobrien /* substitute template parameters */ 799090075Sobrien fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), 799196263Sobrien targ_ptr, tf_error, gen_tmpl); 7992117395Skan 7993117395Skan pop_access_scope (gen_tmpl); 7994117395Skan 799552284Sobrien /* The DECL_TI_TEMPLATE should always be the immediate parent 799652284Sobrien template, not the most general template. */ 799752284Sobrien DECL_TI_TEMPLATE (fndecl) = tmpl; 799850397Sobrien 799950397Sobrien if (flag_external_templates) 800050397Sobrien add_pending_template (fndecl); 800150397Sobrien 800290075Sobrien /* If we've just instantiated the main entry point for a function, 800390075Sobrien instantiate all the alternate entry points as well. We do this 800490075Sobrien by cloning the instantiation of the main entry point, not by 800590075Sobrien instantiating the template clones. */ 800690075Sobrien if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) 800790075Sobrien clone_function_decl (fndecl, /*update_method_vec_p=*/0); 800818334Speter 800918334Speter return fndecl; 801018334Speter} 801118334Speter 801252284Sobrien/* The FN is a TEMPLATE_DECL for a function. The ARGS are the 801352284Sobrien arguments that are being used when calling it. TARGS is a vector 801452284Sobrien into which the deduced template arguments are placed. 801550397Sobrien 801650397Sobrien Return zero for success, 2 for an incomplete match that doesn't resolve 801750397Sobrien all the types, and 1 for complete failure. An error message will be 801850397Sobrien printed only for an incomplete match. 801918334Speter 802090075Sobrien If FN is a conversion operator, or we are trying to produce a specific 802190075Sobrien specialization, RETURN_TYPE is the return type desired. 802250397Sobrien 802350397Sobrien The EXPLICIT_TARGS are explicit template arguments provided via a 802450397Sobrien template-id. 802550397Sobrien 802650397Sobrien The parameter STRICT is one of: 802750397Sobrien 802850397Sobrien DEDUCE_CALL: 802950397Sobrien We are deducing arguments for a function call, as in 803050397Sobrien [temp.deduct.call]. 803150397Sobrien 803250397Sobrien DEDUCE_CONV: 803350397Sobrien We are deducing arguments for a conversion function, as in 803450397Sobrien [temp.deduct.conv]. 803550397Sobrien 803650397Sobrien DEDUCE_EXACT: 803790075Sobrien We are deducing arguments when doing an explicit instantiation 803890075Sobrien as in [temp.explicit], when determining an explicit specialization 803990075Sobrien as in [temp.expl.spec], or when taking the address of a function 804090075Sobrien template, as in [temp.deduct.funcaddr]. 804190075Sobrien 804290075Sobrien DEDUCE_ORDER: 804350397Sobrien We are deducing arguments when calculating the partial 804450397Sobrien ordering between specializations of function or class 804590075Sobrien templates, as in [temp.func.order] and [temp.class.order]. 804650397Sobrien 804790075Sobrien LEN is the number of parms to consider before returning success, or -1 804890075Sobrien for all. This is used in partial ordering to avoid comparing parms for 804990075Sobrien which no actual argument was passed, since they are not considered in 805090075Sobrien overload resolution (and are explicitly excluded from consideration in 805190075Sobrien partial ordering in [temp.func.order]/6). */ 805252284Sobrien 805350397Sobrienint 805452284Sobrienfn_type_unification (fn, explicit_targs, targs, args, return_type, 805590075Sobrien strict, len) 805652284Sobrien tree fn, explicit_targs, targs, args, return_type; 805750397Sobrien unification_kind_t strict; 805890075Sobrien int len; 805918334Speter{ 806052284Sobrien tree parms; 806152284Sobrien tree fntype; 806290075Sobrien int result; 806318334Speter 806452284Sobrien my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); 8065117395Skan 806652284Sobrien fntype = TREE_TYPE (fn); 806752284Sobrien if (explicit_targs) 806818334Speter { 806952284Sobrien /* [temp.deduct] 807052284Sobrien 807152284Sobrien The specified template arguments must match the template 807252284Sobrien parameters in kind (i.e., type, nontype, template), and there 807352284Sobrien must not be more arguments than there are parameters; 807452284Sobrien otherwise type deduction fails. 807518334Speter 807652284Sobrien Nontype arguments must match the types of the corresponding 807752284Sobrien nontype template parameters, or must be convertible to the 807852284Sobrien types of the corresponding nontype parameters as specified in 807952284Sobrien _temp.arg.nontype_, otherwise type deduction fails. 808052284Sobrien 808152284Sobrien All references in the function type of the function template 808252284Sobrien to the corresponding template parameters are replaced by the 808352284Sobrien specified template argument values. If a substitution in a 808452284Sobrien template parameter or in the function type of the function 808552284Sobrien template results in an invalid type, type deduction fails. */ 808652284Sobrien int i; 808752284Sobrien tree converted_args; 808852284Sobrien 808952284Sobrien converted_args 809052284Sobrien = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), 809196263Sobrien explicit_targs, NULL_TREE, tf_none, 809252284Sobrien /*require_all_arguments=*/0)); 809352284Sobrien if (converted_args == error_mark_node) 809450397Sobrien return 1; 809518334Speter 809696263Sobrien fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); 809752284Sobrien if (fntype == error_mark_node) 809852284Sobrien return 1; 809918334Speter 810052284Sobrien /* Place the explicitly specified arguments in TARGS. */ 810152284Sobrien for (i = 0; i < TREE_VEC_LENGTH (targs); i++) 810252284Sobrien TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); 810318334Speter } 810452284Sobrien 810552284Sobrien parms = TYPE_ARG_TYPES (fntype); 810690075Sobrien /* Never do unification on the 'this' parameter. */ 810790075Sobrien if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) 810890075Sobrien parms = TREE_CHAIN (parms); 810990075Sobrien 811090075Sobrien if (return_type) 811152284Sobrien { 811290075Sobrien /* We've been given a return type to match, prepend it. */ 811390075Sobrien parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); 811490075Sobrien args = tree_cons (NULL_TREE, return_type, args); 811590075Sobrien if (len >= 0) 811690075Sobrien ++len; 811752284Sobrien } 811852284Sobrien 811952284Sobrien /* We allow incomplete unification without an error message here 812052284Sobrien because the standard doesn't seem to explicitly prohibit it. Our 812152284Sobrien callers must be ready to deal with unification failures in any 812252284Sobrien event. */ 812390075Sobrien result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), 812490075Sobrien targs, parms, args, /*subr=*/0, 812590075Sobrien strict, /*allow_incomplete*/1, len); 812690075Sobrien 812790075Sobrien if (result == 0) 812890075Sobrien /* All is well so far. Now, check: 812990075Sobrien 813090075Sobrien [temp.deduct] 813190075Sobrien 813290075Sobrien When all template arguments have been deduced, all uses of 813390075Sobrien template parameters in nondeduced contexts are replaced with 813490075Sobrien the corresponding deduced argument values. If the 813590075Sobrien substitution results in an invalid type, as described above, 813690075Sobrien type deduction fails. */ 813796263Sobrien if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) 813890075Sobrien == error_mark_node) 813990075Sobrien return 1; 814090075Sobrien 814190075Sobrien return result; 814218334Speter} 814318334Speter 814450397Sobrien/* Adjust types before performing type deduction, as described in 814550397Sobrien [temp.deduct.call] and [temp.deduct.conv]. The rules in these two 814650397Sobrien sections are symmetric. PARM is the type of a function parameter 814750397Sobrien or the return type of the conversion function. ARG is the type of 814850397Sobrien the argument passed to the call, or the type of the value 814990075Sobrien initialized with the result of the conversion function. */ 815050397Sobrien 815190075Sobrienstatic int 815250397Sobrienmaybe_adjust_types_for_deduction (strict, parm, arg) 815350397Sobrien unification_kind_t strict; 815450397Sobrien tree* parm; 815550397Sobrien tree* arg; 815618334Speter{ 815790075Sobrien int result = 0; 815890075Sobrien 815950397Sobrien switch (strict) 816050397Sobrien { 816150397Sobrien case DEDUCE_CALL: 816250397Sobrien break; 816318334Speter 816450397Sobrien case DEDUCE_CONV: 816550397Sobrien { 816650397Sobrien /* Swap PARM and ARG throughout the remainder of this 816750397Sobrien function; the handling is precisely symmetric since PARM 816850397Sobrien will initialize ARG rather than vice versa. */ 816950397Sobrien tree* temp = parm; 817050397Sobrien parm = arg; 817150397Sobrien arg = temp; 817250397Sobrien break; 817350397Sobrien } 817450397Sobrien 817550397Sobrien case DEDUCE_EXACT: 817650397Sobrien /* There is nothing to do in this case. */ 817790075Sobrien return 0; 817850397Sobrien 817990075Sobrien case DEDUCE_ORDER: 818090075Sobrien /* DR 214. [temp.func.order] is underspecified, and leads to no 818190075Sobrien ordering between things like `T *' and `T const &' for `U *'. 818290075Sobrien The former has T=U and the latter T=U*. The former looks more 818390075Sobrien specialized and John Spicer considers it well-formed (the EDG 818490075Sobrien compiler accepts it). 818590075Sobrien 818690075Sobrien John also confirms that deduction should proceed as in a function 818790075Sobrien call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL. 818890075Sobrien However, in ordering, ARG can have REFERENCE_TYPE, but no argument 818990075Sobrien to an actual call can have such a type. 819090075Sobrien 819190075Sobrien If both ARG and PARM are REFERENCE_TYPE, we change neither. 819290075Sobrien If only ARG is a REFERENCE_TYPE, we look through that and then 819390075Sobrien proceed as with DEDUCE_CALL (which could further convert it). */ 819490075Sobrien if (TREE_CODE (*arg) == REFERENCE_TYPE) 819590075Sobrien { 819690075Sobrien if (TREE_CODE (*parm) == REFERENCE_TYPE) 819790075Sobrien return 0; 819890075Sobrien *arg = TREE_TYPE (*arg); 819990075Sobrien } 820090075Sobrien break; 820150397Sobrien default: 820290075Sobrien abort (); 820318334Speter } 820418334Speter 820550397Sobrien if (TREE_CODE (*parm) != REFERENCE_TYPE) 820618334Speter { 820750397Sobrien /* [temp.deduct.call] 820850397Sobrien 820950397Sobrien If P is not a reference type: 821050397Sobrien 821150397Sobrien --If A is an array type, the pointer type produced by the 821250397Sobrien array-to-pointer standard conversion (_conv.array_) is 821350397Sobrien used in place of A for type deduction; otherwise, 821450397Sobrien 821550397Sobrien --If A is a function type, the pointer type produced by 821650397Sobrien the function-to-pointer standard conversion 821750397Sobrien (_conv.func_) is used in place of A for type deduction; 821850397Sobrien otherwise, 821950397Sobrien 822050397Sobrien --If A is a cv-qualified type, the top level 822150397Sobrien cv-qualifiers of A's type are ignored for type 822250397Sobrien deduction. */ 822350397Sobrien if (TREE_CODE (*arg) == ARRAY_TYPE) 822450397Sobrien *arg = build_pointer_type (TREE_TYPE (*arg)); 822552284Sobrien else if (TREE_CODE (*arg) == FUNCTION_TYPE) 822650397Sobrien *arg = build_pointer_type (*arg); 822750397Sobrien else 822850397Sobrien *arg = TYPE_MAIN_VARIANT (*arg); 822918334Speter } 823050397Sobrien 823150397Sobrien /* [temp.deduct.call] 823250397Sobrien 823350397Sobrien If P is a cv-qualified type, the top level cv-qualifiers 823450397Sobrien of P's type are ignored for type deduction. If P is a 823550397Sobrien reference type, the type referred to by P is used for 823650397Sobrien type deduction. */ 823750397Sobrien *parm = TYPE_MAIN_VARIANT (*parm); 823850397Sobrien if (TREE_CODE (*parm) == REFERENCE_TYPE) 823990075Sobrien { 824090075Sobrien *parm = TREE_TYPE (*parm); 824190075Sobrien result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; 824290075Sobrien } 8243117395Skan 8244117395Skan /* DR 322. For conversion deduction, remove a reference type on parm 8245117395Skan too (which has been swapped into ARG). */ 8246117395Skan if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) 8247117395Skan *arg = TREE_TYPE (*arg); 8248117395Skan 824990075Sobrien return result; 825018334Speter} 825118334Speter 825290075Sobrien/* Most parms like fn_type_unification. 825318334Speter 825450397Sobrien If SUBR is 1, we're being called recursively (to unify the 825550397Sobrien arguments of a function or method parameter of a function 825650397Sobrien template). */ 825718334Speter 825850397Sobrienstatic int 825990075Sobrientype_unification_real (tparms, targs, xparms, xargs, subr, 826090075Sobrien strict, allow_incomplete, xlen) 826190075Sobrien tree tparms, targs, xparms, xargs; 826250397Sobrien int subr; 826350397Sobrien unification_kind_t strict; 826490075Sobrien int allow_incomplete, xlen; 826518334Speter{ 826618334Speter tree parm, arg; 826718334Speter int i; 826818334Speter int ntparms = TREE_VEC_LENGTH (tparms); 826950397Sobrien int sub_strict; 827090075Sobrien int saw_undeduced = 0; 827190075Sobrien tree parms, args; 827290075Sobrien int len; 827318334Speter 827418334Speter my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); 827590075Sobrien my_friendly_assert (xparms == NULL_TREE 827690075Sobrien || TREE_CODE (xparms) == TREE_LIST, 290); 8277117395Skan /* ARGS could be NULL. */ 827890075Sobrien if (xargs) 827990075Sobrien my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291); 828018334Speter my_friendly_assert (ntparms > 0, 292); 828118334Speter 828250397Sobrien switch (strict) 828350397Sobrien { 828450397Sobrien case DEDUCE_CALL: 828590075Sobrien sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL 828690075Sobrien | UNIFY_ALLOW_DERIVED); 828750397Sobrien break; 828850397Sobrien 828950397Sobrien case DEDUCE_CONV: 829050397Sobrien sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; 829150397Sobrien break; 829218334Speter 829350397Sobrien case DEDUCE_EXACT: 829450397Sobrien sub_strict = UNIFY_ALLOW_NONE; 829550397Sobrien break; 829690075Sobrien 829790075Sobrien case DEDUCE_ORDER: 829890075Sobrien sub_strict = UNIFY_ALLOW_NONE; 829990075Sobrien break; 830050397Sobrien 830150397Sobrien default: 830290075Sobrien abort (); 830350397Sobrien } 830450397Sobrien 830590075Sobrien if (xlen == 0) 830690075Sobrien return 0; 830790075Sobrien 830890075Sobrien again: 830990075Sobrien parms = xparms; 831090075Sobrien args = xargs; 831190075Sobrien len = xlen; 831290075Sobrien 831318334Speter while (parms 831418334Speter && parms != void_list_node 831518334Speter && args 831618334Speter && args != void_list_node) 831718334Speter { 831818334Speter parm = TREE_VALUE (parms); 831918334Speter parms = TREE_CHAIN (parms); 832018334Speter arg = TREE_VALUE (args); 832118334Speter args = TREE_CHAIN (args); 832218334Speter 832318334Speter if (arg == error_mark_node) 832418334Speter return 1; 832518334Speter if (arg == unknown_type_node) 832652284Sobrien /* We can't deduce anything from this, but we might get all the 832752284Sobrien template args from other function args. */ 832852284Sobrien continue; 832918334Speter 833050397Sobrien /* Conversions will be performed on a function argument that 833150397Sobrien corresponds with a function parameter that contains only 833250397Sobrien non-deducible template parameters and explicitly specified 833350397Sobrien template parameters. */ 833450397Sobrien if (! uses_template_parms (parm)) 833518334Speter { 833650397Sobrien tree type; 833750397Sobrien 833890075Sobrien if (!TYPE_P (arg)) 833950397Sobrien type = TREE_TYPE (arg); 834050397Sobrien else 834150397Sobrien { 834250397Sobrien type = arg; 834350397Sobrien arg = NULL_TREE; 834450397Sobrien } 834550397Sobrien 834690075Sobrien if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER) 834750397Sobrien { 834852284Sobrien if (same_type_p (parm, type)) 834950397Sobrien continue; 835050397Sobrien } 835150397Sobrien else 835250397Sobrien /* It might work; we shouldn't check now, because we might 835350397Sobrien get into infinite recursion. Overload resolution will 835450397Sobrien handle it. */ 835518334Speter continue; 835650397Sobrien 835718334Speter return 1; 835818334Speter } 835918334Speter 836090075Sobrien if (!TYPE_P (arg)) 836118334Speter { 836218334Speter my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); 836352284Sobrien if (type_unknown_p (arg)) 836418334Speter { 836552284Sobrien /* [temp.deduct.type] A template-argument can be deduced from 836652284Sobrien a pointer to function or pointer to member function 836752284Sobrien argument if the set of overloaded functions does not 836852284Sobrien contain function templates and at most one of a set of 836952284Sobrien overloaded functions provides a unique match. */ 837018334Speter 837152284Sobrien if (resolve_overloaded_unification 837252284Sobrien (tparms, targs, parm, arg, strict, sub_strict) 837352284Sobrien != 0) 837452284Sobrien return 1; 837552284Sobrien continue; 837618334Speter } 837718334Speter arg = TREE_TYPE (arg); 837818334Speter } 837990075Sobrien 838090075Sobrien { 838190075Sobrien int arg_strict = sub_strict; 838290075Sobrien 838390075Sobrien if (!subr) 838490075Sobrien arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); 838518334Speter 838690075Sobrien if (unify (tparms, targs, parm, arg, arg_strict)) 838790075Sobrien return 1; 838890075Sobrien } 838950397Sobrien 839090075Sobrien /* Are we done with the interesting parms? */ 839190075Sobrien if (--len == 0) 839290075Sobrien goto done; 839318334Speter } 839418334Speter /* Fail if we've reached the end of the parm list, and more args 839518334Speter are present, and the parm list isn't variadic. */ 839618334Speter if (args && args != void_list_node && parms == void_list_node) 839718334Speter return 1; 839818334Speter /* Fail if parms are left and they don't have default values. */ 839918334Speter if (parms 840018334Speter && parms != void_list_node 840118334Speter && TREE_PURPOSE (parms) == NULL_TREE) 840218334Speter return 1; 840390075Sobrien 840490075Sobrien done: 840518334Speter if (!subr) 840618334Speter for (i = 0; i < ntparms; i++) 840750397Sobrien if (TREE_VEC_ELT (targs, i) == NULL_TREE) 840818334Speter { 840990075Sobrien tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); 841090075Sobrien 841190075Sobrien /* If this is an undeduced nontype parameter that depends on 841290075Sobrien a type parameter, try another pass; its type may have been 841390075Sobrien deduced from a later argument than the one from which 841490075Sobrien this parameter can be deduced. */ 841590075Sobrien if (TREE_CODE (tparm) == PARM_DECL 841690075Sobrien && uses_template_parms (TREE_TYPE (tparm)) 841790075Sobrien && !saw_undeduced++) 841890075Sobrien goto again; 841990075Sobrien 842050397Sobrien if (!allow_incomplete) 842150397Sobrien error ("incomplete type unification"); 842218334Speter return 2; 842318334Speter } 842418334Speter return 0; 842518334Speter} 842618334Speter 842752284Sobrien/* Subroutine of type_unification_real. Args are like the variables at the 842852284Sobrien call site. ARG is an overloaded function (or template-id); we try 842952284Sobrien deducing template args from each of the overloads, and if only one 843052284Sobrien succeeds, we go with that. Modifies TARGS and returns 0 on success. */ 843152284Sobrien 843252284Sobrienstatic int 843352284Sobrienresolve_overloaded_unification (tparms, targs, parm, arg, strict, 843452284Sobrien sub_strict) 843552284Sobrien tree tparms, targs, parm, arg; 843652284Sobrien unification_kind_t strict; 843752284Sobrien int sub_strict; 843852284Sobrien{ 843952284Sobrien tree tempargs = copy_node (targs); 844052284Sobrien int good = 0; 8441122180Skan bool addr_p; 844252284Sobrien 844352284Sobrien if (TREE_CODE (arg) == ADDR_EXPR) 8444122180Skan { 8445122180Skan arg = TREE_OPERAND (arg, 0); 8446122180Skan addr_p = true; 8447122180Skan } 8448122180Skan else 8449122180Skan addr_p = false; 845052284Sobrien 845152284Sobrien if (TREE_CODE (arg) == COMPONENT_REF) 845252284Sobrien /* Handle `&x' where `x' is some static or non-static member 845352284Sobrien function name. */ 845452284Sobrien arg = TREE_OPERAND (arg, 1); 845552284Sobrien 845652284Sobrien if (TREE_CODE (arg) == OFFSET_REF) 845752284Sobrien arg = TREE_OPERAND (arg, 1); 845852284Sobrien 845952284Sobrien /* Strip baselink information. */ 8460117395Skan if (BASELINK_P (arg)) 8461117395Skan arg = BASELINK_FUNCTIONS (arg); 846252284Sobrien 846352284Sobrien if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) 846452284Sobrien { 846552284Sobrien /* If we got some explicit template args, we need to plug them into 846652284Sobrien the affected templates before we try to unify, in case the 846752284Sobrien explicit args will completely resolve the templates in question. */ 846852284Sobrien 846952284Sobrien tree expl_subargs = TREE_OPERAND (arg, 1); 847052284Sobrien arg = TREE_OPERAND (arg, 0); 847152284Sobrien 847252284Sobrien for (; arg; arg = OVL_NEXT (arg)) 847352284Sobrien { 847452284Sobrien tree fn = OVL_CURRENT (arg); 847552284Sobrien tree subargs, elem; 847652284Sobrien 847752284Sobrien if (TREE_CODE (fn) != TEMPLATE_DECL) 847852284Sobrien continue; 847952284Sobrien 848090075Sobrien subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn), 848190075Sobrien expl_subargs); 848252284Sobrien if (subargs) 848352284Sobrien { 848496263Sobrien elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); 8485122180Skan good += try_one_overload (tparms, targs, tempargs, parm, 8486122180Skan elem, strict, sub_strict, addr_p); 848752284Sobrien } 848852284Sobrien } 848952284Sobrien } 8490117395Skan else if (TREE_CODE (arg) == OVERLOAD 8491117395Skan || TREE_CODE (arg) == FUNCTION_DECL) 849252284Sobrien { 849352284Sobrien for (; arg; arg = OVL_NEXT (arg)) 8494122180Skan good += try_one_overload (tparms, targs, tempargs, parm, 8495122180Skan TREE_TYPE (OVL_CURRENT (arg)), 8496122180Skan strict, sub_strict, addr_p); 849752284Sobrien } 849852284Sobrien else 849990075Sobrien abort (); 850052284Sobrien 850152284Sobrien /* [temp.deduct.type] A template-argument can be deduced from a pointer 850252284Sobrien to function or pointer to member function argument if the set of 850352284Sobrien overloaded functions does not contain function templates and at most 850452284Sobrien one of a set of overloaded functions provides a unique match. 850552284Sobrien 850652284Sobrien So if we found multiple possibilities, we return success but don't 850752284Sobrien deduce anything. */ 850852284Sobrien 850952284Sobrien if (good == 1) 851052284Sobrien { 851152284Sobrien int i = TREE_VEC_LENGTH (targs); 851252284Sobrien for (; i--; ) 851352284Sobrien if (TREE_VEC_ELT (tempargs, i)) 851452284Sobrien TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); 851552284Sobrien } 851652284Sobrien if (good) 851752284Sobrien return 0; 851852284Sobrien 851952284Sobrien return 1; 852052284Sobrien} 852152284Sobrien 852252284Sobrien/* Subroutine of resolve_overloaded_unification; does deduction for a single 852352284Sobrien overload. Fills TARGS with any deduced arguments, or error_mark_node if 852452284Sobrien different overloads deduce different arguments for a given parm. 8525122180Skan ADDR_P is true if the expression for which deduction is being 8526122180Skan performed was of the form "& fn" rather than simply "fn". 8527122180Skan 852852284Sobrien Returns 1 on success. */ 852952284Sobrien 853052284Sobrienstatic int 8531122180Skantry_one_overload (tree tparms, 8532122180Skan tree orig_targs, 8533122180Skan tree targs, 8534122180Skan tree parm, 8535122180Skan tree arg, 8536122180Skan unification_kind_t strict, 8537122180Skan int sub_strict, 8538122180Skan bool addr_p) 853952284Sobrien{ 854052284Sobrien int nargs; 854152284Sobrien tree tempargs; 854252284Sobrien int i; 854352284Sobrien 854452284Sobrien /* [temp.deduct.type] A template-argument can be deduced from a pointer 854552284Sobrien to function or pointer to member function argument if the set of 854652284Sobrien overloaded functions does not contain function templates and at most 854752284Sobrien one of a set of overloaded functions provides a unique match. 854852284Sobrien 854952284Sobrien So if this is a template, just return success. */ 855052284Sobrien 855152284Sobrien if (uses_template_parms (arg)) 855252284Sobrien return 1; 855352284Sobrien 8554122180Skan if (TREE_CODE (arg) == METHOD_TYPE) 8555122180Skan arg = build_ptrmemfunc_type (build_pointer_type (arg)); 8556122180Skan else if (addr_p) 8557122180Skan arg = build_pointer_type (arg); 8558122180Skan 855990075Sobrien sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); 856052284Sobrien 856152284Sobrien /* We don't copy orig_targs for this because if we have already deduced 856252284Sobrien some template args from previous args, unify would complain when we 856352284Sobrien try to deduce a template parameter for the same argument, even though 856452284Sobrien there isn't really a conflict. */ 856552284Sobrien nargs = TREE_VEC_LENGTH (targs); 856690075Sobrien tempargs = make_tree_vec (nargs); 856752284Sobrien 856852284Sobrien if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) 856952284Sobrien return 0; 857052284Sobrien 857152284Sobrien /* First make sure we didn't deduce anything that conflicts with 857252284Sobrien explicitly specified args. */ 857352284Sobrien for (i = nargs; i--; ) 857452284Sobrien { 857552284Sobrien tree elt = TREE_VEC_ELT (tempargs, i); 857652284Sobrien tree oldelt = TREE_VEC_ELT (orig_targs, i); 857752284Sobrien 857852284Sobrien if (elt == NULL_TREE) 857952284Sobrien continue; 858052284Sobrien else if (uses_template_parms (elt)) 858152284Sobrien { 858252284Sobrien /* Since we're unifying against ourselves, we will fill in template 858352284Sobrien args used in the function parm list with our own template parms. 858452284Sobrien Discard them. */ 858552284Sobrien TREE_VEC_ELT (tempargs, i) = NULL_TREE; 858652284Sobrien continue; 858752284Sobrien } 858852284Sobrien else if (oldelt && ! template_args_equal (oldelt, elt)) 858952284Sobrien return 0; 859052284Sobrien } 859152284Sobrien 859252284Sobrien for (i = nargs; i--; ) 859352284Sobrien { 859452284Sobrien tree elt = TREE_VEC_ELT (tempargs, i); 859552284Sobrien 859652284Sobrien if (elt) 859752284Sobrien TREE_VEC_ELT (targs, i) = elt; 859852284Sobrien } 859952284Sobrien 860052284Sobrien return 1; 860152284Sobrien} 860252284Sobrien 860390075Sobrien/* Verify that nondeduce template argument agrees with the type 860490075Sobrien obtained from argument deduction. Return nonzero if the 860590075Sobrien verification fails. 860690075Sobrien 860790075Sobrien For example: 860890075Sobrien 860990075Sobrien struct A { typedef int X; }; 861090075Sobrien template <class T, class U> struct C {}; 861190075Sobrien template <class T> struct C<T, typename T::X> {}; 861290075Sobrien 861390075Sobrien Then with the instantiation `C<A, int>', we can deduce that 861490075Sobrien `T' is `A' but unify () does not check whether `typename T::X' 861590075Sobrien is `int'. This function ensure that they agree. 861690075Sobrien 861790075Sobrien TARGS, PARMS are the same as the arguments of unify. 861890075Sobrien ARGS contains template arguments from all levels. */ 861990075Sobrien 862090075Sobrienstatic int 862190075Sobrienverify_class_unification (targs, parms, args) 862290075Sobrien tree targs, parms, args; 862390075Sobrien{ 8624117395Skan parms = tsubst (parms, add_outermost_template_args (args, targs), 8625117395Skan tf_none, NULL_TREE); 8626117395Skan if (parms == error_mark_node) 862790075Sobrien return 1; 862890075Sobrien 8629117395Skan return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args)); 863090075Sobrien} 863190075Sobrien 863252284Sobrien/* PARM is a template class (perhaps with unbound template 863352284Sobrien parameters). ARG is a fully instantiated type. If ARG can be 863452284Sobrien bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and 863552284Sobrien TARGS are as for unify. */ 863652284Sobrien 863752284Sobrienstatic tree 863852284Sobrientry_class_unification (tparms, targs, parm, arg) 863952284Sobrien tree tparms; 864052284Sobrien tree targs; 864152284Sobrien tree parm; 864252284Sobrien tree arg; 864352284Sobrien{ 864452284Sobrien tree copy_of_targs; 864552284Sobrien 864652284Sobrien if (!CLASSTYPE_TEMPLATE_INFO (arg) 8647117395Skan || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) 8648117395Skan != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) 864952284Sobrien return NULL_TREE; 865052284Sobrien 865152284Sobrien /* We need to make a new template argument vector for the call to 865252284Sobrien unify. If we used TARGS, we'd clutter it up with the result of 865352284Sobrien the attempted unification, even if this class didn't work out. 865452284Sobrien We also don't want to commit ourselves to all the unifications 865552284Sobrien we've already done, since unification is supposed to be done on 865652284Sobrien an argument-by-argument basis. In other words, consider the 865752284Sobrien following pathological case: 865852284Sobrien 865952284Sobrien template <int I, int J, int K> 866052284Sobrien struct S {}; 866152284Sobrien 866252284Sobrien template <int I, int J> 866352284Sobrien struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; 866452284Sobrien 866552284Sobrien template <int I, int J, int K> 866652284Sobrien void f(S<I, J, K>, S<I, I, I>); 866752284Sobrien 866852284Sobrien void g() { 866952284Sobrien S<0, 0, 0> s0; 867052284Sobrien S<0, 1, 2> s2; 867152284Sobrien 867252284Sobrien f(s0, s2); 867352284Sobrien } 867452284Sobrien 867552284Sobrien Now, by the time we consider the unification involving `s2', we 867652284Sobrien already know that we must have `f<0, 0, 0>'. But, even though 8677117395Skan `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid 867852284Sobrien because there are two ways to unify base classes of S<0, 1, 2> 867952284Sobrien with S<I, I, I>. If we kept the already deduced knowledge, we 868052284Sobrien would reject the possibility I=1. */ 868190075Sobrien copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); 868252284Sobrien 868352284Sobrien /* If unification failed, we're done. */ 868490075Sobrien if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), 868590075Sobrien CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) 868652284Sobrien return NULL_TREE; 868790075Sobrien 868890075Sobrien return arg; 868952284Sobrien} 869052284Sobrien 869152284Sobrien/* Subroutine of get_template_base. RVAL, if non-NULL, is a base we 869290075Sobrien have already discovered to be satisfactory. ARG_BINFO is the binfo 869352284Sobrien for the base class of ARG that we are currently examining. */ 869452284Sobrien 869552284Sobrienstatic tree 869652284Sobrienget_template_base_recursive (tparms, targs, parm, 869752284Sobrien arg_binfo, rval, flags) 869852284Sobrien tree tparms; 869952284Sobrien tree targs; 870052284Sobrien tree arg_binfo; 870152284Sobrien tree rval; 870252284Sobrien tree parm; 870352284Sobrien int flags; 870452284Sobrien{ 870552284Sobrien tree binfos; 870652284Sobrien int i, n_baselinks; 870752284Sobrien tree arg = BINFO_TYPE (arg_binfo); 870852284Sobrien 870952284Sobrien if (!(flags & GTB_IGNORE_TYPE)) 871052284Sobrien { 871152284Sobrien tree r = try_class_unification (tparms, targs, 871252284Sobrien parm, arg); 871352284Sobrien 871452284Sobrien /* If there is more than one satisfactory baseclass, then: 871552284Sobrien 871652284Sobrien [temp.deduct.call] 871752284Sobrien 871852284Sobrien If they yield more than one possible deduced A, the type 871952284Sobrien deduction fails. 872052284Sobrien 872152284Sobrien applies. */ 872252284Sobrien if (r && rval && !same_type_p (r, rval)) 872352284Sobrien return error_mark_node; 872452284Sobrien else if (r) 872552284Sobrien rval = r; 872652284Sobrien } 872752284Sobrien 872852284Sobrien binfos = BINFO_BASETYPES (arg_binfo); 872952284Sobrien n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; 873052284Sobrien 873152284Sobrien /* Process base types. */ 873252284Sobrien for (i = 0; i < n_baselinks; i++) 873352284Sobrien { 873452284Sobrien tree base_binfo = TREE_VEC_ELT (binfos, i); 873552284Sobrien int this_virtual; 873652284Sobrien 873752284Sobrien /* Skip this base, if we've already seen it. */ 873852284Sobrien if (BINFO_MARKED (base_binfo)) 873952284Sobrien continue; 874052284Sobrien 874152284Sobrien this_virtual = 874252284Sobrien (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); 874352284Sobrien 874452284Sobrien /* When searching for a non-virtual, we cannot mark virtually 874552284Sobrien found binfos. */ 874652284Sobrien if (! this_virtual) 874752284Sobrien SET_BINFO_MARKED (base_binfo); 874852284Sobrien 874952284Sobrien rval = get_template_base_recursive (tparms, targs, 875052284Sobrien parm, 875152284Sobrien base_binfo, 875252284Sobrien rval, 875352284Sobrien GTB_VIA_VIRTUAL * this_virtual); 875452284Sobrien 875552284Sobrien /* If we discovered more than one matching base class, we can 875652284Sobrien stop now. */ 875752284Sobrien if (rval == error_mark_node) 875852284Sobrien return error_mark_node; 875952284Sobrien } 876052284Sobrien 876152284Sobrien return rval; 876252284Sobrien} 876352284Sobrien 876452284Sobrien/* Given a template type PARM and a class type ARG, find the unique 876552284Sobrien base type in ARG that is an instance of PARM. We do not examine 876652284Sobrien ARG itself; only its base-classes. If there is no appropriate base 876752284Sobrien class, return NULL_TREE. If there is more than one, return 876852284Sobrien error_mark_node. PARM may be the type of a partial specialization, 876952284Sobrien as well as a plain template type. Used by unify. */ 877052284Sobrien 877152284Sobrienstatic tree 877252284Sobrienget_template_base (tparms, targs, parm, arg) 877352284Sobrien tree tparms; 877452284Sobrien tree targs; 877552284Sobrien tree parm; 877652284Sobrien tree arg; 877752284Sobrien{ 877852284Sobrien tree rval; 877952284Sobrien tree arg_binfo; 878052284Sobrien 878152284Sobrien my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); 878252284Sobrien 878352284Sobrien arg_binfo = TYPE_BINFO (complete_type (arg)); 878452284Sobrien rval = get_template_base_recursive (tparms, targs, 878552284Sobrien parm, arg_binfo, 878652284Sobrien NULL_TREE, 878752284Sobrien GTB_IGNORE_TYPE); 878852284Sobrien 878952284Sobrien /* Since get_template_base_recursive marks the bases classes, we 879052284Sobrien must unmark them here. */ 879152284Sobrien dfs_walk (arg_binfo, dfs_unmark, markedp, 0); 879252284Sobrien 879352284Sobrien return rval; 879452284Sobrien} 879552284Sobrien 879650397Sobrien/* Returns the level of DECL, which declares a template parameter. */ 879750397Sobrien 879852284Sobrienstatic int 879950397Sobrientemplate_decl_level (decl) 880050397Sobrien tree decl; 880118334Speter{ 880250397Sobrien switch (TREE_CODE (decl)) 880350397Sobrien { 880450397Sobrien case TYPE_DECL: 880550397Sobrien case TEMPLATE_DECL: 880650397Sobrien return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); 880750397Sobrien 880850397Sobrien case PARM_DECL: 880950397Sobrien return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); 881050397Sobrien 881150397Sobrien default: 881290075Sobrien abort (); 881350397Sobrien return 0; 881450397Sobrien } 881550397Sobrien} 881650397Sobrien 881750397Sobrien/* Decide whether ARG can be unified with PARM, considering only the 881850397Sobrien cv-qualifiers of each type, given STRICT as documented for unify. 8819117395Skan Returns nonzero iff the unification is OK on that basis.*/ 882050397Sobrien 882152284Sobrienstatic int 882250397Sobriencheck_cv_quals_for_unify (strict, arg, parm) 882350397Sobrien int strict; 882450397Sobrien tree arg; 882550397Sobrien tree parm; 882650397Sobrien{ 882796263Sobrien int arg_quals = cp_type_quals (arg); 882896263Sobrien int parm_quals = cp_type_quals (parm); 882996263Sobrien 883096263Sobrien if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM) 883196263Sobrien { 883296263Sobrien /* If the cvr quals of parm will not unify with ARG, they'll be 883396263Sobrien ignored in instantiation, so we have to do the same here. */ 8834110611Skan if (TREE_CODE (arg) == REFERENCE_TYPE) 883596263Sobrien parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); 883696263Sobrien if (!POINTER_TYPE_P (arg) && 883796263Sobrien TREE_CODE (arg) != TEMPLATE_TYPE_PARM) 883896263Sobrien parm_quals &= ~TYPE_QUAL_RESTRICT; 883996263Sobrien } 884096263Sobrien 884190075Sobrien if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) 884296263Sobrien && (arg_quals & parm_quals) != parm_quals) 884390075Sobrien return 0; 884490075Sobrien 884590075Sobrien if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) 884696263Sobrien && (parm_quals & arg_quals) != arg_quals) 884790075Sobrien return 0; 884890075Sobrien 884990075Sobrien return 1; 885050397Sobrien} 885150397Sobrien 885250397Sobrien/* Takes parameters as for type_unification. Returns 0 if the 885390075Sobrien type deduction succeeds, 1 otherwise. The parameter STRICT is a 885450397Sobrien bitwise or of the following flags: 885550397Sobrien 885650397Sobrien UNIFY_ALLOW_NONE: 885750397Sobrien Require an exact match between PARM and ARG. 885850397Sobrien UNIFY_ALLOW_MORE_CV_QUAL: 885990075Sobrien Allow the deduced ARG to be more cv-qualified (by qualification 886090075Sobrien conversion) than ARG. 886150397Sobrien UNIFY_ALLOW_LESS_CV_QUAL: 886250397Sobrien Allow the deduced ARG to be less cv-qualified than ARG. 886350397Sobrien UNIFY_ALLOW_DERIVED: 886450397Sobrien Allow the deduced ARG to be a template base class of ARG, 886550397Sobrien or a pointer to a template base class of the type pointed to by 886652284Sobrien ARG. 886752284Sobrien UNIFY_ALLOW_INTEGER: 886852284Sobrien Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX 886990075Sobrien case for more information. 887090075Sobrien UNIFY_ALLOW_OUTER_LEVEL: 887190075Sobrien This is the outermost level of a deduction. Used to determine validity 887290075Sobrien of qualification conversions. A valid qualification conversion must 887390075Sobrien have const qualified pointers leading up to the inner type which 887490075Sobrien requires additional CV quals, except at the outer level, where const 887590075Sobrien is not required [conv.qual]. It would be normal to set this flag in 887690075Sobrien addition to setting UNIFY_ALLOW_MORE_CV_QUAL. 887790075Sobrien UNIFY_ALLOW_OUTER_MORE_CV_QUAL: 887890075Sobrien This is the outermost level of a deduction, and PARM can be more CV 887990075Sobrien qualified at this point. 888090075Sobrien UNIFY_ALLOW_OUTER_LESS_CV_QUAL: 888190075Sobrien This is the outermost level of a deduction, and PARM can be less CV 888290075Sobrien qualified at this point. 888390075Sobrien UNIFY_ALLOW_MAX_CORRECTION: 888490075Sobrien This is an INTEGER_TYPE's maximum value. Used if the range may 888590075Sobrien have been derived from a size specification, such as an array size. 888690075Sobrien If the size was given by a nontype template parameter N, the maximum 888790075Sobrien value will have the form N-1. The flag says that we can (and indeed 888890075Sobrien must) unify N with (ARG + 1), an exception to the normal rules on 888990075Sobrien folding PARM. */ 889050397Sobrien 889152284Sobrienstatic int 889252284Sobrienunify (tparms, targs, parm, arg, strict) 889350397Sobrien tree tparms, targs, parm, arg; 889450397Sobrien int strict; 889550397Sobrien{ 889618334Speter int idx; 889750397Sobrien tree targ; 889850397Sobrien tree tparm; 889990075Sobrien int strict_in = strict; 890018334Speter 890118334Speter /* I don't think this will do the right thing with respect to types. 890218334Speter But the only case I've seen it in so far has been array bounds, where 890318334Speter signedness is the only information lost, and I think that will be 890418334Speter okay. */ 890518334Speter while (TREE_CODE (parm) == NOP_EXPR) 890618334Speter parm = TREE_OPERAND (parm, 0); 890718334Speter 890818334Speter if (arg == error_mark_node) 890918334Speter return 1; 891018334Speter if (arg == unknown_type_node) 891152284Sobrien /* We can't deduce anything from this, but we might get all the 891252284Sobrien template args from other function args. */ 891352284Sobrien return 0; 891452284Sobrien 891550397Sobrien /* If PARM uses template parameters, then we can't bail out here, 891652284Sobrien even if ARG == PARM, since we won't record unifications for the 891750397Sobrien template parameters. We might need them if we're trying to 891850397Sobrien figure out which of two things is more specialized. */ 891950397Sobrien if (arg == parm && !uses_template_parms (parm)) 892018334Speter return 0; 892118334Speter 892250397Sobrien /* Immediately reject some pairs that won't unify because of 892350397Sobrien cv-qualification mismatches. */ 892450397Sobrien if (TREE_CODE (arg) == TREE_CODE (parm) 892590075Sobrien && TYPE_P (arg) 892690075Sobrien /* It is the elements of the array which hold the cv quals of an array 892790075Sobrien type, and the elements might be template type parms. We'll check 892890075Sobrien when we recurse. */ 892990075Sobrien && TREE_CODE (arg) != ARRAY_TYPE 893050397Sobrien /* We check the cv-qualifiers when unifying with template type 893150397Sobrien parameters below. We want to allow ARG `const T' to unify with 893250397Sobrien PARM `T' for example, when computing which of two templates 893350397Sobrien is more specialized, for example. */ 893450397Sobrien && TREE_CODE (arg) != TEMPLATE_TYPE_PARM 893590075Sobrien && !check_cv_quals_for_unify (strict_in, arg, parm)) 893650397Sobrien return 1; 893750397Sobrien 893890075Sobrien if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) 893990075Sobrien && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) 894090075Sobrien strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; 894190075Sobrien strict &= ~UNIFY_ALLOW_OUTER_LEVEL; 894290075Sobrien strict &= ~UNIFY_ALLOW_DERIVED; 894390075Sobrien strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; 894490075Sobrien strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; 894590075Sobrien strict &= ~UNIFY_ALLOW_MAX_CORRECTION; 894690075Sobrien 894718334Speter switch (TREE_CODE (parm)) 894818334Speter { 894950397Sobrien case TYPENAME_TYPE: 895090075Sobrien case SCOPE_REF: 895190075Sobrien case UNBOUND_CLASS_TEMPLATE: 895250397Sobrien /* In a type which contains a nested-name-specifier, template 895350397Sobrien argument values cannot be deduced for template parameters used 895450397Sobrien within the nested-name-specifier. */ 895550397Sobrien return 0; 895650397Sobrien 895718334Speter case TEMPLATE_TYPE_PARM: 895850397Sobrien case TEMPLATE_TEMPLATE_PARM: 895990075Sobrien case BOUND_TEMPLATE_TEMPLATE_PARM: 896050397Sobrien tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); 896150397Sobrien 896250397Sobrien if (TEMPLATE_TYPE_LEVEL (parm) 896350397Sobrien != template_decl_level (tparm)) 896450397Sobrien /* The PARM is not one we're trying to unify. Just check 896550397Sobrien to see if it matches ARG. */ 896650397Sobrien return (TREE_CODE (arg) == TREE_CODE (parm) 896752284Sobrien && same_type_p (parm, arg)) ? 0 : 1; 896818334Speter idx = TEMPLATE_TYPE_IDX (parm); 896950397Sobrien targ = TREE_VEC_ELT (targs, idx); 897050397Sobrien tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); 897150397Sobrien 897218334Speter /* Check for mixed types and values. */ 897350397Sobrien if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM 897450397Sobrien && TREE_CODE (tparm) != TYPE_DECL) 897550397Sobrien || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 897650397Sobrien && TREE_CODE (tparm) != TEMPLATE_DECL)) 897718334Speter return 1; 897850397Sobrien 897990075Sobrien if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 898018334Speter { 898190075Sobrien /* ARG must be constructed from a template class or a template 898290075Sobrien template parameter. */ 898390075Sobrien if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM 898490075Sobrien && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))) 898590075Sobrien return 1; 898650397Sobrien 898790075Sobrien { 898890075Sobrien tree parmtmpl = TYPE_TI_TEMPLATE (parm); 898990075Sobrien tree parmvec = TYPE_TI_ARGS (parm); 899090075Sobrien tree argvec = TYPE_TI_ARGS (arg); 899190075Sobrien tree argtmplvec 899290075Sobrien = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); 899390075Sobrien int i; 899450397Sobrien 899590075Sobrien /* The parameter and argument roles have to be switched here 899690075Sobrien in order to handle default arguments properly. For example, 899790075Sobrien template<template <class> class TT> void f(TT<int>) 899890075Sobrien should be able to accept vector<int> which comes from 899990075Sobrien template <class T, class Allocator = allocator> 900090075Sobrien class vector. */ 900150397Sobrien 900290075Sobrien if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) 900390075Sobrien == error_mark_node) 900490075Sobrien return 1; 900550397Sobrien 900690075Sobrien /* Deduce arguments T, i from TT<T> or TT<i>. 900790075Sobrien We check each element of PARMVEC and ARGVEC individually 900890075Sobrien rather than the whole TREE_VEC since they can have 900990075Sobrien different number of elements. */ 901052284Sobrien 901190075Sobrien for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) 901290075Sobrien { 901390075Sobrien tree t = TREE_VEC_ELT (parmvec, i); 901450397Sobrien 901590075Sobrien if (unify (tparms, targs, t, 901690075Sobrien TREE_VEC_ELT (argvec, i), 901790075Sobrien UNIFY_ALLOW_NONE)) 901890075Sobrien return 1; 901918334Speter } 902090075Sobrien } 902190075Sobrien arg = TYPE_TI_TEMPLATE (arg); 902290075Sobrien 902390075Sobrien /* Fall through to deduce template name. */ 902418334Speter } 902590075Sobrien 902690075Sobrien if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 902790075Sobrien || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) 902890075Sobrien { 902990075Sobrien /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ 903090075Sobrien 903190075Sobrien /* Simple cases: Value already set, does match or doesn't. */ 903290075Sobrien if (targ != NULL_TREE && template_args_equal (targ, arg)) 903390075Sobrien return 0; 903490075Sobrien else if (targ) 903590075Sobrien return 1; 903690075Sobrien } 903750397Sobrien else 903850397Sobrien { 9039117395Skan /* If ARG is an offset type, we're trying to unify '*T' with 9040117395Skan 'U C::*', which is ill-formed. See the comment in the 9041117395Skan POINTER_TYPE case about this ugliness. */ 9042117395Skan if (TREE_CODE (arg) == OFFSET_TYPE) 9043117395Skan return 1; 9044117395Skan 904550397Sobrien /* If PARM is `const T' and ARG is only `int', we don't have 904650397Sobrien a match unless we are allowing additional qualification. 904750397Sobrien If ARG is `const int' and PARM is just `T' that's OK; 904850397Sobrien that binds `const int' to `T'. */ 904990075Sobrien if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, 905050397Sobrien arg, parm)) 905150397Sobrien return 1; 905218334Speter 905350397Sobrien /* Consider the case where ARG is `const volatile int' and 905450397Sobrien PARM is `const T'. Then, T should be `volatile int'. */ 905596263Sobrien arg = cp_build_qualified_type_real 905696263Sobrien (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); 905790075Sobrien if (arg == error_mark_node) 905890075Sobrien return 1; 905990075Sobrien 906090075Sobrien /* Simple cases: Value already set, does match or doesn't. */ 906190075Sobrien if (targ != NULL_TREE && same_type_p (targ, arg)) 906290075Sobrien return 0; 906390075Sobrien else if (targ) 906490075Sobrien return 1; 9065110611Skan 9066110611Skan /* Make sure that ARG is not a variable-sized array. (Note 9067110611Skan that were talking about variable-sized arrays (like 9068110611Skan `int[n]'), rather than arrays of unknown size (like 9069110611Skan `int[]').) We'll get very confused by such a type since 9070110611Skan the bound of the array will not be computable in an 9071110611Skan instantiation. Besides, such types are not allowed in 9072110611Skan ISO C++, so we can do as we please here. */ 9073110611Skan if (variably_modified_type_p (arg)) 9074110611Skan return 1; 907550397Sobrien } 907650397Sobrien 907750397Sobrien TREE_VEC_ELT (targs, idx) = arg; 907818334Speter return 0; 907918334Speter 908050397Sobrien case TEMPLATE_PARM_INDEX: 908150397Sobrien tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); 908250397Sobrien 908350397Sobrien if (TEMPLATE_PARM_LEVEL (parm) 908450397Sobrien != template_decl_level (tparm)) 908550397Sobrien /* The PARM is not one we're trying to unify. Just check 908650397Sobrien to see if it matches ARG. */ 908750397Sobrien return (TREE_CODE (arg) == TREE_CODE (parm) 908850397Sobrien && cp_tree_equal (parm, arg) > 0) ? 0 : 1; 908950397Sobrien 909050397Sobrien idx = TEMPLATE_PARM_IDX (parm); 909150397Sobrien targ = TREE_VEC_ELT (targs, idx); 909250397Sobrien 909350397Sobrien if (targ) 909450397Sobrien { 909550397Sobrien int i = (cp_tree_equal (targ, arg) > 0); 909650397Sobrien if (i == 1) 909750397Sobrien return 0; 909850397Sobrien else if (i == 0) 909950397Sobrien return 1; 910050397Sobrien else 910190075Sobrien abort (); 910250397Sobrien } 910350397Sobrien 910452284Sobrien /* [temp.deduct.type] If, in the declaration of a function template 910552284Sobrien with a non-type template-parameter, the non-type 910652284Sobrien template-parameter is used in an expression in the function 910752284Sobrien parameter-list and, if the corresponding template-argument is 910852284Sobrien deduced, the template-argument type shall match the type of the 910952284Sobrien template-parameter exactly, except that a template-argument 911090075Sobrien deduced from an array bound may be of any integral type. 911190075Sobrien The non-type parameter might use already deduced type parameters. */ 911290075Sobrien tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); 9113117395Skan if (!TREE_TYPE (arg)) 9114117395Skan /* Template-parameter dependent expression. Just accept it for now. 9115117395Skan It will later be processed in convert_template_argument. */ 9116117395Skan ; 9117117395Skan else if (same_type_p (TREE_TYPE (arg), tparm)) 9118117395Skan /* OK */; 911952284Sobrien else if ((strict & UNIFY_ALLOW_INTEGER) 912090075Sobrien && (TREE_CODE (tparm) == INTEGER_TYPE 912190075Sobrien || TREE_CODE (tparm) == BOOLEAN_TYPE)) 912252284Sobrien /* OK */; 912390075Sobrien else if (uses_template_parms (tparm)) 912490075Sobrien /* We haven't deduced the type of this parameter yet. Try again 912590075Sobrien later. */ 912690075Sobrien return 0; 912752284Sobrien else 912852284Sobrien return 1; 912952284Sobrien 913090075Sobrien TREE_VEC_ELT (targs, idx) = arg; 913150397Sobrien return 0; 913250397Sobrien 913318334Speter case POINTER_TYPE: 913450397Sobrien { 913550397Sobrien if (TREE_CODE (arg) != POINTER_TYPE) 913650397Sobrien return 1; 913750397Sobrien 913850397Sobrien /* [temp.deduct.call] 913950397Sobrien 914050397Sobrien A can be another pointer or pointer to member type that can 914150397Sobrien be converted to the deduced A via a qualification 914250397Sobrien conversion (_conv.qual_). 914350397Sobrien 914450397Sobrien We pass down STRICT here rather than UNIFY_ALLOW_NONE. 914550397Sobrien This will allow for additional cv-qualification of the 914690075Sobrien pointed-to types if appropriate. */ 914750397Sobrien 914890075Sobrien if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) 914950397Sobrien /* The derived-to-base conversion only persists through one 915050397Sobrien level of pointers. */ 915190075Sobrien strict |= (strict_in & UNIFY_ALLOW_DERIVED); 915250397Sobrien 915390075Sobrien if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE 915490075Sobrien && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) 915590075Sobrien { 915690075Sobrien /* Avoid getting confused about cv-quals; don't recurse here. 915790075Sobrien Pointers to members should really be just OFFSET_TYPE, not 9158117395Skan this two-level nonsense... */ 915990075Sobrien 916090075Sobrien parm = TREE_TYPE (parm); 916190075Sobrien arg = TREE_TYPE (arg); 916290075Sobrien goto offset; 916390075Sobrien } 916490075Sobrien 916590075Sobrien return unify (tparms, targs, TREE_TYPE (parm), 916690075Sobrien TREE_TYPE (arg), strict); 916750397Sobrien } 916850397Sobrien 916918334Speter case REFERENCE_TYPE: 917050397Sobrien if (TREE_CODE (arg) != REFERENCE_TYPE) 917150397Sobrien return 1; 917250397Sobrien return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 917390075Sobrien strict & UNIFY_ALLOW_MORE_CV_QUAL); 917418334Speter 917518334Speter case ARRAY_TYPE: 917618334Speter if (TREE_CODE (arg) != ARRAY_TYPE) 917718334Speter return 1; 917850397Sobrien if ((TYPE_DOMAIN (parm) == NULL_TREE) 917950397Sobrien != (TYPE_DOMAIN (arg) == NULL_TREE)) 918018334Speter return 1; 918150397Sobrien if (TYPE_DOMAIN (parm) != NULL_TREE 918250397Sobrien && unify (tparms, targs, TYPE_DOMAIN (parm), 918352284Sobrien TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) 918450397Sobrien return 1; 918550397Sobrien return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 9186117395Skan strict & UNIFY_ALLOW_MORE_CV_QUAL); 918718334Speter 918818334Speter case REAL_TYPE: 918950397Sobrien case COMPLEX_TYPE: 919090075Sobrien case VECTOR_TYPE: 919118334Speter case INTEGER_TYPE: 919250397Sobrien case BOOLEAN_TYPE: 919350397Sobrien case VOID_TYPE: 919418334Speter if (TREE_CODE (arg) != TREE_CODE (parm)) 919518334Speter return 1; 919618334Speter 919752284Sobrien if (TREE_CODE (parm) == INTEGER_TYPE 919852284Sobrien && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) 919918334Speter { 920018334Speter if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) 920150397Sobrien && unify (tparms, targs, TYPE_MIN_VALUE (parm), 920252284Sobrien TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) 920318334Speter return 1; 920418334Speter if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) 920550397Sobrien && unify (tparms, targs, TYPE_MAX_VALUE (parm), 920690075Sobrien TYPE_MAX_VALUE (arg), 920790075Sobrien UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION)) 920818334Speter return 1; 920918334Speter } 921090075Sobrien /* We have already checked cv-qualification at the top of the 921152284Sobrien function. */ 921290075Sobrien else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) 921350397Sobrien return 1; 921450397Sobrien 921518334Speter /* As far as unification is concerned, this wins. Later checks 921618334Speter will invalidate it if necessary. */ 921718334Speter return 0; 921818334Speter 921918334Speter /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ 922050397Sobrien /* Type INTEGER_CST can come from ordinary constant template args. */ 922118334Speter case INTEGER_CST: 922250397Sobrien while (TREE_CODE (arg) == NOP_EXPR) 922350397Sobrien arg = TREE_OPERAND (arg, 0); 922450397Sobrien 922518334Speter if (TREE_CODE (arg) != INTEGER_CST) 922618334Speter return 1; 922718334Speter return !tree_int_cst_equal (parm, arg); 922818334Speter 922918334Speter case TREE_VEC: 923018334Speter { 923118334Speter int i; 923218334Speter if (TREE_CODE (arg) != TREE_VEC) 923318334Speter return 1; 923418334Speter if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) 923518334Speter return 1; 923690075Sobrien for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) 923750397Sobrien if (unify (tparms, targs, 923818334Speter TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), 923952284Sobrien UNIFY_ALLOW_NONE)) 924018334Speter return 1; 924118334Speter return 0; 924218334Speter } 924318334Speter 924418334Speter case RECORD_TYPE: 924552284Sobrien case UNION_TYPE: 924652284Sobrien if (TREE_CODE (arg) != TREE_CODE (parm)) 924718334Speter return 1; 924850397Sobrien 924990075Sobrien if (TYPE_PTRMEMFUNC_P (parm)) 925090075Sobrien { 925190075Sobrien if (!TYPE_PTRMEMFUNC_P (arg)) 925290075Sobrien return 1; 925390075Sobrien 925490075Sobrien return unify (tparms, targs, 925590075Sobrien TYPE_PTRMEMFUNC_FN_TYPE (parm), 925690075Sobrien TYPE_PTRMEMFUNC_FN_TYPE (arg), 925790075Sobrien strict); 925890075Sobrien } 925990075Sobrien 926052284Sobrien if (CLASSTYPE_TEMPLATE_INFO (parm)) 926150397Sobrien { 926250397Sobrien tree t = NULL_TREE; 926352284Sobrien 926490075Sobrien if (strict_in & UNIFY_ALLOW_DERIVED) 926552284Sobrien { 926652284Sobrien /* First, we try to unify the PARM and ARG directly. */ 926752284Sobrien t = try_class_unification (tparms, targs, 926852284Sobrien parm, arg); 926950397Sobrien 927052284Sobrien if (!t) 927152284Sobrien { 927252284Sobrien /* Fallback to the special case allowed in 927352284Sobrien [temp.deduct.call]: 927452284Sobrien 927552284Sobrien If P is a class, and P has the form 927652284Sobrien template-id, then A can be a derived class of 927752284Sobrien the deduced A. Likewise, if P is a pointer to 927852284Sobrien a class of the form template-id, A can be a 927952284Sobrien pointer to a derived class pointed to by the 928052284Sobrien deduced A. */ 928152284Sobrien t = get_template_base (tparms, targs, 928252284Sobrien parm, arg); 928352284Sobrien 928452284Sobrien if (! t || t == error_mark_node) 928552284Sobrien return 1; 928652284Sobrien } 928752284Sobrien } 928852284Sobrien else if (CLASSTYPE_TEMPLATE_INFO (arg) 928952284Sobrien && (CLASSTYPE_TI_TEMPLATE (parm) 929052284Sobrien == CLASSTYPE_TI_TEMPLATE (arg))) 929152284Sobrien /* Perhaps PARM is something like S<U> and ARG is S<int>. 929252284Sobrien Then, we should unify `int' and `U'. */ 929350397Sobrien t = arg; 929452284Sobrien else 929590075Sobrien /* There's no chance of unification succeeding. */ 929650397Sobrien return 1; 929750397Sobrien 929850397Sobrien return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), 929952284Sobrien CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); 930050397Sobrien } 930190075Sobrien else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) 930250397Sobrien return 1; 930318334Speter return 0; 930418334Speter 930518334Speter case METHOD_TYPE: 930650397Sobrien case FUNCTION_TYPE: 930750397Sobrien if (TREE_CODE (arg) != TREE_CODE (parm)) 930818334Speter return 1; 930918334Speter 931050397Sobrien if (unify (tparms, targs, TREE_TYPE (parm), 931152284Sobrien TREE_TYPE (arg), UNIFY_ALLOW_NONE)) 931218334Speter return 1; 931350397Sobrien return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), 931450397Sobrien TYPE_ARG_TYPES (arg), 1, 931590075Sobrien DEDUCE_EXACT, 0, -1); 931618334Speter 931718334Speter case OFFSET_TYPE: 931890075Sobrien offset: 931918334Speter if (TREE_CODE (arg) != OFFSET_TYPE) 932018334Speter return 1; 932150397Sobrien if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), 932252284Sobrien TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) 932318334Speter return 1; 932450397Sobrien return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), 932552284Sobrien strict); 932618334Speter 932750397Sobrien case CONST_DECL: 932850397Sobrien if (arg != decl_constant_value (parm)) 932950397Sobrien return 1; 933050397Sobrien return 0; 933150397Sobrien 933250397Sobrien case TEMPLATE_DECL: 933350397Sobrien /* Matched cases are handled by the ARG == PARM test above. */ 933450397Sobrien return 1; 933550397Sobrien 933650397Sobrien case MINUS_EXPR: 933790075Sobrien if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node) 933890075Sobrien && (strict_in & UNIFY_ALLOW_MAX_CORRECTION)) 933950397Sobrien { 934050397Sobrien /* We handle this case specially, since it comes up with 934150397Sobrien arrays. In particular, something like: 934250397Sobrien 934350397Sobrien template <int N> void f(int (&x)[N]); 934450397Sobrien 934550397Sobrien Here, we are trying to unify the range type, which 934650397Sobrien looks like [0 ... (N - 1)]. */ 934750397Sobrien tree t, t1, t2; 934850397Sobrien t1 = TREE_OPERAND (parm, 0); 934950397Sobrien t2 = TREE_OPERAND (parm, 1); 935050397Sobrien 935190075Sobrien t = fold (build (PLUS_EXPR, integer_type_node, arg, t2)); 935250397Sobrien 935352284Sobrien return unify (tparms, targs, t1, t, strict); 935450397Sobrien } 935550397Sobrien /* else fall through */ 935650397Sobrien 935718334Speter default: 935850397Sobrien if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) 935990075Sobrien { 936090075Sobrien 936190075Sobrien /* We're looking at an expression. This can happen with 936290075Sobrien something like: 936352284Sobrien 936490075Sobrien template <int I> 936590075Sobrien void foo(S<I>, S<I + 2>); 936650397Sobrien 936790075Sobrien This is a "nondeduced context": 936850397Sobrien 936990075Sobrien [deduct.type] 937052284Sobrien 937190075Sobrien The nondeduced contexts are: 937250397Sobrien 937390075Sobrien --A type that is a template-id in which one or more of 937490075Sobrien the template-arguments is an expression that references 937590075Sobrien a template-parameter. 937650397Sobrien 937790075Sobrien In these cases, we assume deduction succeeded, but don't 937890075Sobrien actually infer any unifications. */ 937990075Sobrien 938090075Sobrien if (!uses_template_parms (parm) 938190075Sobrien && !template_args_equal (parm, arg)) 938290075Sobrien return 1; 938390075Sobrien else 938490075Sobrien return 0; 938590075Sobrien } 938650397Sobrien else 938750397Sobrien sorry ("use of `%s' in template type unification", 938850397Sobrien tree_code_name [(int) TREE_CODE (parm)]); 938950397Sobrien 939018334Speter return 1; 939118334Speter } 939218334Speter} 939318334Speter 939452284Sobrien/* Called if RESULT is explicitly instantiated, or is a member of an 939552284Sobrien explicitly instantiated class, or if using -frepo and the 939652284Sobrien instantiation of RESULT has been assigned to this file. */ 939752284Sobrien 939850397Sobrienvoid 939950397Sobrienmark_decl_instantiated (result, extern_p) 940050397Sobrien tree result; 940150397Sobrien int extern_p; 940250397Sobrien{ 940390075Sobrien /* We used to set this unconditionally; we moved that to 940490075Sobrien do_decl_instantiation so it wouldn't get set on members of 940590075Sobrien explicit class template instantiations. But we still need to set 940690075Sobrien it here for the 'extern template' case in order to suppress 940790075Sobrien implicit instantiations. */ 940890075Sobrien if (extern_p) 940990075Sobrien SET_DECL_EXPLICIT_INSTANTIATION (result); 941090075Sobrien 9411117395Skan /* If this entity has already been written out, it's too late to 9412117395Skan make any modifications. */ 9413117395Skan if (TREE_ASM_WRITTEN (result)) 9414117395Skan return; 9415117395Skan 9416117395Skan if (TREE_CODE (result) != FUNCTION_DECL) 9417117395Skan /* The TREE_PUBLIC flag for function declarations will have been 9418117395Skan set correctly by tsubst. */ 9419117395Skan TREE_PUBLIC (result) = 1; 9420117395Skan 9421117395Skan /* This might have been set by an earlier implicit instantiation. */ 9422117395Skan DECL_COMDAT (result) = 0; 9423117395Skan 942450397Sobrien if (! extern_p) 942550397Sobrien { 942650397Sobrien DECL_INTERFACE_KNOWN (result) = 1; 942750397Sobrien DECL_NOT_REALLY_EXTERN (result) = 1; 942850397Sobrien 942952284Sobrien /* Always make artificials weak. */ 943052284Sobrien if (DECL_ARTIFICIAL (result) && flag_weak) 943152284Sobrien comdat_linkage (result); 943250397Sobrien /* For WIN32 we also want to put explicit instantiations in 943350397Sobrien linkonce sections. */ 943452284Sobrien else if (TREE_PUBLIC (result)) 943550397Sobrien maybe_make_one_only (result); 943650397Sobrien } 9437117395Skan 9438117395Skan if (TREE_CODE (result) == FUNCTION_DECL) 943990075Sobrien defer_fn (result); 944050397Sobrien} 944150397Sobrien 944290075Sobrien/* Given two function templates PAT1 and PAT2, return: 944350397Sobrien 944490075Sobrien DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER. 944590075Sobrien 944650397Sobrien 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. 944750397Sobrien -1 if PAT2 is more specialized than PAT1. 944890075Sobrien 0 if neither is more specialized. 944990075Sobrien 945090075Sobrien LEN is passed through to fn_type_unification. */ 945150397Sobrien 945218334Speterint 945390075Sobrienmore_specialized (pat1, pat2, deduce, len) 945490075Sobrien tree pat1, pat2; 945590075Sobrien int deduce; 945690075Sobrien int len; 945718334Speter{ 945850397Sobrien tree targs; 945950397Sobrien int winner = 0; 946018334Speter 946190075Sobrien targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2), 946290075Sobrien NULL_TREE, 0, deduce, len); 946350397Sobrien if (targs) 946452284Sobrien --winner; 946518334Speter 946690075Sobrien targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1), 946790075Sobrien NULL_TREE, 0, deduce, len); 946850397Sobrien if (targs) 946952284Sobrien ++winner; 947018334Speter 947150397Sobrien return winner; 947250397Sobrien} 947350397Sobrien 947450397Sobrien/* Given two class template specialization list nodes PAT1 and PAT2, return: 947550397Sobrien 947650397Sobrien 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. 947750397Sobrien -1 if PAT2 is more specialized than PAT1. 9478119256Skan 0 if neither is more specialized. 9479119256Skan 9480119256Skan FULL_ARGS is the full set of template arguments that triggers this 9481119256Skan partial ordering. */ 948250397Sobrien 948350397Sobrienint 9484119256Skanmore_specialized_class (pat1, pat2, full_args) 9485119256Skan tree pat1, pat2, full_args; 948650397Sobrien{ 948750397Sobrien tree targs; 948850397Sobrien int winner = 0; 948950397Sobrien 949052284Sobrien targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), 9491119256Skan add_outermost_template_args (full_args, TREE_PURPOSE (pat2))); 949250397Sobrien if (targs) 949350397Sobrien --winner; 949450397Sobrien 949552284Sobrien targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), 9496119256Skan add_outermost_template_args (full_args, TREE_PURPOSE (pat1))); 949750397Sobrien if (targs) 949850397Sobrien ++winner; 949950397Sobrien 950050397Sobrien return winner; 950150397Sobrien} 950250397Sobrien 950350397Sobrien/* Return the template arguments that will produce the function signature 950450397Sobrien DECL from the function template FN, with the explicit template 950550397Sobrien arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must 950652284Sobrien also match. Return NULL_TREE if no satisfactory arguments could be 950790075Sobrien found. DEDUCE and LEN are passed through to fn_type_unification. */ 950890075Sobrien 950950397Sobrienstatic tree 951090075Sobrienget_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len) 951150397Sobrien tree fn, decl, explicit_args; 951290075Sobrien int check_rettype, deduce, len; 951350397Sobrien{ 951450397Sobrien int ntparms = DECL_NTPARMS (fn); 951590075Sobrien tree targs = make_tree_vec (ntparms); 951652284Sobrien tree decl_type; 951752284Sobrien tree decl_arg_types; 951850397Sobrien int i; 951950397Sobrien 952052284Sobrien /* Substitute the explicit template arguments into the type of DECL. 952152284Sobrien The call to fn_type_unification will handle substitution into the 952252284Sobrien FN. */ 952352284Sobrien decl_type = TREE_TYPE (decl); 952452284Sobrien if (explicit_args && uses_template_parms (decl_type)) 952518334Speter { 952652284Sobrien tree tmpl; 952752284Sobrien tree converted_args; 952852284Sobrien 952952284Sobrien if (DECL_TEMPLATE_INFO (decl)) 953052284Sobrien tmpl = DECL_TI_TEMPLATE (decl); 953150397Sobrien else 9532117395Skan /* We can get here for some invalid specializations. */ 953352284Sobrien return NULL_TREE; 953452284Sobrien 953552284Sobrien converted_args 953652284Sobrien = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), 953752284Sobrien explicit_args, NULL_TREE, 953896263Sobrien tf_none, /*require_all_arguments=*/0)); 953952284Sobrien if (converted_args == error_mark_node) 954052284Sobrien return NULL_TREE; 954152284Sobrien 954296263Sobrien decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); 954352284Sobrien if (decl_type == error_mark_node) 954452284Sobrien return NULL_TREE; 954550397Sobrien } 954618334Speter 954752284Sobrien decl_arg_types = TYPE_ARG_TYPES (decl_type); 954890075Sobrien /* Never do unification on the 'this' parameter. */ 954990075Sobrien if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 955052284Sobrien decl_arg_types = TREE_CHAIN (decl_arg_types); 955152284Sobrien 955250397Sobrien i = fn_type_unification (fn, explicit_args, targs, 955350397Sobrien decl_arg_types, 955490075Sobrien (check_rettype || DECL_CONV_FN_P (fn) 955590075Sobrien ? TREE_TYPE (decl_type) : NULL_TREE), 955690075Sobrien deduce, len); 955718334Speter 955850397Sobrien if (i != 0) 955950397Sobrien return NULL_TREE; 956018334Speter 956150397Sobrien return targs; 956250397Sobrien} 956350397Sobrien 956450397Sobrien/* For most uses, we want to check the return type. */ 956550397Sobrien 956650397Sobrientree 956750397Sobrienget_bindings (fn, decl, explicit_args) 956850397Sobrien tree fn, decl, explicit_args; 956950397Sobrien{ 957090075Sobrien return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1); 957150397Sobrien} 957250397Sobrien 957390075Sobrien/* But for resolve_overloaded_unification, we only care about the parameter 957490075Sobrien types. */ 957550397Sobrien 957650397Sobrienstatic tree 957750397Sobrienget_bindings_overload (fn, decl, explicit_args) 957850397Sobrien tree fn, decl, explicit_args; 957950397Sobrien{ 958090075Sobrien return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1); 958150397Sobrien} 958250397Sobrien 958352284Sobrien/* Return the innermost template arguments that, when applied to a 958452284Sobrien template specialization whose innermost template parameters are 958590075Sobrien TPARMS, and whose specialization arguments are PARMS, yield the 958652284Sobrien ARGS. 958752284Sobrien 958852284Sobrien For example, suppose we have: 958952284Sobrien 959052284Sobrien template <class T, class U> struct S {}; 959152284Sobrien template <class T> struct S<T*, int> {}; 959252284Sobrien 959352284Sobrien Then, suppose we want to get `S<double*, int>'. The TPARMS will be 959452284Sobrien {T}, the PARMS will be {T*, int} and the ARGS will be {double*, 959552284Sobrien int}. The resulting vector will be {double}, indicating that `T' 959652284Sobrien is bound to `double'. */ 959752284Sobrien 959850397Sobrienstatic tree 959952284Sobrienget_class_bindings (tparms, parms, args) 960052284Sobrien tree tparms, parms, args; 960150397Sobrien{ 960250397Sobrien int i, ntparms = TREE_VEC_LENGTH (tparms); 960390075Sobrien tree vec = make_tree_vec (ntparms); 960450397Sobrien 960590075Sobrien if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args), 960690075Sobrien UNIFY_ALLOW_NONE)) 960752284Sobrien return NULL_TREE; 960818334Speter 960950397Sobrien for (i = 0; i < ntparms; ++i) 961050397Sobrien if (! TREE_VEC_ELT (vec, i)) 961150397Sobrien return NULL_TREE; 961218334Speter 961390075Sobrien if (verify_class_unification (vec, parms, args)) 961490075Sobrien return NULL_TREE; 961590075Sobrien 961650397Sobrien return vec; 961750397Sobrien} 961818334Speter 961952284Sobrien/* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. 962052284Sobrien Pick the most specialized template, and return the corresponding 962152284Sobrien instantiation, or if there is no corresponding instantiation, the 962290075Sobrien template itself. If there is no most specialized template, 962390075Sobrien error_mark_node is returned. If there are no templates at all, 962490075Sobrien NULL_TREE is returned. */ 962518334Speter 962650397Sobrientree 962790075Sobrienmost_specialized_instantiation (instantiations) 962852284Sobrien tree instantiations; 962950397Sobrien{ 963052284Sobrien tree fn, champ; 963150397Sobrien int fate; 963250397Sobrien 963352284Sobrien if (!instantiations) 963450397Sobrien return NULL_TREE; 963518334Speter 963652284Sobrien champ = instantiations; 963752284Sobrien for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) 963818334Speter { 963990075Sobrien fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), 964090075Sobrien DEDUCE_EXACT, -1); 964150397Sobrien if (fate == 1) 964250397Sobrien ; 964350397Sobrien else 964450397Sobrien { 964550397Sobrien if (fate == 0) 964650397Sobrien { 964750397Sobrien fn = TREE_CHAIN (fn); 964850397Sobrien if (! fn) 964950397Sobrien return error_mark_node; 965050397Sobrien } 965152284Sobrien champ = fn; 965250397Sobrien } 965318334Speter } 965418334Speter 965552284Sobrien for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) 965618334Speter { 965790075Sobrien fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), 965890075Sobrien DEDUCE_EXACT, -1); 965950397Sobrien if (fate != 1) 966050397Sobrien return error_mark_node; 966118334Speter } 966218334Speter 966352284Sobrien return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); 966418334Speter} 966518334Speter 966652284Sobrien/* Return the most specialized of the list of templates in FNS that can 966752284Sobrien produce an instantiation matching DECL, given the explicit template 966852284Sobrien arguments EXPLICIT_ARGS. */ 966950397Sobrien 967052284Sobrienstatic tree 967152284Sobrienmost_specialized (fns, decl, explicit_args) 967252284Sobrien tree fns, decl, explicit_args; 967318334Speter{ 967452284Sobrien tree candidates = NULL_TREE; 967552284Sobrien tree fn, args; 967652284Sobrien 967752284Sobrien for (fn = fns; fn; fn = TREE_CHAIN (fn)) 967852284Sobrien { 967952284Sobrien tree candidate = TREE_VALUE (fn); 968052284Sobrien 968152284Sobrien args = get_bindings (candidate, decl, explicit_args); 968252284Sobrien if (args) 968390075Sobrien candidates = tree_cons (NULL_TREE, candidate, candidates); 968452284Sobrien } 968552284Sobrien 968690075Sobrien return most_specialized_instantiation (candidates); 968752284Sobrien} 968852284Sobrien 968952284Sobrien/* If DECL is a specialization of some template, return the most 969090075Sobrien general such template. Otherwise, returns NULL_TREE. 969152284Sobrien 969290075Sobrien For example, given: 969390075Sobrien 969452284Sobrien template <class T> struct S { template <class U> void f(U); }; 969552284Sobrien 969652284Sobrien if TMPL is `template <class U> void S<int>::f(U)' this will return 969752284Sobrien the full template. This function will not trace past partial 969852284Sobrien specializations, however. For example, given in addition: 969952284Sobrien 970052284Sobrien template <class T> struct S<T*> { template <class U> void f(U); }; 970152284Sobrien 970252284Sobrien if TMPL is `template <class U> void S<int*>::f(U)' this will return 970352284Sobrien `template <class T> template <class U> S<T*>::f(U)'. */ 970452284Sobrien 970590075Sobrientree 970652284Sobrienmost_general_template (decl) 970752284Sobrien tree decl; 970852284Sobrien{ 970990075Sobrien /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is 971090075Sobrien an immediate specialization. */ 971190075Sobrien if (TREE_CODE (decl) == FUNCTION_DECL) 971290075Sobrien { 971390075Sobrien if (DECL_TEMPLATE_INFO (decl)) { 971490075Sobrien decl = DECL_TI_TEMPLATE (decl); 971590075Sobrien 971690075Sobrien /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a 971790075Sobrien template friend. */ 971890075Sobrien if (TREE_CODE (decl) != TEMPLATE_DECL) 971990075Sobrien return NULL_TREE; 972090075Sobrien } else 972190075Sobrien return NULL_TREE; 972290075Sobrien } 972390075Sobrien 972490075Sobrien /* Look for more and more general templates. */ 972552284Sobrien while (DECL_TEMPLATE_INFO (decl)) 972690075Sobrien { 972790075Sobrien /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE 972890075Sobrien in some cases. (See cp-tree.h for details.) */ 972990075Sobrien if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) 973090075Sobrien break; 973152284Sobrien 9732117395Skan if (CLASS_TYPE_P (TREE_TYPE (decl)) 9733117395Skan && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) 9734117395Skan break; 9735117395Skan 973690075Sobrien /* Stop if we run into an explicitly specialized class template. */ 973790075Sobrien if (!DECL_NAMESPACE_SCOPE_P (decl) 973890075Sobrien && DECL_CONTEXT (decl) 973990075Sobrien && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) 974090075Sobrien break; 974190075Sobrien 974290075Sobrien decl = DECL_TI_TEMPLATE (decl); 974390075Sobrien } 974490075Sobrien 974552284Sobrien return decl; 974652284Sobrien} 974752284Sobrien 974852284Sobrien/* Return the most specialized of the class template specializations 974952284Sobrien of TMPL which can produce an instantiation matching ARGS, or 975052284Sobrien error_mark_node if the choice is ambiguous. */ 975152284Sobrien 975252284Sobrienstatic tree 975352284Sobrienmost_specialized_class (tmpl, args) 975452284Sobrien tree tmpl; 975552284Sobrien tree args; 975652284Sobrien{ 975752284Sobrien tree list = NULL_TREE; 975852284Sobrien tree t; 975952284Sobrien tree champ; 976050397Sobrien int fate; 976118334Speter 976252284Sobrien tmpl = most_general_template (tmpl); 976352284Sobrien for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) 976418334Speter { 976552284Sobrien tree spec_args 976652284Sobrien = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); 976752284Sobrien if (spec_args) 976850397Sobrien { 976990075Sobrien list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); 977050397Sobrien TREE_TYPE (list) = TREE_TYPE (t); 977150397Sobrien } 977218334Speter } 977350397Sobrien 977450397Sobrien if (! list) 977550397Sobrien return NULL_TREE; 977650397Sobrien 977750397Sobrien t = list; 977850397Sobrien champ = t; 977950397Sobrien t = TREE_CHAIN (t); 978050397Sobrien for (; t; t = TREE_CHAIN (t)) 978150397Sobrien { 9782119256Skan fate = more_specialized_class (champ, t, args); 978350397Sobrien if (fate == 1) 978450397Sobrien ; 978550397Sobrien else 978650397Sobrien { 978750397Sobrien if (fate == 0) 978850397Sobrien { 978950397Sobrien t = TREE_CHAIN (t); 979050397Sobrien if (! t) 979150397Sobrien return error_mark_node; 979250397Sobrien } 979350397Sobrien champ = t; 979450397Sobrien } 979550397Sobrien } 979650397Sobrien 979750397Sobrien for (t = list; t && t != champ; t = TREE_CHAIN (t)) 979850397Sobrien { 9799119256Skan fate = more_specialized_class (champ, t, args); 980050397Sobrien if (fate != 1) 980150397Sobrien return error_mark_node; 980250397Sobrien } 980350397Sobrien 980450397Sobrien return champ; 980518334Speter} 980618334Speter 9807117395Skan/* Explicitly instantiate DECL. */ 980850397Sobrien 980918334Spetervoid 9810117395Skando_decl_instantiation (tree decl, tree storage) 981118334Speter{ 981218334Speter tree result = NULL_TREE; 981318334Speter int extern_p = 0; 981418334Speter 981590075Sobrien if (!decl) 981690075Sobrien /* An error occurred, for which grokdeclarator has already issued 981790075Sobrien an appropriate message. */ 981890075Sobrien return; 981990075Sobrien else if (! DECL_LANG_SPECIFIC (decl)) 982050397Sobrien { 982190075Sobrien error ("explicit instantiation of non-template `%#D'", decl); 982250397Sobrien return; 982350397Sobrien } 982452284Sobrien else if (TREE_CODE (decl) == VAR_DECL) 982518334Speter { 982652284Sobrien /* There is an asymmetry here in the way VAR_DECLs and 982752284Sobrien FUNCTION_DECLs are handled by grokdeclarator. In the case of 982852284Sobrien the latter, the DECL we get back will be marked as a 982952284Sobrien template instantiation, and the appropriate 983052284Sobrien DECL_TEMPLATE_INFO will be set up. This does not happen for 983152284Sobrien VAR_DECLs so we do the lookup here. Probably, grokdeclarator 983252284Sobrien should handle VAR_DECLs as it currently handles 983352284Sobrien FUNCTION_DECLs. */ 983450397Sobrien result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); 983550397Sobrien if (result && TREE_CODE (result) != VAR_DECL) 983652284Sobrien { 983790075Sobrien error ("no matching template for `%D' found", result); 983852284Sobrien return; 983952284Sobrien } 984018334Speter } 984150397Sobrien else if (TREE_CODE (decl) != FUNCTION_DECL) 984250397Sobrien { 984390075Sobrien error ("explicit instantiation of `%#D'", decl); 984450397Sobrien return; 984550397Sobrien } 984652284Sobrien else 984750397Sobrien result = decl; 984850397Sobrien 984952284Sobrien /* Check for various error cases. Note that if the explicit 9850117395Skan instantiation is valid the RESULT will currently be marked as an 985152284Sobrien *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set 985252284Sobrien until we get here. */ 985352284Sobrien 985452284Sobrien if (DECL_TEMPLATE_SPECIALIZATION (result)) 985518334Speter { 9856102780Skan /* DR 259 [temp.spec]. 985752284Sobrien 9858102780Skan Both an explicit instantiation and a declaration of an explicit 9859102780Skan specialization shall not appear in a program unless the explicit 9860102780Skan instantiation follows a declaration of the explicit specialization. 9861102780Skan 9862102780Skan For a given set of template parameters, if an explicit 9863102780Skan instantiation of a template appears after a declaration of an 9864102780Skan explicit specialization for that template, the explicit 9865102780Skan instantiation has no effect. */ 986618334Speter return; 986718334Speter } 986852284Sobrien else if (DECL_EXPLICIT_INSTANTIATION (result)) 986952284Sobrien { 987052284Sobrien /* [temp.spec] 987118334Speter 987252284Sobrien No program shall explicitly instantiate any template more 987352284Sobrien than once. 987452284Sobrien 987590075Sobrien We check DECL_INTERFACE_KNOWN so as not to complain when the first 987690075Sobrien instantiation was `extern' and the second is not, and EXTERN_P for 987790075Sobrien the opposite case. If -frepo, chances are we already got marked 987890075Sobrien as an explicit instantiation because of the repo file. */ 987952750Sobrien if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository) 988090075Sobrien pedwarn ("duplicate explicit instantiation of `%#D'", result); 988152284Sobrien 988252284Sobrien /* If we've already instantiated the template, just return now. */ 988352284Sobrien if (DECL_INTERFACE_KNOWN (result)) 988452284Sobrien return; 988552284Sobrien } 988652284Sobrien else if (!DECL_IMPLICIT_INSTANTIATION (result)) 988750397Sobrien { 988890075Sobrien error ("no matching template for `%D' found", result); 988952284Sobrien return; 989052284Sobrien } 989152284Sobrien else if (!DECL_TEMPLATE_INFO (result)) 989252284Sobrien { 989390075Sobrien pedwarn ("explicit instantiation of non-template `%#D'", result); 989450397Sobrien return; 989550397Sobrien } 989650397Sobrien 989718334Speter if (flag_external_templates) 989818334Speter return; 989918334Speter 990018334Speter if (storage == NULL_TREE) 990118334Speter ; 990218334Speter else if (storage == ridpointers[(int) RID_EXTERN]) 990352284Sobrien { 990496263Sobrien if (pedantic && !in_system_header) 990590075Sobrien pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations"); 990652284Sobrien extern_p = 1; 990752284Sobrien } 990818334Speter else 990990075Sobrien error ("storage class `%D' applied to template instantiation", 991018334Speter storage); 991150397Sobrien 991252284Sobrien SET_DECL_EXPLICIT_INSTANTIATION (result); 991350397Sobrien mark_decl_instantiated (result, extern_p); 991418334Speter repo_template_instantiated (result, extern_p); 991550397Sobrien if (! extern_p) 991690075Sobrien instantiate_decl (result, /*defer_ok=*/1); 991718334Speter} 991818334Speter 991918334Spetervoid 992018334Spetermark_class_instantiated (t, extern_p) 992118334Speter tree t; 992218334Speter int extern_p; 992318334Speter{ 992418334Speter SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); 992518334Speter SET_CLASSTYPE_INTERFACE_KNOWN (t); 992618334Speter CLASSTYPE_INTERFACE_ONLY (t) = extern_p; 992718334Speter TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; 992818334Speter if (! extern_p) 992918334Speter { 993018334Speter CLASSTYPE_DEBUG_REQUESTED (t) = 1; 993118334Speter rest_of_type_compilation (t, 1); 993218334Speter } 993318334Speter} 993418334Speter 9935117395Skan/* Called from do_type_instantiation through binding_table_foreach to 9936117395Skan do recursive instantiation for the type bound in ENTRY. */ 9937117395Skanstatic void 9938117395Skanbt_instantiate_type_proc (binding_entry entry, void *data) 9939117395Skan{ 9940117395Skan tree storage = *(tree *) data; 9941117395Skan 9942117395Skan if (IS_AGGR_TYPE (entry->type) 9943117395Skan && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) 9944117395Skan do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); 9945117395Skan} 9946117395Skan 994790075Sobrien/* Perform an explicit instantiation of template class T. STORAGE, if 994890075Sobrien non-null, is the RID for extern, inline or static. COMPLAIN is 9949117395Skan nonzero if this is called from the parser, zero if called recursively, 995090075Sobrien since the standard is unclear (as detailed below). */ 995190075Sobrien 995218334Spetervoid 995390075Sobriendo_type_instantiation (t, storage, complain) 995450397Sobrien tree t, storage; 995596263Sobrien tsubst_flags_t complain; 995618334Speter{ 995718334Speter int extern_p = 0; 995818334Speter int nomem_p = 0; 995950397Sobrien int static_p = 0; 996018334Speter 996150397Sobrien if (TREE_CODE (t) == TYPE_DECL) 996250397Sobrien t = TREE_TYPE (t); 996350397Sobrien 996452284Sobrien if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) 996550397Sobrien { 996690075Sobrien error ("explicit instantiation of non-template type `%T'", t); 996750397Sobrien return; 996850397Sobrien } 996950397Sobrien 997050397Sobrien complete_type (t); 997150397Sobrien 997218334Speter /* With -fexternal-templates, explicit instantiations are treated the same 997318334Speter as implicit ones. */ 997418334Speter if (flag_external_templates) 997518334Speter return; 997618334Speter 997790075Sobrien if (!COMPLETE_TYPE_P (t)) 997818334Speter { 997996263Sobrien if (complain & tf_error) 998090075Sobrien error ("explicit instantiation of `%#T' before definition of template", 998190075Sobrien t); 998218334Speter return; 998318334Speter } 998418334Speter 998552284Sobrien if (storage != NULL_TREE) 998618334Speter { 998796263Sobrien if (pedantic && !in_system_header) 998890075Sobrien pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations", 998952284Sobrien IDENTIFIER_POINTER (storage)); 999052284Sobrien 999152284Sobrien if (storage == ridpointers[(int) RID_INLINE]) 999252284Sobrien nomem_p = 1; 999352284Sobrien else if (storage == ridpointers[(int) RID_EXTERN]) 999452284Sobrien extern_p = 1; 999552284Sobrien else if (storage == ridpointers[(int) RID_STATIC]) 999652284Sobrien static_p = 1; 999752284Sobrien else 999852284Sobrien { 999990075Sobrien error ("storage class `%D' applied to template instantiation", 1000052284Sobrien storage); 1000152284Sobrien extern_p = 0; 1000252284Sobrien } 1000318334Speter } 1000418334Speter 1000552284Sobrien if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) 1000652284Sobrien { 10007102780Skan /* DR 259 [temp.spec]. 1000818334Speter 10009102780Skan Both an explicit instantiation and a declaration of an explicit 10010102780Skan specialization shall not appear in a program unless the explicit 10011102780Skan instantiation follows a declaration of the explicit specialization. 10012102780Skan 10013102780Skan For a given set of template parameters, if an explicit 10014102780Skan instantiation of a template appears after a declaration of an 10015102780Skan explicit specialization for that template, the explicit 10016102780Skan instantiation has no effect. */ 1001752284Sobrien return; 1001852284Sobrien } 1001952284Sobrien else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) 1002018334Speter { 1002152284Sobrien /* [temp.spec] 1002252284Sobrien 1002352284Sobrien No program shall explicitly instantiate any template more 1002452284Sobrien than once. 1002552284Sobrien 1002690075Sobrien If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation 1002790075Sobrien was `extern'. If EXTERN_P then the second is. If -frepo, chances 1002890075Sobrien are we already got marked as an explicit instantiation because of the 1002990075Sobrien repo file. All these cases are OK. */ 1003090075Sobrien if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository 1003196263Sobrien && (complain & tf_error)) 1003290075Sobrien pedwarn ("duplicate explicit instantiation of `%#T'", t); 1003352284Sobrien 1003452284Sobrien /* If we've already instantiated the template, just return now. */ 1003552284Sobrien if (!CLASSTYPE_INTERFACE_ONLY (t)) 1003652284Sobrien return; 1003718334Speter } 1003818334Speter 1003952284Sobrien mark_class_instantiated (t, extern_p); 1004052284Sobrien repo_template_instantiated (t, extern_p); 1004152284Sobrien 1004218334Speter if (nomem_p) 1004318334Speter return; 1004418334Speter 1004518334Speter { 1004618334Speter tree tmp; 1004718334Speter 1004852284Sobrien /* In contrast to implicit instantiation, where only the 1004952284Sobrien declarations, and not the definitions, of members are 1005052284Sobrien instantiated, we have here: 1005152284Sobrien 1005252284Sobrien [temp.explicit] 1005352284Sobrien 1005452284Sobrien The explicit instantiation of a class template specialization 1005552284Sobrien implies the instantiation of all of its members not 1005652284Sobrien previously explicitly specialized in the translation unit 1005752284Sobrien containing the explicit instantiation. 1005852284Sobrien 1005952284Sobrien Of course, we can't instantiate member template classes, since 1006052284Sobrien we don't have any arguments for them. Note that the standard 1006190075Sobrien is unclear on whether the instantiation of the members are 1006252284Sobrien *explicit* instantiations or not. We choose to be generous, 1006352284Sobrien and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow 1006452284Sobrien the explicit instantiation of a class where some of the members 1006552284Sobrien have no definition in the current translation unit. */ 1006652284Sobrien 1006750397Sobrien if (! static_p) 1006850397Sobrien for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) 1006950397Sobrien if (TREE_CODE (tmp) == FUNCTION_DECL 1007050397Sobrien && DECL_TEMPLATE_INSTANTIATION (tmp)) 1007150397Sobrien { 1007250397Sobrien mark_decl_instantiated (tmp, extern_p); 1007350397Sobrien repo_template_instantiated (tmp, extern_p); 1007450397Sobrien if (! extern_p) 1007590075Sobrien instantiate_decl (tmp, /*defer_ok=*/1); 1007650397Sobrien } 1007750397Sobrien 1007850397Sobrien for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) 1007950397Sobrien if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) 1008018334Speter { 1008150397Sobrien mark_decl_instantiated (tmp, extern_p); 1008218334Speter repo_template_instantiated (tmp, extern_p); 1008350397Sobrien if (! extern_p) 1008490075Sobrien instantiate_decl (tmp, /*defer_ok=*/1); 1008518334Speter } 1008618334Speter 10087117395Skan if (CLASSTYPE_NESTED_UDTS (t)) 10088117395Skan binding_table_foreach (CLASSTYPE_NESTED_UDTS (t), 10089117395Skan bt_instantiate_type_proc, &storage); 1009018334Speter } 1009118334Speter} 1009218334Speter 1009352284Sobrien/* Given a function DECL, which is a specialization of TMPL, modify 1009452284Sobrien DECL to be a re-instantiation of TMPL with the same template 1009552284Sobrien arguments. TMPL should be the template into which tsubst'ing 1009652284Sobrien should occur for DECL, not the most general template. 1009750397Sobrien 1009850397Sobrien One reason for doing this is a scenario like this: 1009950397Sobrien 1010050397Sobrien template <class T> 1010150397Sobrien void f(const T&, int i); 1010250397Sobrien 1010350397Sobrien void g() { f(3, 7); } 1010450397Sobrien 1010550397Sobrien template <class T> 1010650397Sobrien void f(const T& t, const int i) { } 1010750397Sobrien 1010850397Sobrien Note that when the template is first instantiated, with 1010950397Sobrien instantiate_template, the resulting DECL will have no name for the 1011050397Sobrien first parameter, and the wrong type for the second. So, when we go 1011150397Sobrien to instantiate the DECL, we regenerate it. */ 1011250397Sobrien 1011352284Sobrienstatic void 1011450397Sobrienregenerate_decl_from_template (decl, tmpl) 1011550397Sobrien tree decl; 1011650397Sobrien tree tmpl; 1011750397Sobrien{ 1011890075Sobrien /* The most general version of TMPL. */ 1011990075Sobrien tree gen_tmpl; 1012090075Sobrien /* The arguments used to instantiate DECL, from the most general 1012190075Sobrien template. */ 1012250397Sobrien tree args; 1012350397Sobrien tree code_pattern; 1012450397Sobrien tree new_decl; 1012552284Sobrien int unregistered; 1012650397Sobrien 1012750397Sobrien args = DECL_TI_ARGS (decl); 1012850397Sobrien code_pattern = DECL_TEMPLATE_RESULT (tmpl); 1012950397Sobrien 1013052284Sobrien /* Unregister the specialization so that when we tsubst we will not 1013152284Sobrien just return DECL. We don't have to unregister DECL from TMPL 1013252284Sobrien because if would only be registered there if it were a partial 1013352284Sobrien instantiation of a specialization, which it isn't: it's a full 1013452284Sobrien instantiation. */ 1013552284Sobrien gen_tmpl = most_general_template (tmpl); 10136117395Skan push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl)); 10137119256Skan unregistered = reregister_specialization (decl, gen_tmpl, NULL_TREE); 1013850397Sobrien 1013952284Sobrien /* If the DECL was not unregistered then something peculiar is 1014052284Sobrien happening: we created a specialization but did not call 1014152284Sobrien register_specialization for it. */ 1014252284Sobrien my_friendly_assert (unregistered, 0); 1014352284Sobrien 1014452284Sobrien 1014552284Sobrien /* Do the substitution to get the new declaration. */ 1014696263Sobrien new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE); 1014752284Sobrien 1014852284Sobrien if (TREE_CODE (decl) == VAR_DECL) 1014950397Sobrien { 1015050397Sobrien /* Set up DECL_INITIAL, since tsubst doesn't. */ 10151117395Skan if (!DECL_INITIALIZED_IN_CLASS_P (decl)) 10152117395Skan DECL_INITIAL (new_decl) = 10153117395Skan tsubst_expr (DECL_INITIAL (code_pattern), args, 10154117395Skan tf_error, DECL_TI_TEMPLATE (decl)); 1015550397Sobrien } 1015652284Sobrien else if (TREE_CODE (decl) == FUNCTION_DECL) 1015750397Sobrien { 1015850397Sobrien /* Convince duplicate_decls to use the DECL_ARGUMENTS from the 1015950397Sobrien new decl. */ 1016050397Sobrien DECL_INITIAL (new_decl) = error_mark_node; 1016152284Sobrien /* And don't complain about a duplicate definition. */ 1016252284Sobrien DECL_INITIAL (decl) = NULL_TREE; 1016350397Sobrien } 1016450397Sobrien 10165117395Skan pop_access_scope (decl); 10166117395Skan 1016752284Sobrien /* The immediate parent of the new template is still whatever it was 1016852284Sobrien before, even though tsubst sets DECL_TI_TEMPLATE up as the most 1016952284Sobrien general template. We also reset the DECL_ASSEMBLER_NAME since 1017052284Sobrien tsubst always calculates the name as if the function in question 1017152284Sobrien were really a template instance, and sometimes, with friend 1017252284Sobrien functions, this is not so. See tsubst_friend_function for 1017352284Sobrien details. */ 1017452284Sobrien DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); 1017590075Sobrien COPY_DECL_ASSEMBLER_NAME (decl, new_decl); 1017690075Sobrien COPY_DECL_RTL (decl, new_decl); 1017790075Sobrien DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl); 1017852284Sobrien 1017952284Sobrien /* Call duplicate decls to merge the old and new declarations. */ 1018050397Sobrien duplicate_decls (new_decl, decl); 1018150397Sobrien 1018252284Sobrien /* Now, re-register the specialization. */ 1018352284Sobrien register_specialization (decl, gen_tmpl, args); 1018450397Sobrien} 1018550397Sobrien 1018690075Sobrien/* Produce the definition of D, a _DECL generated from a template. If 10187117395Skan DEFER_OK is nonzero, then we don't have to actually do the 1018890075Sobrien instantiation now; we just have to do it sometime. */ 1018950397Sobrien 1019018334Spetertree 1019190075Sobrieninstantiate_decl (d, defer_ok) 1019250397Sobrien tree d; 1019390075Sobrien int defer_ok; 1019418334Speter{ 1019552284Sobrien tree tmpl = DECL_TI_TEMPLATE (d); 10196117395Skan tree gen_args; 10197117395Skan tree args; 1019850397Sobrien tree td; 1019952284Sobrien tree code_pattern; 1020052284Sobrien tree spec; 1020152284Sobrien tree gen_tmpl; 1020250397Sobrien int pattern_defined; 1020350397Sobrien int line = lineno; 1020490075Sobrien int need_push; 1020590075Sobrien const char *file = input_filename; 1020618334Speter 1020752284Sobrien /* This function should only be used to instantiate templates for 1020852284Sobrien functions and static member variables. */ 1020952284Sobrien my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL 1021052284Sobrien || TREE_CODE (d) == VAR_DECL, 0); 1021118334Speter 10212117395Skan if (TREE_CODE (d) == VAR_DECL) 10213117395Skan defer_ok = 0; 10214117395Skan 1021590075Sobrien /* Don't instantiate cloned functions. Instead, instantiate the 1021690075Sobrien functions they cloned. */ 1021790075Sobrien if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) 1021890075Sobrien d = DECL_CLONED_FUNCTION (d); 1021990075Sobrien 1022052284Sobrien if (DECL_TEMPLATE_INSTANTIATED (d)) 1022152284Sobrien /* D has already been instantiated. It might seem reasonable to 1022290075Sobrien check whether or not D is an explicit instantiation, and, if so, 1022352284Sobrien stop here. But when an explicit instantiation is deferred 1022452284Sobrien until the end of the compilation, DECL_EXPLICIT_INSTANTIATION 1022552284Sobrien is set, even though we still need to do the instantiation. */ 1022652284Sobrien return d; 1022750397Sobrien 1022852284Sobrien /* If we already have a specialization of this declaration, then 1022952284Sobrien there's no reason to instantiate it. Note that 1023052284Sobrien retrieve_specialization gives us both instantiations and 1023152284Sobrien specializations, so we must explicitly check 1023252284Sobrien DECL_TEMPLATE_SPECIALIZATION. */ 1023352284Sobrien gen_tmpl = most_general_template (tmpl); 10234117395Skan gen_args = DECL_TI_ARGS (d); 10235117395Skan spec = retrieve_specialization (gen_tmpl, gen_args); 1023652284Sobrien if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) 1023752284Sobrien return spec; 1023850397Sobrien 1023952284Sobrien /* This needs to happen before any tsubsting. */ 1024052284Sobrien if (! push_tinst_level (d)) 1024150397Sobrien return d; 1024250397Sobrien 1024390075Sobrien timevar_push (TV_PARSE); 1024490075Sobrien 1024552284Sobrien /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern 1024652284Sobrien for the instantiation. This is not always the most general 1024752284Sobrien template. Consider, for example: 1024852284Sobrien 1024952284Sobrien template <class T> 1025052284Sobrien struct S { template <class U> void f(); 1025152284Sobrien template <> void f<int>(); }; 1025252284Sobrien 1025352284Sobrien and an instantiation of S<double>::f<int>. We want TD to be the 1025452284Sobrien specialization S<T>::f<int>, not the more general S<T>::f<U>. */ 1025552284Sobrien td = tmpl; 1025690075Sobrien while (/* An instantiation cannot have a definition, so we need a 1025790075Sobrien more general template. */ 1025890075Sobrien DECL_TEMPLATE_INSTANTIATION (td) 1025990075Sobrien /* We must also deal with friend templates. Given: 1026052284Sobrien 1026190075Sobrien template <class T> struct S { 1026290075Sobrien template <class U> friend void f() {}; 1026390075Sobrien }; 1026452284Sobrien 1026590075Sobrien S<int>::f<U> say, is not an instantiation of S<T>::f<U>, 1026690075Sobrien so far as the language is concerned, but that's still 1026790075Sobrien where we get the pattern for the instantiation from. On 1026890075Sobrien other hand, if the definition comes outside the class, say: 1026952284Sobrien 1027090075Sobrien template <class T> struct S { 1027190075Sobrien template <class U> friend void f(); 1027290075Sobrien }; 1027390075Sobrien template <class U> friend void f() {} 1027490075Sobrien 1027590075Sobrien we don't need to look any further. That's what the check for 1027690075Sobrien DECL_INITIAL is for. */ 1027790075Sobrien || (TREE_CODE (d) == FUNCTION_DECL 1027890075Sobrien && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) 1027990075Sobrien && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)))) 1028050397Sobrien { 1028152284Sobrien /* The present template, TD, should not be a definition. If it 1028252284Sobrien were a definition, we should be using it! Note that we 1028352284Sobrien cannot restructure the loop to just keep going until we find 1028452284Sobrien a template with a definition, since that might go too far if 1028552284Sobrien a specialization was declared, but not defined. */ 1028652284Sobrien my_friendly_assert (!(TREE_CODE (d) == VAR_DECL 1028752284Sobrien && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), 1028852284Sobrien 0); 1028950397Sobrien 1029052284Sobrien /* Fetch the more general template. */ 1029152284Sobrien td = DECL_TI_TEMPLATE (td); 1029250397Sobrien } 1029350397Sobrien 1029452284Sobrien code_pattern = DECL_TEMPLATE_RESULT (td); 1029550397Sobrien 10296117395Skan if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) 10297117395Skan || DECL_TEMPLATE_SPECIALIZATION (td)) 10298117395Skan /* In the case of a friend template whose definition is provided 10299117395Skan outside the class, we may have too many arguments. Drop the 10300117395Skan ones we don't need. The same is true for specializations. */ 10301117395Skan args = get_innermost_template_args 10302117395Skan (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); 10303117395Skan else 10304117395Skan args = gen_args; 10305117395Skan 1030652284Sobrien if (TREE_CODE (d) == FUNCTION_DECL) 1030790075Sobrien pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); 1030852284Sobrien else 1030952284Sobrien pattern_defined = ! DECL_IN_AGGR_P (code_pattern); 1031052284Sobrien 1031150397Sobrien lineno = DECL_SOURCE_LINE (d); 1031250397Sobrien input_filename = DECL_SOURCE_FILE (d); 1031350397Sobrien 1031450397Sobrien if (pattern_defined) 1031550397Sobrien { 1031690075Sobrien /* Let the repository code that this template definition is 1031790075Sobrien available. 1031850397Sobrien 1031990075Sobrien The repository doesn't need to know about cloned functions 1032090075Sobrien because they never actually show up in the object file. It 1032190075Sobrien does need to know about the clones; those are the symbols 1032290075Sobrien that the linker will be emitting error messages about. */ 1032390075Sobrien if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d) 1032490075Sobrien || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d)) 1032590075Sobrien { 1032690075Sobrien tree t; 1032790075Sobrien 1032890075Sobrien for (t = TREE_CHAIN (d); 1032990075Sobrien t && DECL_CLONED_FUNCTION_P (t); 1033090075Sobrien t = TREE_CHAIN (t)) 1033190075Sobrien repo_template_used (t); 1033290075Sobrien } 1033390075Sobrien else 1033490075Sobrien repo_template_used (d); 1033590075Sobrien 1033650397Sobrien if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) 1033750397Sobrien { 1033850397Sobrien if (flag_alt_external_templates) 1033950397Sobrien { 1034050397Sobrien if (interface_unknown) 1034150397Sobrien warn_if_unknown_interface (d); 1034250397Sobrien } 1034350397Sobrien else if (DECL_INTERFACE_KNOWN (code_pattern)) 1034450397Sobrien { 1034550397Sobrien DECL_INTERFACE_KNOWN (d) = 1; 1034650397Sobrien DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); 1034750397Sobrien } 1034850397Sobrien else 1034950397Sobrien warn_if_unknown_interface (code_pattern); 1035050397Sobrien } 1035150397Sobrien 1035250397Sobrien if (at_eof) 1035350397Sobrien import_export_decl (d); 1035450397Sobrien } 1035550397Sobrien 1035696263Sobrien if (!defer_ok) 1035796263Sobrien { 1035896263Sobrien /* Recheck the substitutions to obtain any warning messages 1035996263Sobrien about ignoring cv qualifiers. */ 1036096263Sobrien tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); 1036196263Sobrien tree type = TREE_TYPE (gen); 1036296263Sobrien 10363117395Skan /* Make sure that we can see identifiers, and compute access 10364117395Skan correctly. D is already the target FUNCTION_DECL with the 10365117395Skan right context. */ 10366117395Skan push_access_scope (d); 10367117395Skan 1036896263Sobrien if (TREE_CODE (gen) == FUNCTION_DECL) 1036996263Sobrien { 10370117395Skan tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d); 10371117395Skan tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args, 1037296263Sobrien tf_error | tf_warning, d); 1037396263Sobrien /* Don't simply tsubst the function type, as that will give 1037496263Sobrien duplicate warnings about poor parameter qualifications. 1037596263Sobrien The function arguments are the same as the decl_arguments 10376117395Skan without the top level cv qualifiers. */ 1037796263Sobrien type = TREE_TYPE (type); 1037896263Sobrien } 10379117395Skan tsubst (type, gen_args, tf_error | tf_warning, d); 10380117395Skan 10381117395Skan pop_access_scope (d); 1038296263Sobrien } 1038396263Sobrien 1038490075Sobrien if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d) 1038590075Sobrien && DECL_INITIAL (d) == NULL_TREE) 1038690075Sobrien /* We should have set up DECL_INITIAL in instantiate_class_template. */ 1038790075Sobrien abort (); 1038850397Sobrien /* Reject all external templates except inline functions. */ 1038990075Sobrien else if (DECL_INTERFACE_KNOWN (d) 1039090075Sobrien && ! DECL_NOT_REALLY_EXTERN (d) 1039190075Sobrien && ! (TREE_CODE (d) == FUNCTION_DECL 1039290075Sobrien && DECL_INLINE (d))) 1039350397Sobrien goto out; 1039490075Sobrien /* Defer all other templates, unless we have been explicitly 1039590075Sobrien forbidden from doing so. We restore the source position here 1039690075Sobrien because it's used by add_pending_template. */ 1039790075Sobrien else if (! pattern_defined || defer_ok) 1039850397Sobrien { 1039950397Sobrien lineno = line; 1040050397Sobrien input_filename = file; 1040150397Sobrien 1040252284Sobrien if (at_eof && !pattern_defined 1040352284Sobrien && DECL_EXPLICIT_INSTANTIATION (d)) 1040452284Sobrien /* [temp.explicit] 1040552284Sobrien 1040652284Sobrien The definition of a non-exported function template, a 1040752284Sobrien non-exported member function template, or a non-exported 1040852284Sobrien member function or static data member of a class template 1040952284Sobrien shall be present in every translation unit in which it is 1041052284Sobrien explicitly instantiated. */ 1041190075Sobrien pedwarn 1041290075Sobrien ("explicit instantiation of `%D' but no definition available", d); 1041352284Sobrien 1041450397Sobrien add_pending_template (d); 1041550397Sobrien goto out; 1041650397Sobrien } 1041750397Sobrien 1041890075Sobrien need_push = !global_bindings_p (); 1041990075Sobrien if (need_push) 1042090075Sobrien push_to_top_level (); 1042190075Sobrien 1042252284Sobrien /* Regenerate the declaration in case the template has been modified 1042352284Sobrien by a subsequent redeclaration. */ 1042450397Sobrien regenerate_decl_from_template (d, td); 10425117395Skan 1042650397Sobrien /* We already set the file and line above. Reset them now in case 1042750397Sobrien they changed as a result of calling regenerate_decl_from_template. */ 1042850397Sobrien lineno = DECL_SOURCE_LINE (d); 1042950397Sobrien input_filename = DECL_SOURCE_FILE (d); 1043050397Sobrien 1043150397Sobrien if (TREE_CODE (d) == VAR_DECL) 1043250397Sobrien { 10433117395Skan /* Clear out DECL_RTL; whatever was there before may not be right 10434117395Skan since we've reset the type of the declaration. */ 10435117395Skan SET_DECL_RTL (d, NULL_RTX); 10436117395Skan 1043750397Sobrien DECL_IN_AGGR_P (d) = 0; 10438117395Skan import_export_decl (d); 10439117395Skan DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); 10440117395Skan 10441117395Skan if (DECL_EXTERNAL (d)) 10442117395Skan { 10443117395Skan /* The fact that this code is executing indicates that: 10444117395Skan 10445117395Skan (1) D is a template static data member, for which a 10446117395Skan definition is available. 10447117395Skan 10448117395Skan (2) An implicit or explicit instantiation has occured. 10449117395Skan 10450117395Skan (3) We are not going to emit a definition of the static 10451117395Skan data member at this time. 10452117395Skan 10453117395Skan This situation is peculiar, but it occurs on platforms 10454117395Skan without weak symbols when performing an implicit 10455117395Skan instantiation. There, we cannot implicitly instantiate a 10456117395Skan defined static data member in more than one translation 10457117395Skan unit, so import_export_decl marks the declaration as 10458117395Skan external; we must rely on explicit instantiation. */ 10459117395Skan } 1046050397Sobrien else 1046150397Sobrien { 10462117395Skan /* Mark D as instantiated so that recursive calls to 10463117395Skan instantiate_decl do not try to instantiate it again. */ 10464117395Skan DECL_TEMPLATE_INSTANTIATED (d) = 1; 10465117395Skan cp_finish_decl (d, 10466117395Skan (!DECL_INITIALIZED_IN_CLASS_P (d) 10467117395Skan ? DECL_INITIAL (d) : NULL_TREE), 10468117395Skan NULL_TREE, 0); 1046950397Sobrien } 1047050397Sobrien } 1047150397Sobrien else if (TREE_CODE (d) == FUNCTION_DECL) 1047250397Sobrien { 1047390075Sobrien htab_t saved_local_specializations; 1047450397Sobrien 10475117395Skan /* Mark D as instantiated so that recursive calls to 10476117395Skan instantiate_decl do not try to instantiate it again. */ 10477117395Skan DECL_TEMPLATE_INSTANTIATED (d) = 1; 10478117395Skan 1047990075Sobrien /* Save away the current list, in case we are instantiating one 1048090075Sobrien template from within the body of another. */ 1048190075Sobrien saved_local_specializations = local_specializations; 1048250397Sobrien 1048390075Sobrien /* Set up the list of local specializations. */ 1048490075Sobrien local_specializations = htab_create (37, 1048590075Sobrien htab_hash_pointer, 1048690075Sobrien htab_eq_pointer, 1048790075Sobrien NULL); 1048850397Sobrien 1048990075Sobrien /* Set up context. */ 10490117395Skan import_export_decl (d); 1049190075Sobrien start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED); 1049250397Sobrien 1049390075Sobrien /* Substitute into the body of the function. */ 1049490075Sobrien tsubst_expr (DECL_SAVED_TREE (code_pattern), args, 1049596263Sobrien tf_error | tf_warning, tmpl); 1049650397Sobrien 1049790075Sobrien /* We don't need the local specializations any more. */ 1049890075Sobrien htab_delete (local_specializations); 1049990075Sobrien local_specializations = saved_local_specializations; 1050050397Sobrien 1050190075Sobrien /* Finish the function. */ 10502117395Skan d = finish_function (0); 10503117395Skan expand_body (d); 1050450397Sobrien } 1050550397Sobrien 1050690075Sobrien /* We're not deferring instantiation any more. */ 1050790075Sobrien TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; 1050890075Sobrien 1050990075Sobrien if (need_push) 1051090075Sobrien pop_from_top_level (); 1051190075Sobrien 1051250397Sobrienout: 1051350397Sobrien lineno = line; 1051450397Sobrien input_filename = file; 1051550397Sobrien 1051650397Sobrien pop_tinst_level (); 1051750397Sobrien 1051890075Sobrien timevar_pop (TV_PARSE); 1051990075Sobrien 1052018334Speter return d; 1052118334Speter} 1052250397Sobrien 1052352284Sobrien/* Run through the list of templates that we wish we could 1052452284Sobrien instantiate, and instantiate any we can. */ 1052552284Sobrien 1052652284Sobrienint 1052752284Sobrieninstantiate_pending_templates () 1052850397Sobrien{ 1052952284Sobrien tree *t; 1053090075Sobrien tree last = NULL_TREE; 1053152284Sobrien int instantiated_something = 0; 1053252284Sobrien int reconsider; 1053352284Sobrien 1053452284Sobrien do 1053550397Sobrien { 1053652284Sobrien reconsider = 0; 1053750397Sobrien 1053852284Sobrien t = &pending_templates; 1053952284Sobrien while (*t) 1054050397Sobrien { 1054152284Sobrien tree instantiation = TREE_VALUE (*t); 1054252284Sobrien 1054390075Sobrien reopen_tinst_level (TREE_PURPOSE (*t)); 1054452284Sobrien 1054590075Sobrien if (TYPE_P (instantiation)) 1054652284Sobrien { 1054752284Sobrien tree fn; 1054852284Sobrien 1054990075Sobrien if (!COMPLETE_TYPE_P (instantiation)) 1055052284Sobrien { 1055152284Sobrien instantiate_class_template (instantiation); 1055252284Sobrien if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) 1055352284Sobrien for (fn = TYPE_METHODS (instantiation); 1055452284Sobrien fn; 1055552284Sobrien fn = TREE_CHAIN (fn)) 1055652284Sobrien if (! DECL_ARTIFICIAL (fn)) 1055790075Sobrien instantiate_decl (fn, /*defer_ok=*/0); 1055890075Sobrien if (COMPLETE_TYPE_P (instantiation)) 1055952284Sobrien { 1056052284Sobrien instantiated_something = 1; 1056152284Sobrien reconsider = 1; 1056252284Sobrien } 1056352284Sobrien } 1056452284Sobrien 1056590075Sobrien if (COMPLETE_TYPE_P (instantiation)) 1056652284Sobrien /* If INSTANTIATION has been instantiated, then we don't 1056752284Sobrien need to consider it again in the future. */ 1056852284Sobrien *t = TREE_CHAIN (*t); 1056990075Sobrien else 1057090075Sobrien { 1057190075Sobrien last = *t; 1057290075Sobrien t = &TREE_CHAIN (*t); 1057390075Sobrien } 1057452284Sobrien } 1057552284Sobrien else 1057652284Sobrien { 1057790075Sobrien if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) 1057852284Sobrien && !DECL_TEMPLATE_INSTANTIATED (instantiation)) 1057952284Sobrien { 1058090075Sobrien instantiation = instantiate_decl (instantiation, 1058190075Sobrien /*defer_ok=*/0); 1058252284Sobrien if (DECL_TEMPLATE_INSTANTIATED (instantiation)) 1058352284Sobrien { 1058452284Sobrien instantiated_something = 1; 1058552284Sobrien reconsider = 1; 1058652284Sobrien } 1058752284Sobrien } 1058852284Sobrien 1058990075Sobrien if (DECL_TEMPLATE_SPECIALIZATION (instantiation) 1059052284Sobrien || DECL_TEMPLATE_INSTANTIATED (instantiation)) 1059152284Sobrien /* If INSTANTIATION has been instantiated, then we don't 1059252284Sobrien need to consider it again in the future. */ 1059352284Sobrien *t = TREE_CHAIN (*t); 1059490075Sobrien else 1059590075Sobrien { 1059690075Sobrien last = *t; 1059790075Sobrien t = &TREE_CHAIN (*t); 1059890075Sobrien } 1059952284Sobrien } 1060090075Sobrien tinst_depth = 0; 1060190075Sobrien current_tinst_level = NULL_TREE; 1060250397Sobrien } 1060390075Sobrien last_pending_template = last; 1060452284Sobrien } 1060552284Sobrien while (reconsider); 1060652284Sobrien 1060752284Sobrien return instantiated_something; 1060850397Sobrien} 1060950397Sobrien 1061090075Sobrien/* Substitute ARGVEC into T, which is a list of initializers for 1061190075Sobrien either base class or a non-static data member. The TREE_PURPOSEs 1061290075Sobrien are DECLs, and the TREE_VALUEs are the initializer values. Used by 1061390075Sobrien instantiate_decl. */ 1061452284Sobrien 1061550397Sobrienstatic tree 1061690075Sobrientsubst_initializer_list (t, argvec) 1061750397Sobrien tree t, argvec; 1061850397Sobrien{ 10619117395Skan tree inits = NULL_TREE; 1062050397Sobrien 1062150397Sobrien for (; t; t = TREE_CHAIN (t)) 1062250397Sobrien { 1062390075Sobrien tree decl; 1062490075Sobrien tree init; 1062590075Sobrien tree val; 1062650397Sobrien 1062796263Sobrien decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning, 1062890075Sobrien NULL_TREE); 10629117395Skan decl = expand_member_init (decl); 10630117395Skan if (decl && !DECL_P (decl)) 10631117395Skan in_base_initializer = 1; 10632117395Skan 1063396263Sobrien init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning, 1063490075Sobrien NULL_TREE); 1063590075Sobrien if (!init) 1063690075Sobrien ; 1063790075Sobrien else if (TREE_CODE (init) == TREE_LIST) 1063890075Sobrien for (val = init; val; val = TREE_CHAIN (val)) 1063990075Sobrien TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val)); 10640117395Skan else if (init != void_type_node) 1064190075Sobrien init = convert_from_reference (init); 1064250397Sobrien 10643117395Skan in_base_initializer = 0; 10644117395Skan 10645117395Skan if (decl) 10646117395Skan { 10647117395Skan init = build_tree_list (decl, init); 10648117395Skan TREE_CHAIN (init) = inits; 10649117395Skan inits = init; 10650117395Skan } 1065150397Sobrien } 10652117395Skan return inits; 1065350397Sobrien} 1065450397Sobrien 1065552284Sobrien/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ 1065650397Sobrien 1065752284Sobrienstatic void 1065852284Sobrienset_current_access_from_decl (decl) 1065952284Sobrien tree decl; 1066050397Sobrien{ 1066152284Sobrien if (TREE_PRIVATE (decl)) 1066252284Sobrien current_access_specifier = access_private_node; 1066352284Sobrien else if (TREE_PROTECTED (decl)) 1066452284Sobrien current_access_specifier = access_protected_node; 1066552284Sobrien else 1066652284Sobrien current_access_specifier = access_public_node; 1066752284Sobrien} 1066850397Sobrien 1066952284Sobrien/* Instantiate an enumerated type. TAG is the template type, NEWTAG 1067052284Sobrien is the instantiation (which should have been created with 1067152284Sobrien start_enum) and ARGS are the template arguments to use. */ 1067250397Sobrien 1067352284Sobrienstatic void 1067452284Sobrientsubst_enum (tag, newtag, args) 1067552284Sobrien tree tag; 1067652284Sobrien tree newtag; 1067752284Sobrien tree args; 1067852284Sobrien{ 1067952284Sobrien tree e; 1068052284Sobrien 1068150397Sobrien for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) 1068250397Sobrien { 1068352284Sobrien tree value; 1068490075Sobrien 1068552284Sobrien /* Note that in a template enum, the TREE_VALUE is the 1068652284Sobrien CONST_DECL, not the corresponding INTEGER_CST. */ 1068752284Sobrien value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), 1068896263Sobrien args, tf_error | tf_warning, 1068952284Sobrien NULL_TREE); 1069052284Sobrien 1069152284Sobrien /* Give this enumeration constant the correct access. */ 1069252284Sobrien set_current_access_from_decl (TREE_VALUE (e)); 1069352284Sobrien 1069452284Sobrien /* Actually build the enumerator itself. */ 1069590075Sobrien build_enumerator (TREE_PURPOSE (e), value, newtag); 1069650397Sobrien } 1069750397Sobrien 1069852284Sobrien finish_enum (newtag); 10699117395Skan DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) 10700117395Skan = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); 1070152284Sobrien} 1070250397Sobrien 1070390075Sobrien/* DECL is a FUNCTION_DECL that is a template specialization. Return 1070490075Sobrien its type -- but without substituting the innermost set of template 1070590075Sobrien arguments. So, innermost set of template parameters will appear in 10706117395Skan the type. */ 1070750397Sobrien 1070890075Sobrientree 10709117395Skanget_mostly_instantiated_function_type (decl) 1071052284Sobrien tree decl; 1071152284Sobrien{ 1071252284Sobrien tree fn_type; 1071390075Sobrien tree tmpl; 1071490075Sobrien tree targs; 1071552284Sobrien tree tparms; 1071652284Sobrien int parm_depth; 1071750397Sobrien 1071890075Sobrien tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); 1071952284Sobrien targs = DECL_TI_ARGS (decl); 1072052284Sobrien tparms = DECL_TEMPLATE_PARMS (tmpl); 1072152284Sobrien parm_depth = TMPL_PARMS_DEPTH (tparms); 1072252284Sobrien 1072352284Sobrien /* There should be as many levels of arguments as there are levels 1072452284Sobrien of parameters. */ 1072552284Sobrien my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); 1072652284Sobrien 1072752284Sobrien fn_type = TREE_TYPE (tmpl); 1072852284Sobrien 1072952284Sobrien if (parm_depth == 1) 1073052284Sobrien /* No substitution is necessary. */ 1073152284Sobrien ; 1073252284Sobrien else 1073352284Sobrien { 1073452284Sobrien int i; 1073552284Sobrien tree partial_args; 1073652284Sobrien 1073752284Sobrien /* Replace the innermost level of the TARGS with NULL_TREEs to 1073890075Sobrien let tsubst know not to substitute for those parameters. */ 1073990075Sobrien partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); 1074052284Sobrien for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) 1074152284Sobrien SET_TMPL_ARGS_LEVEL (partial_args, i, 1074252284Sobrien TMPL_ARGS_LEVEL (targs, i)); 1074352284Sobrien SET_TMPL_ARGS_LEVEL (partial_args, 1074452284Sobrien TMPL_ARGS_DEPTH (targs), 1074590075Sobrien make_tree_vec (DECL_NTPARMS (tmpl))); 1074652284Sobrien 10747117395Skan /* Make sure that we can see identifiers, and compute access 10748117395Skan correctly. We can just use the context of DECL for the 10749117395Skan partial substitution here. It depends only on outer template 10750117395Skan parameters, regardless of whether the innermost level is 10751117395Skan specialized or not. */ 10752117395Skan push_access_scope (decl); 10753117395Skan 1075452284Sobrien /* Now, do the (partial) substitution to figure out the 1075552284Sobrien appropriate function type. */ 1075696263Sobrien fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); 1075752284Sobrien 1075852284Sobrien /* Substitute into the template parameters to obtain the real 1075952284Sobrien innermost set of parameters. This step is important if the 1076052284Sobrien innermost set of template parameters contains value 1076152284Sobrien parameters whose types depend on outer template parameters. */ 1076252284Sobrien TREE_VEC_LENGTH (partial_args)--; 1076396263Sobrien tparms = tsubst_template_parms (tparms, partial_args, tf_error); 10764117395Skan 10765117395Skan pop_access_scope (decl); 1076652284Sobrien } 1076752284Sobrien 1076890075Sobrien return fn_type; 1076990075Sobrien} 1077052284Sobrien 1077190075Sobrien/* Return truthvalue if we're processing a template different from 1077290075Sobrien the last one involved in diagnostics. */ 1077390075Sobrienint 1077490075Sobrienproblematic_instantiation_changed () 1077590075Sobrien{ 1077690075Sobrien return last_template_error_tick != tinst_level_tick; 1077790075Sobrien} 1077852284Sobrien 1077990075Sobrien/* Remember current template involved in diagnostics. */ 1078090075Sobrienvoid 1078190075Sobrienrecord_last_problematic_instantiation () 1078290075Sobrien{ 1078390075Sobrien last_template_error_tick = tinst_level_tick; 1078490075Sobrien} 1078552284Sobrien 1078690075Sobrientree 1078790075Sobriencurrent_instantiation () 1078890075Sobrien{ 1078990075Sobrien return current_tinst_level; 1079090075Sobrien} 1079152284Sobrien 1079290075Sobrien/* [temp.param] Check that template non-type parm TYPE is of an allowable 10793117395Skan type. Return zero for ok, nonzero for disallowed. Issue error and 1079496263Sobrien warning messages under control of COMPLAIN. */ 1079552284Sobrien 1079690075Sobrienstatic int 1079790075Sobrieninvalid_nontype_parm_type_p (type, complain) 1079890075Sobrien tree type; 1079996263Sobrien tsubst_flags_t complain; 1080090075Sobrien{ 1080190075Sobrien if (INTEGRAL_TYPE_P (type)) 1080290075Sobrien return 0; 1080390075Sobrien else if (POINTER_TYPE_P (type)) 1080490075Sobrien return 0; 1080590075Sobrien else if (TYPE_PTRMEM_P (type)) 1080690075Sobrien return 0; 1080790075Sobrien else if (TYPE_PTRMEMFUNC_P (type)) 1080890075Sobrien return 0; 1080990075Sobrien else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 1081090075Sobrien return 0; 1081190075Sobrien else if (TREE_CODE (type) == TYPENAME_TYPE) 1081290075Sobrien return 0; 1081390075Sobrien 1081496263Sobrien if (complain & tf_error) 1081590075Sobrien error ("`%#T' is not a valid type for a template constant parameter", 1081690075Sobrien type); 1081790075Sobrien return 1; 1081850397Sobrien} 10819117395Skan 10820117395Skan#include "gt-cp-pt.h" 10821