/* Handle parameterized types (templates) for GNU C++. Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. Rewritten by Jason Merrill (jason@cygnus.com). This file is part of GCC. GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GCC; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Known bugs or deficiencies include: all methods must be provided in header files; can't use a source file that contains only the method templates and "just win". */ #include "config.h" #include "system.h" #include "coretypes.h" #include "tm.h" #include "obstack.h" #include "tree.h" #include "flags.h" #include "cp-tree.h" #include "tree-inline.h" #include "decl.h" #include "lex.h" #include "output.h" #include "except.h" #include "toplev.h" #include "rtl.h" #include "timevar.h" /* The type of functions taking a tree, and some additional data, and returning an int. */ typedef int (*tree_fn_t) (tree, void*); /* The PENDING_TEMPLATES is a TREE_LIST of templates whose instantiations have been deferred, either because their definitions were not yet available, or because we were putting off doing the work. The TREE_PURPOSE of each entry is either a DECL (for a function or static data member), or a TYPE (for a class) indicating what we are hoping to instantiate. The TREE_VALUE is not used. */ static GTY(()) tree pending_templates; static GTY(()) tree last_pending_template; int processing_template_parmlist; static int template_header_count; static GTY(()) tree saved_trees; static GTY(()) varray_type inline_parm_levels; static size_t inline_parm_levels_used; static GTY(()) tree current_tinst_level; static GTY(()) tree saved_access_scope; /* A map from local variable declarations in the body of the template presently being instantiated to the corresponding instantiated local variables. */ static htab_t local_specializations; #define UNIFY_ALLOW_NONE 0 #define UNIFY_ALLOW_MORE_CV_QUAL 1 #define UNIFY_ALLOW_LESS_CV_QUAL 2 #define UNIFY_ALLOW_DERIVED 4 #define UNIFY_ALLOW_INTEGER 8 #define UNIFY_ALLOW_OUTER_LEVEL 16 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 #define UNIFY_ALLOW_MAX_CORRECTION 128 #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is virtual, or a base class of a virtual base. */ #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current type with the desired type. */ static void push_access_scope (tree); static void pop_access_scope (tree); static int resolve_overloaded_unification (tree, tree, tree, tree, unification_kind_t, int); static int try_one_overload (tree, tree, tree, tree, tree, unification_kind_t, int, bool); static int unify (tree, tree, tree, tree, int); static void add_pending_template (tree); static void reopen_tinst_level (tree); static tree classtype_mangled_name (tree); static char* mangle_class_name_for_template (const char *, tree, tree); static tree tsubst_initializer_list (tree, tree); static tree get_class_bindings (tree, tree, tree); static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int); static void tsubst_enum (tree, tree, tree); static tree add_to_template_args (tree, tree); static tree add_outermost_template_args (tree, tree); static bool check_instantiated_args (tree, tree, tsubst_flags_t); static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*); static int type_unification_real (tree, tree, tree, tree, int, unification_kind_t, int, int); static void note_template_header (int); static tree convert_nontype_argument (tree, tree); static tree convert_template_argument (tree, tree, tree, tsubst_flags_t, int, tree); static tree get_bindings_overload (tree, tree, tree); static int for_each_template_parm (tree, tree_fn_t, void*, htab_t); static tree build_template_parm_index (int, int, int, tree, tree); static int inline_needs_template_parms (tree); static void push_inline_template_parms_recursive (tree, int); static tree retrieve_specialization (tree, tree); static tree retrieve_local_specialization (tree); static tree register_specialization (tree, tree, tree); static void register_local_specialization (tree, tree); static tree reduce_template_parm_level (tree, tree, int); static tree build_template_decl (tree, tree); static int mark_template_parm (tree, void *); static int template_parm_this_level_p (tree, void *); static tree tsubst_friend_function (tree, tree); static tree tsubst_friend_class (tree, tree); static int can_complete_type_without_circularity (tree); static tree get_bindings (tree, tree, tree); static tree get_bindings_real (tree, tree, tree, int, int, int); static int template_decl_level (tree); static int check_cv_quals_for_unify (int, tree, tree); static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); static tree tsubst_template_parms (tree, tree, tsubst_flags_t); static void regenerate_decl_from_template (tree, tree); static tree most_specialized (tree, tree, tree); static tree most_specialized_class (tree, tree); static int template_class_depth_real (tree, int); static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); static tree tsubst_decl (tree, tree, tree, tsubst_flags_t); static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); static void check_specialization_scope (void); static tree process_partial_specialization (tree); static void set_current_access_from_decl (tree); static void check_default_tmpl_args (tree, tree, int, int); static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree); static tree get_template_base_recursive (tree, tree, tree, tree, tree, int); static tree get_template_base (tree, tree, tree, tree); static int verify_class_unification (tree, tree, tree); static tree try_class_unification (tree, tree, tree, tree); static int coerce_template_template_parms (tree, tree, tsubst_flags_t, tree, tree); static tree determine_specialization (tree, tree, tree *, int); static int template_args_equal (tree, tree); static void tsubst_default_arguments (tree); static tree for_each_template_parm_r (tree *, int *, void *); static tree copy_default_args_to_explicit_spec_1 (tree, tree); static void copy_default_args_to_explicit_spec (tree); static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); static int eq_local_specializations (const void *, const void *); static bool dependent_type_p_r (tree); static tree tsubst (tree, tree, tsubst_flags_t, tree); static tree tsubst_expr (tree, tree, tsubst_flags_t, tree); static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); /* Make the current scope suitable for access checking when we are processing T. T can be FUNCTION_DECL for instantiated function template, or VAR_DECL for static member variable (need by instantiate_decl). */ static void push_access_scope (tree t) { my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL || TREE_CODE (t) == VAR_DECL, 0); if (DECL_FRIEND_CONTEXT (t)) push_nested_class (DECL_FRIEND_CONTEXT (t)); else if (DECL_CLASS_SCOPE_P (t)) push_nested_class (DECL_CONTEXT (t)); else push_to_top_level (); if (TREE_CODE (t) == FUNCTION_DECL) { saved_access_scope = tree_cons (NULL_TREE, current_function_decl, saved_access_scope); current_function_decl = t; } } /* Restore the scope set up by push_access_scope. T is the node we are processing. */ static void pop_access_scope (tree t) { if (TREE_CODE (t) == FUNCTION_DECL) { current_function_decl = TREE_VALUE (saved_access_scope); saved_access_scope = TREE_CHAIN (saved_access_scope); } if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) pop_nested_class (); else pop_from_top_level (); } /* Do any processing required when DECL (a member template declaration) is finished. Returns the TEMPLATE_DECL corresponding to DECL, unless it is a specialization, in which case the DECL itself is returned. */ tree finish_member_template_decl (tree decl) { if (decl == error_mark_node) return error_mark_node; my_friendly_assert (DECL_P (decl), 20020812); if (TREE_CODE (decl) == TYPE_DECL) { tree type; type = TREE_TYPE (decl); if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INFO (type) && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) { tree tmpl = CLASSTYPE_TI_TEMPLATE (type); check_member_template (tmpl); return tmpl; } return NULL_TREE; } else if (TREE_CODE (decl) == FIELD_DECL) error ("data member `%D' cannot be a member template", decl); else if (DECL_TEMPLATE_INFO (decl)) { if (!DECL_TEMPLATE_SPECIALIZATION (decl)) { check_member_template (DECL_TI_TEMPLATE (decl)); return DECL_TI_TEMPLATE (decl); } else return decl; } else error ("invalid member template declaration `%D'", decl); return error_mark_node; } /* Returns the template nesting level of the indicated class TYPE. For example, in: template struct A { template struct B {}; }; A::B has depth two, while A has depth one. Both A::B and A::B have depth one, if COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not specializations. This function is guaranteed to return 0 if passed NULL_TREE so that, for example, `template_class_depth (current_class_type)' is always safe. */ static int template_class_depth_real (tree type, int count_specializations) { int depth; for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; type = (TREE_CODE (type) == FUNCTION_DECL) ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) { if (TREE_CODE (type) != FUNCTION_DECL) { if (CLASSTYPE_TEMPLATE_INFO (type) && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) && ((count_specializations && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) ++depth; } else { if (DECL_TEMPLATE_INFO (type) && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) && ((count_specializations && DECL_TEMPLATE_SPECIALIZATION (type)) || uses_template_parms (DECL_TI_ARGS (type)))) ++depth; } } return depth; } /* Returns the template nesting level of the indicated class TYPE. Like template_class_depth_real, but instantiations do not count in the depth. */ int template_class_depth (tree type) { return template_class_depth_real (type, /*count_specializations=*/0); } /* Returns 1 if processing DECL as part of do_pending_inlines needs us to push template parms. */ static int inline_needs_template_parms (tree decl) { if (! DECL_TEMPLATE_INFO (decl)) return 0; return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); } /* Subroutine of maybe_begin_member_template_processing. Push the template parms in PARMS, starting from LEVELS steps into the chain, and ending at the beginning, since template parms are listed innermost first. */ static void push_inline_template_parms_recursive (tree parmlist, int levels) { tree parms = TREE_VALUE (parmlist); int i; if (levels > 1) push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); ++processing_template_decl; current_template_parms = tree_cons (size_int (processing_template_decl), parms, current_template_parms); TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, NULL); for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) { tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); my_friendly_assert (DECL_P (parm), 0); switch (TREE_CODE (parm)) { case TYPE_DECL: case TEMPLATE_DECL: pushdecl (parm); break; case PARM_DECL: { /* Make a CONST_DECL as is done in process_template_parm. It is ugly that we recreate this here; the original version built in process_template_parm is no longer available. */ tree decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); DECL_ARTIFICIAL (decl) = 1; TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1; DECL_INITIAL (decl) = DECL_INITIAL (parm); SET_DECL_TEMPLATE_PARM_P (decl); pushdecl (decl); } break; default: abort (); } } } /* Restore the template parameter context for a member template or a friend template defined in a class definition. */ void maybe_begin_member_template_processing (tree decl) { tree parms; int levels = 0; if (inline_needs_template_parms (decl)) { parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; if (DECL_TEMPLATE_SPECIALIZATION (decl)) { --levels; parms = TREE_CHAIN (parms); } push_inline_template_parms_recursive (parms, levels); } /* Remember how many levels of template parameters we pushed so that we can pop them later. */ if (!inline_parm_levels) VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); if (inline_parm_levels_used == inline_parm_levels->num_elements) VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; ++inline_parm_levels_used; } /* Undo the effects of begin_member_template_processing. */ void maybe_end_member_template_processing (void) { int i; if (!inline_parm_levels_used) return; --inline_parm_levels_used; for (i = 0; i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); ++i) { --processing_template_decl; current_template_parms = TREE_CHAIN (current_template_parms); poplevel (0, 0, 0); } } /* Returns nonzero iff T is a member template function. We must be careful as in template class C { void f(); } Here, f is a template function, and a member, but not a member template. This function does not concern itself with the origin of T, only its present state. So if we have template class C { template void f(U); } then neither C::f nor C::f is considered to be a member template. But, `template void C::f(U)' is considered a member template. */ int is_member_template (tree t) { if (!DECL_FUNCTION_TEMPLATE_P (t)) /* Anything that isn't a function or a template function is certainly not a member template. */ return 0; /* A local class can't have member templates. */ if (decl_function_context (t)) return 0; return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) /* If there are more levels of template parameters than there are template classes surrounding the declaration, then we have a member template. */ && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > template_class_depth (DECL_CONTEXT (t)))); } #if 0 /* UNUSED */ /* Returns nonzero iff T is a member template class. See is_member_template for a description of what precisely constitutes a member template. */ int is_member_template_class (tree t) { if (!DECL_CLASS_TEMPLATE_P (t)) /* Anything that isn't a class template, is certainly not a member template. */ return 0; if (!DECL_CLASS_SCOPE_P (t)) /* Anything whose context isn't a class type is surely not a member template. */ return 0; /* If there are more levels of template parameters than there are template classes surrounding the declaration, then we have a member template. */ return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > template_class_depth (DECL_CONTEXT (t))); } #endif /* Return a new template argument vector which contains all of ARGS, but has as its innermost set of arguments the EXTRA_ARGS. */ static tree add_to_template_args (tree args, tree extra_args) { tree new_args; int extra_depth; int i; int j; extra_depth = TMPL_ARGS_DEPTH (extra_args); new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); for (j = 1; j <= extra_depth; ++j, ++i) SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); return new_args; } /* Like add_to_template_args, but only the outermost ARGS are added to the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH (EXTRA_ARGS) levels are added. This function is used to combine the template arguments from a partial instantiation with the template arguments used to attain the full instantiation from the partial instantiation. */ static tree add_outermost_template_args (tree args, tree extra_args) { tree new_args; /* If there are more levels of EXTRA_ARGS than there are ARGS, something very fishy is going on. */ my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), 0); /* If *all* the new arguments will be the EXTRA_ARGS, just return them. */ if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) return extra_args; /* For the moment, we make ARGS look like it contains fewer levels. */ TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); new_args = add_to_template_args (args, extra_args); /* Now, we restore ARGS to its full dimensions. */ TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); return new_args; } /* Return the N levels of innermost template arguments from the ARGS. */ tree get_innermost_template_args (tree args, int n) { tree new_args; int extra_levels; int i; my_friendly_assert (n >= 0, 20000603); /* If N is 1, just return the innermost set of template arguments. */ if (n == 1) return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); /* If we're not removing anything, just return the arguments we were given. */ extra_levels = TMPL_ARGS_DEPTH (args) - n; my_friendly_assert (extra_levels >= 0, 20000603); if (extra_levels == 0) return args; /* Make a new set of arguments, not containing the outer arguments. */ new_args = make_tree_vec (n); for (i = 1; i <= n; ++i) SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i + extra_levels)); return new_args; } /* We've got a template header coming up; push to a new level for storing the parms. */ void begin_template_parm_list (void) { /* We use a non-tag-transparent scope here, which causes pushtag to put tags in this scope, rather than in the enclosing class or namespace scope. This is the right thing, since we want TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a global template class, push_template_decl handles putting the TEMPLATE_DECL into top-level scope. For a nested template class, e.g.: template struct S1 { template struct S2 {}; }; pushtag contains special code to call pushdecl_with_scope on the TEMPLATE_DECL for S2. */ begin_scope (sk_template_parms, NULL); ++processing_template_decl; ++processing_template_parmlist; note_template_header (0); } /* This routine is called when a specialization is declared. If it is invalid to declare a specialization here, an error is reported. */ static void check_specialization_scope (void) { tree scope = current_scope (); /* [temp.expl.spec] An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member. An explicit specialization of a member function, member class or static data member of a class template shall be declared in the namespace of which the class template is a member. */ if (scope && TREE_CODE (scope) != NAMESPACE_DECL) error ("explicit specialization in non-namespace scope `%D'", scope); /* [temp.expl.spec] In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. */ if (current_template_parms) error ("enclosing class templates are not explicitly specialized"); } /* We've just seen template <>. */ void begin_specialization (void) { begin_scope (sk_template_spec, NULL); note_template_header (1); check_specialization_scope (); } /* Called at then end of processing a declaration preceded by template<>. */ void end_specialization (void) { finish_scope (); reset_specialization (); } /* Any template <>'s that we have seen thus far are not referring to a function specialization. */ void reset_specialization (void) { processing_specialization = 0; template_header_count = 0; } /* We've just seen a template header. If SPECIALIZATION is nonzero, it was of the form template <>. */ static void note_template_header (int specialization) { processing_specialization = specialization; template_header_count++; } /* We're beginning an explicit instantiation. */ void begin_explicit_instantiation (void) { my_friendly_assert (!processing_explicit_instantiation, 20020913); processing_explicit_instantiation = true; } void end_explicit_instantiation (void) { my_friendly_assert(processing_explicit_instantiation, 20020913); processing_explicit_instantiation = false; } /* A explicit specialization or partial specialization TMPL is being declared. Check that the namespace in which the specialization is occurring is permissible. Returns false iff it is invalid to specialize TMPL in the current namespace. */ static bool check_specialization_namespace (tree tmpl) { tree tpl_ns = decl_namespace_context (tmpl); /* [tmpl.expl.spec] An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member. An explicit specialization of a member function, member class or static data member of a class template shall be declared in the namespace of which the class template is a member. */ if (is_associated_namespace (current_namespace, tpl_ns)) /* Same or super-using namespace. */ return true; else { pedwarn ("specialization of `%D' in different namespace", tmpl); cp_pedwarn_at (" from definition of `%#D'", tmpl); return false; } } /* The TYPE is being declared. If it is a template type, that means it is a partial specialization. Do appropriate error-checking. */ void maybe_process_partial_specialization (tree type) { tree context; if (type == error_mark_node) return; context = TYPE_CONTEXT (type); if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) { /* This is for ordinary explicit specialization and partial specialization of a template class such as: template <> class C; or: template class C; Make sure that `C' and `C' are implicit instantiations. */ if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type)) { check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); if (processing_template_decl) push_template_decl (TYPE_MAIN_DECL (type)); } else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) error ("specialization of `%T' after instantiation", type); } else if (CLASS_TYPE_P (type) && !CLASSTYPE_USE_TEMPLATE (type) && CLASSTYPE_TEMPLATE_INFO (type) && context && CLASS_TYPE_P (context) && CLASSTYPE_TEMPLATE_INFO (context)) { /* This is for an explicit specialization of member class template according to [temp.expl.spec/18]: template <> template class C::D; The context `C' must be an implicit instantiation. Otherwise this is just a member class template declared earlier like: template <> class C { template class D; }; template <> template class C::D; In the first case, `C::D' is a specialization of `C::D' while in the second case, `C::D' is a primary template and `C::D' may not exist. */ if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) && !COMPLETE_TYPE_P (type)) { tree t; if (current_namespace != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) { pedwarn ("specializing `%#T' in different namespace", type); cp_pedwarn_at (" from definition of `%#D'", CLASSTYPE_TI_TEMPLATE (type)); } /* Check for invalid specialization after instantiation: template <> template <> class C::D; template <> template class C::D; */ for (t = DECL_TEMPLATE_INSTANTIATIONS (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); t; t = TREE_CHAIN (t)) if (TREE_VALUE (t) != type && TYPE_CONTEXT (TREE_VALUE (t)) == context) error ("specialization `%T' after instantiation `%T'", type, TREE_VALUE (t)); /* Mark TYPE as a specialization. And as a result, we only have one level of template argument for the innermost class template. */ SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); CLASSTYPE_TI_ARGS (type) = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); } } else if (processing_specialization) error ("explicit specialization of non-template `%T'", type); } /* Retrieve the specialization (in the sense of [temp.spec] - a specialization is either an instantiation or an explicit specialization) of TMPL for the given template ARGS. If there is no such specialization, return NULL_TREE. The ARGS are a vector of arguments, or a vector of vectors of arguments, in the case of templates with more than one level of parameters. */ static tree retrieve_specialization (tree tmpl, tree args) { tree s; my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); /* There should be as many levels of arguments as there are levels of parameters. */ my_friendly_assert (TMPL_ARGS_DEPTH (args) == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), 0); for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); s != NULL_TREE; s = TREE_CHAIN (s)) if (comp_template_args (TREE_PURPOSE (s), args)) return TREE_VALUE (s); return NULL_TREE; } /* Like retrieve_specialization, but for local declarations. */ static tree retrieve_local_specialization (tree tmpl) { tree spec = htab_find_with_hash (local_specializations, tmpl, htab_hash_pointer (tmpl)); return spec ? TREE_PURPOSE (spec) : NULL_TREE; } /* Returns nonzero iff DECL is a specialization of TMPL. */ int is_specialization_of (tree decl, tree tmpl) { tree t; if (TREE_CODE (decl) == FUNCTION_DECL) { for (t = decl; t != NULL_TREE; t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) if (t == tmpl) return 1; } else { my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); for (t = TREE_TYPE (decl); t != NULL_TREE; t = CLASSTYPE_USE_TEMPLATE (t) ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) return 1; } return 0; } /* Returns nonzero iff DECL is a specialization of friend declaration FRIEND according to [temp.friend]. */ bool is_specialization_of_friend (tree decl, tree friend) { bool need_template = true; int template_depth; my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); /* For [temp.friend/6] when FRIEND is an ordinary member function of a template class, we want to check if DECL is a specialization if this. */ if (TREE_CODE (friend) == FUNCTION_DECL && DECL_TEMPLATE_INFO (friend) && !DECL_USE_TEMPLATE (friend)) { friend = DECL_TI_TEMPLATE (friend); need_template = false; } /* There is nothing to do if this is not a template friend. */ if (TREE_CODE (friend) != TEMPLATE_DECL) return 0; if (is_specialization_of (decl, friend)) return 1; /* [temp.friend/6] A member of a class template may be declared to be a friend of a non-template class. In this case, the corresponding member of every specialization of the class template is a friend of the class granting friendship. For example, given a template friend declaration template friend void A::f(); the member function below is considered a friend template <> struct A { void f(); }; For this type of template friend, TEMPLATE_DEPTH below will be nonzero. To determine if DECL is a friend of FRIEND, we first check if the enclosing class is a specialization of another. */ template_depth = template_class_depth (DECL_CONTEXT (friend)); if (template_depth && DECL_CLASS_SCOPE_P (decl) && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) { /* Next, we check the members themselves. In order to handle a few tricky cases like template friend void A::g(T t); template template friend void A::h(); we need to figure out what ARGS is (corresponding to `T' in above examples) from DECL for later processing. */ tree context = DECL_CONTEXT (decl); tree args = NULL_TREE; int current_depth = 0; while (current_depth < template_depth) { if (CLASSTYPE_TEMPLATE_INFO (context)) { if (current_depth == 0) args = TYPE_TI_ARGS (context); else args = add_to_template_args (TYPE_TI_ARGS (context), args); current_depth++; } context = TYPE_CONTEXT (context); } if (TREE_CODE (decl) == FUNCTION_DECL) { bool is_template; tree friend_type; tree decl_type; tree friend_args_type; tree decl_args_type; /* Make sure that both DECL and FRIEND are templates or non-templates. */ is_template = DECL_TEMPLATE_INFO (decl) && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); if (need_template ^ is_template) return 0; else if (is_template) { /* If both are templates, check template parameter list. */ tree friend_parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), args, tf_none); if (!comp_template_parms (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), friend_parms)) return 0; decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); } else decl_type = TREE_TYPE (decl); friend_type = tsubst_function_type (TREE_TYPE (friend), args, tf_none, NULL_TREE); if (friend_type == error_mark_node) return 0; /* Check if return types match. */ if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) return 0; /* Check if function parameter types match, ignoring the `this' parameter. */ friend_args_type = TYPE_ARG_TYPES (friend_type); decl_args_type = TYPE_ARG_TYPES (decl_type); if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) friend_args_type = TREE_CHAIN (friend_args_type); if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) decl_args_type = TREE_CHAIN (decl_args_type); if (compparms (decl_args_type, friend_args_type)) return 1; } } return 0; } /* Register the specialization SPEC as a specialization of TMPL with the indicated ARGS. Returns SPEC, or an equivalent prior declaration, if available. */ static tree register_specialization (tree spec, tree tmpl, tree args) { tree s; my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); if (TREE_CODE (spec) == FUNCTION_DECL && uses_template_parms (DECL_TI_ARGS (spec))) /* This is the FUNCTION_DECL for a partial instantiation. Don't register it; we want the corresponding TEMPLATE_DECL instead. We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than the more obvious `uses_template_parms (spec)' to avoid problems with default function arguments. In particular, given something like this: template void f(T t1, T t = T()) the default argument expression is not substituted for in an instantiation unless and until it is actually needed. */ return spec; /* There should be as many levels of arguments as there are levels of parameters. */ my_friendly_assert (TMPL_ARGS_DEPTH (args) == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), 0); for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); s != NULL_TREE; s = TREE_CHAIN (s)) { tree fn = TREE_VALUE (s); /* We can sometimes try to re-register a specialization that we've already got. In particular, regenerate_decl_from_template calls duplicate_decls which will update the specialization list. But, we'll still get called again here anyhow. It's more convenient to simply allow this than to try to prevent it. */ if (fn == spec) return spec; else if (comp_template_args (TREE_PURPOSE (s), args)) { if (DECL_TEMPLATE_SPECIALIZATION (spec)) { if (DECL_TEMPLATE_INSTANTIATION (fn)) { if (TREE_USED (fn) || DECL_EXPLICIT_INSTANTIATION (fn)) { error ("specialization of %D after instantiation", fn); return spec; } else { /* This situation should occur only if the first specialization is an implicit instantiation, the second is an explicit specialization, and the implicit instantiation has not yet been used. That situation can occur if we have implicitly instantiated a member function and then specialized it later. We can also wind up here if a friend declaration that looked like an instantiation turns out to be a specialization: template void foo(T); class S { friend void foo<>(int) }; template <> void foo(int); We transform the existing DECL in place so that any pointers to it become pointers to the updated declaration. If there was a definition for the template, but not for the specialization, we want this to look as if there is no definition, and vice versa. */ DECL_INITIAL (fn) = NULL_TREE; duplicate_decls (spec, fn); return fn; } } else if (DECL_TEMPLATE_SPECIALIZATION (fn)) { if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec)) /* Dup decl failed, but this is a new definition. Set the line number so any errors match this new definition. */ DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); return fn; } } } } /* A specialization must be declared in the same namespace as the template it is specializing. */ if (DECL_TEMPLATE_SPECIALIZATION (spec) && !check_specialization_namespace (tmpl)) DECL_CONTEXT (spec) = decl_namespace_context (tmpl); DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); return spec; } /* Unregister the specialization SPEC as a specialization of TMPL. Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true if the SPEC was listed as a specialization of TMPL. */ bool reregister_specialization (tree spec, tree tmpl, tree new_spec) { tree* s; for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); *s != NULL_TREE; s = &TREE_CHAIN (*s)) if (TREE_VALUE (*s) == spec) { if (!new_spec) *s = TREE_CHAIN (*s); else TREE_VALUE (*s) = new_spec; return 1; } return 0; } /* Compare an entry in the local specializations hash table P1 (which is really a pointer to a TREE_LIST) with P2 (which is really a DECL). */ static int eq_local_specializations (const void *p1, const void *p2) { return TREE_VALUE ((tree) p1) == (tree) p2; } /* Hash P1, an entry in the local specializations table. */ static hashval_t hash_local_specialization (const void* p1) { return htab_hash_pointer (TREE_VALUE ((tree) p1)); } /* Like register_specialization, but for local declarations. We are registering SPEC, an instantiation of TMPL. */ static void register_local_specialization (tree spec, tree tmpl) { void **slot; slot = htab_find_slot_with_hash (local_specializations, tmpl, htab_hash_pointer (tmpl), INSERT); *slot = build_tree_list (spec, tmpl); } /* Print the list of candidate FNS in an error message. */ void print_candidates (tree fns) { tree fn; const char *str = "candidates are:"; for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) { tree f; for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); str = " "; } } /* Returns the template (one of the functions given by TEMPLATE_ID) which can be specialized to match the indicated DECL with the explicit template args given in TEMPLATE_ID. The DECL may be NULL_TREE if none is available. In that case, the functions in TEMPLATE_ID are non-members. If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a specialization of a member template. The template args (those explicitly specified and those deduced) are output in a newly created vector *TARGS_OUT. If it is impossible to determine the result, an error message is issued. The error_mark_node is returned to indicate failure. */ static tree determine_specialization (tree template_id, tree decl, tree* targs_out, int need_member_template) { tree fns; tree targs; tree explicit_targs; tree candidates = NULL_TREE; tree templates = NULL_TREE; *targs_out = NULL_TREE; if (template_id == error_mark_node) return error_mark_node; fns = TREE_OPERAND (template_id, 0); explicit_targs = TREE_OPERAND (template_id, 1); if (fns == error_mark_node) return error_mark_node; /* Check for baselinks. */ if (BASELINK_P (fns)) fns = BASELINK_FUNCTIONS (fns); if (!is_overloaded_fn (fns)) { error ("`%D' is not a function template", fns); return error_mark_node; } for (; fns; fns = OVL_NEXT (fns)) { tree fn = OVL_CURRENT (fns); if (TREE_CODE (fn) == TEMPLATE_DECL) { tree decl_arg_types; tree fn_arg_types; /* DECL might be a specialization of FN. */ /* Adjust the type of DECL in case FN is a static member. */ decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); if (DECL_STATIC_FUNCTION_P (fn) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) decl_arg_types = TREE_CHAIN (decl_arg_types); /* Check that the number of function parameters matches. For example, template void f(int i = 0); template <> void f(); The specialization f is invalid but is not caught by get_bindings below. */ fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); if (list_length (fn_arg_types) != list_length (decl_arg_types)) continue; /* For a non-static member function, we need to make sure that the const qualification is the same. This can be done by checking the 'this' in the argument list. */ if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) && !same_type_p (TREE_VALUE (fn_arg_types), TREE_VALUE (decl_arg_types))) continue; /* See whether this function might be a specialization of this template. */ targs = get_bindings (fn, decl, explicit_targs); if (!targs) /* We cannot deduce template arguments that when used to specialize TMPL will produce DECL. */ continue; /* Save this template, and the arguments deduced. */ templates = tree_cons (targs, fn, templates); } else if (need_member_template) /* FN is an ordinary member function, and we need a specialization of a member template. */ ; else if (TREE_CODE (fn) != FUNCTION_DECL) /* We can get IDENTIFIER_NODEs here in certain erroneous cases. */ ; else if (!DECL_FUNCTION_MEMBER_P (fn)) /* This is just an ordinary non-member function. Nothing can be a specialization of that. */ ; else if (DECL_ARTIFICIAL (fn)) /* Cannot specialize functions that are created implicitly. */ ; else { tree decl_arg_types; /* This is an ordinary member function. However, since we're here, we can assume it's enclosing class is a template class. For example, template struct S { void f(); }; template <> void S::f() {} Here, S::f is a non-template, but S is a template class. If FN has the same type as DECL, we might be in business. */ if (!DECL_TEMPLATE_INFO (fn)) /* Its enclosing class is an explicit specialization of a template class. This is not a candidate. */ continue; if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), TREE_TYPE (TREE_TYPE (fn)))) /* The return types differ. */ continue; /* Adjust the type of DECL in case FN is a static member. */ decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); if (DECL_STATIC_FUNCTION_P (fn) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) decl_arg_types = TREE_CHAIN (decl_arg_types); if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), decl_arg_types)) /* They match! */ candidates = tree_cons (NULL_TREE, fn, candidates); } } if (templates && TREE_CHAIN (templates)) { /* We have: [temp.expl.spec] It is possible for a specialization with a given function signature to be instantiated from more than one function template. In such cases, explicit specification of the template arguments must be used to uniquely identify the function template specialization being specialized. Note that here, there's no suggestion that we're supposed to determine which of the candidate templates is most specialized. However, we, also have: [temp.func.order] Partial ordering of overloaded function template declarations is used in the following contexts to select the function template to which a function template specialization refers: -- when an explicit specialization refers to a function template. So, we do use the partial ordering rules, at least for now. This extension can only serve to make invalid programs valid, so it's safe. And, there is strong anecdotal evidence that the committee intended the partial ordering rules to apply; the EDG front-end has that behavior, and John Spicer claims that the committee simply forgot to delete the wording in [temp.expl.spec]. */ tree tmpl = most_specialized (templates, decl, explicit_targs); if (tmpl && tmpl != error_mark_node) { targs = get_bindings (tmpl, decl, explicit_targs); templates = tree_cons (targs, tmpl, NULL_TREE); } } if (templates == NULL_TREE && candidates == NULL_TREE) { cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", template_id, decl); return error_mark_node; } else if ((templates && TREE_CHAIN (templates)) || (candidates && TREE_CHAIN (candidates)) || (templates && candidates)) { cp_error_at ("ambiguous template specialization `%D' for `%+D'", template_id, decl); chainon (candidates, templates); print_candidates (candidates); return error_mark_node; } /* We have one, and exactly one, match. */ if (candidates) { /* It was a specialization of an ordinary member function in a template class. */ *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); } /* It was a specialization of a template. */ targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) { *targs_out = copy_node (targs); SET_TMPL_ARGS_LEVEL (*targs_out, TMPL_ARGS_DEPTH (*targs_out), TREE_PURPOSE (templates)); } else *targs_out = TREE_PURPOSE (templates); return TREE_VALUE (templates); } /* Returns a chain of parameter types, exactly like the SPEC_TYPES, but with the default argument values filled in from those in the TMPL_TYPES. */ static tree copy_default_args_to_explicit_spec_1 (tree spec_types, tree tmpl_types) { tree new_spec_types; if (!spec_types) return NULL_TREE; if (spec_types == void_list_node) return void_list_node; /* Substitute into the rest of the list. */ new_spec_types = copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), TREE_CHAIN (tmpl_types)); /* Add the default argument for this parameter. */ return hash_tree_cons (TREE_PURPOSE (tmpl_types), TREE_VALUE (spec_types), new_spec_types); } /* DECL is an explicit specialization. Replicate default arguments from the template it specializes. (That way, code like: template void f(T = 3); template <> void f(double); void g () { f (); } works, as required.) An alternative approach would be to look up the correct default arguments at the call-site, but this approach is consistent with how implicit instantiations are handled. */ static void copy_default_args_to_explicit_spec (tree decl) { tree tmpl; tree spec_types; tree tmpl_types; tree new_spec_types; tree old_type; tree new_type; tree t; tree object_type = NULL_TREE; tree in_charge = NULL_TREE; tree vtt = NULL_TREE; /* See if there's anything we need to do. */ tmpl = DECL_TI_TEMPLATE (decl); tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); for (t = tmpl_types; t; t = TREE_CHAIN (t)) if (TREE_PURPOSE (t)) break; if (!t) return; old_type = TREE_TYPE (decl); spec_types = TYPE_ARG_TYPES (old_type); if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) { /* Remove the this pointer, but remember the object's type for CV quals. */ object_type = TREE_TYPE (TREE_VALUE (spec_types)); spec_types = TREE_CHAIN (spec_types); tmpl_types = TREE_CHAIN (tmpl_types); if (DECL_HAS_IN_CHARGE_PARM_P (decl)) { /* DECL may contain more parameters than TMPL due to the extra in-charge parameter in constructors and destructors. */ in_charge = spec_types; spec_types = TREE_CHAIN (spec_types); } if (DECL_HAS_VTT_PARM_P (decl)) { vtt = spec_types; spec_types = TREE_CHAIN (spec_types); } } /* Compute the merged default arguments. */ new_spec_types = copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); /* Compute the new FUNCTION_TYPE. */ if (object_type) { if (vtt) new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), TREE_VALUE (vtt), new_spec_types); if (in_charge) /* Put the in-charge parameter back. */ new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), TREE_VALUE (in_charge), new_spec_types); new_type = build_method_type_directly (object_type, TREE_TYPE (old_type), new_spec_types); } else new_type = build_function_type (TREE_TYPE (old_type), new_spec_types); new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (old_type)); new_type = build_exception_variant (new_type, TYPE_RAISES_EXCEPTIONS (old_type)); TREE_TYPE (decl) = new_type; } /* Check to see if the function just declared, as indicated in DECLARATOR, and in DECL, is a specialization of a function template. We may also discover that the declaration is an explicit instantiation at this point. Returns DECL, or an equivalent declaration that should be used instead if all goes well. Issues an error message if something is amiss. Returns error_mark_node if the error is not easily recoverable. FLAGS is a bitmask consisting of the following flags: 2: The function has a definition. 4: The function is a friend. The TEMPLATE_COUNT is the number of references to qualifying template classes that appeared in the name of the function. For example, in template struct S { void f(); }; void S::f(); the TEMPLATE_COUNT would be 1. However, explicitly specialized classes are not counted in the TEMPLATE_COUNT, so that in template struct S {}; template <> struct S { void f(); } template <> void S::f(); the TEMPLATE_COUNT would be 0. (Note that this declaration is invalid; there should be no template <>.) If the function is a specialization, it is marked as such via DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO is set up correctly, and it is added to the list of specializations for that template. */ tree check_explicit_specialization (tree declarator, tree decl, int template_count, int flags) { int have_def = flags & 2; int is_friend = flags & 4; int specialization = 0; int explicit_instantiation = 0; int member_specialization = 0; tree ctype = DECL_CLASS_CONTEXT (decl); tree dname = DECL_NAME (decl); tmpl_spec_kind tsk; tsk = current_tmpl_spec_kind (template_count); switch (tsk) { case tsk_none: if (processing_specialization) { specialization = 1; SET_DECL_TEMPLATE_SPECIALIZATION (decl); } else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) { if (is_friend) /* This could be something like: template void f(T); class S { friend void f<>(int); } */ specialization = 1; else { /* This case handles bogus declarations like template <> template void f(); */ error ("template-id `%D' in declaration of primary template", declarator); return decl; } } break; case tsk_invalid_member_spec: /* The error has already been reported in check_specialization_scope. */ return error_mark_node; case tsk_invalid_expl_inst: error ("template parameter list used in explicit instantiation"); /* Fall through. */ case tsk_expl_inst: if (have_def) error ("definition provided for explicit instantiation"); explicit_instantiation = 1; break; case tsk_excessive_parms: error ("too many template parameter lists in declaration of `%D'", decl); return error_mark_node; /* Fall through. */ case tsk_expl_spec: SET_DECL_TEMPLATE_SPECIALIZATION (decl); if (ctype) member_specialization = 1; else specialization = 1; break; case tsk_insufficient_parms: if (template_header_count) { error("too few template parameter lists in declaration of `%D'", decl); return decl; } else if (ctype != NULL_TREE && !TYPE_BEING_DEFINED (ctype) && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) && !is_friend) { /* For backwards compatibility, we accept: template struct S { void f(); }; void S::f() {} // Missing template <> That used to be valid C++. */ if (pedantic) pedwarn ("explicit specialization not preceded by `template <>'"); specialization = 1; SET_DECL_TEMPLATE_SPECIALIZATION (decl); } break; case tsk_template: if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) { /* This case handles bogus declarations like template <> template void f(); */ if (uses_template_parms (declarator)) error ("partial specialization `%D' of function template", declarator); else error ("template-id `%D' in declaration of primary template", declarator); return decl; } if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) /* This is a specialization of a member template, without specialization the containing class. Something like: template struct S { template void f (U); }; template <> template void S::f(U) {} That's a specialization -- but of the entire template. */ specialization = 1; break; default: abort (); } if (specialization || member_specialization) { tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); for (; t; t = TREE_CHAIN (t)) if (TREE_PURPOSE (t)) { pedwarn ("default argument specified in explicit specialization"); break; } if (current_lang_name == lang_name_c) error ("template specialization with C linkage"); } if (specialization || member_specialization || explicit_instantiation) { tree tmpl = NULL_TREE; tree targs = NULL_TREE; /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) { tree fns; my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, 0); if (ctype) fns = dname; else { /* If there is no class context, the explicit instantiation must be at namespace scope. */ my_friendly_assert (DECL_NAMESPACE_SCOPE_P (decl), 20030625); /* Find the namespace binding, using the declaration context. */ fns = namespace_binding (dname, CP_DECL_CONTEXT (decl)); if (!fns || !is_overloaded_fn (fns)) { error ("`%D' is not a template function", dname); fns = error_mark_node; } } declarator = lookup_template_function (fns, NULL_TREE); } if (declarator == error_mark_node) return error_mark_node; if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) { if (!explicit_instantiation) /* A specialization in class scope. This is invalid, but the error will already have been flagged by check_specialization_scope. */ return error_mark_node; else { /* It's not valid to write an explicit instantiation in class scope, e.g.: class C { template void f(); } This case is caught by the parser. However, on something like: template class C { void f(); }; (which is invalid) we can get here. The error will be issued later. */ ; } return decl; } else if (ctype != NULL_TREE && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE)) { /* Find the list of functions in ctype that have the same name as the declared function. */ tree name = TREE_OPERAND (declarator, 0); tree fns = NULL_TREE; int idx; if (constructor_name_p (name, ctype)) { int is_constructor = DECL_CONSTRUCTOR_P (decl); if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) : !TYPE_HAS_DESTRUCTOR (ctype)) { /* From [temp.expl.spec]: If such an explicit specialization for the member of a class template names an implicitly-declared special member function (clause _special_), the program is ill-formed. Similar language is found in [temp.explicit]. */ error ("specialization of implicitly-declared special member function"); return error_mark_node; } name = is_constructor ? ctor_identifier : dtor_identifier; } if (!DECL_CONV_FN_P (decl)) { idx = lookup_fnfields_1 (ctype, name); if (idx >= 0) fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); } else { tree methods; /* For a type-conversion operator, we cannot do a name-based lookup. We might be looking for `operator int' which will be a specialization of `operator T'. So, we find *all* the conversion operators, and then select from them. */ fns = NULL_TREE; methods = CLASSTYPE_METHOD_VEC (ctype); if (methods) for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; idx < TREE_VEC_LENGTH (methods); ++idx) { tree ovl = TREE_VEC_ELT (methods, idx); if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) /* There are no more conversion functions. */ break; /* Glue all these conversion functions together with those we already have. */ for (; ovl; ovl = OVL_NEXT (ovl)) fns = ovl_cons (OVL_CURRENT (ovl), fns); } } if (fns == NULL_TREE) { error ("no member function `%D' declared in `%T'", name, ctype); return error_mark_node; } else TREE_OPERAND (declarator, 0) = fns; } /* Figure out what exactly is being specialized at this point. Note that for an explicit instantiation, even one for a member function, we cannot tell apriori whether the instantiation is for a member template, or just a member function of a template class. Even if a member template is being instantiated, the member template arguments may be elided if they can be deduced from the rest of the declaration. */ tmpl = determine_specialization (declarator, decl, &targs, member_specialization); if (!tmpl || tmpl == error_mark_node) /* We couldn't figure out what this declaration was specializing. */ return error_mark_node; else { tree gen_tmpl = most_general_template (tmpl); if (explicit_instantiation) { /* We don't set DECL_EXPLICIT_INSTANTIATION here; that is done by do_decl_instantiation later. */ int arg_depth = TMPL_ARGS_DEPTH (targs); int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); if (arg_depth > parm_depth) { /* If TMPL is not the most general template (for example, if TMPL is a friend template that is injected into namespace scope), then there will be too many levels of TARGS. Remove some of them here. */ int i; tree new_targs; new_targs = make_tree_vec (parm_depth); for (i = arg_depth - parm_depth; i < arg_depth; ++i) TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) = TREE_VEC_ELT (targs, i); targs = new_targs; } return instantiate_template (tmpl, targs, tf_error); } /* If we thought that the DECL was a member function, but it turns out to be specializing a static member function, make DECL a static member function as well. */ if (DECL_STATIC_FUNCTION_P (tmpl) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) revert_static_member_fn (decl); /* If this is a specialization of a member template of a template class. In we want to return the TEMPLATE_DECL, not the specialization of it. */ if (tsk == tsk_template) { SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; if (have_def) { DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) = DECL_SOURCE_LOCATION (decl); /* We want to use the argument list specified in the definition, not in the original declaration. */ DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) = DECL_ARGUMENTS (decl); } return tmpl; } /* Set up the DECL_TEMPLATE_INFO for DECL. */ DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); /* Inherit default function arguments from the template DECL is specializing. */ copy_default_args_to_explicit_spec (decl); /* This specialization has the same protection as the template it specializes. */ TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); if (is_friend && !have_def) /* This is not really a declaration of a specialization. It's just the name of an instantiation. But, it's not a request for an instantiation, either. */ SET_DECL_IMPLICIT_INSTANTIATION (decl); else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) /* This is indeed a specialization. In case of constructors and destructors, we need in-charge and not-in-charge versions in V3 ABI. */ clone_function_decl (decl, /*update_method_vec_p=*/0); /* Register this specialization so that we can find it again. */ decl = register_specialization (decl, gen_tmpl, targs); } } return decl; } /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template parameters. These are represented in the same format used for DECL_TEMPLATE_PARMS. */ int comp_template_parms (tree parms1, tree parms2) { tree p1; tree p2; if (parms1 == parms2) return 1; for (p1 = parms1, p2 = parms2; p1 != NULL_TREE && p2 != NULL_TREE; p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) { tree t1 = TREE_VALUE (p1); tree t2 = TREE_VALUE (p2); int i; my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) return 0; for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) { tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); if (TREE_CODE (parm1) != TREE_CODE (parm2)) return 0; if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) continue; else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) return 0; } } if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) /* One set of parameters has more parameters lists than the other. */ return 0; return 1; } /* Complain if DECL shadows a template parameter. [temp.local]: A template-parameter shall not be redeclared within its scope (including nested scopes). */ void check_template_shadow (tree decl) { tree olddecl; /* If we're not in a template, we can't possibly shadow a template parameter. */ if (!current_template_parms) return; /* Figure out what we're shadowing. */ if (TREE_CODE (decl) == OVERLOAD) decl = OVL_CURRENT (decl); olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); /* If there's no previous binding for this name, we're not shadowing anything, let alone a template parameter. */ if (!olddecl) return; /* If we're not shadowing a template parameter, we're done. Note that OLDDECL might be an OVERLOAD (or perhaps even an ERROR_MARK), so we can't just blithely assume it to be a _DECL node. */ if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) return; /* We check for decl != olddecl to avoid bogus errors for using a name inside a class. We check TPFI to avoid duplicate errors for inline member templates. */ if (decl == olddecl || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) return; cp_error_at ("declaration of `%#D'", decl); cp_error_at (" shadows template parm `%#D'", olddecl); } /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, ORIG_LEVEL, DECL, and TYPE. */ static tree build_template_parm_index (int index, int level, int orig_level, tree decl, tree type) { tree t = make_node (TEMPLATE_PARM_INDEX); TEMPLATE_PARM_IDX (t) = index; TEMPLATE_PARM_LEVEL (t) = level; TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; TEMPLATE_PARM_DECL (t) = decl; TREE_TYPE (t) = type; TREE_CONSTANT (t) = TREE_CONSTANT (decl); TREE_READONLY (t) = TREE_READONLY (decl); return t; } /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a new one is created. */ static tree reduce_template_parm_level (tree index, tree type, int levels) { if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) != TEMPLATE_PARM_LEVEL (index) - levels)) { tree orig_decl = TEMPLATE_PARM_DECL (index); tree decl, t; decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); TREE_READONLY (decl) = TREE_READONLY (orig_decl); DECL_ARTIFICIAL (decl) = 1; SET_DECL_TEMPLATE_PARM_P (decl); t = build_template_parm_index (TEMPLATE_PARM_IDX (index), TEMPLATE_PARM_LEVEL (index) - levels, TEMPLATE_PARM_ORIG_LEVEL (index), decl, type); TEMPLATE_PARM_DESCENDANTS (index) = t; /* Template template parameters need this. */ DECL_TEMPLATE_PARMS (decl) = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); } return TEMPLATE_PARM_DESCENDANTS (index); } /* Process information from new template parameter NEXT and append it to the LIST being built. */ tree process_template_parm (tree list, tree next) { tree parm; tree decl = 0; tree defval; int is_type, idx; parm = next; my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); defval = TREE_PURPOSE (parm); parm = TREE_VALUE (parm); is_type = TREE_PURPOSE (parm) == class_type_node; if (list) { tree p = TREE_VALUE (tree_last (list)); if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); else idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); ++idx; } else idx = 0; if (!is_type) { my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); /* is a const-param */ parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), PARM, 0, NULL); SET_DECL_TEMPLATE_PARM_P (parm); /* [temp.param] The top-level cv-qualifiers on the template-parameter are ignored when determining its type. */ TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); /* A template parameter is not modifiable. */ TREE_READONLY (parm) = TREE_CONSTANT (parm) = 1; if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) TREE_TYPE (parm) = void_type_node; decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1; DECL_INITIAL (parm) = DECL_INITIAL (decl) = build_template_parm_index (idx, processing_template_decl, processing_template_decl, decl, TREE_TYPE (parm)); } else { tree t; parm = TREE_VALUE (parm); if (parm && TREE_CODE (parm) == TEMPLATE_DECL) { t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); /* This is for distinguishing between real templates and template template parameters */ TREE_TYPE (parm) = t; TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; decl = parm; } else { t = make_aggr_type (TEMPLATE_TYPE_PARM); /* parm is either IDENTIFIER_NODE or NULL_TREE. */ decl = build_decl (TYPE_DECL, parm, t); } TYPE_NAME (t) = decl; TYPE_STUB_DECL (t) = decl; parm = decl; TEMPLATE_TYPE_PARM_INDEX (t) = build_template_parm_index (idx, processing_template_decl, processing_template_decl, decl, TREE_TYPE (parm)); } DECL_ARTIFICIAL (decl) = 1; SET_DECL_TEMPLATE_PARM_P (decl); pushdecl (decl); parm = build_tree_list (defval, parm); return chainon (list, parm); } /* The end of a template parameter list has been reached. Process the tree list into a parameter vector, converting each parameter into a more useful form. Type parameters are saved as IDENTIFIER_NODEs, and others as PARM_DECLs. */ tree end_template_parm_list (tree parms) { int nparms; tree parm, next; tree saved_parmlist = make_tree_vec (list_length (parms)); current_template_parms = tree_cons (size_int (processing_template_decl), saved_parmlist, current_template_parms); for (parm = parms, nparms = 0; parm; parm = next, nparms++) { next = TREE_CHAIN (parm); TREE_VEC_ELT (saved_parmlist, nparms) = parm; TREE_CHAIN (parm) = NULL_TREE; } --processing_template_parmlist; return saved_parmlist; } /* end_template_decl is called after a template declaration is seen. */ void end_template_decl (void) { reset_specialization (); if (! processing_template_decl) return; /* This matches the pushlevel in begin_template_parm_list. */ finish_scope (); --processing_template_decl; current_template_parms = TREE_CHAIN (current_template_parms); } /* Given a template argument vector containing the template PARMS. The innermost PARMS are given first. */ tree current_template_args (void) { tree header; tree args = NULL_TREE; int length = TMPL_PARMS_DEPTH (current_template_parms); int l = length; /* If there is only one level of template parameters, we do not create a TREE_VEC of TREE_VECs. Instead, we return a single TREE_VEC containing the arguments. */ if (length > 1) args = make_tree_vec (length); for (header = current_template_parms; header; header = TREE_CHAIN (header)) { tree a = copy_node (TREE_VALUE (header)); int i; TREE_TYPE (a) = NULL_TREE; for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) { tree t = TREE_VEC_ELT (a, i); /* T will be a list if we are called from within a begin/end_template_parm_list pair, but a vector directly if within a begin/end_member_template_processing pair. */ if (TREE_CODE (t) == TREE_LIST) { t = TREE_VALUE (t); if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL) t = TREE_TYPE (t); else t = DECL_INITIAL (t); TREE_VEC_ELT (a, i) = t; } } if (length > 1) TREE_VEC_ELT (args, --l) = a; else args = a; } return args; } /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated template PARMS. Used by push_template_decl below. */ static tree build_template_decl (tree decl, tree parms) { tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); DECL_TEMPLATE_PARMS (tmpl) = parms; DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); if (DECL_LANG_SPECIFIC (decl)) { DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); if (DECL_OVERLOADED_OPERATOR_P (decl)) SET_OVERLOADED_OPERATOR_CODE (tmpl, DECL_OVERLOADED_OPERATOR_P (decl)); } return tmpl; } struct template_parm_data { /* The level of the template parameters we are currently processing. */ int level; /* The index of the specialization argument we are currently processing. */ int current_arg; /* An array whose size is the number of template parameters. The elements are nonzero if the parameter has been used in any one of the arguments processed so far. */ int* parms; /* An array whose size is the number of template arguments. The elements are nonzero if the argument makes use of template parameters of this level. */ int* arg_uses_template_parms; }; /* Subroutine of push_template_decl used to see if each template parameter in a partial specialization is used in the explicit argument list. If T is of the LEVEL given in DATA (which is treated as a template_parm_data*), then DATA->PARMS is marked appropriately. */ static int mark_template_parm (tree t, void* data) { int level; int idx; struct template_parm_data* tpd = (struct template_parm_data*) data; if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) { level = TEMPLATE_PARM_LEVEL (t); idx = TEMPLATE_PARM_IDX (t); } else { level = TEMPLATE_TYPE_LEVEL (t); idx = TEMPLATE_TYPE_IDX (t); } if (level == tpd->level) { tpd->parms[idx] = 1; tpd->arg_uses_template_parms[tpd->current_arg] = 1; } /* Return zero so that for_each_template_parm will continue the traversal of the tree; we want to mark *every* template parm. */ return 0; } /* Process the partial specialization DECL. */ static tree process_partial_specialization (tree decl) { tree type = TREE_TYPE (decl); tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); tree specargs = CLASSTYPE_TI_ARGS (type); tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); int nargs = TREE_VEC_LENGTH (inner_args); int ntparms = TREE_VEC_LENGTH (inner_parms); int i; int did_error_intro = 0; struct template_parm_data tpd; struct template_parm_data tpd2; /* We check that each of the template parameters given in the partial specialization is used in the argument list to the specialization. For example: template struct S; template struct S; The second declaration is OK because `T*' uses the template parameter T, whereas template struct S; is no good. Even trickier is: template struct S1 { template struct S2; template struct S2; }; The S2 declaration is actually invalid; it is a full-specialization. Of course, template struct S2; or some such would have been OK. */ tpd.level = TMPL_PARMS_DEPTH (current_template_parms); tpd.parms = alloca (sizeof (int) * ntparms); memset (tpd.parms, 0, sizeof (int) * ntparms); tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); for (i = 0; i < nargs; ++i) { tpd.current_arg = i; for_each_template_parm (TREE_VEC_ELT (inner_args, i), &mark_template_parm, &tpd, NULL); } for (i = 0; i < ntparms; ++i) if (tpd.parms[i] == 0) { /* One of the template parms was not used in the specialization. */ if (!did_error_intro) { error ("template parameters not used in partial specialization:"); did_error_intro = 1; } error (" `%D'", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); } /* [temp.class.spec] The argument list of the specialization shall not be identical to the implicit argument list of the primary template. */ if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (maintmpl))))) error ("partial specialization `%T' does not specialize any template arguments", type); /* [temp.class.spec] A partially specialized non-type argument expression shall not involve template parameters of the partial specialization except when the argument expression is a simple identifier. The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization. */ my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); tpd2.parms = 0; for (i = 0; i < nargs; ++i) { tree arg = TREE_VEC_ELT (inner_args, i); if (/* These first two lines are the `non-type' bit. */ !TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_DECL /* This next line is the `argument expression is not just a simple identifier' condition and also the `specialized non-type argument' bit. */ && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) { if (tpd.arg_uses_template_parms[i]) error ("template argument `%E' involves template parameter(s)", arg); else { /* Look at the corresponding template parameter, marking which template parameters its type depends upon. */ tree type = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i))); if (!tpd2.parms) { /* We haven't yet initialized TPD2. Do so now. */ tpd2.arg_uses_template_parms = alloca (sizeof (int) * nargs); /* The number of parameters here is the number in the main template, which, as checked in the assertion above, is NARGS. */ tpd2.parms = alloca (sizeof (int) * nargs); tpd2.level = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); } /* Mark the template parameters. But this time, we're looking for the template parameters of the main template, not in the specialization. */ tpd2.current_arg = i; tpd2.arg_uses_template_parms[i] = 0; memset (tpd2.parms, 0, sizeof (int) * nargs); for_each_template_parm (type, &mark_template_parm, &tpd2, NULL); if (tpd2.arg_uses_template_parms [i]) { /* The type depended on some template parameters. If they are fully specialized in the specialization, that's OK. */ int j; for (j = 0; j < nargs; ++j) if (tpd2.parms[j] != 0 && tpd.arg_uses_template_parms [j]) { error ("type `%T' of template argument `%E' depends on template parameter(s)", type, arg); break; } } } } } if (retrieve_specialization (maintmpl, specargs)) /* We've already got this specialization. */ return decl; DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = tree_cons (inner_args, inner_parms, DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; return decl; } /* Check that a template declaration's use of default arguments is not invalid. Here, PARMS are the template parameters. IS_PRIMARY is nonzero if DECL is the thing declared by a primary template. IS_PARTIAL is nonzero if DECL is a partial specialization. */ static void check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial) { const char *msg; int last_level_to_check; tree parm_level; /* [temp.param] A default template-argument shall not be specified in a function template declaration or a function template definition, nor in the template-parameter-list of the definition of a member of a class template. */ if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) /* You can't have a function template declaration in a local scope, nor you can you define a member of a class template in a local scope. */ return; if (current_class_type && !TYPE_BEING_DEFINED (current_class_type) && DECL_LANG_SPECIFIC (decl) /* If this is either a friend defined in the scope of the class or a member function. */ && (DECL_FUNCTION_MEMBER_P (decl) ? same_type_p (DECL_CONTEXT (decl), current_class_type) : DECL_FRIEND_CONTEXT (decl) ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) : false) /* And, if it was a member function, it really was defined in the scope of the class. */ && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl))) /* We already checked these parameters when the template was declared, so there's no need to do it again now. This function was defined in class scope, but we're processing it's body now that the class is complete. */ return; /* [temp.param] If a template-parameter has a default template-argument, all subsequent template-parameters shall have a default template-argument supplied. */ for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) { tree inner_parms = TREE_VALUE (parm_level); int ntparms = TREE_VEC_LENGTH (inner_parms); int seen_def_arg_p = 0; int i; for (i = 0; i < ntparms; ++i) { tree parm = TREE_VEC_ELT (inner_parms, i); if (TREE_PURPOSE (parm)) seen_def_arg_p = 1; else if (seen_def_arg_p) { error ("no default argument for `%D'", TREE_VALUE (parm)); /* For better subsequent error-recovery, we indicate that there should have been a default argument. */ TREE_PURPOSE (parm) = error_mark_node; } } } if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) /* For an ordinary class template, default template arguments are allowed at the innermost level, e.g.: template struct S {}; but, in a partial specialization, they're not allowed even there, as we have in [temp.class.spec]: The template parameter list of a specialization shall not contain default template argument values. So, for a partial specialization, or for a function template, we look at all of them. */ ; else /* But, for a primary class template that is not a partial specialization we look at all template parameters except the innermost ones. */ parms = TREE_CHAIN (parms); /* Figure out what error message to issue. */ if (TREE_CODE (decl) == FUNCTION_DECL) msg = "default template arguments may not be used in function templates"; else if (is_partial) msg = "default template arguments may not be used in partial specializations"; else msg = "default argument for template parameter for class enclosing `%D'"; if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) /* If we're inside a class definition, there's no need to examine the parameters to the class itself. On the one hand, they will be checked when the class is defined, and, on the other, default arguments are valid in things like: template struct S { template void f(U); }; Here the default argument for `S' has no bearing on the declaration of `f'. */ last_level_to_check = template_class_depth (current_class_type) + 1; else /* Check everything. */ last_level_to_check = 0; for (parm_level = parms; parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; parm_level = TREE_CHAIN (parm_level)) { tree inner_parms = TREE_VALUE (parm_level); int i; int ntparms; ntparms = TREE_VEC_LENGTH (inner_parms); for (i = 0; i < ntparms; ++i) if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) { if (msg) { error (msg, decl); msg = 0; } /* Clear out the default argument so that we are not confused later. */ TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; } /* At this point, if we're still interested in issuing messages, they must apply to classes surrounding the object declared. */ if (msg) msg = "default argument for template parameter for class enclosing `%D'"; } } /* Worker for push_template_decl_real, called via for_each_template_parm. DATA is really an int, indicating the level of the parameters we are interested in. If T is a template parameter of that level, return nonzero. */ static int template_parm_this_level_p (tree t, void* data) { int this_level = *(int *)data; int level; if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) level = TEMPLATE_PARM_LEVEL (t); else level = TEMPLATE_TYPE_LEVEL (t); return level == this_level; } /* Creates a TEMPLATE_DECL for the indicated DECL using the template parameters given by current_template_args, or reuses a previously existing one, if appropriate. Returns the DECL, or an equivalent one, if it is replaced via a call to duplicate_decls. If IS_FRIEND is nonzero, DECL is a friend declaration. */ tree push_template_decl_real (tree decl, int is_friend) { tree tmpl; tree args; tree info; tree ctx; int primary; int is_partial; int new_template_p = 0; /* See if this is a partial specialization. */ is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); if (is_friend) /* For a friend, we want the context of the friend function, not the type of which it is a friend. */ ctx = DECL_CONTEXT (decl); else if (CP_DECL_CONTEXT (decl) && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) /* In the case of a virtual function, we want the class in which it is defined. */ ctx = CP_DECL_CONTEXT (decl); else /* Otherwise, if we're currently defining some class, the DECL is assumed to be a member of the class. */ ctx = current_scope (); if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) ctx = NULL_TREE; if (!DECL_CONTEXT (decl)) DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); /* See if this is a primary template. */ primary = template_parm_scope_p (); if (primary) { if (current_lang_name == lang_name_c) error ("template with C linkage"); else if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl))) error ("template class without a name"); else if (TREE_CODE (decl) == FUNCTION_DECL) { if (DECL_DESTRUCTOR_P (decl)) { /* [temp.mem] A destructor shall not be a member template. */ error ("destructor `%D' declared as member template", decl); return error_mark_node; } if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) == void_list_node))) { /* [basic.stc.dynamic.allocation] An allocation function can be a function template. ... Template allocation functions shall have two or more parameters. */ error ("invalid template declaration of `%D'", decl); return decl; } } else if (DECL_IMPLICIT_TYPEDEF_P (decl) && CLASS_TYPE_P (TREE_TYPE (decl))) /* OK */; else { error ("template declaration of `%#D'", decl); return error_mark_node; } } /* Check to see that the rules regarding the use of default arguments are not being violated. */ check_default_tmpl_args (decl, current_template_parms, primary, is_partial); if (is_partial) return process_partial_specialization (decl); args = current_template_args (); if (!ctx || TREE_CODE (ctx) == FUNCTION_DECL || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) || (is_friend && !DECL_TEMPLATE_INFO (decl))) { if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)) tmpl = DECL_TI_TEMPLATE (decl); /* If DECL is a TYPE_DECL for a class-template, then there won't be DECL_LANG_SPECIFIC. The information equivalent to DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ else if (DECL_IMPLICIT_TYPEDEF_P (decl) && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) { /* Since a template declaration already existed for this class-type, we must be redeclaring it here. Make sure that the redeclaration is valid. */ redeclare_class_template (TREE_TYPE (decl), current_template_parms); /* We don't need to create a new TEMPLATE_DECL; just use the one we already had. */ tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); } else { tmpl = build_template_decl (decl, current_template_parms); new_template_p = 1; if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_SPECIALIZATION (decl)) { /* A specialization of a member template of a template class. */ SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); DECL_TEMPLATE_INFO (decl) = NULL_TREE; } } } else { tree a, t, current, parms; int i; if (TREE_CODE (decl) == TYPE_DECL) { if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); else { error ("`%D' does not declare a template type", decl); return decl; } } else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) { error ("template definition of non-template `%#D'", decl); return decl; } else tmpl = DECL_TI_TEMPLATE (decl); if (DECL_FUNCTION_TEMPLATE_P (tmpl) && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) && DECL_TEMPLATE_SPECIALIZATION (decl) && is_member_template (tmpl)) { tree new_tmpl; /* The declaration is a specialization of a member template, declared outside the class. Therefore, the innermost template arguments will be NULL, so we replace them with the arguments determined by the earlier call to check_explicit_specialization. */ args = DECL_TI_ARGS (decl); new_tmpl = build_template_decl (decl, current_template_parms); DECL_TEMPLATE_RESULT (new_tmpl) = decl; TREE_TYPE (new_tmpl) = TREE_TYPE (decl); DECL_TI_TEMPLATE (decl) = new_tmpl; SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); DECL_TEMPLATE_INFO (new_tmpl) = tree_cons (tmpl, args, NULL_TREE); register_specialization (new_tmpl, most_general_template (tmpl), args); return decl; } /* Make sure the template headers we got make sense. */ parms = DECL_TEMPLATE_PARMS (tmpl); i = TMPL_PARMS_DEPTH (parms); if (TMPL_ARGS_DEPTH (args) != i) { error ("expected %d levels of template parms for `%#D', got %d", i, decl, TMPL_ARGS_DEPTH (args)); } else for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) { a = TMPL_ARGS_LEVEL (args, i); t = INNERMOST_TEMPLATE_PARMS (parms); if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) { if (current == decl) error ("got %d template parameters for `%#D'", TREE_VEC_LENGTH (a), decl); else error ("got %d template parameters for `%#T'", TREE_VEC_LENGTH (a), current); error (" but %d required", TREE_VEC_LENGTH (t)); return error_mark_node; } /* Perhaps we should also check that the parms are used in the appropriate qualifying scopes in the declarator? */ if (current == decl) current = ctx; else current = TYPE_CONTEXT (current); } } DECL_TEMPLATE_RESULT (tmpl) = decl; TREE_TYPE (tmpl) = TREE_TYPE (decl); /* Push template declarations for global functions and types. Note that we do not try to push a global template friend declared in a template class; such a thing may well depend on the template parameters of the class. */ if (new_template_p && !ctx && !(is_friend && template_class_depth (current_class_type) > 0)) tmpl = pushdecl_namespace_level (tmpl); if (primary) { DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; if (DECL_CONV_FN_P (tmpl)) { int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); /* It is a conversion operator. See if the type converted to depends on innermost template operands. */ if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), depth)) DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; } } /* The DECL_TI_ARGS of DECL contains full set of arguments refering back to its most general template. If TMPL is a specialization, ARGS may only have the innermost set of arguments. Add the missing argument levels if necessary. */ if (DECL_TEMPLATE_INFO (tmpl)) args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); info = tree_cons (tmpl, args, NULL_TREE); if (DECL_IMPLICIT_TYPEDEF_P (decl)) { SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE /* Don't change the name if we've already set it up. */ && !IDENTIFIER_TEMPLATE (DECL_NAME (decl))) DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); } else if (DECL_LANG_SPECIFIC (decl)) DECL_TEMPLATE_INFO (decl) = info; return DECL_TEMPLATE_RESULT (tmpl); } tree push_template_decl (tree decl) { return push_template_decl_real (decl, 0); } /* Called when a class template TYPE is redeclared with the indicated template PARMS, e.g.: template struct S; template struct S {}; */ void redeclare_class_template (tree type, tree parms) { tree tmpl; tree tmpl_parms; int i; if (!TYPE_TEMPLATE_INFO (type)) { error ("`%T' is not a template type", type); return; } tmpl = TYPE_TI_TEMPLATE (type); if (!PRIMARY_TEMPLATE_P (tmpl)) /* The type is nested in some template class. Nothing to worry about here; there are no new template parameters for the nested type. */ return; parms = INNERMOST_TEMPLATE_PARMS (parms); tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) { cp_error_at ("previous declaration `%D'", tmpl); error ("used %d template parameter(s) instead of %d", TREE_VEC_LENGTH (tmpl_parms), TREE_VEC_LENGTH (parms)); return; } for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) { tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) { cp_error_at ("template parameter `%#D'", tmpl_parm); error ("redeclared here as `%#D'", parm); return; } if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) { /* We have in [temp.param]: A template-parameter may not be given default arguments by two different declarations in the same scope. */ error ("redefinition of default argument for `%#D'", parm); error ("%J original definition appeared here", tmpl_parm); return; } if (parm_default != NULL_TREE) /* Update the previous template parameters (which are the ones that will really count) with the new default value. */ TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; else if (tmpl_default != NULL_TREE) /* Update the new parameters, too; they'll be used as the parameters for any members. */ TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; } } /* Simplify EXPR if it is a non-dependent expression. Returns the (possibly simplified) expression. */ tree fold_non_dependent_expr (tree expr) { /* If we're in a template, but EXPR isn't value dependent, simplify it. We're supposed to treat: template void f(T[1 + 1]); template void f(T[2]); as two declarations of the same function, for example. */ if (processing_template_decl && !type_dependent_expression_p (expr) && !value_dependent_expression_p (expr)) { HOST_WIDE_INT saved_processing_template_decl; saved_processing_template_decl = processing_template_decl; processing_template_decl = 0; expr = tsubst_copy_and_build (expr, /*args=*/NULL_TREE, tf_error, /*in_decl=*/NULL_TREE, /*function_p=*/false); processing_template_decl = saved_processing_template_decl; } return expr; } /* Attempt to convert the non-type template parameter EXPR to the indicated TYPE. If the conversion is successful, return the converted value. If the conversion is unsuccessful, return NULL_TREE if we issued an error message, or error_mark_node if we did not. We issue error messages for out-and-out bad template parameters, but not simply because the conversion failed, since we might be just trying to do argument deduction. Both TYPE and EXPR must be non-dependent. */ static tree convert_nontype_argument (tree type, tree expr) { tree expr_type; /* If we are in a template, EXPR may be non-dependent, but still have a syntactic, rather than semantic, form. For example, EXPR might be a SCOPE_REF, rather than the VAR_DECL to which the SCOPE_REF refers. Preserving the qualifying scope is necessary so that access checking can be performed when the template is instantiated -- but here we need the resolved form so that we can convert the argument. */ expr = fold_non_dependent_expr (expr); expr_type = TREE_TYPE (expr); /* A template-argument for a non-type, non-template template-parameter shall be one of: --an integral constant-expression of integral or enumeration type; or --the name of a non-type template-parameter; or --the name of an object or function with external linkage, including function templates and function template-ids but excluding non-static class members, expressed as id-expression; or --the address of an object or function with external linkage, including function templates and function template-ids but excluding non-static class members, expressed as & id-expression where the & is optional if the name refers to a function or array; or --a pointer to member expressed as described in _expr.unary.op_. */ /* An integral constant-expression can include const variables or . enumerators. Simplify things by folding them to their values, unless we're about to bind the declaration to a reference parameter. */ if (INTEGRAL_TYPE_P (expr_type) && TREE_CODE (type) != REFERENCE_TYPE) while (true) { tree const_expr = decl_constant_value (expr); /* In a template, the initializer for a VAR_DECL may not be marked as TREE_CONSTANT, in which case decl_constant_value will not return the initializer. Handle that special case here. */ if (expr == const_expr && DECL_INTEGRAL_CONSTANT_VAR_P (expr) /* DECL_INITIAL can be NULL if we are processing a variable initialized to an expression involving itself. We know it is initialized to a constant -- but not what constant, yet. */ && DECL_INITIAL (expr)) const_expr = DECL_INITIAL (expr); if (expr == const_expr) break; expr = fold_non_dependent_expr (const_expr); } if (is_overloaded_fn (expr)) /* OK for now. We'll check that it has external linkage later. Check this first since if expr_type is the unknown_type_node we would otherwise complain below. */ ; else if (TYPE_PTR_TO_MEMBER_P (expr_type)) { if (TREE_CODE (expr) != PTRMEM_CST) goto bad_argument; } else if (TYPE_PTR_P (expr_type) || TREE_CODE (expr_type) == ARRAY_TYPE || TREE_CODE (type) == REFERENCE_TYPE /* If expr is the address of an overloaded function, we will get the unknown_type_node at this point. */ || expr_type == unknown_type_node) { tree referent; tree e = expr; STRIP_NOPS (e); if (TREE_CODE (expr_type) == ARRAY_TYPE || (TREE_CODE (type) == REFERENCE_TYPE && TREE_CODE (e) != ADDR_EXPR)) referent = e; else { if (TREE_CODE (e) != ADDR_EXPR) { bad_argument: error ("`%E' is not a valid template argument", expr); if (TYPE_PTR_P (expr_type)) { if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) error ("it must be the address of a function with external linkage"); else error ("it must be the address of an object with external linkage"); } else if (TYPE_PTR_TO_MEMBER_P (expr_type)) error ("it must be a pointer-to-member of the form `&X::Y'"); return NULL_TREE; } referent = TREE_OPERAND (e, 0); STRIP_NOPS (referent); } if (TREE_CODE (referent) == STRING_CST) { error ("string literal %E is not a valid template argument because it is the address of an object with static linkage", referent); return NULL_TREE; } if (TREE_CODE (referent) == SCOPE_REF) referent = TREE_OPERAND (referent, 1); if (is_overloaded_fn (referent)) /* We'll check that it has external linkage later. */ ; else if (TREE_CODE (referent) != VAR_DECL) goto bad_argument; else if (!DECL_EXTERNAL_LINKAGE_P (referent)) { error ("address of non-extern `%E' cannot be used as template argument", referent); return error_mark_node; } } else if (INTEGRAL_TYPE_P (expr_type) || TYPE_PTR_TO_MEMBER_P (expr_type)) { if (! TREE_CONSTANT (expr)) { non_constant: error ("non-constant `%E' cannot be used as template argument", expr); return NULL_TREE; } } else { if (TYPE_P (expr)) error ("type '%T' cannot be used as a value for a non-type " "template-parameter", expr); else if (DECL_P (expr)) error ("invalid use of '%D' as a non-type template-argument", expr); else error ("invalid use of '%E' as a non-type template-argument", expr); return NULL_TREE; } switch (TREE_CODE (type)) { case INTEGER_TYPE: case BOOLEAN_TYPE: case ENUMERAL_TYPE: /* For a non-type template-parameter of integral or enumeration type, integral promotions (_conv.prom_) and integral conversions (_conv.integral_) are applied. */ if (!INTEGRAL_TYPE_P (expr_type)) return error_mark_node; /* It's safe to call digest_init in this case; we know we're just converting one integral constant expression to another. */ expr = digest_init (type, expr, (tree*) 0); if (TREE_CODE (expr) != INTEGER_CST) /* Curiously, some TREE_CONSTANT integral expressions do not simplify to integer constants. For example, `3 % 0', remains a TRUNC_MOD_EXPR. */ goto non_constant; return expr; case OFFSET_TYPE: { tree e; /* For a non-type template-parameter of type pointer to data member, qualification conversions (_conv.qual_) are applied. */ e = perform_qualification_conversions (type, expr); if (TREE_CODE (e) == NOP_EXPR) /* The call to perform_qualification_conversions will insert a NOP_EXPR over EXPR to do express conversion, if necessary. But, that will confuse us if we use this (converted) template parameter to instantiate another template; then the thing will not look like a valid template argument. So, just make a new constant, of the appropriate type. */ e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); return e; } case POINTER_TYPE: { tree type_pointed_to = TREE_TYPE (type); if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) { /* For a non-type template-parameter of type pointer to function, only the function-to-pointer conversion (_conv.func_) is applied. If the template-argument represents a set of overloaded functions (or a pointer to such), the matching function is selected from the set (_over.over_). */ tree fns; tree fn; if (TREE_CODE (expr) == ADDR_EXPR) fns = TREE_OPERAND (expr, 0); else fns = expr; fn = instantiate_type (type_pointed_to, fns, tf_none); if (fn == error_mark_node) return error_mark_node; if (!DECL_EXTERNAL_LINKAGE_P (fn)) { if (really_overloaded_fn (fns)) return error_mark_node; else goto bad_argument; } expr = build_unary_op (ADDR_EXPR, fn, 0); my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), 0); return expr; } else { /* For a non-type template-parameter of type pointer to object, qualification conversions (_conv.qual_) and the array-to-pointer conversion (_conv.array_) are applied. [Note: In particular, neither the null pointer conversion (_conv.ptr_) nor the derived-to-base conversion (_conv.ptr_) are applied. Although 0 is a valid template-argument for a non-type template-parameter of integral type, it is not a valid template-argument for a non-type template-parameter of pointer type.] The call to decay_conversion performs the array-to-pointer conversion, if appropriate. */ expr = decay_conversion (expr); if (expr == error_mark_node) return error_mark_node; else return perform_qualification_conversions (type, expr); } } break; case REFERENCE_TYPE: { tree type_referred_to = TREE_TYPE (type); /* If this expression already has reference type, get the underlying object. */ if (TREE_CODE (expr_type) == REFERENCE_TYPE) { if (TREE_CODE (expr) == NOP_EXPR && TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR) STRIP_NOPS (expr); my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604); expr = TREE_OPERAND (expr, 0); expr_type = TREE_TYPE (expr); } if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) { /* For a non-type template-parameter of type reference to function, no conversions apply. If the template-argument represents a set of overloaded functions, the matching function is selected from the set (_over.over_). */ tree fn; fn = instantiate_type (type_referred_to, expr, tf_none); if (fn == error_mark_node) return error_mark_node; if (!DECL_EXTERNAL_LINKAGE_P (fn)) { if (really_overloaded_fn (expr)) /* Don't issue an error here; we might get a different function if the overloading had worked out differently. */ return error_mark_node; else goto bad_argument; } my_friendly_assert (same_type_p (type_referred_to, TREE_TYPE (fn)), 0); expr = fn; } else { /* For a non-type template-parameter of type reference to object, no conversions apply. The type referred to by the reference may be more cv-qualified than the (otherwise identical) type of the template-argument. The template-parameter is bound directly to the template-argument, which must be an lvalue. */ if (!same_type_p (TYPE_MAIN_VARIANT (expr_type), TYPE_MAIN_VARIANT (type_referred_to)) || !at_least_as_qualified_p (type_referred_to, expr_type) || !real_lvalue_p (expr)) return error_mark_node; } cxx_mark_addressable (expr); return build_nop (type, build_address (expr)); } break; case RECORD_TYPE: { my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112); /* For a non-type template-parameter of type pointer to member function, no conversions apply. If the template-argument represents a set of overloaded member functions, the matching member function is selected from the set (_over.over_). */ if (!TYPE_PTRMEMFUNC_P (expr_type) && expr_type != unknown_type_node) return error_mark_node; if (TREE_CODE (expr) == PTRMEM_CST) { /* A ptr-to-member constant. */ if (!same_type_p (type, expr_type)) return error_mark_node; else return expr; } if (TREE_CODE (expr) != ADDR_EXPR) return error_mark_node; expr = instantiate_type (type, expr, tf_none); if (expr == error_mark_node) return error_mark_node; if (!same_type_p (type, TREE_TYPE (expr))) return error_mark_node; return expr; } break; default: /* All non-type parameters must have one of these types. */ abort (); break; } return error_mark_node; } /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for template template parameters. Both PARM_PARMS and ARG_PARMS are vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. ARG_PARMS may contain more parameters than PARM_PARMS. If this is the case, then extra parameters must have default arguments. Consider the example: template class vector; template